上次,我們寫了一個LED的驅動程序,這一節(jié),我們只需稍微改動一下就可以實現(xiàn)蜂鳴器的驅動,讓我們來看看吧。file:///C:\Users\郭曉娟\AppData\Local\Temp\ksohtml\wps3E58.tmp.png 1、看電路圖file:///C:\Users\郭曉娟\AppData\Local\Temp\ksohtml\wps3E68.tmp.png (1)蜂鳴器接口位于電路板的底板,看電路圖可知道是高電平有效。 file:///C:\Users\郭曉娟\AppData\Local\Temp\ksohtml\wps3E69.tmp.jpg (2)相對應的找到核心板的接口。由此可知,我們的蜂鳴器是GPD0_0 file:///C:\Users\郭曉娟\AppData\Local\Temp\ksohtml\wps3E7A.tmp.jpg 接下來找數(shù)據(jù)手冊,嵌入式物聯(lián)網(wǎng)等系統(tǒng)學習企鵝意義氣嗚嗚吧久零就易,找到對應的寄存器,然后配置它就可以了。 2、查數(shù)據(jù)手冊,找到相關的寄存器,并配置file:///C:\Users\郭曉娟\AppData\Local\Temp\ksohtml\wps3E8B.tmp.png (1)找到GPD0CON,地址是0x114000A0,我們需要配置GPD0CON(0)為輸出狀態(tài)。也就是寫0x1這個值到這個寄存器。 file:///C:\Users\郭曉娟\AppData\Local\Temp\ksohtml\wps3E9B.tmp.png (2)找到GPD0DAT這個寄存器,用于配置蜂鳴器的高低電平,物理地址是0x114000A4,剛好與上一個差4個字節(jié)的偏移 我們只要對這個寄存器寫1和寫0,那么蜂鳴器就可以叫起來了,哈哈。是不是很簡單?file:///C:\Users\郭曉娟\AppData\Local\Temp\ksohtml\wps3EAC.tmp.png file:///C:\Users\郭曉娟\AppData\Local\Temp\ksohtml\wps3EBC.tmp.png 3、開始寫驅動程序。 [plain] view plain copy print?file:///C:\Users\郭曉娟\AppData\Local\Temp\ksohtml\wps3ECD.tmp.png 1. #include 2. 3. #include 4. 5. #include 6. 7. #include 8. 9. #include 10. 11. #include 12. 13. #include 14. 15. #include 16. 17. #define DEV_NAME "test-dev" 18. 19. //定義蜂鳴器配置IO的地址 20. 21. #define GPD0CON 0x114000A0 22. 23. volatile unsigned long *bell_config = NULL ; 24. 25. volatile unsigned long *bell_dat = NULL ; 26. 27. int bell_open(struct inode *inode, struct file *filp) 28. 29. { 30. 31. printk("bell_open\n"); 32. 33. //清寄存器 34. 35. *bell_config &= ~(0xf); 36. 37. //設置io為輸出 38. 39. *bell_config |= (0x1); 40. 41. return 0; 42. 43. } 44. 45. 46. 47. int bell_close(struct inode *inode, struct file *filp) 48. 49. { 50. 51. printk("bell_close\n"); 52. 53. //關閉蜂鳴器 54. 55. *bell_dat &= ~0x1 ; 56. 57. return 0; 58. 59. } 60. 61. 62. 63. long bell_ioctl(struct file *filp, unsigned int request, unsigned long arg) 64. 65. { 66. 67. //控制蜂鳴器的狀態(tài) 68. 69. switch(request) 70. 71. { 72. 73. case 0: 74. 75. printk(KERN_EMERG"bell on\n"); 76. 77. *bell_dat |= 0x1 ; 78. 79. break; 80. 81. 82. 83. case 1: 84. 85. printk(KERN_EMERG"bell off\n"); 86. 87. *bell_dat &=~0x1 ; 88. 89. break; 90. 91. } 92. 93. return 0 ; 94. 95. } 96. 97. 98. 99. struct file_operations fops = { 100. 101. .owner = THIS_MODULE , 102. 103. .open = bell_open, 104. 105. .release = bell_close, 106. 107. .unlocked_ioctl = bell_ioctl, 108. 109. }; 110. 111. 112. 113. int major ; 114. 115. int test_init(void) 116. 117. { 118. 119. printk("bell_init\n"); 120. 121. //注冊設備 122. 123. major = register_chrdev(major, DEV_NAME, &fops); 124. 125. //映射IO 126. 127. bell_config = (volatile unsigned long *)ioremap(GPD0CON , 16); 128. 129. //加4個字節(jié)偏移到GP0DAT順便映射該物理地址 130. 131. bell_dat = bell_config + 1 ; 132. 133. return 0; 134. 135. } 136. 137. 138. 139. void test_exit(void) 140. 141. { 142. 143. printk("bell_exit\n"); 144. 145. //解除注冊 146. 147. unregister_chrdev(major, DEV_NAME); 148. 149. //取消映射 150. 151. iounmap(bell_config); 152. 153. } 154. 155. 156. 157. module_init(test_init); 158. 159. module_exit(test_exit); 160. 161. 162. 163. MODULE_LICENSE("GPL"); 164. 165. MODULE_AUTHOR("Y.X.YANG"); 166. 167. MODULE_VERSION("2016.1.16"); 168. 4、寫測試程序 [plain] view plain copy print?file:///C:\Users\郭曉娟\AppData\Local\Temp\ksohtml\wps3EDE.tmp.png 1. #include 2. 3. #include 4. 5. #include 6. 7. #include 8. 9. 10. 11. int main(int argc, char **argv) 12. 13. { 14. 15. int fd; 16. 17. //打開設備 18. 19. fd = open("/dev/test-dev",O_RDWR) ; 20. 21. if(-1 == fd) 22. 23. { 24. 25. printf("open fair!\n"); 26. 27. return -1 ; 28. 29. } 30. 31. while(1){ 32. 33. //打開蜂鳴器 34. 35. ioctl(fd,1); 36. 37. sleep(1); 38. 39. //關閉蜂鳴器 40. 41. ioctl(fd,0); 42. 43. sleep(1); 44. 45. } 46. 47. return 0; 48. 49. } 50. 5、編寫makefile [plain] view plain copy print?file:///C:\Users\郭曉娟\AppData\Local\Temp\ksohtml\wps3EEE.tmp.png 1. obj-m += bell.o 2. 3. 4. 5. ROOTFS = /disk/A9/filesystem 6. 7. KERNEL = /disk/A9/linux-3.5/ 8. 9. all: 10. 11. make -C $(KERNEL) M=`pwd` modules 12. 13. 14. 15. clean: 16. 17. make -C $(KERNEL) M=`pwd` clean 18. 19. rm -rf my_bell 20. 21. 22. 23. install: 24. 25. make -C $(KERNEL) M=`pwd` modules_install INSTALL_MOD_PATH=$(ROOTFS) 26. 27. 28. 29. my_bell: 30. 31. arm-linux-gcc my_bell.c -o my_bell 32. 33. 34. |