Putting it all together

  1. #![no_main]
  2. #![no_std]
  3. use aux9::{entry, tim6};
  4. #[inline(never)]
  5. fn delay(tim6: &tim6::RegisterBlock, ms: u16) {
  6. // Set the timer to go off in `ms` ticks
  7. // 1 tick = 1 ms
  8. tim6.arr.write(|w| w.arr().bits(ms));
  9. // CEN: Enable the counter
  10. tim6.cr1.modify(|_, w| w.cen().set_bit());
  11. // Wait until the alarm goes off (until the update event occurs)
  12. while !tim6.sr.read().uif().bit_is_set() {}
  13. // Clear the update event flag
  14. tim6.sr.modify(|_, w| w.uif().clear_bit());
  15. }
  16. #[entry]
  17. fn main() -> ! {
  18. let (mut leds, rcc, tim6) = aux9::init();
  19. // Power on the TIM6 timer
  20. rcc.apb1enr.modify(|_, w| w.tim6en().set_bit());
  21. // OPM Select one pulse mode
  22. // CEN Keep the counter disabled for now
  23. tim6.cr1.write(|w| w.opm().set_bit().cen().clear_bit());
  24. // Configure the prescaler to have the counter operate at 1 KHz
  25. // APB1_CLOCK = 8 MHz
  26. // PSC = 7999
  27. // 8 MHz / (7999 + 1) = 1 KHz
  28. // The counter (CNT) will increase on every millisecond
  29. tim6.psc.write(|w| w.psc().bits(7_999));
  30. let ms = 50;
  31. loop {
  32. for curr in 0..8 {
  33. let next = (curr + 1) % 8;
  34. leds[next].on();
  35. delay(tim6, ms);
  36. leds[curr].off();
  37. delay(tim6, ms);
  38. }
  39. }
  40. }