Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

mk20dx128.c 47KB

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