1. 程式人生 > 其它 >【GPIO】linux中GPIO相關函式介紹

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