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 46KB

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