Kiibohd Controller
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

mk20dx.c 25KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  1. /* Teensyduino Core Library
  2. * http://www.pjrc.com/teensy/
  3. * Copyright (c) 2013 PJRC.COM, LLC.
  4. * Modifications by Jacob Alexander 2014
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining
  7. * a copy of this software and associated documentation files (the
  8. * "Software"), to deal in the Software without restriction, including
  9. * without limitation the rights to use, copy, modify, merge, publish,
  10. * distribute, sublicense, and/or sell copies of the Software, and to
  11. * permit persons to whom the Software is furnished to do so, subject to
  12. * the following conditions:
  13. *
  14. * 1. The above copyright notice and this permission notice shall be
  15. * included in all copies or substantial portions of the Software.
  16. *
  17. * 2. If the Software is incorporated into a build system that allows
  18. * selection among a list of target devices, then similar target
  19. * devices manufactured by PJRC.COM must be included in the list of
  20. * target devices and selectable in the same manner.
  21. *
  22. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  26. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  27. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  28. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  29. * SOFTWARE.
  30. */
  31. // Local Includes
  32. #include "mk20dx.h"
  33. // ----- Variables -----
  34. extern unsigned long _stext;
  35. extern unsigned long _etext;
  36. extern unsigned long _sdata;
  37. extern unsigned long _edata;
  38. extern unsigned long _sbss;
  39. extern unsigned long _ebss;
  40. extern unsigned long _estack;
  41. // ----- Function Declarations -----
  42. extern int main ();
  43. void ResetHandler();
  44. // ----- Interrupts -----
  45. // NVIC - Default ISR
  46. void fault_isr()
  47. {
  48. while ( 1 )
  49. {
  50. // keep polling some communication while in fault
  51. // mode, so we don't completely die.
  52. if ( SIM_SCGC4 & SIM_SCGC4_USBOTG ) usb_isr();
  53. if ( SIM_SCGC4 & SIM_SCGC4_UART0 ) uart0_status_isr();
  54. if ( SIM_SCGC4 & SIM_SCGC4_UART1 ) uart1_status_isr();
  55. if ( SIM_SCGC4 & SIM_SCGC4_UART2 ) uart2_status_isr();
  56. }
  57. }
  58. void unused_isr()
  59. {
  60. fault_isr();
  61. }
  62. // NVIC - SysTick ISR
  63. extern volatile uint32_t systick_millis_count;
  64. void systick_default_isr()
  65. {
  66. systick_millis_count++;
  67. }
  68. // NVIC - Default ISR/Vector Linking
  69. void nmi_isr() __attribute__ ((weak, alias("unused_isr")));
  70. void hard_fault_isr() __attribute__ ((weak, alias("unused_isr")));
  71. void memmanage_fault_isr() __attribute__ ((weak, alias("unused_isr")));
  72. void bus_fault_isr() __attribute__ ((weak, alias("unused_isr")));
  73. void usage_fault_isr() __attribute__ ((weak, alias("unused_isr")));
  74. void svcall_isr() __attribute__ ((weak, alias("unused_isr")));
  75. void debugmonitor_isr() __attribute__ ((weak, alias("unused_isr")));
  76. void pendablesrvreq_isr() __attribute__ ((weak, alias("unused_isr")));
  77. void systick_isr() __attribute__ ((weak, alias("systick_default_isr")));
  78. void dma_ch0_isr() __attribute__ ((weak, alias("unused_isr")));
  79. void dma_ch1_isr() __attribute__ ((weak, alias("unused_isr")));
  80. void dma_ch2_isr() __attribute__ ((weak, alias("unused_isr")));
  81. void dma_ch3_isr() __attribute__ ((weak, alias("unused_isr")));
  82. void dma_ch4_isr() __attribute__ ((weak, alias("unused_isr")));
  83. void dma_ch5_isr() __attribute__ ((weak, alias("unused_isr")));
  84. void dma_ch6_isr() __attribute__ ((weak, alias("unused_isr")));
  85. void dma_ch7_isr() __attribute__ ((weak, alias("unused_isr")));
  86. void dma_ch8_isr() __attribute__ ((weak, alias("unused_isr")));
  87. void dma_ch9_isr() __attribute__ ((weak, alias("unused_isr")));
  88. void dma_ch10_isr() __attribute__ ((weak, alias("unused_isr")));
  89. void dma_ch11_isr() __attribute__ ((weak, alias("unused_isr")));
  90. void dma_ch12_isr() __attribute__ ((weak, alias("unused_isr")));
  91. void dma_ch13_isr() __attribute__ ((weak, alias("unused_isr")));
  92. void dma_ch14_isr() __attribute__ ((weak, alias("unused_isr")));
  93. void dma_ch15_isr() __attribute__ ((weak, alias("unused_isr")));
  94. void dma_error_isr() __attribute__ ((weak, alias("unused_isr")));
  95. void mcm_isr() __attribute__ ((weak, alias("unused_isr")));
  96. void flash_cmd_isr() __attribute__ ((weak, alias("unused_isr")));
  97. void flash_error_isr() __attribute__ ((weak, alias("unused_isr")));
  98. void low_voltage_isr() __attribute__ ((weak, alias("unused_isr")));
  99. void wakeup_isr() __attribute__ ((weak, alias("unused_isr")));
  100. void watchdog_isr() __attribute__ ((weak, alias("unused_isr")));
  101. void i2c0_isr() __attribute__ ((weak, alias("unused_isr")));
  102. void i2c1_isr() __attribute__ ((weak, alias("unused_isr")));
  103. void i2c2_isr() __attribute__ ((weak, alias("unused_isr")));
  104. void spi0_isr() __attribute__ ((weak, alias("unused_isr")));
  105. void spi1_isr() __attribute__ ((weak, alias("unused_isr")));
  106. void spi2_isr() __attribute__ ((weak, alias("unused_isr")));
  107. void sdhc_isr() __attribute__ ((weak, alias("unused_isr")));
  108. void can0_message_isr() __attribute__ ((weak, alias("unused_isr")));
  109. void can0_bus_off_isr() __attribute__ ((weak, alias("unused_isr")));
  110. void can0_error_isr() __attribute__ ((weak, alias("unused_isr")));
  111. void can0_tx_warn_isr() __attribute__ ((weak, alias("unused_isr")));
  112. void can0_rx_warn_isr() __attribute__ ((weak, alias("unused_isr")));
  113. void can0_wakeup_isr() __attribute__ ((weak, alias("unused_isr")));
  114. void i2s0_tx_isr() __attribute__ ((weak, alias("unused_isr")));
  115. void i2s0_rx_isr() __attribute__ ((weak, alias("unused_isr")));
  116. void uart0_lon_isr() __attribute__ ((weak, alias("unused_isr")));
  117. void uart0_status_isr() __attribute__ ((weak, alias("unused_isr")));
  118. void uart0_error_isr() __attribute__ ((weak, alias("unused_isr")));
  119. void uart1_status_isr() __attribute__ ((weak, alias("unused_isr")));
  120. void uart1_error_isr() __attribute__ ((weak, alias("unused_isr")));
  121. void uart2_status_isr() __attribute__ ((weak, alias("unused_isr")));
  122. void uart2_error_isr() __attribute__ ((weak, alias("unused_isr")));
  123. void uart3_status_isr() __attribute__ ((weak, alias("unused_isr")));
  124. void uart3_error_isr() __attribute__ ((weak, alias("unused_isr")));
  125. void uart4_status_isr() __attribute__ ((weak, alias("unused_isr")));
  126. void uart4_error_isr() __attribute__ ((weak, alias("unused_isr")));
  127. void uart5_status_isr() __attribute__ ((weak, alias("unused_isr")));
  128. void uart5_error_isr() __attribute__ ((weak, alias("unused_isr")));
  129. void adc0_isr() __attribute__ ((weak, alias("unused_isr")));
  130. void adc1_isr() __attribute__ ((weak, alias("unused_isr")));
  131. void cmp0_isr() __attribute__ ((weak, alias("unused_isr")));
  132. void cmp1_isr() __attribute__ ((weak, alias("unused_isr")));
  133. void cmp2_isr() __attribute__ ((weak, alias("unused_isr")));
  134. void ftm0_isr() __attribute__ ((weak, alias("unused_isr")));
  135. void ftm1_isr() __attribute__ ((weak, alias("unused_isr")));
  136. void ftm2_isr() __attribute__ ((weak, alias("unused_isr")));
  137. void ftm3_isr() __attribute__ ((weak, alias("unused_isr")));
  138. void cmt_isr() __attribute__ ((weak, alias("unused_isr")));
  139. void rtc_alarm_isr() __attribute__ ((weak, alias("unused_isr")));
  140. void rtc_seconds_isr() __attribute__ ((weak, alias("unused_isr")));
  141. void pit0_isr() __attribute__ ((weak, alias("unused_isr")));
  142. void pit1_isr() __attribute__ ((weak, alias("unused_isr")));
  143. void pit2_isr() __attribute__ ((weak, alias("unused_isr")));
  144. void pit3_isr() __attribute__ ((weak, alias("unused_isr")));
  145. void pdb_isr() __attribute__ ((weak, alias("unused_isr")));
  146. void usb_isr() __attribute__ ((weak, alias("unused_isr")));
  147. void usb_charge_isr() __attribute__ ((weak, alias("unused_isr")));
  148. void dac0_isr() __attribute__ ((weak, alias("unused_isr")));
  149. void dac1_isr() __attribute__ ((weak, alias("unused_isr")));
  150. void tsi0_isr() __attribute__ ((weak, alias("unused_isr")));
  151. void mcg_isr() __attribute__ ((weak, alias("unused_isr")));
  152. void lptmr_isr() __attribute__ ((weak, alias("unused_isr")));
  153. void porta_isr() __attribute__ ((weak, alias("unused_isr")));
  154. void portb_isr() __attribute__ ((weak, alias("unused_isr")));
  155. void portc_isr() __attribute__ ((weak, alias("unused_isr")));
  156. void portd_isr() __attribute__ ((weak, alias("unused_isr")));
  157. void porte_isr() __attribute__ ((weak, alias("unused_isr")));
  158. void software_isr() __attribute__ ((weak, alias("unused_isr")));
  159. // NVIC - Interrupt Vector Table
  160. __attribute__ ((section(".vectors"), used))
  161. void (* const gVectors[])() =
  162. {
  163. (void (*)(void))((unsigned long)&_estack), // 0 ARM: Initial Stack Pointer
  164. ResetHandler, // 1 ARM: Initial Program Counter
  165. nmi_isr, // 2 ARM: Non-maskable Interrupt (NMI)
  166. hard_fault_isr, // 3 ARM: Hard Fault
  167. memmanage_fault_isr, // 4 ARM: MemManage Fault
  168. bus_fault_isr, // 5 ARM: Bus Fault
  169. usage_fault_isr, // 6 ARM: Usage Fault
  170. fault_isr, // 7 --
  171. fault_isr, // 8 --
  172. fault_isr, // 9 --
  173. fault_isr, // 10 --
  174. svcall_isr, // 11 ARM: Supervisor call (SVCall)
  175. debugmonitor_isr, // 12 ARM: Debug Monitor
  176. fault_isr, // 13 --
  177. pendablesrvreq_isr, // 14 ARM: Pendable req serv(PendableSrvReq)
  178. systick_isr, // 15 ARM: System tick timer (SysTick)
  179. #if defined(_mk20dx128_) || defined(_mk20dx128vlf5_)
  180. dma_ch0_isr, // 16 DMA channel 0 transfer complete
  181. dma_ch1_isr, // 17 DMA channel 1 transfer complete
  182. dma_ch2_isr, // 18 DMA channel 2 transfer complete
  183. dma_ch3_isr, // 19 DMA channel 3 transfer complete
  184. dma_error_isr, // 20 DMA error interrupt channel
  185. unused_isr, // 21 DMA --
  186. flash_cmd_isr, // 22 Flash Memory Command complete
  187. flash_error_isr, // 23 Flash Read collision
  188. low_voltage_isr, // 24 Low-voltage detect/warning
  189. wakeup_isr, // 25 Low Leakage Wakeup
  190. watchdog_isr, // 26 Both EWM and WDOG interrupt
  191. i2c0_isr, // 27 I2C0
  192. spi0_isr, // 28 SPI0
  193. i2s0_tx_isr, // 29 I2S0 Transmit
  194. i2s0_rx_isr, // 30 I2S0 Receive
  195. uart0_lon_isr, // 31 UART0 CEA709.1-B (LON) status
  196. uart0_status_isr, // 32 UART0 status
  197. uart0_error_isr, // 33 UART0 error
  198. uart1_status_isr, // 34 UART1 status
  199. uart1_error_isr, // 35 UART1 error
  200. uart2_status_isr, // 36 UART2 status
  201. uart2_error_isr, // 37 UART2 error
  202. adc0_isr, // 38 ADC0
  203. cmp0_isr, // 39 CMP0
  204. cmp1_isr, // 40 CMP1
  205. ftm0_isr, // 41 FTM0
  206. ftm1_isr, // 42 FTM1
  207. cmt_isr, // 43 CMT
  208. rtc_alarm_isr, // 44 RTC Alarm interrupt
  209. rtc_seconds_isr, // 45 RTC Seconds interrupt
  210. pit0_isr, // 46 PIT Channel 0
  211. pit1_isr, // 47 PIT Channel 1
  212. pit2_isr, // 48 PIT Channel 2
  213. pit3_isr, // 49 PIT Channel 3
  214. pdb_isr, // 50 PDB Programmable Delay Block
  215. usb_isr, // 51 USB OTG
  216. usb_charge_isr, // 52 USB Charger Detect
  217. tsi0_isr, // 53 TSI0
  218. mcg_isr, // 54 MCG
  219. lptmr_isr, // 55 Low Power Timer
  220. porta_isr, // 56 Pin detect (Port A)
  221. portb_isr, // 57 Pin detect (Port B)
  222. portc_isr, // 58 Pin detect (Port C)
  223. portd_isr, // 59 Pin detect (Port D)
  224. porte_isr, // 60 Pin detect (Port E)
  225. software_isr, // 61 Software interrupt
  226. #elif defined(_mk20dx256_)
  227. dma_ch0_isr, // 16 DMA channel 0 transfer complete
  228. dma_ch1_isr, // 17 DMA channel 1 transfer complete
  229. dma_ch2_isr, // 18 DMA channel 2 transfer complete
  230. dma_ch3_isr, // 19 DMA channel 3 transfer complete
  231. dma_ch4_isr, // 20 DMA channel 4 transfer complete
  232. dma_ch5_isr, // 21 DMA channel 5 transfer complete
  233. dma_ch6_isr, // 22 DMA channel 6 transfer complete
  234. dma_ch7_isr, // 23 DMA channel 7 transfer complete
  235. dma_ch8_isr, // 24 DMA channel 8 transfer complete
  236. dma_ch9_isr, // 25 DMA channel 9 transfer complete
  237. dma_ch10_isr, // 26 DMA channel 10 transfer complete
  238. dma_ch11_isr, // 27 DMA channel 10 transfer complete
  239. dma_ch12_isr, // 28 DMA channel 10 transfer complete
  240. dma_ch13_isr, // 29 DMA channel 10 transfer complete
  241. dma_ch14_isr, // 30 DMA channel 10 transfer complete
  242. dma_ch15_isr, // 31 DMA channel 10 transfer complete
  243. dma_error_isr, // 32 DMA error interrupt channel
  244. unused_isr, // 33 --
  245. flash_cmd_isr, // 34 Flash Memory Command complete
  246. flash_error_isr, // 35 Flash Read collision
  247. low_voltage_isr, // 36 Low-voltage detect/warning
  248. wakeup_isr, // 37 Low Leakage Wakeup
  249. watchdog_isr, // 38 Both EWM and WDOG interrupt
  250. unused_isr, // 39 --
  251. i2c0_isr, // 40 I2C0
  252. i2c1_isr, // 41 I2C1
  253. spi0_isr, // 42 SPI0
  254. spi1_isr, // 43 SPI1
  255. unused_isr, // 44 --
  256. can0_message_isr, // 45 CAN OR'ed Message buffer (0-15)
  257. can0_bus_off_isr, // 46 CAN Bus Off
  258. can0_error_isr, // 47 CAN Error
  259. can0_tx_warn_isr, // 48 CAN Transmit Warning
  260. can0_rx_warn_isr, // 49 CAN Receive Warning
  261. can0_wakeup_isr, // 50 CAN Wake Up
  262. i2s0_tx_isr, // 51 I2S0 Transmit
  263. i2s0_rx_isr, // 52 I2S0 Receive
  264. unused_isr, // 53 --
  265. unused_isr, // 54 --
  266. unused_isr, // 55 --
  267. unused_isr, // 56 --
  268. unused_isr, // 57 --
  269. unused_isr, // 58 --
  270. unused_isr, // 59 --
  271. uart0_lon_isr, // 60 UART0 CEA709.1-B (LON) status
  272. uart0_status_isr, // 61 UART0 status
  273. uart0_error_isr, // 62 UART0 error
  274. uart1_status_isr, // 63 UART1 status
  275. uart1_error_isr, // 64 UART1 error
  276. uart2_status_isr, // 65 UART2 status
  277. uart2_error_isr, // 66 UART2 error
  278. unused_isr, // 67 --
  279. unused_isr, // 68 --
  280. unused_isr, // 69 --
  281. unused_isr, // 70 --
  282. unused_isr, // 71 --
  283. unused_isr, // 72 --
  284. adc0_isr, // 73 ADC0
  285. adc1_isr, // 74 ADC1
  286. cmp0_isr, // 75 CMP0
  287. cmp1_isr, // 76 CMP1
  288. cmp2_isr, // 77 CMP2
  289. ftm0_isr, // 78 FTM0
  290. ftm1_isr, // 79 FTM1
  291. ftm2_isr, // 80 FTM2
  292. cmt_isr, // 81 CMT
  293. rtc_alarm_isr, // 82 RTC Alarm interrupt
  294. rtc_seconds_isr, // 83 RTC Seconds interrupt
  295. pit0_isr, // 84 PIT Channel 0
  296. pit1_isr, // 85 PIT Channel 1
  297. pit2_isr, // 86 PIT Channel 2
  298. pit3_isr, // 87 PIT Channel 3
  299. pdb_isr, // 88 PDB Programmable Delay Block
  300. usb_isr, // 89 USB OTG
  301. usb_charge_isr, // 90 USB Charger Detect
  302. unused_isr, // 91 --
  303. unused_isr, // 92 --
  304. unused_isr, // 93 --
  305. unused_isr, // 94 --
  306. unused_isr, // 95 --
  307. unused_isr, // 96 --
  308. dac0_isr, // 97 DAC0
  309. unused_isr, // 98 --
  310. tsi0_isr, // 99 TSI0
  311. mcg_isr, // 100 MCG
  312. lptmr_isr, // 101 Low Power Timer
  313. unused_isr, // 102 --
  314. porta_isr, // 103 Pin detect (Port A)
  315. portb_isr, // 104 Pin detect (Port B)
  316. portc_isr, // 105 Pin detect (Port C)
  317. portd_isr, // 106 Pin detect (Port D)
  318. porte_isr, // 107 Pin detect (Port E)
  319. unused_isr, // 108 --
  320. unused_isr, // 109 --
  321. software_isr, // 110 Software interrupt
  322. #endif
  323. };
  324. // ----- Flash Configuration -----
  325. // Only necessary for Teensy 3s, MCHCK uses the Bootloader to handle this
  326. #if defined(_mk20dx128_) || defined(_mk20dx256_)
  327. __attribute__ ((section(".flashconfig"), used))
  328. const uint8_t flashconfigbytes[16] = {
  329. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  330. 0xFF, 0xFF, 0xFF, 0xFF, 0xFE, 0xFF, 0xFF, 0xFF
  331. };
  332. #endif
  333. // ----- Chip Entry Point -----
  334. __attribute__ ((section(".startup")))
  335. void ResetHandler()
  336. {
  337. uint32_t *src = &_etext;
  338. uint32_t *dest = &_sdata;
  339. /* Disable Watchdog */
  340. WDOG_UNLOCK = WDOG_UNLOCK_SEQ1;
  341. WDOG_UNLOCK = WDOG_UNLOCK_SEQ2;
  342. WDOG_STCTRLH = WDOG_STCTRLH_ALLOWUPDATE;
  343. // enable clocks to always-used peripherals
  344. #if defined(_mk20dx128_) || defined(_mk20dx128vlf5_)
  345. SIM_SCGC5 = 0x00043F82; // clocks active to all GPIO
  346. SIM_SCGC6 = SIM_SCGC6_RTC | SIM_SCGC6_FTM0 | SIM_SCGC6_FTM1 | SIM_SCGC6_ADC0 | SIM_SCGC6_FTFL;
  347. #elif defined(_mk20dx256_)
  348. SIM_SCGC3 = SIM_SCGC3_ADC1 | SIM_SCGC3_FTM2;
  349. SIM_SCGC5 = 0x00043F82; // clocks active to all GPIO
  350. SIM_SCGC6 = SIM_SCGC6_RTC | SIM_SCGC6_FTM0 | SIM_SCGC6_FTM1 | SIM_SCGC6_ADC0 | SIM_SCGC6_FTFL;
  351. #endif
  352. #if defined(_mk20dx128_) || defined(_mk20dx256_) // Teensy 3s
  353. // if the RTC oscillator isn't enabled, get it started early
  354. if ( !(RTC_CR & RTC_CR_OSCE) )
  355. {
  356. RTC_SR = 0;
  357. RTC_CR = RTC_CR_SC16P | RTC_CR_SC4P | RTC_CR_OSCE;
  358. }
  359. #endif
  360. // release I/O pins hold, if we woke up from VLLS mode
  361. if (PMC_REGSC & PMC_REGSC_ACKISO) PMC_REGSC |= PMC_REGSC_ACKISO;
  362. // Prepare RAM
  363. while ( dest < &_edata ) *dest++ = *src++;
  364. dest = &_sbss;
  365. while ( dest < &_ebss ) *dest++ = 0;
  366. // MCHCK
  367. #if defined(_mk20dx128vlf5_)
  368. /* FLL at 48MHz */
  369. MCG_C4 = MCG_C4_DMX32 | MCG_C4_DRST_DRS( 1 );
  370. //SIM_SOPT2 = SIM_SOPT2_PLLFLLSEL;
  371. SIM_SOPT2 = SIM_SOPT2_USBSRC | SIM_SOPT2_PLLFLLSEL | SIM_SOPT2_TRACECLKSEL | SIM_SOPT2_CLKOUTSEL( 6 );
  372. // Teensy 3.0 and 3.1
  373. #else
  374. unsigned int i;
  375. SCB_VTOR = 0; // use vector table in flash
  376. // default all interrupts to medium priority level
  377. for ( i = 0; i < NVIC_NUM_INTERRUPTS; i++ )
  378. {
  379. NVIC_SET_PRIORITY( i, 128 );
  380. }
  381. // start in FEI mode
  382. // enable capacitors for crystal
  383. OSC0_CR = OSC_SC8P | OSC_SC2P;
  384. // enable osc, 8-32 MHz range, low power mode
  385. MCG_C2 = MCG_C2_RANGE0( 2 ) | MCG_C2_EREFS;
  386. // switch to crystal as clock source, FLL input = 16 MHz / 512
  387. MCG_C1 = MCG_C1_CLKS( 2 ) | MCG_C1_FRDIV( 4 );
  388. // wait for crystal oscillator to begin
  389. while ( (MCG_S & MCG_S_OSCINIT0) == 0 );
  390. // wait for FLL to use oscillator
  391. while ( (MCG_S & MCG_S_IREFST) != 0 );
  392. // wait for MCGOUT to use oscillator
  393. while ( (MCG_S & MCG_S_CLKST_MASK) != MCG_S_CLKST( 2 ) );
  394. // now we're in FBE mode
  395. // config PLL input for 16 MHz Crystal / 4 = 4 MHz
  396. MCG_C5 = MCG_C5_PRDIV0( 3 );
  397. // config PLL for 96 MHz output
  398. MCG_C6 = MCG_C6_PLLS | MCG_C6_VDIV0( 0 );
  399. // wait for PLL to start using xtal as its input
  400. while ( !(MCG_S & MCG_S_PLLST) );
  401. // wait for PLL to lock
  402. while ( !(MCG_S & MCG_S_LOCK0) );
  403. // now we're in PBE mode
  404. #if F_CPU == 96000000
  405. // config divisors: 96 MHz core, 48 MHz bus, 24 MHz flash
  406. SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIV1( 0 ) | SIM_CLKDIV1_OUTDIV2( 1 ) | SIM_CLKDIV1_OUTDIV4( 3 );
  407. #elif F_CPU == 48000000
  408. // config divisors: 48 MHz core, 48 MHz bus, 24 MHz flash
  409. SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIV1( 1 ) | SIM_CLKDIV1_OUTDIV2( 1 ) | SIM_CLKDIV1_OUTDIV4( 3 );
  410. #elif F_CPU == 24000000
  411. // config divisors: 24 MHz core, 24 MHz bus, 24 MHz flash
  412. SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIV1( 3 ) | SIM_CLKDIV1_OUTDIV2( 3 ) | SIM_CLKDIV1_OUTDIV4( 3 );
  413. #else
  414. #error "Error, F_CPU must be 96000000, 48000000, or 24000000"
  415. #endif
  416. // switch to PLL as clock source, FLL input = 16 MHz / 512
  417. MCG_C1 = MCG_C1_CLKS( 0 ) | MCG_C1_FRDIV( 4 );
  418. // wait for PLL clock to be used
  419. while ( (MCG_S & MCG_S_CLKST_MASK) != MCG_S_CLKST( 3 ) );
  420. // now we're in PEE mode
  421. // configure USB for 48 MHz clock
  422. SIM_CLKDIV2 = SIM_CLKDIV2_USBDIV( 1 ); // USB = 96 MHz PLL / 2
  423. // USB uses PLL clock, trace is CPU clock, CLKOUT=OSCERCLK0
  424. SIM_SOPT2 = SIM_SOPT2_USBSRC | SIM_SOPT2_PLLFLLSEL | SIM_SOPT2_TRACECLKSEL | SIM_SOPT2_CLKOUTSEL( 6 );
  425. #endif
  426. // Initialize the SysTick counter
  427. SYST_RVR = (F_CPU / 1000) - 1;
  428. SYST_CSR = SYST_CSR_CLKSOURCE | SYST_CSR_TICKINT | SYST_CSR_ENABLE;
  429. __enable_irq();
  430. main();
  431. while ( 1 ); // Shouldn't get here...
  432. }
  433. // ----- RAM Setup -----
  434. char *__brkval = (char *)&_ebss;
  435. void * _sbrk( int incr )
  436. {
  437. char *prev = __brkval;
  438. __brkval += incr;
  439. return prev;
  440. }
  441. // ----- Interrupt Execution Priority -----
  442. int nvic_execution_priority()
  443. {
  444. int priority = 256;
  445. uint32_t primask, faultmask, basepri, ipsr;
  446. // full algorithm in ARM DDI0403D, page B1-639
  447. // this isn't quite complete, but hopefully good enough
  448. asm volatile( "mrs %0, faultmask\n" : "=r" (faultmask):: );
  449. if ( faultmask )
  450. {
  451. return -1;
  452. }
  453. asm volatile( "mrs %0, primask\n" : "=r" (primask):: );
  454. if ( primask )
  455. {
  456. return 0;
  457. }
  458. asm volatile( "mrs %0, ipsr\n" : "=r" (ipsr):: );
  459. if ( ipsr )
  460. {
  461. if ( ipsr < 16)
  462. {
  463. priority = 0; // could be non-zero
  464. }
  465. else
  466. {
  467. priority = NVIC_GET_PRIORITY( ipsr - 16 );
  468. }
  469. }
  470. asm volatile( "mrs %0, basepri\n" : "=r" (basepri):: );
  471. if ( basepri > 0 && basepri < priority )
  472. {
  473. priority = basepri;
  474. }
  475. return priority;
  476. }