Teensy 4.1 core updated for C++20
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

mk20dx128.c 26KB

10 år sedan
10 år sedan
10 år sedan
10 år sedan
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667
  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. extern unsigned long _stext;
  32. extern unsigned long _etext;
  33. extern unsigned long _sdata;
  34. extern unsigned long _edata;
  35. extern unsigned long _sbss;
  36. extern unsigned long _ebss;
  37. extern unsigned long _estack;
  38. //extern void __init_array_start(void);
  39. //extern void __init_array_end(void);
  40. extern int main (void);
  41. void ResetHandler(void);
  42. void _init_Teensyduino_internal_(void);
  43. void __libc_init_array(void);
  44. void fault_isr(void)
  45. {
  46. while (1) {
  47. // keep polling some communication while in fault
  48. // mode, so we don't completely die.
  49. if (SIM_SCGC4 & SIM_SCGC4_USBOTG) usb_isr();
  50. if (SIM_SCGC4 & SIM_SCGC4_UART0) uart0_status_isr();
  51. if (SIM_SCGC4 & SIM_SCGC4_UART1) uart1_status_isr();
  52. if (SIM_SCGC4 & SIM_SCGC4_UART2) uart2_status_isr();
  53. }
  54. }
  55. void unused_isr(void)
  56. {
  57. fault_isr();
  58. }
  59. extern volatile uint32_t systick_millis_count;
  60. void systick_default_isr(void)
  61. {
  62. systick_millis_count++;
  63. }
  64. void nmi_isr(void) __attribute__ ((weak, alias("unused_isr")));
  65. void hard_fault_isr(void) __attribute__ ((weak, alias("unused_isr")));
  66. void memmanage_fault_isr(void) __attribute__ ((weak, alias("unused_isr")));
  67. void bus_fault_isr(void) __attribute__ ((weak, alias("unused_isr")));
  68. void usage_fault_isr(void) __attribute__ ((weak, alias("unused_isr")));
  69. void svcall_isr(void) __attribute__ ((weak, alias("unused_isr")));
  70. void debugmonitor_isr(void) __attribute__ ((weak, alias("unused_isr")));
  71. void pendablesrvreq_isr(void) __attribute__ ((weak, alias("unused_isr")));
  72. void systick_isr(void) __attribute__ ((weak, alias("systick_default_isr")));
  73. void dma_ch0_isr(void) __attribute__ ((weak, alias("unused_isr")));
  74. void dma_ch1_isr(void) __attribute__ ((weak, alias("unused_isr")));
  75. void dma_ch2_isr(void) __attribute__ ((weak, alias("unused_isr")));
  76. void dma_ch3_isr(void) __attribute__ ((weak, alias("unused_isr")));
  77. void dma_ch4_isr(void) __attribute__ ((weak, alias("unused_isr")));
  78. void dma_ch5_isr(void) __attribute__ ((weak, alias("unused_isr")));
  79. void dma_ch6_isr(void) __attribute__ ((weak, alias("unused_isr")));
  80. void dma_ch7_isr(void) __attribute__ ((weak, alias("unused_isr")));
  81. void dma_ch8_isr(void) __attribute__ ((weak, alias("unused_isr")));
  82. void dma_ch9_isr(void) __attribute__ ((weak, alias("unused_isr")));
  83. void dma_ch10_isr(void) __attribute__ ((weak, alias("unused_isr")));
  84. void dma_ch11_isr(void) __attribute__ ((weak, alias("unused_isr")));
  85. void dma_ch12_isr(void) __attribute__ ((weak, alias("unused_isr")));
  86. void dma_ch13_isr(void) __attribute__ ((weak, alias("unused_isr")));
  87. void dma_ch14_isr(void) __attribute__ ((weak, alias("unused_isr")));
  88. void dma_ch15_isr(void) __attribute__ ((weak, alias("unused_isr")));
  89. void dma_error_isr(void) __attribute__ ((weak, alias("unused_isr")));
  90. void mcm_isr(void) __attribute__ ((weak, alias("unused_isr")));
  91. void flash_cmd_isr(void) __attribute__ ((weak, alias("unused_isr")));
  92. void flash_error_isr(void) __attribute__ ((weak, alias("unused_isr")));
  93. void low_voltage_isr(void) __attribute__ ((weak, alias("unused_isr")));
  94. void wakeup_isr(void) __attribute__ ((weak, alias("unused_isr")));
  95. void watchdog_isr(void) __attribute__ ((weak, alias("unused_isr")));
  96. void i2c0_isr(void) __attribute__ ((weak, alias("unused_isr")));
  97. void i2c1_isr(void) __attribute__ ((weak, alias("unused_isr")));
  98. void i2c2_isr(void) __attribute__ ((weak, alias("unused_isr")));
  99. void spi0_isr(void) __attribute__ ((weak, alias("unused_isr")));
  100. void spi1_isr(void) __attribute__ ((weak, alias("unused_isr")));
  101. void spi2_isr(void) __attribute__ ((weak, alias("unused_isr")));
  102. void sdhc_isr(void) __attribute__ ((weak, alias("unused_isr")));
  103. void can0_message_isr(void) __attribute__ ((weak, alias("unused_isr")));
  104. void can0_bus_off_isr(void) __attribute__ ((weak, alias("unused_isr")));
  105. void can0_error_isr(void) __attribute__ ((weak, alias("unused_isr")));
  106. void can0_tx_warn_isr(void) __attribute__ ((weak, alias("unused_isr")));
  107. void can0_rx_warn_isr(void) __attribute__ ((weak, alias("unused_isr")));
  108. void can0_wakeup_isr(void) __attribute__ ((weak, alias("unused_isr")));
  109. void i2s0_tx_isr(void) __attribute__ ((weak, alias("unused_isr")));
  110. void i2s0_rx_isr(void) __attribute__ ((weak, alias("unused_isr")));
  111. void uart0_lon_isr(void) __attribute__ ((weak, alias("unused_isr")));
  112. void uart0_status_isr(void) __attribute__ ((weak, alias("unused_isr")));
  113. void uart0_error_isr(void) __attribute__ ((weak, alias("unused_isr")));
  114. void uart1_status_isr(void) __attribute__ ((weak, alias("unused_isr")));
  115. void uart1_error_isr(void) __attribute__ ((weak, alias("unused_isr")));
  116. void uart2_status_isr(void) __attribute__ ((weak, alias("unused_isr")));
  117. void uart2_error_isr(void) __attribute__ ((weak, alias("unused_isr")));
  118. void uart3_status_isr(void) __attribute__ ((weak, alias("unused_isr")));
  119. void uart3_error_isr(void) __attribute__ ((weak, alias("unused_isr")));
  120. void uart4_status_isr(void) __attribute__ ((weak, alias("unused_isr")));
  121. void uart4_error_isr(void) __attribute__ ((weak, alias("unused_isr")));
  122. void uart5_status_isr(void) __attribute__ ((weak, alias("unused_isr")));
  123. void uart5_error_isr(void) __attribute__ ((weak, alias("unused_isr")));
  124. void adc0_isr(void) __attribute__ ((weak, alias("unused_isr")));
  125. void adc1_isr(void) __attribute__ ((weak, alias("unused_isr")));
  126. void cmp0_isr(void) __attribute__ ((weak, alias("unused_isr")));
  127. void cmp1_isr(void) __attribute__ ((weak, alias("unused_isr")));
  128. void cmp2_isr(void) __attribute__ ((weak, alias("unused_isr")));
  129. void ftm0_isr(void) __attribute__ ((weak, alias("unused_isr")));
  130. void ftm1_isr(void) __attribute__ ((weak, alias("unused_isr")));
  131. void ftm2_isr(void) __attribute__ ((weak, alias("unused_isr")));
  132. void ftm3_isr(void) __attribute__ ((weak, alias("unused_isr")));
  133. void cmt_isr(void) __attribute__ ((weak, alias("unused_isr")));
  134. void rtc_alarm_isr(void) __attribute__ ((weak, alias("unused_isr")));
  135. void rtc_seconds_isr(void) __attribute__ ((weak, alias("unused_isr")));
  136. void pit0_isr(void) __attribute__ ((weak, alias("unused_isr")));
  137. void pit1_isr(void) __attribute__ ((weak, alias("unused_isr")));
  138. void pit2_isr(void) __attribute__ ((weak, alias("unused_isr")));
  139. void pit3_isr(void) __attribute__ ((weak, alias("unused_isr")));
  140. void pdb_isr(void) __attribute__ ((weak, alias("unused_isr")));
  141. void usb_isr(void) __attribute__ ((weak, alias("unused_isr")));
  142. void usb_charge_isr(void) __attribute__ ((weak, alias("unused_isr")));
  143. void dac0_isr(void) __attribute__ ((weak, alias("unused_isr")));
  144. void dac1_isr(void) __attribute__ ((weak, alias("unused_isr")));
  145. void tsi0_isr(void) __attribute__ ((weak, alias("unused_isr")));
  146. void mcg_isr(void) __attribute__ ((weak, alias("unused_isr")));
  147. void lptmr_isr(void) __attribute__ ((weak, alias("unused_isr")));
  148. void porta_isr(void) __attribute__ ((weak, alias("unused_isr")));
  149. void portb_isr(void) __attribute__ ((weak, alias("unused_isr")));
  150. void portc_isr(void) __attribute__ ((weak, alias("unused_isr")));
  151. void portd_isr(void) __attribute__ ((weak, alias("unused_isr")));
  152. void porte_isr(void) __attribute__ ((weak, alias("unused_isr")));
  153. void software_isr(void) __attribute__ ((weak, alias("unused_isr")));
  154. #if defined(__MK20DX128__)
  155. __attribute__ ((section(".dmabuffers"), used, aligned(256)))
  156. #else
  157. __attribute__ ((section(".dmabuffers"), used, aligned(512)))
  158. #endif
  159. void (* _VectorsRam[NVIC_NUM_INTERRUPTS+16])(void);
  160. __attribute__ ((section(".vectors"), used))
  161. void (* const _VectorsFlash[NVIC_NUM_INTERRUPTS+16])(void) =
  162. {
  163. (void (*)(void))((unsigned long)&_estack), // 0 ARM: Initial Stack Pointer
  164. ResetHandler, // 1 ARM: Initial Program Counter
  165. nmi_isr, // 2 ARM: Non-maskable Interrupt (NMI)
  166. hard_fault_isr, // 3 ARM: Hard Fault
  167. memmanage_fault_isr, // 4 ARM: MemManage Fault
  168. bus_fault_isr, // 5 ARM: Bus Fault
  169. usage_fault_isr, // 6 ARM: Usage Fault
  170. fault_isr, // 7 --
  171. fault_isr, // 8 --
  172. fault_isr, // 9 --
  173. fault_isr, // 10 --
  174. svcall_isr, // 11 ARM: Supervisor call (SVCall)
  175. debugmonitor_isr, // 12 ARM: Debug Monitor
  176. fault_isr, // 13 --
  177. pendablesrvreq_isr, // 14 ARM: Pendable req serv(PendableSrvReq)
  178. systick_isr, // 15 ARM: System tick timer (SysTick)
  179. #if defined(__MK20DX128__)
  180. dma_ch0_isr, // 16 DMA channel 0 transfer complete
  181. dma_ch1_isr, // 17 DMA channel 1 transfer complete
  182. dma_ch2_isr, // 18 DMA channel 2 transfer complete
  183. dma_ch3_isr, // 19 DMA channel 3 transfer complete
  184. dma_error_isr, // 20 DMA error interrupt channel
  185. unused_isr, // 21 DMA --
  186. flash_cmd_isr, // 22 Flash Memory Command complete
  187. flash_error_isr, // 23 Flash Read collision
  188. low_voltage_isr, // 24 Low-voltage detect/warning
  189. wakeup_isr, // 25 Low Leakage Wakeup
  190. watchdog_isr, // 26 Both EWM and WDOG interrupt
  191. i2c0_isr, // 27 I2C0
  192. spi0_isr, // 28 SPI0
  193. i2s0_tx_isr, // 29 I2S0 Transmit
  194. i2s0_rx_isr, // 30 I2S0 Receive
  195. uart0_lon_isr, // 31 UART0 CEA709.1-B (LON) status
  196. uart0_status_isr, // 32 UART0 status
  197. uart0_error_isr, // 33 UART0 error
  198. uart1_status_isr, // 34 UART1 status
  199. uart1_error_isr, // 35 UART1 error
  200. uart2_status_isr, // 36 UART2 status
  201. uart2_error_isr, // 37 UART2 error
  202. adc0_isr, // 38 ADC0
  203. cmp0_isr, // 39 CMP0
  204. cmp1_isr, // 40 CMP1
  205. ftm0_isr, // 41 FTM0
  206. ftm1_isr, // 42 FTM1
  207. cmt_isr, // 43 CMT
  208. rtc_alarm_isr, // 44 RTC Alarm interrupt
  209. rtc_seconds_isr, // 45 RTC Seconds interrupt
  210. pit0_isr, // 46 PIT Channel 0
  211. pit1_isr, // 47 PIT Channel 1
  212. pit2_isr, // 48 PIT Channel 2
  213. pit3_isr, // 49 PIT Channel 3
  214. pdb_isr, // 50 PDB Programmable Delay Block
  215. usb_isr, // 51 USB OTG
  216. usb_charge_isr, // 52 USB Charger Detect
  217. tsi0_isr, // 53 TSI0
  218. mcg_isr, // 54 MCG
  219. lptmr_isr, // 55 Low Power Timer
  220. porta_isr, // 56 Pin detect (Port A)
  221. portb_isr, // 57 Pin detect (Port B)
  222. portc_isr, // 58 Pin detect (Port C)
  223. portd_isr, // 59 Pin detect (Port D)
  224. porte_isr, // 60 Pin detect (Port E)
  225. software_isr, // 61 Software interrupt
  226. #elif defined(__MK20DX256__)
  227. dma_ch0_isr, // 16 DMA channel 0 transfer complete
  228. dma_ch1_isr, // 17 DMA channel 1 transfer complete
  229. dma_ch2_isr, // 18 DMA channel 2 transfer complete
  230. dma_ch3_isr, // 19 DMA channel 3 transfer complete
  231. dma_ch4_isr, // 20 DMA channel 4 transfer complete
  232. dma_ch5_isr, // 21 DMA channel 5 transfer complete
  233. dma_ch6_isr, // 22 DMA channel 6 transfer complete
  234. dma_ch7_isr, // 23 DMA channel 7 transfer complete
  235. dma_ch8_isr, // 24 DMA channel 8 transfer complete
  236. dma_ch9_isr, // 25 DMA channel 9 transfer complete
  237. dma_ch10_isr, // 26 DMA channel 10 transfer complete
  238. dma_ch11_isr, // 27 DMA channel 10 transfer complete
  239. dma_ch12_isr, // 28 DMA channel 10 transfer complete
  240. dma_ch13_isr, // 29 DMA channel 10 transfer complete
  241. dma_ch14_isr, // 30 DMA channel 10 transfer complete
  242. dma_ch15_isr, // 31 DMA channel 10 transfer complete
  243. dma_error_isr, // 32 DMA error interrupt channel
  244. unused_isr, // 33 --
  245. flash_cmd_isr, // 34 Flash Memory Command complete
  246. flash_error_isr, // 35 Flash Read collision
  247. low_voltage_isr, // 36 Low-voltage detect/warning
  248. wakeup_isr, // 37 Low Leakage Wakeup
  249. watchdog_isr, // 38 Both EWM and WDOG interrupt
  250. unused_isr, // 39 --
  251. i2c0_isr, // 40 I2C0
  252. i2c1_isr, // 41 I2C1
  253. spi0_isr, // 42 SPI0
  254. spi1_isr, // 43 SPI1
  255. unused_isr, // 44 --
  256. can0_message_isr, // 45 CAN OR'ed Message buffer (0-15)
  257. can0_bus_off_isr, // 46 CAN Bus Off
  258. can0_error_isr, // 47 CAN Error
  259. can0_tx_warn_isr, // 48 CAN Transmit Warning
  260. can0_rx_warn_isr, // 49 CAN Receive Warning
  261. can0_wakeup_isr, // 50 CAN Wake Up
  262. i2s0_tx_isr, // 51 I2S0 Transmit
  263. i2s0_rx_isr, // 52 I2S0 Receive
  264. unused_isr, // 53 --
  265. unused_isr, // 54 --
  266. unused_isr, // 55 --
  267. unused_isr, // 56 --
  268. unused_isr, // 57 --
  269. unused_isr, // 58 --
  270. unused_isr, // 59 --
  271. uart0_lon_isr, // 60 UART0 CEA709.1-B (LON) status
  272. uart0_status_isr, // 61 UART0 status
  273. uart0_error_isr, // 62 UART0 error
  274. uart1_status_isr, // 63 UART1 status
  275. uart1_error_isr, // 64 UART1 error
  276. uart2_status_isr, // 65 UART2 status
  277. uart2_error_isr, // 66 UART2 error
  278. unused_isr, // 67 --
  279. unused_isr, // 68 --
  280. unused_isr, // 69 --
  281. unused_isr, // 70 --
  282. unused_isr, // 71 --
  283. unused_isr, // 72 --
  284. adc0_isr, // 73 ADC0
  285. adc1_isr, // 74 ADC1
  286. cmp0_isr, // 75 CMP0
  287. cmp1_isr, // 76 CMP1
  288. cmp2_isr, // 77 CMP2
  289. ftm0_isr, // 78 FTM0
  290. ftm1_isr, // 79 FTM1
  291. ftm2_isr, // 80 FTM2
  292. cmt_isr, // 81 CMT
  293. rtc_alarm_isr, // 82 RTC Alarm interrupt
  294. rtc_seconds_isr, // 83 RTC Seconds interrupt
  295. pit0_isr, // 84 PIT Channel 0
  296. pit1_isr, // 85 PIT Channel 1
  297. pit2_isr, // 86 PIT Channel 2
  298. pit3_isr, // 87 PIT Channel 3
  299. pdb_isr, // 88 PDB Programmable Delay Block
  300. usb_isr, // 89 USB OTG
  301. usb_charge_isr, // 90 USB Charger Detect
  302. unused_isr, // 91 --
  303. unused_isr, // 92 --
  304. unused_isr, // 93 --
  305. unused_isr, // 94 --
  306. unused_isr, // 95 --
  307. unused_isr, // 96 --
  308. dac0_isr, // 97 DAC0
  309. unused_isr, // 98 --
  310. tsi0_isr, // 99 TSI0
  311. mcg_isr, // 100 MCG
  312. lptmr_isr, // 101 Low Power Timer
  313. unused_isr, // 102 --
  314. porta_isr, // 103 Pin detect (Port A)
  315. portb_isr, // 104 Pin detect (Port B)
  316. portc_isr, // 105 Pin detect (Port C)
  317. portd_isr, // 106 Pin detect (Port D)
  318. porte_isr, // 107 Pin detect (Port E)
  319. unused_isr, // 108 --
  320. unused_isr, // 109 --
  321. software_isr, // 110 Software interrupt
  322. #endif
  323. };
  324. //void usb_isr(void)
  325. //{
  326. //}
  327. __attribute__ ((section(".flashconfig"), used))
  328. const uint8_t flashconfigbytes[16] = {
  329. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  330. 0xFF, 0xFF, 0xFF, 0xFF, 0xFE, 0xFF, 0xFF, 0xFF
  331. };
  332. // Automatically initialize the RTC. When the build defines the compile
  333. // time, and the user has added a crystal, the RTC will automatically
  334. // begin at the time of the first upload.
  335. #ifndef TIME_T
  336. #define TIME_T 1349049600 // default 1 Oct 2012 (never used, Arduino sets this)
  337. #endif
  338. extern void rtc_set(unsigned long t);
  339. static void startup_default_early_hook(void) { WDOG_STCTRLH = WDOG_STCTRLH_ALLOWUPDATE; }
  340. static void startup_default_late_hook(void) {}
  341. void startup_early_hook(void) __attribute__ ((weak, alias("startup_default_early_hook")));
  342. void startup_late_hook(void) __attribute__ ((weak, alias("startup_default_late_hook")));
  343. __attribute__ ((section(".startup")))
  344. void ResetHandler(void)
  345. {
  346. uint32_t *src = &_etext;
  347. uint32_t *dest = &_sdata;
  348. unsigned int i;
  349. #if F_CPU <= 2000000
  350. volatile int n;
  351. #endif
  352. WDOG_UNLOCK = WDOG_UNLOCK_SEQ1;
  353. WDOG_UNLOCK = WDOG_UNLOCK_SEQ2;
  354. __asm__ volatile ("nop");
  355. __asm__ volatile ("nop");
  356. // programs using the watchdog timer or needing to initialize hardware as
  357. // early as possible can implement startup_early_hook()
  358. startup_early_hook();
  359. // enable clocks to always-used peripherals
  360. #if defined(__MK20DX128__)
  361. SIM_SCGC5 = 0x00043F82; // clocks active to all GPIO
  362. SIM_SCGC6 = SIM_SCGC6_RTC | SIM_SCGC6_FTM0 | SIM_SCGC6_FTM1 | SIM_SCGC6_ADC0 | SIM_SCGC6_FTFL;
  363. #elif defined(__MK20DX256__)
  364. SIM_SCGC3 = SIM_SCGC3_ADC1 | SIM_SCGC3_FTM2;
  365. SIM_SCGC5 = 0x00043F82; // clocks active to all GPIO
  366. SIM_SCGC6 = SIM_SCGC6_RTC | SIM_SCGC6_FTM0 | SIM_SCGC6_FTM1 | SIM_SCGC6_ADC0 | SIM_SCGC6_FTFL;
  367. #endif
  368. // if the RTC oscillator isn't enabled, get it started early
  369. if (!(RTC_CR & RTC_CR_OSCE)) {
  370. RTC_SR = 0;
  371. RTC_CR = RTC_CR_SC16P | RTC_CR_SC4P | RTC_CR_OSCE;
  372. }
  373. // release I/O pins hold, if we woke up from VLLS mode
  374. if (PMC_REGSC & PMC_REGSC_ACKISO) PMC_REGSC |= PMC_REGSC_ACKISO;
  375. // since this is a write once register, make it visible to all F_CPU's
  376. // so we can into other sleep modes in the future at any speed
  377. SMC_PMPROT = SMC_PMPROT_AVLP | SMC_PMPROT_ALLS | SMC_PMPROT_AVLLS;
  378. // TODO: do this while the PLL is waiting to lock....
  379. while (dest < &_edata) *dest++ = *src++;
  380. dest = &_sbss;
  381. while (dest < &_ebss) *dest++ = 0;
  382. // default all interrupts to medium priority level
  383. for (i=0; i < NVIC_NUM_INTERRUPTS + 16; i++) _VectorsRam[i] = _VectorsFlash[i];
  384. for (i=0; i < NVIC_NUM_INTERRUPTS; i++) NVIC_SET_PRIORITY(i, 128);
  385. SCB_VTOR = (uint32_t)_VectorsRam; // use vector table in RAM
  386. // hardware always starts in FEI mode
  387. // C1[CLKS] bits are written to 00
  388. // C1[IREFS] bit is written to 1
  389. // C6[PLLS] bit is written to 0
  390. // MCG_SC[FCDIV] defaults to divide by two for internal ref clock
  391. // I tried changing MSG_SC to divide by 1, it didn't work for me
  392. #if F_CPU <= 2000000
  393. // use the internal oscillator
  394. MCG_C1 = MCG_C1_CLKS(1) | MCG_C1_IREFS;
  395. // wait for MCGOUT to use oscillator
  396. while ((MCG_S & MCG_S_CLKST_MASK) != MCG_S_CLKST(1)) ;
  397. for (n=0; n<10; n++) ; // TODO: why do we get 2 mA extra without this delay?
  398. MCG_C2 = MCG_C2_IRCS;
  399. while (!(MCG_S & MCG_S_IRCST)) ;
  400. // now in FBI mode:
  401. // C1[CLKS] bits are written to 01
  402. // C1[IREFS] bit is written to 1
  403. // C6[PLLS] is written to 0
  404. // C2[LP] is written to 0
  405. MCG_C2 = MCG_C2_IRCS | MCG_C2_LP;
  406. // now in BLPI mode:
  407. // C1[CLKS] bits are written to 01
  408. // C1[IREFS] bit is written to 1
  409. // C6[PLLS] bit is written to 0
  410. // C2[LP] bit is written to 1
  411. #else
  412. // enable capacitors for crystal
  413. OSC0_CR = OSC_SC8P | OSC_SC2P;
  414. // enable osc, 8-32 MHz range, low power mode
  415. MCG_C2 = MCG_C2_RANGE0(2) | MCG_C2_EREFS;
  416. // switch to crystal as clock source, FLL input = 16 MHz / 512
  417. MCG_C1 = MCG_C1_CLKS(2) | MCG_C1_FRDIV(4);
  418. // wait for crystal oscillator to begin
  419. while ((MCG_S & MCG_S_OSCINIT0) == 0) ;
  420. // wait for FLL to use oscillator
  421. while ((MCG_S & MCG_S_IREFST) != 0) ;
  422. // wait for MCGOUT to use oscillator
  423. while ((MCG_S & MCG_S_CLKST_MASK) != MCG_S_CLKST(2)) ;
  424. // now in FBE mode
  425. // C1[CLKS] bits are written to 10
  426. // C1[IREFS] bit is written to 0
  427. // C1[FRDIV] must be written to divide xtal to 31.25-39 kHz
  428. // C6[PLLS] bit is written to 0
  429. // C2[LP] is written to 0
  430. #if F_CPU <= 16000000
  431. // if the crystal is fast enough, use it directly (no FLL or PLL)
  432. MCG_C2 = MCG_C2_RANGE0(2) | MCG_C2_EREFS | MCG_C2_LP;
  433. // BLPE mode:
  434. // C1[CLKS] bits are written to 10
  435. // C1[IREFS] bit is written to 0
  436. // C2[LP] bit is written to 1
  437. #else
  438. // if we need faster than the crystal, turn on the PLL
  439. #if F_CPU == 72000000
  440. MCG_C5 = MCG_C5_PRDIV0(5); // config PLL input for 16 MHz Crystal / 6 = 2.667 Hz
  441. #else
  442. MCG_C5 = MCG_C5_PRDIV0(3); // config PLL input for 16 MHz Crystal / 4 = 4 MHz
  443. #endif
  444. #if F_CPU == 168000000
  445. MCG_C6 = MCG_C6_PLLS | MCG_C6_VDIV0(18); // config PLL for 168 MHz output
  446. #elif F_CPU == 144000000
  447. MCG_C6 = MCG_C6_PLLS | MCG_C6_VDIV0(12); // config PLL for 144 MHz output
  448. #elif F_CPU == 120000000
  449. MCG_C6 = MCG_C6_PLLS | MCG_C6_VDIV0(6); // config PLL for 120 MHz output
  450. #elif F_CPU == 72000000
  451. MCG_C6 = MCG_C6_PLLS | MCG_C6_VDIV0(3); // config PLL for 72 MHz output
  452. #else
  453. MCG_C6 = MCG_C6_PLLS | MCG_C6_VDIV0(0); // config PLL for 96 MHz output
  454. #endif
  455. // wait for PLL to start using xtal as its input
  456. while (!(MCG_S & MCG_S_PLLST)) ;
  457. // wait for PLL to lock
  458. while (!(MCG_S & MCG_S_LOCK0)) ;
  459. // now we're in PBE mode
  460. #endif
  461. #endif
  462. // now program the clock dividers
  463. #if F_CPU == 168000000
  464. // config divisors: 168 MHz core, 56 MHz bus, 33.6 MHz flash, USB = 168 * 2 / 7
  465. SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIV1(0) | SIM_CLKDIV1_OUTDIV2(2) | SIM_CLKDIV1_OUTDIV4(4);
  466. SIM_CLKDIV2 = SIM_CLKDIV2_USBDIV(6) | SIM_CLKDIV2_USBFRAC;
  467. #elif F_CPU == 144000000
  468. // config divisors: 144 MHz core, 48 MHz bus, 28.8 MHz flash, USB = 144 / 3
  469. SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIV1(0) | SIM_CLKDIV1_OUTDIV2(2) | SIM_CLKDIV1_OUTDIV4(4);
  470. SIM_CLKDIV2 = SIM_CLKDIV2_USBDIV(2);
  471. #elif F_CPU == 120000000
  472. // config divisors: 120 MHz core, 60 MHz bus, 24 MHz flash, USB = 128 * 2 / 5
  473. SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIV1(0) | SIM_CLKDIV1_OUTDIV2(1) | SIM_CLKDIV1_OUTDIV4(4);
  474. SIM_CLKDIV2 = SIM_CLKDIV2_USBDIV(4) | SIM_CLKDIV2_USBFRAC;
  475. #elif F_CPU == 96000000
  476. // config divisors: 96 MHz core, 48 MHz bus, 24 MHz flash, USB = 96 / 2
  477. SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIV1(0) | SIM_CLKDIV1_OUTDIV2(1) | SIM_CLKDIV1_OUTDIV4(3);
  478. SIM_CLKDIV2 = SIM_CLKDIV2_USBDIV(1);
  479. #elif F_CPU == 72000000
  480. // config divisors: 72 MHz core, 36 MHz bus, 24 MHz flash, USB = 72 * 2 / 3
  481. SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIV1(0) | SIM_CLKDIV1_OUTDIV2(1) | SIM_CLKDIV1_OUTDIV4(2);
  482. SIM_CLKDIV2 = SIM_CLKDIV2_USBDIV(2) | SIM_CLKDIV2_USBFRAC;
  483. #elif F_CPU == 48000000
  484. // config divisors: 48 MHz core, 48 MHz bus, 24 MHz flash, USB = 96 / 2
  485. SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIV1(1) | SIM_CLKDIV1_OUTDIV2(1) | SIM_CLKDIV1_OUTDIV4(3);
  486. SIM_CLKDIV2 = SIM_CLKDIV2_USBDIV(1);
  487. #elif F_CPU == 24000000
  488. // config divisors: 24 MHz core, 24 MHz bus, 24 MHz flash, USB = 96 / 2
  489. SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIV1(3) | SIM_CLKDIV1_OUTDIV2(3) | SIM_CLKDIV1_OUTDIV4(3);
  490. SIM_CLKDIV2 = SIM_CLKDIV2_USBDIV(1);
  491. #elif F_CPU == 16000000
  492. // config divisors: 16 MHz core, 16 MHz bus, 16 MHz flash
  493. SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIV1(0) | SIM_CLKDIV1_OUTDIV2(0) | SIM_CLKDIV1_OUTDIV4(0);
  494. #elif F_CPU == 8000000
  495. // config divisors: 8 MHz core, 8 MHz bus, 8 MHz flash
  496. SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIV1(1) | SIM_CLKDIV1_OUTDIV2(1) | SIM_CLKDIV1_OUTDIV4(1);
  497. #elif F_CPU == 4000000
  498. // config divisors: 4 MHz core, 4 MHz bus, 2 MHz flash
  499. // since we are running from external clock 16MHz
  500. // fix outdiv too -> cpu 16/4, bus 16/4, flash 16/4
  501. // here we can go into vlpr?
  502. // config divisors: 4 MHz core, 4 MHz bus, 4 MHz flash
  503. SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIV1(3) | SIM_CLKDIV1_OUTDIV2(3) | SIM_CLKDIV1_OUTDIV4(3);
  504. #elif F_CPU == 2000000
  505. // since we are running from the fast internal reference clock 4MHz
  506. // but is divided down by 2 so we actually have a 2MHz, MCG_SC[FCDIV] default is 2
  507. // fix outdiv -> cpu 2/1, bus 2/1, flash 2/2
  508. // config divisors: 2 MHz core, 2 MHz bus, 1 MHz flash
  509. SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIV1(0) | SIM_CLKDIV1_OUTDIV2(0) | SIM_CLKDIV1_OUTDIV4(1);
  510. #else
  511. #error "Error, F_CPU must be 168, 144, 120, 96, 72, 48, 24, 16, 8, 4, or 2 MHz"
  512. #endif
  513. #if F_CPU > 16000000
  514. // switch to PLL as clock source, FLL input = 16 MHz / 512
  515. MCG_C1 = MCG_C1_CLKS(0) | MCG_C1_FRDIV(4);
  516. // wait for PLL clock to be used
  517. while ((MCG_S & MCG_S_CLKST_MASK) != MCG_S_CLKST(3)) ;
  518. // now we're in PEE mode
  519. // USB uses PLL clock, trace is CPU clock, CLKOUT=OSCERCLK0
  520. SIM_SOPT2 = SIM_SOPT2_USBSRC | SIM_SOPT2_PLLFLLSEL | SIM_SOPT2_TRACECLKSEL | SIM_SOPT2_CLKOUTSEL(6);
  521. #else
  522. SIM_SOPT2 = SIM_SOPT2_TRACECLKSEL | SIM_SOPT2_CLKOUTSEL(3);
  523. #endif
  524. #if F_CPU <= 2000000
  525. // since we are not going into "stop mode" i removed it
  526. SMC_PMCTRL = SMC_PMCTRL_RUNM(2); // VLPR mode :-)
  527. #endif
  528. // initialize the SysTick counter
  529. SYST_RVR = (F_CPU / 1000) - 1;
  530. SYST_CSR = SYST_CSR_CLKSOURCE | SYST_CSR_TICKINT | SYST_CSR_ENABLE;
  531. //init_pins();
  532. __enable_irq();
  533. _init_Teensyduino_internal_();
  534. if (RTC_SR & RTC_SR_TIF) {
  535. // TODO: this should probably set the time more agressively, if
  536. // we could reliably detect the first reboot after programming.
  537. rtc_set(TIME_T);
  538. }
  539. __libc_init_array();
  540. startup_late_hook();
  541. main();
  542. while (1) ;
  543. }
  544. char *__brkval = (char *)&_ebss;
  545. void * _sbrk(int incr)
  546. {
  547. char *prev = __brkval;
  548. __brkval += incr;
  549. return prev;
  550. }
  551. __attribute__((weak))
  552. int _read(int file, char *ptr, int len)
  553. {
  554. return 0;
  555. }
  556. __attribute__((weak))
  557. int _close(int fd)
  558. {
  559. return -1;
  560. }
  561. #include <sys/stat.h>
  562. __attribute__((weak))
  563. int _fstat(int fd, struct stat *st)
  564. {
  565. st->st_mode = S_IFCHR;
  566. return 0;
  567. }
  568. __attribute__((weak))
  569. int _isatty(int fd)
  570. {
  571. return 1;
  572. }
  573. __attribute__((weak))
  574. int _lseek(int fd, long long offset, int whence)
  575. {
  576. return -1;
  577. }
  578. __attribute__((weak))
  579. void _exit(int status)
  580. {
  581. while (1);
  582. }
  583. __attribute__((weak))
  584. void __cxa_pure_virtual()
  585. {
  586. while (1);
  587. }
  588. __attribute__((weak))
  589. int __cxa_guard_acquire (char *g)
  590. {
  591. return !(*g);
  592. }
  593. __attribute__((weak))
  594. void __cxa_guard_release(char *g)
  595. {
  596. *g = 1;
  597. }
  598. int nvic_execution_priority(void)
  599. {
  600. int priority=256;
  601. uint32_t primask, faultmask, basepri, ipsr;
  602. // full algorithm in ARM DDI0403D, page B1-639
  603. // this isn't quite complete, but hopefully good enough
  604. __asm__ volatile("mrs %0, faultmask\n" : "=r" (faultmask)::);
  605. if (faultmask) return -1;
  606. __asm__ volatile("mrs %0, primask\n" : "=r" (primask)::);
  607. if (primask) return 0;
  608. __asm__ volatile("mrs %0, ipsr\n" : "=r" (ipsr)::);
  609. if (ipsr) {
  610. if (ipsr < 16) priority = 0; // could be non-zero
  611. else priority = NVIC_GET_PRIORITY(ipsr - 16);
  612. }
  613. __asm__ volatile("mrs %0, basepri\n" : "=r" (basepri)::);
  614. if (basepri > 0 && basepri < priority) priority = basepri;
  615. return priority;
  616. }