Teensy 4.1 core updated for C++20
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.

995 lines
30KB

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