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.

984 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. #endif
  481. max = 1 << analog_write_res;
  482. if (val <= 0) {
  483. digitalWrite(pin, LOW);
  484. pinMode(pin, OUTPUT); // TODO: implement OUTPUT_LOW
  485. return;
  486. } else if (val >= max) {
  487. digitalWrite(pin, HIGH);
  488. pinMode(pin, OUTPUT); // TODO: implement OUTPUT_HIGH
  489. return;
  490. }
  491. //serial_print("analogWrite\n");
  492. //serial_print("val = ");
  493. //serial_phex32(val);
  494. //serial_print("\n");
  495. //serial_print("analog_write_res = ");
  496. //serial_phex(analog_write_res);
  497. //serial_print("\n");
  498. if (pin == FTM1_CH0_PIN || pin == FTM1_CH1_PIN) {
  499. cval = ((uint32_t)val * (uint32_t)(FTM1_MOD + 1)) >> analog_write_res;
  500. #if defined(FTM2_CH0_PIN)
  501. } else if (pin == FTM2_CH0_PIN || pin == FTM2_CH1_PIN) {
  502. cval = ((uint32_t)val * (uint32_t)(FTM2_MOD + 1)) >> analog_write_res;
  503. #endif
  504. } else {
  505. cval = ((uint32_t)val * (uint32_t)(FTM0_MOD + 1)) >> analog_write_res;
  506. }
  507. //serial_print("cval = ");
  508. //serial_phex32(cval);
  509. //serial_print("\n");
  510. switch (pin) {
  511. #ifdef FTM0_CH0_PIN
  512. case FTM0_CH0_PIN: // PTC1, FTM0_CH0
  513. FTM0_C0V = cval;
  514. FTM_PINCFG(FTM0_CH0_PIN) = PORT_PCR_MUX(4) | PORT_PCR_DSE | PORT_PCR_SRE;
  515. break;
  516. #endif
  517. #ifdef FTM0_CH1_PIN
  518. case FTM0_CH1_PIN: // PTC2, FTM0_CH1
  519. FTM0_C1V = cval;
  520. FTM_PINCFG(FTM0_CH1_PIN) = PORT_PCR_MUX(4) | PORT_PCR_DSE | PORT_PCR_SRE;
  521. break;
  522. #endif
  523. #ifdef FTM0_CH2_PIN
  524. case FTM0_CH2_PIN: // PTC3, FTM0_CH2
  525. FTM0_C2V = cval;
  526. FTM_PINCFG(FTM0_CH2_PIN) = PORT_PCR_MUX(4) | PORT_PCR_DSE | PORT_PCR_SRE;
  527. break;
  528. #endif
  529. #ifdef FTM0_CH3_PIN
  530. case FTM0_CH3_PIN: // PTC4, FTM0_CH3
  531. FTM0_C3V = cval;
  532. FTM_PINCFG(FTM0_CH3_PIN) = PORT_PCR_MUX(4) | PORT_PCR_DSE | PORT_PCR_SRE;
  533. break;
  534. #endif
  535. #ifdef FTM0_CH4_PIN
  536. case FTM0_CH4_PIN: // PTD4, FTM0_CH4
  537. FTM0_C4V = cval;
  538. FTM_PINCFG(FTM0_CH4_PIN) = PORT_PCR_MUX(4) | PORT_PCR_DSE | PORT_PCR_SRE;
  539. break;
  540. #endif
  541. #ifdef FTM0_CH5_PIN
  542. case FTM0_CH5_PIN: // PTD5, FTM0_CH5
  543. FTM0_C5V = cval;
  544. FTM_PINCFG(FTM0_CH5_PIN) = PORT_PCR_MUX(4) | PORT_PCR_DSE | PORT_PCR_SRE;
  545. break;
  546. #endif
  547. #ifdef FTM0_CH6_PIN
  548. case FTM0_CH6_PIN: // PTD6, FTM0_CH6
  549. FTM0_C6V = cval;
  550. FTM_PINCFG(FTM0_CH6_PIN) = PORT_PCR_MUX(4) | PORT_PCR_DSE | PORT_PCR_SRE;
  551. break;
  552. #endif
  553. #ifdef FTM0_CH7_PIN
  554. case FTM0_CH7_PIN: // PTD7, FTM0_CH7
  555. FTM0_C7V = cval;
  556. FTM_PINCFG(FTM0_CH7_PIN) = PORT_PCR_MUX(4) | PORT_PCR_DSE | PORT_PCR_SRE;
  557. break;
  558. #endif
  559. #ifdef FTM1_CH0_PIN
  560. case FTM1_CH0_PIN: // PTA12, FTM1_CH0
  561. FTM1_C0V = cval;
  562. FTM_PINCFG(FTM1_CH0_PIN) = PORT_PCR_MUX(3) | PORT_PCR_DSE | PORT_PCR_SRE;
  563. break;
  564. #endif
  565. #ifdef FTM1_CH1_PIN
  566. case FTM1_CH1_PIN: // PTA13, FTM1_CH1
  567. FTM1_C1V = cval;
  568. FTM_PINCFG(FTM1_CH1_PIN) = PORT_PCR_MUX(3) | PORT_PCR_DSE | PORT_PCR_SRE;
  569. break;
  570. #endif
  571. #ifdef FTM2_CH0_PIN
  572. case FTM2_CH0_PIN: // PTB18, FTM2_CH0
  573. FTM2_C0V = cval;
  574. FTM_PINCFG(FTM2_CH0_PIN) = PORT_PCR_MUX(3) | PORT_PCR_DSE | PORT_PCR_SRE;
  575. break;
  576. #endif
  577. #ifdef FTM2_CH1_PIN
  578. case FTM2_CH1_PIN: // PTB19, FTM1_CH1
  579. FTM2_C1V = cval;
  580. FTM_PINCFG(FTM2_CH1_PIN) = PORT_PCR_MUX(3) | PORT_PCR_DSE | PORT_PCR_SRE;
  581. break;
  582. #endif
  583. default:
  584. digitalWrite(pin, (val > 127) ? HIGH : LOW);
  585. pinMode(pin, OUTPUT);
  586. }
  587. }
  588. void analogWriteRes(uint32_t bits)
  589. {
  590. if (bits < 1) {
  591. bits = 1;
  592. } else if (bits > 16) {
  593. bits = 16;
  594. }
  595. analog_write_res = bits;
  596. }
  597. void analogWriteFrequency(uint8_t pin, uint32_t frequency)
  598. {
  599. uint32_t minfreq, prescale, mod;
  600. //serial_print("analogWriteFrequency: pin = ");
  601. //serial_phex(pin);
  602. //serial_print(", freq = ");
  603. //serial_phex32(frequency);
  604. //serial_print("\n");
  605. for (prescale = 0; prescale < 7; prescale++) {
  606. minfreq = (F_TIMER >> 16) >> prescale;
  607. if (frequency > minfreq) break;
  608. }
  609. //serial_print("F_TIMER = ");
  610. //serial_phex32(F_TIMER >> prescale);
  611. //serial_print("\n");
  612. //serial_print("prescale = ");
  613. //serial_phex(prescale);
  614. //serial_print("\n");
  615. //mod = ((F_TIMER >> prescale) / frequency) - 1;
  616. mod = (((F_TIMER >> prescale) + (frequency >> 1)) / frequency) - 1;
  617. if (mod > 65535) mod = 65535;
  618. //serial_print("mod = ");
  619. //serial_phex32(mod);
  620. //serial_print("\n");
  621. if (pin == FTM1_CH0_PIN || pin == FTM1_CH1_PIN) {
  622. FTM1_SC = 0;
  623. FTM1_CNT = 0;
  624. FTM1_MOD = mod;
  625. FTM1_SC = FTM_SC_CLKS(1) | FTM_SC_PS(prescale);
  626. } else if (pin == FTM0_CH0_PIN || pin == FTM0_CH1_PIN
  627. || pin == FTM0_CH2_PIN || pin == FTM0_CH3_PIN
  628. || pin == FTM0_CH4_PIN || pin == FTM0_CH5_PIN
  629. #ifdef FTM0_CH6_PIN
  630. || pin == FTM0_CH6_PIN || pin == FTM0_CH7_PIN
  631. #endif
  632. ) {
  633. FTM0_SC = 0;
  634. FTM0_CNT = 0;
  635. FTM0_MOD = mod;
  636. FTM0_SC = FTM_SC_CLKS(1) | FTM_SC_PS(prescale);
  637. }
  638. #ifdef FTM2_CH0_PIN
  639. else if (pin == FTM2_CH0_PIN || pin == FTM2_CH1_PIN) {
  640. FTM2_SC = 0;
  641. FTM2_CNT = 0;
  642. FTM2_MOD = mod;
  643. FTM2_SC = FTM_SC_CLKS(1) | FTM_SC_PS(prescale);
  644. }
  645. #endif
  646. }
  647. // TODO: startup code needs to initialize all pins to GPIO mode, input by default
  648. void digitalWrite(uint8_t pin, uint8_t val)
  649. {
  650. if (pin >= CORE_NUM_DIGITAL) return;
  651. #ifdef KINETISK
  652. if (*portModeRegister(pin)) {
  653. if (val) {
  654. *portSetRegister(pin) = 1;
  655. } else {
  656. *portClearRegister(pin) = 1;
  657. }
  658. #else
  659. if (*portModeRegister(pin) & digitalPinToBitMask(pin)) {
  660. if (val) {
  661. *portSetRegister(pin) = digitalPinToBitMask(pin);
  662. } else {
  663. *portClearRegister(pin) = digitalPinToBitMask(pin);
  664. }
  665. #endif
  666. } else {
  667. volatile uint32_t *config = portConfigRegister(pin);
  668. if (val) {
  669. // TODO use bitband for atomic read-mod-write
  670. *config |= (PORT_PCR_PE | PORT_PCR_PS);
  671. //*config = PORT_PCR_MUX(1) | PORT_PCR_PE | PORT_PCR_PS;
  672. } else {
  673. // TODO use bitband for atomic read-mod-write
  674. *config &= ~(PORT_PCR_PE);
  675. //*config = PORT_PCR_MUX(1);
  676. }
  677. }
  678. }
  679. uint8_t digitalRead(uint8_t pin)
  680. {
  681. if (pin >= CORE_NUM_DIGITAL) return 0;
  682. #ifdef KINETISK
  683. return *portInputRegister(pin);
  684. #else
  685. return (*portInputRegister(pin) & digitalPinToBitMask(pin)) ? 1 : 0;
  686. #endif
  687. }
  688. void pinMode(uint8_t pin, uint8_t mode)
  689. {
  690. volatile uint32_t *config;
  691. if (pin >= CORE_NUM_DIGITAL) return;
  692. config = portConfigRegister(pin);
  693. if (mode == OUTPUT) {
  694. #ifdef KINETISK
  695. *portModeRegister(pin) = 1;
  696. #else
  697. *portModeRegister(pin) |= digitalPinToBitMask(pin); // TODO: atomic
  698. #endif
  699. *config = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
  700. } else {
  701. #ifdef KINETISK
  702. *portModeRegister(pin) = 0;
  703. #else
  704. *portModeRegister(pin) &= ~digitalPinToBitMask(pin);
  705. #endif
  706. if (mode == INPUT) {
  707. *config = PORT_PCR_MUX(1);
  708. } else {
  709. *config = PORT_PCR_MUX(1) | PORT_PCR_PE | PORT_PCR_PS; // pullup
  710. }
  711. }
  712. }
  713. void _shiftOut(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder, uint8_t value)
  714. {
  715. if (bitOrder == LSBFIRST) {
  716. shiftOut_lsbFirst(dataPin, clockPin, value);
  717. } else {
  718. shiftOut_msbFirst(dataPin, clockPin, value);
  719. }
  720. }
  721. void shiftOut_lsbFirst(uint8_t dataPin, uint8_t clockPin, uint8_t value)
  722. {
  723. uint8_t mask;
  724. for (mask=0x01; mask; mask <<= 1) {
  725. digitalWrite(dataPin, value & mask);
  726. digitalWrite(clockPin, HIGH);
  727. digitalWrite(clockPin, LOW);
  728. }
  729. }
  730. void shiftOut_msbFirst(uint8_t dataPin, uint8_t clockPin, uint8_t value)
  731. {
  732. uint8_t mask;
  733. for (mask=0x80; mask; mask >>= 1) {
  734. digitalWrite(dataPin, value & mask);
  735. digitalWrite(clockPin, HIGH);
  736. digitalWrite(clockPin, LOW);
  737. }
  738. }
  739. uint8_t _shiftIn(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder)
  740. {
  741. if (bitOrder == LSBFIRST) {
  742. return shiftIn_lsbFirst(dataPin, clockPin);
  743. } else {
  744. return shiftIn_msbFirst(dataPin, clockPin);
  745. }
  746. }
  747. uint8_t shiftIn_lsbFirst(uint8_t dataPin, uint8_t clockPin)
  748. {
  749. uint8_t mask, value=0;
  750. for (mask=0x01; mask; mask <<= 1) {
  751. digitalWrite(clockPin, HIGH);
  752. if (digitalRead(dataPin)) value |= mask;
  753. digitalWrite(clockPin, LOW);
  754. }
  755. return value;
  756. }
  757. uint8_t shiftIn_msbFirst(uint8_t dataPin, uint8_t clockPin)
  758. {
  759. uint8_t mask, value=0;
  760. for (mask=0x80; mask; mask >>= 1) {
  761. digitalWrite(clockPin, HIGH);
  762. if (digitalRead(dataPin)) value |= mask;
  763. digitalWrite(clockPin, LOW);
  764. }
  765. return value;
  766. }
  767. // the systick interrupt is supposed to increment this at 1 kHz rate
  768. volatile uint32_t systick_millis_count = 0;
  769. //uint32_t systick_current, systick_count, systick_istatus; // testing only
  770. uint32_t micros(void)
  771. {
  772. uint32_t count, current, istatus;
  773. __disable_irq();
  774. current = SYST_CVR;
  775. count = systick_millis_count;
  776. istatus = SCB_ICSR; // bit 26 indicates if systick exception pending
  777. __enable_irq();
  778. //systick_current = current;
  779. //systick_count = count;
  780. //systick_istatus = istatus & SCB_ICSR_PENDSTSET ? 1 : 0;
  781. if ((istatus & SCB_ICSR_PENDSTSET) && current > 50) count++;
  782. current = ((F_CPU / 1000) - 1) - current;
  783. return count * 1000 + current / (F_CPU / 1000000);
  784. }
  785. void delay(uint32_t ms)
  786. {
  787. uint32_t start = micros();
  788. if (ms > 0) {
  789. while (1) {
  790. if ((micros() - start) >= 1000) {
  791. ms--;
  792. if (ms == 0) return;
  793. start += 1000;
  794. }
  795. yield();
  796. }
  797. }
  798. }
  799. // TODO: verify these result in correct timeouts...
  800. #if F_CPU == 168000000
  801. #define PULSEIN_LOOPS_PER_USEC 25
  802. #elif F_CPU == 144000000
  803. #define PULSEIN_LOOPS_PER_USEC 21
  804. #elif F_CPU == 120000000
  805. #define PULSEIN_LOOPS_PER_USEC 18
  806. #elif F_CPU == 96000000
  807. #define PULSEIN_LOOPS_PER_USEC 14
  808. #elif F_CPU == 72000000
  809. #define PULSEIN_LOOPS_PER_USEC 10
  810. #elif F_CPU == 48000000
  811. #define PULSEIN_LOOPS_PER_USEC 7
  812. #elif F_CPU == 24000000
  813. #define PULSEIN_LOOPS_PER_USEC 4
  814. #elif F_CPU == 16000000
  815. #define PULSEIN_LOOPS_PER_USEC 1
  816. #elif F_CPU == 8000000
  817. #define PULSEIN_LOOPS_PER_USEC 1
  818. #elif F_CPU == 4000000
  819. #define PULSEIN_LOOPS_PER_USEC 1
  820. #elif F_CPU == 2000000
  821. #define PULSEIN_LOOPS_PER_USEC 1
  822. #endif
  823. uint32_t pulseIn_high(volatile uint8_t *reg, uint32_t timeout)
  824. {
  825. uint32_t timeout_count = timeout * PULSEIN_LOOPS_PER_USEC;
  826. uint32_t usec_start, usec_stop;
  827. // wait for any previous pulse to end
  828. while (*reg) {
  829. if (--timeout_count == 0) return 0;
  830. }
  831. // wait for the pulse to start
  832. while (!*reg) {
  833. if (--timeout_count == 0) return 0;
  834. }
  835. usec_start = micros();
  836. // wait for the pulse to stop
  837. while (*reg) {
  838. if (--timeout_count == 0) return 0;
  839. }
  840. usec_stop = micros();
  841. return usec_stop - usec_start;
  842. }
  843. uint32_t pulseIn_low(volatile uint8_t *reg, uint32_t timeout)
  844. {
  845. uint32_t timeout_count = timeout * PULSEIN_LOOPS_PER_USEC;
  846. uint32_t usec_start, usec_stop;
  847. // wait for any previous pulse to end
  848. while (!*reg) {
  849. if (--timeout_count == 0) return 0;
  850. }
  851. // wait for the pulse to start
  852. while (*reg) {
  853. if (--timeout_count == 0) return 0;
  854. }
  855. usec_start = micros();
  856. // wait for the pulse to stop
  857. while (!*reg) {
  858. if (--timeout_count == 0) return 0;
  859. }
  860. usec_stop = micros();
  861. return usec_stop - usec_start;
  862. }
  863. // TODO: an inline version should handle the common case where state is const
  864. uint32_t pulseIn(uint8_t pin, uint8_t state, uint32_t timeout)
  865. {
  866. if (pin >= CORE_NUM_DIGITAL) return 0;
  867. if (state) return pulseIn_high(portInputRegister(pin), timeout);
  868. return pulseIn_low(portInputRegister(pin), timeout);;
  869. }