https://falsinsoft.blogspot.tw/2012/11/access-gpio-from-linux-user-space.html
https://elinux.org/GPIO




Linux 提供標準的 gpio 使用方法,以下是 TI 
reference
http://processors.wiki.ti.com/index.php/Sitara_GPIO_Driver_User_Guide#Useful_docs
http://www.mjmwired.net/kernel/Documentation/gpio.txt


Driver Configuration
This section describes about the kernel configurations for GPIO driver
The default kernel configuration enables support for GPIO driver (built into the kernel).
To enable or disable GPIO driver from kernel build, follow these steps:
$ make CROSS_COMPILE=arm-none-linux-gnueabi- ARCH=arm menuconfig
  • Select Device Drivers from the main menu.
    Power management options  --- >
[*] Networking support  --- >
    Device Drivers  --- >
    File systems  --- >
...
...
  • Select GPIO Support from the menu.
    PPS support  --- >
    PTP clock support  --- >
-*- GPIO Support  --- >
< > Dallas's 1-wire support  --- >
...
...
 

Sysfs entries configuration

GPIO can be access using SYSFS entries from User Space. For that Select /sys/class/gpio/... (sysfs interface) from the GPIO support.
[ ] Debug GPIO calls
[*] /sys/class/gpio/... (sysfs interface)
    *** Memory mapped GPIO drivers: ***
< > Basic memory-mapped GPIO controllers support
...
...
  • After doing driver selection, exit and save the kernel configuration when prompted.
 

IRQ handling

GPIO pin is also used as a interrupt source, these are the general usage of IRQ handling using GPIO lines.
  • Map GPIO number to corresponding IRQ number, GPIO 0 need not use IRQ 0
irq_num = gpio_to_irq(30)
  • Request IRQ, make sure that irq_num should be non-error value
request_irq(irq_num, handler, 0, "gpio_test", NULL);
  • Set IRQ type Raising/Falling/Level triggered
set_irq_type(irq_num, IRQ_TYPE_EDGE_RISING);
  • During the clean-up path free the IRQ and gpio
free_irq(irq_num, NULL);
gpio_free(30);
 

Driver Usage

Kernel Level

  • Allocate memory to GPIO line, can be achieved by doing gpio_request()
err = gpio_request(30, "sample_name");

  • Depending on the requirement set GPIO as input or output pin then set gpio value as high or low.
Setting the GPIO pin 30 as input
gpio_direction_input(30);
Make pin 30 as output and set the value as high.
gpio_direction_output(30, 1);
Exporting that particular pin (30) to sysfs entry then use this API
gpio_export(30, true);
Get value from GPIO pin
gpio_get_value(30);
 

User Space - Sysfs control

  • Enable GPIO sysfs support in kernel configuration and build the kernel
Device Drivers  --- > GPIO Support  --- > /sys/class/gpio/... (sysfs interface)
  • Sysfs entries
Export the particular GPIO pin for user control. GPIO30 is taken as example.
$ echo 30 > /sys/class/gpio/export
Change the GPIO pin direction to in/out
$ echo "out" > /sys/class/gpio/gpio30/direction
or
$ echo "in" > /sys/class/gpio/gpio30/direction
Change the value
$ echo 1 > /sys/class/gpio/gpio30/value
or
$ echo 0 > /sys/class/gpio/gpio30/value
Unexport the GPIO pin
$ echo 30 > /sys/class/gpio/unexport
Note: GPIO's which are used already in the drivers can not be control from sysfs, unless until driver export that particular pin.
Run these commands for knowing what are the GPIO's already requested in the drivers.
$ mount -t debugfs debugfs /sys/kernel/debug
$ cat /sys/kernel/debug/gpio

 

最後這個是重點!  因為有些driver 沒按照devicetree 架構寫 而是在驅動內自行設定GPIO 所以有時須要看一下哪些IO已經被要走了! 避免devicetree 寫半天 但還是有問題的狀況

# cat /sys/kernel/debug/gpio
GPIOs 0-31, platform/pinctrl, gpio0:
 gpio-1   (                    |vcc_sd              ) out lo
 gpio-4   (                    |bt_default_wake_host) out hi
 gpio-5   (                    |GPIO Key Power      ) in  hi
 gpio-8   (                    |cx20921 rstn        ) out hi
 gpio-9   (                    |bt_default_reset    ) out hi
 gpio-10  (                    |reset               ) out hi
 gpio-11  (                    |SPK_EN              ) out hi

GPIOs 32-63, platform/pinctrl, gpio1:
 gpio-33  (                    |vbus-5v             ) out lo
 gpio-34  (                    |int-n               ) in  hi
 gpio-45  (                    |enable              ) out lo
 gpio-46  (                    |vsel                ) out lo
 gpio-49  (                    |vsel                ) out lo

GPIOs 64-95, platform/pinctrl, gpio2:
 gpio-83  (                    |bt_default_rts      ) out lo
 gpio-90  (                    |bt_default_wake     ) out hi
 gpio-92  (                    |WLAN_PEN            ) out lo

GPIOs 96-127, platform/pinctrl, gpio3:
 gpio-111 (                    |mdio-reset          ) out hi

GPIOs 128-159, platform/pinctrl, gpio4:
 gpio-149 (                    |AUDIO_GPIO          ) out hi
 gpio-150 (                    |wdt87xx_Reset_Pin   ) out hi
 gpio-153 (                    |vcc5v0_host         ) out hi
 gpio-157 (                    |enable              ) out lo
 gpio-158 (                    |enable              ) out hi

 

 

 
arrow
arrow
    全站熱搜

    立你斯 發表在 痞客邦 留言(1) 人氣()