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 27KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638
  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. const uint8_t sys_reset_to_loader_magic[22] = "\xff\x00\x7fRESET TO LOADER\x7f\x00\xff";
  42. // ----- Function Declarations -----
  43. extern int main();
  44. void ResetHandler();
  45. // ----- Interrupts -----
  46. // NVIC - Default ISR
  47. void fault_isr()
  48. {
  49. while ( 1 )
  50. {
  51. // keep polling some communication while in fault
  52. // mode, so we don't completely die.
  53. if ( SIM_SCGC4 & SIM_SCGC4_USBOTG ) usb_isr();
  54. if ( SIM_SCGC4 & SIM_SCGC4_UART0 ) uart0_status_isr();
  55. if ( SIM_SCGC4 & SIM_SCGC4_UART1 ) uart1_status_isr();
  56. if ( SIM_SCGC4 & SIM_SCGC4_UART2 ) uart2_status_isr();
  57. }
  58. }
  59. void unused_isr()
  60. {
  61. fault_isr();
  62. }
  63. // NVIC - SysTick ISR
  64. extern volatile uint32_t systick_millis_count;
  65. void systick_default_isr()
  66. {
  67. systick_millis_count++;
  68. }
  69. // NVIC - Default ISR/Vector Linking
  70. void nmi_isr() __attribute__ ((weak, alias("unused_isr")));
  71. void hard_fault_isr() __attribute__ ((weak, alias("unused_isr")));
  72. void memmanage_fault_isr() __attribute__ ((weak, alias("unused_isr")));
  73. void bus_fault_isr() __attribute__ ((weak, alias("unused_isr")));
  74. void usage_fault_isr() __attribute__ ((weak, alias("unused_isr")));
  75. void svcall_isr() __attribute__ ((weak, alias("unused_isr")));
  76. void debugmonitor_isr() __attribute__ ((weak, alias("unused_isr")));
  77. void pendablesrvreq_isr() __attribute__ ((weak, alias("unused_isr")));
  78. void systick_isr() __attribute__ ((weak, alias("systick_default_isr")));
  79. void dma_ch0_isr() __attribute__ ((weak, alias("unused_isr")));
  80. void dma_ch1_isr() __attribute__ ((weak, alias("unused_isr")));
  81. void dma_ch2_isr() __attribute__ ((weak, alias("unused_isr")));
  82. void dma_ch3_isr() __attribute__ ((weak, alias("unused_isr")));
  83. void dma_ch4_isr() __attribute__ ((weak, alias("unused_isr")));
  84. void dma_ch5_isr() __attribute__ ((weak, alias("unused_isr")));
  85. void dma_ch6_isr() __attribute__ ((weak, alias("unused_isr")));
  86. void dma_ch7_isr() __attribute__ ((weak, alias("unused_isr")));
  87. void dma_ch8_isr() __attribute__ ((weak, alias("unused_isr")));
  88. void dma_ch9_isr() __attribute__ ((weak, alias("unused_isr")));
  89. void dma_ch10_isr() __attribute__ ((weak, alias("unused_isr")));
  90. void dma_ch11_isr() __attribute__ ((weak, alias("unused_isr")));
  91. void dma_ch12_isr() __attribute__ ((weak, alias("unused_isr")));
  92. void dma_ch13_isr() __attribute__ ((weak, alias("unused_isr")));
  93. void dma_ch14_isr() __attribute__ ((weak, alias("unused_isr")));
  94. void dma_ch15_isr() __attribute__ ((weak, alias("unused_isr")));
  95. void dma_error_isr() __attribute__ ((weak, alias("unused_isr")));
  96. void mcm_isr() __attribute__ ((weak, alias("unused_isr")));
  97. void flash_cmd_isr() __attribute__ ((weak, alias("unused_isr")));
  98. void flash_error_isr() __attribute__ ((weak, alias("unused_isr")));
  99. void low_voltage_isr() __attribute__ ((weak, alias("unused_isr")));
  100. void wakeup_isr() __attribute__ ((weak, alias("unused_isr")));
  101. void watchdog_isr() __attribute__ ((weak, alias("unused_isr")));
  102. void i2c0_isr() __attribute__ ((weak, alias("unused_isr")));
  103. void i2c1_isr() __attribute__ ((weak, alias("unused_isr")));
  104. void i2c2_isr() __attribute__ ((weak, alias("unused_isr")));
  105. void spi0_isr() __attribute__ ((weak, alias("unused_isr")));
  106. void spi1_isr() __attribute__ ((weak, alias("unused_isr")));
  107. void spi2_isr() __attribute__ ((weak, alias("unused_isr")));
  108. void sdhc_isr() __attribute__ ((weak, alias("unused_isr")));
  109. void can0_message_isr() __attribute__ ((weak, alias("unused_isr")));
  110. void can0_bus_off_isr() __attribute__ ((weak, alias("unused_isr")));
  111. void can0_error_isr() __attribute__ ((weak, alias("unused_isr")));
  112. void can0_tx_warn_isr() __attribute__ ((weak, alias("unused_isr")));
  113. void can0_rx_warn_isr() __attribute__ ((weak, alias("unused_isr")));
  114. void can0_wakeup_isr() __attribute__ ((weak, alias("unused_isr")));
  115. void i2s0_tx_isr() __attribute__ ((weak, alias("unused_isr")));
  116. void i2s0_rx_isr() __attribute__ ((weak, alias("unused_isr")));
  117. void uart0_lon_isr() __attribute__ ((weak, alias("unused_isr")));
  118. void uart0_status_isr() __attribute__ ((weak, alias("unused_isr")));
  119. void uart0_error_isr() __attribute__ ((weak, alias("unused_isr")));
  120. void uart1_status_isr() __attribute__ ((weak, alias("unused_isr")));
  121. void uart1_error_isr() __attribute__ ((weak, alias("unused_isr")));
  122. void uart2_status_isr() __attribute__ ((weak, alias("unused_isr")));
  123. void uart2_error_isr() __attribute__ ((weak, alias("unused_isr")));
  124. void uart3_status_isr() __attribute__ ((weak, alias("unused_isr")));
  125. void uart3_error_isr() __attribute__ ((weak, alias("unused_isr")));
  126. void uart4_status_isr() __attribute__ ((weak, alias("unused_isr")));
  127. void uart4_error_isr() __attribute__ ((weak, alias("unused_isr")));
  128. void uart5_status_isr() __attribute__ ((weak, alias("unused_isr")));
  129. void uart5_error_isr() __attribute__ ((weak, alias("unused_isr")));
  130. void adc0_isr() __attribute__ ((weak, alias("unused_isr")));
  131. void adc1_isr() __attribute__ ((weak, alias("unused_isr")));
  132. void cmp0_isr() __attribute__ ((weak, alias("unused_isr")));
  133. void cmp1_isr() __attribute__ ((weak, alias("unused_isr")));
  134. void cmp2_isr() __attribute__ ((weak, alias("unused_isr")));
  135. void ftm0_isr() __attribute__ ((weak, alias("unused_isr")));
  136. void ftm1_isr() __attribute__ ((weak, alias("unused_isr")));
  137. void ftm2_isr() __attribute__ ((weak, alias("unused_isr")));
  138. void ftm3_isr() __attribute__ ((weak, alias("unused_isr")));
  139. void cmt_isr() __attribute__ ((weak, alias("unused_isr")));
  140. void rtc_alarm_isr() __attribute__ ((weak, alias("unused_isr")));
  141. void rtc_seconds_isr() __attribute__ ((weak, alias("unused_isr")));
  142. void pit0_isr() __attribute__ ((weak, alias("unused_isr")));
  143. void pit1_isr() __attribute__ ((weak, alias("unused_isr")));
  144. void pit2_isr() __attribute__ ((weak, alias("unused_isr")));
  145. void pit3_isr() __attribute__ ((weak, alias("unused_isr")));
  146. void pdb_isr() __attribute__ ((weak, alias("unused_isr")));
  147. void usb_isr() __attribute__ ((weak, alias("unused_isr")));
  148. void usb_charge_isr() __attribute__ ((weak, alias("unused_isr")));
  149. void dac0_isr() __attribute__ ((weak, alias("unused_isr")));
  150. void dac1_isr() __attribute__ ((weak, alias("unused_isr")));
  151. void tsi0_isr() __attribute__ ((weak, alias("unused_isr")));
  152. void mcg_isr() __attribute__ ((weak, alias("unused_isr")));
  153. void lptmr_isr() __attribute__ ((weak, alias("unused_isr")));
  154. void porta_isr() __attribute__ ((weak, alias("unused_isr")));
  155. void portb_isr() __attribute__ ((weak, alias("unused_isr")));
  156. void portc_isr() __attribute__ ((weak, alias("unused_isr")));
  157. void portd_isr() __attribute__ ((weak, alias("unused_isr")));
  158. void porte_isr() __attribute__ ((weak, alias("unused_isr")));
  159. void software_isr() __attribute__ ((weak, alias("unused_isr")));
  160. // NVIC - Interrupt Vector Table
  161. __attribute__ ((section(".vectors"), used))
  162. void (* const gVectors[])() =
  163. {
  164. (void (*)(void))((unsigned long)&_estack), // 0 ARM: Initial Stack Pointer
  165. ResetHandler, // 1 ARM: Initial Program Counter
  166. nmi_isr, // 2 ARM: Non-maskable Interrupt (NMI)
  167. hard_fault_isr, // 3 ARM: Hard Fault
  168. memmanage_fault_isr, // 4 ARM: MemManage Fault
  169. bus_fault_isr, // 5 ARM: Bus Fault
  170. usage_fault_isr, // 6 ARM: Usage Fault
  171. fault_isr, // 7 --
  172. fault_isr, // 8 --
  173. fault_isr, // 9 --
  174. fault_isr, // 10 --
  175. svcall_isr, // 11 ARM: Supervisor call (SVCall)
  176. debugmonitor_isr, // 12 ARM: Debug Monitor
  177. fault_isr, // 13 --
  178. pendablesrvreq_isr, // 14 ARM: Pendable req serv(PendableSrvReq)
  179. systick_isr, // 15 ARM: System tick timer (SysTick)
  180. #if defined(_mk20dx128_) || defined(_mk20dx128vlf5_)
  181. dma_ch0_isr, // 16 DMA channel 0 transfer complete
  182. dma_ch1_isr, // 17 DMA channel 1 transfer complete
  183. dma_ch2_isr, // 18 DMA channel 2 transfer complete
  184. dma_ch3_isr, // 19 DMA channel 3 transfer complete
  185. dma_error_isr, // 20 DMA error interrupt channel
  186. unused_isr, // 21 DMA --
  187. flash_cmd_isr, // 22 Flash Memory Command complete
  188. flash_error_isr, // 23 Flash Read collision
  189. low_voltage_isr, // 24 Low-voltage detect/warning
  190. wakeup_isr, // 25 Low Leakage Wakeup
  191. watchdog_isr, // 26 Both EWM and WDOG interrupt
  192. i2c0_isr, // 27 I2C0
  193. spi0_isr, // 28 SPI0
  194. i2s0_tx_isr, // 29 I2S0 Transmit
  195. i2s0_rx_isr, // 30 I2S0 Receive
  196. uart0_lon_isr, // 31 UART0 CEA709.1-B (LON) status
  197. uart0_status_isr, // 32 UART0 status
  198. uart0_error_isr, // 33 UART0 error
  199. uart1_status_isr, // 34 UART1 status
  200. uart1_error_isr, // 35 UART1 error
  201. uart2_status_isr, // 36 UART2 status
  202. uart2_error_isr, // 37 UART2 error
  203. adc0_isr, // 38 ADC0
  204. cmp0_isr, // 39 CMP0
  205. cmp1_isr, // 40 CMP1
  206. ftm0_isr, // 41 FTM0
  207. ftm1_isr, // 42 FTM1
  208. cmt_isr, // 43 CMT
  209. rtc_alarm_isr, // 44 RTC Alarm interrupt
  210. rtc_seconds_isr, // 45 RTC Seconds interrupt
  211. pit0_isr, // 46 PIT Channel 0
  212. pit1_isr, // 47 PIT Channel 1
  213. pit2_isr, // 48 PIT Channel 2
  214. pit3_isr, // 49 PIT Channel 3
  215. pdb_isr, // 50 PDB Programmable Delay Block
  216. usb_isr, // 51 USB OTG
  217. usb_charge_isr, // 52 USB Charger Detect
  218. tsi0_isr, // 53 TSI0
  219. mcg_isr, // 54 MCG
  220. lptmr_isr, // 55 Low Power Timer
  221. porta_isr, // 56 Pin detect (Port A)
  222. portb_isr, // 57 Pin detect (Port B)
  223. portc_isr, // 58 Pin detect (Port C)
  224. portd_isr, // 59 Pin detect (Port D)
  225. porte_isr, // 60 Pin detect (Port E)
  226. software_isr, // 61 Software interrupt
  227. #elif defined(_mk20dx256_)
  228. dma_ch0_isr, // 16 DMA channel 0 transfer complete
  229. dma_ch1_isr, // 17 DMA channel 1 transfer complete
  230. dma_ch2_isr, // 18 DMA channel 2 transfer complete
  231. dma_ch3_isr, // 19 DMA channel 3 transfer complete
  232. dma_ch4_isr, // 20 DMA channel 4 transfer complete
  233. dma_ch5_isr, // 21 DMA channel 5 transfer complete
  234. dma_ch6_isr, // 22 DMA channel 6 transfer complete
  235. dma_ch7_isr, // 23 DMA channel 7 transfer complete
  236. dma_ch8_isr, // 24 DMA channel 8 transfer complete
  237. dma_ch9_isr, // 25 DMA channel 9 transfer complete
  238. dma_ch10_isr, // 26 DMA channel 10 transfer complete
  239. dma_ch11_isr, // 27 DMA channel 10 transfer complete
  240. dma_ch12_isr, // 28 DMA channel 10 transfer complete
  241. dma_ch13_isr, // 29 DMA channel 10 transfer complete
  242. dma_ch14_isr, // 30 DMA channel 10 transfer complete
  243. dma_ch15_isr, // 31 DMA channel 10 transfer complete
  244. dma_error_isr, // 32 DMA error interrupt channel
  245. unused_isr, // 33 --
  246. flash_cmd_isr, // 34 Flash Memory Command complete
  247. flash_error_isr, // 35 Flash Read collision
  248. low_voltage_isr, // 36 Low-voltage detect/warning
  249. wakeup_isr, // 37 Low Leakage Wakeup
  250. watchdog_isr, // 38 Both EWM and WDOG interrupt
  251. unused_isr, // 39 --
  252. i2c0_isr, // 40 I2C0
  253. i2c1_isr, // 41 I2C1
  254. spi0_isr, // 42 SPI0
  255. spi1_isr, // 43 SPI1
  256. unused_isr, // 44 --
  257. can0_message_isr, // 45 CAN OR'ed Message buffer (0-15)
  258. can0_bus_off_isr, // 46 CAN Bus Off
  259. can0_error_isr, // 47 CAN Error
  260. can0_tx_warn_isr, // 48 CAN Transmit Warning
  261. can0_rx_warn_isr, // 49 CAN Receive Warning
  262. can0_wakeup_isr, // 50 CAN Wake Up
  263. i2s0_tx_isr, // 51 I2S0 Transmit
  264. i2s0_rx_isr, // 52 I2S0 Receive
  265. unused_isr, // 53 --
  266. unused_isr, // 54 --
  267. unused_isr, // 55 --
  268. unused_isr, // 56 --
  269. unused_isr, // 57 --
  270. unused_isr, // 58 --
  271. unused_isr, // 59 --
  272. uart0_lon_isr, // 60 UART0 CEA709.1-B (LON) status
  273. uart0_status_isr, // 61 UART0 status
  274. uart0_error_isr, // 62 UART0 error
  275. uart1_status_isr, // 63 UART1 status
  276. uart1_error_isr, // 64 UART1 error
  277. uart2_status_isr, // 65 UART2 status
  278. uart2_error_isr, // 66 UART2 error
  279. unused_isr, // 67 --
  280. unused_isr, // 68 --
  281. unused_isr, // 69 --
  282. unused_isr, // 70 --
  283. unused_isr, // 71 --
  284. unused_isr, // 72 --
  285. adc0_isr, // 73 ADC0
  286. adc1_isr, // 74 ADC1
  287. cmp0_isr, // 75 CMP0
  288. cmp1_isr, // 76 CMP1
  289. cmp2_isr, // 77 CMP2
  290. ftm0_isr, // 78 FTM0
  291. ftm1_isr, // 79 FTM1
  292. ftm2_isr, // 80 FTM2
  293. cmt_isr, // 81 CMT
  294. rtc_alarm_isr, // 82 RTC Alarm interrupt
  295. rtc_seconds_isr, // 83 RTC Seconds interrupt
  296. pit0_isr, // 84 PIT Channel 0
  297. pit1_isr, // 85 PIT Channel 1
  298. pit2_isr, // 86 PIT Channel 2
  299. pit3_isr, // 87 PIT Channel 3
  300. pdb_isr, // 88 PDB Programmable Delay Block
  301. usb_isr, // 89 USB OTG
  302. usb_charge_isr, // 90 USB Charger Detect
  303. unused_isr, // 91 --
  304. unused_isr, // 92 --
  305. unused_isr, // 93 --
  306. unused_isr, // 94 --
  307. unused_isr, // 95 --
  308. unused_isr, // 96 --
  309. dac0_isr, // 97 DAC0
  310. unused_isr, // 98 --
  311. tsi0_isr, // 99 TSI0
  312. mcg_isr, // 100 MCG
  313. lptmr_isr, // 101 Low Power Timer
  314. unused_isr, // 102 --
  315. porta_isr, // 103 Pin detect (Port A)
  316. portb_isr, // 104 Pin detect (Port B)
  317. portc_isr, // 105 Pin detect (Port C)
  318. portd_isr, // 106 Pin detect (Port D)
  319. porte_isr, // 107 Pin detect (Port E)
  320. unused_isr, // 108 --
  321. unused_isr, // 109 --
  322. software_isr, // 110 Software interrupt
  323. #endif
  324. };
  325. // ----- Flash Configuration -----
  326. // Only necessary for Teensy 3s, MCHCK uses the Bootloader to handle this
  327. #if defined(_mk20dx128_) || defined(_mk20dx256_)
  328. __attribute__ ((section(".flashconfig"), used))
  329. const uint8_t flashconfigbytes[16] = {
  330. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  331. 0xFF, 0xFF, 0xFF, 0xFF, 0xFE, 0xFF, 0xFF, 0xFF
  332. };
  333. #elif defined(_mk20dx128vlf5_) && defined(_bootloader_)
  334. // XXX Byte labels may be in incorrect positions, double check before modifying
  335. // FSEC is in correct location -Jacob
  336. __attribute__ ((section(".flashconfig"), used))
  337. const uint8_t flashconfigbytes[16] = {
  338. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // Backdoor Verif Key 28.3.1
  339. 0xFF, 0xFF, 0xFF, 0xFF, // Program Flash Protection Bytes FPROT0-3
  340. 0xBE, // Flash security byte FSEC
  341. 0x03, // Flash nonvolatile option byte FOPT
  342. 0xFF, // EEPROM Protection Byte FEPROT
  343. 0xFF, // Data Flash Protection Byte FDPROT
  344. };
  345. #endif
  346. // ----- Functions -----
  347. __attribute__((noreturn))
  348. static inline void jump_to_app( uintptr_t addr )
  349. {
  350. // addr is in r0
  351. __asm__("ldr sp, [%[addr], #0]\n"
  352. "ldr pc, [%[addr], #4]"
  353. :: [addr] "r" (addr));
  354. // NOTREACHED
  355. __builtin_unreachable();
  356. }
  357. void *memset( void *addr, int val, unsigned int len )
  358. {
  359. char *buf = addr;
  360. for (; len > 0; --len, ++buf)
  361. *buf = val;
  362. return (addr);
  363. }
  364. int memcmp( const void *a, const void *b, unsigned int len )
  365. {
  366. const uint8_t *ap = a, *bp = b;
  367. int val = 0;
  368. for (; len > 0 && (val = *ap - *bp) == 0; --len, ++ap, ++bp)
  369. /* NOTHING */;
  370. return (val);
  371. }
  372. void *memcpy( void *dst, const void *src, unsigned int len )
  373. {
  374. char *dstbuf = dst;
  375. const char *srcbuf = src;
  376. for (; len > 0; --len, ++dstbuf, ++srcbuf)
  377. *dstbuf = *srcbuf;
  378. return (dst);
  379. }
  380. // ----- Chip Entry Point -----
  381. __attribute__ ((section(".startup")))
  382. void ResetHandler()
  383. {
  384. // Disable Watchdog
  385. WDOG_UNLOCK = WDOG_UNLOCK_SEQ1;
  386. WDOG_UNLOCK = WDOG_UNLOCK_SEQ2;
  387. WDOG_STCTRLH = WDOG_STCTRLH_ALLOWUPDATE;
  388. #if defined(_mk20dx128vlf5_) && defined(_bootloader_) // Bootloader Section
  389. extern uint32_t _app_rom;
  390. // We treat _app_rom as pointer to directly read the stack
  391. // pointer and check for valid app code. This is no fool
  392. // proof method, but it should help for the first flash.
  393. if ( RCM_SRS0 & 0x40 || _app_rom == 0xffffffff ||
  394. memcmp( (uint8_t*)&VBAT, sys_reset_to_loader_magic, sizeof(sys_reset_to_loader_magic) ) == 0 ) // Check for soft reload
  395. {
  396. memset( (uint8_t*)&VBAT, 0, sizeof(VBAT) );
  397. }
  398. else
  399. {
  400. uint32_t addr = (uintptr_t)&_app_rom;
  401. SCB_VTOR = addr; // relocate vector table
  402. jump_to_app( addr );
  403. }
  404. #endif
  405. uint32_t *src = &_etext;
  406. uint32_t *dest = &_sdata;
  407. // Enable clocks to always-used peripherals
  408. SIM_SCGC5 = 0x00043F82; // Clocks active to all GPIO
  409. SIM_SCGC6 = SIM_SCGC6_FTM0 | SIM_SCGC6_FTM1 | SIM_SCGC6_ADC0 | SIM_SCGC6_FTFL;
  410. #if defined(_mk20dx128_)
  411. SIM_SCGC6 |= SIM_SCGC6_RTC;
  412. #elif defined(_mk20dx256_)
  413. SIM_SCGC3 = SIM_SCGC3_ADC1 | SIM_SCGC3_FTM2;
  414. SIM_SCGC6 |= SIM_SCGC6_RTC;
  415. #endif
  416. #if defined(_mk20dx128_) || defined(_mk20dx256_) // Teensy 3s
  417. // if the RTC oscillator isn't enabled, get it started early
  418. if ( !(RTC_CR & RTC_CR_OSCE) )
  419. {
  420. RTC_SR = 0;
  421. RTC_CR = RTC_CR_SC16P | RTC_CR_SC4P | RTC_CR_OSCE;
  422. }
  423. #endif
  424. // release I/O pins hold, if we woke up from VLLS mode
  425. if ( PMC_REGSC & PMC_REGSC_ACKISO )
  426. {
  427. PMC_REGSC |= PMC_REGSC_ACKISO;
  428. }
  429. // Prepare RAM
  430. while ( dest < &_edata ) *dest++ = *src++;
  431. dest = &_sbss;
  432. while ( dest < &_ebss ) *dest++ = 0;
  433. // MCHCK
  434. #if defined(_mk20dx128vlf5_)
  435. // Default all interrupts to medium priority level
  436. for ( unsigned int i = 0; i < NVIC_NUM_INTERRUPTS; i++ )
  437. {
  438. NVIC_SET_PRIORITY( i, 128 );
  439. }
  440. // FLL at 48MHz
  441. MCG_C4 = MCG_C4_DMX32 | MCG_C4_DRST_DRS( 1 );
  442. // USB Clock and FLL select
  443. SIM_SOPT2 = SIM_SOPT2_USBSRC | SIM_SOPT2_TRACECLKSEL;
  444. // Teensy 3.0 and 3.1
  445. #else
  446. unsigned int i;
  447. SCB_VTOR = 0; // use vector table in flash
  448. // default all interrupts to medium priority level
  449. for ( i = 0; i < NVIC_NUM_INTERRUPTS; i++ )
  450. {
  451. NVIC_SET_PRIORITY( i, 128 );
  452. }
  453. // start in FEI mode
  454. // enable capacitors for crystal
  455. OSC0_CR = OSC_SC8P | OSC_SC2P;
  456. // enable osc, 8-32 MHz range, low power mode
  457. MCG_C2 = MCG_C2_RANGE0( 2 ) | MCG_C2_EREFS;
  458. // switch to crystal as clock source, FLL input = 16 MHz / 512
  459. MCG_C1 = MCG_C1_CLKS( 2 ) | MCG_C1_FRDIV( 4 );
  460. // wait for crystal oscillator to begin
  461. while ( (MCG_S & MCG_S_OSCINIT0) == 0 );
  462. // wait for FLL to use oscillator
  463. while ( (MCG_S & MCG_S_IREFST) != 0 );
  464. // wait for MCGOUT to use oscillator
  465. while ( (MCG_S & MCG_S_CLKST_MASK) != MCG_S_CLKST( 2 ) );
  466. // now we're in FBE mode
  467. // config PLL input for 16 MHz Crystal / 4 = 4 MHz
  468. MCG_C5 = MCG_C5_PRDIV0( 3 );
  469. // config PLL for 96 MHz output
  470. MCG_C6 = MCG_C6_PLLS | MCG_C6_VDIV0( 0 );
  471. // wait for PLL to start using xtal as its input
  472. while ( !(MCG_S & MCG_S_PLLST) );
  473. // wait for PLL to lock
  474. while ( !(MCG_S & MCG_S_LOCK0) );
  475. // now we're in PBE mode
  476. #if F_CPU == 96000000
  477. // config divisors: 96 MHz core, 48 MHz bus, 24 MHz flash
  478. SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIV1( 0 ) | SIM_CLKDIV1_OUTDIV2( 1 ) | SIM_CLKDIV1_OUTDIV4( 3 );
  479. #elif F_CPU == 48000000
  480. // config divisors: 48 MHz core, 48 MHz bus, 24 MHz flash
  481. SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIV1( 1 ) | SIM_CLKDIV1_OUTDIV2( 1 ) | SIM_CLKDIV1_OUTDIV4( 3 );
  482. #elif F_CPU == 24000000
  483. // config divisors: 24 MHz core, 24 MHz bus, 24 MHz flash
  484. SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIV1( 3 ) | SIM_CLKDIV1_OUTDIV2( 3 ) | SIM_CLKDIV1_OUTDIV4( 3 );
  485. #else
  486. #error "Error, F_CPU must be 96000000, 48000000, or 24000000"
  487. #endif
  488. // switch to PLL as clock source, FLL input = 16 MHz / 512
  489. MCG_C1 = MCG_C1_CLKS( 0 ) | MCG_C1_FRDIV( 4 );
  490. // wait for PLL clock to be used
  491. while ( (MCG_S & MCG_S_CLKST_MASK) != MCG_S_CLKST( 3 ) );
  492. // now we're in PEE mode
  493. // configure USB for 48 MHz clock
  494. SIM_CLKDIV2 = SIM_CLKDIV2_USBDIV( 1 ); // USB = 96 MHz PLL / 2
  495. // USB uses PLL clock, trace is CPU clock, CLKOUT=OSCERCLK0
  496. SIM_SOPT2 = SIM_SOPT2_USBSRC | SIM_SOPT2_PLLFLLSEL | SIM_SOPT2_TRACECLKSEL | SIM_SOPT2_CLKOUTSEL( 6 );
  497. #endif
  498. #if !defined(_bootloader_)
  499. // Initialize the SysTick counter
  500. SYST_RVR = (F_CPU / 1000) - 1;
  501. SYST_CSR = SYST_CSR_CLKSOURCE | SYST_CSR_TICKINT | SYST_CSR_ENABLE;
  502. __enable_irq();
  503. #else
  504. // Disable Watchdog for bootloader
  505. WDOG_STCTRLH &= ~WDOG_STCTRLH_WDOGEN;
  506. #endif
  507. main();
  508. while ( 1 ); // Shouldn't get here...
  509. }
  510. // ----- RAM Setup -----
  511. char *__brkval = (char *)&_ebss;
  512. void * _sbrk( int incr )
  513. {
  514. char *prev = __brkval;
  515. __brkval += incr;
  516. return prev;
  517. }
  518. // ----- Interrupt Execution Priority -----
  519. int nvic_execution_priority()
  520. {
  521. int priority = 256;
  522. uint32_t primask, faultmask, basepri, ipsr;
  523. // full algorithm in ARM DDI0403D, page B1-639
  524. // this isn't quite complete, but hopefully good enough
  525. asm volatile( "mrs %0, faultmask\n" : "=r" (faultmask):: );
  526. if ( faultmask )
  527. {
  528. return -1;
  529. }
  530. asm volatile( "mrs %0, primask\n" : "=r" (primask):: );
  531. if ( primask )
  532. {
  533. return 0;
  534. }
  535. asm volatile( "mrs %0, ipsr\n" : "=r" (ipsr):: );
  536. if ( ipsr )
  537. {
  538. if ( ipsr < 16)
  539. {
  540. priority = 0; // could be non-zero
  541. }
  542. else
  543. {
  544. priority = NVIC_GET_PRIORITY( ipsr - 16 );
  545. }
  546. }
  547. asm volatile( "mrs %0, basepri\n" : "=r" (basepri):: );
  548. if ( basepri > 0 && basepri < priority )
  549. {
  550. priority = basepri;
  551. }
  552. return priority;
  553. }