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 години
преди 9 години
преди 11 години
преди 8 години
преди 11 години
преди 11 години
преди 11 години
преди 9 години
преди 9 години
преди 9 години
преди 11 години
преди 9 години
преди 11 години
преди 7 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 9 години
преди 11 години
преди 11 години
преди 9 години
преди 11 години
преди 11 години
преди 9 години
преди 11 години
преди 9 години
преди 11 години
преди 9 години
преди 11 години
преди 11 години
преди 9 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 9 години
преди 11 години
преди 11 години
преди 9 години
преди 11 години
преди 9 години
преди 11 години
преди 9 години
преди 11 години
преди 11 години
преди 9 години
преди 11 години
преди 10 години
преди 9 години
преди 10 години
преди 9 години
преди 8 години
преди 9 години
преди 10 години
преди 11 години
преди 10 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 9 години
преди 11 години
преди 9 години
преди 8 години
преди 9 години
преди 8 години
преди 9 години
преди 11 години
преди 11 години
преди 11 години
преди 9 години
преди 11 години
преди 9 години
преди 11 години
преди 9 години
преди 7 години
преди 9 години
преди 11 години
преди 11 години
преди 11 години
преди 10 години
преди 9 години
преди 11 години
преди 9 години
преди 11 години
преди 9 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 8 години
преди 9 години
преди 8 години
преди 9 години
преди 9 години
преди 11 години
преди 8 години
преди 8 години
преди 9 години
преди 11 години
преди 9 години
преди 11 години
преди 8 години
преди 8 години
преди 11 години
преди 11 години
преди 11 години
преди 10 години
преди 11 години
преди 10 години
преди 11 години
преди 10 години
преди 10 години
преди 9 години
преди 10 години
преди 9 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 9 години
преди 9 години
преди 11 години
преди 9 години
преди 10 години
преди 9 години
преди 8 години
преди 6 години
преди 6 години
преди 6 години
преди 8 години
преди 8 години
преди 9 години
преди 8 години
преди 9 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 8 години
преди 8 години
преди 10 години
преди 9 години
преди 11 години
преди 10 години
преди 6 години
преди 8 години
преди 8 години
преди 8 години
преди 8 години
преди 8 години
преди 8 години
преди 8 години
преди 8 години
преди 8 години
преди 6 години
преди 8 години
преди 8 години
преди 8 години
преди 8 години
преди 8 години
преди 8 години
преди 8 години
преди 8 години
преди 8 години
преди 8 години
преди 8 години
преди 8 години
преди 10 години
преди 10 години
преди 8 години
преди 8 години
преди 8 години
преди 10 години
преди 10 години
преди 8 години
преди 8 години
преди 8 години
преди 10 години
преди 10 години
преди 8 години
преди 8 години
преди 8 години
преди 10 години
преди 10 години
преди 8 години
преди 8 години
преди 8 години
преди 10 години
преди 11 години
преди 10 години
преди 8 години
преди 8 години
преди 10 години
преди 8 години
преди 9 години
преди 8 години
преди 11 години
преди 10 години
преди 9 години
преди 8 години
преди 10 години
преди 9 години
преди 10 години
преди 10 години
преди 8 години
преди 8 години
преди 10 години
преди 10 години
преди 8 години
преди 8 години
преди 10 години
преди 8 години
преди 10 години
преди 8 години
преди 8 години
преди 10 години
преди 8 години
преди 10 години
преди 8 години
преди 11 години
преди 6 години
преди 11 години
преди 10 години
преди 10 години
преди 6 години
преди 9 години
преди 6 години
преди 6 години
преди 6 години
преди 9 години
преди 10 години
преди 9 години
преди 10 години
преди 10 години
преди 11 години
преди 11 години
преди 11 години
преди 9 години
преди 9 години
преди 9 години
преди 10 години
преди 9 години
преди 10 години
преди 9 години
преди 10 години
преди 9 години
преди 9 години
преди 11 години
преди 11 години
преди 6 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 8 години
преди 8 години
преди 6 години
преди 8 години
преди 8 години
преди 6 години
преди 8 години
преди 8 години
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383
  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 "kinetis.h"
  31. #include "core_pins.h" // testing only
  32. #include "ser_print.h" // testing only
  33. #include <errno.h>
  34. // Flash Security Setting. On Teensy 3.2, you can lock the MK20 chip to prevent
  35. // anyone from reading your code. You CAN still reprogram your Teensy while
  36. // security is set, but the bootloader will be unable to respond to auto-reboot
  37. // requests from Arduino. Pressing the program button will cause a full chip
  38. // erase to gain access, because the bootloader chip is locked out. Normally,
  39. // erase occurs when uploading begins, so if you press the Program button
  40. // accidentally, simply power cycling will run your program again. When
  41. // security is locked, any Program button press causes immediate full erase.
  42. // Special care must be used with the Program button, because it must be made
  43. // accessible to initiate reprogramming, but it must not be accidentally
  44. // pressed when Teensy Loader is not being used to reprogram. To set lock the
  45. // security change this to 0xDC. Teensy 3.0 and 3.1 do not support security lock.
  46. #define FSEC 0xDE
  47. // Flash Options
  48. #define FOPT 0xF9
  49. extern unsigned long _stext;
  50. extern unsigned long _etext;
  51. extern unsigned long _sdata;
  52. extern unsigned long _edata;
  53. extern unsigned long _sbss;
  54. extern unsigned long _ebss;
  55. extern unsigned long _estack;
  56. //extern void __init_array_start(void);
  57. //extern void __init_array_end(void);
  58. extern int main (void);
  59. void ResetHandler(void);
  60. void _init_Teensyduino_internal_(void) __attribute__((noinline));
  61. void __libc_init_array(void);
  62. void fault_isr(void)
  63. {
  64. #if 0
  65. uint32_t addr;
  66. SIM_SCGC4 |= 0x00000400;
  67. UART0_BDH = 0;
  68. UART0_BDL = 26; // 115200 at 48 MHz
  69. UART0_C2 = UART_C2_TE;
  70. PORTB_PCR17 = PORT_PCR_MUX(3);
  71. ser_print("\nfault: \n??: ");
  72. asm("ldr %0, [sp, #52]" : "=r" (addr) ::);
  73. ser_print_hex32(addr);
  74. ser_print("\n??: ");
  75. asm("ldr %0, [sp, #48]" : "=r" (addr) ::);
  76. ser_print_hex32(addr);
  77. ser_print("\n??: ");
  78. asm("ldr %0, [sp, #44]" : "=r" (addr) ::);
  79. ser_print_hex32(addr);
  80. ser_print("\npsr:");
  81. asm("ldr %0, [sp, #40]" : "=r" (addr) ::);
  82. ser_print_hex32(addr);
  83. ser_print("\nadr:");
  84. asm("ldr %0, [sp, #36]" : "=r" (addr) ::);
  85. ser_print_hex32(addr);
  86. ser_print("\nlr: ");
  87. asm("ldr %0, [sp, #32]" : "=r" (addr) ::);
  88. ser_print_hex32(addr);
  89. ser_print("\nr12:");
  90. asm("ldr %0, [sp, #28]" : "=r" (addr) ::);
  91. ser_print_hex32(addr);
  92. ser_print("\nr3: ");
  93. asm("ldr %0, [sp, #24]" : "=r" (addr) ::);
  94. ser_print_hex32(addr);
  95. ser_print("\nr2: ");
  96. asm("ldr %0, [sp, #20]" : "=r" (addr) ::);
  97. ser_print_hex32(addr);
  98. ser_print("\nr1: ");
  99. asm("ldr %0, [sp, #16]" : "=r" (addr) ::);
  100. ser_print_hex32(addr);
  101. ser_print("\nr0: ");
  102. asm("ldr %0, [sp, #12]" : "=r" (addr) ::);
  103. ser_print_hex32(addr);
  104. ser_print("\nr4: ");
  105. asm("ldr %0, [sp, #8]" : "=r" (addr) ::);
  106. ser_print_hex32(addr);
  107. ser_print("\nlr: ");
  108. asm("ldr %0, [sp, #4]" : "=r" (addr) ::);
  109. ser_print_hex32(addr);
  110. ser_print("\n");
  111. asm("ldr %0, [sp, #0]" : "=r" (addr) ::);
  112. #endif
  113. while (1) {
  114. // keep polling some communication while in fault
  115. // mode, so we don't completely die.
  116. if (SIM_SCGC4 & SIM_SCGC4_USBOTG) usb_isr();
  117. if (SIM_SCGC4 & SIM_SCGC4_UART0) uart0_status_isr();
  118. if (SIM_SCGC4 & SIM_SCGC4_UART1) uart1_status_isr();
  119. if (SIM_SCGC4 & SIM_SCGC4_UART2) uart2_status_isr();
  120. }
  121. }
  122. void unused_isr(void)
  123. {
  124. fault_isr();
  125. }
  126. void nmi_isr(void) __attribute__ ((weak, alias("unused_isr")));
  127. void hard_fault_isr(void) __attribute__ ((weak, alias("fault_isr")));
  128. void memmanage_fault_isr(void) __attribute__ ((weak, alias("fault_isr")));
  129. void bus_fault_isr(void) __attribute__ ((weak, alias("fault_isr")));
  130. void usage_fault_isr(void) __attribute__ ((weak, alias("fault_isr")));
  131. void svcall_isr(void) __attribute__ ((weak, alias("unused_isr")));
  132. void debugmonitor_isr(void) __attribute__ ((weak, alias("unused_isr")));
  133. void pendablesrvreq_isr(void) __attribute__ ((weak, alias("unused_isr")));
  134. void systick_isr(void);
  135. void dma_ch0_isr(void) __attribute__ ((weak, alias("unused_isr")));
  136. void dma_ch1_isr(void) __attribute__ ((weak, alias("unused_isr")));
  137. void dma_ch2_isr(void) __attribute__ ((weak, alias("unused_isr")));
  138. void dma_ch3_isr(void) __attribute__ ((weak, alias("unused_isr")));
  139. void dma_ch4_isr(void) __attribute__ ((weak, alias("unused_isr")));
  140. void dma_ch5_isr(void) __attribute__ ((weak, alias("unused_isr")));
  141. void dma_ch6_isr(void) __attribute__ ((weak, alias("unused_isr")));
  142. void dma_ch7_isr(void) __attribute__ ((weak, alias("unused_isr")));
  143. void dma_ch8_isr(void) __attribute__ ((weak, alias("unused_isr")));
  144. void dma_ch9_isr(void) __attribute__ ((weak, alias("unused_isr")));
  145. void dma_ch10_isr(void) __attribute__ ((weak, alias("unused_isr")));
  146. void dma_ch11_isr(void) __attribute__ ((weak, alias("unused_isr")));
  147. void dma_ch12_isr(void) __attribute__ ((weak, alias("unused_isr")));
  148. void dma_ch13_isr(void) __attribute__ ((weak, alias("unused_isr")));
  149. void dma_ch14_isr(void) __attribute__ ((weak, alias("unused_isr")));
  150. void dma_ch15_isr(void) __attribute__ ((weak, alias("unused_isr")));
  151. void dma_error_isr(void) __attribute__ ((weak, alias("unused_isr")));
  152. void mcm_isr(void) __attribute__ ((weak, alias("unused_isr")));
  153. void randnum_isr(void) __attribute__ ((weak, alias("unused_isr")));
  154. void flash_cmd_isr(void) __attribute__ ((weak, alias("unused_isr")));
  155. void flash_error_isr(void) __attribute__ ((weak, alias("unused_isr")));
  156. void low_voltage_isr(void) __attribute__ ((weak, alias("unused_isr")));
  157. void wakeup_isr(void) __attribute__ ((weak, alias("unused_isr")));
  158. void watchdog_isr(void) __attribute__ ((weak, alias("unused_isr")));
  159. void i2c0_isr(void) __attribute__ ((weak, alias("unused_isr")));
  160. void i2c1_isr(void) __attribute__ ((weak, alias("unused_isr")));
  161. void i2c2_isr(void) __attribute__ ((weak, alias("unused_isr")));
  162. void i2c3_isr(void) __attribute__ ((weak, alias("unused_isr")));
  163. void spi0_isr(void) __attribute__ ((weak, alias("unused_isr")));
  164. void spi1_isr(void) __attribute__ ((weak, alias("unused_isr")));
  165. void spi2_isr(void) __attribute__ ((weak, alias("unused_isr")));
  166. void sdhc_isr(void) __attribute__ ((weak, alias("unused_isr")));
  167. void enet_timer_isr(void) __attribute__ ((weak, alias("unused_isr")));
  168. void enet_tx_isr(void) __attribute__ ((weak, alias("unused_isr")));
  169. void enet_rx_isr(void) __attribute__ ((weak, alias("unused_isr")));
  170. void enet_error_isr(void) __attribute__ ((weak, alias("unused_isr")));
  171. void can0_message_isr(void) __attribute__ ((weak, alias("unused_isr")));
  172. void can0_bus_off_isr(void) __attribute__ ((weak, alias("unused_isr")));
  173. void can0_error_isr(void) __attribute__ ((weak, alias("unused_isr")));
  174. void can0_tx_warn_isr(void) __attribute__ ((weak, alias("unused_isr")));
  175. void can0_rx_warn_isr(void) __attribute__ ((weak, alias("unused_isr")));
  176. void can0_wakeup_isr(void) __attribute__ ((weak, alias("unused_isr")));
  177. void can1_message_isr(void) __attribute__ ((weak, alias("unused_isr")));
  178. void can1_bus_off_isr(void) __attribute__ ((weak, alias("unused_isr")));
  179. void can1_error_isr(void) __attribute__ ((weak, alias("unused_isr")));
  180. void can1_tx_warn_isr(void) __attribute__ ((weak, alias("unused_isr")));
  181. void can1_rx_warn_isr(void) __attribute__ ((weak, alias("unused_isr")));
  182. void can1_wakeup_isr(void) __attribute__ ((weak, alias("unused_isr")));
  183. void i2s0_tx_isr(void) __attribute__ ((weak, alias("unused_isr")));
  184. void i2s0_rx_isr(void) __attribute__ ((weak, alias("unused_isr")));
  185. void i2s0_isr(void) __attribute__ ((weak, alias("unused_isr")));
  186. void uart0_lon_isr(void) __attribute__ ((weak, alias("unused_isr")));
  187. void uart0_status_isr(void) __attribute__ ((weak, alias("unused_isr")));
  188. void uart0_error_isr(void) __attribute__ ((weak, alias("unused_isr")));
  189. void uart1_status_isr(void) __attribute__ ((weak, alias("unused_isr")));
  190. void uart1_error_isr(void) __attribute__ ((weak, alias("unused_isr")));
  191. void uart2_status_isr(void) __attribute__ ((weak, alias("unused_isr")));
  192. void uart2_error_isr(void) __attribute__ ((weak, alias("unused_isr")));
  193. void uart3_status_isr(void) __attribute__ ((weak, alias("unused_isr")));
  194. void uart3_error_isr(void) __attribute__ ((weak, alias("unused_isr")));
  195. void uart4_status_isr(void) __attribute__ ((weak, alias("unused_isr")));
  196. void uart4_error_isr(void) __attribute__ ((weak, alias("unused_isr")));
  197. void uart5_status_isr(void) __attribute__ ((weak, alias("unused_isr")));
  198. void uart5_error_isr(void) __attribute__ ((weak, alias("unused_isr")));
  199. void lpuart0_status_isr(void) __attribute__ ((weak, alias("unused_isr")));
  200. void adc0_isr(void) __attribute__ ((weak, alias("unused_isr")));
  201. void adc1_isr(void) __attribute__ ((weak, alias("unused_isr")));
  202. void cmp0_isr(void) __attribute__ ((weak, alias("unused_isr")));
  203. void cmp1_isr(void) __attribute__ ((weak, alias("unused_isr")));
  204. void cmp2_isr(void) __attribute__ ((weak, alias("unused_isr")));
  205. void cmp3_isr(void) __attribute__ ((weak, alias("unused_isr")));
  206. void ftm0_isr(void) __attribute__ ((weak, alias("unused_isr")));
  207. void ftm1_isr(void) __attribute__ ((weak, alias("unused_isr")));
  208. void ftm2_isr(void) __attribute__ ((weak, alias("unused_isr")));
  209. void ftm3_isr(void) __attribute__ ((weak, alias("unused_isr")));
  210. void tpm0_isr(void) __attribute__ ((weak, alias("unused_isr")));
  211. void tpm1_isr(void) __attribute__ ((weak, alias("unused_isr")));
  212. void tpm2_isr(void) __attribute__ ((weak, alias("unused_isr")));
  213. void cmt_isr(void) __attribute__ ((weak, alias("unused_isr")));
  214. void rtc_alarm_isr(void) __attribute__ ((weak, alias("unused_isr")));
  215. void rtc_seconds_isr(void) __attribute__ ((weak, alias("unused_isr")));
  216. void pit_isr(void) __attribute__ ((weak, alias("unused_isr")));
  217. void pit0_isr(void) __attribute__ ((weak, alias("unused_isr")));
  218. void pit1_isr(void) __attribute__ ((weak, alias("unused_isr")));
  219. void pit2_isr(void) __attribute__ ((weak, alias("unused_isr")));
  220. void pit3_isr(void) __attribute__ ((weak, alias("unused_isr")));
  221. void pdb_isr(void) __attribute__ ((weak, alias("unused_isr")));
  222. void usb_isr(void) __attribute__ ((weak, alias("unused_isr")));
  223. void usb_charge_isr(void) __attribute__ ((weak, alias("unused_isr")));
  224. void usbhs_isr(void) __attribute__ ((weak, alias("unused_isr")));
  225. void usbhs_phy_isr(void) __attribute__ ((weak, alias("unused_isr")));
  226. void dac0_isr(void) __attribute__ ((weak, alias("unused_isr")));
  227. void dac1_isr(void) __attribute__ ((weak, alias("unused_isr")));
  228. void tsi0_isr(void) __attribute__ ((weak, alias("unused_isr")));
  229. void mcg_isr(void) __attribute__ ((weak, alias("unused_isr")));
  230. void lptmr_isr(void) __attribute__ ((weak, alias("unused_isr")));
  231. void porta_isr(void) __attribute__ ((weak, alias("unused_isr")));
  232. void portb_isr(void) __attribute__ ((weak, alias("unused_isr")));
  233. void portc_isr(void) __attribute__ ((weak, alias("unused_isr")));
  234. void portd_isr(void) __attribute__ ((weak, alias("unused_isr")));
  235. void porte_isr(void) __attribute__ ((weak, alias("unused_isr")));
  236. void portcd_isr(void) __attribute__ ((weak, alias("unused_isr")));
  237. void software_isr(void) __attribute__ ((weak, alias("unused_isr")));
  238. #if defined(__MK20DX128__)
  239. __attribute__ ((section(".dmabuffers"), used, aligned(256)))
  240. #elif defined(__MK20DX256__)
  241. __attribute__ ((section(".dmabuffers"), used, aligned(512)))
  242. #elif defined(__MKL26Z64__)
  243. __attribute__ ((section(".dmabuffers"), used, aligned(256)))
  244. #elif defined(__MK64FX512__)
  245. __attribute__ ((section(".dmabuffers"), used, aligned(512)))
  246. #elif defined(__MK66FX1M0__)
  247. __attribute__ ((section(".dmabuffers"), used, aligned(512)))
  248. #endif
  249. void (* _VectorsRam[NVIC_NUM_INTERRUPTS+16])(void);
  250. __attribute__ ((section(".vectors"), used))
  251. void (* const _VectorsFlash[NVIC_NUM_INTERRUPTS+16])(void) =
  252. {
  253. (void (*)(void))((unsigned long)&_estack), // 0 ARM: Initial Stack Pointer
  254. ResetHandler, // 1 ARM: Initial Program Counter
  255. nmi_isr, // 2 ARM: Non-maskable Interrupt (NMI)
  256. hard_fault_isr, // 3 ARM: Hard Fault
  257. memmanage_fault_isr, // 4 ARM: MemManage Fault
  258. bus_fault_isr, // 5 ARM: Bus Fault
  259. usage_fault_isr, // 6 ARM: Usage Fault
  260. fault_isr, // 7 --
  261. fault_isr, // 8 --
  262. fault_isr, // 9 --
  263. fault_isr, // 10 --
  264. svcall_isr, // 11 ARM: Supervisor call (SVCall)
  265. debugmonitor_isr, // 12 ARM: Debug Monitor
  266. fault_isr, // 13 --
  267. pendablesrvreq_isr, // 14 ARM: Pendable req serv(PendableSrvReq)
  268. systick_isr, // 15 ARM: System tick timer (SysTick)
  269. #if defined(__MK20DX128__)
  270. dma_ch0_isr, // 16 DMA channel 0 transfer complete
  271. dma_ch1_isr, // 17 DMA channel 1 transfer complete
  272. dma_ch2_isr, // 18 DMA channel 2 transfer complete
  273. dma_ch3_isr, // 19 DMA channel 3 transfer complete
  274. dma_error_isr, // 20 DMA error interrupt channel
  275. unused_isr, // 21 DMA --
  276. flash_cmd_isr, // 22 Flash Memory Command complete
  277. flash_error_isr, // 23 Flash Read collision
  278. low_voltage_isr, // 24 Low-voltage detect/warning
  279. wakeup_isr, // 25 Low Leakage Wakeup
  280. watchdog_isr, // 26 Both EWM and WDOG interrupt
  281. i2c0_isr, // 27 I2C0
  282. spi0_isr, // 28 SPI0
  283. i2s0_tx_isr, // 29 I2S0 Transmit
  284. i2s0_rx_isr, // 30 I2S0 Receive
  285. uart0_lon_isr, // 31 UART0 CEA709.1-B (LON) status
  286. uart0_status_isr, // 32 UART0 status
  287. uart0_error_isr, // 33 UART0 error
  288. uart1_status_isr, // 34 UART1 status
  289. uart1_error_isr, // 35 UART1 error
  290. uart2_status_isr, // 36 UART2 status
  291. uart2_error_isr, // 37 UART2 error
  292. adc0_isr, // 38 ADC0
  293. cmp0_isr, // 39 CMP0
  294. cmp1_isr, // 40 CMP1
  295. ftm0_isr, // 41 FTM0
  296. ftm1_isr, // 42 FTM1
  297. cmt_isr, // 43 CMT
  298. rtc_alarm_isr, // 44 RTC Alarm interrupt
  299. rtc_seconds_isr, // 45 RTC Seconds interrupt
  300. pit0_isr, // 46 PIT Channel 0
  301. pit1_isr, // 47 PIT Channel 1
  302. pit2_isr, // 48 PIT Channel 2
  303. pit3_isr, // 49 PIT Channel 3
  304. pdb_isr, // 50 PDB Programmable Delay Block
  305. usb_isr, // 51 USB OTG
  306. usb_charge_isr, // 52 USB Charger Detect
  307. tsi0_isr, // 53 TSI0
  308. mcg_isr, // 54 MCG
  309. lptmr_isr, // 55 Low Power Timer
  310. porta_isr, // 56 Pin detect (Port A)
  311. portb_isr, // 57 Pin detect (Port B)
  312. portc_isr, // 58 Pin detect (Port C)
  313. portd_isr, // 59 Pin detect (Port D)
  314. porte_isr, // 60 Pin detect (Port E)
  315. software_isr, // 61 Software interrupt
  316. #elif defined(__MK20DX256__)
  317. dma_ch0_isr, // 16 DMA channel 0 transfer complete
  318. dma_ch1_isr, // 17 DMA channel 1 transfer complete
  319. dma_ch2_isr, // 18 DMA channel 2 transfer complete
  320. dma_ch3_isr, // 19 DMA channel 3 transfer complete
  321. dma_ch4_isr, // 20 DMA channel 4 transfer complete
  322. dma_ch5_isr, // 21 DMA channel 5 transfer complete
  323. dma_ch6_isr, // 22 DMA channel 6 transfer complete
  324. dma_ch7_isr, // 23 DMA channel 7 transfer complete
  325. dma_ch8_isr, // 24 DMA channel 8 transfer complete
  326. dma_ch9_isr, // 25 DMA channel 9 transfer complete
  327. dma_ch10_isr, // 26 DMA channel 10 transfer complete
  328. dma_ch11_isr, // 27 DMA channel 11 transfer complete
  329. dma_ch12_isr, // 28 DMA channel 12 transfer complete
  330. dma_ch13_isr, // 29 DMA channel 13 transfer complete
  331. dma_ch14_isr, // 30 DMA channel 14 transfer complete
  332. dma_ch15_isr, // 31 DMA channel 15 transfer complete
  333. dma_error_isr, // 32 DMA error interrupt channel
  334. unused_isr, // 33 --
  335. flash_cmd_isr, // 34 Flash Memory Command complete
  336. flash_error_isr, // 35 Flash Read collision
  337. low_voltage_isr, // 36 Low-voltage detect/warning
  338. wakeup_isr, // 37 Low Leakage Wakeup
  339. watchdog_isr, // 38 Both EWM and WDOG interrupt
  340. unused_isr, // 39 --
  341. i2c0_isr, // 40 I2C0
  342. i2c1_isr, // 41 I2C1
  343. spi0_isr, // 42 SPI0
  344. spi1_isr, // 43 SPI1
  345. unused_isr, // 44 --
  346. can0_message_isr, // 45 CAN OR'ed Message buffer (0-15)
  347. can0_bus_off_isr, // 46 CAN Bus Off
  348. can0_error_isr, // 47 CAN Error
  349. can0_tx_warn_isr, // 48 CAN Transmit Warning
  350. can0_rx_warn_isr, // 49 CAN Receive Warning
  351. can0_wakeup_isr, // 50 CAN Wake Up
  352. i2s0_tx_isr, // 51 I2S0 Transmit
  353. i2s0_rx_isr, // 52 I2S0 Receive
  354. unused_isr, // 53 --
  355. unused_isr, // 54 --
  356. unused_isr, // 55 --
  357. unused_isr, // 56 --
  358. unused_isr, // 57 --
  359. unused_isr, // 58 --
  360. unused_isr, // 59 --
  361. uart0_lon_isr, // 60 UART0 CEA709.1-B (LON) status
  362. uart0_status_isr, // 61 UART0 status
  363. uart0_error_isr, // 62 UART0 error
  364. uart1_status_isr, // 63 UART1 status
  365. uart1_error_isr, // 64 UART1 error
  366. uart2_status_isr, // 65 UART2 status
  367. uart2_error_isr, // 66 UART2 error
  368. unused_isr, // 67 --
  369. unused_isr, // 68 --
  370. unused_isr, // 69 --
  371. unused_isr, // 70 --
  372. unused_isr, // 71 --
  373. unused_isr, // 72 --
  374. adc0_isr, // 73 ADC0
  375. adc1_isr, // 74 ADC1
  376. cmp0_isr, // 75 CMP0
  377. cmp1_isr, // 76 CMP1
  378. cmp2_isr, // 77 CMP2
  379. ftm0_isr, // 78 FTM0
  380. ftm1_isr, // 79 FTM1
  381. ftm2_isr, // 80 FTM2
  382. cmt_isr, // 81 CMT
  383. rtc_alarm_isr, // 82 RTC Alarm interrupt
  384. rtc_seconds_isr, // 83 RTC Seconds interrupt
  385. pit0_isr, // 84 PIT Channel 0
  386. pit1_isr, // 85 PIT Channel 1
  387. pit2_isr, // 86 PIT Channel 2
  388. pit3_isr, // 87 PIT Channel 3
  389. pdb_isr, // 88 PDB Programmable Delay Block
  390. usb_isr, // 89 USB OTG
  391. usb_charge_isr, // 90 USB Charger Detect
  392. unused_isr, // 91 --
  393. unused_isr, // 92 --
  394. unused_isr, // 93 --
  395. unused_isr, // 94 --
  396. unused_isr, // 95 --
  397. unused_isr, // 96 --
  398. dac0_isr, // 97 DAC0
  399. unused_isr, // 98 --
  400. tsi0_isr, // 99 TSI0
  401. mcg_isr, // 100 MCG
  402. lptmr_isr, // 101 Low Power Timer
  403. unused_isr, // 102 --
  404. porta_isr, // 103 Pin detect (Port A)
  405. portb_isr, // 104 Pin detect (Port B)
  406. portc_isr, // 105 Pin detect (Port C)
  407. portd_isr, // 106 Pin detect (Port D)
  408. porte_isr, // 107 Pin detect (Port E)
  409. unused_isr, // 108 --
  410. unused_isr, // 109 --
  411. software_isr, // 110 Software interrupt
  412. #elif defined(__MKL26Z64__)
  413. dma_ch0_isr, // 16 DMA channel 0 transfer complete
  414. dma_ch1_isr, // 17 DMA channel 1 transfer complete
  415. dma_ch2_isr, // 18 DMA channel 2 transfer complete
  416. dma_ch3_isr, // 19 DMA channel 3 transfer complete
  417. unused_isr, // 20 --
  418. flash_cmd_isr, // 21 Flash Memory Command complete
  419. low_voltage_isr, // 22 Low-voltage detect/warning
  420. wakeup_isr, // 23 Low Leakage Wakeup
  421. i2c0_isr, // 24 I2C0
  422. i2c1_isr, // 25 I2C1
  423. spi0_isr, // 26 SPI0
  424. spi1_isr, // 27 SPI1
  425. uart0_status_isr, // 28 UART0 status & error
  426. uart1_status_isr, // 29 UART1 status & error
  427. uart2_status_isr, // 30 UART2 status & error
  428. adc0_isr, // 31 ADC0
  429. cmp0_isr, // 32 CMP0
  430. ftm0_isr, // 33 FTM0
  431. ftm1_isr, // 34 FTM1
  432. ftm2_isr, // 35 FTM2
  433. rtc_alarm_isr, // 36 RTC Alarm interrupt
  434. rtc_seconds_isr, // 37 RTC Seconds interrupt
  435. pit_isr, // 38 PIT Both Channels
  436. i2s0_isr, // 39 I2S0 Transmit & Receive
  437. usb_isr, // 40 USB OTG
  438. dac0_isr, // 41 DAC0
  439. tsi0_isr, // 42 TSI0
  440. mcg_isr, // 43 MCG
  441. lptmr_isr, // 44 Low Power Timer
  442. software_isr, // 45 Software interrupt
  443. porta_isr, // 46 Pin detect (Port A)
  444. portcd_isr, // 47 Pin detect (Port C and D)
  445. #elif defined(__MK64FX512__)
  446. dma_ch0_isr, // 16 DMA channel 0 transfer complete
  447. dma_ch1_isr, // 17 DMA channel 1 transfer complete
  448. dma_ch2_isr, // 18 DMA channel 2 transfer complete
  449. dma_ch3_isr, // 19 DMA channel 3 transfer complete
  450. dma_ch4_isr, // 20 DMA channel 4 transfer complete
  451. dma_ch5_isr, // 21 DMA channel 5 transfer complete
  452. dma_ch6_isr, // 22 DMA channel 6 transfer complete
  453. dma_ch7_isr, // 23 DMA channel 7 transfer complete
  454. dma_ch8_isr, // 24 DMA channel 8 transfer complete
  455. dma_ch9_isr, // 25 DMA channel 9 transfer complete
  456. dma_ch10_isr, // 26 DMA channel 10 transfer complete
  457. dma_ch11_isr, // 27 DMA channel 11 transfer complete
  458. dma_ch12_isr, // 28 DMA channel 12 transfer complete
  459. dma_ch13_isr, // 29 DMA channel 13 transfer complete
  460. dma_ch14_isr, // 30 DMA channel 14 transfer complete
  461. dma_ch15_isr, // 31 DMA channel 15 transfer complete
  462. dma_error_isr, // 32 DMA error interrupt channel
  463. mcm_isr, // 33 MCM
  464. flash_cmd_isr, // 34 Flash Memory Command complete
  465. flash_error_isr, // 35 Flash Read collision
  466. low_voltage_isr, // 36 Low-voltage detect/warning
  467. wakeup_isr, // 37 Low Leakage Wakeup
  468. watchdog_isr, // 38 Both EWM and WDOG interrupt
  469. randnum_isr, // 39 Random Number Generator
  470. i2c0_isr, // 40 I2C0
  471. i2c1_isr, // 41 I2C1
  472. spi0_isr, // 42 SPI0
  473. spi1_isr, // 43 SPI1
  474. i2s0_tx_isr, // 44 I2S0 Transmit
  475. i2s0_rx_isr, // 45 I2S0 Receive
  476. unused_isr, // 46 --
  477. uart0_status_isr, // 47 UART0 status
  478. uart0_error_isr, // 48 UART0 error
  479. uart1_status_isr, // 49 UART1 status
  480. uart1_error_isr, // 50 UART1 error
  481. uart2_status_isr, // 51 UART2 status
  482. uart2_error_isr, // 52 UART2 error
  483. uart3_status_isr, // 53 UART3 status
  484. uart3_error_isr, // 54 UART3 error
  485. adc0_isr, // 55 ADC0
  486. cmp0_isr, // 56 CMP0
  487. cmp1_isr, // 57 CMP1
  488. ftm0_isr, // 58 FTM0
  489. ftm1_isr, // 59 FTM1
  490. ftm2_isr, // 60 FTM2
  491. cmt_isr, // 61 CMT
  492. rtc_alarm_isr, // 62 RTC Alarm interrupt
  493. rtc_seconds_isr, // 63 RTC Seconds interrupt
  494. pit0_isr, // 64 PIT Channel 0
  495. pit1_isr, // 65 PIT Channel 1
  496. pit2_isr, // 66 PIT Channel 2
  497. pit3_isr, // 67 PIT Channel 3
  498. pdb_isr, // 68 PDB Programmable Delay Block
  499. usb_isr, // 69 USB OTG
  500. usb_charge_isr, // 70 USB Charger Detect
  501. unused_isr, // 71 --
  502. dac0_isr, // 72 DAC0
  503. mcg_isr, // 73 MCG
  504. lptmr_isr, // 74 Low Power Timer
  505. porta_isr, // 75 Pin detect (Port A)
  506. portb_isr, // 76 Pin detect (Port B)
  507. portc_isr, // 77 Pin detect (Port C)
  508. portd_isr, // 78 Pin detect (Port D)
  509. porte_isr, // 79 Pin detect (Port E)
  510. software_isr, // 80 Software interrupt
  511. spi2_isr, // 81 SPI2
  512. uart4_status_isr, // 82 UART4 status
  513. uart4_error_isr, // 83 UART4 error
  514. uart5_status_isr, // 84 UART4 status
  515. uart5_error_isr, // 85 UART4 error
  516. cmp2_isr, // 86 CMP2
  517. ftm3_isr, // 87 FTM3
  518. dac1_isr, // 88 DAC1
  519. adc1_isr, // 89 ADC1
  520. i2c2_isr, // 90 I2C2
  521. can0_message_isr, // 91 CAN OR'ed Message buffer (0-15)
  522. can0_bus_off_isr, // 92 CAN Bus Off
  523. can0_error_isr, // 93 CAN Error
  524. can0_tx_warn_isr, // 94 CAN Transmit Warning
  525. can0_rx_warn_isr, // 95 CAN Receive Warning
  526. can0_wakeup_isr, // 96 CAN Wake Up
  527. sdhc_isr, // 97 SDHC
  528. enet_timer_isr, // 98 Ethernet IEEE1588 Timers
  529. enet_tx_isr, // 99 Ethernet Transmit
  530. enet_rx_isr, // 100 Ethernet Receive
  531. enet_error_isr, // 101 Ethernet Error
  532. #elif defined(__MK66FX1M0__)
  533. dma_ch0_isr, // 16 DMA channel 0 transfer complete
  534. dma_ch1_isr, // 17 DMA channel 1 transfer complete
  535. dma_ch2_isr, // 18 DMA channel 2 transfer complete
  536. dma_ch3_isr, // 19 DMA channel 3 transfer complete
  537. dma_ch4_isr, // 20 DMA channel 4 transfer complete
  538. dma_ch5_isr, // 21 DMA channel 5 transfer complete
  539. dma_ch6_isr, // 22 DMA channel 6 transfer complete
  540. dma_ch7_isr, // 23 DMA channel 7 transfer complete
  541. dma_ch8_isr, // 24 DMA channel 8 transfer complete
  542. dma_ch9_isr, // 25 DMA channel 9 transfer complete
  543. dma_ch10_isr, // 26 DMA channel 10 transfer complete
  544. dma_ch11_isr, // 27 DMA channel 11 transfer complete
  545. dma_ch12_isr, // 28 DMA channel 12 transfer complete
  546. dma_ch13_isr, // 29 DMA channel 13 transfer complete
  547. dma_ch14_isr, // 30 DMA channel 14 transfer complete
  548. dma_ch15_isr, // 31 DMA channel 15 transfer complete
  549. dma_error_isr, // 32 DMA error interrupt channel
  550. mcm_isr, // 33 MCM
  551. flash_cmd_isr, // 34 Flash Memory Command complete
  552. flash_error_isr, // 35 Flash Read collision
  553. low_voltage_isr, // 36 Low-voltage detect/warning
  554. wakeup_isr, // 37 Low Leakage Wakeup
  555. watchdog_isr, // 38 Both EWM and WDOG interrupt
  556. randnum_isr, // 39 Random Number Generator
  557. i2c0_isr, // 40 I2C0
  558. i2c1_isr, // 41 I2C1
  559. spi0_isr, // 42 SPI0
  560. spi1_isr, // 43 SPI1
  561. i2s0_tx_isr, // 44 I2S0 Transmit
  562. i2s0_rx_isr, // 45 I2S0 Receive
  563. unused_isr, // 46 --
  564. uart0_status_isr, // 47 UART0 status
  565. uart0_error_isr, // 48 UART0 error
  566. uart1_status_isr, // 49 UART1 status
  567. uart1_error_isr, // 50 UART1 error
  568. uart2_status_isr, // 51 UART2 status
  569. uart2_error_isr, // 52 UART2 error
  570. uart3_status_isr, // 53 UART3 status
  571. uart3_error_isr, // 54 UART3 error
  572. adc0_isr, // 55 ADC0
  573. cmp0_isr, // 56 CMP0
  574. cmp1_isr, // 57 CMP1
  575. ftm0_isr, // 58 FTM0
  576. ftm1_isr, // 59 FTM1
  577. ftm2_isr, // 60 FTM2
  578. cmt_isr, // 61 CMT
  579. rtc_alarm_isr, // 62 RTC Alarm interrupt
  580. rtc_seconds_isr, // 63 RTC Seconds interrupt
  581. pit0_isr, // 64 PIT Channel 0
  582. pit1_isr, // 65 PIT Channel 1
  583. pit2_isr, // 66 PIT Channel 2
  584. pit3_isr, // 67 PIT Channel 3
  585. pdb_isr, // 68 PDB Programmable Delay Block
  586. usb_isr, // 69 USB OTG
  587. usb_charge_isr, // 70 USB Charger Detect
  588. unused_isr, // 71 --
  589. dac0_isr, // 72 DAC0
  590. mcg_isr, // 73 MCG
  591. lptmr_isr, // 74 Low Power Timer
  592. porta_isr, // 75 Pin detect (Port A)
  593. portb_isr, // 76 Pin detect (Port B)
  594. portc_isr, // 77 Pin detect (Port C)
  595. portd_isr, // 78 Pin detect (Port D)
  596. porte_isr, // 79 Pin detect (Port E)
  597. software_isr, // 80 Software interrupt
  598. spi2_isr, // 81 SPI2
  599. uart4_status_isr, // 82 UART4 status
  600. uart4_error_isr, // 83 UART4 error
  601. unused_isr, // 84 --
  602. unused_isr, // 85 --
  603. cmp2_isr, // 86 CMP2
  604. ftm3_isr, // 87 FTM3
  605. dac1_isr, // 88 DAC1
  606. adc1_isr, // 89 ADC1
  607. i2c2_isr, // 90 I2C2
  608. can0_message_isr, // 91 CAN OR'ed Message buffer (0-15)
  609. can0_bus_off_isr, // 92 CAN Bus Off
  610. can0_error_isr, // 93 CAN Error
  611. can0_tx_warn_isr, // 94 CAN Transmit Warning
  612. can0_rx_warn_isr, // 95 CAN Receive Warning
  613. can0_wakeup_isr, // 96 CAN Wake Up
  614. sdhc_isr, // 97 SDHC
  615. enet_timer_isr, // 98 Ethernet IEEE1588 Timers
  616. enet_tx_isr, // 99 Ethernet Transmit
  617. enet_rx_isr, // 100 Ethernet Receive
  618. enet_error_isr, // 101 Ethernet Error
  619. lpuart0_status_isr, // 102 LPUART
  620. tsi0_isr, // 103 TSI0
  621. tpm1_isr, // 104 FTM1
  622. tpm2_isr, // 105 FTM2
  623. usbhs_phy_isr, // 106 USB-HS Phy
  624. i2c3_isr, // 107 I2C3
  625. cmp3_isr, // 108 CMP3
  626. usbhs_isr, // 109 USB-HS
  627. can1_message_isr, // 110 CAN OR'ed Message buffer (0-15)
  628. can1_bus_off_isr, // 111 CAN Bus Off
  629. can1_error_isr, // 112 CAN Error
  630. can1_tx_warn_isr, // 113 CAN Transmit Warning
  631. can1_rx_warn_isr, // 114 CAN Receive Warning
  632. can1_wakeup_isr, // 115 CAN Wake Up
  633. #endif
  634. };
  635. __attribute__ ((section(".flashconfig"), used))
  636. const uint8_t flashconfigbytes[16] = {
  637. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  638. 0xFF, 0xFF, 0xFF, 0xFF, FSEC, FOPT, 0xFF, 0xFF
  639. };
  640. // Automatically initialize the RTC. When the build defines the compile
  641. // time, and the user has added a crystal, the RTC will automatically
  642. // begin at the time of the first upload.
  643. #ifndef TIME_T
  644. #define TIME_T 1349049600 // default 1 Oct 2012 (never used, Arduino sets this)
  645. #endif
  646. extern void *__rtc_localtime; // Arduino build process sets this
  647. extern void rtc_set(unsigned long t);
  648. static void startup_default_early_hook(void) {
  649. #if defined(KINETISK)
  650. WDOG_STCTRLH = WDOG_STCTRLH_ALLOWUPDATE;
  651. #elif defined(KINETISL)
  652. SIM_COPC = 0; // disable the watchdog
  653. #endif
  654. }
  655. static void startup_default_late_hook(void) {}
  656. void startup_early_hook(void) __attribute__ ((weak, alias("startup_default_early_hook")));
  657. void startup_late_hook(void) __attribute__ ((weak, alias("startup_default_late_hook")));
  658. #if defined(__PURE_CODE__) || !defined(__OPTIMIZE__) || defined(__clang__)
  659. // cases known to compile too large for 0-0x400 memory region
  660. __attribute__ ((optimize("-Os")))
  661. #else
  662. // hopefully all others fit into startup section (below 0x400)
  663. __attribute__ ((section(".startup"),optimize("-Os")))
  664. #endif
  665. void ResetHandler(void)
  666. {
  667. uint32_t *src = &_etext;
  668. uint32_t *dest = &_sdata;
  669. unsigned int i;
  670. #if F_CPU <= 2000000
  671. volatile int n;
  672. #endif
  673. //volatile int count;
  674. #ifdef KINETISK
  675. WDOG_UNLOCK = WDOG_UNLOCK_SEQ1;
  676. WDOG_UNLOCK = WDOG_UNLOCK_SEQ2;
  677. __asm__ volatile ("nop");
  678. __asm__ volatile ("nop");
  679. #endif
  680. // programs using the watchdog timer or needing to initialize hardware as
  681. // early as possible can implement startup_early_hook()
  682. startup_early_hook();
  683. // enable clocks to always-used peripherals
  684. #if defined(__MK20DX128__)
  685. SIM_SCGC5 = 0x00043F82; // clocks active to all GPIO
  686. SIM_SCGC6 = SIM_SCGC6_RTC | SIM_SCGC6_FTM0 | SIM_SCGC6_FTM1 | SIM_SCGC6_ADC0 | SIM_SCGC6_FTFL;
  687. #elif defined(__MK20DX256__)
  688. SIM_SCGC3 = SIM_SCGC3_ADC1 | SIM_SCGC3_FTM2;
  689. SIM_SCGC5 = 0x00043F82; // clocks active to all GPIO
  690. SIM_SCGC6 = SIM_SCGC6_RTC | SIM_SCGC6_FTM0 | SIM_SCGC6_FTM1 | SIM_SCGC6_ADC0 | SIM_SCGC6_FTFL;
  691. #elif defined(__MK64FX512__) || defined(__MK66FX1M0__)
  692. SIM_SCGC3 = SIM_SCGC3_ADC1 | SIM_SCGC3_FTM2 | SIM_SCGC3_FTM3;
  693. SIM_SCGC5 = 0x00043F82; // clocks active to all GPIO
  694. SIM_SCGC6 = SIM_SCGC6_RTC | SIM_SCGC6_FTM0 | SIM_SCGC6_FTM1 | SIM_SCGC6_ADC0 | SIM_SCGC6_FTFL;
  695. //PORTC_PCR5 = PORT_PCR_MUX(1) | PORT_PCR_DSE | PORT_PCR_SRE;
  696. //GPIOC_PDDR |= (1<<5);
  697. //GPIOC_PSOR = (1<<5);
  698. //while (1);
  699. #elif defined(__MKL26Z64__)
  700. SIM_SCGC4 = SIM_SCGC4_USBOTG | 0xF0000030;
  701. SIM_SCGC5 = 0x00003F82; // clocks active to all GPIO
  702. SIM_SCGC6 = SIM_SCGC6_ADC0 | SIM_SCGC6_TPM0 | SIM_SCGC6_TPM1 | SIM_SCGC6_TPM2 | SIM_SCGC6_FTFL;
  703. #endif
  704. #if defined(__MK64FX512__) || defined(__MK66FX1M0__)
  705. SCB_CPACR = 0x00F00000;
  706. #endif
  707. #if defined(__MK66FX1M0__)
  708. LMEM_PCCCR = 0x85000003;
  709. #endif
  710. #if 0
  711. // testing only, enable ser_print
  712. SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIV1(0) | SIM_CLKDIV1_OUTDIV4(1);
  713. MCG_C4 |= MCG_C4_DMX32 | MCG_C4_DRST_DRS(1);
  714. SIM_SOPT2 = SIM_SOPT2_UART0SRC(1) | SIM_SOPT2_TPMSRC(1);
  715. SIM_SCGC4 |= 0x00000400;
  716. UART0_BDH = 0;
  717. UART0_BDL = 26; // 115200 at 48 MHz
  718. UART0_C2 = UART_C2_TE;
  719. PORTB_PCR17 = PORT_PCR_MUX(3);
  720. #endif
  721. #if defined(KINETISK) && !defined(__MK66FX1M0__)
  722. // If the RTC oscillator isn't enabled, get it started early.
  723. // But don't do this early on Teensy 3.6 - RTC_CR depends on 3.3V+VBAT
  724. // which may be ~0.4V "behind" 3.3V if the power ramps up slowly.
  725. if (!(RTC_CR & RTC_CR_OSCE)) {
  726. RTC_SR = 0;
  727. RTC_CR = RTC_CR_SC16P | RTC_CR_SC4P | RTC_CR_OSCE;
  728. }
  729. #endif
  730. // release I/O pins hold, if we woke up from VLLS mode
  731. if (PMC_REGSC & PMC_REGSC_ACKISO) PMC_REGSC |= PMC_REGSC_ACKISO;
  732. // since this is a write once register, make it visible to all F_CPU's
  733. // so we can into other sleep modes in the future at any speed
  734. #if defined(__MK66FX1M0__)
  735. SMC_PMPROT = SMC_PMPROT_AHSRUN | SMC_PMPROT_AVLP | SMC_PMPROT_ALLS | SMC_PMPROT_AVLLS;
  736. #else
  737. SMC_PMPROT = SMC_PMPROT_AVLP | SMC_PMPROT_ALLS | SMC_PMPROT_AVLLS;
  738. #endif
  739. // TODO: do this while the PLL is waiting to lock....
  740. while (dest < &_edata) *dest++ = *src++;
  741. dest = &_sbss;
  742. while (dest < &_ebss) *dest++ = 0;
  743. // default all interrupts to medium priority level
  744. for (i=0; i < NVIC_NUM_INTERRUPTS + 16; i++) _VectorsRam[i] = _VectorsFlash[i];
  745. for (i=0; i < NVIC_NUM_INTERRUPTS; i++) NVIC_SET_PRIORITY(i, 128);
  746. SCB_VTOR = (uint32_t)_VectorsRam; // use vector table in RAM
  747. // hardware always starts in FEI mode
  748. // C1[CLKS] bits are written to 00
  749. // C1[IREFS] bit is written to 1
  750. // C6[PLLS] bit is written to 0
  751. // MCG_SC[FCDIV] defaults to divide by two for internal ref clock
  752. // I tried changing MSG_SC to divide by 1, it didn't work for me
  753. #if F_CPU <= 2000000
  754. #if defined(KINETISK)
  755. MCG_C1 = MCG_C1_CLKS(1) | MCG_C1_IREFS;
  756. #elif defined(KINETISL)
  757. // use the internal oscillator
  758. MCG_C1 = MCG_C1_CLKS(1) | MCG_C1_IREFS | MCG_C1_IRCLKEN;
  759. #endif
  760. // wait for MCGOUT to use oscillator
  761. while ((MCG_S & MCG_S_CLKST_MASK) != MCG_S_CLKST(1)) ;
  762. for (n=0; n<10; n++) ; // TODO: why do we get 2 mA extra without this delay?
  763. MCG_C2 = MCG_C2_IRCS;
  764. while (!(MCG_S & MCG_S_IRCST)) ;
  765. // now in FBI mode:
  766. // C1[CLKS] bits are written to 01
  767. // C1[IREFS] bit is written to 1
  768. // C6[PLLS] is written to 0
  769. // C2[LP] is written to 0
  770. MCG_C2 = MCG_C2_IRCS | MCG_C2_LP;
  771. // now in BLPI mode:
  772. // C1[CLKS] bits are written to 01
  773. // C1[IREFS] bit is written to 1
  774. // C6[PLLS] bit is written to 0
  775. // C2[LP] bit is written to 1
  776. #else
  777. #if defined(KINETISK)
  778. // enable capacitors for crystal
  779. OSC0_CR = OSC_SC8P | OSC_SC2P | OSC_ERCLKEN;
  780. #elif defined(KINETISL)
  781. // enable capacitors for crystal
  782. OSC0_CR = OSC_SC8P | OSC_SC2P | OSC_ERCLKEN;
  783. #endif
  784. // enable osc, 8-32 MHz range, low power mode
  785. MCG_C2 = MCG_C2_RANGE0(2) | MCG_C2_EREFS;
  786. // switch to crystal as clock source, FLL input = 16 MHz / 512
  787. MCG_C1 = MCG_C1_CLKS(2) | MCG_C1_FRDIV(4);
  788. // wait for crystal oscillator to begin
  789. while ((MCG_S & MCG_S_OSCINIT0) == 0) ;
  790. // wait for FLL to use oscillator
  791. while ((MCG_S & MCG_S_IREFST) != 0) ;
  792. // wait for MCGOUT to use oscillator
  793. while ((MCG_S & MCG_S_CLKST_MASK) != MCG_S_CLKST(2)) ;
  794. // now in FBE mode
  795. // C1[CLKS] bits are written to 10
  796. // C1[IREFS] bit is written to 0
  797. // C1[FRDIV] must be written to divide xtal to 31.25-39 kHz
  798. // C6[PLLS] bit is written to 0
  799. // C2[LP] is written to 0
  800. #if F_CPU <= 16000000
  801. // if the crystal is fast enough, use it directly (no FLL or PLL)
  802. MCG_C2 = MCG_C2_RANGE0(2) | MCG_C2_EREFS | MCG_C2_LP;
  803. // BLPE mode:
  804. // C1[CLKS] bits are written to 10
  805. // C1[IREFS] bit is written to 0
  806. // C2[LP] bit is written to 1
  807. #else
  808. // if we need faster than the crystal, turn on the PLL
  809. #if defined(__MK66FX1M0__)
  810. #if F_CPU > 120000000
  811. SMC_PMCTRL = SMC_PMCTRL_RUNM(3); // enter HSRUN mode
  812. while (SMC_PMSTAT != SMC_PMSTAT_HSRUN) ; // wait for HSRUN
  813. #endif
  814. #if F_CPU == 256000000
  815. //See table in 27.4.6 MCG Control 6 Register (MCG_C6)
  816. //16 -> Multiply factor 32. 32*8MHz =256MHz
  817. MCG_C5 = MCG_C5_PRDIV0(0);
  818. MCG_C6 = MCG_C6_PLLS | MCG_C6_VDIV0(16);
  819. #elif F_CPU == 240000000
  820. MCG_C5 = MCG_C5_PRDIV0(0);
  821. MCG_C6 = MCG_C6_PLLS | MCG_C6_VDIV0(14);
  822. #elif F_CPU == 216000000
  823. MCG_C5 = MCG_C5_PRDIV0(0);
  824. MCG_C6 = MCG_C6_PLLS | MCG_C6_VDIV0(11);
  825. #elif F_CPU == 192000000
  826. MCG_C5 = MCG_C5_PRDIV0(0);
  827. MCG_C6 = MCG_C6_PLLS | MCG_C6_VDIV0(8);
  828. #elif F_CPU == 180000000
  829. MCG_C5 = MCG_C5_PRDIV0(1);
  830. MCG_C6 = MCG_C6_PLLS | MCG_C6_VDIV0(29);
  831. #elif F_CPU == 168000000
  832. MCG_C5 = MCG_C5_PRDIV0(0);
  833. MCG_C6 = MCG_C6_PLLS | MCG_C6_VDIV0(5);
  834. #elif F_CPU == 144000000
  835. MCG_C5 = MCG_C5_PRDIV0(0);
  836. MCG_C6 = MCG_C6_PLLS | MCG_C6_VDIV0(2);
  837. #elif F_CPU == 120000000
  838. MCG_C5 = MCG_C5_PRDIV0(1);
  839. MCG_C6 = MCG_C6_PLLS | MCG_C6_VDIV0(14);
  840. #elif F_CPU == 96000000 || F_CPU == 48000000 || F_CPU == 24000000
  841. MCG_C5 = MCG_C5_PRDIV0(1);
  842. MCG_C6 = MCG_C6_PLLS | MCG_C6_VDIV0(8);
  843. #elif F_CPU == 72000000
  844. MCG_C5 = MCG_C5_PRDIV0(1);
  845. MCG_C6 = MCG_C6_PLLS | MCG_C6_VDIV0(2);
  846. #elif F_CPU > 16000000
  847. #error "MK66FX1M0 does not support this clock speed yet...."
  848. #endif
  849. #else
  850. #if F_CPU == 72000000
  851. MCG_C5 = MCG_C5_PRDIV0(5); // config PLL input for 16 MHz Crystal / 6 = 2.667 Hz
  852. #else
  853. MCG_C5 = MCG_C5_PRDIV0(3); // config PLL input for 16 MHz Crystal / 4 = 4 MHz
  854. #endif
  855. #if F_CPU == 168000000
  856. MCG_C6 = MCG_C6_PLLS | MCG_C6_VDIV0(18); // config PLL for 168 MHz output
  857. #elif F_CPU == 144000000
  858. MCG_C6 = MCG_C6_PLLS | MCG_C6_VDIV0(12); // config PLL for 144 MHz output
  859. #elif F_CPU == 120000000
  860. MCG_C6 = MCG_C6_PLLS | MCG_C6_VDIV0(6); // config PLL for 120 MHz output
  861. #elif F_CPU == 72000000
  862. MCG_C6 = MCG_C6_PLLS | MCG_C6_VDIV0(3); // config PLL for 72 MHz output
  863. #elif F_CPU == 96000000 || F_CPU == 48000000 || F_CPU == 24000000
  864. MCG_C6 = MCG_C6_PLLS | MCG_C6_VDIV0(0); // config PLL for 96 MHz output
  865. #elif F_CPU > 16000000
  866. #error "This clock speed isn't supported..."
  867. #endif
  868. #endif
  869. // wait for PLL to start using xtal as its input
  870. while (!(MCG_S & MCG_S_PLLST)) ;
  871. // wait for PLL to lock
  872. while (!(MCG_S & MCG_S_LOCK0)) ;
  873. // now we're in PBE mode
  874. #endif
  875. #endif
  876. // now program the clock dividers
  877. #if F_CPU == 256000000
  878. // config divisors: 256 MHz core, 64 MHz bus, 32 MHz flash, USB = IRC48M
  879. // TODO: gradual ramp-up for HSRUN mode
  880. #if F_BUS == 64000000
  881. SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIV1(0) | SIM_CLKDIV1_OUTDIV2(3) | SIM_CLKDIV1_OUTDIV4(7);
  882. #elif F_BUS == 128000000
  883. SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIV1(0) | SIM_CLKDIV1_OUTDIV2(1) | SIM_CLKDIV1_OUTDIV4(7);
  884. #else
  885. #error "This F_CPU & F_BUS combination is not supported"
  886. #endif
  887. SIM_CLKDIV2 = SIM_CLKDIV2_USBDIV(0);
  888. #elif F_CPU == 240000000
  889. // config divisors: 240 MHz core, 60 MHz bus, 30 MHz flash, USB = 240 / 5
  890. // TODO: gradual ramp-up for HSRUN mode
  891. #if F_BUS == 60000000
  892. SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIV1(0) | SIM_CLKDIV1_OUTDIV2(3) | SIM_CLKDIV1_OUTDIV4(7);
  893. #elif F_BUS == 80000000
  894. SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIV1(0) | SIM_CLKDIV1_OUTDIV2(2) | SIM_CLKDIV1_OUTDIV4(7);
  895. #elif F_BUS == 120000000
  896. SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIV1(0) | SIM_CLKDIV1_OUTDIV2(1) | SIM_CLKDIV1_OUTDIV4(7);
  897. #else
  898. #error "This F_CPU & F_BUS combination is not supported"
  899. #endif
  900. SIM_CLKDIV2 = SIM_CLKDIV2_USBDIV(4);
  901. #elif F_CPU == 216000000
  902. // config divisors: 216 MHz core, 54 MHz bus, 27 MHz flash, USB = IRC48M
  903. // TODO: gradual ramp-up for HSRUN mode
  904. #if F_BUS == 54000000
  905. SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIV1(0) | SIM_CLKDIV1_OUTDIV2(3) | SIM_CLKDIV1_OUTDIV4(7);
  906. #elif F_BUS == 72000000
  907. SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIV1(0) | SIM_CLKDIV1_OUTDIV2(2) | SIM_CLKDIV1_OUTDIV4(7);
  908. #elif F_BUS == 108000000
  909. SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIV1(0) | SIM_CLKDIV1_OUTDIV2(1) | SIM_CLKDIV1_OUTDIV4(7);
  910. #else
  911. #error "This F_CPU & F_BUS combination is not supported"
  912. #endif
  913. SIM_CLKDIV2 = SIM_CLKDIV2_USBDIV(0);
  914. #elif F_CPU == 192000000
  915. // config divisors: 192 MHz core, 48 MHz bus, 27.4 MHz flash, USB = 192 / 4
  916. // TODO: gradual ramp-up for HSRUN mode
  917. #if F_BUS == 48000000
  918. SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIV1(0) | SIM_CLKDIV1_OUTDIV2(3) | SIM_CLKDIV1_OUTDIV4(6);
  919. #elif F_BUS == 64000000
  920. SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIV1(0) | SIM_CLKDIV1_OUTDIV2(2) | SIM_CLKDIV1_OUTDIV4(6);
  921. #elif F_BUS == 96000000
  922. SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIV1(0) | SIM_CLKDIV1_OUTDIV2(1) | SIM_CLKDIV1_OUTDIV4(6);
  923. #else
  924. #error "This F_CPU & F_BUS combination is not supported"
  925. #endif
  926. SIM_CLKDIV2 = SIM_CLKDIV2_USBDIV(3);
  927. #elif F_CPU == 180000000
  928. // config divisors: 180 MHz core, 60 MHz bus, 25.7 MHz flash, USB = IRC48M
  929. #if F_BUS == 60000000
  930. SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIV1(0) | SIM_CLKDIV1_OUTDIV2(2) | SIM_CLKDIV1_OUTDIV4(6);
  931. #elif F_BUS == 90000000
  932. SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIV1(0) | SIM_CLKDIV1_OUTDIV2(1) | SIM_CLKDIV1_OUTDIV4(6);
  933. #else
  934. #error "This F_CPU & F_BUS combination is not supported"
  935. #endif
  936. SIM_CLKDIV2 = SIM_CLKDIV2_USBDIV(0);
  937. #elif F_CPU == 168000000
  938. // config divisors: 168 MHz core, 56 MHz bus, 28 MHz flash, USB = 168 * 2 / 7
  939. SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIV1(0) | SIM_CLKDIV1_OUTDIV2(2) | SIM_CLKDIV1_OUTDIV4(5);
  940. SIM_CLKDIV2 = SIM_CLKDIV2_USBDIV(6) | SIM_CLKDIV2_USBFRAC;
  941. #elif F_CPU == 144000000
  942. // config divisors: 144 MHz core, 48 MHz bus, 28.8 MHz flash, USB = 144 / 3
  943. #if F_BUS == 48000000
  944. SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIV1(0) | SIM_CLKDIV1_OUTDIV2(2) | SIM_CLKDIV1_OUTDIV4(4);
  945. #elif F_BUS == 72000000
  946. SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIV1(0) | SIM_CLKDIV1_OUTDIV2(1) | SIM_CLKDIV1_OUTDIV4(4);
  947. #else
  948. #error "This F_CPU & F_BUS combination is not supported"
  949. #endif
  950. SIM_CLKDIV2 = SIM_CLKDIV2_USBDIV(2);
  951. #elif F_CPU == 120000000
  952. // config divisors: 120 MHz core, 60 MHz bus, 24 MHz flash, USB = 128 * 2 / 5
  953. #if F_BUS == 60000000
  954. SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIV1(0) | SIM_CLKDIV1_OUTDIV2(1) | SIM_CLKDIV1_OUTDIV4(4);
  955. #elif F_BUS == 120000000
  956. SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIV1(0) | SIM_CLKDIV1_OUTDIV2(0) | SIM_CLKDIV1_OUTDIV4(4);
  957. #else
  958. #error "This F_CPU & F_BUS combination is not supported"
  959. #endif
  960. SIM_CLKDIV2 = SIM_CLKDIV2_USBDIV(4) | SIM_CLKDIV2_USBFRAC;
  961. #elif F_CPU == 96000000
  962. // config divisors: 96 MHz core, 48 MHz bus, 24 MHz flash, USB = 96 / 2
  963. #if F_BUS == 48000000
  964. SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIV1(0) | SIM_CLKDIV1_OUTDIV2(1) | SIM_CLKDIV1_OUTDIV4(3);
  965. #elif F_BUS == 96000000
  966. SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIV1(0) | SIM_CLKDIV1_OUTDIV2(0) | SIM_CLKDIV1_OUTDIV4(3);
  967. #else
  968. #error "This F_CPU & F_BUS combination is not supported"
  969. #endif
  970. SIM_CLKDIV2 = SIM_CLKDIV2_USBDIV(1);
  971. #elif F_CPU == 72000000
  972. // config divisors: 72 MHz core, 36 MHz bus, 24 MHz flash, USB = 72 * 2 / 3
  973. #if F_BUS == 36000000
  974. SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIV1(0) | SIM_CLKDIV1_OUTDIV2(1) | SIM_CLKDIV1_OUTDIV4(2);
  975. #elif F_BUS == 72000000
  976. SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIV1(0) | SIM_CLKDIV1_OUTDIV2(0) | SIM_CLKDIV1_OUTDIV4(2);
  977. #else
  978. #error "This F_CPU & F_BUS combination is not supported"
  979. #endif
  980. SIM_CLKDIV2 = SIM_CLKDIV2_USBDIV(2) | SIM_CLKDIV2_USBFRAC;
  981. #elif F_CPU == 48000000
  982. // config divisors: 48 MHz core, 48 MHz bus, 24 MHz flash, USB = 96 / 2
  983. #if defined(KINETISK)
  984. SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIV1(1) | SIM_CLKDIV1_OUTDIV2(1) | SIM_CLKDIV1_OUTDIV3(1) | SIM_CLKDIV1_OUTDIV4(3);
  985. SIM_CLKDIV2 = SIM_CLKDIV2_USBDIV(1);
  986. #elif defined(KINETISL)
  987. SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIV1(1) | SIM_CLKDIV1_OUTDIV4(1);
  988. #endif
  989. #elif F_CPU == 24000000
  990. // config divisors: 24 MHz core, 24 MHz bus, 24 MHz flash, USB = 96 / 2
  991. #if defined(KINETISK)
  992. SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIV1(3) | SIM_CLKDIV1_OUTDIV2(3) | SIM_CLKDIV1_OUTDIV3(3) | SIM_CLKDIV1_OUTDIV4(3);
  993. SIM_CLKDIV2 = SIM_CLKDIV2_USBDIV(1);
  994. #elif defined(KINETISL)
  995. SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIV1(3) | SIM_CLKDIV1_OUTDIV4(0);
  996. #endif
  997. #elif F_CPU == 16000000
  998. // config divisors: 16 MHz core, 16 MHz bus, 16 MHz flash
  999. #if defined(KINETISK)
  1000. SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIV1(0) | SIM_CLKDIV1_OUTDIV2(0) | SIM_CLKDIV1_OUTDIV3(0) | SIM_CLKDIV1_OUTDIV4(0);
  1001. #elif defined(KINETISL)
  1002. SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIV1(0) | SIM_CLKDIV1_OUTDIV4(0);
  1003. #endif
  1004. #elif F_CPU == 8000000
  1005. // config divisors: 8 MHz core, 8 MHz bus, 8 MHz flash
  1006. #if defined(KINETISK)
  1007. SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIV1(1) | SIM_CLKDIV1_OUTDIV2(1) | SIM_CLKDIV1_OUTDIV3(1) | SIM_CLKDIV1_OUTDIV4(1);
  1008. #elif defined(KINETISL)
  1009. SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIV1(1) | SIM_CLKDIV1_OUTDIV4(0);
  1010. #endif
  1011. #elif F_CPU == 4000000
  1012. // config divisors: 4 MHz core, 4 MHz bus, 2 MHz flash
  1013. // since we are running from external clock 16MHz
  1014. // fix outdiv too -> cpu 16/4, bus 16/4, flash 16/4
  1015. // here we can go into vlpr?
  1016. // config divisors: 4 MHz core, 4 MHz bus, 4 MHz flash
  1017. #if defined(KINETISK)
  1018. SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIV1(3) | SIM_CLKDIV1_OUTDIV2(3) | SIM_CLKDIV1_OUTDIV3(3) | SIM_CLKDIV1_OUTDIV4(3);
  1019. #elif defined(KINETISL)
  1020. SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIV1(3) | SIM_CLKDIV1_OUTDIV4(0);
  1021. #endif
  1022. #elif F_CPU == 2000000
  1023. // since we are running from the fast internal reference clock 4MHz
  1024. // but is divided down by 2 so we actually have a 2MHz, MCG_SC[FCDIV] default is 2
  1025. // fix outdiv -> cpu 2/1, bus 2/1, flash 2/2
  1026. // config divisors: 2 MHz core, 2 MHz bus, 1 MHz flash
  1027. #if defined(KINETISK)
  1028. SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIV1(0) | SIM_CLKDIV1_OUTDIV2(0) | SIM_CLKDIV1_OUTDIV4(1);
  1029. #elif defined(KINETISL)
  1030. // config divisors: 2 MHz core, 1 MHz bus, 1 MHz flash
  1031. SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIV1(0) | SIM_CLKDIV1_OUTDIV4(1);
  1032. #endif
  1033. #else
  1034. #error "Error, F_CPU must be 256, 240, 216, 192, 180, 168, 144, 120, 96, 72, 48, 24, 16, 8, 4, or 2 MHz"
  1035. #endif
  1036. #if F_CPU > 16000000
  1037. // switch to PLL as clock source, FLL input = 16 MHz / 512
  1038. MCG_C1 = MCG_C1_CLKS(0) | MCG_C1_FRDIV(4);
  1039. // wait for PLL clock to be used
  1040. while ((MCG_S & MCG_S_CLKST_MASK) != MCG_S_CLKST(3)) ;
  1041. // now we're in PEE mode
  1042. // trace is CPU clock, CLKOUT=OSCERCLK0
  1043. #if defined(KINETISK)
  1044. #if F_CPU == 256000000 || F_CPU == 216000000 || F_CPU == 180000000
  1045. // USB uses IRC48
  1046. SIM_SOPT2 = SIM_SOPT2_USBSRC | SIM_SOPT2_IRC48SEL | SIM_SOPT2_TRACECLKSEL | SIM_SOPT2_CLKOUTSEL(6);
  1047. #else
  1048. // USB uses PLL clock
  1049. SIM_SOPT2 = SIM_SOPT2_USBSRC | SIM_SOPT2_PLLFLLSEL | SIM_SOPT2_TRACECLKSEL | SIM_SOPT2_CLKOUTSEL(6);
  1050. #endif
  1051. #elif defined(KINETISL)
  1052. SIM_SOPT2 = SIM_SOPT2_USBSRC | SIM_SOPT2_PLLFLLSEL | SIM_SOPT2_CLKOUTSEL(6)
  1053. | SIM_SOPT2_UART0SRC(1) | SIM_SOPT2_TPMSRC(1);
  1054. #endif
  1055. #else
  1056. #if F_CPU == 2000000
  1057. SIM_SOPT2 = SIM_SOPT2_TRACECLKSEL | SIM_SOPT2_CLKOUTSEL(4) | SIM_SOPT2_UART0SRC(3);
  1058. #else
  1059. SIM_SOPT2 = SIM_SOPT2_TRACECLKSEL | SIM_SOPT2_CLKOUTSEL(6) | SIM_SOPT2_UART0SRC(2);
  1060. #endif
  1061. #endif
  1062. #if F_CPU <= 2000000
  1063. // since we are not going into "stop mode" i removed it
  1064. SMC_PMCTRL = SMC_PMCTRL_RUNM(2); // VLPR mode :-)
  1065. #endif
  1066. #if defined(__MK66FX1M0__)
  1067. // If the RTC oscillator isn't enabled, get it started. For Teensy 3.6
  1068. // we don't do this early. See comment above about slow rising power.
  1069. if (!(RTC_CR & RTC_CR_OSCE)) {
  1070. RTC_SR = 0;
  1071. RTC_CR = RTC_CR_SC16P | RTC_CR_SC4P | RTC_CR_OSCE;
  1072. }
  1073. #endif
  1074. // initialize the SysTick counter
  1075. SYST_RVR = (F_CPU / 1000) - 1;
  1076. SYST_CVR = 0;
  1077. SYST_CSR = SYST_CSR_CLKSOURCE | SYST_CSR_TICKINT | SYST_CSR_ENABLE;
  1078. SCB_SHPR3 = 0x20200000; // Systick = priority 32
  1079. //init_pins();
  1080. __enable_irq();
  1081. _init_Teensyduino_internal_();
  1082. #if defined(KINETISK)
  1083. // RTC initialization
  1084. if (RTC_SR & RTC_SR_TIF) {
  1085. // this code will normally run on a power-up reset
  1086. // when VBAT has detected a power-up. Normally our
  1087. // compiled-in time will be stale. Write a special
  1088. // flag into the VBAT register file indicating the
  1089. // RTC is set with known-stale time and should be
  1090. // updated when fresh time is known.
  1091. #if ARDUINO >= 10600
  1092. rtc_set((uint32_t)&__rtc_localtime);
  1093. #else
  1094. rtc_set(TIME_T);
  1095. #endif
  1096. *(uint32_t *)0x4003E01C = 0x5A94C3A5;
  1097. }
  1098. if ((RCM_SRS0 & RCM_SRS0_PIN) && (*(uint32_t *)0x4003E01C == 0x5A94C3A5)) {
  1099. // this code should run immediately after an upload
  1100. // where the Teensy Loader causes the Mini54 to reset.
  1101. // Our compiled-in time will be very fresh, so set
  1102. // the RTC with this, and clear the VBAT resister file
  1103. // data so we don't mess with the time after it's been
  1104. // set well.
  1105. #if ARDUINO >= 10600
  1106. rtc_set((uint32_t)&__rtc_localtime);
  1107. #else
  1108. rtc_set(TIME_T);
  1109. #endif
  1110. *(uint32_t *)0x4003E01C = 0;
  1111. }
  1112. #endif
  1113. __libc_init_array();
  1114. startup_late_hook();
  1115. main();
  1116. while (1) ;
  1117. }
  1118. char *__brkval = (char *)&_ebss;
  1119. #ifndef STACK_MARGIN
  1120. #if defined(__MKL26Z64__)
  1121. #define STACK_MARGIN 512
  1122. #elif defined(__MK20DX128__)
  1123. #define STACK_MARGIN 1024
  1124. #elif defined(__MK20DX256__)
  1125. #define STACK_MARGIN 4096
  1126. #elif defined(__MK64FX512__) || defined(__MK66FX1M0__)
  1127. #define STACK_MARGIN 8192
  1128. #endif
  1129. #endif
  1130. #pragma GCC diagnostic push
  1131. #pragma GCC diagnostic ignored "-Wunused-parameter"
  1132. void * _sbrk(int incr)
  1133. {
  1134. char *prev, *stack;
  1135. prev = __brkval;
  1136. if (incr != 0) {
  1137. __asm__ volatile("mov %0, sp" : "=r" (stack) ::);
  1138. if (prev + incr >= stack - STACK_MARGIN) {
  1139. errno = ENOMEM;
  1140. return (void *)-1;
  1141. }
  1142. __brkval = prev + incr;
  1143. }
  1144. return prev;
  1145. }
  1146. __attribute__((weak))
  1147. int _read(int file, char *ptr, int len)
  1148. {
  1149. return 0;
  1150. }
  1151. __attribute__((weak))
  1152. int _close(int fd)
  1153. {
  1154. return -1;
  1155. }
  1156. #include <sys/stat.h>
  1157. __attribute__((weak))
  1158. int _fstat(int fd, struct stat *st)
  1159. {
  1160. st->st_mode = S_IFCHR;
  1161. return 0;
  1162. }
  1163. __attribute__((weak))
  1164. int _isatty(int fd)
  1165. {
  1166. return 1;
  1167. }
  1168. __attribute__((weak))
  1169. int _lseek(int fd, long long offset, int whence)
  1170. {
  1171. return -1;
  1172. }
  1173. __attribute__((weak))
  1174. void _exit(int status)
  1175. {
  1176. while (1);
  1177. }
  1178. __attribute__((weak))
  1179. void __cxa_pure_virtual()
  1180. {
  1181. while (1);
  1182. }
  1183. __attribute__((weak))
  1184. int __cxa_guard_acquire (char *g)
  1185. {
  1186. return !(*g);
  1187. }
  1188. __attribute__((weak))
  1189. void __cxa_guard_release(char *g)
  1190. {
  1191. *g = 1;
  1192. }
  1193. __attribute__((weak))
  1194. void abort(void)
  1195. {
  1196. while (1) ;
  1197. }
  1198. #pragma GCC diagnostic pop
  1199. int nvic_execution_priority(void)
  1200. {
  1201. uint32_t priority=256;
  1202. uint32_t primask, faultmask, basepri, ipsr;
  1203. // full algorithm in ARM DDI0403D, page B1-639
  1204. // this isn't quite complete, but hopefully good enough
  1205. __asm__ volatile("mrs %0, faultmask\n" : "=r" (faultmask)::);
  1206. if (faultmask) return -1;
  1207. __asm__ volatile("mrs %0, primask\n" : "=r" (primask)::);
  1208. if (primask) return 0;
  1209. __asm__ volatile("mrs %0, ipsr\n" : "=r" (ipsr)::);
  1210. if (ipsr) {
  1211. if (ipsr < 16) priority = 0; // could be non-zero
  1212. else priority = NVIC_GET_PRIORITY(ipsr - 16);
  1213. }
  1214. __asm__ volatile("mrs %0, basepri\n" : "=r" (basepri)::);
  1215. if (basepri > 0 && basepri < priority) priority = basepri;
  1216. return priority;
  1217. }
  1218. #if defined(HAS_KINETIS_HSRUN) && F_CPU > 120000000
  1219. int kinetis_hsrun_disable(void)
  1220. {
  1221. if (SMC_PMSTAT == SMC_PMSTAT_HSRUN) {
  1222. // First, reduce the CPU clock speed, but do not change
  1223. // the peripheral speed (F_BUS). Serial1 & Serial2 baud
  1224. // rates will be impacted, but most other peripherals
  1225. // will continue functioning at the same speed.
  1226. #if F_CPU == 256000000 && F_BUS == 64000000
  1227. SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIVS(1, 3, 1, 7); // TODO: TEST
  1228. #elif F_CPU == 256000000 && F_BUS == 128000000
  1229. SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIVS(1, 1, 1, 7); // TODO: TEST
  1230. #elif F_CPU == 240000000 && F_BUS == 60000000
  1231. SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIVS(1, 3, 1, 7); // ok
  1232. #elif F_CPU == 240000000 && F_BUS == 80000000
  1233. SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIVS(2, 2, 2, 8); // ok
  1234. #elif F_CPU == 240000000 && F_BUS == 120000000
  1235. SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIVS(1, 1, 1, 7); // ok
  1236. #elif F_CPU == 216000000 && F_BUS == 54000000
  1237. SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIVS(1, 3, 1, 7); // ok
  1238. #elif F_CPU == 216000000 && F_BUS == 72000000
  1239. SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIVS(2, 2, 2, 8); // ok
  1240. #elif F_CPU == 216000000 && F_BUS == 108000000
  1241. SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIVS(1, 1, 1, 7); // ok
  1242. #elif F_CPU == 192000000 && F_BUS == 48000000
  1243. SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIVS(1, 3, 1, 7); // ok
  1244. #elif F_CPU == 192000000 && F_BUS == 64000000
  1245. SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIVS(2, 2, 2, 8); // ok
  1246. #elif F_CPU == 192000000 && F_BUS == 96000000
  1247. SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIVS(1, 1, 1, 7); // ok
  1248. #elif F_CPU == 180000000 && F_BUS == 60000000
  1249. SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIVS(2, 2, 2, 8); // ok
  1250. #elif F_CPU == 180000000 && F_BUS == 90000000
  1251. SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIVS(1, 1, 1, 7); // ok
  1252. #elif F_CPU == 168000000 && F_BUS == 56000000
  1253. SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIVS(2, 2, 2, 5); // ok
  1254. #elif F_CPU == 144000000 && F_BUS == 48000000
  1255. SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIVS(2, 2, 2, 5); // ok
  1256. #elif F_CPU == 144000000 && F_BUS == 72000000
  1257. SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIVS(1, 1, 1, 5); // ok
  1258. #else
  1259. return 0;
  1260. #endif
  1261. // Then turn off HSRUN mode
  1262. SMC_PMCTRL = SMC_PMCTRL_RUNM(0);
  1263. while (SMC_PMSTAT == SMC_PMSTAT_HSRUN) ; // wait
  1264. return 1;
  1265. }
  1266. return 0;
  1267. }
  1268. int kinetis_hsrun_enable(void)
  1269. {
  1270. if (SMC_PMSTAT == SMC_PMSTAT_RUN) {
  1271. // Turn HSRUN mode on
  1272. SMC_PMCTRL = SMC_PMCTRL_RUNM(3);
  1273. while (SMC_PMSTAT != SMC_PMSTAT_HSRUN) {;} // wait
  1274. // Then configure clock for full speed
  1275. #if F_CPU == 256000000 && F_BUS == 64000000
  1276. SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIVS(0, 3, 0, 7);
  1277. #elif F_CPU == 256000000 && F_BUS == 128000000
  1278. SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIVS(0, 1, 0, 7);
  1279. #elif F_CPU == 240000000 && F_BUS == 60000000
  1280. SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIVS(0, 3, 0, 7);
  1281. #elif F_CPU == 240000000 && F_BUS == 80000000
  1282. SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIVS(0, 2, 0, 7);
  1283. #elif F_CPU == 240000000 && F_BUS == 120000000
  1284. SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIVS(0, 1, 0, 7);
  1285. #elif F_CPU == 216000000 && F_BUS == 54000000
  1286. SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIVS(0, 3, 0, 7);
  1287. #elif F_CPU == 216000000 && F_BUS == 72000000
  1288. SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIVS(0, 2, 0, 7);
  1289. #elif F_CPU == 216000000 && F_BUS == 108000000
  1290. SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIVS(0, 1, 0, 7);
  1291. #elif F_CPU == 192000000 && F_BUS == 48000000
  1292. SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIVS(0, 3, 0, 6);
  1293. #elif F_CPU == 192000000 && F_BUS == 64000000
  1294. SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIVS(0, 2, 0, 6);
  1295. #elif F_CPU == 192000000 && F_BUS == 96000000
  1296. SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIVS(0, 1, 0, 6);
  1297. #elif F_CPU == 180000000 && F_BUS == 60000000
  1298. SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIVS(0, 2, 0, 6);
  1299. #elif F_CPU == 180000000 && F_BUS == 90000000
  1300. SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIVS(0, 1, 0, 6);
  1301. #elif F_CPU == 168000000 && F_BUS == 56000000
  1302. SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIVS(0, 2, 0, 5);
  1303. #elif F_CPU == 144000000 && F_BUS == 48000000
  1304. SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIVS(0, 2, 0, 4);
  1305. #elif F_CPU == 144000000 && F_BUS == 72000000
  1306. SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIVS(0, 1, 0, 4);
  1307. #else
  1308. return 0;
  1309. #endif
  1310. return 1;
  1311. }
  1312. return 0;
  1313. }
  1314. #endif // HAS_KINETIS_HSRUN && F_CPU > 120000000