8. General Purpose Input

Using digital input ports in an application follows the same scheme as we saw with digital outputs:
  1. Port and Pin Definition

  2. Reservation of the pin

  3. Configuration of the pin

  4. Reading the pin state

You can learn more about digital inputs by downloading and extracting the following sample project Simple Button Example

8.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 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

Button Configuration Across SmartBond™ Development Kits

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 DA1453x,DA14531 USB kit has a button named SW2. This button connects to P0_11

  • The DA14531,DA1453x DEVKT-P 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 DEVKT-P has two buttons. We will use SW3 which connects to P1_1

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:
  1. Input with pull-up

  2. Input with pull-down

  3. 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 and deploy the project onto the target device. Next, employ your BLE explorer app to confirm that the LED on the development kit stays off when the button is pressed during an established connection.

Hint

The LED will consistently turn off upon connection termination, irrespective of the button’s state.