Putting it all together

1. #![no_main]
2. #![no_std]

4. use aux9::{entry, tim6};

6. #[inline(never)]
7. fn delay(tim6: &tim6::RegisterBlock, ms: u16) {
8. // Set the timer to go off in `ms` ticks
9. // 1 tick = 1 ms
10. tim6.arr.write(|w| w.arr().bits(ms));

12. // CEN: Enable the counter
13. tim6.cr1.modify(|_, w| w.cen().set_bit());

15. // Wait until the alarm goes off (until the update event occurs)
16. while !tim6.sr.read().uif().bit_is_set() {}

18. // Clear the update event flag
19. tim6.sr.modify(|_, w| w.uif().clear_bit());
20. }

22. #[entry]
23. fn main() -> ! {
24. let (mut leds, rcc, tim6) = aux9::init();

26. // Power on the TIM6 timer
27. rcc.apb1enr.modify(|_, w| w.tim6en().set_bit());

29. // OPM Select one pulse mode
30. // CEN Keep the counter disabled for now
31. tim6.cr1.write(|w| w.opm().set_bit().cen().clear_bit());

33. // Configure the prescaler to have the counter operate at 1 KHz
34. // APB1_CLOCK = 8 MHz
35. // PSC = 7999
36. // 8 MHz / (7999 + 1) = 1 KHz
37. // The counter (CNT) will increase on every millisecond
38. tim6.psc.write(|w| w.psc().bits(7_999));

40. let ms = 50;
41. loop {
42. for curr in 0..8 {
43. let next = (curr + 1) % 8;

45. leds[next].on();
46. delay(tim6, ms);
47. leds[curr].off();
48. delay(tim6, ms);
49. }
50. }
51. }