8. General Purpose Input

Using digital input ports in an application follows the same scheme as we saw with digital outputs:

  • Definition of the port and pin name

  • Reservation of the pin

  • Configuration of the pin

  • Reading the pin state

You can learn more about digital inputs by downloading and extracting the following sample project Simple Button Example (details in the doc/Readme.html file)

8.1. Port and Pin Definition

Some SmartBond™ development kits have buttons that we can use for this part of the tutorial. The port/pin pair differ from kit to kit:

  • The DA14531 USB kit has a button named SW2. This button connects to P0_11

  • The DA14531 PRO kit has two buttons. We will use SW2. This button connects to P0_11

  • The DA14585/6 BASIC kit does not have a button, but we can use P1_1 and use a wire from P1_1 to GND to emulate a button press (J4 pin 2 to pin 7)

  • The DA14585/6 PRO kit has two buttons. We will use SW3 which connects to P1_1

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 input pin on the development kit as follows:

#if defined (__DA14531__)
   #define BTN_PORT      GPIO_PORT_0
   #define BTN_PIN       GPIO_PIN_11
#else
   #define BTN_PORT      GPIO_PORT_1
   #define BTN_PIN       GPIO_PIN_1
#endif

8.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 switch as follows:

RESERVE_GPIO(BTN, BTN_PORT, BTN_PIN, PID_GPIO);

8.3. Configuration of the Pin

Digital inputs on the SmartBond™ devices can be used in three modes:

  • Input with pull-up

  • Input with pull-down

  • Input with neither pull-up nor pull-down

We must set the pin configuration in order to define it as a digital input with pull-up.

  • In set_pad_functions() of user_periph_setup.c add the following line of code (note: the last argument is not used and can be set either true or false)

GPIO_ConfigurePin(BTN_PORT, BTN_PIN, INPUT_PULLUP, PID_GPIO, true);

8.4. Reading the Pin State

We will now be able to read the state of the input pin in our application using the GPIO_GetPinStatus() function. In the previous section, we turned the LED on when a BLE connection was established. We will now make a slight change to not turn the LED on if the button was pressed at the time of connection.

  • In user_on_connection() of user_empty_peripheral_template.c, add the highlighted line:

 void user_on_connection(uint8_t connection_idx, struct gapc_connection_req_ind const *param)
 {
    default_app_on_connection(connection_idx, param);
    if(GPIO_GetPinStatus(BTN_PORT, BTN_PIN)) // Turn the LED on unless the button is pressed
       control_LED(true);
 ~
  • Build the project and load it onto target

  • Now, use your BLE explorer app to verify that the LED on the development kit remains off if the button is pressed when the connection is established. The LED will still turn off when the connection is terminated, regardless of the state of the button.