Teensy 4.1 core updated for 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.

354 lines
11KB

  1. #include "imxrt.h"
  2. #include "core_pins.h"
  3. #include "debug/printf.h"
  4. struct pwm_pin_info_struct {
  5. uint8_t type; // 0=no pwm, 1=flexpwm, 2=quad
  6. uint8_t module; // 0-3, 0-3
  7. uint8_t channel; // 0=X, 1=A, 2=B
  8. uint8_t muxval; //
  9. };
  10. uint8_t analog_write_res = 8;
  11. #define M(a, b) ((((a) - 1) << 4) | (b))
  12. #if defined(__IMXRT1062__)
  13. const struct pwm_pin_info_struct pwm_pin_info[] = {
  14. {1, M(1, 1), 0, 4}, // FlexPWM1_1_X 0 // AD_B0_03
  15. {1, M(1, 0), 0, 4}, // FlexPWM1_0_X 1 // AD_B0_02
  16. {1, M(4, 2), 1, 1}, // FlexPWM4_2_A 2 // EMC_04
  17. {1, M(4, 2), 2, 1}, // FlexPWM4_2_B 3 // EMC_05
  18. {1, M(2, 0), 1, 1}, // FlexPWM2_0_A 4 // EMC_06
  19. {1, M(2, 1), 1, 1}, // FlexPWM2_1_A 5 // EMC_08
  20. {1, M(2, 2), 1, 2}, // FlexPWM2_2_A 6 // B0_10
  21. {1, M(1, 3), 2, 6}, // FlexPWM1_3_B 7 // B1_01
  22. {1, M(1, 3), 1, 6}, // FlexPWM1_3_A 8 // B1_00
  23. {1, M(2, 2), 2, 2}, // FlexPWM2_2_B 9 // B0_11
  24. {2, M(1, 0), 0, 1}, // QuadTimer1_0 10 // B0_00
  25. {2, M(1, 2), 0, 1}, // QuadTimer1_2 11 // B0_02
  26. {2, M(1, 1), 0, 1}, // QuadTimer1_1 12 // B0_01
  27. {2, M(2, 0), 0, 1}, // QuadTimer2_0 13 // B0_03
  28. {2, M(3, 2), 0, 1}, // QuadTimer3_2 14 // AD_B1_02
  29. {2, M(3, 3), 0, 1}, // QuadTimer3_3 15 // AD_B1_03
  30. {0, M(1, 0), 0, 0},
  31. {0, M(1, 0), 0, 0},
  32. {2, M(3, 1), 0, 1}, // QuadTimer3_1 18 // AD_B1_01
  33. {2, M(3, 0), 0, 1}, // QuadTimer3_0 19 // AD_B1_00
  34. {0, M(1, 0), 0, 0},
  35. {0, M(1, 0), 0, 0},
  36. {1, M(4, 0), 1, 1}, // FlexPWM4_0_A 22 // AD_B1_08
  37. {1, M(4, 1), 1, 1}, // FlexPWM4_1_A 23 // AD_B1_09
  38. {1, M(1, 2), 0, 4}, // FlexPWM1_2_X 24 // AD_B0_12
  39. {1, M(1, 3), 0, 4}, // FlexPWM1_3_X 25 // AD_B0_13
  40. {0, M(1, 0), 0, 0},
  41. {0, M(1, 0), 0, 0},
  42. {1, M(3, 1), 2, 1}, // FlexPWM3_1_B 28 // EMC_32
  43. {1, M(3, 1), 1, 1}, // FlexPWM3_1_A 29 // EMC_31
  44. {0, M(1, 0), 0, 0},
  45. {0, M(1, 0), 0, 0},
  46. {0, M(1, 0), 0, 0},
  47. {1, M(2, 0), 2, 1}, // FlexPWM2_0_B 33 // EMC_07
  48. };
  49. #elif defined(__IMXRT1052__)
  50. const struct pwm_pin_info_struct pwm_pin_info[] = {
  51. {1, M(1, 1), 0, 4}, // FlexPWM1_1_X 0 // AD_B0_03
  52. {1, M(1, 0), 0, 4}, // FlexPWM1_0_X 1 // AD_B0_02
  53. {1, M(4, 2), 1, 1}, // FlexPWM4_2_A 2 // EMC_04
  54. {1, M(4, 2), 2, 1}, // FlexPWM4_2_B 3 // EMC_05
  55. {1, M(2, 0), 1, 1}, // FlexPWM2_0_A 4 // EMC_06
  56. {1, M(2, 0), 2, 1}, // FlexPWM2_0_B 5 // EMC_07
  57. {1, M(1, 3), 2, 6}, // FlexPWM1_3_B 6 // B1_01
  58. {1, M(1, 3), 1, 6}, // FlexPWM1_3_A 7 // B1_00
  59. {1, M(2, 2), 1, 2}, // FlexPWM2_2_A 8 // B0_10
  60. {1, M(2, 2), 2, 2}, // FlexPWM2_2_B 9 // B0_11
  61. {2, M(1, 0), 0, 1}, // QuadTimer1_0 10 // B0_00
  62. {2, M(1, 2), 0, 1}, // QuadTimer1_2 11 // B0_02
  63. {2, M(1, 1), 0, 1}, // QuadTimer1_1 12 // B0_01
  64. {2, M(2, 0), 0, 1}, // QuadTimer2_0 13 // B0_03
  65. {2, M(3, 2), 0, 1}, // QuadTimer3_2 14 // AD_B1_02
  66. {2, M(3, 3), 0, 1}, // QuadTimer3_3 15 // AD_B1_03
  67. {0, M(1, 0), 0, 0},
  68. {0, M(1, 0), 0, 0},
  69. {2, M(3, 1), 0, 1}, // QuadTimer3_1 18 // AD_B1_01
  70. {2, M(3, 0), 0, 1}, // QuadTimer3_0 19 // AD_B1_00
  71. {0, M(1, 0), 0, 0},
  72. {0, M(1, 0), 0, 0},
  73. {1, M(4, 0), 1, 1}, // FlexPWM4_0_A 22 // AD_B1_08
  74. {1, M(4, 1), 1, 1}, // FlexPWM4_1_A 23 // AD_B1_09
  75. {1, M(1, 2), 0, 4}, // FlexPWM1_2_X 24 // AD_B0_12
  76. {1, M(1, 3), 0, 4}, // FlexPWM1_3_X 25 // AD_B0_13
  77. {0, M(1, 0), 0, 0},
  78. {0, M(1, 0), 0, 0},
  79. {1, M(3, 1), 2, 1}, // FlexPWM3_1_B 28 // EMC_32
  80. {1, M(3, 1), 1, 1}, // FlexPWM3_1_A 29 // EMC_31
  81. {1, M(1, 0), 2, 1}, // FlexPWM1_0_B 30 // EMC_24
  82. {1, M(1, 0), 1, 1}, // FlexPWM1_0_A 31 // EMC_23
  83. {0, M(1, 0), 0, 0},
  84. {1, M(2, 1), 1, 1}, // FlexPWM2_1_A 33 // EMC_08
  85. };
  86. #endif // __IMXRT1052__
  87. void flexpwmWrite(IMXRT_FLEXPWM_t *p, unsigned int submodule, uint8_t channel, uint16_t val)
  88. {
  89. uint16_t mask = 1 << submodule;
  90. uint32_t modulo = p->SM[submodule].VAL1;
  91. uint32_t cval = ((uint32_t)val * (modulo + 1)) >> analog_write_res;
  92. if (cval > modulo) cval = modulo; // TODO: is this check correct?
  93. //printf("flexpwmWrite, p=%08lX, sm=%d, ch=%c, cval=%ld\n",
  94. //(uint32_t)p, submodule, channel == 0 ? 'X' : (channel == 1 ? 'A' : 'B'), cval);
  95. p->MCTRL |= FLEXPWM_MCTRL_CLDOK(mask);
  96. switch (channel) {
  97. case 0: // X
  98. p->SM[submodule].VAL0 = modulo - cval;
  99. p->OUTEN |= FLEXPWM_OUTEN_PWMX_EN(mask);
  100. //printf(" write channel X\n");
  101. break;
  102. case 1: // A
  103. p->SM[submodule].VAL3 = cval;
  104. p->OUTEN |= FLEXPWM_OUTEN_PWMA_EN(mask);
  105. //printf(" write channel A\n");
  106. break;
  107. case 2: // B
  108. p->SM[submodule].VAL5 = cval;
  109. p->OUTEN |= FLEXPWM_OUTEN_PWMB_EN(mask);
  110. //printf(" write channel B\n");
  111. }
  112. p->MCTRL |= FLEXPWM_MCTRL_LDOK(mask);
  113. }
  114. void flexpwmFrequency(IMXRT_FLEXPWM_t *p, unsigned int submodule, uint8_t channel, float frequency)
  115. {
  116. uint16_t mask = 1 << submodule;
  117. uint32_t olddiv = p->SM[submodule].VAL1;
  118. uint32_t newdiv = (uint32_t)((float)F_BUS_ACTUAL / frequency + 0.5);
  119. uint32_t prescale = 0;
  120. //printf(" div=%lu\n", newdiv);
  121. while (newdiv > 65535 && prescale < 7) {
  122. newdiv = newdiv >> 1;
  123. prescale = prescale + 1;
  124. }
  125. if (newdiv > 65535) {
  126. newdiv = 65535;
  127. } else if (newdiv < 2) {
  128. newdiv = 2;
  129. }
  130. //printf(" div=%lu, scale=%lu\n", newdiv, prescale);
  131. p->MCTRL |= FLEXPWM_MCTRL_CLDOK(mask);
  132. p->SM[submodule].CTRL = FLEXPWM_SMCTRL_FULL | FLEXPWM_SMCTRL_PRSC(prescale);
  133. p->SM[submodule].VAL1 = newdiv - 1;
  134. p->SM[submodule].VAL0 = (p->SM[submodule].VAL0 * newdiv) / olddiv;
  135. p->SM[submodule].VAL3 = (p->SM[submodule].VAL3 * newdiv) / olddiv;
  136. p->SM[submodule].VAL5 = (p->SM[submodule].VAL5 * newdiv) / olddiv;
  137. p->MCTRL |= FLEXPWM_MCTRL_LDOK(mask);
  138. }
  139. void quadtimerWrite(IMXRT_TMR_t *p, unsigned int submodule, uint16_t val)
  140. {
  141. uint32_t modulo = 65537 - p->CH[submodule].LOAD + p->CH[submodule].CMPLD1;
  142. uint32_t high = ((uint32_t)val * (modulo - 1)) >> analog_write_res;
  143. if (high >= modulo) high = modulo - 1; // TODO: is this check correct?
  144. //printf(" modulo=%lu\n", modulo);
  145. //printf(" high=%lu\n", high);
  146. uint32_t low = modulo - high; // TODO: low must never be 0 or 1 - can it be??
  147. //printf(" low=%lu\n", low);
  148. p->CH[submodule].LOAD = 65537 - low;
  149. p->CH[submodule].CMPLD1 = high;
  150. }
  151. void quadtimerFrequency(IMXRT_TMR_t *p, unsigned int submodule, float frequency)
  152. {
  153. uint32_t newdiv = (uint32_t)((float)F_BUS_ACTUAL / frequency + 0.5);
  154. uint32_t prescale = 0;
  155. //printf(" div=%lu\n", newdiv);
  156. while (newdiv > 65534 && prescale < 7) {
  157. newdiv = newdiv >> 1;
  158. prescale = prescale + 1;
  159. }
  160. if (newdiv > 65534) {
  161. newdiv = 65534;
  162. } else if (newdiv < 2) {
  163. newdiv = 2;
  164. }
  165. //printf(" div=%lu, scale=%lu\n", newdiv, prescale);
  166. uint32_t oldhigh = p->CH[submodule].CMPLD1;
  167. uint32_t oldlow = 65537 - p->CH[submodule].LOAD;
  168. uint32_t high = (oldhigh * newdiv) / (oldhigh + oldlow);
  169. // TODO: low must never be less than 2 - can it happen with this?
  170. uint32_t low = newdiv - high;
  171. //printf(" high=%lu, low=%lu\n", high, low);
  172. p->CH[submodule].LOAD = 65537 - low;
  173. p->CH[submodule].CMPLD1 = high;
  174. p->CH[submodule].CTRL = TMR_CTRL_CM(1) | TMR_CTRL_PCS(8 + prescale) |
  175. TMR_CTRL_LENGTH | TMR_CTRL_OUTMODE(6);
  176. }
  177. void analogWrite(uint8_t pin, int val)
  178. {
  179. const struct pwm_pin_info_struct *info;
  180. if (pin >= CORE_NUM_DIGITAL) return;
  181. //printf("analogWrite, pin %d, val %d\n", pin, val);
  182. info = pwm_pin_info + pin;
  183. if (info->type == 1) {
  184. // FlexPWM pin
  185. IMXRT_FLEXPWM_t *flexpwm;
  186. switch ((info->module >> 4) & 3) {
  187. case 0: flexpwm = &IMXRT_FLEXPWM1; break;
  188. case 1: flexpwm = &IMXRT_FLEXPWM2; break;
  189. case 2: flexpwm = &IMXRT_FLEXPWM3; break;
  190. default: flexpwm = &IMXRT_FLEXPWM4;
  191. }
  192. flexpwmWrite(flexpwm, info->module & 0x03, info->channel, val);
  193. } else if (info->type == 2) {
  194. // QuadTimer pin
  195. IMXRT_TMR_t *qtimer;
  196. switch ((info->module >> 4) & 3) {
  197. case 0: qtimer = &IMXRT_TMR1; break;
  198. case 1: qtimer = &IMXRT_TMR2; break;
  199. case 2: qtimer = &IMXRT_TMR3; break;
  200. default: qtimer = &IMXRT_TMR4;
  201. }
  202. quadtimerWrite(qtimer, info->module & 0x03, val);
  203. } else {
  204. return;
  205. }
  206. *(portConfigRegister(pin)) = info->muxval;
  207. // TODO: pad config register
  208. }
  209. void analogWriteFrequency(uint8_t pin, float frequency)
  210. {
  211. const struct pwm_pin_info_struct *info;
  212. if (pin >= CORE_NUM_DIGITAL) return;
  213. //printf("analogWriteFrequency, pin %d, freq %d\n", pin, (int)frequency);
  214. info = pwm_pin_info + pin;
  215. if (info->type == 1) {
  216. // FlexPWM pin
  217. IMXRT_FLEXPWM_t *flexpwm;
  218. switch ((info->module >> 4) & 3) {
  219. case 0: flexpwm = &IMXRT_FLEXPWM1; break;
  220. case 1: flexpwm = &IMXRT_FLEXPWM2; break;
  221. case 2: flexpwm = &IMXRT_FLEXPWM3; break;
  222. default: flexpwm = &IMXRT_FLEXPWM4;
  223. }
  224. flexpwmFrequency(flexpwm, info->module & 0x03, info->channel, frequency);
  225. } else if (info->type == 2) {
  226. // QuadTimer pin
  227. IMXRT_TMR_t *qtimer;
  228. switch ((info->module >> 4) & 3) {
  229. case 0: qtimer = &IMXRT_TMR1; break;
  230. case 1: qtimer = &IMXRT_TMR2; break;
  231. case 2: qtimer = &IMXRT_TMR3; break;
  232. default: qtimer = &IMXRT_TMR4;
  233. }
  234. quadtimerFrequency(qtimer, info->module & 0x03, frequency);
  235. }
  236. }
  237. void flexpwm_init(IMXRT_FLEXPWM_t *p)
  238. {
  239. int i;
  240. p->FCTRL0 = FLEXPWM_FCTRL0_FLVL(15); // logic high = fault
  241. p->FSTS0 = 0x000F; // clear fault status
  242. p->FFILT0 = 0;
  243. p->MCTRL |= FLEXPWM_MCTRL_CLDOK(15);
  244. for (i=0; i < 4; i++) {
  245. p->SM[i].CTRL2 = FLEXPWM_SMCTRL2_INDEP | FLEXPWM_SMCTRL2_WAITEN
  246. | FLEXPWM_SMCTRL2_DBGEN;
  247. p->SM[i].CTRL = FLEXPWM_SMCTRL_FULL;
  248. p->SM[i].OCTRL = 0;
  249. p->SM[i].DTCNT0 = 0;
  250. p->SM[i].INIT = 0;
  251. p->SM[i].VAL0 = 0;
  252. p->SM[i].VAL1 = 33464;
  253. p->SM[i].VAL2 = 0;
  254. p->SM[i].VAL3 = 0;
  255. p->SM[i].VAL4 = 0;
  256. p->SM[i].VAL5 = 0;
  257. }
  258. p->MCTRL |= FLEXPWM_MCTRL_LDOK(15);
  259. p->MCTRL |= FLEXPWM_MCTRL_RUN(15);
  260. }
  261. void quadtimer_init(IMXRT_TMR_t *p)
  262. {
  263. int i;
  264. for (i=0; i < 4; i++) {
  265. p->CH[i].CTRL = 0; // stop timer
  266. p->CH[i].CNTR = 0;
  267. p->CH[i].SCTRL = TMR_SCTRL_OEN | TMR_SCTRL_OPS | TMR_SCTRL_VAL | TMR_SCTRL_FORCE;
  268. p->CH[i].CSCTRL = TMR_CSCTRL_CL1(1) | TMR_CSCTRL_ALT_LOAD;
  269. // COMP must be less than LOAD - otherwise output is always low
  270. p->CH[i].LOAD = 24000; // low time (65537 - x) -
  271. p->CH[i].COMP1 = 0; // high time (0 = always low, max = LOAD-1)
  272. p->CH[i].CMPLD1 = 0;
  273. p->CH[i].CTRL = TMR_CTRL_CM(1) | TMR_CTRL_PCS(8) |
  274. TMR_CTRL_LENGTH | TMR_CTRL_OUTMODE(6);
  275. }
  276. }
  277. void pwm_init(void)
  278. {
  279. //printf("pwm init\n");
  280. CCM_CCGR4 |= CCM_CCGR4_PWM1(CCM_CCGR_ON) | CCM_CCGR4_PWM2(CCM_CCGR_ON) |
  281. CCM_CCGR4_PWM3(CCM_CCGR_ON) | CCM_CCGR4_PWM4(CCM_CCGR_ON);
  282. CCM_CCGR6 |= CCM_CCGR6_QTIMER1(CCM_CCGR_ON) | CCM_CCGR6_QTIMER2(CCM_CCGR_ON) |
  283. CCM_CCGR6_QTIMER3(CCM_CCGR_ON) | CCM_CCGR6_QTIMER4(CCM_CCGR_ON);
  284. flexpwm_init(&IMXRT_FLEXPWM1);
  285. flexpwm_init(&IMXRT_FLEXPWM2);
  286. flexpwm_init(&IMXRT_FLEXPWM3);
  287. flexpwm_init(&IMXRT_FLEXPWM4);
  288. quadtimer_init(&IMXRT_TMR1);
  289. quadtimer_init(&IMXRT_TMR2);
  290. quadtimer_init(&IMXRT_TMR3);
  291. }
  292. void xbar_connect(unsigned int input, unsigned int output)
  293. {
  294. if (input >= 88) return;
  295. if (output >= 132) return;
  296. #if 1
  297. volatile uint16_t *xbar = &XBARA1_SEL0 + (output / 2);
  298. uint16_t val = *xbar;
  299. if (!(output & 1)) {
  300. val = (val & 0xFF00) | input;
  301. } else {
  302. val = (val & 0x00FF) | (input << 8);
  303. }
  304. *xbar = val;
  305. #else
  306. // does not work, seems 8 bit access is not allowed
  307. volatile uint8_t *xbar = (volatile uint8_t *)XBARA1_SEL0;
  308. xbar[output] = input;
  309. #endif
  310. }
  311. uint32_t analogWriteRes(uint32_t bits)
  312. {
  313. uint32_t prior;
  314. if (bits < 1) {
  315. bits = 1;
  316. } else if (bits > 16) {
  317. bits = 16;
  318. }
  319. prior = analog_write_res;
  320. analog_write_res = bits;
  321. return prior;
  322. }