今天弄orangepi zero2w H618设备树驱动,直接进入正题
进入orangepi的linux内核目录
cd ~/orangepi-build/kernel/orange-pi-6.1-sun50iw9/
vim arch/arm64/boot/dts/allwinner/sun50i-h616-orangepi-zero2w.dts
修改设备树,添加下面的第16行、46行到54行代码
1 // SPDX-License-Identifier: (GPL-2.0+ or MIT)
2 /*
3 * Copyright (C) 2020 Arm Ltd.
4 */
5
6 /dts-v1/;
7
8 #include "sun50i-h616.dtsi"
9 #include "sun50i-h616-cpu-opp.dtsi"
10
11 #include <dt-bindings/gpio/gpio.h>
12 #include <dt-bindings/interrupt-controller/arm-gic.h>
13 #include <dt-bindings/input/input.h>
14 #include <dt-bindings/leds/common.h>
15
16 #define GROUP_PIN(g,p) ((g<<16) | (p))
17
18 / {
19 model = "OrangePi Zero2 W";
20 compatible = "xunlong,orangepi-zero2w", "allwinner,sun50i-h616";
21
22 aliases {
23 serial0 = &uart0;
24 serial2 = &uart2;
25 serial3 = &uart3;
26 serial4 = &uart4;
27 serial5 = &uart5;
28 ethernet0 = &emac1;
29 };
30
31 chosen {
32 stdout-path = "serial0:115200n8";
33 };
34
35 connector {
36 compatible = "hdmi-connector";
37 type = "d";
38
39 port {
40 hdmi_con_in: endpoint {
41 remote-endpoint = <&hdmi_out_con>;
42 };
43 };
44 };
45
46 coolx_led@0{
47 compatible = "coolx,led_drv";
48 pin = <GROUP_PIN(2, 12)>;
49 };
50
51 coolx_led@1{
52 compatible = "coolx,led_drv";
53 pin = <GROUP_PIN(2, 13)>;
54 };
执行make dtbs编译设备树
将新的设备树文件上传到开发板
scp arch/arm64/boot/dts/allwinner/sun50i-h616-orangepi-zero2w.dtb orangepi@开发板局域网ip地址:/home/orangepi
在开发板上拷贝一份sun50i-h616-orangepi-zero2w.dtb设备树文件
sudo cp -r /boot/dtb-6.1.31-sun50iw9/allwinner/sun50i-h616-orangepi-zero2w.dtb /boot/dtb-6.1.31-sun50iw9/allwinner/sun50i-h616-orangepi-zero2w.dtb.old
将PC传来的sun50i-h616-orangepi-zero2w.dtb拷贝到内核加载设备树的地方
sudo cp -r ~/sun50i-h616-orangepi-zero2w.dtb /boot/dtb-6.1.31-sun50iw9/allwinner/sun50i-h616-orangepi-zero2w.dtb
然后重启,让内核加载新生成的设备树
PC端编写以下文件,放在同一目录下
Makefile
#Makefile
# 1. 使用不同的开发板内核时, 一定要修改KERN_DIR
# 2. KERN_DIR中的内核要事先配置、编译, 为了能编译内核, 要先设置下列环境变量:
# 2.1 ARCH, 比如: export ARCH=arm64
# 2.2 CROSS_COMPILE, 比如: export CROSS_COMPILE=aarch64-linux-gnu-
# 2.3 PATH, 比如: export PATH=$PATH:/home/book/100ask_roc-rk3399-pc/ToolChain-6.3.1/gcc-linaro-6.3.1-2017.05-x86_64_aarch64-linux-gnu/bin
# 注意: 不同的开发板不同的编译器上述3个环境变量不一定相同,
# 请参考各开发板的高级用户使用手册
KERN_DIR = /home/coolx/orangepi-build/kernel/orange-pi-6.1-sun50iw9
all:
make -C $(KERN_DIR) M=`pwd` modules
#$(CROSS_COMPILE)gcc -o led_dev led_dev.c
clean:
make -C $(KERN_DIR) M=`pwd` modules clean
rm -rf modules.order
#rm -f led_dev
#obj-m += pindriver.o
obj-m += led_dev.o led_tree_drv.o
led_opr.h
//led_opr.h
#ifndef _LED_OPR_H
#define _LED_OPR_H
// #define PC_CFG0_REG 0x0300B000 + 2 * 0x24 + 0x04 //PC配置寄存器 A:0 B:1 C:2 ....
// #define PC_DATA_REG 0x0300B000 + 2 * 0x24 + 0x10//PC数据寄存器 A:0 B:1 C:2 ....
#define PC_CFG0_REG 0x0300B000 //PC配置寄存器 A:0 B:1 C:2 ....
#define PC_DATA_REG 0x0300B000 //PC数据寄存器 A:0 B:1 C:2 ....
#define PIN_N 13 //第5个引脚
#define N (PIN_N % 8 * 4) //引脚x : x % 8 * 4
struct led_operations {
int (*init) (int which); /* 初始化LED, which-哪个LED */
int (*ctl) (int which, char status); /* 控制LED, which-哪个LED, status:1-亮,0-灭 */
int (*close) (int which); /* 控制LED, which-哪个LED, status:1-亮,0-灭 */
};
struct led_operations *get_board_led_opr(void);
void led_class_create_device(int minor);
void led_class_destroy_device(int minor);
void register_led_operations(struct led_operations *opr);
#endif
led_dev.c
//led_dev.c
#include <linux/fs.h> //file_operations声明
#include <linux/module.h> //module_init module_exit声明
#include <linux/init.h> //__init __exit 宏定义声明
#include <linux/device.h> //class devise声明
#include <linux/uaccess.h> //copy_from_user 的头文件
#include <linux/types.h> //设备号 dev_t 类型声明
#include <asm/io.h> //ioremap iounmap的头文件
#include "led_opr.h"
static int major =231; //主设备号
static char *module_name="coolx_led"; //模块名
static struct class *Coolx_class;
struct led_operations *p_led_opr;
void led_class_create_device(int minor)
{
device_create(Coolx_class,NULL,MKDEV(major,minor),NULL,"%s%d",module_name,minor); //创建设备文件 /dev/
}
void led_class_destroy_device(int minor)
{
device_destroy(Coolx_class, MKDEV(major, minor));
}
void register_led_operations(struct led_operations *opr)
{
p_led_opr = opr;
}
EXPORT_SYMBOL(led_class_create_device);//EXPORT_SYMBOL声明后其他模块可以调用,但先必须加载此模块
EXPORT_SYMBOL(led_class_destroy_device);
EXPORT_SYMBOL(register_led_operations);
/*打开设备*/
static int led_open (struct inode *node, struct file *filp)
{
int minor = iminor(node);
/* 根据次设备号初始化LED */
p_led_opr->init(minor);
return 0;
}
/*写设备*/
static ssize_t led_write (struct file *file, const char __user *buf, size_t size, loff_t *off)
{
char val;
struct inode *inode = file_inode(file);
int minor = iminor(inode);
printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
copy_from_user(&val, buf, 1);
printk("val = %s",val);
/* 根据次设备号和status控制LED */
p_led_opr->ctl(minor, val);
return 1;
}
static ssize_t led_read (struct file *file,char __user *buf,size_t count, loff_t *ppos)
{
return 0;
}
/*关闭设备*/
static int led_drv_close (struct inode *node, struct file *file)
{
printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
struct inode *inode = file_inode(file);
int minor = iminor(inode);
p_led_opr->close(minor);
return 0;
}
/*
* 设备操作函数结构体
*/
static struct file_operations file_operation = {
.owner = THIS_MODULE,
.open = led_open,
.write = led_write,
.read = led_read,
.release = led_drv_close,
};
/* 设备初始化 */
int __init Coolx_led_init(void)
{
int ret;
printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
//major == 0 autogive
ret = register_chrdev(major, module_name, &file_operation);// cat /proc/devices
if (ret < 0) {
printk(KERN_ERR "fail\n");
return -1;
}
Coolx_class=class_create(THIS_MODULE,module_name);//由代码在/sys/class 下自动生成设备 /sys/class
// devno = MKDEV(major,minor); //创建设备号
//device_create(Coolx_class,NULL,devno,NULL,module_name); //创建设备文件 /dev/
printk(KERN_ERR "init \n");
return 0;
}
void __exit Coolx_led_exit(void)
{
//device_destroy(Coolx_class,devno);
class_destroy(Coolx_class);
/* 注销字符设备驱动 */
unregister_chrdev(major, module_name);
//iounmap(gpio_con);//
//iounmap(gpio_dat);
printk("exit!\r\n");
}
//注册模块加载函数
module_init(Coolx_led_init);
//卸载模块加载函数
module_exit(Coolx_led_exit);
//开源信息
MODULE_LICENSE("GPL");
led_tree_drv.c
//led_tree_drv.c
#include <linux/module.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/platform_device.h>
#include <linux/of.h>
#include "led_opr.h"
#define GROUP(x) (x>>16)
#define PIN(x) (x&0xFFFF)
static int g_ledpins[100];
static int g_ledcnt = 0;
volatile unsigned int *gpio_con = NULL;
volatile unsigned int *gpio_dat = NULL;
static int board_demo_led_init (int which) /* 初始化LED, which-哪个LED */
{
//printk("%s %s line %d, led %d\n", __FILE__, __FUNCTION__, __LINE__, which);
unsigned int gpioREG = PC_CFG0_REG + GROUP(g_ledpins[which])*0x24 + PIN(g_ledpins[which])/8*0x04;
gpio_con = (volatile unsigned int *)ioremap(gpioREG, 1);//iounmap函数用于取消ioremap()所做的映射
//选择1
gpio_dat = (volatile unsigned int *)ioremap(PC_CFG0_REG + GROUP(g_ledpins[which])*0x24 + 0x10, 1);
//选择2
//gpio_dat = gpio_con + 4; //数据寄存器 指针+4是移动了4*4=16个字节 原来是0x24 现在是0x34
*gpio_con &= ~(7 << (PIN(g_ledpins[which]) % 8 * 4)); //7=111 取反000 20:22设置000 默认是0x7=111 失能
*gpio_con |= (1 << (PIN(g_ledpins[which]) % 8 * 4)); //设置输出 20:22设置001
*gpio_dat &= ~(1 << PIN(g_ledpins[which])); //第n个引脚初始化设置0
printk("init gpio: group %d, pin %d\n", GROUP(g_ledpins[which]), PIN(g_ledpins[which]));
return 0;
}
static int board_demo_led_ctl (int which, char status) /* 控制LED, which-哪个LED, status:1-亮,0-灭 */
{
//printk("%s %s line %d, led %d, %s\n", __FILE__, __FUNCTION__, __LINE__, which, status ? "on" : "off");
printk("set led %s: group %d, pin %d\n", status ? "on" : "off", GROUP(g_ledpins[which]), PIN(g_ledpins[which]));
if(status)
{
*gpio_dat |= (1 << PIN(g_ledpins[which]));//引脚13设置1
}
else
{
*gpio_dat &= ~(1 << PIN(g_ledpins[which]));//引脚13设置0
}
return 0;
}
static int board_demo_led_close (int which) /* 控制LED, which-哪个LED, status:1-亮,0-灭 */
{
//printk("%s %s line %d, led %d, %s\n", __FILE__, __FUNCTION__, __LINE__, which, status ? "on" : "off");
printk("close led: group %d, pin %d\n",GROUP(g_ledpins[which]), PIN(g_ledpins[which]));
unsigned int gpioREG = PC_CFG0_REG + GROUP(g_ledpins[which])*0x24 + PIN(g_ledpins[which])/8*0x04;
iounmap(gpio_con);
iounmap(gpio_dat);
return 0;
}
static struct led_operations board_demo_led_opr = {
.init = board_demo_led_init,
.ctl = board_demo_led_ctl,
.close = board_demo_led_close,
};
static int coolx_led_probe(struct platform_device *pdev)
{
struct device_node *np;
int err = 0;
int led_pin;
np = pdev->dev.of_node;
if (!np)
return -1;
err = of_property_read_u32(np, "pin", &led_pin);
g_ledpins[g_ledcnt] = led_pin;
led_class_create_device(g_ledcnt);
g_ledcnt++;
return 0;
}
static int coolx_led_remove(struct platform_device *pdev)
{
int i = 0;
int err;
struct device_node *np;
int led_pin;
np = pdev->dev.of_node;
if (!np)
return -1;
err = of_property_read_u32(np, "pin", &led_pin);
for (i = 0; i < g_ledcnt; i++)
{
if (g_ledpins[i] == led_pin)
{
led_class_destroy_device(i);
g_ledpins[i] = -1;
break;
};
}
for (i = 0; i < g_ledcnt; i++)
{
if (g_ledpins[i] != -1)
break;
}
if (i == g_ledcnt)
g_ledcnt = 0;
return 0;
}
static const struct of_device_id coolx_leds[] = {
{ .compatible = "coolx,led_drv" },
{ },
};
static struct platform_driver coolx_led_platform_driver = {
.probe = coolx_led_probe,
.remove = coolx_led_remove,
.driver = {
.name = "coolx_led",
//if dts have compatile == of_match_table.compatible == "coolx,led_drv"
//call coolx_led_probe
.of_match_table = coolx_leds,
},
};
int __init led_tree_drv_init(void)
{
int err;
err = platform_driver_register(&coolx_led_platform_driver);
register_led_operations(&board_demo_led_opr);
return 0;
}
void __exit led_tree_drv_exit(void)
{
platform_driver_unregister(&coolx_led_platform_driver);
}
module_init(led_tree_drv_init);
module_exit(led_tree_drv_exit);
MODULE_LICENSE("GPL");
编写完后,在这个目录,执行make,编译上面写的内核模块,得到led_dev.ko led_tree_drv.ko
上传到开发板上的/home/orangepi目录
scp led_dev.ko led_tree_drv.ko orangepi@开发板局域网ip地址:/home/orangepi
led_tree_drv.ko使用了led_dev.ko中EXPORT_SYMBOL
声明的函数,所以要先加载led_dev.ko模块,再加载led_tree_drv.ko
在开发板上,加载两个模块:
sudo insmod led_dev.ko
sudo insmod led_tree_drv.ko
关闭心跳灯:
sudo chmod 666 /sys/class/leds/green_led/trigger
sudo echo none > /sys/class/leds/green_led/trigger
给/dev/coolx_led1权限:
sudo chmod 666 /dev/coolx_led1
写1到/dev/coolx_led1,打开绿色led灯(对应设备树的pin = <GROUP_PIN(2, 13)>;):
echo -e -n "\x01" > /dev/coolx_led1
orangepi@orangepizero2w:~$ sudo echo -e -n "\x01" > /dev/coolx_led1
orangepi@orangepizero2w:~$ dmesg
[81966.834602] init gpio: group 2, pin 13
[81966.896633] /home/coolx/Desktop/code/led_dev.c led_write line 84
[81966.902746] val = (efault)
[81966.902757] set led on: group 2, pin 13
[81966.917332] /home/coolx/Desktop/code/led_dev.c led_drv_close line 103
[81966.923869] close led: group 2, pin 13
orangepi@orangepizero2w:~$
写0到/dev/coolx_led1,关闭绿色led灯(对应设备树的pin = <GROUP_PIN(2, 13)>;):
echo -e -n "\x00" > /dev/coolx_led1
orangepi@orangepizero2w:~$ sudo echo -e -n "\x00" > /dev/coolx_led1
orangepi@orangepizero2w:~$ dmesg
[81966.834602] init gpio: group 2, pin 13
[81966.896633] /home/coolx/Desktop/code/led_dev.c led_write line 84
[81966.902746] val = (efault)
[81966.902757] set led on: group 2, pin 13
[81966.917332] /home/coolx/Desktop/code/led_dev.c led_drv_close line 103
[81966.923869] close led: group 2, pin 13
[82075.784874] init gpio: group 2, pin 13
[82075.846728] /home/coolx/Desktop/code/led_dev.c led_write line 84
[82075.852833] val = (null)
[82075.852843] set led off: group 2, pin 13
[82075.867296] /home/coolx/Desktop/code/led_dev.c led_drv_close line 103
[82075.873820] close led: group 2, pin 13
orangepi@orangepizero2w:~$
总结
led_tree_drv模块从设备树获取到IO引脚资源,在/dev/目录下创建设备文件,还提供IO初始化、操作和重置接口给led_dev模块。
led_dev模块则在 /proc/devices注册字符设备、在/sys/class中生成设备,提供open、write和close让用户层可以对IO进行操作。
参考链接:
https://www.100ask.net/