Fixed length uart frame detect on Zephyr

Hello. 

I've got problem with detect uart frame received from another device. This device send me frames which has  fixed lenght (20B) and it always starts with 2B header ("**").

I used UART implementation with one static buffer. 

static int uart0_init(void)
{
	int err;

	if (!device_is_ready(uart_central)) 
	{
		LOG_ERR("UART0 device not ready");
		return -ENODEV;
	}

	err = uart_callback_set(uart_central, uart0_cb, NULL);
	if (err) 
	{
		return err;
	}

	return uart_rx_enable(uart_central, uart_central_rx_buff, sizeof(uart_central_rx_buff), SYS_FOREVER_US);
}

static void uart0_cb(const struct device *dev, struct uart_event *evt, void *user_data)
{
	ARG_UNUSED(dev);

	switch (evt->type) {
	case UART_TX_DONE:
		break;

	case UART_RX_RDY:
		break;

	case UART_RX_DISABLED:
		uart_rx_enable(uart_central, uart_central_rx_buff, sizeof(uart_central_rx_buff), UART_RX_TIMEOUT);
		break;

	case UART_RX_BUF_REQUEST:
		break;

	case UART_RX_BUF_RELEASED:
		break;

	case UART_TX_ABORTED:
		break;

	default:
		break;
	}
}

I tried to use UART_RX_RDY event type to detect frame header but it doesn't work well when UART_RX_BUF_REQUEST event occur and the buffer is fill from beggining and there are extra UART_RX_RDY events. I can make bigger buffer than frame which I'll receive from second device and clear buffer offset after receive frame but this solution won't be good if I receive some trash data or frame will have incorrect length. It could be hours between correct frames and I'm afraid that there could be some incorrect data. How to recognize corrrect frame in this situation. There would be no problem if there was an event after one byte receive. 

My device is build on nRF52840.

Regards,

P_WE

Parents Reply
  • My mistake, of course it is 115200. I didn't notice issue when baudrate is slower. The problem is that last byte is in the buffer, but it is not read by:

    k_msgq_get(&uart_rx_msgq, &incoming_message, K_FOREVER);
     

    It is read when next frame has come.

    Frame has 20B length, buffer is 16B long.

    Situation is explained on screens.

    UART0 is read in separate thread. There is also BLE scanner active in one time.

    Regads,

    P_WE

Children
Related