DM8168 GPIO driver and test program

Source: Internet
Author: User

In this test, GP1 [31] is selected for GPIO1. By default, GPIO is used for Pin multiplexing.

Stick the driver. c, Makefile, test. c:

Dm8168_gpio.c:

#include 
 
  #include 
  
   #include 
   
    #include 
    
      /* copy_to_user,copy_from_user */#include 
     
      #include 
      
       #include static struct class *gpio_class;volatile unsigned long *DIR;volatile unsigned long *DAT;int gpio_open (struct inode *inode,struct file *filp){*DIR = 0x0; //outputreturn 0;}ssize_t gpio_read (struct file *filp, char __user *buf, size_t count,loff_t *f_pos){return 0;}ssize_t gpio_write (struct file *filp, const char __user *buf, size_t count,loff_t *f_pos){char val_buf[2];int ret;ret = copy_from_user(val_buf,buf,count);switch(val_buf[0]){case 0x31 :*DAT = 0xf0000000; //gp1[31]break;case 0x30 :*DAT = 0x0; //gp1[31]break;default :break;}return count;}struct file_operations gpio_fops ={.owner = THIS_MODULE,.open = gpio_open,.read = gpio_read,.write = gpio_write,} ;int major;int gpio_init (void){ major = register_chrdev(0,"DM8168_gpio",&gpio_fops);gpio_class = class_create(THIS_MODULE, "DM8168_gpio");device_create(gpio_class,NULL,MKDEV(major,0),NULL,"gpio");DIR = (volatile unsigned long *)ioremap(0x4804C134,4);DAT = (volatile unsigned long *)ioremap(0x4804C13C,4);printk ("gpio is ready\n");return 0;}void gpio_exit (void){unregister_chrdev(major,"DM8168_gpio");device_destroy(gpio_class,MKDEV(major,0));class_destroy(gpio_class);iounmap(DIR);iounmap(DAT);printk ("module exit\n");return ;}MODULE_LICENSE("GPL");module_init(gpio_init);module_exit(gpio_exit);
      
     
    
   
  
 


Makefile:

obj-m:= dm8168_gpio.oCROSSCOMPILE := /opt/codesourcery/arm-2009q1/bin/arm-none-linux-gnueabi-CC  := $(CROSSCOMPILE)gcc KDIR:=/home/***/ti-ezsdk_dm816x-evm_5_03_01_15/board-support/linux-2.6.37-psp04.00.01.13.patch2PWD :=$(shell pwd)default:$(MAKE) -C $(KDIR) M=$(PWD) modules ARCH=arm CROSS_COMPILE=arm-none-linux-gnueabi-clean:rm -rf *.ko *.o .*cmd *.mod.c .tmp_versions 

Gpio_test:

#include 
 
  #include 
  
   #include 
   
    int main(int argc, char *argv[]){int  fd;int  val=0;fd=open("/dev/gpio",O_RDWR);if(fd<0){printf("open device failed !\n");exit(1);}else{printf("open success ! \n");}write(fd,argv[1],1);close(fd);return 0;}
   
  
 

Test:

Module loaded after compilation: insmod dm8168_gpio.ko

Cross-compilation test program: arm-none-linux-gnueabi-gcc-o gpio_test gpio_test.c

Run:./gpio_test 1

GP1 [31] is 3.3 V

Run:./gpio_test 0

GP1 [31] is 0 V




Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.