|
|
A **GPIO** is a software-controllable peripheral used to configure the device IO ports, also called pins.
|
|
|
|
|
|
A **GPIO** is a software-controllable peripheral used to configure the device IO ports, also called pins.
|
|
|
|
|
|
GPIOs are grouped in ports and so each GPIO has an identifier.
|
|
|
The identifiers have the following format: `PXY`, where `X` is the GPIO port name and `Y` is the pin number.
|
|
|
|
... | ... | @@ -21,16 +21,16 @@ typedef struct |
|
|
volatile uint32_t AFR[2];
|
|
|
} GPIO_TypeDef;
|
|
|
```
|
|
|
Remmeber that strcut member's are allocated to consecutive addresses in memory.
|
|
|
|
|
|
Remember that strcut members are allocated to consecutive addresses in memory.
|
|
|
|
|
|
In order to access a specific register (address) we need a pointer to a `GPIO_TypeDef` structure.
|
|
|
Assuming we want to control the `GPIOA` port and that it can be found at `0x48000000`:
|
|
|
```cpp
|
|
|
#define GPIOA ((GPIO_TypeDef *) 0x48000000)
|
|
|
```
|
|
|
By simply changing the pointer address we can access to different GPIO ports.
|
|
|
|
|
|
Via *bitwise operations* we can modify single bits of the GPIO registers:
|
|
|
By simply changing the pointer address we can access to different GPIO ports.
|
|
|
|
|
|
Via _bitwise operations_ we can modify single bits of the GPIO registers:
|
|
|
```cpp
|
|
|
// set 5th bit of ODR to 1
|
|
|
GPIOA->ODR |= (1 << 4);
|
... | ... | @@ -51,25 +51,24 @@ GPIOA->ODR |= (1 << led_pin); |
|
|
where `led_pin` is the pin number.
|
|
|
|
|
|
## Configuration
|
|
|
> :warning: **WARNING: Before using a GPIO it has to be correctly configured. A lot of times you can experience non-working code or drivers due to not initialized or configured GPIOs.**
|
|
|
|
|
|
> :warning: **WARNING: Before using a GPIO it has to be correctly configured. A lot of times you can experience non-working code or drivers due to not initialized or configured GPIOs.**
|
|
|
|
|
|
Examples of possible configurations for a GPIO are:
|
|
|
- **Output**: the pin voltage level can be set (logic level high or low)
|
|
|
- **Input**: the pin voltage level can be read (logic level high or low)
|
|
|
- **Alternate Function**: set the GPIO to be controlled by some hardware peripheral, such as an SPI, I2C, UART or timer. You should check your microcontroller's datasheet to know which pins have some alternate function.
|
|
|
|
|
|
Other possible modes are: **analog input**, **input pull-up**, **input pull-down**, **open drain**, **alternate open drain**.
|
|
|
|
|
|
Other possible modes are: **analog input**, **input pull-up**, **input pull-down**, **open drain**, **alternate open drain**.
|
|
|
|
|
|
The available pin modes and speeds can be found in the
|
|
|
`gpio_impl.h` inside `libs/miosix-kernel/miosix/arch/<your_arch>/common/interfaces-impl`, according to the microcontroller you are using.
|
|
|
|
|
|
`gpio_impl.h` inside `libs/miosix-kernel/miosix/arch/<your_arch>/common/interfaces-impl`, according to the microcontroller you are using.
|
|
|
|
|
|
If you want to know more about GPIOs, check out the [GPIO Tutorial - Miosix Wiki](https://miosix.org/wiki/index.php?title=GPIO_tutorial) page.
|
|
|
|
|
|
## GPIO Drivers
|
|
|
|
|
|
Lukily for us, Miosix provides all the needed drivers for manipulate the GPIOs and enabling all their possible functionalities and configurations.
|
|
|
|
|
|
##### 1. Template API
|
|
|
#### 1. Template API
|
|
|
To use a pin in your board you can simply do:
|
|
|
```cpp
|
|
|
// define pin PA5 as somePin
|
... | ... | @@ -98,7 +97,7 @@ somePin::alternateFunction(5); // Specify the alternate function number |
|
|
```
|
|
|
The above method provides *optimal performance*.
|
|
|
|
|
|
##### 2. GpioPin Class
|
|
|
#### 2. GpioPin Class
|
|
|
As an alternative you can use the `GpioPin` class.
|
|
|
```cpp
|
|
|
GpioPin somePin(GPIOA_BASE, 5);
|
... | ... | @@ -122,7 +121,7 @@ somePin.alternateFunction(5); |
|
|
> ```
|
|
|
|
|
|
## Discovery Board Pins
|
|
|
These are some commonly used pins that you'll need when writing software for **Discovery boards**.
|
|
|
These are some commonly used pins that you'll need when writing software for _Discovery boards_.
|
|
|
|
|
|
#### STM32F429ZI Discovery
|
|
|
|
... | ... | @@ -147,7 +146,7 @@ You can find a complete pin usage description [here](https://os.mbed.com/platfor |
|
|
You can find more in the [user manual](http://www.st.com/content/ccc/resource/technical/document/user_manual/70/fe/4a/3f/e7/e1/4f/7d/DM00039084.pdf/files/DM00039084.pdf/jcr:content/translations/en.DM00039084.pdf) for more.
|
|
|
|
|
|
## Miosix Used Pins
|
|
|
These are some pins that are used by default from the kernel when running on **STM32 micros** (all paths are referred to `/libs/miosix-kernel/miosix`):
|
|
|
These are some pins that are used by default from the kernel when running on _STM32 micros_ (all paths are referred to `/libs/miosix-kernel/miosix`):
|
|
|
|
|
|
- SD memory: `arch/common/drivers/sd_stm32f2_f4.cpp`
|
|
|
|
... | ... | |