7. Debugging using printf

It is quite convenient to be able to output various debugging information via a serial port to a terminal emulator application. In this section we will demonstrate how to enable the serial port for this purpose.

7.1. Serial Port Terminal

In the following we will demonstrate this using a standard and free terminal emulator, Tera Term. Tera Term can be downloaded from Tera Term, but you can use any terminal emulator that you are comfortable with (PuTTY, RealTerm, +++)

First, we will set up Tera Term by selecting the virtual COM port and set the port configuration in the Setup->Serial Port window as shown below:

../_images/serial_port_setup.png

Figure 5 Set the serial port to 115k2/8/1/n as shown above.

7.2. Enabling printf

To enable printf via the serial port, we must again open the custom configuration file \config\custom_config_eflash.h, and add the following define:

Code 8 Implementing support for printf via serial port
#define CONFIG_RETARGET (1)

We must also add the following include in main.c:

Code 9 Inclusion of stdio.h
#include <stdio.h>

At this point, we will be able to use printf in our project. We will modify the connection and disconnection handler to output the connection status.

In main.c find the connection event handler: handle_evt_gap_connected() and modify it as shown below:

Code 10 Modified Connection Handler
static void handle_evt_gap_connected(ble_evt_gap_connected_t *evt)
{
   printf("BLE Connection established: %d \n\r", evt->conn_idx);
}

Similarly, we will modify the disconnection event handler: handle_evt_gap_disconnected():

Code 11 Modified Disconnection Handler
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);
}

Using the SmartBond™ Scanner smartphone app and Tera Term (or any similar terminal emulator), we can verify that our printf statements displays the BLE connection and disconnection events:

../_images/serial_port_use.png

Figure 6 Tera Term output.