Teensy 4.1 core updated for C++20
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

1410 lines
46KB

  1. /* Teensyduino Core Library
  2. * http://www.pjrc.com/teensy/
  3. * Copyright (c) 2013 PJRC.COM, LLC.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining
  6. * a copy of this software and associated documentation files (the
  7. * "Software"), to deal in the Software without restriction, including
  8. * without limitation the rights to use, copy, modify, merge, publish,
  9. * distribute, sublicense, and/or sell copies of the Software, and to
  10. * permit persons to whom the Software is furnished to do so, subject to
  11. * the following conditions:
  12. *
  13. * 1. The above copyright notice and this permission notice shall be
  14. * included in all copies or substantial portions of the Software.
  15. *
  16. * 2. If the Software is incorporated into a build system that allows
  17. * selection among a list of target devices, then similar target
  18. * devices manufactured by PJRC.COM must be included in the list of
  19. * target devices and selectable in the same manner.
  20. *
  21. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  25. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  26. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  27. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  28. * SOFTWARE.
  29. */
  30. #include "core_pins.h"
  31. #include "pins_arduino.h"
  32. #include "HardwareSerial.h"
  33. #if defined(KINETISK)
  34. #define GPIO_BITBAND_ADDR(reg, bit) (((uint32_t)&(reg) - 0x40000000) * 32 + (bit) * 4 + 0x42000000)
  35. #define GPIO_BITBAND_PTR(reg, bit) ((uint32_t *)GPIO_BITBAND_ADDR((reg), (bit)))
  36. //#define GPIO_SET_BIT(reg, bit) (*GPIO_BITBAND_PTR((reg), (bit)) = 1)
  37. //#define GPIO_CLR_BIT(reg, bit) (*GPIO_BITBAND_PTR((reg), (bit)) = 0)
  38. const struct digital_pin_bitband_and_config_table_struct digital_pin_to_info_PGM[] = {
  39. {GPIO_BITBAND_PTR(CORE_PIN0_PORTREG, CORE_PIN0_BIT), &CORE_PIN0_CONFIG},
  40. {GPIO_BITBAND_PTR(CORE_PIN1_PORTREG, CORE_PIN1_BIT), &CORE_PIN1_CONFIG},
  41. {GPIO_BITBAND_PTR(CORE_PIN2_PORTREG, CORE_PIN2_BIT), &CORE_PIN2_CONFIG},
  42. {GPIO_BITBAND_PTR(CORE_PIN3_PORTREG, CORE_PIN3_BIT), &CORE_PIN3_CONFIG},
  43. {GPIO_BITBAND_PTR(CORE_PIN4_PORTREG, CORE_PIN4_BIT), &CORE_PIN4_CONFIG},
  44. {GPIO_BITBAND_PTR(CORE_PIN5_PORTREG, CORE_PIN5_BIT), &CORE_PIN5_CONFIG},
  45. {GPIO_BITBAND_PTR(CORE_PIN6_PORTREG, CORE_PIN6_BIT), &CORE_PIN6_CONFIG},
  46. {GPIO_BITBAND_PTR(CORE_PIN7_PORTREG, CORE_PIN7_BIT), &CORE_PIN7_CONFIG},
  47. {GPIO_BITBAND_PTR(CORE_PIN8_PORTREG, CORE_PIN8_BIT), &CORE_PIN8_CONFIG},
  48. {GPIO_BITBAND_PTR(CORE_PIN9_PORTREG, CORE_PIN9_BIT), &CORE_PIN9_CONFIG},
  49. {GPIO_BITBAND_PTR(CORE_PIN10_PORTREG, CORE_PIN10_BIT), &CORE_PIN10_CONFIG},
  50. {GPIO_BITBAND_PTR(CORE_PIN11_PORTREG, CORE_PIN11_BIT), &CORE_PIN11_CONFIG},
  51. {GPIO_BITBAND_PTR(CORE_PIN12_PORTREG, CORE_PIN12_BIT), &CORE_PIN12_CONFIG},
  52. {GPIO_BITBAND_PTR(CORE_PIN13_PORTREG, CORE_PIN13_BIT), &CORE_PIN13_CONFIG},
  53. {GPIO_BITBAND_PTR(CORE_PIN14_PORTREG, CORE_PIN14_BIT), &CORE_PIN14_CONFIG},
  54. {GPIO_BITBAND_PTR(CORE_PIN15_PORTREG, CORE_PIN15_BIT), &CORE_PIN15_CONFIG},
  55. {GPIO_BITBAND_PTR(CORE_PIN16_PORTREG, CORE_PIN16_BIT), &CORE_PIN16_CONFIG},
  56. {GPIO_BITBAND_PTR(CORE_PIN17_PORTREG, CORE_PIN17_BIT), &CORE_PIN17_CONFIG},
  57. {GPIO_BITBAND_PTR(CORE_PIN18_PORTREG, CORE_PIN18_BIT), &CORE_PIN18_CONFIG},
  58. {GPIO_BITBAND_PTR(CORE_PIN19_PORTREG, CORE_PIN19_BIT), &CORE_PIN19_CONFIG},
  59. {GPIO_BITBAND_PTR(CORE_PIN20_PORTREG, CORE_PIN20_BIT), &CORE_PIN20_CONFIG},
  60. {GPIO_BITBAND_PTR(CORE_PIN21_PORTREG, CORE_PIN21_BIT), &CORE_PIN21_CONFIG},
  61. {GPIO_BITBAND_PTR(CORE_PIN22_PORTREG, CORE_PIN22_BIT), &CORE_PIN22_CONFIG},
  62. {GPIO_BITBAND_PTR(CORE_PIN23_PORTREG, CORE_PIN23_BIT), &CORE_PIN23_CONFIG},
  63. {GPIO_BITBAND_PTR(CORE_PIN24_PORTREG, CORE_PIN24_BIT), &CORE_PIN24_CONFIG},
  64. {GPIO_BITBAND_PTR(CORE_PIN25_PORTREG, CORE_PIN25_BIT), &CORE_PIN25_CONFIG},
  65. {GPIO_BITBAND_PTR(CORE_PIN26_PORTREG, CORE_PIN26_BIT), &CORE_PIN26_CONFIG},
  66. {GPIO_BITBAND_PTR(CORE_PIN27_PORTREG, CORE_PIN27_BIT), &CORE_PIN27_CONFIG},
  67. {GPIO_BITBAND_PTR(CORE_PIN28_PORTREG, CORE_PIN28_BIT), &CORE_PIN28_CONFIG},
  68. {GPIO_BITBAND_PTR(CORE_PIN29_PORTREG, CORE_PIN29_BIT), &CORE_PIN29_CONFIG},
  69. {GPIO_BITBAND_PTR(CORE_PIN30_PORTREG, CORE_PIN30_BIT), &CORE_PIN30_CONFIG},
  70. {GPIO_BITBAND_PTR(CORE_PIN31_PORTREG, CORE_PIN31_BIT), &CORE_PIN31_CONFIG},
  71. {GPIO_BITBAND_PTR(CORE_PIN32_PORTREG, CORE_PIN32_BIT), &CORE_PIN32_CONFIG},
  72. {GPIO_BITBAND_PTR(CORE_PIN33_PORTREG, CORE_PIN33_BIT), &CORE_PIN33_CONFIG},
  73. #ifdef CORE_PIN34_PORTREG
  74. {GPIO_BITBAND_PTR(CORE_PIN34_PORTREG, CORE_PIN34_BIT), &CORE_PIN34_CONFIG},
  75. {GPIO_BITBAND_PTR(CORE_PIN35_PORTREG, CORE_PIN35_BIT), &CORE_PIN35_CONFIG},
  76. {GPIO_BITBAND_PTR(CORE_PIN36_PORTREG, CORE_PIN36_BIT), &CORE_PIN36_CONFIG},
  77. {GPIO_BITBAND_PTR(CORE_PIN37_PORTREG, CORE_PIN37_BIT), &CORE_PIN37_CONFIG},
  78. {GPIO_BITBAND_PTR(CORE_PIN38_PORTREG, CORE_PIN38_BIT), &CORE_PIN38_CONFIG},
  79. {GPIO_BITBAND_PTR(CORE_PIN39_PORTREG, CORE_PIN39_BIT), &CORE_PIN39_CONFIG},
  80. {GPIO_BITBAND_PTR(CORE_PIN40_PORTREG, CORE_PIN40_BIT), &CORE_PIN40_CONFIG},
  81. {GPIO_BITBAND_PTR(CORE_PIN41_PORTREG, CORE_PIN41_BIT), &CORE_PIN41_CONFIG},
  82. {GPIO_BITBAND_PTR(CORE_PIN42_PORTREG, CORE_PIN42_BIT), &CORE_PIN42_CONFIG},
  83. {GPIO_BITBAND_PTR(CORE_PIN43_PORTREG, CORE_PIN43_BIT), &CORE_PIN43_CONFIG},
  84. {GPIO_BITBAND_PTR(CORE_PIN44_PORTREG, CORE_PIN44_BIT), &CORE_PIN44_CONFIG},
  85. {GPIO_BITBAND_PTR(CORE_PIN45_PORTREG, CORE_PIN45_BIT), &CORE_PIN45_CONFIG},
  86. {GPIO_BITBAND_PTR(CORE_PIN46_PORTREG, CORE_PIN46_BIT), &CORE_PIN46_CONFIG},
  87. {GPIO_BITBAND_PTR(CORE_PIN47_PORTREG, CORE_PIN47_BIT), &CORE_PIN47_CONFIG},
  88. {GPIO_BITBAND_PTR(CORE_PIN48_PORTREG, CORE_PIN48_BIT), &CORE_PIN48_CONFIG},
  89. {GPIO_BITBAND_PTR(CORE_PIN49_PORTREG, CORE_PIN49_BIT), &CORE_PIN49_CONFIG},
  90. {GPIO_BITBAND_PTR(CORE_PIN50_PORTREG, CORE_PIN50_BIT), &CORE_PIN50_CONFIG},
  91. {GPIO_BITBAND_PTR(CORE_PIN51_PORTREG, CORE_PIN51_BIT), &CORE_PIN51_CONFIG},
  92. {GPIO_BITBAND_PTR(CORE_PIN52_PORTREG, CORE_PIN52_BIT), &CORE_PIN52_CONFIG},
  93. {GPIO_BITBAND_PTR(CORE_PIN53_PORTREG, CORE_PIN53_BIT), &CORE_PIN53_CONFIG},
  94. {GPIO_BITBAND_PTR(CORE_PIN54_PORTREG, CORE_PIN54_BIT), &CORE_PIN54_CONFIG},
  95. {GPIO_BITBAND_PTR(CORE_PIN55_PORTREG, CORE_PIN55_BIT), &CORE_PIN55_CONFIG},
  96. {GPIO_BITBAND_PTR(CORE_PIN56_PORTREG, CORE_PIN56_BIT), &CORE_PIN56_CONFIG},
  97. {GPIO_BITBAND_PTR(CORE_PIN57_PORTREG, CORE_PIN57_BIT), &CORE_PIN57_CONFIG},
  98. #ifdef USE_SDCARD_PINS
  99. {GPIO_BITBAND_PTR(CORE_PIN58_PORTREG, CORE_PIN58_BIT), &CORE_PIN58_CONFIG},
  100. {GPIO_BITBAND_PTR(CORE_PIN59_PORTREG, CORE_PIN59_BIT), &CORE_PIN59_CONFIG},
  101. {GPIO_BITBAND_PTR(CORE_PIN60_PORTREG, CORE_PIN60_BIT), &CORE_PIN60_CONFIG},
  102. {GPIO_BITBAND_PTR(CORE_PIN61_PORTREG, CORE_PIN61_BIT), &CORE_PIN61_CONFIG},
  103. {GPIO_BITBAND_PTR(CORE_PIN62_PORTREG, CORE_PIN62_BIT), &CORE_PIN62_CONFIG},
  104. {GPIO_BITBAND_PTR(CORE_PIN63_PORTREG, CORE_PIN63_BIT), &CORE_PIN63_CONFIG},
  105. #endif
  106. #endif
  107. };
  108. #elif defined(KINETISL)
  109. const struct digital_pin_bitband_and_config_table_struct digital_pin_to_info_PGM[] = {
  110. {((volatile uint8_t *)&CORE_PIN0_PORTREG + (CORE_PIN0_BIT >> 3)), &CORE_PIN0_CONFIG, (1<<(CORE_PIN0_BIT & 7))},
  111. {((volatile uint8_t *)&CORE_PIN1_PORTREG + (CORE_PIN1_BIT >> 3)), &CORE_PIN1_CONFIG, (1<<(CORE_PIN1_BIT & 7))},
  112. {((volatile uint8_t *)&CORE_PIN2_PORTREG + (CORE_PIN2_BIT >> 3)), &CORE_PIN2_CONFIG, (1<<(CORE_PIN2_BIT & 7))},
  113. {((volatile uint8_t *)&CORE_PIN3_PORTREG + (CORE_PIN3_BIT >> 3)), &CORE_PIN3_CONFIG, (1<<(CORE_PIN3_BIT & 7))},
  114. {((volatile uint8_t *)&CORE_PIN4_PORTREG + (CORE_PIN4_BIT >> 3)), &CORE_PIN4_CONFIG, (1<<(CORE_PIN4_BIT & 7))},
  115. {((volatile uint8_t *)&CORE_PIN5_PORTREG + (CORE_PIN5_BIT >> 3)), &CORE_PIN5_CONFIG, (1<<(CORE_PIN5_BIT & 7))},
  116. {((volatile uint8_t *)&CORE_PIN6_PORTREG + (CORE_PIN6_BIT >> 3)), &CORE_PIN6_CONFIG, (1<<(CORE_PIN6_BIT & 7))},
  117. {((volatile uint8_t *)&CORE_PIN7_PORTREG + (CORE_PIN7_BIT >> 3)), &CORE_PIN7_CONFIG, (1<<(CORE_PIN7_BIT & 7))},
  118. {((volatile uint8_t *)&CORE_PIN8_PORTREG + (CORE_PIN8_BIT >> 3)), &CORE_PIN8_CONFIG, (1<<(CORE_PIN8_BIT & 7))},
  119. {((volatile uint8_t *)&CORE_PIN9_PORTREG + (CORE_PIN9_BIT >> 3)), &CORE_PIN9_CONFIG, (1<<(CORE_PIN9_BIT & 7))},
  120. {((volatile uint8_t *)&CORE_PIN10_PORTREG + (CORE_PIN10_BIT >> 3)), &CORE_PIN10_CONFIG, (1<<(CORE_PIN10_BIT & 7))},
  121. {((volatile uint8_t *)&CORE_PIN11_PORTREG + (CORE_PIN11_BIT >> 3)), &CORE_PIN11_CONFIG, (1<<(CORE_PIN11_BIT & 7))},
  122. {((volatile uint8_t *)&CORE_PIN12_PORTREG + (CORE_PIN12_BIT >> 3)), &CORE_PIN12_CONFIG, (1<<(CORE_PIN12_BIT & 7))},
  123. {((volatile uint8_t *)&CORE_PIN13_PORTREG + (CORE_PIN13_BIT >> 3)), &CORE_PIN13_CONFIG, (1<<(CORE_PIN13_BIT & 7))},
  124. {((volatile uint8_t *)&CORE_PIN14_PORTREG + (CORE_PIN14_BIT >> 3)), &CORE_PIN14_CONFIG, (1<<(CORE_PIN14_BIT & 7))},
  125. {((volatile uint8_t *)&CORE_PIN15_PORTREG + (CORE_PIN15_BIT >> 3)), &CORE_PIN15_CONFIG, (1<<(CORE_PIN15_BIT & 7))},
  126. {((volatile uint8_t *)&CORE_PIN16_PORTREG + (CORE_PIN16_BIT >> 3)), &CORE_PIN16_CONFIG, (1<<(CORE_PIN16_BIT & 7))},
  127. {((volatile uint8_t *)&CORE_PIN17_PORTREG + (CORE_PIN17_BIT >> 3)), &CORE_PIN17_CONFIG, (1<<(CORE_PIN17_BIT & 7))},
  128. {((volatile uint8_t *)&CORE_PIN18_PORTREG + (CORE_PIN18_BIT >> 3)), &CORE_PIN18_CONFIG, (1<<(CORE_PIN18_BIT & 7))},
  129. {((volatile uint8_t *)&CORE_PIN19_PORTREG + (CORE_PIN19_BIT >> 3)), &CORE_PIN19_CONFIG, (1<<(CORE_PIN19_BIT & 7))},
  130. {((volatile uint8_t *)&CORE_PIN20_PORTREG + (CORE_PIN20_BIT >> 3)), &CORE_PIN20_CONFIG, (1<<(CORE_PIN20_BIT & 7))},
  131. {((volatile uint8_t *)&CORE_PIN21_PORTREG + (CORE_PIN21_BIT >> 3)), &CORE_PIN21_CONFIG, (1<<(CORE_PIN21_BIT & 7))},
  132. {((volatile uint8_t *)&CORE_PIN22_PORTREG + (CORE_PIN22_BIT >> 3)), &CORE_PIN22_CONFIG, (1<<(CORE_PIN22_BIT & 7))},
  133. {((volatile uint8_t *)&CORE_PIN23_PORTREG + (CORE_PIN23_BIT >> 3)), &CORE_PIN23_CONFIG, (1<<(CORE_PIN23_BIT & 7))},
  134. {((volatile uint8_t *)&CORE_PIN24_PORTREG + (CORE_PIN24_BIT >> 3)), &CORE_PIN24_CONFIG, (1<<(CORE_PIN24_BIT & 7))},
  135. {((volatile uint8_t *)&CORE_PIN25_PORTREG + (CORE_PIN25_BIT >> 3)), &CORE_PIN25_CONFIG, (1<<(CORE_PIN25_BIT & 7))},
  136. {((volatile uint8_t *)&CORE_PIN26_PORTREG + (CORE_PIN26_BIT >> 3)), &CORE_PIN26_CONFIG, (1<<(CORE_PIN26_BIT & 7))}
  137. };
  138. #endif
  139. typedef void (*voidFuncPtr)(void);
  140. volatile static voidFuncPtr intFunc[CORE_NUM_DIGITAL];
  141. #if defined(KINETISK)
  142. static void porta_interrupt(void);
  143. static void portb_interrupt(void);
  144. static void portc_interrupt(void);
  145. static void portd_interrupt(void);
  146. static void porte_interrupt(void);
  147. #elif defined(KINETISL)
  148. static void porta_interrupt(void);
  149. static void portcd_interrupt(void);
  150. #endif
  151. void attachInterruptVector(enum IRQ_NUMBER_t irq, void (*function)(void))
  152. {
  153. _VectorsRam[irq + 16] = function;
  154. }
  155. void attachInterrupt(uint8_t pin, void (*function)(void), int mode)
  156. {
  157. volatile uint32_t *config;
  158. uint32_t cfg, mask;
  159. if (pin >= CORE_NUM_DIGITAL) return;
  160. switch (mode) {
  161. case CHANGE: mask = 0x0B; break;
  162. case RISING: mask = 0x09; break;
  163. case FALLING: mask = 0x0A; break;
  164. case LOW: mask = 0x08; break;
  165. case HIGH: mask = 0x0C; break;
  166. default: return;
  167. }
  168. mask = (mask << 16) | 0x01000000;
  169. config = portConfigRegister(pin);
  170. #if defined(KINETISK)
  171. attachInterruptVector(IRQ_PORTA, porta_interrupt);
  172. attachInterruptVector(IRQ_PORTB, portb_interrupt);
  173. attachInterruptVector(IRQ_PORTC, portc_interrupt);
  174. attachInterruptVector(IRQ_PORTD, portd_interrupt);
  175. attachInterruptVector(IRQ_PORTE, porte_interrupt);
  176. #elif defined(KINETISL)
  177. attachInterruptVector(IRQ_PORTA, porta_interrupt);
  178. attachInterruptVector(IRQ_PORTCD, portcd_interrupt);
  179. #endif
  180. __disable_irq();
  181. cfg = *config;
  182. cfg &= ~0x000F0000; // disable any previous interrupt
  183. *config = cfg;
  184. intFunc[pin] = function; // set the function pointer
  185. cfg |= mask;
  186. *config = cfg; // enable the new interrupt
  187. __enable_irq();
  188. }
  189. void detachInterrupt(uint8_t pin)
  190. {
  191. volatile uint32_t *config;
  192. config = portConfigRegister(pin);
  193. __disable_irq();
  194. *config = ((*config & ~0x000F0000) | 0x01000000);
  195. intFunc[pin] = NULL;
  196. __enable_irq();
  197. }
  198. #if defined(__MK20DX128__) || defined(__MK20DX256__)
  199. static void porta_interrupt(void)
  200. {
  201. uint32_t isfr = PORTA_ISFR;
  202. PORTA_ISFR = isfr;
  203. if ((isfr & CORE_PIN3_BITMASK) && intFunc[3]) intFunc[3]();
  204. if ((isfr & CORE_PIN4_BITMASK) && intFunc[4]) intFunc[4]();
  205. if ((isfr & CORE_PIN24_BITMASK) && intFunc[24]) intFunc[24]();
  206. if ((isfr & CORE_PIN33_BITMASK) && intFunc[33]) intFunc[33]();
  207. }
  208. static void portb_interrupt(void)
  209. {
  210. uint32_t isfr = PORTB_ISFR;
  211. PORTB_ISFR = isfr;
  212. if ((isfr & CORE_PIN0_BITMASK) && intFunc[0]) intFunc[0]();
  213. if ((isfr & CORE_PIN1_BITMASK) && intFunc[1]) intFunc[1]();
  214. if ((isfr & CORE_PIN16_BITMASK) && intFunc[16]) intFunc[16]();
  215. if ((isfr & CORE_PIN17_BITMASK) && intFunc[17]) intFunc[17]();
  216. if ((isfr & CORE_PIN18_BITMASK) && intFunc[18]) intFunc[18]();
  217. if ((isfr & CORE_PIN19_BITMASK) && intFunc[19]) intFunc[19]();
  218. if ((isfr & CORE_PIN25_BITMASK) && intFunc[25]) intFunc[25]();
  219. if ((isfr & CORE_PIN32_BITMASK) && intFunc[32]) intFunc[32]();
  220. }
  221. static void portc_interrupt(void)
  222. {
  223. // TODO: these are inefficent. Use CLZ somehow....
  224. uint32_t isfr = PORTC_ISFR;
  225. PORTC_ISFR = isfr;
  226. if ((isfr & CORE_PIN9_BITMASK) && intFunc[9]) intFunc[9]();
  227. if ((isfr & CORE_PIN10_BITMASK) && intFunc[10]) intFunc[10]();
  228. if ((isfr & CORE_PIN11_BITMASK) && intFunc[11]) intFunc[11]();
  229. if ((isfr & CORE_PIN12_BITMASK) && intFunc[12]) intFunc[12]();
  230. if ((isfr & CORE_PIN13_BITMASK) && intFunc[13]) intFunc[13]();
  231. if ((isfr & CORE_PIN15_BITMASK) && intFunc[15]) intFunc[15]();
  232. if ((isfr & CORE_PIN22_BITMASK) && intFunc[22]) intFunc[22]();
  233. if ((isfr & CORE_PIN23_BITMASK) && intFunc[23]) intFunc[23]();
  234. if ((isfr & CORE_PIN27_BITMASK) && intFunc[27]) intFunc[27]();
  235. if ((isfr & CORE_PIN28_BITMASK) && intFunc[28]) intFunc[28]();
  236. if ((isfr & CORE_PIN29_BITMASK) && intFunc[29]) intFunc[29]();
  237. if ((isfr & CORE_PIN30_BITMASK) && intFunc[30]) intFunc[30]();
  238. }
  239. static void portd_interrupt(void)
  240. {
  241. uint32_t isfr = PORTD_ISFR;
  242. PORTD_ISFR = isfr;
  243. if ((isfr & CORE_PIN2_BITMASK) && intFunc[2]) intFunc[2]();
  244. if ((isfr & CORE_PIN5_BITMASK) && intFunc[5]) intFunc[5]();
  245. if ((isfr & CORE_PIN6_BITMASK) && intFunc[6]) intFunc[6]();
  246. if ((isfr & CORE_PIN7_BITMASK) && intFunc[7]) intFunc[7]();
  247. if ((isfr & CORE_PIN8_BITMASK) && intFunc[8]) intFunc[8]();
  248. if ((isfr & CORE_PIN14_BITMASK) && intFunc[14]) intFunc[14]();
  249. if ((isfr & CORE_PIN20_BITMASK) && intFunc[20]) intFunc[20]();
  250. if ((isfr & CORE_PIN21_BITMASK) && intFunc[21]) intFunc[21]();
  251. }
  252. static void porte_interrupt(void)
  253. {
  254. uint32_t isfr = PORTE_ISFR;
  255. PORTE_ISFR = isfr;
  256. if ((isfr & CORE_PIN26_BITMASK) && intFunc[26]) intFunc[26]();
  257. if ((isfr & CORE_PIN31_BITMASK) && intFunc[31]) intFunc[31]();
  258. }
  259. #elif defined(__MKL26Z64__)
  260. static void porta_interrupt(void)
  261. {
  262. uint32_t isfr = PORTA_ISFR;
  263. PORTA_ISFR = isfr;
  264. if ((isfr & CORE_PIN3_BITMASK) && intFunc[3]) intFunc[3]();
  265. if ((isfr & CORE_PIN4_BITMASK) && intFunc[4]) intFunc[4]();
  266. }
  267. static void portcd_interrupt(void)
  268. {
  269. uint32_t isfr = PORTC_ISFR;
  270. PORTC_ISFR = isfr;
  271. if ((isfr & CORE_PIN9_BITMASK) && intFunc[9]) intFunc[9]();
  272. if ((isfr & CORE_PIN10_BITMASK) && intFunc[10]) intFunc[10]();
  273. if ((isfr & CORE_PIN11_BITMASK) && intFunc[11]) intFunc[11]();
  274. if ((isfr & CORE_PIN12_BITMASK) && intFunc[12]) intFunc[12]();
  275. if ((isfr & CORE_PIN13_BITMASK) && intFunc[13]) intFunc[13]();
  276. if ((isfr & CORE_PIN15_BITMASK) && intFunc[15]) intFunc[15]();
  277. if ((isfr & CORE_PIN22_BITMASK) && intFunc[22]) intFunc[22]();
  278. if ((isfr & CORE_PIN23_BITMASK) && intFunc[23]) intFunc[23]();
  279. isfr = PORTD_ISFR;
  280. PORTD_ISFR = isfr;
  281. if ((isfr & CORE_PIN2_BITMASK) && intFunc[2]) intFunc[2]();
  282. if ((isfr & CORE_PIN5_BITMASK) && intFunc[5]) intFunc[5]();
  283. if ((isfr & CORE_PIN6_BITMASK) && intFunc[6]) intFunc[6]();
  284. if ((isfr & CORE_PIN7_BITMASK) && intFunc[7]) intFunc[7]();
  285. if ((isfr & CORE_PIN8_BITMASK) && intFunc[8]) intFunc[8]();
  286. if ((isfr & CORE_PIN14_BITMASK) && intFunc[14]) intFunc[14]();
  287. if ((isfr & CORE_PIN20_BITMASK) && intFunc[20]) intFunc[20]();
  288. if ((isfr & CORE_PIN21_BITMASK) && intFunc[21]) intFunc[21]();
  289. }
  290. #elif defined(__MK64FX512__) || defined(__MK66FX1M0__)
  291. static void porta_interrupt(void)
  292. {
  293. uint32_t isfr = PORTA_ISFR;
  294. PORTA_ISFR = isfr;
  295. if ((isfr & CORE_PIN3_BITMASK) && intFunc[3]) intFunc[3]();
  296. if ((isfr & CORE_PIN4_BITMASK) && intFunc[4]) intFunc[4]();
  297. if ((isfr & CORE_PIN25_BITMASK) && intFunc[25]) intFunc[25]();
  298. if ((isfr & CORE_PIN26_BITMASK) && intFunc[26]) intFunc[26]();
  299. if ((isfr & CORE_PIN27_BITMASK) && intFunc[27]) intFunc[27]();
  300. if ((isfr & CORE_PIN28_BITMASK) && intFunc[28]) intFunc[28]();
  301. if ((isfr & CORE_PIN39_BITMASK) && intFunc[39]) intFunc[39]();
  302. if ((isfr & CORE_PIN40_BITMASK) && intFunc[40]) intFunc[40]();
  303. if ((isfr & CORE_PIN41_BITMASK) && intFunc[41]) intFunc[41]();
  304. if ((isfr & CORE_PIN42_BITMASK) && intFunc[42]) intFunc[42]();
  305. }
  306. static void portb_interrupt(void)
  307. {
  308. uint32_t isfr = PORTB_ISFR;
  309. PORTB_ISFR = isfr;
  310. if ((isfr & CORE_PIN0_BITMASK) && intFunc[0]) intFunc[0]();
  311. if ((isfr & CORE_PIN1_BITMASK) && intFunc[1]) intFunc[1]();
  312. if ((isfr & CORE_PIN16_BITMASK) && intFunc[16]) intFunc[16]();
  313. if ((isfr & CORE_PIN17_BITMASK) && intFunc[17]) intFunc[17]();
  314. if ((isfr & CORE_PIN18_BITMASK) && intFunc[18]) intFunc[18]();
  315. if ((isfr & CORE_PIN19_BITMASK) && intFunc[19]) intFunc[19]();
  316. if ((isfr & CORE_PIN29_BITMASK) && intFunc[29]) intFunc[29]();
  317. if ((isfr & CORE_PIN30_BITMASK) && intFunc[30]) intFunc[30]();
  318. if ((isfr & CORE_PIN31_BITMASK) && intFunc[31]) intFunc[31]();
  319. if ((isfr & CORE_PIN32_BITMASK) && intFunc[32]) intFunc[32]();
  320. if ((isfr & CORE_PIN43_BITMASK) && intFunc[43]) intFunc[43]();
  321. if ((isfr & CORE_PIN44_BITMASK) && intFunc[44]) intFunc[44]();
  322. if ((isfr & CORE_PIN45_BITMASK) && intFunc[45]) intFunc[45]();
  323. if ((isfr & CORE_PIN46_BITMASK) && intFunc[46]) intFunc[46]();
  324. if ((isfr & CORE_PIN49_BITMASK) && intFunc[49]) intFunc[49]();
  325. if ((isfr & CORE_PIN50_BITMASK) && intFunc[50]) intFunc[50]();
  326. }
  327. static void portc_interrupt(void)
  328. {
  329. // TODO: these are inefficent. Use CLZ somehow....
  330. uint32_t isfr = PORTC_ISFR;
  331. PORTC_ISFR = isfr;
  332. if ((isfr & CORE_PIN9_BITMASK) && intFunc[9]) intFunc[9]();
  333. if ((isfr & CORE_PIN10_BITMASK) && intFunc[10]) intFunc[10]();
  334. if ((isfr & CORE_PIN11_BITMASK) && intFunc[11]) intFunc[11]();
  335. if ((isfr & CORE_PIN12_BITMASK) && intFunc[12]) intFunc[12]();
  336. if ((isfr & CORE_PIN13_BITMASK) && intFunc[13]) intFunc[13]();
  337. if ((isfr & CORE_PIN15_BITMASK) && intFunc[15]) intFunc[15]();
  338. if ((isfr & CORE_PIN22_BITMASK) && intFunc[22]) intFunc[22]();
  339. if ((isfr & CORE_PIN23_BITMASK) && intFunc[23]) intFunc[23]();
  340. if ((isfr & CORE_PIN35_BITMASK) && intFunc[35]) intFunc[35]();
  341. if ((isfr & CORE_PIN36_BITMASK) && intFunc[36]) intFunc[36]();
  342. if ((isfr & CORE_PIN37_BITMASK) && intFunc[37]) intFunc[37]();
  343. if ((isfr & CORE_PIN38_BITMASK) && intFunc[38]) intFunc[38]();
  344. }
  345. static void portd_interrupt(void)
  346. {
  347. uint32_t isfr = PORTD_ISFR;
  348. PORTD_ISFR = isfr;
  349. if ((isfr & CORE_PIN2_BITMASK) && intFunc[2]) intFunc[2]();
  350. if ((isfr & CORE_PIN5_BITMASK) && intFunc[5]) intFunc[5]();
  351. if ((isfr & CORE_PIN6_BITMASK) && intFunc[6]) intFunc[6]();
  352. if ((isfr & CORE_PIN7_BITMASK) && intFunc[7]) intFunc[7]();
  353. if ((isfr & CORE_PIN8_BITMASK) && intFunc[8]) intFunc[8]();
  354. if ((isfr & CORE_PIN14_BITMASK) && intFunc[14]) intFunc[14]();
  355. if ((isfr & CORE_PIN20_BITMASK) && intFunc[20]) intFunc[20]();
  356. if ((isfr & CORE_PIN21_BITMASK) && intFunc[21]) intFunc[21]();
  357. if ((isfr & CORE_PIN47_BITMASK) && intFunc[47]) intFunc[47]();
  358. if ((isfr & CORE_PIN48_BITMASK) && intFunc[48]) intFunc[48]();
  359. if ((isfr & CORE_PIN51_BITMASK) && intFunc[51]) intFunc[51]();
  360. if ((isfr & CORE_PIN52_BITMASK) && intFunc[52]) intFunc[52]();
  361. if ((isfr & CORE_PIN53_BITMASK) && intFunc[53]) intFunc[53]();
  362. if ((isfr & CORE_PIN54_BITMASK) && intFunc[54]) intFunc[54]();
  363. if ((isfr & CORE_PIN55_BITMASK) && intFunc[55]) intFunc[55]();
  364. }
  365. static void porte_interrupt(void)
  366. {
  367. uint32_t isfr = PORTE_ISFR;
  368. PORTE_ISFR = isfr;
  369. if ((isfr & CORE_PIN24_BITMASK) && intFunc[24]) intFunc[24]();
  370. if ((isfr & CORE_PIN33_BITMASK) && intFunc[33]) intFunc[33]();
  371. if ((isfr & CORE_PIN34_BITMASK) && intFunc[34]) intFunc[34]();
  372. if ((isfr & CORE_PIN56_BITMASK) && intFunc[56]) intFunc[56]();
  373. if ((isfr & CORE_PIN57_BITMASK) && intFunc[57]) intFunc[57]();
  374. #ifdef USE_SDCARD_PINS
  375. if ((isfr & CORE_PIN58_BITMASK) && intFunc[58]) intFunc[58]();
  376. if ((isfr & CORE_PIN59_BITMASK) && intFunc[59]) intFunc[59]();
  377. if ((isfr & CORE_PIN60_BITMASK) && intFunc[60]) intFunc[60]();
  378. if ((isfr & CORE_PIN61_BITMASK) && intFunc[61]) intFunc[61]();
  379. if ((isfr & CORE_PIN62_BITMASK) && intFunc[62]) intFunc[62]();
  380. if ((isfr & CORE_PIN63_BITMASK) && intFunc[63]) intFunc[63]();
  381. #endif
  382. }
  383. #endif
  384. #if defined(__MK20DX128__) || defined(__MK20DX256__) || defined(__MK64FX512__) || defined(__MK66FX1M0__)
  385. unsigned long rtc_get(void)
  386. {
  387. return RTC_TSR;
  388. }
  389. void rtc_set(unsigned long t)
  390. {
  391. RTC_SR = 0;
  392. RTC_TPR = 0;
  393. RTC_TSR = t;
  394. RTC_SR = RTC_SR_TCE;
  395. }
  396. // adjust is the amount of crystal error to compensate, 1 = 0.1192 ppm
  397. // For example, adjust = -100 is slows the clock by 11.92 ppm
  398. //
  399. void rtc_compensate(int adjust)
  400. {
  401. uint32_t comp, interval, tcr;
  402. // This simple approach tries to maximize the interval.
  403. // Perhaps minimizing TCR would be better, so the
  404. // compensation is distributed more evenly across
  405. // many seconds, rather than saving it all up and then
  406. // altering one second up to +/- 0.38%
  407. if (adjust >= 0) {
  408. comp = adjust;
  409. interval = 256;
  410. while (1) {
  411. tcr = comp * interval;
  412. if (tcr < 128*256) break;
  413. if (--interval == 1) break;
  414. }
  415. tcr = tcr >> 8;
  416. } else {
  417. comp = -adjust;
  418. interval = 256;
  419. while (1) {
  420. tcr = comp * interval;
  421. if (tcr < 129*256) break;
  422. if (--interval == 1) break;
  423. }
  424. tcr = tcr >> 8;
  425. tcr = 256 - tcr;
  426. }
  427. RTC_TCR = ((interval - 1) << 8) | tcr;
  428. }
  429. #else
  430. unsigned long rtc_get(void) { return 0; }
  431. void rtc_set(unsigned long t) { }
  432. void rtc_compensate(int adjust) { }
  433. #endif
  434. #if 0
  435. // TODO: build system should define this
  436. // so RTC is automatically initialized to approx correct time
  437. // at least when the program begins running right after upload
  438. #ifndef TIME_T
  439. #define TIME_T 1350160272
  440. #endif
  441. void init_rtc(void)
  442. {
  443. serial_print("init_rtc\n");
  444. //SIM_SCGC6 |= SIM_SCGC6_RTC;
  445. // enable the RTC crystal oscillator, for approx 12pf crystal
  446. if (!(RTC_CR & RTC_CR_OSCE)) {
  447. serial_print("start RTC oscillator\n");
  448. RTC_SR = 0;
  449. RTC_CR = RTC_CR_SC16P | RTC_CR_SC4P | RTC_CR_OSCE;
  450. }
  451. // should wait for crystal to stabilize.....
  452. serial_print("SR=");
  453. serial_phex32(RTC_SR);
  454. serial_print("\n");
  455. serial_print("CR=");
  456. serial_phex32(RTC_CR);
  457. serial_print("\n");
  458. serial_print("TSR=");
  459. serial_phex32(RTC_TSR);
  460. serial_print("\n");
  461. serial_print("TCR=");
  462. serial_phex32(RTC_TCR);
  463. serial_print("\n");
  464. if (RTC_SR & RTC_SR_TIF) {
  465. // enable the RTC
  466. RTC_SR = 0;
  467. RTC_TPR = 0;
  468. RTC_TSR = TIME_T;
  469. RTC_SR = RTC_SR_TCE;
  470. }
  471. }
  472. #endif
  473. extern void usb_init(void);
  474. // create a default PWM at the same 488.28 Hz as Arduino Uno
  475. #if defined(KINETISK)
  476. #define F_TIMER F_BUS
  477. #elif defined(KINETISL)
  478. #if F_CPU > 16000000
  479. #define F_TIMER (F_PLL/2)
  480. #else
  481. #define F_TIMER (F_PLL)
  482. #endif//Low Power
  483. #endif
  484. #if F_TIMER == 120000000
  485. #define DEFAULT_FTM_MOD (61440 - 1)
  486. #define DEFAULT_FTM_PRESCALE 2
  487. #elif F_TIMER == 108000000
  488. #define DEFAULT_FTM_MOD (55296 - 1)
  489. #define DEFAULT_FTM_PRESCALE 2
  490. #elif F_TIMER == 96000000
  491. #define DEFAULT_FTM_MOD (49152 - 1)
  492. #define DEFAULT_FTM_PRESCALE 2
  493. #elif F_TIMER == 90000000
  494. #define DEFAULT_FTM_MOD (46080 - 1)
  495. #define DEFAULT_FTM_PRESCALE 2
  496. #elif F_TIMER == 80000000
  497. #define DEFAULT_FTM_MOD (40960 - 1)
  498. #define DEFAULT_FTM_PRESCALE 2
  499. #elif F_TIMER == 72000000
  500. #define DEFAULT_FTM_MOD (36864 - 1)
  501. #define DEFAULT_FTM_PRESCALE 2
  502. #elif F_TIMER == 64000000
  503. #define DEFAULT_FTM_MOD (65536 - 1)
  504. #define DEFAULT_FTM_PRESCALE 1
  505. #elif F_TIMER == 60000000
  506. #define DEFAULT_FTM_MOD (61440 - 1)
  507. #define DEFAULT_FTM_PRESCALE 1
  508. #elif F_TIMER == 56000000
  509. #define DEFAULT_FTM_MOD (57344 - 1)
  510. #define DEFAULT_FTM_PRESCALE 1
  511. #elif F_TIMER == 54000000
  512. #define DEFAULT_FTM_MOD (55296 - 1)
  513. #define DEFAULT_FTM_PRESCALE 1
  514. #elif F_TIMER == 48000000
  515. #define DEFAULT_FTM_MOD (49152 - 1)
  516. #define DEFAULT_FTM_PRESCALE 1
  517. #elif F_TIMER == 40000000
  518. #define DEFAULT_FTM_MOD (40960 - 1)
  519. #define DEFAULT_FTM_PRESCALE 1
  520. #elif F_TIMER == 36000000
  521. #define DEFAULT_FTM_MOD (36864 - 1)
  522. #define DEFAULT_FTM_PRESCALE 1
  523. #elif F_TIMER == 24000000
  524. #define DEFAULT_FTM_MOD (49152 - 1)
  525. #define DEFAULT_FTM_PRESCALE 0
  526. #elif F_TIMER == 16000000
  527. #define DEFAULT_FTM_MOD (32768 - 1)
  528. #define DEFAULT_FTM_PRESCALE 0
  529. #elif F_TIMER == 8000000
  530. #define DEFAULT_FTM_MOD (16384 - 1)
  531. #define DEFAULT_FTM_PRESCALE 0
  532. #elif F_TIMER == 4000000
  533. #define DEFAULT_FTM_MOD (8192 - 1)
  534. #define DEFAULT_FTM_PRESCALE 0
  535. #elif F_TIMER == 2000000
  536. #define DEFAULT_FTM_MOD (4096 - 1)
  537. #define DEFAULT_FTM_PRESCALE 0
  538. #endif
  539. //void init_pins(void)
  540. void _init_Teensyduino_internal_(void)
  541. {
  542. #if defined(__MK20DX128__) || defined(__MK20DX256__) || defined(__MK64FX512__) || defined(__MK66FX1M0__)
  543. NVIC_ENABLE_IRQ(IRQ_PORTA);
  544. NVIC_ENABLE_IRQ(IRQ_PORTB);
  545. NVIC_ENABLE_IRQ(IRQ_PORTC);
  546. NVIC_ENABLE_IRQ(IRQ_PORTD);
  547. NVIC_ENABLE_IRQ(IRQ_PORTE);
  548. #elif defined(__MKL26Z64__)
  549. NVIC_ENABLE_IRQ(IRQ_PORTA);
  550. NVIC_ENABLE_IRQ(IRQ_PORTCD);
  551. #endif
  552. //SIM_SCGC6 |= SIM_SCGC6_FTM0; // TODO: use bitband for atomic read-mod-write
  553. //SIM_SCGC6 |= SIM_SCGC6_FTM1;
  554. FTM0_CNT = 0;
  555. FTM0_MOD = DEFAULT_FTM_MOD;
  556. FTM0_C0SC = 0x28; // MSnB:MSnA = 10, ELSnB:ELSnA = 10
  557. FTM0_C1SC = 0x28;
  558. FTM0_C2SC = 0x28;
  559. FTM0_C3SC = 0x28;
  560. FTM0_C4SC = 0x28;
  561. FTM0_C5SC = 0x28;
  562. #if defined(__MK20DX128__) || defined(__MK20DX256__) || defined(__MK64FX512__) || defined(__MK66FX1M0__)
  563. FTM0_C6SC = 0x28;
  564. FTM0_C7SC = 0x28;
  565. #endif
  566. #if defined(__MK64FX512__) || defined(__MK66FX1M0__)
  567. FTM3_C0SC = 0x28;
  568. FTM3_C1SC = 0x28;
  569. FTM3_C2SC = 0x28;
  570. FTM3_C3SC = 0x28;
  571. FTM3_C4SC = 0x28;
  572. FTM3_C5SC = 0x28;
  573. FTM3_C6SC = 0x28;
  574. FTM3_C7SC = 0x28;
  575. #endif
  576. FTM0_SC = FTM_SC_CLKS(1) | FTM_SC_PS(DEFAULT_FTM_PRESCALE);
  577. FTM1_CNT = 0;
  578. FTM1_MOD = DEFAULT_FTM_MOD;
  579. FTM1_C0SC = 0x28;
  580. FTM1_C1SC = 0x28;
  581. FTM1_SC = FTM_SC_CLKS(1) | FTM_SC_PS(DEFAULT_FTM_PRESCALE);
  582. #if defined(__MK20DX256__) || defined(__MK64FX512__) || defined(__MK66FX1M0__) || defined(__MKL26Z64__)
  583. FTM2_CNT = 0;
  584. FTM2_MOD = DEFAULT_FTM_MOD;
  585. FTM2_C0SC = 0x28;
  586. FTM2_C1SC = 0x28;
  587. FTM2_SC = FTM_SC_CLKS(1) | FTM_SC_PS(DEFAULT_FTM_PRESCALE);
  588. #endif
  589. #if defined(__MK64FX512__) || defined(__MK66FX1M0__)
  590. FTM3_CNT = 0;
  591. FTM3_MOD = DEFAULT_FTM_MOD;
  592. FTM3_C0SC = 0x28;
  593. FTM3_C1SC = 0x28;
  594. FTM3_SC = FTM_SC_CLKS(1) | FTM_SC_PS(DEFAULT_FTM_PRESCALE);
  595. #endif
  596. #if defined(__MK66FX1M0__)
  597. SIM_SCGC2 |= SIM_SCGC2_TPM1;
  598. SIM_SOPT2 |= SIM_SOPT2_TPMSRC(2);
  599. TPM1_CNT = 0;
  600. TPM1_MOD = 32767;
  601. TPM1_C0SC = 0x28;
  602. TPM1_C1SC = 0x28;
  603. TPM1_SC = FTM_SC_CLKS(1) | FTM_SC_PS(0);
  604. #endif
  605. analog_init();
  606. // for background about this startup delay, please see these conversations
  607. // https://forum.pjrc.com/threads/36606-startup-time-(400ms)?p=113980&viewfull=1#post113980
  608. // https://forum.pjrc.com/threads/31290-Teensey-3-2-Teensey-Loader-1-24-Issues?p=87273&viewfull=1#post87273
  609. delay(400);
  610. usb_init();
  611. }
  612. #if defined(__MK20DX128__)
  613. #define FTM0_CH0_PIN 22
  614. #define FTM0_CH1_PIN 23
  615. #define FTM0_CH2_PIN 9
  616. #define FTM0_CH3_PIN 10
  617. #define FTM0_CH4_PIN 6
  618. #define FTM0_CH5_PIN 20
  619. #define FTM0_CH6_PIN 21
  620. #define FTM0_CH7_PIN 5
  621. #define FTM1_CH0_PIN 3
  622. #define FTM1_CH1_PIN 4
  623. #elif defined(__MK20DX256__)
  624. #define FTM0_CH0_PIN 22
  625. #define FTM0_CH1_PIN 23
  626. #define FTM0_CH2_PIN 9
  627. #define FTM0_CH3_PIN 10
  628. #define FTM0_CH4_PIN 6
  629. #define FTM0_CH5_PIN 20
  630. #define FTM0_CH6_PIN 21
  631. #define FTM0_CH7_PIN 5
  632. #define FTM1_CH0_PIN 3
  633. #define FTM1_CH1_PIN 4
  634. #define FTM2_CH0_PIN 32
  635. #define FTM2_CH1_PIN 25
  636. #elif defined(__MKL26Z64__)
  637. #define FTM0_CH0_PIN 22
  638. #define FTM0_CH1_PIN 23
  639. #define FTM0_CH2_PIN 9
  640. #define FTM0_CH3_PIN 10
  641. #define FTM0_CH4_PIN 6
  642. #define FTM0_CH5_PIN 20
  643. #define FTM1_CH0_PIN 16
  644. #define FTM1_CH1_PIN 17
  645. #define FTM2_CH0_PIN 3
  646. #define FTM2_CH1_PIN 4
  647. #elif defined(__MK64FX512__)
  648. #define FTM0_CH0_PIN 22
  649. #define FTM0_CH1_PIN 23
  650. #define FTM0_CH2_PIN 9
  651. #define FTM0_CH3_PIN 10
  652. #define FTM0_CH4_PIN 6
  653. #define FTM0_CH5_PIN 20
  654. #define FTM0_CH6_PIN 21
  655. #define FTM0_CH7_PIN 5
  656. #define FTM1_CH0_PIN 3
  657. #define FTM1_CH1_PIN 4
  658. #define FTM2_CH0_PIN 29
  659. #define FTM2_CH1_PIN 30
  660. #define FTM3_CH0_PIN 2
  661. #define FTM3_CH1_PIN 14
  662. #define FTM3_CH2_PIN 7
  663. #define FTM3_CH3_PIN 8
  664. #define FTM3_CH4_PIN 35
  665. #define FTM3_CH5_PIN 36
  666. #define FTM3_CH6_PIN 37
  667. #define FTM3_CH7_PIN 38
  668. #elif defined(__MK66FX1M0__)
  669. #define FTM0_CH0_PIN 22
  670. #define FTM0_CH1_PIN 23
  671. #define FTM0_CH2_PIN 9
  672. #define FTM0_CH3_PIN 10
  673. #define FTM0_CH4_PIN 6
  674. #define FTM0_CH5_PIN 20
  675. #define FTM0_CH6_PIN 21
  676. #define FTM0_CH7_PIN 5
  677. #define FTM1_CH0_PIN 3
  678. #define FTM1_CH1_PIN 4
  679. #define FTM2_CH0_PIN 29
  680. #define FTM2_CH1_PIN 30
  681. #define FTM3_CH0_PIN 2
  682. #define FTM3_CH1_PIN 14
  683. #define FTM3_CH2_PIN 7
  684. #define FTM3_CH3_PIN 8
  685. #define FTM3_CH4_PIN 35
  686. #define FTM3_CH5_PIN 36
  687. #define FTM3_CH6_PIN 37
  688. #define FTM3_CH7_PIN 38
  689. #define TPM1_CH0_PIN 16
  690. #define TPM1_CH1_PIN 17
  691. #endif
  692. #define FTM_PINCFG(pin) FTM_PINCFG2(pin)
  693. #define FTM_PINCFG2(pin) CORE_PIN ## pin ## _CONFIG
  694. static uint8_t analog_write_res = 8;
  695. // SOPT4 is SIM select clocks?
  696. // FTM is clocked by the bus clock, either 24 or 48 MHz
  697. // input capture can be FTM1_CH0, CMP0 or CMP1 or USB start of frame
  698. // 24 MHz with reload 49152 to match Arduino's speed = 488.28125 Hz
  699. void analogWrite(uint8_t pin, int val)
  700. {
  701. uint32_t cval, max;
  702. #if defined(__MK20DX256__)
  703. if (pin == A14) {
  704. uint8_t res = analog_write_res;
  705. if (res < 12) {
  706. val <<= 12 - res;
  707. } else if (res > 12) {
  708. val >>= res - 12;
  709. }
  710. analogWriteDAC0(val);
  711. return;
  712. }
  713. #elif defined(__MKL26Z64__)
  714. if (pin == A12) {
  715. uint8_t res = analog_write_res;
  716. if (res < 12) {
  717. val <<= 12 - res;
  718. } else if (res > 12) {
  719. val >>= res - 12;
  720. }
  721. analogWriteDAC0(val);
  722. return;
  723. }
  724. #elif defined(__MK64FX512__) || defined(__MK66FX1M0__)
  725. if (pin == A21 || pin == A22) {
  726. uint8_t res = analog_write_res;
  727. if (res < 12) {
  728. val <<= 12 - res;
  729. } else if (res > 12) {
  730. val >>= res - 12;
  731. }
  732. if (pin == A21) analogWriteDAC0(val);
  733. else analogWriteDAC1(val);
  734. return;
  735. }
  736. #endif
  737. max = 1 << analog_write_res;
  738. if (val <= 0) {
  739. digitalWrite(pin, LOW);
  740. pinMode(pin, OUTPUT); // TODO: implement OUTPUT_LOW
  741. return;
  742. } else if (val >= max) {
  743. digitalWrite(pin, HIGH);
  744. pinMode(pin, OUTPUT); // TODO: implement OUTPUT_HIGH
  745. return;
  746. }
  747. //serial_print("analogWrite\n");
  748. //serial_print("val = ");
  749. //serial_phex32(val);
  750. //serial_print("\n");
  751. //serial_print("analog_write_res = ");
  752. //serial_phex(analog_write_res);
  753. //serial_print("\n");
  754. if (pin == FTM1_CH0_PIN || pin == FTM1_CH1_PIN) {
  755. cval = ((uint32_t)val * (uint32_t)(FTM1_MOD + 1)) >> analog_write_res;
  756. #if defined(FTM2_CH0_PIN)
  757. } else if (pin == FTM2_CH0_PIN || pin == FTM2_CH1_PIN) {
  758. cval = ((uint32_t)val * (uint32_t)(FTM2_MOD + 1)) >> analog_write_res;
  759. #endif
  760. #if defined(FTM3_CH0_PIN)
  761. } else if (pin == FTM3_CH0_PIN || pin == FTM3_CH1_PIN || pin == FTM3_CH2_PIN
  762. || pin == FTM3_CH3_PIN || pin == FTM3_CH4_PIN || pin == FTM3_CH5_PIN
  763. || pin == FTM3_CH6_PIN || pin == FTM3_CH7_PIN) {
  764. cval = ((uint32_t)val * (uint32_t)(FTM3_MOD + 1)) >> analog_write_res;
  765. #endif
  766. #if defined(TPM1_CH0_PIN)
  767. } else if (pin == TPM1_CH0_PIN || pin == TPM1_CH1_PIN) {
  768. cval = ((uint32_t)val * (uint32_t)(TPM1_MOD + 1)) >> analog_write_res;
  769. #endif
  770. } else {
  771. cval = ((uint32_t)val * (uint32_t)(FTM0_MOD + 1)) >> analog_write_res;
  772. }
  773. //serial_print("cval = ");
  774. //serial_phex32(cval);
  775. //serial_print("\n");
  776. switch (pin) {
  777. #ifdef FTM0_CH0_PIN
  778. case FTM0_CH0_PIN: // PTC1, FTM0_CH0
  779. FTM0_C0V = cval;
  780. FTM_PINCFG(FTM0_CH0_PIN) = PORT_PCR_MUX(4) | PORT_PCR_DSE | PORT_PCR_SRE;
  781. break;
  782. #endif
  783. #ifdef FTM0_CH1_PIN
  784. case FTM0_CH1_PIN: // PTC2, FTM0_CH1
  785. FTM0_C1V = cval;
  786. FTM_PINCFG(FTM0_CH1_PIN) = PORT_PCR_MUX(4) | PORT_PCR_DSE | PORT_PCR_SRE;
  787. break;
  788. #endif
  789. #ifdef FTM0_CH2_PIN
  790. case FTM0_CH2_PIN: // PTC3, FTM0_CH2
  791. FTM0_C2V = cval;
  792. FTM_PINCFG(FTM0_CH2_PIN) = PORT_PCR_MUX(4) | PORT_PCR_DSE | PORT_PCR_SRE;
  793. break;
  794. #endif
  795. #ifdef FTM0_CH3_PIN
  796. case FTM0_CH3_PIN: // PTC4, FTM0_CH3
  797. FTM0_C3V = cval;
  798. FTM_PINCFG(FTM0_CH3_PIN) = PORT_PCR_MUX(4) | PORT_PCR_DSE | PORT_PCR_SRE;
  799. break;
  800. #endif
  801. #ifdef FTM0_CH4_PIN
  802. case FTM0_CH4_PIN: // PTD4, FTM0_CH4
  803. FTM0_C4V = cval;
  804. FTM_PINCFG(FTM0_CH4_PIN) = PORT_PCR_MUX(4) | PORT_PCR_DSE | PORT_PCR_SRE;
  805. break;
  806. #endif
  807. #ifdef FTM0_CH5_PIN
  808. case FTM0_CH5_PIN: // PTD5, FTM0_CH5
  809. FTM0_C5V = cval;
  810. FTM_PINCFG(FTM0_CH5_PIN) = PORT_PCR_MUX(4) | PORT_PCR_DSE | PORT_PCR_SRE;
  811. break;
  812. #endif
  813. #ifdef FTM0_CH6_PIN
  814. case FTM0_CH6_PIN: // PTD6, FTM0_CH6
  815. FTM0_C6V = cval;
  816. FTM_PINCFG(FTM0_CH6_PIN) = PORT_PCR_MUX(4) | PORT_PCR_DSE | PORT_PCR_SRE;
  817. break;
  818. #endif
  819. #ifdef FTM0_CH7_PIN
  820. case FTM0_CH7_PIN: // PTD7, FTM0_CH7
  821. FTM0_C7V = cval;
  822. FTM_PINCFG(FTM0_CH7_PIN) = PORT_PCR_MUX(4) | PORT_PCR_DSE | PORT_PCR_SRE;
  823. break;
  824. #endif
  825. #ifdef FTM1_CH0_PIN
  826. case FTM1_CH0_PIN: // PTA12, FTM1_CH0
  827. FTM1_C0V = cval;
  828. FTM_PINCFG(FTM1_CH0_PIN) = PORT_PCR_MUX(3) | PORT_PCR_DSE | PORT_PCR_SRE;
  829. break;
  830. #endif
  831. #ifdef FTM1_CH1_PIN
  832. case FTM1_CH1_PIN: // PTA13, FTM1_CH1
  833. FTM1_C1V = cval;
  834. FTM_PINCFG(FTM1_CH1_PIN) = PORT_PCR_MUX(3) | PORT_PCR_DSE | PORT_PCR_SRE;
  835. break;
  836. #endif
  837. #ifdef FTM2_CH0_PIN
  838. case FTM2_CH0_PIN: // PTB18, FTM2_CH0
  839. FTM2_C0V = cval;
  840. FTM_PINCFG(FTM2_CH0_PIN) = PORT_PCR_MUX(3) | PORT_PCR_DSE | PORT_PCR_SRE;
  841. break;
  842. #endif
  843. #ifdef FTM2_CH1_PIN
  844. case FTM2_CH1_PIN: // PTB19, FTM1_CH1
  845. FTM2_C1V = cval;
  846. FTM_PINCFG(FTM2_CH1_PIN) = PORT_PCR_MUX(3) | PORT_PCR_DSE | PORT_PCR_SRE;
  847. break;
  848. #endif
  849. #ifdef FTM3_CH0_PIN
  850. case FTM3_CH0_PIN:
  851. FTM3_C0V = cval;
  852. FTM_PINCFG(FTM3_CH0_PIN) = PORT_PCR_MUX(4) | PORT_PCR_DSE | PORT_PCR_SRE;
  853. break;
  854. #endif
  855. #ifdef FTM3_CH1_PIN
  856. case FTM3_CH1_PIN:
  857. FTM3_C1V = cval;
  858. FTM_PINCFG(FTM3_CH1_PIN) = PORT_PCR_MUX(4) | PORT_PCR_DSE | PORT_PCR_SRE;
  859. break;
  860. #endif
  861. #ifdef FTM3_CH2_PIN
  862. case FTM3_CH2_PIN:
  863. FTM3_C2V = cval;
  864. FTM_PINCFG(FTM3_CH2_PIN) = PORT_PCR_MUX(4) | PORT_PCR_DSE | PORT_PCR_SRE;
  865. break;
  866. #endif
  867. #ifdef FTM3_CH3_PIN
  868. case FTM3_CH3_PIN:
  869. FTM3_C3V = cval;
  870. FTM_PINCFG(FTM3_CH3_PIN) = PORT_PCR_MUX(4) | PORT_PCR_DSE | PORT_PCR_SRE;
  871. break;
  872. #endif
  873. #ifdef FTM3_CH4_PIN
  874. case FTM3_CH4_PIN:
  875. FTM3_C4V = cval;
  876. FTM_PINCFG(FTM3_CH4_PIN) = PORT_PCR_MUX(3) | PORT_PCR_DSE | PORT_PCR_SRE;
  877. break;
  878. #endif
  879. #ifdef FTM3_CH5_PIN
  880. case FTM3_CH5_PIN:
  881. FTM3_C5V = cval;
  882. FTM_PINCFG(FTM3_CH5_PIN) = PORT_PCR_MUX(3) | PORT_PCR_DSE | PORT_PCR_SRE;
  883. break;
  884. #endif
  885. #ifdef FTM3_CH6_PIN
  886. case FTM3_CH6_PIN:
  887. FTM3_C6V = cval;
  888. FTM_PINCFG(FTM3_CH6_PIN) = PORT_PCR_MUX(3) | PORT_PCR_DSE | PORT_PCR_SRE;
  889. break;
  890. #endif
  891. #ifdef FTM3_CH7_PIN
  892. case FTM3_CH7_PIN:
  893. FTM3_C7V = cval;
  894. FTM_PINCFG(FTM3_CH7_PIN) = PORT_PCR_MUX(3) | PORT_PCR_DSE | PORT_PCR_SRE;
  895. break;
  896. #endif
  897. #ifdef TPM1_CH0_PIN
  898. case TPM1_CH0_PIN:
  899. TPM1_C0V = cval;
  900. FTM_PINCFG(TPM1_CH0_PIN) = PORT_PCR_MUX(6) | PORT_PCR_DSE | PORT_PCR_SRE;
  901. break;
  902. #endif
  903. #ifdef TPM1_CH1_PIN
  904. case TPM1_CH1_PIN:
  905. TPM1_C1V = cval;
  906. FTM_PINCFG(TPM1_CH1_PIN) = PORT_PCR_MUX(6) | PORT_PCR_DSE | PORT_PCR_SRE;
  907. break;
  908. #endif
  909. default:
  910. digitalWrite(pin, (val > 127) ? HIGH : LOW);
  911. pinMode(pin, OUTPUT);
  912. }
  913. }
  914. void analogWriteRes(uint32_t bits)
  915. {
  916. if (bits < 1) {
  917. bits = 1;
  918. } else if (bits > 16) {
  919. bits = 16;
  920. }
  921. analog_write_res = bits;
  922. }
  923. void analogWriteFrequency(uint8_t pin, float frequency)
  924. {
  925. uint32_t prescale, mod, ftmClock, ftmClockSource;
  926. float minfreq;
  927. //serial_print("analogWriteFrequency: pin = ");
  928. //serial_phex(pin);
  929. //serial_print(", freq = ");
  930. //serial_phex32((uint32_t)frequency);
  931. //serial_print("\n");
  932. #ifdef TPM1_CH0_PIN
  933. if (pin == TPM1_CH0_PIN || pin == TPM1_CH1_PIN) {
  934. ftmClockSource = 1;
  935. ftmClock = 16000000;
  936. } else
  937. #endif
  938. if (frequency < (float)(F_TIMER >> 7) / 65536.0f) {
  939. // frequency is too low for working with F_TIMER:
  940. ftmClockSource = 2; // Use alternative 31250Hz clock source
  941. ftmClock = 31250; // Set variable for the actual timer clock frequency
  942. } else {
  943. ftmClockSource = 1; // Use default F_TIMER clock source
  944. ftmClock = F_TIMER; // Set variable for the actual timer clock frequency
  945. }
  946. for (prescale = 0; prescale < 7; prescale++) {
  947. minfreq = (float)(ftmClock >> prescale) / 65536.0f; //Use ftmClock instead of F_TIMER
  948. if (frequency >= minfreq) break;
  949. }
  950. //serial_print("F_TIMER/ftm_Clock = ");
  951. //serial_phex32(ftmClock >> prescale);
  952. //serial_print("\n");
  953. //serial_print("prescale = ");
  954. //serial_phex(prescale);
  955. //serial_print("\n");
  956. mod = (float)(ftmClock >> prescale) / frequency - 0.5f; //Use ftmClock instead of F_TIMER
  957. if (mod > 65535) mod = 65535;
  958. //serial_print("mod = ");
  959. //serial_phex32(mod);
  960. //serial_print("\n");
  961. if (pin == FTM1_CH0_PIN || pin == FTM1_CH1_PIN) {
  962. FTM1_SC = 0;
  963. FTM1_CNT = 0;
  964. FTM1_MOD = mod;
  965. FTM1_SC = FTM_SC_CLKS(ftmClockSource) | FTM_SC_PS(prescale); //Use ftmClockSource instead of 1
  966. } else if (pin == FTM0_CH0_PIN || pin == FTM0_CH1_PIN
  967. || pin == FTM0_CH2_PIN || pin == FTM0_CH3_PIN
  968. || pin == FTM0_CH4_PIN || pin == FTM0_CH5_PIN
  969. #ifdef FTM0_CH6_PIN
  970. || pin == FTM0_CH6_PIN || pin == FTM0_CH7_PIN
  971. #endif
  972. ) {
  973. FTM0_SC = 0;
  974. FTM0_CNT = 0;
  975. FTM0_MOD = mod;
  976. FTM0_SC = FTM_SC_CLKS(ftmClockSource) | FTM_SC_PS(prescale); //Use ftmClockSource instead of 1
  977. }
  978. #ifdef FTM2_CH0_PIN
  979. else if (pin == FTM2_CH0_PIN || pin == FTM2_CH1_PIN) {
  980. FTM2_SC = 0;
  981. FTM2_CNT = 0;
  982. FTM2_MOD = mod;
  983. FTM2_SC = FTM_SC_CLKS(ftmClockSource) | FTM_SC_PS(prescale); //Use ftmClockSource instead of 1
  984. }
  985. #endif
  986. #ifdef FTM3_CH0_PIN
  987. else if (pin == FTM3_CH0_PIN || pin == FTM3_CH1_PIN
  988. || pin == FTM3_CH2_PIN || pin == FTM3_CH3_PIN
  989. || pin == FTM3_CH4_PIN || pin == FTM3_CH5_PIN
  990. || pin == FTM3_CH6_PIN || pin == FTM3_CH7_PIN) {
  991. FTM3_SC = 0;
  992. FTM3_CNT = 0;
  993. FTM3_MOD = mod;
  994. FTM3_SC = FTM_SC_CLKS(ftmClockSource) | FTM_SC_PS(prescale); //Use the new ftmClockSource instead of 1
  995. }
  996. #endif
  997. #ifdef TPM1_CH0_PIN
  998. else if (pin == TPM1_CH0_PIN || pin == TPM1_CH1_PIN) {
  999. TPM1_SC = 0;
  1000. TPM1_CNT = 0;
  1001. TPM1_MOD = mod;
  1002. TPM1_SC = FTM_SC_CLKS(ftmClockSource) | FTM_SC_PS(prescale);
  1003. }
  1004. #endif
  1005. }
  1006. // TODO: startup code needs to initialize all pins to GPIO mode, input by default
  1007. void digitalWrite(uint8_t pin, uint8_t val)
  1008. {
  1009. if (pin >= CORE_NUM_DIGITAL) return;
  1010. #ifdef KINETISK
  1011. if (*portModeRegister(pin)) {
  1012. if (val) {
  1013. *portSetRegister(pin) = 1;
  1014. } else {
  1015. *portClearRegister(pin) = 1;
  1016. }
  1017. #else
  1018. if (*portModeRegister(pin) & digitalPinToBitMask(pin)) {
  1019. if (val) {
  1020. *portSetRegister(pin) = digitalPinToBitMask(pin);
  1021. } else {
  1022. *portClearRegister(pin) = digitalPinToBitMask(pin);
  1023. }
  1024. #endif
  1025. } else {
  1026. volatile uint32_t *config = portConfigRegister(pin);
  1027. if (val) {
  1028. // TODO use bitband for atomic read-mod-write
  1029. *config |= (PORT_PCR_PE | PORT_PCR_PS);
  1030. //*config = PORT_PCR_MUX(1) | PORT_PCR_PE | PORT_PCR_PS;
  1031. } else {
  1032. // TODO use bitband for atomic read-mod-write
  1033. *config &= ~(PORT_PCR_PE);
  1034. //*config = PORT_PCR_MUX(1);
  1035. }
  1036. }
  1037. }
  1038. uint8_t digitalRead(uint8_t pin)
  1039. {
  1040. if (pin >= CORE_NUM_DIGITAL) return 0;
  1041. #ifdef KINETISK
  1042. return *portInputRegister(pin);
  1043. #else
  1044. return (*portInputRegister(pin) & digitalPinToBitMask(pin)) ? 1 : 0;
  1045. #endif
  1046. }
  1047. void pinMode(uint8_t pin, uint8_t mode)
  1048. {
  1049. volatile uint32_t *config;
  1050. if (pin >= CORE_NUM_DIGITAL) return;
  1051. config = portConfigRegister(pin);
  1052. if (mode == OUTPUT || mode == OUTPUT_OPENDRAIN) {
  1053. #ifdef KINETISK
  1054. *portModeRegister(pin) = 1;
  1055. #else
  1056. *portModeRegister(pin) |= digitalPinToBitMask(pin); // TODO: atomic
  1057. #endif
  1058. *config = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
  1059. if (mode == OUTPUT_OPENDRAIN) {
  1060. *config |= PORT_PCR_ODE;
  1061. } else {
  1062. *config &= ~PORT_PCR_ODE;
  1063. }
  1064. } else {
  1065. #ifdef KINETISK
  1066. *portModeRegister(pin) = 0;
  1067. #else
  1068. *portModeRegister(pin) &= ~digitalPinToBitMask(pin);
  1069. #endif
  1070. if (mode == INPUT || mode == INPUT_PULLUP || mode == INPUT_PULLDOWN) {
  1071. *config = PORT_PCR_MUX(1);
  1072. if (mode == INPUT_PULLUP) {
  1073. *config |= (PORT_PCR_PE | PORT_PCR_PS); // pullup
  1074. } else if (mode == INPUT_PULLDOWN) {
  1075. *config |= (PORT_PCR_PE); // pulldown
  1076. *config &= ~(PORT_PCR_PS);
  1077. }
  1078. } else {
  1079. *config = PORT_PCR_MUX(1) | PORT_PCR_PE | PORT_PCR_PS; // pullup
  1080. }
  1081. }
  1082. }
  1083. void _shiftOut(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder, uint8_t value)
  1084. {
  1085. if (bitOrder == LSBFIRST) {
  1086. shiftOut_lsbFirst(dataPin, clockPin, value);
  1087. } else {
  1088. shiftOut_msbFirst(dataPin, clockPin, value);
  1089. }
  1090. }
  1091. void shiftOut_lsbFirst(uint8_t dataPin, uint8_t clockPin, uint8_t value)
  1092. {
  1093. uint8_t mask;
  1094. for (mask=0x01; mask; mask <<= 1) {
  1095. digitalWrite(dataPin, value & mask);
  1096. digitalWrite(clockPin, HIGH);
  1097. digitalWrite(clockPin, LOW);
  1098. }
  1099. }
  1100. void shiftOut_msbFirst(uint8_t dataPin, uint8_t clockPin, uint8_t value)
  1101. {
  1102. uint8_t mask;
  1103. for (mask=0x80; mask; mask >>= 1) {
  1104. digitalWrite(dataPin, value & mask);
  1105. digitalWrite(clockPin, HIGH);
  1106. digitalWrite(clockPin, LOW);
  1107. }
  1108. }
  1109. uint8_t _shiftIn(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder)
  1110. {
  1111. if (bitOrder == LSBFIRST) {
  1112. return shiftIn_lsbFirst(dataPin, clockPin);
  1113. } else {
  1114. return shiftIn_msbFirst(dataPin, clockPin);
  1115. }
  1116. }
  1117. uint8_t shiftIn_lsbFirst(uint8_t dataPin, uint8_t clockPin)
  1118. {
  1119. uint8_t mask, value=0;
  1120. for (mask=0x01; mask; mask <<= 1) {
  1121. digitalWrite(clockPin, HIGH);
  1122. if (digitalRead(dataPin)) value |= mask;
  1123. digitalWrite(clockPin, LOW);
  1124. }
  1125. return value;
  1126. }
  1127. uint8_t shiftIn_msbFirst(uint8_t dataPin, uint8_t clockPin)
  1128. {
  1129. uint8_t mask, value=0;
  1130. for (mask=0x80; mask; mask >>= 1) {
  1131. digitalWrite(clockPin, HIGH);
  1132. if (digitalRead(dataPin)) value |= mask;
  1133. digitalWrite(clockPin, LOW);
  1134. }
  1135. return value;
  1136. }
  1137. // the systick interrupt is supposed to increment this at 1 kHz rate
  1138. volatile uint32_t systick_millis_count = 0;
  1139. //uint32_t systick_current, systick_count, systick_istatus; // testing only
  1140. uint32_t micros(void)
  1141. {
  1142. uint32_t count, current, istatus;
  1143. __disable_irq();
  1144. current = SYST_CVR;
  1145. count = systick_millis_count;
  1146. istatus = SCB_ICSR; // bit 26 indicates if systick exception pending
  1147. __enable_irq();
  1148. //systick_current = current;
  1149. //systick_count = count;
  1150. //systick_istatus = istatus & SCB_ICSR_PENDSTSET ? 1 : 0;
  1151. if ((istatus & SCB_ICSR_PENDSTSET) && current > 50) count++;
  1152. current = ((F_CPU / 1000) - 1) - current;
  1153. #if defined(KINETISL) && F_CPU == 48000000
  1154. return count * 1000 + ((current * (uint32_t)87381) >> 22);
  1155. #elif defined(KINETISL) && F_CPU == 24000000
  1156. return count * 1000 + ((current * (uint32_t)174763) >> 22);
  1157. #endif
  1158. return count * 1000 + current / (F_CPU / 1000000);
  1159. }
  1160. void delay(uint32_t ms)
  1161. {
  1162. uint32_t start = micros();
  1163. if (ms > 0) {
  1164. while (1) {
  1165. while ((micros() - start) >= 1000) {
  1166. ms--;
  1167. if (ms == 0) return;
  1168. start += 1000;
  1169. }
  1170. yield();
  1171. }
  1172. }
  1173. }
  1174. // TODO: verify these result in correct timeouts...
  1175. #if F_CPU == 240000000
  1176. #define PULSEIN_LOOPS_PER_USEC 33
  1177. #elif F_CPU == 216000000
  1178. #define PULSEIN_LOOPS_PER_USEC 31
  1179. #elif F_CPU == 192000000
  1180. #define PULSEIN_LOOPS_PER_USEC 29
  1181. #elif F_CPU == 180000000
  1182. #define PULSEIN_LOOPS_PER_USEC 27
  1183. #elif F_CPU == 168000000
  1184. #define PULSEIN_LOOPS_PER_USEC 25
  1185. #elif F_CPU == 144000000
  1186. #define PULSEIN_LOOPS_PER_USEC 21
  1187. #elif F_CPU == 120000000
  1188. #define PULSEIN_LOOPS_PER_USEC 18
  1189. #elif F_CPU == 96000000
  1190. #define PULSEIN_LOOPS_PER_USEC 14
  1191. #elif F_CPU == 72000000
  1192. #define PULSEIN_LOOPS_PER_USEC 10
  1193. #elif F_CPU == 48000000
  1194. #define PULSEIN_LOOPS_PER_USEC 7
  1195. #elif F_CPU == 24000000
  1196. #define PULSEIN_LOOPS_PER_USEC 4
  1197. #elif F_CPU == 16000000
  1198. #define PULSEIN_LOOPS_PER_USEC 1
  1199. #elif F_CPU == 8000000
  1200. #define PULSEIN_LOOPS_PER_USEC 1
  1201. #elif F_CPU == 4000000
  1202. #define PULSEIN_LOOPS_PER_USEC 1
  1203. #elif F_CPU == 2000000
  1204. #define PULSEIN_LOOPS_PER_USEC 1
  1205. #endif
  1206. #if defined(KINETISK)
  1207. uint32_t pulseIn_high(volatile uint8_t *reg, uint32_t timeout)
  1208. {
  1209. uint32_t timeout_count = timeout * PULSEIN_LOOPS_PER_USEC;
  1210. uint32_t usec_start, usec_stop;
  1211. // wait for any previous pulse to end
  1212. while (*reg) {
  1213. if (--timeout_count == 0) return 0;
  1214. }
  1215. // wait for the pulse to start
  1216. while (!*reg) {
  1217. if (--timeout_count == 0) return 0;
  1218. }
  1219. usec_start = micros();
  1220. // wait for the pulse to stop
  1221. while (*reg) {
  1222. if (--timeout_count == 0) return 0;
  1223. }
  1224. usec_stop = micros();
  1225. return usec_stop - usec_start;
  1226. }
  1227. uint32_t pulseIn_low(volatile uint8_t *reg, uint32_t timeout)
  1228. {
  1229. uint32_t timeout_count = timeout * PULSEIN_LOOPS_PER_USEC;
  1230. uint32_t usec_start, usec_stop;
  1231. // wait for any previous pulse to end
  1232. while (!*reg) {
  1233. if (--timeout_count == 0) return 0;
  1234. }
  1235. // wait for the pulse to start
  1236. while (*reg) {
  1237. if (--timeout_count == 0) return 0;
  1238. }
  1239. usec_start = micros();
  1240. // wait for the pulse to stop
  1241. while (!*reg) {
  1242. if (--timeout_count == 0) return 0;
  1243. }
  1244. usec_stop = micros();
  1245. return usec_stop - usec_start;
  1246. }
  1247. // TODO: an inline version should handle the common case where state is const
  1248. uint32_t pulseIn(uint8_t pin, uint8_t state, uint32_t timeout)
  1249. {
  1250. if (pin >= CORE_NUM_DIGITAL) return 0;
  1251. if (state) return pulseIn_high(portInputRegister(pin), timeout);
  1252. return pulseIn_low(portInputRegister(pin), timeout);;
  1253. }
  1254. #elif defined(KINETISL)
  1255. // For TeencyLC need to use mask on the input register as the register is shared by several IO pins
  1256. uint32_t pulseIn_high(volatile uint8_t *reg, uint8_t mask, uint32_t timeout)
  1257. {
  1258. uint32_t timeout_count = timeout * PULSEIN_LOOPS_PER_USEC;
  1259. uint32_t usec_start, usec_stop;
  1260. // wait for any previous pulse to end
  1261. while (*reg & mask) {
  1262. if (--timeout_count == 0) return -1;
  1263. }
  1264. // wait for the pulse to start
  1265. while (!(*reg & mask)) {
  1266. if (--timeout_count == 0) return 0;
  1267. }
  1268. usec_start = micros();
  1269. // wait for the pulse to stop
  1270. while (*reg & mask) {
  1271. if (--timeout_count == 0) return 0;
  1272. }
  1273. usec_stop = micros();
  1274. return usec_stop - usec_start;
  1275. }
  1276. uint32_t pulseIn_low(volatile uint8_t *reg, uint8_t mask, uint32_t timeout)
  1277. {
  1278. uint32_t timeout_count = timeout * PULSEIN_LOOPS_PER_USEC;
  1279. uint32_t usec_start, usec_stop;
  1280. // wait for any previous pulse to end
  1281. while (!(*reg & mask)) {
  1282. if (--timeout_count == 0) return 0;
  1283. }
  1284. // wait for the pulse to start
  1285. while (*reg & mask) {
  1286. if (--timeout_count == 0) return 0;
  1287. }
  1288. usec_start = micros();
  1289. // wait for the pulse to stop
  1290. while (!(*reg & mask)) {
  1291. if (--timeout_count == 0) return 0;
  1292. }
  1293. usec_stop = micros();
  1294. return usec_stop - usec_start;
  1295. }
  1296. // TODO: an inline version should handle the common case where state is const
  1297. uint32_t pulseIn(uint8_t pin, uint8_t state, uint32_t timeout)
  1298. {
  1299. if (pin >= CORE_NUM_DIGITAL) return 0;
  1300. if (state) return pulseIn_high(portInputRegister(pin), digitalPinToBitMask(pin), timeout);
  1301. return pulseIn_low(portInputRegister(pin), digitalPinToBitMask(pin), timeout);;
  1302. }
  1303. #endif