网站导航栏设计步骤,策划书网站项目目标需求分析,公司做公司网站广告,亚马逊电商现在好做吗LED 驱动程序移植 编者#xff1b;对于led的驱动程序#xff0c;很多文章都有详细的介绍#xff0c;我的博客里面有一篇专门详解这个的。需要看的#xff0c;可以找下。led灯的驱动其实就代表了I/O口的驱动。在linux系统下#xff0c;操作一个I/O口#xff0c;可以说实在… LED 驱动程序移植 编者对于led的驱动程序很多文章都有详细的介绍我的博客里面有一篇专门详解这个的。需要看的可以找下。led灯的驱动其实就代表了I/O口的驱动。在linux系统下操作一个I/O口可以说实在是麻烦至极与裸机操作相比较的话。这里简介移植过程没写分析。 1 LED 驱动原理 这个就给个图就够了搞驱动要连这个都搞不懂那就完了。 2、驱动的移植。 在drivers/char 目录下我们建立一个驱动程序文件mini2440_leds.c内容如下: [cpp] view plaincopy span stylefont-size:18px;#include linux/miscdevice.h #include linux/delay.h #include asm/irq.h #include mach/regs-gpio.h #include mach/hardware.h #include linux/kernel.h #include linux/module.h #include linux/init.h #include linux/mm.h #include linux/fs.h #include linux/types.h #include linux/delay.h #include linux/moduleparam.h #include linux/slab.h #include linux/errno.h #include linux/ioctl.h #include linux/cdev.h #include linux/string.h #include linux/list.h #include linux/pci.h #include linux/gpio.h #include asm/uaccess.h #include asm/atomic.h #include asm/unistd.h #define DEVICE_NAME leds span stylecolor:#3366ff;//设备名(/dev/leds/span) span stylecolor:#3366ff;//LED 对应的GPIO 端口列表 /spanstatic unsigned long led_table [] { S3C2410_GPB(5), S3C2410_GPB(6), S3C2410_GPB(7), S3C2410_GPB(8), }; span stylecolor:#3366ff;//LED 对应端口将要输出的状态列表 /spanstatic unsigned int led_cfg_table [] { S3C2410_GPIO_OUTPUT, S3C2410_GPIO_OUTPUT, S3C2410_GPIO_OUTPUT, S3C2410_GPIO_OUTPUT, }; span stylecolor:#3366ff;/*ioctl 函数的实现 * 在应用/用户层将通过ioctl 函数向内核传递参数以控制LED 的输出状态 */ /spanstatic int sbc2440_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; } span stylecolor:#3366ff;//根据应用/用户层传递来的参数(取反)通过s3c2410_gpio_setpin 函数设置LED 对应的端口寄存 器/span s3c2410_gpio_setpin(led_table[arg], !cmd); return 0; default: return -EINVAL; } } span stylecolor:#3366ff;/* * 设备函数操作集在此只有ioctl 函数通常还有read, write, open, close 等因为本LED 驱动在下面已经 * 注册为misc 设备因此也可以不用open/close */ /spanstatic struct file_operations dev_fops { .owner THIS_MODULE, .ioctl sbc2440_leds_ioctl, }; span stylecolor:#3366ff;BACKGROUND-COLOR: #ffffff/* * 把LED 驱动注册为MISC 设备 */ /spanstatic struct miscdevice misc { .minor MISC_DYNAMIC_MINOR, //动态设备号 .name DEVICE_NAME, .fops dev_fops, }; span stylecolor:#3366ff;/* * 设备初始化 */ /spanstatic int __init dev_init(void) { int ret; int i; for (i 0; i 4; i) { span stylecolor:#3366ff;//设置LED 对应的端口寄存器为输出(OUTPUT) /spans3c2410_gpio_cfgpin(led_table[i], led_cfg_table[i]); span stylecolor:#3366ff;//设置LED 对应的端口寄存器为低电平输出在模块加载结束后四个LED 应该是全部都是发光 状态/span s3c2410_gpio_setpin(led_table[i], 0); } ret misc_register(misc); span stylecolor:#3366ff;//注册设备/span printk (DEVICE_NAME\tinitialized\n); span stylecolor:#3366ff;//打印初始化信息/span return ret; } static void __exit dev_exit(void) { misc_deregister(misc); } module_init(dev_init); span stylecolor:#3333ff;//模块初始化仅当使用insmod/podprobe 命令加载时有用如果设备不是通过模块方式加载此处将不会被调用 /spanmodule_exit(dev_exitspan styleBACKGROUND-COLOR: #ffffff);span stylecolor:#3366ff;//卸载模块当该设备通过模块方式加载后可以通过rmmod 命令卸载将调用此函 /span/spanspan styleBACKGROUND-COLOR: #3366ff数 /spanMODULE_LICENSE(GPL); span stylecolor:#3366ff;//版权信息 /spanMODULE_AUTHOR(FriendlyARM Inc.); span stylecolor:#3366ff;BACKGROUND-COLOR: #ffffff//开发者信息/span/span 接下来我们添加LED 设备的内核配置选项打开drivers/char/Kconfig 文件添加如下红色部分内容 config DEVKMEM bool /dev/kmem virtual device support default y help Say Y here if you want to support the /dev/kmem device. The /dev/kmem device is rarely used, but can be used for certain kind of kernel debugging operations. When in doubt, say N. config LEDS_MINI2440 tristate LED Support for Mini2440 GPIO LEDs depends on MACH_MINI2440 default y if MACH_MINI2440 help This option enables support for LEDs connected to GPIO lines on Mini2440 boards. config MINI2440_ADC bool ADC driver for FriendlyARM Mini2440 development boards depends on MACH_MINI2440 default y if MACH_MINI2440 help this is ADC driver for FriendlyARM Mini2440 development boards Notes: the touch-screen-driver required this option 接下来再根据该驱动的配置定义把对应的驱动目标文件加入内核中打开linux-2.6.32.2/drivers/char/Makefile 文件添加如下红色部分内容 obj-$(CONFIG_JS_RTC) js-rtc.o js-rtc-y rtc.o obj-$(CONFIG_LEDS_MINI2440) mini2440_leds.o obj-$(CONFIG_MINI2440_ADC) mini2440_adc.o # Files generated that shall be removed upon make clean clean-files : consolemap_deftbl.c defkeymap.c 这样我们就在内核中添加做好了LED 驱动 3 配置编译新内核并测试LED 接上面的步骤在内核源代码目录下执行make menuconfig 重新配置内核依次选择进入如下子菜单项 Device Drivers --- Character devices --- 进入LED 驱动配置菜单进行内核配置。 在内核源代码根目录下执行make zImage把生成的新内核烧写到开发板中。 3 测试LED 用自带的文件系统启动后就会运行一个led程序。测试结果如图。