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

854 lines
32KB

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