From f7858631d8cd876b41f02f2f4d6613b6d34bc68a Mon Sep 17 00:00:00 2001 From: Dario Nieuwenhuis Date: Mon, 17 May 2021 03:01:30 +0200 Subject: [PATCH] stm32: fix build, add ci --- .github/workflows/rust.yml | 8 ++ embassy-nrf/src/chips/nrf52805.rs | 2 + embassy-nrf/src/chips/nrf52810.rs | 2 + embassy-nrf/src/chips/nrf52811.rs | 2 + embassy-nrf/src/chips/nrf52820.rs | 2 + embassy-nrf/src/chips/nrf52832.rs | 2 + embassy-nrf/src/chips/nrf52833.rs | 2 + embassy-nrf/src/chips/nrf52840.rs | 2 + embassy-rp/src/dma.rs | 5 +- embassy-rp/src/interrupt.rs | 1 + embassy-rp/src/pll.rs | 2 +- embassy-stm32-examples/src/example_common.rs | 17 +++ embassy-stm32/Cargo.toml | 1 + embassy-stm32/src/dma/v2.rs | 2 +- embassy-stm32/src/fmt.rs | 114 +++++++++++++++++++ embassy-stm32/src/usart/v1.rs | 6 +- embassy-stm32/src/usart/v2.rs | 1 + embassy-traits/src/lib.rs | 2 +- 18 files changed, 166 insertions(+), 7 deletions(-) create mode 100644 embassy-stm32-examples/src/example_common.rs create mode 100644 embassy-stm32/src/fmt.rs diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 97c668aca..931a7bc01 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -57,6 +57,14 @@ jobs: features: nrf52840,defmt - package: embassy-rp-examples target: thumbv6m-none-eabi + - package: embassy-stm32 + target: thumbv7em-none-eabi + features: stm32f411ce + - package: embassy-stm32 + target: thumbv7em-none-eabi + features: stm32f429zi + - package: embassy-stm32-examples + target: thumbv7em-none-eabi steps: - uses: actions/checkout@v2 diff --git a/embassy-nrf/src/chips/nrf52805.rs b/embassy-nrf/src/chips/nrf52805.rs index 272cabc06..f3ceb98e4 100644 --- a/embassy-nrf/src/chips/nrf52805.rs +++ b/embassy-nrf/src/chips/nrf52805.rs @@ -153,7 +153,9 @@ impl_pin!(P0_30, 0, 30); impl_pin!(P0_31, 0, 31); pub mod irqs { + use crate::pac::Interrupt as InterruptEnum; use embassy_macros::interrupt_declare as declare; + declare!(POWER_CLOCK); declare!(RADIO); declare!(UARTE0_UART0); diff --git a/embassy-nrf/src/chips/nrf52810.rs b/embassy-nrf/src/chips/nrf52810.rs index 9e9f80090..dae260545 100644 --- a/embassy-nrf/src/chips/nrf52810.rs +++ b/embassy-nrf/src/chips/nrf52810.rs @@ -158,7 +158,9 @@ impl_pin!(P0_30, 0, 30); impl_pin!(P0_31, 0, 31); pub mod irqs { + use crate::pac::Interrupt as InterruptEnum; use embassy_macros::interrupt_declare as declare; + declare!(POWER_CLOCK); declare!(RADIO); declare!(UARTE0_UART0); diff --git a/embassy-nrf/src/chips/nrf52811.rs b/embassy-nrf/src/chips/nrf52811.rs index a9ef0ed19..6f9edff35 100644 --- a/embassy-nrf/src/chips/nrf52811.rs +++ b/embassy-nrf/src/chips/nrf52811.rs @@ -159,7 +159,9 @@ impl_pin!(P0_30, 0, 30); impl_pin!(P0_31, 0, 31); pub mod irqs { + use crate::pac::Interrupt as InterruptEnum; use embassy_macros::interrupt_declare as declare; + declare!(POWER_CLOCK); declare!(RADIO); declare!(UARTE0_UART0); diff --git a/embassy-nrf/src/chips/nrf52820.rs b/embassy-nrf/src/chips/nrf52820.rs index d0a8b9e9a..8bc50b6d3 100644 --- a/embassy-nrf/src/chips/nrf52820.rs +++ b/embassy-nrf/src/chips/nrf52820.rs @@ -157,7 +157,9 @@ impl_pin!(P0_30, 0, 30); impl_pin!(P0_31, 0, 31); pub mod irqs { + use crate::pac::Interrupt as InterruptEnum; use embassy_macros::interrupt_declare as declare; + declare!(POWER_CLOCK); declare!(RADIO); declare!(UARTE0_UART0); diff --git a/embassy-nrf/src/chips/nrf52832.rs b/embassy-nrf/src/chips/nrf52832.rs index 4e57f2fa4..6f3f7fc7b 100644 --- a/embassy-nrf/src/chips/nrf52832.rs +++ b/embassy-nrf/src/chips/nrf52832.rs @@ -172,7 +172,9 @@ impl_pin!(P0_30, 0, 30); impl_pin!(P0_31, 0, 31); pub mod irqs { + use crate::pac::Interrupt as InterruptEnum; use embassy_macros::interrupt_declare as declare; + declare!(POWER_CLOCK); declare!(RADIO); declare!(UARTE0_UART0); diff --git a/embassy-nrf/src/chips/nrf52833.rs b/embassy-nrf/src/chips/nrf52833.rs index 30b0ab007..a0240b196 100644 --- a/embassy-nrf/src/chips/nrf52833.rs +++ b/embassy-nrf/src/chips/nrf52833.rs @@ -212,7 +212,9 @@ impl_pin!(P1_14, 1, 14); impl_pin!(P1_15, 1, 15); pub mod irqs { + use crate::pac::Interrupt as InterruptEnum; use embassy_macros::interrupt_declare as declare; + declare!(POWER_CLOCK); declare!(RADIO); declare!(UARTE0_UART0); diff --git a/embassy-nrf/src/chips/nrf52840.rs b/embassy-nrf/src/chips/nrf52840.rs index 0b6c3f68e..d4dcfd063 100644 --- a/embassy-nrf/src/chips/nrf52840.rs +++ b/embassy-nrf/src/chips/nrf52840.rs @@ -217,7 +217,9 @@ impl_pin!(P1_14, 1, 14); impl_pin!(P1_15, 1, 15); pub mod irqs { + use crate::pac::Interrupt as InterruptEnum; use embassy_macros::interrupt_declare as declare; + declare!(POWER_CLOCK); declare!(RADIO); declare!(UARTE0_UART0); diff --git a/embassy-rp/src/dma.rs b/embassy-rp/src/dma.rs index 73be6d76c..60c1b8809 100644 --- a/embassy-rp/src/dma.rs +++ b/embassy-rp/src/dma.rs @@ -1,9 +1,8 @@ use core::sync::atomic::{compiler_fence, Ordering}; -use defmt::{assert, *}; - +use crate::fmt::{assert, *}; +use crate::pac::dma::vals; use crate::{pac, peripherals}; -use pac::dma::vals; pub struct Dma { inner: T, diff --git a/embassy-rp/src/interrupt.rs b/embassy-rp/src/interrupt.rs index 262f7f546..a4e5959ab 100644 --- a/embassy-rp/src/interrupt.rs +++ b/embassy-rp/src/interrupt.rs @@ -9,6 +9,7 @@ pub use embassy_extras::interrupt::Priority3 as Priority; mod irqs { use super::*; + use crate::pac::Interrupt as InterruptEnum; declare!(TIMER_IRQ_0); declare!(TIMER_IRQ_1); diff --git a/embassy-rp/src/pll.rs b/embassy-rp/src/pll.rs index 2726367e7..fb643b016 100644 --- a/embassy-rp/src/pll.rs +++ b/embassy-rp/src/pll.rs @@ -1,6 +1,6 @@ use core::ops::Deref; -use defmt::{assert, *}; +use crate::fmt::{assert, *}; use crate::pac; const XOSC_MHZ: u32 = 12; diff --git a/embassy-stm32-examples/src/example_common.rs b/embassy-stm32-examples/src/example_common.rs new file mode 100644 index 000000000..54d633837 --- /dev/null +++ b/embassy-stm32-examples/src/example_common.rs @@ -0,0 +1,17 @@ +#![macro_use] + +use defmt_rtt as _; // global logger +use panic_probe as _; + +pub use defmt::*; + +use core::sync::atomic::{AtomicUsize, Ordering}; + +defmt::timestamp! {"{=u64}", { + static COUNT: AtomicUsize = AtomicUsize::new(0); + // NOTE(no-CAS) `timestamps` runs with interrupts disabled + let n = COUNT.load(Ordering::Relaxed); + COUNT.store(n + 1, Ordering::Relaxed); + n as u64 + } +} diff --git a/embassy-stm32/Cargo.toml b/embassy-stm32/Cargo.toml index 3d5bb57cf..81bd032c8 100644 --- a/embassy-stm32/Cargo.toml +++ b/embassy-stm32/Cargo.toml @@ -19,6 +19,7 @@ futures = { version = "0.3.5", default-features = false, features = ["async-awai rand_core = { version = "0.6.2", optional = true } sdio-host = { version = "0.5.0", optional = true } embedded-sdmmc = { git = "https://github.com/thalesfragoso/embedded-sdmmc-rs", branch = "async", optional = true } +critical-section = "0.2.1" [build-dependencies] regex = "1.4.6" diff --git a/embassy-stm32/src/dma/v2.rs b/embassy-stm32/src/dma/v2.rs index 63d0586f3..0dfa01dcd 100644 --- a/embassy-stm32/src/dma/v2.rs +++ b/embassy-stm32/src/dma/v2.rs @@ -184,4 +184,4 @@ pub(crate) unsafe fn init() { interrupt::DMA2_Stream5::steal().enable(); interrupt::DMA2_Stream6::steal().enable(); interrupt::DMA2_Stream7::steal().enable(); -} \ No newline at end of file +} diff --git a/embassy-stm32/src/fmt.rs b/embassy-stm32/src/fmt.rs new file mode 100644 index 000000000..160642ccd --- /dev/null +++ b/embassy-stm32/src/fmt.rs @@ -0,0 +1,114 @@ +#![macro_use] +#![allow(clippy::module_inception)] +#![allow(unused)] + +#[cfg(all(feature = "defmt", feature = "log"))] +compile_error!("You may not enable both `defmt` and `log` features."); + +pub use fmt::*; + +#[cfg(feature = "defmt")] +mod fmt { + pub use defmt::{ + assert, assert_eq, assert_ne, debug, debug_assert, debug_assert_eq, debug_assert_ne, error, + info, panic, todo, trace, unreachable, unwrap, warn, + }; +} + +#[cfg(feature = "log")] +mod fmt { + pub use core::{ + assert, assert_eq, assert_ne, debug_assert, debug_assert_eq, debug_assert_ne, panic, todo, + unreachable, + }; + pub use log::{debug, error, info, trace, warn}; +} + +#[cfg(not(any(feature = "defmt", feature = "log")))] +mod fmt { + #![macro_use] + + pub use core::{ + assert, assert_eq, assert_ne, debug_assert, debug_assert_eq, debug_assert_ne, panic, todo, + unreachable, + }; + + macro_rules! trace { + ($($msg:expr),+ $(,)?) => { + () + }; + } + + macro_rules! debug { + ($($msg:expr),+ $(,)?) => { + () + }; + } + + macro_rules! info { + ($($msg:expr),+ $(,)?) => { + () + }; + } + + macro_rules! warn { + ($($msg:expr),+ $(,)?) => { + () + }; + } + + macro_rules! error { + ($($msg:expr),+ $(,)?) => { + () + }; + } +} + +#[cfg(not(feature = "defmt"))] +macro_rules! unwrap { + ($arg:expr) => { + match $crate::fmt::Try::into_result($arg) { + ::core::result::Result::Ok(t) => t, + ::core::result::Result::Err(e) => { + ::core::panic!("unwrap of `{}` failed: {:?}", ::core::stringify!($arg), e); + } + } + }; + ($arg:expr, $($msg:expr),+ $(,)? ) => { + match $crate::fmt::Try::into_result($arg) { + ::core::result::Result::Ok(t) => t, + ::core::result::Result::Err(e) => { + ::core::panic!("unwrap of `{}` failed: {}: {:?}", ::core::stringify!($arg), ::core::format_args!($($msg,)*), e); + } + } + } +} + +#[derive(Debug, Copy, Clone, Eq, PartialEq)] +pub struct NoneError; + +pub trait Try { + type Ok; + type Error; + fn into_result(self) -> Result; +} + +impl Try for Option { + type Ok = T; + type Error = NoneError; + + #[inline] + fn into_result(self) -> Result { + self.ok_or(NoneError) + } +} + +impl Try for Result { + type Ok = T; + type Error = E; + + #[inline] + fn into_result(self) -> Self { + self + } +} diff --git a/embassy-stm32/src/usart/v1.rs b/embassy-stm32/src/usart/v1.rs index 78a53b53e..5fbe14388 100644 --- a/embassy-stm32/src/usart/v1.rs +++ b/embassy-stm32/src/usart/v1.rs @@ -103,7 +103,11 @@ impl<'d, T: Instance> Uart<'d, T> { } #[cfg(feature = "_dma_v2")] - pub async fn write_dma(&mut self, ch: &mut impl crate::dma::Channel, buffer: &[u8]) -> Result<(), Error> { + pub async fn write_dma( + &mut self, + ch: &mut impl crate::dma::Channel, + buffer: &[u8], + ) -> Result<(), Error> { let ch_func = 4; // USART3_TX let r = self.inner.regs(); diff --git a/embassy-stm32/src/usart/v2.rs b/embassy-stm32/src/usart/v2.rs index e69de29bb..8b1378917 100644 --- a/embassy-stm32/src/usart/v2.rs +++ b/embassy-stm32/src/usart/v2.rs @@ -0,0 +1 @@ + diff --git a/embassy-traits/src/lib.rs b/embassy-traits/src/lib.rs index 047417870..3d84cf078 100644 --- a/embassy-traits/src/lib.rs +++ b/embassy-traits/src/lib.rs @@ -11,6 +11,6 @@ pub mod delay; pub mod flash; pub mod gpio; pub mod i2c; +pub mod rng; pub mod spi; pub mod uart; -pub mod rng;