Redirect printf for STM32L476 Discovery?
I'll pass on what I found so perhaps others will not have to waste as much time as I did on it.
Here's a code snippet of what I found worked to redirect printf() and scanf() though USART2:
int _write (int fd, char *ptr, int len)
{
HAL_Status = HAL_UART_Transmit(&huart2, (uint8_t*) ptr, len, 0xFFFF);
return len;
}
int _read (int fd, char *ptr, int len)
{
*ptr = 0x00; // Flush the character buffer
HAL_Status = HAL_UART_Receive(&huart2, (uint8_t*) ptr, 1, 0xFFFF);
return 1;
}
I found by trial and error if I didn't have the line to clear the buffer, the old data was retained ... causing an endless loop if you're looking for a cetain character to generate a response.
The read is a simple single character. Those familiar with HAL should be aware that reading in a longer string will require a more complex solution where you to set a ring buffer ..... and is probably best done with DMA.
There were other things needed to get the terminal emulator on the PC working:
Install Windows lib_usb support with Zadig
Update the STLink firmware
Run the terminal emulator in administrator mode (perhaps a Windows 10 quirk?)
Modify printf statements to end with a return and line feed
For the record, this is my environment:
Windows 10 laptop, Intel i5
Eclipse Mars.1 Release 4.5.1
GNU Tools Arm Embedded 4.9 2015q3
OpenSTM32 1.5.2
WinUSB (v6.1.7600.16385)
STM32CubeMX V 4.10.0
STM32L476 Discovery
ST-Link V3.7.0