uart_read_bytes() and UART_PATTERN_DET do not work well together

psiphi75
Posts: 13
Joined: Thu May 04, 2017 4:45 am

uart_read_bytes() and UART_PATTERN_DET do not work well together

Postby psiphi75 » Mon Jul 31, 2017 1:21 am

Hi,

I have some time critical code where I need to read data from the UART, wait for a specific character (using UART_PATTERN_DET), read the buffer up until the detected character and then, if conditions are met, write a new set of characters to UART TX (within 4 milliseconds). The only issue is that when an UART_PATTERN_DET event is triggered and I use uart_read_bytes() no characters are in the buffer. I set ticks_to_wait to zero, because this data should be in the UART ringbuffer. I would expect that the uart_read_bytes() to return all the characters up until the UART_PATTERN_DET event was triggered.

How do I get all the characters in the UART buffer after the UART_PATTERN_DET event is triggered without waiting any ticks?

Below is the sample code (note: CONFIG_FREERTOS_HZ = 1000).

Code: Select all

  while (true) {
    if (pdTRUE == xQueueReceive(uart_read_queue, &uart_read_event, 0)) {

      switch (uart_read_event.type) {
      case UART_PATTERN_DET:
        uart_get_buffered_data_len(UART_NUM_1, &current_buf_len);     // This always sets current_buf_len to zero.
        // Read the data, set the wait time to zero, because all the data should be in the buffer already.
        num_bytes_read = uart_read_bytes(UART_NUM_1, uart_read_buf, current_buf_len, 0);
        ESP_LOGI("uart", "UART_PATTERN_DET: current_buf_len=>%d, read %d bytes", current_buf_len, num_bytes_read);

        break;

      case UART_DATA:
        uart_get_buffered_data_len(UART_NUM_1, &current_buf_len);
        num_bytes_read = uart_read_bytes(UART_NUM_1, uart_read_buf, current_buf_len, 0);
        ESP_LOGI("uart", "UART_DATA: current_buf_len=>%d, read %d bytes", current_buf_len, num_bytes_read);
        break;

      default:
        ESP_LOGI("uart", "other event.");
        break;
      }
    }
  }

WiFive
Posts: 3529
Joined: Tue Dec 01, 2015 7:35 am

Re: uart_read_bytes() and UART_PATTERN_DET do not work well together

Postby WiFive » Mon Jul 31, 2017 3:15 am

Uart has a hardware fifo and bytes are only copied from there to the buffer when a threshold or timeout is reached. But yes it makes sense that this should also happen with the pattern detect interrupt. Or that uart_read_bytes will empty the fifo whenever possible, even when not full.

psiphi75
Posts: 13
Joined: Thu May 04, 2017 4:45 am

Re: uart_read_bytes() and UART_PATTERN_DET do not work well together

Postby psiphi75 » Mon Jul 31, 2017 6:19 am

Thanks. Is there anyway to force a flush of the hardware fifo buffer such that it can be read by uart_read_bytes()?

WiFive
Posts: 3529
Joined: Tue Dec 01, 2015 7:35 am

Re: uart_read_bytes() and UART_PATTERN_DET do not work well together

Postby WiFive » Mon Jul 31, 2017 7:48 am

Modify the driver or set RX threshold to 1 (but maybe performance impact)

Who is online

Users browsing this forum: Majestic-12 [Bot] and 100 guests