Better FreeRTOS Debugging with SEGGER SystemView

October 23rd 2020

By Azema Talam, Firmware Design Engineer

FreeRTOS is one of the leading real-time operating systems for microcontrollers.  It is easy to learn, open-source, and with all extra tools like SEGGER SystemView it is a reason why it is used in many projects.

One of the most important aspects when developing an embedded application is getting insights into what is going on application. No matter how carefully you design and implement the application when there is an issue, developers are often making assumptions on code timing, code flows, interrupt firing, etc.

FreeRTOS comes with a bunch of cool features that can be used to make debugging easier and better. One of the features that can be enabled is collecting run time statistics. FreeRTOS can collect information on the amount of the time that each task spends in a running state. This is a very simple but powerful method of estimating task execution time and getting the percentage of total time spent on each task. But still, this gives us just a piece of information on what is happening in the system. Furthermore, we can add some trace capabilities to the FreeRTOS application. Trace hooks macros are a feature that can be used to trace the behavior and timing of the application and operating system. FreeRTOS source code contains empty macros that an application can re-define for the purpose of providing application-specific trace facilities. Trace hooks macros are really good to use if you want to toggle LED on task context switching and analyze timings using the scope. You can try to use trace hooks macros to make your own tool for real-time recording and visualization but adding more and more code to trace hooks will affect the timings of your application so be careful.

I am not trying to discourage you from implementing run-time statistics or using trace macros. Using it carefully together you can add great diagnostic to your production application. But this is where the power of trace tools such as SystemView is shown when debugging embedded systems.

SystemView is a tool that enables real-time recording and visualization of trace data on how embedded applications are behaving. By adding necessary files, our application can generate events when tasks become ready for execution, starts or stop executing, when an interrupt happens, and so on. It is based on the technology known as Real-Time Transfer (RTT) which is just a small memory buffer on the target device. With RTT it is possible to output information from the microcontroller at very high speed without affecting the device real-time behavior.

All it is needed is a few files on the target device that communicate with the host using J-Link debug connection.

In order to add SystemView to your project you need to:

1. Install SEGGER SystemView

  • Installer for SEGGER SystemView for Linux, macOS, and Windows here.
  • Link for User Manual here

2. SEGGER J-Link Install

  • Instructions on how to download and install are here.

3. Add the SystemView module to the project. 

  • It is architecture-independent. 
  • The default installation path on Windows is:

 C:/Program Files (x86)/SEGGER/SystemView_V252d.

4. In your project add three folders

  • SEGGER
  • Config
  • FreeRTOS_SEGGER.

5. For SEGGER folder copy all the files from

  • <SystemView_installation_path>/Src/SEGGER

6. For Config folder copy all the files from

  • <SystemView_installation_path>/Src/Config

7. For FreeRTOS_SEGGER folder copy the following files

  • <SystemView_installation_path>/Src/Sample/FreeRTOSV10/SEGGER_SYSVIEW_FreeRTOS.c
  • <SystemView_installation_path>Src/Sample/FreeRTOSV10SEGGER_SYSVIEW_FreeRTOS.h
  • <SystemView_installation_path>Src/Sample/FreeRTOSV10ConfigSEGGER_SYSVIEW_Config_FreeRTOS.c

8. Apply a patch to the FreeRTOS source code

  • Depending on the version of FreeRTOS you are using you might need to apply a patch to the FreeRTOS source code. 
  • You need to compare files from the patch with files from the FreeRTOS source code to see do you need to apply the patch manually.
  • If needed, a patch can be found at this location:<SystemView_installation_path>/Src/Sample.

9. Add SEGGER_SYSVIEW_GET_TIMESTAMP() function if needed.

  • On Cortex-M3/4/7 devices, the Cortex-M cycle counter can be used as a system timestamp.
  • On most other devices the system timestamp has to be generated by a timer.
  • In the user manual, there is an example of how to implement this function.

10. Add #include "SEGGER_SYSVIEW_FreeRTOS.h" at the end of the “FreeRTOSConfig.h” file.

11. Modify the SYSVIEW_RAM_BASE (SEGGER_SYSVIEW_Config_FreeRTOS.c file) value to the lowest RAM address for your device.

12. Add #include "SEGGER_SYSVIEW.h" in main file.

13. Search the main function and add the following function calls

  • SEGGER_SYSVIEW_Conf();
  • SEGGER_SYSVIEW_Start();

14. Build and run your example, run SystemView and start recording.

I used one of the development boards from NXP and wrote the simple application “Hello World”. After following the steps from above I was able to see how the application is behaving. 

Happy Debugging!

 

Popular posts

Cookies help us deliver our services. By using our services, you agree to our use of cookies.