【嵌入式Linux】Jetson nano GPIO应用 | 驱动开发 | 官方gpiolib、设备树与chip_driver

GPIO子系统

image-20230415163259138

0.暴露给应用层

应用

$ echo 79 > /sys/class/gpio/export   //导出79号gpio 引脚,使得可在应用层访问
$ echo out > /sys/class/gpio/gpio79/direction  //设置 为输出
$ echo 1 > /sys/class/gpio/gpio79/value //输出高电平 开灯
$ echo 0 > /sys/class/gpio/gpio79/value  //输出低电平, 关灯	
$ cat /sys/kernel/debug/gpio  //查询gpio状态(问题:发现找不到gpio文件)
$ echo 79 > unexport //取消导出(发现gpio79消失了)

解决调试目录为空的问题

原因 //debug需要的文件系统 debugfs没有挂载/etc/fstab	 的后面添加一行
debugfs     /sys/kernel/debug   debugfs  defaults         0       0

调试信息

$ cat /sys/kernel/debug/gpio  //查看gpio 当前配置情况(驱动暴露的调试信息)
$ cat  /sys/kernel/debug/tegra_gpio  //查看GPIO 寄存器内容(和芯片手册进行对应)
$ cat  /sys/kernel/debug/tegra_pinctrl_reg   //查看 pinctrl 寄存器内容	

1.最简读写文件(在/SYS下)

设备树

   sys_rw_led{  //对应生成 /sys/devices/sys_rw_led/led_gpio
        compatible = "sys_rw_led";
        led_gpio  = <&gpio TEGRA_GPIO(J,7) GPIO_ACTIVE_HIGH>;
    };

验证测试

$ cd sys_rw_led
$ make
$ cp led.ko /nfs/rootfs

$ cd ~/kernel-4.9
$ make dtbs
$ cp arch/arm64/boot/dts/tegra210-p3448-0000-p3449-0000-b00.dtb   /tftpboot/	

重启板子
$ insmod led.ko
$ cd /sys/devices/sys_rw_led
$ echo 0 > led_gpio  //关灯
$ echo 1 > led_gpio  //亮灯

编译文件

//Makefile
ifeq ($(KERNELRELEASE),)
KERNELDIR ?= ~/kernel-4.9 
PWD := $(shell pwd)

all:
	$(MAKE) -C $(KERNELDIR) M=$(PWD) modules

clean:
	rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versions Module* modules* a.out *.bak

else
    obj-m := led.o
endif	

驱动

  • of_get_named_gpio_flags//获取设备树节点的属性gpio_is_valid//判断是否合法devm_gpio_request//申请使用gpio,并调用设置pinctrl

  • device_create_file //根据设备树节点属性,创建相应的属性文件 /sys/devices/sys_rw_led/led_gpio

    • static struct device_attribute dev_attr_file //属性文件的描述
//led.c
#include <linux/init.h>                        
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/err.h>

#include <linux/of_gpio.h>
#include <linux/gpio.h>

#include <linux/platform_device.h>
#include <asm/io.h>


#include <dt-bindings/gpio/gpio.h>

#ifdef CONFIG_OF
#include <linux/of.h>
#include <linux/of_device.h>
#include <linux/of_gpio.h>
#endif

int  led_gpio;
static ssize_t led_store(struct device *dev, struct device_attribute *attr,
              const char *buf, size_t count) {
              
    //写文件,控制 gpio 输出 ( echo 1 >   led_gpio)                
    if (buf[0] == '0') {
        gpio_direction_output(led_gpio, 0);
    }else if (buf[0] == '1') {
        gpio_direction_output(led_gpio, 1);
    }    

    printk(KERN_ERR "led_gpio_store %c \n",buf[0]);   
    return count;
}

ssize_t led_show(struct device *dev, struct device_attribute *attr,char *buf){  
	printk("led_show go \n");	
 	return 0;
}
//属性文件的描述
static struct device_attribute dev_attr_file = {    
    .attr = {        
       .name = "led_gpio",        
       .mode = (S_IRUGO | S_IWUSR)  
    },   
    .store = led_store, //echo 1> 就调用到这里了
    .show = led_show,  //cat led_gpio时调用,如果无需读的功能,可设为NULL,  且删除前面的S_IRUGO
};


int leds_probe(struct platform_device *pdev)
{
   int  ret = 0;
	
   enum of_gpio_flags flags;

  //获取设备树节点的属性 "led_gpio"
   led_gpio = of_get_named_gpio_flags(pdev->dev.of_node, "led_gpio", 0, &flags);
   if (gpio_is_valid(led_gpio)) //判断是否合法
   {
      ret = devm_gpio_request(&pdev->dev,led_gpio, "led_gpio"); //申请使用gpio(如果被占用,将申请失败)
      if (ret) {
	  printk("Failed to get led_gpio gpio.\n");
	  return -1;
       } 
   }

   //根据设备树节点属性,创建相应的属性文件 /sys/devices/sys_rw_led/led_gpio
   device_create_file(&pdev->dev, &dev_attr_file);  // /device_create_file 里面是调用了 sysfs_create_file
   printk("leds_probe 1 ok\n");
   return 0;
}

