Teensy 4.1 core updated for C++20
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

790 líneas
30KB

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