• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

全志H3-nanopi-duo2开发板GPIO驱动开发

武飞扬头像
漏洞百出
帮助1

1:获取对应开发板duo2的内核源码

从官网获取
[friendlyarm的nanopi-duo2](https://wiki.friendlyelec.com/wiki/index.php/NanoPi_Duo2/zh#.E5.AE.9A.E5.88.B6.E5.91.BD.E4.BB.A4.E8.A1.8C.E7.9A.84.E6.AC.A2.E8.BF.8E.E4.BF.A1.E6.81.AF.EF.BC.88.E6.96.87.E5.AD.97LOGO.EF.BC.89)
此网页里面有duo2的很多资料,用户使用,uboot,kernel等等
需要从中下载linux-4.14内核源码,以下是官网维基中的部分内容,
用来作为参考

下载Linux内核源码,并切换分支:
$ git clone https://github.com/friendlyarm/linux.git -b sunxi-4.14.y --depth 1

编译和更新Linux内核:

$ apt-get install u-boot-tools
$ cd linux
$ touch .scmversion
$ make sunxi_defconfig ARCH=arm CROSS_COMPILE=arm-linux-
$ make zImage dtbs ARCH=arm CROSS_COMPILE=arm-linux-

假设SD卡的boot分区挂载在/media/SD/boot/,更新SD卡上的zImage和dtb文件:

$ cp arch/arm/boot/zImage /media/SD/boot/
$ cp arch/arm/boot/dts/sun8i-*-nanopi-*.dtb /media/SD/boot/

也可以用scp命令通过网络更新:

$ scp arch/arm/boot/zImage root@192.168.1.230:/boot
$ scp arch/arm/boot/dts/sun8i-*-nanopi-*.dtb root@192.168.1.230:/boot

2:进入源码目录(确保交叉编译工具链和环境变量配置正确)

2.1:源码根目录新建my_make内容为:

#!/bin/sh
export CROSS_COMPILE=$HOME/pan/arm_gcc/bin/arm-cortexa9-linux-gnueabihf-
export ARCH=arm 

然后source ./my_make

2.2:执行一下命令:

apt-get install u-boot-tools
touch .scmversion
make sunxi_defconfig //获取默认配置
make dtbs //编译设备树文件

执行make dtbs没有报错,说明环境变量和交叉编译都没问题

3: 修改设备树文件

duo2板子对应此文件:

arch\arm\boot\dts\sun8i-h3-nanopi-duo2.dts

设备树文件怎么修改,取决于要使用什么硬件或者说哪个引脚;
这里我要使用PA11引脚用来控制继电器,输出高低电平即可

学新通
在官方的设备树文件里面,此引脚被用作I2C引脚,被占用了
需要让占用的节点 status = “disabled”;

//这是pinctrl子节点,可以看到使用了引脚PA11
	...
	i2c0_pins: i2c0 {
					pins = "PA11", "PA12";
					function = "i2c0";
				};
	...
/*----------------------*/	
//这是client节点	
...	
&i2c0 {
	status = "okay"; //这里修改为disabled,禁用此节点I2C
	rtc@68 {
		compatible = "dallas,ds1307";
		reg = <0x68>;
	};
};
...
学新通

3.1: 添加自己的GPIO节点

在sun8i-h3-nanopi-duo2.dts中添加pinctrl子节点PA11引脚的复用功能

/{ //根节点
	...
}
//此内容书写位置平行于根节点
&pio {
	gpio_pin: gpio { //在pinctrl追加复用功能,设置gpio复用
		pins = "PA11";
		function = "gpio_out";
	};
};

在sun8i-h3-nanopi-duo2.dts中,在根节点内部添加client节点

/{ //根节点
	...
	...
	my_gpio{
		compatible = "gin,gpio";
		pinctrl-names = "default";
		pinctrl-0 = <&gpio_pin>; //选择复用功能
		mydvc-gpios = <&pio 0 11 GPIO_ACTIVE_HIGH>; /* PA11 第0组第11个引脚*/
		status = "okay";
	};
}
...
...
&pio {
	gpio_pin: gpio { //在pinctrl追加复用功能,设置gpio复用
		pins = "PA11";
		function = "gpio_out";
	};
};
学新通

3.2: make dtbs 编译设备树

make dtbs
//得到:sun8i-h3-nanopi-duo2.dtb

把dtb文件放入开发板目录下
挂载SD卡的boot分区

//挂载
sudo mount /dev/mmcblk0p1 /media/SD/boot/
//拷贝
sudo cp ./sun8i-h3-nanopi-duo2.dtb /media/SD/boot/
//取消挂载
sudo umount /media/SD/boot/
//重启
sudo reboot
//查看自己的节点信息,成功之后就会看到my_gpio节点,如下图效果
ls /sys/devices/platform/

学新通

4:编写驱动程序

4.1: 驱动代码内容

直接使用韦东山老师的课程led驱动源码,简单修改了一下下
#include <linux/module.h>
#include <linux/platform_device.h>

#include <linux/fs.h>
#include <linux/errno.h>
#include <linux/miscdevice.h>
#include <linux/kernel.h>
#include <linux/major.h>
#include <linux/mutex.h>
#include <linux/proc_fs.h>
#include <linux/seq_file.h>
#include <linux/stat.h>
#include <linux/init.h>
#include <linux/device.h>
#include <linux/tty.h>
#include <linux/kmod.h>
#include <linux/gfp.h>
#include <linux/gpio/consumer.h>
#include <linux/of.h>


/* 1. 确定主设备号                                                                 */
static int major = 0;
static struct class *led_class;
static struct gpio_desc *led_gpio;


/* 3. 实现对应的open/read/write等函数,填入file_operations结构体                   */
static ssize_t led_drv_read (struct file *file, char __user *buf, size_t size, loff_t *offset)
{
	printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
	return 0;
}

/* write(fd, &val, 1); */
static ssize_t led_drv_write (struct file *file, const char __user *buf, size_t size, loff_t *offset)
{
	int err;
	char status;
	
	printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
	err = copy_from_user(&status, buf, 1);

	/* 根据次设备号和status控制LED */
	gpiod_set_value(led_gpio, status);
	
	return 1;
}

static int led_drv_open (struct inode *node, struct file *file)
{	
	printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
	/* 根据次设备号初始化LED,输出模式,初始低电平 */
	gpiod_direction_output(led_gpio, 0);
	
	return 0;
}

static int led_drv_close (struct inode *node, struct file *file)
{
	printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
	return 0;
}

/* 定义自己的file_operations结构体                                              */
static struct file_operations led_drv = {
	.owner	 = THIS_MODULE,
	.open    = led_drv_open,
	.read    = led_drv_read,
	.write   = led_drv_write,
	.release = led_drv_close,
};

/* 4. 从platform_device获得GPIO
 *    把file_operations结构体告诉内核:注册驱动程序
 */
static int chip_demo_gpio_probe(struct platform_device *pdev)
{
	printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);

	/* 4.1 设备树中定义有: led-gpios=<...>;	*/
    led_gpio = gpiod_get(&pdev->dev, "mydvc", 0);
	if (IS_ERR(led_gpio)) {
		dev_err(&pdev->dev, "Failed to get GPIO for led\n");
		return PTR_ERR(led_gpio);
	}else{
        printk("get GPIO ");
    }
    
	/* 4.2 注册file_operations 	*/
	major = register_chrdev(0, "Gin_gpio", &led_drv);  /* /dev/led */

	led_class = class_create(THIS_MODULE, "Gin_gpio_class");
	if (IS_ERR(led_class)) {
		printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
		unregister_chrdev(major, "led");
		gpiod_put(led_gpio);
		return PTR_ERR(led_class);
	}

	device_create(led_class, NULL, MKDEV(major, 0), NULL, "Gin_gpio%d", 0); /* /dev/Gin_gpio */
        
    return 0;
    
}

