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.

преди 11 години
преди 10 години
преди 11 години
преди 10 години
преди 10 години
преди 11 години
преди 10 години
преди 11 години
преди 11 години
преди 11 години
преди 10 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 10 години
преди 11 години
преди 10 години
преди 11 години
преди 10 години
преди 11 години
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807
  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 0
  34. // moved to pins_arduino.h
  35. struct digital_pin_bitband_and_config_table_struct {
  36. volatile uint32_t *reg;
  37. volatile uint32_t *config;
  38. };
  39. const struct digital_pin_bitband_and_config_table_struct digital_pin_to_info_PGM[];
  40. // compatibility macros
  41. #define digitalPinToPort(pin) (pin)
  42. #define digitalPinToBitMask(pin) (1)
  43. #define portOutputRegister(pin) ((volatile uint8_t *)(digital_pin_to_info_PGM[(pin)].reg + 0))
  44. #define portSetRegister(pin) ((volatile uint8_t *)(digital_pin_to_info_PGM[(pin)].reg + 32))
  45. #define portClearRegister(pin) ((volatile uint8_t *)(digital_pin_to_info_PGM[(pin)].reg + 64))
  46. #define portToggleRegister(pin) ((volatile uint8_t *)(digital_pin_to_info_PGM[(pin)].reg + 96))
  47. #define portInputRegister(pin) ((volatile uint8_t *)(digital_pin_to_info_PGM[(pin)].reg + 128))
  48. #define portModeRegister(pin) ((volatile uint8_t *)(digital_pin_to_info_PGM[(pin)].reg + 160))
  49. #define portConfigRegister(pin) ((volatile uint32_t *)(digital_pin_to_info_PGM[(pin)].config))
  50. #endif
  51. //#define digitalPinToTimer(P) ( pgm_read_byte( digital_pin_to_timer_PGM + (P) ) )
  52. //#define analogInPinToBit(P) (P)
  53. #define GPIO_BITBAND_ADDR(reg, bit) (((uint32_t)&(reg) - 0x40000000) * 32 + (bit) * 4 + 0x42000000)
  54. #define GPIO_BITBAND_PTR(reg, bit) ((uint32_t *)GPIO_BITBAND_ADDR((reg), (bit)))
  55. //#define GPIO_SET_BIT(reg, bit) (*GPIO_BITBAND_PTR((reg), (bit)) = 1)
  56. //#define GPIO_CLR_BIT(reg, bit) (*GPIO_BITBAND_PTR((reg), (bit)) = 0)
  57. const struct digital_pin_bitband_and_config_table_struct digital_pin_to_info_PGM[] = {
  58. {GPIO_BITBAND_PTR(CORE_PIN0_PORTREG, CORE_PIN0_BIT), &CORE_PIN0_CONFIG},
  59. {GPIO_BITBAND_PTR(CORE_PIN1_PORTREG, CORE_PIN1_BIT), &CORE_PIN1_CONFIG},
  60. {GPIO_BITBAND_PTR(CORE_PIN2_PORTREG, CORE_PIN2_BIT), &CORE_PIN2_CONFIG},
  61. {GPIO_BITBAND_PTR(CORE_PIN3_PORTREG, CORE_PIN3_BIT), &CORE_PIN3_CONFIG},
  62. {GPIO_BITBAND_PTR(CORE_PIN4_PORTREG, CORE_PIN4_BIT), &CORE_PIN4_CONFIG},
  63. {GPIO_BITBAND_PTR(CORE_PIN5_PORTREG, CORE_PIN5_BIT), &CORE_PIN5_CONFIG},
  64. {GPIO_BITBAND_PTR(CORE_PIN6_PORTREG, CORE_PIN6_BIT), &CORE_PIN6_CONFIG},
  65. {GPIO_BITBAND_PTR(CORE_PIN7_PORTREG, CORE_PIN7_BIT), &CORE_PIN7_CONFIG},
  66. {GPIO_BITBAND_PTR(CORE_PIN8_PORTREG, CORE_PIN8_BIT), &CORE_PIN8_CONFIG},
  67. {GPIO_BITBAND_PTR(CORE_PIN9_PORTREG, CORE_PIN9_BIT), &CORE_PIN9_CONFIG},
  68. {GPIO_BITBAND_PTR(CORE_PIN10_PORTREG, CORE_PIN10_BIT), &CORE_PIN10_CONFIG},
  69. {GPIO_BITBAND_PTR(CORE_PIN11_PORTREG, CORE_PIN11_BIT), &CORE_PIN11_CONFIG},
  70. {GPIO_BITBAND_PTR(CORE_PIN12_PORTREG, CORE_PIN12_BIT), &CORE_PIN12_CONFIG},
  71. {GPIO_BITBAND_PTR(CORE_PIN13_PORTREG, CORE_PIN13_BIT), &CORE_PIN13_CONFIG},
  72. {GPIO_BITBAND_PTR(CORE_PIN14_PORTREG, CORE_PIN14_BIT), &CORE_PIN14_CONFIG},
  73. {GPIO_BITBAND_PTR(CORE_PIN15_PORTREG, CORE_PIN15_BIT), &CORE_PIN15_CONFIG},
  74. {GPIO_BITBAND_PTR(CORE_PIN16_PORTREG, CORE_PIN16_BIT), &CORE_PIN16_CONFIG},
  75. {GPIO_BITBAND_PTR(CORE_PIN17_PORTREG, CORE_PIN17_BIT), &CORE_PIN17_CONFIG},
  76. {GPIO_BITBAND_PTR(CORE_PIN18_PORTREG, CORE_PIN18_BIT), &CORE_PIN18_CONFIG},
  77. {GPIO_BITBAND_PTR(CORE_PIN19_PORTREG, CORE_PIN19_BIT), &CORE_PIN19_CONFIG},
  78. {GPIO_BITBAND_PTR(CORE_PIN20_PORTREG, CORE_PIN20_BIT), &CORE_PIN20_CONFIG},
  79. {GPIO_BITBAND_PTR(CORE_PIN21_PORTREG, CORE_PIN21_BIT), &CORE_PIN21_CONFIG},
  80. {GPIO_BITBAND_PTR(CORE_PIN22_PORTREG, CORE_PIN22_BIT), &CORE_PIN22_CONFIG},
  81. {GPIO_BITBAND_PTR(CORE_PIN23_PORTREG, CORE_PIN23_BIT), &CORE_PIN23_CONFIG},
  82. {GPIO_BITBAND_PTR(CORE_PIN24_PORTREG, CORE_PIN24_BIT), &CORE_PIN24_CONFIG},
  83. {GPIO_BITBAND_PTR(CORE_PIN25_PORTREG, CORE_PIN25_BIT), &CORE_PIN25_CONFIG},
  84. {GPIO_BITBAND_PTR(CORE_PIN26_PORTREG, CORE_PIN26_BIT), &CORE_PIN26_CONFIG},
  85. {GPIO_BITBAND_PTR(CORE_PIN27_PORTREG, CORE_PIN27_BIT), &CORE_PIN27_CONFIG},
  86. {GPIO_BITBAND_PTR(CORE_PIN28_PORTREG, CORE_PIN28_BIT), &CORE_PIN28_CONFIG},
  87. {GPIO_BITBAND_PTR(CORE_PIN29_PORTREG, CORE_PIN29_BIT), &CORE_PIN29_CONFIG},
  88. {GPIO_BITBAND_PTR(CORE_PIN30_PORTREG, CORE_PIN30_BIT), &CORE_PIN30_CONFIG},
  89. {GPIO_BITBAND_PTR(CORE_PIN31_PORTREG, CORE_PIN31_BIT), &CORE_PIN31_CONFIG},
  90. {GPIO_BITBAND_PTR(CORE_PIN32_PORTREG, CORE_PIN32_BIT), &CORE_PIN32_CONFIG},
  91. {GPIO_BITBAND_PTR(CORE_PIN33_PORTREG, CORE_PIN33_BIT), &CORE_PIN33_CONFIG}
  92. };
  93. typedef void (*voidFuncPtr)(void);
  94. volatile static voidFuncPtr intFunc[CORE_NUM_DIGITAL];
  95. void init_pin_interrupts(void)
  96. {
  97. //SIM_SCGC5 = 0x00043F82; // clocks active to all GPIO
  98. NVIC_ENABLE_IRQ(IRQ_PORTA);
  99. NVIC_ENABLE_IRQ(IRQ_PORTB);
  100. NVIC_ENABLE_IRQ(IRQ_PORTC);
  101. NVIC_ENABLE_IRQ(IRQ_PORTD);
  102. NVIC_ENABLE_IRQ(IRQ_PORTE);
  103. // TODO: maybe these should be set to a lower priority
  104. // so if the user puts lots of slow code on attachInterrupt
  105. // fast interrupts will still be serviced quickly?
  106. }
  107. void attachInterrupt(uint8_t pin, void (*function)(void), int mode)
  108. {
  109. volatile uint32_t *config;
  110. uint32_t cfg, mask;
  111. if (pin >= CORE_NUM_DIGITAL) return;
  112. switch (mode) {
  113. case CHANGE: mask = 0x0B; break;
  114. case RISING: mask = 0x09; break;
  115. case FALLING: mask = 0x0A; break;
  116. case LOW: mask = 0x08; break;
  117. case HIGH: mask = 0x0C; break;
  118. default: return;
  119. }
  120. mask = (mask << 16) | 0x01000000;
  121. config = portConfigRegister(pin);
  122. __disable_irq();
  123. cfg = *config;
  124. cfg &= ~0x000F0000; // disable any previous interrupt
  125. *config = cfg;
  126. intFunc[pin] = function; // set the function pointer
  127. cfg |= mask;
  128. *config = cfg; // enable the new interrupt
  129. __enable_irq();
  130. }
  131. void detachInterrupt(uint8_t pin)
  132. {
  133. volatile uint32_t *config;
  134. config = portConfigRegister(pin);
  135. __disable_irq();
  136. *config = ((*config & ~0x000F0000) | 0x01000000);
  137. intFunc[pin] = NULL;
  138. __enable_irq();
  139. }
  140. void porta_isr(void)
  141. {
  142. uint32_t isfr = PORTA_ISFR;
  143. PORTA_ISFR = isfr;
  144. if ((isfr & CORE_PIN3_BITMASK) && intFunc[3]) intFunc[3]();
  145. if ((isfr & CORE_PIN4_BITMASK) && intFunc[4]) intFunc[4]();
  146. if ((isfr & CORE_PIN24_BITMASK) && intFunc[24]) intFunc[24]();
  147. if ((isfr & CORE_PIN33_BITMASK) && intFunc[33]) intFunc[33]();
  148. }
  149. void portb_isr(void)
  150. {
  151. uint32_t isfr = PORTB_ISFR;
  152. PORTB_ISFR = isfr;
  153. if ((isfr & CORE_PIN0_BITMASK) && intFunc[0]) intFunc[0]();
  154. if ((isfr & CORE_PIN1_BITMASK) && intFunc[1]) intFunc[1]();
  155. if ((isfr & CORE_PIN16_BITMASK) && intFunc[16]) intFunc[16]();
  156. if ((isfr & CORE_PIN17_BITMASK) && intFunc[17]) intFunc[17]();
  157. if ((isfr & CORE_PIN18_BITMASK) && intFunc[18]) intFunc[18]();
  158. if ((isfr & CORE_PIN19_BITMASK) && intFunc[19]) intFunc[19]();
  159. if ((isfr & CORE_PIN25_BITMASK) && intFunc[25]) intFunc[25]();
  160. if ((isfr & CORE_PIN32_BITMASK) && intFunc[32]) intFunc[32]();
  161. }
  162. void portc_isr(void)
  163. {
  164. // TODO: these are inefficent. Use CLZ somehow....
  165. uint32_t isfr = PORTC_ISFR;
  166. PORTC_ISFR = isfr;
  167. if ((isfr & CORE_PIN9_BITMASK) && intFunc[9]) intFunc[9]();
  168. if ((isfr & CORE_PIN10_BITMASK) && intFunc[10]) intFunc[10]();
  169. if ((isfr & CORE_PIN11_BITMASK) && intFunc[11]) intFunc[11]();
  170. if ((isfr & CORE_PIN12_BITMASK) && intFunc[12]) intFunc[12]();
  171. if ((isfr & CORE_PIN13_BITMASK) && intFunc[13]) intFunc[13]();
  172. if ((isfr & CORE_PIN15_BITMASK) && intFunc[15]) intFunc[15]();
  173. if ((isfr & CORE_PIN22_BITMASK) && intFunc[22]) intFunc[22]();
  174. if ((isfr & CORE_PIN23_BITMASK) && intFunc[23]) intFunc[23]();
  175. if ((isfr & CORE_PIN27_BITMASK) && intFunc[27]) intFunc[27]();
  176. if ((isfr & CORE_PIN28_BITMASK) && intFunc[28]) intFunc[28]();
  177. if ((isfr & CORE_PIN29_BITMASK) && intFunc[29]) intFunc[29]();
  178. if ((isfr & CORE_PIN30_BITMASK) && intFunc[30]) intFunc[30]();
  179. }
  180. void portd_isr(void)
  181. {
  182. uint32_t isfr = PORTD_ISFR;
  183. PORTD_ISFR = isfr;
  184. if ((isfr & CORE_PIN2_BITMASK) && intFunc[2]) intFunc[2]();
  185. if ((isfr & CORE_PIN5_BITMASK) && intFunc[5]) intFunc[5]();
  186. if ((isfr & CORE_PIN6_BITMASK) && intFunc[6]) intFunc[6]();
  187. if ((isfr & CORE_PIN7_BITMASK) && intFunc[7]) intFunc[7]();
  188. if ((isfr & CORE_PIN8_BITMASK) && intFunc[8]) intFunc[8]();
  189. if ((isfr & CORE_PIN14_BITMASK) && intFunc[14]) intFunc[14]();
  190. if ((isfr & CORE_PIN20_BITMASK) && intFunc[20]) intFunc[20]();
  191. if ((isfr & CORE_PIN21_BITMASK) && intFunc[21]) intFunc[21]();
  192. }
  193. void porte_isr(void)
  194. {
  195. uint32_t isfr = PORTE_ISFR;
  196. PORTE_ISFR = isfr;
  197. if ((isfr & CORE_PIN26_BITMASK) && intFunc[26]) intFunc[26]();
  198. if ((isfr & CORE_PIN31_BITMASK) && intFunc[31]) intFunc[31]();
  199. }
  200. unsigned long rtc_get(void)
  201. {
  202. return RTC_TSR;
  203. }
  204. void rtc_set(unsigned long t)
  205. {
  206. RTC_SR = 0;
  207. RTC_TPR = 0;
  208. RTC_TSR = t;
  209. RTC_SR = RTC_SR_TCE;
  210. }
  211. // adjust is the amount of crystal error to compensate, 1 = 0.1192 ppm
  212. // For example, adjust = -100 is slows the clock by 11.92 ppm
  213. //
  214. void rtc_compensate(int adjust)
  215. {
  216. uint32_t comp, interval, tcr;
  217. // This simple approach tries to maximize the interval.
  218. // Perhaps minimizing TCR would be better, so the
  219. // compensation is distributed more evenly across
  220. // many seconds, rather than saving it all up and then
  221. // altering one second up to +/- 0.38%
  222. if (adjust >= 0) {
  223. comp = adjust;
  224. interval = 256;
  225. while (1) {
  226. tcr = comp * interval;
  227. if (tcr < 128*256) break;
  228. if (--interval == 1) break;
  229. }
  230. tcr = tcr >> 8;
  231. } else {
  232. comp = -adjust;
  233. interval = 256;
  234. while (1) {
  235. tcr = comp * interval;
  236. if (tcr < 129*256) break;
  237. if (--interval == 1) break;
  238. }
  239. tcr = tcr >> 8;
  240. tcr = 256 - tcr;
  241. }
  242. RTC_TCR = ((interval - 1) << 8) | tcr;
  243. }
  244. #if 0
  245. // TODO: build system should define this
  246. // so RTC is automatically initialized to approx correct time
  247. // at least when the program begins running right after upload
  248. #ifndef TIME_T
  249. #define TIME_T 1350160272
  250. #endif
  251. void init_rtc(void)
  252. {
  253. serial_print("init_rtc\n");
  254. //SIM_SCGC6 |= SIM_SCGC6_RTC;
  255. // enable the RTC crystal oscillator, for approx 12pf crystal
  256. if (!(RTC_CR & RTC_CR_OSCE)) {
  257. serial_print("start RTC oscillator\n");
  258. RTC_SR = 0;
  259. RTC_CR = RTC_CR_SC16P | RTC_CR_SC4P | RTC_CR_OSCE;
  260. }
  261. // should wait for crystal to stabilize.....
  262. serial_print("SR=");
  263. serial_phex32(RTC_SR);
  264. serial_print("\n");
  265. serial_print("CR=");
  266. serial_phex32(RTC_CR);
  267. serial_print("\n");
  268. serial_print("TSR=");
  269. serial_phex32(RTC_TSR);
  270. serial_print("\n");
  271. serial_print("TCR=");
  272. serial_phex32(RTC_TCR);
  273. serial_print("\n");
  274. if (RTC_SR & RTC_SR_TIF) {
  275. // enable the RTC
  276. RTC_SR = 0;
  277. RTC_TPR = 0;
  278. RTC_TSR = TIME_T;
  279. RTC_SR = RTC_SR_TCE;
  280. }
  281. }
  282. #endif
  283. extern void usb_init(void);
  284. // create a default PWM at the same 488.28 Hz as Arduino Uno
  285. #if F_BUS == 60000000
  286. #define DEFAULT_FTM_MOD (61440 - 1)
  287. #define DEFAULT_FTM_PRESCALE 1
  288. #elif F_BUS == 56000000
  289. #define DEFAULT_FTM_MOD (57344 - 1)
  290. #define DEFAULT_FTM_PRESCALE 1
  291. #elif F_BUS == 48000000
  292. #define DEFAULT_FTM_MOD (49152 - 1)
  293. #define DEFAULT_FTM_PRESCALE 1
  294. #elif F_BUS == 40000000
  295. #define DEFAULT_FTM_MOD (40960 - 1)
  296. #define DEFAULT_FTM_PRESCALE 1
  297. #elif F_BUS == 36000000
  298. #define DEFAULT_FTM_MOD (36864 - 1)
  299. #define DEFAULT_FTM_PRESCALE 1
  300. #elif F_BUS == 24000000
  301. #define DEFAULT_FTM_MOD (49152 - 1)
  302. #define DEFAULT_FTM_PRESCALE 0
  303. #elif F_BUS == 4000000
  304. #define DEFAULT_FTM_MOD (8192 - 1)
  305. #define DEFAULT_FTM_PRESCALE 0
  306. #elif F_BUS == 2000000
  307. #define DEFAULT_FTM_MOD (4096 - 1)
  308. #define DEFAULT_FTM_PRESCALE 0
  309. #endif
  310. //void init_pins(void)
  311. void _init_Teensyduino_internal_(void)
  312. {
  313. init_pin_interrupts();
  314. //SIM_SCGC6 |= SIM_SCGC6_FTM0; // TODO: use bitband for atomic read-mod-write
  315. //SIM_SCGC6 |= SIM_SCGC6_FTM1;
  316. FTM0_CNT = 0;
  317. FTM0_MOD = DEFAULT_FTM_MOD;
  318. FTM0_C0SC = 0x28; // MSnB:MSnA = 10, ELSnB:ELSnA = 10
  319. FTM0_C1SC = 0x28;
  320. FTM0_C2SC = 0x28;
  321. FTM0_C3SC = 0x28;
  322. FTM0_C4SC = 0x28;
  323. FTM0_C5SC = 0x28;
  324. FTM0_C6SC = 0x28;
  325. FTM0_C7SC = 0x28;
  326. FTM0_SC = FTM_SC_CLKS(1) | FTM_SC_PS(DEFAULT_FTM_PRESCALE);
  327. FTM1_CNT = 0;
  328. FTM1_MOD = DEFAULT_FTM_MOD;
  329. FTM1_C0SC = 0x28;
  330. FTM1_C1SC = 0x28;
  331. FTM1_SC = FTM_SC_CLKS(1) | FTM_SC_PS(DEFAULT_FTM_PRESCALE);
  332. #if defined(__MK20DX256__)
  333. FTM2_CNT = 0;
  334. FTM2_MOD = DEFAULT_FTM_MOD;
  335. FTM2_C0SC = 0x28;
  336. FTM2_C1SC = 0x28;
  337. FTM2_SC = FTM_SC_CLKS(1) | FTM_SC_PS(DEFAULT_FTM_PRESCALE);
  338. #endif
  339. analog_init();
  340. //delay(100); // TODO: this is not necessary, right?
  341. delay(4);
  342. usb_init();
  343. }
  344. static uint8_t analog_write_res = 8;
  345. // SOPT4 is SIM select clocks?
  346. // FTM is clocked by the bus clock, either 24 or 48 MHz
  347. // input capture can be FTM1_CH0, CMP0 or CMP1 or USB start of frame
  348. // 24 MHz with reload 49152 to match Arduino's speed = 488.28125 Hz
  349. void analogWrite(uint8_t pin, int val)
  350. {
  351. uint32_t cval, max;
  352. #if defined(__MK20DX256__)
  353. if (pin == A14) {
  354. uint8_t res = analog_write_res;
  355. if (res < 12) {
  356. val <<= 12 - res;
  357. } else if (res > 12) {
  358. val >>= res - 12;
  359. }
  360. analogWriteDAC0(val);
  361. return;
  362. }
  363. #endif
  364. max = 1 << analog_write_res;
  365. if (val <= 0) {
  366. digitalWrite(pin, LOW);
  367. pinMode(pin, OUTPUT); // TODO: implement OUTPUT_LOW
  368. return;
  369. } else if (val >= max) {
  370. digitalWrite(pin, HIGH);
  371. pinMode(pin, OUTPUT); // TODO: implement OUTPUT_HIGH
  372. return;
  373. }
  374. //serial_print("analogWrite\n");
  375. //serial_print("val = ");
  376. //serial_phex32(val);
  377. //serial_print("\n");
  378. //serial_print("analog_write_res = ");
  379. //serial_phex(analog_write_res);
  380. //serial_print("\n");
  381. if (pin == 3 || pin == 4) {
  382. cval = ((uint32_t)val * (uint32_t)(FTM1_MOD + 1)) >> analog_write_res;
  383. #if defined(__MK20DX256__)
  384. } else if (pin == 25 || pin == 32) {
  385. cval = ((uint32_t)val * (uint32_t)(FTM2_MOD + 1)) >> analog_write_res;
  386. #endif
  387. } else {
  388. cval = ((uint32_t)val * (uint32_t)(FTM0_MOD + 1)) >> analog_write_res;
  389. }
  390. //serial_print("cval = ");
  391. //serial_phex32(cval);
  392. //serial_print("\n");
  393. switch (pin) {
  394. case 3: // PTA12, FTM1_CH0
  395. FTM1_C0V = cval;
  396. CORE_PIN3_CONFIG = PORT_PCR_MUX(3) | PORT_PCR_DSE | PORT_PCR_SRE;
  397. break;
  398. case 4: // PTA13, FTM1_CH1
  399. FTM1_C1V = cval;
  400. CORE_PIN4_CONFIG = PORT_PCR_MUX(3) | PORT_PCR_DSE | PORT_PCR_SRE;
  401. break;
  402. case 5: // PTD7, FTM0_CH7
  403. FTM0_C7V = cval;
  404. CORE_PIN5_CONFIG = PORT_PCR_MUX(4) | PORT_PCR_DSE | PORT_PCR_SRE;
  405. break;
  406. case 6: // PTD4, FTM0_CH4
  407. FTM0_C4V = cval;
  408. CORE_PIN6_CONFIG = PORT_PCR_MUX(4) | PORT_PCR_DSE | PORT_PCR_SRE;
  409. break;
  410. case 9: // PTC3, FTM0_CH2
  411. FTM0_C2V = cval;
  412. CORE_PIN9_CONFIG = PORT_PCR_MUX(4) | PORT_PCR_DSE | PORT_PCR_SRE;
  413. break;
  414. case 10: // PTC4, FTM0_CH3
  415. FTM0_C3V = cval;
  416. CORE_PIN10_CONFIG = PORT_PCR_MUX(4) | PORT_PCR_DSE | PORT_PCR_SRE;
  417. break;
  418. case 20: // PTD5, FTM0_CH5
  419. FTM0_C5V = cval;
  420. CORE_PIN20_CONFIG = PORT_PCR_MUX(4) | PORT_PCR_DSE | PORT_PCR_SRE;
  421. break;
  422. case 21: // PTD6, FTM0_CH6
  423. FTM0_C6V = cval;
  424. CORE_PIN21_CONFIG = PORT_PCR_MUX(4) | PORT_PCR_DSE | PORT_PCR_SRE;
  425. break;
  426. case 22: // PTC1, FTM0_CH0
  427. FTM0_C0V = cval;
  428. CORE_PIN22_CONFIG = PORT_PCR_MUX(4) | PORT_PCR_DSE | PORT_PCR_SRE;
  429. break;
  430. case 23: // PTC2, FTM0_CH1
  431. FTM0_C1V = cval;
  432. CORE_PIN23_CONFIG = PORT_PCR_MUX(4) | PORT_PCR_DSE | PORT_PCR_SRE;
  433. break;
  434. #if defined(__MK20DX256__)
  435. case 32: // PTB18, FTM2_CH0
  436. FTM2_C0V = cval;
  437. CORE_PIN32_CONFIG = PORT_PCR_MUX(3) | PORT_PCR_DSE | PORT_PCR_SRE;
  438. break;
  439. case 25: // PTB19, FTM1_CH1
  440. FTM2_C1V = cval;
  441. CORE_PIN25_CONFIG = PORT_PCR_MUX(3) | PORT_PCR_DSE | PORT_PCR_SRE;
  442. break;
  443. #endif
  444. default:
  445. digitalWrite(pin, (val > 127) ? HIGH : LOW);
  446. pinMode(pin, OUTPUT);
  447. }
  448. }
  449. void analogWriteRes(uint32_t bits)
  450. {
  451. if (bits < 1) {
  452. bits = 1;
  453. } else if (bits > 16) {
  454. bits = 16;
  455. }
  456. analog_write_res = bits;
  457. }
  458. void analogWriteFrequency(uint8_t pin, uint32_t frequency)
  459. {
  460. uint32_t minfreq, prescale, mod;
  461. //serial_print("analogWriteFrequency: pin = ");
  462. //serial_phex(pin);
  463. //serial_print(", freq = ");
  464. //serial_phex32(frequency);
  465. //serial_print("\n");
  466. for (prescale = 0; prescale < 7; prescale++) {
  467. minfreq = (F_BUS >> 16) >> prescale;
  468. if (frequency > minfreq) break;
  469. }
  470. //serial_print("F_BUS = ");
  471. //serial_phex32(F_BUS >> prescale);
  472. //serial_print("\n");
  473. //serial_print("prescale = ");
  474. //serial_phex(prescale);
  475. //serial_print("\n");
  476. //mod = ((F_BUS >> prescale) / frequency) - 1;
  477. mod = (((F_BUS >> prescale) + (frequency >> 1)) / frequency) - 1;
  478. if (mod > 65535) mod = 65535;
  479. //serial_print("mod = ");
  480. //serial_phex32(mod);
  481. //serial_print("\n");
  482. if (pin == 3 || pin == 4) {
  483. FTM1_SC = 0;
  484. FTM1_CNT = 0;
  485. FTM1_MOD = mod;
  486. FTM1_SC = FTM_SC_CLKS(1) | FTM_SC_PS(prescale);
  487. } else if (pin == 5 || pin == 6 || pin == 9 || pin == 10 ||
  488. (pin >= 20 && pin <= 23)) {
  489. FTM0_SC = 0;
  490. FTM0_CNT = 0;
  491. FTM0_MOD = mod;
  492. FTM0_SC = FTM_SC_CLKS(1) | FTM_SC_PS(prescale);
  493. }
  494. }
  495. // TODO: startup code needs to initialize all pins to GPIO mode, input by default
  496. void digitalWrite(uint8_t pin, uint8_t val)
  497. {
  498. if (pin >= CORE_NUM_DIGITAL) return;
  499. if (*portModeRegister(pin)) {
  500. if (val) {
  501. *portSetRegister(pin) = 1;
  502. } else {
  503. *portClearRegister(pin) = 1;
  504. }
  505. } else {
  506. volatile uint32_t *config = portConfigRegister(pin);
  507. if (val) {
  508. // TODO use bitband for atomic read-mod-write
  509. *config |= (PORT_PCR_PE | PORT_PCR_PS);
  510. //*config = PORT_PCR_MUX(1) | PORT_PCR_PE | PORT_PCR_PS;
  511. } else {
  512. // TODO use bitband for atomic read-mod-write
  513. *config &= ~(PORT_PCR_PE);
  514. //*config = PORT_PCR_MUX(1);
  515. }
  516. }
  517. }
  518. uint8_t digitalRead(uint8_t pin)
  519. {
  520. if (pin >= CORE_NUM_DIGITAL) return 0;
  521. return *portInputRegister(pin);
  522. }
  523. void pinMode(uint8_t pin, uint8_t mode)
  524. {
  525. volatile uint32_t *config;
  526. if (pin >= CORE_NUM_DIGITAL) return;
  527. config = portConfigRegister(pin);
  528. if (mode == OUTPUT) {
  529. *portModeRegister(pin) = 1;
  530. *config = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
  531. } else {
  532. *portModeRegister(pin) = 0;
  533. if (mode == INPUT) {
  534. *config = PORT_PCR_MUX(1);
  535. } else {
  536. *config = PORT_PCR_MUX(1) | PORT_PCR_PE | PORT_PCR_PS; // pullup
  537. }
  538. }
  539. }
  540. void _shiftOut(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder, uint8_t value)
  541. {
  542. if (bitOrder == LSBFIRST) {
  543. shiftOut_lsbFirst(dataPin, clockPin, value);
  544. } else {
  545. shiftOut_msbFirst(dataPin, clockPin, value);
  546. }
  547. }
  548. void shiftOut_lsbFirst(uint8_t dataPin, uint8_t clockPin, uint8_t value)
  549. {
  550. uint8_t mask;
  551. for (mask=0x01; mask; mask <<= 1) {
  552. digitalWrite(dataPin, value & mask);
  553. digitalWrite(clockPin, HIGH);
  554. digitalWrite(clockPin, LOW);
  555. }
  556. }
  557. void shiftOut_msbFirst(uint8_t dataPin, uint8_t clockPin, uint8_t value)
  558. {
  559. uint8_t mask;
  560. for (mask=0x80; mask; mask >>= 1) {
  561. digitalWrite(dataPin, value & mask);
  562. digitalWrite(clockPin, HIGH);
  563. digitalWrite(clockPin, LOW);
  564. }
  565. }
  566. uint8_t _shiftIn(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder)
  567. {
  568. if (bitOrder == LSBFIRST) {
  569. return shiftIn_lsbFirst(dataPin, clockPin);
  570. } else {
  571. return shiftIn_msbFirst(dataPin, clockPin);
  572. }
  573. }
  574. uint8_t shiftIn_lsbFirst(uint8_t dataPin, uint8_t clockPin)
  575. {
  576. uint8_t mask, value=0;
  577. for (mask=0x01; mask; mask <<= 1) {
  578. digitalWrite(clockPin, HIGH);
  579. if (digitalRead(dataPin)) value |= mask;
  580. digitalWrite(clockPin, LOW);
  581. }
  582. return value;
  583. }
  584. uint8_t shiftIn_msbFirst(uint8_t dataPin, uint8_t clockPin)
  585. {
  586. uint8_t mask, value=0;
  587. for (mask=0x80; mask; mask >>= 1) {
  588. digitalWrite(clockPin, HIGH);
  589. if (digitalRead(dataPin)) value |= mask;
  590. digitalWrite(clockPin, LOW);
  591. }
  592. return value;
  593. }
  594. // the systick interrupt is supposed to increment this at 1 kHz rate
  595. volatile uint32_t systick_millis_count = 0;
  596. //uint32_t systick_current, systick_count, systick_istatus; // testing only
  597. uint32_t micros(void)
  598. {
  599. uint32_t count, current, istatus;
  600. __disable_irq();
  601. current = SYST_CVR;
  602. count = systick_millis_count;
  603. istatus = SCB_ICSR; // bit 26 indicates if systick exception pending
  604. __enable_irq();
  605. //systick_current = current;
  606. //systick_count = count;
  607. //systick_istatus = istatus & SCB_ICSR_PENDSTSET ? 1 : 0;
  608. if ((istatus & SCB_ICSR_PENDSTSET) && current > 50) count++;
  609. current = ((F_CPU / 1000) - 1) - current;
  610. return count * 1000 + current / (F_CPU / 1000000);
  611. }
  612. void delay(uint32_t ms)
  613. {
  614. uint32_t start = micros();
  615. if (ms > 0) {
  616. while (1) {
  617. if ((micros() - start) >= 1000) {
  618. ms--;
  619. if (ms == 0) return;
  620. start += 1000;
  621. }
  622. yield();
  623. }
  624. }
  625. }
  626. // TODO: verify these result in correct timeouts...
  627. #if F_CPU == 168000000
  628. #define PULSEIN_LOOPS_PER_USEC 25
  629. #elif F_CPU == 144000000
  630. #define PULSEIN_LOOPS_PER_USEC 21
  631. #elif F_CPU == 120000000
  632. #define PULSEIN_LOOPS_PER_USEC 18
  633. #elif F_CPU == 96000000
  634. #define PULSEIN_LOOPS_PER_USEC 14
  635. #elif F_CPU == 72000000
  636. #define PULSEIN_LOOPS_PER_USEC 10
  637. #elif F_CPU == 48000000
  638. #define PULSEIN_LOOPS_PER_USEC 7
  639. #elif F_CPU == 24000000
  640. #define PULSEIN_LOOPS_PER_USEC 4
  641. #elif F_CPU == 4000000
  642. #define PULSEIN_LOOPS_PER_USEC 1
  643. #elif F_CPU == 2000000
  644. #define PULSEIN_LOOPS_PER_USEC 1
  645. #endif
  646. uint32_t pulseIn_high(volatile uint8_t *reg, uint32_t timeout)
  647. {
  648. uint32_t timeout_count = timeout * PULSEIN_LOOPS_PER_USEC;
  649. uint32_t usec_start, usec_stop;
  650. // wait for any previous pulse to end
  651. while (*reg) {
  652. if (--timeout_count == 0) return 0;
  653. }
  654. // wait for the pulse to start
  655. while (!*reg) {
  656. if (--timeout_count == 0) return 0;
  657. }
  658. usec_start = micros();
  659. // wait for the pulse to stop
  660. while (*reg) {
  661. if (--timeout_count == 0) return 0;
  662. }
  663. usec_stop = micros();
  664. return usec_stop - usec_start;
  665. }
  666. uint32_t pulseIn_low(volatile uint8_t *reg, uint32_t timeout)
  667. {
  668. uint32_t timeout_count = timeout * PULSEIN_LOOPS_PER_USEC;
  669. uint32_t usec_start, usec_stop;
  670. // wait for any previous pulse to end
  671. while (!*reg) {
  672. if (--timeout_count == 0) return 0;
  673. }
  674. // wait for the pulse to start
  675. while (*reg) {
  676. if (--timeout_count == 0) return 0;
  677. }
  678. usec_start = micros();
  679. // wait for the pulse to stop
  680. while (!*reg) {
  681. if (--timeout_count == 0) return 0;
  682. }
  683. usec_stop = micros();
  684. return usec_stop - usec_start;
  685. }
  686. // TODO: an inline version should handle the common case where state is const
  687. uint32_t pulseIn(uint8_t pin, uint8_t state, uint32_t timeout)
  688. {
  689. if (pin >= CORE_NUM_DIGITAL) return 0;
  690. if (state) return pulseIn_high(portInputRegister(pin), timeout);
  691. return pulseIn_low(portInputRegister(pin), timeout);;
  692. }