定位内核源代码,我用的天嵌的TQ210,LED驱动路径为drivers/char/tq210_leds.c
通过代码分析,得知参数应该怎么写其中,ioctl的第二个参数是控制亮和灭的,第三个参数是控制哪个led的
然后分析代码查看设备文件路径,
#define DEVICE_NAME "led"
/*椹卞姩鎺ュ彛璁剧疆*/ static struct file_operations dev_fops = { .owner = THIS_MODULE, .unlocked_ioctl = tq210_gpio_ioctl, .open = tq210_gpio_open, .release = tq210_gpio_close, }; /*璁惧缁撴瀯鐨勮缃?/ static struct miscdevice misc = { .minor = MISC_DYNAMIC_MINOR, .name = DEVICE_NAME, .fops = &dev_fops, };
测试代码如下:两个led交替闪烁
#include <stdio.h> #include <sys/ioctl.h> #include<fcntl.h> void main() { int fd; fd = open("/dev/led",O_RDWR); while(1) { ioctl(fd,0,2); ioctl(fd,1,1); sleep(2); ioctl(fd,0,1); ioctl(fd,1,2); sleep(2); } }