Android驅(qū)動開發(fā)實例(控制LED燈)_第1頁
Android驅(qū)動開發(fā)實例(控制LED燈)_第2頁
Android驅(qū)動開發(fā)實例(控制LED燈)_第3頁
Android驅(qū)動開發(fā)實例(控制LED燈)_第4頁
Android驅(qū)動開發(fā)實例(控制LED燈)_第5頁
已閱讀5頁,還剩9頁未讀 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

1、Android驅(qū)動例子(LED燈控制)本例子,講述在Android2.1上完全自已開發(fā)一個驅(qū)動去控制硬件口并寫應(yīng)用測試該驅(qū)動,通過這樣一個例子,解析android下的驅(qū)動開發(fā)流程的應(yīng)用調(diào)用流程,可以說是很好的入門引導(dǎo) 要達到的效果:通過Android的應(yīng)用,調(diào)用驅(qū)動程序,在開發(fā)板上控制4個LED的亮滅。一、硬件原理如上圖,通過4個IO口控制這LED,低電平LED亮,這4個IO口分別是GPM1, GPM2, GPM3, GPM4,二、驅(qū)動程序1、在kernel文件夾下的driver目錄,新鍵驅(qū)動文件夾# cd kernel_Android進到開發(fā)板的kernel目錄,建驅(qū)動文件夾#mkdir l

2、edtest2、在/driver/ledtest目錄下,新建leddriver.c ,leddriver.h , Kconfig, Makefile 等4個文件1. #include 2. #include 3. #include 4. #include              /* For _init/_exit/. */ 5. #include 6. #include 7. #include 8. #include 9. #include 10. #include 11

3、. #include 12. #include 13. #include 14. #include 15. #include 16. #include 17. #include /for register_chrdev( 18. #include 19. #include 20. #include "leddriver.h" 21. #include          /* For MODULE_ALIAS_MISCDEV 22.      &#

