和菜鸟一起学android4.0.3源码之vibrator振动器移植心得

转载:http://blog.csdn.net/eastmoon502136/article/details/7909688

手机都是有震动的效果的,前天飞刀从手机里拆了一个振动器,然后让我下周把这个调一下,昨天来公司小试了一把,就搞定了。下面把过程讲一下吧。

       其实android中已经做好了底层的驱动,那便是timed_gpio,就是把定时功能和gpio的功能结合在一起了,振动就是一个小直流电机了,当gpio口是高电平的时候,电机就转动了,当gpio口为低电平的时候,电机就不转了,而time是控制转的时间,也就是gpio口处于高电平的时间。

       具体的代码就在/drivers/staging/android/timed_gpio.c

       在相关平台的platform.c中加入platform device就可以了。

 

    1. static struct timed_gpio vibrator =   
    2.   
    3. {  
    4.   
    5.        .name = “vibrator”,  
    6.   
    7.        .gpio = 61, //对应自己平台的gpio号  
    8.   
    9.        .max_timeout = 100000,  
    10.   
    11.        .active_low = 0;  
    12.   
    13. };  
    14.   
    15.    
    16.   
    17. static  struct timed_gpio_platform_data timed_gpio_data =   
    18.   
    19. {  
    20.   
    21.        .num_gpios = 1,  
    22.   
    23.        .gpios = &vibrator,  
    24.   
    25. };  
    26.   
    27.    
    28.   
    29. static struct platform_device my_timed_gpio =   
    30.   
    31. {  
    32.   
    33.        .name = “timed-gpio”,  
    34.   
    35.        .id = -1,  
    36.   
    37.        .dev =   
    38.   
    39.        {  
    40.   
    41.              .platform_data = &timed_gpio_data,  
    42.   
    43.        },  
    44.   
    45. }; 
    46. 然后在make menuconfig中选上device下的staging下的android中的相关选项

       

       

             然后就可以跑一下内核来了,当内核跑起来后,就可以测试了。

             因为timed gpio驱动程序为每个设备在/sys/class/timed_output/目录下建立一个子

      录,设备子目录的enable文件就是控制设备的时间的。因为在platform中名称为vibrator

      所以,用以下命令可以测试:

      1. echo 10000 > /sys/class/timed_output/vibrator/enable   


              然后可以看下振动器在转了,也可以用示波器或者万用表来验证

              接着可以

      1. cat /sys/class/timed_output/vibrator/enable  


              发现enable的值一直在变小,直到为0的时候停止了转动了。

      OK,底层驱动好了,那么android上层就好办多了,因为android上层几乎和平台关系不大,要改的东西很少很少。

              至于android硬件抽象层,在hardware/libhardware_legacy/include/hardware_legacy/ vibrator目录下。

       

      1. #include <hardware_legacy/vibrator.h>  
      2.   
      3. #include "qemu.h"  
      4.   
      5.    
      6.   
      7. #include <stdio.h>  
      8.   
      9. #include <unistd.h>  
      10.   
      11. #include <fcntl.h>  
      12.   
      13. #include <errno.h>  
      14.   
      15.    
      16.   
      17. #define THE_DEVICE "/sys/class/timed_output/vibrator/enable"  
      18.   
      19.    
      20.   
      21. int vibrator_exists()  
      22.   
      23. {  
      24.   
      25.     int fd;  
      26.   
      27.    
      28.   
      29. #ifdef QEMU_HARDWARE  
      30.   
      31.     if (qemu_check()) {  
      32.   
      33.         return 1;  
      34.   
      35.     }  
      36.   
      37. #endif  
      38.   
      39.    
      40.   
      41.     fd = open(THE_DEVICE, O_RDWR);  
      42.   
      43.     if(fd < 0)  
      44.   
      45.         return 0;  
      46.   
      47.     close(fd);  
      48.   
      49.     return 1;  
      50.   
      51. }  
      52.   
      53.    
      54.   
      55. static int sendit(int timeout_ms)  
      56.   
      57. {  
      58.   
      59.     int nwr, ret, fd;  
      60.   
      61.     char value[20];  
      62.   
      63.    
      64.   
      65. #ifdef QEMU_HARDWARE  
      66.   
      67.     if (qemu_check()) {  
      68.   
      69.         return qemu_control_command( "vibrator:%d", timeout_ms );  
      70.   
      71.     }  
      72.   
      73. #endif  
      74.   
      75.    
      76.   
      77.     fd = open(THE_DEVICE, O_RDWR);  
      78.   
      79.     if(fd < 0)  
      80.   
      81.         return errno;  
      82.   
      83.    
      84.   
      85.     nwr = sprintf(value, "%d\n", timeout_ms);  
      86.   
      87.     ret = write(fd, value, nwr);  
      88.   
      89.    
      90.   
      91.     close(fd);  
      92.   
      93.    
      94.   
      95.     return (ret == nwr) ? 0 : -1;  
      96.   
      97. }  
      98.   
      99.    
      100.   
      101. int vibrator_on(int timeout_ms)  
      102.   
      103. {  
      104.   
      105.     /* constant on, up to maximum allowed time */  
      106.   
      107.     return sendit(timeout_ms);  
      108.   
      109. }  
      110.   
      111.    
      112.   
      113. int vibrator_off()  
      114.   
      115. {  
      116.   
      117.     return sendit(0);  
      118.   
      119. }  


       

              看到了吧

      1. #define THE_DEVICE "/sys/class/timed_output/vibrator/enable"  


              就是我们要操作的底层驱动的地方,只要这个和驱动配上,那么剩下的事情就木有了,直接搞定了。

              其实她也是往这里写数据,androidjava层就不关心她了。好了,然后可以在android启动后设置一个闹钟来测试下了,发现可以,至此androidvibrator移植成功。

              突然发现了,其实以前觉得很难得东西,很不好理解的东西,在过一段时间后再回过头去看的时候才会恍然大悟。学习是个漫长的过程,是一个知识慢慢积累的过程,一口气是吃不成胖子的

 

posted @ 2013-07-06 17:57  云翔世界  阅读(400)  评论(0)    收藏  举报