在这里我用的板子是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 "can't 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: can't 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;
}
}
上一篇:基于ARM的W5100底层驱动设计
下一篇:基于STM32的USB枚举过程学习笔记(一)
推荐阅读最新更新时间:2024-03-16 14:54
设计资源 培训 开发板 精华推荐
- 首都医科大学王长明:针对癫痫的数字疗法已进入使用阶段
- 非常见问题解答第223期:如何在没有软启动方程的情况下测量和确定软启动时序?
- 兆易创新GD25/55全系列车规级SPI NOR Flash荣获ISO 26262 ASIL D功能安全认证证书
- 新型IsoVu™ 隔离电流探头:为电流测量带来全新维度
- 英飞凌推出简化电机控制开发的ModusToolbox™电机套件
- 意法半导体IO-Link执行器电路板为工业监控和设备厂商带来一站式参考设计
- Melexis采用无磁芯技术缩小电流感测装置尺寸
- 千丘智能侍淳博:用数字疗法,点亮“孤独症”儿童的光
- 数药智能冯尚:ADHD数字疗法正为儿童“多动症”提供更有效便捷服务
- Vicor高性能电源模块助力低空航空电子设备和 EVTOL的发展