Debugging

Starting simple

The easiest way to debug and also the way how all the things started was just print out whatever you consider important to know.

Wait the embedded system does not have any screen or printer connected.

Well you are right, but there used to be a serial port. And if it is hopefully free to use and can be connected to real PC then you have your first poor man’s debugger.

Core Module

Note

If you have older version of Core Module you can visit another chapter to see how to connect it with UART.

Core Module have integrated FTDI chip connected to the UART2. You do not need to use separate serial converter, just connect USB cable to your computer.

Example code

You need to add just two function calls into your application:

  • twr_log_init into application_init

  • twr_log_debug, twr_log_info, twr_log_warning, twr_log_error of twr_log_dump into handlers

Tip

Have a look into HARDWARIO TOWER SDK twr_log for more detailed info.

Example of modified src/application.c from default project twr-skeleton available at GitHub:

 1#include <application.h>
 2
 3// LED instance
 4twr_led_t led;
 5
 6// Button instance
 7twr_button_t button;
 8
 9void button_event_handler(twr_button_t *self, twr_button_event_t event, void *event_param)
10{
11    (void) self;
12    (void) event_param;
13
14    if (event == TWR_BUTTON_EVENT_PRESS)
15    {
16        twr_led_set_mode(&led, TWR_LED_MODE_TOGGLE);
17    }
18    // Logging in action
19    twr_log_info("Button event handler - event: %i", event);
20}
21
22void application_init(void)
23{
24    // Initialize logging
25    twr_log_init(TWR_LOG_LEVEL_DUMP, TWR_LOG_TIMESTAMP_ABS);
26
27    // Initialize LED
28    twr_led_init(&led, TWR_GPIO_LED, false, false);
29    twr_led_set_mode(&led, TWR_LED_MODE_ON);
30
31    // Initialize button
32    twr_button_init(&button, TWR_GPIO_BUTTON, TWR_GPIO_PULL_DOWN, false);
33    twr_button_set_event_handler(&button, button_event_handler, NULL);
34}

After flashing of Core Module execute terminal emulator, open serial port with USB UART and set communication parameters:

  • speed: 115 200 baud

  • 8 bits, no parity (8N)

Example of output:

1    # 4.54 <I> Button event handler - event: 0
2    # 4.84 <I> Button event handler - event: 1
3    # 4.84 <I> Button event handler - event: 2
4    # 10.24 <I> Button event handler - event: 0
5    # 12.24 <I> Button event handler - event: 3
6    # 13.64 <I> Button event handler - event: 1

For mapping number to event type have a look into HARDWARIO SDK documentation for twr_button

Read logs with PlatformIO

If you want to easily read the logs from the device, you dont have to install any additional program. You should read the PlatformIO installation to know how to install PlatformIO.

After you installed the PlatformIO you can use it for reading the logs from the device.

There are two ways to do it:

  • PlatformIO IDE

  • PlatformIO Core CLI

    • pio device monitor in your favourite terminal if you have Platformio Core (CLI) installed

Caution

The serial monitor button does not build nor flash the firmware into the device so keep that in mind.