Hi!
my project uses the printf implementation in this way:
#ifdef __CC_ARM
#pragma import(__use_no_semihosting)
struct __FILE
{
int handle;
};
FILE __stdout;
FILE __stdin;
void _sys_exit(int x)
{
x = x;
}
/* __use_no_semihosting was requested, but _ttywrch was */
void _ttywrch(int ch)
{
ch = ch;
}
#endif
#endif
#if defined (__GNUC__) && !defined (__clang__)
#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#define GETCHAR_PROTOTYPE int fgetc(FILE *f)
#endif
/**
* @brief retargets the c library printf function to the usart.
* @param none
* @retval none
*/
PUTCHAR_PROTOTYPE
{
while(usart_flag_get(PRINT_UART, USART_TDBE_FLAG) == RESET);
usart_data_transmit(PRINT_UART, (uint16_t)ch);
while(usart_flag_get(PRINT_UART, USART_TDC_FLAG) == RESET);
return ch;
}
GETCHAR_PROTOTYPE
{
// while(usart_flag_get(PRINT_UART, USART_RDBF_FLAG) == RESET);
// return(usart_data_receive(PRINT_UART));
char ch;
extern StreamBufferHandle_t stream_console;
xStreamBufferReceive(stream_console,&ch,1,portMAX_DELAY);
return (ch);
}
I implemented the functions as follows, but judging by the log, the output has not changed.
How to change the code for redirecting the print via printf-stdarg.c?
We got fed up with all the library stuff the printf and friends in newlib pulled in, and we needed to redirect the output, so we use an adapted implementation of printf in our projects. It’s at RRFLibraries/src/General/SafeVsnprintf.cpp at 3.5-dev · Duet3D/RRFLibraries · GitHub. It doesn’t guarantee to print doubles to the full available precision, but is otherwise fairly complete and doesn’t use heap memory (unlike the standard one). Then we define printf-like functions in a way similar to this:
int printf(const char * _ecv_array fmt, ...) noexcept
{
va_list vargs;
va_start(vargs, fmt);
const int ret = vprintf(fmt, vargs);
va_end(vargs);
return ret;
}
int vprintf(const char * _ecv_array fmt, va_list vargs) noexcept
{
return vuprintf([](char c)->bool
{
return c == 0 || write(c) >= 0;
},
fmt, vargs);
}
where ‘write(c)’ does whatever we want to do with each character.