How To Add Printf Support For The MSP432 Launchpad Serial Port

The humble serial printf never seems to die. It is usually the primary source for debugging your source if you do not have a debugger that steps through code. That and the blinking LED routine gets old.

Printf – A Versatile tool, but a memory hog

If you are not aware, using printf in your code will usually blow any tiny little microcontroller’s memory away even if you used it sparingly without float support. For those familiar with the MSP432 Launchpad, this was a common problem and opossum came up with his popular Tiny Printf code.

Fortunately for us, there is the mighty MSP432 now. The little ARM on the Launchpad packs in 256KB Flash and 64KB SRAM which leaves us enough space left over if we use the printf library.

The Backchannel UART

We will be using the Application/User UART on the MSP432 Launchpad which connects to pins P1.2 and P1.3 and further connects to the eUSCI_A0 module. In Launchpad terms, this is also known as the Backchannel UART. The below picture shows you where the two jumpers are placed on the Launchpad, inside the orange circle. They should be on by default.

Rerouting printf()

If you use printf() in your code without setting it up for channeling it through the UART, the strings will be viewable on the debug console of the IDE. The way this works is that the compiler/debugger adds a breakpoint right after the code converting the strings finishes, and then pull the strings out the memory via JTAG and displays it on the console.

To get strings to display on the UART, we need to override fputc() and fputs() of the RTS (Run-Time-Support) library functions. To do this, create a new file printfOveride.c and copy the below code to it. Remember, we are channeling through eUSCI_A0, which means setting up the correct interrupt(IFG) and transmit buffers(TXBUF) is necessary.

[codesyntax lang=”c” lines=”fancy” container=”pre” title=”Code listing:” blockstate=”expanded”]

int fputc(int _c, register FILE *_fp); int fputs(const char *_ptr, register FILE *_fp); int fputc(int _c, register FILE *_fp) { while(!(UCA0IFG&UCTXIFG)); UCA0TXBUF = (unsigned char) _c; return((unsigned char)_c); } int fputs(const char *_ptr, register FILE *_fp) { unsigned int i, len; len = strlen(_ptr); for(i=0 ; i<len ; i++) { while(!(UCA0IFG&UCTXIFG)); UCA0TXBUF = (unsigned char) _ptr[i]; } return len; }

[/codesyntax]

Printf is resource heavy, which means we also have to allocate some heap space for it. This can be done by going to Project Properties->Build->MSP432 Linker->Basic Options, and changing the heap size to 320bytes.

This is for your knowledge as the project below has everything setup for you.

Sample Project

Time for the sample project.

Download the project from github and extract it.

Open CCS and import the project via File->Import->Code Composer Stduio->CCS Projects.

In the next window, select the project and click “Copy projects into workspace”

Click the bug icon drop down and select “Debug As…”. This will download code to your MSP432 Launchpad and halt at main.

Launch your serial terminal program. This can be TeraTerm, Docklight or Putty. I’m using TeraTerm.

Select the Launchpad XDS110 Class Application/User UART(COM10). The COM number will vary depending on your PC.

Set the terminal to 115000bps, no parity at 8 data bits and 1 stop bit.

Click the green play button in CCS and you should see the following output on the serial terminal.

Enjoy printing over the serial port! If you liked this writeup, please subscribe in the box on the right sidebar.

Resources: