Advertisement
Guest User

rtic_blinky_stm32f2

a guest
Jul 15th, 2021
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 2.30 KB | None | 0 0
  1. // run with gdb, either from terminal or in vscode
  2. //
  3. // from terminal:
  4. // start openocd in separate terminal
  5. // > openocd -f openocd.cfg
  6. //
  7. // start gdb
  8. // > arm-none-eabi-gdb target/thumbv7em-none-eabihf/debug/examples/rtic_blinky -x openocd.gdb
  9.  
  10. #![deny(unsafe_code)]
  11. #![deny(warnings)]
  12. #![no_main]
  13. #![no_std]
  14.  
  15. use panic_semihosting as _;
  16.  
  17. #[rtic::app(device = stm32f2xx_hal::stm32, dispatchers = [EXTI0], peripherals = true)]
  18. mod app {
  19.     use cortex_m_semihosting::hprintln;
  20.     use dwt_systick_monotonic::DwtSystick;
  21.     use rtic::time::{duration::Seconds};
  22.  
  23.     const MONO_HZ: u32 = 8_000_000; // 8 MHz
  24.  
  25.     #[monotonic(binds = SysTick, default = true)]
  26.     type MyMono = DwtSystick<MONO_HZ>;
  27.  
  28.     #[shared]
  29.     struct Shared {
  30.         gpioa: stm32f2xx_hal::stm32::GPIOA,
  31.         toggle: bool,
  32.     }
  33.  
  34.     #[local]
  35.     struct Local {}
  36.  
  37.     #[init]
  38.     fn init(cx: init::Context) -> (Shared, Local, init::Monotonics) {
  39.         let mut dcb = cx.core.DCB;
  40.         let dwt = cx.core.DWT;
  41.         let systick = cx.core.SYST;
  42.  
  43.         let mono = DwtSystick::new(&mut dcb, dwt, systick, 8_000_000); // maybe MONO_HZ?
  44.  
  45.         let device = cx.device;
  46.  
  47.         // power on GPIOA, RM0368 6.3.11
  48.         device.RCC.ahb1enr.modify(|_, w| w.gpioaen().set_bit());
  49.         // configure PA5 as output, RM0368 8.4.1
  50.         device.GPIOA.moder.modify(|_, w| w.moder5().bits(1));
  51.  
  52.         hprintln!("init").ok();
  53.  
  54.         // Schedule `toggle` to run 8e6 cycles (clock cycles) in the future
  55.         toggle::spawn_after(Seconds(1u32)).ok();
  56.  
  57.         (
  58.             Shared {
  59.                 gpioa: device.GPIOA,
  60.                 toggle: false,
  61.             },
  62.             Local {},
  63.             init::Monotonics(mono),
  64.         )
  65.     }
  66.  
  67.     #[task(shared = [toggle, gpioa])]
  68.     fn toggle(cx: toggle::Context) {
  69.         let toggle = cx.shared.toggle;
  70.         let gpioa = cx.shared.gpioa;
  71.  
  72.        
  73.  
  74.         (toggle, gpioa).lock(|toggle, gpioa| {
  75.             hprintln!("toggle: {}  @ !", toggle).unwrap();
  76.            
  77.             if *toggle {
  78.                 gpioa.bsrr.write(|w| w.bs5().set_bit());
  79.             } else {
  80.                 gpioa.bsrr.write(|w| w.br5().set_bit());
  81.             }
  82.  
  83.             *toggle = !*toggle;
  84.         });
  85.         toggle::spawn_after(Seconds(1u32)).ok();
  86.     }
  87. }
  88.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement