RT-Thread RTOS
An open source embedded real-time operating system
|
The pins on the chip are generally divided into four categories: power supply, clock, control, and I/O. The I/O pins are further divided into General Purpose Input Output (GPIO) and function-multiplexed I/O (such as SPI/I2C/UART, etc.) pins, referring to their usage mode.
Most MCU pins have more than one function. Their internal structure is different and their supported functionality are different. The actual function of the pin can be switched through different configurations. The main features of the General Purpose Input Output (GPIO) port are as follows:
Programmable Interrupt: The interrupt trigger mode is configurable. Generally, there are five interrupt trigger modes as shown in the following figure:
The application accesses the GPIO through the PIN device management interface provided by RT-Thread. The related interfaces are as follows:
Function | Description |
---|---|
rt_pin_mode() | Set pin mode |
rt_pin_write() | Set the pin level |
rt_pin_read() | Read pin level |
rt_pin_attach_irq() | Bind pin interrupt callback function |
rt_pin_irq_enable() | Enable pin interrupt |
rt_pin_detach_irq() | Detach pin interrupt callback function |
The pin numbers provided by RT-Thread need to be distinguished from the chip pin numbers, which not the same. The pin numbers are defined by the PIN device driver and are related to the specific chip used. There are two ways to obtain the pin number: use the macro definition or view the PIN driver file.
If you use the BSP in the rt-thread/bsp/stm32
directory, you can use the following macro to obtain the pin number:
The sample code for the pin number corresponding to LED0 with pin number PF9 is as follows:
If you use a different BSP, you will need to check the PIN driver code drv_gpio.c
file to confirm the pin number. There is an array in this file that holds the number information for each PIN pin, as shown below:
Take __STM32_PIN(2, A, 15)
as an example, 2 is the pin number used by RT-Thread, A is the port number, and 15 is the pin number, so the pin number corresponding to PA15 is 2.
Before the pin is used, you need to set the input or output mode first, and the following functions are used:
Parameter | Discription |
---|---|
pin | Pin number |
mode | Pin operation mode |
At present, the pin working mode supported by RT-Thread can take one of the five macro definition values as shown. The mode supported by the chip corresponding to each mode needs to refer to the specific implementation of the PIN device driver:
An example of use is as follows:
The function to set the pin output level is as follows:
Parameter | Discription |
---|---|
pin | Pin number |
value | Level logic value, which can take one of two macro definition values: PIN_LOW means low level, or PIN_HIGH means high level |
Examples of use are as follows:
The functions to read the pin level are as follows:
Parameter | Description |
---|---|
pin | Pin number |
return | —— |
PIN_LOW | Low level |
PIN_HIGH | High level |
Examples of use are as follows:
To use the interrupt functionality of a pin, you can use the following function to configure the pin to some interrupt trigger mode and bind an interrupt callback function to the corresponding pin. When the pin interrupt occurs, the callback function will be executed.:
Parameter | Description |
---|---|
pin | Pin number |
mode | Interrupt trigger mode |
hdr | Interrupt callback function. Users need to define this function |
args | Interrupt the parameters of the callback function, set to RT_NULL when not needed |
return | —— |
RT_EOK | Binding succeeded |
error code | Binding failed |
Interrupt trigger mode mode can take one of the following five macro definition values:
Examples of use are as follows:
After binding the pin interrupt callback function, use the following function to enable pin interrupt:
Parameter | Description |
---|---|
pin | Pin number |
enabled | Status, one of two values: PIN_IRQ_ENABLE, and PIN_IRQ_DISABLE |
return | —— |
RT_EOK | Enablement succeeded |
error code | Enablement failed |
Examples of use are as follows:
You can use the following function to detach the pin interrupt callback function:
Parameter | Description |
---|---|
pin | Pin number |
return | —— |
RT_EOK | Detachment succeeded |
error code | Detachment failed |
After the pin detaches the interrupt callback function, the interrupt is not closed. You can also call the bind interrupt callback function to bind the other callback functions again.
The following sample code is the pin device usage example. The main steps of the sample code are as follows: