cancel
Showing results for 
Search instead for 
Did you mean: 

printf to IDE console?

I'd like to be able to printf into IDE console during debug. I'm using Atollic 9.1, Nucleus F413 board with Segger debug programmed into onboard debug interface. I followed the instructions here:

http://blog.atollic.com/cortex-m-debugging-printf-redirection-to-a-debugger-console-using-swv/itm-part-1

Nothing happens when I printf: The debugger never hits a breakpoint in the modified _write in syscalls.

What am I doing wrong?

Thanks!

Best Regards, Dave

12 REPLIES 12

The map shows that _write is loaded from library and not syscalls as expected.

c:/program files (x86)/atollic/truestudio for stm32 9.1.0/armtools/bin/../lib/gcc/arm-atollic-eabi/6.3.1/../../../../arm-atollic-eabi/lib/armv7e-m/fpu\libnosys.a(write.o)
                0x08004de8                _write

Yikes - Generated project (CubeMX) placed syscalls.c at root of project and it was not included in the project!

Isn't this a CubeMX bug?

Moving it to src directory tries to build it but I can't find the required CMSIS file core_cmX.h...

Anybody know where this is? Or is the Atollic application note referenced above out of date?

Thanks for any help here,

Best Regards, Dave

Those should be in the CMSIS directories for the HAL libraries, make sure you have the "Include Paths" set appropriately.

Perhaps it removes things where the call-trees don't go anywhere, via dead-code removal phase of linker?

In GNU/GCC more generally doesn't ST use __io_putchar() and have some code in syscalls/newlib to catch the other side?

/**
  * @brief  Retargets the C library printf function to the USART.
  * @param  None
  * @retval None
  */
PUTCHAR_PROTOTYPE
{
  /* Place your implementation of fputc here */
  /* e.g. write a character to the USART1 and Loop until the end of transmission */
  HAL_UART_Transmit(&UartHandle, (uint8_t *)&ch, 1, 0xFFFF);
 
  return ch;
}
#ifdef __GNUC__
/* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf
   set to 'Yes') calls __io_putchar() */
#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif /* __GNUC__ */

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

Right, I was missing (in syscalls.c) the #include "stm32f413xx.h" // ITM_SendChar (includes core_cm4.h)

Fixing that allows build.

Unfortunately, now trying printf lands in the hard-fault handler.

Any idea what I did wrong?

>>Any idea what I did wrong?

Not really, identify the code that's faulting and work back the logic. Usually busted pointers, illegal memory access or alignment issues.

I'm not an Atollic users, when I use GNU/GCC I use make/makefiles. Professionally I typically use Keil, it's far less of a goat rodeo..

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

Its faulting within newlib; I guess I could go read the sources as I don't have a newlib built for debugging handy. Perhaps STM has also set up newlib incorrectly as here: http://www.nadler.com/embedded/newlibAndFreeRTOS.html

The SW4STM32 forks look innoxious

extern int __io_putchar(int ch) __attribute__((weak));
extern int __io_getchar(void) __attribute__((weak));
..
int _write(int file, char *ptr, int len)
{
        int DataIdx;
 
                for (DataIdx = 0; DataIdx < len; DataIdx++)
                {
                   __io_putchar( *ptr++ );
                }
        return len;
}

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

Well, this is an unfortunate time-sink:

  • _write is never called by printf or puts
  • calling the suggested _write code with a test string results in no output in IDE console window
  • printf and puts sometimes generate a hard fault
  • looks like there's adequate memory for both malloc and FreeRTOS heaps.

I'm using AT 6.1, F413 Nuckeo board with Segger debug loaded into onboard debugger. Newlib 2.5 is included in STM distribution (htis project uses newlib-nano).

Anybody know how to get printf during debug to output to IDE console???

Thanks in advance for any explanation,

Best Regards, Dave

Piranha
Chief II

@Piranha​ - I don't know why you've provided this link? My test strings provide include newline, and in any case calling _write directly does not generate output in the IDE...