Arm驱动程序设计.ppt
Arm驱动程序设计,LED灯驱动,任务要求,要求:对开发板上的LED灯点亮实现:两部分1、驱动设计(在linux内核中加入LED驱动)2、应用程序实现(应用程序调用驱动实现点亮),硬件原理图,驱动编写,第一步:解压源码我们要编写的驱动是对应于LED第二步:进入drivers目录cd driversLED属于字符设备cd char进入char目录第三步:编写led-test.c驱动源程序gedit led-test.c,第一步、编写驱动包括6部分,1头文件2注册函数3卸载函数找一个类似的驱动打开,编写首先把头文件全部copy4定义设备名#define DEVICE_NAME leds“5硬件引脚定义6模块信息,#include#include#include#include#include#include#include#include#include#include#include#include#include#include#include#include#include#include#include,1加入硬件引脚,static unsigned long gpio_table=S3C2410_GPB5,S3C2410_GPB6,S3C2410_GPB7,S3C2410_GPB8,;,static int leds_init(void),static void leds_exit(void),2编写注册函数,3编写卸载函数,4加入模块信息,module_init(leds_init);module_exit(leds_exit);MODULE_LICENSE(GPL);MODULE_AUTHOR(zjl);,static unsigned int gpio_cfg_table=S3C2410_GPB5_OUTP,S3C2410_GPB6_OUTP,S3C2410_GPB7_OUTP,S3C2410_GPB8_OUTP,;,说明引脚功能,输出,static int leds_init(void)int ret;ret=misc_register(,misc说明LED属于混合设备(杂项),static void leds_exit(void)misc_deregister(,编写注册函数,编写卸载函数,结构体,定义结构体misc_leds,static struct miscdevice misc_leds=.minor=MISC_DYNAMIC_MINOR,.name=DEVICE_NAME,.fops=,static struct file_operations dev_leds_fops=.owner=THIS_MODULE,.open=leds_open,.ioctl=leds_ioctl,;,杂项的次设备号,定义结构体dev_leds_fops,static int leds_open(struct inode*inode,struct file*file)printk(TQ2440 leds opend!n);return 0;,完成结构体中的两个成员函数,.open=leds_open,.ioctl=leds_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(gpio_tablearg,cmd);return 0;default:return-EINVAL;,完整的驱动程序,#include#include#include#include#include#include#include#include#include#include#include#include#include#include#include#include#include#include#include#include#include#include#include#define DEVICE_NAME leds,/*用来指定LED所用的GPIO引脚*/static unsigned long gpio_table=S3C2410_GPB5,S3C2410_GPB6,S3C2410_GPB7,S3C2410_GPB8,;/*用来指定GPIO引脚的功能:输出*/static unsigned int gpio_cfg_table=S3C2410_GPB5_OUTP,S3C2410_GPB6_OUTP,S3C2410_GPB7_OUTP,S3C2410_GPB8_OUTP,;,static struct file_operations dev_leds_fops=.owner=THIS_MODULE,.open=leds_open,.ioctl=leds_ioctl,;static struct miscdevice misc_leds=.minor=MISC_DYNAMIC_MINOR,.name=DEVICE_NAME,.fops=,static int leds_open(struct inode*inode,struct file*file)printk(TQ2440 leds opend!n);return 0;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(gpio_tablearg,cmd);return 0;default:return-EINVAL;,第二步、修改配置文件Kconfig,gedit Kconfig,添加驱动选项,第三步、修改Makefile,gedit Makefile添加,名字与.c文件名一致,第四步、make menuconfig,退到根目录执行make menuconfig,Device drivers Character devices,找到你新添加的设备,选择,三种空白不选*内核选择M作为模块选择,第五步 编译,执行make zImage下载新的内核文件驱动制作完成,下一步、编写应用程序,#include#include#include#include int main(int argc,char*arg)int fd;fd=open(/dev/leds,0);if(fd 0)printf(TQ2440 leds open failed!n);ioctl(fd,1,0);ioctl(fd,0,1);ioctl(fd,1,2);ioctl(fd,1,3);close(fd);return 0;,调用驱动,调试,运行,第一步,下载zImage第二步,运行应用程序效果很好!,