Chinaunix首页 | 论坛 | 博客
  • 博客访问: 168514
  • 博文数量: 109
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 147
  • 用 户 组: 普通用户
  • 注册时间: 2015-01-23 16:12
文章分类

全部博文(109)

文章存档

2015年(109)

我的朋友

分类: LINUX

2015-01-29 16:41:36

原文地址:Linux下AM335X的GPIO控制 作者:futter521


本文章转自:
1. GPIO
char型驱动,这里主要就是点个灯,感受一下驱动的设计和硬件的控制驱动程序:

  1. #include <linux/init.h>
  2. #include <linux/module.h>
  3. #include <linux/leds.h>
  4. #include <linux/io.h>
  5. #include <linux/semaphore.h>
  6. #include <linux/kernel.h>
  7. #include <linux/cdev.h>
  8. #include <linux/types.h>
  9. #include <linux/fs.h>
  10. #include <mach/gpio.h>
  11. #include <plat/mux.h>
  12. #include <linux/gpio.h>
  13. /*******************************************/
  14. #define NAME "leds"
  15. #define GPIO_TO_PIN(bank, gpio) (32 * (bank) + (gpio))
  16. static int major =251;//定义主设备号
  17. /*******************************************/
  18. void led_on(void)
  19. {
  20.      gpio_set_value(GPIO_TO_PIN(1,22), 1);
  21. }

  22. void led_off(void)
  23. {
  24.      gpio_set_value(GPIO_TO_PIN(1,22), 0);
  25. }

  26. void led_init(void)
  27. {
  28.         int result;
  29.          /* Allocating GPIOs and setting direction */
  30.         result = gpio_request(GPIO_TO_PIN(1,22), "Leds");//usr1
  31.         if (result != 0)
  32.                 printk("gpio_request(1_22) failed!\n");
  33.         result = gpio_direction_output(GPIO_TO_PIN(1,22), 1);
  34.         if (result != 0)
  35.                 printk("gpio_direction(1_22) failed!\n");
  36. }

  37. struct light_dev
  38. {
  39.     struct cdev cdev;
  40.     unsigned char value;
  41. };

  42. struct light_dev *light_devp;

  43. MODULE_AUTHOR("chenzhufly");
  44. MODULE_LICENSE("Dual BSD/GPL");

  45. // 打开和关闭函数
  46. int light_open(struct inode *inode,struct file *filp)
  47. {
  48.     struct light_dev *dev;

  49.     // 获得设备结构体指针
  50.     dev = container_of(inode->i_cdev,struct light_dev,cdev);
  51.     // 让设备结构体作为设备的私有信息
  52.     filp->private_data = dev;

  53.     return 0;
  54. }

  55. int light_release(struct inode *inode,struct file *filp)
  56. {
  57.     return 0;
  58. }


  59. // ioctl
  60. int light_ioctl(struct file *filp,unsigned int cmd,
  61. unsigned long arg)
  62. {
  63.     struct light_dev *dev = filp->private_data;

  64.     switch(cmd)
  65.     {
  66.         case 0:
  67.             dev->value = 0;
  68.             led_off();
  69.         break;

  70.         case 1:
  71.             dev->value = 1;
  72.             led_on();
  73.         break;

  74.         default:

  75.             return -ENOTTY;
  76.         // break;
  77.     }

  78.     return 0;
  79. }

  80. struct file_operations light_fops =
  81. {
  82.     .owner = THIS_MODULE,
  83.     .unlocked_ioctl = light_ioctl,
  84.     .open = light_open,
  85.     .release = light_release,
  86. };
  87. // 模块加载函数
  88. int light_init(void)
  89. {
  90.     int ret;
  91.     led_init();
  92.     printk(KERN_ALERT "led modules is install\n");
  93.     ret=register_chrdev(major,NAME,&light_fops);
  94.     if(ret<0)
  95.     {
  96.       printk("unable to register myled driver!\n");
  97.       return ret;
  98.      }
  99.     return 0;
  100.     
  101. }

  102. // 模块卸载函数
  103. void light_cleanup(void)
  104. {
  105.     unregister_chrdev(major,NAME);
  106.     printk("Goodbye,cruel world!\n");
  107. }

  108. module_init(light_init);
  109. module_exit(light_cleanup);
