2022年linux驱动程序设计方案实例.docx
《2022年linux驱动程序设计方案实例.docx》由会员分享,可在线阅读,更多相关《2022年linux驱动程序设计方案实例.docx(22页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、精品学习资源封面欢迎下载精品学习资源作者: PanHongliang仅供个人学习AT91SAM9G20 驱动程序设计开发环境: Vmware + ubuntu10.04硬件平台: AT91SAM9G20欢迎下载精品学习资源Linux 版本: linux2.6.27一: led 驱动说明:由于设计的开发板上没有led 灯,便通过 PC0 来演示,通过示波器来观看引脚端的电平变化;1.驱动程序: my_led.c#include #include #include #include #include #include #include #define MY_LED_MAJOR250/定义主设备号#
2、define LED_ON0#define LED_OFF 1 struct global_devstruct cdev cdev; ;/定义设备结构体struct global_dev *global_devp ;/定义一个指向设备结构体的指针static int my_led_openstruct inode *inode, struct file *filpfilp-private_data = global_devp ;return 0;static int my_led_releasestruct inode *inode, struct file *filereturn 0;sta
3、tic int my_led_ioctlstruct inode *inode, struct file *filp, unsigned int cmd, unsigned long dataswitchcmdcase LED_ON:at91_set_gpio_valueA T91_PIN_PC0, 0 ;/将 PC0 引脚置低break;case LED_OFF:at91_set_gpio_valueA T91_PIN_PC0, 1 ;/将 PC1 引脚置高break;欢迎下载精品学习资源default:printkno valid cmd input.n;break;欢迎下载精品学习资源r
4、eturn 0;struct file_operations my_led_ctl_ops =欢迎下载精品学习资源.owner = THIS_MODULE,.open = my_led_open,.release = my_led_release,.ioctl = my_led_ioctl, ;/* 初始化设备结构体 */static void my_led_setupstruct global_dev *dev, int indexint err ;int devno = MKDEVMY_LED_MAJOR, index;cdev_init&dev-cdev, &my_led_ctl_ops
5、;dev-cdev.owner = THIS_MODULE;dev-cdev.ops = &my_led_ctl_ops ;err = cdev_add&dev-cdev, devno, 1 ;iferrprintkadd my led setup failed.n;static int my_led_initvoidint ret ;dev_t devno = MKDEVMY_LED_MAJOR, 0; / 创建设备号printkmy first driver-led.n;at91_set_GPIO_periphA T91_PIN_PC0, 1 ;at91_set_gpio_outputA
6、T91_PIN_PC0, 1 ;/对 PC0 引脚的初始化ret = register_chrdev_regiondevno, 1, my_led;/申请设备号if ret cdev ;/删除设备kfreeglobal_devp ;/释放内存unregister_chrdev_regionMKDEVMY_LED_MAJOR, 0, 1;/释放设备号MODULE_LICENSEMYGPL;MODULE_AUTHORFANY;module_initmy_led_init; /注册设备module_exitmy_led_cleanup ;/卸载设备2:如何将驱动驱动程序编译成模块在 drivers
7、目录下新建 led 目录,并在该目录下添加 Kconfig ,欢迎下载精品学习资源Makefile 文件;Kconfig :Menu My driver support ConfigTrisate led driver. HelpLed driverEndmenu:Makefile :Obj-$CONFIG_MY_LED+= my_led.o修改 linux/drivers 目录下的 Kconfig , Makefile 文件Kconfig:Source drivers/led/Kconfig Makefile:Obj-y+= my_led/修改体系结构目录 arch/arm 目录下的 Kco
8、nfig 文件,否就在配置菜单中将无法看到 led 的配置选项;(假如是在 drivers 目录下新建一文件夹,并在其中添加驱动程序,必需相应的体系结构目录下添加配置选项);Kconfig:Source driver/led/Kconfig3.测试程序: my_led_test.c#include #include #include #include #include #define DEVICE_NAME /dev/my_led #define LED_ON0#define LED_OFF1int mainvoidint fd ;int ret ;int i ;printfmy_led_dr
9、iver test.n ;fd = openDEVICE_NAME, O_RDONLY ;iffd = -1printfopen device %s error.n, DEVICE_NAME;fori = 0 ; i 50 ; i+欢迎下载精品学习资源ioctlfd, LED_OFF ;sleep1;ioctlfd, LED_ON ;sleep1;ret = closefd ;printfret = %dn, ret ;printfclose my_led_driver.n;return 0;将测试程序编译成目标平台的可执行文件,并下载到开发板GCC=/home/zzq/9G20/arm-20
10、07q1/bin/arm-none-linux-gnueabi-gcc # 交叉编译器的路径My_led_test:my_led_test.c$GCC -o my_led_test my_led_test.c clean:rm -f my_led_test学习总结:熟识驱动程序的架构,如何将驱动程序添加到内核即如何写测试程序;二:按键驱动设计1. 硬件部分: PC4 接按键;2. 驱动程序:#include #include #include #include #include #include #include #include #include #include #include #inc
11、lude #include #include #include #include #include #include #define BUTTON_MAJOR245#define DEVICE_NAME/dev/button static volatile int ev_press = 0 ;static struct cdev button_cdev;static void button_do_taskletunsigned long n ;欢迎下载精品学习资源DECLARE_TASKLETbutton_tasklet, button_do_tasklet, 0;/定义 tasklet 并与
12、处理函数关联起来static DECLARE_WAIT_QUEUE_HEADbutton_waitq;/ 静态的初始化一个等待队列struct button_irq_desc int irq ;int irq_type ;int pin ;int number;char *name; ;staticstructbutton_irq_descbutton_irq1=A T91_PIN_PB22,AT91_AIC_SRCTYPE_LOW,AT91_PIN_PB22, 0, KEY0 ;static int key_values1=0;/中断处理底半部static void button_do_ta
13、skletunsigned long nwake_up_interruptible&button_waitq;/唤醒队列printkbutton press.n ;/中断处理顶半部static irqreturn_t button_interruptint irq, void *dev_id, struct pt_regs *regsint up ;static int press_down;up = gpio_get_valuebutton_irq0.pin ;printkirqn ;/* 按键消抖 */ifuppress_down = 1; /当按键没有按下 ,置标志位为 1. if.up
14、 & press_down = 1 press_down = 0; / 当按键按下 ,置标志位为 0. ev_press = 1;at91_set_gpio_valuebutton_irq0.pin, 1 ;key_valuesbutton_irq0.number = .up ;tasklet_schedule&button_tasklet ;return IRQ_RETV ALIRQ_HANDLED;static int button_openstruct inode *inode, struct file *filpreturn 0;static int button_releasestr
15、uct inode *inode, struct file *filpreturn 0;static int button_readstruct file *filp, charuser *buff, size_t count, loff_t *offpint ret ;if.ev_press /当按键没有按下时,读进程挂起,知道按键按下;wait_event_interruptiblebutton_waitq, ev_press ;ev_press = 0;ret = copy_to_userbuff, const void *key_values, minsizeofkey_values,
16、 count;欢迎下载精品学习资源memsetvoiduser *key_values, 0, sizeofkey_values ;return ret . -EFAULT:minsizeofkey_values, count ;static struct file_operations button_fops = .owner = THIS_MODULE,.open = button_open,.release = button_release,.read = button_read, ;static int irq_initvoidint err ;at91_set_gpio_inputb
17、utton_irq0.pin, 1 ;at91_set_deglitchbutton_irq0.pin, 1 ;/ 将 PC0 设置为中断功能set_irq_typebutton_irq0.irq, button_irq0.irq_type;/设置中断类型at91_set_gpio_valuebutton_irq0.pin, 1 ;err = request_irq button_irq0.irq, button_interrupt, IRQF_DISABLED, button_irq0.name, void *&button_irq0;/申请中断欢迎下载精品学习资源if err disabl
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 2022 linux 驱动程序 设计方案 实例
限制150内