int leds_remove(struct platform_device *pdev)
{   
    device_remove_file(&pdev->dev, &dev_attr_file);
    printk("leds_remove  ok\n");    
    return 0;
}

static const struct of_device_id of_led_match[] = {
    { .compatible = "sys_rw_led", },
    {},
};

MODULE_DEVICE_TABLE(of, of_led_match);

struct platform_driver leds_drv = {
   .driver = {
      .owner = THIS_MODULE,
      .name = "sys_rw_led driver" ,
      .of_match_table = of_led_match,
   },
   .probe = leds_probe,
   .remove = leds_remove,    
};
module_platform_driver(leds_drv);
MODULE_LICENSE("GPL");

2.读写多个属性文件(在SYS下)

设备树

sys_rw_gpio{
    compatible = "bbcen,sys_rw_gpio";
    led_gpio  = <&gpio TEGRA_GPIO(J,7) GPIO_ACTIVE_HIGH>;     //40pin 丝印 12
    smoke_sensor_gpio = <&gpio TEGRA_GPIO(S,5) GPIO_ACTIVE_HIGH>; //40pin 丝印 29
};

验证测试

$ cd sys_read_write
$ make
$ cp gpio.ko /nfs/rootfs

$ cd ~/kernel-4.9
$ make dtbs
$ cp arch/arm64/boot/dts/tegra210-p3448-0000-p3449-0000-b00.dtb   /tftpboot/	

重启板子
$ insmod gpio.ko
$ cd /sys/devices/sys_rw_gpio
$ echo 0 > led_gpio  //关灯
$ echo 1 > led_gpio  //亮灯	
$ cat smoke_sensor_gpio  //读管脚输入电平,默认读到0,用杜邦线,把它接到前面亮灯的管脚时,能成功读到1

编译文件

ifeq ($(KERNELRELEASE),)
KERNELDIR ?= ~/kernel-4.9 
PWD := $(shell pwd)

all:
	$(MAKE) -C $(KERNELDIR) M=$(PWD) modules

clean:
	rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versions Module* modules* a.out *.bak

else
    obj-m := gpio.o
endif	

驱动

  • 定义一个结构体指针,分配空间,指定内容

  • device_attribute gpio_attr[]

  • sysfs_create_file //for循环读取,和上面的数组配合实现读多个属性

#include <linux/init.h>                        
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/slab.h>

#include <linux/delay.h>
#include <linux/err.h>

#include <linux/of_gpio.h>
#include <linux/gpio.h>

#include <linux/platform_device.h>
#include <asm/irq.h>
#include <asm/io.h>

#include <linux/delay.h>

#include <dt-bindings/gpio/gpio.h>

#ifdef CONFIG_OF
#include <linux/of.h>
#include <linux/of_device.h>
#include <linux/of_gpio.h>
#endif

struct gpio_dev{
     struct platform_device *pdev;
     int led_gpio;        
     int led_gpio_direction;	 
     int smoke_sensor_gpio;
};

static struct gpio_dev *gpio_dev = NULL;

static ssize_t led_gpio_store(struct device *dev, struct device_attribute *attr,
              const char *buf, size_t count) {
              
    //写文件,控制 gpio 输出 ( echo 1 >   led_gpio)        
    struct gpio_dev *pdata = gpio_dev;            
    if (buf[0] == '0') {
        pdata->led_gpio_direction = 0;
        gpio_direction_output(pdata->led_gpio, pdata->led_gpio_direction);
    }else if (buf[0] == '1') {
        pdata->led_gpio_direction = 1;
        gpio_direction_output(pdata->led_gpio, pdata->led_gpio_direction);
    }    

    printk(KERN_ERR "led_gpio_store %d \n",pdata->led_gpio_direction);   
    return count;
}

static ssize_t smoke_sensor_gpio_show(struct device *dev, struct device_attribute *attr, char *buf) {
    struct gpio_dev *pdata = gpio_dev; 
    int gpio_value = 0;

    gpio_value = gpio_get_value(pdata->smoke_sensor_gpio); //获取 gpio的输入值(cat  smoke_sensor_gpio 时会触发)
    
    return snprintf(buf, PAGE_SIZE, "%d\n",gpio_value);
}



static struct device_attribute gpio_attr[] = {  
    __ATTR(led_gpio, 0664, NULL, led_gpio_store), //写:关联属性文件的写回调函数(*store)  echo时触发
    __ATTR(smoke_sensor_gpio, 0664, smoke_sensor_gpio_show,NULL), //读:关联属性文件的读回调函数(*show) cat 时触发
};

