Teensy 4.1 core updated for C++20
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

1340 行
44KB

  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. analog_init();
  597. // for background about this startup delay, please see this conversation
  598. // https://forum.pjrc.com/threads/31290-Teensey-3-2-Teensey-Loader-1-24-Issues?p=87273&viewfull=1#post87273
  599. delay(400);
  600. usb_init();
  601. }
  602. #if defined(__MK20DX128__)
  603. #define FTM0_CH0_PIN 22
  604. #define FTM0_CH1_PIN 23
  605. #define FTM0_CH2_PIN 9
  606. #define FTM0_CH3_PIN 10
  607. #define FTM0_CH4_PIN 6
  608. #define FTM0_CH5_PIN 20
  609. #define FTM0_CH6_PIN 21
  610. #define FTM0_CH7_PIN 5
  611. #define FTM1_CH0_PIN 3
  612. #define FTM1_CH1_PIN 4
  613. #elif defined(__MK20DX256__)
  614. #define FTM0_CH0_PIN 22
  615. #define FTM0_CH1_PIN 23
  616. #define FTM0_CH2_PIN 9
  617. #define FTM0_CH3_PIN 10
  618. #define FTM0_CH4_PIN 6
  619. #define FTM0_CH5_PIN 20
  620. #define FTM0_CH6_PIN 21
  621. #define FTM0_CH7_PIN 5
  622. #define FTM1_CH0_PIN 3
  623. #define FTM1_CH1_PIN 4
  624. #define FTM2_CH0_PIN 32
  625. #define FTM2_CH1_PIN 25
  626. #elif defined(__MKL26Z64__)
  627. #define FTM0_CH0_PIN 22
  628. #define FTM0_CH1_PIN 23
  629. #define FTM0_CH2_PIN 9
  630. #define FTM0_CH3_PIN 10
  631. #define FTM0_CH4_PIN 6
  632. #define FTM0_CH5_PIN 20
  633. #define FTM1_CH0_PIN 16
  634. #define FTM1_CH1_PIN 17
  635. #define FTM2_CH0_PIN 3
  636. #define FTM2_CH1_PIN 4
  637. #elif defined(__MK64FX512__) || defined(__MK66FX1M0__)
  638. #define FTM0_CH0_PIN 22
  639. #define FTM0_CH1_PIN 23
  640. #define FTM0_CH2_PIN 9
  641. #define FTM0_CH3_PIN 10
  642. #define FTM0_CH4_PIN 6
  643. #define FTM0_CH5_PIN 20
  644. #define FTM0_CH6_PIN 21
  645. #define FTM0_CH7_PIN 5
  646. #define FTM1_CH0_PIN 3
  647. #define FTM1_CH1_PIN 4
  648. #define FTM2_CH0_PIN 29
  649. #define FTM2_CH1_PIN 30
  650. #define FTM3_CH0_PIN 2
  651. #define FTM3_CH1_PIN 14
  652. #define FTM3_CH2_PIN 7
  653. #define FTM3_CH3_PIN 8
  654. #define FTM3_CH4_PIN 35
  655. #define FTM3_CH5_PIN 36
  656. #define FTM3_CH6_PIN 37
  657. #define FTM3_CH7_PIN 38
  658. #endif
  659. #define FTM_PINCFG(pin) FTM_PINCFG2(pin)
  660. #define FTM_PINCFG2(pin) CORE_PIN ## pin ## _CONFIG
  661. static uint8_t analog_write_res = 8;
  662. // SOPT4 is SIM select clocks?
  663. // FTM is clocked by the bus clock, either 24 or 48 MHz
  664. // input capture can be FTM1_CH0, CMP0 or CMP1 or USB start of frame
  665. // 24 MHz with reload 49152 to match Arduino's speed = 488.28125 Hz
  666. void analogWrite(uint8_t pin, int val)
  667. {
  668. uint32_t cval, max;
  669. #if defined(__MK20DX256__)
  670. if (pin == A14) {
  671. uint8_t res = analog_write_res;
  672. if (res < 12) {
  673. val <<= 12 - res;
  674. } else if (res > 12) {
  675. val >>= res - 12;
  676. }
  677. analogWriteDAC0(val);
  678. return;
  679. }
  680. #elif defined(__MKL26Z64__)
  681. if (pin == A12) {
  682. uint8_t res = analog_write_res;
  683. if (res < 12) {
  684. val <<= 12 - res;
  685. } else if (res > 12) {
  686. val >>= res - 12;
  687. }
  688. analogWriteDAC0(val);
  689. return;
  690. }
  691. #elif defined(__MK64FX512__) || defined(__MK66FX1M0__)
  692. if (pin == A21 || pin == A22) {
  693. uint8_t res = analog_write_res;
  694. if (res < 12) {
  695. val <<= 12 - res;
  696. } else if (res > 12) {
  697. val >>= res - 12;
  698. }
  699. if (pin == A21) analogWriteDAC0(val);
  700. else analogWriteDAC1(val);
  701. return;
  702. }
  703. #endif
  704. max = 1 << analog_write_res;
  705. if (val <= 0) {
  706. digitalWrite(pin, LOW);
  707. pinMode(pin, OUTPUT); // TODO: implement OUTPUT_LOW
  708. return;
  709. } else if (val >= max) {
  710. digitalWrite(pin, HIGH);
  711. pinMode(pin, OUTPUT); // TODO: implement OUTPUT_HIGH
  712. return;
  713. }
  714. //serial_print("analogWrite\n");
  715. //serial_print("val = ");
  716. //serial_phex32(val);
  717. //serial_print("\n");
  718. //serial_print("analog_write_res = ");
  719. //serial_phex(analog_write_res);
  720. //serial_print("\n");
  721. if (pin == FTM1_CH0_PIN || pin == FTM1_CH1_PIN) {
  722. cval = ((uint32_t)val * (uint32_t)(FTM1_MOD + 1)) >> analog_write_res;
  723. #if defined(FTM2_CH0_PIN)
  724. } else if (pin == FTM2_CH0_PIN || pin == FTM2_CH1_PIN) {
  725. cval = ((uint32_t)val * (uint32_t)(FTM2_MOD + 1)) >> analog_write_res;
  726. #endif
  727. } else {
  728. cval = ((uint32_t)val * (uint32_t)(FTM0_MOD + 1)) >> analog_write_res;
  729. }
  730. //serial_print("cval = ");
  731. //serial_phex32(cval);
  732. //serial_print("\n");
  733. switch (pin) {
  734. #ifdef FTM0_CH0_PIN
  735. case FTM0_CH0_PIN: // PTC1, FTM0_CH0
  736. FTM0_C0V = cval;
  737. FTM_PINCFG(FTM0_CH0_PIN) = PORT_PCR_MUX(4) | PORT_PCR_DSE | PORT_PCR_SRE;
  738. break;
  739. #endif
  740. #ifdef FTM0_CH1_PIN
  741. case FTM0_CH1_PIN: // PTC2, FTM0_CH1
  742. FTM0_C1V = cval;
  743. FTM_PINCFG(FTM0_CH1_PIN) = PORT_PCR_MUX(4) | PORT_PCR_DSE | PORT_PCR_SRE;
  744. break;
  745. #endif
  746. #ifdef FTM0_CH2_PIN
  747. case FTM0_CH2_PIN: // PTC3, FTM0_CH2
  748. FTM0_C2V = cval;
  749. FTM_PINCFG(FTM0_CH2_PIN) = PORT_PCR_MUX(4) | PORT_PCR_DSE | PORT_PCR_SRE;
  750. break;
  751. #endif
  752. #ifdef FTM0_CH3_PIN
  753. case FTM0_CH3_PIN: // PTC4, FTM0_CH3
  754. FTM0_C3V = cval;
  755. FTM_PINCFG(FTM0_CH3_PIN) = PORT_PCR_MUX(4) | PORT_PCR_DSE | PORT_PCR_SRE;
  756. break;
  757. #endif
  758. #ifdef FTM0_CH4_PIN
  759. case FTM0_CH4_PIN: // PTD4, FTM0_CH4
  760. FTM0_C4V = cval;
  761. FTM_PINCFG(FTM0_CH4_PIN) = PORT_PCR_MUX(4) | PORT_PCR_DSE | PORT_PCR_SRE;
  762. break;
  763. #endif
  764. #ifdef FTM0_CH5_PIN
  765. case FTM0_CH5_PIN: // PTD5, FTM0_CH5
  766. FTM0_C5V = cval;
  767. FTM_PINCFG(FTM0_CH5_PIN) = PORT_PCR_MUX(4) | PORT_PCR_DSE | PORT_PCR_SRE;
  768. break;
  769. #endif
  770. #ifdef FTM0_CH6_PIN
  771. case FTM0_CH6_PIN: // PTD6, FTM0_CH6
  772. FTM0_C6V = cval;
  773. FTM_PINCFG(FTM0_CH6_PIN) = PORT_PCR_MUX(4) | PORT_PCR_DSE | PORT_PCR_SRE;
  774. break;
  775. #endif
  776. #ifdef FTM0_CH7_PIN
  777. case FTM0_CH7_PIN: // PTD7, FTM0_CH7
  778. FTM0_C7V = cval;
  779. FTM_PINCFG(FTM0_CH7_PIN) = PORT_PCR_MUX(4) | PORT_PCR_DSE | PORT_PCR_SRE;
  780. break;
  781. #endif
  782. #ifdef FTM1_CH0_PIN
  783. case FTM1_CH0_PIN: // PTA12, FTM1_CH0
  784. FTM1_C0V = cval;
  785. FTM_PINCFG(FTM1_CH0_PIN) = PORT_PCR_MUX(3) | PORT_PCR_DSE | PORT_PCR_SRE;
  786. break;
  787. #endif
  788. #ifdef FTM1_CH1_PIN
  789. case FTM1_CH1_PIN: // PTA13, FTM1_CH1
  790. FTM1_C1V = cval;
  791. FTM_PINCFG(FTM1_CH1_PIN) = PORT_PCR_MUX(3) | PORT_PCR_DSE | PORT_PCR_SRE;
  792. break;
  793. #endif
  794. #ifdef FTM2_CH0_PIN
  795. case FTM2_CH0_PIN: // PTB18, FTM2_CH0
  796. FTM2_C0V = cval;
  797. FTM_PINCFG(FTM2_CH0_PIN) = PORT_PCR_MUX(3) | PORT_PCR_DSE | PORT_PCR_SRE;
  798. break;
  799. #endif
  800. #ifdef FTM2_CH1_PIN
  801. case FTM2_CH1_PIN: // PTB19, FTM1_CH1
  802. FTM2_C1V = cval;
  803. FTM_PINCFG(FTM2_CH1_PIN) = PORT_PCR_MUX(3) | PORT_PCR_DSE | PORT_PCR_SRE;
  804. break;
  805. #endif
  806. #ifdef FTM3_CH0_PIN
  807. case FTM3_CH0_PIN:
  808. FTM3_C0V = cval;
  809. FTM_PINCFG(FTM3_CH0_PIN) = PORT_PCR_MUX(4) | PORT_PCR_DSE | PORT_PCR_SRE;
  810. break;
  811. #endif
  812. #ifdef FTM3_CH1_PIN
  813. case FTM3_CH1_PIN:
  814. FTM3_C1V = cval;
  815. FTM_PINCFG(FTM3_CH1_PIN) = PORT_PCR_MUX(4) | PORT_PCR_DSE | PORT_PCR_SRE;
  816. break;
  817. #endif
  818. #ifdef FTM3_CH2_PIN
  819. case FTM3_CH2_PIN:
  820. FTM3_C2V = cval;
  821. FTM_PINCFG(FTM3_CH2_PIN) = PORT_PCR_MUX(4) | PORT_PCR_DSE | PORT_PCR_SRE;
  822. break;
  823. #endif
  824. #ifdef FTM3_CH3_PIN
  825. case FTM3_CH3_PIN:
  826. FTM3_C3V = cval;
  827. FTM_PINCFG(FTM3_CH3_PIN) = PORT_PCR_MUX(4) | PORT_PCR_DSE | PORT_PCR_SRE;
  828. break;
  829. #endif
  830. #ifdef FTM3_CH4_PIN
  831. case FTM3_CH4_PIN:
  832. FTM3_C4V = cval;
  833. FTM_PINCFG(FTM3_CH4_PIN) = PORT_PCR_MUX(3) | PORT_PCR_DSE | PORT_PCR_SRE;
  834. break;
  835. #endif
  836. #ifdef FTM3_CH5_PIN
  837. case FTM3_CH5_PIN:
  838. FTM3_C5V = cval;
  839. FTM_PINCFG(FTM3_CH5_PIN) = PORT_PCR_MUX(3) | PORT_PCR_DSE | PORT_PCR_SRE;
  840. break;
  841. #endif
  842. #ifdef FTM3_CH6_PIN
  843. case FTM3_CH6_PIN:
  844. FTM3_C6V = cval;
  845. FTM_PINCFG(FTM3_CH6_PIN) = PORT_PCR_MUX(3) | PORT_PCR_DSE | PORT_PCR_SRE;
  846. break;
  847. #endif
  848. #ifdef FTM3_CH7_PIN
  849. case FTM3_CH7_PIN:
  850. FTM3_C7V = cval;
  851. FTM_PINCFG(FTM3_CH7_PIN) = PORT_PCR_MUX(3) | PORT_PCR_DSE | PORT_PCR_SRE;
  852. break;
  853. #endif
  854. default:
  855. digitalWrite(pin, (val > 127) ? HIGH : LOW);
  856. pinMode(pin, OUTPUT);
  857. }
  858. }
  859. void analogWriteRes(uint32_t bits)
  860. {
  861. if (bits < 1) {
  862. bits = 1;
  863. } else if (bits > 16) {
  864. bits = 16;
  865. }
  866. analog_write_res = bits;
  867. }
  868. void analogWriteFrequency(uint8_t pin, float frequency)
  869. {
  870. uint32_t prescale, mod, ftmClock, ftmClockSource;
  871. float minfreq;
  872. //serial_print("analogWriteFrequency: pin = ");
  873. //serial_phex(pin);
  874. //serial_print(", freq = ");
  875. //serial_phex32((uint32_t)frequency);
  876. //serial_print("\n");
  877. if (frequency < (float)(F_TIMER >> 7) / 65536.0f) { //If frequency is too low for working with F_TIMER:
  878. ftmClockSource = 2; //Use alternative 31250Hz clock source
  879. ftmClock = 31250; //Set variable for the actual timer clock frequency
  880. } else { //Else do as before:
  881. ftmClockSource = 1; //Use default F_Timer clock source
  882. ftmClock = F_TIMER; //Set variable for the actual timer clock frequency
  883. }
  884. for (prescale = 0; prescale < 7; prescale++) {
  885. minfreq = (float)(ftmClock >> prescale) / 65536.0f; //Use ftmClock instead of F_TIMER
  886. if (frequency >= minfreq) break;
  887. }
  888. //serial_print("F_TIMER/ftm_Clock = ");
  889. //serial_phex32(ftmClock >> prescale);
  890. //serial_print("\n");
  891. //serial_print("prescale = ");
  892. //serial_phex(prescale);
  893. //serial_print("\n");
  894. mod = (float)(ftmClock >> prescale) / frequency - 0.5f; //Use ftmClock instead of F_TIMER
  895. if (mod > 65535) mod = 65535;
  896. //serial_print("mod = ");
  897. //serial_phex32(mod);
  898. //serial_print("\n");
  899. if (pin == FTM1_CH0_PIN || pin == FTM1_CH1_PIN) {
  900. FTM1_SC = 0;
  901. FTM1_CNT = 0;
  902. FTM1_MOD = mod;
  903. FTM1_SC = FTM_SC_CLKS(ftmClockSource) | FTM_SC_PS(prescale); //Use ftmClockSource instead of 1
  904. } else if (pin == FTM0_CH0_PIN || pin == FTM0_CH1_PIN
  905. || pin == FTM0_CH2_PIN || pin == FTM0_CH3_PIN
  906. || pin == FTM0_CH4_PIN || pin == FTM0_CH5_PIN
  907. #ifdef FTM0_CH6_PIN
  908. || pin == FTM0_CH6_PIN || pin == FTM0_CH7_PIN
  909. #endif
  910. ) {
  911. FTM0_SC = 0;
  912. FTM0_CNT = 0;
  913. FTM0_MOD = mod;
  914. FTM0_SC = FTM_SC_CLKS(ftmClockSource) | FTM_SC_PS(prescale); //Use ftmClockSource instead of 1
  915. }
  916. #ifdef FTM2_CH0_PIN
  917. else if (pin == FTM2_CH0_PIN || pin == FTM2_CH1_PIN) {
  918. FTM2_SC = 0;
  919. FTM2_CNT = 0;
  920. FTM2_MOD = mod;
  921. FTM2_SC = FTM_SC_CLKS(ftmClockSource) | FTM_SC_PS(prescale); //Use ftmClockSource instead of 1
  922. }
  923. #endif
  924. #ifdef FTM3_CH0_PIN
  925. else if (pin == FTM3_CH0_PIN || pin == FTM3_CH1_PIN
  926. || pin == FTM3_CH2_PIN || pin == FTM3_CH3_PIN
  927. || pin == FTM3_CH4_PIN || pin == FTM3_CH5_PIN
  928. || pin == FTM3_CH6_PIN || pin == FTM3_CH7_PIN) {
  929. FTM3_SC = 0;
  930. FTM3_CNT = 0;
  931. FTM3_MOD = mod;
  932. FTM3_SC = FTM_SC_CLKS(ftmClockSource) | FTM_SC_PS(prescale); //Use the new ftmClockSource instead of 1
  933. }
  934. #endif
  935. }
  936. // TODO: startup code needs to initialize all pins to GPIO mode, input by default
  937. void digitalWrite(uint8_t pin, uint8_t val)
  938. {
  939. if (pin >= CORE_NUM_DIGITAL) return;
  940. #ifdef KINETISK
  941. if (*portModeRegister(pin)) {
  942. if (val) {
  943. *portSetRegister(pin) = 1;
  944. } else {
  945. *portClearRegister(pin) = 1;
  946. }
  947. #else
  948. if (*portModeRegister(pin) & digitalPinToBitMask(pin)) {
  949. if (val) {
  950. *portSetRegister(pin) = digitalPinToBitMask(pin);
  951. } else {
  952. *portClearRegister(pin) = digitalPinToBitMask(pin);
  953. }
  954. #endif
  955. } else {
  956. volatile uint32_t *config = portConfigRegister(pin);
  957. if (val) {
  958. // TODO use bitband for atomic read-mod-write
  959. *config |= (PORT_PCR_PE | PORT_PCR_PS);
  960. //*config = PORT_PCR_MUX(1) | PORT_PCR_PE | PORT_PCR_PS;
  961. } else {
  962. // TODO use bitband for atomic read-mod-write
  963. *config &= ~(PORT_PCR_PE);
  964. //*config = PORT_PCR_MUX(1);
  965. }
  966. }
  967. }
  968. uint8_t digitalRead(uint8_t pin)
  969. {
  970. if (pin >= CORE_NUM_DIGITAL) return 0;
  971. #ifdef KINETISK
  972. return *portInputRegister(pin);
  973. #else
  974. return (*portInputRegister(pin) & digitalPinToBitMask(pin)) ? 1 : 0;
  975. #endif
  976. }
  977. void pinMode(uint8_t pin, uint8_t mode)
  978. {
  979. volatile uint32_t *config;
  980. if (pin >= CORE_NUM_DIGITAL) return;
  981. config = portConfigRegister(pin);
  982. if (mode == OUTPUT || mode == OUTPUT_OPENDRAIN) {
  983. #ifdef KINETISK
  984. *portModeRegister(pin) = 1;
  985. #else
  986. *portModeRegister(pin) |= digitalPinToBitMask(pin); // TODO: atomic
  987. #endif
  988. *config = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
  989. if (mode == OUTPUT_OPENDRAIN) {
  990. *config |= PORT_PCR_ODE;
  991. } else {
  992. *config &= ~PORT_PCR_ODE;
  993. }
  994. } else {
  995. #ifdef KINETISK
  996. *portModeRegister(pin) = 0;
  997. #else
  998. *portModeRegister(pin) &= ~digitalPinToBitMask(pin);
  999. #endif
  1000. if (mode == INPUT || mode == INPUT_PULLUP || mode == INPUT_PULLDOWN) {
  1001. *config = PORT_PCR_MUX(1);
  1002. if (mode == INPUT_PULLUP) {
  1003. *config |= (PORT_PCR_PE | PORT_PCR_PS); // pullup
  1004. } else if (mode == INPUT_PULLDOWN) {
  1005. *config |= (PORT_PCR_PE); // pulldown
  1006. *config &= ~(PORT_PCR_PS);
  1007. }
  1008. } else {
  1009. *config = PORT_PCR_MUX(1) | PORT_PCR_PE | PORT_PCR_PS; // pullup
  1010. }
  1011. }
  1012. }
  1013. void _shiftOut(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder, uint8_t value)
  1014. {
  1015. if (bitOrder == LSBFIRST) {
  1016. shiftOut_lsbFirst(dataPin, clockPin, value);
  1017. } else {
  1018. shiftOut_msbFirst(dataPin, clockPin, value);
  1019. }
  1020. }
  1021. void shiftOut_lsbFirst(uint8_t dataPin, uint8_t clockPin, uint8_t value)
  1022. {
  1023. uint8_t mask;
  1024. for (mask=0x01; mask; mask <<= 1) {
  1025. digitalWrite(dataPin, value & mask);
  1026. digitalWrite(clockPin, HIGH);
  1027. digitalWrite(clockPin, LOW);
  1028. }
  1029. }
  1030. void shiftOut_msbFirst(uint8_t dataPin, uint8_t clockPin, uint8_t value)
  1031. {
  1032. uint8_t mask;
  1033. for (mask=0x80; mask; mask >>= 1) {
  1034. digitalWrite(dataPin, value & mask);
  1035. digitalWrite(clockPin, HIGH);
  1036. digitalWrite(clockPin, LOW);
  1037. }
  1038. }
  1039. uint8_t _shiftIn(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder)
  1040. {
  1041. if (bitOrder == LSBFIRST) {
  1042. return shiftIn_lsbFirst(dataPin, clockPin);
  1043. } else {
  1044. return shiftIn_msbFirst(dataPin, clockPin);
  1045. }
  1046. }
  1047. uint8_t shiftIn_lsbFirst(uint8_t dataPin, uint8_t clockPin)
  1048. {
  1049. uint8_t mask, value=0;
  1050. for (mask=0x01; mask; mask <<= 1) {
  1051. digitalWrite(clockPin, HIGH);
  1052. if (digitalRead(dataPin)) value |= mask;
  1053. digitalWrite(clockPin, LOW);
  1054. }
  1055. return value;
  1056. }
  1057. uint8_t shiftIn_msbFirst(uint8_t dataPin, uint8_t clockPin)
  1058. {
  1059. uint8_t mask, value=0;
  1060. for (mask=0x80; mask; mask >>= 1) {
  1061. digitalWrite(clockPin, HIGH);
  1062. if (digitalRead(dataPin)) value |= mask;
  1063. digitalWrite(clockPin, LOW);
  1064. }
  1065. return value;
  1066. }
  1067. // the systick interrupt is supposed to increment this at 1 kHz rate
  1068. volatile uint32_t systick_millis_count = 0;
  1069. //uint32_t systick_current, systick_count, systick_istatus; // testing only
  1070. uint32_t micros(void)
  1071. {
  1072. uint32_t count, current, istatus;
  1073. __disable_irq();
  1074. current = SYST_CVR;
  1075. count = systick_millis_count;
  1076. istatus = SCB_ICSR; // bit 26 indicates if systick exception pending
  1077. __enable_irq();
  1078. //systick_current = current;
  1079. //systick_count = count;
  1080. //systick_istatus = istatus & SCB_ICSR_PENDSTSET ? 1 : 0;
  1081. if ((istatus & SCB_ICSR_PENDSTSET) && current > 50) count++;
  1082. current = ((F_CPU / 1000) - 1) - current;
  1083. #if defined(KINETISL) && F_CPU == 48000000
  1084. return count * 1000 + ((current * (uint32_t)87381) >> 22);
  1085. #elif defined(KINETISL) && F_CPU == 24000000
  1086. return count * 1000 + ((current * (uint32_t)174763) >> 22);
  1087. #endif
  1088. return count * 1000 + current / (F_CPU / 1000000);
  1089. }
  1090. void delay(uint32_t ms)
  1091. {
  1092. uint32_t start = micros();
  1093. if (ms > 0) {
  1094. while (1) {
  1095. while ((micros() - start) >= 1000) {
  1096. ms--;
  1097. if (ms == 0) return;
  1098. start += 1000;
  1099. }
  1100. yield();
  1101. }
  1102. }
  1103. }
  1104. // TODO: verify these result in correct timeouts...
  1105. #if F_CPU == 240000000
  1106. #define PULSEIN_LOOPS_PER_USEC 33
  1107. #elif F_CPU == 216000000
  1108. #define PULSEIN_LOOPS_PER_USEC 31
  1109. #elif F_CPU == 192000000
  1110. #define PULSEIN_LOOPS_PER_USEC 29
  1111. #elif F_CPU == 180000000
  1112. #define PULSEIN_LOOPS_PER_USEC 27
  1113. #elif F_CPU == 168000000
  1114. #define PULSEIN_LOOPS_PER_USEC 25
  1115. #elif F_CPU == 144000000
  1116. #define PULSEIN_LOOPS_PER_USEC 21
  1117. #elif F_CPU == 120000000
  1118. #define PULSEIN_LOOPS_PER_USEC 18
  1119. #elif F_CPU == 96000000
  1120. #define PULSEIN_LOOPS_PER_USEC 14
  1121. #elif F_CPU == 72000000
  1122. #define PULSEIN_LOOPS_PER_USEC 10
  1123. #elif F_CPU == 48000000
  1124. #define PULSEIN_LOOPS_PER_USEC 7
  1125. #elif F_CPU == 24000000
  1126. #define PULSEIN_LOOPS_PER_USEC 4
  1127. #elif F_CPU == 16000000
  1128. #define PULSEIN_LOOPS_PER_USEC 1
  1129. #elif F_CPU == 8000000
  1130. #define PULSEIN_LOOPS_PER_USEC 1
  1131. #elif F_CPU == 4000000
  1132. #define PULSEIN_LOOPS_PER_USEC 1
  1133. #elif F_CPU == 2000000
  1134. #define PULSEIN_LOOPS_PER_USEC 1
  1135. #endif
  1136. #if defined(KINETISK)
  1137. uint32_t pulseIn_high(volatile uint8_t *reg, uint32_t timeout)
  1138. {
  1139. uint32_t timeout_count = timeout * PULSEIN_LOOPS_PER_USEC;
  1140. uint32_t usec_start, usec_stop;
  1141. // wait for any previous pulse to end
  1142. while (*reg) {
  1143. if (--timeout_count == 0) return 0;
  1144. }
  1145. // wait for the pulse to start
  1146. while (!*reg) {
  1147. if (--timeout_count == 0) return 0;
  1148. }
  1149. usec_start = micros();
  1150. // wait for the pulse to stop
  1151. while (*reg) {
  1152. if (--timeout_count == 0) return 0;
  1153. }
  1154. usec_stop = micros();
  1155. return usec_stop - usec_start;
  1156. }
  1157. uint32_t pulseIn_low(volatile uint8_t *reg, uint32_t timeout)
  1158. {
  1159. uint32_t timeout_count = timeout * PULSEIN_LOOPS_PER_USEC;
  1160. uint32_t usec_start, usec_stop;
  1161. // wait for any previous pulse to end
  1162. while (!*reg) {
  1163. if (--timeout_count == 0) return 0;
  1164. }
  1165. // wait for the pulse to start
  1166. while (*reg) {
  1167. if (--timeout_count == 0) return 0;
  1168. }
  1169. usec_start = micros();
  1170. // wait for the pulse to stop
  1171. while (!*reg) {
  1172. if (--timeout_count == 0) return 0;
  1173. }
  1174. usec_stop = micros();
  1175. return usec_stop - usec_start;
  1176. }
  1177. // TODO: an inline version should handle the common case where state is const
  1178. uint32_t pulseIn(uint8_t pin, uint8_t state, uint32_t timeout)
  1179. {
  1180. if (pin >= CORE_NUM_DIGITAL) return 0;
  1181. if (state) return pulseIn_high(portInputRegister(pin), timeout);
  1182. return pulseIn_low(portInputRegister(pin), timeout);;
  1183. }
  1184. #elif defined(KINETISL)
  1185. // For TeencyLC need to use mask on the input register as the register is shared by several IO pins
  1186. uint32_t pulseIn_high(volatile uint8_t *reg, uint8_t mask, uint32_t timeout)
  1187. {
  1188. uint32_t timeout_count = timeout * PULSEIN_LOOPS_PER_USEC;
  1189. uint32_t usec_start, usec_stop;
  1190. // wait for any previous pulse to end
  1191. while (*reg & mask) {
  1192. if (--timeout_count == 0) return -1;
  1193. }
  1194. // wait for the pulse to start
  1195. while (!(*reg & mask)) {
  1196. if (--timeout_count == 0) return 0;
  1197. }
  1198. usec_start = micros();
  1199. // wait for the pulse to stop
  1200. while (*reg & mask) {
  1201. if (--timeout_count == 0) return 0;
  1202. }
  1203. usec_stop = micros();
  1204. return usec_stop - usec_start;
  1205. }
  1206. uint32_t pulseIn_low(volatile uint8_t *reg, uint8_t mask, uint32_t timeout)
  1207. {
  1208. uint32_t timeout_count = timeout * PULSEIN_LOOPS_PER_USEC;
  1209. uint32_t usec_start, usec_stop;
  1210. // wait for any previous pulse to end
  1211. while (!(*reg & mask)) {
  1212. if (--timeout_count == 0) return 0;
  1213. }
  1214. // wait for the pulse to start
  1215. while (*reg & mask) {
  1216. if (--timeout_count == 0) return 0;
  1217. }
  1218. usec_start = micros();
  1219. // wait for the pulse to stop
  1220. while (!(*reg & mask)) {
  1221. if (--timeout_count == 0) return 0;
  1222. }
  1223. usec_stop = micros();
  1224. return usec_stop - usec_start;
  1225. }
  1226. // TODO: an inline version should handle the common case where state is const
  1227. uint32_t pulseIn(uint8_t pin, uint8_t state, uint32_t timeout)
  1228. {
  1229. if (pin >= CORE_NUM_DIGITAL) return 0;
  1230. if (state) return pulseIn_high(portInputRegister(pin), digitalPinToBitMask(pin), timeout);
  1231. return pulseIn_low(portInputRegister(pin), digitalPinToBitMask(pin), timeout);;
  1232. }
  1233. #endif