Easily Use printf on STM32

Hello @shouryuken,

It’s not clear from the code you posted what you mean by “bare-metal programming”. I assume you are simply not using either the HAL or LL abstraction layers? Which libraries/drivers are you using, if any?

Making some assumptions, I attempted to replicate your issue using a NUCLEO-L476RG board I have on-hand (I don’t have a NUCLEO-L452RE-P at the moment…). First, I created a new project using the procedure outlined in Setting Up an Empty STM32CubeIDE Project. I then created the following main.c file based on what you’ve provided:

#include <stdint.h>
#include <stdio.h>
#include "stm32l4xx.h"

char str[30];

void Display_Centimeters(void);

int main(void)
{
  Display_Centimeters();

	while(1)
	{

	}
}

void Display_Centimeters(void)
{
  int n = 30;
  int a = 10;
  float f = 5.0;

  n = sprintf((char *)str, "distance = %d \r\n", a);
  sprintf((char *)str, "distance = %f \r\n", f);

  (void)n; // avoid compiler warning
}

Note that my project settings match those in the screenshot you provided (except the MCU part number is different, of course). When I debug the project, I don’t receive any of the errors you describe. Both calls to sprintf() work as expected.

If you’re still having issues with your code, It would be very helpful to see a complete copy of your project (or a scaled-back project with the same issue) so we can figure out what’s missing. You can attach it to a private message if you prefer.

1 Like