8. Input and Output pins
In this section, we will demonstrate how to control and monitor GPIOs.
We will use the push-button K1 on the motherboard to demonstrate input functionality, and the LED D1 on the daughter card for output functionality.
8.1. General Purpose Output
Controlling output pins is straight forward. The required steps are:
Enable the COM Power domain
Configure the output pin
Latch the output pin status
Disable the COM Power Domain
In main.c, find the connection event handler: handle_evt_gap_connected() and add this LED controller function just above it:
static void led_control(bool status)
{
// Enable the COM power domain before handling any GPIO pin
hw_sys_pd_com_enable();
hw_gpio_configure_pin(LED1_PORT, LED1_PIN, LED1_MODE, LED1_FUNC, status);
hw_gpio_pad_latch_enable (LED1_PORT, LED1_PIN);
hw_gpio_pad_latch_disable(LED1_PORT, LED1_PIN); // Lock pin status
// Disable the COM power domain after handling the GPIO pins
hw_sys_pd_com_disable();
}
We will use this function to turn the LED on when a connection is established, and turn it back off when the connection is terminated.
To do this, modify handle_evt_gap_connected() as shown below:
static void handle_evt_gap_connected(ble_evt_gap_connected_t *evt)
{
printf("BLE Connection established: %d \n\r", evt->conn_idx);
// Turn LED D1 on
led_control(true);
}
Similarly, we will modify the disconnection event handler: handle_evt_gap_disconnected():
static void handle_evt_gap_disconnected(ble_evt_gap_disconnected_t *evt)
{
printf("BLE Connection terminated: %d \n\r", evt->conn_idx);
// Restart advertising
ble_gap_adv_start(GAP_CONN_MODE_UNDIRECTED);
// Turn LED D1 off
led_control(false);
}
Using the SmartBond™ Scanner smartphone app, we can verify that the LED turns on and off as we connect to or disconnect from the device.
8.2. General Purpose Input
In this subsection, we will read and output the status of an input pin when a BLE connection is established.
The push-button K1 on the motherboard is already mapped to P0_10 of the SoC in the header file brd_prodk_da1459x.h.
The function periph_init() in main.c is executed after the device is initially powered up and whenever the system wakes up from sleep.
In this function, we will configure Key1 as an input with pull-up (the push-button will connect P0_10 to ground through a 1kOhm resistor when pressed):
static void periph_init(void)
{
// Enable the COM power domain before handling any GPIO pin
hw_sys_pd_com_enable();
// Configure Key1 as input with pull-up
hw_gpio_configure_pin(KEY1_PORT, KEY1_PIN, KEY1_MODE, KEY1_FUNC, true);
hw_gpio_pad_latch_enable (KEY1_PORT, KEY1_PIN);
hw_gpio_pad_latch_disable(KEY1_PORT, KEY1_PIN); // Lock pin status
// Disable the COM power domain after handling the GPIO pins
hw_sys_pd_com_disable();
}
As shown, the configuration of the input pin is handled using the same flow as we used for configuration of the LED output pin:
Enable the COM Power domain
Configure the input pin
Latch the output pin status
Disable the COM Power Domain
Now, all we have to do is to read the status of the pin when a connection is established, and use the puts() function to output this status:
static void handle_evt_gap_connected(ble_evt_gap_connected_t *evt)
{
printf("BLE Connection established: %d \n\r", evt->conn_idx);
led_control(true);
// Get the input status of Key1
bool k1_status = hw_gpio_get_pin_status(KEY1_PORT, KEY1_PIN);
// Output the status of Key1
if(k1_status == false)
puts("Key1 was pressed as the connection was established\n\r");
else
puts("Key1 was NOT pressed as the connection was established\n\r");
}
Using the SmartBond™ Scanner smartphone app, we can verify that the push-button status is reported when we connect to the device.
Figure 9 The push-button state is reported when we connect to the device. The button was held down during the second connection attempt.