Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

1288 Zeilen
41KB

  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 == 120000000
  433. #define DEFAULT_FTM_MOD (61440 - 1)
  434. #define DEFAULT_FTM_PRESCALE 2
  435. #elif F_TIMER == 108000000
  436. #define DEFAULT_FTM_MOD (55296 - 1)
  437. #define DEFAULT_FTM_PRESCALE 2
  438. #elif F_TIMER == 96000000
  439. #define DEFAULT_FTM_MOD (49152 - 1)
  440. #define DEFAULT_FTM_PRESCALE 2
  441. #elif F_TIMER == 90000000
  442. #define DEFAULT_FTM_MOD (46080 - 1)
  443. #define DEFAULT_FTM_PRESCALE 2
  444. #elif F_TIMER == 80000000
  445. #define DEFAULT_FTM_MOD (40960 - 1)
  446. #define DEFAULT_FTM_PRESCALE 2
  447. #elif F_TIMER == 72000000
  448. #define DEFAULT_FTM_MOD (36864 - 1)
  449. #define DEFAULT_FTM_PRESCALE 2
  450. #elif F_TIMER == 64000000
  451. #define DEFAULT_FTM_MOD (65536 - 1)
  452. #define DEFAULT_FTM_PRESCALE 1
  453. #elif F_TIMER == 60000000
  454. #define DEFAULT_FTM_MOD (61440 - 1)
  455. #define DEFAULT_FTM_PRESCALE 1
  456. #elif F_TIMER == 56000000
  457. #define DEFAULT_FTM_MOD (57344 - 1)
  458. #define DEFAULT_FTM_PRESCALE 1
  459. #elif F_TIMER == 54000000
  460. #define DEFAULT_FTM_MOD (55296 - 1)
  461. #define DEFAULT_FTM_PRESCALE 1
  462. #elif F_TIMER == 48000000
  463. #define DEFAULT_FTM_MOD (49152 - 1)
  464. #define DEFAULT_FTM_PRESCALE 1
  465. #elif F_TIMER == 40000000
  466. #define DEFAULT_FTM_MOD (40960 - 1)
  467. #define DEFAULT_FTM_PRESCALE 1
  468. #elif F_TIMER == 36000000
  469. #define DEFAULT_FTM_MOD (36864 - 1)
  470. #define DEFAULT_FTM_PRESCALE 1
  471. #elif F_TIMER == 24000000
  472. #define DEFAULT_FTM_MOD (49152 - 1)
  473. #define DEFAULT_FTM_PRESCALE 0
  474. #elif F_TIMER == 16000000
  475. #define DEFAULT_FTM_MOD (32768 - 1)
  476. #define DEFAULT_FTM_PRESCALE 0
  477. #elif F_TIMER == 8000000
  478. #define DEFAULT_FTM_MOD (16384 - 1)
  479. #define DEFAULT_FTM_PRESCALE 0
  480. #elif F_TIMER == 4000000
  481. #define DEFAULT_FTM_MOD (8192 - 1)
  482. #define DEFAULT_FTM_PRESCALE 0
  483. #elif F_TIMER == 2000000
  484. #define DEFAULT_FTM_MOD (4096 - 1)
  485. #define DEFAULT_FTM_PRESCALE 0
  486. #endif
  487. //void init_pins(void)
  488. void _init_Teensyduino_internal_(void)
  489. {
  490. #if defined(__MK20DX128__) || defined(__MK20DX256__) || defined(__MK64FX512__) || defined(__MK66FX1M0__)
  491. NVIC_ENABLE_IRQ(IRQ_PORTA);
  492. NVIC_ENABLE_IRQ(IRQ_PORTB);
  493. NVIC_ENABLE_IRQ(IRQ_PORTC);
  494. NVIC_ENABLE_IRQ(IRQ_PORTD);
  495. NVIC_ENABLE_IRQ(IRQ_PORTE);
  496. #elif defined(__MKL26Z64__)
  497. NVIC_ENABLE_IRQ(IRQ_PORTA);
  498. NVIC_ENABLE_IRQ(IRQ_PORTCD);
  499. #endif
  500. //SIM_SCGC6 |= SIM_SCGC6_FTM0; // TODO: use bitband for atomic read-mod-write
  501. //SIM_SCGC6 |= SIM_SCGC6_FTM1;
  502. FTM0_CNT = 0;
  503. FTM0_MOD = DEFAULT_FTM_MOD;
  504. FTM0_C0SC = 0x28; // MSnB:MSnA = 10, ELSnB:ELSnA = 10
  505. FTM0_C1SC = 0x28;
  506. FTM0_C2SC = 0x28;
  507. FTM0_C3SC = 0x28;
  508. FTM0_C4SC = 0x28;
  509. FTM0_C5SC = 0x28;
  510. #if defined(__MK20DX128__) || defined(__MK20DX256__) || defined(__MK64FX512__) || defined(__MK66FX1M0__)
  511. FTM0_C6SC = 0x28;
  512. FTM0_C7SC = 0x28;
  513. #endif
  514. #if defined(__MK64FX512__) || defined(__MK66FX1M0__)
  515. FTM3_C0SC = 0x28;
  516. FTM3_C1SC = 0x28;
  517. FTM3_C2SC = 0x28;
  518. FTM3_C3SC = 0x28;
  519. FTM3_C4SC = 0x28;
  520. FTM3_C5SC = 0x28;
  521. FTM3_C6SC = 0x28;
  522. FTM3_C7SC = 0x28;
  523. #endif
  524. FTM0_SC = FTM_SC_CLKS(1) | FTM_SC_PS(DEFAULT_FTM_PRESCALE);
  525. FTM1_CNT = 0;
  526. FTM1_MOD = DEFAULT_FTM_MOD;
  527. FTM1_C0SC = 0x28;
  528. FTM1_C1SC = 0x28;
  529. FTM1_SC = FTM_SC_CLKS(1) | FTM_SC_PS(DEFAULT_FTM_PRESCALE);
  530. #if defined(__MK20DX256__) || defined(__MK64FX512__) || defined(__MK66FX1M0__) || defined(__MKL26Z64__)
  531. FTM2_CNT = 0;
  532. FTM2_MOD = DEFAULT_FTM_MOD;
  533. FTM2_C0SC = 0x28;
  534. FTM2_C1SC = 0x28;
  535. FTM2_SC = FTM_SC_CLKS(1) | FTM_SC_PS(DEFAULT_FTM_PRESCALE);
  536. #endif
  537. #if defined(__MK64FX512__) || defined(__MK66FX1M0__)
  538. FTM3_CNT = 0;
  539. FTM3_MOD = DEFAULT_FTM_MOD;
  540. FTM3_C0SC = 0x28;
  541. FTM3_C1SC = 0x28;
  542. FTM3_SC = FTM_SC_CLKS(1) | FTM_SC_PS(DEFAULT_FTM_PRESCALE);
  543. #endif
  544. analog_init();
  545. // for background about this startup delay, please see this conversation
  546. // https://forum.pjrc.com/threads/31290-Teensey-3-2-Teensey-Loader-1-24-Issues?p=87273&viewfull=1#post87273
  547. delay(250);
  548. usb_init();
  549. }
  550. #if defined(__MK20DX128__)
  551. #define FTM0_CH0_PIN 22
  552. #define FTM0_CH1_PIN 23
  553. #define FTM0_CH2_PIN 9
  554. #define FTM0_CH3_PIN 10
  555. #define FTM0_CH4_PIN 6
  556. #define FTM0_CH5_PIN 20
  557. #define FTM0_CH6_PIN 21
  558. #define FTM0_CH7_PIN 5
  559. #define FTM1_CH0_PIN 3
  560. #define FTM1_CH1_PIN 4
  561. #elif defined(__MK20DX256__)
  562. #define FTM0_CH0_PIN 22
  563. #define FTM0_CH1_PIN 23
  564. #define FTM0_CH2_PIN 9
  565. #define FTM0_CH3_PIN 10
  566. #define FTM0_CH4_PIN 6
  567. #define FTM0_CH5_PIN 20
  568. #define FTM0_CH6_PIN 21
  569. #define FTM0_CH7_PIN 5
  570. #define FTM1_CH0_PIN 3
  571. #define FTM1_CH1_PIN 4
  572. #define FTM2_CH0_PIN 32
  573. #define FTM2_CH1_PIN 25
  574. #elif defined(__MKL26Z64__)
  575. #define FTM0_CH0_PIN 22
  576. #define FTM0_CH1_PIN 23
  577. #define FTM0_CH2_PIN 9
  578. #define FTM0_CH3_PIN 10
  579. #define FTM0_CH4_PIN 6
  580. #define FTM0_CH5_PIN 20
  581. #define FTM1_CH0_PIN 16
  582. #define FTM1_CH1_PIN 17
  583. #define FTM2_CH0_PIN 3
  584. #define FTM2_CH1_PIN 4
  585. #elif defined(__MK64FX512__) || defined(__MK66FX1M0__)
  586. #define FTM0_CH0_PIN 22
  587. #define FTM0_CH1_PIN 23
  588. #define FTM0_CH2_PIN 9
  589. #define FTM0_CH3_PIN 10
  590. #define FTM0_CH4_PIN 6
  591. #define FTM0_CH5_PIN 20
  592. #define FTM0_CH6_PIN 21
  593. #define FTM0_CH7_PIN 5
  594. #define FTM1_CH0_PIN 3
  595. #define FTM1_CH1_PIN 4
  596. #define FTM2_CH0_PIN 29
  597. #define FTM2_CH1_PIN 30
  598. #define FTM3_CH0_PIN 2
  599. #define FTM3_CH1_PIN 14
  600. #define FTM3_CH2_PIN 7
  601. #define FTM3_CH3_PIN 8
  602. #define FTM3_CH4_PIN 35
  603. #define FTM3_CH5_PIN 36
  604. #define FTM3_CH6_PIN 37
  605. #define FTM3_CH7_PIN 38
  606. #endif
  607. #define FTM_PINCFG(pin) FTM_PINCFG2(pin)
  608. #define FTM_PINCFG2(pin) CORE_PIN ## pin ## _CONFIG
  609. static uint8_t analog_write_res = 8;
  610. // SOPT4 is SIM select clocks?
  611. // FTM is clocked by the bus clock, either 24 or 48 MHz
  612. // input capture can be FTM1_CH0, CMP0 or CMP1 or USB start of frame
  613. // 24 MHz with reload 49152 to match Arduino's speed = 488.28125 Hz
  614. void analogWrite(uint8_t pin, int val)
  615. {
  616. uint32_t cval, max;
  617. #if defined(__MK20DX256__)
  618. if (pin == A14) {
  619. uint8_t res = analog_write_res;
  620. if (res < 12) {
  621. val <<= 12 - res;
  622. } else if (res > 12) {
  623. val >>= res - 12;
  624. }
  625. analogWriteDAC0(val);
  626. return;
  627. }
  628. #elif defined(__MKL26Z64__)
  629. if (pin == A12) {
  630. uint8_t res = analog_write_res;
  631. if (res < 12) {
  632. val <<= 12 - res;
  633. } else if (res > 12) {
  634. val >>= res - 12;
  635. }
  636. analogWriteDAC0(val);
  637. return;
  638. }
  639. #elif defined(__MK64FX512__) || defined(__MK66FX1M0__)
  640. if (pin == A21 || pin == A22) {
  641. uint8_t res = analog_write_res;
  642. if (res < 12) {
  643. val <<= 12 - res;
  644. } else if (res > 12) {
  645. val >>= res - 12;
  646. }
  647. if (pin == A21) analogWriteDAC0(val);
  648. else analogWriteDAC1(val);
  649. return;
  650. }
  651. #endif
  652. max = 1 << analog_write_res;
  653. if (val <= 0) {
  654. digitalWrite(pin, LOW);
  655. pinMode(pin, OUTPUT); // TODO: implement OUTPUT_LOW
  656. return;
  657. } else if (val >= max) {
  658. digitalWrite(pin, HIGH);
  659. pinMode(pin, OUTPUT); // TODO: implement OUTPUT_HIGH
  660. return;
  661. }
  662. //serial_print("analogWrite\n");
  663. //serial_print("val = ");
  664. //serial_phex32(val);
  665. //serial_print("\n");
  666. //serial_print("analog_write_res = ");
  667. //serial_phex(analog_write_res);
  668. //serial_print("\n");
  669. if (pin == FTM1_CH0_PIN || pin == FTM1_CH1_PIN) {
  670. cval = ((uint32_t)val * (uint32_t)(FTM1_MOD + 1)) >> analog_write_res;
  671. #if defined(FTM2_CH0_PIN)
  672. } else if (pin == FTM2_CH0_PIN || pin == FTM2_CH1_PIN) {
  673. cval = ((uint32_t)val * (uint32_t)(FTM2_MOD + 1)) >> analog_write_res;
  674. #endif
  675. } else {
  676. cval = ((uint32_t)val * (uint32_t)(FTM0_MOD + 1)) >> analog_write_res;
  677. }
  678. //serial_print("cval = ");
  679. //serial_phex32(cval);
  680. //serial_print("\n");
  681. switch (pin) {
  682. #ifdef FTM0_CH0_PIN
  683. case FTM0_CH0_PIN: // PTC1, FTM0_CH0
  684. FTM0_C0V = cval;
  685. FTM_PINCFG(FTM0_CH0_PIN) = PORT_PCR_MUX(4) | PORT_PCR_DSE | PORT_PCR_SRE;
  686. break;
  687. #endif
  688. #ifdef FTM0_CH1_PIN
  689. case FTM0_CH1_PIN: // PTC2, FTM0_CH1
  690. FTM0_C1V = cval;
  691. FTM_PINCFG(FTM0_CH1_PIN) = PORT_PCR_MUX(4) | PORT_PCR_DSE | PORT_PCR_SRE;
  692. break;
  693. #endif
  694. #ifdef FTM0_CH2_PIN
  695. case FTM0_CH2_PIN: // PTC3, FTM0_CH2
  696. FTM0_C2V = cval;
  697. FTM_PINCFG(FTM0_CH2_PIN) = PORT_PCR_MUX(4) | PORT_PCR_DSE | PORT_PCR_SRE;
  698. break;
  699. #endif
  700. #ifdef FTM0_CH3_PIN
  701. case FTM0_CH3_PIN: // PTC4, FTM0_CH3
  702. FTM0_C3V = cval;
  703. FTM_PINCFG(FTM0_CH3_PIN) = PORT_PCR_MUX(4) | PORT_PCR_DSE | PORT_PCR_SRE;
  704. break;
  705. #endif
  706. #ifdef FTM0_CH4_PIN
  707. case FTM0_CH4_PIN: // PTD4, FTM0_CH4
  708. FTM0_C4V = cval;
  709. FTM_PINCFG(FTM0_CH4_PIN) = PORT_PCR_MUX(4) | PORT_PCR_DSE | PORT_PCR_SRE;
  710. break;
  711. #endif
  712. #ifdef FTM0_CH5_PIN
  713. case FTM0_CH5_PIN: // PTD5, FTM0_CH5
  714. FTM0_C5V = cval;
  715. FTM_PINCFG(FTM0_CH5_PIN) = PORT_PCR_MUX(4) | PORT_PCR_DSE | PORT_PCR_SRE;
  716. break;
  717. #endif
  718. #ifdef FTM0_CH6_PIN
  719. case FTM0_CH6_PIN: // PTD6, FTM0_CH6
  720. FTM0_C6V = cval;
  721. FTM_PINCFG(FTM0_CH6_PIN) = PORT_PCR_MUX(4) | PORT_PCR_DSE | PORT_PCR_SRE;
  722. break;
  723. #endif
  724. #ifdef FTM0_CH7_PIN
  725. case FTM0_CH7_PIN: // PTD7, FTM0_CH7
  726. FTM0_C7V = cval;
  727. FTM_PINCFG(FTM0_CH7_PIN) = PORT_PCR_MUX(4) | PORT_PCR_DSE | PORT_PCR_SRE;
  728. break;
  729. #endif
  730. #ifdef FTM1_CH0_PIN
  731. case FTM1_CH0_PIN: // PTA12, FTM1_CH0
  732. FTM1_C0V = cval;
  733. FTM_PINCFG(FTM1_CH0_PIN) = PORT_PCR_MUX(3) | PORT_PCR_DSE | PORT_PCR_SRE;
  734. break;
  735. #endif
  736. #ifdef FTM1_CH1_PIN
  737. case FTM1_CH1_PIN: // PTA13, FTM1_CH1
  738. FTM1_C1V = cval;
  739. FTM_PINCFG(FTM1_CH1_PIN) = PORT_PCR_MUX(3) | PORT_PCR_DSE | PORT_PCR_SRE;
  740. break;
  741. #endif
  742. #ifdef FTM2_CH0_PIN
  743. case FTM2_CH0_PIN: // PTB18, FTM2_CH0
  744. FTM2_C0V = cval;
  745. FTM_PINCFG(FTM2_CH0_PIN) = PORT_PCR_MUX(3) | PORT_PCR_DSE | PORT_PCR_SRE;
  746. break;
  747. #endif
  748. #ifdef FTM2_CH1_PIN
  749. case FTM2_CH1_PIN: // PTB19, FTM1_CH1
  750. FTM2_C1V = cval;
  751. FTM_PINCFG(FTM2_CH1_PIN) = PORT_PCR_MUX(3) | PORT_PCR_DSE | PORT_PCR_SRE;
  752. break;
  753. #endif
  754. #ifdef FTM3_CH0_PIN
  755. case FTM3_CH0_PIN:
  756. FTM3_C0V = cval;
  757. FTM_PINCFG(FTM3_CH0_PIN) = PORT_PCR_MUX(4) | PORT_PCR_DSE | PORT_PCR_SRE;
  758. break;
  759. #endif
  760. #ifdef FTM3_CH1_PIN
  761. case FTM3_CH1_PIN:
  762. FTM3_C1V = cval;
  763. FTM_PINCFG(FTM3_CH1_PIN) = PORT_PCR_MUX(4) | PORT_PCR_DSE | PORT_PCR_SRE;
  764. break;
  765. #endif
  766. #ifdef FTM3_CH2_PIN
  767. case FTM3_CH2_PIN:
  768. FTM3_C2V = cval;
  769. FTM_PINCFG(FTM3_CH2_PIN) = PORT_PCR_MUX(4) | PORT_PCR_DSE | PORT_PCR_SRE;
  770. break;
  771. #endif
  772. #ifdef FTM3_CH3_PIN
  773. case FTM3_CH3_PIN:
  774. FTM3_C3V = cval;
  775. FTM_PINCFG(FTM3_CH3_PIN) = PORT_PCR_MUX(4) | PORT_PCR_DSE | PORT_PCR_SRE;
  776. break;
  777. #endif
  778. #ifdef FTM3_CH4_PIN
  779. case FTM3_CH4_PIN:
  780. FTM3_C4V = cval;
  781. FTM_PINCFG(FTM3_CH4_PIN) = PORT_PCR_MUX(3) | PORT_PCR_DSE | PORT_PCR_SRE;
  782. break;
  783. #endif
  784. #ifdef FTM3_CH5_PIN
  785. case FTM3_CH5_PIN:
  786. FTM3_C5V = cval;
  787. FTM_PINCFG(FTM3_CH5_PIN) = PORT_PCR_MUX(3) | PORT_PCR_DSE | PORT_PCR_SRE;
  788. break;
  789. #endif
  790. #ifdef FTM3_CH6_PIN
  791. case FTM3_CH6_PIN:
  792. FTM3_C6V = cval;
  793. FTM_PINCFG(FTM3_CH6_PIN) = PORT_PCR_MUX(3) | PORT_PCR_DSE | PORT_PCR_SRE;
  794. break;
  795. #endif
  796. #ifdef FTM3_CH7_PIN
  797. case FTM3_CH7_PIN:
  798. FTM3_C7V = cval;
  799. FTM_PINCFG(FTM3_CH7_PIN) = PORT_PCR_MUX(3) | PORT_PCR_DSE | PORT_PCR_SRE;
  800. break;
  801. #endif
  802. default:
  803. digitalWrite(pin, (val > 127) ? HIGH : LOW);
  804. pinMode(pin, OUTPUT);
  805. }
  806. }
  807. void analogWriteRes(uint32_t bits)
  808. {
  809. if (bits < 1) {
  810. bits = 1;
  811. } else if (bits > 16) {
  812. bits = 16;
  813. }
  814. analog_write_res = bits;
  815. }
  816. void analogWriteFrequency(uint8_t pin, float frequency)
  817. {
  818. uint32_t prescale, mod, ftmClock, ftmClockSource;
  819. float minfreq;
  820. //serial_print("analogWriteFrequency: pin = ");
  821. //serial_phex(pin);
  822. //serial_print(", freq = ");
  823. //serial_phex32((uint32_t)frequency);
  824. //serial_print("\n");
  825. if (frequency < (float)(F_TIMER >> 7) / 65536.0f) { //If frequency is too low for working with F_TIMER:
  826. ftmClockSource = 2; //Use alternative 31250Hz clock source
  827. ftmClock = 31250; //Set variable for the actual timer clock frequency
  828. } else { //Else do as before:
  829. ftmClockSource = 1; //Use default F_Timer clock source
  830. ftmClock = F_TIMER; //Set variable for the actual timer clock frequency
  831. }
  832. for (prescale = 0; prescale < 7; prescale++) {
  833. minfreq = (float)(ftmClock >> prescale) / 65536.0f; //Use ftmClock instead of F_TIMER
  834. if (frequency >= minfreq) break;
  835. }
  836. //serial_print("F_TIMER/ftm_Clock = ");
  837. //serial_phex32(ftmClock >> prescale);
  838. //serial_print("\n");
  839. //serial_print("prescale = ");
  840. //serial_phex(prescale);
  841. //serial_print("\n");
  842. mod = (float)(ftmClock >> prescale) / frequency - 0.5f; //Use ftmClock instead of F_TIMER
  843. if (mod > 65535) mod = 65535;
  844. //serial_print("mod = ");
  845. //serial_phex32(mod);
  846. //serial_print("\n");
  847. if (pin == FTM1_CH0_PIN || pin == FTM1_CH1_PIN) {
  848. FTM1_SC = 0;
  849. FTM1_CNT = 0;
  850. FTM1_MOD = mod;
  851. FTM1_SC = FTM_SC_CLKS(ftmClockSource) | FTM_SC_PS(prescale); //Use ftmClockSource instead of 1
  852. } else if (pin == FTM0_CH0_PIN || pin == FTM0_CH1_PIN
  853. || pin == FTM0_CH2_PIN || pin == FTM0_CH3_PIN
  854. || pin == FTM0_CH4_PIN || pin == FTM0_CH5_PIN
  855. #ifdef FTM0_CH6_PIN
  856. || pin == FTM0_CH6_PIN || pin == FTM0_CH7_PIN
  857. #endif
  858. ) {
  859. FTM0_SC = 0;
  860. FTM0_CNT = 0;
  861. FTM0_MOD = mod;
  862. FTM0_SC = FTM_SC_CLKS(ftmClockSource) | FTM_SC_PS(prescale); //Use ftmClockSource instead of 1
  863. }
  864. #ifdef FTM2_CH0_PIN
  865. else if (pin == FTM2_CH0_PIN || pin == FTM2_CH1_PIN) {
  866. FTM2_SC = 0;
  867. FTM2_CNT = 0;
  868. FTM2_MOD = mod;
  869. FTM2_SC = FTM_SC_CLKS(ftmClockSource) | FTM_SC_PS(prescale); //Use ftmClockSource instead of 1
  870. }
  871. #endif
  872. #ifdef FTM3_CH0_PIN
  873. else if (pin == FTM3_CH0_PIN || pin == FTM3_CH1_PIN
  874. || pin == FTM3_CH2_PIN || pin == FTM3_CH3_PIN
  875. || pin == FTM3_CH4_PIN || pin == FTM3_CH5_PIN
  876. || pin == FTM3_CH6_PIN || pin == FTM3_CH7_PIN) {
  877. FTM3_SC = 0;
  878. FTM3_CNT = 0;
  879. FTM3_MOD = mod;
  880. FTM3_SC = FTM_SC_CLKS(ftmClockSource) | FTM_SC_PS(prescale); //Use the new ftmClockSource instead of 1
  881. }
  882. #endif
  883. }
  884. // TODO: startup code needs to initialize all pins to GPIO mode, input by default
  885. void digitalWrite(uint8_t pin, uint8_t val)
  886. {
  887. if (pin >= CORE_NUM_DIGITAL) return;
  888. #ifdef KINETISK
  889. if (*portModeRegister(pin)) {
  890. if (val) {
  891. *portSetRegister(pin) = 1;
  892. } else {
  893. *portClearRegister(pin) = 1;
  894. }
  895. #else
  896. if (*portModeRegister(pin) & digitalPinToBitMask(pin)) {
  897. if (val) {
  898. *portSetRegister(pin) = digitalPinToBitMask(pin);
  899. } else {
  900. *portClearRegister(pin) = digitalPinToBitMask(pin);
  901. }
  902. #endif
  903. } else {
  904. volatile uint32_t *config = portConfigRegister(pin);
  905. if (val) {
  906. // TODO use bitband for atomic read-mod-write
  907. *config |= (PORT_PCR_PE | PORT_PCR_PS);
  908. //*config = PORT_PCR_MUX(1) | PORT_PCR_PE | PORT_PCR_PS;
  909. } else {
  910. // TODO use bitband for atomic read-mod-write
  911. *config &= ~(PORT_PCR_PE);
  912. //*config = PORT_PCR_MUX(1);
  913. }
  914. }
  915. }
  916. uint8_t digitalRead(uint8_t pin)
  917. {
  918. if (pin >= CORE_NUM_DIGITAL) return 0;
  919. #ifdef KINETISK
  920. return *portInputRegister(pin);
  921. #else
  922. return (*portInputRegister(pin) & digitalPinToBitMask(pin)) ? 1 : 0;
  923. #endif
  924. }
  925. void pinMode(uint8_t pin, uint8_t mode)
  926. {
  927. volatile uint32_t *config;
  928. if (pin >= CORE_NUM_DIGITAL) return;
  929. config = portConfigRegister(pin);
  930. if (mode == OUTPUT || mode == OUTPUT_OPENDRAIN) {
  931. #ifdef KINETISK
  932. *portModeRegister(pin) = 1;
  933. #else
  934. *portModeRegister(pin) |= digitalPinToBitMask(pin); // TODO: atomic
  935. #endif
  936. *config = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
  937. if (mode == OUTPUT_OPENDRAIN) {
  938. *config |= PORT_PCR_ODE;
  939. } else {
  940. *config &= ~PORT_PCR_ODE;
  941. }
  942. } else {
  943. #ifdef KINETISK
  944. *portModeRegister(pin) = 0;
  945. #else
  946. *portModeRegister(pin) &= ~digitalPinToBitMask(pin);
  947. #endif
  948. if (mode == INPUT || mode == INPUT_PULLUP || mode == INPUT_PULLDOWN) {
  949. *config = PORT_PCR_MUX(1);
  950. if (mode == INPUT_PULLUP) {
  951. *config |= (PORT_PCR_PE | PORT_PCR_PS); // pullup
  952. } else if (mode == INPUT_PULLDOWN) {
  953. *config |= (PORT_PCR_PE); // pulldown
  954. *config &= ~(PORT_PCR_PS);
  955. }
  956. } else {
  957. *config = PORT_PCR_MUX(1) | PORT_PCR_PE | PORT_PCR_PS; // pullup
  958. }
  959. }
  960. }
  961. void _shiftOut(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder, uint8_t value)
  962. {
  963. if (bitOrder == LSBFIRST) {
  964. shiftOut_lsbFirst(dataPin, clockPin, value);
  965. } else {
  966. shiftOut_msbFirst(dataPin, clockPin, value);
  967. }
  968. }
  969. void shiftOut_lsbFirst(uint8_t dataPin, uint8_t clockPin, uint8_t value)
  970. {
  971. uint8_t mask;
  972. for (mask=0x01; mask; mask <<= 1) {
  973. digitalWrite(dataPin, value & mask);
  974. digitalWrite(clockPin, HIGH);
  975. digitalWrite(clockPin, LOW);
  976. }
  977. }
  978. void shiftOut_msbFirst(uint8_t dataPin, uint8_t clockPin, uint8_t value)
  979. {
  980. uint8_t mask;
  981. for (mask=0x80; mask; mask >>= 1) {
  982. digitalWrite(dataPin, value & mask);
  983. digitalWrite(clockPin, HIGH);
  984. digitalWrite(clockPin, LOW);
  985. }
  986. }
  987. uint8_t _shiftIn(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder)
  988. {
  989. if (bitOrder == LSBFIRST) {
  990. return shiftIn_lsbFirst(dataPin, clockPin);
  991. } else {
  992. return shiftIn_msbFirst(dataPin, clockPin);
  993. }
  994. }
  995. uint8_t shiftIn_lsbFirst(uint8_t dataPin, uint8_t clockPin)
  996. {
  997. uint8_t mask, value=0;
  998. for (mask=0x01; mask; mask <<= 1) {
  999. digitalWrite(clockPin, HIGH);
  1000. if (digitalRead(dataPin)) value |= mask;
  1001. digitalWrite(clockPin, LOW);
  1002. }
  1003. return value;
  1004. }
  1005. uint8_t shiftIn_msbFirst(uint8_t dataPin, uint8_t clockPin)
  1006. {
  1007. uint8_t mask, value=0;
  1008. for (mask=0x80; mask; mask >>= 1) {
  1009. digitalWrite(clockPin, HIGH);
  1010. if (digitalRead(dataPin)) value |= mask;
  1011. digitalWrite(clockPin, LOW);
  1012. }
  1013. return value;
  1014. }
  1015. // the systick interrupt is supposed to increment this at 1 kHz rate
  1016. volatile uint32_t systick_millis_count = 0;
  1017. //uint32_t systick_current, systick_count, systick_istatus; // testing only
  1018. uint32_t micros(void)
  1019. {
  1020. uint32_t count, current, istatus;
  1021. __disable_irq();
  1022. current = SYST_CVR;
  1023. count = systick_millis_count;
  1024. istatus = SCB_ICSR; // bit 26 indicates if systick exception pending
  1025. __enable_irq();
  1026. //systick_current = current;
  1027. //systick_count = count;
  1028. //systick_istatus = istatus & SCB_ICSR_PENDSTSET ? 1 : 0;
  1029. if ((istatus & SCB_ICSR_PENDSTSET) && current > 50) count++;
  1030. current = ((F_CPU / 1000) - 1) - current;
  1031. #if defined(KINETISL) && F_CPU == 48000000
  1032. return count * 1000 + ((current * (uint32_t)87381) >> 22);
  1033. #elif defined(KINETISL) && F_CPU == 24000000
  1034. return count * 1000 + ((current * (uint32_t)174763) >> 22);
  1035. #endif
  1036. return count * 1000 + current / (F_CPU / 1000000);
  1037. }
  1038. void delay(uint32_t ms)
  1039. {
  1040. uint32_t start = micros();
  1041. if (ms > 0) {
  1042. while (1) {
  1043. while ((micros() - start) >= 1000) {
  1044. ms--;
  1045. if (ms == 0) return;
  1046. start += 1000;
  1047. }
  1048. yield();
  1049. }
  1050. }
  1051. }
  1052. // TODO: verify these result in correct timeouts...
  1053. #if F_CPU == 240000000
  1054. #define PULSEIN_LOOPS_PER_USEC 33
  1055. #elif F_CPU == 216000000
  1056. #define PULSEIN_LOOPS_PER_USEC 31
  1057. #elif F_CPU == 192000000
  1058. #define PULSEIN_LOOPS_PER_USEC 29
  1059. #elif F_CPU == 180000000
  1060. #define PULSEIN_LOOPS_PER_USEC 27
  1061. #elif F_CPU == 168000000
  1062. #define PULSEIN_LOOPS_PER_USEC 25
  1063. #elif F_CPU == 144000000
  1064. #define PULSEIN_LOOPS_PER_USEC 21
  1065. #elif F_CPU == 120000000
  1066. #define PULSEIN_LOOPS_PER_USEC 18
  1067. #elif F_CPU == 96000000
  1068. #define PULSEIN_LOOPS_PER_USEC 14
  1069. #elif F_CPU == 72000000
  1070. #define PULSEIN_LOOPS_PER_USEC 10
  1071. #elif F_CPU == 48000000
  1072. #define PULSEIN_LOOPS_PER_USEC 7
  1073. #elif F_CPU == 24000000
  1074. #define PULSEIN_LOOPS_PER_USEC 4
  1075. #elif F_CPU == 16000000
  1076. #define PULSEIN_LOOPS_PER_USEC 1
  1077. #elif F_CPU == 8000000
  1078. #define PULSEIN_LOOPS_PER_USEC 1
  1079. #elif F_CPU == 4000000
  1080. #define PULSEIN_LOOPS_PER_USEC 1
  1081. #elif F_CPU == 2000000
  1082. #define PULSEIN_LOOPS_PER_USEC 1
  1083. #endif
  1084. #if defined(KINETISK)
  1085. uint32_t pulseIn_high(volatile uint8_t *reg, uint32_t timeout)
  1086. {
  1087. uint32_t timeout_count = timeout * PULSEIN_LOOPS_PER_USEC;
  1088. uint32_t usec_start, usec_stop;
  1089. // wait for any previous pulse to end
  1090. while (*reg) {
  1091. if (--timeout_count == 0) return 0;
  1092. }
  1093. // wait for the pulse to start
  1094. while (!*reg) {
  1095. if (--timeout_count == 0) return 0;
  1096. }
  1097. usec_start = micros();
  1098. // wait for the pulse to stop
  1099. while (*reg) {
  1100. if (--timeout_count == 0) return 0;
  1101. }
  1102. usec_stop = micros();
  1103. return usec_stop - usec_start;
  1104. }
  1105. uint32_t pulseIn_low(volatile uint8_t *reg, uint32_t timeout)
  1106. {
  1107. uint32_t timeout_count = timeout * PULSEIN_LOOPS_PER_USEC;
  1108. uint32_t usec_start, usec_stop;
  1109. // wait for any previous pulse to end
  1110. while (!*reg) {
  1111. if (--timeout_count == 0) return 0;
  1112. }
  1113. // wait for the pulse to start
  1114. while (*reg) {
  1115. if (--timeout_count == 0) return 0;
  1116. }
  1117. usec_start = micros();
  1118. // wait for the pulse to stop
  1119. while (!*reg) {
  1120. if (--timeout_count == 0) return 0;
  1121. }
  1122. usec_stop = micros();
  1123. return usec_stop - usec_start;
  1124. }
  1125. // TODO: an inline version should handle the common case where state is const
  1126. uint32_t pulseIn(uint8_t pin, uint8_t state, uint32_t timeout)
  1127. {
  1128. if (pin >= CORE_NUM_DIGITAL) return 0;
  1129. if (state) return pulseIn_high(portInputRegister(pin), timeout);
  1130. return pulseIn_low(portInputRegister(pin), timeout);;
  1131. }
  1132. #elif defined(KINETISL)
  1133. // For TeencyLC need to use mask on the input register as the register is shared by several IO pins
  1134. uint32_t pulseIn_high(volatile uint8_t *reg, uint8_t mask, uint32_t timeout)
  1135. {
  1136. uint32_t timeout_count = timeout * PULSEIN_LOOPS_PER_USEC;
  1137. uint32_t usec_start, usec_stop;
  1138. // wait for any previous pulse to end
  1139. while (*reg & mask) {
  1140. if (--timeout_count == 0) return -1;
  1141. }
  1142. // wait for the pulse to start
  1143. while (!(*reg & mask)) {
  1144. if (--timeout_count == 0) return 0;
  1145. }
  1146. usec_start = micros();
  1147. // wait for the pulse to stop
  1148. while (*reg & mask) {
  1149. if (--timeout_count == 0) return 0;
  1150. }
  1151. usec_stop = micros();
  1152. return usec_stop - usec_start;
  1153. }
  1154. uint32_t pulseIn_low(volatile uint8_t *reg, uint8_t mask, uint32_t timeout)
  1155. {
  1156. uint32_t timeout_count = timeout * PULSEIN_LOOPS_PER_USEC;
  1157. uint32_t usec_start, usec_stop;
  1158. // wait for any previous pulse to end
  1159. while (!(*reg & mask)) {
  1160. if (--timeout_count == 0) return 0;
  1161. }
  1162. // wait for the pulse to start
  1163. while (*reg & mask) {
  1164. if (--timeout_count == 0) return 0;
  1165. }
  1166. usec_start = micros();
  1167. // wait for the pulse to stop
  1168. while (!(*reg & mask)) {
  1169. if (--timeout_count == 0) return 0;
  1170. }
  1171. usec_stop = micros();
  1172. return usec_stop - usec_start;
  1173. }
  1174. // TODO: an inline version should handle the common case where state is const
  1175. uint32_t pulseIn(uint8_t pin, uint8_t state, uint32_t timeout)
  1176. {
  1177. if (pin >= CORE_NUM_DIGITAL) return 0;
  1178. if (state) return pulseIn_high(portInputRegister(pin), digitalPinToBitMask(pin), timeout);
  1179. return pulseIn_low(portInputRegister(pin), digitalPinToBitMask(pin), timeout);;
  1180. }
  1181. #endif