We use cookies to provide our visitors with an optimal site experience. View our privacy notice and cookie notice to learn more about how we use cookies and how to manage your settings. By proceeding on our website you consent to the use of cookies.
New to embedded programming, coming from the world of backend development. In high-level programming, we have something like .ReadBytes("\n"), which returns a byte list/vector/slice or a string as whole. However, with UART/HAL in STM32, I see that the only option is to read a fixed amount of bytes, which doesn’t work in my case. How can I implement that .ReadBytes("\n") in STM32 w/ HAL?
I tried the following:
uint8_t buf[256];
int i = 0;
for (i = 0; i < 256; i++) {
HAL_UART_Receive(&huart1, &buf[i], 1, HAL_MAX_DELAY);
if (buf[i] == '\n') {
break;
}
}
HAL_UART_Transmit(&huart2, &buf[0], i, HAL_MAX_DELAY)
And the code above kind of works; however, I see malformed data sent to the other UART interface, which makes me think that reading byte by byte is incredibly inefficient and the controller is just missing/dropping some of transmitted data.
The only reason I could come up with was for educational use. I could see an instructor having the students start with polling @ 75 baud, moving higher, seeing the failure, then teaching the reasons why. It would help teach the inner workings of microcontrollers which was, maybe still is, essential for successful engineering of embedded systems.
When I learned back in the 90s we did some obviously outdated, now obsolete things to learn the inner working details e.g. program in machine code (no assembler), burn a rom with a 9V batteries