《Android驱动开发实例(控制LED灯)》由会员分享,可在线阅读,更多相关《Android驱动开发实例(控制LED灯)(11页珍藏版)》请在金锄头文库上搜索。
1、Android驱动例子(LED灯控制)本例子,讲述在An droid2.1上完全自已开发一个驱动去控制硬件口并写应用测试该驱动,通过这样一个例 子,解析an droid下的驱动开发流程的应用调用流程,可以说是很好的入门引导要达到的效果:通过Android的应用,调用驱动程序,在开发板上控制4个LED的亮灭。、硬件原理如上图,通过4个10 口控制这LED,低电平LED亮,这 4 个 I0 口分别是 GPM1, GPM2, GPM3, GPM4,二、驱动程序1、在kernel文件夹下的driver目录,新键驱动文件夹# cd kernel_Android_2.6.28.6/drivers进到开发板
2、的kernel目录,建驱动文件夹#mkdir ledtest2、在/driver/ledtest 目录下,新建 leddriver.c,leddriver.h , Keonfig, Makefile 等 4 个文件leddriver.c1. #include 2. #include linux/sched.h3. #include 4. #include /* For init/exit/. */5. #include 6. #include 7. #include 8. #include asm/uaccess h9. #include plat/gpio-cfg h10. #include
3、 11. #include 12. #include 13. #include 14. #include 15. #include 16. #include 17. #include /for register_chrdev()18. #include 19. #include 20. #include leddriver.h21. #include linux/miscdevice.h/* For MODULE_ALIAS_MISCDEV22. (WATCHDOG_MINOR) */23. #include /* For the watchdog specific items */24. #
4、include /* For file operations */25. #define Viberator_MAJOR97 /?ee?o?26. #define SCULL_NR_DEVS427. #define SCULL_QUANTUM400028. #define SCULL_QSET100029. /do as the GIO driver30. #define DEVCOUNT431. #define GIO_MINOR2/* GIO minor no. */32. static dev_t dev;/ee ? p?p?一 ee? o?33. static struct cdev
5、*cdev_p;34. static int openCnt;35. /e土?自?36. int VIB_major = 97;/we asigment it for test37. int VIB_minor = 0;38int VIB_nr_devs = SCULL_NR_DEVS;39. int VIB_quantum = SCULL_QUANTUM;40int VIB_qset = SCULL_QSET;41.42. static struct class *vib_dev_class;43. #define GPNCON S3C64XX_GPNCON44. #define GPNDA
6、T S3C64XX GPNDAT45. #define GPNPUD S3C64XX_GPNPUD46. #define GPMCON S3C64XX_GPMCON47. #define GPMDAT S3C64XX_GPMDAT48. #define GPMPUD S3C64XX_GPMPUD49.50. #define VIB ON 0x1151.#defineVIB OFF 0x2252.static conststruct file operationsGPIO Viberator ctl ops=53.owner=THIS MODULE,54.open=GPIO VIB open,5
7、5.read=GPIO VIB read,5 6.write=GPIO VIB write,57.ioctl=GPIO VIB ioctl,58.release=GPIO VIB release,59.;60.ssize t GPIOs)VIB read(struct file *file,char * buf,size t count,loff t * f 61.62.printk(GPIO VIB read rn);63. gpio_direction_output(S3C64XX_GPM(3), 0);/64. return count ;65. 66. ssize_t GPIO_VIB
8、_write (struct file * file,const char * buf, size_t count,loff_t * f_ops)67. 68. printk( GPIO_VIB_write rn);69. gpio_direction_output(S3C64XX_GPM(3), 1);/70. return count;71. 72.72. /ssize_t GPIO_VIB_ioctl(struct inode * inode,struct file * file,unsigned incmd, long data)73. static int GPIO_VIB_ioct
9、l(struct inode *inode, struct file *file, unsigned intcmd, unsigned long arg)74. 75. printk(KERN_ERRVIB:GPIO_VIB_ioctl -CMD=%x n,cmd);76. switch(cmd)77. 78. case VIB_ON:79. gpio_direction_output(S3C64XX_GPM(1),0);/80. gpio_direction_output(S3C64XX_GPM(2),0);/81. gpio_direction_output(S3C64XX_GPM(3),
10、0);/82. gpio_direction_output(S3C64XX_GPM(4),1);/83. printk(KERN ERRVIB:GPIO VIB ioctl -VIB ONn);85.break;86.case VIB OFF:87.88.printk(KERN ERRVIB:GPIO VIB ioctl -VIB OFFn);89.gpio direction output(S3C64XX GPM(1), 1);/90.gpio direction output(S3C64XX GPM(2), 1);/91.gpio direction output(S3C64XX GPM(
11、3), 1);/92.gpio direction output(S3C64XX GPM(4), 0);/93.break;94.95.default:break;96.97.98./gpio_free(S3C64XX_GPN(7);99. 100.ssize t GPIO VIB open(struct inode * inode,struct file * file)101.102./? ?eey103./MOD INC USE COUNT;104.printk(GPIO VIB open() n);105.return 0;106.107.ssize t GPIO VIB release
12、(struct inode * inode, struct file * file)108.109./ MOD DEC USE COUNT;/? ?eey?110.printk(GPIO VIB release() n);111.return 0;112.113.static int GPIO VIB CTL init(void)114.115.int ret = -ENODEV;116.int error ;117.printk( rn);118./3?e?_u119.s3c_gpio_cfgpin(S3C64XX_GPM(1), S3C_GPIO_SFN(1);/GPM1 output12
13、0.s3c_gpio_cfgpin(S3C64XX_GPM(2), S3C_GPIO_SFN(1);/GPM2 output121.s3c_gpio_cfgpin(S3C64XX_GPM(3), S3C_GPIO_SFN(1);/GPM3 output122.s3c_gpio_cfgpin(S3C64XX_GPM(4), S3C_GPIO_SFN(1);/GPM4 output123.#if 1 /*?2i?e?xC2a?y?-*/124.ret = register chrdev(Viberator MAJOR, viberator,&GPIO Viberatorctl ops);125.if (ret 0)126.printk(KERN ERR VIB: unable to get major %dn, ret);127.return ret;128.129./,?uc1class130.vib dev class = class create(THIS MODULE, viberator);131.if (IS ERR(vib dev class)