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

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