嵌入式linux驱动程序 -- LED驱动
2013-09-24 22:00 TLLED 阅读(250) 评论(0) 收藏 举报LED--linux驱动程序
1 //zhui_led.c 2 #include <linux/config.h> 3 #include <linux/module.h> 4 #include <linux/kernel.h> 5 #include <linux/init.h> 6 #include <linux/fs.h> 7 #include <linux/devfs_fs_kernel.h> 8 #include <linux/miscdevice.h> 9 #include <linux/delay.h> 10 #include <asm/irq.h> 11 #include <asm/arch/regs-gpio.h> 12 #include <asm/hardware.h> 13 14 #include <asm/io.h> 15 16 #define DEVICE_NAME "ZHUI_LED" 17 #define LED_MAJOR 230 18 19 int major = 0; 20 struct cdev* leds_cdev; 21 22 23 24 25 static int zhui_led_ioctl(struct inode* inode, struct file* filp, unsigned int cmd, unsigned long arg) 26 { 27 if (arg > 4) 28 { 29 printk("deyond the led no\n"); 30 } 31 32 switch (cmd) 33 { 34 case 0: 35 case 1: 36 //*GPBDAT &= ~(1<<(arg + 4)); 37 if(arg==0) { 38 s3c2410_gpio_setpin(S3C2410_GPB5, !cmd); 39 } 40 if(arg==1) { 41 s3c2410_gpio_setpin(S3C2410_GPB6, !cmd); 42 } 43 if(arg==2) { 44 s3c2410_gpio_setpin(S3C2410_GPB7, !cmd); 45 } 46 if(arg==3) { 47 s3c2410_gpio_setpin(S3C2410_GPB8, !cmd); 48 } 49 break; 50 default: 51 printk("cmd error\n"); 52 break; 53 } 54 return 0; 55 } 56 57 58 struct file_operations zhui_led_fops = 59 { 60 .owner = THIS_MODULE, 61 .ioctl = zhui_led_ioctl, 62 }; 63 64 //模块加载 65 66 static int __init zhui_led_init(void) 67 { 68 int ret; 69 int i; 70 ret = register_chrdev(LED_MAJOR, DEVICE_NAME, &zhui_led_fops); //在内核中注册设备 71 72 if(ret<0) { 73 printk(DEVICE_NAME"can't register major number \n"); 74 return ret; 75 } 76 77 devfs_mk_cdev(MKDEV(LED_MAJOR, 0), S_IFCHR | S_IRUSR | S_IWUSR | S_IRGRP, DEVICE_NAME); 78 //初始化GPIO 79 s3c2410_gpio_cfpin(S3C2410_GPB5, S3C2410_GPB5_OUTP); 80 s3c2410_gpio_cfpin(S3C2410_GPB6, S3C2410_GPB6_OUTP); 81 s3c2410_gpio_cfpin(S3C2410_GPB7, S3C2410_GPB7_OUTP); 82 s3c2410_gpio_cfpin(S3C2410_GPB8, S3C2410_GPB8_OUTP); 83 84 s3c2410_gpio_setpin(S3C2410_GPB5, S3C2410_GPB5_OUTP); 85 s3c2410_gpio_setpin(S3C2410_GPB6, S3C2410_GPB6_OUTP); 86 s3c2410_gpio_setpin(S3C2410_GPB7, S3C2410_GPB7_OUTP); 87 s3c2410_gpio_setpin(S3C2410_GPB8, S3C2410_GPB8_OUTP); 88 89 printk(DEVICE_NAME "initialized\n"); 90 return 0; 91 } 92 93 static void __exit zhui_led_exit(void) 94 { 95 devfs_remove(DEVICE_NAME); 96 unregister_chrdev(LED_MAJOR, DEVICE_NAME); 97 } 98 99 module_init(zhui_led_init); 100 module_exit(zhui_led_exit); 101 102 MODULE_LICENSE("GPL"); 103 104 MODULE_AUTHOR("不做超哥已多年"); 105 MODULE_DESCRIPTION("leds");
1 //ZHUI_LED_TEST.C 2 3 #include <stdio.h> 4 #include <stdlib.h> 5 #include <unistd.h> 6 #include <sys/ioctl.h> 7 8 int main(int argc, char **argv) 9 { 10 int on; 11 int led_no; 12 int fd; 13 int i; 14 15 if(argc != 3 || sscanf(argv[1], "%d", &led_no) != 1 || sscanf(argv[2], "%d", &on) != 1 || on<0 || on>1 || led_no<0 || led_no>3) { 16 17 fprintf(stderr, "Usage:zhui_led_test led_no 0|1"); 18 exit(1); 19 } 20 //打开设备 21 fd = open("/dev/ZHUI_LED", 0); 22 if(fd<0) { 23 perror("open zhui_led"); 24 exit(1); 25 } 26 // 27 for(i=0;i<5;i++) 28 { 29 if(on==1) { 30 ioctl(fd, 0, led_no); 31 } else { 32 ioctl(fd, 1, led_no); 33 } 34 35 on=0; 36 sleep(1); 37 38 if(on==1) { 39 ioctl(fd, 0, led_no); 40 } else { 41 ioctl(fd, 1, led_no); 42 } 43 44 on=1; 45 sleep(1); 46 } 47 48 //关闭设备 49 close(fd); 50 return 0; 51 52 }
浙公网安备 33010602011771号