九鼎创展论坛中文版English
登录 | 立即注册 设为首页收藏本站 切换到宽版
查看: 7754|回复: 3
打印 上一主题 下一主题

x6818开发板安卓驱动开发(1)-LED-driver

[复制链接]
跳转到指定楼层
楼主
发表于 2017-5-6 14:13:28 | 只看该作者 |只看大图 回帖奖励 |倒序浏览 |阅读模式
本帖最后由 redstone 于 2017-5-10 16:00 编辑

参考了 http://bbs.9tripod.com/forum.php?mod=viewthread&tid=27392&highlight=%E9%A9%B1%E5%8A%A8
通过sysfs,即kobject实现LED灯的控制。
x6818_led_drv.c:
  1. #include <linux/module.h>
  2. #include <linux/init.h>
  3. #include <linux/platform_device.h>
  4. #include <asm/mach-types.h>
  5. #include <linux/gpio.h>
  6. #include <mach/platform.h>
  7. #include <mach/soc.h>

  8. #define X6818_LEDS_MODULE_NAME        "x6818-leds"

  9. /*
  10. * X6818 LED:
  11. * LED1 -> D22 -> GPIO_B26
  12. * LED2 -> D23 -> GPIOC11
  13. * LED3 -> D24 -> GPIOC7
  14. * LED4 -> D25 -> GPIOC12
  15. * 4个LED为供阳极LED,相关的GPIO电压拉低灯亮,拉高灯灭
  16. */
  17. #define X6818_LEDS_NUMBER        4
  18. #define X6818_LED1_GPIO                (PAD_GPIO_B + 26)
  19. #define X6818_LED2_GPIO                (PAD_GPIO_C + 11)
  20. #define X6818_LED3_GPIO                (PAD_GPIO_C + 7)
  21. #define X6818_LED4_GPIO                (PAD_GPIO_C + 12)

  22. #define X6818_LED_ON                0
  23. #define X6818_LED_OFF                1

  24. typedef struct __led{
  25.         char * name;
  26.         unsigned gpio;
  27.         int status;
  28. }led;
  29. static led x6818_leds[X6818_LEDS_NUMBER] ={
  30.         { .name = "led1", .gpio = X6818_LED1_GPIO, .status = 0},
  31.         { .name = "led2", .gpio = X6818_LED2_GPIO, .status = 0},
  32.         { .name = "led3", .gpio = X6818_LED3_GPIO, .status = 0},
  33.         { .name = "led4", .gpio = X6818_LED4_GPIO, .status = 0},
  34. };


  35. static void __x6818_leds_probe(void)       //初始化LED对应GPIO口
  36. {
  37.         int ret = 0;
  38.         int i;
  39.         for(i = 0; i < X6818_LEDS_NUMBER; i++){
  40.                 nxp_soc_gpio_set_io_func(x6818_leds[i].gpio, NX_GPIO_PADFUNC_1); //gpio mode
  41.                 nxp_soc_gpio_set_io_dir(x6818_leds[i].gpio, 1);                 //output mode
  42.                 nxp_soc_gpio_set_io_pull_sel(x6818_leds[i].gpio, 1); // pull up select
  43.                 nxp_soc_gpio_set_io_pull_enb(x6818_leds[i].gpio, 1); // pull up  enable
  44.                 nxp_soc_gpio_set_out_value(x6818_leds[i].gpio,X6818_LED_OFF); //默认led关
  45.                 x6818_leds[i].status = 0;
  46.         }

  47. }
  48. static void __x6818_leds_remove(void)   
  49. {

  50. }
  51. static ssize_t x6818_leds_read(struct device *dev, struct device_attribute *attr, char *buf)
  52. {
  53.         int i;
  54.         for(i = 0; i < X6818_LEDS_NUMBER; i++){
  55.                 if(!strcmp(attr->attr.name, x6818_leds[i].name)){
  56.                         if(x6818_leds[i].status)
  57.                                 return strlcpy(buf, "1\n", 3);
  58.                         else
  59.                                 return strlcpy(buf, "0\n", 3);
  60.                                        
  61.                 }

  62.         }
  63.         return strlcpy(buf, "\n", 3);
  64. }
  65. static ssize_t x6818_leds_write(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  66. {
  67.         int i;
  68.         unsigned long on = simple_strtoul(buf, NULL, 10);//convert a string to an unsigned long 从应用读取需要写入的数据 10进制
  69.         for(i = 0; i < X6818_LEDS_NUMBER; i++){
  70.                 if(!strcmp(attr->attr.name, x6818_leds[i].name)){
  71.                         if(on){
  72.                                 nxp_soc_gpio_set_out_value(x6818_leds[i].gpio,X6818_LED_ON); //默认led关
  73.                                 x6818_leds[i].status = 1;
  74.                         }
  75.                         else{
  76.                                 nxp_soc_gpio_set_out_value(x6818_leds[i].gpio,X6818_LED_OFF); //默认led关
  77.                                 x6818_leds[i].status = 0;
  78.                         }
  79.                         break;
  80.                 }
  81.         }        
  82.         return count;
  83. }



  84. //kobject目录下的四个文件对应的属性,读写函数
  85. static DEVICE_ATTR(led1, 0666, x6818_leds_read, x6818_leds_write);
  86. static DEVICE_ATTR(led2, 0666, x6818_leds_read, x6818_leds_write);
  87. static DEVICE_ATTR(led3, 0666, x6818_leds_read, x6818_leds_write);
  88. static DEVICE_ATTR(led4, 0666, x6818_leds_read, x6818_leds_write);
  89. static struct attribute * x6818_leds_sysfs_entries[] = {                  //对应kobject目录下的四个文件
  90.                  &dev_attr_led1.attr,
  91.                  &dev_attr_led2.attr,
  92.                  &dev_attr_led3.attr,
  93.                  &dev_attr_led4.attr,
  94.                  NULL,
  95. };

  96. static struct attribute_group x6818_leds_attr_group = {
  97.          .name   = NULL,
  98.          .attrs  = x6818_leds_sysfs_entries,   //指定注册的kobject对应的文件属性接入点
  99. };
  100. static int x6818_leds_probe(struct platform_device *pdev)
  101. {
  102.          __x6818_leds_probe();                                       //初始化LED对应GPIO口
  103.          return sysfs_create_group(&pdev->dev.kobj, &x6818_leds_attr_group);//注册kobject
  104. }
  105. static int x6818_leds_remove(struct platform_device *pdev)
  106. {
  107.          __x6818_leds_remove();                                     //释放GPIO
  108.          sysfs_remove_group(&pdev->dev.kobj, &x6818_leds_attr_group);//注销kobject
  109.          return 0;
  110. }


  111. static struct platform_driver x6818_leds_driver = {
  112.         .probe                = x6818_leds_probe,
  113.         .remove         = x6818_leds_remove,
  114.         .driver         = {
  115.                 .name        = X6818_LEDS_MODULE_NAME,
  116.         },
  117. };
  118.                
  119. static struct platform_device x6818_leds_device = {
  120.         .name      = X6818_LEDS_MODULE_NAME,
  121.         .id        = -1,
  122. };               
  123.         
  124. static int __devinit x6818_leds_init(void)
  125. {
  126.         int ret;

  127.         printk("x6818 leds driver\r\n");

  128.         ret = platform_device_register(&x6818_leds_device);
  129.         if(ret)
  130.                 printk("failed to register x6818 leds device\n");

  131.         ret = platform_driver_register(&x6818_leds_driver);
  132.         if(ret)
  133.                 printk("failed to register x6818 leds driver\n");

  134.         return ret;
  135. }

  136. static void x6818_leds_exit(void)
  137. {
  138.         platform_device_unregister(&x6818_leds_device);
  139.         platform_driver_unregister(&x6818_leds_driver);
  140. }

  141. module_init(x6818_leds_init);
  142. module_exit(x6818_leds_exit);

  143. MODULE_LICENSE("GPL");
  144. MODULE_DESCRIPTION("x6818 leds driver");

复制代码



Makefile:
  1. KERN_DIR = /home/wzs/Android/x6818/kernel/
  2. all:
  3.         make -C $(KERN_DIR) M=`pwd` modules

  4. clean:
  5.         make -C $(KERN_DIR) M=`pwd` modules clean
  6.         rm -rf modules.order

  7. obj-m        += x6818_led_drv.o
复制代码



通过adb上传到安卓系统:
  1. adb push ./x6818_led_drv.ko /sdcard/Download
复制代码
插入驱动
  1. insmod x6818_led_drv.ko
复制代码


测试:
  1. echo 1 > /sys/devices/platform/x6818-leds/led1
复制代码


安卓app测试见:x6818开发板安卓驱动开发(1)-LED-APP



回复

使用道具 举报

板凳
发表于 2017-7-28 09:50:04 | 只看该作者
请问开发用到的软件有什么?没有买TF卡 ,能用eslipse下载到板子上么
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|手机版|小黑屋|深圳市九鼎创展科技官方论坛 ( 粤ICP备11028681号-2  

GMT+8, 2024-4-26 14:31 , Processed in 0.021663 second(s), 19 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表