|
|
### Code implementation
|
|
|
|
|
|
To use a pin in your board you can simply:
|
|
|
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 port name and Y is the pin number.
|
|
|
|
|
|
### Configuration
|
|
|
Before using a GPIO it has to be configured.
|
|
|
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**.
|
|
|
|
|
|
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.
|
|
|
|
|
|
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.
|
|
|
|
|
|
### Examples
|
|
|
|
|
|
#### 1. Template API
|
|
|
To use a pin in your board you can simply do:
|
|
|
```cpp
|
|
|
typedef miosix::Gpio<GPIOA, 9> somePin; //Define pin PA9 as somePin
|
|
|
typedef miosix::Gpio<GPIOA_BASE, 5> somePin; // Define pin PA5 as somePin
|
|
|
```
|
|
|
|
|
|
To read from that pin:
|
... | ... | @@ -18,8 +38,24 @@ somePin::high(); |
|
|
somePin::low();
|
|
|
```
|
|
|
|
|
|
All the possible pin modes and speeds can be found in the
|
|
|
gpio_impl.h inside `libs/miosix-kernel/miosix/arch/<your_arch>/common/interfaces-impl`.
|
|
|
To set the pin's alternate function:
|
|
|
```cpp
|
|
|
somePin::mode(Mode::ALTERNATE);
|
|
|
somePin::alternateFunction(5); // Specify the alternate function number
|
|
|
```
|
|
|
The above method provides *optimal performance*.
|
|
|
|
|
|
#### 2. GpioPin Class
|
|
|
As an alternative you can use the `GpioPin` class.
|
|
|
```cpp
|
|
|
GpioPin somePin(GPIOA_BASE, 5);
|
|
|
|
|
|
somePin.mode(Mode::OUTPUT);
|
|
|
somePin.high();
|
|
|
|
|
|
somePin.mode(Mode::ALTERNATE);
|
|
|
somePin.alternateFunction(5);
|
|
|
```
|
|
|
|
|
|
### Discovery Board Pins
|
|
|
These are some commonly used pins that you'll need when writing software for **Discovery boards**.
|
... | ... | @@ -39,7 +75,9 @@ You can find a complete pin usage description |
|
|
|
|
|
| stm32f407vg | |
|
|
|
|--------------|---|
|
|
|
| Blue Led | PD15 |
|
|
|
| Red Led | PD14 |
|
|
|
| Orange Led | PD13 |
|
|
|
| Green Led | PD12 |
|
|
|
| User Button | PA0 |
|
|
|
|
... | ... | |