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.

mk20dx128.c 38KB

11 jaren geleden
11 jaren geleden
11 jaren geleden
11 jaren geleden
11 jaren geleden
11 jaren geleden
11 jaren geleden
11 jaren geleden
11 jaren geleden
11 jaren geleden
11 jaren geleden
11 jaren geleden
11 jaren geleden
11 jaren geleden
11 jaren geleden
11 jaren geleden
11 jaren geleden
11 jaren geleden
11 jaren geleden
11 jaren geleden
10 jaren geleden
11 jaren geleden
11 jaren geleden
11 jaren geleden
11 jaren geleden
11 jaren geleden
10 jaren geleden
10 jaren geleden
11 jaren geleden
11 jaren geleden
10 jaren geleden
10 jaren geleden
10 jaren geleden
11 jaren geleden
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996
  1. /* Teensyduino Core Library
  2. * http://www.pjrc.com/teensy/
  3. * Copyright (c) 2013 PJRC.COM, LLC.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining
  6. * a copy of this software and associated documentation files (the
  7. * "Software"), to deal in the Software without restriction, including
  8. * without limitation the rights to use, copy, modify, merge, publish,
  9. * distribute, sublicense, and/or sell copies of the Software, and to
  10. * permit persons to whom the Software is furnished to do so, subject to
  11. * the following conditions:
  12. *
  13. * 1. The above copyright notice and this permission notice shall be
  14. * included in all copies or substantial portions of the Software.
  15. *
  16. * 2. If the Software is incorporated into a build system that allows
  17. * selection among a list of target devices, then similar target
  18. * devices manufactured by PJRC.COM must be included in the list of
  19. * target devices and selectable in the same manner.
  20. *
  21. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  25. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  26. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  27. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  28. * SOFTWARE.
  29. */
  30. #include "kinetis.h"
  31. #include "core_pins.h" // testing only
  32. #include "ser_print.h" // testing only
  33. extern unsigned long _stext;
  34. extern unsigned long _etext;
  35. extern unsigned long _sdata;
  36. extern unsigned long _edata;
  37. extern unsigned long _sbss;
  38. extern unsigned long _ebss;
  39. extern unsigned long _estack;
  40. //extern void __init_array_start(void);
  41. //extern void __init_array_end(void);
  42. extern int main (void);
  43. void ResetHandler(void);
  44. void _init_Teensyduino_internal_(void);
  45. void __libc_init_array(void);
  46. void fault_isr(void)
  47. {
  48. #if 0
  49. uint32_t addr;
  50. SIM_SCGC4 |= 0x00000400;
  51. UART0_BDH = 0;
  52. UART0_BDL = 26; // 115200 at 48 MHz
  53. UART0_C2 = UART_C2_TE;
  54. PORTB_PCR17 = PORT_PCR_MUX(3);
  55. ser_print("\nfault: \n??: ");
  56. asm("ldr %0, [sp, #52]" : "=r" (addr) ::);
  57. ser_print_hex32(addr);
  58. ser_print("\n??: ");
  59. asm("ldr %0, [sp, #48]" : "=r" (addr) ::);
  60. ser_print_hex32(addr);
  61. ser_print("\n??: ");
  62. asm("ldr %0, [sp, #44]" : "=r" (addr) ::);
  63. ser_print_hex32(addr);
  64. ser_print("\npsr:");
  65. asm("ldr %0, [sp, #40]" : "=r" (addr) ::);
  66. ser_print_hex32(addr);
  67. ser_print("\nadr:");
  68. asm("ldr %0, [sp, #36]" : "=r" (addr) ::);
  69. ser_print_hex32(addr);
  70. ser_print("\nlr: ");
  71. asm("ldr %0, [sp, #32]" : "=r" (addr) ::);
  72. ser_print_hex32(addr);
  73. ser_print("\nr12:");
  74. asm("ldr %0, [sp, #28]" : "=r" (addr) ::);
  75. ser_print_hex32(addr);
  76. ser_print("\nr3: ");
  77. asm("ldr %0, [sp, #24]" : "=r" (addr) ::);
  78. ser_print_hex32(addr);
  79. ser_print("\nr2: ");
  80. asm("ldr %0, [sp, #20]" : "=r" (addr) ::);
  81. ser_print_hex32(addr);
  82. ser_print("\nr1: ");
  83. asm("ldr %0, [sp, #16]" : "=r" (addr) ::);
  84. ser_print_hex32(addr);
  85. ser_print("\nr0: ");
  86. asm("ldr %0, [sp, #12]" : "=r" (addr) ::);
  87. ser_print_hex32(addr);
  88. ser_print("\nr4: ");
  89. asm("ldr %0, [sp, #8]" : "=r" (addr) ::);
  90. ser_print_hex32(addr);
  91. ser_print("\nlr: ");
  92. asm("ldr %0, [sp, #4]" : "=r" (addr) ::);
  93. ser_print_hex32(addr);
  94. ser_print("\n");
  95. asm("ldr %0, [sp, #0]" : "=r" (addr) ::);
  96. #endif
  97. while (1) {
  98. // keep polling some communication while in fault
  99. // mode, so we don't completely die.
  100. if (SIM_SCGC4 & SIM_SCGC4_USBOTG) usb_isr();
  101. if (SIM_SCGC4 & SIM_SCGC4_UART0) uart0_status_isr();
  102. if (SIM_SCGC4 & SIM_SCGC4_UART1) uart1_status_isr();
  103. if (SIM_SCGC4 & SIM_SCGC4_UART2) uart2_status_isr();
  104. }
  105. }
  106. void unused_isr(void)
  107. {
  108. fault_isr();
  109. }
  110. extern volatile uint32_t systick_millis_count;
  111. void systick_default_isr(void)
  112. {
  113. systick_millis_count++;
  114. }
  115. void nmi_isr(void) __attribute__ ((weak, alias("unused_isr")));
  116. void hard_fault_isr(void) __attribute__ ((weak, alias("fault_isr")));
  117. void memmanage_fault_isr(void) __attribute__ ((weak, alias("fault_isr")));
  118. void bus_fault_isr(void) __attribute__ ((weak, alias("fault_isr")));
  119. void usage_fault_isr(void) __attribute__ ((weak, alias("fault_isr")));
  120. void svcall_isr(void) __attribute__ ((weak, alias("unused_isr")));
  121. void debugmonitor_isr(void) __attribute__ ((weak, alias("unused_isr")));
  122. void pendablesrvreq_isr(void) __attribute__ ((weak, alias("unused_isr")));
  123. void systick_isr(void) __attribute__ ((weak, alias("systick_default_isr")));
  124. void dma_ch0_isr(void) __attribute__ ((weak, alias("unused_isr")));
  125. void dma_ch1_isr(void) __attribute__ ((weak, alias("unused_isr")));
  126. void dma_ch2_isr(void) __attribute__ ((weak, alias("unused_isr")));
  127. void dma_ch3_isr(void) __attribute__ ((weak, alias("unused_isr")));
  128. void dma_ch4_isr(void) __attribute__ ((weak, alias("unused_isr")));
  129. void dma_ch5_isr(void) __attribute__ ((weak, alias("unused_isr")));
  130. void dma_ch6_isr(void) __attribute__ ((weak, alias("unused_isr")));
  131. void dma_ch7_isr(void) __attribute__ ((weak, alias("unused_isr")));
  132. void dma_ch8_isr(void) __attribute__ ((weak, alias("unused_isr")));
  133. void dma_ch9_isr(void) __attribute__ ((weak, alias("unused_isr")));
  134. void dma_ch10_isr(void) __attribute__ ((weak, alias("unused_isr")));
  135. void dma_ch11_isr(void) __attribute__ ((weak, alias("unused_isr")));
  136. void dma_ch12_isr(void) __attribute__ ((weak, alias("unused_isr")));
  137. void dma_ch13_isr(void) __attribute__ ((weak, alias("unused_isr")));
  138. void dma_ch14_isr(void) __attribute__ ((weak, alias("unused_isr")));
  139. void dma_ch15_isr(void) __attribute__ ((weak, alias("unused_isr")));
  140. void dma_error_isr(void) __attribute__ ((weak, alias("unused_isr")));
  141. void mcm_isr(void) __attribute__ ((weak, alias("unused_isr")));
  142. void randnum_isr(void) __attribute__ ((weak, alias("unused_isr")));
  143. void flash_cmd_isr(void) __attribute__ ((weak, alias("unused_isr")));
  144. void flash_error_isr(void) __attribute__ ((weak, alias("unused_isr")));
  145. void low_voltage_isr(void) __attribute__ ((weak, alias("unused_isr")));
  146. void wakeup_isr(void) __attribute__ ((weak, alias("unused_isr")));
  147. void watchdog_isr(void) __attribute__ ((weak, alias("unused_isr")));
  148. void i2c0_isr(void) __attribute__ ((weak, alias("unused_isr")));
  149. void i2c1_isr(void) __attribute__ ((weak, alias("unused_isr")));
  150. void i2c2_isr(void) __attribute__ ((weak, alias("unused_isr")));
  151. void i2c3_isr(void) __attribute__ ((weak, alias("unused_isr")));
  152. void spi0_isr(void) __attribute__ ((weak, alias("unused_isr")));
  153. void spi1_isr(void) __attribute__ ((weak, alias("unused_isr")));
  154. void spi2_isr(void) __attribute__ ((weak, alias("unused_isr")));
  155. void sdhc_isr(void) __attribute__ ((weak, alias("unused_isr")));
  156. void enet_timer_isr(void) __attribute__ ((weak, alias("unused_isr")));
  157. void enet_tx_isr(void) __attribute__ ((weak, alias("unused_isr")));
  158. void enet_rx_isr(void) __attribute__ ((weak, alias("unused_isr")));
  159. void enet_error_isr(void) __attribute__ ((weak, alias("unused_isr")));
  160. void can0_message_isr(void) __attribute__ ((weak, alias("unused_isr")));
  161. void can0_bus_off_isr(void) __attribute__ ((weak, alias("unused_isr")));
  162. void can0_error_isr(void) __attribute__ ((weak, alias("unused_isr")));
  163. void can0_tx_warn_isr(void) __attribute__ ((weak, alias("unused_isr")));
  164. void can0_rx_warn_isr(void) __attribute__ ((weak, alias("unused_isr")));
  165. void can0_wakeup_isr(void) __attribute__ ((weak, alias("unused_isr")));
  166. void can1_message_isr(void) __attribute__ ((weak, alias("unused_isr")));
  167. void can1_bus_off_isr(void) __attribute__ ((weak, alias("unused_isr")));
  168. void can1_error_isr(void) __attribute__ ((weak, alias("unused_isr")));
  169. void can1_tx_warn_isr(void) __attribute__ ((weak, alias("unused_isr")));
  170. void can1_rx_warn_isr(void) __attribute__ ((weak, alias("unused_isr")));
  171. void can1_wakeup_isr(void) __attribute__ ((weak, alias("unused_isr")));
  172. void i2s0_tx_isr(void) __attribute__ ((weak, alias("unused_isr")));
  173. void i2s0_rx_isr(void) __attribute__ ((weak, alias("unused_isr")));
  174. void i2s0_isr(void) __attribute__ ((weak, alias("unused_isr")));
  175. void uart0_lon_isr(void) __attribute__ ((weak, alias("unused_isr")));
  176. void uart0_status_isr(void) __attribute__ ((weak, alias("unused_isr")));
  177. void uart0_error_isr(void) __attribute__ ((weak, alias("unused_isr")));
  178. void uart1_status_isr(void) __attribute__ ((weak, alias("unused_isr")));
  179. void uart1_error_isr(void) __attribute__ ((weak, alias("unused_isr")));
  180. void uart2_status_isr(void) __attribute__ ((weak, alias("unused_isr")));
  181. void uart2_error_isr(void) __attribute__ ((weak, alias("unused_isr")));
  182. void uart3_status_isr(void) __attribute__ ((weak, alias("unused_isr")));
  183. void uart3_error_isr(void) __attribute__ ((weak, alias("unused_isr")));
  184. void uart4_status_isr(void) __attribute__ ((weak, alias("unused_isr")));
  185. void uart4_error_isr(void) __attribute__ ((weak, alias("unused_isr")));
  186. void uart5_status_isr(void) __attribute__ ((weak, alias("unused_isr")));
  187. void uart5_error_isr(void) __attribute__ ((weak, alias("unused_isr")));
  188. void lpuart0_status_isr(void) __attribute__ ((weak, alias("unused_isr")));
  189. void adc0_isr(void) __attribute__ ((weak, alias("unused_isr")));
  190. void adc1_isr(void) __attribute__ ((weak, alias("unused_isr")));
  191. void cmp0_isr(void) __attribute__ ((weak, alias("unused_isr")));
  192. void cmp1_isr(void) __attribute__ ((weak, alias("unused_isr")));
  193. void cmp2_isr(void) __attribute__ ((weak, alias("unused_isr")));
  194. void cmp3_isr(void) __attribute__ ((weak, alias("unused_isr")));
  195. void ftm0_isr(void) __attribute__ ((weak, alias("unused_isr")));
  196. void ftm1_isr(void) __attribute__ ((weak, alias("unused_isr")));
  197. void ftm2_isr(void) __attribute__ ((weak, alias("unused_isr")));
  198. void ftm3_isr(void) __attribute__ ((weak, alias("unused_isr")));
  199. void tpm0_isr(void) __attribute__ ((weak, alias("unused_isr")));
  200. void tpm1_isr(void) __attribute__ ((weak, alias("unused_isr")));
  201. void tpm2_isr(void) __attribute__ ((weak, alias("unused_isr")));
  202. void cmt_isr(void) __attribute__ ((weak, alias("unused_isr")));
  203. void rtc_alarm_isr(void) __attribute__ ((weak, alias("unused_isr")));
  204. void rtc_seconds_isr(void) __attribute__ ((weak, alias("unused_isr")));
  205. void pit_isr(void) __attribute__ ((weak, alias("unused_isr")));
  206. void pit0_isr(void) __attribute__ ((weak, alias("unused_isr")));
  207. void pit1_isr(void) __attribute__ ((weak, alias("unused_isr")));
  208. void pit2_isr(void) __attribute__ ((weak, alias("unused_isr")));
  209. void pit3_isr(void) __attribute__ ((weak, alias("unused_isr")));
  210. void pdb_isr(void) __attribute__ ((weak, alias("unused_isr")));
  211. void usb_isr(void) __attribute__ ((weak, alias("unused_isr")));
  212. void usb_charge_isr(void) __attribute__ ((weak, alias("unused_isr")));
  213. void usbhs_isr(void) __attribute__ ((weak, alias("unused_isr")));
  214. void usbhs_phy_isr(void) __attribute__ ((weak, alias("unused_isr")));
  215. void dac0_isr(void) __attribute__ ((weak, alias("unused_isr")));
  216. void dac1_isr(void) __attribute__ ((weak, alias("unused_isr")));
  217. void tsi0_isr(void) __attribute__ ((weak, alias("unused_isr")));
  218. void mcg_isr(void) __attribute__ ((weak, alias("unused_isr")));
  219. void lptmr_isr(void) __attribute__ ((weak, alias("unused_isr")));
  220. void porta_isr(void) __attribute__ ((weak, alias("unused_isr")));
  221. void portb_isr(void) __attribute__ ((weak, alias("unused_isr")));
  222. void portc_isr(void) __attribute__ ((weak, alias("unused_isr")));
  223. void portd_isr(void) __attribute__ ((weak, alias("unused_isr")));
  224. void porte_isr(void) __attribute__ ((weak, alias("unused_isr")));
  225. void portcd_isr(void) __attribute__ ((weak, alias("unused_isr")));
  226. void software_isr(void) __attribute__ ((weak, alias("unused_isr")));
  227. #if defined(__MK20DX128__)
  228. __attribute__ ((section(".dmabuffers"), used, aligned(256)))
  229. #elif defined(__MK20DX256__)
  230. __attribute__ ((section(".dmabuffers"), used, aligned(512)))
  231. #elif defined(__MKL26Z64__)
  232. __attribute__ ((section(".dmabuffers"), used, aligned(256)))
  233. #elif defined(__MK66FX1M0__)
  234. __attribute__ ((section(".dmabuffers"), used, aligned(512)))
  235. #endif
  236. void (* _VectorsRam[NVIC_NUM_INTERRUPTS+16])(void);
  237. __attribute__ ((section(".vectors"), used))
  238. void (* const _VectorsFlash[NVIC_NUM_INTERRUPTS+16])(void) =
  239. {
  240. (void (*)(void))((unsigned long)&_estack), // 0 ARM: Initial Stack Pointer
  241. ResetHandler, // 1 ARM: Initial Program Counter
  242. nmi_isr, // 2 ARM: Non-maskable Interrupt (NMI)
  243. hard_fault_isr, // 3 ARM: Hard Fault
  244. memmanage_fault_isr, // 4 ARM: MemManage Fault
  245. bus_fault_isr, // 5 ARM: Bus Fault
  246. usage_fault_isr, // 6 ARM: Usage Fault
  247. fault_isr, // 7 --
  248. fault_isr, // 8 --
  249. fault_isr, // 9 --
  250. fault_isr, // 10 --
  251. svcall_isr, // 11 ARM: Supervisor call (SVCall)
  252. debugmonitor_isr, // 12 ARM: Debug Monitor
  253. fault_isr, // 13 --
  254. pendablesrvreq_isr, // 14 ARM: Pendable req serv(PendableSrvReq)
  255. systick_isr, // 15 ARM: System tick timer (SysTick)
  256. #if defined(__MK20DX128__)
  257. dma_ch0_isr, // 16 DMA channel 0 transfer complete
  258. dma_ch1_isr, // 17 DMA channel 1 transfer complete
  259. dma_ch2_isr, // 18 DMA channel 2 transfer complete
  260. dma_ch3_isr, // 19 DMA channel 3 transfer complete
  261. dma_error_isr, // 20 DMA error interrupt channel
  262. unused_isr, // 21 DMA --
  263. flash_cmd_isr, // 22 Flash Memory Command complete
  264. flash_error_isr, // 23 Flash Read collision
  265. low_voltage_isr, // 24 Low-voltage detect/warning
  266. wakeup_isr, // 25 Low Leakage Wakeup
  267. watchdog_isr, // 26 Both EWM and WDOG interrupt
  268. i2c0_isr, // 27 I2C0
  269. spi0_isr, // 28 SPI0
  270. i2s0_tx_isr, // 29 I2S0 Transmit
  271. i2s0_rx_isr, // 30 I2S0 Receive
  272. uart0_lon_isr, // 31 UART0 CEA709.1-B (LON) status
  273. uart0_status_isr, // 32 UART0 status
  274. uart0_error_isr, // 33 UART0 error
  275. uart1_status_isr, // 34 UART1 status
  276. uart1_error_isr, // 35 UART1 error
  277. uart2_status_isr, // 36 UART2 status
  278. uart2_error_isr, // 37 UART2 error
  279. adc0_isr, // 38 ADC0
  280. cmp0_isr, // 39 CMP0
  281. cmp1_isr, // 40 CMP1
  282. ftm0_isr, // 41 FTM0
  283. ftm1_isr, // 42 FTM1
  284. cmt_isr, // 43 CMT
  285. rtc_alarm_isr, // 44 RTC Alarm interrupt
  286. rtc_seconds_isr, // 45 RTC Seconds interrupt
  287. pit0_isr, // 46 PIT Channel 0
  288. pit1_isr, // 47 PIT Channel 1
  289. pit2_isr, // 48 PIT Channel 2
  290. pit3_isr, // 49 PIT Channel 3
  291. pdb_isr, // 50 PDB Programmable Delay Block
  292. usb_isr, // 51 USB OTG
  293. usb_charge_isr, // 52 USB Charger Detect
  294. tsi0_isr, // 53 TSI0
  295. mcg_isr, // 54 MCG
  296. lptmr_isr, // 55 Low Power Timer
  297. porta_isr, // 56 Pin detect (Port A)
  298. portb_isr, // 57 Pin detect (Port B)
  299. portc_isr, // 58 Pin detect (Port C)
  300. portd_isr, // 59 Pin detect (Port D)
  301. porte_isr, // 60 Pin detect (Port E)
  302. software_isr, // 61 Software interrupt
  303. #elif defined(__MK20DX256__)
  304. dma_ch0_isr, // 16 DMA channel 0 transfer complete
  305. dma_ch1_isr, // 17 DMA channel 1 transfer complete
  306. dma_ch2_isr, // 18 DMA channel 2 transfer complete
  307. dma_ch3_isr, // 19 DMA channel 3 transfer complete
  308. dma_ch4_isr, // 20 DMA channel 4 transfer complete
  309. dma_ch5_isr, // 21 DMA channel 5 transfer complete
  310. dma_ch6_isr, // 22 DMA channel 6 transfer complete
  311. dma_ch7_isr, // 23 DMA channel 7 transfer complete
  312. dma_ch8_isr, // 24 DMA channel 8 transfer complete
  313. dma_ch9_isr, // 25 DMA channel 9 transfer complete
  314. dma_ch10_isr, // 26 DMA channel 10 transfer complete
  315. dma_ch11_isr, // 27 DMA channel 11 transfer complete
  316. dma_ch12_isr, // 28 DMA channel 12 transfer complete
  317. dma_ch13_isr, // 29 DMA channel 13 transfer complete
  318. dma_ch14_isr, // 30 DMA channel 14 transfer complete
  319. dma_ch15_isr, // 31 DMA channel 15 transfer complete
  320. dma_error_isr, // 32 DMA error interrupt channel
  321. unused_isr, // 33 --
  322. flash_cmd_isr, // 34 Flash Memory Command complete
  323. flash_error_isr, // 35 Flash Read collision
  324. low_voltage_isr, // 36 Low-voltage detect/warning
  325. wakeup_isr, // 37 Low Leakage Wakeup
  326. watchdog_isr, // 38 Both EWM and WDOG interrupt
  327. unused_isr, // 39 --
  328. i2c0_isr, // 40 I2C0
  329. i2c1_isr, // 41 I2C1
  330. spi0_isr, // 42 SPI0
  331. spi1_isr, // 43 SPI1
  332. unused_isr, // 44 --
  333. can0_message_isr, // 45 CAN OR'ed Message buffer (0-15)
  334. can0_bus_off_isr, // 46 CAN Bus Off
  335. can0_error_isr, // 47 CAN Error
  336. can0_tx_warn_isr, // 48 CAN Transmit Warning
  337. can0_rx_warn_isr, // 49 CAN Receive Warning
  338. can0_wakeup_isr, // 50 CAN Wake Up
  339. i2s0_tx_isr, // 51 I2S0 Transmit
  340. i2s0_rx_isr, // 52 I2S0 Receive
  341. unused_isr, // 53 --
  342. unused_isr, // 54 --
  343. unused_isr, // 55 --
  344. unused_isr, // 56 --
  345. unused_isr, // 57 --
  346. unused_isr, // 58 --
  347. unused_isr, // 59 --
  348. uart0_lon_isr, // 60 UART0 CEA709.1-B (LON) status
  349. uart0_status_isr, // 61 UART0 status
  350. uart0_error_isr, // 62 UART0 error
  351. uart1_status_isr, // 63 UART1 status
  352. uart1_error_isr, // 64 UART1 error
  353. uart2_status_isr, // 65 UART2 status
  354. uart2_error_isr, // 66 UART2 error
  355. unused_isr, // 67 --
  356. unused_isr, // 68 --
  357. unused_isr, // 69 --
  358. unused_isr, // 70 --
  359. unused_isr, // 71 --
  360. unused_isr, // 72 --
  361. adc0_isr, // 73 ADC0
  362. adc1_isr, // 74 ADC1
  363. cmp0_isr, // 75 CMP0
  364. cmp1_isr, // 76 CMP1
  365. cmp2_isr, // 77 CMP2
  366. ftm0_isr, // 78 FTM0
  367. ftm1_isr, // 79 FTM1
  368. ftm2_isr, // 80 FTM2
  369. cmt_isr, // 81 CMT
  370. rtc_alarm_isr, // 82 RTC Alarm interrupt
  371. rtc_seconds_isr, // 83 RTC Seconds interrupt
  372. pit0_isr, // 84 PIT Channel 0
  373. pit1_isr, // 85 PIT Channel 1
  374. pit2_isr, // 86 PIT Channel 2
  375. pit3_isr, // 87 PIT Channel 3
  376. pdb_isr, // 88 PDB Programmable Delay Block
  377. usb_isr, // 89 USB OTG
  378. usb_charge_isr, // 90 USB Charger Detect
  379. unused_isr, // 91 --
  380. unused_isr, // 92 --
  381. unused_isr, // 93 --
  382. unused_isr, // 94 --
  383. unused_isr, // 95 --
  384. unused_isr, // 96 --
  385. dac0_isr, // 97 DAC0
  386. unused_isr, // 98 --
  387. tsi0_isr, // 99 TSI0
  388. mcg_isr, // 100 MCG
  389. lptmr_isr, // 101 Low Power Timer
  390. unused_isr, // 102 --
  391. porta_isr, // 103 Pin detect (Port A)
  392. portb_isr, // 104 Pin detect (Port B)
  393. portc_isr, // 105 Pin detect (Port C)
  394. portd_isr, // 106 Pin detect (Port D)
  395. porte_isr, // 107 Pin detect (Port E)
  396. unused_isr, // 108 --
  397. unused_isr, // 109 --
  398. software_isr, // 110 Software interrupt
  399. #elif defined(__MKL26Z64__)
  400. dma_ch0_isr, // 16 DMA channel 0 transfer complete
  401. dma_ch1_isr, // 17 DMA channel 1 transfer complete
  402. dma_ch2_isr, // 18 DMA channel 2 transfer complete
  403. dma_ch3_isr, // 19 DMA channel 3 transfer complete
  404. unused_isr, // 20 --
  405. flash_cmd_isr, // 21 Flash Memory Command complete
  406. low_voltage_isr, // 22 Low-voltage detect/warning
  407. wakeup_isr, // 23 Low Leakage Wakeup
  408. i2c0_isr, // 24 I2C0
  409. i2c1_isr, // 25 I2C1
  410. spi0_isr, // 26 SPI0
  411. spi1_isr, // 27 SPI1
  412. uart0_status_isr, // 28 UART0 status & error
  413. uart1_status_isr, // 29 UART1 status & error
  414. uart2_status_isr, // 30 UART2 status & error
  415. adc0_isr, // 31 ADC0
  416. cmp0_isr, // 32 CMP0
  417. ftm0_isr, // 33 FTM0
  418. ftm1_isr, // 34 FTM1
  419. ftm2_isr, // 35 FTM2
  420. rtc_alarm_isr, // 36 RTC Alarm interrupt
  421. rtc_seconds_isr, // 37 RTC Seconds interrupt
  422. pit_isr, // 38 PIT Both Channels
  423. i2s0_isr, // 39 I2S0 Transmit & Receive
  424. usb_isr, // 40 USB OTG
  425. dac0_isr, // 41 DAC0
  426. tsi0_isr, // 42 TSI0
  427. mcg_isr, // 43 MCG
  428. lptmr_isr, // 44 Low Power Timer
  429. software_isr, // 45 Software interrupt
  430. porta_isr, // 46 Pin detect (Port A)
  431. portcd_isr, // 47 Pin detect (Port C and D)
  432. #elif defined(__MK66FX1M0__)
  433. dma_ch0_isr, // 16 DMA channel 0 transfer complete
  434. dma_ch1_isr, // 17 DMA channel 1 transfer complete
  435. dma_ch2_isr, // 18 DMA channel 2 transfer complete
  436. dma_ch3_isr, // 19 DMA channel 3 transfer complete
  437. dma_ch4_isr, // 20 DMA channel 4 transfer complete
  438. dma_ch5_isr, // 21 DMA channel 5 transfer complete
  439. dma_ch6_isr, // 22 DMA channel 6 transfer complete
  440. dma_ch7_isr, // 23 DMA channel 7 transfer complete
  441. dma_ch8_isr, // 24 DMA channel 8 transfer complete
  442. dma_ch9_isr, // 25 DMA channel 9 transfer complete
  443. dma_ch10_isr, // 26 DMA channel 10 transfer complete
  444. dma_ch11_isr, // 27 DMA channel 11 transfer complete
  445. dma_ch12_isr, // 28 DMA channel 12 transfer complete
  446. dma_ch13_isr, // 29 DMA channel 13 transfer complete
  447. dma_ch14_isr, // 30 DMA channel 14 transfer complete
  448. dma_ch15_isr, // 31 DMA channel 15 transfer complete
  449. dma_error_isr, // 32 DMA error interrupt channel
  450. mcm_isr, // 33 MCM
  451. flash_cmd_isr, // 34 Flash Memory Command complete
  452. flash_error_isr, // 35 Flash Read collision
  453. low_voltage_isr, // 36 Low-voltage detect/warning
  454. wakeup_isr, // 37 Low Leakage Wakeup
  455. watchdog_isr, // 38 Both EWM and WDOG interrupt
  456. randnum_isr, // 39 Random Number Generator
  457. i2c0_isr, // 40 I2C0
  458. i2c1_isr, // 41 I2C1
  459. spi0_isr, // 42 SPI0
  460. spi1_isr, // 43 SPI1
  461. i2s0_tx_isr, // 44 I2S0 Transmit
  462. i2s0_rx_isr, // 45 I2S0 Receive
  463. unused_isr, // 46 --
  464. uart0_status_isr, // 47 UART0 status
  465. uart0_error_isr, // 48 UART0 error
  466. uart1_status_isr, // 49 UART1 status
  467. uart1_error_isr, // 50 UART1 error
  468. uart2_status_isr, // 51 UART2 status
  469. uart2_error_isr, // 52 UART2 error
  470. uart3_status_isr, // 53 UART3 status
  471. uart3_error_isr, // 54 UART3 error
  472. adc0_isr, // 55 ADC0
  473. cmp0_isr, // 56 CMP0
  474. cmp1_isr, // 57 CMP1
  475. ftm0_isr, // 58 FTM0
  476. ftm1_isr, // 59 FTM1
  477. ftm2_isr, // 60 FTM2
  478. cmt_isr, // 61 CMT
  479. rtc_alarm_isr, // 62 RTC Alarm interrupt
  480. rtc_seconds_isr, // 63 RTC Seconds interrupt
  481. pit0_isr, // 64 PIT Channel 0
  482. pit1_isr, // 65 PIT Channel 1
  483. pit2_isr, // 66 PIT Channel 2
  484. pit3_isr, // 67 PIT Channel 3
  485. pdb_isr, // 68 PDB Programmable Delay Block
  486. usb_isr, // 69 USB OTG
  487. usb_charge_isr, // 70 USB Charger Detect
  488. unused_isr, // 71 --
  489. dac0_isr, // 72 DAC0
  490. mcg_isr, // 73 MCG
  491. lptmr_isr, // 74 Low Power Timer
  492. porta_isr, // 75 Pin detect (Port A)
  493. portb_isr, // 76 Pin detect (Port B)
  494. portc_isr, // 77 Pin detect (Port C)
  495. portd_isr, // 78 Pin detect (Port D)
  496. porte_isr, // 79 Pin detect (Port E)
  497. software_isr, // 80 Software interrupt
  498. spi2_isr, // 81 SPI2
  499. uart4_status_isr, // 82 UART4 status
  500. uart4_error_isr, // 83 UART4 error
  501. unused_isr, // 84 --
  502. unused_isr, // 85 --
  503. cmp2_isr, // 86 CMP2
  504. ftm3_isr, // 87 FTM3
  505. dac1_isr, // 88 DAC1
  506. adc1_isr, // 89 ADC1
  507. i2c2_isr, // 90 I2C2
  508. can0_message_isr, // 91 CAN OR'ed Message buffer (0-15)
  509. can0_bus_off_isr, // 92 CAN Bus Off
  510. can0_error_isr, // 93 CAN Error
  511. can0_tx_warn_isr, // 94 CAN Transmit Warning
  512. can0_rx_warn_isr, // 95 CAN Receive Warning
  513. can0_wakeup_isr, // 96 CAN Wake Up
  514. sdhc_isr, // 97 SDHC
  515. enet_timer_isr, // 98 Ethernet IEEE1588 Timers
  516. enet_tx_isr, // 99 Ethernet Transmit
  517. enet_rx_isr, // 100 Ethernet Receive
  518. enet_error_isr, // 101 Ethernet Error
  519. lpuart0_status_isr, // 102 ADC1
  520. tsi0_isr, // 103 TSI0
  521. tpm1_isr, // 104 FTM1
  522. tpm2_isr, // 105 FTM2
  523. usbhs_phy_isr, // 106 USB-HS Phy
  524. i2c3_isr, // 107 I2C3
  525. cmp3_isr, // 108 CMP3
  526. usbhs_isr, // 109 USB-HS
  527. can1_message_isr, // 110 CAN OR'ed Message buffer (0-15)
  528. can1_bus_off_isr, // 111 CAN Bus Off
  529. can1_error_isr, // 112 CAN Error
  530. can1_tx_warn_isr, // 113 CAN Transmit Warning
  531. can1_rx_warn_isr, // 114 CAN Receive Warning
  532. can1_wakeup_isr, // 115 CAN Wake Up
  533. #endif
  534. };
  535. __attribute__ ((section(".flashconfig"), used))
  536. const uint8_t flashconfigbytes[16] = {
  537. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  538. 0xFF, 0xFF, 0xFF, 0xFF, 0xFE, 0xFF, 0xFF, 0xFF
  539. };
  540. // Automatically initialize the RTC. When the build defines the compile
  541. // time, and the user has added a crystal, the RTC will automatically
  542. // begin at the time of the first upload.
  543. #ifndef TIME_T
  544. #define TIME_T 1349049600 // default 1 Oct 2012 (never used, Arduino sets this)
  545. #endif
  546. extern void *__rtc_localtime; // Arduino build process sets this
  547. extern void rtc_set(unsigned long t);
  548. static void startup_default_early_hook(void) {
  549. #if defined(KINETISK)
  550. WDOG_STCTRLH = WDOG_STCTRLH_ALLOWUPDATE;
  551. #elif defined(KINETISL)
  552. SIM_COPC = 0; // disable the watchdog
  553. #endif
  554. }
  555. static void startup_default_late_hook(void) {}
  556. void startup_early_hook(void) __attribute__ ((weak, alias("startup_default_early_hook")));
  557. void startup_late_hook(void) __attribute__ ((weak, alias("startup_default_late_hook")));
  558. __attribute__ ((section(".startup"),optimize("-Os")))
  559. void ResetHandler(void)
  560. {
  561. uint32_t *src = &_etext;
  562. uint32_t *dest = &_sdata;
  563. unsigned int i;
  564. #if F_CPU <= 2000000
  565. volatile int n;
  566. #endif
  567. //volatile int count;
  568. #ifdef KINETISK
  569. WDOG_UNLOCK = WDOG_UNLOCK_SEQ1;
  570. WDOG_UNLOCK = WDOG_UNLOCK_SEQ2;
  571. __asm__ volatile ("nop");
  572. __asm__ volatile ("nop");
  573. #endif
  574. // programs using the watchdog timer or needing to initialize hardware as
  575. // early as possible can implement startup_early_hook()
  576. startup_early_hook();
  577. // enable clocks to always-used peripherals
  578. #if defined(__MK20DX128__)
  579. SIM_SCGC5 = 0x00043F82; // clocks active to all GPIO
  580. SIM_SCGC6 = SIM_SCGC6_RTC | SIM_SCGC6_FTM0 | SIM_SCGC6_FTM1 | SIM_SCGC6_ADC0 | SIM_SCGC6_FTFL;
  581. #elif defined(__MK20DX256__)
  582. SIM_SCGC3 = SIM_SCGC3_ADC1 | SIM_SCGC3_FTM2;
  583. SIM_SCGC5 = 0x00043F82; // clocks active to all GPIO
  584. SIM_SCGC6 = SIM_SCGC6_RTC | SIM_SCGC6_FTM0 | SIM_SCGC6_FTM1 | SIM_SCGC6_ADC0 | SIM_SCGC6_FTFL;
  585. #elif defined(__MK66FX1M0__)
  586. SIM_SCGC3 = SIM_SCGC3_ADC1 | SIM_SCGC3_FTM2 | SIM_SCGC3_FTM3;
  587. SIM_SCGC5 = 0x00043F82; // clocks active to all GPIO
  588. SIM_SCGC6 = SIM_SCGC6_RTC | SIM_SCGC6_FTM0 | SIM_SCGC6_FTM1 | SIM_SCGC6_ADC0 | SIM_SCGC6_FTFL;
  589. PORTC_PCR5 = PORT_PCR_MUX(1) | PORT_PCR_DSE | PORT_PCR_SRE;
  590. GPIOC_PDDR |= (1<<5);
  591. GPIOC_PSOR = (1<<5);
  592. //while (1);
  593. #elif defined(__MKL26Z64__)
  594. SIM_SCGC4 = SIM_SCGC4_USBOTG | 0xF0000030;
  595. SIM_SCGC5 = 0x00003F82; // clocks active to all GPIO
  596. SIM_SCGC6 = SIM_SCGC6_ADC0 | SIM_SCGC6_TPM0 | SIM_SCGC6_TPM1 | SIM_SCGC6_TPM2 | SIM_SCGC6_FTFL;
  597. #endif
  598. #if 0
  599. // testing only, enable ser_print
  600. SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIV1(0) | SIM_CLKDIV1_OUTDIV4(1);
  601. MCG_C4 |= MCG_C4_DMX32 | MCG_C4_DRST_DRS(1);
  602. SIM_SOPT2 = SIM_SOPT2_UART0SRC(1) | SIM_SOPT2_TPMSRC(1);
  603. SIM_SCGC4 |= 0x00000400;
  604. UART0_BDH = 0;
  605. UART0_BDL = 26; // 115200 at 48 MHz
  606. UART0_C2 = UART_C2_TE;
  607. PORTB_PCR17 = PORT_PCR_MUX(3);
  608. #endif
  609. #ifdef KINETISK
  610. // if the RTC oscillator isn't enabled, get it started early
  611. if (!(RTC_CR & RTC_CR_OSCE)) {
  612. RTC_SR = 0;
  613. RTC_CR = RTC_CR_SC16P | RTC_CR_SC4P | RTC_CR_OSCE;
  614. }
  615. #endif
  616. // release I/O pins hold, if we woke up from VLLS mode
  617. if (PMC_REGSC & PMC_REGSC_ACKISO) PMC_REGSC |= PMC_REGSC_ACKISO;
  618. // since this is a write once register, make it visible to all F_CPU's
  619. // so we can into other sleep modes in the future at any speed
  620. SMC_PMPROT = SMC_PMPROT_AVLP | SMC_PMPROT_ALLS | SMC_PMPROT_AVLLS;
  621. // TODO: do this while the PLL is waiting to lock....
  622. while (dest < &_edata) *dest++ = *src++;
  623. dest = &_sbss;
  624. while (dest < &_ebss) *dest++ = 0;
  625. // default all interrupts to medium priority level
  626. for (i=0; i < NVIC_NUM_INTERRUPTS + 16; i++) _VectorsRam[i] = _VectorsFlash[i];
  627. for (i=0; i < NVIC_NUM_INTERRUPTS; i++) NVIC_SET_PRIORITY(i, 128);
  628. SCB_VTOR = (uint32_t)_VectorsRam; // use vector table in RAM
  629. // hardware always starts in FEI mode
  630. // C1[CLKS] bits are written to 00
  631. // C1[IREFS] bit is written to 1
  632. // C6[PLLS] bit is written to 0
  633. // MCG_SC[FCDIV] defaults to divide by two for internal ref clock
  634. // I tried changing MSG_SC to divide by 1, it didn't work for me
  635. #if F_CPU <= 2000000
  636. #if defined(KINETISK)
  637. MCG_C1 = MCG_C1_CLKS(1) | MCG_C1_IREFS;
  638. #elif defined(KINETISL)
  639. // use the internal oscillator
  640. MCG_C1 = MCG_C1_CLKS(1) | MCG_C1_IREFS | MCG_C1_IRCLKEN;
  641. #endif
  642. // wait for MCGOUT to use oscillator
  643. while ((MCG_S & MCG_S_CLKST_MASK) != MCG_S_CLKST(1)) ;
  644. for (n=0; n<10; n++) ; // TODO: why do we get 2 mA extra without this delay?
  645. MCG_C2 = MCG_C2_IRCS;
  646. while (!(MCG_S & MCG_S_IRCST)) ;
  647. // now in FBI mode:
  648. // C1[CLKS] bits are written to 01
  649. // C1[IREFS] bit is written to 1
  650. // C6[PLLS] is written to 0
  651. // C2[LP] is written to 0
  652. MCG_C2 = MCG_C2_IRCS | MCG_C2_LP;
  653. // now in BLPI mode:
  654. // C1[CLKS] bits are written to 01
  655. // C1[IREFS] bit is written to 1
  656. // C6[PLLS] bit is written to 0
  657. // C2[LP] bit is written to 1
  658. #else
  659. #if defined(KINETISK)
  660. // enable capacitors for crystal
  661. OSC0_CR = OSC_SC8P | OSC_SC2P;
  662. #elif defined(KINETISL)
  663. // enable capacitors for crystal
  664. OSC0_CR = OSC_SC8P | OSC_SC2P | OSC_ERCLKEN;
  665. #endif
  666. // enable osc, 8-32 MHz range, low power mode
  667. MCG_C2 = MCG_C2_RANGE0(2) | MCG_C2_EREFS;
  668. // switch to crystal as clock source, FLL input = 16 MHz / 512
  669. MCG_C1 = MCG_C1_CLKS(2) | MCG_C1_FRDIV(4);
  670. // wait for crystal oscillator to begin
  671. while ((MCG_S & MCG_S_OSCINIT0) == 0) ;
  672. // wait for FLL to use oscillator
  673. while ((MCG_S & MCG_S_IREFST) != 0) ;
  674. // wait for MCGOUT to use oscillator
  675. while ((MCG_S & MCG_S_CLKST_MASK) != MCG_S_CLKST(2)) ;
  676. // now in FBE mode
  677. // C1[CLKS] bits are written to 10
  678. // C1[IREFS] bit is written to 0
  679. // C1[FRDIV] must be written to divide xtal to 31.25-39 kHz
  680. // C6[PLLS] bit is written to 0
  681. // C2[LP] is written to 0
  682. #if F_CPU <= 16000000
  683. // if the crystal is fast enough, use it directly (no FLL or PLL)
  684. MCG_C2 = MCG_C2_RANGE0(2) | MCG_C2_EREFS | MCG_C2_LP;
  685. // BLPE mode:
  686. // C1[CLKS] bits are written to 10
  687. // C1[IREFS] bit is written to 0
  688. // C2[LP] bit is written to 1
  689. #else
  690. // if we need faster than the crystal, turn on the PLL
  691. #if defined(__MK66FX1M0__)
  692. #if F_CPU == 96000000
  693. MCG_C5 = MCG_C5_PRDIV0(1);
  694. MCG_C6 = MCG_C6_PLLS | MCG_C6_VDIV0(8);
  695. #else
  696. #error "MK66FX1M0 only supports 96 MHz so far...."
  697. #endif
  698. #else
  699. #if F_CPU == 72000000
  700. MCG_C5 = MCG_C5_PRDIV0(5); // config PLL input for 16 MHz Crystal / 6 = 2.667 Hz
  701. #else
  702. MCG_C5 = MCG_C5_PRDIV0(3); // config PLL input for 16 MHz Crystal / 4 = 4 MHz
  703. #endif
  704. #if F_CPU == 168000000
  705. MCG_C6 = MCG_C6_PLLS | MCG_C6_VDIV0(18); // config PLL for 168 MHz output
  706. #elif F_CPU == 144000000
  707. MCG_C6 = MCG_C6_PLLS | MCG_C6_VDIV0(12); // config PLL for 144 MHz output
  708. #elif F_CPU == 120000000
  709. MCG_C6 = MCG_C6_PLLS | MCG_C6_VDIV0(6); // config PLL for 120 MHz output
  710. #elif F_CPU == 72000000
  711. MCG_C6 = MCG_C6_PLLS | MCG_C6_VDIV0(3); // config PLL for 72 MHz output
  712. #else
  713. MCG_C6 = MCG_C6_PLLS | MCG_C6_VDIV0(0); // config PLL for 96 MHz output
  714. #endif
  715. #endif
  716. // wait for PLL to start using xtal as its input
  717. while (!(MCG_S & MCG_S_PLLST)) ;
  718. // wait for PLL to lock
  719. while (!(MCG_S & MCG_S_LOCK0)) ;
  720. // now we're in PBE mode
  721. #endif
  722. #endif
  723. // now program the clock dividers
  724. #if F_CPU == 168000000
  725. // config divisors: 168 MHz core, 56 MHz bus, 28 MHz flash, USB = 168 * 2 / 7
  726. SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIV1(0) | SIM_CLKDIV1_OUTDIV2(2) | SIM_CLKDIV1_OUTDIV4(5);
  727. SIM_CLKDIV2 = SIM_CLKDIV2_USBDIV(6) | SIM_CLKDIV2_USBFRAC;
  728. #elif F_CPU == 144000000
  729. // config divisors: 144 MHz core, 48 MHz bus, 28.8 MHz flash, USB = 144 / 3
  730. SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIV1(0) | SIM_CLKDIV1_OUTDIV2(2) | SIM_CLKDIV1_OUTDIV4(4);
  731. SIM_CLKDIV2 = SIM_CLKDIV2_USBDIV(2);
  732. #elif F_CPU == 120000000
  733. // config divisors: 120 MHz core, 60 MHz bus, 24 MHz flash, USB = 128 * 2 / 5
  734. SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIV1(0) | SIM_CLKDIV1_OUTDIV2(1) | SIM_CLKDIV1_OUTDIV4(4);
  735. SIM_CLKDIV2 = SIM_CLKDIV2_USBDIV(4) | SIM_CLKDIV2_USBFRAC;
  736. #elif F_CPU == 96000000
  737. // config divisors: 96 MHz core, 48 MHz bus, 24 MHz flash, USB = 96 / 2
  738. SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIV1(0) | SIM_CLKDIV1_OUTDIV2(1) | SIM_CLKDIV1_OUTDIV4(3);
  739. SIM_CLKDIV2 = SIM_CLKDIV2_USBDIV(1);
  740. #elif F_CPU == 72000000
  741. // config divisors: 72 MHz core, 36 MHz bus, 24 MHz flash, USB = 72 * 2 / 3
  742. SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIV1(0) | SIM_CLKDIV1_OUTDIV2(1) | SIM_CLKDIV1_OUTDIV4(2);
  743. SIM_CLKDIV2 = SIM_CLKDIV2_USBDIV(2) | SIM_CLKDIV2_USBFRAC;
  744. #elif F_CPU == 48000000
  745. // config divisors: 48 MHz core, 48 MHz bus, 24 MHz flash, USB = 96 / 2
  746. #if defined(KINETISK)
  747. SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIV1(1) | SIM_CLKDIV1_OUTDIV2(1) | SIM_CLKDIV1_OUTDIV4(3);
  748. SIM_CLKDIV2 = SIM_CLKDIV2_USBDIV(1);
  749. #elif defined(KINETISL)
  750. SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIV1(1) | SIM_CLKDIV1_OUTDIV4(1);
  751. #endif
  752. #elif F_CPU == 24000000
  753. // config divisors: 24 MHz core, 24 MHz bus, 24 MHz flash, USB = 96 / 2
  754. #if defined(KINETISK)
  755. SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIV1(3) | SIM_CLKDIV1_OUTDIV2(3) | SIM_CLKDIV1_OUTDIV4(3);
  756. SIM_CLKDIV2 = SIM_CLKDIV2_USBDIV(1);
  757. #elif defined(KINETISL)
  758. SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIV1(3) | SIM_CLKDIV1_OUTDIV4(0);
  759. #endif
  760. #elif F_CPU == 16000000
  761. // config divisors: 16 MHz core, 16 MHz bus, 16 MHz flash
  762. #if defined(KINETISK)
  763. SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIV1(0) | SIM_CLKDIV1_OUTDIV2(0) | SIM_CLKDIV1_OUTDIV4(0);
  764. #elif defined(KINETISL)
  765. SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIV1(0) | SIM_CLKDIV1_OUTDIV4(0);
  766. #endif
  767. #elif F_CPU == 8000000
  768. // config divisors: 8 MHz core, 8 MHz bus, 8 MHz flash
  769. #if defined(KINETISK)
  770. SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIV1(1) | SIM_CLKDIV1_OUTDIV2(1) | SIM_CLKDIV1_OUTDIV4(1);
  771. #elif defined(KINETISL)
  772. SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIV1(1) | SIM_CLKDIV1_OUTDIV4(0);
  773. #endif
  774. #elif F_CPU == 4000000
  775. // config divisors: 4 MHz core, 4 MHz bus, 2 MHz flash
  776. // since we are running from external clock 16MHz
  777. // fix outdiv too -> cpu 16/4, bus 16/4, flash 16/4
  778. // here we can go into vlpr?
  779. // config divisors: 4 MHz core, 4 MHz bus, 4 MHz flash
  780. #if defined(KINETISK)
  781. SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIV1(3) | SIM_CLKDIV1_OUTDIV2(3) | SIM_CLKDIV1_OUTDIV4(3);
  782. #elif defined(KINETISL)
  783. SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIV1(3) | SIM_CLKDIV1_OUTDIV4(0);
  784. #endif
  785. #elif F_CPU == 2000000
  786. // since we are running from the fast internal reference clock 4MHz
  787. // but is divided down by 2 so we actually have a 2MHz, MCG_SC[FCDIV] default is 2
  788. // fix outdiv -> cpu 2/1, bus 2/1, flash 2/2
  789. // config divisors: 2 MHz core, 2 MHz bus, 1 MHz flash
  790. #if defined(KINETISK)
  791. SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIV1(0) | SIM_CLKDIV1_OUTDIV2(0) | SIM_CLKDIV1_OUTDIV4(1);
  792. #elif defined(KINETISL)
  793. // config divisors: 2 MHz core, 1 MHz bus, 1 MHz flash
  794. SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIV1(0) | SIM_CLKDIV1_OUTDIV4(1);
  795. #endif
  796. #else
  797. #error "Error, F_CPU must be 168, 144, 120, 96, 72, 48, 24, 16, 8, 4, or 2 MHz"
  798. #endif
  799. #if F_CPU > 16000000
  800. // switch to PLL as clock source, FLL input = 16 MHz / 512
  801. MCG_C1 = MCG_C1_CLKS(0) | MCG_C1_FRDIV(4);
  802. // wait for PLL clock to be used
  803. while ((MCG_S & MCG_S_CLKST_MASK) != MCG_S_CLKST(3)) ;
  804. // now we're in PEE mode
  805. // USB uses PLL clock, trace is CPU clock, CLKOUT=OSCERCLK0
  806. #if defined(KINETISK)
  807. SIM_SOPT2 = SIM_SOPT2_USBSRC | SIM_SOPT2_PLLFLLSEL | SIM_SOPT2_TRACECLKSEL
  808. | SIM_SOPT2_CLKOUTSEL(6);
  809. #elif defined(KINETISL)
  810. SIM_SOPT2 = SIM_SOPT2_USBSRC | SIM_SOPT2_PLLFLLSEL | SIM_SOPT2_CLKOUTSEL(6)
  811. | SIM_SOPT2_UART0SRC(1) | SIM_SOPT2_TPMSRC(1);
  812. #endif
  813. #else
  814. #if F_CPU == 2000000
  815. SIM_SOPT2 = SIM_SOPT2_TRACECLKSEL | SIM_SOPT2_CLKOUTSEL(4) | SIM_SOPT2_UART0SRC(3);
  816. #else
  817. SIM_SOPT2 = SIM_SOPT2_TRACECLKSEL | SIM_SOPT2_CLKOUTSEL(6) | SIM_SOPT2_UART0SRC(2);
  818. #endif
  819. #endif
  820. #if F_CPU <= 2000000
  821. // since we are not going into "stop mode" i removed it
  822. SMC_PMCTRL = SMC_PMCTRL_RUNM(2); // VLPR mode :-)
  823. #endif
  824. // initialize the SysTick counter
  825. SYST_RVR = (F_CPU / 1000) - 1;
  826. SYST_CVR = 0;
  827. SYST_CSR = SYST_CSR_CLKSOURCE | SYST_CSR_TICKINT | SYST_CSR_ENABLE;
  828. SCB_SHPR3 = 0x20200000; // Systick = priority 32
  829. //init_pins();
  830. __enable_irq();
  831. _init_Teensyduino_internal_();
  832. #if defined(KINETISK)
  833. // RTC initialization
  834. if (RTC_SR & RTC_SR_TIF) {
  835. // this code will normally run on a power-up reset
  836. // when VBAT has detected a power-up. Normally our
  837. // compiled-in time will be stale. Write a special
  838. // flag into the VBAT register file indicating the
  839. // RTC is set with known-stale time and should be
  840. // updated when fresh time is known.
  841. #if ARDUINO >= 10600
  842. rtc_set((uint32_t)&__rtc_localtime);
  843. #else
  844. rtc_set(TIME_T);
  845. #endif
  846. *(uint32_t *)0x4003E01C = 0x5A94C3A5;
  847. }
  848. if ((RCM_SRS0 & RCM_SRS0_PIN) && (*(uint32_t *)0x4003E01C == 0x5A94C3A5)) {
  849. // this code should run immediately after an upload
  850. // where the Teensy Loader causes the Mini54 to reset.
  851. // Our compiled-in time will be very fresh, so set
  852. // the RTC with this, and clear the VBAT resister file
  853. // data so we don't mess with the time after it's been
  854. // set well.
  855. #if ARDUINO >= 10600
  856. rtc_set((uint32_t)&__rtc_localtime);
  857. #else
  858. rtc_set(TIME_T);
  859. #endif
  860. *(uint32_t *)0x4003E01C = 0;
  861. }
  862. #endif
  863. __libc_init_array();
  864. startup_late_hook();
  865. main();
  866. while (1) ;
  867. }
  868. char *__brkval = (char *)&_ebss;
  869. void * _sbrk(int incr)
  870. {
  871. char *prev = __brkval;
  872. __brkval += incr;
  873. return prev;
  874. }
  875. __attribute__((weak))
  876. int _read(int file, char *ptr, int len)
  877. {
  878. return 0;
  879. }
  880. __attribute__((weak))
  881. int _close(int fd)
  882. {
  883. return -1;
  884. }
  885. #include <sys/stat.h>
  886. __attribute__((weak))
  887. int _fstat(int fd, struct stat *st)
  888. {
  889. st->st_mode = S_IFCHR;
  890. return 0;
  891. }
  892. __attribute__((weak))
  893. int _isatty(int fd)
  894. {
  895. return 1;
  896. }
  897. __attribute__((weak))
  898. int _lseek(int fd, long long offset, int whence)
  899. {
  900. return -1;
  901. }
  902. __attribute__((weak))
  903. void _exit(int status)
  904. {
  905. while (1);
  906. }
  907. __attribute__((weak))
  908. void __cxa_pure_virtual()
  909. {
  910. while (1);
  911. }
  912. __attribute__((weak))
  913. int __cxa_guard_acquire (char *g)
  914. {
  915. return !(*g);
  916. }
  917. __attribute__((weak))
  918. void __cxa_guard_release(char *g)
  919. {
  920. *g = 1;
  921. }
  922. int nvic_execution_priority(void)
  923. {
  924. int priority=256;
  925. uint32_t primask, faultmask, basepri, ipsr;
  926. // full algorithm in ARM DDI0403D, page B1-639
  927. // this isn't quite complete, but hopefully good enough
  928. __asm__ volatile("mrs %0, faultmask\n" : "=r" (faultmask)::);
  929. if (faultmask) return -1;
  930. __asm__ volatile("mrs %0, primask\n" : "=r" (primask)::);
  931. if (primask) return 0;
  932. __asm__ volatile("mrs %0, ipsr\n" : "=r" (ipsr)::);
  933. if (ipsr) {
  934. if (ipsr < 16) priority = 0; // could be non-zero
  935. else priority = NVIC_GET_PRIORITY(ipsr - 16);
  936. }
  937. __asm__ volatile("mrs %0, basepri\n" : "=r" (basepri)::);
  938. if (basepri > 0 && basepri < priority) priority = basepri;
  939. return priority;
  940. }