Skip to content
Snippets Groups Projects
Commit d4c46632 authored by Benjamin Koch's avatar Benjamin Koch
Browse files

clean up

parent ea2e5fee
No related branches found
No related tags found
No related merge requests found
use defmt::*; use defmt::*;
use embassy_futures::select::*; use embassy_futures::select::*;
use embassy_futures::yield_now;
use embassy_rp::gpio; use embassy_rp::gpio;
use embassy_rp::pac; use embassy_rp::pac;
use embassy_rp::interrupt::UART0_IRQ; use embassy_rp::interrupt::UART0_IRQ;
...@@ -482,24 +483,25 @@ impl<H: RS485Handler> RS485<H> { ...@@ -482,24 +483,25 @@ impl<H: RS485Handler> RS485<H> {
*/ */
}, },
Either4::Fourth(Either3::Third(())) => { Either4::Fourth(Either3::Third(())) => {
let x = unsafe { embassy_rp::pac::UART0.uartfr().read().rxfe() }; // We were sometimes missing the last two chars so wait for the IRQ to copy them to the buffer.
let x2 = unsafe { embassy_rp::pac::UART0.uartfr().read().0 }; let mut wait_for_rx_irq_cnt = 0;
let mut x5 = 0; while ! unsafe { embassy_rp::pac::UART0.uartfr().read().rxfe() } && wait_for_rx_irq_cnt < 500 {
while ! unsafe { embassy_rp::pac::UART0.uartfr().read().rxfe() } {
// wait for IRQ to handle this // wait for IRQ to handle this
x5 += 1; wait_for_rx_irq_cnt += 1;
if false {
Timer::after(Duration::from_micros(1)).await;
} else {
yield_now().await;
}
} }
let rx_data = match select(self.rx.read(&mut self.rx_buffer), core::future::ready(())).await { let rx_data = match select(self.rx.read(&mut self.rx_buffer), core::future::ready(())).await {
Either::First(Ok(len)) => { Either::First(Ok(len)) => {
// The UART uses a ringbuffer and it only pops data once so we need to ask a second // The UART uses a ringbuffer and it only pops data once so we need to ask a second
// time if the frame wraps around the end of the ringbuffer and we want all data. // time if the frame wraps around the end of the ringbuffer and we want all data.
//FIXME There is a race condition: If the ringbuffer was full, it will exactly fill our buffer.
// However, we could be receiving another character right now, which will probably cause a panic in read().
// -> Actually, pop() has a sane API so I think this cannot happen: The closure returns how
// much bytes have been processed and I think embassy-rp does that correctly.
match select(self.rx.read(&mut self.rx_buffer[len..]), core::future::ready(())).await { match select(self.rx.read(&mut self.rx_buffer[len..]), core::future::ready(())).await {
Either::First(Ok(len2)) => { Either::First(Ok(len2)) => {
info!("DEBUG: We needed two calls to self.rx.read(): {} + {}", len, len2); //info!("DEBUG: We needed two calls to self.rx.read(): {} + {}", len, len2);
if false { if false {
// ... and a third time's the charm because we may have received the last char right now. // ... and a third time's the charm because we may have received the last char right now.
// -> Actually, it isn't. That's still not enough so we have the loop with rxfe above. // -> Actually, it isn't. That's still not enough so we have the loop with rxfe above.
...@@ -524,10 +526,8 @@ impl<H: RS485Handler> RS485<H> { ...@@ -524,10 +526,8 @@ impl<H: RS485Handler> RS485<H> {
Ok(&self.rx_buffer[0..0]) Ok(&self.rx_buffer[0..0])
}, },
}; };
let x3 = unsafe { embassy_rp::pac::UART0.uartfr().read().rxfe() }; if wait_for_rx_irq_cnt > 0 {
let x4 = unsafe { embassy_rp::pac::UART0.uartfr().read().0 }; info!("DEBUG: RX: {} bytes, waited for {} cycles", rx_data.map(|x| x.len()), wait_for_rx_irq_cnt);
if !x || !x3 {
info!("DEBUG: RX: {} bytes, {}, {}, {}, {}, x5={}", rx_data.map(|x| x.len()), x, x2, x3, x4, x5);
} }
//let addr = unsafe { embassy_rp::pac::PIO0.sm(0).addr().read().addr() }; //let addr = unsafe { embassy_rp::pac::PIO0.sm(0).addr().read().addr() };
......
...@@ -90,6 +90,7 @@ impl<const FLASH_SIZE: usize> UF2UpdateHandler<FLASH_SIZE> { ...@@ -90,6 +90,7 @@ impl<const FLASH_SIZE: usize> UF2UpdateHandler<FLASH_SIZE> {
} }
pub fn write(self: &mut Self, pos: u32, data: &[u8]) -> Result<(), ModbusErrorCode> { pub fn write(self: &mut Self, pos: u32, data: &[u8]) -> Result<(), ModbusErrorCode> {
//info!("UF2Updater::write: {}, len={}", pos, data.len());
if data.len() > 256 { if data.len() > 256 {
// We could handle these cases but we are limited by Modbus frames anyway. // We could handle these cases but we are limited by Modbus frames anyway.
info!("Too much data in one call to UF2UpdateHandler::write()"); info!("Too much data in one call to UF2UpdateHandler::write()");
...@@ -144,6 +145,9 @@ impl<const FLASH_SIZE: usize> UF2UpdateHandler<FLASH_SIZE> { ...@@ -144,6 +145,9 @@ impl<const FLASH_SIZE: usize> UF2UpdateHandler<FLASH_SIZE> {
let uf2_block = &self.buf.0[0..512]; let uf2_block = &self.buf.0[0..512];
//info!("process_sector: {}", uf2_block);
//info!("process_sector: len={}", uf2_block.len());
let uf2_header = Uf2BlockHeader::read_from_prefix(uf2_block).unwrap(); let uf2_header = Uf2BlockHeader::read_from_prefix(uf2_block).unwrap();
let uf2_footer = Uf2BlockFooter::read_from_suffix(uf2_block).unwrap(); let uf2_footer = Uf2BlockFooter::read_from_suffix(uf2_block).unwrap();
if uf2_header.magic_start0 != UF2_MAGIC_START0 || uf2_header.magic_start1 != UF2_MAGIC_START1 || uf2_footer.magic_end != UF2_MAGIC_END { if uf2_header.magic_start0 != UF2_MAGIC_START0 || uf2_header.magic_start1 != UF2_MAGIC_START1 || uf2_footer.magic_end != UF2_MAGIC_END {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment