首页 > 其他分享 >泰山派学习14--pinctr、gpio子系统控制设备树LED

泰山派学习14--pinctr、gpio子系统控制设备树LED

时间:2024-07-15 22:51:51浏览次数:13  
标签:pinctr 14 -- ret ALERT KERN gpio led gpioled

1、在泰山派设备树的/根节点上添加zbl_led子节点(路径:Z:\sdk\linux\kernel\arch\arm64\boot\dts\rockchip)

  打开tspi-rk3566-user-v10-linux.dts设备树源文件

       

  在根目录下添加gpioled子节点

       

 

2、在SDK上编译kernel(./build.sh kernel)

  执行内核编译:./build.sh kernel

       

  内核编译成功输出:

       

  查看boot.img是否生成最新的

       

 

3、烧录boot.img镜像(前提已经烧录了buildroot的uboot,且正常运行的)

  仅勾选boot.img,进行烧录

      

4、查看设备树上节点是否添加成功(cd /proc/device-tree)

  ls 

  cd gpioled

  ls 

    

    

   查看对应的属性是否与设置的一致

  cat name

      

 

 5、编写设备树的字符驱动对应函数

/*
** gpioled.c
** 复用型引脚分为5组(GPIO0~4),每组里面都有32个复用型引脚,而且又分为4个小组(A、B、C、D),每个小组8个引脚(0~7)
** GPIO3_B4
** 在GPIO3大组,第二的B小组,第4个引脚,

group = 1; //GPIO3_B4 => 1, group ∈ {(A=0), (B=1), (C=2), (D=3)}

bank = 3; //GPIO3_B4 => 3, bank ∈ [0,4]

x = 4; //GPIO3_B4 => 4, X ∈ [0,7]

number = group * 8 + X = 1 * 8 + 4 = 12

pin = bank*32 + number= 3 * 32 + 28 = 108;

*/

#include <linux/module.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/slab.h>
#include <linux/vmalloc.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/uaccess.h>
#include <linux/types.h>
#include <linux/moduleparam.h>
#include <linux/device.h>
#include <linux/gpio.h>
#include <linux/io.h>
#include <linux/of.h>
#include <linux/of_address.h>
#include <linux/of_gpio.h>


//设备名称
#define DEV_NAME "gpioled"
#define DEV_CNT 1
#define LED_ON 1
#define LED_OFF 0

char kbuf[128] = {0};


//定义新的一个结构体 struct chr_dev
struct gpioled_dev{
dev_t dev_id; //设备编号
struct cdev cdev; //内核字符设备
struct class *class; //设备类
struct device *device; //设备
int major; //主设备
int minor; //从设备
struct device_node *nd; //设备节点
int led_gpio;
};

static struct gpioled_dev gpioled;


int led_open(struct inode *inode, struct file *file)
{

//把文件的私有数据 private_data 指向设备结构体 dtsled
file->private_data = &gpioled;

printk(KERN_ALERT "[ KERN_ALERT ] gpioled open ...\n");

return 0;
}

ssize_t led_read(struct file *file, char __user *ubuf, size_t size, loff_t *offset)
{
printk(KERN_ALERT "[ KERN_ALERT ] gpioled read!\n");
return 0;
}

ssize_t led_write(struct file *file, const char __user *ubuf, size_t size, loff_t *offset)
{
unsigned char ret;
struct gpioled_dev *dev = file->private_data;

if(size > sizeof(kbuf)){
size = sizeof(kbuf);

}
if(copy_from_user(kbuf, ubuf, size)){
printk(KERN_ALERT "[ KERN_ALERT ] copy data form user fail!\n");
return -EIO;
}

ret = kbuf[0];

if(ret == LED_ON){
gpio_set_value(dev->led_gpio, LED_ON);
}
else if(ret == LED_OFF){
gpio_set_value(dev->led_gpio, LED_OFF);
}

printk(KERN_ALERT "[ KERN_ALERT ] gpioled write!\n");
return size;

}

