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