diff options
Diffstat (limited to 'docs/modules/ROOT/examples')
10 files changed, 307 insertions, 0 deletions
diff --git a/docs/modules/ROOT/examples/layer-by-layer/.cargo/config.toml b/docs/modules/ROOT/examples/layer-by-layer/.cargo/config.toml new file mode 100644 index 000000000..946ff250b --- /dev/null +++ b/docs/modules/ROOT/examples/layer-by-layer/.cargo/config.toml | |||
| @@ -0,0 +1,11 @@ | |||
| 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" | ||
diff --git a/docs/modules/ROOT/examples/layer-by-layer/Cargo.toml b/docs/modules/ROOT/examples/layer-by-layer/Cargo.toml new file mode 100644 index 000000000..2dca3cc8d --- /dev/null +++ b/docs/modules/ROOT/examples/layer-by-layer/Cargo.toml | |||
| @@ -0,0 +1,22 @@ | |||
| 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 = { path = "../../../../../embassy" } | ||
| 12 | embassy-stm32 = { path = "../../../../../embassy-stm32" } | ||
| 13 | stm32-metapac = { path = "../../../../../stm32-metapac" } | ||
| 14 | |||
| 15 | [profile.release] | ||
| 16 | codegen-units = 1 | ||
| 17 | debug = 2 | ||
| 18 | debug-assertions = false | ||
| 19 | incremental = false | ||
| 20 | lto = "fat" | ||
| 21 | opt-level = 's' | ||
| 22 | overflow-checks = false | ||
diff --git a/docs/modules/ROOT/examples/layer-by-layer/blinky-async/Cargo.toml b/docs/modules/ROOT/examples/layer-by-layer/blinky-async/Cargo.toml new file mode 100644 index 000000000..e0c63251e --- /dev/null +++ b/docs/modules/ROOT/examples/layer-by-layer/blinky-async/Cargo.toml | |||
| @@ -0,0 +1,14 @@ | |||
| 1 | [package] | ||
| 2 | name = "blinky-async" | ||
| 3 | version = "0.1.0" | ||
| 4 | edition = "2021" | ||
| 5 | |||
| 6 | [dependencies] | ||
| 7 | cortex-m = "0.7" | ||
| 8 | cortex-m-rt = "0.7" | ||
| 9 | embassy-stm32 = { version = "0.1.0", features = ["stm32l475vg", "memory-x", "exti"], default-features = false } | ||
| 10 | embassy = { version = "0.1.0", default-features = false, features = ["nightly"] } | ||
| 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/modules/ROOT/examples/layer-by-layer/blinky-async/src/main.rs b/docs/modules/ROOT/examples/layer-by-layer/blinky-async/src/main.rs new file mode 100644 index 000000000..35726be64 --- /dev/null +++ b/docs/modules/ROOT/examples/layer-by-layer/blinky-async/src/main.rs | |||
| @@ -0,0 +1,28 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | #![feature(type_alias_impl_trait)] | ||
| 4 | |||
| 5 | use defmt_rtt as _; | ||
| 6 | use panic_probe as _; | ||
| 7 | |||
| 8 | use embassy::executor::Spawner; | ||
| 9 | use embassy_stm32::{ | ||
| 10 | exti::ExtiInput, | ||
| 11 | gpio::{Input, Level, Output, Pull, Speed}, | ||
| 12 | Peripherals, | ||
| 13 | }; | ||
| 14 | |||
| 15 | #[embassy::main] | ||
| 16 | async fn main(_s: Spawner, p: Peripherals) { | ||
| 17 | let mut led = Output::new(p.PB14, Level::Low, Speed::VeryHigh); | ||
| 18 | let mut button = ExtiInput::new(Input::new(p.PC13, Pull::Up), p.EXTI13); | ||
| 19 | |||
| 20 | loop { | ||
| 21 | button.wait_for_any_edge().await; | ||
| 22 | if button.is_low() { | ||
| 23 | led.set_high(); | ||
| 24 | } else { | ||
| 25 | led.set_low(); | ||
| 26 | } | ||
| 27 | } | ||
| 28 | } | ||
diff --git a/docs/modules/ROOT/examples/layer-by-layer/blinky-hal/Cargo.toml b/docs/modules/ROOT/examples/layer-by-layer/blinky-hal/Cargo.toml new file mode 100644 index 000000000..dbd3aba8b --- /dev/null +++ b/docs/modules/ROOT/examples/layer-by-layer/blinky-hal/Cargo.toml | |||
| @@ -0,0 +1,13 @@ | |||
| 1 | [package] | ||
| 2 | name = "blinky-hal" | ||
| 3 | version = "0.1.0" | ||
| 4 | edition = "2021" | ||
| 5 | |||
| 6 | [dependencies] | ||
| 7 | cortex-m = "0.7" | ||
| 8 | cortex-m-rt = "0.7" | ||
| 9 | embassy-stm32 = { version = "0.1.0", features = ["stm32l475vg", "memory-x"], default-features = false } | ||
| 10 | |||
| 11 | defmt = "0.3.0" | ||
| 12 | defmt-rtt = "0.3.0" | ||
| 13 | panic-probe = { version = "0.3.0", features = ["print-defmt"] } | ||
diff --git a/docs/modules/ROOT/examples/layer-by-layer/blinky-hal/src/main.rs b/docs/modules/ROOT/examples/layer-by-layer/blinky-hal/src/main.rs new file mode 100644 index 000000000..2064ea616 --- /dev/null +++ b/docs/modules/ROOT/examples/layer-by-layer/blinky-hal/src/main.rs | |||
| @@ -0,0 +1,23 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | |||
| 4 | use cortex_m_rt::entry; | ||
| 5 | use defmt_rtt as _; | ||
| 6 | use panic_probe as _; | ||
| 7 | |||
| 8 | use embassy_stm32::gpio::{Input, Level, Output, Pull, Speed}; | ||
| 9 | |||
| 10 | #[entry] | ||
| 11 | fn main() -> ! { | ||
| 12 | let p = embassy_stm32::init(Default::default()); | ||
| 13 | let mut led = Output::new(p.PB14, Level::High, Speed::VeryHigh); | ||
| 14 | let button = Input::new(p.PC13, Pull::Up); | ||
| 15 | |||
| 16 | loop { | ||
| 17 | if button.is_low() { | ||
| 18 | led.set_high(); | ||
| 19 | } else { | ||
| 20 | led.set_low(); | ||
| 21 | } | ||
| 22 | } | ||
| 23 | } | ||
diff --git a/docs/modules/ROOT/examples/layer-by-layer/blinky-irq/Cargo.toml b/docs/modules/ROOT/examples/layer-by-layer/blinky-irq/Cargo.toml new file mode 100644 index 000000000..0dd326015 --- /dev/null +++ b/docs/modules/ROOT/examples/layer-by-layer/blinky-irq/Cargo.toml | |||
| @@ -0,0 +1,13 @@ | |||
| 1 | [package] | ||
| 2 | name = "blinky-irq" | ||
| 3 | version = "0.1.0" | ||
| 4 | edition = "2021" | ||
| 5 | |||
| 6 | [dependencies] | ||
| 7 | cortex-m = "0.7" | ||
| 8 | cortex-m-rt = { version = "0.7" } | ||
| 9 | embassy-stm32 = { version = "0.1.0", features = ["stm32l475vg", "memory-x", "unstable-pac"] } | ||
| 10 | |||
| 11 | defmt = "0.3.0" | ||
| 12 | defmt-rtt = "0.3.0" | ||
| 13 | panic-probe = { version = "0.3.0", features = ["print-defmt"] } | ||
diff --git a/docs/modules/ROOT/examples/layer-by-layer/blinky-irq/src/main.rs b/docs/modules/ROOT/examples/layer-by-layer/blinky-irq/src/main.rs new file mode 100644 index 000000000..6a75384c4 --- /dev/null +++ b/docs/modules/ROOT/examples/layer-by-layer/blinky-irq/src/main.rs | |||
| @@ -0,0 +1,101 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | |||
| 4 | use defmt_rtt as _; | ||
| 5 | use panic_probe as _; | ||
| 6 | |||
| 7 | use core::cell::RefCell; | ||
| 8 | use cortex_m::interrupt::Mutex; | ||
| 9 | use cortex_m::peripheral::NVIC; | ||
| 10 | use cortex_m_rt::entry; | ||
| 11 | use embassy_stm32::{ | ||
| 12 | gpio::{Input, Level, Output, Pin, Pull, Speed}, | ||
| 13 | interrupt, pac, | ||
| 14 | peripherals::{PB14, PC13}, | ||
| 15 | }; | ||
| 16 | |||
| 17 | static BUTTON: Mutex<RefCell<Option<Input<'static, PC13>>>> = Mutex::new(RefCell::new(None)); | ||
| 18 | static LED: Mutex<RefCell<Option<Output<'static, PB14>>>> = Mutex::new(RefCell::new(None)); | ||
| 19 | |||
| 20 | #[entry] | ||
| 21 | fn main() -> ! { | ||
| 22 | let p = embassy_stm32::init(Default::default()); | ||
| 23 | let led = Output::new(p.PB14, Level::Low, Speed::Low); | ||
| 24 | let mut button = Input::new(p.PC13, Pull::Up); | ||
| 25 | |||
| 26 | cortex_m::interrupt::free(|cs| unsafe { | ||
| 27 | enable_interrupt(&mut button); | ||
| 28 | |||
| 29 | LED.borrow(cs).borrow_mut().replace(led); | ||
| 30 | BUTTON.borrow(cs).borrow_mut().replace(button); | ||
| 31 | |||
| 32 | NVIC::unmask(pac::Interrupt::EXTI15_10); | ||
| 33 | }); | ||
| 34 | |||
| 35 | loop { | ||
| 36 | cortex_m::asm::wfe(); | ||
| 37 | } | ||
| 38 | } | ||
| 39 | |||
| 40 | #[interrupt] | ||
| 41 | fn EXTI15_10() { | ||
| 42 | cortex_m::interrupt::free(|cs| { | ||
| 43 | let mut button = BUTTON.borrow(cs).borrow_mut(); | ||
| 44 | let button = button.as_mut().unwrap(); | ||
| 45 | |||
| 46 | let mut led = LED.borrow(cs).borrow_mut(); | ||
| 47 | let led = led.as_mut().unwrap(); | ||
| 48 | if check_interrupt(button) { | ||
| 49 | if button.is_low() { | ||
| 50 | led.set_high(); | ||
| 51 | } else { | ||
| 52 | led.set_low(); | ||
| 53 | } | ||
| 54 | } | ||
| 55 | clear_interrupt(button); | ||
| 56 | }); | ||
| 57 | } | ||
| 58 | // | ||
| 59 | // | ||
| 60 | // | ||
| 61 | // | ||
| 62 | // | ||
| 63 | // | ||
| 64 | // "Hidden" HAL-like methods for doing interrupts with embassy. Hardcode pin just to give audience an idea of what it looks like | ||
| 65 | |||
| 66 | const PORT: u8 = 2; | ||
| 67 | const PIN: usize = 13; | ||
| 68 | fn check_interrupt<P: Pin>(_pin: &mut Input<'static, P>) -> bool { | ||
| 69 | let exti = pac::EXTI; | ||
| 70 | unsafe { | ||
| 71 | let pin = PIN; | ||
| 72 | let lines = exti.pr(0).read(); | ||
| 73 | lines.line(pin) | ||
| 74 | } | ||
| 75 | } | ||
| 76 | |||
| 77 | fn clear_interrupt<P: Pin>(_pin: &mut Input<'static, P>) { | ||
| 78 | let exti = pac::EXTI; | ||
| 79 | unsafe { | ||
| 80 | let pin = PIN; | ||
| 81 | let mut lines = exti.pr(0).read(); | ||
| 82 | lines.set_line(pin, true); | ||
| 83 | exti.pr(0).write_value(lines); | ||
| 84 | } | ||
| 85 | } | ||
| 86 | |||
| 87 | fn enable_interrupt<P: Pin>(_pin: &mut Input<'static, P>) { | ||
| 88 | cortex_m::interrupt::free(|_| unsafe { | ||
| 89 | let rcc = pac::RCC; | ||
| 90 | rcc.apb2enr().modify(|w| w.set_syscfgen(true)); | ||
| 91 | |||
| 92 | let port = PORT; | ||
| 93 | let pin = PIN; | ||
| 94 | let syscfg = pac::SYSCFG; | ||
| 95 | let exti = pac::EXTI; | ||
| 96 | syscfg.exticr(pin / 4).modify(|w| w.set_exti(pin % 4, port)); | ||
| 97 | exti.imr(0).modify(|w| w.set_line(pin, true)); | ||
| 98 | exti.rtsr(0).modify(|w| w.set_line(pin, true)); | ||
| 99 | exti.ftsr(0).modify(|w| w.set_line(pin, true)); | ||
| 100 | }); | ||
| 101 | } | ||
diff --git a/docs/modules/ROOT/examples/layer-by-layer/blinky-pac/Cargo.toml b/docs/modules/ROOT/examples/layer-by-layer/blinky-pac/Cargo.toml new file mode 100644 index 000000000..e7f4f5d1f --- /dev/null +++ b/docs/modules/ROOT/examples/layer-by-layer/blinky-pac/Cargo.toml | |||
| @@ -0,0 +1,13 @@ | |||
| 1 | [package] | ||
| 2 | name = "blinky-pac" | ||
| 3 | version = "0.1.0" | ||
| 4 | edition = "2021" | ||
| 5 | |||
| 6 | [dependencies] | ||
| 7 | cortex-m = "0.7" | ||
| 8 | cortex-m-rt = "0.7" | ||
| 9 | stm32-metapac = { version = "0.1.0", features = ["stm32l475vg", "memory-x"] } | ||
| 10 | |||
| 11 | defmt = "0.3.0" | ||
| 12 | defmt-rtt = "0.3.0" | ||
| 13 | panic-probe = { version = "0.3.0", features = ["print-defmt"] } | ||
diff --git a/docs/modules/ROOT/examples/layer-by-layer/blinky-pac/src/main.rs b/docs/modules/ROOT/examples/layer-by-layer/blinky-pac/src/main.rs new file mode 100644 index 000000000..239eac188 --- /dev/null +++ b/docs/modules/ROOT/examples/layer-by-layer/blinky-pac/src/main.rs | |||
| @@ -0,0 +1,69 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | |||
| 4 | use defmt_rtt as _; | ||
| 5 | use panic_probe as _; | ||
| 6 | |||
| 7 | use stm32_metapac as pac; | ||
| 8 | |||
| 9 | use pac::gpio::vals; | ||
| 10 | |||
| 11 | #[cortex_m_rt::entry] | ||
| 12 | fn main() -> ! { | ||
| 13 | // Enable GPIO clock | ||
| 14 | let rcc = pac::RCC; | ||
| 15 | unsafe { | ||
| 16 | rcc.ahb2enr().modify(|w| { | ||
| 17 | w.set_gpioben(true); | ||
| 18 | w.set_gpiocen(true); | ||
| 19 | }); | ||
| 20 | |||
| 21 | rcc.ahb2rstr().modify(|w| { | ||
| 22 | w.set_gpiobrst(true); | ||
| 23 | w.set_gpiocrst(true); | ||
| 24 | w.set_gpiobrst(false); | ||
| 25 | w.set_gpiocrst(false); | ||
| 26 | }); | ||
| 27 | } | ||
| 28 | |||
| 29 | // Setup button | ||
| 30 | let gpioc = pac::GPIOC; | ||
| 31 | const BUTTON_PIN: usize = 13; | ||
| 32 | unsafe { | ||
| 33 | gpioc | ||
| 34 | .pupdr() | ||
| 35 | .modify(|w| w.set_pupdr(BUTTON_PIN, vals::Pupdr::PULLUP)); | ||
| 36 | gpioc | ||
| 37 | .otyper() | ||
| 38 | .modify(|w| w.set_ot(BUTTON_PIN, vals::Ot::PUSHPULL)); | ||
| 39 | gpioc | ||
| 40 | .moder() | ||
| 41 | .modify(|w| w.set_moder(BUTTON_PIN, vals::Moder::INPUT)); | ||
| 42 | } | ||
| 43 | |||
| 44 | // Setup LED | ||
| 45 | let gpiob = pac::GPIOB; | ||
| 46 | const LED_PIN: usize = 14; | ||
| 47 | unsafe { | ||
| 48 | gpiob | ||
| 49 | .pupdr() | ||
| 50 | .modify(|w| w.set_pupdr(LED_PIN, vals::Pupdr::FLOATING)); | ||
| 51 | gpiob | ||
| 52 | .otyper() | ||
| 53 | .modify(|w| w.set_ot(LED_PIN, vals::Ot::PUSHPULL)); | ||
| 54 | gpiob | ||
| 55 | .moder() | ||
| 56 | .modify(|w| w.set_moder(LED_PIN, vals::Moder::OUTPUT)); | ||
| 57 | } | ||
| 58 | |||
| 59 | // Main loop | ||
| 60 | loop { | ||
| 61 | unsafe { | ||
| 62 | if gpioc.idr().read().idr(BUTTON_PIN) == vals::Idr::LOW { | ||
| 63 | gpiob.bsrr().write(|w| w.set_bs(LED_PIN, true)); | ||
| 64 | } else { | ||
| 65 | gpiob.bsrr().write(|w| w.set_br(LED_PIN, true)); | ||
| 66 | } | ||
| 67 | } | ||
| 68 | } | ||
| 69 | } | ||
