android下调试3G之gpio控制3G上电【转】

来源:互联网 发布:js实现搜索显示和隐藏 编辑:程序博客网 时间:2024/05/20 03:48

来自:http://blog.csdn.net/hanbo622/article/details/40656011


 如果是自己开发的板子,需要用GPIO引脚控制3G模块开机/关机时,下面的文章会对你有所帮助,是以处理器IMX6和中兴MG3732模块为例介绍。

一、引脚连接

       处理器的gpio3_GPIO[9]连接3G模块的ON/OFF(29)引脚来控制3G的开机/关机。

二、开关机条件

       给ON/OFF引脚连续2500~3500毫秒低电平则开机,给再ON/OFF引脚连续2500~3500毫秒低电平则关机。

三、gpio驱动程序

       gpio_3g.c 

[cpp] view plain copy
  1. #include<linux/module.h>  
  2. #include<linux/kernel.h>  
  3. #include<linux/init.h>  
  4. #include<linux/fs.h>  
  5. #include<linux/cdev.h>  
  6. #include<linux/device.h>  
  7. #include<asm/io.h>  
  8. #include<linux/mm.h>  
  9. #include<asm/uaccess.h>  
  10. #include<linux/gpio.h>  
  11.   
  12. #define GPIO_IOF_MAGIC 0xd0  
  13. #define 3G_INPUT        _IO(GPIO_IOF_MAGIC,8)  
  14. #define 3G_OUTPUT       _IO(GPIO_IOF_MAGIC,9)  
  15. #define 3G_ON_OFF       IMX_GPIO_NR(3,9)    //控制引脚为:gpio3[9]  
  16.   
  17. MODULE_LICENSE ("GPL");  
  18. int gpio_major=2555;  
  19. int gpio_minor=0;  
  20. int number_devices=1;  
  21. struct cdev gpiocdev;  
  22. struct class *my_class;  
  23.   
  24. static int gpio_open(struct inode *inode,struct file *file)  
  25. {  
  26.     printk (KERN_INFO "GPIO device opened\n");  
  27.       
  28.     if(gpio_direction_output(3G_ON_OFF,0x00)<0){  
  29.         printk("3G_ON_OFF set direction failed\n");  
  30.     }    
  31.     return 0;  
  32. }  
  33.   
  34. static long gpio_ioctl(struct file *file,unsigned int cmd,unsigned long arg)  
  35. {  
  36.     int val;  
  37.     printk("GPIO:ioctl running....\n");  
  38.       
  39.     switch(cmd){  
  40.         case 3G_INPUT:  
  41.             if(gpio_direction_input(3G_ON_OFF)<0){  
  42.                 printk("3G_ON_OFF set input failed\n");  
  43.             }  
  44.             if(arg>0){  
  45.                 gpio_set_value(3G_ON_OFF,0x01);  
  46.             }else{  
  47.                 gpio_set_value(3G_ON_OFF,0x00);  
  48.             }  
  49.             val=gpio_get_value(3G_ON_OFF);  
  50.             printk("3G_ON_OFF ioctl val=%d\n",val);  
  51.         break;  
  52.         case 3G_OUTPUT:  
  53.             if(gpio_direction_output(3G_ON_OFF,0x00)<0){  
  54.                 printk("3G_ON_OFF set direction failed\n");  
  55.             }  
  56.             if(arg>0){  
  57.                 gpio_set_value(3G_ON_OFF,0x01);  
  58.             }else{  
  59.                 gpio_set_value(3G_ON_OFF,0x00);  
  60.             }  
  61.             val=gpio_get_value(3G_ON_OFF);  
  62.             printk("3G_ON_OFF ioctl val=%d\n",val);  
  63.         break;        
  64.         default:  
  65.             printk("cmd is not exist\n");  
  66.             return -1;  
  67.     }  
  68.     return val;  
  69. }  
  70.   
  71.   
  72. static int gpio_release (struct inode *inode, struct file *file)  
  73. {  
  74.     printk (KERN_INFO "GPIO device closed\n");    
  75.     return 0;  
  76. }  
  77.   
  78. struct file_operations gpio_fops = {  
  79.     .owner=THIS_MODULE,  
  80.     .open=gpio_open,  
  81.     .release=gpio_release,  
  82.     .unlocked_ioctl=gpio_ioctl,  
  83. };  
  84.   
  85. static void char_reg_setup_cdev (void)  
  86. {  
  87.     int error;  
  88.     dev_t devno;  
  89.     devno=MKDEV(gpio_major,gpio_minor);  
  90.     cdev_init(&gpiocdev,&gpio_fops);  
  91.     gpiocdev.owner=THIS_MODULE;  
  92.     error=cdev_add(&gpiocdev,devno,1);  
  93.     if (error)  
  94.         printk(KERN_NOTICE "Error %d adding char device GPIO",error);  
  95. }  
  96.   
  97. static int __init gpio_init (void)  
  98. {  
  99.     int result;  
  100.     dev_t devno;  
  101.   
  102.     devno=MKDEV(gpio_major,gpio_minor);  
  103.   
  104.     if(gpio_major){  
  105.         result=register_chrdev_region(devno,number_devices,"gpio_3g");  
  106.     }else{  
  107.         result=alloc_chrdev_region(&devno,0,number_devices,"gpio_3g");  
  108.         gpio_major=MAJOR(devno);  
  109.     }  
  110.     if (result<0){  
  111.         printk(KERN_WARNING "GPIO:can't get major number %d\n", gpio_major);  
  112.         return result;  
  113.     }  
  114.     char_reg_setup_cdev ();   
  115.     my_class=class_create(THIS_MODULE,"GPIOCLASS");  
  116.     device_create(my_class,NULL,MKDEV(gpio_major,gpio_minor),NULL,"gpio_3g");  
  117.   
  118.     if(gpio_request(3G_ON_OFF,"UnKnow")<0){  
  119.         printk("3G_ON_OFF request failed\n");  
  120.     }  
  121.     printk (KERN_INFO "char device GPIO registered\n");  
  122.   
  123.     return 0;  
  124. }  
  125.   
  126. static void __exit gpio_exit(void)  
  127. {  
  128.     dev_t devno=MKDEV(gpio_major,gpio_minor);  
  129.     gpio_free(3G_ON_OFF);  
  130.   
  131.     cdev_del(&gpiocdev);  
  132.     unregister_chrdev_region(devno,number_devices);  
  133.     device_destroy(my_class,devno);  
  134.     class_destroy(my_class);  
  135.     printk(KERN_INFO"char device GPIO  unregister\n");  
  136. }  
  137.   
  138. module_init (gpio_init);  
  139. module_exit (gpio_exit);  


