Redirecting printf with a custom, non-global, callback
On an AVR microcontroller, I do this:
int printCHAR(char character, FILE *stream)
{
BOARD_TypeDef_t* udata = (BOARD_TypeDef_t*) fdev_get_udata(stream);
display_write(&udata->display[displayA], character);
return 0;
}
void main()
{
/* Declare BOARD typedef. */
BOARD_TypeDef_t board;
/* Initialize BOARD typedef. */
board_init(&board);
/* Initialize display */
display_init(&board.display[displayA]);
FILE uart_str = FDEV_SETUP_STREAM(printCHAR, NULL, _FDEV_SETUP_RW);
fdev_setup_stream(&uart_str, printCHAR, NULL, _FDEV_SETUP_RW);
fdev_set_udata(&uart_str, &board);
stdout = &uart_str;
printf(“%s”, “ABC”);
}
This prints the letters “ABC” on the memory lcd via the display_write function in printCHAR, with the non-global parameter &udata->display[displayA].
I’d like to achieve something similar on an STM32 microcontroller (on the G071RB Nucleo board) to which an spi display is attached. The FDEV set of functions appear to be customized, lightweight workarounds for the avr-libc library and are hence unavailable for STM32 processors.
For now, my main on the STM32 looks something like this:
void main(void)
{
/* Declare BOARD typedef. */
BOARD_TypeDef_t board;
/* Initialize BOARD typedef. */
board_init(&board);
/* Initialize display */
display_init(&board.display[displayA]);
/* Writes a “B” to the display */
display_write(&board.display[displayA], 0x42);
}
What I would like to do is to use my display_write function and the display destination in a retargetted printf.
The options I have seen are:
1. Using __io_putchar function: This is available to be externed from the syscalls.c file. However, the only parameter it takes is the character to be printed, so I’m not sure and don’t think I can get my BOARD_TypeDef_t in there.
2. Using write function: This is available in the syscalls.c file. However, I would rather use this the canonical way via __io_putchar and not by directly editing the write function.
3. Using fopencookie: I think this is the way I would like to do this but I’m having some difficulty, with my cookie_write_function_t function defined inside of the cookie_io_functions_t array of functions not getting called at all but rather getting fwrite being called instead. That quickly degenerates into nothing getting printed to the spi display. However, subsequent calls via the display_write primitive do continue to print to the spi display.
Also, it would be good to know if this could be achieved perhaps with fputc or some other way.
Any ideas of what I might be doing wrong, or guidance on how I might do this right, are appreciated.
Regards,
J