【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)

 

  

  

 

  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM