【GPIO】linux中GPIO相关函数介绍

原文链接

1、设置GPIO口方向

int gpiod_direction_input(struct gpio_desc *desc)
int gpiod_direction_output(struct gpio_ desc *desc, int value) 

2、获取GPIO口方向

int gpiod_get_direction(const struct gpio_desc *desc)huoqu
  • 函数返回GPIOF_DIR_ IN或者GPIOF_DIR_OUT

3、读取GPIO口电平

访问分为以下两种

(1)一种是通过储存器读写实现的,这种操作属于原子操作,不需要等待,所以可以在中断处理程序中使用:.

int gpiod_get_value(const struct gpio_ _desc *desc);
void gpiod_set_value(struct gpio_ desc *desc, int value);

(2)还有一种访问必须通过消息总线比如I2C或者SPI,这种访问需要在总线访问队列中等待,所以可能进入睡眠,此类访问不能出现在IRQ handler。 可以使用如下函数分辨这些设备:

int gpiod_cansleep(const struct gpio_desc *desc)

使用如下函数读写:

int gpiod_ get_value_cansleep(const struct gpio_desc *desc)
void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)

4、#active-low和raw-value

active-low & raw value有些设备采用低电平有效的方式输出逻辑信号。此时低电平输出1,高电平输出0。此时可以通过访问raw_ value 的方式来访问,实际电路上的值,与逻辑处理无关:假设我们在DTS里面这样设置

reset-gpios = <&gpio3 RK_PA3 GPIO_ACTIVE_LOW>;

然后我们这样调用

gpiod_set_value_ cansleep( gc5025->reset_gpio, 1);

 因为DTS里面的active 状态是GPI0_ ACTIVE_ LOW, 所以这个代码输出的是低电平

gpiod_set_value_cansleep( gc5025->reset_gpio, 0); 

输出的是高电平
这几个函数如下:

int gpiod_ get_ raw_value(const struct gpio_desc *desc)
void gpiod_set_ raw_value(struct gpio_desc *desc, int value)
int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc)
void gpiod_set_ raw_value_cansleep(struct gpio_desc *desc, int value)
int gpiod_direction_ output_raw(struct gpio_desc *desc, int value)
raw- value的意思就是不在乎DTS里面的ACTIVE,我set高电平,就是高电平。
逻辑关系汇总如下:
Function (example) active-low property physical line
gpiod_set_raw_value(desc, 0);     //don 't care low
gpiod_ set_ raw_ value(desc, 1);   //don't care high
gpiod_ _set_ value(desc, 0);        //default (active -high) low
gpiod_ set_ value(desc, 1);         //default (active-high) high
gpiod_ _set_ value(desc, 0);       //active-low high
gpiod_ set_ _value(desc, 1);       //active-low low
//可以使用如下函数判断一个设备是否是低电平有效的设备。
int gpiod_is_active_low(const struct gpio_desc *desc)

5、設置多个输出

这个没使用过使用如下函数设置一组设备的输出值

void gpiod_set_array_value(unsigned int array_size,
struct gpio_desc **desc_array,
int *value_array)
void gpiod_set_raw_array_value(unsigned int array_size,
struct gpio_desc **desc_array,
int *value_array)
void gpiod_set_array_value_cansleep(unsigned int array_size,
struct gpio_desc **desc_array,
int *value_array)
void gpiod_set_raw_array_value_cansleep(unsigned int array_size,struct gpio_desc **desc_array,int *value_array)

 

  

  

  

原文地址:https://www.cnblogs.com/yuanqiangfei/p/15572865.html