static int  gpio_init_sysfs(struct device *dev)
{
  int i, ret;
  for (i = 0; i < ARRAY_SIZE(gpio_attr); i++) {
    ret = sysfs_create_file(&dev->kobj,  //创建设备的属性文件
			&gpio_attr[i].attr);
     if (ret){
	dev_err(dev, "create charger node(%s) error\n",gpio_attr[i].attr.name);
        return -1;
    }
  }
  return 0;
}

static int gpio_dt(struct gpio_dev *pdata) {
    int gpio;
    int ret;
    enum of_gpio_flags flags;
    struct device *dev = &pdata->pdev->dev;
    struct device_node *node = dev->of_node;
    
    gpio = of_get_named_gpio_flags(node, "led_gpio", 0, &flags);
    if (gpio_is_valid(gpio))
    {
	    pdata->led_gpio           = gpio;
	    pdata->led_gpio_direction = (flags == GPIO_ACTIVE_HIGH)? 1:0;	    
	    printk(KERN_ERR"led_gpio: %d\n", pdata->led_gpio);
	    
	    ret = devm_gpio_request(dev,gpio, "led_gpio");
	    if (ret) {
		printk("Failed to get led_gpio gpio.\n");
		return -1;
	    }
    }


    gpio = of_get_named_gpio_flags(node, "smoke_sensor_gpio", 0, &flags);
    if (gpio_is_valid(gpio))
    {
	pdata->smoke_sensor_gpio        = gpio;
	printk(KERN_ERR"smoke_sensor_gpio: %d\n", pdata->smoke_sensor_gpio);

	ret = devm_gpio_request(dev,gpio, "smoke_sensor_gpio");
	if (ret) {
	   printk("Failed to get smoke_sensor_gpio gpio.\n");
	   return -1;
	}

	ret = gpio_direction_input(gpio);
	if (ret) {
	   printk("Failed to set flame_sensor_gpio gpio.\n");
	   return -1;
	}
    }

    return 0;
}

static void gpio_set_default(struct gpio_dev *pdata) {
    if (pdata->led_gpio) {
        gpio_direction_output(pdata->led_gpio, pdata->led_gpio_direction);
    }
}


static int gpio_probe(struct platform_device *pdev) {
     int ret=0;	
     printk(KERN_ALERT "%s \n",__func__);      
     gpio_dev = kmalloc(sizeof(struct gpio_dev), GFP_KERNEL);
     if (gpio_dev == NULL) {
        printk(KERN_ERR"kmalloc struct gpio_dev err \n");
        return -ENOMEM;
     }

     memset(gpio_dev, 0, sizeof(struct gpio_dev));

     gpio_dev->pdev = pdev;

     ret = gpio_dt(gpio_dev);
     if(ret<0){
	  printk(KERN_ERR"gpio_dt err \n");	
	  return -1;
     }
     gpio_set_default(gpio_dev);
     ret = gpio_init_sysfs(&gpio_dev->pdev->dev);
     if(ret<0){
	  printk(KERN_ERR"gpio_init_sysfs  err \n");	
	  return -1;
     }	     
     printk(KERN_ALERT "%s  ok !!\n",__func__);
     return 0;
}

static int  gpio_remove(struct platform_device *pdev){
     kfree(gpio_dev);
     return 0;
}


static struct of_device_id gpio_of_match[] = {
	{ .compatible = "bbcen,sys_rw_gpio" },
	{ }
};

static struct platform_driver gpio_driver = {
	.driver		= {
		.name		= "sys_rw_gpio driver ",
		.of_match_table	= of_match_ptr(gpio_of_match),
	},
	.probe		= gpio_probe,
	.remove         = gpio_remove,
};

3.点多个灯(官方示例)

帮助文档

官方有驱动文件了,只需要参考帮助文档改设备树,make menuconfig对应选配,就能用了

//Documentation/devicetree/bindings/leds/leds-gpio.txt

设备树

//tegra210-p3448-0000-p3449-0000-b00.dts
yhai-gpio-led { 
  compatible = "gpio-leds"; 
  led1 {
  	label = "led1"; 
  	gpios = <&gpio TEGRA_GPIO(J,7) GPIO_ACTIVE_HIGH>;  //对应40pin 丝印 12脚  接第一个灯
  	default-state = "off"; 
  }; 
  led2 { 
  	label = "led2"; 
  	gpios = <&gpio TEGRA_GPIO(Z,0) GPIO_ACTIVE_HIGH>;  //对应40pin 丝印 31脚  接第二个灯
  	default-state = "off"; 
  }; 
  led3 { 
  	label = "led3"; 
  	gpios = <&gpio TEGRA_GPIO(S,5) GPIO_ACTIVE_HIGH>;  //对应40pin 丝印 29脚  接第三个灯
  	default-state = "off"; 
  }; 
};

官方驱动

//见drivers/leds/leds-gpio.c   

static const struct of_device_id of_gpio_leds_match[] = { 
    { .compatible = "gpio-leds", },
    {}, 
};

