Go Back

Trace Debugging Part One: Configuring J-Trace for the LPC4357

Monday, September 16, 2013
While you can accomplish a lot debugging with a standard SWD or JTAG debugger (like the J-Link or the LPC-Link 2), there are situations where being able to set individual breakpoints isn't always terribly efficient when something goes wrong and you end up in the HardFault handler but don't know why.  This is particularly true of complex stack development or when using an RTOS, where the execution paths aren't always clear.

Many of the higher end Cortex M devices include both SWD debugging and Trace, including the LPC1700, LPC1800 and LPC4300 families of LPC microcontrollers.

To highlight some of the advantages of Trace, and why it might be a useful tool to add to your debugging arsenal, we wanted to write a couple tutorials on debugging with trace using the LPC4357.

While Segger is apparently working on implementing GDB support for J-Trace, at the moment it's unfortunately limited to the major commercial toolchains (Keil, IAR, Atollic, and Crossworks support is apparently in the works in a future release from Rowley Associates).  While GCC w/GDB is our preffered toolchain here, for this tutorial we will have to use the free 32KB limited version of Keil uVision, and we will update this tutorial once GDB support is available.

Disclaimer: Segger was kind enough to provide us with a J-Trace for Cortex M unit that we will be using for these tutorials, but this was at our request as long time fans of Segger's debuggers in general for their flexibility, speed and reliability, and had no bearing on our choice of trace debugger.  We've been using Segger devices forever, and it's what we always recommend when people ask us for a reliable, professionally HW debugging solution.

Configuring the Debugger Settings for the J-Trace

The first step to using trace is to configure your Keil project to use the J-Trace unit.  You can do that by opening up your 'Debug' tab and selecting the 'J-LINK / J-Trace Cortex' option shown below:

The next (and very important) step is to force Keil to setup the target MCU to enable trace support, specifically to setup the trace pins for the trace functions before we start executing our own code.

This is accomplished by creating an .ini file that sets a few registers to enable trace.  You need to create this .ini file by hand, and point to it in the 'Initialization File' text box.  The contents of the .ini file as well as the config dialogue can be seen below:

** Define the function to enable the trace port
FUNC void EnableTPIU(void) {

  /* Configure TRACE pins for MCB4357 */
  //_WDWORD(0x40086790, 0x000000B2);  // LPC_SCU->SFSPF_4 = 2;
  //_WDWORD(0x40086794, 0x000000B3);  // LPC_SCU->SFSPF_5 = 3;
  //_WDWORD(0x40086798, 0x000000B3);  // LPC_SCU->SFSPF_6 = 3;
  //_WDWORD(0x4008679C, 0x000000B3);  // LPC_SCU->SFSPF_7 = 3;
  //_WDWORD(0x400867A0, 0x000000B3);  // LPC_SCU->SFSPF_8 = 3;
  /* Configure TRACE pins for LPC4357USB */
  _WDWORD(0x40086790, 0x000000B2);  // LPC_SCU->SFSPF_4 = 2 - TRACECLK
  _WDWORD(0x40086390, 0x000000B5);  // LPC_SCU->SFSP7_4 = 5 - TRACEDATA[0]
  _WDWORD(0x40086394, 0x000000B5);  // LPC_SCU->SFSP7_5 = 5 - TRACEDATA[1]
  _WDWORD(0x40086398, 0x000000B5);  // LPC_SCU->SFSP7_6 = 5 - TRACEDATA[2]
  _WDWORD(0x4008639C, 0x000000B5);  // LPC_SCU->SFSP7_7 = 5 - TRACEDATA[3]  

** Invoke the function at debugger startup

** Execute upon software RESET
FUNC void OnResetExec(void)  {

Configuring the J-Trace

The next step is to configure the Trace settings.  You can do this by clicking the 'Settings' button beside 'J-LINK / J-Trace Cortex' in the dialogues above, and then switching to the 'Trace' tab, seen below:

You need to make a number of changes to these settings, most importantly you need to set the 'Core Clock' to whatever clock speed you are running your board at, keeping in mind that trace support on the LPC4357 is limited to 120MHz, so you may need to run the LPC4357 slower than usual if you require trace debugging!

Debugging with Trace Support

This should be everything you need to do to get trace working in Keil, and you can debug as normal.  You simply need to enable the 'Instruction Trace' window, and whenever you hit a breakpoint of pause execution, the full trace log will be populated showing every single instruction and branch that happened up to the point where execution stopped, making it much easier to 'trace' back errors to their source, limited only by the amount of memory you have in the trace buffer!

In the next tutorial, we'll go into a lot more detail of how to get the most out of trace, but just getting things setup is the first step, and there wasn't any information out there on configuring the J-Trace for the LPC4357 so we wanted to start with the absolute basics.

Full story  | Comments (0)

LPCXpresso Support Added to LPC1343 Code Base

Friday, March 23, 2012
Debugging the LPC1343 Code Base in LPCXpresso

I've never been a huge fan of Eclipse, but since it's incredibly hard to argue with the price of LPCXpresso development boards with their removable SWD debuggers, basic support for flashing devices and debugging (size permitting) has just been added to the LPC1343 Code Base.  Since the code base isn't based on CMSIS, and still runs from a Makefile, the process of creating a project and selecting debug or release mode is a bit different, but we've put together a fairly straight-forward tutorial on it here: Debugging the LPC1343 Code Base in LPCXpresso.  Hopefully this will open the code base up to a few more people.

You will need to pull the latest version of the LPC1343 Code Base from Github to take advantage of LPCXpresso support, since the project files need to be more thoroughly tested before an official release.  A final v1.0.0 release of the LPC1343 Code Base is right around the corner, though, wrapping up about 2 years of constant development effort on this library.

Note: Project files also exist for Rowley Associate's Crossworks for ARM, and the fully cross-platform and open-source CodeLite IDE, both of which are found in the /build folder.  A very alpha DLL for CodeLite is also included in /tools, allowing you to program the flash and do step-through debugging via GDB Server with the Segger J-Link, but it's far from ready to see the light of day just yet.

Update 24-Mar-2012: LPCXpresso support has also been added to the LPC1114 Code Base as of v0.7.5.

Full story  | Comments (1)

Electronic & Embedded Fundamentals

These articles are part of our regularly updated Fundamentals series and may be useful if you're new to electronics or embedded development: