Page 1 of 1

uart_read_bytes() and UART_PATTERN_DET do not work well together

Posted: Mon Jul 31, 2017 1:21 am
by psiphi75
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;
      }
    }
  }

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

Posted: Mon Jul 31, 2017 3:15 am
by WiFive
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.

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

Posted: Mon Jul 31, 2017 6:19 am
by psiphi75
Thanks. Is there anyway to force a flush of the hardware fifo buffer such that it can be read by uart_read_bytes()?

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

Posted: Mon Jul 31, 2017 7:48 am
by WiFive
Modify the driver or set RX threshold to 1 (but maybe performance impact)