I manage to do something that works for me, so I share it.
When installing the driver I set the event queue:
Code: Select all
esp_err_t err = uart_driver_install(m_uart_device.uartInstance, k_uart_buf_size, k_uart_buf_size, 25, &m_uart_queue, 0);
And then when reading the data:
Code: Select all
/**
* @brief Read bytes from UART buffer
* @param frame pointer to the buffer to place the data
* @param statics UART statics pointer destination. Null for don't get the statics
* @return
* - (-1) Error
* - OTHERS (>=0) The number of bytes read from UART FIFO
*/
int32_t Uart::getFrame(uint8_t **frame, Statics *statics)
{
int32_t rsp = UART_ERR;
if (m_rx_buff != NULL)
{
memset(m_rx_buff, 0x00, m_rx_buff_size);
rsp = uart_read_bytes(m_uart_device.uartInstance, m_rx_buff, m_frame_size,
(m_interFrame_tout_us / 1000) / portTICK_RATE_MS);
(*frame) = m_rx_buff;
// If 'm_max_frame_size' is reached, internal rx buffer will keep the pending bytes for the next
// 'uart_read_bytes'
// And now check all the UART events
check_uart_events(statics);
if (rsp > 0)
{
if (m_rx_data_inverted)
{
invertData(m_rx_buff, rsp);
}
}
}
return rsp;
}
I called "check_uart_events" function:
Code: Select all
/**
* @brief Get UART events and fill statics struct accordingly
* @param statics UART statics pointer destination. Null for don't get the statics
* @return
*/
uart_func_rsp_t Uart::check_uart_events(Statics *statics)
{
uart_func_rsp_t rsp = UART_ERR;
if (statics != NULL)
{
statics->INTERRUPTS = 0;
statics->RX_BREAKS = 0;
statics->RX_CHARS = 0;
statics->RX_DROPPED_INPUT = 0;
statics->RX_FRAMING_ERRORS = 0;
statics->RX_NOISE_ERRORS = 0;
statics->RX_OVERRUNS = 0;
statics->RX_PARITY_ERRORS = 0;
statics->TX_CHARS = 0;
}
do
{
uart_event_t uart_event;
// We must not wait here, but in 'uart_read_bytes' function to keep the compatibility with old/inherited code
if (xQueueReceive(m_uart_queue, (void *)&uart_event, 0x00))
{
switch (uart_event.type)
{
case UART_DATA:
// We will always read the data, no matter the event type.
printf("UART#%u: UART_DATA\r\n", m_uart_device.uartInstance);
break;
// Event of UART parity check error
case UART_PARITY_ERR:
printf("UART#%u: UART_PARITY_ERR\r\n", m_uart_device.uartInstance);
if (statics != NULL)
{
statics->RX_PARITY_ERRORS++;
}
break;
// Event of UART frame error
case UART_FRAME_ERR:
printf("UART#%u: UART_FRAME_ERR\r\n", m_uart_device.uartInstance);
if (statics != NULL)
{
statics->RX_FRAMING_ERRORS++;
}
break;
// Other events
default:
printf("UART#%u: OTHER_UART_EVENT\r\n", m_uart_device.uartInstance);
break;
}
}
} while (uxQueueMessagesWaiting(m_uart_queue));
return rsp;
}