Teensy 4.1 core updated for C++20
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.

602 lines
22KB

  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 "mk20dx128.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. // TODO: create AVR-stype ISR() macro, with default linkage to undefined handler
  155. //
  156. __attribute__ ((section(".vectors"), used))
  157. void (* const gVectors[])(void) =
  158. {
  159. (void (*)(void))((unsigned long)&_estack), // 0 ARM: Initial Stack Pointer
  160. ResetHandler, // 1 ARM: Initial Program Counter
  161. nmi_isr, // 2 ARM: Non-maskable Interrupt (NMI)
  162. hard_fault_isr, // 3 ARM: Hard Fault
  163. memmanage_fault_isr, // 4 ARM: MemManage Fault
  164. bus_fault_isr, // 5 ARM: Bus Fault
  165. usage_fault_isr, // 6 ARM: Usage Fault
  166. fault_isr, // 7 --
  167. fault_isr, // 8 --
  168. fault_isr, // 9 --
  169. fault_isr, // 10 --
  170. svcall_isr, // 11 ARM: Supervisor call (SVCall)
  171. debugmonitor_isr, // 12 ARM: Debug Monitor
  172. fault_isr, // 13 --
  173. pendablesrvreq_isr, // 14 ARM: Pendable req serv(PendableSrvReq)
  174. systick_isr, // 15 ARM: System tick timer (SysTick)
  175. #if defined(__MK20DX128__)
  176. dma_ch0_isr, // 16 DMA channel 0 transfer complete
  177. dma_ch1_isr, // 17 DMA channel 1 transfer complete
  178. dma_ch2_isr, // 18 DMA channel 2 transfer complete
  179. dma_ch3_isr, // 19 DMA channel 3 transfer complete
  180. dma_error_isr, // 20 DMA error interrupt channel
  181. unused_isr, // 21 DMA --
  182. flash_cmd_isr, // 22 Flash Memory Command complete
  183. flash_error_isr, // 23 Flash Read collision
  184. low_voltage_isr, // 24 Low-voltage detect/warning
  185. wakeup_isr, // 25 Low Leakage Wakeup
  186. watchdog_isr, // 26 Both EWM and WDOG interrupt
  187. i2c0_isr, // 27 I2C0
  188. spi0_isr, // 28 SPI0
  189. i2s0_tx_isr, // 29 I2S0 Transmit
  190. i2s0_rx_isr, // 30 I2S0 Receive
  191. uart0_lon_isr, // 31 UART0 CEA709.1-B (LON) status
  192. uart0_status_isr, // 32 UART0 status
  193. uart0_error_isr, // 33 UART0 error
  194. uart1_status_isr, // 34 UART1 status
  195. uart1_error_isr, // 35 UART1 error
  196. uart2_status_isr, // 36 UART2 status
  197. uart2_error_isr, // 37 UART2 error
  198. adc0_isr, // 38 ADC0
  199. cmp0_isr, // 39 CMP0
  200. cmp1_isr, // 40 CMP1
  201. ftm0_isr, // 41 FTM0
  202. ftm1_isr, // 42 FTM1
  203. cmt_isr, // 43 CMT
  204. rtc_alarm_isr, // 44 RTC Alarm interrupt
  205. rtc_seconds_isr, // 45 RTC Seconds interrupt
  206. pit0_isr, // 46 PIT Channel 0
  207. pit1_isr, // 47 PIT Channel 1
  208. pit2_isr, // 48 PIT Channel 2
  209. pit3_isr, // 49 PIT Channel 3
  210. pdb_isr, // 50 PDB Programmable Delay Block
  211. usb_isr, // 51 USB OTG
  212. usb_charge_isr, // 52 USB Charger Detect
  213. tsi0_isr, // 53 TSI0
  214. mcg_isr, // 54 MCG
  215. lptmr_isr, // 55 Low Power Timer
  216. porta_isr, // 56 Pin detect (Port A)
  217. portb_isr, // 57 Pin detect (Port B)
  218. portc_isr, // 58 Pin detect (Port C)
  219. portd_isr, // 59 Pin detect (Port D)
  220. porte_isr, // 60 Pin detect (Port E)
  221. software_isr, // 61 Software interrupt
  222. #elif defined(__MK20DX256__)
  223. dma_ch0_isr, // 16 DMA channel 0 transfer complete
  224. dma_ch1_isr, // 17 DMA channel 1 transfer complete
  225. dma_ch2_isr, // 18 DMA channel 2 transfer complete
  226. dma_ch3_isr, // 19 DMA channel 3 transfer complete
  227. dma_ch4_isr, // 20 DMA channel 4 transfer complete
  228. dma_ch5_isr, // 21 DMA channel 5 transfer complete
  229. dma_ch6_isr, // 22 DMA channel 6 transfer complete
  230. dma_ch7_isr, // 23 DMA channel 7 transfer complete
  231. dma_ch8_isr, // 24 DMA channel 8 transfer complete
  232. dma_ch9_isr, // 25 DMA channel 9 transfer complete
  233. dma_ch10_isr, // 26 DMA channel 10 transfer complete
  234. dma_ch11_isr, // 27 DMA channel 10 transfer complete
  235. dma_ch12_isr, // 28 DMA channel 10 transfer complete
  236. dma_ch13_isr, // 29 DMA channel 10 transfer complete
  237. dma_ch14_isr, // 30 DMA channel 10 transfer complete
  238. dma_ch15_isr, // 31 DMA channel 10 transfer complete
  239. dma_error_isr, // 32 DMA error interrupt channel
  240. unused_isr, // 33 --
  241. flash_cmd_isr, // 34 Flash Memory Command complete
  242. flash_error_isr, // 35 Flash Read collision
  243. low_voltage_isr, // 36 Low-voltage detect/warning
  244. wakeup_isr, // 37 Low Leakage Wakeup
  245. watchdog_isr, // 38 Both EWM and WDOG interrupt
  246. unused_isr, // 39 --
  247. i2c0_isr, // 40 I2C0
  248. i2c1_isr, // 41 I2C1
  249. spi0_isr, // 42 SPI0
  250. spi1_isr, // 43 SPI1
  251. unused_isr, // 44 --
  252. can0_message_isr, // 45 CAN OR'ed Message buffer (0-15)
  253. can0_bus_off_isr, // 46 CAN Bus Off
  254. can0_error_isr, // 47 CAN Error
  255. can0_tx_warn_isr, // 48 CAN Transmit Warning
  256. can0_rx_warn_isr, // 49 CAN Receive Warning
  257. can0_wakeup_isr, // 50 CAN Wake Up
  258. i2s0_tx_isr, // 51 I2S0 Transmit
  259. i2s0_rx_isr, // 52 I2S0 Receive
  260. unused_isr, // 53 --
  261. unused_isr, // 54 --
  262. unused_isr, // 55 --
  263. unused_isr, // 56 --
  264. unused_isr, // 57 --
  265. unused_isr, // 58 --
  266. unused_isr, // 59 --
  267. uart0_lon_isr, // 60 UART0 CEA709.1-B (LON) status
  268. uart0_status_isr, // 61 UART0 status
  269. uart0_error_isr, // 62 UART0 error
  270. uart1_status_isr, // 63 UART1 status
  271. uart1_error_isr, // 64 UART1 error
  272. uart2_status_isr, // 65 UART2 status
  273. uart2_error_isr, // 66 UART2 error
  274. unused_isr, // 67 --
  275. unused_isr, // 68 --
  276. unused_isr, // 69 --
  277. unused_isr, // 70 --
  278. unused_isr, // 71 --
  279. unused_isr, // 72 --
  280. adc0_isr, // 73 ADC0
  281. adc1_isr, // 74 ADC1
  282. cmp0_isr, // 75 CMP0
  283. cmp1_isr, // 76 CMP1
  284. cmp2_isr, // 77 CMP2
  285. ftm0_isr, // 78 FTM0
  286. ftm1_isr, // 79 FTM1
  287. ftm2_isr, // 80 FTM2
  288. cmt_isr, // 81 CMT
  289. rtc_alarm_isr, // 82 RTC Alarm interrupt
  290. rtc_seconds_isr, // 83 RTC Seconds interrupt
  291. pit0_isr, // 84 PIT Channel 0
  292. pit1_isr, // 85 PIT Channel 1
  293. pit2_isr, // 86 PIT Channel 2
  294. pit3_isr, // 87 PIT Channel 3
  295. pdb_isr, // 88 PDB Programmable Delay Block
  296. usb_isr, // 89 USB OTG
  297. usb_charge_isr, // 90 USB Charger Detect
  298. unused_isr, // 91 --
  299. unused_isr, // 92 --
  300. unused_isr, // 93 --
  301. unused_isr, // 94 --
  302. unused_isr, // 95 --
  303. unused_isr, // 96 --
  304. dac0_isr, // 97 DAC0
  305. unused_isr, // 98 --
  306. tsi0_isr, // 99 TSI0
  307. mcg_isr, // 100 MCG
  308. lptmr_isr, // 101 Low Power Timer
  309. unused_isr, // 102 --
  310. porta_isr, // 103 Pin detect (Port A)
  311. portb_isr, // 104 Pin detect (Port B)
  312. portc_isr, // 105 Pin detect (Port C)
  313. portd_isr, // 106 Pin detect (Port D)
  314. porte_isr, // 107 Pin detect (Port E)
  315. unused_isr, // 108 --
  316. unused_isr, // 109 --
  317. software_isr, // 110 Software interrupt
  318. #endif
  319. };
  320. //void usb_isr(void)
  321. //{
  322. //}
  323. __attribute__ ((section(".flashconfig"), used))
  324. const uint8_t flashconfigbytes[16] = {
  325. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  326. 0xFF, 0xFF, 0xFF, 0xFF, 0xFE, 0xFF, 0xFF, 0xFF
  327. };
  328. // Automatically initialize the RTC. When the build defines the compile
  329. // time, and the user has added a crystal, the RTC will automatically
  330. // begin at the time of the first upload.
  331. #ifndef TIME_T
  332. #define TIME_T 1349049600 // default 1 Oct 2012 (never used, Arduino sets this)
  333. #endif
  334. extern void rtc_set(unsigned long t);
  335. static void startup_default_early_hook(void) { WDOG_STCTRLH = WDOG_STCTRLH_ALLOWUPDATE; }
  336. static void startup_default_late_hook(void) {}
  337. void startup_early_hook(void) __attribute__ ((weak, alias("startup_default_early_hook")));
  338. void startup_late_hook(void) __attribute__ ((weak, alias("startup_default_late_hook")));
  339. __attribute__ ((section(".startup")))
  340. void ResetHandler(void)
  341. {
  342. uint32_t *src = &_etext;
  343. uint32_t *dest = &_sdata;
  344. unsigned int i;
  345. WDOG_UNLOCK = WDOG_UNLOCK_SEQ1;
  346. WDOG_UNLOCK = WDOG_UNLOCK_SEQ2;
  347. asm volatile ("nop");
  348. asm volatile ("nop");
  349. // programs using the watchdog timer or needing to initialize hardware as
  350. // early as possible can implement startup_early_hook()
  351. startup_early_hook();
  352. // enable clocks to always-used peripherals
  353. #if defined(__MK20DX128__)
  354. SIM_SCGC5 = 0x00043F82; // clocks active to all GPIO
  355. SIM_SCGC6 = SIM_SCGC6_RTC | SIM_SCGC6_FTM0 | SIM_SCGC6_FTM1 | SIM_SCGC6_ADC0 | SIM_SCGC6_FTFL;
  356. #elif defined(__MK20DX256__)
  357. SIM_SCGC3 = SIM_SCGC3_ADC1 | SIM_SCGC3_FTM2;
  358. SIM_SCGC5 = 0x00043F82; // clocks active to all GPIO
  359. SIM_SCGC6 = SIM_SCGC6_RTC | SIM_SCGC6_FTM0 | SIM_SCGC6_FTM1 | SIM_SCGC6_ADC0 | SIM_SCGC6_FTFL;
  360. #endif
  361. // if the RTC oscillator isn't enabled, get it started early
  362. if (!(RTC_CR & RTC_CR_OSCE)) {
  363. RTC_SR = 0;
  364. RTC_CR = RTC_CR_SC16P | RTC_CR_SC4P | RTC_CR_OSCE;
  365. }
  366. // release I/O pins hold, if we woke up from VLLS mode
  367. if (PMC_REGSC & PMC_REGSC_ACKISO) PMC_REGSC |= PMC_REGSC_ACKISO;
  368. // TODO: do this while the PLL is waiting to lock....
  369. while (dest < &_edata) *dest++ = *src++;
  370. dest = &_sbss;
  371. while (dest < &_ebss) *dest++ = 0;
  372. SCB_VTOR = 0; // use vector table in flash
  373. // default all interrupts to medium priority level
  374. for (i=0; i < NVIC_NUM_INTERRUPTS; i++) NVIC_SET_PRIORITY(i, 128);
  375. // start in FEI mode
  376. // enable capacitors for crystal
  377. OSC0_CR = OSC_SC8P | OSC_SC2P;
  378. // enable osc, 8-32 MHz range, low power mode
  379. MCG_C2 = MCG_C2_RANGE0(2) | MCG_C2_EREFS;
  380. // switch to crystal as clock source, FLL input = 16 MHz / 512
  381. MCG_C1 = MCG_C1_CLKS(2) | MCG_C1_FRDIV(4);
  382. // wait for crystal oscillator to begin
  383. while ((MCG_S & MCG_S_OSCINIT0) == 0) ;
  384. // wait for FLL to use oscillator
  385. while ((MCG_S & MCG_S_IREFST) != 0) ;
  386. // wait for MCGOUT to use oscillator
  387. while ((MCG_S & MCG_S_CLKST_MASK) != MCG_S_CLKST(2)) ;
  388. // now we're in FBE mode
  389. // config PLL input for 16 MHz Crystal / 4 = 4 MHz
  390. MCG_C5 = MCG_C5_PRDIV0(3);
  391. #if F_CPU == 168000000
  392. MCG_C6 = MCG_C6_PLLS | MCG_C6_VDIV0(18); // config PLL for 168 MHz output
  393. #elif F_CPU == 144000000
  394. MCG_C6 = MCG_C6_PLLS | MCG_C6_VDIV0(12); // config PLL for 144 MHz output
  395. #elif F_CPU == 120000000
  396. MCG_C6 = MCG_C6_PLLS | MCG_C6_VDIV0(6); // config PLL for 120 MHz output
  397. #else
  398. MCG_C6 = MCG_C6_PLLS | MCG_C6_VDIV0(0); // config PLL for 96 MHz output
  399. #endif
  400. // wait for PLL to start using xtal as its input
  401. while (!(MCG_S & MCG_S_PLLST)) ;
  402. // wait for PLL to lock
  403. while (!(MCG_S & MCG_S_LOCK0)) ;
  404. // now we're in PBE mode
  405. #if F_CPU == 168000000
  406. // config divisors: 168 MHz core, 42 MHz bus, 28 MHz flash
  407. SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIV1(0) | SIM_CLKDIV1_OUTDIV2(3) | SIM_CLKDIV1_OUTDIV4(5);
  408. #elif F_CPU == 144000000
  409. // config divisors: 144 MHz core, 48 MHz bus, 24 MHz flash
  410. SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIV1(0) | SIM_CLKDIV1_OUTDIV2(2) | SIM_CLKDIV1_OUTDIV4(5);
  411. #elif F_CPU == 120000000
  412. // config divisors: 120 MHz core, 40 MHz bus, 30 MHz flash
  413. SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIV1(0) | SIM_CLKDIV1_OUTDIV2(2) | SIM_CLKDIV1_OUTDIV4(3);
  414. #elif F_CPU == 96000000
  415. // config divisors: 96 MHz core, 48 MHz bus, 24 MHz flash
  416. SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIV1(0) | SIM_CLKDIV1_OUTDIV2(1) | SIM_CLKDIV1_OUTDIV4(3);
  417. #elif F_CPU == 48000000
  418. // config divisors: 48 MHz core, 48 MHz bus, 24 MHz flash
  419. SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIV1(1) | SIM_CLKDIV1_OUTDIV2(1) | SIM_CLKDIV1_OUTDIV4(3);
  420. #elif F_CPU == 24000000
  421. // config divisors: 24 MHz core, 24 MHz bus, 24 MHz flash
  422. SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIV1(3) | SIM_CLKDIV1_OUTDIV2(3) | SIM_CLKDIV1_OUTDIV4(3);
  423. #else
  424. #error "Error, F_CPU must be 168000000, 144000000, 120000000, 96000000, 48000000, or 24000000"
  425. #endif
  426. // switch to PLL as clock source, FLL input = 16 MHz / 512
  427. MCG_C1 = MCG_C1_CLKS(0) | MCG_C1_FRDIV(4);
  428. // wait for PLL clock to be used
  429. while ((MCG_S & MCG_S_CLKST_MASK) != MCG_S_CLKST(3)) ;
  430. // now we're in PEE mode
  431. // configure USB for 48 MHz clock
  432. #if F_CPU == 168000000
  433. SIM_CLKDIV2 = SIM_CLKDIV2_USBDIV(6) | SIM_CLKDIV2_USBFRAC; // USB = 168 MHz PLL / 3.5
  434. #elif F_CPU == 144000000
  435. SIM_CLKDIV2 = SIM_CLKDIV2_USBDIV(2); // USB = 144 MHz PLL / 3
  436. #elif F_CPU == 120000000
  437. SIM_CLKDIV2 = SIM_CLKDIV2_USBDIV(4) | SIM_CLKDIV2_USBFRAC; // USB = 120 MHz PLL / 2.5
  438. #else
  439. SIM_CLKDIV2 = SIM_CLKDIV2_USBDIV(1); // USB = 96 MHz PLL / 2
  440. #endif
  441. // USB uses PLL clock, trace is CPU clock, CLKOUT=OSCERCLK0
  442. SIM_SOPT2 = SIM_SOPT2_USBSRC | SIM_SOPT2_PLLFLLSEL | SIM_SOPT2_TRACECLKSEL | SIM_SOPT2_CLKOUTSEL(6);
  443. // initialize the SysTick counter
  444. SYST_RVR = (F_CPU / 1000) - 1;
  445. SYST_CSR = SYST_CSR_CLKSOURCE | SYST_CSR_TICKINT | SYST_CSR_ENABLE;
  446. //init_pins();
  447. __enable_irq();
  448. _init_Teensyduino_internal_();
  449. if (RTC_SR & RTC_SR_TIF) rtc_set(TIME_T);
  450. __libc_init_array();
  451. /*
  452. for (ptr = &__init_array_start; ptr < &__init_array_end; ptr++) {
  453. (*ptr)();
  454. }
  455. */
  456. startup_late_hook();
  457. main();
  458. while (1) ;
  459. }
  460. // TODO: is this needed for c++ and where does it come from?
  461. /*
  462. void _init(void)
  463. {
  464. }
  465. */
  466. char *__brkval = (char *)&_ebss;
  467. void * _sbrk(int incr)
  468. {
  469. //static char *heap_end = (char *)&_ebss;
  470. //char *prev = heap_end;
  471. //heap_end += incr;
  472. char *prev = __brkval;
  473. __brkval += incr;
  474. return prev;
  475. }
  476. __attribute__((weak))
  477. int _read(int file, char *ptr, int len)
  478. {
  479. return 0;
  480. }
  481. /* moved to Print.cpp, to support Print::printf()
  482. __attribute__((weak))
  483. int _write(int file, char *ptr, int len)
  484. {
  485. return 0;
  486. }
  487. */
  488. __attribute__((weak))
  489. int _close(int fd)
  490. {
  491. return -1;
  492. }
  493. #include <sys/stat.h>
  494. __attribute__((weak))
  495. int _fstat(int fd, struct stat *st)
  496. {
  497. st->st_mode = S_IFCHR;
  498. return 0;
  499. }
  500. __attribute__((weak))
  501. int _isatty(int fd)
  502. {
  503. return 1;
  504. }
  505. __attribute__((weak))
  506. int _lseek(int fd, long long offset, int whence)
  507. {
  508. return -1;
  509. }
  510. __attribute__((weak))
  511. void _exit(int status)
  512. {
  513. while (1);
  514. }
  515. __attribute__((weak))
  516. void __cxa_pure_virtual()
  517. {
  518. while (1);
  519. }
  520. __attribute__((weak))
  521. int __cxa_guard_acquire (char *g)
  522. {
  523. return !(*g);
  524. }
  525. __attribute__((weak))
  526. void __cxa_guard_release(char *g)
  527. {
  528. *g = 1;
  529. }
  530. int nvic_execution_priority(void)
  531. {
  532. int priority=256;
  533. uint32_t primask, faultmask, basepri, ipsr;
  534. // full algorithm in ARM DDI0403D, page B1-639
  535. // this isn't quite complete, but hopefully good enough
  536. asm volatile("mrs %0, faultmask\n" : "=r" (faultmask)::);
  537. if (faultmask) return -1;
  538. asm volatile("mrs %0, primask\n" : "=r" (primask)::);
  539. if (primask) return 0;
  540. asm volatile("mrs %0, ipsr\n" : "=r" (ipsr)::);
  541. if (ipsr) {
  542. if (ipsr < 16) priority = 0; // could be non-zero
  543. else priority = NVIC_GET_PRIORITY(ipsr - 16);
  544. }
  545. asm volatile("mrs %0, basepri\n" : "=r" (basepri)::);
  546. if (basepri > 0 && basepri < priority) priority = basepri;
  547. return priority;
  548. }