效果是隔一段时间反转led。
led驱动:
#include#include #include #include #include #include #include #include #include #include #define GPIO_TO_PIN(bank, gpio) (32 * (bank) + (gpio))#define AM335X_led_0 GPIO_TO_PIN(1, 30)#define AM335X_led_1 GPIO_TO_PIN(1, 31)// #define TEST_IO_NUM(117) #define NAME_MISC"GpioTest"#define NAME_MOUDULE"GpioTest1"#define USE_MISC_MODEstatic int major = 251; void GpioTest(void); static long GpioIOctl(struct file *filp, unsigned cmd, unsigned long arg) { GpioTest(); return 1; } void GpioTest(void) { int iCount = 0; for(iCount = 0; iCount <=20; iCount++ ) { if(iCount%2 == 0) { // gpio_direction_output(TEST_IO_NUM, 1); gpio_direction_output(AM335X_led_0, 1);printk(KERN_INFO"#######AM335X_led_0 statu is high.\r\n"); } else { gpio_direction_output(AM335X_led_0, 0); printk(KERN_INFO"#######AM335X_led_0 statu is low.\r\n"); } mdelay(3000); } printk(KERN_INFO"#######App run over!"); } static int GpioOpen(struct inode *inode, struct file *file) { int iRen = -1; iRen = gpio_request(AM335X_led_0, "AM335X_led_0"); if(iRen < 0) { printk(KERN_INFO"#######Failed to request the AM335X_led_0!"); }else { printk(KERN_INFO"#######Success to request the AM335X_led_0"); } return iRen; } static int GpioClose(struct inode *inode, struct file *file) { printk(KERN_INFO"#######Free the AM335X_led_0"); gpio_free(AM335X_led_0); return 1; } //****entry point for TEST GPIO module static const struct file_operations gpio_test_driver = { .owner = THIS_MODULE, .unlocked_ioctl= GpioIOctl, .llseek = no_llseek, .open = GpioOpen, .release = GpioClose, }; #ifdef USE_MISC_MODE static struct miscdevice gpiotest_misc_device = { .minor = MISC_DYNAMIC_MINOR, .name = NAME_MISC, .fops = &gpio_test_driver, }; #endif static int __init GpioTestInit(void) { int iRet; printk(KERN_INFO"#######GpioTest modules is install!\r\n"); #ifdef USE_MISC_MODE iRet = misc_register(&gpiotest_misc_device); if (iRet) { printk(KERN_INFO"#######unable to register a misc device\r\n"); return iRet; } #else iRet = register_chrdev(major, NAME_MOUDULE, &gpio_test_driver); if (iRet < 0) { printk(KERN_INFO"#######unable to register a chr device\r\n"); return iRet; } #endif return iRet; } static void __exit GpioTestExit(void) { #ifdef USE_MISC_MODE misc_deregister(&gpiotest_misc_device); #else unregister_chrdev(major, NAME_MOUDULE); #endif printk(KERN_INFO"#######GpioTest modules is exit!\r\n"); } module_init(GpioTestInit); module_exit(GpioTestExit); MODULE_AUTHOR("linjie");MODULE_DESCRIPTION("description~~");MODULE_ALIAS("word count moduel");MODULE_LICENSE("GPL");
- 编译驱动 make -C ******* M=*******
- 安装驱动 insmod ****
- lsmod
- ls /dev/
测试
#include#include #include #include #include #include #include int main() { int fd; printf("***running\r\n"); fd = open("/dev/GpioTest", O_RDWR); if(fd < 0) { printf("***Can't open the gpiotest!\r\n"); return -1; } ioctl(fd, 0, 0); close(fd); printf("***App run over!\r\n"); return 1; }
编译 native C arm-nono-linux-gcc -static -o testApp testLed.c