zoukankan      html  css  js  c++  java
  • gpio IOCTL控制

    之前工作的时候,linux下用过GPIO的,无非就是配置输出输入模式,set/get value ,或者是gpio中断之类的,用户态配置GPIO主要是两种方式:用户态使用mmap直接将GPIO 地址映射过来,操作地址, 或者 IOCTL发命令给内核,内核来控制,最近半年都在写单片机的代码。时间久了有点忘了,最近使用都是偷懒直接使用了/sys下的设备,通过
    system("echo "out" > /sys/class/gpio/gpio47/direction"); 这种方法去配置GPIO ,程序跑起来会经常出现sh: write error: Device or resource busy 这种问题,所以感觉还是使用IOCTL来管理GPIO 口,网上找了找,http://blog.csdn.net/oyhb_1992/article/details/77227276 大概参照这位大侠的用法。。先记下来,改改代码,再继续编辑

    9月20日更新,参照网上的例子,写了些代码,期间简单学习了下IOCTL参数及幻数的用法,思路很简单,编写一个模块,里面初始化gpio,执行对于的IOCTL操作,只是简单实现了read/write gpio vlaue的功能,set output/input 因为用不到没有去实现,测试正常

    #include <linux/kernel.h>
    #include <linux/module.h>
    #include <linux/fs.h>
    #include <linux/types.h>
    #include <linux/init.h>
    #include <linux/uaccess.h> 
    //#include <linux/moduleparam.h>
    //#include <linux/slab.h>//kcalloc,kzalloc等内存分配函数
    
    //---------ioctl------------
    #include <linux/ioctl.h>
    
    //---------misc_register----
    #include <linux/miscdevice.h>
    
    //----------cdev--------------
    #include <linux/cdev.h>
    
    //----------delay-------------
    #include <linux/delay.h>
    
    //----------GPIO---------------
    #include <mach/gpio.h>
    #include <linux/gpio.h>
    #include <mach/regs-gpio.h>
    //#include <plat/gpio-cfg.h>
    
    //#define MISC_DYNAMIC_MINOR 0
    #define DEVICE_NAME "gpio_ctrl"
    //静态映射的管脚虚拟地址
    static int gpios[] = {
            NUC970_PB15,//reset gps
            NUC970_PH10,//reset 4g
            NUC970_PH13,//do1
            NUC970_PH14,//12v
            NUC970_PH15,//do2
            NUC970_PI5,
            NUC970_PI8,
            NUC970_PI9,
            NUC970_PI10,
            NUC970_PI11,
    };//4个LED
    #define GPIO_NUM        ARRAY_SIZE(gpios)
    #define GET_CMD_MAGIC 0xde              //type字段,由于字段宽度为8 bits,所以不能大于0xff
    #define SET_CMD_MAGIC 0xdf              //type字段,由于字段宽度为8 bits,所以不能大于0xff
    #define GET_CMD  _IOWR(GET_CMD_MAGIC,0,unsigned int)
    #define SET_CMD  _IOW(SET_CMD_MAGIC,0,unsigned int)           
    struct ioctl_data{
            int gpionum;
            int value;
    };
    static long gpio_ctrl_ioctl(struct file *filp, unsigned int cmd,
                    unsigned long arg)    //第二个参数是命令号,第三个参数是附加参数
    {
            int ret = 0;
    
            struct ioctl_data val;    
            switch(cmd) {
                    case SET_CMD:     //命令码:如果写规范格式如:#define LED2_OFF _IOR(‘L’,0,unsigned char) 
                            if(copy_from_user(&val, (struct ioctl_data *)arg, sizeof(struct ioctl_data))){
                                    ret = - EFAULT;
                                    return ret;
                            }
                            gpio_set_value(val.gpionum,val.value);
                            printk(DEVICE_NAME": gpio %d set value %d
    ", val.gpionum,val.value);    
                            break;
                    case GET_CMD:     //命令码:如果写规范格式如:#define LED2_ON _IOR(‘L’,1,unsigned char) 也可以把命令码放在一个头文件里,应用和驱动都包含它
                            if(copy_from_user(&val, (struct ioctl_data *)arg, sizeof(struct ioctl_data))){
                                    return -EFAULT;
                            }
    
                            val.value = gpio_get_value(val.gpionum);//
                            printk(DEVICE_NAME": gpio %d value %d
    ", val.gpionum,val.value);
                            //        retval = copy_to_user((unsigned int *)arg, &phone_num, sizeof(unsigned int));
                            if(copy_to_user((struct ioctl_data *)arg,&val,sizeof(struct ioctl_data))){
                                    return -EFAULT;
                            }
                            break;
    
                    default:
                            return -EINVAL;
            }
            return 0;
    }
    
    static struct file_operations gpio_ctrl_dev_fops = {
            .owner            = THIS_MODULE,
            .unlocked_ioctl    = gpio_ctrl_ioctl,
    };
    
    //----------------miscdevice------------------
    static struct miscdevice gpio_ctrl_dev = {
            .minor            = MISC_DYNAMIC_MINOR,
            .name            = DEVICE_NAME,
            .fops            = &gpio_ctrl_dev_fops,
    };
    //--------------------------------------------
    
    
    static int __init gpio_ctrl_dev_init(void) {
            int ret;
            int i;
            //申请gpio,只有在gpio_request后才可以调用gpio_set_value,gpio_get_value等函数
            for (i = 0; i < GPIO_NUM; i++) {
                    ret = gpio_request(gpios[i], "GPIOCTRL");//申请GPIO口
                    if (ret) {
                            printk("%s: request GPIO %d  failed, ret = %d
    ", DEVICE_NAME,
                                            gpios[i], ret);
                            return ret;
                    }
    
                    gpio_direction_output(gpios[i],1);
                    //也可以用函数gpio_direction_output(unsigned gpio, int value);
            //        gpio_set_value(led_gpios[i], 1);//初始化GPIO口的值
            }
    
            ret = misc_register(&gpio_ctrl_dev);//注册混杂设备
    
            printk(DEVICE_NAME"	initialized
    ");
    //        printk("led num is: %d
    ",LED_NUM);
            return ret;
    }
    
    static void __exit gpio_ctrl_dev_exit(void) {
            int i;
    
            for (i = 0; i < GPIO_NUM; i++) {
                    gpio_free(gpios[i]);//释放GPIO口
            }
    
            misc_deregister(&gpio_ctrl_dev);//注销设备
    }
    
    module_init(gpio_ctrl_dev_init);
    module_exit(gpio_ctrl_dev_exit);
    
    MODULE_LICENSE("GPL");
    MODULE_AUTHOR("");

    用户态代码:

    //应用程序设计
    
    #include <stdio.h>
    #include <sys/ioctl.h>
    #include <stdlib.h>
    #include <unistd.h>//read,write等等
    #include <fcntl.h>
    
    #define GET_CMD_MAGIC 0xde              //type字段,由于字段宽度为8 bits,所以不能大于0xff
    #define SET_CMD_MAGIC 0xdf              //type字段,由于字段宽度为8 bits,所以不能大于0xff
    #define GET_CMD  _IOWR(GET_CMD_MAGIC,0,unsigned int)
    #define SET_CMD  _IOW(SET_CMD_MAGIC,0,unsigned int)           
    struct ioctl_data{
            int gpionum;
            int value;
    };
    int main(int argc,char *argv[])
    {
        int fd;
        struct ioctl_data val;
        val.gpionum = 237;
        val.value = atoi(argv[1]);
        if ((fd=open("/dev/gpio_ctrl",O_RDWR /*| O_NDELAY | O_NOCTTY*/)) < 0)
        {
            printf("Open Device  failed.
    ");
            exit(1);
        }
        else
        {
            printf("Open Device  successed.
    ");
        }
    
    
            if(ioctl(fd,SET_CMD,&val)<0)//命令附加参数是atoi(argv[2])
            {
                printf("ioctl err!!
    ");     
            }
        close(fd);
    }
    
    
    
    /*应用层里ioctl函数的原型是:
    *#man ioctl 
    *int ioctl(int d, int request, ...)
    *内核驱动源码里ioctl函数的原型是:
    *long (*unlocked_ioctl) (struct file *, unsigned int, unsigned long);
    *可以知道虽然应用层是可变参数...,但是实际上应用层和驱动层的ioctl函数是对应的,
    *所以我们知道,虽然应用层是用...,表示ioctl参数,但是我们应该明白
    *应用层的ioctl函数的参数最多只能有3个
    */
  • 相关阅读:
    ETL工具主流产品
    深入了解当前ETL中用到的一些基本技术
    用C#实现通用守护进程
    80端口被system占用的问题
    Python Web 性能和压力测试 multi-mechanize
    [python]用profile协助程序性能优化
    python代码优化技巧
    转:Java学习路线图
    SecureCRT 颜色
    深度学习(Deep Learning)算法简介
  • 原文地址:https://www.cnblogs.com/yinseyingji/p/7550825.html
Copyright © 2011-2022 走看看