Teensy 4.1 core updated for C++20
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

преди 11 години
преди 10 години
преди 11 години
преди 9 години
преди 8 години
преди 9 години
преди 11 години
преди 10 години
преди 11 години
преди 10 години
преди 11 години
преди 10 години
преди 11 години
преди 10 години
преди 11 години
преди 10 години
преди 11 години
преди 9 години
преди 11 години
преди 10 години
преди 11 години
преди 10 години
преди 11 години
преди 10 години
преди 11 години
преди 10 години
преди 11 години
преди 10 години
преди 11 години
преди 10 години
преди 11 години
преди 9 години
преди 9 години
преди 8 години
преди 9 години
преди 8 години
преди 9 години
преди 8 години
преди 9 години
преди 8 години
преди 9 години
преди 10 години
преди 11 години
преди 9 години
преди 10 години
преди 11 години
преди 10 години
преди 11 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 8 години
преди 10 години
преди 11 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 11 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 11 години
преди 9 години
преди 10 години
преди 11 години
преди 9 години
преди 11 години
преди 9 години
преди 9 години
преди 9 години
преди 10 години
преди 11 години
преди 9 години
преди 11 години
преди 9 години
преди 9 години
преди 9 години
преди 11 години
преди 11 години
преди 8 години
преди 11 години
преди 10 години
преди 9 години
преди 10 години
преди 9 години
преди 10 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 10 години
преди 9 години
преди 9 години
преди 11 години
преди 11 години
преди 10 години
преди 11 години
преди 10 години
преди 11 години
преди 11 години
преди 11 години
преди 10 години
преди 11 години
преди 10 години
преди 11 години
преди 10 години
преди 11 години
преди 10 години
преди 11 години
преди 10 години
преди 11 години
преди 10 години
преди 11 години
преди 10 години
преди 11 години
преди 10 години
преди 11 години
преди 10 години
преди 11 години
преди 10 години
преди 11 години
преди 10 години
преди 11 години
преди 10 години
преди 11 години
преди 10 години
преди 11 години
преди 10 години
преди 11 години
преди 10 години
преди 11 години
преди 10 години
преди 11 години
преди 9 години
преди 11 години
преди 11 години
преди 10 години
преди 11 години
преди 10 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 10 години
преди 11 години
преди 10 години
преди 11 години
преди 11 години
преди 10 години
преди 9 години
преди 9 години
преди 11 години
преди 10 години
преди 11 години
преди 10 години
преди 11 години
преди 10 години
преди 11 години
преди 10 години
преди 11 години
преди 10 години
преди 11 години
преди 10 години
преди 11 години
преди 11 години
преди 10 години
преди 11 години
преди 10 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 9 години
преди 11 години
преди 10 години
преди 8 години
преди 9 години
преди 10 години
преди 11 години
преди 10 години
преди 11 години
преди 10 години
преди 10 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393
  1. /* Teensyduino Core Library
  2. * http://www.pjrc.com/teensy/
  3. * Copyright (c) 2013 PJRC.COM, LLC.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining
  6. * a copy of this software and associated documentation files (the
  7. * "Software"), to deal in the Software without restriction, including
  8. * without limitation the rights to use, copy, modify, merge, publish,
  9. * distribute, sublicense, and/or sell copies of the Software, and to
  10. * permit persons to whom the Software is furnished to do so, subject to
  11. * the following conditions:
  12. *
  13. * 1. The above copyright notice and this permission notice shall be
  14. * included in all copies or substantial portions of the Software.
  15. *
  16. * 2. If the Software is incorporated into a build system that allows
  17. * selection among a list of target devices, then similar target
  18. * devices manufactured by PJRC.COM must be included in the list of
  19. * target devices and selectable in the same manner.
  20. *
  21. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  25. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  26. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  27. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  28. * SOFTWARE.
  29. */
  30. #include "core_pins.h"
  31. #include "pins_arduino.h"
  32. #include "HardwareSerial.h"
  33. #if defined(KINETISK)
  34. #define GPIO_BITBAND_ADDR(reg, bit) (((uint32_t)&(reg) - 0x40000000) * 32 + (bit) * 4 + 0x42000000)
  35. #define GPIO_BITBAND_PTR(reg, bit) ((uint32_t *)GPIO_BITBAND_ADDR((reg), (bit)))
  36. //#define GPIO_SET_BIT(reg, bit) (*GPIO_BITBAND_PTR((reg), (bit)) = 1)
  37. //#define GPIO_CLR_BIT(reg, bit) (*GPIO_BITBAND_PTR((reg), (bit)) = 0)
  38. const struct digital_pin_bitband_and_config_table_struct digital_pin_to_info_PGM[] = {
  39. {GPIO_BITBAND_PTR(CORE_PIN0_PORTREG, CORE_PIN0_BIT), &CORE_PIN0_CONFIG},
  40. {GPIO_BITBAND_PTR(CORE_PIN1_PORTREG, CORE_PIN1_BIT), &CORE_PIN1_CONFIG},
  41. {GPIO_BITBAND_PTR(CORE_PIN2_PORTREG, CORE_PIN2_BIT), &CORE_PIN2_CONFIG},
  42. {GPIO_BITBAND_PTR(CORE_PIN3_PORTREG, CORE_PIN3_BIT), &CORE_PIN3_CONFIG},
  43. {GPIO_BITBAND_PTR(CORE_PIN4_PORTREG, CORE_PIN4_BIT), &CORE_PIN4_CONFIG},
  44. {GPIO_BITBAND_PTR(CORE_PIN5_PORTREG, CORE_PIN5_BIT), &CORE_PIN5_CONFIG},
  45. {GPIO_BITBAND_PTR(CORE_PIN6_PORTREG, CORE_PIN6_BIT), &CORE_PIN6_CONFIG},
  46. {GPIO_BITBAND_PTR(CORE_PIN7_PORTREG, CORE_PIN7_BIT), &CORE_PIN7_CONFIG},
  47. {GPIO_BITBAND_PTR(CORE_PIN8_PORTREG, CORE_PIN8_BIT), &CORE_PIN8_CONFIG},
  48. {GPIO_BITBAND_PTR(CORE_PIN9_PORTREG, CORE_PIN9_BIT), &CORE_PIN9_CONFIG},
  49. {GPIO_BITBAND_PTR(CORE_PIN10_PORTREG, CORE_PIN10_BIT), &CORE_PIN10_CONFIG},
  50. {GPIO_BITBAND_PTR(CORE_PIN11_PORTREG, CORE_PIN11_BIT), &CORE_PIN11_CONFIG},
  51. {GPIO_BITBAND_PTR(CORE_PIN12_PORTREG, CORE_PIN12_BIT), &CORE_PIN12_CONFIG},
  52. {GPIO_BITBAND_PTR(CORE_PIN13_PORTREG, CORE_PIN13_BIT), &CORE_PIN13_CONFIG},
  53. {GPIO_BITBAND_PTR(CORE_PIN14_PORTREG, CORE_PIN14_BIT), &CORE_PIN14_CONFIG},
  54. {GPIO_BITBAND_PTR(CORE_PIN15_PORTREG, CORE_PIN15_BIT), &CORE_PIN15_CONFIG},
  55. {GPIO_BITBAND_PTR(CORE_PIN16_PORTREG, CORE_PIN16_BIT), &CORE_PIN16_CONFIG},
  56. {GPIO_BITBAND_PTR(CORE_PIN17_PORTREG, CORE_PIN17_BIT), &CORE_PIN17_CONFIG},
  57. {GPIO_BITBAND_PTR(CORE_PIN18_PORTREG, CORE_PIN18_BIT), &CORE_PIN18_CONFIG},
  58. {GPIO_BITBAND_PTR(CORE_PIN19_PORTREG, CORE_PIN19_BIT), &CORE_PIN19_CONFIG},
  59. {GPIO_BITBAND_PTR(CORE_PIN20_PORTREG, CORE_PIN20_BIT), &CORE_PIN20_CONFIG},
  60. {GPIO_BITBAND_PTR(CORE_PIN21_PORTREG, CORE_PIN21_BIT), &CORE_PIN21_CONFIG},
  61. {GPIO_BITBAND_PTR(CORE_PIN22_PORTREG, CORE_PIN22_BIT), &CORE_PIN22_CONFIG},
  62. {GPIO_BITBAND_PTR(CORE_PIN23_PORTREG, CORE_PIN23_BIT), &CORE_PIN23_CONFIG},
  63. {GPIO_BITBAND_PTR(CORE_PIN24_PORTREG, CORE_PIN24_BIT), &CORE_PIN24_CONFIG},
  64. {GPIO_BITBAND_PTR(CORE_PIN25_PORTREG, CORE_PIN25_BIT), &CORE_PIN25_CONFIG},
  65. {GPIO_BITBAND_PTR(CORE_PIN26_PORTREG, CORE_PIN26_BIT), &CORE_PIN26_CONFIG},
  66. {GPIO_BITBAND_PTR(CORE_PIN27_PORTREG, CORE_PIN27_BIT), &CORE_PIN27_CONFIG},
  67. {GPIO_BITBAND_PTR(CORE_PIN28_PORTREG, CORE_PIN28_BIT), &CORE_PIN28_CONFIG},
  68. {GPIO_BITBAND_PTR(CORE_PIN29_PORTREG, CORE_PIN29_BIT), &CORE_PIN29_CONFIG},
  69. {GPIO_BITBAND_PTR(CORE_PIN30_PORTREG, CORE_PIN30_BIT), &CORE_PIN30_CONFIG},
  70. {GPIO_BITBAND_PTR(CORE_PIN31_PORTREG, CORE_PIN31_BIT), &CORE_PIN31_CONFIG},
  71. {GPIO_BITBAND_PTR(CORE_PIN32_PORTREG, CORE_PIN32_BIT), &CORE_PIN32_CONFIG},
  72. {GPIO_BITBAND_PTR(CORE_PIN33_PORTREG, CORE_PIN33_BIT), &CORE_PIN33_CONFIG},
  73. #ifdef CORE_PIN34_PORTREG
  74. {GPIO_BITBAND_PTR(CORE_PIN34_PORTREG, CORE_PIN34_BIT), &CORE_PIN34_CONFIG},
  75. {GPIO_BITBAND_PTR(CORE_PIN35_PORTREG, CORE_PIN35_BIT), &CORE_PIN35_CONFIG},
  76. {GPIO_BITBAND_PTR(CORE_PIN36_PORTREG, CORE_PIN36_BIT), &CORE_PIN36_CONFIG},
  77. {GPIO_BITBAND_PTR(CORE_PIN37_PORTREG, CORE_PIN37_BIT), &CORE_PIN37_CONFIG},
  78. {GPIO_BITBAND_PTR(CORE_PIN38_PORTREG, CORE_PIN38_BIT), &CORE_PIN38_CONFIG},
  79. {GPIO_BITBAND_PTR(CORE_PIN39_PORTREG, CORE_PIN39_BIT), &CORE_PIN39_CONFIG},
  80. {GPIO_BITBAND_PTR(CORE_PIN40_PORTREG, CORE_PIN40_BIT), &CORE_PIN40_CONFIG},
  81. {GPIO_BITBAND_PTR(CORE_PIN41_PORTREG, CORE_PIN41_BIT), &CORE_PIN41_CONFIG},
  82. {GPIO_BITBAND_PTR(CORE_PIN42_PORTREG, CORE_PIN42_BIT), &CORE_PIN42_CONFIG},
  83. {GPIO_BITBAND_PTR(CORE_PIN43_PORTREG, CORE_PIN43_BIT), &CORE_PIN43_CONFIG},
  84. {GPIO_BITBAND_PTR(CORE_PIN44_PORTREG, CORE_PIN44_BIT), &CORE_PIN44_CONFIG},
  85. {GPIO_BITBAND_PTR(CORE_PIN45_PORTREG, CORE_PIN45_BIT), &CORE_PIN45_CONFIG},
  86. {GPIO_BITBAND_PTR(CORE_PIN46_PORTREG, CORE_PIN46_BIT), &CORE_PIN46_CONFIG},
  87. {GPIO_BITBAND_PTR(CORE_PIN47_PORTREG, CORE_PIN47_BIT), &CORE_PIN47_CONFIG},
  88. {GPIO_BITBAND_PTR(CORE_PIN48_PORTREG, CORE_PIN48_BIT), &CORE_PIN48_CONFIG},
  89. {GPIO_BITBAND_PTR(CORE_PIN49_PORTREG, CORE_PIN49_BIT), &CORE_PIN49_CONFIG},
  90. {GPIO_BITBAND_PTR(CORE_PIN50_PORTREG, CORE_PIN50_BIT), &CORE_PIN50_CONFIG},
  91. {GPIO_BITBAND_PTR(CORE_PIN51_PORTREG, CORE_PIN51_BIT), &CORE_PIN51_CONFIG},
  92. {GPIO_BITBAND_PTR(CORE_PIN52_PORTREG, CORE_PIN52_BIT), &CORE_PIN52_CONFIG},
  93. {GPIO_BITBAND_PTR(CORE_PIN53_PORTREG, CORE_PIN53_BIT), &CORE_PIN53_CONFIG},
  94. {GPIO_BITBAND_PTR(CORE_PIN54_PORTREG, CORE_PIN54_BIT), &CORE_PIN54_CONFIG},
  95. {GPIO_BITBAND_PTR(CORE_PIN55_PORTREG, CORE_PIN55_BIT), &CORE_PIN55_CONFIG},
  96. {GPIO_BITBAND_PTR(CORE_PIN56_PORTREG, CORE_PIN56_BIT), &CORE_PIN56_CONFIG},
  97. {GPIO_BITBAND_PTR(CORE_PIN57_PORTREG, CORE_PIN57_BIT), &CORE_PIN57_CONFIG},
  98. #endif
  99. };
  100. #elif defined(KINETISL)
  101. const struct digital_pin_bitband_and_config_table_struct digital_pin_to_info_PGM[] = {
  102. {((volatile uint8_t *)&CORE_PIN0_PORTREG + (CORE_PIN0_BIT >> 3)), &CORE_PIN0_CONFIG, (1<<(CORE_PIN0_BIT & 7))},
  103. {((volatile uint8_t *)&CORE_PIN1_PORTREG + (CORE_PIN1_BIT >> 3)), &CORE_PIN1_CONFIG, (1<<(CORE_PIN1_BIT & 7))},
  104. {((volatile uint8_t *)&CORE_PIN2_PORTREG + (CORE_PIN2_BIT >> 3)), &CORE_PIN2_CONFIG, (1<<(CORE_PIN2_BIT & 7))},
  105. {((volatile uint8_t *)&CORE_PIN3_PORTREG + (CORE_PIN3_BIT >> 3)), &CORE_PIN3_CONFIG, (1<<(CORE_PIN3_BIT & 7))},
  106. {((volatile uint8_t *)&CORE_PIN4_PORTREG + (CORE_PIN4_BIT >> 3)), &CORE_PIN4_CONFIG, (1<<(CORE_PIN4_BIT & 7))},
  107. {((volatile uint8_t *)&CORE_PIN5_PORTREG + (CORE_PIN5_BIT >> 3)), &CORE_PIN5_CONFIG, (1<<(CORE_PIN5_BIT & 7))},
  108. {((volatile uint8_t *)&CORE_PIN6_PORTREG + (CORE_PIN6_BIT >> 3)), &CORE_PIN6_CONFIG, (1<<(CORE_PIN6_BIT & 7))},
  109. {((volatile uint8_t *)&CORE_PIN7_PORTREG + (CORE_PIN7_BIT >> 3)), &CORE_PIN7_CONFIG, (1<<(CORE_PIN7_BIT & 7))},
  110. {((volatile uint8_t *)&CORE_PIN8_PORTREG + (CORE_PIN8_BIT >> 3)), &CORE_PIN8_CONFIG, (1<<(CORE_PIN8_BIT & 7))},
  111. {((volatile uint8_t *)&CORE_PIN9_PORTREG + (CORE_PIN9_BIT >> 3)), &CORE_PIN9_CONFIG, (1<<(CORE_PIN9_BIT & 7))},
  112. {((volatile uint8_t *)&CORE_PIN10_PORTREG + (CORE_PIN10_BIT >> 3)), &CORE_PIN10_CONFIG, (1<<(CORE_PIN10_BIT & 7))},
  113. {((volatile uint8_t *)&CORE_PIN11_PORTREG + (CORE_PIN11_BIT >> 3)), &CORE_PIN11_CONFIG, (1<<(CORE_PIN11_BIT & 7))},
  114. {((volatile uint8_t *)&CORE_PIN12_PORTREG + (CORE_PIN12_BIT >> 3)), &CORE_PIN12_CONFIG, (1<<(CORE_PIN12_BIT & 7))},
  115. {((volatile uint8_t *)&CORE_PIN13_PORTREG + (CORE_PIN13_BIT >> 3)), &CORE_PIN13_CONFIG, (1<<(CORE_PIN13_BIT & 7))},
  116. {((volatile uint8_t *)&CORE_PIN14_PORTREG + (CORE_PIN14_BIT >> 3)), &CORE_PIN14_CONFIG, (1<<(CORE_PIN14_BIT & 7))},
  117. {((volatile uint8_t *)&CORE_PIN15_PORTREG + (CORE_PIN15_BIT >> 3)), &CORE_PIN15_CONFIG, (1<<(CORE_PIN15_BIT & 7))},
  118. {((volatile uint8_t *)&CORE_PIN16_PORTREG + (CORE_PIN16_BIT >> 3)), &CORE_PIN16_CONFIG, (1<<(CORE_PIN16_BIT & 7))},
  119. {((volatile uint8_t *)&CORE_PIN17_PORTREG + (CORE_PIN17_BIT >> 3)), &CORE_PIN17_CONFIG, (1<<(CORE_PIN17_BIT & 7))},
  120. {((volatile uint8_t *)&CORE_PIN18_PORTREG + (CORE_PIN18_BIT >> 3)), &CORE_PIN18_CONFIG, (1<<(CORE_PIN18_BIT & 7))},
  121. {((volatile uint8_t *)&CORE_PIN19_PORTREG + (CORE_PIN19_BIT >> 3)), &CORE_PIN19_CONFIG, (1<<(CORE_PIN19_BIT & 7))},
  122. {((volatile uint8_t *)&CORE_PIN20_PORTREG + (CORE_PIN20_BIT >> 3)), &CORE_PIN20_CONFIG, (1<<(CORE_PIN20_BIT & 7))},
  123. {((volatile uint8_t *)&CORE_PIN21_PORTREG + (CORE_PIN21_BIT >> 3)), &CORE_PIN21_CONFIG, (1<<(CORE_PIN21_BIT & 7))},
  124. {((volatile uint8_t *)&CORE_PIN22_PORTREG + (CORE_PIN22_BIT >> 3)), &CORE_PIN22_CONFIG, (1<<(CORE_PIN22_BIT & 7))},
  125. {((volatile uint8_t *)&CORE_PIN23_PORTREG + (CORE_PIN23_BIT >> 3)), &CORE_PIN23_CONFIG, (1<<(CORE_PIN23_BIT & 7))},
  126. {((volatile uint8_t *)&CORE_PIN24_PORTREG + (CORE_PIN24_BIT >> 3)), &CORE_PIN24_CONFIG, (1<<(CORE_PIN24_BIT & 7))},
  127. {((volatile uint8_t *)&CORE_PIN25_PORTREG + (CORE_PIN25_BIT >> 3)), &CORE_PIN25_CONFIG, (1<<(CORE_PIN25_BIT & 7))},
  128. {((volatile uint8_t *)&CORE_PIN26_PORTREG + (CORE_PIN26_BIT >> 3)), &CORE_PIN26_CONFIG, (1<<(CORE_PIN26_BIT & 7))}
  129. };
  130. #endif
  131. typedef void (*voidFuncPtr)(void);
  132. volatile static voidFuncPtr intFunc[CORE_NUM_DIGITAL];
  133. #if defined(KINETISK)
  134. static void porta_interrupt(void);
  135. static void portb_interrupt(void);
  136. static void portc_interrupt(void);
  137. static void portd_interrupt(void);
  138. static void porte_interrupt(void);
  139. #elif defined(KINETISL)
  140. static void porta_interrupt(void);
  141. static void portcd_interrupt(void);
  142. #endif
  143. void attachInterruptVector(enum IRQ_NUMBER_t irq, void (*function)(void))
  144. {
  145. _VectorsRam[irq + 16] = function;
  146. }
  147. void attachInterrupt(uint8_t pin, void (*function)(void), int mode)
  148. {
  149. volatile uint32_t *config;
  150. uint32_t cfg, mask;
  151. if (pin >= CORE_NUM_DIGITAL) return;
  152. switch (mode) {
  153. case CHANGE: mask = 0x0B; break;
  154. case RISING: mask = 0x09; break;
  155. case FALLING: mask = 0x0A; break;
  156. case LOW: mask = 0x08; break;
  157. case HIGH: mask = 0x0C; break;
  158. default: return;
  159. }
  160. mask = (mask << 16) | 0x01000000;
  161. config = portConfigRegister(pin);
  162. #if defined(KINETISK)
  163. attachInterruptVector(IRQ_PORTA, porta_interrupt);
  164. attachInterruptVector(IRQ_PORTB, portb_interrupt);
  165. attachInterruptVector(IRQ_PORTC, portc_interrupt);
  166. attachInterruptVector(IRQ_PORTD, portd_interrupt);
  167. attachInterruptVector(IRQ_PORTE, porte_interrupt);
  168. #elif defined(KINETISL)
  169. attachInterruptVector(IRQ_PORTA, porta_interrupt);
  170. attachInterruptVector(IRQ_PORTCD, portcd_interrupt);
  171. #endif
  172. __disable_irq();
  173. cfg = *config;
  174. cfg &= ~0x000F0000; // disable any previous interrupt
  175. *config = cfg;
  176. intFunc[pin] = function; // set the function pointer
  177. cfg |= mask;
  178. *config = cfg; // enable the new interrupt
  179. __enable_irq();
  180. }
  181. void detachInterrupt(uint8_t pin)
  182. {
  183. volatile uint32_t *config;
  184. config = portConfigRegister(pin);
  185. __disable_irq();
  186. *config = ((*config & ~0x000F0000) | 0x01000000);
  187. intFunc[pin] = NULL;
  188. __enable_irq();
  189. }
  190. #if defined(__MK20DX128__) || defined(__MK20DX256__)
  191. static void porta_interrupt(void)
  192. {
  193. uint32_t isfr = PORTA_ISFR;
  194. PORTA_ISFR = isfr;
  195. if ((isfr & CORE_PIN3_BITMASK) && intFunc[3]) intFunc[3]();
  196. if ((isfr & CORE_PIN4_BITMASK) && intFunc[4]) intFunc[4]();
  197. if ((isfr & CORE_PIN24_BITMASK) && intFunc[24]) intFunc[24]();
  198. if ((isfr & CORE_PIN33_BITMASK) && intFunc[33]) intFunc[33]();
  199. }
  200. static void portb_interrupt(void)
  201. {
  202. uint32_t isfr = PORTB_ISFR;
  203. PORTB_ISFR = isfr;
  204. if ((isfr & CORE_PIN0_BITMASK) && intFunc[0]) intFunc[0]();
  205. if ((isfr & CORE_PIN1_BITMASK) && intFunc[1]) intFunc[1]();
  206. if ((isfr & CORE_PIN16_BITMASK) && intFunc[16]) intFunc[16]();
  207. if ((isfr & CORE_PIN17_BITMASK) && intFunc[17]) intFunc[17]();
  208. if ((isfr & CORE_PIN18_BITMASK) && intFunc[18]) intFunc[18]();
  209. if ((isfr & CORE_PIN19_BITMASK) && intFunc[19]) intFunc[19]();
  210. if ((isfr & CORE_PIN25_BITMASK) && intFunc[25]) intFunc[25]();
  211. if ((isfr & CORE_PIN32_BITMASK) && intFunc[32]) intFunc[32]();
  212. }
  213. static void portc_interrupt(void)
  214. {
  215. // TODO: these are inefficent. Use CLZ somehow....
  216. uint32_t isfr = PORTC_ISFR;
  217. PORTC_ISFR = isfr;
  218. if ((isfr & CORE_PIN9_BITMASK) && intFunc[9]) intFunc[9]();
  219. if ((isfr & CORE_PIN10_BITMASK) && intFunc[10]) intFunc[10]();
  220. if ((isfr & CORE_PIN11_BITMASK) && intFunc[11]) intFunc[11]();
  221. if ((isfr & CORE_PIN12_BITMASK) && intFunc[12]) intFunc[12]();
  222. if ((isfr & CORE_PIN13_BITMASK) && intFunc[13]) intFunc[13]();
  223. if ((isfr & CORE_PIN15_BITMASK) && intFunc[15]) intFunc[15]();
  224. if ((isfr & CORE_PIN22_BITMASK) && intFunc[22]) intFunc[22]();
  225. if ((isfr & CORE_PIN23_BITMASK) && intFunc[23]) intFunc[23]();
  226. if ((isfr & CORE_PIN27_BITMASK) && intFunc[27]) intFunc[27]();
  227. if ((isfr & CORE_PIN28_BITMASK) && intFunc[28]) intFunc[28]();
  228. if ((isfr & CORE_PIN29_BITMASK) && intFunc[29]) intFunc[29]();
  229. if ((isfr & CORE_PIN30_BITMASK) && intFunc[30]) intFunc[30]();
  230. }
  231. static void portd_interrupt(void)
  232. {
  233. uint32_t isfr = PORTD_ISFR;
  234. PORTD_ISFR = isfr;
  235. if ((isfr & CORE_PIN2_BITMASK) && intFunc[2]) intFunc[2]();
  236. if ((isfr & CORE_PIN5_BITMASK) && intFunc[5]) intFunc[5]();
  237. if ((isfr & CORE_PIN6_BITMASK) && intFunc[6]) intFunc[6]();
  238. if ((isfr & CORE_PIN7_BITMASK) && intFunc[7]) intFunc[7]();
  239. if ((isfr & CORE_PIN8_BITMASK) && intFunc[8]) intFunc[8]();
  240. if ((isfr & CORE_PIN14_BITMASK) && intFunc[14]) intFunc[14]();
  241. if ((isfr & CORE_PIN20_BITMASK) && intFunc[20]) intFunc[20]();
  242. if ((isfr & CORE_PIN21_BITMASK) && intFunc[21]) intFunc[21]();
  243. }
  244. static void porte_interrupt(void)
  245. {
  246. uint32_t isfr = PORTE_ISFR;
  247. PORTE_ISFR = isfr;
  248. if ((isfr & CORE_PIN26_BITMASK) && intFunc[26]) intFunc[26]();
  249. if ((isfr & CORE_PIN31_BITMASK) && intFunc[31]) intFunc[31]();
  250. }
  251. #elif defined(__MKL26Z64__)
  252. static void porta_interrupt(void)
  253. {
  254. uint32_t isfr = PORTA_ISFR;
  255. PORTA_ISFR = isfr;
  256. if ((isfr & CORE_PIN3_BITMASK) && intFunc[3]) intFunc[3]();
  257. if ((isfr & CORE_PIN4_BITMASK) && intFunc[4]) intFunc[4]();
  258. }
  259. static void portcd_interrupt(void)
  260. {
  261. uint32_t isfr = PORTC_ISFR;
  262. PORTC_ISFR = isfr;
  263. if ((isfr & CORE_PIN9_BITMASK) && intFunc[9]) intFunc[9]();
  264. if ((isfr & CORE_PIN10_BITMASK) && intFunc[10]) intFunc[10]();
  265. if ((isfr & CORE_PIN11_BITMASK) && intFunc[11]) intFunc[11]();
  266. if ((isfr & CORE_PIN12_BITMASK) && intFunc[12]) intFunc[12]();
  267. if ((isfr & CORE_PIN13_BITMASK) && intFunc[13]) intFunc[13]();
  268. if ((isfr & CORE_PIN15_BITMASK) && intFunc[15]) intFunc[15]();
  269. if ((isfr & CORE_PIN22_BITMASK) && intFunc[22]) intFunc[22]();
  270. if ((isfr & CORE_PIN23_BITMASK) && intFunc[23]) intFunc[23]();
  271. isfr = PORTD_ISFR;
  272. PORTD_ISFR = isfr;
  273. if ((isfr & CORE_PIN2_BITMASK) && intFunc[2]) intFunc[2]();
  274. if ((isfr & CORE_PIN5_BITMASK) && intFunc[5]) intFunc[5]();
  275. if ((isfr & CORE_PIN6_BITMASK) && intFunc[6]) intFunc[6]();
  276. if ((isfr & CORE_PIN7_BITMASK) && intFunc[7]) intFunc[7]();
  277. if ((isfr & CORE_PIN8_BITMASK) && intFunc[8]) intFunc[8]();
  278. if ((isfr & CORE_PIN14_BITMASK) && intFunc[14]) intFunc[14]();
  279. if ((isfr & CORE_PIN20_BITMASK) && intFunc[20]) intFunc[20]();
  280. if ((isfr & CORE_PIN21_BITMASK) && intFunc[21]) intFunc[21]();
  281. }
  282. #elif defined(__MK64FX512__) || defined(__MK66FX1M0__)
  283. static void porta_interrupt(void)
  284. {
  285. uint32_t isfr = PORTA_ISFR;
  286. PORTA_ISFR = isfr;
  287. if ((isfr & CORE_PIN3_BITMASK) && intFunc[3]) intFunc[3]();
  288. if ((isfr & CORE_PIN4_BITMASK) && intFunc[4]) intFunc[4]();
  289. if ((isfr & CORE_PIN25_BITMASK) && intFunc[25]) intFunc[25]();
  290. if ((isfr & CORE_PIN26_BITMASK) && intFunc[26]) intFunc[26]();
  291. if ((isfr & CORE_PIN27_BITMASK) && intFunc[27]) intFunc[27]();
  292. if ((isfr & CORE_PIN28_BITMASK) && intFunc[28]) intFunc[28]();
  293. if ((isfr & CORE_PIN39_BITMASK) && intFunc[39]) intFunc[39]();
  294. if ((isfr & CORE_PIN40_BITMASK) && intFunc[40]) intFunc[40]();
  295. if ((isfr & CORE_PIN41_BITMASK) && intFunc[41]) intFunc[41]();
  296. if ((isfr & CORE_PIN42_BITMASK) && intFunc[42]) intFunc[42]();
  297. }
  298. static void portb_interrupt(void)
  299. {
  300. uint32_t isfr = PORTB_ISFR;
  301. PORTB_ISFR = isfr;
  302. if ((isfr & CORE_PIN0_BITMASK) && intFunc[0]) intFunc[0]();
  303. if ((isfr & CORE_PIN1_BITMASK) && intFunc[1]) intFunc[1]();
  304. if ((isfr & CORE_PIN16_BITMASK) && intFunc[16]) intFunc[16]();
  305. if ((isfr & CORE_PIN17_BITMASK) && intFunc[17]) intFunc[17]();
  306. if ((isfr & CORE_PIN18_BITMASK) && intFunc[18]) intFunc[18]();
  307. if ((isfr & CORE_PIN19_BITMASK) && intFunc[19]) intFunc[19]();
  308. if ((isfr & CORE_PIN29_BITMASK) && intFunc[29]) intFunc[29]();
  309. if ((isfr & CORE_PIN30_BITMASK) && intFunc[30]) intFunc[30]();
  310. if ((isfr & CORE_PIN31_BITMASK) && intFunc[31]) intFunc[31]();
  311. if ((isfr & CORE_PIN32_BITMASK) && intFunc[32]) intFunc[32]();
  312. if ((isfr & CORE_PIN43_BITMASK) && intFunc[43]) intFunc[43]();
  313. if ((isfr & CORE_PIN44_BITMASK) && intFunc[44]) intFunc[44]();
  314. if ((isfr & CORE_PIN45_BITMASK) && intFunc[45]) intFunc[45]();
  315. if ((isfr & CORE_PIN46_BITMASK) && intFunc[46]) intFunc[46]();
  316. if ((isfr & CORE_PIN49_BITMASK) && intFunc[49]) intFunc[49]();
  317. if ((isfr & CORE_PIN50_BITMASK) && intFunc[50]) intFunc[50]();
  318. }
  319. static void portc_interrupt(void)
  320. {
  321. // TODO: these are inefficent. Use CLZ somehow....
  322. uint32_t isfr = PORTC_ISFR;
  323. PORTC_ISFR = isfr;
  324. if ((isfr & CORE_PIN9_BITMASK) && intFunc[9]) intFunc[9]();
  325. if ((isfr & CORE_PIN10_BITMASK) && intFunc[10]) intFunc[10]();
  326. if ((isfr & CORE_PIN11_BITMASK) && intFunc[11]) intFunc[11]();
  327. if ((isfr & CORE_PIN12_BITMASK) && intFunc[12]) intFunc[12]();
  328. if ((isfr & CORE_PIN13_BITMASK) && intFunc[13]) intFunc[13]();
  329. if ((isfr & CORE_PIN15_BITMASK) && intFunc[15]) intFunc[15]();
  330. if ((isfr & CORE_PIN22_BITMASK) && intFunc[22]) intFunc[22]();
  331. if ((isfr & CORE_PIN23_BITMASK) && intFunc[23]) intFunc[23]();
  332. if ((isfr & CORE_PIN35_BITMASK) && intFunc[35]) intFunc[35]();
  333. if ((isfr & CORE_PIN36_BITMASK) && intFunc[36]) intFunc[36]();
  334. if ((isfr & CORE_PIN37_BITMASK) && intFunc[37]) intFunc[37]();
  335. if ((isfr & CORE_PIN38_BITMASK) && intFunc[38]) intFunc[38]();
  336. }
  337. static void portd_interrupt(void)
  338. {
  339. uint32_t isfr = PORTD_ISFR;
  340. PORTD_ISFR = isfr;
  341. if ((isfr & CORE_PIN2_BITMASK) && intFunc[2]) intFunc[2]();
  342. if ((isfr & CORE_PIN5_BITMASK) && intFunc[5]) intFunc[5]();
  343. if ((isfr & CORE_PIN6_BITMASK) && intFunc[6]) intFunc[6]();
  344. if ((isfr & CORE_PIN7_BITMASK) && intFunc[7]) intFunc[7]();
  345. if ((isfr & CORE_PIN8_BITMASK) && intFunc[8]) intFunc[8]();
  346. if ((isfr & CORE_PIN14_BITMASK) && intFunc[14]) intFunc[14]();
  347. if ((isfr & CORE_PIN20_BITMASK) && intFunc[20]) intFunc[20]();
  348. if ((isfr & CORE_PIN21_BITMASK) && intFunc[21]) intFunc[21]();
  349. if ((isfr & CORE_PIN47_BITMASK) && intFunc[47]) intFunc[47]();
  350. if ((isfr & CORE_PIN48_BITMASK) && intFunc[48]) intFunc[48]();
  351. if ((isfr & CORE_PIN51_BITMASK) && intFunc[51]) intFunc[51]();
  352. if ((isfr & CORE_PIN52_BITMASK) && intFunc[52]) intFunc[52]();
  353. if ((isfr & CORE_PIN53_BITMASK) && intFunc[53]) intFunc[53]();
  354. if ((isfr & CORE_PIN54_BITMASK) && intFunc[54]) intFunc[54]();
  355. if ((isfr & CORE_PIN55_BITMASK) && intFunc[55]) intFunc[55]();
  356. }
  357. static void porte_interrupt(void)
  358. {
  359. uint32_t isfr = PORTE_ISFR;
  360. PORTE_ISFR = isfr;
  361. if ((isfr & CORE_PIN24_BITMASK) && intFunc[24]) intFunc[24]();
  362. if ((isfr & CORE_PIN33_BITMASK) && intFunc[33]) intFunc[33]();
  363. if ((isfr & CORE_PIN34_BITMASK) && intFunc[34]) intFunc[34]();
  364. if ((isfr & CORE_PIN56_BITMASK) && intFunc[56]) intFunc[56]();
  365. if ((isfr & CORE_PIN57_BITMASK) && intFunc[57]) intFunc[57]();
  366. }
  367. #endif
  368. #if defined(__MK20DX128__) || defined(__MK20DX256__) || defined(__MK64FX512__) || defined(__MK66FX1M0__)
  369. unsigned long rtc_get(void)
  370. {
  371. return RTC_TSR;
  372. }
  373. void rtc_set(unsigned long t)
  374. {
  375. RTC_SR = 0;
  376. RTC_TPR = 0;
  377. RTC_TSR = t;
  378. RTC_SR = RTC_SR_TCE;
  379. }
  380. // adjust is the amount of crystal error to compensate, 1 = 0.1192 ppm
  381. // For example, adjust = -100 is slows the clock by 11.92 ppm
  382. //
  383. void rtc_compensate(int adjust)
  384. {
  385. uint32_t comp, interval, tcr;
  386. // This simple approach tries to maximize the interval.
  387. // Perhaps minimizing TCR would be better, so the
  388. // compensation is distributed more evenly across
  389. // many seconds, rather than saving it all up and then
  390. // altering one second up to +/- 0.38%
  391. if (adjust >= 0) {
  392. comp = adjust;
  393. interval = 256;
  394. while (1) {
  395. tcr = comp * interval;
  396. if (tcr < 128*256) break;
  397. if (--interval == 1) break;
  398. }
  399. tcr = tcr >> 8;
  400. } else {
  401. comp = -adjust;
  402. interval = 256;
  403. while (1) {
  404. tcr = comp * interval;
  405. if (tcr < 129*256) break;
  406. if (--interval == 1) break;
  407. }
  408. tcr = tcr >> 8;
  409. tcr = 256 - tcr;
  410. }
  411. RTC_TCR = ((interval - 1) << 8) | tcr;
  412. }
  413. #else
  414. unsigned long rtc_get(void) { return 0; }
  415. void rtc_set(unsigned long t) { }
  416. void rtc_compensate(int adjust) { }
  417. #endif
  418. #if 0
  419. // TODO: build system should define this
  420. // so RTC is automatically initialized to approx correct time
  421. // at least when the program begins running right after upload
  422. #ifndef TIME_T
  423. #define TIME_T 1350160272
  424. #endif
  425. void init_rtc(void)
  426. {
  427. serial_print("init_rtc\n");
  428. //SIM_SCGC6 |= SIM_SCGC6_RTC;
  429. // enable the RTC crystal oscillator, for approx 12pf crystal
  430. if (!(RTC_CR & RTC_CR_OSCE)) {
  431. serial_print("start RTC oscillator\n");
  432. RTC_SR = 0;
  433. RTC_CR = RTC_CR_SC16P | RTC_CR_SC4P | RTC_CR_OSCE;
  434. }
  435. // should wait for crystal to stabilize.....
  436. serial_print("SR=");
  437. serial_phex32(RTC_SR);
  438. serial_print("\n");
  439. serial_print("CR=");
  440. serial_phex32(RTC_CR);
  441. serial_print("\n");
  442. serial_print("TSR=");
  443. serial_phex32(RTC_TSR);
  444. serial_print("\n");
  445. serial_print("TCR=");
  446. serial_phex32(RTC_TCR);
  447. serial_print("\n");
  448. if (RTC_SR & RTC_SR_TIF) {
  449. // enable the RTC
  450. RTC_SR = 0;
  451. RTC_TPR = 0;
  452. RTC_TSR = TIME_T;
  453. RTC_SR = RTC_SR_TCE;
  454. }
  455. }
  456. #endif
  457. extern void usb_init(void);
  458. // create a default PWM at the same 488.28 Hz as Arduino Uno
  459. #if defined(KINETISK)
  460. #define F_TIMER F_BUS
  461. #elif defined(KINETISL)
  462. #if F_CPU > 16000000
  463. #define F_TIMER (F_PLL/2)
  464. #else
  465. #define F_TIMER (F_PLL)
  466. #endif//Low Power
  467. #endif
  468. #if F_TIMER == 120000000
  469. #define DEFAULT_FTM_MOD (61440 - 1)
  470. #define DEFAULT_FTM_PRESCALE 2
  471. #elif F_TIMER == 108000000
  472. #define DEFAULT_FTM_MOD (55296 - 1)
  473. #define DEFAULT_FTM_PRESCALE 2
  474. #elif F_TIMER == 96000000
  475. #define DEFAULT_FTM_MOD (49152 - 1)
  476. #define DEFAULT_FTM_PRESCALE 2
  477. #elif F_TIMER == 90000000
  478. #define DEFAULT_FTM_MOD (46080 - 1)
  479. #define DEFAULT_FTM_PRESCALE 2
  480. #elif F_TIMER == 80000000
  481. #define DEFAULT_FTM_MOD (40960 - 1)
  482. #define DEFAULT_FTM_PRESCALE 2
  483. #elif F_TIMER == 72000000
  484. #define DEFAULT_FTM_MOD (36864 - 1)
  485. #define DEFAULT_FTM_PRESCALE 2
  486. #elif F_TIMER == 64000000
  487. #define DEFAULT_FTM_MOD (65536 - 1)
  488. #define DEFAULT_FTM_PRESCALE 1
  489. #elif F_TIMER == 60000000
  490. #define DEFAULT_FTM_MOD (61440 - 1)
  491. #define DEFAULT_FTM_PRESCALE 1
  492. #elif F_TIMER == 56000000
  493. #define DEFAULT_FTM_MOD (57344 - 1)
  494. #define DEFAULT_FTM_PRESCALE 1
  495. #elif F_TIMER == 54000000
  496. #define DEFAULT_FTM_MOD (55296 - 1)
  497. #define DEFAULT_FTM_PRESCALE 1
  498. #elif F_TIMER == 48000000
  499. #define DEFAULT_FTM_MOD (49152 - 1)
  500. #define DEFAULT_FTM_PRESCALE 1
  501. #elif F_TIMER == 40000000
  502. #define DEFAULT_FTM_MOD (40960 - 1)
  503. #define DEFAULT_FTM_PRESCALE 1
  504. #elif F_TIMER == 36000000
  505. #define DEFAULT_FTM_MOD (36864 - 1)
  506. #define DEFAULT_FTM_PRESCALE 1
  507. #elif F_TIMER == 24000000
  508. #define DEFAULT_FTM_MOD (49152 - 1)
  509. #define DEFAULT_FTM_PRESCALE 0
  510. #elif F_TIMER == 16000000
  511. #define DEFAULT_FTM_MOD (32768 - 1)
  512. #define DEFAULT_FTM_PRESCALE 0
  513. #elif F_TIMER == 8000000
  514. #define DEFAULT_FTM_MOD (16384 - 1)
  515. #define DEFAULT_FTM_PRESCALE 0
  516. #elif F_TIMER == 4000000
  517. #define DEFAULT_FTM_MOD (8192 - 1)
  518. #define DEFAULT_FTM_PRESCALE 0
  519. #elif F_TIMER == 2000000
  520. #define DEFAULT_FTM_MOD (4096 - 1)
  521. #define DEFAULT_FTM_PRESCALE 0
  522. #endif
  523. //void init_pins(void)
  524. void _init_Teensyduino_internal_(void)
  525. {
  526. #if defined(__MK20DX128__) || defined(__MK20DX256__) || defined(__MK64FX512__) || defined(__MK66FX1M0__)
  527. NVIC_ENABLE_IRQ(IRQ_PORTA);
  528. NVIC_ENABLE_IRQ(IRQ_PORTB);
  529. NVIC_ENABLE_IRQ(IRQ_PORTC);
  530. NVIC_ENABLE_IRQ(IRQ_PORTD);
  531. NVIC_ENABLE_IRQ(IRQ_PORTE);
  532. #elif defined(__MKL26Z64__)
  533. NVIC_ENABLE_IRQ(IRQ_PORTA);
  534. NVIC_ENABLE_IRQ(IRQ_PORTCD);
  535. #endif
  536. //SIM_SCGC6 |= SIM_SCGC6_FTM0; // TODO: use bitband for atomic read-mod-write
  537. //SIM_SCGC6 |= SIM_SCGC6_FTM1;
  538. FTM0_CNT = 0;
  539. FTM0_MOD = DEFAULT_FTM_MOD;
  540. FTM0_C0SC = 0x28; // MSnB:MSnA = 10, ELSnB:ELSnA = 10
  541. FTM0_C1SC = 0x28;
  542. FTM0_C2SC = 0x28;
  543. FTM0_C3SC = 0x28;
  544. FTM0_C4SC = 0x28;
  545. FTM0_C5SC = 0x28;
  546. #if defined(__MK20DX128__) || defined(__MK20DX256__) || defined(__MK64FX512__) || defined(__MK66FX1M0__)
  547. FTM0_C6SC = 0x28;
  548. FTM0_C7SC = 0x28;
  549. #endif
  550. #if defined(__MK64FX512__) || defined(__MK66FX1M0__)
  551. FTM3_C0SC = 0x28;
  552. FTM3_C1SC = 0x28;
  553. FTM3_C2SC = 0x28;
  554. FTM3_C3SC = 0x28;
  555. FTM3_C4SC = 0x28;
  556. FTM3_C5SC = 0x28;
  557. FTM3_C6SC = 0x28;
  558. FTM3_C7SC = 0x28;
  559. #endif
  560. FTM0_SC = FTM_SC_CLKS(1) | FTM_SC_PS(DEFAULT_FTM_PRESCALE);
  561. FTM1_CNT = 0;
  562. FTM1_MOD = DEFAULT_FTM_MOD;
  563. FTM1_C0SC = 0x28;
  564. FTM1_C1SC = 0x28;
  565. FTM1_SC = FTM_SC_CLKS(1) | FTM_SC_PS(DEFAULT_FTM_PRESCALE);
  566. #if defined(__MK20DX256__) || defined(__MK64FX512__) || defined(__MK66FX1M0__) || defined(__MKL26Z64__)
  567. FTM2_CNT = 0;
  568. FTM2_MOD = DEFAULT_FTM_MOD;
  569. FTM2_C0SC = 0x28;
  570. FTM2_C1SC = 0x28;
  571. FTM2_SC = FTM_SC_CLKS(1) | FTM_SC_PS(DEFAULT_FTM_PRESCALE);
  572. #endif
  573. #if defined(__MK64FX512__) || defined(__MK66FX1M0__)
  574. FTM3_CNT = 0;
  575. FTM3_MOD = DEFAULT_FTM_MOD;
  576. FTM3_C0SC = 0x28;
  577. FTM3_C1SC = 0x28;
  578. FTM3_SC = FTM_SC_CLKS(1) | FTM_SC_PS(DEFAULT_FTM_PRESCALE);
  579. #endif
  580. #if defined(__MK66FX1M0__)
  581. SIM_SCGC2 |= SIM_SCGC2_TPM1;
  582. SIM_SOPT2 |= SIM_SOPT2_TPMSRC(2);
  583. TPM1_CNT = 0;
  584. TPM1_MOD = 32767;
  585. TPM1_C0SC = 0x28;
  586. TPM1_C1SC = 0x28;
  587. TPM1_SC = FTM_SC_CLKS(1) | FTM_SC_PS(0);
  588. #endif
  589. analog_init();
  590. // for background about this startup delay, please see these conversations
  591. // https://forum.pjrc.com/threads/36606-startup-time-(400ms)?p=113980&viewfull=1#post113980
  592. // https://forum.pjrc.com/threads/31290-Teensey-3-2-Teensey-Loader-1-24-Issues?p=87273&viewfull=1#post87273
  593. delay(400);
  594. usb_init();
  595. }
  596. #if defined(__MK20DX128__)
  597. #define FTM0_CH0_PIN 22
  598. #define FTM0_CH1_PIN 23
  599. #define FTM0_CH2_PIN 9
  600. #define FTM0_CH3_PIN 10
  601. #define FTM0_CH4_PIN 6
  602. #define FTM0_CH5_PIN 20
  603. #define FTM0_CH6_PIN 21
  604. #define FTM0_CH7_PIN 5
  605. #define FTM1_CH0_PIN 3
  606. #define FTM1_CH1_PIN 4
  607. #elif defined(__MK20DX256__)
  608. #define FTM0_CH0_PIN 22
  609. #define FTM0_CH1_PIN 23
  610. #define FTM0_CH2_PIN 9
  611. #define FTM0_CH3_PIN 10
  612. #define FTM0_CH4_PIN 6
  613. #define FTM0_CH5_PIN 20
  614. #define FTM0_CH6_PIN 21
  615. #define FTM0_CH7_PIN 5
  616. #define FTM1_CH0_PIN 3
  617. #define FTM1_CH1_PIN 4
  618. #define FTM2_CH0_PIN 32
  619. #define FTM2_CH1_PIN 25
  620. #elif defined(__MKL26Z64__)
  621. #define FTM0_CH0_PIN 22
  622. #define FTM0_CH1_PIN 23
  623. #define FTM0_CH2_PIN 9
  624. #define FTM0_CH3_PIN 10
  625. #define FTM0_CH4_PIN 6
  626. #define FTM0_CH5_PIN 20
  627. #define FTM1_CH0_PIN 16
  628. #define FTM1_CH1_PIN 17
  629. #define FTM2_CH0_PIN 3
  630. #define FTM2_CH1_PIN 4
  631. #elif defined(__MK64FX512__)
  632. #define FTM0_CH0_PIN 22
  633. #define FTM0_CH1_PIN 23
  634. #define FTM0_CH2_PIN 9
  635. #define FTM0_CH3_PIN 10
  636. #define FTM0_CH4_PIN 6
  637. #define FTM0_CH5_PIN 20
  638. #define FTM0_CH6_PIN 21
  639. #define FTM0_CH7_PIN 5
  640. #define FTM1_CH0_PIN 3
  641. #define FTM1_CH1_PIN 4
  642. #define FTM2_CH0_PIN 29
  643. #define FTM2_CH1_PIN 30
  644. #define FTM3_CH0_PIN 2
  645. #define FTM3_CH1_PIN 14
  646. #define FTM3_CH2_PIN 7
  647. #define FTM3_CH3_PIN 8
  648. #define FTM3_CH4_PIN 35
  649. #define FTM3_CH5_PIN 36
  650. #define FTM3_CH6_PIN 37
  651. #define FTM3_CH7_PIN 38
  652. #elif defined(__MK66FX1M0__)
  653. #define FTM0_CH0_PIN 22
  654. #define FTM0_CH1_PIN 23
  655. #define FTM0_CH2_PIN 9
  656. #define FTM0_CH3_PIN 10
  657. #define FTM0_CH4_PIN 6
  658. #define FTM0_CH5_PIN 20
  659. #define FTM0_CH6_PIN 21
  660. #define FTM0_CH7_PIN 5
  661. #define FTM1_CH0_PIN 3
  662. #define FTM1_CH1_PIN 4
  663. #define FTM2_CH0_PIN 29
  664. #define FTM2_CH1_PIN 30
  665. #define FTM3_CH0_PIN 2
  666. #define FTM3_CH1_PIN 14
  667. #define FTM3_CH2_PIN 7
  668. #define FTM3_CH3_PIN 8
  669. #define FTM3_CH4_PIN 35
  670. #define FTM3_CH5_PIN 36
  671. #define FTM3_CH6_PIN 37
  672. #define FTM3_CH7_PIN 38
  673. #define TPM1_CH0_PIN 16
  674. #define TPM1_CH1_PIN 17
  675. #endif
  676. #define FTM_PINCFG(pin) FTM_PINCFG2(pin)
  677. #define FTM_PINCFG2(pin) CORE_PIN ## pin ## _CONFIG
  678. static uint8_t analog_write_res = 8;
  679. // SOPT4 is SIM select clocks?
  680. // FTM is clocked by the bus clock, either 24 or 48 MHz
  681. // input capture can be FTM1_CH0, CMP0 or CMP1 or USB start of frame
  682. // 24 MHz with reload 49152 to match Arduino's speed = 488.28125 Hz
  683. void analogWrite(uint8_t pin, int val)
  684. {
  685. uint32_t cval, max;
  686. #if defined(__MK20DX256__)
  687. if (pin == A14) {
  688. uint8_t res = analog_write_res;
  689. if (res < 12) {
  690. val <<= 12 - res;
  691. } else if (res > 12) {
  692. val >>= res - 12;
  693. }
  694. analogWriteDAC0(val);
  695. return;
  696. }
  697. #elif defined(__MKL26Z64__)
  698. if (pin == A12) {
  699. uint8_t res = analog_write_res;
  700. if (res < 12) {
  701. val <<= 12 - res;
  702. } else if (res > 12) {
  703. val >>= res - 12;
  704. }
  705. analogWriteDAC0(val);
  706. return;
  707. }
  708. #elif defined(__MK64FX512__) || defined(__MK66FX1M0__)
  709. if (pin == A21 || pin == A22) {
  710. uint8_t res = analog_write_res;
  711. if (res < 12) {
  712. val <<= 12 - res;
  713. } else if (res > 12) {
  714. val >>= res - 12;
  715. }
  716. if (pin == A21) analogWriteDAC0(val);
  717. else analogWriteDAC1(val);
  718. return;
  719. }
  720. #endif
  721. max = 1 << analog_write_res;
  722. if (val <= 0) {
  723. digitalWrite(pin, LOW);
  724. pinMode(pin, OUTPUT); // TODO: implement OUTPUT_LOW
  725. return;
  726. } else if (val >= max) {
  727. digitalWrite(pin, HIGH);
  728. pinMode(pin, OUTPUT); // TODO: implement OUTPUT_HIGH
  729. return;
  730. }
  731. //serial_print("analogWrite\n");
  732. //serial_print("val = ");
  733. //serial_phex32(val);
  734. //serial_print("\n");
  735. //serial_print("analog_write_res = ");
  736. //serial_phex(analog_write_res);
  737. //serial_print("\n");
  738. if (pin == FTM1_CH0_PIN || pin == FTM1_CH1_PIN) {
  739. cval = ((uint32_t)val * (uint32_t)(FTM1_MOD + 1)) >> analog_write_res;
  740. #if defined(FTM2_CH0_PIN)
  741. } else if (pin == FTM2_CH0_PIN || pin == FTM2_CH1_PIN) {
  742. cval = ((uint32_t)val * (uint32_t)(FTM2_MOD + 1)) >> analog_write_res;
  743. #endif
  744. #if defined(FTM3_CH0_PIN)
  745. } else if (pin == FTM3_CH0_PIN || pin == FTM3_CH1_PIN || pin == FTM3_CH2_PIN
  746. || pin == FTM3_CH3_PIN || pin == FTM3_CH4_PIN || pin == FTM3_CH5_PIN
  747. || pin == FTM3_CH6_PIN || pin == FTM3_CH7_PIN) {
  748. cval = ((uint32_t)val * (uint32_t)(FTM3_MOD + 1)) >> analog_write_res;
  749. #endif
  750. #if defined(TPM1_CH0_PIN)
  751. } else if (pin == TPM1_CH0_PIN || pin == TPM1_CH1_PIN) {
  752. cval = ((uint32_t)val * (uint32_t)(TPM1_MOD + 1)) >> analog_write_res;
  753. #endif
  754. } else {
  755. cval = ((uint32_t)val * (uint32_t)(FTM0_MOD + 1)) >> analog_write_res;
  756. }
  757. //serial_print("cval = ");
  758. //serial_phex32(cval);
  759. //serial_print("\n");
  760. switch (pin) {
  761. #ifdef FTM0_CH0_PIN
  762. case FTM0_CH0_PIN: // PTC1, FTM0_CH0
  763. FTM0_C0V = cval;
  764. FTM_PINCFG(FTM0_CH0_PIN) = PORT_PCR_MUX(4) | PORT_PCR_DSE | PORT_PCR_SRE;
  765. break;
  766. #endif
  767. #ifdef FTM0_CH1_PIN
  768. case FTM0_CH1_PIN: // PTC2, FTM0_CH1
  769. FTM0_C1V = cval;
  770. FTM_PINCFG(FTM0_CH1_PIN) = PORT_PCR_MUX(4) | PORT_PCR_DSE | PORT_PCR_SRE;
  771. break;
  772. #endif
  773. #ifdef FTM0_CH2_PIN
  774. case FTM0_CH2_PIN: // PTC3, FTM0_CH2
  775. FTM0_C2V = cval;
  776. FTM_PINCFG(FTM0_CH2_PIN) = PORT_PCR_MUX(4) | PORT_PCR_DSE | PORT_PCR_SRE;
  777. break;
  778. #endif
  779. #ifdef FTM0_CH3_PIN
  780. case FTM0_CH3_PIN: // PTC4, FTM0_CH3
  781. FTM0_C3V = cval;
  782. FTM_PINCFG(FTM0_CH3_PIN) = PORT_PCR_MUX(4) | PORT_PCR_DSE | PORT_PCR_SRE;
  783. break;
  784. #endif
  785. #ifdef FTM0_CH4_PIN
  786. case FTM0_CH4_PIN: // PTD4, FTM0_CH4
  787. FTM0_C4V = cval;
  788. FTM_PINCFG(FTM0_CH4_PIN) = PORT_PCR_MUX(4) | PORT_PCR_DSE | PORT_PCR_SRE;
  789. break;
  790. #endif
  791. #ifdef FTM0_CH5_PIN
  792. case FTM0_CH5_PIN: // PTD5, FTM0_CH5
  793. FTM0_C5V = cval;
  794. FTM_PINCFG(FTM0_CH5_PIN) = PORT_PCR_MUX(4) | PORT_PCR_DSE | PORT_PCR_SRE;
  795. break;
  796. #endif
  797. #ifdef FTM0_CH6_PIN
  798. case FTM0_CH6_PIN: // PTD6, FTM0_CH6
  799. FTM0_C6V = cval;
  800. FTM_PINCFG(FTM0_CH6_PIN) = PORT_PCR_MUX(4) | PORT_PCR_DSE | PORT_PCR_SRE;
  801. break;
  802. #endif
  803. #ifdef FTM0_CH7_PIN
  804. case FTM0_CH7_PIN: // PTD7, FTM0_CH7
  805. FTM0_C7V = cval;
  806. FTM_PINCFG(FTM0_CH7_PIN) = PORT_PCR_MUX(4) | PORT_PCR_DSE | PORT_PCR_SRE;
  807. break;
  808. #endif
  809. #ifdef FTM1_CH0_PIN
  810. case FTM1_CH0_PIN: // PTA12, FTM1_CH0
  811. FTM1_C0V = cval;
  812. FTM_PINCFG(FTM1_CH0_PIN) = PORT_PCR_MUX(3) | PORT_PCR_DSE | PORT_PCR_SRE;
  813. break;
  814. #endif
  815. #ifdef FTM1_CH1_PIN
  816. case FTM1_CH1_PIN: // PTA13, FTM1_CH1
  817. FTM1_C1V = cval;
  818. FTM_PINCFG(FTM1_CH1_PIN) = PORT_PCR_MUX(3) | PORT_PCR_DSE | PORT_PCR_SRE;
  819. break;
  820. #endif
  821. #ifdef FTM2_CH0_PIN
  822. case FTM2_CH0_PIN: // PTB18, FTM2_CH0
  823. FTM2_C0V = cval;
  824. FTM_PINCFG(FTM2_CH0_PIN) = PORT_PCR_MUX(3) | PORT_PCR_DSE | PORT_PCR_SRE;
  825. break;
  826. #endif
  827. #ifdef FTM2_CH1_PIN
  828. case FTM2_CH1_PIN: // PTB19, FTM1_CH1
  829. FTM2_C1V = cval;
  830. FTM_PINCFG(FTM2_CH1_PIN) = PORT_PCR_MUX(3) | PORT_PCR_DSE | PORT_PCR_SRE;
  831. break;
  832. #endif
  833. #ifdef FTM3_CH0_PIN
  834. case FTM3_CH0_PIN:
  835. FTM3_C0V = cval;
  836. FTM_PINCFG(FTM3_CH0_PIN) = PORT_PCR_MUX(4) | PORT_PCR_DSE | PORT_PCR_SRE;
  837. break;
  838. #endif
  839. #ifdef FTM3_CH1_PIN
  840. case FTM3_CH1_PIN:
  841. FTM3_C1V = cval;
  842. FTM_PINCFG(FTM3_CH1_PIN) = PORT_PCR_MUX(4) | PORT_PCR_DSE | PORT_PCR_SRE;
  843. break;
  844. #endif
  845. #ifdef FTM3_CH2_PIN
  846. case FTM3_CH2_PIN:
  847. FTM3_C2V = cval;
  848. FTM_PINCFG(FTM3_CH2_PIN) = PORT_PCR_MUX(4) | PORT_PCR_DSE | PORT_PCR_SRE;
  849. break;
  850. #endif
  851. #ifdef FTM3_CH3_PIN
  852. case FTM3_CH3_PIN:
  853. FTM3_C3V = cval;
  854. FTM_PINCFG(FTM3_CH3_PIN) = PORT_PCR_MUX(4) | PORT_PCR_DSE | PORT_PCR_SRE;
  855. break;
  856. #endif
  857. #ifdef FTM3_CH4_PIN
  858. case FTM3_CH4_PIN:
  859. FTM3_C4V = cval;
  860. FTM_PINCFG(FTM3_CH4_PIN) = PORT_PCR_MUX(3) | PORT_PCR_DSE | PORT_PCR_SRE;
  861. break;
  862. #endif
  863. #ifdef FTM3_CH5_PIN
  864. case FTM3_CH5_PIN:
  865. FTM3_C5V = cval;
  866. FTM_PINCFG(FTM3_CH5_PIN) = PORT_PCR_MUX(3) | PORT_PCR_DSE | PORT_PCR_SRE;
  867. break;
  868. #endif
  869. #ifdef FTM3_CH6_PIN
  870. case FTM3_CH6_PIN:
  871. FTM3_C6V = cval;
  872. FTM_PINCFG(FTM3_CH6_PIN) = PORT_PCR_MUX(3) | PORT_PCR_DSE | PORT_PCR_SRE;
  873. break;
  874. #endif
  875. #ifdef FTM3_CH7_PIN
  876. case FTM3_CH7_PIN:
  877. FTM3_C7V = cval;
  878. FTM_PINCFG(FTM3_CH7_PIN) = PORT_PCR_MUX(3) | PORT_PCR_DSE | PORT_PCR_SRE;
  879. break;
  880. #endif
  881. #ifdef TPM1_CH0_PIN
  882. case TPM1_CH0_PIN:
  883. TPM1_C0V = cval;
  884. FTM_PINCFG(TPM1_CH0_PIN) = PORT_PCR_MUX(6) | PORT_PCR_DSE | PORT_PCR_SRE;
  885. break;
  886. #endif
  887. #ifdef TPM1_CH1_PIN
  888. case TPM1_CH1_PIN:
  889. TPM1_C1V = cval;
  890. FTM_PINCFG(TPM1_CH1_PIN) = PORT_PCR_MUX(6) | PORT_PCR_DSE | PORT_PCR_SRE;
  891. break;
  892. #endif
  893. default:
  894. digitalWrite(pin, (val > 127) ? HIGH : LOW);
  895. pinMode(pin, OUTPUT);
  896. }
  897. }
  898. void analogWriteRes(uint32_t bits)
  899. {
  900. if (bits < 1) {
  901. bits = 1;
  902. } else if (bits > 16) {
  903. bits = 16;
  904. }
  905. analog_write_res = bits;
  906. }
  907. void analogWriteFrequency(uint8_t pin, float frequency)
  908. {
  909. uint32_t prescale, mod, ftmClock, ftmClockSource;
  910. float minfreq;
  911. //serial_print("analogWriteFrequency: pin = ");
  912. //serial_phex(pin);
  913. //serial_print(", freq = ");
  914. //serial_phex32((uint32_t)frequency);
  915. //serial_print("\n");
  916. #ifdef TPM1_CH0_PIN
  917. if (pin == TPM1_CH0_PIN || pin == TPM1_CH1_PIN) {
  918. ftmClockSource = 1;
  919. ftmClock = 16000000;
  920. } else
  921. #endif
  922. if (frequency < (float)(F_TIMER >> 7) / 65536.0f) {
  923. // frequency is too low for working with F_TIMER:
  924. ftmClockSource = 2; // Use alternative 31250Hz clock source
  925. ftmClock = 31250; // Set variable for the actual timer clock frequency
  926. } else {
  927. ftmClockSource = 1; // Use default F_TIMER clock source
  928. ftmClock = F_TIMER; // Set variable for the actual timer clock frequency
  929. }
  930. for (prescale = 0; prescale < 7; prescale++) {
  931. minfreq = (float)(ftmClock >> prescale) / 65536.0f; //Use ftmClock instead of F_TIMER
  932. if (frequency >= minfreq) break;
  933. }
  934. //serial_print("F_TIMER/ftm_Clock = ");
  935. //serial_phex32(ftmClock >> prescale);
  936. //serial_print("\n");
  937. //serial_print("prescale = ");
  938. //serial_phex(prescale);
  939. //serial_print("\n");
  940. mod = (float)(ftmClock >> prescale) / frequency - 0.5f; //Use ftmClock instead of F_TIMER
  941. if (mod > 65535) mod = 65535;
  942. //serial_print("mod = ");
  943. //serial_phex32(mod);
  944. //serial_print("\n");
  945. if (pin == FTM1_CH0_PIN || pin == FTM1_CH1_PIN) {
  946. FTM1_SC = 0;
  947. FTM1_CNT = 0;
  948. FTM1_MOD = mod;
  949. FTM1_SC = FTM_SC_CLKS(ftmClockSource) | FTM_SC_PS(prescale); //Use ftmClockSource instead of 1
  950. } else if (pin == FTM0_CH0_PIN || pin == FTM0_CH1_PIN
  951. || pin == FTM0_CH2_PIN || pin == FTM0_CH3_PIN
  952. || pin == FTM0_CH4_PIN || pin == FTM0_CH5_PIN
  953. #ifdef FTM0_CH6_PIN
  954. || pin == FTM0_CH6_PIN || pin == FTM0_CH7_PIN
  955. #endif
  956. ) {
  957. FTM0_SC = 0;
  958. FTM0_CNT = 0;
  959. FTM0_MOD = mod;
  960. FTM0_SC = FTM_SC_CLKS(ftmClockSource) | FTM_SC_PS(prescale); //Use ftmClockSource instead of 1
  961. }
  962. #ifdef FTM2_CH0_PIN
  963. else if (pin == FTM2_CH0_PIN || pin == FTM2_CH1_PIN) {
  964. FTM2_SC = 0;
  965. FTM2_CNT = 0;
  966. FTM2_MOD = mod;
  967. FTM2_SC = FTM_SC_CLKS(ftmClockSource) | FTM_SC_PS(prescale); //Use ftmClockSource instead of 1
  968. }
  969. #endif
  970. #ifdef FTM3_CH0_PIN
  971. else if (pin == FTM3_CH0_PIN || pin == FTM3_CH1_PIN
  972. || pin == FTM3_CH2_PIN || pin == FTM3_CH3_PIN
  973. || pin == FTM3_CH4_PIN || pin == FTM3_CH5_PIN
  974. || pin == FTM3_CH6_PIN || pin == FTM3_CH7_PIN) {
  975. FTM3_SC = 0;
  976. FTM3_CNT = 0;
  977. FTM3_MOD = mod;
  978. FTM3_SC = FTM_SC_CLKS(ftmClockSource) | FTM_SC_PS(prescale); //Use the new ftmClockSource instead of 1
  979. }
  980. #endif
  981. #ifdef TPM1_CH0_PIN
  982. else if (pin == TPM1_CH0_PIN || pin == TPM1_CH1_PIN) {
  983. TPM1_SC = 0;
  984. TPM1_CNT = 0;
  985. TPM1_MOD = mod;
  986. TPM1_SC = FTM_SC_CLKS(ftmClockSource) | FTM_SC_PS(prescale);
  987. }
  988. #endif
  989. }
  990. // TODO: startup code needs to initialize all pins to GPIO mode, input by default
  991. void digitalWrite(uint8_t pin, uint8_t val)
  992. {
  993. if (pin >= CORE_NUM_DIGITAL) return;
  994. #ifdef KINETISK
  995. if (*portModeRegister(pin)) {
  996. if (val) {
  997. *portSetRegister(pin) = 1;
  998. } else {
  999. *portClearRegister(pin) = 1;
  1000. }
  1001. #else
  1002. if (*portModeRegister(pin) & digitalPinToBitMask(pin)) {
  1003. if (val) {
  1004. *portSetRegister(pin) = digitalPinToBitMask(pin);
  1005. } else {
  1006. *portClearRegister(pin) = digitalPinToBitMask(pin);
  1007. }
  1008. #endif
  1009. } else {
  1010. volatile uint32_t *config = portConfigRegister(pin);
  1011. if (val) {
  1012. // TODO use bitband for atomic read-mod-write
  1013. *config |= (PORT_PCR_PE | PORT_PCR_PS);
  1014. //*config = PORT_PCR_MUX(1) | PORT_PCR_PE | PORT_PCR_PS;
  1015. } else {
  1016. // TODO use bitband for atomic read-mod-write
  1017. *config &= ~(PORT_PCR_PE);
  1018. //*config = PORT_PCR_MUX(1);
  1019. }
  1020. }
  1021. }
  1022. uint8_t digitalRead(uint8_t pin)
  1023. {
  1024. if (pin >= CORE_NUM_DIGITAL) return 0;
  1025. #ifdef KINETISK
  1026. return *portInputRegister(pin);
  1027. #else
  1028. return (*portInputRegister(pin) & digitalPinToBitMask(pin)) ? 1 : 0;
  1029. #endif
  1030. }
  1031. void pinMode(uint8_t pin, uint8_t mode)
  1032. {
  1033. volatile uint32_t *config;
  1034. if (pin >= CORE_NUM_DIGITAL) return;
  1035. config = portConfigRegister(pin);
  1036. if (mode == OUTPUT || mode == OUTPUT_OPENDRAIN) {
  1037. #ifdef KINETISK
  1038. *portModeRegister(pin) = 1;
  1039. #else
  1040. *portModeRegister(pin) |= digitalPinToBitMask(pin); // TODO: atomic
  1041. #endif
  1042. *config = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
  1043. if (mode == OUTPUT_OPENDRAIN) {
  1044. *config |= PORT_PCR_ODE;
  1045. } else {
  1046. *config &= ~PORT_PCR_ODE;
  1047. }
  1048. } else {
  1049. #ifdef KINETISK
  1050. *portModeRegister(pin) = 0;
  1051. #else
  1052. *portModeRegister(pin) &= ~digitalPinToBitMask(pin);
  1053. #endif
  1054. if (mode == INPUT || mode == INPUT_PULLUP || mode == INPUT_PULLDOWN) {
  1055. *config = PORT_PCR_MUX(1);
  1056. if (mode == INPUT_PULLUP) {
  1057. *config |= (PORT_PCR_PE | PORT_PCR_PS); // pullup
  1058. } else if (mode == INPUT_PULLDOWN) {
  1059. *config |= (PORT_PCR_PE); // pulldown
  1060. *config &= ~(PORT_PCR_PS);
  1061. }
  1062. } else {
  1063. *config = PORT_PCR_MUX(1) | PORT_PCR_PE | PORT_PCR_PS; // pullup
  1064. }
  1065. }
  1066. }
  1067. void _shiftOut(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder, uint8_t value)
  1068. {
  1069. if (bitOrder == LSBFIRST) {
  1070. shiftOut_lsbFirst(dataPin, clockPin, value);
  1071. } else {
  1072. shiftOut_msbFirst(dataPin, clockPin, value);
  1073. }
  1074. }
  1075. void shiftOut_lsbFirst(uint8_t dataPin, uint8_t clockPin, uint8_t value)
  1076. {
  1077. uint8_t mask;
  1078. for (mask=0x01; mask; mask <<= 1) {
  1079. digitalWrite(dataPin, value & mask);
  1080. digitalWrite(clockPin, HIGH);
  1081. digitalWrite(clockPin, LOW);
  1082. }
  1083. }
  1084. void shiftOut_msbFirst(uint8_t dataPin, uint8_t clockPin, uint8_t value)
  1085. {
  1086. uint8_t mask;
  1087. for (mask=0x80; mask; mask >>= 1) {
  1088. digitalWrite(dataPin, value & mask);
  1089. digitalWrite(clockPin, HIGH);
  1090. digitalWrite(clockPin, LOW);
  1091. }
  1092. }
  1093. uint8_t _shiftIn(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder)
  1094. {
  1095. if (bitOrder == LSBFIRST) {
  1096. return shiftIn_lsbFirst(dataPin, clockPin);
  1097. } else {
  1098. return shiftIn_msbFirst(dataPin, clockPin);
  1099. }
  1100. }
  1101. uint8_t shiftIn_lsbFirst(uint8_t dataPin, uint8_t clockPin)
  1102. {
  1103. uint8_t mask, value=0;
  1104. for (mask=0x01; mask; mask <<= 1) {
  1105. digitalWrite(clockPin, HIGH);
  1106. if (digitalRead(dataPin)) value |= mask;
  1107. digitalWrite(clockPin, LOW);
  1108. }
  1109. return value;
  1110. }
  1111. uint8_t shiftIn_msbFirst(uint8_t dataPin, uint8_t clockPin)
  1112. {
  1113. uint8_t mask, value=0;
  1114. for (mask=0x80; mask; mask >>= 1) {
  1115. digitalWrite(clockPin, HIGH);
  1116. if (digitalRead(dataPin)) value |= mask;
  1117. digitalWrite(clockPin, LOW);
  1118. }
  1119. return value;
  1120. }
  1121. // the systick interrupt is supposed to increment this at 1 kHz rate
  1122. volatile uint32_t systick_millis_count = 0;
  1123. //uint32_t systick_current, systick_count, systick_istatus; // testing only
  1124. uint32_t micros(void)
  1125. {
  1126. uint32_t count, current, istatus;
  1127. __disable_irq();
  1128. current = SYST_CVR;
  1129. count = systick_millis_count;
  1130. istatus = SCB_ICSR; // bit 26 indicates if systick exception pending
  1131. __enable_irq();
  1132. //systick_current = current;
  1133. //systick_count = count;
  1134. //systick_istatus = istatus & SCB_ICSR_PENDSTSET ? 1 : 0;
  1135. if ((istatus & SCB_ICSR_PENDSTSET) && current > 50) count++;
  1136. current = ((F_CPU / 1000) - 1) - current;
  1137. #if defined(KINETISL) && F_CPU == 48000000
  1138. return count * 1000 + ((current * (uint32_t)87381) >> 22);
  1139. #elif defined(KINETISL) && F_CPU == 24000000
  1140. return count * 1000 + ((current * (uint32_t)174763) >> 22);
  1141. #endif
  1142. return count * 1000 + current / (F_CPU / 1000000);
  1143. }
  1144. void delay(uint32_t ms)
  1145. {
  1146. uint32_t start = micros();
  1147. if (ms > 0) {
  1148. while (1) {
  1149. while ((micros() - start) >= 1000) {
  1150. ms--;
  1151. if (ms == 0) return;
  1152. start += 1000;
  1153. }
  1154. yield();
  1155. }
  1156. }
  1157. }
  1158. // TODO: verify these result in correct timeouts...
  1159. #if F_CPU == 240000000
  1160. #define PULSEIN_LOOPS_PER_USEC 33
  1161. #elif F_CPU == 216000000
  1162. #define PULSEIN_LOOPS_PER_USEC 31
  1163. #elif F_CPU == 192000000
  1164. #define PULSEIN_LOOPS_PER_USEC 29
  1165. #elif F_CPU == 180000000
  1166. #define PULSEIN_LOOPS_PER_USEC 27
  1167. #elif F_CPU == 168000000
  1168. #define PULSEIN_LOOPS_PER_USEC 25
  1169. #elif F_CPU == 144000000
  1170. #define PULSEIN_LOOPS_PER_USEC 21
  1171. #elif F_CPU == 120000000
  1172. #define PULSEIN_LOOPS_PER_USEC 18
  1173. #elif F_CPU == 96000000
  1174. #define PULSEIN_LOOPS_PER_USEC 14
  1175. #elif F_CPU == 72000000
  1176. #define PULSEIN_LOOPS_PER_USEC 10
  1177. #elif F_CPU == 48000000
  1178. #define PULSEIN_LOOPS_PER_USEC 7
  1179. #elif F_CPU == 24000000
  1180. #define PULSEIN_LOOPS_PER_USEC 4
  1181. #elif F_CPU == 16000000
  1182. #define PULSEIN_LOOPS_PER_USEC 1
  1183. #elif F_CPU == 8000000
  1184. #define PULSEIN_LOOPS_PER_USEC 1
  1185. #elif F_CPU == 4000000
  1186. #define PULSEIN_LOOPS_PER_USEC 1
  1187. #elif F_CPU == 2000000
  1188. #define PULSEIN_LOOPS_PER_USEC 1
  1189. #endif
  1190. #if defined(KINETISK)
  1191. uint32_t pulseIn_high(volatile uint8_t *reg, uint32_t timeout)
  1192. {
  1193. uint32_t timeout_count = timeout * PULSEIN_LOOPS_PER_USEC;
  1194. uint32_t usec_start, usec_stop;
  1195. // wait for any previous pulse to end
  1196. while (*reg) {
  1197. if (--timeout_count == 0) return 0;
  1198. }
  1199. // wait for the pulse to start
  1200. while (!*reg) {
  1201. if (--timeout_count == 0) return 0;
  1202. }
  1203. usec_start = micros();
  1204. // wait for the pulse to stop
  1205. while (*reg) {
  1206. if (--timeout_count == 0) return 0;
  1207. }
  1208. usec_stop = micros();
  1209. return usec_stop - usec_start;
  1210. }
  1211. uint32_t pulseIn_low(volatile uint8_t *reg, uint32_t timeout)
  1212. {
  1213. uint32_t timeout_count = timeout * PULSEIN_LOOPS_PER_USEC;
  1214. uint32_t usec_start, usec_stop;
  1215. // wait for any previous pulse to end
  1216. while (!*reg) {
  1217. if (--timeout_count == 0) return 0;
  1218. }
  1219. // wait for the pulse to start
  1220. while (*reg) {
  1221. if (--timeout_count == 0) return 0;
  1222. }
  1223. usec_start = micros();
  1224. // wait for the pulse to stop
  1225. while (!*reg) {
  1226. if (--timeout_count == 0) return 0;
  1227. }
  1228. usec_stop = micros();
  1229. return usec_stop - usec_start;
  1230. }
  1231. // TODO: an inline version should handle the common case where state is const
  1232. uint32_t pulseIn(uint8_t pin, uint8_t state, uint32_t timeout)
  1233. {
  1234. if (pin >= CORE_NUM_DIGITAL) return 0;
  1235. if (state) return pulseIn_high(portInputRegister(pin), timeout);
  1236. return pulseIn_low(portInputRegister(pin), timeout);;
  1237. }
  1238. #elif defined(KINETISL)
  1239. // For TeencyLC need to use mask on the input register as the register is shared by several IO pins
  1240. uint32_t pulseIn_high(volatile uint8_t *reg, uint8_t mask, uint32_t timeout)
  1241. {
  1242. uint32_t timeout_count = timeout * PULSEIN_LOOPS_PER_USEC;
  1243. uint32_t usec_start, usec_stop;
  1244. // wait for any previous pulse to end
  1245. while (*reg & mask) {
  1246. if (--timeout_count == 0) return -1;
  1247. }
  1248. // wait for the pulse to start
  1249. while (!(*reg & mask)) {
  1250. if (--timeout_count == 0) return 0;
  1251. }
  1252. usec_start = micros();
  1253. // wait for the pulse to stop
  1254. while (*reg & mask) {
  1255. if (--timeout_count == 0) return 0;
  1256. }
  1257. usec_stop = micros();
  1258. return usec_stop - usec_start;
  1259. }
  1260. uint32_t pulseIn_low(volatile uint8_t *reg, uint8_t mask, uint32_t timeout)
  1261. {
  1262. uint32_t timeout_count = timeout * PULSEIN_LOOPS_PER_USEC;
  1263. uint32_t usec_start, usec_stop;
  1264. // wait for any previous pulse to end
  1265. while (!(*reg & mask)) {
  1266. if (--timeout_count == 0) return 0;
  1267. }
  1268. // wait for the pulse to start
  1269. while (*reg & mask) {
  1270. if (--timeout_count == 0) return 0;
  1271. }
  1272. usec_start = micros();
  1273. // wait for the pulse to stop
  1274. while (!(*reg & mask)) {
  1275. if (--timeout_count == 0) return 0;
  1276. }
  1277. usec_stop = micros();
  1278. return usec_stop - usec_start;
  1279. }
  1280. // TODO: an inline version should handle the common case where state is const
  1281. uint32_t pulseIn(uint8_t pin, uint8_t state, uint32_t timeout)
  1282. {
  1283. if (pin >= CORE_NUM_DIGITAL) return 0;
  1284. if (state) return pulseIn_high(portInputRegister(pin), digitalPinToBitMask(pin), timeout);
  1285. return pulseIn_low(portInputRegister(pin), digitalPinToBitMask(pin), timeout);;
  1286. }
  1287. #endif