PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

272 lines
7.8KB

  1. #include "SnoozeSleepPWM.h"
  2. #include "defines.h"
  3. /*
  4. Set the FTM | TPM back to 2 MHz operation. This happens right before
  5. we switch to 2 MHz then put the processor to sleep.
  6. */
  7. void SnoozeSleepPWM::enableDriver( void ) {
  8. #if defined(KINETISL)
  9. SIM_SOPT2_VALUE = SIM_SOPT2;
  10. SIM_SOPT2 |= SIM_SOPT2_TPMSRC( 3 );
  11. #endif
  12. *FTMx_CNT = 0;
  13. *FTMx_MOD = DEFAULT_FTM_02_MOD;
  14. while ( *FTMx_MOD != DEFAULT_FTM_02_MOD );
  15. *FTMx_SC = FTM_SC_CLKS( 1 ) | FTM_SC_PS( DEFAULT_FTM_02_PRESCALE );
  16. #if defined(__MK66FX1M0__) && defined(TPM1_CH0_PIN) || defined(TPM1_CH1_PIN)
  17. TPMx_CxV_VALUE = *TPMx_CxV;
  18. *TPMx_CxV = cval;
  19. #else
  20. FTMx_CxV_VALUE = *FTMx_CxV;
  21. *FTMx_CxV = cval;
  22. #endif
  23. }
  24. /*
  25. Not used, but must be defined. This would be called after we are
  26. at F_CPU processor speed by then the glitch to the pwm waveform
  27. is to critical.
  28. */
  29. void SnoozeSleepPWM::disableDriver( void ) {
  30. }
  31. /*
  32. Set the FTM | TPM back to F_CPU operation. This gets called from
  33. whatever driver interrupt wakes the processor.
  34. */
  35. void SnoozeSleepPWM::clearIsrFlags( void ) {
  36. #if defined(KINETISL)
  37. SIM_SOPT2 = SIM_SOPT2_VALUE;
  38. #endif
  39. *FTMx_CNT = 0;
  40. *FTMx_MOD = DEFAULT_FTM_MOD;
  41. while ( *FTMx_MOD != DEFAULT_FTM_MOD );
  42. *FTMx_SC = FTMx_SC_VALUE;
  43. *FTMx_CxV = rval;
  44. }
  45. /*
  46. Saves all the FTM | TPM register values at F_CPU and also saves and reconfigures
  47. these values for 2 MHz operation while in sleep.
  48. */
  49. void SnoozeSleepPWM::configure( uint8_t pin, int val, uint8_t res ) {
  50. isUsed = true;
  51. pwm_pin = pin;
  52. pwm_value = val;
  53. pwm_resloution = res;
  54. rval = ( ( uint32_t )val * ( uint32_t )( DEFAULT_FTM_MOD + 1 ) ) >> pwm_resloution;
  55. cval = ( ( uint32_t )val * ( uint32_t )( DEFAULT_FTM_02_MOD + 1 ) ) >> pwm_resloution;
  56. FTMx_MOD_VALUE = DEFAULT_FTM_MOD;
  57. switch ( pwm_pin ) {
  58. #ifdef FTM0_CH0_PIN
  59. case FTM0_CH0_PIN: // PTC1, FTM0_CH0
  60. FTMx_CxV = &FTM0_C0V;
  61. FTMx_SC_VALUE = FTM0_SC;
  62. FTMx_MOD = &FTM0_MOD;
  63. FTMx_SC = &FTM0_SC;
  64. FTMx_CNT = &FTM0_CNT;
  65. break;
  66. #endif
  67. #ifdef FTM0_CH1_PIN
  68. case FTM0_CH1_PIN: // PTC2, FTM0_CH1
  69. FTMx_CxV = &FTM0_C1V;
  70. FTMx_SC_VALUE = FTM0_SC;
  71. FTMx_MOD = &FTM0_MOD;
  72. FTMx_SC = &FTM0_SC;
  73. FTMx_CNT = &FTM0_CNT;
  74. break;
  75. #endif
  76. #ifdef FTM0_CH2_PIN
  77. case FTM0_CH2_PIN: // PTC3, FTM0_CH2
  78. FTMx_CxV = &FTM0_C2V;
  79. FTMx_SC_VALUE = FTM0_SC;
  80. FTMx_MOD = &FTM0_MOD;
  81. FTMx_SC = &FTM0_SC;
  82. FTMx_CNT = &FTM0_CNT;
  83. break;
  84. #endif
  85. #ifdef FTM0_CH3_PIN
  86. case FTM0_CH3_PIN: // PTC4, FTM0_CH3
  87. FTMx_CxV = &FTM0_C3V;
  88. FTMx_SC_VALUE = FTM0_SC;
  89. FTMx_MOD = &FTM0_MOD;
  90. FTMx_SC = &FTM0_SC;
  91. FTMx_CNT = &FTM0_CNT;
  92. break;
  93. #endif
  94. #ifdef FTM0_CH4_PIN
  95. case FTM0_CH4_PIN: // PTD4, FTM0_CH4
  96. FTMx_CxV = &FTM0_C4V;
  97. FTMx_SC_VALUE = FTM0_SC;
  98. FTMx_MOD = &FTM0_MOD;
  99. FTMx_SC = &FTM0_SC;
  100. FTMx_CNT = &FTM0_CNT;
  101. break;
  102. #endif
  103. #ifdef FTM0_CH5_PIN
  104. case FTM0_CH5_PIN: // PTD5, FTM0_CH5
  105. FTMx_CxV = &FTM0_C5V;
  106. FTMx_SC_VALUE = FTM0_SC;
  107. FTMx_MOD = &FTM0_MOD;
  108. FTMx_SC = &FTM0_SC;
  109. FTMx_CNT = &FTM0_CNT;
  110. break;
  111. #endif
  112. #ifdef FTM0_CH6_PIN
  113. case FTM0_CH6_PIN: // PTD6, FTM0_CH6
  114. FTMx_CxV = &FTM0_C6V;
  115. FTMx_SC_VALUE = FTM0_SC;
  116. FTMx_MOD = &FTM0_MOD;
  117. FTMx_SC = &FTM0_SC;
  118. FTMx_CNT = &FTM0_CNT;
  119. break;
  120. #endif
  121. #ifdef FTM0_CH7_PIN
  122. case FTM0_CH7_PIN: // PTD7, FTM0_CH7
  123. FTMx_CxV = &FTM0_C7V;
  124. FTMx_SC_VALUE = FTM0_SC;
  125. FTMx_MOD = &FTM0_MOD;
  126. FTMx_SC = &FTM0_SC;
  127. FTMx_CNT = &FTM0_CNT;
  128. break;
  129. #endif
  130. #ifdef FTM1_CH0_PIN
  131. case FTM1_CH0_PIN: // PTA12, FTM1_CH0
  132. //used
  133. FTMx_CxV = &FTM1_C0V;
  134. FTMx_SC_VALUE = FTM1_SC;
  135. FTMx_SC = &FTM1_SC;
  136. FTMx_MOD = &FTM1_MOD;
  137. FTMx_CNT = &FTM1_CNT;
  138. break;
  139. #endif
  140. #ifdef FTM1_CH1_PIN
  141. case FTM1_CH1_PIN: // PTA13, FTM1_CH1
  142. FTMx_CxV = &FTM1_C1V;
  143. FTMx_SC_VALUE = FTM1_SC;
  144. FTMx_MOD = &FTM1_MOD;
  145. FTMx_SC = &FTM1_SC;
  146. FTMx_CNT = &FTM1_CNT;
  147. break;
  148. #endif
  149. #ifdef FTM2_CH0_PIN
  150. case FTM2_CH0_PIN: // PTB18, FTM2_CH0
  151. FTMx_CxV = &FTM2_C0V;
  152. FTMx_SC_VALUE = FTM2_SC;
  153. FTMx_MOD = &FTM2_MOD;
  154. FTMx_SC = &FTM2_SC;
  155. FTMx_CNT = &FTM2_CNT;
  156. break;
  157. #endif
  158. #ifdef FTM2_CH1_PIN
  159. case FTM2_CH1_PIN: // PTB19, FTM1_CH1
  160. FTMx_CxV = &FTM2_C1V;
  161. FTMx_SC_VALUE = FTM2_SC;
  162. FTMx_MOD = &FTM2_MOD;
  163. FTMx_SC = &FTM2_SC;
  164. FTMx_CNT = &FTM2_CNT;
  165. break;
  166. #endif
  167. #ifdef FTM3_CH0_PIN
  168. case FTM3_CH0_PIN:
  169. FTMx_CxV = &FTM3_C0V;
  170. FTMx_SC_VALUE = FTM3_SC;
  171. FTMx_MOD = &FTM3_MOD;
  172. FTMx_SC = &FTM3_SC;
  173. FTMx_CNT = &FTM3_CNT;
  174. break;
  175. #endif
  176. #ifdef FTM3_CH1_PIN
  177. case FTM3_CH1_PIN:
  178. FTMx_CxV = &FTM3_C1V;
  179. FTMx_SC_VALUE = FTM3_SC;
  180. FTMx_MOD = &FTM3_MOD;
  181. FTMx_SC = &FTM3_SC;
  182. FTMx_CNT = &FTM3_CNT;
  183. break;
  184. #endif
  185. #ifdef FTM3_CH2_PIN
  186. case FTM3_CH2_PIN:
  187. FTMx_CxV = &FTM3_C2V;
  188. FTMx_SC_VALUE = FTM3_SC;
  189. FTMx_MOD = &FTM3_MOD;
  190. FTMx_SC = &FTM3_SC;
  191. FTMx_CNT = &FTM3_CNT;
  192. break;
  193. #endif
  194. #ifdef FTM3_CH3_PIN
  195. case FTM3_CH3_PIN:
  196. FTMx_CxV = &FTM3_C3V;
  197. FTMx_SC_VALUE = FTM3_SC;
  198. FTMx_MOD = &FTM3_MOD;
  199. FTMx_SC = &FTM3_SC;
  200. FTMx_CNT = &FTM3_CNT;
  201. break;
  202. #endif
  203. #ifdef FTM3_CH4_PIN
  204. case FTM3_CH4_PIN:
  205. FTMx_CxV = &FTM3_C4V;
  206. FTMx_SC_VALUE = FTM3_SC;
  207. FTMx_MOD = &FTM3_MOD;
  208. FTMx_SC = &FTM3_SC;
  209. FTMx_CNT = &FTM3_CNT;
  210. break;
  211. #endif
  212. #ifdef FTM3_CH5_PIN
  213. case FTM3_CH5_PIN:
  214. FTMx_CxV = &FTM3_C5V;
  215. FTMx_SC_VALUE = FTM3_SC;
  216. FTMx_MOD = &FTM3_MOD;
  217. FTMx_SC = &FTM3_SC;
  218. FTMx_CNT = &FTM3_CNT;
  219. break;
  220. #endif
  221. #ifdef FTM3_CH6_PIN
  222. case FTM3_CH6_PIN:
  223. FTMx_CxV = &FTM3_C6V;
  224. FTMx_SC_VALUE = FTM3_SC;
  225. FTMx_MOD = &FTM3_MOD;
  226. FTMx_SC = &FTM3_SC;
  227. FTMx_CNT = &FTM3_CNT;
  228. break;
  229. #endif
  230. #ifdef FTM3_CH7_PIN
  231. case FTM3_CH7_PIN:
  232. FTMx_CxV = &FTM3_C7V;
  233. FTMx_SC_VALUE = FTM3_SC;
  234. FTMx_MOD = &FTM3_MOD;
  235. FTMx_SC = &FTM3_SC;
  236. FTMx_CNT = &FTM3_CNT;
  237. break;
  238. #endif
  239. #ifdef TPM1_CH0_PIN
  240. case TPM1_CH0_PIN:
  241. // not implemented
  242. TPMx_CxV = &TPM1_C0V;
  243. FTMx_SC_VALUE = TPM1_SC;
  244. FTMx_MOD = &TPM1_MOD;
  245. FTMx_SC = &TPM1_SC;
  246. FTMx_CNT = &FTM1_CNT;
  247. break;
  248. #endif
  249. #ifdef TPM1_CH1_PIN
  250. case TPM1_CH1_PIN:
  251. // not implemented
  252. TPMx_CxV = &TPM1_C1V;
  253. FTMx_SC_VALUE = TPM1_SC;
  254. FTMx_MOD = &TPM1_MOD;
  255. FTMx_SC = &TPM1_SC;
  256. FTMx_CNT = &TTM1_CNT;
  257. break;
  258. #endif
  259. default:
  260. break;
  261. }
  262. }