diff options
Diffstat (limited to 'examples/stm32f4/src/bin')
| -rw-r--r-- | examples/stm32f4/src/bin/blinky.rs | 54 | ||||
| -rw-r--r-- | examples/stm32f4/src/bin/button.rs | 59 | ||||
| -rw-r--r-- | examples/stm32f4/src/bin/button_exti.rs | 83 | ||||
| -rw-r--r-- | examples/stm32f4/src/bin/spi.rs | 71 | ||||
| -rw-r--r-- | examples/stm32f4/src/bin/usart.rs | 86 | ||||
| -rw-r--r-- | examples/stm32f4/src/bin/usart_dma.rs | 90 |
6 files changed, 443 insertions, 0 deletions
diff --git a/examples/stm32f4/src/bin/blinky.rs b/examples/stm32f4/src/bin/blinky.rs new file mode 100644 index 000000000..7590764d8 --- /dev/null +++ b/examples/stm32f4/src/bin/blinky.rs | |||
| @@ -0,0 +1,54 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | #![feature(trait_alias)] | ||
| 4 | #![feature(min_type_alias_impl_trait)] | ||
| 5 | #![feature(impl_trait_in_bindings)] | ||
| 6 | #![feature(type_alias_impl_trait)] | ||
| 7 | #![allow(incomplete_features)] | ||
| 8 | |||
| 9 | #[path = "../example_common.rs"] | ||
| 10 | mod example_common; | ||
| 11 | use embassy_stm32::gpio::{Level, Output}; | ||
| 12 | use embedded_hal::digital::v2::OutputPin; | ||
| 13 | use example_common::*; | ||
| 14 | |||
| 15 | use cortex_m_rt::entry; | ||
| 16 | use stm32f4::stm32f429 as pac; | ||
| 17 | |||
| 18 | #[entry] | ||
| 19 | fn main() -> ! { | ||
| 20 | info!("Hello World!"); | ||
| 21 | |||
| 22 | let pp = pac::Peripherals::take().unwrap(); | ||
| 23 | |||
| 24 | pp.DBGMCU.cr.modify(|_, w| { | ||
| 25 | w.dbg_sleep().set_bit(); | ||
| 26 | w.dbg_standby().set_bit(); | ||
| 27 | w.dbg_stop().set_bit() | ||
| 28 | }); | ||
| 29 | pp.RCC.ahb1enr.modify(|_, w| w.dma1en().enabled()); | ||
| 30 | |||
| 31 | pp.RCC.ahb1enr.modify(|_, w| { | ||
| 32 | w.gpioaen().enabled(); | ||
| 33 | w.gpioben().enabled(); | ||
| 34 | w.gpiocen().enabled(); | ||
| 35 | w.gpioden().enabled(); | ||
| 36 | w.gpioeen().enabled(); | ||
| 37 | w.gpiofen().enabled(); | ||
| 38 | w | ||
| 39 | }); | ||
| 40 | |||
| 41 | let p = embassy_stm32::init(Default::default()); | ||
| 42 | |||
| 43 | let mut led = Output::new(p.PB7, Level::High); | ||
| 44 | |||
| 45 | loop { | ||
| 46 | info!("high"); | ||
| 47 | led.set_high().unwrap(); | ||
| 48 | cortex_m::asm::delay(10_000_000); | ||
| 49 | |||
| 50 | info!("low"); | ||
| 51 | led.set_low().unwrap(); | ||
| 52 | cortex_m::asm::delay(10_000_000); | ||
| 53 | } | ||
| 54 | } | ||
diff --git a/examples/stm32f4/src/bin/button.rs b/examples/stm32f4/src/bin/button.rs new file mode 100644 index 000000000..1ee99f527 --- /dev/null +++ b/examples/stm32f4/src/bin/button.rs | |||
| @@ -0,0 +1,59 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | #![feature(trait_alias)] | ||
| 4 | #![feature(min_type_alias_impl_trait)] | ||
| 5 | #![feature(impl_trait_in_bindings)] | ||
| 6 | #![feature(type_alias_impl_trait)] | ||
| 7 | #![allow(incomplete_features)] | ||
| 8 | |||
| 9 | #[path = "../example_common.rs"] | ||
| 10 | mod example_common; | ||
| 11 | use embassy_stm32::gpio::{Input, Level, Output, Pull}; | ||
| 12 | use embedded_hal::digital::v2::{InputPin, OutputPin}; | ||
| 13 | use example_common::*; | ||
| 14 | |||
| 15 | use cortex_m_rt::entry; | ||
| 16 | use stm32f4::stm32f429 as pac; | ||
| 17 | |||
| 18 | #[entry] | ||
| 19 | fn main() -> ! { | ||
| 20 | info!("Hello World!"); | ||
| 21 | |||
| 22 | let pp = pac::Peripherals::take().unwrap(); | ||
| 23 | |||
| 24 | pp.DBGMCU.cr.modify(|_, w| { | ||
| 25 | w.dbg_sleep().set_bit(); | ||
| 26 | w.dbg_standby().set_bit(); | ||
| 27 | w.dbg_stop().set_bit() | ||
| 28 | }); | ||
| 29 | pp.RCC.ahb1enr.modify(|_, w| w.dma1en().enabled()); | ||
| 30 | |||
| 31 | pp.RCC.ahb1enr.modify(|_, w| { | ||
| 32 | w.gpioaen().enabled(); | ||
| 33 | w.gpioben().enabled(); | ||
| 34 | w.gpiocen().enabled(); | ||
| 35 | w.gpioden().enabled(); | ||
| 36 | w.gpioeen().enabled(); | ||
| 37 | w.gpiofen().enabled(); | ||
| 38 | w | ||
| 39 | }); | ||
| 40 | |||
| 41 | let p = embassy_stm32::init(Default::default()); | ||
| 42 | |||
| 43 | let button = Input::new(p.PC13, Pull::Down); | ||
| 44 | let mut led1 = Output::new(p.PB0, Level::High); | ||
| 45 | let _led2 = Output::new(p.PB7, Level::High); | ||
| 46 | let mut led3 = Output::new(p.PB14, Level::High); | ||
| 47 | |||
| 48 | loop { | ||
| 49 | if button.is_high().unwrap() { | ||
| 50 | info!("high"); | ||
| 51 | led1.set_high().unwrap(); | ||
| 52 | led3.set_low().unwrap(); | ||
| 53 | } else { | ||
| 54 | info!("low"); | ||
| 55 | led1.set_low().unwrap(); | ||
| 56 | led3.set_high().unwrap(); | ||
| 57 | } | ||
| 58 | } | ||
| 59 | } | ||
diff --git a/examples/stm32f4/src/bin/button_exti.rs b/examples/stm32f4/src/bin/button_exti.rs new file mode 100644 index 000000000..8fc889dad --- /dev/null +++ b/examples/stm32f4/src/bin/button_exti.rs | |||
| @@ -0,0 +1,83 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | #![feature(trait_alias)] | ||
| 4 | #![feature(min_type_alias_impl_trait)] | ||
| 5 | #![feature(impl_trait_in_bindings)] | ||
| 6 | #![feature(type_alias_impl_trait)] | ||
| 7 | #![allow(incomplete_features)] | ||
| 8 | |||
| 9 | #[path = "../example_common.rs"] | ||
| 10 | mod example_common; | ||
| 11 | use embassy::executor::Executor; | ||
| 12 | use embassy::time::Clock; | ||
| 13 | use embassy::util::Forever; | ||
| 14 | use embassy_stm32::exti::ExtiInput; | ||
| 15 | use embassy_stm32::gpio::{Input, Pull}; | ||
| 16 | use embassy_traits::gpio::{WaitForFallingEdge, WaitForRisingEdge}; | ||
| 17 | use example_common::*; | ||
| 18 | |||
| 19 | use cortex_m_rt::entry; | ||
| 20 | use stm32f4::stm32f429 as pac; | ||
| 21 | |||
| 22 | #[embassy::task] | ||
| 23 | async fn main_task() { | ||
| 24 | let p = embassy_stm32::init(Default::default()); | ||
| 25 | |||
| 26 | let button = Input::new(p.PC13, Pull::Down); | ||
| 27 | let mut button = ExtiInput::new(button, p.EXTI13); | ||
| 28 | |||
| 29 | info!("Press the USER button..."); | ||
| 30 | |||
| 31 | loop { | ||
| 32 | button.wait_for_rising_edge().await; | ||
| 33 | info!("Pressed!"); | ||
| 34 | button.wait_for_falling_edge().await; | ||
| 35 | info!("Released!"); | ||
| 36 | } | ||
| 37 | } | ||
| 38 | |||
| 39 | struct ZeroClock; | ||
| 40 | |||
| 41 | impl Clock for ZeroClock { | ||
| 42 | fn now(&self) -> u64 { | ||
| 43 | 0 | ||
| 44 | } | ||
| 45 | } | ||
| 46 | |||
| 47 | static EXECUTOR: Forever<Executor> = Forever::new(); | ||
| 48 | |||
| 49 | #[entry] | ||
| 50 | fn main() -> ! { | ||
| 51 | info!("Hello World!"); | ||
| 52 | |||
| 53 | let pp = pac::Peripherals::take().unwrap(); | ||
| 54 | |||
| 55 | pp.DBGMCU.cr.modify(|_, w| { | ||
| 56 | w.dbg_sleep().set_bit(); | ||
| 57 | w.dbg_standby().set_bit(); | ||
| 58 | w.dbg_stop().set_bit() | ||
| 59 | }); | ||
| 60 | pp.RCC.ahb1enr.modify(|_, w| w.dma1en().enabled()); | ||
| 61 | |||
| 62 | pp.RCC.ahb1enr.modify(|_, w| { | ||
| 63 | w.gpioaen().enabled(); | ||
| 64 | w.gpioben().enabled(); | ||
| 65 | w.gpiocen().enabled(); | ||
| 66 | w.gpioden().enabled(); | ||
| 67 | w.gpioeen().enabled(); | ||
| 68 | w.gpiofen().enabled(); | ||
| 69 | w | ||
| 70 | }); | ||
| 71 | pp.RCC.apb2enr.modify(|_, w| { | ||
| 72 | w.syscfgen().enabled(); | ||
| 73 | w | ||
| 74 | }); | ||
| 75 | |||
| 76 | unsafe { embassy::time::set_clock(&ZeroClock) }; | ||
| 77 | |||
| 78 | let executor = EXECUTOR.put(Executor::new()); | ||
| 79 | |||
| 80 | executor.run(|spawner| { | ||
| 81 | unwrap!(spawner.spawn(main_task())); | ||
| 82 | }) | ||
| 83 | } | ||
diff --git a/examples/stm32f4/src/bin/spi.rs b/examples/stm32f4/src/bin/spi.rs new file mode 100644 index 000000000..af0d57412 --- /dev/null +++ b/examples/stm32f4/src/bin/spi.rs | |||
| @@ -0,0 +1,71 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | #![feature(trait_alias)] | ||
| 4 | #![feature(min_type_alias_impl_trait)] | ||
| 5 | #![feature(impl_trait_in_bindings)] | ||
| 6 | #![feature(type_alias_impl_trait)] | ||
| 7 | #![allow(incomplete_features)] | ||
| 8 | |||
| 9 | #[path = "../example_common.rs"] | ||
| 10 | mod example_common; | ||
| 11 | |||
| 12 | use embassy_stm32::gpio::{Level, Output}; | ||
| 13 | use embedded_hal::digital::v2::OutputPin; | ||
| 14 | use example_common::*; | ||
| 15 | |||
| 16 | use cortex_m_rt::entry; | ||
| 17 | use embassy_stm32::spi::{Config, Spi}; | ||
| 18 | use embassy_stm32::time::Hertz; | ||
| 19 | use embedded_hal::blocking::spi::Transfer; | ||
| 20 | use stm32f4::stm32f429 as pac; | ||
| 21 | |||
| 22 | #[entry] | ||
| 23 | fn main() -> ! { | ||
| 24 | info!("Hello World, dude!"); | ||
| 25 | |||
| 26 | let pp = pac::Peripherals::take().unwrap(); | ||
| 27 | |||
| 28 | pp.DBGMCU.cr.modify(|_, w| { | ||
| 29 | w.dbg_sleep().set_bit(); | ||
| 30 | w.dbg_standby().set_bit(); | ||
| 31 | w.dbg_stop().set_bit() | ||
| 32 | }); | ||
| 33 | pp.RCC.ahb1enr.modify(|_, w| w.dma1en().set_bit()); | ||
| 34 | |||
| 35 | pp.RCC.apb1enr.modify(|_, w| { | ||
| 36 | w.spi3en().enabled(); | ||
| 37 | w | ||
| 38 | }); | ||
| 39 | |||
| 40 | pp.RCC.ahb1enr.modify(|_, w| { | ||
| 41 | w.gpioaen().enabled(); | ||
| 42 | w.gpioben().enabled(); | ||
| 43 | w.gpiocen().enabled(); | ||
| 44 | w.gpioden().enabled(); | ||
| 45 | w.gpioeen().enabled(); | ||
| 46 | w.gpiofen().enabled(); | ||
| 47 | w | ||
| 48 | }); | ||
| 49 | |||
| 50 | let p = embassy_stm32::init(Default::default()); | ||
| 51 | |||
| 52 | let mut spi = Spi::new( | ||
| 53 | Hertz(16_000_000), | ||
| 54 | p.SPI3, | ||
| 55 | p.PC10, | ||
| 56 | p.PC12, | ||
| 57 | p.PC11, | ||
| 58 | Hertz(1_000_000), | ||
| 59 | Config::default(), | ||
| 60 | ); | ||
| 61 | |||
| 62 | let mut cs = Output::new(p.PE0, Level::High); | ||
| 63 | |||
| 64 | loop { | ||
| 65 | let mut buf = [0x0A; 4]; | ||
| 66 | unwrap!(cs.set_low()); | ||
| 67 | unwrap!(spi.transfer(&mut buf)); | ||
| 68 | unwrap!(cs.set_high()); | ||
| 69 | info!("xfer {=[u8]:x}", buf); | ||
| 70 | } | ||
| 71 | } | ||
diff --git a/examples/stm32f4/src/bin/usart.rs b/examples/stm32f4/src/bin/usart.rs new file mode 100644 index 000000000..f7b66f86b --- /dev/null +++ b/examples/stm32f4/src/bin/usart.rs | |||
| @@ -0,0 +1,86 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | #![feature(trait_alias)] | ||
| 4 | #![feature(min_type_alias_impl_trait)] | ||
| 5 | #![feature(impl_trait_in_bindings)] | ||
| 6 | #![feature(type_alias_impl_trait)] | ||
| 7 | #![allow(incomplete_features)] | ||
| 8 | |||
| 9 | #[path = "../example_common.rs"] | ||
| 10 | mod example_common; | ||
| 11 | use cortex_m::prelude::_embedded_hal_blocking_serial_Write; | ||
| 12 | use embassy::executor::Executor; | ||
| 13 | use embassy::time::Clock; | ||
| 14 | use embassy::util::Forever; | ||
| 15 | use embassy_stm32::usart::{Config, Uart}; | ||
| 16 | use example_common::*; | ||
| 17 | |||
| 18 | use cortex_m_rt::entry; | ||
| 19 | use stm32f4::stm32f429 as pac; | ||
| 20 | |||
| 21 | #[embassy::task] | ||
| 22 | async fn main_task() { | ||
| 23 | let p = embassy_stm32::init(Default::default()); | ||
| 24 | |||
| 25 | let config = Config::default(); | ||
| 26 | let mut usart = Uart::new(p.USART3, p.PD9, p.PD8, config, 16_000_000); | ||
| 27 | |||
| 28 | usart.bwrite_all(b"Hello Embassy World!\r\n").unwrap(); | ||
| 29 | info!("wrote Hello, starting echo"); | ||
| 30 | |||
| 31 | let mut buf = [0u8; 1]; | ||
| 32 | loop { | ||
| 33 | usart.read(&mut buf).unwrap(); | ||
| 34 | usart.bwrite_all(&buf).unwrap(); | ||
| 35 | } | ||
| 36 | } | ||
| 37 | |||
| 38 | struct ZeroClock; | ||
| 39 | |||
| 40 | impl Clock for ZeroClock { | ||
| 41 | fn now(&self) -> u64 { | ||
| 42 | 0 | ||
| 43 | } | ||
| 44 | } | ||
| 45 | |||
| 46 | static EXECUTOR: Forever<Executor> = Forever::new(); | ||
| 47 | |||
| 48 | #[entry] | ||
| 49 | fn main() -> ! { | ||
| 50 | info!("Hello World!"); | ||
| 51 | |||
| 52 | let pp = pac::Peripherals::take().unwrap(); | ||
| 53 | |||
| 54 | pp.DBGMCU.cr.modify(|_, w| { | ||
| 55 | w.dbg_sleep().set_bit(); | ||
| 56 | w.dbg_standby().set_bit(); | ||
| 57 | w.dbg_stop().set_bit() | ||
| 58 | }); | ||
| 59 | pp.RCC.ahb1enr.modify(|_, w| w.dma1en().enabled()); | ||
| 60 | |||
| 61 | pp.RCC.ahb1enr.modify(|_, w| { | ||
| 62 | w.gpioaen().enabled(); | ||
| 63 | w.gpioben().enabled(); | ||
| 64 | w.gpiocen().enabled(); | ||
| 65 | w.gpioden().enabled(); | ||
| 66 | w.gpioeen().enabled(); | ||
| 67 | w.gpiofen().enabled(); | ||
| 68 | w | ||
| 69 | }); | ||
| 70 | pp.RCC.apb2enr.modify(|_, w| { | ||
| 71 | w.syscfgen().enabled(); | ||
| 72 | w | ||
| 73 | }); | ||
| 74 | pp.RCC.apb1enr.modify(|_, w| { | ||
| 75 | w.usart3en().enabled(); | ||
| 76 | w | ||
| 77 | }); | ||
| 78 | |||
| 79 | unsafe { embassy::time::set_clock(&ZeroClock) }; | ||
| 80 | |||
| 81 | let executor = EXECUTOR.put(Executor::new()); | ||
| 82 | |||
| 83 | executor.run(|spawner| { | ||
| 84 | unwrap!(spawner.spawn(main_task())); | ||
| 85 | }) | ||
| 86 | } | ||
diff --git a/examples/stm32f4/src/bin/usart_dma.rs b/examples/stm32f4/src/bin/usart_dma.rs new file mode 100644 index 000000000..fae05b607 --- /dev/null +++ b/examples/stm32f4/src/bin/usart_dma.rs | |||
| @@ -0,0 +1,90 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | #![feature(trait_alias)] | ||
| 4 | #![feature(min_type_alias_impl_trait)] | ||
| 5 | #![feature(impl_trait_in_bindings)] | ||
| 6 | #![feature(type_alias_impl_trait)] | ||
| 7 | #![allow(incomplete_features)] | ||
| 8 | |||
| 9 | #[path = "../example_common.rs"] | ||
| 10 | mod example_common; | ||
| 11 | use core::fmt::Write; | ||
| 12 | use cortex_m_rt::entry; | ||
| 13 | use embassy::executor::Executor; | ||
| 14 | use embassy::time::Clock; | ||
| 15 | use embassy::util::Forever; | ||
| 16 | use embassy_stm32::usart::{Config, Uart}; | ||
| 17 | use example_common::*; | ||
| 18 | use heapless::String; | ||
| 19 | use stm32f4::stm32f429 as pac; | ||
| 20 | |||
| 21 | #[embassy::task] | ||
| 22 | async fn main_task() { | ||
| 23 | let mut p = embassy_stm32::init(Default::default()); | ||
| 24 | |||
| 25 | let config = Config::default(); | ||
| 26 | let mut usart = Uart::new(p.USART3, p.PD9, p.PD8, config, 16_000_000); | ||
| 27 | |||
| 28 | for n in 0u32.. { | ||
| 29 | let mut s: String<128> = String::new(); | ||
| 30 | core::write!(&mut s, "Hello DMA World {}!\r\n", n).unwrap(); | ||
| 31 | |||
| 32 | usart | ||
| 33 | .write_dma(&mut p.DMA1_CH3, s.as_bytes()) | ||
| 34 | .await | ||
| 35 | .unwrap(); | ||
| 36 | info!("wrote DMA"); | ||
| 37 | } | ||
| 38 | } | ||
| 39 | |||
| 40 | struct ZeroClock; | ||
| 41 | |||
| 42 | impl Clock for ZeroClock { | ||
| 43 | fn now(&self) -> u64 { | ||
| 44 | 0 | ||
| 45 | } | ||
| 46 | } | ||
| 47 | |||
| 48 | static EXECUTOR: Forever<Executor> = Forever::new(); | ||
| 49 | |||
| 50 | #[entry] | ||
| 51 | fn main() -> ! { | ||
| 52 | info!("Hello World!"); | ||
| 53 | |||
| 54 | let pp = pac::Peripherals::take().unwrap(); | ||
| 55 | |||
| 56 | pp.DBGMCU.cr.modify(|_, w| { | ||
| 57 | w.dbg_sleep().set_bit(); | ||
| 58 | w.dbg_standby().set_bit(); | ||
| 59 | w.dbg_stop().set_bit() | ||
| 60 | }); | ||
| 61 | pp.RCC.ahb1enr.modify(|_, w| w.dma1en().enabled()); | ||
| 62 | |||
| 63 | pp.RCC.ahb1enr.modify(|_, w| { | ||
| 64 | w.gpioaen().enabled(); | ||
| 65 | w.gpioben().enabled(); | ||
| 66 | w.gpiocen().enabled(); | ||
| 67 | w.gpioden().enabled(); | ||
| 68 | w.gpioeen().enabled(); | ||
| 69 | w.gpiofen().enabled(); | ||
| 70 | w.dma1en().enabled(); | ||
| 71 | w.dma2en().enabled(); | ||
| 72 | w | ||
| 73 | }); | ||
| 74 | pp.RCC.apb2enr.modify(|_, w| { | ||
| 75 | w.syscfgen().enabled(); | ||
| 76 | w | ||
| 77 | }); | ||
| 78 | pp.RCC.apb1enr.modify(|_, w| { | ||
| 79 | w.usart3en().enabled(); | ||
| 80 | w | ||
| 81 | }); | ||
| 82 | |||
| 83 | unsafe { embassy::time::set_clock(&ZeroClock) }; | ||
| 84 | |||
| 85 | let executor = EXECUTOR.put(Executor::new()); | ||
| 86 | |||
| 87 | executor.run(|spawner| { | ||
| 88 | unwrap!(spawner.spawn(main_task())); | ||
| 89 | }) | ||
| 90 | } | ||
