timer.rs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. use cast::{u16, u32};
  2. use cortex_m::peripheral::syst::SystClkSource;
  3. use cortex_m::peripheral::SYST;
  4. use hal::timer::{CountDown, Periodic};
  5. use nb;
  6. use stm32::{TIM1, TIM2, TIM3, TIM4};
  7. use void::Void;
  8. use core::any::TypeId;
  9. use rcc::{APB1, APB2, Clocks};
  10. use time::Hertz;
  11. /// Interrupt events
  12. pub enum Event {
  13. /// Timer timed out / count down ended
  14. Update,
  15. }
  16. pub struct Timer<TIM> {
  17. tim: TIM,
  18. clocks: Clocks,
  19. }
  20. impl Timer<SYST> {
  21. pub fn syst<T>(mut syst: SYST, timeout: T, clocks: Clocks) -> Self
  22. where
  23. T: Into<Hertz>,
  24. {
  25. syst.set_clock_source(SystClkSource::Core);
  26. let mut timer = Timer { tim: syst, clocks };
  27. timer.start(timeout);
  28. timer
  29. }
  30. /// Starts listening for an `event`
  31. pub fn listen(&mut self, event: Event) {
  32. match event {
  33. Event::Update => self.tim.enable_interrupt(),
  34. }
  35. }
  36. /// Stops listening for an `event`
  37. pub fn unlisten(&mut self, event: Event) {
  38. match event {
  39. Event::Update => self.tim.disable_interrupt(),
  40. }
  41. }
  42. }
  43. impl CountDown for Timer<SYST> {
  44. type Time = Hertz;
  45. fn start<T>(&mut self, timeout: T)
  46. where
  47. T: Into<Hertz>,
  48. {
  49. let rvr = self.clocks.sysclk().0 / timeout.into().0 - 1;
  50. assert!(rvr < (1 << 24));
  51. self.tim.set_reload(rvr);
  52. self.tim.clear_current();
  53. self.tim.enable_counter();
  54. }
  55. fn wait(&mut self) -> nb::Result<(), Void> {
  56. if self.tim.has_wrapped() {
  57. Ok(())
  58. } else {
  59. Err(nb::Error::WouldBlock)
  60. }
  61. }
  62. }
  63. impl Periodic for Timer<SYST> {}
  64. macro_rules! hal {
  65. ($($TIMX:ident: ($timX:ident, $timXen:ident, $timXrst:ident, $apbX:ident),)+) => {
  66. $(
  67. impl Timer<$TIMX> {
  68. pub fn $timX<T>(tim: $TIMX, timeout: T, clocks: Clocks, apb1: &mut $apbX) -> Self
  69. where
  70. T: Into<Hertz>,
  71. {
  72. // enable and reset peripheral to a clean slate state
  73. apb1.enr().modify(|_, w| w.$timXen().set_bit());
  74. apb1.rstr().modify(|_, w| w.$timXrst().set_bit());
  75. apb1.rstr().modify(|_, w| w.$timXrst().clear_bit());
  76. let mut timer = Timer { clocks, tim };
  77. timer.start(timeout);
  78. timer
  79. }
  80. /// Starts listening for an `event`
  81. pub fn listen(&mut self, event: Event) {
  82. match event {
  83. Event::Update => self.tim.dier.write(|w| w.uie().set_bit()),
  84. }
  85. }
  86. /// Stops listening for an `event`
  87. pub fn unlisten(&mut self, event: Event) {
  88. match event {
  89. Event::Update => self.tim.dier.write(|w| w.uie().clear_bit()),
  90. }
  91. }
  92. /// Return the bus clock frequency in hertz.
  93. fn get_bus_clock(&self) -> Hertz {
  94. if TypeId::of::<$apbX>() == TypeId::of::<APB1>() {
  95. Hertz(self.clocks.pclk1().0 * self.get_bus_frequency_multiplier())
  96. } else if TypeId::of::<$apbX>() == TypeId::of::<APB2>() {
  97. Hertz(self.clocks.pclk2().0 * self.get_bus_frequency_multiplier())
  98. } else {
  99. unreachable!()
  100. }
  101. }
  102. /// Return the bus frequency multiplier.
  103. fn get_bus_frequency_multiplier(&self) -> u32 {
  104. if TypeId::of::<$apbX>() == TypeId::of::<APB1>() {
  105. if self.clocks.ppre1() == 1 {
  106. 1
  107. } else {
  108. 2
  109. }
  110. } else if TypeId::of::<$apbX>() == TypeId::of::<APB2>() {
  111. if self.clocks.ppre2() == 1 {
  112. 1
  113. } else {
  114. 2
  115. }
  116. } else {
  117. unreachable!()
  118. }
  119. }
  120. }
  121. impl CountDown for Timer<$TIMX> {
  122. type Time = Hertz;
  123. fn start<T>(&mut self, timeout: T)
  124. where
  125. T: Into<Hertz>,
  126. {
  127. // pause
  128. self.tim.cr1.modify(|_, w| w.cen().clear_bit());
  129. let frequency = timeout.into().0;
  130. let timer_clock = self.get_bus_clock();
  131. let ticks = timer_clock.0 / frequency;
  132. let psc = u16((ticks - 1) / (1 << 16)).unwrap();
  133. self.tim.psc.write(|w| w.psc().bits(psc));
  134. let arr = u16(ticks / u32(psc + 1)).unwrap();
  135. self.tim.arr.write(|w| unsafe { w.bits(u32(arr)) });
  136. // Trigger an update event to load the prescaler value to the clock
  137. self.tim.egr.write(|w| w.ug().set_bit());
  138. // The above line raises an update event which will indicate
  139. // that the timer is already finished. Since this is not the case,
  140. // it should be cleared
  141. self.tim.sr.modify(|_, w| w.uif().clear_bit());
  142. // start counter
  143. self.tim.cr1.modify(|_, w| w.cen().set_bit());
  144. }
  145. fn wait(&mut self) -> nb::Result<(), Void> {
  146. if self.tim.sr.read().uif().bit_is_clear() {
  147. Err(nb::Error::WouldBlock)
  148. } else {
  149. self.tim.sr.modify(|_, w| w.uif().clear_bit());
  150. Ok(())
  151. }
  152. }
  153. }
  154. impl Periodic for Timer<$TIMX> {}
  155. )+
  156. }
  157. }
  158. hal! {
  159. TIM1: (tim1, tim1en, tim1rst, APB2),
  160. TIM2: (tim2, tim2en, tim2rst, APB1),
  161. TIM3: (tim3, tim3en, tim3rst, APB1),
  162. TIM4: (tim4, tim4en, tim4rst, APB1),
  163. }