无网不进  
软硬件开发

本次测试针对GPIO1进行,挑选了GP1[31],引脚的复用默认的就是GPIO

还是老规矩,贴上driver.c,Makefile,test.c:

dm8168_gpio.c:

 

[javascript] view plain copy
 
  1. #include <linux/module.h>  
  2. #include <linux/kernel.h>  
  3. #include <linux/fs.h>  
  4. #include <linux/uaccess.h> /* copy_to_user,copy_from_user */  
  5. #include <linux/miscdevice.h>  
  6. #include <linux/device.h>  
  7. #include <asm/io.h>  
  8.   
  9. static struct class  *gpio_class;  
  10.   
  11. volatile unsigned long *DIR;  
  12. volatile unsigned long *DAT;  
  13.   
  14. int gpio_open (struct inode *inode,struct file *filp)  
  15.   
  16. {  
  17.     *DIR = 0x0;  //output  
  18.     return 0;  
  19. }  
  20.   
  21. ssize_t gpio_read (struct file *filp, char __user *buf, size_t count,loff_t *f_pos)  
  22. {  
  23.     return 0;  
  24. }  
  25.   
  26. ssize_t gpio_write (struct file *filp, const char __user *buf, size_t count,loff_t *f_pos)  
  27. {  
  28.     char val_buf[2];  
  29.     int ret;  
  30.     ret = copy_from_user(val_buf,buf,count);  
  31.           
  32.     switch(val_buf[0])  
  33.     {  
  34.         case 0x31 :  
  35.             *DAT = 0xf0000000;  //gp1[31]  
  36.             break;  
  37.         case 0x30 :  
  38.             *DAT = 0x0;         //gp1[31]  
  39.             break;  
  40.         default :  
  41.             break;  
  42.     }  
  43.     return count;  
  44. }  
  45.   
  46. struct file_operations gpio_fops =  
  47. {  
  48.     .owner   = THIS_MODULE,  
  49.     .open    = gpio_open,  
  50.     .read    = gpio_read,  
  51.     .write   = gpio_write,  
  52. } ;  
  53.   
  54. int major;  
  55. int gpio_init (void)  
  56. {     
  57.       
  58.     major = register_chrdev(0,"DM8168_gpio",&gpio_fops);  
  59.     gpio_class = class_create(THIS_MODULE, "DM8168_gpio");  
  60.     device_create(gpio_class,NULL,MKDEV(major,0),NULL,"gpio");  
  61.   
  62.     DIR = (volatile unsigned long *)ioremap(0x4804C134,4);  
  63.     DAT = (volatile unsigned long *)ioremap(0x4804C13C,4);  
  64.   
  65.     printk ("gpio is ready\n");  
  66.     return 0;  
  67. }  
  68.   
  69. void gpio_exit (void)  
  70. {  
  71.     unregister_chrdev(major,"DM8168_gpio");  
  72.     device_destroy(gpio_class,MKDEV(major,0));  
  73.     class_destroy(gpio_class);  
  74.   
  75.     iounmap(DIR);  
  76.     iounmap(DAT);  
  77.   
  78.     printk ("module exit\n");  
  79.     return ;  
  80. }  
  81.   
  82. MODULE_LICENSE("GPL");  
  83. module_init(gpio_init);  
  84. module_exit(gpio_exit);  

 

 

Makefile:

 

[javascript] view plain copy
 
  1. obj-m:= dm8168_gpio.o  
  2.   
  3. CROSSCOMPILE := /opt/codesourcery/arm-2009q1/bin/arm-none-linux-gnueabi-  
  4.   
  5. CC  := $(CROSSCOMPILE)gcc   
  6.   
  7. KDIR:=/home/***/ti-ezsdk_dm816x-evm_5_03_01_15/board-support/linux-2.6.37-psp04.00.01.13.patch2  
  8.   
  9. PWD :=$(shell pwd)  
  10.   
  11. default:  
  12.     $(MAKE) -C $(KDIR) M=$(PWD) modules ARCH=arm CROSS_COMPILE=arm-none-linux-gnueabi-  
  13. clean:  
  14.     rm -rf *.ko *.o .*cmd *.mod.c .tmp_versions   


gpio_test:

 

 

[javascript] view plain copy
 
  1. #include <stdio.h>  
  2. #include <stdlib.h>  
  3. #include <fcntl.h>  
  4.   
  5. int main(int argc, char *argv[])  
  6. {  
  7.     int  fd;  
  8.     int  val=0;  
  9.   
  10.     fd=open("/dev/gpio",O_RDWR);  
  11.   
  12.     if(fd<0)  
  13.     {  
  14.         printf("open device failed !\n");  
  15.         exit(1);  
  16.     }  
  17.     else  
  18.     {  
  19.         printf("open success ! \n");  
  20.     }  
  21.       
  22.     write(fd,argv[1],1);  
  23.       
  24.     close(fd);  
  25.     return 0;  
  26. }  


测试 :

 

 

模块编译后加载:insmod dm8168_gpio.ko

交叉编译测试程序:arm-none-linux-gnueabi-gcc -o gpio_test gpio_test.c

运行:./gpio_test 1

GP1[31]为3.3V

 

运行:./gpio_test 0

GP1[31]为0V

posted on 2018-01-10 13:48  无网不进  阅读(119)  评论(0)    收藏  举报