微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > arm中驱动模块加载并由应用程序调用

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

时间:11-09 来源:互联网 点击:
开发板: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

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top