static struct platform_driver gpio_led_driver = {
    .probe      = gpio_led_probe,
    .driver     = {
        .name   = "leds-gpio",
        .of_match_table = of_gpio_leds_match,
    },
};
module_platform_driver(gpio_led_driver);

内核配置

$ cd ~/kernel-4.9
$ make menuconfig 
 Device Drivers ---> 
   [*] LED Support ---> 
     <*> LED Class Support 
     <*> LED Support for GPIO connected LEDs

验证测试

$ make dtbs
$ cp arch/arm64/boot/dts/tegra210-p3448-0000-p3449-0000-b00.dtb   /tftpboot/	
重启板子
# cd /sys/class/leds/
# echo 1 > led1/brightness //点亮第一个灯
# echo 0 > led1/brightness //灭第一个灯
# echo 1 > led2/brightness //点亮第二个灯
# echo 0 > led2/brightness //灭第二个灯
# echo 1 > led3/brightness //点亮第三个灯
# echo 0 > led3/brightness //灭第三个灯	

4.GPIO库gpiolib(对上统一操作接口)

//源码 drivers/gpio/gpiolib.c:  gpio子系统的核心实现,对外提供驱动API接口
EXPORT_SYMBOL_GPL(gpiod_get);
EXPORT_SYMBOL_GPL(gpiod_direction_output);
EXPORT_SYMBOL_GPL(gpiod_set_value);
//源码 drivers/gpio/gpiolib-sysfs.c
int gpiod_export(struct gpio_desc *desc, bool direction_may_change)
{ 
	device_create_with_groups(&gpio_class, &gdev->dev,
                    MKDEV(0, 0), data, gpio_groups,
                    ioname ? ioname : "gpio%u",  //创建 /sys/class/gpio/gpio79 文件夹
                    desc_to_gpio(desc));
}


static ssize_t export_store(struct class *class,
                struct class_attribute *attr,
                const char *buf, size_t len)
{
  gpio_to_desc(gpio);
  gpiod_request(desc, "sysfs");  //先申请(检测是否被占用)
  gpiod_export(desc, true); //导出 生成 /sys/class/gpio/gpio79 文件夹(包括direction  value等)
}

static struct class_attribute gpio_class_attrs[] = {
    __ATTR(export, 0200, NULL, export_store), //写:关联属性文件的写回调函数(*store)   echo 79 > /sys/class/gpio/export 时触发
    __ATTR(unexport, 0200, NULL, unexport_store),
    __ATTR_NULL,
};

static struct class gpio_class = {
    .name =     "gpio",
    .owner =    THIS_MODULE,
    .class_attrs =  gpio_class_attrs,
};

int gpiochip_sysfs_register(struct gpio_device *gdev)
{
	//创建 /sys/class/gpio/gpiochip0 文件夹
	device_create_with_groups(&gpio_class, parent,
	                    MKDEV(0, 0),
	                    chip, gpiochip_groups,
	                    "gpiochip%d", chip->base);	
}

static int __init gpiolib_sysfs_init(void)
{
	struct gpio_device *gdev;
  	class_register(&gpio_class); //注册 生成 /sys/class/gpio 文件夹

  	gpiochip_sysfs_register(gdev);
}

postcore_initcall(gpiolib_sysfs_init); //声明 在内核启动时自动加载 

5.GPIO设备树

//帮助文档  Documentation/devicetree/bindings/gpio/nvidia,tegra186-gpio.txt

//tegra210-soc-base.dtsi   cpu的基本配置
//位置: public_sources\hardware\nvidia\soc\t210\kernel-dts\tegra210-soc\
gpio: gpio@6000d000 {//gpio子节点
    compatible = "nvidia,tegra210-gpio", "nvidia,tegra124-gpio", "nvidia,tegra30-gpio";
    reg = <0x0 0x6000d000 0x0 0x1000>; //前2个数表示起始地址(由父节点的#address-cells = <2> 决定)
                                       //后2个数表示长度范围(由父节点的#size-cells = <2> 决定)
    //域名(0:spi 1:ppi) + 中断索引(36) + 触发方式(高电平触发) Documentation/devicetree/bindings/interrupt-controller/arm,gic.txt
    interrupts = <  0 32 0x04    //32: gpio编号  0x04:高电平触发中断 
                    0 33 0x04  
                    0 34 0x04
                    0 35 0x04
                    0 55 0x04
                    0 87 0x04
                    0 89 0x04
                    0 125 0x04>;
    status = "disabled";  //声明 本节点是禁用状态
    #gpio-cells = <2>;  //声明 引用本节点,用 gpios 属性传参时 参数占2个单位
    gpio-controller;  //声明本节点 是 GPIO 控制器 
    #interrupt-cells = <2>;

    interrupt-controller;  //声明本节点 是 中断 控制器 
    gpio-ranges = <&pinmux 0 0 246>;  //设置gpio number 与 pinctrl number的 映射,详见PinCtrl子系统
  };

6.GPIO芯片驱动gpio_chip_driver

