diff options
Diffstat (limited to 'docs/examples/layer-by-layer/blinky-pac/src/main.rs')
| -rw-r--r-- | docs/examples/layer-by-layer/blinky-pac/src/main.rs | 53 |
1 files changed, 53 insertions, 0 deletions
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 | } | ||
