Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How GPIO is mapped in memory?

I am recently browsing GPIO driver for pi2, I found user space pi2 GPIO lib (like RPi.GPIO 0.5.11 of python) use /dev/mem for BCM2708 (begins at 0x20000000,and GPIO begins at 0x200000 relatively) to mmap a user space memory region in order to handler GPIO. But I found drivers/gpio in linux source tree is designed to be handled by /sys/class/gpio/*. I found nothing like I/O ports mapping like request_io_region and __io_remap. My question is How GPIO for BCM2708 mapped in memory ? Is there another driver? And can I handle GPIO just by R&W to /sys/class/gpio/*?

like image 905
ggaaooppeenngg Avatar asked Jul 25 '15 03:07

ggaaooppeenngg


People also ask

What is GPIO mapping?

A general-purpose input/output (GPIO) is an uncommitted digital signal pin on an integrated circuit or electronic circuit board which may be used as an input or output, or both, and is controllable by software. GPIOs have no predefined purpose and are unused by default.

How do GPIO pins work?

A GPIO pin is a generic pin whose value consists of one of two voltage settings (high or low) and whose behavior can be programmed through software. A GPIO port is a platform-defined grouping of GPIO pins (often 4 or more pins).

How many pins are assigned as GPIO?

The GPIO has 54 general-purpose I/O pins, which are controlled by 41 32-bit registers. The function of each pin is selected through six of these registers, GPFSEL0 – GPFSEL5 . Three bits are used to select the function of a GPIO pin, so each 32-bit register selects the function of ten pins, with two unused bits.

Which module is used to access GPIO pins *?

Raspberry-gpio-python or RPi. GPIO, is a Python module to control the GPIO interface on the Raspberry Pi.


1 Answers

I found nothing like I/O ports mapping like request_io_region and __io_remap.

ARM does not have an I/O port space. All peripheral registers are assigned to addresses in memory space.

How GPIO for BCM2708 mapped in memory ?

GPIOs are typically implemented as a peripheral of control registers, and the GPIOs in the BCM2835 of the RPi follows this convention. This set of control register may have a different name; for example Atmel refers to these registers as the Parallel I/O (PIO) peripheral.

Each GPIO (or more accurately each pin) will be represented by one or more bits in each control register function. The control register functions include pin assignment (aka multiplexing), set output to high, set output to low, read pin level, and level and edge detection control.

IOW there is no single bit that can be read and written that corresponds to a GPIO. For a GPIO there would be a bit in a specific register to fetch the input level. There's a bit in another register to set that GPIO output high, and bit in another register to set that GPIO output low.

Is there another driver?

Yes. The pinctrl (pin control) driver is a lower-layer (i.e. closer to the HW) than GPIO. It's the pinctrl layer that handles pin multiplexing (i.e. whether a pin is used for a peripheral function or as a GPIO).
The pinctrl driver for the SoC (e.g. drivers/pinctrl/pinctrl-bcm2835.c) is where you would find devm_ioremap_resources() (which in turn calls devm_request_mem_region() and devm_ioremap()) for the GPIO register block.

And can I handle GPIO just by R&W to /sys/class/gpio/*?

Yes. the sysfs interface is provided for accessing those pins not assigned to peripherals.

ADDENDUM
The sysfs GPIO interface has limited capabilities.
Apparently there are userspace libraries to access additional pin attributes (e.g. enabling pull-up or pull-down resistor) that are normally in the domain of the pinctrl driver. Typically such libraries access the PIO hardware registers directly through the /dev/mem psuedo-file. Be cognizant that such techniques are not secure and could interfere with other device drivers.

like image 61
sawdust Avatar answered Nov 13 '22 01:11

sawdust