7. General Purpose Output

Controlling output ports on the Dialog SoC is pretty straight forward. It involves four steps:

  • Definition of the port and pin name

  • Reservation of the pin

  • Configuration of the pin

  • Controlling the pin

7.1. Port and Pin Definition

The Port and Pin definition specifies which physical pin is being used and is managed in the file user_periph_setup.h:

  • In user_periph_setup.h, we will define the LED on the development kit as P1_0 for the DA14585/6 variants and P0_9 for the DA14531 PRO kit:

#if defined (__DA14531__)
    #define LED_PORT      GPIO_PORT_0
    #define LED_PIN       GPIO_PIN_9
#else
    #define LED_PORT      GPIO_PORT_1
    #define LED_PIN       GPIO_PIN_0
#endif

7.2. Reservation of the Pin

GPIO reservation ensures that we avoid conflicting usage of a port/pin pair during development.

  • In the GPIO_reservations() function of user_periph_setup.c, we must reserve the port/pin pair for our LED as follows:

RESERVE_GPIO(LED, LED_PORT, LED_PIN, PID_GPIO);

7.3. Configuration of the Pin

We must set the pin configuration in order to define it as a digital output.

  • In set_pad_functions() of user_periph_setup.c add the following line of code (note: the last argument is set false, indicating that we want the LED to be off initially)

GPIO_ConfigurePin(LED_PORT, LED_PIN, OUTPUT, PID_GPIO, false);

7.4. Controlling the Pin

In order to demonstrate control of the general purpose output that we just created, we will use the LED to indicate BLE connection status. We will turn the LED on when the device is connected and turn it back off when the connection is terminated.

  • In the file user_peripheral_template.c, add the following include statements at the top of the file:

#include "gpio.h"
#include "user_periph_setup.h"

We will implement a function to turn on and off our LED. The function takes a boolean as an argument. When the argument is set to true, the function turns the LED on and when set to false it turns the LED off.

  • In user_peripheral_template.c add this function right after the include statements:

void control_LED(bool state)
{
    if(state == true)
        GPIO_SetActive(LED_PORT,LED_PIN);
    else
        GPIO_SetInactive(LED_PORT,LED_PIN);
}
  • The user_on_connection() function will run whenever a BLE connection is established. We can turn the LED on in that function by adding a call to our new function:

control_LED(true);
  • And in the user_on_disconnect() we can turn the LED back off:

control_LED(false);
  • Build the project and load it onto your target

  • Now, use your BLE explorer app to verify that the LED on the development kit turns on and off to indicate the connection state

Note

If we enabled sleep mode at this point, we would observe an unexpected behavior. The LED would only light up for a very short period of time. This issue is explained and addressed in the Sleep Mode section of this tutorial