2018-11-14 05:22 PM
I use MXCube to create a simple project based on the STM32F3Discovery board. Only USB is enabled as a CDC device. Other settings remain untouched. The code is generated for Truestudio.
In Truestudio I created the syscalls.c by New->Other->System Calls->Minimal System Calls Implementation. In syscalls.c I add the following code for the function _write as follows:
int _write(int32_t file, uint8_t *ptr, int32_t len)
{
/* Implement your write code here, this is used by puts and printf for example */
/* return len; */
CDC_Transmit_FS(ptr, len);
errno = ENOSYS;
return -1;
}
Only one line of code is inserted to redirect the printing to the USB CDC device.
In main.c I added the following code in the main loop to test the printing:
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
printf("Hello World!!");
HAL_Delay(500);
}
/* USER CODE END 3 */
Two lines of codes are added to print "Hello Word!!" and delay 500ms.
The code compiles ok but there is no message printed on the console. Interestingly, if I change the print content to:
printf("Hello World!!\n");
That is, with a carriage return inserted in the printed string the strings appear on the console one by one as would be expected.
I've tried various combinations to verify this behavior. With/without RTOS. newlib and newlib-nono. The behavior is consistent. I traced the code a little and it looks like _write is only called if there is a carriage return '\n' in the string to be printed. If I send several strings without carriage return and followed by a string with a carriage return all previous strings will be printed out in one shot. It looks like the printf function buffers data sent to it and calls _write() only if it receives a carriage return.
This behavior is quite unusual. Is there any way to change it so that printf would call _write() immediately without waiting for a carriage return?
Regards,
Chris Chang
2018-11-15 12:21 PM
Read my second post in this topic: