Teensy 4.1 core updated for C++20
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

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