From f4950c44499b2d1ba1280000a05a1a5edd03d8ca Mon Sep 17 00:00:00 2001 From: Ben Gamari Date: Sat, 31 Jul 2021 11:51:40 -0400 Subject: [PATCH 1/5] examples: Consistently use unwrap! in favor of .unwrap() Unfortunately errors from `embedded_graphics` and `core` doesn't provide the necessary instances currently. --- examples/nrf/src/bin/blinky.rs | 5 +++-- examples/nrf/src/bin/gpiote_port.rs | 8 ++++---- examples/nrf/src/bin/mpsc.rs | 7 ++++--- examples/nrf/src/bin/qspi.rs | 12 ++++++------ examples/nrf/src/bin/qspi_lowpower.rs | 8 ++++---- examples/nrf/src/bin/rng.rs | 6 +++--- examples/nrf/src/bin/spim.rs | 16 ++++++++-------- examples/nrf/src/bin/twim.rs | 2 +- examples/nrf/src/bin/twim_lowpower.rs | 2 +- examples/stm32f4/src/bin/blinky.rs | 4 ++-- examples/stm32f4/src/bin/button.rs | 10 +++++----- examples/stm32f4/src/bin/usart.rs | 6 +++--- examples/stm32f4/src/bin/usart_dma.rs | 2 +- examples/stm32h7/src/bin/blinky.rs | 4 ++-- examples/stm32h7/src/bin/usart.rs | 6 +++--- examples/stm32l0/src/bin/blinky.rs | 4 ++-- examples/stm32l0/src/bin/button.rs | 10 +++++----- examples/stm32l4/src/bin/blinky.rs | 4 ++-- examples/stm32l4/src/bin/button.rs | 2 +- examples/stm32l4/src/bin/spi_dma.rs | 4 ++-- examples/stm32l4/src/bin/usart.rs | 6 +++--- examples/stm32wb55/src/bin/blinky.rs | 4 ++-- 22 files changed, 67 insertions(+), 65 deletions(-) diff --git a/examples/nrf/src/bin/blinky.rs b/examples/nrf/src/bin/blinky.rs index 6d4561beb..8fa1f87a9 100644 --- a/examples/nrf/src/bin/blinky.rs +++ b/examples/nrf/src/bin/blinky.rs @@ -6,6 +6,7 @@ #[path = "../example_common.rs"] mod example_common; +use defmt::unwrap; use embassy::executor::Spawner; use embassy::time::{Duration, Timer}; use embassy_nrf::gpio::{Level, Output, OutputDrive}; @@ -17,9 +18,9 @@ async fn main(_spawner: Spawner, p: Peripherals) { let mut led = Output::new(p.P0_13, Level::Low, OutputDrive::Standard); loop { - led.set_high().unwrap(); + unwrap!(led.set_high()); Timer::after(Duration::from_millis(300)).await; - led.set_low().unwrap(); + unwrap!(led.set_low()); Timer::after(Duration::from_millis(300)).await; } } diff --git a/examples/nrf/src/bin/gpiote_port.rs b/examples/nrf/src/bin/gpiote_port.rs index 700247d37..57f8a518e 100644 --- a/examples/nrf/src/bin/gpiote_port.rs +++ b/examples/nrf/src/bin/gpiote_port.rs @@ -32,8 +32,8 @@ async fn main(spawner: Spawner, p: Peripherals) { let btn3 = PortInput::new(Input::new(p.P0_24.degrade(), Pull::Up)); let btn4 = PortInput::new(Input::new(p.P0_25.degrade(), Pull::Up)); - spawner.spawn(button_task(1, btn1)).unwrap(); - spawner.spawn(button_task(2, btn2)).unwrap(); - spawner.spawn(button_task(3, btn3)).unwrap(); - spawner.spawn(button_task(4, btn4)).unwrap(); + unwrap!(spawner.spawn(button_task(1, btn1))); + unwrap!(spawner.spawn(button_task(2, btn2))); + unwrap!(spawner.spawn(button_task(3, btn3))); + unwrap!(spawner.spawn(button_task(4, btn4))); } diff --git a/examples/nrf/src/bin/mpsc.rs b/examples/nrf/src/bin/mpsc.rs index e31754eb8..b40ed5e0e 100644 --- a/examples/nrf/src/bin/mpsc.rs +++ b/examples/nrf/src/bin/mpsc.rs @@ -6,6 +6,7 @@ #[path = "../example_common.rs"] mod example_common; +use defmt::unwrap; use embassy::executor::Spawner; use embassy::time::{Duration, Timer}; use embassy::util::mpsc::TryRecvError; @@ -39,7 +40,7 @@ async fn main(spawner: Spawner, p: Peripherals) { let channel = CHANNEL.put(Channel::new()); let (sender, mut receiver) = mpsc::split(channel); - spawner.spawn(my_task(sender)).unwrap(); + unwrap!(spawner.spawn(my_task(sender))); // We could just loop on `receiver.recv()` for simplicity. The code below // is optimized to drain the queue as fast as possible in the spirit of @@ -53,8 +54,8 @@ async fn main(spawner: Spawner, p: Peripherals) { Err(TryRecvError::Closed) => break, }; match maybe_message { - Some(LedState::On) => led.set_high().unwrap(), - Some(LedState::Off) => led.set_low().unwrap(), + Some(LedState::On) => unwrap!(led.set_high()), + Some(LedState::Off) => unwrap!(led.set_low()), _ => (), } } diff --git a/examples/nrf/src/bin/qspi.rs b/examples/nrf/src/bin/qspi.rs index 43bfb83ca..0adeadd65 100644 --- a/examples/nrf/src/bin/qspi.rs +++ b/examples/nrf/src/bin/qspi.rs @@ -35,19 +35,19 @@ async fn main(_spawner: Spawner, p: Peripherals) { .await; let mut id = [1; 3]; - q.custom_instruction(0x9F, &[], &mut id).await.unwrap(); + unwrap!(q.custom_instruction(0x9F, &[], &mut id).await); info!("id: {}", id); // Read status register let mut status = [4; 1]; - q.custom_instruction(0x05, &[], &mut status).await.unwrap(); + unwrap!(q.custom_instruction(0x05, &[], &mut status).await); info!("status: {:?}", status[0]); if status[0] & 0x40 == 0 { status[0] |= 0x40; - q.custom_instruction(0x01, &status, &mut []).await.unwrap(); + unwrap!(q.custom_instruction(0x01, &status, &mut []).await); info!("enabled quad in status"); } @@ -58,19 +58,19 @@ async fn main(_spawner: Spawner, p: Peripherals) { for i in 0..8 { info!("page {:?}: erasing... ", i); - q.erase(i * PAGE_SIZE).await.unwrap(); + unwrap!(q.erase(i * PAGE_SIZE).await); for j in 0..PAGE_SIZE { buf.0[j] = pattern((j + i * PAGE_SIZE) as u32); } info!("programming..."); - q.write(i * PAGE_SIZE, &buf.0).await.unwrap(); + unwrap!(q.write(i * PAGE_SIZE, &buf.0).await); } for i in 0..8 { info!("page {:?}: reading... ", i); - q.read(i * PAGE_SIZE, &mut buf.0).await.unwrap(); + unwrap!(q.read(i * PAGE_SIZE, &mut buf.0).await); info!("verifying..."); for j in 0..PAGE_SIZE { diff --git a/examples/nrf/src/bin/qspi_lowpower.rs b/examples/nrf/src/bin/qspi_lowpower.rs index 502710db9..30df22451 100644 --- a/examples/nrf/src/bin/qspi_lowpower.rs +++ b/examples/nrf/src/bin/qspi_lowpower.rs @@ -49,19 +49,19 @@ async fn main(_spawner: Spawner, mut p: Peripherals) { .await; let mut id = [1; 3]; - q.custom_instruction(0x9F, &[], &mut id).await.unwrap(); + unwrap!(q.custom_instruction(0x9F, &[], &mut id).await); info!("id: {}", id); // Read status register let mut status = [4; 1]; - q.custom_instruction(0x05, &[], &mut status).await.unwrap(); + unwrap!(q.custom_instruction(0x05, &[], &mut status).await); info!("status: {:?}", status[0]); if status[0] & 0x40 == 0 { status[0] |= 0x40; - q.custom_instruction(0x01, &status, &mut []).await.unwrap(); + unwrap!(q.custom_instruction(0x01, &status, &mut []).await); info!("enabled quad in status"); } @@ -69,7 +69,7 @@ async fn main(_spawner: Spawner, mut p: Peripherals) { let mut buf = AlignedBuf([0u8; 64]); info!("reading..."); - q.read(0, &mut buf.0).await.unwrap(); + unwrap!(q.read(0, &mut buf.0).await); info!("read: {=[u8]:x}", buf.0); // Drop the QSPI instance. This disables the peripehral and deconfigures the pins. diff --git a/examples/nrf/src/bin/rng.rs b/examples/nrf/src/bin/rng.rs index e19982312..aa5215af6 100644 --- a/examples/nrf/src/bin/rng.rs +++ b/examples/nrf/src/bin/rng.rs @@ -6,7 +6,7 @@ #[path = "../example_common.rs"] mod example_common; -use defmt::panic; +use defmt::{panic, unwrap}; use embassy::executor::Spawner; use embassy::traits::rng::Rng as _; use embassy_nrf::interrupt; @@ -20,14 +20,14 @@ async fn main(_spawner: Spawner, p: Peripherals) { // Async API let mut bytes = [0; 4]; - rng.fill_bytes(&mut bytes).await.unwrap(); // nRF RNG is infallible + unwrap!(rng.fill_bytes(&mut bytes).await); // nRF RNG is infallible defmt::info!("Some random bytes: {:?}", bytes); // Sync API with `rand` defmt::info!("A random number from 1 to 10: {:?}", rng.gen_range(1..=10)); let mut bytes = [0; 1024]; - rng.fill_bytes(&mut bytes).await.unwrap(); + unwrap!(rng.fill_bytes(&mut bytes).await); let zero_count: u32 = bytes.iter().fold(0, |acc, val| acc + val.count_zeros()); let one_count: u32 = bytes.iter().fold(0, |acc, val| acc + val.count_ones()); defmt::info!( diff --git a/examples/nrf/src/bin/spim.rs b/examples/nrf/src/bin/spim.rs index 444e50bcf..7c9779a36 100644 --- a/examples/nrf/src/bin/spim.rs +++ b/examples/nrf/src/bin/spim.rs @@ -31,12 +31,12 @@ async fn main(_spawner: Spawner, p: Peripherals) { // softreset cortex_m::asm::delay(10); - ncs.set_low().unwrap(); + unwrap!(ncs.set_low()); cortex_m::asm::delay(5); let tx = [0xFF]; unwrap!(spim.read_write(&mut [], &tx).await); cortex_m::asm::delay(10); - ncs.set_high().unwrap(); + unwrap!(ncs.set_high()); cortex_m::asm::delay(100000); @@ -44,31 +44,31 @@ async fn main(_spawner: Spawner, p: Peripherals) { // read ESTAT cortex_m::asm::delay(5000); - ncs.set_low().unwrap(); + unwrap!(ncs.set_low()); cortex_m::asm::delay(5000); let tx = [0b000_11101, 0]; unwrap!(spim.read_write(&mut rx, &tx).await); cortex_m::asm::delay(5000); - ncs.set_high().unwrap(); + unwrap!(ncs.set_high()); info!("estat: {=[?]}", rx); // Switch to bank 3 cortex_m::asm::delay(10); - ncs.set_low().unwrap(); + unwrap!(ncs.set_low()); cortex_m::asm::delay(5); let tx = [0b100_11111, 0b11]; unwrap!(spim.read_write(&mut rx, &tx).await); cortex_m::asm::delay(10); - ncs.set_high().unwrap(); + unwrap!(ncs.set_high()); // read EREVID cortex_m::asm::delay(10); - ncs.set_low().unwrap(); + unwrap!(ncs.set_low()); cortex_m::asm::delay(5); let tx = [0b000_10010, 0]; unwrap!(spim.read_write(&mut rx, &tx).await); cortex_m::asm::delay(10); - ncs.set_high().unwrap(); + unwrap!(ncs.set_high()); info!("erevid: {=[?]}", rx); } diff --git a/examples/nrf/src/bin/twim.rs b/examples/nrf/src/bin/twim.rs index ad5653cfc..0b8f1407b 100644 --- a/examples/nrf/src/bin/twim.rs +++ b/examples/nrf/src/bin/twim.rs @@ -27,7 +27,7 @@ async fn main(_spawner: Spawner, p: Peripherals) { info!("Reading..."); let mut buf = [0u8; 16]; - twi.write_then_read(ADDRESS, &mut [0x00], &mut buf).unwrap(); + unwrap!(twi.write_then_read(ADDRESS, &mut [0x00], &mut buf)); info!("Read: {=[u8]:x}", buf); } diff --git a/examples/nrf/src/bin/twim_lowpower.rs b/examples/nrf/src/bin/twim_lowpower.rs index f8cba2496..a4bab9606 100644 --- a/examples/nrf/src/bin/twim_lowpower.rs +++ b/examples/nrf/src/bin/twim_lowpower.rs @@ -37,7 +37,7 @@ async fn main(_spawner: Spawner, mut p: Peripherals) { info!("Reading..."); let mut buf = [0u8; 16]; - twi.write_then_read(ADDRESS, &mut [0x00], &mut buf).unwrap(); + unwrap!(twi.write_then_read(ADDRESS, &mut [0x00], &mut buf)); info!("Read: {=[u8]:x}", buf); diff --git a/examples/stm32f4/src/bin/blinky.rs b/examples/stm32f4/src/bin/blinky.rs index 00eab761e..b3a0ffb8e 100644 --- a/examples/stm32f4/src/bin/blinky.rs +++ b/examples/stm32f4/src/bin/blinky.rs @@ -26,11 +26,11 @@ async fn main(_spawner: Spawner, p: Peripherals) { loop { info!("high"); - led.set_high().unwrap(); + unwrap!(led.set_high()); Timer::after(Duration::from_millis(300)).await; info!("low"); - led.set_low().unwrap(); + unwrap!(led.set_low()); Timer::after(Duration::from_millis(300)).await; } } diff --git a/examples/stm32f4/src/bin/button.rs b/examples/stm32f4/src/bin/button.rs index 82c83e80e..90998cf46 100644 --- a/examples/stm32f4/src/bin/button.rs +++ b/examples/stm32f4/src/bin/button.rs @@ -28,14 +28,14 @@ fn main() -> ! { let mut led3 = Output::new(p.PB14, Level::High, Speed::Low); loop { - if button.is_high().unwrap() { + if unwrap!(button.is_high()) { info!("high"); - led1.set_high().unwrap(); - led3.set_low().unwrap(); + unwrap!(led1.set_high()); + unwrap!(led3.set_low()); } else { info!("low"); - led1.set_low().unwrap(); - led3.set_high().unwrap(); + unwrap!(led1.set_low()); + unwrap!(led3.set_high()); } } } diff --git a/examples/stm32f4/src/bin/usart.rs b/examples/stm32f4/src/bin/usart.rs index da3261949..fd4f19e20 100644 --- a/examples/stm32f4/src/bin/usart.rs +++ b/examples/stm32f4/src/bin/usart.rs @@ -26,12 +26,12 @@ fn main() -> ! { let config = Config::default(); let mut usart = Uart::new(p.USART3, p.PD9, p.PD8, NoDma, NoDma, config); - usart.bwrite_all(b"Hello Embassy World!\r\n").unwrap(); + unwrap!(usart.bwrite_all(b"Hello Embassy World!\r\n")); info!("wrote Hello, starting echo"); let mut buf = [0u8; 1]; loop { - usart.read_blocking(&mut buf).unwrap(); - usart.bwrite_all(&buf).unwrap(); + unwrap!(usart.read_blocking(&mut buf).unwbrap()); + unwrap!(usart.bwrite_all(&buf).unwrap()); } } diff --git a/examples/stm32f4/src/bin/usart_dma.rs b/examples/stm32f4/src/bin/usart_dma.rs index 05a550e95..bf50b8ad9 100644 --- a/examples/stm32f4/src/bin/usart_dma.rs +++ b/examples/stm32f4/src/bin/usart_dma.rs @@ -31,7 +31,7 @@ async fn main(_spawner: Spawner, p: Peripherals) { let mut s: String<128> = String::new(); core::write!(&mut s, "Hello DMA World {}!\r\n", n).unwrap(); - usart.write(s.as_bytes()).await.unwrap(); + unwrap!(usart.write(s.as_bytes()).await); info!("wrote DMA"); } } diff --git a/examples/stm32h7/src/bin/blinky.rs b/examples/stm32h7/src/bin/blinky.rs index b5e0c18a5..09b34eacb 100644 --- a/examples/stm32h7/src/bin/blinky.rs +++ b/examples/stm32h7/src/bin/blinky.rs @@ -24,11 +24,11 @@ async fn main(_spawner: Spawner, p: Peripherals) { loop { info!("high"); - led.set_high().unwrap(); + unwrap!(led.set_high()); Timer::after(Duration::from_millis(500)).await; info!("low"); - led.set_low().unwrap(); + unwrap!(led.set_low()); Timer::after(Duration::from_millis(500)).await; } } diff --git a/examples/stm32h7/src/bin/usart.rs b/examples/stm32h7/src/bin/usart.rs index 9c93d3f22..a3fcc6d3f 100644 --- a/examples/stm32h7/src/bin/usart.rs +++ b/examples/stm32h7/src/bin/usart.rs @@ -23,13 +23,13 @@ async fn main_task() { let config = Config::default(); let mut usart = Uart::new(p.UART7, p.PF6, p.PF7, NoDma, NoDma, config); - usart.bwrite_all(b"Hello Embassy World!\r\n").unwrap(); + unwrap!(usart.bwrite_all(b"Hello Embassy World!\r\n")); info!("wrote Hello, starting echo"); let mut buf = [0u8; 1]; loop { - usart.read_blocking(&mut buf).unwrap(); - usart.bwrite_all(&buf).unwrap(); + unwrap!(usart.read_blocking(&mut buf)); + unwrap!(usart.bwrite_all(&buf)); } } diff --git a/examples/stm32l0/src/bin/blinky.rs b/examples/stm32l0/src/bin/blinky.rs index faa42896b..29f5d7c74 100644 --- a/examples/stm32l0/src/bin/blinky.rs +++ b/examples/stm32l0/src/bin/blinky.rs @@ -25,11 +25,11 @@ async fn main(_spawner: Spawner, mut p: Peripherals) { loop { info!("high"); - led.set_high().unwrap(); + unwrap!(led.set_high()); Timer::after(Duration::from_millis(300)).await; info!("low"); - led.set_low().unwrap(); + unwrap!(led.set_low()); Timer::after(Duration::from_millis(300)).await; } } diff --git a/examples/stm32l0/src/bin/button.rs b/examples/stm32l0/src/bin/button.rs index 6b75de509..e37d3f26d 100644 --- a/examples/stm32l0/src/bin/button.rs +++ b/examples/stm32l0/src/bin/button.rs @@ -27,14 +27,14 @@ fn main() -> ! { let mut led2 = Output::new(p.PB5, Level::High, Speed::Low); loop { - if button.is_high().unwrap() { + if unwrap!(button.is_high()) { info!("high"); - led1.set_high().unwrap(); - led2.set_low().unwrap(); + unwrap!(led1.set_high()); + unwrap!(led2.set_low()); } else { info!("low"); - led1.set_low().unwrap(); - led2.set_high().unwrap(); + unwrap!(led1.set_low()); + unwrap!(led2.set_high()); } } } diff --git a/examples/stm32l4/src/bin/blinky.rs b/examples/stm32l4/src/bin/blinky.rs index fc86b11eb..c9ba587e4 100644 --- a/examples/stm32l4/src/bin/blinky.rs +++ b/examples/stm32l4/src/bin/blinky.rs @@ -25,9 +25,9 @@ async fn main(_spawner: Spawner, p: Peripherals) { let mut led = Output::new(p.PB14, Level::High, Speed::Low); loop { - led.set_high().unwrap(); + unwrap!(led.set_high()); Timer::after(Duration::from_millis(300)).await; - led.set_low().unwrap(); + unwrap!(led.set_low()); Timer::after(Duration::from_millis(300)).await; } } diff --git a/examples/stm32l4/src/bin/button.rs b/examples/stm32l4/src/bin/button.rs index e2756b711..883a0d6c8 100644 --- a/examples/stm32l4/src/bin/button.rs +++ b/examples/stm32l4/src/bin/button.rs @@ -24,7 +24,7 @@ fn main() -> ! { let button = Input::new(p.PC13, Pull::Up); loop { - if button.is_high().unwrap() { + if unwrap!(button.is_high()) { info!("high"); } else { info!("low"); diff --git a/examples/stm32l4/src/bin/spi_dma.rs b/examples/stm32l4/src/bin/spi_dma.rs index ba77331be..d626a1290 100644 --- a/examples/stm32l4/src/bin/spi_dma.rs +++ b/examples/stm32l4/src/bin/spi_dma.rs @@ -45,10 +45,10 @@ async fn main(_spawner: Spawner, p: Peripherals) { let ready = Input::new(p.PE1, Pull::Up); cortex_m::asm::delay(100_000); - reset.set_high().unwrap(); + unwrap!(reset.set_high()); cortex_m::asm::delay(100_000); - while ready.is_low().unwrap() { + while unwrap!(ready.is_low()) { info!("waiting for ready"); } diff --git a/examples/stm32l4/src/bin/usart.rs b/examples/stm32l4/src/bin/usart.rs index ccb82b8d4..95ac84b23 100644 --- a/examples/stm32l4/src/bin/usart.rs +++ b/examples/stm32l4/src/bin/usart.rs @@ -26,12 +26,12 @@ fn main() -> ! { let config = Config::default(); let mut usart = Uart::new(p.UART4, p.PA1, p.PA0, NoDma, NoDma, config); - usart.bwrite_all(b"Hello Embassy World!\r\n").unwrap(); + unwrap!(usart.bwrite_all(b"Hello Embassy World!\r\n")); info!("wrote Hello, starting echo"); let mut buf = [0u8; 1]; loop { - usart.read_blocking(&mut buf).unwrap(); - usart.bwrite_all(&buf).unwrap(); + unwrap!(usart.read_blocking(&mut buf)); + unwrap!(usart.bwrite_all(&buf)); } } diff --git a/examples/stm32wb55/src/bin/blinky.rs b/examples/stm32wb55/src/bin/blinky.rs index 5ffef9b38..2deaf5f55 100644 --- a/examples/stm32wb55/src/bin/blinky.rs +++ b/examples/stm32wb55/src/bin/blinky.rs @@ -24,11 +24,11 @@ async fn main(_spawner: Spawner, p: Peripherals) { loop { info!("high"); - led.set_high().unwrap(); + unwrap!(led.set_high()); Timer::after(Duration::from_millis(500)).await; info!("low"); - led.set_low().unwrap(); + unwrap!(led.set_low()); Timer::after(Duration::from_millis(500)).await; } } From e44acd0d561d3e7ad58437ddced76b6e6a1fa387 Mon Sep 17 00:00:00 2001 From: Ben Gamari Date: Sat, 31 Jul 2021 12:26:12 -0400 Subject: [PATCH 2/5] stm32f4: Use unwrap! where possible --- embassy-stm32/src/rcc/f4/mod.rs | 28 ++++++++++++---------------- examples/stm32f4/src/bin/usart.rs | 4 ++-- 2 files changed, 14 insertions(+), 18 deletions(-) diff --git a/embassy-stm32/src/rcc/f4/mod.rs b/embassy-stm32/src/rcc/f4/mod.rs index 1cf3e5bd9..eab98daf8 100644 --- a/embassy-stm32/src/rcc/f4/mod.rs +++ b/embassy-stm32/src/rcc/f4/mod.rs @@ -63,7 +63,7 @@ impl<'d> Rcc<'d> { } let sysclk = if sysclk_on_pll { - plls.pllsysclk.unwrap() + unwrap!(plls.pllsysclk) } else { sysclk }; @@ -245,13 +245,11 @@ impl<'d> Rcc<'d> { // Find the lowest pllm value that minimize the difference between // target frequency and the real vco_out frequency. - let pllm = (pllm_min..=pllm_max) - .min_by_key(|pllm| { - let vco_in = pllsrcclk / pllm; - let plln = target_freq / vco_in; - target_freq - vco_in * plln - }) - .unwrap(); + let pllm = unwrap!((pllm_min..=pllm_max).min_by_key(|pllm| { + let vco_in = pllsrcclk / pllm; + let plln = target_freq / vco_in; + target_freq - vco_in * plln + })); let vco_in = pllsrcclk / pllm; assert!((1_000_000..=2_000_000).contains(&vco_in)); @@ -261,14 +259,12 @@ impl<'d> Rcc<'d> { let plln = if pll48clk { // try the different valid pllq according to the valid // main scaller values, and take the best - let pllq = (4..=9) - .min_by_key(|pllq| { - let plln = 48_000_000 * pllq / vco_in; - let pll48_diff = 48_000_000 - vco_in * plln / pllq; - let sysclk_diff = (sysclk as i32 - (vco_in * plln / sysclk_div) as i32).abs(); - (pll48_diff, sysclk_diff) - }) - .unwrap(); + let pllq = unwrap!((4..=9).min_by_key(|pllq| { + let plln = 48_000_000 * pllq / vco_in; + let pll48_diff = 48_000_000 - vco_in * plln / pllq; + let sysclk_diff = (sysclk as i32 - (vco_in * plln / sysclk_div) as i32).abs(); + (pll48_diff, sysclk_diff) + })); 48_000_000 * pllq / vco_in } else { sysclk * sysclk_div / vco_in diff --git a/examples/stm32f4/src/bin/usart.rs b/examples/stm32f4/src/bin/usart.rs index fd4f19e20..781c6a958 100644 --- a/examples/stm32f4/src/bin/usart.rs +++ b/examples/stm32f4/src/bin/usart.rs @@ -31,7 +31,7 @@ fn main() -> ! { let mut buf = [0u8; 1]; loop { - unwrap!(usart.read_blocking(&mut buf).unwbrap()); - unwrap!(usart.bwrite_all(&buf).unwrap()); + unwrap!(usart.read_blocking(&mut buf)); + unwrap!(usart.bwrite_all(&buf)); } } From 41aaff95f866a5958bd06c0ee6adc2267b34e3c6 Mon Sep 17 00:00:00 2001 From: Ben Gamari Date: Sat, 31 Jul 2021 12:32:14 -0400 Subject: [PATCH 3/5] stm32h7: Use unwrap! --- embassy-stm32/src/rcc/h7/mod.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/embassy-stm32/src/rcc/h7/mod.rs b/embassy-stm32/src/rcc/h7/mod.rs index 2d9602a34..d6a55c3b4 100644 --- a/embassy-stm32/src/rcc/h7/mod.rs +++ b/embassy-stm32/src/rcc/h7/mod.rs @@ -120,7 +120,7 @@ impl<'d> Rcc<'d> { unsafe { pll_setup(srcclk.0, &self.config.pll3, 2) }; let sys_ck = if sys_use_pll1_p { - Hertz(pll1_p_ck.unwrap()) // Must have been set by sys_ck_setup + Hertz(unwrap!(pll1_p_ck)) // Must have been set by sys_ck_setup } else { sys_ck }; @@ -390,7 +390,7 @@ impl<'d> Rcc<'d> { // set. The traceclk mux is synchronous with the system // clock mux, but has pll1_r_ck as an input. In order to // keep traceclk running, we force a pll1_r_ck. - (true, None) => Some(Hertz(self.config.pll1.p_ck.unwrap().0 / 2)), + (true, None) => Some(Hertz(unwrap!(self.config.pll1.p_ck).0 / 2)), // Either pll1 not selected as system clock, free choice // of pll1_r_ck. Or pll1 is selected, assume user has set From 40e7176e13a3166b8f1e01e179ee38efe85bfa71 Mon Sep 17 00:00:00 2001 From: Ben Gamari Date: Sat, 31 Jul 2021 16:34:50 -0400 Subject: [PATCH 4/5] embassy-stm32: Eliminate use of unwrap --- embassy-stm32/src/time_driver.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/embassy-stm32/src/time_driver.rs b/embassy-stm32/src/time_driver.rs index 3ae4b1c4d..91b8525ae 100644 --- a/embassy-stm32/src/time_driver.rs +++ b/embassy-stm32/src/time_driver.rs @@ -106,7 +106,10 @@ impl State { r.cnt().write(|w| w.set_cnt(0)); let psc = timer_freq.0 / TICKS_PER_SECOND as u32 - 1; - let psc: u16 = psc.try_into().unwrap(); + let psc: u16 = match psc.try_into() { + Err(_) => panic!("psc division overflow: {}", psc), + Ok(n) => n, + }; r.psc().write(|w| w.set_psc(psc)); r.arr().write(|w| w.set_arr(u16::MAX)); From a3b56a3764d223d96aaaa76e59e8741ffad179dc Mon Sep 17 00:00:00 2001 From: Ben Gamari Date: Sat, 31 Jul 2021 12:34:07 -0400 Subject: [PATCH 5/5] embassy-macros: Use `defmt::unwrap!` when spawning `embassy::main` But only when `defmt` feature is enabled. --- embassy-macros/src/lib.rs | 2 +- embassy/src/executor/mod.rs | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/embassy-macros/src/lib.rs b/embassy-macros/src/lib.rs index d00baebfe..ddcee0cb1 100644 --- a/embassy-macros/src/lib.rs +++ b/embassy-macros/src/lib.rs @@ -364,7 +364,7 @@ pub fn main(args: TokenStream, item: TokenStream) -> TokenStream { #chip_setup executor.run(|spawner| { - spawner.spawn(__embassy_main(spawner, p)).unwrap(); + spawner.must_spawn(__embassy_main(spawner, p)); }) } diff --git a/embassy/src/executor/mod.rs b/embassy/src/executor/mod.rs index ee05b6760..f3c877290 100644 --- a/embassy/src/executor/mod.rs +++ b/embassy/src/executor/mod.rs @@ -56,6 +56,14 @@ impl Spawner { } } + /// Used by the `embassy_macros::main!` macro to throw an error when spawn + /// fails. This is here to allow conditional use of `defmt::unwrap!` + /// without introducing a `defmt` feature in the `embassy_macros` package, + /// which would require use of `-Z namespaced-features`. + pub fn must_spawn(&self, token: SpawnToken) -> () { + unwrap!(self.spawn(token)); + } + /// Convert this Spawner to a SendSpawner. This allows you to send the /// spawner to other threads, but the spawner loses the ability to spawn /// non-Send tasks.