Real-time trace of RTOS execution

Modern microcontrollers have much more processing power and memory than those of a few years ago. This has opened the opportunity to create more complex behaviors in the products we design. Managing this complexity requires the use of more sophisticated architectures to break the design into manageable components. An RTOS allows you to break your design into smaller chucks which are more manageable.

Each thread running in an RTOS is independent. It can generally execute its behaviors without regard to the other executing threads. When a thread has nothing to do it can yield execution to allow other threads to run. If its behavior is critical it can be assigned a higher priority assuring it runs as soon as it has work to do.

Because the RTOS is selecting which thread will run based on the current system state, it can be difficult to troubleshoot these systems, especially real-time systems which don’t tolerate setting a break point and stopping the execution.

One approach to tracking execution is to use a output port and pulse it each time the thread runs. This allows you to see if the thread runs and how often. Additional output ports can be added to trace more complex relationships. However, this solution is addhock and provides only minimal information.

Five Wire provides a more useful solution using the Logic Analyzer tool and the value nibble decoder. Four output pins must be assigned to make up a nibble that is captured by the Logic Analyzer. A small set of C functions are called within your thread to encode ‘marks’ or ‘variables’ which send short nibble packets to the Logic Analyzer. Once captured, the nibble packets are decoded into human readable values and displayed in the Logic Analyzer trace.

Here is an example capturing a span of time from an ST32F429 running 5 threads in FreeRTOS. Each of the spikes is a mark or variable being exported using the nibble decoder.

Zooming in on one of the spikes reveals three variables being exported and one mark indicating the monitor task is running. By calling the void nibMark(uint8_t n) function in your thread, a mark packet is sent to the Logic Analyzer. Up to 7 unique marks can be set. You can define your own label to reflect the purpose of each mark in the decoder definition file. For example, you could set a mark in each thread if the thread runs. This way you can see the sequence of thread execution, measure the time between executions, or compare the execution time with other external external signals captured on Logic Analyzer channels.

Even more powerful are the variable outputs. You can send up to 16 different variable values by calling void nibVar8(uint8_t n), void nibVar16(uint16_t n), or void nibVar32(uint32_t n). You can also customize the decoder to label each variable value. The variable nibble packets do take more time to send but usually less than a few microseconds.

Here we zoom in on the exported 32 bit tick timer to measure the time taken for the nibble export.

Here we zoom in on a mark to measure its export time.

Using this tool, you can trace the execution of your threads. You can also verify their reaction time to external logic signals by capturing additional signals on other Logic Analyzer channels. You can watch the values of variables as they change in reaction the external stimulus or track a control variable a state machine.

Here is a nibble capture on the same system but also capturing and decoding an RS232 input and the monThread RS232 response output.

The Five Wire Logic Analyzer with the nibble decoder provides a powerful tool to see whats going on in your multi-tasking environment.


Serial port debug

When developing firmware, it’s nice to have a serial debug port. By adding a little software, you can get information about the current operating state, “how much heap space have I used – heap?”, or exercise parts of your design, “set the motor to run at 50% – mot 50”, or tweak parameters of a PID loop to see the effect on system behavior.

Of course you have to first turn on and debug the serial port and parser before you can use it. Common issues are getting the serial inputs/outputs connected correctly and correct setup of the baud rate on your microprocessor. This is where Live Logic can help.

Live Logic provides high performance digital capture with easy to use scope probes. First, connect the probes to the Rx and Tx pins on your board. Live logic includes a DVM and you should immediately note that the Rx and Tx lines are high – close to the positive supply rail.

Next you can verify that The microprocessor serial input is connected to the terminals Tx output. Start the terminal program and hit the enter button a couple of times. The Live Logic activity LEDs will flash indicating activity on the channel connected to the terminal Tx line. Verify that this is the serial input pin to the microprocessor.

Finally, you want to verify that the baud rate for the terminal and the microprocessor UART are correct. Set Live Logic to single, SAMPLES to 512, and start a Live Logic capture. When you hit enter on the terminal you should see the Live Logic channel activity light flash and if your parser is working the Rx channel activity LED should also flash. Stop the acquisition and you should see spikes in the data display. These are the logic activity on the Rx and Tx lines.

Live Logic can capture up to 45 minutes of signal activity. In this case, it captured the entire time span from when you clicked start until you clicked stop. The width of the serial activity is very small compared to your capture time, more than 6 seconds. To see the serial activity, we will have to zoom in on the spikes.

To verify that the baud rate is correct, 38,400 in this example, we can zoom in further and use the A/B marks to measure the narrowest bit. To switch between time and frequency measurement just click ‘/’ on the keyboard.

The measurement is 1/dt=38.45KHz. We have the right baud rate programmed.

Live Logic can help you to quickly get a serial debug port up and running. Its long time capture eliminates the need to set up a trigger to capture the serial transaction. The DVM, activity LEDs, and A/B measurements make it easy to verify your connections and baud rate. The high sample resolution, 2.5 nanoseconds, ensures that the measurement is accurate, even at high baud rates.