cancel
Showing results for 
Search instead for 
Did you mean: 

Error when use printf UART

HODEFIANT
Associate II

hi everyone

use printf in UART, i add in file main.c

#include <stdio.h>

//--------------

#ifdef __GNUC__

#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)

#else

#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)

#endif 

//------------

PUTCHAR_PROTOTYPE

{

 HAL_UART_Transmit(&huart1, (uint8_t *)&ch, 1, HAL_MAX_DELAY);

 return ch;

}

when reset device stm32 and printf uart doesn't work on serial

but when i debug printf work

5 REPLIES 5

"when reset device stm32 and printf uart doesn't work on serial, but when i debug printf work"

Is that repeatable, or did it just happen once?

When you say "didn't work", what exactly does that mean:

  • no output ?
  • corrupted output ?
  • other ??

Is it just the UART output that "doesn't work" (sic); ie, do other parts of the code work OK?

0693W000008xsqBQAQ.png

Paul1
Lead

I've found that if printf seems to work/not work when you change build (debug or regular build)

it is due to a stack issue or an index issue, such as printf a parameter on the stack, or a printf of data beyond the end of an array.

i.e. You are using a pointer in printf, but the pointer was temporary and the data isn't valid anymore, such as a pointer to a stacked variable in a function that disappears/released when function exists.

*To test for this change the printf to print constants

printf("Data %u\n", *fred); //crashing code may be due to pointer whose target no longer exists

printf("Data %u\n", 12345); //Try like this to see if pointer issue

When you run in debug the stack is altered with all the debug data, and the item on the stack may remain there while in normal build the stack gets changed.

As Andrew wrote - What do you mean by "didn't work", I didn't see example output of "didn't work" in this or your other issue.

Paul

thanks i will test. .

i use uart to print data in serial.

-> "didn't work", there is no data on serial, here i use app hercules

https://www.hw-group.com/software/hercules-setup-utility

@HODEFIANT​ "there is no data on serial, here i use app hercules"

So you mean you see nothing in Hercules?

Have you checked if anything at all is happening on the serial line; eg, using an oscilloscope?