With an older version of Particle firmware, I was able to successfully redirect standard out and standard in to use the USB serial port.
Here’s a snippet of my retarget code:
#if PLATFORM_ID == PLATFORM_ELECTRON_PRODUCTION
#define SERIAL USBSerial1
#elif PLATFORM_ID == PLATFORM_BORON
#define SERIAL Serial
#else
#error "No compatible platform"
#endif
extern "C" void retarget_init()
{
// Initialize UART
// done in application.c
setvbuf(stdin, NULL, _IONBF, 0);
setvbuf(stdout, NULL, _IONBF, 0);
setvbuf(stderr, NULL, _IONBF, 0);
SERIAL.begin(115200);
SERIAL.blockOnOverrun(true);
}
extern "C" int fgetc(FILE * p_file)
{
uint8_t input = -1;
if (SERIAL.available() > 0) {
input = SERIAL.read();
}
return input;
}
extern "C" int fputc(int ch, FILE * p_file)
{
(void)p_file;
SERIAL.write(ch);
return ch;
}
Then I use putchar
and fgetc
in my simple shell console module to read and write to the targeted UART.
With newer Particle IOT firmware that introduces “dynalib” feature, I am no longer able to link my image due to the stdio (repeated) definitions in hal_dynalib_posix_syscall.h
.
I’ve browsed the dynalib code for a bit but it is not clear to me how this organized.
What is the “proper” way to retarget STDIO to the USB serial port?