-
Notifications
You must be signed in to change notification settings - Fork 0
tutorial debugging
Now that you know a little about variables, if statements, and loops, you've probably run into (or are about to run into) a bug.
Unlike a unix system, you can't just printf
things. printf
sends output to stdout
, which embedded systems (which lack operating systems) do not have.
The simplest, easiest, and least informative way to obtain debug output is through the onboard LEDs of the TM4C. This requires no additional hardware.
#include <RASLib/inc/gpio.h>
int main() {
InitializeGPIO();
SetPin(PIN_F1, true); // Red channel
SetPin(PIN_F2, true); // Blue channel
SetPin(PIN_F3, true); // Green channel
}
One common application of this technique is to set the LED, or one channel of the LED, to toggle on and off at regular intervals in your code. Then, you can tell if your code is still running, or if it got stuck in an infinite loop somewhere.
Sidenote: Ever wondered why the LED is so bright? Apparently (coworker of a friend; believe what you will) this is due to a former senior engineer at TI; when they were designing the TM4C as an update to the LM4F, some (more sane) engineers suggested that the LED be made dimmer. However, the senior engineer, for whatever reason -- perhaps to spite the junior engineers? -- insisted that the LED be left as it was, and had the clout to have it so.
If you need any amount of information to debug your program (sensor inputs, current motor power, etc), your best bet is to output these values via Serial (Also referred to as 'UART': Universal Asynchronous Receiver Transmitter).
As an example, here's Hello World, except with UART instead of stdout:
#include <RASLib/inc/uart.h>
int main() {
InitializeUART(115200);
Printf("Hello World!\n");
}
We can print out values via UART by passing 'format strings' into Printf. In order to use a format string, put the format specifier corresponding to the data type you want to print out into your string. Then, pass the variable as an argument in the function. This format specifier is then replaced with the value of your variable. If there are multiple format specifiers, simply add more arguments to the function; the format specifiers are then replaced in order.
#include <RASLib/inc/uart.h>
int main() {
InitializeUART(115200); // 115200 is the baudrate (number of bits per second).
// This can be other numbers, but 115200 is generally
// the highest stable baudrate.
Printf("Hello World!\n"); // Printf (with a capital p; from uart.h) supports format
// strings, but some data types may not be supported.
int one = 1;
Printf("%d + %d = %d", // %d is the format specifier for an int. The three
one, one, one + one); // '%d's are replaced with 1, 1, and 1 + 1, respectively.
}
To view the UART output, RASLib comes with a serial interface.
make flash
make flash # if the first one fails
# Reset the TM4C AFTER the flash finishes
# Please don't brick your TM4C
# I've done it before and it sucks
make uart