16_ZYNQ7020开发板gpio输入实验

gpio输出最经典的例子就是按键,我们就用按键做一个简单的gpio输入实验。
应用程序通过read函数读取按键状态,如果按键按下,就翻转一次led电平
1)原理图
LED
在这里插入图片描述
KEY
在这里插入图片描述
LED MIO0_LED
在这里插入图片描述
KEY MIO_KEY1
在这里插入图片描述
2)设备树

/include/ "system-conf.dtsi"
/ {
    model = "Zynq ALINX Development Board";
    compatible = "alinx,zynq", "xlnx,zynq-7000";

    aliases {
        ethernet0 = "&gem0";
        serial0 = "&uart1";
    };

    usb_phy0: usb_phy@0 {
        compatible = "ulpi-phy";
        #phy-cells = <0>;
        reg = <0xe0002000 0x1000>;
        view-port = <0x0170>;
        drv-vbus;
    };
    
    amba {
        slcr@f8000000 {
            pinctrl@700 {
                pinctrl_led_default: led-default {  
                    mux {  
                        groups = "gpio0_0_grp";  
                        function = "gpio0";  
                    };  

                    conf {  
                        pins = "MIO0"; 
                        io-standard = <1>; 
                        bias-disable;  
                        slew-rate = <0>;  
                    };      
                }; 
                pinctrl_key_default: key-default {
                    mux {
                        groups = "gpio0_50_grp";
                        function = "gpio0";
                    };

                    conf {
                        pins = "MIO50";
                        io-standard = <1>;
                        bias-high-impedance;
                        slew-rate = <0>;
                    };
                };
            };
        };
    };

    alinxled {
        compatible = "alinx-led";
        pinctrl-names = "default";
        pinctrl-0 = <&pinctrl_led_default>;
        alinxled-gpios = <&gpio0 0 0>;
    };

    alinxkey {
        compatible = "alinx-key";
        pinctrl-names = "default";
        pinctrl-0 = <&pinctrl_key_default>;
        alinxkey-gpios = <&gpio0 50 0>;
    };
};

&i2c0 {
    clock-frequency = <100000>;
};

&usb0 {
    dr_mode = "host";
    usb-phy = <&usb_phy0>;
};

&sdhci0 {
    u-boot,dm-pre-reloc;
};

&uart1 {
    u-boot,dm-pre-reloc;
};

&flash0 {
    compatible = "micron,m25p80", "w25q256", "spi-flash";
};

&gem0 {
    phy-handle = <&ethernet_phy>;
    ethernet_phy: ethernet-phy@1 {
        reg = <1>;
        device_type = "ethernet-phy";
    };
};

&amba_pl {
    hdmi_encoder_0:hdmi_encoder {
        compatible = "digilent,drm-encoder";
        digilent,edid-i2c = <&i2c0>;
    };

    xilinx_drm {
        compatible = "xlnx,drm";
        xlnx,vtc = <&v_tc_0>;
        xlnx,connector-type = "HDMIA";
        xlnx,encoder-slave = <&hdmi_encoder_0>;
        clocks = <&axi_dynclk_0>;
        dglnt,edid-i2c = <&i2c0>;
        planes {
            xlnx,pixel-format = "rgb888";
            plane0 {
                dmas = <&axi_vdma_0 0>;
                dma-names = "dma";
            };
        };
    };
};

&axi_dynclk_0 {
    compatible = "digilent,axi-dynclk";
    #clock-cells = <0>;
    clocks = <&clkc 15>;
};

&v_tc_0 {
    compatible = "xlnx,v-tc-5.01.a";
};

KEY使用的IO是MIO50,所以groups="gpio_50_grp",pins=“MIO50
alinxkey-gpio=<&gpio0 50 0>输入引脚电气特性为
高阻态bias-high-impedance
3)驱动代码

#include <linux/module.h>  
#include <linux/kernel.h>  
#include <linux/fs.h>  
#include <linux/init.h>  
#include <linux/ide.h>  
#include <linux/types.h>  
#include <linux/errno.h>
#include <linux/cdev.h>
#include <linux/of.h>
#include <linux/of_address.h>
#include <linux/of_gpio.h>
#include <linux/device.h>
#include <linux/delay.h>
#include <linux/init.h>
#include <linux/gpio.h>
#include <linux/delay.h>
#include <asm/uaccess.h>
#include <asm/mach/map.h>
#include <asm/io.h>
  