应用程序:


  1. #include <stdlib.h>
  2. #include <sys/types.h>
  3. #include <sys/stat.h>
  4. #include <fcntl.h>
  5. #include <linux/input.h>
  6. #include <unistd.h>
  7. #include <sys/ioctl.h>

  8. int main(int argc, char * argv)
  9. {
  10.     int i, n, fd;

  11.     fd = open("/dev/leds", O_RDWR);
  12.         if (fd < 0)
  13.         {
  14.             printf("can't open /dev/leds!\n");
  15.             exit(1);
  16.         }

  17.         while (1) {
  18.             ioctl(fd, 1, 1);
  19.             sleep(1);

  20.             ioctl(fd, 0, 1);
  21.             sleep(1);
  22.         }

  23.     close(fd);

  24.     return 0;
  25. }
Makefile文件:


  1. ARCH=arm
  2. CROSS_COMPILE=/home/chenzhufly/beaglebone/linux-devkit/bin/arm-arago-linux-gnueabi-
  3. obj-m := leds.o
  4. KDIR := /home/chenzhufly/beaglebone/board-support/linux-3.1.0-psp04.06.00.03.sdk
  5. PWD := $(shell pwd)
  6. default:
  7.         make -C $(KDIR) M=$(PWD) ARCH=$(ARCH) CROSS_COMPILE=$(CROSS_COMPILE) modules
  8. app: leds_test.c
  9.         $(CROSS_COMPILE)gcc -o leds_test leds_test.c
  10. clean:
  11.         $(MAKE) -C $(KDIR) M=$(PWD) clean
leds.sh脚本

  1. insmod leds.ko
  2. mknod /dev/leds c 251 0
  3. ./leds_test
2. 使用echo命令,这个我在前面也说过

  1. 点亮usr1
  2. root@beaglebone:~# echo 1 >
  3. /sys/class/leds/beaglebone::usr1/brightness
  4. 关闭usr1
  5. root@beaglebone:~# echo 0 > /sys/class/leds/beaglebone::usr1/brightness

3. 做个应用程序实现流水灯功能吧

  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <sys/types.h>
  4. #include <sys/ipc.h>
  5. #include <sys/ioctl.h>
  6. #include <fcntl.h>
  7. #define LED1 "/sys/class/leds/beaglebone::usr1/brightness" // usr1 led
  8. #define LED2 "/sys/class/leds/beaglebone::usr2/brightness" // usr2 led
  9. #define LED3 "/sys/class/leds/beaglebone::usr3/brightness" // usr3 led

  10. int main(void)
  11. {
  12.     int f_led1 = open(LED1, O_RDWR);
  13.     int f_led2 = open(LED2, O_RDWR);
  14.     int f_led3 = open(LED3, O_RDWR);
  15.     
  16.     unsigned char dat1, dat2, dat3;
  17.     unsigned char i = 0;
  18.    
  19.     if (f_led1 < 0)
  20. {
  21. printf("error in open %s",LED1);
  22.         return -1;
  23.     }
  24.     if (f_led2 < 0)
  25.     {
  26.            printf("error in open %s",LED2);
  27.         return -1;
  28.     }
  29.     if (f_led3 < 0)
  30.     {
  31.            printf("error in open %s",LED3);
  32.         return -1;
  33.     }
  34.     //add 10 times
  35.     for(i=1; i<30; i++)
  36.     {
  37.     dat1 = ((i%3) == 1) ? '1' : '0';
  38.     dat2 = ((i%3) == 2) ? '1' : '0';
  39.     dat3 = ((i%3) == 0) ? '1' : '0';
  40.     write(f_led1, &dat1, sizeof(dat1));
  41.         write(f_led2, &dat2, sizeof(dat2));
  42.         write(f_led3, &dat3, sizeof(dat3));
  43.         usleep(300000);
  44.     }
  45.     // all the bright
  46.     {
  47.         dat1 = '1';
  48.         dat2 = '1';
  49.         dat3 = '1';
  50.         write(f_led1, &dat1, sizeof(dat1));
  51.         write(f_led2, &dat2, sizeof(dat2));
  52.         write(f_led3, &dat3, sizeof(dat3));
  53.     }
  54. }


阅读(3249) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~