您的位置 首页 编程

arm中驱动模块加载并由应用程序调用

开发板:s3c2440驱动模块程序如下:#includelinux/types.h>#includelinux/fs.h>#includelinux/mm.h>#includelin

开发板:s3c2440

驱动模块程序如下:

#include #include #include #include #include #include #include #include
#include #include #include #include #include
#include
#include

#include “my_ioctl.h”

unsigned int test_major= 253;//主设备号
unsigned int test_minor= 0;//次设备号
struct cdev cdevc;
MODULE_LICENSE(“Dual BSD/GPL”);//奉告内核,该模块带有一个自在的许可证

static int read_test(struct file *file, const char *buf, int count, loff_t *f_pos)
{
if (copy_to_user(buf, S3C2410_GPGDAT, sizeof(unsigned int)))
{
printk(“error!/n”);
return -1;
}
return sizeof(unsigned int);
}

static int write_test(struct file *file, const char *buf, int count, loff_t *f_pos)
{
__raw_writel (*buf, S3C2410_GPGDAT);//把buf中的数据赋值给GPGDAT,不行直接赋值,一定要调用此函数
printk (“write_test/n”);
return 0;
}

int write_CFG(GPIO_Data_S *arg)
{
__raw_writel (0x0001<<2*arg->bit, S3C2410_GPACON+arg->port*0x10);//找到寄存器的某个引脚,并装备成输入或输出
__raw_writel (arg->valueport*0x10);//
return 0;
}

static int ioctl_test (struct inode *inode, struct file *file , unsigned int cmd, unsigned long arg)
{
GPIO_Data_S *data;
data= (GPIO_Data_S *)arg;

switch(cmd)
{
// case GPIO_IO_SET_CFG: set_CFG(data); break;
case GPIO_IO_GET_CFG: get_CFG(data); break;
case GPIO_IO_WRITE: write_CFG(data); break;
// case GPIO_IO_READ: read_CFG(data); break;
default: printk(“The command is errot!/n”);
}
return 0;
}

static int open_test(struct inode *inode, struct file *file)
{
// __raw_writel (0x1400, S3C2410_GPGCON);//”S3C2410_GPGCON”在asm/arch/regs-gpio.h库函数中得到宏界说,可查阅

printk (“open_test/n”);
return 0;
}

static int release_test(struct inode *inode, struct file *file)
{
printk (“release_test/n”);
return 0;
}
//初始化字符设备驱动的file_operations结构体,在设备编号和驱动程序之间树立链接
struct file_operations test_fops={
.owner= THIS_MODULE,
.read= read_test,
.write= write_test,
.ioctl= ioctl_test,
.open= open_test,
.release= release_test,
};

int dev_test_init_module(void)
{
int result;
dev_t dev= 0;

dev= MKDEV (test_major, test_minor);//获取设备编号
result= register_chrdev_region(dev, 1, “dev_test”);//静态方法注册设备驱动

printk (“major= %d, minor= %d/n”, test_major, test_minor);
if(result<0)
{
printk (KERN_INFO”test:cant get major nuber!/n”);
return result;
}

cdev_init(&cdevc, &test_fops);
cdevc.owner= THIS_MODULE;
cdevc.ops= &test_fops;
result= cdev_add(&cdevc, dev, 1);
if (result)
{
printk(“Error %d adding test”, result);
}

return 0;
}

void dev_test_cleanup_module(void)
{
dev_t dev= 0;
dev= MKDEV(test_major, test_minor);
cdev_del(&cdevc);
unregister_chrdev_region(dev, 1);

printk(“Module Exit!/n”);
}

module_init(dev_test_init_module);
module_exit(dev_test_cleanup_module);

应用程序如下:

#include
#include
#include
#include
#include
#include #include “my_ioctl.h”

int main(void)
{
int dev_test;
int in_port;
int in_bit;
int in_value;
GPIO_Data_S data;

printf(“Input the number: “);
scanf(“%d %d %d”, &in_port, &in_bit, &in_value);
printf(“%d, %d, %d/n”,in_port, in_bit, in_value);

data.port= in_port;
data.bit= in_bit;
data.value= in_value;

dev_test= open(“/dev/dev_test1”, O_RDWR);
if (dev_test== -1)
{
printf(“Cant open file…/n”);
exit(0);
}

while(1)
{
sleep(1);
ioctl(dev_test , GPIO_IO_WRITE, &data);
sleep(1);
data.value= ~(data.value);
ioctl(dev_test , GPIO_IO_WRITE, &data);
}

close (dev_test);
}

my_ioctl.h库函数如下:

#ifndef __IOCTL_C_H__
#define __IOCTL_C_H__

typedef struct GPIO_Data_t
{
unsigned int port;
unsigned int bit;
unsigned int value;
} GPIO_Data_S;

#define GPIO_IOC_MAGIC 12 //Documentation/ioctl-number.txt

#define GPIO_IO_SET_CFG _IOW(GPIO_IOC_MAGIC,0,sizeof(GPIO_Data_S))
#define GPIO_IO_GET_CFG _IOWR(GPIO_IOC_MAGIC,1,sizeof(GPIO_Data_S))
#define GPIO_IO_WRITE _IOW(GPIO_IOC_MAGIC,2,sizeof(GPIO_Data_S))
#define GPIO_IO_READ _IOWR(GPIO_IOC_MAGIC,3,sizeof(GPIO_Data_S))

#endif

Makefile文件如下:

# To build modules outside of the kernel tree, we run “make”
# in the kernel source tree; the Makefile these then includes this
# Makefile once again.
# This conditional selects whether we are being included from the
# kernel Makefile or not.
ifeq ($(KERNELRELEASE),)

# Assume the source tree is where the running kernel was built
# You should set KERNELDIR in the environment if its elsewhere
# KERNELDIR ?= /lib/modules/$(shell uname -r)/build
KERNELDIR = /home/wangwei/utu-linux_for_s3c2440_V1.5.3
# KERNELDIR=$(KDIR)
#
# The current directory is passed to sub-makes as argument
PWD := $(shell pwd)
modules:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules

modules_install:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules_install

clean:
rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versions

.PHONY: modules modules_install clean

else
# called from kernel build system: just declare what our modules are
obj-m := dev_test1.o
endif

履行次序:

1.编译驱动模块程序和应用程序,别离生成dev_test1.ko和data_deal1,并用串口烧到开发板

2.加载模块:insmod dev_test1

3.创立设备结点,mknod /dev/dev_test1 c 253 0

4.履行应用程序:./data_deal1(或许需求修正权限)

串口烧写方法简介:

1.发动开发板,进入开发板体系,按ctrl+a并开释,再按z键

2.呈现许多选项,选s–传输文件

3.enter键选zmoden

4.按向右方向键,选中[转到],按enter键

5.打入所要传输文件在你主机上的绝对路径,按enter键

6.选中要传输文件,按enter键

声明:本文内容来自网络转载或用户投稿,文章版权归原作者和原出处所有。文中观点,不代表本站立场。若有侵权请联系本站删除(kf@86ic.com)https://www.86ic.net/fangan/biancheng/277113.html

为您推荐

联系我们

联系我们

在线咨询: QQ交谈

邮箱: kf@86ic.com

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

返回顶部