( 由原厂开发)

//查看驱动信息
$ cat  /proc/devices  //查看内核加载的驱动设备
   254 gpiochip    
$ cat   /sys/class/gpio/gpiochip0/label //查看驱动设备的标签名
   tegra-gpio  //对应 gpio-tegra.c    的  tgi->gc.label   = "tegra-gpio";

//源码 gpio-tegra.c  英伟达原厂开发的芯片驱动

定义类

//gpio-tegra.c	
struct tegra_gpio_info { //定义gpio类
    struct device               *dev;
    void __iomem                *regs;
    struct irq_domain           *irq_domain;
    struct tegra_gpio_bank          *bank_info;
    const struct tegra_gpio_soc_config  *soc;
    struct gpio_chip            gc; //gpio控制器 
    struct irq_chip             ic; //irq控制器
    u32                 bank_count;
};
static struct tegra_gpio_info *gpio_info; //定义 gpio对象


static const struct of_device_id tegra_gpio_of_match[] = {               
    { .compatible = "nvidia,tegra210-gpio", .data = &tegra210_gpio_config },                                                           
};  

static struct platform_driver tegra_gpio_driver = {                      
    .driver     = {                                                      
        .name   = "tegra-gpio",                                          
        .of_match_table = tegra_gpio_of_match,
    },
    .probe      = tegra_gpio_probe,                                      
};      
    
static int __init tegra_gpio_init(void)                                  
{
    return platform_driver_register(&tegra_gpio_driver);                 
}
subsys_initcall(tegra_gpio_init);  //添加到内核启动列表中

构建对象(注册制)

  • devm_kzalloc会自动释放资源,常用
static int tegra_gpio_probe(struct platform_device *pdev)
{
	struct tegra_gpio_info *tgi;  //定义对象

	//为对象分配空间
	tgi = devm_kzalloc(&pdev->dev, sizeof(*tgi), GFP_KERNEL);
	if (!tgi)
	    return -ENODEV;
	gpio_info = tgi;

	//获取所有bank的中断资源
	for (;;) {
	    res = platform_get_resource(pdev, IORESOURCE_IRQ,   //见设备树 interrupts =<  0 32 0x04 ...>
	                    tgi->bank_count);
	    if (!res)
	        break;
	    tgi->bank_count++;
	}

	//初始化对象:里面的变量都赋值,指针都指向实体,
	tgi->gc.label           = "tegra-gpio";  //对应  /sys/class/gpio/gpiochip0/label
	tgi->gc.request         = tegra_gpio_request; //申请gpio(如果被占用,将申请失败)
	tgi->gc.free            = tegra_gpio_free; //释放gpio(释放后,别的驱动才能申请)
	tgi->gc.direction_input     = tegra_gpio_direction_input; //
	tgi->gc.get         = tegra_gpio_get;
	tgi->gc.direction_output    = tegra_gpio_direction_output;//gpio 方向设为输出
	tgi->gc.set         = tegra_gpio_set;
	
	//获取MEM资源
	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);  //见设备树 reg = <0x0 0x6000d000 0x0 0x1000>;
	tgi->regs = devm_ioremap_resource(&pdev->dev, res);
	if (IS_ERR(tgi->regs))
		return PTR_ERR(tgi->regs);	
	
	
	tegra_gpio_debuginit(tgi); //添加 调试功能
}	

static inline void tegra_gpio_writel(struct tegra_gpio_info *tgi,
				     u32 val, u32 reg)
{
	__raw_writel(val, tgi->regs + reg); /* 实现见  arch/arm64/include/asm/io.h
	
	  用C语言中嵌入汇编方式   ,用str  汇编指令 实现写数据
		static inline void __raw_writel(u32 val, volatile void __iomem *addr)
		{
			asm volatile("str %w0, [%1]" : : "rZ" (val), "r" (addr));
		}
			
	  */
}

static void tegra_gpio_mask_write(struct tegra_gpio_info *tgi, u32 reg,
				  int gpio, int value)
{
	tegra_gpio_writel(tgi, val, reg);
}

static void tegra_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
{
	//通过 GPIO_MSK_OUT 宏 实现各gpio 地址间转换关系(如不同端口 偏移量)
	tegra_gpio_mask_write(tgi, GPIO_MSK_OUT(tgi, offset), offset, value);
}

static int tegra_gpio_direction_output(struct gpio_chip *chip, unsigned offset,
                    int value)
{
    struct tegra_gpio_info *tgi = gpiochip_get_data(chip);
    int ret;

    tegra_gpio_set(chip, offset, value);
    tegra_gpio_mask_write(tgi, GPIO_MSK_OE(tgi, offset), offset, 1);
    tegra_gpio_enable(tgi, offset);

    ret = pinctrl_gpio_direction_output(chip->base + offset); //向pinctrl子系统 申请管脚 设为gpio输出模式

    return ret;
}

支持SYS控制

