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

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