The gpio interface is used for data interaction with hardware; gpio is the abbreviation of "General Purpose I/O Ports", which means general-purpose input and output ports. Its pins can be freely used by users through program control. You can output high and low levels through the interface or read whether the status of the pin is high level or low level.
The operating environment of this tutorial: Windows 10 system, DELL G3 computer.
GPIO, the abbreviation for general-purpose input and output, has functions similar to P0-P3 of 8051. Its pins can be freely used by the user through program control. The PIN pin is Practical considerations can be used as general input (GPI) or general output (GPO) or general input and output (GPIO), such as clk generator, chip select, etc.
GPIO (General Purpose I/O Ports) means general Input/output ports, in layman's terms, are pins through which high and low levels can be output or through which the status of the pin can be read - whether it is high level or low level.
GPIO port one is a relatively important concept. Users can interact with hardware through GPIO port for data interaction (such as UART), control hardware work (such as LED, buzzer, etc.), and read the working status signal of the hardware. (such as interrupt signal) etc. GPIO ports are widely used.
How to use the gpio interface
To use GPIO, the system must first allocate a GPIO, and use gpio_request() to allocate a GPIO to the system.
The next thing to do is to mark the direction of the GPIO, usually when using GPIO to create a platform_device (located in the setup code of the board):
/* set as input or output, returning 0 or negative errno */ int gpio_direction_input(unsigned gpio); int gpio_direction_output(unsigned gpio, int value);
Return 0 to indicate success. Or a negative errno error code. It should be checked because get/set calls do not return errors and there may be misconfigurations. You should generally use these calls in a thread context. However, for spinlock-safe GPIO, it is also possible to use it before tasking is enabled, as an early single board build.
For output GPIO, the value parameter provides the initial output value. This helps avoid signal interference during system startup.
In order to be compatible with the early GPIO interface, setting a GPIO direction implicitly requires applying for GPIO. This compatibility is removed from the optional gpiolib architecture.
If the GPIO number is invalid or the specified GPIO cannot operate in the corresponding mode, setting the direction will fail. It is usually not a good idea to rely on the boot firmware to set the GPIO direction, because the boot functionality may not be verified (except for boot linux). (Similarly, the single-board setup code may need to multiplex the pin as a GPIO and configure the appropriate pull-up/pull-down.)
Spinlock-Safe GPIO Access
Most GPIO The controller can be accessed using memory read and write instructions. They do not require sleep and can be completed safely from internal hardware interrupt handling (non-threaded) and similar contexts.
Use the following calls to access these GPIOs. At this time, gpio_cansleep will always return an error
/* GPIO INPUT: return zero or nonzero */ int gpio_get_value(unsigned gpio); /* GPIO OUTPUT */ void gpio_set_value(unsigned gpio, int value);
where value is a Boolean parameter, zero means low, and non-zero means high. When reading the value of an output pin, the value returned should be the value seen on the pin. . . This does not always match the specified output value because of open-drain signaling and output delay issues.
The get/set calls return no errors because "invalid GPIO" should have been reported early by gpio_direction_*(). Nonetheless, not all platforms can read the value of an output pin, and those that cannot should always return zero. At the same time, it is a mistake to use these interfaces with GPIOs that may cause sleep.
Platform specific implementations are encouraged to optimize these two calls to obtain GPIO values. In those cases where the GPIO numbers are constant, they usually require only a pair of instructions (read or write a hardware register) to access, and no spinlock is required. Such optimizations can make bit-splitting applications more efficient (in time and space) (compared to spending a bunch of instructions on subroutine calls).
What does the gpio interface do?
The output value can be written (high=1, low=0 ). Some chips can also choose how they drive these values to support a "wired-OR" or similar scheme (open-drain signal lines).
The input value is readable (1, 0). Some chips support output pin readback, which is useful in wire-OR situations (to support bidirectional signal lines). The GPIO controller may have an input failsafe/antibounce logic and sometimes software control.
Inputs are often used as interrupt signals, usually edge triggered, but may also be level triggered. These interrupts can be configured as system wake-up events to wake the system from low-power modes.
A GPIO is often configured as bidirectional input/output, depending on the requirements of different product boards, but there are also unidirectional situations.
Most GPIOs can be accessed when acquiring the spinlock, but those accessed through the serial bus usually cannot be operated in this way (reasons for hibernation). Both forms of GPIO exist in some systems.
On a given board, each GPIO is used for a specific purpose, such as monitoring MMC/SD card insertion/removal, checking card write protection status, driving LEDs, configuring transmitters, serial bus bit removal , trigger a hardware watchdog, trigger a switch or the like.
For more related knowledge, please visit the FAQ column!
The above is the detailed content of What does the gpio interface do?. For more information, please follow other related articles on the PHP Chinese website!