How to: GPIO Pins¶
You can use many GPIOs (General Purpose Input/Output pins) to connect the Core Module with the outside world.
The pins are described in the Header Pinout. The pins in SDK has names TWR_GPIO_P0
to TWR_GPIO_P17
.
There are also two special pins dedicated to TWR_GPIO_LED
and TWR_GPIO_BUTTON
.
Tip
GPIO Configuration¶
First important task is to enable clock to the GPIO peripheral by calling twr_gpio_init
.
ARM Cortex micros have clock gating which disables the peripheral clock and saves more power.
Then you need to configure the pin mode by calling twr_gpio_set_mode
.
The Mode can be input, output, analog, open-drain or alternate mode which is used when configuring UART or SPI peripheral.
Then it is possible to configure internal **pull-up** or **pull-down** resistor by calling twr_gpio_set_pull
so can define the default logic
level when there’s nothing connected to the GPIO pin.
GPIO as output¶
This example will turn on the LED on the Core Module.
The usual and more comfortable way to control LED is to use twr_led
code,
but we use the twr_gpio
for now to explain the GPIO basics.
1#include <application.h>
2
3void application_init(void)
4{
5 twr_gpio_init(TWR_GPIO_LED);
6 twr_gpio_set_mode(TWR_GPIO_LED, TWR_GPIO_MODE_OUTPUT);
7 twr_gpio_set_output(TWR_GPIO_LED, 1);
8}
GPIO as Input¶
This example will read the button state and then the value will be written to the LED.
1#include <application.h>
2
3void application_init(void)
4{
5 twr_gpio_init(TWR_GPIO_LED);
6 twr_gpio_set_mode(TWR_GPIO_LED, TWR_GPIO_MODE_OUTPUT);
7
8 twr_gpio_init(TWR_GPIO_BUTTON);
9 twr_gpio_set_mode(TWR_GPIO_BUTTON, TWR_GPIO_MODE_INPUT);
10
11 // The Core Module has hardware pull-down so next call is commented out
12 // twr_gpio_set_pull(TWR_GPIO_BUTTON, TWR_GPIO_PULL_DOWN);
13}
14
15void application_task()
16{
17 uint8_t button_state = twr_gpio_get_input(TWR_GPIO_BUTTON);
18 twr_gpio_set_output(TWR_GPIO_LED, button_state);
19
20 // Repeat this task again after 10 ms
21 twr_scheduler_plan_current_relative(10);
22}