Using printf for MCU debugging

MCU
Video
SEGGER Embedded Studio

In Lab 6 you will learn how to use the UART peripheral to send debugging logs to your laptop from the MCU.

The MCU can also print to a specialized debug channel using ITM_SendChar and printf:

// Necessary includes for printf to work
#include <stdio.h>
#include "stm32l432xx.h"

// Function used by printf to send characters to the laptop
int _write(int file, char *ptr, int len) {
  int i = 0;
  for (i = 0; i < len; i++) {
    ITM_SendChar((*ptr++));
  }
  return len;
}

int main(void) {
  int i;

  for (i = 0; i < 100; i++) {
      // printf
    printf("Hello World %d!\n", i);
  }
  do {
    i++;
  } while (1);
}

Note that this only works in the debug mode, and the logs appear here:

Screenshot of Segger demonstrating printouts using the above code

If you want to include floating point values in your prints, you need to enable fPrintf Floating Point Support in your solution settings. Right click on the solution name in the left panel and select options. In the window that opens search for “float” and change “Printf Floating Point Supported” from No to Yes.

Share Your Feedback

NoteShare Your Feedback!

If you caught any typos or have any suggestions for this page, please open an issue on the website Github repository. Click the link here for instructions on how to create an issue.