您的当前位置:首页正文

linux led驱动程序分析

2022-01-10 来源:爱go旅游网
linux led 驱动程序分析

(2010-08-16 23:20:12) 转载

标签: 分类: linux程序我的分析

linux led

驱动程序分析 it

分析版本V0.1

我先说一下驱动的加载顺序 如果我们在加载模块,使用insmod,程序就执行module_init(TX2440_led_init); 注册

如果我们要卸载模块,使用rmmod ,程序就执行module_exit(TX2440_led_exit); 卸载

在执行的时候不会在用到上面两个函数

#include //模块加载的头文件 #include

#include //文件系统

#include //用户定义模块初始函数名需引用的头文件 #include #include

//#include #include //#include #include #include #include

#define DEVICE_NAME \"TX2440-led\" //设备名称

static int LED_Major = 0; // 主设备号 如果是0系统自己分配 #define LED_OFF 0 //几个宏定义 #define LED_ON 1 #define ALL_LED_OFF 3 #define ALL_LED_ON 4

// 用来指定LED所用的GPIO引脚

static unsigned long led_table [] = {

S3C2410_GPF(0), S3C2410_GPF(1), S3C2410_GPF(2), S3C2410_GPF(3),

}; //定义引脚的寄存器数组(无符号长整形,对应于引脚的地址)

static int TX2440_led_open(struct inode *inode, struct file *file) {

// MOD_INC_USE_COUNT;

printk(\"TX2440-LED Driver Open Called!\\n\"); return 0;

}

static int TX2440_led_release(struct inode *inode, struct file *file) {

// MOD_DEC_USE_COUNT;

printk(\"TX2440-LED Driver Release Called!\\n\"); return 0; }

static int TX2440_led_ioctl( struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg) {

int i;

if (arg > 4) {

return -EINVAL; }

switch(cmd) {

case LED_ON: //set the pin 第几盏灯亮 s3c2410_gpio_setpin(led_table[arg], 0); break;

case LED_OFF: //clr the pin 第几盏灯灭 s3c2410_gpio_setpin(led_table[arg], 1); break;

case ALL_LED_ON: //set all pin 全亮 for (i = 0; i < 4; i++)

s3c2410_gpio_setpin(led_table[i], 0);

break;

case ALL_LED_OFF: //clr all pin 全灭 for (i = 0; i < 4; i++)

s3c2410_gpio_setpin(led_table[i], 1); break;

default:

return -EINVAL; } }

static struct file_operations TX2440_led_fops = {

.owner = THIS_MODULE, .open = TX2440_led_open, .release = TX2440_led_release, .ioctl = TX2440_led_ioctl, };

static struct class *led_class;

static int __init TX2440_led_init(void) //这个是驱动程序的初始化,关键字“__init ” {

printk(\"TX2440 LED DRIVER MODULE INIT\\n\");

LED_Major = register_chrdev(0, DEVICE_NAME, &TX2440_led_fops); //主设备号,设备名称,结构体地址

//返回设备号

if (LED_Major < 0) {

printk(DEVICE_NAME \" can't register major number\\n\"); return LED_Major; }

printk(\"register TX2440-LED Driver OK! Major = %d\\n\ led_class = class_create(THIS_MODULE, DEVICE_NAME); if(IS_ERR(led_class)) {

printk(\"Err: failed in TX2440-LED class. \\n\"); return -1; }

device_create(led_class, NULL, MKDEV(LED_Major, 0), NULL, DEVICE_NAME);//创建一个设备节点,在dev目录下可见 printk(DEVICE_NAME \" initialized\\n\"); return 0; }

static void __exit TX2440_led_exit(void) //注销 关键字‘__exit’ {

printk(\"TX2440 LED DRIVER MODULE EXIT\\n\"); unregister_chrdev(LED_Major, DEVICE_NAME);

device_destroy(led_class, MKDEV(LED_Major, 0)); class_destroy(led_class); }

module_init(TX2440_led_init); //声明加载模块初始化函数 module_exit(TX2440_led_exit); //声明卸载模块清除函数 MODULE_AUTHOR(\"www.txmcu.com\"); //作者信息 MODULE_DESCRIPTION(\"TX2440 LED Driver\");

MODULE_LICENSE(\"GPL\"); //许可证声明

因篇幅问题不能全部显示,请点此查看更多更全内容