#ifdef	CONFIG_DEBUG_FS  //当打开 内核调试关,才把调试源码编译进来

#include <linux/debugfs.h>
#include <linux/seq_file.h>

static int dbg_gpio_open(struct inode *inode, struct file *file)
{
	return single_open(file, dbg_gpio_show, inode->i_private);
}

static const struct file_operations debug_fops = {
	.open		= dbg_gpio_open,
	.read		= seq_read,
	.llseek		= seq_lseek,
	.release	= single_release,
};

static void tegra_gpio_debuginit(struct tegra_gpio_info *tgi)
{
	(void) debugfs_create_file("tegra_gpio", S_IRUGO,  //创建调试文件 
					NULL, tgi, &debug_fops);
}

#else

static inline void tegra_gpio_debuginit(struct tegra_gpio_info *tgi)
{
}

#endif

寄存器自适应(管脚变了,不用再该代码,只需该改备树)

#include <linux/gpio.h>  //for gpio_chip

#define GPIO_BANK(x)        ((x) >> 5)
#define GPIO_PORT(x)        (((x) >> 3) & 0x3)
#define GPIO_BIT(x)     ((x) & 0x7)

#define GPIO_REG(tgi, x)    (GPIO_BANK(x) * tgi->soc->bank_stride + \
                    GPIO_PORT(x) * 4)

#define GPIO_CNF(t, x)      (GPIO_REG(t, x) + 0x00)
#define GPIO_OE(t, x)       (GPIO_REG(t, x) + 0x10)
#define GPIO_OUT(t, x)      (GPIO_REG(t, x) + 0X20)
#define GPIO_IN(t, x)       (GPIO_REG(t, x) + 0x30)
#define GPIO_INT_STA(t, x)  (GPIO_REG(t, x) + 0x40)
#define GPIO_INT_ENB(t, x)  (GPIO_REG(t, x) + 0x50)
#define GPIO_INT_LVL(t, x)  (GPIO_REG(t, x) + 0x60)
#define GPIO_INT_CLR(t, x)  (GPIO_REG(t, x) + 0x70)
#define GPIO_DBC_CNT(t, x)  (GPIO_REG(t, x) + 0xF0)


#define GPIO_MSK_CNF(t, x)  (GPIO_REG(t, x) + t->soc->upper_offset + 0x00)
#define GPIO_MSK_OE(t, x)   (GPIO_REG(t, x) + t->soc->upper_offset + 0x10)
#define GPIO_MSK_OUT(t, x)  (GPIO_REG(t, x) + t->soc->upper_offset + 0X20)
#define GPIO_MSK_DBC_EN(t, x)   (GPIO_REG(t, x) + t->soc->upper_offset + 0x30)
#define GPIO_MSK_INT_STA(t, x)  (GPIO_REG(t, x) + t->soc->upper_offset + 0x40)
#define GPIO_MSK_INT_ENB(t, x)  (GPIO_REG(t, x) + t->soc->upper_offset + 0x50)
#define GPIO_MSK_INT_LVL(t, x)  (GPIO_REG(t, x) + t->soc->upper_offset + 0x60)

#define GPIO_INT_LVL_MASK           0x010101
#define GPIO_INT_LVL_EDGE_RISING    0x000101
#define GPIO_INT_LVL_EDGE_FALLING   0x000100
#define GPIO_INT_LVL_EDGE_BOTH      0x010100
#define GPIO_INT_LVL_LEVEL_HIGH     0x000001
#define GPIO_INT_LVL_LEVEL_LOW      0x000000	

struct tegra_gpio_soc_config {
	bool debounce_supported;
	u32 bank_stride;
	u32 upper_offset;
};

static const struct tegra_gpio_soc_config tegra210_gpio_config = {
	.debounce_supported = true,
	.bank_stride = 0x100,
	.upper_offset = 0x80,
};

static const struct of_device_id tegra_gpio_of_match[] = {
	{ .compatible = "nvidia,tegra210-gpio", .data = &tegra210_gpio_config },
}

