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 години
преди 7 години
преди 11 години
преди 7 години
преди 11 години
преди 7 години
преди 11 години
преди 10 години
преди 11 години
преди 9 години
преди 8 години
преди 11 години
преди 10 години
преди 11 години
преди 10 години
преди 11 години
преди 11 години
преди 10 години
преди 10 години
преди 10 години
преди 11 години
преди 10 години
преди 11 години
преди 10 години
преди 10 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 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 години
преди 11 години
преди 9 години
преди 10 години
преди 11 години
преди 9 години
преди 11 години
преди 9 години
преди 9 години
преди 9 години
преди 10 години
преди 11 години
преди 9 години
преди 11 години
преди 9 години
преди 9 години
преди 9 години
преди 11 години
преди 11 години
преди 6 години
преди 11 години
преди 6 години
преди 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 години
преди 11 години
преди 11 години
преди 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 години
преди 8 години
преди 11 години
преди 8 години
преди 11 години
преди 11 години
преди 9 години
преди 11 години
преди 10 години
преди 8 години
преди 9 години
преди 10 години
преди 11 години
преди 10 години
преди 11 години
преди 10 години
преди 10 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346
  1. /* Teensyduino Core Library
  2. * http://www.pjrc.com/teensy/
  3. * Copyright (c) 2017 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. {GPIO_BITBAND_PTR(CORE_PIN58_PORTREG, CORE_PIN58_BIT), &CORE_PIN58_CONFIG},
  99. {GPIO_BITBAND_PTR(CORE_PIN59_PORTREG, CORE_PIN59_BIT), &CORE_PIN59_CONFIG},
  100. {GPIO_BITBAND_PTR(CORE_PIN60_PORTREG, CORE_PIN60_BIT), &CORE_PIN60_CONFIG},
  101. {GPIO_BITBAND_PTR(CORE_PIN61_PORTREG, CORE_PIN61_BIT), &CORE_PIN61_CONFIG},
  102. {GPIO_BITBAND_PTR(CORE_PIN62_PORTREG, CORE_PIN62_BIT), &CORE_PIN62_CONFIG},
  103. {GPIO_BITBAND_PTR(CORE_PIN63_PORTREG, CORE_PIN63_BIT), &CORE_PIN63_CONFIG},
  104. #endif
  105. };
  106. #elif defined(KINETISL)
  107. const struct digital_pin_bitband_and_config_table_struct digital_pin_to_info_PGM[] = {
  108. {((volatile uint8_t *)&CORE_PIN0_PORTREG + (CORE_PIN0_BIT >> 3)), &CORE_PIN0_CONFIG, (1<<(CORE_PIN0_BIT & 7))},
  109. {((volatile uint8_t *)&CORE_PIN1_PORTREG + (CORE_PIN1_BIT >> 3)), &CORE_PIN1_CONFIG, (1<<(CORE_PIN1_BIT & 7))},
  110. {((volatile uint8_t *)&CORE_PIN2_PORTREG + (CORE_PIN2_BIT >> 3)), &CORE_PIN2_CONFIG, (1<<(CORE_PIN2_BIT & 7))},
  111. {((volatile uint8_t *)&CORE_PIN3_PORTREG + (CORE_PIN3_BIT >> 3)), &CORE_PIN3_CONFIG, (1<<(CORE_PIN3_BIT & 7))},
  112. {((volatile uint8_t *)&CORE_PIN4_PORTREG + (CORE_PIN4_BIT >> 3)), &CORE_PIN4_CONFIG, (1<<(CORE_PIN4_BIT & 7))},
  113. {((volatile uint8_t *)&CORE_PIN5_PORTREG + (CORE_PIN5_BIT >> 3)), &CORE_PIN5_CONFIG, (1<<(CORE_PIN5_BIT & 7))},
  114. {((volatile uint8_t *)&CORE_PIN6_PORTREG + (CORE_PIN6_BIT >> 3)), &CORE_PIN6_CONFIG, (1<<(CORE_PIN6_BIT & 7))},
  115. {((volatile uint8_t *)&CORE_PIN7_PORTREG + (CORE_PIN7_BIT >> 3)), &CORE_PIN7_CONFIG, (1<<(CORE_PIN7_BIT & 7))},
  116. {((volatile uint8_t *)&CORE_PIN8_PORTREG + (CORE_PIN8_BIT >> 3)), &CORE_PIN8_CONFIG, (1<<(CORE_PIN8_BIT & 7))},
  117. {((volatile uint8_t *)&CORE_PIN9_PORTREG + (CORE_PIN9_BIT >> 3)), &CORE_PIN9_CONFIG, (1<<(CORE_PIN9_BIT & 7))},
  118. {((volatile uint8_t *)&CORE_PIN10_PORTREG + (CORE_PIN10_BIT >> 3)), &CORE_PIN10_CONFIG, (1<<(CORE_PIN10_BIT & 7))},
  119. {((volatile uint8_t *)&CORE_PIN11_PORTREG + (CORE_PIN11_BIT >> 3)), &CORE_PIN11_CONFIG, (1<<(CORE_PIN11_BIT & 7))},
  120. {((volatile uint8_t *)&CORE_PIN12_PORTREG + (CORE_PIN12_BIT >> 3)), &CORE_PIN12_CONFIG, (1<<(CORE_PIN12_BIT & 7))},
  121. {((volatile uint8_t *)&CORE_PIN13_PORTREG + (CORE_PIN13_BIT >> 3)), &CORE_PIN13_CONFIG, (1<<(CORE_PIN13_BIT & 7))},
  122. {((volatile uint8_t *)&CORE_PIN14_PORTREG + (CORE_PIN14_BIT >> 3)), &CORE_PIN14_CONFIG, (1<<(CORE_PIN14_BIT & 7))},
  123. {((volatile uint8_t *)&CORE_PIN15_PORTREG + (CORE_PIN15_BIT >> 3)), &CORE_PIN15_CONFIG, (1<<(CORE_PIN15_BIT & 7))},
  124. {((volatile uint8_t *)&CORE_PIN16_PORTREG + (CORE_PIN16_BIT >> 3)), &CORE_PIN16_CONFIG, (1<<(CORE_PIN16_BIT & 7))},
  125. {((volatile uint8_t *)&CORE_PIN17_PORTREG + (CORE_PIN17_BIT >> 3)), &CORE_PIN17_CONFIG, (1<<(CORE_PIN17_BIT & 7))},
  126. {((volatile uint8_t *)&CORE_PIN18_PORTREG + (CORE_PIN18_BIT >> 3)), &CORE_PIN18_CONFIG, (1<<(CORE_PIN18_BIT & 7))},
  127. {((volatile uint8_t *)&CORE_PIN19_PORTREG + (CORE_PIN19_BIT >> 3)), &CORE_PIN19_CONFIG, (1<<(CORE_PIN19_BIT & 7))},
  128. {((volatile uint8_t *)&CORE_PIN20_PORTREG + (CORE_PIN20_BIT >> 3)), &CORE_PIN20_CONFIG, (1<<(CORE_PIN20_BIT & 7))},
  129. {((volatile uint8_t *)&CORE_PIN21_PORTREG + (CORE_PIN21_BIT >> 3)), &CORE_PIN21_CONFIG, (1<<(CORE_PIN21_BIT & 7))},
  130. {((volatile uint8_t *)&CORE_PIN22_PORTREG + (CORE_PIN22_BIT >> 3)), &CORE_PIN22_CONFIG, (1<<(CORE_PIN22_BIT & 7))},
  131. {((volatile uint8_t *)&CORE_PIN23_PORTREG + (CORE_PIN23_BIT >> 3)), &CORE_PIN23_CONFIG, (1<<(CORE_PIN23_BIT & 7))},
  132. {((volatile uint8_t *)&CORE_PIN24_PORTREG + (CORE_PIN24_BIT >> 3)), &CORE_PIN24_CONFIG, (1<<(CORE_PIN24_BIT & 7))},
  133. {((volatile uint8_t *)&CORE_PIN25_PORTREG + (CORE_PIN25_BIT >> 3)), &CORE_PIN25_CONFIG, (1<<(CORE_PIN25_BIT & 7))},
  134. {((volatile uint8_t *)&CORE_PIN26_PORTREG + (CORE_PIN26_BIT >> 3)), &CORE_PIN26_CONFIG, (1<<(CORE_PIN26_BIT & 7))}
  135. };
  136. #endif
  137. static void dummy_isr() {};
  138. typedef void (*voidFuncPtr)(void);
  139. #if defined(KINETISK)
  140. #ifdef NO_PORT_ISR_FASTRUN
  141. static void port_A_isr(void);
  142. static void port_B_isr(void);
  143. static void port_C_isr(void);
  144. static void port_D_isr(void);
  145. static void port_E_isr(void);
  146. #else
  147. static void port_A_isr(void) __attribute__ ((section(".fastrun"), noinline, noclone ));
  148. static void port_B_isr(void) __attribute__ ((section(".fastrun"), noinline, noclone ));
  149. static void port_C_isr(void) __attribute__ ((section(".fastrun"), noinline, noclone ));
  150. static void port_D_isr(void) __attribute__ ((section(".fastrun"), noinline, noclone ));
  151. static void port_E_isr(void) __attribute__ ((section(".fastrun"), noinline, noclone ));
  152. #endif
  153. voidFuncPtr isr_table_portA[CORE_MAX_PIN_PORTA+1] = { [0 ... CORE_MAX_PIN_PORTA] = dummy_isr };
  154. voidFuncPtr isr_table_portB[CORE_MAX_PIN_PORTB+1] = { [0 ... CORE_MAX_PIN_PORTB] = dummy_isr };
  155. voidFuncPtr isr_table_portC[CORE_MAX_PIN_PORTC+1] = { [0 ... CORE_MAX_PIN_PORTC] = dummy_isr };
  156. voidFuncPtr isr_table_portD[CORE_MAX_PIN_PORTD+1] = { [0 ... CORE_MAX_PIN_PORTD] = dummy_isr };
  157. voidFuncPtr isr_table_portE[CORE_MAX_PIN_PORTE+1] = { [0 ... CORE_MAX_PIN_PORTE] = dummy_isr };
  158. // The Pin Config Register is used to look up the correct interrupt table
  159. // for the corresponding port.
  160. inline voidFuncPtr* getIsrTable(volatile uint32_t *config) {
  161. voidFuncPtr* isr_table = NULL;
  162. if(&PORTA_PCR0 <= config && config <= &PORTA_PCR31) isr_table = isr_table_portA;
  163. else if(&PORTB_PCR0 <= config && config <= &PORTB_PCR31) isr_table = isr_table_portB;
  164. else if(&PORTC_PCR0 <= config && config <= &PORTC_PCR31) isr_table = isr_table_portC;
  165. else if(&PORTD_PCR0 <= config && config <= &PORTD_PCR31) isr_table = isr_table_portD;
  166. else if(&PORTE_PCR0 <= config && config <= &PORTE_PCR31) isr_table = isr_table_portE;
  167. return isr_table;
  168. }
  169. inline uint32_t getPinIndex(volatile uint32_t *config) {
  170. uintptr_t v = (uintptr_t) config;
  171. // There are 32 pin config registers for each port, each port starting at a round address.
  172. // They are spaced 4 bytes apart.
  173. return (v % 128) / 4;
  174. }
  175. #elif defined(KINETISL)
  176. volatile static voidFuncPtr intFunc[CORE_NUM_DIGITAL] = { [0 ... CORE_NUM_DIGITAL-1] = dummy_isr };
  177. static void porta_interrupt(void);
  178. static void portcd_interrupt(void);
  179. #endif
  180. void attachInterruptVector(enum IRQ_NUMBER_t irq, void (*function)(void))
  181. {
  182. _VectorsRam[irq + 16] = function;
  183. }
  184. void attachInterrupt(uint8_t pin, void (*function)(void), int mode)
  185. {
  186. volatile uint32_t *config;
  187. uint32_t cfg, mask;
  188. if (pin >= CORE_NUM_DIGITAL) return;
  189. switch (mode) {
  190. case CHANGE: mask = 0x0B; break;
  191. case RISING: mask = 0x09; break;
  192. case FALLING: mask = 0x0A; break;
  193. case LOW: mask = 0x08; break;
  194. case HIGH: mask = 0x0C; break;
  195. default: return;
  196. }
  197. mask = (mask << 16) | 0x01000000;
  198. config = portConfigRegister(pin);
  199. if ((*config & 0x00000700) == 0) {
  200. // for compatibility with programs which depend
  201. // on AVR hardware default to input mode.
  202. pinMode(pin, INPUT);
  203. }
  204. #if defined(KINETISK)
  205. attachInterruptVector(IRQ_PORTA, port_A_isr);
  206. attachInterruptVector(IRQ_PORTB, port_B_isr);
  207. attachInterruptVector(IRQ_PORTC, port_C_isr);
  208. attachInterruptVector(IRQ_PORTD, port_D_isr);
  209. attachInterruptVector(IRQ_PORTE, port_E_isr);
  210. voidFuncPtr* isr_table = getIsrTable(config);
  211. if(!isr_table) return;
  212. uint32_t pin_index = getPinIndex(config);
  213. __disable_irq();
  214. cfg = *config;
  215. cfg &= ~0x000F0000; // disable any previous interrupt
  216. *config = cfg;
  217. isr_table[pin_index] = function; // set the function pointer
  218. cfg |= mask;
  219. *config = cfg; // enable the new interrupt
  220. __enable_irq();
  221. #elif defined(KINETISL)
  222. attachInterruptVector(IRQ_PORTA, porta_interrupt);
  223. attachInterruptVector(IRQ_PORTCD, portcd_interrupt);
  224. __disable_irq();
  225. cfg = *config;
  226. cfg &= ~0x000F0000; // disable any previous interrupt
  227. *config = cfg;
  228. intFunc[pin] = function; // set the function pointer
  229. cfg |= mask;
  230. *config = cfg; // enable the new interrupt
  231. __enable_irq();
  232. #endif
  233. }
  234. void detachInterrupt(uint8_t pin)
  235. {
  236. volatile uint32_t *config;
  237. config = portConfigRegister(pin);
  238. #if defined(KINETISK)
  239. voidFuncPtr* isr_table = getIsrTable(config);
  240. if(!isr_table) return;
  241. uint32_t pin_index = getPinIndex(config);
  242. __disable_irq();
  243. *config = ((*config & ~0x000F0000) | 0x01000000);
  244. isr_table[pin_index] = dummy_isr;
  245. __enable_irq();
  246. #elif defined(KINETISL)
  247. __disable_irq();
  248. *config = ((*config & ~0x000F0000) | 0x01000000);
  249. intFunc[pin] = dummy_isr;
  250. __enable_irq();
  251. #endif
  252. }
  253. typedef void (*voidFuncPtr)(void);
  254. // Using CTZ instead of CLZ is faster, since it allows more efficient bit
  255. // clearing and fast indexing into the pin ISR table.
  256. #define PORT_ISR_FUNCTION_CLZ(port_name) \
  257. static void port_ ## port_name ## _isr(void) { \
  258. uint32_t isfr = PORT ## port_name ##_ISFR; \
  259. PORT ## port_name ##_ISFR = isfr; \
  260. voidFuncPtr* isr_table = isr_table_port ## port_name; \
  261. uint32_t bit_nr; \
  262. while(isfr) { \
  263. bit_nr = __builtin_ctz(isfr); \
  264. isr_table[bit_nr](); \
  265. isfr = isfr & (isfr-1); \
  266. if(!isfr) return; \
  267. } \
  268. }
  269. // END PORT_ISR_FUNCTION_CLZ
  270. #if defined(KINETISK)
  271. PORT_ISR_FUNCTION_CLZ(A)
  272. PORT_ISR_FUNCTION_CLZ(B)
  273. PORT_ISR_FUNCTION_CLZ(C)
  274. PORT_ISR_FUNCTION_CLZ(D)
  275. PORT_ISR_FUNCTION_CLZ(E)
  276. #elif defined(KINETISL)
  277. // Kinetis L (Teensy LC) is based on Cortex M0 and doesn't have hardware
  278. // support for CLZ.
  279. #define DISPATCH_PIN_ISR(pin_nr) { voidFuncPtr pin_isr = intFunc[pin_nr]; \
  280. if(isfr & CORE_PIN ## pin_nr ## _BITMASK) pin_isr(); }
  281. static void porta_interrupt(void)
  282. {
  283. uint32_t isfr = PORTA_ISFR;
  284. PORTA_ISFR = isfr;
  285. DISPATCH_PIN_ISR(3);
  286. DISPATCH_PIN_ISR(4);
  287. }
  288. static void portcd_interrupt(void)
  289. {
  290. uint32_t isfr = PORTC_ISFR;
  291. PORTC_ISFR = isfr;
  292. DISPATCH_PIN_ISR(9);
  293. DISPATCH_PIN_ISR(10);
  294. DISPATCH_PIN_ISR(11);
  295. DISPATCH_PIN_ISR(12);
  296. DISPATCH_PIN_ISR(13);
  297. DISPATCH_PIN_ISR(15);
  298. DISPATCH_PIN_ISR(22);
  299. DISPATCH_PIN_ISR(23);
  300. isfr = PORTD_ISFR;
  301. PORTD_ISFR = isfr;
  302. DISPATCH_PIN_ISR(2);
  303. DISPATCH_PIN_ISR(5);
  304. DISPATCH_PIN_ISR(6);
  305. DISPATCH_PIN_ISR(7);
  306. DISPATCH_PIN_ISR(8);
  307. DISPATCH_PIN_ISR(14);
  308. DISPATCH_PIN_ISR(20);
  309. DISPATCH_PIN_ISR(21);
  310. }
  311. #undef DISPATCH_PIN_ISR
  312. #endif
  313. #if defined(__MK20DX128__) || defined(__MK20DX256__) || defined(__MK64FX512__) || defined(__MK66FX1M0__)
  314. unsigned long rtc_get(void)
  315. {
  316. return RTC_TSR;
  317. }
  318. void rtc_set(unsigned long t)
  319. {
  320. RTC_SR = 0;
  321. RTC_TPR = 0;
  322. RTC_TSR = t;
  323. RTC_SR = RTC_SR_TCE;
  324. }
  325. // adjust is the amount of crystal error to compensate, 1 = 0.1192 ppm
  326. // For example, adjust = -100 is slows the clock by 11.92 ppm
  327. //
  328. void rtc_compensate(int adjust)
  329. {
  330. uint32_t comp, interval, tcr;
  331. // This simple approach tries to maximize the interval.
  332. // Perhaps minimizing TCR would be better, so the
  333. // compensation is distributed more evenly across
  334. // many seconds, rather than saving it all up and then
  335. // altering one second up to +/- 0.38%
  336. if (adjust >= 0) {
  337. comp = adjust;
  338. interval = 256;
  339. while (1) {
  340. tcr = comp * interval;
  341. if (tcr < 128*256) break;
  342. if (--interval == 1) break;
  343. }
  344. tcr = tcr >> 8;
  345. } else {
  346. comp = -adjust;
  347. interval = 256;
  348. while (1) {
  349. tcr = comp * interval;
  350. if (tcr < 129*256) break;
  351. if (--interval == 1) break;
  352. }
  353. tcr = tcr >> 8;
  354. tcr = 256 - tcr;
  355. }
  356. RTC_TCR = ((interval - 1) << 8) | tcr;
  357. }
  358. #else
  359. unsigned long rtc_get(void) { return 0; }
  360. void rtc_set(unsigned long t) { }
  361. void rtc_compensate(int adjust) { }
  362. #endif
  363. #if 0
  364. // TODO: build system should define this
  365. // so RTC is automatically initialized to approx correct time
  366. // at least when the program begins running right after upload
  367. #ifndef TIME_T
  368. #define TIME_T 1350160272
  369. #endif
  370. void init_rtc(void)
  371. {
  372. serial_print("init_rtc\n");
  373. //SIM_SCGC6 |= SIM_SCGC6_RTC;
  374. // enable the RTC crystal oscillator, for approx 12pf crystal
  375. if (!(RTC_CR & RTC_CR_OSCE)) {
  376. serial_print("start RTC oscillator\n");
  377. RTC_SR = 0;
  378. RTC_CR = RTC_CR_SC16P | RTC_CR_SC4P | RTC_CR_OSCE;
  379. }
  380. // should wait for crystal to stabilize.....
  381. serial_print("SR=");
  382. serial_phex32(RTC_SR);
  383. serial_print("\n");
  384. serial_print("CR=");
  385. serial_phex32(RTC_CR);
  386. serial_print("\n");
  387. serial_print("TSR=");
  388. serial_phex32(RTC_TSR);
  389. serial_print("\n");
  390. serial_print("TCR=");
  391. serial_phex32(RTC_TCR);
  392. serial_print("\n");
  393. if (RTC_SR & RTC_SR_TIF) {
  394. // enable the RTC
  395. RTC_SR = 0;
  396. RTC_TPR = 0;
  397. RTC_TSR = TIME_T;
  398. RTC_SR = RTC_SR_TCE;
  399. }
  400. }
  401. #endif
  402. extern void usb_init(void);
  403. // create a default PWM at the same 488.28 Hz as Arduino Uno
  404. #if defined(KINETISK)
  405. #define F_TIMER F_BUS
  406. #elif defined(KINETISL)
  407. #if F_CPU > 16000000
  408. #define F_TIMER (F_PLL/2)
  409. #else
  410. #define F_TIMER (F_PLL)
  411. #endif//Low Power
  412. #endif
  413. #if F_TIMER == 120000000
  414. #define DEFAULT_FTM_MOD (61440 - 1)
  415. #define DEFAULT_FTM_PRESCALE 2
  416. #elif F_TIMER == 108000000
  417. #define DEFAULT_FTM_MOD (55296 - 1)
  418. #define DEFAULT_FTM_PRESCALE 2
  419. #elif F_TIMER == 96000000
  420. #define DEFAULT_FTM_MOD (49152 - 1)
  421. #define DEFAULT_FTM_PRESCALE 2
  422. #elif F_TIMER == 90000000
  423. #define DEFAULT_FTM_MOD (46080 - 1)
  424. #define DEFAULT_FTM_PRESCALE 2
  425. #elif F_TIMER == 80000000
  426. #define DEFAULT_FTM_MOD (40960 - 1)
  427. #define DEFAULT_FTM_PRESCALE 2
  428. #elif F_TIMER == 72000000
  429. #define DEFAULT_FTM_MOD (36864 - 1)
  430. #define DEFAULT_FTM_PRESCALE 2
  431. #elif F_TIMER == 64000000
  432. #define DEFAULT_FTM_MOD (65536 - 1)
  433. #define DEFAULT_FTM_PRESCALE 1
  434. #elif F_TIMER == 60000000
  435. #define DEFAULT_FTM_MOD (61440 - 1)
  436. #define DEFAULT_FTM_PRESCALE 1
  437. #elif F_TIMER == 56000000
  438. #define DEFAULT_FTM_MOD (57344 - 1)
  439. #define DEFAULT_FTM_PRESCALE 1
  440. #elif F_TIMER == 54000000
  441. #define DEFAULT_FTM_MOD (55296 - 1)
  442. #define DEFAULT_FTM_PRESCALE 1
  443. #elif F_TIMER == 48000000
  444. #define DEFAULT_FTM_MOD (49152 - 1)
  445. #define DEFAULT_FTM_PRESCALE 1
  446. #elif F_TIMER == 40000000
  447. #define DEFAULT_FTM_MOD (40960 - 1)
  448. #define DEFAULT_FTM_PRESCALE 1
  449. #elif F_TIMER == 36000000
  450. #define DEFAULT_FTM_MOD (36864 - 1)
  451. #define DEFAULT_FTM_PRESCALE 1
  452. #elif F_TIMER == 24000000
  453. #define DEFAULT_FTM_MOD (49152 - 1)
  454. #define DEFAULT_FTM_PRESCALE 0
  455. #elif F_TIMER == 16000000
  456. #define DEFAULT_FTM_MOD (32768 - 1)
  457. #define DEFAULT_FTM_PRESCALE 0
  458. #elif F_TIMER == 8000000
  459. #define DEFAULT_FTM_MOD (16384 - 1)
  460. #define DEFAULT_FTM_PRESCALE 0
  461. #elif F_TIMER == 4000000
  462. #define DEFAULT_FTM_MOD (8192 - 1)
  463. #define DEFAULT_FTM_PRESCALE 0
  464. #elif F_TIMER == 2000000
  465. #define DEFAULT_FTM_MOD (4096 - 1)
  466. #define DEFAULT_FTM_PRESCALE 0
  467. #endif
  468. //void init_pins(void)
  469. __attribute__((noinline))
  470. void _init_Teensyduino_internal_(void)
  471. {
  472. #if defined(__MK20DX128__) || defined(__MK20DX256__) || defined(__MK64FX512__) || defined(__MK66FX1M0__)
  473. NVIC_ENABLE_IRQ(IRQ_PORTA);
  474. NVIC_ENABLE_IRQ(IRQ_PORTB);
  475. NVIC_ENABLE_IRQ(IRQ_PORTC);
  476. NVIC_ENABLE_IRQ(IRQ_PORTD);
  477. NVIC_ENABLE_IRQ(IRQ_PORTE);
  478. #elif defined(__MKL26Z64__)
  479. NVIC_ENABLE_IRQ(IRQ_PORTA);
  480. NVIC_ENABLE_IRQ(IRQ_PORTCD);
  481. #endif
  482. //SIM_SCGC6 |= SIM_SCGC6_FTM0; // TODO: use bitband for atomic read-mod-write
  483. //SIM_SCGC6 |= SIM_SCGC6_FTM1;
  484. FTM0_CNT = 0;
  485. FTM0_MOD = DEFAULT_FTM_MOD;
  486. FTM0_C0SC = 0x28; // MSnB:MSnA = 10, ELSnB:ELSnA = 10
  487. FTM0_C1SC = 0x28;
  488. FTM0_C2SC = 0x28;
  489. FTM0_C3SC = 0x28;
  490. FTM0_C4SC = 0x28;
  491. FTM0_C5SC = 0x28;
  492. #if defined(__MK20DX128__) || defined(__MK20DX256__) || defined(__MK64FX512__) || defined(__MK66FX1M0__)
  493. FTM0_C6SC = 0x28;
  494. FTM0_C7SC = 0x28;
  495. #endif
  496. #if defined(__MK64FX512__) || defined(__MK66FX1M0__)
  497. FTM3_C0SC = 0x28;
  498. FTM3_C1SC = 0x28;
  499. FTM3_C2SC = 0x28;
  500. FTM3_C3SC = 0x28;
  501. FTM3_C4SC = 0x28;
  502. FTM3_C5SC = 0x28;
  503. FTM3_C6SC = 0x28;
  504. FTM3_C7SC = 0x28;
  505. #endif
  506. FTM0_SC = FTM_SC_CLKS(1) | FTM_SC_PS(DEFAULT_FTM_PRESCALE);
  507. FTM1_CNT = 0;
  508. FTM1_MOD = DEFAULT_FTM_MOD;
  509. FTM1_C0SC = 0x28;
  510. FTM1_C1SC = 0x28;
  511. FTM1_SC = FTM_SC_CLKS(1) | FTM_SC_PS(DEFAULT_FTM_PRESCALE);
  512. #if defined(__MK20DX256__) || defined(__MK64FX512__) || defined(__MK66FX1M0__) || defined(__MKL26Z64__)
  513. FTM2_CNT = 0;
  514. FTM2_MOD = DEFAULT_FTM_MOD;
  515. FTM2_C0SC = 0x28;
  516. FTM2_C1SC = 0x28;
  517. FTM2_SC = FTM_SC_CLKS(1) | FTM_SC_PS(DEFAULT_FTM_PRESCALE);
  518. #endif
  519. #if defined(__MK64FX512__) || defined(__MK66FX1M0__)
  520. FTM3_CNT = 0;
  521. FTM3_MOD = DEFAULT_FTM_MOD;
  522. FTM3_C0SC = 0x28;
  523. FTM3_C1SC = 0x28;
  524. FTM3_SC = FTM_SC_CLKS(1) | FTM_SC_PS(DEFAULT_FTM_PRESCALE);
  525. #endif
  526. #if defined(__MK66FX1M0__)
  527. SIM_SCGC2 |= SIM_SCGC2_TPM1;
  528. SIM_SOPT2 |= SIM_SOPT2_TPMSRC(2);
  529. TPM1_CNT = 0;
  530. TPM1_MOD = 32767;
  531. TPM1_C0SC = 0x28;
  532. TPM1_C1SC = 0x28;
  533. TPM1_SC = FTM_SC_CLKS(1) | FTM_SC_PS(0);
  534. #endif
  535. analog_init();
  536. // for background about this startup delay, please see these conversations
  537. // https://forum.pjrc.com/threads/36606-startup-time-(400ms)?p=113980&viewfull=1#post113980
  538. // https://forum.pjrc.com/threads/31290-Teensey-3-2-Teensey-Loader-1-24-Issues?p=87273&viewfull=1#post87273
  539. #if TEENSYDUINO >= 142
  540. delay(25);
  541. usb_init();
  542. delay(275);
  543. #else
  544. delay(50);
  545. usb_init();
  546. delay(350);
  547. #endif
  548. }
  549. #if defined(__MK20DX128__)
  550. #define FTM0_CH0_PIN 22
  551. #define FTM0_CH1_PIN 23
  552. #define FTM0_CH2_PIN 9
  553. #define FTM0_CH3_PIN 10
  554. #define FTM0_CH4_PIN 6
  555. #define FTM0_CH5_PIN 20
  556. #define FTM0_CH6_PIN 21
  557. #define FTM0_CH7_PIN 5
  558. #define FTM1_CH0_PIN 3
  559. #define FTM1_CH1_PIN 4
  560. #elif defined(__MK20DX256__)
  561. #define FTM0_CH0_PIN 22
  562. #define FTM0_CH1_PIN 23
  563. #define FTM0_CH2_PIN 9
  564. #define FTM0_CH3_PIN 10
  565. #define FTM0_CH4_PIN 6
  566. #define FTM0_CH5_PIN 20
  567. #define FTM0_CH6_PIN 21
  568. #define FTM0_CH7_PIN 5
  569. #define FTM1_CH0_PIN 3
  570. #define FTM1_CH1_PIN 4
  571. #define FTM2_CH0_PIN 32
  572. #define FTM2_CH1_PIN 25
  573. #elif defined(__MKL26Z64__)
  574. #define FTM0_CH0_PIN 22
  575. #define FTM0_CH1_PIN 23
  576. #define FTM0_CH2_PIN 9
  577. #define FTM0_CH3_PIN 10
  578. #define FTM0_CH4_PIN 6
  579. #define FTM0_CH5_PIN 20
  580. #define FTM1_CH0_PIN 16
  581. #define FTM1_CH1_PIN 17
  582. #define FTM2_CH0_PIN 3
  583. #define FTM2_CH1_PIN 4
  584. #elif defined(__MK64FX512__)
  585. #define FTM0_CH0_PIN 22
  586. #define FTM0_CH1_PIN 23
  587. #define FTM0_CH2_PIN 9
  588. #define FTM0_CH3_PIN 10
  589. #define FTM0_CH4_PIN 6
  590. #define FTM0_CH5_PIN 20
  591. #define FTM0_CH6_PIN 21
  592. #define FTM0_CH7_PIN 5
  593. #define FTM1_CH0_PIN 3
  594. #define FTM1_CH1_PIN 4
  595. #define FTM2_CH0_PIN 29
  596. #define FTM2_CH1_PIN 30
  597. #define FTM3_CH0_PIN 2
  598. #define FTM3_CH1_PIN 14
  599. #define FTM3_CH2_PIN 7
  600. #define FTM3_CH3_PIN 8
  601. #define FTM3_CH4_PIN 35
  602. #define FTM3_CH5_PIN 36
  603. #define FTM3_CH6_PIN 37
  604. #define FTM3_CH7_PIN 38
  605. #elif defined(__MK66FX1M0__)
  606. #define FTM0_CH0_PIN 22
  607. #define FTM0_CH1_PIN 23
  608. #define FTM0_CH2_PIN 9
  609. #define FTM0_CH3_PIN 10
  610. #define FTM0_CH4_PIN 6
  611. #define FTM0_CH5_PIN 20
  612. #define FTM0_CH6_PIN 21
  613. #define FTM0_CH7_PIN 5
  614. #define FTM1_CH0_PIN 3
  615. #define FTM1_CH1_PIN 4
  616. #define FTM2_CH0_PIN 29
  617. #define FTM2_CH1_PIN 30
  618. #define FTM3_CH0_PIN 2
  619. #define FTM3_CH1_PIN 14
  620. #define FTM3_CH2_PIN 7
  621. #define FTM3_CH3_PIN 8
  622. #define FTM3_CH4_PIN 35
  623. #define FTM3_CH5_PIN 36
  624. #define FTM3_CH6_PIN 37
  625. #define FTM3_CH7_PIN 38
  626. #define TPM1_CH0_PIN 16
  627. #define TPM1_CH1_PIN 17
  628. #endif
  629. #define FTM_PINCFG(pin) FTM_PINCFG2(pin)
  630. #define FTM_PINCFG2(pin) CORE_PIN ## pin ## _CONFIG
  631. static uint8_t analog_write_res = 8;
  632. // SOPT4 is SIM select clocks?
  633. // FTM is clocked by the bus clock, either 24 or 48 MHz
  634. // input capture can be FTM1_CH0, CMP0 or CMP1 or USB start of frame
  635. // 24 MHz with reload 49152 to match Arduino's speed = 488.28125 Hz
  636. void analogWrite(uint8_t pin, int val)
  637. {
  638. uint32_t cval, max;
  639. #if defined(__MK20DX256__)
  640. if (pin == A14) {
  641. uint8_t res = analog_write_res;
  642. if (res < 12) {
  643. val <<= 12 - res;
  644. } else if (res > 12) {
  645. val >>= res - 12;
  646. }
  647. analogWriteDAC0(val);
  648. return;
  649. }
  650. #elif defined(__MKL26Z64__)
  651. if (pin == A12) {
  652. uint8_t res = analog_write_res;
  653. if (res < 12) {
  654. val <<= 12 - res;
  655. } else if (res > 12) {
  656. val >>= res - 12;
  657. }
  658. analogWriteDAC0(val);
  659. return;
  660. }
  661. #elif defined(__MK64FX512__) || defined(__MK66FX1M0__)
  662. if (pin == A21 || pin == A22) {
  663. uint8_t res = analog_write_res;
  664. if (res < 12) {
  665. val <<= 12 - res;
  666. } else if (res > 12) {
  667. val >>= res - 12;
  668. }
  669. if (pin == A21) analogWriteDAC0(val);
  670. else analogWriteDAC1(val);
  671. return;
  672. }
  673. #endif
  674. max = 1 << analog_write_res;
  675. if (val <= 0) {
  676. digitalWrite(pin, LOW);
  677. pinMode(pin, OUTPUT); // TODO: implement OUTPUT_LOW
  678. return;
  679. } else if (val >= max) {
  680. digitalWrite(pin, HIGH);
  681. pinMode(pin, OUTPUT); // TODO: implement OUTPUT_HIGH
  682. return;
  683. }
  684. //serial_print("analogWrite\n");
  685. //serial_print("val = ");
  686. //serial_phex32(val);
  687. //serial_print("\n");
  688. //serial_print("analog_write_res = ");
  689. //serial_phex(analog_write_res);
  690. //serial_print("\n");
  691. if (pin == FTM1_CH0_PIN || pin == FTM1_CH1_PIN) {
  692. cval = ((uint32_t)val * (uint32_t)(FTM1_MOD + 1)) >> analog_write_res;
  693. #if defined(FTM2_CH0_PIN)
  694. } else if (pin == FTM2_CH0_PIN || pin == FTM2_CH1_PIN) {
  695. cval = ((uint32_t)val * (uint32_t)(FTM2_MOD + 1)) >> analog_write_res;
  696. #endif
  697. #if defined(FTM3_CH0_PIN)
  698. } else if (pin == FTM3_CH0_PIN || pin == FTM3_CH1_PIN || pin == FTM3_CH2_PIN
  699. || pin == FTM3_CH3_PIN || pin == FTM3_CH4_PIN || pin == FTM3_CH5_PIN
  700. || pin == FTM3_CH6_PIN || pin == FTM3_CH7_PIN) {
  701. cval = ((uint32_t)val * (uint32_t)(FTM3_MOD + 1)) >> analog_write_res;
  702. #endif
  703. #if defined(TPM1_CH0_PIN)
  704. } else if (pin == TPM1_CH0_PIN || pin == TPM1_CH1_PIN) {
  705. cval = ((uint32_t)val * (uint32_t)(TPM1_MOD + 1)) >> analog_write_res;
  706. #endif
  707. } else {
  708. cval = ((uint32_t)val * (uint32_t)(FTM0_MOD + 1)) >> analog_write_res;
  709. }
  710. //serial_print("cval = ");
  711. //serial_phex32(cval);
  712. //serial_print("\n");
  713. switch (pin) {
  714. #ifdef FTM0_CH0_PIN
  715. case FTM0_CH0_PIN: // PTC1, FTM0_CH0
  716. FTM0_C0V = cval;
  717. FTM_PINCFG(FTM0_CH0_PIN) = PORT_PCR_MUX(4) | PORT_PCR_DSE | PORT_PCR_SRE;
  718. break;
  719. #endif
  720. #ifdef FTM0_CH1_PIN
  721. case FTM0_CH1_PIN: // PTC2, FTM0_CH1
  722. FTM0_C1V = cval;
  723. FTM_PINCFG(FTM0_CH1_PIN) = PORT_PCR_MUX(4) | PORT_PCR_DSE | PORT_PCR_SRE;
  724. break;
  725. #endif
  726. #ifdef FTM0_CH2_PIN
  727. case FTM0_CH2_PIN: // PTC3, FTM0_CH2
  728. FTM0_C2V = cval;
  729. FTM_PINCFG(FTM0_CH2_PIN) = PORT_PCR_MUX(4) | PORT_PCR_DSE | PORT_PCR_SRE;
  730. break;
  731. #endif
  732. #ifdef FTM0_CH3_PIN
  733. case FTM0_CH3_PIN: // PTC4, FTM0_CH3
  734. FTM0_C3V = cval;
  735. FTM_PINCFG(FTM0_CH3_PIN) = PORT_PCR_MUX(4) | PORT_PCR_DSE | PORT_PCR_SRE;
  736. break;
  737. #endif
  738. #ifdef FTM0_CH4_PIN
  739. case FTM0_CH4_PIN: // PTD4, FTM0_CH4
  740. FTM0_C4V = cval;
  741. FTM_PINCFG(FTM0_CH4_PIN) = PORT_PCR_MUX(4) | PORT_PCR_DSE | PORT_PCR_SRE;
  742. break;
  743. #endif
  744. #ifdef FTM0_CH5_PIN
  745. case FTM0_CH5_PIN: // PTD5, FTM0_CH5
  746. FTM0_C5V = cval;
  747. FTM_PINCFG(FTM0_CH5_PIN) = PORT_PCR_MUX(4) | PORT_PCR_DSE | PORT_PCR_SRE;
  748. break;
  749. #endif
  750. #ifdef FTM0_CH6_PIN
  751. case FTM0_CH6_PIN: // PTD6, FTM0_CH6
  752. FTM0_C6V = cval;
  753. FTM_PINCFG(FTM0_CH6_PIN) = PORT_PCR_MUX(4) | PORT_PCR_DSE | PORT_PCR_SRE;
  754. break;
  755. #endif
  756. #ifdef FTM0_CH7_PIN
  757. case FTM0_CH7_PIN: // PTD7, FTM0_CH7
  758. FTM0_C7V = cval;
  759. FTM_PINCFG(FTM0_CH7_PIN) = PORT_PCR_MUX(4) | PORT_PCR_DSE | PORT_PCR_SRE;
  760. break;
  761. #endif
  762. #ifdef FTM1_CH0_PIN
  763. case FTM1_CH0_PIN: // PTA12, FTM1_CH0
  764. FTM1_C0V = cval;
  765. FTM_PINCFG(FTM1_CH0_PIN) = PORT_PCR_MUX(3) | PORT_PCR_DSE | PORT_PCR_SRE;
  766. break;
  767. #endif
  768. #ifdef FTM1_CH1_PIN
  769. case FTM1_CH1_PIN: // PTA13, FTM1_CH1
  770. FTM1_C1V = cval;
  771. FTM_PINCFG(FTM1_CH1_PIN) = PORT_PCR_MUX(3) | PORT_PCR_DSE | PORT_PCR_SRE;
  772. break;
  773. #endif
  774. #ifdef FTM2_CH0_PIN
  775. case FTM2_CH0_PIN: // PTB18, FTM2_CH0
  776. FTM2_C0V = cval;
  777. FTM_PINCFG(FTM2_CH0_PIN) = PORT_PCR_MUX(3) | PORT_PCR_DSE | PORT_PCR_SRE;
  778. break;
  779. #endif
  780. #ifdef FTM2_CH1_PIN
  781. case FTM2_CH1_PIN: // PTB19, FTM1_CH1
  782. FTM2_C1V = cval;
  783. FTM_PINCFG(FTM2_CH1_PIN) = PORT_PCR_MUX(3) | PORT_PCR_DSE | PORT_PCR_SRE;
  784. break;
  785. #endif
  786. #ifdef FTM3_CH0_PIN
  787. case FTM3_CH0_PIN:
  788. FTM3_C0V = cval;
  789. FTM_PINCFG(FTM3_CH0_PIN) = PORT_PCR_MUX(4) | PORT_PCR_DSE | PORT_PCR_SRE;
  790. break;
  791. #endif
  792. #ifdef FTM3_CH1_PIN
  793. case FTM3_CH1_PIN:
  794. FTM3_C1V = cval;
  795. FTM_PINCFG(FTM3_CH1_PIN) = PORT_PCR_MUX(4) | PORT_PCR_DSE | PORT_PCR_SRE;
  796. break;
  797. #endif
  798. #ifdef FTM3_CH2_PIN
  799. case FTM3_CH2_PIN:
  800. FTM3_C2V = cval;
  801. FTM_PINCFG(FTM3_CH2_PIN) = PORT_PCR_MUX(4) | PORT_PCR_DSE | PORT_PCR_SRE;
  802. break;
  803. #endif
  804. #ifdef FTM3_CH3_PIN
  805. case FTM3_CH3_PIN:
  806. FTM3_C3V = cval;
  807. FTM_PINCFG(FTM3_CH3_PIN) = PORT_PCR_MUX(4) | PORT_PCR_DSE | PORT_PCR_SRE;
  808. break;
  809. #endif
  810. #ifdef FTM3_CH4_PIN
  811. case FTM3_CH4_PIN:
  812. FTM3_C4V = cval;
  813. FTM_PINCFG(FTM3_CH4_PIN) = PORT_PCR_MUX(3) | PORT_PCR_DSE | PORT_PCR_SRE;
  814. break;
  815. #endif
  816. #ifdef FTM3_CH5_PIN
  817. case FTM3_CH5_PIN:
  818. FTM3_C5V = cval;
  819. FTM_PINCFG(FTM3_CH5_PIN) = PORT_PCR_MUX(3) | PORT_PCR_DSE | PORT_PCR_SRE;
  820. break;
  821. #endif
  822. #ifdef FTM3_CH6_PIN
  823. case FTM3_CH6_PIN:
  824. FTM3_C6V = cval;
  825. FTM_PINCFG(FTM3_CH6_PIN) = PORT_PCR_MUX(3) | PORT_PCR_DSE | PORT_PCR_SRE;
  826. break;
  827. #endif
  828. #ifdef FTM3_CH7_PIN
  829. case FTM3_CH7_PIN:
  830. FTM3_C7V = cval;
  831. FTM_PINCFG(FTM3_CH7_PIN) = PORT_PCR_MUX(3) | PORT_PCR_DSE | PORT_PCR_SRE;
  832. break;
  833. #endif
  834. #ifdef TPM1_CH0_PIN
  835. case TPM1_CH0_PIN:
  836. TPM1_C0V = cval;
  837. FTM_PINCFG(TPM1_CH0_PIN) = PORT_PCR_MUX(6) | PORT_PCR_DSE | PORT_PCR_SRE;
  838. break;
  839. #endif
  840. #ifdef TPM1_CH1_PIN
  841. case TPM1_CH1_PIN:
  842. TPM1_C1V = cval;
  843. FTM_PINCFG(TPM1_CH1_PIN) = PORT_PCR_MUX(6) | PORT_PCR_DSE | PORT_PCR_SRE;
  844. break;
  845. #endif
  846. default:
  847. digitalWrite(pin, (val > 127) ? HIGH : LOW);
  848. pinMode(pin, OUTPUT);
  849. }
  850. }
  851. uint32_t analogWriteRes(uint32_t bits)
  852. {
  853. uint32_t prior_res;
  854. if (bits < 1) {
  855. bits = 1;
  856. } else if (bits > 16) {
  857. bits = 16;
  858. }
  859. prior_res = analog_write_res;
  860. analog_write_res = bits;
  861. return prior_res;
  862. }
  863. void analogWriteFrequency(uint8_t pin, float frequency)
  864. {
  865. uint32_t prescale, mod, ftmClock, ftmClockSource;
  866. float minfreq;
  867. //serial_print("analogWriteFrequency: pin = ");
  868. //serial_phex(pin);
  869. //serial_print(", freq = ");
  870. //serial_phex32((uint32_t)frequency);
  871. //serial_print("\n");
  872. #ifdef TPM1_CH0_PIN
  873. if (pin == TPM1_CH0_PIN || pin == TPM1_CH1_PIN) {
  874. ftmClockSource = 1;
  875. ftmClock = 16000000;
  876. } else
  877. #endif
  878. #if defined(__MKL26Z64__)
  879. // Teensy LC does not support slow clock source (ftmClockSource = 2)
  880. ftmClockSource = 1; // Use default F_TIMER clock source
  881. ftmClock = F_TIMER; // Set variable for the actual timer clock frequency
  882. #else
  883. if (frequency < (float)(F_TIMER >> 7) / 65536.0f) {
  884. // frequency is too low for working with F_TIMER:
  885. ftmClockSource = 2; // Use alternative 31250Hz clock source
  886. ftmClock = 31250; // Set variable for the actual timer clock frequency
  887. } else {
  888. ftmClockSource = 1; // Use default F_TIMER clock source
  889. ftmClock = F_TIMER; // Set variable for the actual timer clock frequency
  890. }
  891. #endif
  892. for (prescale = 0; prescale < 7; prescale++) {
  893. minfreq = (float)(ftmClock >> prescale) / 65536.0f; //Use ftmClock instead of F_TIMER
  894. if (frequency >= minfreq) break;
  895. }
  896. //serial_print("F_TIMER/ftm_Clock = ");
  897. //serial_phex32(ftmClock >> prescale);
  898. //serial_print("\n");
  899. //serial_print("prescale = ");
  900. //serial_phex(prescale);
  901. //serial_print("\n");
  902. mod = (float)(ftmClock >> prescale) / frequency - 0.5f; //Use ftmClock instead of F_TIMER
  903. if (mod > 65535) mod = 65535;
  904. //serial_print("mod = ");
  905. //serial_phex32(mod);
  906. //serial_print("\n");
  907. if (pin == FTM1_CH0_PIN || pin == FTM1_CH1_PIN) {
  908. FTM1_SC = 0;
  909. FTM1_CNT = 0;
  910. FTM1_MOD = mod;
  911. FTM1_SC = FTM_SC_CLKS(ftmClockSource) | FTM_SC_PS(prescale); //Use ftmClockSource instead of 1
  912. } else if (pin == FTM0_CH0_PIN || pin == FTM0_CH1_PIN
  913. || pin == FTM0_CH2_PIN || pin == FTM0_CH3_PIN
  914. || pin == FTM0_CH4_PIN || pin == FTM0_CH5_PIN
  915. #ifdef FTM0_CH6_PIN
  916. || pin == FTM0_CH6_PIN || pin == FTM0_CH7_PIN
  917. #endif
  918. ) {
  919. FTM0_SC = 0;
  920. FTM0_CNT = 0;
  921. FTM0_MOD = mod;
  922. FTM0_SC = FTM_SC_CLKS(ftmClockSource) | FTM_SC_PS(prescale); //Use ftmClockSource instead of 1
  923. }
  924. #ifdef FTM2_CH0_PIN
  925. else if (pin == FTM2_CH0_PIN || pin == FTM2_CH1_PIN) {
  926. FTM2_SC = 0;
  927. FTM2_CNT = 0;
  928. FTM2_MOD = mod;
  929. FTM2_SC = FTM_SC_CLKS(ftmClockSource) | FTM_SC_PS(prescale); //Use ftmClockSource instead of 1
  930. }
  931. #endif
  932. #ifdef FTM3_CH0_PIN
  933. else if (pin == FTM3_CH0_PIN || pin == FTM3_CH1_PIN
  934. || pin == FTM3_CH2_PIN || pin == FTM3_CH3_PIN
  935. || pin == FTM3_CH4_PIN || pin == FTM3_CH5_PIN
  936. || pin == FTM3_CH6_PIN || pin == FTM3_CH7_PIN) {
  937. FTM3_SC = 0;
  938. FTM3_CNT = 0;
  939. FTM3_MOD = mod;
  940. FTM3_SC = FTM_SC_CLKS(ftmClockSource) | FTM_SC_PS(prescale); //Use the new ftmClockSource instead of 1
  941. }
  942. #endif
  943. #ifdef TPM1_CH0_PIN
  944. else if (pin == TPM1_CH0_PIN || pin == TPM1_CH1_PIN) {
  945. TPM1_SC = 0;
  946. TPM1_CNT = 0;
  947. TPM1_MOD = mod;
  948. TPM1_SC = FTM_SC_CLKS(ftmClockSource) | FTM_SC_PS(prescale);
  949. }
  950. #endif
  951. }
  952. // TODO: startup code needs to initialize all pins to GPIO mode, input by default
  953. void digitalWrite(uint8_t pin, uint8_t val)
  954. {
  955. if (pin >= CORE_NUM_DIGITAL) return;
  956. #ifdef KINETISK
  957. if (*portModeRegister(pin)) {
  958. if (val) {
  959. *portSetRegister(pin) = 1;
  960. } else {
  961. *portClearRegister(pin) = 1;
  962. }
  963. #else
  964. if (*portModeRegister(pin) & digitalPinToBitMask(pin)) {
  965. if (val) {
  966. *portSetRegister(pin) = digitalPinToBitMask(pin);
  967. } else {
  968. *portClearRegister(pin) = digitalPinToBitMask(pin);
  969. }
  970. #endif
  971. } else {
  972. volatile uint32_t *config = portConfigRegister(pin);
  973. if (val) {
  974. // TODO use bitband for atomic read-mod-write
  975. *config |= (PORT_PCR_PE | PORT_PCR_PS);
  976. //*config = PORT_PCR_MUX(1) | PORT_PCR_PE | PORT_PCR_PS;
  977. } else {
  978. // TODO use bitband for atomic read-mod-write
  979. *config &= ~(PORT_PCR_PE);
  980. //*config = PORT_PCR_MUX(1);
  981. }
  982. }
  983. }
  984. uint8_t digitalRead(uint8_t pin)
  985. {
  986. if (pin >= CORE_NUM_DIGITAL) return 0;
  987. #ifdef KINETISK
  988. return *portInputRegister(pin);
  989. #else
  990. return (*portInputRegister(pin) & digitalPinToBitMask(pin)) ? 1 : 0;
  991. #endif
  992. }
  993. void pinMode(uint8_t pin, uint8_t mode)
  994. {
  995. volatile uint32_t *config;
  996. if (pin >= CORE_NUM_DIGITAL) return;
  997. config = portConfigRegister(pin);
  998. if (mode == OUTPUT || mode == OUTPUT_OPENDRAIN) {
  999. #ifdef KINETISK
  1000. *portModeRegister(pin) = 1;
  1001. #else
  1002. *portModeRegister(pin) |= digitalPinToBitMask(pin); // TODO: atomic
  1003. #endif
  1004. *config = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
  1005. if (mode == OUTPUT_OPENDRAIN) {
  1006. *config |= PORT_PCR_ODE;
  1007. } else {
  1008. *config &= ~PORT_PCR_ODE;
  1009. }
  1010. } else {
  1011. #ifdef KINETISK
  1012. *portModeRegister(pin) = 0;
  1013. #else
  1014. *portModeRegister(pin) &= ~digitalPinToBitMask(pin);
  1015. #endif
  1016. if (mode == INPUT) {
  1017. *config = PORT_PCR_MUX(1);
  1018. } else if (mode == INPUT_PULLUP) {
  1019. *config = PORT_PCR_MUX(1) | PORT_PCR_PE | PORT_PCR_PS;
  1020. } else if (mode == INPUT_PULLDOWN) {
  1021. *config = PORT_PCR_MUX(1) | PORT_PCR_PE;
  1022. } else { // INPUT_DISABLE
  1023. *config = 0;
  1024. }
  1025. }
  1026. }
  1027. void _shiftOut(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder, uint8_t value)
  1028. {
  1029. if (bitOrder == LSBFIRST) {
  1030. shiftOut_lsbFirst(dataPin, clockPin, value);
  1031. } else {
  1032. shiftOut_msbFirst(dataPin, clockPin, value);
  1033. }
  1034. }
  1035. void shiftOut_lsbFirst(uint8_t dataPin, uint8_t clockPin, uint8_t value)
  1036. {
  1037. uint8_t mask;
  1038. for (mask=0x01; mask; mask <<= 1) {
  1039. digitalWrite(dataPin, value & mask);
  1040. digitalWrite(clockPin, HIGH);
  1041. digitalWrite(clockPin, LOW);
  1042. }
  1043. }
  1044. void shiftOut_msbFirst(uint8_t dataPin, uint8_t clockPin, uint8_t value)
  1045. {
  1046. uint8_t mask;
  1047. for (mask=0x80; mask; mask >>= 1) {
  1048. digitalWrite(dataPin, value & mask);
  1049. digitalWrite(clockPin, HIGH);
  1050. digitalWrite(clockPin, LOW);
  1051. }
  1052. }
  1053. uint8_t _shiftIn(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder)
  1054. {
  1055. if (bitOrder == LSBFIRST) {
  1056. return shiftIn_lsbFirst(dataPin, clockPin);
  1057. } else {
  1058. return shiftIn_msbFirst(dataPin, clockPin);
  1059. }
  1060. }
  1061. uint8_t shiftIn_lsbFirst(uint8_t dataPin, uint8_t clockPin)
  1062. {
  1063. uint8_t mask, value=0;
  1064. for (mask=0x01; mask; mask <<= 1) {
  1065. digitalWrite(clockPin, HIGH);
  1066. if (digitalRead(dataPin)) value |= mask;
  1067. digitalWrite(clockPin, LOW);
  1068. }
  1069. return value;
  1070. }
  1071. uint8_t shiftIn_msbFirst(uint8_t dataPin, uint8_t clockPin)
  1072. {
  1073. uint8_t mask, value=0;
  1074. for (mask=0x80; mask; mask >>= 1) {
  1075. digitalWrite(clockPin, HIGH);
  1076. if (digitalRead(dataPin)) value |= mask;
  1077. digitalWrite(clockPin, LOW);
  1078. }
  1079. return value;
  1080. }
  1081. // the systick interrupt is supposed to increment this at 1 kHz rate
  1082. volatile uint32_t systick_millis_count = 0;
  1083. //uint32_t systick_current, systick_count, systick_istatus; // testing only
  1084. uint32_t micros(void)
  1085. {
  1086. uint32_t count, current, istatus;
  1087. __disable_irq();
  1088. current = SYST_CVR;
  1089. count = systick_millis_count;
  1090. istatus = SCB_ICSR; // bit 26 indicates if systick exception pending
  1091. __enable_irq();
  1092. //systick_current = current;
  1093. //systick_count = count;
  1094. //systick_istatus = istatus & SCB_ICSR_PENDSTSET ? 1 : 0;
  1095. if ((istatus & SCB_ICSR_PENDSTSET) && current > 50) count++;
  1096. current = ((F_CPU / 1000) - 1) - current;
  1097. #if defined(KINETISL) && F_CPU == 48000000
  1098. return count * 1000 + ((current * (uint32_t)87381) >> 22);
  1099. #elif defined(KINETISL) && F_CPU == 24000000
  1100. return count * 1000 + ((current * (uint32_t)174763) >> 22);
  1101. #endif
  1102. return count * 1000 + current / (F_CPU / 1000000);
  1103. }
  1104. void delay(uint32_t ms)
  1105. {
  1106. uint32_t start = micros();
  1107. if (ms > 0) {
  1108. while (1) {
  1109. while ((micros() - start) >= 1000) {
  1110. ms--;
  1111. if (ms == 0) return;
  1112. start += 1000;
  1113. }
  1114. yield();
  1115. }
  1116. }
  1117. }
  1118. // TODO: verify these result in correct timeouts...
  1119. #if F_CPU == 240000000
  1120. #define PULSEIN_LOOPS_PER_USEC 33
  1121. #elif F_CPU == 216000000
  1122. #define PULSEIN_LOOPS_PER_USEC 31
  1123. #elif F_CPU == 192000000
  1124. #define PULSEIN_LOOPS_PER_USEC 29
  1125. #elif F_CPU == 180000000
  1126. #define PULSEIN_LOOPS_PER_USEC 27
  1127. #elif F_CPU == 168000000
  1128. #define PULSEIN_LOOPS_PER_USEC 25
  1129. #elif F_CPU == 144000000
  1130. #define PULSEIN_LOOPS_PER_USEC 21
  1131. #elif F_CPU == 120000000
  1132. #define PULSEIN_LOOPS_PER_USEC 18
  1133. #elif F_CPU == 96000000
  1134. #define PULSEIN_LOOPS_PER_USEC 14
  1135. #elif F_CPU == 72000000
  1136. #define PULSEIN_LOOPS_PER_USEC 10
  1137. #elif F_CPU == 48000000
  1138. #define PULSEIN_LOOPS_PER_USEC 7
  1139. #elif F_CPU == 24000000
  1140. #define PULSEIN_LOOPS_PER_USEC 4
  1141. #elif F_CPU == 16000000
  1142. #define PULSEIN_LOOPS_PER_USEC 1
  1143. #elif F_CPU == 8000000
  1144. #define PULSEIN_LOOPS_PER_USEC 1
  1145. #elif F_CPU == 4000000
  1146. #define PULSEIN_LOOPS_PER_USEC 1
  1147. #elif F_CPU == 2000000
  1148. #define PULSEIN_LOOPS_PER_USEC 1
  1149. #endif
  1150. #if defined(KINETISK)
  1151. uint32_t pulseIn_high(volatile uint8_t *reg, uint32_t timeout)
  1152. {
  1153. uint32_t timeout_count = timeout * PULSEIN_LOOPS_PER_USEC;
  1154. uint32_t usec_start, usec_stop;
  1155. // wait for any previous pulse to end
  1156. while (*reg) {
  1157. if (--timeout_count == 0) return 0;
  1158. }
  1159. // wait for the pulse to start
  1160. while (!*reg) {
  1161. if (--timeout_count == 0) return 0;
  1162. }
  1163. usec_start = micros();
  1164. // wait for the pulse to stop
  1165. while (*reg) {
  1166. if (--timeout_count == 0) return 0;
  1167. }
  1168. usec_stop = micros();
  1169. return usec_stop - usec_start;
  1170. }
  1171. uint32_t pulseIn_low(volatile uint8_t *reg, uint32_t timeout)
  1172. {
  1173. uint32_t timeout_count = timeout * PULSEIN_LOOPS_PER_USEC;
  1174. uint32_t usec_start, usec_stop;
  1175. // wait for any previous pulse to end
  1176. while (!*reg) {
  1177. if (--timeout_count == 0) return 0;
  1178. }
  1179. // wait for the pulse to start
  1180. while (*reg) {
  1181. if (--timeout_count == 0) return 0;
  1182. }
  1183. usec_start = micros();
  1184. // wait for the pulse to stop
  1185. while (!*reg) {
  1186. if (--timeout_count == 0) return 0;
  1187. }
  1188. usec_stop = micros();
  1189. return usec_stop - usec_start;
  1190. }
  1191. // TODO: an inline version should handle the common case where state is const
  1192. uint32_t pulseIn(uint8_t pin, uint8_t state, uint32_t timeout)
  1193. {
  1194. if (pin >= CORE_NUM_DIGITAL) return 0;
  1195. if (state) return pulseIn_high(portInputRegister(pin), timeout);
  1196. return pulseIn_low(portInputRegister(pin), timeout);;
  1197. }
  1198. #elif defined(KINETISL)
  1199. // For TeencyLC need to use mask on the input register as the register is shared by several IO pins
  1200. uint32_t pulseIn_high(volatile uint8_t *reg, uint8_t mask, uint32_t timeout)
  1201. {
  1202. uint32_t timeout_count = timeout * PULSEIN_LOOPS_PER_USEC;
  1203. uint32_t usec_start, usec_stop;
  1204. // wait for any previous pulse to end
  1205. while (*reg & mask) {
  1206. if (--timeout_count == 0) return -1;
  1207. }
  1208. // wait for the pulse to start
  1209. while (!(*reg & mask)) {
  1210. if (--timeout_count == 0) return 0;
  1211. }
  1212. usec_start = micros();
  1213. // wait for the pulse to stop
  1214. while (*reg & mask) {
  1215. if (--timeout_count == 0) return 0;
  1216. }
  1217. usec_stop = micros();
  1218. return usec_stop - usec_start;
  1219. }
  1220. uint32_t pulseIn_low(volatile uint8_t *reg, uint8_t mask, uint32_t timeout)
  1221. {
  1222. uint32_t timeout_count = timeout * PULSEIN_LOOPS_PER_USEC;
  1223. uint32_t usec_start, usec_stop;
  1224. // wait for any previous pulse to end
  1225. while (!(*reg & mask)) {
  1226. if (--timeout_count == 0) return 0;
  1227. }
  1228. // wait for the pulse to start
  1229. while (*reg & mask) {
  1230. if (--timeout_count == 0) return 0;
  1231. }
  1232. usec_start = micros();
  1233. // wait for the pulse to stop
  1234. while (!(*reg & mask)) {
  1235. if (--timeout_count == 0) return 0;
  1236. }
  1237. usec_stop = micros();
  1238. return usec_stop - usec_start;
  1239. }
  1240. // TODO: an inline version should handle the common case where state is const
  1241. uint32_t pulseIn(uint8_t pin, uint8_t state, uint32_t timeout)
  1242. {
  1243. if (pin >= CORE_NUM_DIGITAL) return 0;
  1244. if (state) return pulseIn_high(portInputRegister(pin), digitalPinToBitMask(pin), timeout);
  1245. return pulseIn_low(portInputRegister(pin), digitalPinToBitMask(pin), timeout);;
  1246. }
  1247. #endif