diff options
| author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2021-12-16 07:30:03 +0000 |
|---|---|---|
| committer | GitHub <[email protected]> | 2021-12-16 07:30:03 +0000 |
| commit | d5a3064c2c20b4a9515e5322bb9a74724ebcf7c9 (patch) | |
| tree | ec1c6b4bcbb00458fdf38b1658535903d6e80eec /examples/stm32f3/src | |
| parent | 2d6111ed439a45a9001ab92ce0b2101836711317 (diff) | |
| parent | 1b3367e9a2732aebdd9db18caf2a3034fc26a21a (diff) | |
Merge #540
540: Initial support for STM32F3 r=Dirbaio a=VasanthakumarV
The [companion PR](https://github.com/embassy-rs/stm32-data/pull/109) in `stm32-data` should be merged before this PR.
The examples were tested on an STM32F303VC MCU.
Co-authored-by: VasanthakumarV <[email protected]>
Co-authored-by: Dario Nieuwenhuis <[email protected]>
Diffstat (limited to 'examples/stm32f3/src')
| -rw-r--r-- | examples/stm32f3/src/bin/blinky.rs | 30 | ||||
| -rw-r--r-- | examples/stm32f3/src/bin/button.rs | 33 | ||||
| -rw-r--r-- | examples/stm32f3/src/bin/button_exti.rs | 29 | ||||
| -rw-r--r-- | examples/stm32f3/src/bin/hello.rs | 28 | ||||
| -rw-r--r-- | examples/stm32f3/src/bin/spi_dma.rs | 41 | ||||
| -rw-r--r-- | examples/stm32f3/src/bin/usart_dma.rs | 30 | ||||
| -rw-r--r-- | examples/stm32f3/src/example_common.rs | 19 |
7 files changed, 210 insertions, 0 deletions
diff --git a/examples/stm32f3/src/bin/blinky.rs b/examples/stm32f3/src/bin/blinky.rs new file mode 100644 index 000000000..321643557 --- /dev/null +++ b/examples/stm32f3/src/bin/blinky.rs | |||
| @@ -0,0 +1,30 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | #![feature(type_alias_impl_trait)] | ||
| 4 | |||
| 5 | #[path = "../example_common.rs"] | ||
| 6 | mod example_common; | ||
| 7 | |||
| 8 | use embassy::executor::Spawner; | ||
| 9 | use embassy::time::{Duration, Timer}; | ||
| 10 | use embassy_stm32::gpio::{Level, Output, Speed}; | ||
| 11 | use embassy_stm32::Peripherals; | ||
| 12 | use embedded_hal::digital::v2::OutputPin; | ||
| 13 | use example_common::*; | ||
| 14 | |||
| 15 | #[embassy::main] | ||
| 16 | async fn main(_spawner: Spawner, p: Peripherals) { | ||
| 17 | info!("Hello World!"); | ||
| 18 | |||
| 19 | let mut led = Output::new(p.PE12, Level::High, Speed::Low); | ||
| 20 | |||
| 21 | loop { | ||
| 22 | info!("high"); | ||
| 23 | unwrap!(led.set_high()); | ||
| 24 | Timer::after(Duration::from_millis(1000)).await; | ||
| 25 | |||
| 26 | info!("low"); | ||
| 27 | unwrap!(led.set_low()); | ||
| 28 | Timer::after(Duration::from_millis(1000)).await; | ||
| 29 | } | ||
| 30 | } | ||
diff --git a/examples/stm32f3/src/bin/button.rs b/examples/stm32f3/src/bin/button.rs new file mode 100644 index 000000000..c5fab138b --- /dev/null +++ b/examples/stm32f3/src/bin/button.rs | |||
| @@ -0,0 +1,33 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | #![feature(type_alias_impl_trait)] | ||
| 4 | |||
| 5 | #[path = "../example_common.rs"] | ||
| 6 | mod example_common; | ||
| 7 | use cortex_m_rt::entry; | ||
| 8 | use embassy_stm32::gpio::{Input, Level, Output, Pull, Speed}; | ||
| 9 | use embedded_hal::digital::v2::{InputPin, OutputPin}; | ||
| 10 | use example_common::*; | ||
| 11 | |||
| 12 | #[entry] | ||
| 13 | fn main() -> ! { | ||
| 14 | info!("Hello World!"); | ||
| 15 | |||
| 16 | let p = embassy_stm32::init(Default::default()); | ||
| 17 | |||
| 18 | let button = Input::new(p.PA0, Pull::Down); | ||
| 19 | let mut led1 = Output::new(p.PE9, Level::High, Speed::Low); | ||
| 20 | let mut led2 = Output::new(p.PE15, Level::High, Speed::Low); | ||
| 21 | |||
| 22 | loop { | ||
| 23 | if unwrap!(button.is_high()) { | ||
| 24 | info!("high"); | ||
| 25 | unwrap!(led1.set_high()); | ||
| 26 | unwrap!(led2.set_low()); | ||
| 27 | } else { | ||
| 28 | info!("low"); | ||
| 29 | unwrap!(led1.set_low()); | ||
| 30 | unwrap!(led2.set_high()); | ||
| 31 | } | ||
| 32 | } | ||
| 33 | } | ||
diff --git a/examples/stm32f3/src/bin/button_exti.rs b/examples/stm32f3/src/bin/button_exti.rs new file mode 100644 index 000000000..d45e4365b --- /dev/null +++ b/examples/stm32f3/src/bin/button_exti.rs | |||
| @@ -0,0 +1,29 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | #![feature(type_alias_impl_trait)] | ||
| 4 | |||
| 5 | #[path = "../example_common.rs"] | ||
| 6 | mod example_common; | ||
| 7 | use embassy::executor::Spawner; | ||
| 8 | use embassy_stm32::exti::ExtiInput; | ||
| 9 | use embassy_stm32::gpio::{Input, Pull}; | ||
| 10 | use embassy_stm32::Peripherals; | ||
| 11 | use embassy_traits::gpio::{WaitForFallingEdge, WaitForRisingEdge}; | ||
| 12 | use example_common::*; | ||
| 13 | |||
| 14 | #[embassy::main] | ||
| 15 | async fn main(_spawner: Spawner, p: Peripherals) { | ||
| 16 | info!("Hello World!"); | ||
| 17 | |||
| 18 | let button = Input::new(p.PA0, Pull::Down); | ||
| 19 | let mut button = ExtiInput::new(button, p.EXTI0); | ||
| 20 | |||
| 21 | info!("Press the USER button..."); | ||
| 22 | |||
| 23 | loop { | ||
| 24 | button.wait_for_rising_edge().await; | ||
| 25 | info!("Pressed!"); | ||
| 26 | button.wait_for_falling_edge().await; | ||
| 27 | info!("Released!"); | ||
| 28 | } | ||
| 29 | } | ||
diff --git a/examples/stm32f3/src/bin/hello.rs b/examples/stm32f3/src/bin/hello.rs new file mode 100644 index 000000000..9a67419fb --- /dev/null +++ b/examples/stm32f3/src/bin/hello.rs | |||
| @@ -0,0 +1,28 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | #![feature(type_alias_impl_trait)] | ||
| 4 | |||
| 5 | use defmt::info; | ||
| 6 | use embassy::executor::Spawner; | ||
| 7 | use embassy::time::{Duration, Timer}; | ||
| 8 | use embassy_stm32::time::Hertz; | ||
| 9 | use embassy_stm32::Config; | ||
| 10 | use embassy_stm32::Peripherals; | ||
| 11 | |||
| 12 | #[path = "../example_common.rs"] | ||
| 13 | mod example_common; | ||
| 14 | |||
| 15 | fn config() -> Config { | ||
| 16 | let mut config = Config::default(); | ||
| 17 | config.rcc.hse = Some(Hertz(8_000_000)); | ||
| 18 | config.rcc.sysclk = Some(Hertz(16_000_000)); | ||
| 19 | config | ||
| 20 | } | ||
| 21 | |||
| 22 | #[embassy::main(config = "config()")] | ||
| 23 | async fn main(_spawner: Spawner, _p: Peripherals) -> ! { | ||
| 24 | loop { | ||
| 25 | info!("Hello World!"); | ||
| 26 | Timer::after(Duration::from_secs(1)).await; | ||
| 27 | } | ||
| 28 | } | ||
diff --git a/examples/stm32f3/src/bin/spi_dma.rs b/examples/stm32f3/src/bin/spi_dma.rs new file mode 100644 index 000000000..a87a36f73 --- /dev/null +++ b/examples/stm32f3/src/bin/spi_dma.rs | |||
| @@ -0,0 +1,41 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | #![feature(type_alias_impl_trait)] | ||
| 4 | |||
| 5 | #[path = "../example_common.rs"] | ||
| 6 | mod example_common; | ||
| 7 | use core::fmt::Write; | ||
| 8 | use core::str::from_utf8; | ||
| 9 | use embassy::executor::Spawner; | ||
| 10 | use embassy_stm32::spi::{Config, Spi}; | ||
| 11 | use embassy_stm32::time::Hertz; | ||
| 12 | use embassy_stm32::Peripherals; | ||
| 13 | use embassy_traits::spi::FullDuplex; | ||
| 14 | use example_common::*; | ||
| 15 | use heapless::String; | ||
| 16 | |||
| 17 | #[embassy::main] | ||
| 18 | async fn main(_spawner: Spawner, p: Peripherals) { | ||
| 19 | info!("Hello World!"); | ||
| 20 | |||
| 21 | let mut spi = Spi::new( | ||
| 22 | p.SPI1, | ||
| 23 | p.PB3, | ||
| 24 | p.PB5, | ||
| 25 | p.PB4, | ||
| 26 | p.DMA1_CH3, | ||
| 27 | p.DMA1_CH2, | ||
| 28 | Hertz(1_000_000), | ||
| 29 | Config::default(), | ||
| 30 | ); | ||
| 31 | |||
| 32 | for n in 0u32.. { | ||
| 33 | let mut write: String<128> = String::new(); | ||
| 34 | let mut read = [0; 128]; | ||
| 35 | core::write!(&mut write, "Hello DMA World {}!\r\n", n).unwrap(); | ||
| 36 | spi.read_write(&mut read[0..write.len()], write.as_bytes()) | ||
| 37 | .await | ||
| 38 | .ok(); | ||
| 39 | info!("read via spi+dma: {}", from_utf8(&read).unwrap()); | ||
| 40 | } | ||
| 41 | } | ||
diff --git a/examples/stm32f3/src/bin/usart_dma.rs b/examples/stm32f3/src/bin/usart_dma.rs new file mode 100644 index 000000000..99530b5c0 --- /dev/null +++ b/examples/stm32f3/src/bin/usart_dma.rs | |||
| @@ -0,0 +1,30 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | #![feature(type_alias_impl_trait)] | ||
| 4 | |||
| 5 | #[path = "../example_common.rs"] | ||
| 6 | mod example_common; | ||
| 7 | use core::fmt::Write; | ||
| 8 | use embassy::executor::Spawner; | ||
| 9 | use embassy_stm32::dma::NoDma; | ||
| 10 | use embassy_stm32::usart::{Config, Uart}; | ||
| 11 | use embassy_stm32::Peripherals; | ||
| 12 | use embassy_traits::uart::Write as _; | ||
| 13 | use example_common::*; | ||
| 14 | use heapless::String; | ||
| 15 | |||
| 16 | #[embassy::main] | ||
| 17 | async fn main(_spawner: Spawner, p: Peripherals) { | ||
| 18 | info!("Hello World!"); | ||
| 19 | |||
| 20 | let config = Config::default(); | ||
| 21 | let mut usart = Uart::new(p.USART1, p.PE1, p.PE0, p.DMA1_CH4, NoDma, config); | ||
| 22 | |||
| 23 | for n in 0u32.. { | ||
| 24 | let mut s: String<128> = String::new(); | ||
| 25 | core::write!(&mut s, "Hello DMA World {}!\r\n", n).unwrap(); | ||
| 26 | |||
| 27 | unwrap!(usart.write(s.as_bytes()).await); | ||
| 28 | info!("wrote DMA"); | ||
| 29 | } | ||
| 30 | } | ||
diff --git a/examples/stm32f3/src/example_common.rs b/examples/stm32f3/src/example_common.rs new file mode 100644 index 000000000..e14517033 --- /dev/null +++ b/examples/stm32f3/src/example_common.rs | |||
| @@ -0,0 +1,19 @@ | |||
| 1 | #![macro_use] | ||
| 2 | |||
| 3 | use defmt_rtt as _; // global logger | ||
| 4 | use panic_probe as _; | ||
| 5 | |||
| 6 | pub use defmt::*; | ||
| 7 | |||
| 8 | use core::sync::atomic::{AtomicUsize, Ordering}; | ||
| 9 | |||
| 10 | defmt::timestamp! { | ||
| 11 | "{=u64}", | ||
| 12 | { | ||
| 13 | static COUNT: AtomicUsize = AtomicUsize::new(0); | ||
| 14 | // NOTE(no-CAS) `timestamps` runs with interrupts disabled | ||
| 15 | let n = COUNT.load(Ordering::Relaxed); | ||
| 16 | COUNT.store(n + 1, Ordering::Relaxed); | ||
| 17 | n as u64 | ||
| 18 | } | ||
| 19 | } | ||