4、160;                      (WATCHDOG_MINOR */ 23. #include          /* For the watchdog specific items */ 24. #include      &#

5、160;       /* For file operations */ 25. #define Viberator_MAJOR        97  /?÷éè±?o? 26. #define SCULL_NR_DEVS           4 27. #define SCULL_QUA

6、NTUM         4000 28. #define SCULL_QSET             1000 29. /-do as the GIO driver 30. #define DEVCOUNT               

7、 4 31. #define GIO_MINOR            2    /* GIO minor no. */ 32. static dev_t dev;        /éê ? ?÷ éè±? o? 33. static struct cdev *cdev_p; 34. st

8、atic int openCnt; 35. /-è?±?á?- 36. int VIB_major  = 97;/we asigment it for test 37. int VIB_minor = 0; 38. int VIB_nr_devs = SCULL_NR_DEVS; 39. int VIB_quantum = SCULL_QUANTUM; 40. int VIB_qset = SCULL_QSET; 41.   42. static struct class *vib_dev_class; 43. #define GPN

9、CON  S3C64XX_GPNCON 44. #define GPNDAT  S3C64XX_GPNDAT 45. #define GPNPUD  S3C64XX_GPNPUD 46. #define GPMCON S3C64XX_GPMCON 47. #define GPMDAT  S3C64XX_GPMDAT 48. #define GPMPUD S3C64XX_GPMPUD 49.   50. #define VIB_ON 0x11 51.   #defineVIB_OFF 0

10、x22 52. static const struct file_operations GPIO_Viberator_ctl_ops= 53.     .owner        = THIS_MODULE, 54.     .open    = GPIO_VIB_open, 55.     .read    =GPI

11、O_VIB_read, 56.     .write    =GPIO_VIB_write, 57.     .ioctl     = GPIO_VIB_ioctl, 58.     .release     =GPIO_VIB_release, 59. ; 60. ssize_t GPIO_VIB_read(struct file * file,char

12、* buf,size_t count,loff_t * f_ops 61. 62.     printk(" GPIO_VIB_read rn" 63.     gpio_direction_output(S3C64XX_GPM(3, 0;/ 64.     return count ; 65. 66. ssize_t GPIO_VIB_write (struct file * file,const char * buf, size_t count,lof

13、f_t * f_ops 67. 68.       printk(" GPIO_VIB_write rn" 69.       gpio_direction_output(S3C64XX_GPM(3, 1;/ 70.    return count; 71. 72.   73. /ssize_t GPIO_VIB_ioctl(struct inode *  inode,struct file *

14、file,unsigned int cmd, long data 74. static int GPIO_VIB_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg 75. 76.     printk(KERN_ERR"VIB:GPIO_VIB_ioctl -CMD=%x n",cmd; 77.     switch(cmd 78.     &

15、#160;    79.            case VIB_ON: 80.             gpio_direction_output(S3C64XX_GPM(1, 0;/ 81.             

16、gpio_direction_output(S3C64XX_GPM(2, 0;/ 82.             gpio_direction_output(S3C64XX_GPM(3, 0;/ 83.             gpio_direction_output(S3C64XX_GPM(4, 1;/ 84.    

17、60;        printk(KERN_ERR"VIB:GPIO_VIB_ioctl -VIB_ONn" 85.             break; 86.          case VIB_OFF: 87.       

18、;       88.             printk(KERN_ERR"VIB:GPIO_VIB_ioctl -VIB_OFFn" 89.             gpio_direction_output(S3C64XX_GPM(1, 1;/ 90.  

19、60;          gpio_direction_output(S3C64XX_GPM(2, 1;/ 91.             gpio_direction_output(S3C64XX_GPM(3, 1;/ 92.             gpi

20、o_direction_output(S3C64XX_GPM(4, 0;/ 93.             break; 94.           95.          default:break; 96.        &

21、#160;         97.      98.         /gpio_free(S3C64XX_GPN(7; 99. 100. ssize_t GPIO_VIB_open(struct inode * inode,struct file * file 101. 102. /?éêy 103.   /MOD_INC_USE_COUNT; 104.

22、  printk("GPIO_VIB_open( n"    105.   return 0; 106. 107. ssize_t GPIO_VIB_release(struct inode * inode, struct file * file 108. 109.    / MOD_DEC_USE_COUNT;/?éêy? 110.      printk("GPIO_VIB_release(

23、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?ê?

24、ú 119.         s3c_gpio_cfgpin(S3C64XX_GPM(1, S3C_GPIO_SFN(1;/GPM1 output 120.         s3c_gpio_cfgpin(S3C64XX_GPM(2, S3C_GPIO_SFN(1;/GPM2 output 121.         s3c_gpio_cfgpin(

25、S3C64XX_GPM(3, S3C_GPIO_SFN(1;/GPM3 output 122.         s3c_gpio_cfgpin(S3C64XX_GPM(4, S3C_GPIO_SFN(1;/GPM4 output 123. #if 1 /*?2ì?·?ê?×2á?y?*/ 124. ret = register_chrdev(Viberator_MAJOR, "viberator", &GPIO_Viberator_ctl

26、_ops; 125. if (ret < 0 126.     printk(KERN_ERR "VIB: unable to get major %dn", ret; 127.     return ret; 128. 129. /?¨uc1class 130. vib_dev_class = class_create(THIS_MODULE, "viberator" 131. if (IS_ERR(vib_dev_class 132.  

27、0;  unregister_chrdev(Viberator_MAJOR, "capi20" 133.     return PTR_ERR(vib_dev_class; 134. 135. /?¨?ú? 136. device_create(vib_dev_class, NULL, MKDEV(Viberator_MAJOR, 0, NULL, "vib" 137. / create a point under /dev/class/vib   138

28、. /í¨1yé?aá?2?y?ó?oó?í?á?ú/dev/class/?éú3éuc1vib?ú?ó|ó?3ìDò?éò?2ù×÷?t?ù2ù×÷?a?ú?í¨1yuc1open ,write,read èoêy2ù×÷?ê?é?

29、33;ò?oó?ó|ó?ê?ày3ìDò? 139.   return 0;     140. #endif 141. #if 0/* ×2á?ì?*/ 142.     if (error = alloc_chrdev_region(&dev, 0, DEVCOUNT, "vibrate" < 0 143.    

30、0;     144.         printk(KERN_ERR 145.                "VIB: Couldn't alloc_chrdev_region, error=%dn", 146.        

31、        error; 147.         return 1; 148.      149.          printk("dev = %d  n",dev; 150.     cdev_p = cdev_allo

32、c(; 151.     cdev_p->ops = &GPIO_Viberator_ctl_ops; 152.     error = cdev_add(cdev_p, dev, DEVCOUNT; 153.     if (error 154.         printk(KERN_ERR 155.       

33、60;        "VIB: Couldn't cdev_add, error=%dn", error; 156.         return 1; 157.      158.   159.     vib_dev_class = class_create(THIS_MODULE, "vib-dev&q

34、uot; 160.     if (IS_ERR(vib_dev_class 161.         res = PTR_ERR(vib_dev_class; 162.         goto out_unreg_class; 163.      164.     return 0; 165. #endif 166

35、. out_unreg_class: 167.     class_destroy(vib_dev_class; 168. return 1; 169. 170.   171. static int _init S3C6410_VIB_init(void 172. 173.     int ret = -ENODEV; 174.     /÷ó?oêy    175.    &

36、#160;    printk(KERN_ERR "Auly: S3C6410_VIB_init-n" 176.     ret = GPIO_VIB_CTL_init(; 177.     if(ret 178.          179.         printk(KERN_ERR &quo

37、t;Auly: S3C6410_VIB_init-Fail !n" 180.         return ret; 181.      182.     return 0; 183. 184. static void _exit cleanup_GPIO_VIB(void 185. 186.     /×?úéè±? 187. 

38、0; /  devfs_unregister_chrdev(Viberator_MAJOR,"gpio_vib_ctl" 188. #if 0 189.       cdev_del(cdev_p; 190.     unregister_chrdev_region(dev, DEVCOUNT; 191.     class_destroy(vib_dev_class; 192. #endif 193. d

39、evice_destroy(vib_dev_class, MKDEV(Viberator_MAJOR, 0; 194. class_destroy(vib_dev_class; 195. unregister_chrdev(Viberator_MAJOR, "viberator" 196. 197. MODULE_LICENSE("GPL" 198. MODULE_DESCRIPTION("Peter first driver" 199. MODULE_ALIAS_CHARDEV(Viberator_MAJOR, 0; 200. &#

40、160; 201. module_init(S3C6410_VIB_init; 202. module_exit(cleanup_GPIO_VIB; 1. ssize_t GPIO_VIB_read(struct file * file,char * buf,size_t count,loff_t * f_ops; 2. ssize_t GPIO_VIB_write (struct file * file,const char * buf, size_t count,loff_t * f_ops; 3. static int GPIO_VIB_ioctl(struct inode *inode

41、, struct file *file, unsigned int cmd, unsigned long arg; 4. ssize_t GPIO_VIB_open(struct inode * inode,struct file * file; 5. ssize_t GPIO_VIB_release(struct inode * inode, struct file * file; 6. static int GPIO_VIB_CTL_init(void; Kconfig 文件Kconfig 文件1. config LEDTEST 2.   tristate "

42、LED test for ARMeasy" 3.   default n 4.   help 5.   this is a LED driver for ARMEASY with S3C6410 Makefile文件Makefile文件1. obj-$(CONFIG_LEDTEST+=leddriver.o 3、在內(nèi)核配置里菜單里加入本驅(qū)動的配置項達到的效果是,可以通過內(nèi)核配置來選擇是否把本驅(qū)動編譯進內(nèi)核里,也就是出現(xiàn)在make menuconfig 后出來的界面里,可以參考9.8章<Android添加新驅(qū)動方法>

43、;1)在arch/arm/Kconfig文件里 menu "Device Drivers"與 endmenu之間添加1. source "drivers/ledtest/Kconfig" 2)在drivers/Kconfig menu "Device Drivers" 和 endmenu之間添加1. source "drivers/ledtest/Kconfig" 3修改/drivers/Makefile文件1. Obj-$(CONFIG_LEDTEST +=ledtest/ 4、編譯驅(qū)動在kernel目錄下,終端

44、輸入1. #make menuconfig “Device Drivers” 下面會看到 “LED test for ARMeasy”,選擇它,保存并退出1. #make 這樣,就會在/drivers/ledtest目錄下得到leddrivr.ko文件,它就是驅(qū)動目標文件,已級編譯進了zImage 里了,只要用這個kernel燒錄到開發(fā)板,開機就會自動加載本驅(qū)動,PS,如果發(fā)現(xiàn)如上編譯,沒有在ledtest目錄下生成leddriver.ko文件,也就是本根沒有去編譯本驅(qū)動,那就把ledtest目錄入到/drivers/misc目錄下,相應(yīng)的修改misc下面,然后,在makefile里,不用選直

45、接寫成obj-y += leddriver.o,這樣強制的包含進去編譯。三、 應(yīng)用程序(測試驅(qū)動)用C語言寫一個應(yīng)用,測試上面寫的驅(qū)動是否工作正常,這是一般的驅(qū)動開發(fā)流程主要有兩個文件:Android1、在$(YOUR_Android/external/LEDTEST/目錄編寫ledtest.c文件,ledtest.c 1. #include 2. #include 3. #include / contact the open(,close(,read(,write( and so on! 4.   #define DEVICE_NAME "/dev/vib&qu

46、ot;/device point 5. #define LED_ON     0x11 6. #define LED_OFF     0x22 7. int main(int argc,char *argv 8.     9.     int fd; 10.     int ret; 11.     char *i; 12.    

47、60;printf("n start gpio_led_driver test rn" 13.     fd = open(DEVICE_NAME,O_RDWR;/Open device ,get the handle 14.     printf("fd = %d n",fd; 15.     if(fd = -1 /open fail 16.      17.   

48、0;   printf("open device %s error n",DEVICE_NAME; 18.      19.     else 20.      21.         while(1 22.          23.    

49、;       ioctl(fd,LED_OFF; /call the output function to off LEDs 24.           sleep(1;/wait 1 second 25.           ioctl(fd,LED_ON; 26.      

50、;     sleep(1; 27.          28.         ret = close(fd; /close device 29.         printf("ret = %d n",ret; 30.         printf("close gpio_led test n" 31.      32.   return 0; 33. 2、在$(YOUR_Android/external/ LEDTEST /目錄編寫Android.mk文件。這是Android Makefile的標準命名,不要更改。Android.mk文件的格

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論