微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > ARM嵌入式LINUX设备驱动设计入门学习

ARM嵌入式LINUX设备驱动设计入门学习

时间:11-19 来源:互联网 点击:
经过一段时间的学习之后,也开发了一些小型的驱动,正如我之前一篇中写到得,现在我就来写一下在ARM嵌入式LINUX下如何设计驱动的框架。

在这里我用的板子是micro2440板子,板子上的linux版本是2.6.13。因为我在前一篇介绍了驱动编程的两种框架设计,所以现在我就来分别写一下这两种框架设计的程序。

开发平台:RED HAT LINUX 9(Linux 2.4.18)

开发板:micro2440(友善之臂)(Linux 2.6.13)

交叉编译工具:arm-linux-gcc-3.4.1

---------------------------------------------------------------------

---------------------------------------------------------------------

---------------------------------------------------------------------

2.4内核驱动框架:

static int __init leds_init(void)
{
int result;
int i;

result = register_chrdev(LED_MAJOR, DEVICE_NAME, &leds_fops);
if(result < 0){
printk(DEVICE_NAME "cant register major number/n");
return result;
}

//static devfs_handle_t devfs_handle;
//devfs_handle = devfs_register(NULL, DEVICE_NAME, DEVFS_FL_DEFAULT, LED_MAJOR, &leds_fops, NULL);

/*

*之所以不用devfs_register,而用devfs_mk_cdev,原因是因为在linux2.6内核里没有devfs_register函数,而改用*devfs_mk_cdev

*/
devfs_mk_cdev(MKDEV(LED_MAJOR, 0), S_IFCHR | S_IRUSR | S_IWUSR | S_IRGRP, DEVICE_NAME);

for(i = 0; i < 4; i++){
s3c2410_gpio_cfgpin(led_table[i], led_cfg_table[i]);
s3c2410_gpio_setpin(led_table[i], 1);
}

printk(DEVICE_NAME "initialized/n");
return 0;
}

static void __exit leds_exit(void)
{
devfs_remove(DEVICE_NAME);
unregister_chrdev(LED_MAJOR, DEVICE_NAME);
}

---------------------------------------------------------------------

---------------------------------------------------------------------

---------------------------------------------------------------------

2.6内核驱动框架:

static int __init leds_init(void)
{
int result;
int i;
dev_t dev = 0;

dev = MKDEV(LED_MAJOR, 0);
result = register_chrdev_region(dev, 1, DEVICE_NAME);
if (result < 0)
{
printk(KERN_WARNING "DEMO: cant get major %d/n", LED_MAJOR);
return result;
}

LED_devices = kmalloc(sizeof(struct DEMO_dev), GFP_KERNEL);
if (!LED_devices)
{
result = -ENOMEM;
goto fail;
}
memset(LED_devices, 0, sizeof(struct DEMO_dev));

cdev_init(&LED_devices->cdev, &leds_fops);
LED_devices->cdev.owner = THIS_MODULE;
LED_devices->cdev.ops = &leds_fops;
result = cdev_add (&LED_devices->cdev, dev, 1);
if(result)
{
printk(KERN_NOTICE "Error %d adding DEMO/n", result);
goto fail;
}
devfs_mk_cdev(MKDEV(LED_MAJOR, 0), S_IFCHR | S_IRUSR | S_IWUSR | S_IRGRP, DEVICE_NAME);

for(i = 0; i < 4; i++){
s3c2410_gpio_cfgpin(led_table[i], led_cfg_table[i]);
s3c2410_gpio_setpin(led_table[i], 1);
}

printk(DEVICE_NAME "initialized/n");
return 0;
fail:
leds_exit();
return result;

}

static void __exit leds_exit(void)
{
dev_t devno = MKDEV(LED_MAJOR, 0);

if (LED_devices)
{
devfs_remove(DEVICE_NAME);
cdev_del(&LED_devices->cdev);
kfree(LED_devices);
}

unregister_chrdev_region(devno,1);
}

---------------------------------------------------------------------

---------------------------------------------------------------------

---------------------------------------------------------------------

在这里我事先都写好了文件才做结构体,和操作函数ioctl,下面我就来介绍介绍

static int leds_ioctl(
struct inode *inode,
struct file *file,
unsigned int cmd,
unsigned long arg)
{
switch(cmd){
case 0:
case 1:
if(arg > 4){
return -EINVAL;
}
s3c2410_gpio_setpin(led_table[arg], !cmd);
return 0;
default:
return -EINVAL;
}
}

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

网站地图

Top