int led_close(struct inode *inode, struct file *file)
{
printk(KERN_ALERT "[ KERN_ALERT ] gpioled close!\n");
return 0;
}

struct file_operations gpioled_fops= {
.owner = THIS_MODULE,
.open = led_open,
.read = led_read,
.write = led_write,
.release = led_close
};


static int __init ledcdev_init(void)
{
int ret = 0;
const char *str;


//读取设备树节点的属性数据
//获取设备节点:gpioled
gpioled.nd = of_find_node_by_path("/gpioled");
if(gpioled.nd == NULL){
printk(KERN_ALERT "[ KERN_ALERT ] gpioled node find failed.\n");
return -EINVAL;
}else{
printk(KERN_ALERT "[ KERN_ALERT ] gpioled node find ok.\n");
}

//获取 status 属性内容
ret = of_property_read_string(gpioled.nd, "status", &str);
if(ret < 0){
printk(KERN_ALERT "[ KERN_ALERT ] status read failed!\n");
return -EINVAL;

}else{
printk(KERN_ALERT "[ KERN_ALERT ] status = %s\n",str);
}

if(strcmp(str, "okay")){
return -EINVAL;
}

//获取 compatible 属性内容
ret = of_property_read_string(gpioled.nd, "compatible", &str);
if(ret < 0){
printk(KERN_ALERT "[ KERN_ALERT ] compatible read failed!\n");
return -EINVAL;

}else{
printk(KERN_ALERT "[ KERN_ALERT ] compatible = %s\n",str);
}

if(strcmp(str, "zbl,led")){
printk(KERN_ALERT "[ KERN_ALERT ] gpioled: Compatible match failed\n");
return -EINVAL;
}


//获取设备树中的 gpio 属性
gpioled.led_gpio = of_get_named_gpio(gpioled.nd, "led-gpio", 0);
if(gpioled.led_gpio < 0 ){
printk(KERN_ALERT "[ KERN_ALERT ] led-gpio find failed.\n");
return -EINVAL;
}else{
printk(KERN_ALERT "[ KERN_ALERT ] led-gpio num=%d\n", gpioled.led_gpio);
}


//向gpio子系统申请GPIO
ret = gpio_request(gpioled.led_gpio, "LED-GPIO");
if(ret){
printk(KERN_ALERT "[ KERN_ALERT ] gpioled failed to request led-gpio\n");
return ret;
}

ret = gpio_direction_output(gpioled.led_gpio, 0);
if(ret < 0){
printk(KERN_ALERT "[ KERN_ALERT ] can't set gpio output mode.\n");
}

//注册字符设备驱动
//创建设备号
if(gpioled.major){
gpioled.dev_id = MKDEV(gpioled.major, 0);
ret = register_chrdev_region(gpioled.dev_id, DEV_CNT, DEV_NAME);
if(ret < 0){
printk(KERN_ALERT "[ KERN_ALERT ] can't regsiter %s char driver [ret=%d]\n", DEV_CNT, DEV_NAME);
goto free_gpio;
}
}else{
ret = alloc_chrdev_region(&gpioled.dev_id, 0, DEV_CNT, DEV_NAME);
if(ret < 0){
printk(KERN_ALERT "[ KERN_ALERT ] %s can't alloc chrdev region, ret=%d.\n", DEV_NAME, ret);
goto free_gpio;
}
gpioled.major = MAJOR(gpioled.dev_id);
gpioled.minor = MINOR(gpioled.dev_id);
}

printk(KERN_ALERT "[ KERN_ALERT ] dtsled major=%d, minor=%d.\n", gpioled.major, gpioled.minor);

//关联结构体
gpioled.cdev.owner = THIS_MODULE;
cdev_init(&gpioled.cdev, &gpioled_fops);

ret = cdev_add(&gpioled.cdev, gpioled.dev_id, DEV_CNT);
if(ret < 0){
goto del_unregister;
}

gpioled.class = class_create(THIS_MODULE, DEV_NAME);
if(IS_ERR(gpioled.class)){
goto del_cdev;
}


gpioled.device = device_create(gpioled.class, NULL, gpioled.dev_id, NULL, DEV_NAME);
if(IS_ERR(gpioled.device)){
goto destroy_class;
}


printk(KERN_ALERT "[ KERN_ALERT ] gpioled init ...\n");
return 0;

destroy_class:
class_destroy(gpioled.class);
del_cdev:
cdev_del(&gpioled.cdev);
del_unregister:
unregister_chrdev_region(gpioled.dev_id, DEV_CNT);
free_gpio:
gpio_free(gpioled.led_gpio);
return -EIO;

}


