-
Hi! I am using slightly modified example of embassy_serial.rs I have a device which is sending me exact 4-bytes frames with a constant startup byte (0xff). Generaly it is working really well. I am sending a 0x01, the device respond with the response frame. It starts with the read call giving me Ok(3): 3 bytes instead of 4. I mean this:
And after this some magic is happening because when I request for another sample data from the device then whatever I will try to read I always got (one byte off in the buffer), I mean this readings:
Calling I was trying hard to somehow solve this - even trying to call The flushing function works only for output buffer, moreover looking at the code it looks that is not flushing anything, but only checking is_tx_idle() and returning depending on this: esp-hal/esp-hal-common/src/uart.rs Line 576 in 92a2cc7 I can see that in IDF there is: uart_flush/uart_flush_input Please help - I am out of ideas. I don't want to reset the chip in such cases, but I am trying to find some reliable "recovery" method. |
Beta Was this translation helpful? Give feedback.
Replies: 17 comments
-
Hello! There is another user also having problems with UART ( esp-rs/esp-wifi-sys#214 ) ... But I think you already discovered that The other side of the communication is a PC or another embedded device? Is there a chance to hook-up a logic analyzer to see what is happening on the wire (e.g. if the other side might just happen to send three bytes and later the next byte) Does the behavior change by changing the baud rate? Would it be possible to test this with non-async code? Regarding the ESP-IDF function
There is also https://www.espressif.com/sites/default/files/documentation/esp32_errata_en.pdf - |
Beta Was this translation helpful? Give feedback.
-
Yes, I am following the esp-rs/esp-wifi-sys#214 The device is the ultrasonic sensor which is providing the UART communication mode. I am not able to lower the baud rate as it is hardcoded in the device. Testing in non-async code would be hard as even now I sometimes need to wait days to have this occur. Thank you for valuable tips - I think I'll start with hooking the scope but I need to consider proper triggering and stop the execution in proper place. |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
-
I was testing re-enabling interrupts. It also doesn't help :( hal::interrupt::disable(hal::Cpu::ProCpu, hal::peripherals::Interrupt::UART1);
hal::interrupt::enable(hal::peripherals::Interrupt::UART1, hal::interrupt::Priority::Priority1).unwrap(); @bjoernQ I hope this is what you meant? |
Beta Was this translation helpful? Give feedback.
-
Thanks! Most probably the errata applies here. I guess an inconsistent fifo_cnt would explain the observed behavior. |
Beta Was this translation helpful? Give feedback.
-
OK, so according to this errata the problem is this line, right? esp-hal/esp-hal-common/src/uart.rs Line 592 in 7866896 because it is basing on the fifo_cnt , not a FIFO pointer .
I created a simple function which is returning me this value and I am trying to confirm the problem... pub fn get_fifo_counter(&mut self) -> u16 {
self.uart.get_rx_fifo_count()
} |
Beta Was this translation helpful? Give feedback.
-
Really curious about the outcome. If we can track it down to fifo-count being incorrect we hopefully can apply a workaround for ESP32 |
Beta Was this translation helpful? Give feedback.
-
Ok, just catch it again with my debug enabled. Lastly I enhanced my debug function to return tuple: not only fifo_count but also a fifo.as_ptr() pub fn get_fifo_counter(&mut self) -> (u8,u16) {
(self.uart.register_block().fifo.as_ptr() as u8,
self.uart.get_rx_fifo_count())
} During normal run I've got:
but when the problem starts (I've got 3 bytes), then my debug function still returning this (0,0):
The PDF is saying: Is the FIFO pointer the following? |
Beta Was this translation helpful? Give feedback.
-
I think the RX FIFO pointer is this: https://github.com/espressif/esp-idf/blob/3640dc86bb4b007da0c53500d90e318f0b7543ef/components/soc/esp32/include/soc/uart_reg.h#L1116 Unfortunately it's not mentioned in the TRM at all |
Beta Was this translation helpful? Give feedback.
-
@bjoernQ Today I created the following function: fn get_rx_fifo_ptr(&mut self) -> u16 {
self.register_block()
.mem_rx_status
.read()
.mem_rx_rd_addr()
.bits()
.into()
} And this gives me in each read() iteration from the program start: Edit: |
Beta Was this translation helpful? Give feedback.
-
@bjoernQ The last good call:
Now I've got 3 bytes:
The last value from But then it is something more interesting...
And here I can see that when everything is working ok (from the boot-up) these values are in pair. At the end I read additional single byte regardless of this: esp-hal/esp-hal-common/src/uart.rs Line 597 in 976549f I was just trying to read those missing byte anyway - no matter what the get_rx_fifo_count() was returning. Then I was trying to get back to normal reading but those "offset" problem is still there:
My assumption are:
Maybe I'll try to read 3 bytes instead of this one and will see ... |
Beta Was this translation helpful? Give feedback.
-
Ok ... I am doing some progress. I can now influence the For instance:
I know that the buffer is now misaligned and I am probably reading the most latest data in this round buffer, but at least I can now force this somehow. |
Beta Was this translation helpful? Give feedback.
-
Ok... it seems that my approach did work :)
Now I am debugging what is returning the get_rx_fifo_count() with every read() call. Now I fixed this and I can see value of 4, which is what I expected to see... |
Beta Was this translation helpful? Give feedback.
-
Ok, I think it's working great, so I prepared a PR for this problem: #804 I am pretty sure that it was not there, and in fact it wasn't:
So it was added yesterday! |
Beta Was this translation helpful? Give feedback.
-
@bjoernQ We have it merged now, much thanks for your time on this! btw: just curious: was this errata update initiated by you?
|
Beta Was this translation helpful? Give feedback.
-
Thanks for the fix in esp-hal! The update of the errata document was really just a coincidence - interesting timing. Would you mind submitting your feedback to the document via the link at the bottom of each page in the pdf: Submit Documentation Feedback Thanks again! Good job! |
Beta Was this translation helpful? Give feedback.
-
Sure I'll submit this, thanks! |
Beta Was this translation helpful? Give feedback.
Thanks! Most probably the errata applies here. I guess an inconsistent fifo_cnt would explain the observed behavior.