My Marlin configs for Fabrikator Mini and CTC i3 Pro B
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.

LPC1768_PWM.cpp 24KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (C) 2017 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
  4. *
  5. * Based on Sprinter and grbl.
  6. * Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. /**
  23. * The class Servo uses the PWM class to implement its functions
  24. *
  25. * All PWMs use the same repetition rate - 20mS because that's the normal servo rate
  26. */
  27. /**
  28. * This is a hybrid system.
  29. *
  30. * The PWM1 module is used to directly control the Servo 0, 1 & 3 pins. This keeps
  31. * the pulse width jitter to under a microsecond.
  32. *
  33. * For all other pins the PWM1 module is used to generate interrupts. The ISR
  34. * routine does the actual setting/clearing of pins. The upside is that any pin can
  35. * have a PWM channel assigned to it. The downside is that there is more pulse width
  36. * jitter. The jitter depends on what else is happening in the system and what ISRs
  37. * prempt the PWM ISR. Writing to the SD card can add 20 microseconds to the pulse
  38. * width.
  39. */
  40. /**
  41. * The data structures are setup to minimize the computation done by the ISR which
  42. * minimizes ISR execution time. Execution times are 2.2 - 3.7 microseconds.
  43. *
  44. * Two tables are used. active_table is used by the ISR. Changes to the table are
  45. * are done by copying the active_table into the work_table, updating the work_table
  46. * and then swapping the two tables. Swapping is done by manipulating pointers.
  47. *
  48. * Immediately after the swap the ISR uses the work_table until the start of the
  49. * next 20mS cycle. During this transition the "work_table" is actually the table
  50. * that was being used before the swap. The "active_table" contains the data that
  51. * will start being used at the start of the next 20mS period. This keeps the pins
  52. * well behaved during the transition.
  53. *
  54. * The ISR's priority is set to the maximum otherwise other ISRs can cause considerable
  55. * jitter in the PWM high time.
  56. *
  57. * See the end of this file for details on the hardware/firmware interaction
  58. */
  59. #ifdef TARGET_LPC1768
  60. #include <lpc17xx_pinsel.h>
  61. #include "LPC1768_PWM.h"
  62. #include "arduino.h"
  63. #define NUM_PWMS 6
  64. typedef struct { // holds all data needed to control/init one of the PWM channels
  65. uint8_t sequence; // 0: available slot, 1 - 6: PWM channel assigned to that slot
  66. pin_t pin;
  67. uint16_t PWM_mask; // MASK TO CHECK/WRITE THE IR REGISTER
  68. volatile uint32_t* set_register;
  69. volatile uint32_t* clr_register;
  70. uint32_t write_mask; // USED BY SET/CLEAR COMMANDS
  71. uint32_t microseconds; // value written to MR register
  72. uint32_t min; // lower value limit checked by WRITE routine before writing to the MR register
  73. uint32_t max; // upper value limit checked by WRITE routine before writing to the MR register
  74. bool PWM_flag; // 0 - USED BY sERVO, 1 - USED BY ANALOGWRITE
  75. uint8_t servo_index; // 0 - MAX_SERVO -1 : servo index, 0xFF : PWM channel
  76. bool active_flag; // THIS TABLE ENTRY IS ACTIVELY TOGGLING A PIN
  77. uint8_t assigned_MR; // Which MR (1-6) is used by this logical channel
  78. uint32_t PCR_bit; // PCR register bit to enable PWM1 control of this pin
  79. uint32_t PINSEL3_bits; // PINSEL3 register bits to set pin mode to PWM1 control
  80. } PWM_map;
  81. #define MICRO_MAX 0xffffffff
  82. #define PWM_MAP_INIT_ROW {0, 0xff, 0, 0, 0, 0, MICRO_MAX, 0, 0, 0, 0, 0, 0, 0, 0}
  83. #define PWM_MAP_INIT {PWM_MAP_INIT_ROW,\
  84. PWM_MAP_INIT_ROW,\
  85. PWM_MAP_INIT_ROW,\
  86. PWM_MAP_INIT_ROW,\
  87. PWM_MAP_INIT_ROW,\
  88. PWM_MAP_INIT_ROW,\
  89. };
  90. PWM_map PWM1_map_A[NUM_PWMS] = PWM_MAP_INIT;
  91. PWM_map PWM1_map_B[NUM_PWMS] = PWM_MAP_INIT;
  92. PWM_map *active_table = PWM1_map_A;
  93. PWM_map *work_table = PWM1_map_B;
  94. PWM_map *ISR_table;
  95. #define IR_BIT(p) (p >= 0 && p <= 3 ? p : p + 4 )
  96. #define COPY_ACTIVE_TABLE for (uint8_t i = 0; i < 6 ; i++) work_table[i] = active_table[i]
  97. #define PIN_IS_INVERTED(p) 0 // place holder in case inverting PWM output is offered
  98. /**
  99. * Prescale register and MR0 register values
  100. *
  101. * 100MHz PCLK 50MHz PCLK 25MHz PCLK 12.5MHz PCLK
  102. * ----------------- ----------------- ----------------- -----------------
  103. * desired prescale MR0 prescale MR0 prescale MR0 prescale MR0 resolution
  104. * prescale register register register register register register register register in degrees
  105. * freq value value value value value value value value
  106. *
  107. * 8 11.5 159,999 5.25 159,999 2.13 159,999 0.5625 159,999 0.023
  108. * 4 24 79,999 11.5 79,999 5.25 79,999 2.125 79,999 0.045
  109. * 2 49 39,999 24 39,999 11.5 39,999 5.25 39,999 0.090
  110. * 1 99 19,999 49 19,999 24 19,999 11.5 19,999 0.180
  111. * 0.5 199 9,999 99 9,999 49 9,999 24 9,999 0.360
  112. * 0.25 399 4,999 199 4,999 99 4,999 49 4,999 0.720
  113. * 0.125 799 2,499 399 2,499 199 2,499 99 2,499 1.440
  114. *
  115. * The desired prescale frequency comes from an input in the range of 544 - 2400 microseconds and the
  116. * desire to just shift the input left or right as needed.
  117. *
  118. * A resolution of 0.2 degrees seems reasonable so a prescale frequency output of 1MHz is being used.
  119. * It also means we don't need to scale the input.
  120. *
  121. * The PCLK is set to 25MHz because that's the slowest one that gives whole numbers for prescale and
  122. * MR0 registers.
  123. *
  124. * Final settings:
  125. * PCLKSEL0: 0x0
  126. * PWM1PR: 0x018 (24)
  127. * PWM1MR0: 0x04E1F (19,999)
  128. *
  129. */
  130. void LPC1768_PWM_init(void) {
  131. #define SBIT_CNTEN 0 // PWM1 counter & pre-scaler enable/disable
  132. #define SBIT_CNTRST 1 // reset counters to known state
  133. #define SBIT_PWMEN 3 // 1 - PWM, 0 - timer
  134. #define SBIT_PWMMR0R 1
  135. #define PCPWM1 6
  136. #define PCLK_PWM1 12
  137. LPC_SC->PCONP |= (1 << PCPWM1); // enable PWM1 controller (enabled on power up)
  138. LPC_SC->PCLKSEL0 &= ~(0x3 << PCLK_PWM1);
  139. LPC_SC->PCLKSEL0 |= (LPC_PWM1_PCLKSEL0 << PCLK_PWM1);
  140. LPC_PWM1->MR0 = LPC_PWM1_MR0; // TC resets every 19,999 + 1 cycles - sets PWM cycle(Ton+Toff) to 20 mS
  141. // MR0 must be set before TCR enables the PWM
  142. LPC_PWM1->TCR = _BV(SBIT_CNTEN) | _BV(SBIT_CNTRST)| _BV(SBIT_PWMEN);; // enable counters, reset counters, set mode to PWM
  143. LPC_PWM1->TCR &= ~(_BV(SBIT_CNTRST)); // take counters out of reset
  144. LPC_PWM1->PR = LPC_PWM1_PR;
  145. LPC_PWM1->MCR = (_BV(SBIT_PWMMR0R) | _BV(0)); // Reset TC if it matches MR0, disable all interrupts except for MR0
  146. LPC_PWM1->CTCR = 0; // disable counter mode (enable PWM mode)
  147. LPC_PWM1->LER = 0x07F; // Set the latch Enable Bits to load the new Match Values for MR0 - MR6
  148. // Set all PWMs to single edge
  149. LPC_PWM1->PCR = 0; // single edge mode for all channels, PWM1 control of outputs off
  150. NVIC_EnableIRQ(PWM1_IRQn); // Enable interrupt handler
  151. // NVIC_SetPriority(PWM1_IRQn, NVIC_EncodePriority(0, 10, 0)); // normal priority for PWM module
  152. NVIC_SetPriority(PWM1_IRQn, NVIC_EncodePriority(0, 0, 0)); // minimizes jitter due to higher priority ISRs
  153. }
  154. bool PWM_table_swap = false; // flag to tell the ISR that the tables have been swapped
  155. bool PWM_MR0_wait = false; // flag to ensure don't delay MR0 interrupt
  156. bool LPC1768_PWM_attach_pin(pin_t pin, uint32_t min /* = 1 */, uint32_t max /* = (LPC_PWM1_MR0 - MR0_MARGIN) */, uint8_t servo_index /* = 0xff */) {
  157. while (PWM_table_swap) delay(5); // don't do anything until the previous change has been implemented by the ISR
  158. COPY_ACTIVE_TABLE; // copy active table into work table
  159. uint8_t slot = 0;
  160. for (uint8_t i = 0; i < NUM_PWMS ; i++) // see if already in table
  161. if (work_table[i].pin == pin) return 1;
  162. for (uint8_t i = 1; (i < NUM_PWMS + 1) && !slot; i++) // find empty slot
  163. if ( !(work_table[i - 1].set_register)) slot = i; // any item that can't be zero when active or just attached is OK
  164. if (!slot) return 0;
  165. slot--; // turn it into array index
  166. work_table[slot].pin = pin; // init slot
  167. work_table[slot].PWM_mask = 0; // real value set by PWM_write
  168. work_table[slot].set_register = PIN_IS_INVERTED(pin) ? &LPC_GPIO(LPC1768_PIN_PORT(pin))->FIOCLR : &LPC_GPIO(LPC1768_PIN_PORT(pin))->FIOSET;
  169. work_table[slot].clr_register = PIN_IS_INVERTED(pin) ? &LPC_GPIO(LPC1768_PIN_PORT(pin))->FIOSET : &LPC_GPIO(LPC1768_PIN_PORT(pin))->FIOCLR;
  170. work_table[slot].write_mask = LPC_PIN(LPC1768_PIN_PIN(pin));
  171. work_table[slot].microseconds = MICRO_MAX;
  172. work_table[slot].min = min;
  173. work_table[slot].max = MIN(max, LPC_PWM1_MR0 - MR0_MARGIN);
  174. work_table[slot].servo_index = servo_index;
  175. work_table[slot].active_flag = false;
  176. //swap tables
  177. PWM_MR0_wait = true;
  178. while (PWM_MR0_wait) delay(5); //wait until MR0 interrupt has happend so don't delay it.
  179. NVIC_DisableIRQ(PWM1_IRQn);
  180. PWM_map *pointer_swap = active_table;
  181. active_table = work_table;
  182. work_table = pointer_swap;
  183. PWM_table_swap = true; // tell the ISR that the tables have been swapped
  184. NVIC_EnableIRQ(PWM1_IRQn); // re-enable PWM interrupts
  185. return 1;
  186. }
  187. #define pin_11_PWM_channel 2
  188. #define pin_6_PWM_channel 3
  189. #define pin_4_PWM_channel 1
  190. // used to keep track of which Match Registers have been used and if they will be used by the
  191. // PWM1 module to directly control the pin or will be used to generate an interrupt
  192. typedef struct { // status of PWM1 channel
  193. uint8_t map_used; // 0 - this MR register not used/assigned
  194. uint8_t map_PWM_INT; // 0 - available for interrupts, 1 - in use by PWM
  195. pin_t map_PWM_PIN; // pin for this PwM1 controlled pin / port
  196. volatile uint32_t* MR_register; // address of the MR register for this PWM1 channel
  197. uint32_t PCR_bit; // PCR register bit to enable PWM1 control of this pin
  198. uint32_t PINSEL3_bits; // PINSEL3 register bits to set pin mode to PWM1 control
  199. } MR_map;
  200. MR_map map_MR[NUM_PWMS];
  201. void LPC1768_PWM_update_map_MR(void) {
  202. map_MR[0] = {0, (uint8_t) (LPC_PWM1->PCR & _BV(8 + pin_4_PWM_channel) ? 1 : 0), 4, &LPC_PWM1->MR1, 0, 0};
  203. map_MR[1] = {0, (uint8_t) (LPC_PWM1->PCR & _BV(8 + pin_11_PWM_channel) ? 1 : 0), 11, &LPC_PWM1->MR2, 0, 0};
  204. map_MR[2] = {0, (uint8_t) (LPC_PWM1->PCR & _BV(8 + pin_6_PWM_channel) ? 1 : 0), 6, &LPC_PWM1->MR3, 0, 0};
  205. map_MR[3] = {0, 0, 0, &LPC_PWM1->MR4, 0, 0};
  206. map_MR[4] = {0, 0, 0, &LPC_PWM1->MR5, 0, 0};
  207. map_MR[5] = {0, 0, 0, &LPC_PWM1->MR6, 0, 0};
  208. }
  209. uint32_t LPC1768_PWM_interrupt_mask = 1;
  210. void LPC1768_PWM_update(void) {
  211. for (uint8_t i = NUM_PWMS; --i;) { // (bubble) sort table by microseconds
  212. bool didSwap = false;
  213. PWM_map temp;
  214. for (uint16_t j = 0; j < i; ++j) {
  215. if (work_table[j].microseconds > work_table[j + 1].microseconds) {
  216. temp = work_table[j + 1];
  217. work_table[j + 1] = work_table[j];
  218. work_table[j] = temp;
  219. didSwap = true;
  220. }
  221. }
  222. if (!didSwap) break;
  223. }
  224. LPC1768_PWM_interrupt_mask = 0; // set match registers to new values, build IRQ mask
  225. for (uint8_t i = 0; i < NUM_PWMS; i++) {
  226. if (work_table[i].active_flag == true) {
  227. work_table[i].sequence = i + 1;
  228. // first see if there is a PWM1 controlled pin for this entry
  229. bool found = false;
  230. for (uint8_t j = 0; (j < NUM_PWMS) && !found; j++) {
  231. if ( (map_MR[j].map_PWM_PIN == work_table[i].pin) && map_MR[j].map_PWM_INT ) {
  232. *map_MR[j].MR_register = work_table[i].microseconds; // found one of the PWM pins
  233. work_table[i].PWM_mask = 0;
  234. work_table[i].PCR_bit = map_MR[j].PCR_bit; // PCR register bit to enable PWM1 control of this pin
  235. work_table[i].PINSEL3_bits = map_MR[j].PINSEL3_bits; // PINSEL3 register bits to set pin mode to PWM1 control} MR_map;
  236. map_MR[j].map_used = 2;
  237. work_table[i].assigned_MR = j +1; // only used to help in debugging
  238. found = true;
  239. }
  240. }
  241. // didn't find a PWM1 pin so get an interrupt
  242. for (uint8_t k = 0; (k < NUM_PWMS) && !found; k++) {
  243. if ( !(map_MR[k].map_PWM_INT || map_MR[k].map_used)) {
  244. *map_MR[k].MR_register = work_table[i].microseconds; // found one for an interrupt pin
  245. map_MR[k].map_used = 1;
  246. LPC1768_PWM_interrupt_mask |= _BV(3 * (k + 1)); // set bit in the MCR to enable this MR to generate an interrupt
  247. work_table[i].PWM_mask = _BV(IR_BIT(k + 1)); // bit in the IR that will go active when this MR generates an interrupt
  248. work_table[i].assigned_MR = k +1; // only used to help in debugging
  249. found = true;
  250. }
  251. }
  252. }
  253. else
  254. work_table[i].sequence = 0;
  255. }
  256. LPC1768_PWM_interrupt_mask |= (uint32_t) _BV(0); // add in MR0 interrupt
  257. // swap tables
  258. PWM_MR0_wait = true;
  259. while (PWM_MR0_wait) delay(5); //wait until MR0 interrupt has happend so don't delay it.
  260. NVIC_DisableIRQ(PWM1_IRQn);
  261. LPC_PWM1->LER = 0x07E; // Set the latch Enable Bits to load the new Match Values for MR1 - MR6
  262. PWM_map *pointer_swap = active_table;
  263. active_table = work_table;
  264. work_table = pointer_swap;
  265. PWM_table_swap = true; // tell the ISR that the tables have been swapped
  266. NVIC_EnableIRQ(PWM1_IRQn); // re-enable PWM interrupts
  267. }
  268. bool LPC1768_PWM_write(pin_t pin, uint32_t value) {
  269. while (PWM_table_swap) delay(5); // don't do anything until the previous change has been implemented by the ISR
  270. COPY_ACTIVE_TABLE; // copy active table into work table
  271. uint8_t slot = 0xFF;
  272. for (uint8_t i = 0; i < NUM_PWMS; i++) // find slot
  273. if (work_table[i].pin == pin) slot = i;
  274. if (slot == 0xFF) return false; // return error if pin not found
  275. LPC1768_PWM_update_map_MR();
  276. switch(pin) {
  277. case P1_20: // Servo 0, PWM1 channel 2 (Pin 11 P1.20 PWM1.2)
  278. map_MR[pin_11_PWM_channel - 1].PCR_bit = _BV(8 + pin_11_PWM_channel); // enable PWM1 module control of this pin
  279. map_MR[pin_11_PWM_channel - 1].map_PWM_INT = 1; // 0 - available for interrupts, 1 - in use by PWM
  280. map_MR[pin_11_PWM_channel - 1].PINSEL3_bits = 0x2 << 8; // ISR must do this AFTER setting PCR
  281. break;
  282. case P1_21: // Servo 1, PWM1 channel 3 (Pin 6 P1.21 PWM1.3)
  283. map_MR[pin_6_PWM_channel - 1].PCR_bit = _BV(8 + pin_6_PWM_channel); // enable PWM1 module control of this pin
  284. map_MR[pin_6_PWM_channel - 1].map_PWM_INT = 1; // 0 - available for interrupts, 1 - in use by PWM
  285. map_MR[pin_6_PWM_channel - 1].PINSEL3_bits = 0x2 << 10; // ISR must do this AFTER setting PCR
  286. break;
  287. case P1_18: // Servo 3, PWM1 channel 1 (Pin 4 P1.18 PWM1.1)
  288. map_MR[pin_4_PWM_channel - 1].PCR_bit = _BV(8 + pin_4_PWM_channel); // enable PWM1 module control of this pin
  289. map_MR[pin_4_PWM_channel - 1].map_PWM_INT = 1; // 0 - available for interrupts, 1 - in use by PWM
  290. map_MR[pin_4_PWM_channel - 1].PINSEL3_bits = 0x2 << 4; // ISR must do this AFTER setting PCR
  291. break;
  292. default: // ISR pins
  293. pinMode(pin, OUTPUT); // set pin to output but don't write anything in case it's already in use
  294. break;
  295. }
  296. work_table[slot].microseconds = MAX(MIN(value, work_table[slot].max), work_table[slot].min);
  297. work_table[slot].active_flag = true;
  298. LPC1768_PWM_update();
  299. return 1;
  300. }
  301. bool LPC1768_PWM_detach_pin(pin_t pin) {
  302. while (PWM_table_swap) delay(5); // don't do anything until the previous change has been implemented by the ISR
  303. COPY_ACTIVE_TABLE; // copy active table into work table
  304. uint8_t slot = 0xFF;
  305. for (uint8_t i = 0; i < NUM_PWMS; i++) // find slot
  306. if (work_table[i].pin == pin) slot = i;
  307. if (slot == 0xFF) return false; // return error if pin not found
  308. LPC1768_PWM_update_map_MR();
  309. // OK to make these changes before the MR0 interrupt
  310. switch(pin) {
  311. case P1_20: // Servo 0, PWM1 channel 2 (Pin 11 P1.20 PWM1.2)
  312. LPC_PWM1->PCR &= ~(_BV(8 + pin_11_PWM_channel)); // disable PWM1 module control of this pin
  313. map_MR[pin_11_PWM_channel - 1].PCR_bit = 0;
  314. LPC_PINCON->PINSEL3 &= ~(0x3 << 8); // return pin to general purpose I/O
  315. map_MR[pin_11_PWM_channel - 1].PINSEL3_bits = 0;
  316. map_MR[pin_11_PWM_channel - 1].map_PWM_INT = 0; // 0 - available for interrupts, 1 - in use by PWM
  317. break;
  318. case P1_21: // Servo 1, PWM1 channel 3 (Pin 6 P1.21 PWM1.3)
  319. LPC_PWM1->PCR &= ~(_BV(8 + pin_6_PWM_channel)); // disable PWM1 module control of this pin
  320. map_MR[pin_6_PWM_channel - 1].PCR_bit = 0;
  321. LPC_PINCON->PINSEL3 &= ~(0x3 << 10); // return pin to general purpose I/O
  322. map_MR[pin_6_PWM_channel - 1].PINSEL3_bits = 0;
  323. map_MR[pin_6_PWM_channel - 1].map_PWM_INT = 0; // 0 - available for interrupts, 1 - in use by PWM
  324. break;
  325. case P1_18: // Servo 3, PWM1 channel 1 (Pin 4 P1.18 PWM1.1)
  326. LPC_PWM1->PCR &= ~(_BV(8 + pin_4_PWM_channel)); // disable PWM1 module control of this pin
  327. map_MR[pin_4_PWM_channel - 1].PCR_bit = 0;
  328. LPC_PINCON->PINSEL3 &= ~(0x3 << 4); // return pin to general purpose I/O
  329. map_MR[pin_4_PWM_channel - 1].PINSEL3_bits = 0;
  330. map_MR[pin_4_PWM_channel - 1].map_PWM_INT = 0; // 0 - available for interrupts, 1 - in use by PWM
  331. break;
  332. }
  333. pinMode(pin, INPUT);
  334. work_table[slot] = PWM_MAP_INIT_ROW;
  335. LPC1768_PWM_update();
  336. return 1;
  337. }
  338. bool useable_hardware_PWM(pin_t pin) {
  339. COPY_ACTIVE_TABLE; // copy active table into work table
  340. for (uint8_t i = 0; i < NUM_PWMS; i++) // see if it's already setup
  341. if (work_table[i].pin == pin && work_table[i].sequence) return true;
  342. for (uint8_t i = 0; i < NUM_PWMS; i++) // see if there is an empty slot
  343. if (!work_table[i].sequence) return true;
  344. return false; // only get here if neither the above are true
  345. }
  346. ////////////////////////////////////////////////////////////////////////////////
  347. #define HAL_PWM_LPC1768_ISR extern "C" void PWM1_IRQHandler(void)
  348. // Both loops could be terminated when the last active channel is found but that would
  349. // result in variations ISR run time which results in variations in pulse width
  350. /**
  351. * Changes to PINSEL3, PCR and MCR are only done during the MR0 interrupt otherwise
  352. * the wrong pin may be toggled or even have the system hang.
  353. */
  354. HAL_PWM_LPC1768_ISR {
  355. if (PWM_table_swap) ISR_table = work_table; // use old table if a swap was just done
  356. else ISR_table = active_table;
  357. if (LPC_PWM1->IR & 0x1) { // MR0 interrupt
  358. ISR_table = active_table; // MR0 means new values could have been loaded so set everything
  359. if (PWM_table_swap) LPC_PWM1->MCR = LPC1768_PWM_interrupt_mask; // enable new PWM individual channel interrupts
  360. for (uint8_t i = 0; i < NUM_PWMS; i++) {
  361. if(ISR_table[i].active_flag && !((ISR_table[i].pin == P1_20) ||
  362. (ISR_table[i].pin == P1_21) ||
  363. (ISR_table[i].pin == P1_18)))
  364. *ISR_table[i].set_register = ISR_table[i].write_mask; // set pins for all enabled interrupt channels active
  365. if (PWM_table_swap && ISR_table[i].PCR_bit) {
  366. LPC_PWM1->PCR |= ISR_table[i].PCR_bit; // enable PWM1 module control of this pin
  367. LPC_PINCON->PINSEL3 |= ISR_table[i].PINSEL3_bits; // set pin mode to PWM1 control - must be done after PCR
  368. }
  369. }
  370. PWM_table_swap = false;
  371. PWM_MR0_wait = false;
  372. LPC_PWM1->IR = 0x01; // clear the MR0 interrupt flag bit
  373. }
  374. else {
  375. for (uint8_t i = 0; i < NUM_PWMS ; i++)
  376. if (ISR_table[i].active_flag && (LPC_PWM1->IR & ISR_table[i].PWM_mask) ){
  377. LPC_PWM1->IR = ISR_table[i].PWM_mask; // clear the interrupt flag bits for expected interrupts
  378. *ISR_table[i].clr_register = ISR_table[i].write_mask; // set channel to inactive
  379. }
  380. }
  381. LPC_PWM1->IR = 0x70F; // guarantees all interrupt flags are cleared which, if there is an unexpected
  382. // PWM interrupt, will keep the ISR from hanging which will crash the controller
  383. return;
  384. }
  385. #endif
  386. /////////////////////////////////////////////////////////////////
  387. ///////////////// HARDWARE FIRMWARE INTERACTION ////////////////
  388. /////////////////////////////////////////////////////////////////
  389. /**
  390. * Almost all changes to the hardware registers must be coordinated with the Match Register 0 (MR0)
  391. * interrupt. The only exception is detaching pins. It doesn't matter when they go
  392. * tristate.
  393. *
  394. * The LPC1768_PWM_init routine kicks off the MR0 interrupt. This interrupt is never disabled or
  395. * delayed.
  396. *
  397. * The PWM_table_swap flag is set when the firmware has swapped in an updated table. It is
  398. * cleared by the ISR during the MR0 interrupt as it completes the swap and accompanying updates.
  399. * It serves two purposes:
  400. * 1) Tells the ISR that the tables have been swapped
  401. * 2) Keeps the firmware from starting a new update until the previous one has been completed.
  402. *
  403. * The PWM_MR0_wait flag is set when the firmware is ready to swap in an updated table and cleared by
  404. * the ISR during the MR0 interrupt. It is used to avoid delaying the MR0 interrupt when swapping in
  405. * an updated table. This avoids glitches in pulse width and/or repetition rate.
  406. *
  407. * The sequence of events during a write to a PWM channel is:
  408. * 1) Waits until PWM_table_swap flag is false before starting
  409. * 2) Copies the active table into the work table
  410. * 3) Updates the work table
  411. * NOTES - MR1-MR6 are updated at this time. The updates aren't put into use until the first
  412. * MR0 after the LER register has been written. The LER register is written during the
  413. * table swap process.
  414. * - The MCR mask is created at this time. It is not used until the ISR writes the MCR
  415. * during the MR0 interrupt in the table swap process.
  416. * 4) Sets the PWM_MR0_wait flag
  417. * 5) ISR clears the PWM_MR0_wait flag during the next MR0 interrupt
  418. * 6) Once the PWM_MR0_wait flag is cleared then the firmware:
  419. * disables the ISR interrupt
  420. * swaps the pointers to the tables
  421. * writes to the LER register
  422. * sets the PWM_table_swap flag active
  423. * re-enables the ISR
  424. * 7) On the next interrupt the ISR changes its pointer to the work table which is now the old,
  425. * unmodified, active table.
  426. * 8) On the next MR0 interrupt the ISR:
  427. * switches over to the active table
  428. * clears the PWM_table_swap and PWM_MR0_wait flags
  429. * updates the MCR register with the possibly new interrupt sources/assignments
  430. * writes to the PCR register to enable the direct control of the Servo 0, 1 & 3 pins by the PWM1 module
  431. * sets the PINSEL3 register to function/mode 0x2 for the Servo 0, 1 & 3 pins
  432. * NOTE - PCR must be set before PINSEL
  433. * sets the pins controlled by the ISR to their active states
  434. */