You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1241 lines
39KB

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