diff options
Diffstat (limited to 'docs/examples/layer-by-layer')
| -rw-r--r-- | docs/examples/layer-by-layer/.cargo/config.toml | 14 | ||||
| -rw-r--r-- | docs/examples/layer-by-layer/Cargo.toml | 21 | ||||
| -rw-r--r-- | docs/examples/layer-by-layer/blinky-async/Cargo.toml | 15 | ||||
| -rw-r--r-- | docs/examples/layer-by-layer/blinky-async/src/main.rs | 23 | ||||
| -rw-r--r-- | docs/examples/layer-by-layer/blinky-hal/Cargo.toml | 14 | ||||
| -rw-r--r-- | docs/examples/layer-by-layer/blinky-hal/src/main.rs | 21 | ||||
| -rw-r--r-- | docs/examples/layer-by-layer/blinky-irq/Cargo.toml | 14 | ||||
| -rw-r--r-- | docs/examples/layer-by-layer/blinky-irq/src/main.rs | 93 | ||||
| -rw-r--r-- | docs/examples/layer-by-layer/blinky-pac/Cargo.toml | 14 | ||||
| -rw-r--r-- | docs/examples/layer-by-layer/blinky-pac/src/main.rs | 53 |
10 files changed, 282 insertions, 0 deletions
diff --git a/docs/examples/layer-by-layer/.cargo/config.toml b/docs/examples/layer-by-layer/.cargo/config.toml new file mode 100644 index 000000000..3012f05dc --- /dev/null +++ b/docs/examples/layer-by-layer/.cargo/config.toml | |||
| @@ -0,0 +1,14 @@ | |||
| 1 | [target.'cfg(all(target_arch = "arm", target_os = "none"))'] | ||
| 2 | runner = "probe-run --chip STM32L475VG" | ||
| 3 | |||
| 4 | rustflags = [ | ||
| 5 | "-C", "link-arg=--nmagic", | ||
| 6 | "-C", "link-arg=-Tlink.x", | ||
| 7 | "-C", "link-arg=-Tdefmt.x", | ||
| 8 | ] | ||
| 9 | |||
| 10 | [build] | ||
| 11 | target = "thumbv7em-none-eabihf" | ||
| 12 | |||
| 13 | [env] | ||
| 14 | DEFMT_LOG = "trace" | ||
diff --git a/docs/examples/layer-by-layer/Cargo.toml b/docs/examples/layer-by-layer/Cargo.toml new file mode 100644 index 000000000..0f233eae5 --- /dev/null +++ b/docs/examples/layer-by-layer/Cargo.toml | |||
| @@ -0,0 +1,21 @@ | |||
| 1 | [workspace] | ||
| 2 | resolver = "2" | ||
| 3 | members = [ | ||
| 4 | "blinky-pac", | ||
| 5 | "blinky-hal", | ||
| 6 | "blinky-irq", | ||
| 7 | "blinky-async", | ||
| 8 | ] | ||
| 9 | |||
| 10 | [patch.crates-io] | ||
| 11 | embassy-executor = { path = "../../../embassy-executor" } | ||
| 12 | embassy-stm32 = { path = "../../../embassy-stm32" } | ||
| 13 | |||
| 14 | [profile.release] | ||
| 15 | codegen-units = 1 | ||
| 16 | debug = 2 | ||
| 17 | debug-assertions = false | ||
| 18 | incremental = false | ||
| 19 | lto = "fat" | ||
| 20 | opt-level = 's' | ||
| 21 | overflow-checks = false | ||
diff --git a/docs/examples/layer-by-layer/blinky-async/Cargo.toml b/docs/examples/layer-by-layer/blinky-async/Cargo.toml new file mode 100644 index 000000000..64f7e8403 --- /dev/null +++ b/docs/examples/layer-by-layer/blinky-async/Cargo.toml | |||
| @@ -0,0 +1,15 @@ | |||
| 1 | [package] | ||
| 2 | name = "blinky-async" | ||
| 3 | version = "0.1.0" | ||
| 4 | edition = "2021" | ||
| 5 | license = "MIT OR Apache-2.0" | ||
| 6 | |||
| 7 | [dependencies] | ||
| 8 | cortex-m = "0.7" | ||
| 9 | cortex-m-rt = "0.7" | ||
| 10 | embassy-stm32 = { version = "0.1.0", features = ["stm32l475vg", "memory-x", "exti"] } | ||
| 11 | embassy-executor = { version = "0.5.0", features = ["arch-cortex-m", "executor-thread"] } | ||
| 12 | |||
| 13 | defmt = "0.3.0" | ||
| 14 | defmt-rtt = "0.3.0" | ||
| 15 | panic-probe = { version = "0.3.0", features = ["print-defmt"] } | ||
diff --git a/docs/examples/layer-by-layer/blinky-async/src/main.rs b/docs/examples/layer-by-layer/blinky-async/src/main.rs new file mode 100644 index 000000000..004602816 --- /dev/null +++ b/docs/examples/layer-by-layer/blinky-async/src/main.rs | |||
| @@ -0,0 +1,23 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | |||
| 4 | use embassy_executor::Spawner; | ||
| 5 | use embassy_stm32::exti::ExtiInput; | ||
| 6 | use embassy_stm32::gpio::{Level, Output, Pull, Speed}; | ||
| 7 | use {defmt_rtt as _, panic_probe as _}; | ||
| 8 | |||
| 9 | #[embassy_executor::main] | ||
| 10 | async fn main(_spawner: Spawner) { | ||
| 11 | let p = embassy_stm32::init(Default::default()); | ||
| 12 | let mut led = Output::new(p.PB14, Level::Low, Speed::VeryHigh); | ||
| 13 | let mut button = ExtiInput::new(p.PC13, p.EXTI13, Pull::Up); | ||
| 14 | |||
| 15 | loop { | ||
| 16 | button.wait_for_any_edge().await; | ||
| 17 | if button.is_low() { | ||
| 18 | led.set_high(); | ||
| 19 | } else { | ||
| 20 | led.set_low(); | ||
| 21 | } | ||
| 22 | } | ||
| 23 | } | ||
diff --git a/docs/examples/layer-by-layer/blinky-hal/Cargo.toml b/docs/examples/layer-by-layer/blinky-hal/Cargo.toml new file mode 100644 index 000000000..c15de2db2 --- /dev/null +++ b/docs/examples/layer-by-layer/blinky-hal/Cargo.toml | |||
| @@ -0,0 +1,14 @@ | |||
| 1 | [package] | ||
| 2 | name = "blinky-hal" | ||
| 3 | version = "0.1.0" | ||
| 4 | edition = "2021" | ||
| 5 | license = "MIT OR Apache-2.0" | ||
| 6 | |||
| 7 | [dependencies] | ||
| 8 | cortex-m = "0.7" | ||
| 9 | cortex-m-rt = "0.7" | ||
| 10 | embassy-stm32 = { version = "0.1.0", features = ["stm32l475vg", "memory-x"] } | ||
| 11 | |||
| 12 | defmt = "0.3.0" | ||
| 13 | defmt-rtt = "0.3.0" | ||
| 14 | panic-probe = { version = "0.3.0", features = ["print-defmt"] } | ||
diff --git a/docs/examples/layer-by-layer/blinky-hal/src/main.rs b/docs/examples/layer-by-layer/blinky-hal/src/main.rs new file mode 100644 index 000000000..d0c9f4907 --- /dev/null +++ b/docs/examples/layer-by-layer/blinky-hal/src/main.rs | |||
| @@ -0,0 +1,21 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | |||
| 4 | use cortex_m_rt::entry; | ||
| 5 | use embassy_stm32::gpio::{Input, Level, Output, Pull, Speed}; | ||
| 6 | use {defmt_rtt as _, panic_probe as _}; | ||
| 7 | |||
| 8 | #[entry] | ||
| 9 | fn main() -> ! { | ||
| 10 | let p = embassy_stm32::init(Default::default()); | ||
| 11 | let mut led = Output::new(p.PB14, Level::High, Speed::VeryHigh); | ||
| 12 | let button = Input::new(p.PC13, Pull::Up); | ||
| 13 | |||
| 14 | loop { | ||
| 15 | if button.is_low() { | ||
| 16 | led.set_high(); | ||
| 17 | } else { | ||
| 18 | led.set_low(); | ||
| 19 | } | ||
| 20 | } | ||
| 21 | } | ||
diff --git a/docs/examples/layer-by-layer/blinky-irq/Cargo.toml b/docs/examples/layer-by-layer/blinky-irq/Cargo.toml new file mode 100644 index 000000000..9733658b6 --- /dev/null +++ b/docs/examples/layer-by-layer/blinky-irq/Cargo.toml | |||
| @@ -0,0 +1,14 @@ | |||
| 1 | [package] | ||
| 2 | name = "blinky-irq" | ||
| 3 | version = "0.1.0" | ||
| 4 | edition = "2021" | ||
| 5 | license = "MIT OR Apache-2.0" | ||
| 6 | |||
| 7 | [dependencies] | ||
| 8 | cortex-m = "0.7" | ||
| 9 | cortex-m-rt = { version = "0.7" } | ||
| 10 | embassy-stm32 = { version = "0.1.0", features = ["stm32l475vg", "memory-x", "unstable-pac"] } | ||
| 11 | |||
| 12 | defmt = "0.3.0" | ||
| 13 | defmt-rtt = "0.3.0" | ||
| 14 | panic-probe = { version = "0.3.0", features = ["print-defmt"] } | ||
diff --git a/docs/examples/layer-by-layer/blinky-irq/src/main.rs b/docs/examples/layer-by-layer/blinky-irq/src/main.rs new file mode 100644 index 000000000..743c9d99b --- /dev/null +++ b/docs/examples/layer-by-layer/blinky-irq/src/main.rs | |||
| @@ -0,0 +1,93 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | |||
| 4 | use core::cell::RefCell; | ||
| 5 | |||
| 6 | use cortex_m::interrupt::Mutex; | ||
| 7 | use cortex_m::peripheral::NVIC; | ||
| 8 | use cortex_m_rt::entry; | ||
| 9 | use embassy_stm32::gpio::{Input, Level, Output, Pull, Speed}; | ||
| 10 | use embassy_stm32::{interrupt, pac}; | ||
| 11 | use {defmt_rtt as _, panic_probe as _}; | ||
| 12 | |||
| 13 | static BUTTON: Mutex<RefCell<Option<Input<'static>>>> = Mutex::new(RefCell::new(None)); | ||
| 14 | static LED: Mutex<RefCell<Option<Output<'static>>>> = Mutex::new(RefCell::new(None)); | ||
| 15 | |||
| 16 | #[entry] | ||
| 17 | fn main() -> ! { | ||
| 18 | let p = embassy_stm32::init(Default::default()); | ||
| 19 | let led = Output::new(p.PB14, Level::Low, Speed::Low); | ||
| 20 | let mut button = Input::new(p.PC13, Pull::Up); | ||
| 21 | |||
| 22 | cortex_m::interrupt::free(|cs| { | ||
| 23 | enable_interrupt(&mut button); | ||
| 24 | |||
| 25 | LED.borrow(cs).borrow_mut().replace(led); | ||
| 26 | BUTTON.borrow(cs).borrow_mut().replace(button); | ||
| 27 | |||
| 28 | unsafe { NVIC::unmask(pac::Interrupt::EXTI15_10) }; | ||
| 29 | }); | ||
| 30 | |||
| 31 | loop { | ||
| 32 | cortex_m::asm::wfe(); | ||
| 33 | } | ||
| 34 | } | ||
| 35 | |||
| 36 | #[interrupt] | ||
| 37 | fn EXTI15_10() { | ||
| 38 | cortex_m::interrupt::free(|cs| { | ||
| 39 | let mut button = BUTTON.borrow(cs).borrow_mut(); | ||
| 40 | let button = button.as_mut().unwrap(); | ||
| 41 | |||
| 42 | let mut led = LED.borrow(cs).borrow_mut(); | ||
| 43 | let led = led.as_mut().unwrap(); | ||
| 44 | if check_interrupt(button) { | ||
| 45 | if button.is_low() { | ||
| 46 | led.set_high(); | ||
| 47 | } else { | ||
| 48 | led.set_low(); | ||
| 49 | } | ||
| 50 | } | ||
| 51 | clear_interrupt(button); | ||
| 52 | }); | ||
| 53 | } | ||
| 54 | // | ||
| 55 | // | ||
| 56 | // | ||
| 57 | // | ||
| 58 | // | ||
| 59 | // | ||
| 60 | // "Hidden" HAL-like methods for doing interrupts with embassy. Hardcode pin just to give audience an idea of what it looks like | ||
| 61 | |||
| 62 | const PORT: u8 = 2; | ||
| 63 | const PIN: usize = 13; | ||
| 64 | fn check_interrupt(_pin: &mut Input<'static>) -> bool { | ||
| 65 | let exti = pac::EXTI; | ||
| 66 | let pin = PIN; | ||
| 67 | let lines = exti.pr(0).read(); | ||
| 68 | lines.line(pin) | ||
| 69 | } | ||
| 70 | |||
| 71 | fn clear_interrupt(_pin: &mut Input<'static>) { | ||
| 72 | let exti = pac::EXTI; | ||
| 73 | let pin = PIN; | ||
| 74 | let mut lines = exti.pr(0).read(); | ||
| 75 | lines.set_line(pin, true); | ||
| 76 | exti.pr(0).write_value(lines); | ||
| 77 | } | ||
| 78 | |||
| 79 | fn enable_interrupt(_pin: &mut Input<'static>) { | ||
| 80 | cortex_m::interrupt::free(|_| { | ||
| 81 | let rcc = pac::RCC; | ||
| 82 | rcc.apb2enr().modify(|w| w.set_syscfgen(true)); | ||
| 83 | |||
| 84 | let port = PORT; | ||
| 85 | let pin = PIN; | ||
| 86 | let syscfg = pac::SYSCFG; | ||
| 87 | let exti = pac::EXTI; | ||
| 88 | syscfg.exticr(pin / 4).modify(|w| w.set_exti(pin % 4, port)); | ||
| 89 | exti.imr(0).modify(|w| w.set_line(pin, true)); | ||
| 90 | exti.rtsr(0).modify(|w| w.set_line(pin, true)); | ||
| 91 | exti.ftsr(0).modify(|w| w.set_line(pin, true)); | ||
| 92 | }); | ||
| 93 | } | ||
diff --git a/docs/examples/layer-by-layer/blinky-pac/Cargo.toml b/docs/examples/layer-by-layer/blinky-pac/Cargo.toml new file mode 100644 index 000000000..f872b94cb --- /dev/null +++ b/docs/examples/layer-by-layer/blinky-pac/Cargo.toml | |||
| @@ -0,0 +1,14 @@ | |||
| 1 | [package] | ||
| 2 | name = "blinky-pac" | ||
| 3 | version = "0.1.0" | ||
| 4 | edition = "2021" | ||
| 5 | license = "MIT OR Apache-2.0" | ||
| 6 | |||
| 7 | [dependencies] | ||
| 8 | cortex-m = "0.7" | ||
| 9 | cortex-m-rt = "0.7" | ||
| 10 | stm32-metapac = { version = "1", features = ["stm32l475vg", "memory-x"] } | ||
| 11 | |||
| 12 | defmt = "0.3.0" | ||
| 13 | defmt-rtt = "0.3.0" | ||
| 14 | panic-probe = { version = "0.3.0", features = ["print-defmt"] } | ||
diff --git a/docs/examples/layer-by-layer/blinky-pac/src/main.rs b/docs/examples/layer-by-layer/blinky-pac/src/main.rs new file mode 100644 index 000000000..990d46cb6 --- /dev/null +++ b/docs/examples/layer-by-layer/blinky-pac/src/main.rs | |||
| @@ -0,0 +1,53 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | |||
| 4 | use pac::gpio::vals; | ||
| 5 | use {defmt_rtt as _, panic_probe as _, stm32_metapac as pac}; | ||
| 6 | |||
| 7 | #[cortex_m_rt::entry] | ||
| 8 | fn main() -> ! { | ||
| 9 | // Enable GPIO clock | ||
| 10 | let rcc = pac::RCC; | ||
| 11 | unsafe { | ||
| 12 | rcc.ahb2enr().modify(|w| { | ||
| 13 | w.set_gpioben(true); | ||
| 14 | w.set_gpiocen(true); | ||
| 15 | }); | ||
| 16 | |||
| 17 | rcc.ahb2rstr().modify(|w| { | ||
| 18 | w.set_gpiobrst(true); | ||
| 19 | w.set_gpiocrst(true); | ||
| 20 | w.set_gpiobrst(false); | ||
| 21 | w.set_gpiocrst(false); | ||
| 22 | }); | ||
| 23 | } | ||
| 24 | |||
| 25 | // Setup button | ||
| 26 | let gpioc = pac::GPIOC; | ||
| 27 | const BUTTON_PIN: usize = 13; | ||
| 28 | unsafe { | ||
| 29 | gpioc.pupdr().modify(|w| w.set_pupdr(BUTTON_PIN, vals::Pupdr::PULLUP)); | ||
| 30 | gpioc.otyper().modify(|w| w.set_ot(BUTTON_PIN, vals::Ot::PUSHPULL)); | ||
| 31 | gpioc.moder().modify(|w| w.set_moder(BUTTON_PIN, vals::Moder::INPUT)); | ||
| 32 | } | ||
| 33 | |||
| 34 | // Setup LED | ||
| 35 | let gpiob = pac::GPIOB; | ||
| 36 | const LED_PIN: usize = 14; | ||
| 37 | unsafe { | ||
| 38 | gpiob.pupdr().modify(|w| w.set_pupdr(LED_PIN, vals::Pupdr::FLOATING)); | ||
| 39 | gpiob.otyper().modify(|w| w.set_ot(LED_PIN, vals::Ot::PUSHPULL)); | ||
| 40 | gpiob.moder().modify(|w| w.set_moder(LED_PIN, vals::Moder::OUTPUT)); | ||
| 41 | } | ||
| 42 | |||
| 43 | // Main loop | ||
| 44 | loop { | ||
| 45 | unsafe { | ||
| 46 | if gpioc.idr().read().idr(BUTTON_PIN) == vals::Idr::LOW { | ||
| 47 | gpiob.bsrr().write(|w| w.set_bs(LED_PIN, true)); | ||
| 48 | } else { | ||
| 49 | gpiob.bsrr().write(|w| w.set_br(LED_PIN, true)); | ||
| 50 | } | ||
| 51 | } | ||
| 52 | } | ||
| 53 | } | ||
