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

1339 lines
51KB

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