/*打印 各bank gpio 寄存器列表的值 -> 对应芯片手册里 
	#define GPIO3  	0x6000D200 // 第3个Bank GPIO 的基地址
	//---偏移量
	#define CNF		0x04  //配置寄存器 (0:GPIO  1:SFIO)
	#define OE   	0x14  //输出使能寄存器 (1:使能 0:关闭)
	#define OUT  	0x24  //输出寄存器(1:高电平 0:低电平)
	#define MSK_CNF 0x84  //配置屏蔽寄存器(高位1:屏蔽 高位0:不屏蔽   低位1:GPIO模式 低位0:SFIO模式)
	#define MSK_OE  0x94  //输出使能屏蔽寄存器(高位1:禁止写   低位1:使能)
	#define MSK_OUT 0xA4  //输出屏蔽寄存器(高位1:禁止写   低位1:高电平)
*/
static int dbg_gpio_show(struct seq_file *s, void *unused)
{
	struct tegra_gpio_info *tgi = s->private;
	int i;
	int j;
	char x, y;

	x = ' ';
	y = 'A';

	seq_printf(s, "Name:Bank:Port CNF OE OUT IN INT_STA INT_ENB INT_LVL\n");
	for (i = 0; i < tgi->bank_count; i++) {
		for (j = 0; j < 4; j++) {
		int gpio = tegra_gpio_compose(i, j, 0);

		seq_printf(s,
		     "%c%c: %d:%d %02x %02x %02x %02x %02x %02x %06x\n",
			x, y, i, j,
			tegra_gpio_readl(tgi, GPIO_CNF(tgi, gpio)), //用GPIO_CNF 宏,实现各gpio 地址间转换关系(如不同端口 偏移量)
			tegra_gpio_readl(tgi, GPIO_OE(tgi, gpio)),
			tegra_gpio_readl(tgi, GPIO_OUT(tgi, gpio)),
			tegra_gpio_readl(tgi, GPIO_IN(tgi, gpio)),
			tegra_gpio_readl(tgi, GPIO_INT_STA(tgi, gpio)),
			tegra_gpio_readl(tgi, GPIO_INT_ENB(tgi, gpio)),
			tegra_gpio_readl(tgi, GPIO_INT_LVL(tgi, gpio)));

		if (x != ' ')
			x++;
		if (y == 'Z') {
			y = 'A';
			x = 'A';
		} else {
			y++;
		};
		}
	}
	return 0;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/10196.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

Spark对正常日志文件清洗并分析

目录 日志文件准备&#xff1a; 一.日志数据清洗&#xff1a; 第一步&#xff1a;数据清洗需求分析&#xff1a; 二.代码实现 2.1 代码和其详解 2.2创建jdbcUtils来连接Mysql数据库 2.3 运行后结果展示&#xff1a; 三、留存用户分析 3.1需求概览 3.2.代码实现 3…

T 级数据量迁移!知名云巨头如何从 Jira 切换至 ONES?

2021 年&#xff0c;Atlassian 旗下 Jira&Confluence 等系列产品 Server 版&#xff08;本地私有化部署版&#xff09;全面停售&#xff0c;并将在 2024 年停止维护&#xff0c;Server 版客户必须迁移至 Cloud&#xff08;云&#xff09;或 Data Center&#xff08;数据中心…

全网最详细,Jmeter性能测试-性能基础详解,控制器不同选择(四)

目录&#xff1a;导读前言一、Python编程入门到精通二、接口自动化项目实战三、Web自动化项目实战四、App自动化项目实战五、一线大厂简历六、测试开发DevOps体系七、常用自动化测试工具八、JMeter性能测试九、总结&#xff08;尾部小惊喜&#xff09;前言 逻辑控制器 提前说…

一篇文章让你搞懂TypeScript中的??和?:和?.和!.是什么意思

TypeScript中的??和?:和?.和!.是什么意思&#xff1f;知识回调&#xff08;不懂就看这儿&#xff01;&#xff09;场景复现核心干货???:?.!.知识回调&#xff08;不懂就看这儿&#xff01;&#xff09; 知识专栏专栏链接TypeScript知识专栏https://blog.csdn.net/xsl_…

Scrapy爬虫框架(概念)

Scrapy 入门教程 | 菜鸟教程 (runoob.com) Scrapy是一个快速功能强大的网络爬虫框架 Scrapy的安装 通过 pip 安装 Scrapy 框架: pip install Scrapy 安装后小测&#xff1a; 执行 scrapy ‐h Scrapy不是一个函数功能库&#xff0c;而是一个爬虫框架。 Scrapy架构图(绿线是…

CentOS7 虚拟机 双网卡绑定

一、网卡绑定模式 模式类型特点mode0round-robin&#xff08;平衡轮询策略&#xff09;基于per packet方式&#xff0c;轮询往每条链路发送报文。提供负载均衡和容错的能力&#xff0c;当有链路出问题&#xff0c;会把流量切换到正常的链路上。交换机端需要配置聚合口。mode1a…

【论文笔记】CRN: Camera Radar Net for Accurate, Robust, Efficient 3D Perception

原文链接&#xff1a;https://arxiv.org/abs/2304.00670 1. 引言 本文提出两阶段融合方法CRN&#xff0c;能使用相机和雷达生成语义丰富且位置精确的BEV特征。具体来说&#xff0c;首先将图像透视特征转换到BEV下&#xff0c;该步骤依赖雷达&#xff0c;称为雷达辅助的视图变换…

C#基础复习

语句 目录 语句 switch&#xff1a; 跳转语句 标签语句 标签&#xff1a; 标签语句的作用域 goto语句 using 语句 资源的包装使用 using 语句示例&#xff1a; 多个资源和嵌套 语句是描述某个类型或让程序执行某个动作的源代码指令 块在语法上算作一个单条嵌入语句。任何语…

电脑无法正常关机?点了关机又会自动重启

“真木马”相信不少朋友遇到过电脑关机自动重启现象&#xff0c;一点关机&#xff0c;但随后电脑有会进入重启状态&#xff0c;就是一直不会停&#xff0c;属实是很难崩。 目录 一、问题症状 二、问题原因 三、解决方案 方法一&#xff1a; 1.关闭系统发生错误时电脑自动…

企业数字化转型全是坑?这几篇数字化转型成功案例,减少70%损失

这篇给大家整理了200企业数字化转型案例合集&#xff0c;涵盖了制造、建筑、教育、零售、互联网等10行业的大中小型企业数字化转型思路&#xff0c;希望对大家有所帮助。 案例全部整合在这篇文章中&#xff0c;点击即可查看>>数字化干货资料合集&#xff01; 01 首先&…

C++编程法则365条一天一条(359)认识各种初始化术语

文章目录Default initialization默认初始化Copy initialization拷贝初始化Aggregate initialization聚合初始化Direct initialization直接初始化list_initialization列表初始化value_initialization值初始化参考&#xff1a; https://en.cppreference.com/w/cpp/language/copy_…

项目打包发布流程

---》》》项目打包发布 1.编译并构建项目 2.部署 npm i npm run build scp2&#xff1a;需要写代码 ---》》》 后续有空更新&#xff1a;赋几个链接&#xff1a; Jenkins官网 nullhttps://www.jenkins.io/zh/一文详解Jenkins的安装与配置Jenkins是一个基于Java开发的开源…

ERROR:org.apache.hadoop.hbase.PleaseHoldException: Master is initializing错误

一、问题 重新安装hbase后&#xff0c;在hbase shell中查看所有命名空间时&#xff0c;出现了ERROR:org.apache.hadoop.hbase.PleaseHoldException: Master is initializing错误。 二、方法 1、root用户下&#xff0c;关闭hbase stop-hbase.sh 2、执行以下命令删除HDFS下的hb…

深度学习环境配置超详细教程【Anaconda+PyTorch(GPU版)+CUDA+cuDNN】

深度学习环境配置 入门深度学习&#xff0c;首先要做的事情就是要搭建深度学习的环境。不管你是Windows用户&#xff0c;Mac用户还是Ubuntu用户&#xff0c;只要电脑配置允许&#xff0c;都可以做深度学习&#xff0c;毕竟Windows、Mac和Ubuntu系统都可以进行深度学习环境的搭…

惊呆了,2小时我就学会了Charles抓包的详细教程

目录 一、什么是Charles 二、下载Charles 三、设置Charles代理 四、配置设备代理 五、抓包操作 六、常见问题及解决方法 抓包不到某些应用程序 Charles抓包后网站出现异常 七、总结 一、什么是Charles Charles是一个跨平台的HTTP代理服务工具&#xff0c;可以用来查看…

软件测试工作主要做什么

随着信息技术的发展和普及&#xff0c;人们对软件的使用越来越普及。但是在软件的使用过程中&#xff0c;软件的效果却不尽如人意。为了确保软件的质量&#xff0c;整个软件业界已经逐渐意识到测试的重要性&#xff0c;也有越来越多的小伙伴加入了软件测试这个行业中来。软件测…

从FPGA说起的深度学习(六)-任务并行性

这是新的系列教程&#xff0c;在本教程中&#xff0c;我们将介绍使用 FPGA 实现深度学习的技术&#xff0c;深度学习是近年来人工智能领域的热门话题。在本教程中&#xff0c;旨在加深对深度学习和 FPGA 的理解。用 C/C 编写深度学习推理代码高级综合 (HLS) 将 C/C 代码转换为硬…

ServletAPI详解(四)-HttpServletResponse

我们来看第三个方法,HttpServletResponse 在servlet运行原理中提到,servlet代码中的doXXX方法的目的就是根据请求计算响应,然后将响应数据设置到HttpServletResponse对象中,然后 Tomcat 就会把这个 HttpServletResponse 对象按照 HTTP 协议的格式, 转成一个字符串, 并通过 Soc…

Linux Shell 实现一键部署二进制Rabbitmq

rabbitmq 前言 RabbitMQ是实现了高级消息队列协议&#xff08;AMQP&#xff09;的开源消息代理软件&#xff08;亦称面向消息的中间件&#xff09;。RabbitMQ服务器是用Erlang语言编写的&#xff0c;而集群和故障转移是构建在开放电信平台框架上的。所有主要的编程语言均有与代…

MPC的560x系列的运行模式的介绍

一、模式简介 1、运行模式 一共11种模式&#xff0c;分别为RESET、DRUN、SAFE、TEST、RUN0、RUN1、RUN2、RUN3、HALT、STOP、STANDBY。其中RESET、DRUN、SAFE、TEST是系统工作模式&#xff0c;用户不用个特别关系&#xff0c;而后面几种是用于经常使用到的工作模式。 RESET&a…
最新文章