pwm.rs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. use core::marker::PhantomData;
  2. use core::mem;
  3. use cast::{u16, u32};
  4. use hal;
  5. use stm32::{TIM2, TIM3, TIM4};
  6. use afio::MAPR;
  7. use bb;
  8. use gpio::gpioa::{PA0, PA1, PA2, PA3, PA6, PA7};
  9. use gpio::gpiob::{PB0, PB1, PB6, PB7, PB8, PB9};
  10. use gpio::{Alternate, PushPull};
  11. use rcc::{APB1, Clocks};
  12. use time::Hertz;
  13. pub trait Pins<TIM> {
  14. const REMAP: u8;
  15. const C1: bool;
  16. const C2: bool;
  17. const C3: bool;
  18. const C4: bool;
  19. type Channels;
  20. }
  21. impl Pins<TIM2>
  22. for (
  23. PA0<Alternate<PushPull>>,
  24. PA1<Alternate<PushPull>>,
  25. PA2<Alternate<PushPull>>,
  26. PA3<Alternate<PushPull>>,
  27. )
  28. {
  29. const REMAP: u8 = 0b00;
  30. const C1: bool = true;
  31. const C2: bool = true;
  32. const C3: bool = true;
  33. const C4: bool = true;
  34. type Channels = (Pwm<TIM2, C1>, Pwm<TIM2, C2>, Pwm<TIM2, C3>, Pwm<TIM2, C4>);
  35. }
  36. impl Pins<TIM2> for PA0<Alternate<PushPull>> {
  37. const REMAP: u8 = 0b00;
  38. const C1: bool = true;
  39. const C2: bool = false;
  40. const C3: bool = false;
  41. const C4: bool = false;
  42. type Channels = Pwm<TIM2, C1>;
  43. }
  44. impl Pins<TIM3>
  45. for (
  46. PA6<Alternate<PushPull>>,
  47. PA7<Alternate<PushPull>>,
  48. PB0<Alternate<PushPull>>,
  49. PB1<Alternate<PushPull>>,
  50. )
  51. {
  52. const REMAP: u8 = 0b00;
  53. const C1: bool = true;
  54. const C2: bool = true;
  55. const C3: bool = true;
  56. const C4: bool = true;
  57. type Channels = (Pwm<TIM3, C1>, Pwm<TIM3, C2>, Pwm<TIM3, C3>, Pwm<TIM3, C4>);
  58. }
  59. impl Pins<TIM3> for (PB0<Alternate<PushPull>>, PB1<Alternate<PushPull>>) {
  60. const REMAP: u8 = 0b00;
  61. const C1: bool = false;
  62. const C2: bool = false;
  63. const C3: bool = true;
  64. const C4: bool = true;
  65. type Channels = (Pwm<TIM3, C3>, Pwm<TIM3, C4>);
  66. }
  67. impl Pins<TIM4>
  68. for (
  69. PB6<Alternate<PushPull>>,
  70. PB7<Alternate<PushPull>>,
  71. PB8<Alternate<PushPull>>,
  72. PB9<Alternate<PushPull>>,
  73. )
  74. {
  75. const REMAP: u8 = 0b0;
  76. const C1: bool = true;
  77. const C2: bool = true;
  78. const C3: bool = true;
  79. const C4: bool = true;
  80. type Channels = (Pwm<TIM4, C1>, Pwm<TIM4, C2>, Pwm<TIM4, C3>, Pwm<TIM4, C4>);
  81. }
  82. pub trait PwmExt: Sized {
  83. fn pwm<PINS, T>(
  84. self,
  85. PINS,
  86. mapr: &mut MAPR,
  87. frequency: T,
  88. clocks: Clocks,
  89. apb: &mut APB1,
  90. ) -> PINS::Channels
  91. where
  92. PINS: Pins<Self>,
  93. T: Into<Hertz>;
  94. }
  95. impl PwmExt for TIM2 {
  96. fn pwm<PINS, T>(
  97. self,
  98. _pins: PINS,
  99. mapr: &mut MAPR,
  100. freq: T,
  101. clocks: Clocks,
  102. apb: &mut APB1,
  103. ) -> PINS::Channels
  104. where
  105. PINS: Pins<Self>,
  106. T: Into<Hertz>,
  107. {
  108. mapr.mapr()
  109. .modify(|_, w| unsafe { w.tim2_remap().bits(PINS::REMAP) });
  110. tim2(self, _pins, freq.into(), clocks, apb)
  111. }
  112. }
  113. impl PwmExt for TIM3 {
  114. fn pwm<PINS, T>(
  115. self,
  116. _pins: PINS,
  117. mapr: &mut MAPR,
  118. freq: T,
  119. clocks: Clocks,
  120. apb: &mut APB1,
  121. ) -> PINS::Channels
  122. where
  123. PINS: Pins<Self>,
  124. T: Into<Hertz>,
  125. {
  126. mapr.mapr()
  127. .modify(|_, w| unsafe { w.tim3_remap().bits(PINS::REMAP) });
  128. tim3(self, _pins, freq.into(), clocks, apb)
  129. }
  130. }
  131. impl PwmExt for TIM4 {
  132. fn pwm<PINS, T>(
  133. self,
  134. _pins: PINS,
  135. mapr: &mut MAPR,
  136. freq: T,
  137. clocks: Clocks,
  138. apb: &mut APB1,
  139. ) -> PINS::Channels
  140. where
  141. PINS: Pins<Self>,
  142. T: Into<Hertz>,
  143. {
  144. mapr.mapr()
  145. .modify(|_, w| w.tim4_remap().bit(PINS::REMAP == 1));
  146. tim4(self, _pins, freq.into(), clocks, apb)
  147. }
  148. }
  149. pub struct Pwm<TIM, CHANNEL> {
  150. _channel: PhantomData<CHANNEL>,
  151. _tim: PhantomData<TIM>,
  152. }
  153. pub struct C1;
  154. pub struct C2;
  155. pub struct C3;
  156. pub struct C4;
  157. macro_rules! hal {
  158. ($($TIMX:ident: ($timX:ident, $timXen:ident, $timXrst:ident),)+) => {
  159. $(
  160. fn $timX<PINS>(
  161. tim: $TIMX,
  162. _pins: PINS,
  163. freq: Hertz,
  164. clocks: Clocks,
  165. apb: &mut APB1,
  166. ) -> PINS::Channels
  167. where
  168. PINS: Pins<$TIMX>,
  169. {
  170. apb.enr().modify(|_, w| w.$timXen().set_bit());
  171. apb.rstr().modify(|_, w| w.$timXrst().set_bit());
  172. apb.rstr().modify(|_, w| w.$timXrst().clear_bit());
  173. if PINS::C1 {
  174. tim.ccmr1_output
  175. .modify(|_, w| w.oc1pe().set_bit().oc1m().bits(6));
  176. }
  177. if PINS::C2 {
  178. tim.ccmr1_output
  179. .modify(|_, w| w.oc2pe().set_bit().oc2m().bits(6));
  180. }
  181. if PINS::C3 {
  182. tim.ccmr2_output
  183. .modify(|_, w| w.oc3pe().set_bit().oc3m().bits(6));
  184. }
  185. if PINS::C4 {
  186. tim.ccmr2_output
  187. .modify(|_, w| w.oc4pe().set_bit().oc4m().bits(6));
  188. }
  189. let clk = clocks.pclk1().0 * if clocks.ppre1() == 1 { 1 } else { 2 };
  190. let freq = freq.0;
  191. let ticks = clk / freq;
  192. let psc = u16(ticks / (1 << 16)).unwrap();
  193. tim.psc.write(|w| w.psc().bits(psc));
  194. let arr = u16(ticks / u32(psc + 1)).unwrap();
  195. tim.arr.write(|w| w.arr().bits(arr));
  196. tim.cr1.write(|w| unsafe {
  197. w.cms()
  198. .bits(0b00)
  199. .dir()
  200. .clear_bit()
  201. .opm()
  202. .clear_bit()
  203. .cen()
  204. .set_bit()
  205. });
  206. unsafe { mem::uninitialized() }
  207. }
  208. impl hal::PwmPin for Pwm<$TIMX, C1> {
  209. type Duty = u16;
  210. fn disable(&mut self) {
  211. unsafe { bb::clear(&(*$TIMX::ptr()).ccer, 0) }
  212. }
  213. fn enable(&mut self) {
  214. unsafe { bb::set(&(*$TIMX::ptr()).ccer, 0) }
  215. }
  216. fn get_duty(&self) -> u16 {
  217. unsafe { (*$TIMX::ptr()).ccr1.read().ccr1().bits() }
  218. }
  219. fn get_max_duty(&self) -> u16 {
  220. unsafe { (*$TIMX::ptr()).arr.read().arr().bits() }
  221. }
  222. fn set_duty(&mut self, duty: u16) {
  223. unsafe { (*$TIMX::ptr()).ccr1.write(|w| w.ccr1().bits(duty)) }
  224. }
  225. }
  226. impl hal::PwmPin for Pwm<$TIMX, C2> {
  227. type Duty = u16;
  228. fn disable(&mut self) {
  229. unsafe { bb::clear(&(*$TIMX::ptr()).ccer, 4) }
  230. }
  231. fn enable(&mut self) {
  232. unsafe { bb::set(&(*$TIMX::ptr()).ccer, 4) }
  233. }
  234. fn get_duty(&self) -> u16 {
  235. unsafe { (*$TIMX::ptr()).ccr2.read().ccr2().bits() }
  236. }
  237. fn get_max_duty(&self) -> u16 {
  238. unsafe { (*$TIMX::ptr()).arr.read().arr().bits() }
  239. }
  240. fn set_duty(&mut self, duty: u16) {
  241. unsafe { (*$TIMX::ptr()).ccr2.write(|w| w.ccr2().bits(duty)) }
  242. }
  243. }
  244. impl hal::PwmPin for Pwm<$TIMX, C3> {
  245. type Duty = u16;
  246. fn disable(&mut self) {
  247. unsafe { bb::clear(&(*$TIMX::ptr()).ccer, 8) }
  248. }
  249. fn enable(&mut self) {
  250. unsafe { bb::set(&(*$TIMX::ptr()).ccer, 8) }
  251. }
  252. fn get_duty(&self) -> u16 {
  253. unsafe { (*$TIMX::ptr()).ccr3.read().ccr3().bits() }
  254. }
  255. fn get_max_duty(&self) -> u16 {
  256. unsafe { (*$TIMX::ptr()).arr.read().arr().bits() }
  257. }
  258. fn set_duty(&mut self, duty: u16) {
  259. unsafe { (*$TIMX::ptr()).ccr3.write(|w| w.ccr3().bits(duty)) }
  260. }
  261. }
  262. impl hal::PwmPin for Pwm<$TIMX, C4> {
  263. type Duty = u16;
  264. fn disable(&mut self) {
  265. unsafe { bb::clear(&(*$TIMX::ptr()).ccer, 12) }
  266. }
  267. fn enable(&mut self) {
  268. unsafe { bb::set(&(*$TIMX::ptr()).ccer, 12) }
  269. }
  270. fn get_duty(&self) -> u16 {
  271. unsafe { (*$TIMX::ptr()).ccr4.read().ccr4().bits() }
  272. }
  273. fn get_max_duty(&self) -> u16 {
  274. unsafe { (*$TIMX::ptr()).arr.read().arr().bits() }
  275. }
  276. fn set_duty(&mut self, duty: u16) {
  277. unsafe { (*$TIMX::ptr()).ccr4.write(|w| w.ccr4().bits(duty)) }
  278. }
  279. }
  280. )+
  281. }
  282. }
  283. hal! {
  284. TIM2: (tim2, tim2en, tim2rst),
  285. TIM3: (tim3, tim3en, tim3rst),
  286. TIM4: (tim4, tim4en, tim4rst),
  287. }