blinky_timer_irq.rs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. //! blinky timer using interrupts on TIM2
  2. //!
  3. //! This assumes that a LED is connected to pc13 as is the case on the blue pill board.
  4. //!
  5. //! Please note according to RM0008:
  6. //! "Due to the fact that the switch only sinks a limited amount of current (3 mA), the use of
  7. //! GPIOs PC13 to PC15 in output mode is restricted: the speed has to be limited to 2MHz with
  8. //! a maximum load of 30pF and these IOs must not be used as a current source (e.g. to drive a LED)"
  9. #![no_main]
  10. #![no_std]
  11. use panic_halt as _;
  12. use stm32f1xx_hal as hal;
  13. use crate::hal::{
  14. gpio::{gpioc, Output, PushPull},
  15. pac::{interrupt, Interrupt, Peripherals, TIM2},
  16. prelude::*,
  17. timer::{CountDownTimer, Event, Timer},
  18. };
  19. use core::cell::RefCell;
  20. use cortex_m::{asm::wfi, interrupt::Mutex};
  21. use cortex_m_rt::entry;
  22. use embedded_hal::digital::v2::OutputPin;
  23. // NOTE You can uncomment 'hprintln' here and in the code below for a bit more
  24. // verbosity at runtime, at the cost of throwing off the timing of the blink
  25. // (using 'semihosting' for printing debug info anywhere slows program
  26. // execution down)
  27. //use cortex_m_semihosting::hprintln;
  28. // A type definition for the GPIO pin to be used for our LED
  29. type LEDPIN = gpioc::PC13<Output<PushPull>>;
  30. // Make LED pin globally available
  31. static G_LED: Mutex<RefCell<Option<LEDPIN>>> = Mutex::new(RefCell::new(None));
  32. // Make timer interrupt registers globally available
  33. static G_TIM: Mutex<RefCell<Option<CountDownTimer<TIM2>>>> = Mutex::new(RefCell::new(None));
  34. // Define an interupt handler, i.e. function to call when interrupt occurs.
  35. // This specific interrupt will "trip" when the timer TIM2 times out
  36. #[interrupt]
  37. fn TIM2() {
  38. static mut LED: Option<LEDPIN> = None;
  39. static mut TIM: Option<CountDownTimer<TIM2>> = None;
  40. let led = LED.get_or_insert_with(|| {
  41. cortex_m::interrupt::free(|cs| {
  42. // Move LED pin here, leaving a None in its place
  43. G_LED.borrow(cs).replace(None).unwrap()
  44. })
  45. });
  46. let tim = TIM.get_or_insert_with(|| {
  47. cortex_m::interrupt::free(|cs| {
  48. // Move LED pin here, leaving a None in its place
  49. G_TIM.borrow(cs).replace(None).unwrap()
  50. })
  51. });
  52. let _ = led.toggle();
  53. let _ = tim.wait();
  54. }
  55. #[entry]
  56. fn main() -> ! {
  57. let dp = Peripherals::take().unwrap();
  58. let mut rcc = dp.RCC.constrain();
  59. let mut flash = dp.FLASH.constrain();
  60. let clocks = rcc
  61. .cfgr
  62. .sysclk(8.mhz())
  63. .pclk1(8.mhz())
  64. .freeze(&mut flash.acr);
  65. // Configure PC13 pin to blink LED
  66. let mut gpioc = dp.GPIOC.split(&mut rcc.apb2);
  67. let mut led = gpioc.pc13.into_push_pull_output(&mut gpioc.crh);
  68. let _ = led.set_high(); // Turn off
  69. // Move the pin into our global storage
  70. cortex_m::interrupt::free(|cs| *G_LED.borrow(cs).borrow_mut() = Some(led));
  71. // Set up a timer expiring after 1s
  72. let mut timer = Timer::tim2(dp.TIM2, &clocks, &mut rcc.apb1).start_count_down(1.hz());
  73. // Generate an interrupt when the timer expires
  74. timer.listen(Event::Update);
  75. // Move the timer into our global storage
  76. cortex_m::interrupt::free(|cs| *G_TIM.borrow(cs).borrow_mut() = Some(timer));
  77. unsafe {
  78. cortex_m::peripheral::NVIC::unmask(Interrupt::TIM2);
  79. }
  80. loop {
  81. wfi();
  82. }
  83. }