阅读:993回复:2
希望早点入门
我有C的基础,但我对驱动程序一点的看不懂。我从网上荡了一个有关LED外部中断驱动程序,希望高手门能从门外汗的角度指点我从那些方面去全面看懂以下的程序。不胜感激!!
#include <linux/types.h> #include <linux/module.h> #include <linux/sched.h> #include <linux/kernel.h> #include <linux/init.h> #include <linux/fs.h> #include <linux/delay.h> #include <linux/fs.h> #include <linux/poll.h> #include <linux/string.h> #include <linux/devfs_fs_kernel.h> #include <asm/uaccess.h> #include <asm/hardware.h> #include <asm/irq.h> #define JUPITER_LED_FLASH_NAME \"jpledflash\" #define JUPITER_LED_FLASH_MAJOR 230 static devfs_handle_t devfs_handle, devfs_led_flash_dir; static char *dev_idledflash = JUPITER_LED_FLASH_NAME; int led_state, timer_state; static ssize_t jupiter_led_flash_read(struct file *filp, char *buf, size_t count, loff_t *l) { return 0; } static ssize_t jupiter_led_flash_write(struct file *filp, const char *buf, size_t count, loff_t *l) { return 0; } /* static void jupiter_led_flash_timer(int irq, void *dev_idledflash, struct pt_regs *regs) { disable_irq(IRQ_TIMER1); disable_irq(IRQ_KEYSCAN); if(led_state==0){ *PIOLDAT1=0x00000000;//led on led_state=1; }else if(led_state==1){ *PIOLDAT1=0x02000000;//led off led_state=0; } enable_irq(IRQ_KEYSCAN); enable_irq(IRQ_TIMER1); } */ static void jupiter_led_flash_irq4(int irq, void *dev_idledflash, struct pt_regs *regs) { disable_irq(IRQ_EXTIRQ4); // disable_irq(IRQ_KEYSCAN); if(led_state==0){ printk(\"led on\\n\"); *PIOLDAT1=0x00000000;//led on led_state=1; }else if(led_state==1){ printk(\"led off\\n\"); *PIOLDAT1=0x02000000;//led off led_state=0; } mdelay(10); // enable_irq(IRQ_KEYSCAN); enable_irq(IRQ_EXTIRQ4); } /* static void jupiter_led_flash_keypad(int irq, void *dev_idledflash, struct pt_regs *regs) { disable_irq(IRQ_KEYSCAN); disable_irq(IRQ_EXTIRQ4); if(timer_state==0){ // *TIMCON1=0x0000C703;//TIMER1 run timer_state=1; if(led_state=1) *PIOLDAT1=0x02000000; }else if (timer_state==1){ // *TIMCON1=0x0000C702;//TIMER1 STOP timer_state=0; if(led_state=1) *PIOLDAT1=0x02000000; } enable_irq(IRQ_EXTIRQ4); enable_irq(IRQ_KEYSCAN); } */ static int jupiter_led_flash_open(struct inode *inode, struct file *filp) { int ret; /* if ((ret = request_irq(IRQ_KEYSCAN, jupiter_led_flash_keypad, SA_INTERRUPT, JUPITER_LED_FLASH_NAME, dev_idledflash))) { printk(\"jupiter_led_flash_keypad_init: failed to register IRQ_KEYSCAN\\n\"); free_irq(IRQ_KEYSCAN, dev_idledflash); return ret; } */ if((ret=request_irq(IRQ_EXTIRQ4,jupiter_led_flash_irq4, SA_INTERRUPT,JUPITER_LED_FLASH_NAME, dev_idledflash))) { printk(\"jupiter_led_flash_irq4_init: failed to register IRQ_TIMER1\\n\"); free_irq(IRQ_EXTIRQ4, dev_idledflash); return ret; } *INTMOD = 0x1000; //falling edge trigger //*TIMCNT1=0x0000C34F; //*TIMCON1=0x0000C703;/*TIMER1 run*/ timer_state=1; return 0; } static int jupiter_led_flash_release(struct inode *inode, struct file *filp) { *PIOLDAT1=0x02000000; /*led off*/ led_state=0; MOD_DEC_USE_COUNT; return 0; } static struct file_operations jupiter_led_flash_fops = { read: jupiter_led_flash_read, write: jupiter_led_flash_write, ioctl: NULL, open: jupiter_led_flash_open, release: jupiter_led_flash_release, }; int __init jupiter_led_flash_init(void) { int ret; if ((ret=devfs_register_chrdev(JUPITER_LED_FLASH_MAJOR, JUPITER_LED_FLASH_NAME, &jupiter_led_flash_fops))!=0) { printk(\"registering of \" JUPITER_LED_FLASH_NAME \" is failed\\n\"); return ret; } devfs_led_flash_dir = devfs_mk_dir(NULL, \"ledflash\", NULL); devfs_handle = devfs_register(devfs_led_flash_dir, \"ledflash\", DEVFS_FL_DEFAULT, JUPITER_LED_FLASH_MAJOR, 0, S_IFCHR | S_IRUSR | S_IWUSR, &jupiter_led_flash_fops, NULL); printk(\"jupiter led_flash driver initialized\\n\"); *PIOMOD1=0x00000000; *PIOLDAT1=0x02000000; led_state=0; return 0; } void __exit jupiter_led_flash_cleanup(void) { free_irq(IRQ_KEYSCAN, dev_idledflash); devfs_unregister_chrdev(JUPITER_LED_FLASH_MAJOR, JUPITER_LED_FLASH_NAME); printk(\"Jupiter led_flash driver removed\\n\"); } module_init(jupiter_led_flash_init); module_exit(jupiter_led_flash_cleanup); |
|
沙发#
发布于:2005-04-04 13:13
晕倒,这个看起来像c++,可是怎么没有main{}函数?
最开始的很多include#...是调用头文件,问问别的高手这些头文件是什么功能,从哪里调出来的吧(这个问题我一直没想清楚,看了很多例子里的头文件,复杂得很,我想肯定是调用现成的什么东西,绝对不可能每一个驱动开发者都有本事自己编写这些复杂的定义文件)。 后面的我也看不太懂了,呵呵...都是初学者么,来帮楼主顶。 基本上每一步的数据操作顺序还是明白的,就是不知道调用这些函数或者做循环到底是为了完成什么功能 |
|
板凳#
发布于:2005-04-04 14:25
这是linux的驱动吧
|
|