Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

321 Zeilen
9.7KB

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