I\'m trying to figure out how to use this new HAL driver. I want to receive data using the HAL_UART_Receive_IT()
which sets up the device to run an interrupt functi
Have a different approach patching e.g. "void USART2_IRQHandler(void)" in the file "stm32l0xx_it.c" (or l4xx as needed). Every time a character is received this interrupt is called. There is space to insert user code which keeps unchanged when updating with CubeMX code generator. Patch:
void USART2_IRQHandler(void)
{
/* USER CODE BEGIN USART2_IRQn 0 */
/* USER CODE END USART2_IRQn 0 */
HAL_UART_IRQHandler(&huart2);
/* USER CODE BEGIN USART2_IRQn 1 */
usart_irqHandler_callback( &huart2 ); // patch: call to my function
/* USER CODE END USART2_IRQn 1 */
}
I supply a small character buffer and start the receive IT function. Up to 115200 Baud it never consumed more than 1 Byte leaving the rest of the buffer unused.
st = HAL_UART_Receive_IT( &huart2, (uint8_t*)rx2BufIT, RX_BUF_IT_SIZE );
When receiving a byte I capture it and put it to my own ring-buffer and set the character-pointer and -counter back:
// placed in my own source-code module:
void usart_irqHandler_callback( UART_HandleTypeDef* huart ) {
HAL_UART_StateTypeDef st;
uint8_t c;
if(huart->Instance==USART2) {
if( huart->RxXferCount >= RX_BUF_IT_SIZE ) {
rx2rb.err = 2; // error: IT buffer overflow
}
else {
huart->pRxBuffPtr--; // point back to just received char
c = (uint8_t) *huart->pRxBuffPtr; // newly received char
ringbuf_in( &rx2rb, c ); // put c in rx ring-buffer
huart2.RxXferCount++; // increment xfer-counter avoids end of rx
}
}
}
This method proved to be rather fast. Receiving only one byte using IT or DMA always de-initializes and needs initializing the receiving process again which turned out to be too slow. The code above is only a frame; I used to count newline characters here in a status structure which allows me any time to read completed lines from the ring-buffer. Also a check if a received character or some other event caused the interrupt should be included.
EDIT:
This method proved to work fine with USARTS which are not supported by DMA and use IT instead.
Using DMA with 1 byte in circular mode is shorter and easier to implement when using CubeMX generator with HAL library.
EDIT2:
Due to changes in more recent HAL Libraries this does not work line by line. The principle still works fast and fine but has to be adapted to these 'dialects'. Sorry, but it is floor-less barrel to change it all-time.