diff --git a/examples/rtic_bare8.rs b/examples/rtic_bare8.rs index 5364384cb1898fc54efb0642bbf3ee2c13aafbdf..24c1f2e084bbbce96bbd636eab892bffd8d7c6d5 100644 --- a/examples/rtic_bare8.rs +++ b/examples/rtic_bare8.rs @@ -175,6 +175,7 @@ const APP: () = { // How did the added tracing/instrumentation affect the behavior? // // ** your answer here ** +// More error prone // // Commit your answer (bare8_3) // @@ -187,6 +188,7 @@ const APP: () = { // Experiment a bit, what is the max length sequence you can receive without errors? // // ** your answer here ** +// we got 25 // // Commit your answer (bare8_4) // diff --git a/examples/rtic_bare9.rs b/examples/rtic_bare9.rs index a1fc61c6b3a97855fc50f1fb85419a0bfe7c27ce..f39150e7c22c7393a7e1adb56bb50f5771c56ecd 100644 --- a/examples/rtic_bare9.rs +++ b/examples/rtic_bare9.rs @@ -8,6 +8,7 @@ #![no_std] use panic_rtt_target as _; +use stm32f4xx_hal::nb::block; use stm32f4xx_hal::{ prelude::*, @@ -24,6 +25,8 @@ const APP: () = { // Late resources TX: Tx<USART2>, RX: Rx<USART2>, + RECV: i32, + ERR: i32, } // init runs in an interrupt free section @@ -44,6 +47,9 @@ const APP: () = { let tx = gpioa.pa2.into_alternate_af7(); let rx = gpioa.pa3.into_alternate_af7(); + let mut errors:i32 = 0; + let mut received:i32 = 0; + let mut serial = Serial::usart2( device.USART2, (tx, rx), @@ -59,7 +65,7 @@ const APP: () = { let (tx, rx) = serial.split(); // Late resources - init::LateResources { TX: tx, RX: rx } + init::LateResources { TX: tx, RX: rx, ERR:errors, RECV:received } } // idle may be interrupted by other interrupts/tasks in the system @@ -74,16 +80,36 @@ const APP: () = { #[task(resources = [TX], priority = 1, capacity = 128)] fn rx(cx: rx::Context, data: u8) { let tx = cx.resources.TX; - tx.write(data).unwrap(); - rprintln!("data {}", data); + rprintln!("data {:b}", data); + match block!(tx.write(data)) { + Ok(_val) => { + rprintln!("Ok {:b}", data); + } + Err(err) => { + rprintln!("Error {:?}", err); + } + } } // Task bound to the USART2 interrupt. - #[task(binds = USART2, priority = 2, resources = [RX], spawn = [rx])] + #[task(binds = USART2, priority = 2, resources = [RX, ERR, RECV], spawn = [rx])] fn usart2(cx: usart2::Context) { let rx = cx.resources.RX; - let data = rx.read().unwrap(); - cx.spawn.rx(data).unwrap(); + let received = cx.resources.RECV; + let errors = cx.resources.ERR; + + match block!(rx.read()) { + Ok(byte) => { + *received += 1; + rprintln!("Ok {:b} Nmbr {:?}", byte, received); + cx.spawn.rx(byte).unwrap(); + } + Err(err) => { + *errors += 1; + rprintln!("Error {:?} Nmbr {:?}", err, errors); + } + } + } extern "C" { @@ -149,6 +175,7 @@ const APP: () = { // Were you able to crash it? // // ** your answer here ** +// yes, couldnt handle 123 // // Notice, the input tracing in `moserial` seems broken, and may loose data. // So don't be alarmed if data is missing, its a GUI tool after all.