SEGGER SystemView & Cortex-M0+

SEGGER SystemView is a useful tool for visualizing what’s going on in your embedded system. It works on ARM Cortex-M0(+), M1, M3, M4 and M7 microcontrollers, but does not interface with them identically.

I’m teaching myself how to use FreeRTOS using a FRDM-KL82Z board, which has a Kinetis KL82 Cortex-M0+ chip by NXP. It’s a size-optimized chip, with a 2-stage pipeline instead of the 3-stage pipeline present in Cortex-M0 non-plus, and also lacks a cycle count register. This register can be used for event time-stamping on chips possessing it, e.g. the Cortex-M4.

The documentation for SystemView states that one has to add their own time-stamping functionality for event data on this chip, and it provides an example of doing just that for SEGGER’s embOS. I’ll be doing the same for FreeRTOS. Hopefully this will save someone else some time.

/*********************************************************************
*
*       Cortex-M0+ components
*
**********************************************************************
*/
extern uint32_t SEGGER_SYSVIEW_TickCnt;

We need a shared resource for OS tick counts. This is placed at the top of SEGGER_SYSVIEW.h It gets read by the SEGGER_SYSVIEW_X_GetTimestamp() function in SEGGER_SYSVIEW.c

//  For SEGGER SSystem View Timestamping
uint32_t SEGGER_SYSVIEW_TickCnt = 0;

This gets added to the top of tasks.c in the FreeRTOS library.

BaseType_t xTaskIncrementTick( void ) {
	TCB_t * pxTCB;
	TickType_t xItemValue;
	BaseType_t xSwitchRequired = pdFALSE;
	SEGGER_SYSVIEW_TickCnt++;

And it gets incremented very close to the beginning of the FreeRTOS tick handler. Now SystemView can calculate time-stamp data based off of the FreeRTOS system tick.

et voila, data

Notes

  • I originally put the SystemView tick count increment in the pend context switch section of xPortSysTickHandler() in port.c, which is not where it should go, as it needs increment every FreeRTOS tick.
  • The SystemView documentation mentions this, but I didn’t at first follow its direction to increment the SystemView tick count before calling SEGGER_SystemView_Start(). This resulted in my timestamps jumping from 0 seconds to 59 minutes. I resolved this by starting the RTOS scheduler before this call.