/* 设备节点名称 */  
#define DEVICE_NAME       "gpio_key"
/* 设备号个数 */  
#define DEVID_COUNT       1
/* 驱动个数 */  
#define DRIVE_COUNT       1
/* 主设备号 */
#define MAJOR1
/* 次设备号 */
#define MINOR1            0

/* 把驱动代码中会用到的数据打包进设备结构体 */
struct alinx_char_dev{
    dev_t              devid;             //设备号
    struct cdev        cdev;              //字符设备
    struct class       *class;            //类
    struct device      *device;           //设备
    struct device_node *nd;               //设备树的设备节点
    int                alinx_key_gpio;    //gpio号
};
/* 声明设备结构体 */
static struct alinx_char_dev alinx_char = {
    .cdev = {
        .owner = THIS_MODULE,
    },
};

/* open函数实现, 对应到Linux系统调用函数的open函数 */  
static int gpio_key_open(struct inode *inode_p, struct file *file_p)  
{
    /* 设置私有数据 */
    file_p->private_data = &alinx_char;
    printk("gpio_test module open\n");  
    return 0;  
}  
  
  
/* write函数实现, 对应到Linux系统调用函数的write函数 */  
static ssize_t gpio_key_read(struct file *file_p, char __user *buf, size_t len, loff_t *loff_t_p)  
{  
    int ret = 0;
    /* 返回按键的值 */
	unsigned int key_value = 0;
    /* 获取私有数据 */
    struct alinx_char_dev *dev = file_p->private_data;
  
    /* 检查按键是否被按下 */
    if(0 == gpio_get_value(dev->alinx_key_gpio))
    {
        /* 按键被按下 */
        /* 防抖 */
        mdelay(50);
        /* 等待按键抬起 */
        while(!gpio_get_value(dev->alinx_key_gpio));
        key_value = 1;
    }
    else
    {
        /* 按键未被按下 */
    }
    /* 返回按键状态 */
    ret = copy_to_user(buf, &key_value, sizeof(key_value));
    
    return ret;  
}  
  
/* release函数实现, 对应到Linux系统调用函数的close函数 */  
static int gpio_key_release(struct inode *inode_p, struct file *file_p)  
{  
    printk("gpio_test module release\n");  
    return 0;  
}  
      
/* file_operations结构体声明, 是上面open、write实现函数与系统调用函数对应的关键 */  
static struct file_operations ax_char_fops = {  
    .owner   = THIS_MODULE,  
    .open    = gpio_key_open,  
    .read    = gpio_key_read,     
    .release = gpio_key_release,   
};  
  
/* 模块加载时会调用的函数 */  
static int __init gpio_key_init(void)  
{
    /* 用于接受返回值 */
    u32 ret = 0;
    
    /* 获取设备节点 */
    alinx_char.nd = of_find_node_by_path("/alinxkey");
    if(alinx_char.nd == NULL)
    {
        printk("alinx_char node not find\r\n");
        return -EINVAL;
    }
    else
    {
        printk("alinx_char node find\r\n");
    }
    
    /* 获取节点中gpio标号 */
    alinx_char.alinx_key_gpio = of_get_named_gpio(alinx_char.nd, "alinxkey-gpios", 0);
    if(alinx_char.alinx_key_gpio < 0)
    {
        printk("can not get alinxkey-gpios");
        return -EINVAL;
    }
    printk("alinxkey-gpio num = %d\r\n", alinx_char.alinx_key_gpio);
    
    /* 申请gpio标号对应的引脚 */
    ret = gpio_request(alinx_char.alinx_key_gpio, "alinxkey");
    if(ret != 0)
    {
        printk("can not request gpio\r\n");
        return -EINVAL;
    }
    
    /* 把这个io设置为输入 */
    ret = gpio_direction_input(alinx_char.alinx_key_gpio);
    if(ret < 0)
    {
        printk("can not set gpio\r\n");
        return -EINVAL;
    }
    
    /* 注册设备号 */
    alloc_chrdev_region(&alinx_char.devid, MINOR1, DEVID_COUNT, DEVICE_NAME);
    
    /* 初始化字符设备结构体 */
    cdev_init(&alinx_char.cdev, &ax_char_fops);
    
    /* 注册字符设备 */
    cdev_add(&alinx_char.cdev, alinx_char.devid, DRIVE_COUNT);
    
    /* 创建类 */
    alinx_char.class = class_create(THIS_MODULE, DEVICE_NAME);
    if(IS_ERR(alinx_char.class)) 
    {
        return PTR_ERR(alinx_char.class);
    }
    
    /* 创建设备节点 */
    alinx_char.device = device_create(alinx_char.class, NULL, 
                                      alinx_char.devid, NULL, 
                                      DEVICE_NAME);
    if (IS_ERR(alinx_char.device)) 
    {
        return PTR_ERR(alinx_char.device);
    }
    
    return 0;  
}