static void __exit ledcdev_exit(void)
{

printk(KERN_ALERT "[ KERN_ALERT ] gpioled exit ...\n");

cdev_del(&gpioled.cdev);

unregister_chrdev_region(gpioled.dev_id, DEV_CNT);

device_destroy(gpioled.class, gpioled.dev_id);

class_destroy(gpioled.class);

gpio_free(gpioled.led_gpio);

}

module_init(ledcdev_init);

module_exit(ledcdev_exit);

MODULE_LICENSE("GPL");

MODULE_AUTHOR("zbl");

 

 

6、编写应用函数

/*
** dtsledapp.c
**
*/

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


#define LED_ON 1
#define LED_OFF 0


int main(int argc, char *argv[])
{

int fd;
int ret;
unsigned char databuf[1] = {0};

if(argc != 2){
printf("Error Usage!\n");
return -1;
}

fd = open("/dev/gpioled", O_RDWR);
if(fd == -1)
{
printf("open %s failed.\n", "/dev/gpioled");
return -1;
}

databuf[0] = atoi(argv[1]);
ret = write(fd, databuf, sizeof(databuf));
if(ret < 0){
printf("LED control failed.\n");
close(fd);
return -1;
}

ret = close(fd);
if(ret < 0){
printf("close %s failed.\n", "/dev/gpioled");
return -1;
}

return 0;
}

7、编写makefile (修改对应文件名称)

     

PWD ?= $(shell pwd)

KERNELDIR := /home/zbl/tspi-rk3566/sdk/linux/kernel
CROSS_COMPILE ?= /home/zbl/tspi-rk3566/sdk/linux/prebuilts/gcc/linux-x86/aarch64/gcc-linaro-6.3.1-2017.05-x86_64_aarch64-linux-gnu/bin/aarch64-linux-gnu-
CC := $(CROSS_COMPILE)gcc


obj-m += gpioled.o

module:
make -C $(KERNELDIR) M=$(PWD) ARCH=arm64 modules
@# -C 表示从当前目录切换到内核源码目录下,借助内核源码makefile进行make编译
@# M=$(PWD) 表示只编译当前目录下的驱动
@# ARCH=arm64 指定编译架构

$(CC) gpioledapp.c -o app
@# 交叉编译应用程序

.PHONE:clean

clean:
make -C $(KERNELDIR) M=$(PWD) ARCH=arm64 clean
rm app

 

8、拷贝到开发板(adb push xxx xxx),并对文件进行赋权(chmod 777 app gpioled.ko)

     

9、执行测试验证

  查看现有内核模块(lsmod), 并加载LED模块(insmod gpioled.ko)

     

   查看设备驱动(cat  /proc/devices)

      

  执行应用程序进行LED测试

      

   卸载驱动模块

      

 

标签:pinctr,14,--,ret,ALERT,KERN,gpio,led,gpioled
From: https://www.cnblogs.com/zblblog/p/18304167