static int chip_demo_gpio_remove(struct platform_device *pdev)
{
	device_destroy(led_class, MKDEV(major, 0));
	class_destroy(led_class);
	unregister_chrdev(major, "Gin_gpio");
	gpiod_put(led_gpio);
    
    return 0;
}


static const struct of_device_id Gin_gpio_dvc_id[] = {
    { .compatible = "gin,gpio" },
    { },
};

/* 1. 定义platform_driver */
static struct platform_driver chip_demo_gpio_driver = {
    .probe      = chip_demo_gpio_probe,
    .remove     = chip_demo_gpio_remove,
    .driver     = {
        .name   = "Gin_gpio",
        .of_match_table = Gin_gpio_dvc_id,
    },
};

/* 2. 在入口函数注册platform_driver */
static int __init led_init(void)
{
    int err;
    
	printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
	
    err = platform_driver_register(&chip_demo_gpio_driver); 
	
	return err;
}

/* 3. 有入口函数就应该有出口函数:卸载驱动程序时,就会去调用这个出口函数
 *     卸载platform_driver
 */
static void __exit led_exit(void)
{
	printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);

    platform_driver_unregister(&chip_demo_gpio_driver);
}


/* 7. 其他完善:提供设备信息,自动创建设备节点*/

module_init(led_init);
module_exit(led_exit);

MODULE_LICENSE("GPL")
学新通

4.2:编译驱动模块

进入menuconfig ,找到自己的驱动,设置成M,模块方式编译
保存退出
make -j8 models
得到my_driver.ko
拷贝到开发板,安装驱动
sudo insmod my_driver.ko
安装成功之后,在/dev/下有Gin_gpio0设备

学新通

5: 应用测试程序

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>


void ctrl_gpio(int fd,int state){
    char status;
        /* 3. 写文件 */
    if (state)
    {
        status = 1;
        write(fd, &status, 1);
    }
    else
    {
        status = 0;
        write(fd, &status, 1);
    }
}

/*
 * ./ledtest /dev/100ask_led0 on
 * ./ledtest /dev/100ask_led0 off
 */
int main(int argc, char **argv)
{
    int fd;
    int count = 0;

    /* 1. 判断参数 */
    if (argc != 2)
    {
        printf("Usage: %s <dev> <on | off>\n", argv[0]);
        return -1;
    }

    /* 2. 打开文件 */
    fd = open(argv[1], O_RDWR);
    if (fd == -1)
    {
        printf("can not open file %s\n", argv[1]);
        return -1;
    }
    while (count   < 10)
    {
        if((count % 2) == 0){
            ctrl_gpio(fd,1);
        }else{
            ctrl_gpio(fd,0);
        }
        sleep(2);
    }

    close(fd);

    return 0;
}
学新通

编译之后执行 ./test /dev/Gin_gpio0
效果就是PA11引脚拉高拉低

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhfhffcf
系列文章
更多 icon
同类精品
更多 icon
继续加载