Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

1006 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. #if defined(__MK20DX128__) || defined(__MK20DX256__)
  258. unsigned long rtc_get(void)
  259. {
  260. return RTC_TSR;
  261. }
  262. void rtc_set(unsigned long t)
  263. {
  264. RTC_SR = 0;
  265. RTC_TPR = 0;
  266. RTC_TSR = t;
  267. RTC_SR = RTC_SR_TCE;
  268. }
  269. // adjust is the amount of crystal error to compensate, 1 = 0.1192 ppm
  270. // For example, adjust = -100 is slows the clock by 11.92 ppm
  271. //
  272. void rtc_compensate(int adjust)
  273. {
  274. uint32_t comp, interval, tcr;
  275. // This simple approach tries to maximize the interval.
  276. // Perhaps minimizing TCR would be better, so the
  277. // compensation is distributed more evenly across
  278. // many seconds, rather than saving it all up and then
  279. // altering one second up to +/- 0.38%
  280. if (adjust >= 0) {
  281. comp = adjust;
  282. interval = 256;
  283. while (1) {
  284. tcr = comp * interval;
  285. if (tcr < 128*256) break;
  286. if (--interval == 1) break;
  287. }
  288. tcr = tcr >> 8;
  289. } else {
  290. comp = -adjust;
  291. interval = 256;
  292. while (1) {
  293. tcr = comp * interval;
  294. if (tcr < 129*256) break;
  295. if (--interval == 1) break;
  296. }
  297. tcr = tcr >> 8;
  298. tcr = 256 - tcr;
  299. }
  300. RTC_TCR = ((interval - 1) << 8) | tcr;
  301. }
  302. #else
  303. unsigned long rtc_get(void) { return 0; }
  304. void rtc_set(unsigned long t) { }
  305. void rtc_compensate(int adjust) { }
  306. #endif
  307. #if 0
  308. // TODO: build system should define this
  309. // so RTC is automatically initialized to approx correct time
  310. // at least when the program begins running right after upload
  311. #ifndef TIME_T
  312. #define TIME_T 1350160272
  313. #endif
  314. void init_rtc(void)
  315. {
  316. serial_print("init_rtc\n");
  317. //SIM_SCGC6 |= SIM_SCGC6_RTC;
  318. // enable the RTC crystal oscillator, for approx 12pf crystal
  319. if (!(RTC_CR & RTC_CR_OSCE)) {
  320. serial_print("start RTC oscillator\n");
  321. RTC_SR = 0;
  322. RTC_CR = RTC_CR_SC16P | RTC_CR_SC4P | RTC_CR_OSCE;
  323. }
  324. // should wait for crystal to stabilize.....
  325. serial_print("SR=");
  326. serial_phex32(RTC_SR);
  327. serial_print("\n");
  328. serial_print("CR=");
  329. serial_phex32(RTC_CR);
  330. serial_print("\n");
  331. serial_print("TSR=");
  332. serial_phex32(RTC_TSR);
  333. serial_print("\n");
  334. serial_print("TCR=");
  335. serial_phex32(RTC_TCR);
  336. serial_print("\n");
  337. if (RTC_SR & RTC_SR_TIF) {
  338. // enable the RTC
  339. RTC_SR = 0;
  340. RTC_TPR = 0;
  341. RTC_TSR = TIME_T;
  342. RTC_SR = RTC_SR_TCE;
  343. }
  344. }
  345. #endif
  346. extern void usb_init(void);
  347. // create a default PWM at the same 488.28 Hz as Arduino Uno
  348. #if defined(KINETISK)
  349. #define F_TIMER F_BUS
  350. #elif defined(KINETISL)
  351. #define F_TIMER (F_PLL/2)
  352. #endif
  353. #if F_TIMER == 60000000
  354. #define DEFAULT_FTM_MOD (61440 - 1)
  355. #define DEFAULT_FTM_PRESCALE 1
  356. #elif F_TIMER == 56000000
  357. #define DEFAULT_FTM_MOD (57344 - 1)
  358. #define DEFAULT_FTM_PRESCALE 1
  359. #elif F_TIMER == 48000000
  360. #define DEFAULT_FTM_MOD (49152 - 1)
  361. #define DEFAULT_FTM_PRESCALE 1
  362. #elif F_TIMER == 40000000
  363. #define DEFAULT_FTM_MOD (40960 - 1)
  364. #define DEFAULT_FTM_PRESCALE 1
  365. #elif F_TIMER == 36000000
  366. #define DEFAULT_FTM_MOD (36864 - 1)
  367. #define DEFAULT_FTM_PRESCALE 1
  368. #elif F_TIMER == 24000000
  369. #define DEFAULT_FTM_MOD (49152 - 1)
  370. #define DEFAULT_FTM_PRESCALE 0
  371. #elif F_TIMER == 16000000
  372. #define DEFAULT_FTM_MOD (32768 - 1)
  373. #define DEFAULT_FTM_PRESCALE 0
  374. #elif F_TIMER == 8000000
  375. #define DEFAULT_FTM_MOD (16384 - 1)
  376. #define DEFAULT_FTM_PRESCALE 0
  377. #elif F_TIMER == 4000000
  378. #define DEFAULT_FTM_MOD (8192 - 1)
  379. #define DEFAULT_FTM_PRESCALE 0
  380. #elif F_TIMER == 2000000
  381. #define DEFAULT_FTM_MOD (4096 - 1)
  382. #define DEFAULT_FTM_PRESCALE 0
  383. #endif
  384. //void init_pins(void)
  385. void _init_Teensyduino_internal_(void)
  386. {
  387. #if defined(__MK20DX128__) || defined(__MK20DX256__)
  388. NVIC_ENABLE_IRQ(IRQ_PORTA);
  389. NVIC_ENABLE_IRQ(IRQ_PORTB);
  390. NVIC_ENABLE_IRQ(IRQ_PORTC);
  391. NVIC_ENABLE_IRQ(IRQ_PORTD);
  392. NVIC_ENABLE_IRQ(IRQ_PORTE);
  393. #elif defined(__MKL26Z64__)
  394. NVIC_ENABLE_IRQ(IRQ_PORTA);
  395. NVIC_ENABLE_IRQ(IRQ_PORTCD);
  396. #endif
  397. //SIM_SCGC6 |= SIM_SCGC6_FTM0; // TODO: use bitband for atomic read-mod-write
  398. //SIM_SCGC6 |= SIM_SCGC6_FTM1;
  399. FTM0_CNT = 0;
  400. FTM0_MOD = DEFAULT_FTM_MOD;
  401. FTM0_C0SC = 0x28; // MSnB:MSnA = 10, ELSnB:ELSnA = 10
  402. FTM0_C1SC = 0x28;
  403. FTM0_C2SC = 0x28;
  404. FTM0_C3SC = 0x28;
  405. FTM0_C4SC = 0x28;
  406. FTM0_C5SC = 0x28;
  407. #if defined(__MK20DX128__) || defined(__MK20DX256__)
  408. FTM0_C6SC = 0x28;
  409. FTM0_C7SC = 0x28;
  410. #endif
  411. FTM0_SC = FTM_SC_CLKS(1) | FTM_SC_PS(DEFAULT_FTM_PRESCALE);
  412. FTM1_CNT = 0;
  413. FTM1_MOD = DEFAULT_FTM_MOD;
  414. FTM1_C0SC = 0x28;
  415. FTM1_C1SC = 0x28;
  416. FTM1_SC = FTM_SC_CLKS(1) | FTM_SC_PS(DEFAULT_FTM_PRESCALE);
  417. #if defined(__MK20DX256__) || defined(__MKL26Z64__)
  418. FTM2_CNT = 0;
  419. FTM2_MOD = DEFAULT_FTM_MOD;
  420. FTM2_C0SC = 0x28;
  421. FTM2_C1SC = 0x28;
  422. FTM2_SC = FTM_SC_CLKS(1) | FTM_SC_PS(DEFAULT_FTM_PRESCALE);
  423. #endif
  424. analog_init();
  425. //delay(100); // TODO: this is not necessary, right?
  426. delay(4);
  427. usb_init();
  428. }
  429. #if defined(__MK20DX128__)
  430. #define FTM0_CH0_PIN 22
  431. #define FTM0_CH1_PIN 23
  432. #define FTM0_CH2_PIN 9
  433. #define FTM0_CH3_PIN 10
  434. #define FTM0_CH4_PIN 6
  435. #define FTM0_CH5_PIN 20
  436. #define FTM0_CH6_PIN 21
  437. #define FTM0_CH7_PIN 5
  438. #define FTM1_CH0_PIN 3
  439. #define FTM1_CH1_PIN 4
  440. #elif defined(__MK20DX256__)
  441. #define FTM0_CH0_PIN 22
  442. #define FTM0_CH1_PIN 23
  443. #define FTM0_CH2_PIN 9
  444. #define FTM0_CH3_PIN 10
  445. #define FTM0_CH4_PIN 6
  446. #define FTM0_CH5_PIN 20
  447. #define FTM0_CH6_PIN 21
  448. #define FTM0_CH7_PIN 5
  449. #define FTM1_CH0_PIN 3
  450. #define FTM1_CH1_PIN 4
  451. #define FTM2_CH0_PIN 32
  452. #define FTM2_CH1_PIN 25
  453. #elif defined(__MKL26Z64__)
  454. #define FTM0_CH0_PIN 22
  455. #define FTM0_CH1_PIN 23
  456. #define FTM0_CH2_PIN 9
  457. #define FTM0_CH3_PIN 10
  458. #define FTM0_CH4_PIN 6
  459. #define FTM0_CH5_PIN 20
  460. #define FTM1_CH0_PIN 16
  461. #define FTM1_CH1_PIN 17
  462. #define FTM2_CH0_PIN 3
  463. #define FTM2_CH1_PIN 4
  464. #endif
  465. #define FTM_PINCFG(pin) FTM_PINCFG2(pin)
  466. #define FTM_PINCFG2(pin) CORE_PIN ## pin ## _CONFIG
  467. static uint8_t analog_write_res = 8;
  468. // SOPT4 is SIM select clocks?
  469. // FTM is clocked by the bus clock, either 24 or 48 MHz
  470. // input capture can be FTM1_CH0, CMP0 or CMP1 or USB start of frame
  471. // 24 MHz with reload 49152 to match Arduino's speed = 488.28125 Hz
  472. void analogWrite(uint8_t pin, int val)
  473. {
  474. uint32_t cval, max;
  475. #if defined(__MK20DX256__)
  476. if (pin == A14) {
  477. uint8_t res = analog_write_res;
  478. if (res < 12) {
  479. val <<= 12 - res;
  480. } else if (res > 12) {
  481. val >>= res - 12;
  482. }
  483. analogWriteDAC0(val);
  484. return;
  485. }
  486. #elif defined(__MKL26Z64__)
  487. if (pin == A12) {
  488. uint8_t res = analog_write_res;
  489. if (res < 12) {
  490. val <<= 12 - res;
  491. } else if (res > 12) {
  492. val >>= res - 12;
  493. }
  494. analogWriteDAC0(val);
  495. return;
  496. }
  497. #endif
  498. max = 1 << analog_write_res;
  499. if (val <= 0) {
  500. digitalWrite(pin, LOW);
  501. pinMode(pin, OUTPUT); // TODO: implement OUTPUT_LOW
  502. return;
  503. } else if (val >= max) {
  504. digitalWrite(pin, HIGH);
  505. pinMode(pin, OUTPUT); // TODO: implement OUTPUT_HIGH
  506. return;
  507. }
  508. //serial_print("analogWrite\n");
  509. //serial_print("val = ");
  510. //serial_phex32(val);
  511. //serial_print("\n");
  512. //serial_print("analog_write_res = ");
  513. //serial_phex(analog_write_res);
  514. //serial_print("\n");
  515. if (pin == FTM1_CH0_PIN || pin == FTM1_CH1_PIN) {
  516. cval = ((uint32_t)val * (uint32_t)(FTM1_MOD + 1)) >> analog_write_res;
  517. #if defined(FTM2_CH0_PIN)
  518. } else if (pin == FTM2_CH0_PIN || pin == FTM2_CH1_PIN) {
  519. cval = ((uint32_t)val * (uint32_t)(FTM2_MOD + 1)) >> analog_write_res;
  520. #endif
  521. } else {
  522. cval = ((uint32_t)val * (uint32_t)(FTM0_MOD + 1)) >> analog_write_res;
  523. }
  524. //serial_print("cval = ");
  525. //serial_phex32(cval);
  526. //serial_print("\n");
  527. switch (pin) {
  528. #ifdef FTM0_CH0_PIN
  529. case FTM0_CH0_PIN: // PTC1, FTM0_CH0
  530. FTM0_C0V = cval;
  531. FTM_PINCFG(FTM0_CH0_PIN) = PORT_PCR_MUX(4) | PORT_PCR_DSE | PORT_PCR_SRE;
  532. break;
  533. #endif
  534. #ifdef FTM0_CH1_PIN
  535. case FTM0_CH1_PIN: // PTC2, FTM0_CH1
  536. FTM0_C1V = cval;
  537. FTM_PINCFG(FTM0_CH1_PIN) = PORT_PCR_MUX(4) | PORT_PCR_DSE | PORT_PCR_SRE;
  538. break;
  539. #endif
  540. #ifdef FTM0_CH2_PIN
  541. case FTM0_CH2_PIN: // PTC3, FTM0_CH2
  542. FTM0_C2V = cval;
  543. FTM_PINCFG(FTM0_CH2_PIN) = PORT_PCR_MUX(4) | PORT_PCR_DSE | PORT_PCR_SRE;
  544. break;
  545. #endif
  546. #ifdef FTM0_CH3_PIN
  547. case FTM0_CH3_PIN: // PTC4, FTM0_CH3
  548. FTM0_C3V = cval;
  549. FTM_PINCFG(FTM0_CH3_PIN) = PORT_PCR_MUX(4) | PORT_PCR_DSE | PORT_PCR_SRE;
  550. break;
  551. #endif
  552. #ifdef FTM0_CH4_PIN
  553. case FTM0_CH4_PIN: // PTD4, FTM0_CH4
  554. FTM0_C4V = cval;
  555. FTM_PINCFG(FTM0_CH4_PIN) = PORT_PCR_MUX(4) | PORT_PCR_DSE | PORT_PCR_SRE;
  556. break;
  557. #endif
  558. #ifdef FTM0_CH5_PIN
  559. case FTM0_CH5_PIN: // PTD5, FTM0_CH5
  560. FTM0_C5V = cval;
  561. FTM_PINCFG(FTM0_CH5_PIN) = PORT_PCR_MUX(4) | PORT_PCR_DSE | PORT_PCR_SRE;
  562. break;
  563. #endif
  564. #ifdef FTM0_CH6_PIN
  565. case FTM0_CH6_PIN: // PTD6, FTM0_CH6
  566. FTM0_C6V = cval;
  567. FTM_PINCFG(FTM0_CH6_PIN) = PORT_PCR_MUX(4) | PORT_PCR_DSE | PORT_PCR_SRE;
  568. break;
  569. #endif
  570. #ifdef FTM0_CH7_PIN
  571. case FTM0_CH7_PIN: // PTD7, FTM0_CH7
  572. FTM0_C7V = cval;
  573. FTM_PINCFG(FTM0_CH7_PIN) = PORT_PCR_MUX(4) | PORT_PCR_DSE | PORT_PCR_SRE;
  574. break;
  575. #endif
  576. #ifdef FTM1_CH0_PIN
  577. case FTM1_CH0_PIN: // PTA12, FTM1_CH0
  578. FTM1_C0V = cval;
  579. FTM_PINCFG(FTM1_CH0_PIN) = PORT_PCR_MUX(3) | PORT_PCR_DSE | PORT_PCR_SRE;
  580. break;
  581. #endif
  582. #ifdef FTM1_CH1_PIN
  583. case FTM1_CH1_PIN: // PTA13, FTM1_CH1
  584. FTM1_C1V = cval;
  585. FTM_PINCFG(FTM1_CH1_PIN) = PORT_PCR_MUX(3) | PORT_PCR_DSE | PORT_PCR_SRE;
  586. break;
  587. #endif
  588. #ifdef FTM2_CH0_PIN
  589. case FTM2_CH0_PIN: // PTB18, FTM2_CH0
  590. FTM2_C0V = cval;
  591. FTM_PINCFG(FTM2_CH0_PIN) = PORT_PCR_MUX(3) | PORT_PCR_DSE | PORT_PCR_SRE;
  592. break;
  593. #endif
  594. #ifdef FTM2_CH1_PIN
  595. case FTM2_CH1_PIN: // PTB19, FTM1_CH1
  596. FTM2_C1V = cval;
  597. FTM_PINCFG(FTM2_CH1_PIN) = PORT_PCR_MUX(3) | PORT_PCR_DSE | PORT_PCR_SRE;
  598. break;
  599. #endif
  600. default:
  601. digitalWrite(pin, (val > 127) ? HIGH : LOW);
  602. pinMode(pin, OUTPUT);
  603. }
  604. }
  605. void analogWriteRes(uint32_t bits)
  606. {
  607. if (bits < 1) {
  608. bits = 1;
  609. } else if (bits > 16) {
  610. bits = 16;
  611. }
  612. analog_write_res = bits;
  613. }
  614. void analogWriteFrequency(uint8_t pin, uint32_t frequency)
  615. {
  616. uint32_t minfreq, prescale, mod;
  617. //serial_print("analogWriteFrequency: pin = ");
  618. //serial_phex(pin);
  619. //serial_print(", freq = ");
  620. //serial_phex32(frequency);
  621. //serial_print("\n");
  622. for (prescale = 0; prescale < 7; prescale++) {
  623. minfreq = (F_TIMER >> 16) >> prescale;
  624. if (frequency > minfreq) break;
  625. }
  626. //serial_print("F_TIMER = ");
  627. //serial_phex32(F_TIMER >> prescale);
  628. //serial_print("\n");
  629. //serial_print("prescale = ");
  630. //serial_phex(prescale);
  631. //serial_print("\n");
  632. //mod = ((F_TIMER >> prescale) / frequency) - 1;
  633. mod = (((F_TIMER >> prescale) + (frequency >> 1)) / frequency) - 1;
  634. if (mod > 65535) mod = 65535;
  635. //serial_print("mod = ");
  636. //serial_phex32(mod);
  637. //serial_print("\n");
  638. if (pin == FTM1_CH0_PIN || pin == FTM1_CH1_PIN) {
  639. FTM1_SC = 0;
  640. FTM1_CNT = 0;
  641. FTM1_MOD = mod;
  642. FTM1_SC = FTM_SC_CLKS(1) | FTM_SC_PS(prescale);
  643. } else if (pin == FTM0_CH0_PIN || pin == FTM0_CH1_PIN
  644. || pin == FTM0_CH2_PIN || pin == FTM0_CH3_PIN
  645. || pin == FTM0_CH4_PIN || pin == FTM0_CH5_PIN
  646. #ifdef FTM0_CH6_PIN
  647. || pin == FTM0_CH6_PIN || pin == FTM0_CH7_PIN
  648. #endif
  649. ) {
  650. FTM0_SC = 0;
  651. FTM0_CNT = 0;
  652. FTM0_MOD = mod;
  653. FTM0_SC = FTM_SC_CLKS(1) | FTM_SC_PS(prescale);
  654. }
  655. #ifdef FTM2_CH0_PIN
  656. else if (pin == FTM2_CH0_PIN || pin == FTM2_CH1_PIN) {
  657. FTM2_SC = 0;
  658. FTM2_CNT = 0;
  659. FTM2_MOD = mod;
  660. FTM2_SC = FTM_SC_CLKS(1) | FTM_SC_PS(prescale);
  661. }
  662. #endif
  663. }
  664. // TODO: startup code needs to initialize all pins to GPIO mode, input by default
  665. void digitalWrite(uint8_t pin, uint8_t val)
  666. {
  667. if (pin >= CORE_NUM_DIGITAL) return;
  668. #ifdef KINETISK
  669. if (*portModeRegister(pin)) {
  670. if (val) {
  671. *portSetRegister(pin) = 1;
  672. } else {
  673. *portClearRegister(pin) = 1;
  674. }
  675. #else
  676. if (*portModeRegister(pin) & digitalPinToBitMask(pin)) {
  677. if (val) {
  678. *portSetRegister(pin) = digitalPinToBitMask(pin);
  679. } else {
  680. *portClearRegister(pin) = digitalPinToBitMask(pin);
  681. }
  682. #endif
  683. } else {
  684. volatile uint32_t *config = portConfigRegister(pin);
  685. if (val) {
  686. // TODO use bitband for atomic read-mod-write
  687. *config |= (PORT_PCR_PE | PORT_PCR_PS);
  688. //*config = PORT_PCR_MUX(1) | PORT_PCR_PE | PORT_PCR_PS;
  689. } else {
  690. // TODO use bitband for atomic read-mod-write
  691. *config &= ~(PORT_PCR_PE);
  692. //*config = PORT_PCR_MUX(1);
  693. }
  694. }
  695. }
  696. uint8_t digitalRead(uint8_t pin)
  697. {
  698. if (pin >= CORE_NUM_DIGITAL) return 0;
  699. #ifdef KINETISK
  700. return *portInputRegister(pin);
  701. #else
  702. return (*portInputRegister(pin) & digitalPinToBitMask(pin)) ? 1 : 0;
  703. #endif
  704. }
  705. void pinMode(uint8_t pin, uint8_t mode)
  706. {
  707. volatile uint32_t *config;
  708. if (pin >= CORE_NUM_DIGITAL) return;
  709. config = portConfigRegister(pin);
  710. if (mode == OUTPUT) {
  711. #ifdef KINETISK
  712. *portModeRegister(pin) = 1;
  713. #else
  714. *portModeRegister(pin) |= digitalPinToBitMask(pin); // TODO: atomic
  715. #endif
  716. *config = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
  717. } else {
  718. #ifdef KINETISK
  719. *portModeRegister(pin) = 0;
  720. #else
  721. *portModeRegister(pin) &= ~digitalPinToBitMask(pin);
  722. #endif
  723. if (mode == INPUT) {
  724. *config = PORT_PCR_MUX(1);
  725. } else {
  726. *config = PORT_PCR_MUX(1) | PORT_PCR_PE | PORT_PCR_PS; // pullup
  727. }
  728. }
  729. }
  730. void _shiftOut(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder, uint8_t value)
  731. {
  732. if (bitOrder == LSBFIRST) {
  733. shiftOut_lsbFirst(dataPin, clockPin, value);
  734. } else {
  735. shiftOut_msbFirst(dataPin, clockPin, value);
  736. }
  737. }
  738. void shiftOut_lsbFirst(uint8_t dataPin, uint8_t clockPin, uint8_t value)
  739. {
  740. uint8_t mask;
  741. for (mask=0x01; mask; mask <<= 1) {
  742. digitalWrite(dataPin, value & mask);
  743. digitalWrite(clockPin, HIGH);
  744. digitalWrite(clockPin, LOW);
  745. }
  746. }
  747. void shiftOut_msbFirst(uint8_t dataPin, uint8_t clockPin, uint8_t value)
  748. {
  749. uint8_t mask;
  750. for (mask=0x80; mask; mask >>= 1) {
  751. digitalWrite(dataPin, value & mask);
  752. digitalWrite(clockPin, HIGH);
  753. digitalWrite(clockPin, LOW);
  754. }
  755. }
  756. uint8_t _shiftIn(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder)
  757. {
  758. if (bitOrder == LSBFIRST) {
  759. return shiftIn_lsbFirst(dataPin, clockPin);
  760. } else {
  761. return shiftIn_msbFirst(dataPin, clockPin);
  762. }
  763. }
  764. uint8_t shiftIn_lsbFirst(uint8_t dataPin, uint8_t clockPin)
  765. {
  766. uint8_t mask, value=0;
  767. for (mask=0x01; mask; mask <<= 1) {
  768. digitalWrite(clockPin, HIGH);
  769. if (digitalRead(dataPin)) value |= mask;
  770. digitalWrite(clockPin, LOW);
  771. }
  772. return value;
  773. }
  774. uint8_t shiftIn_msbFirst(uint8_t dataPin, uint8_t clockPin)
  775. {
  776. uint8_t mask, value=0;
  777. for (mask=0x80; mask; mask >>= 1) {
  778. digitalWrite(clockPin, HIGH);
  779. if (digitalRead(dataPin)) value |= mask;
  780. digitalWrite(clockPin, LOW);
  781. }
  782. return value;
  783. }
  784. // the systick interrupt is supposed to increment this at 1 kHz rate
  785. volatile uint32_t systick_millis_count = 0;
  786. //uint32_t systick_current, systick_count, systick_istatus; // testing only
  787. uint32_t micros(void)
  788. {
  789. uint32_t count, current, istatus;
  790. __disable_irq();
  791. current = SYST_CVR;
  792. count = systick_millis_count;
  793. istatus = SCB_ICSR; // bit 26 indicates if systick exception pending
  794. __enable_irq();
  795. //systick_current = current;
  796. //systick_count = count;
  797. //systick_istatus = istatus & SCB_ICSR_PENDSTSET ? 1 : 0;
  798. if ((istatus & SCB_ICSR_PENDSTSET) && current > 50) count++;
  799. current = ((F_CPU / 1000) - 1) - current;
  800. return count * 1000 + current / (F_CPU / 1000000);
  801. }
  802. void delay(uint32_t ms)
  803. {
  804. uint32_t start = micros();
  805. if (ms > 0) {
  806. while (1) {
  807. if ((micros() - start) >= 1000) {
  808. ms--;
  809. if (ms == 0) return;
  810. start += 1000;
  811. }
  812. yield();
  813. }
  814. }
  815. }
  816. // TODO: verify these result in correct timeouts...
  817. #if F_CPU == 168000000
  818. #define PULSEIN_LOOPS_PER_USEC 25
  819. #elif F_CPU == 144000000
  820. #define PULSEIN_LOOPS_PER_USEC 21
  821. #elif F_CPU == 120000000
  822. #define PULSEIN_LOOPS_PER_USEC 18
  823. #elif F_CPU == 96000000
  824. #define PULSEIN_LOOPS_PER_USEC 14
  825. #elif F_CPU == 72000000
  826. #define PULSEIN_LOOPS_PER_USEC 10
  827. #elif F_CPU == 48000000
  828. #define PULSEIN_LOOPS_PER_USEC 7
  829. #elif F_CPU == 24000000
  830. #define PULSEIN_LOOPS_PER_USEC 4
  831. #elif F_CPU == 16000000
  832. #define PULSEIN_LOOPS_PER_USEC 1
  833. #elif F_CPU == 8000000
  834. #define PULSEIN_LOOPS_PER_USEC 1
  835. #elif F_CPU == 4000000
  836. #define PULSEIN_LOOPS_PER_USEC 1
  837. #elif F_CPU == 2000000
  838. #define PULSEIN_LOOPS_PER_USEC 1
  839. #endif
  840. uint32_t pulseIn_high(volatile uint8_t *reg, uint32_t timeout)
  841. {
  842. uint32_t timeout_count = timeout * PULSEIN_LOOPS_PER_USEC;
  843. uint32_t usec_start, usec_stop;
  844. // wait for any previous pulse to end
  845. while (*reg) {
  846. if (--timeout_count == 0) return 0;
  847. }
  848. // wait for the pulse to start
  849. while (!*reg) {
  850. if (--timeout_count == 0) return 0;
  851. }
  852. usec_start = micros();
  853. // wait for the pulse to stop
  854. while (*reg) {
  855. if (--timeout_count == 0) return 0;
  856. }
  857. usec_stop = micros();
  858. return usec_stop - usec_start;
  859. }
  860. uint32_t pulseIn_low(volatile uint8_t *reg, uint32_t timeout)
  861. {
  862. uint32_t timeout_count = timeout * PULSEIN_LOOPS_PER_USEC;
  863. uint32_t usec_start, usec_stop;
  864. // wait for any previous pulse to end
  865. while (!*reg) {
  866. if (--timeout_count == 0) return 0;
  867. }
  868. // wait for the pulse to start
  869. while (*reg) {
  870. if (--timeout_count == 0) return 0;
  871. }
  872. usec_start = micros();
  873. // wait for the pulse to stop
  874. while (!*reg) {
  875. if (--timeout_count == 0) return 0;
  876. }
  877. usec_stop = micros();
  878. return usec_stop - usec_start;
  879. }
  880. // TODO: an inline version should handle the common case where state is const
  881. uint32_t pulseIn(uint8_t pin, uint8_t state, uint32_t timeout)
  882. {
  883. if (pin >= CORE_NUM_DIGITAL) return 0;
  884. if (state) return pulseIn_high(portInputRegister(pin), timeout);
  885. return pulseIn_low(portInputRegister(pin), timeout);;
  886. }