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
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.
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 page.
Examples
1. Template API
To use a pin in your board you can simply do:
typedef miosix::Gpio<GPIOA_BASE, 5> somePin; // Define pin PA5 as somePin
To read from that pin:
somePin::mode(Mode::INPUT);
int value = somePin::value();
To set the pin's logical voltage:
somePin::mode(Mode::OUTPUT);
somePin::high();
somePin::low();
To set the pin's alternate function:
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.
GpioPin somePin(GPIOA_BASE, 5);
somePin.mode(Mode::OUTPUT);
somePin.high();
somePin.mode(Mode::ALTERNATE);
somePin.alternateFunction(5);
WARNING: when configuring GPIOs you should disable interrupts to avoid concurrent calls to mode() (for example if it is done at runtime). This can be done since pins configuration is a very fast operation, but it should in general be avoided, in order not to compromise real-time capabilities of Miosix. A normal mutex would introduce more overhead. void someFunction() { { FastInterruptDisableLock dLock; // disable interrupts somePin::mode(Mode::OUTPUT); } // dLock scope ends: re-enable interrupts }
Discovery Board Pins
These are some commonly used pins that you'll need when writing software for Discovery boards.
-
STM32F429ZI_DISCO
stm32f429zi Red Led PG13 Green Led PG14 User Button PA0
You can find a complete pin usage description here.
-
STM32F407VG_DISCO
stm32f407vg Blue Led PD15 Red Led PD14 Orange Led PD13 Green Led PD12 User Button PA0
You can find more in the datasheet 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
):
-
SD memory:
arch/common/drivers/sd_stm32f2_f4.cpp
D0 D1 D2 D3 CLK CMD PC8 PC9 PC10 PC11 PC12 PD2 -
Serial Ports:
arch/common/drivers/serial_stm32.cpp
USART TX RX CTS RTS 1 PA9 PA10 PA11 PA12 2 PA2 PA3 PA0 PA1 3 PB10 PB11 PB13 PB14 -
Servo:
arch/common/drivers/servo_stm32.cpp
SERVO1 SERVO2 SERVO3 SERVO4 PB6 PB7 PB8 PB9