/* 卸载模块 */  
static void __exit gpio_key_exit(void)  
{  
    /* 释放gpio */
    gpio_free(alinx_char.alinx_key_gpio);

    /* 注销字符设备 */
    cdev_del(&alinx_char.cdev);
    
    /* 注销设备号 */
    unregister_chrdev_region(alinx_char.devid, DEVID_COUNT);
    
    /* 删除设备节点 */
    device_destroy(alinx_char.class, alinx_char.devid);
    
    /* 删除类 */
    class_destroy(alinx_char.class);
       
    printk("gpio_key_dev_exit_ok\n");  
}  
  
/* 标记加载、卸载函数 */  
module_init(gpio_key_init);  
module_exit(gpio_key_exit);  
  
/* 驱动描述信息 */  
MODULE_AUTHOR("Alinx");  
MODULE_ALIAS("gpio_key");  
MODULE_DESCRIPTION("GPIO OUT driver");  
MODULE_VERSION("v1.0");  
MODULE_LICENSE("GPL");  

138设置为输入、
在这里插入图片描述

read函数最后把读到的电平返回给buf
4)测试代码

/** ===================================================== **
 *Author : ALINX Electronic Technology (Shanghai) Co., Ltd.
 *Website: http://www.alinx.com
 *Address: Room 202, building 18, 
           No.518 xinbrick Road, 
           Songjiang District, Shanghai
 *Created: 2020-3-2 
 *Version: 1.0
 ** ===================================================== **/


#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>

int main(int argc, char *argv[])
{
    int fd, fd_l ,ret;
    char *filename, led_value = 0;
    unsigned int key_value;

    /* 验证输入参数个数 */
    if(argc != 2)
    {
        printf("Error Usage\r\n");
        return -1;
    }

    /* 打开输入的设备文件, 获取文件句柄 */
    filename = argv[1];
    fd = open(filename, O_RDWR);
    if(fd < 0)
    {
        /* 打开文件失败 */
        printf("file %s open failed\r\n", argv[1]);
        return -1;
    }

    while(1)
    {
        /* 读取按键状态 */
        ret = read(fd, &key_value, sizeof(key_value));
        if(ret < 0)
        {
            printf("read failed\r\n");
            break;
        }
        /* 按键被按下 */
        if(1 == key_value)
        {
            printf("ps_key1 press\r\n");
            led_value = !led_value;

            /* 用设备节点/dev/gpio_leds, 点亮led */
            fd_l = open("/dev/gpio_leds", O_RDWR);
            if(fd_l < 0)
            {
                printf("file /dev/gpio_leds open failed\r\n");
                break;
            }

            ret = write(fd_l, &led_value, sizeof(led_value));
            if(ret < 0)
            {
                printf("write failed\r\n");
                break;
            }

            ret = close(fd_l);
            if(ret < 0)
            {
                printf("file /dev/gpio_leds close failed\r\n");
                break;
            }
        }
    }

    ret = close(fd);
    if(ret < 0)
    {
        printf("file %s close failed\r\n", argv[1]);
        return -1;
    }

    return 0;
}

7)命令

./ax-key-test /dev/gpio_key

板子上可以观察到按键按下和松开,LED等交替闪烁
在这里插入图片描述

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值