Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

1324 lines
43KB

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