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