前言
了解了设备树后,这一小节将使用设备树方式点亮LED
一、使用设备树
imx6ull-alientek-emmc.dts 文件中添加
tree_led {
#address-cells = <1>;/*起始地址占用一个字长*/
#size-cells = <1>; /*地址长度占用一个字长*/
compatible = "atkalpha-led";
status = "okay";
reg = < 0X020C406C 0X04 /* CCM_CCGR1_BASE */
0X020E0068 0X04 /* SW_MUX_GPIO1_IO03_BASE */
0X020E02F4 0X04 /* SW_PAD_GPIO1_IO03_BASE */
0X0209C000 0X04 /* GPIO1_DR_BASE */
0X0209C004 0X04 >; /* GPIO1_GDIR_BASE */
};
在内核源码根目录中执行
make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- dtbs,将imx6ull-alientek-emmc.dtb替换掉旧的文件。
root@ATK-IMX6U:/proc/device-tree# ls -l
total 0
-r--r--r-- 1 root root 4 Apr 15 07:51 '#address-cells'
drwxr-xr-x 2 root root 0 Apr 15 07:51 aliases
drwxr-xr-x 2 root root 0 Apr 15 07:51 backlight
drwxr-xr-x 2 root root 0 Apr 15 07:51 chosen
drwxr-xr-x 6 root root 0 Apr 15 07:51 clocks
-r--r--r-- 1 root root 34 Apr 15 07:51 compatible
drwxr-xr-x 3 root root 0 Apr 15 07:51 cpus
drwxr-xr-x 3 root root 0 Apr 15 07:51 gpio_keys@0
drwxr-xr-x 2 root root 0 Apr 15 07:51 interrupt-controller@00a01000
drwxr-xr-x 4 root root 0 Apr 15 07:51 leds
drwxr-xr-x 2 root root 0 Apr 15 07:51 memory
-r--r--r-- 1 root root 36 Apr 15 07:51 model
-r--r--r-- 1 root root 1 Apr 15 07:51 name
drwxr-xr-x 2 root root 0 Apr 15 07:51 pxp_v4l2
drwxr-xr-x 5 root root 0 Apr 15 07:51 regulators
drwxr-xr-x 3 root root 0 Apr 15 07:51 reserved-memory
drwxr-xr-x 2 root root 0 Apr 15 07:51 sii902x-reset
-r--r--r-- 1 root root 4 Apr 15 07:51 '#size-cells'
drwxr-xr-x 12 root root 0 Apr 15 07:51 soc
drwxr-xr-x 2 root root 0 Apr 15 07:51 sound
drwxr-xr-x 3 root root 0 Apr 15 07:51 spi4
drwxr-xr-x 2 root root 0 Apr 15 07:50 tree_led
进入到tree_led目录,看到的属性就是在设备树中设置的
root@ATK-IMX6U:/proc/device-tree/tree_led# ls -l
total 0
-r--r--r-- 1 root root 4 Apr 15 07:50 '#address-cells'
-r--r--r-- 1 root root 9 Apr 15 07:50 compatible
-r--r--r-- 1 root root 9 Apr 15 07:50 name
-r--r--r-- 1 root root 40 Apr 15 07:50 reg
-r--r--r-- 1 root root 4 Apr 15 07:50 '#size-cells'
-r--r--r-- 1 root root 5 Apr 15 07:50 status
1.1、使用寄存器函数读写
依然使用readl writel系列函数实现,这种方式比较繁琐,没有使用linux内核的便利。
驱动程序:
static int __init led_init(void)
{
u32 val = 0;
int ret;
u32 regdata[14];
const char *str;
struct property *proper;
/* 1、获取设备节点:tree_led */
dtsled.nd = of_find_node_by_path("/tree_led");
if(dtsled.nd == NULL) {
printk("alphaled node nost find!\r\n");
return -EINVAL;
} else {
printk("alphaled node find!\r\n");
}
/* 2、获取compatible属性内容 */
proper = of_find_property(dtsled.nd, "compatible", NULL);
if(proper == NULL) {
printk("compatible property find failed\r\n");
} else {
printk("compatible = %s\r\n", (char*)proper->value);
}
/* 3、获取status属性内容 */
ret = of_property_read_string(dtsled.nd, "status", &str);
if(ret < 0){
printk("status read failed!\r\n");
} else {
printk("status = %s\r\n",str);
}
/* 4、获取reg属性内容 */
ret = of_property_read_u32_array(dtsled.nd, "reg", regdata, 10);
if(ret < 0) {
printk("reg property read failed!\r\n");
} else {
u8 i = 0;
printk("reg data:\r\n");
for(i = 0; i < 10; i++)
printk("%#X ", regdata[i]);
printk("\r\n");
}
/* 初始化LED */
IMX6U_CCM_CCGR1 = of_iomap(dtsled.nd, 0);
SW_MUX_GPIO1_IO03 = of_iomap(dtsled.nd, 1);
SW_PAD_GPIO1_IO03 = of_iomap(dtsled.nd, 2);
GPIO1_DR = of_iomap(dtsled.nd, 3);
GPIO1_GDIR = of_iomap(dtsled.nd, 4);
/* 2、使能GPIO1时钟 */
val = readl(IMX6U_CCM_CCGR1);
val &= ~(3 << 26);
val |= (3 << 26);
writel(val, IMX6U_CCM_CCGR1);
writel(5, SW_MUX_GPIO1_IO03);/*复用*/
writel(0x10B0, SW_PAD_GPIO1_IO03);/*属性配置*/
/* 4、设置GPIO1_IO03为输出功能 */
val = readl(GPIO1_GDIR);
val &= ~(1 << 3);
val |= (1 << 3);
writel(val, GPIO1_GDIR);
/* 5、默认关闭LED */
val = readl(GPIO1_DR);
val |= (1 << 3);
writel(val, GPIO1_DR);
/* 注册字符设备驱动 */
/* 1、创建设备号 */
if (dtsled.major) { /* 定义了设备号 */
dtsled.devid = MKDEV(dtsled.major, 0);
register_chrdev_region(dtsled.devid, DTSLED_CNT, DTSLED_NAME);
} else { /* 没有定义设备号 */
alloc_chrdev_region(&dtsled.devid, 0, DTSLED_CNT, DTSLED_NAME); /* 申请设备号 */
dtsled.major = MAJOR(dtsled.devid); /* 获取分配号的主设备号 */
dtsled.minor = MINOR(dtsled.devid); /* 获取分配号的次设备号 */
}
printk("dtsled major=%d,minor=%d\r\n",dtsled.major, dtsled.minor);
/* 2、初始化cdev */
dtsled.cdev.owner = THIS_MODULE;
cdev_init(&dtsled.cdev, &dtsled_fops);
/* 3、添加一个cdev */
cdev_add(&dtsled.cdev, dtsled.devid, DTSLED_CNT);
/* 4、创建类 */
dtsled.class = class_create(THIS_MODULE, DTSLED_NAME);
if (IS_ERR(dtsled.class)) {
return PTR_ERR(dtsled.class);
}
/* 5、创建设备 */
dtsled.device = device_create(dtsled.class, NULL, dtsled.devid, NULL, DTSLED_NAME);
if (IS_ERR(dtsled.device)) {
return PTR_ERR(dtsled.device);
}
return 0;
}
1.2、使用pinctrl和gpio子系统
上一节驱动开发方式和裸机基本没啥区别,Linux 内核提供了 pinctrl 和 gpio 子系统用于GPIO 驱动,可以直接使用。
在 iomuxc 节点的 imx6ul-evk 子节点下创建一个名为“pinctrl_led”的子节点,节点内容如下所示:
pinctrl_led: ledgrp {
fsl,pins = <
MX6UL_PAD_GPIO1_IO03__GPIO1_IO03 0x10B0 /* LED0 */
>;
};
节点必须要pinctrl_开头,属性的配置一定要使用fsl,pins;
尖括号的内容是宏定义和对应的值,其中宏定义在固件库中可以找到,GPIO1_IO03表示复用为GPIO功能,0x10B0 为属性值。
gpioled {
#address-cells = <1>;
#size-cells = <1>;
compatible = "atkalpha-gpioled";
pinctrl-names = "default";
pinctrl-0 = <&pinctrl_led>;
led-gpio = <&gpio1 3 GPIO_ACTIVE_LOW>;
status = "okay";
}
pinctrl-0 属性设置 LED 灯所使用的 PIN 对应的 pinctrl 节点,电器属性在pinctrl已经设置。
led-gpio 属性指定了 LED 灯所使用的 GPIO,在这里就是 GPIO1 的 IO03,低电平有效。
在内核跟目录下设备树重新编译:
make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- dtbs
将imx6ull-alientek-emmc.dtb替换掉旧的文件。加载后在/proc/device-tree目录下发现有gpioled属性的文件夹
root@ATK-IMX6U:/proc/device-tree/gpioled# ls -l
total 0
-r--r--r-- 1 root root 4 Apr 16 07:28 '#address-cells'
-r--r--r-- 1 root root 17 Apr 16 07:28 compatible
-r--r--r-- 1 root root 12 Apr 16 07:28 led-gpio
-r--r--r-- 1 root root 8 Apr 16 07:28 name
-r--r--r-- 1 root root 4 Apr 16 07:28 pinctrl-0
-r--r--r-- 1 root root 8 Apr 16 07:28 pinctrl-names
-r--r--r-- 1 root root 4 Apr 16 07:28 '#size-cells'
-r--r--r-- 1 root root 5 Apr 16 07:28 status
使用pinctrl和gpio后,xxx_init函数变化,其它函数不变,直接使用内核提供的函数gpio_direction_output等程序如下。:
static int __init led_init(void)
{
int ret = 0;
/* 1、获取设备节点:gpioled */
gpioled.nd = of_find_node_by_path("/gpioled");
if(gpioled.nd == NULL) {
printk("gpioled node not find!\r\n");
return -EINVAL;
} else {
printk("gpioled node find!\r\n");
}
/* 2、 获取设备树中的gpio属性,得到LED所使用的LED编号 */
gpioled.led_gpio = of_get_named_gpio(gpioled.nd, "led-gpio", 0);
if(gpioled.led_gpio < 0) {
printk("can't get led-gpio");
return -EINVAL;
}
printk("led-gpio num = %d\r\n", gpioled.led_gpio);
/* 3、设置GPIO1_IO03为输出,并且输出高电平,默认关闭LED灯 */
ret = gpio_direction_output(gpioled.led_gpio, 1);
if(ret < 0) {
printk("can't set gpio!\r\n");
}
/* 注册字符设备驱动 */
/* 1、创建设备号 */
if (gpioled.major) {
gpioled.devid = MKDEV(gpioled.major, 0);
register_chrdev_region(gpioled.devid, GPIOLED_CNT, GPIOLED_NAME);
} else {
alloc_chrdev_region(&gpioled.devid, 0, GPIOLED_CNT, GPIOLED_NAME);
gpioled.major = MAJOR(gpioled.devid);
gpioled.minor = MINOR(gpioled.devid);
}
printk("gpioled major=%d,minor=%d\r\n",gpioled.major, gpioled.minor);
/* 2、初始化cdev */
gpioled.cdev.owner = THIS_MODULE;
cdev_init(&gpioled.cdev, &gpioled_fops);
/* 3、添加一个cdev */
cdev_add(&gpioled.cdev, gpioled.devid, GPIOLED_CNT);
/* 4、创建类 */
gpioled.class = class_create(THIS_MODULE, GPIOLED_NAME);
if (IS_ERR(gpioled.class)) {
return PTR_ERR(gpioled.class);
}
/* 5、创建设备 */
gpioled.device = device_create(gpioled.class, NULL, gpioled.devid, NULL, GPIOLED_NAME);
if (IS_ERR(gpioled.device)) {
return PTR_ERR(gpioled.device);
}
return 0;
}
Makefile文件
KERNELDIR := /home/wy/imx6ul/kernel
MAKE_ENV := ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf-
CURRENT_PATH := $(shell pwd)
obj-m := gpioled.o
build: kernel_modules
kernel_modules:
$(MAKE) $(MAKE_ENV) -C $(KERNELDIR) M=$(CURRENT_PATH) modules
clean:
$(MAKE) $(MAKE_ENV) -C $(KERNELDIR) M=$(CURRENT_PATH) clean
加载驱动:
root@ATK-IMX6U:/opt/mnt/imx6ul/driver/5_gpioled# insmod gpioled.ko
[ 97.474920] gpioled node find!
[ 97.481945] led-gpio num = 3
[ 97.485867] gpioled major=247,minor=0
GPIO设备节点找到了,gpio为3 ;主设备号为247 在dev下查看:
root@ATK-IMX6U:/opt/mnt/imx6ul/driver/5_gpioled# ls -l /dev/gpioled
crw------- 1 root root 247, 0 Apr 16 07:18 /dev/gpioled
驱动程序已经完成!!!
应用程序,led.c
#include "stdio.h"
#include "unistd.h"
#include "sys/types.h"
#include "sys/stat.h"
#include "fcntl.h"
#include "stdlib.h"
#include "string.h"
#define LEDOFF 0
#define LEDON 1
int main(int argc, char *argv[])
{
int fd, retvalue;
char *filename;
unsigned char databuf[1];
if(argc != 3){
printf("Error Usage!\r\n");
return -1;
}
filename = argv[1];
/* 打开led驱动 */
fd = open(filename, O_RDWR);
if(fd < 0){
printf("file %s open failed!\r\n", argv[1]);
return -1;
}
databuf[0] = atoi(argv[2]); /* 要执行的操作:打开或关闭 */
/* 向/dev/led文件写入数据 */
retvalue = write(fd, databuf, sizeof(databuf));
if(retvalue < 0){
printf("LED Control Failed!\r\n");
close(fd);
return -1;
}
retvalue = close(fd); /* 关闭文件 */
if(retvalue < 0){
printf("file %s close failed!\r\n", argv[1]);
return -1;
}
return 0;
}
交叉编译
arm-linux-gnueabihf-gcc led.c -o led
点亮led,最后参数1:点亮,0:熄灭
./led /dev/gpioled 1