相关文章

  • 电力需求预测挑战赛——Datawhale AI 夏令营第2期【从零入门AI竞赛之机器学习】
    赛事官网https://challenge.xfyun.cn/h5/detail?type=electricity-demand&ch=dw24_uGS8Gs学习者手册https://exn8g66dnwu.feishu.cn/docx/T7WGd7goqowRvFxwoApclo9Pn0bTask1——传统时序模型(2024/7/14)阶段要求根据文档跑通提交并拿下第一个分数;学习相关知识点,理解赛题。......
  • git冲突发生原因-两个人同时对文件的同一部分进行了修改
    在甲负责分支 b 的开发,每次修改后推送到远程分支,乙需要将远程分支 b 拉取更新到本地进行测试,并且乙不修改分支 b 的情况下,通常不会产生冲突。这是因为冲突通常发生在不同的人对同一个文件的同一部分进行了不同的修改,而乙只是在拉取和合并更新,并不进行修改。再解释的专业一......
  • 从远程拉取分支并更新到本地方法1
    要一次性拉取远程仓库中所有分支的更新,通常使用以下方法:gitfetch:gitfetch 会获取远程仓库的所有分支,但不会合并到当前分支。这是最安全的方法,因为它不会改变你当前的工作状态。gitfetch--allgitpull:gitpull 通常用于当前分支的合并。要拉取并合并所有分支的......
  • Spark _Exam_ 20240715
    SparkExam20240715ConclusionSB出题人出DP场,T1靠小常数通过不给提示干死选手,T2出题人认为思维难度低代码5KB,NOIP场的T3放黑题,T4又是区间DP\(\mathcalO(n^6)=117649000000\)竟然能够通过?你代码常数真的小!好的喷完了。这种场的后果就是,平均分50,最高90,最低0实际上如......
  • 四、Python集合与函数
    集合set1.不同元素组成2.无序3.集合中元素必须是不可变类型s={1,2,3,4,5}集合常用魔法s={1,2,3,4,5}s.add('s')print(s)#>>>{1,2,3,4,5,'s'}s.add(6)print(s)#>>>{1,2,3,4,5,'s',6}s.clear()print(s)s={1,2,3,4,5}v=s......
  • C++(2) 从yml或者txt读取和保存数据
      %YAML:1.0---gps:"2132312" CMakeLists.txtcmake_minimum_required(VERSION3.5)set(CMAKE_CXX_STANDARD11)#设置项目名称和语言project(run_nodeLANGUAGESCXX)#设置opencv安装路径#set(CMAKE_PREFIX_PATH"/home/r9000k/v1_software/opencv/o......
  • Franka Robot 如何理解机器人的笛卡尔阻抗运动
    在笛卡尔阻抗模式下,用手将机器人移动到一个新位置后,机器人的行为取决于其控制参数(刚度、阻尼、质量)的设定和外部力的作用。当你将机器人移动到一个新位置并释放它时,以下是可能的情况:高刚度情况下如果机器人的刚度(Stiffness)参数设置较高,意味着机器人对位置偏差有很强的恢复力。当......
  • DO、DTO、BO、AO、VO、POJO定义规范
    DO、DTO、BO、AO、VO、POJO定义分层领域模型规约:DO(DataObject):与数据库表结构一一对应,通过DAO层向上传输数据源对象DTO(DataTransferObject):数据传输对象,Service或Manager向外传输的对象BO(BusinessObject):业务对象。由Service层输出的封装业务逻辑的对象AO(Applicatio......
  • C#基础:partial关键字和类的继承
    代码示例publicpartialclassForm1:Form{publicForm1(){InitializeComponent();}//Button的Click点击事件(自动添加)privatevoidshowMessage(objectsender,EventArgse){MessageBox.Show("HelloWorld!");}......
  • KMP算法
    KMP算法KMP算法是一个字符串算法,通常用于匹配字符串。KMP算法的原理如果我们暴力枚举下标\(i,j\),\(i\)是文本串的下标,\(j\)是模式串(你要在文本串中匹配的字符串)的下标,时间复杂度\(O(NM)\),其中\(N,M\)分别为文本串和模式串的长度。我们看一下匹配过程:(gif动图请耐心观看)......