四、内核添加gpio驱动步骤(android4.3源码)

      1、把gpio_3g.c放到~/myandroid/kernel_imx/drivers/char目录下(假设源码在~/myandroid下)。

      2、~/myandroid/kernel_imx/drivers/char/Konfig中最后添加红色框部分语句。

           

      3、在~/myandroid/kernel_imx/drivers/char/Makefile中添加红色框部分语句。

           

      4、在~/myandroid/kernel_imx下 make  menuconfig 来配置内核,配置完成后退出并保存。如下图:

            



           

      5、在进入~/myandroid/kernel_imx目录执行:

            cp  .config   arch/arm/configs/imx6_android_defconfig

            到此后,行以下命令编译:

             source build/enevsetup.sh

             lunch sabresb_6dq-user

             make

 

五、编译3G模块开关机程序

      1、程序代码

       3g_on_off.c  

[cpp] view plain copy
  1. #include<stdio.h>  
  2. #include<unistd.h>  
  3. #include<fcntl.h>  
  4. #include<errno.h>  
  5. #include<sys/ioctl.h>  
  6. #include<sys/types.h>  
  7.   
  8. #define 3G_INPUT     _IO(GPIO_IOF_MAGIC,8)  
  9. #define 3G_OUTPUT    _IO(GPIO_IOF_MAGIC,9)  
  10.   
  11. #define SET_ON           0x01  
  12. #define SET_OFF          0x00  
  13.   
  14. int main(int argc, char *argv[])  
  15. {  
  16.     int fd;  
  17.     fd=open("/dev/gpio_3g",O_RDWR); //打开gpio_3g设备文件  
  18.     if(fd<0){  
  19.         perror("open:");  
  20.     }  
  21.     if(ioctl(fd,3G_OUTPUT,SET_ON)<0){ //设置该引脚为输出高  
  22.         perror("3G_OUTPUT ioctl:");  
  23.     }  
  24.     sleep(3);  
  25.     if(ioctl(fd,3G_OUTPUT,SET_OFF)<0){ //设置该引脚为输出低  
  26.         perror("3G_OUTPUT ioctl:");  
  27.     }  
  28.       
  29.     if(ioctl(fd,3G_INPUT,SET_OFF)<0){ //设置该引脚为输入,防止被意外篡改  
  30.         perror("3G_INPUT ioctl:");  
  31.     }  
  32.     close(fd);  
  33.     return 0;  
  34. }  

       Android.mk   

[cpp] view plain copy
  1. LOCAL_PATH  :=$(call my-dir)  
  2. include $(CLEAR_VARS)  
  3. LOCAL_MODULE_TAGES  :=optional  
  4. LOCAL_MODULE    :=3g_on_off  
  5. LOCAL_SRC_FILES :=3g_on_off.c  
  6. include $(BUILD_EXECUTABLE)  

     

      2、在~/myandroid/external下创建 3g_on_off文件夹

      3、把 3g_on_off.c 和 Android.mk 文件放到新建文件夹下。

      4、执行以下命令编译:

            source  build/enevsetup.sh

            lunch  sabresb_6dq-user

            mmm  external/3g_on_off

      5、把生成的可执行程序:

            ~/myandroid/out/target/product/sabresd_6dq/obj/EXECUTABLES/3g_on_off_intermediates/3g_on_off放到

            ~/myandroid/out/target/product/sabresd_6dq/system里面

            执行:make  snod   打包

六、烧写镜像到开发板,执行3g_on_off程序对3G模块进行开关机操作。

      3G上电后,连接板子的调试串口终端会输出以下红色框的信息,此时表明已识别USB串口设备

      



0 0
原创粉丝点击