選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

mk20dx128.c 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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 "mk20dx128.h"
  31. extern unsigned long _stext;
  32. extern unsigned long _etext;
  33. extern unsigned long _sdata;
  34. extern unsigned long _edata;
  35. extern unsigned long _sbss;
  36. extern unsigned long _ebss;
  37. extern unsigned long _estack;
  38. //extern void __init_array_start(void);
  39. //extern void __init_array_end(void);
  40. extern int main (void);
  41. void ResetHandler(void);
  42. void _init_Teensyduino_internal_(void);
  43. void __libc_init_array(void);
  44. void fault_isr(void)
  45. {
  46. while (1) {
  47. // keep polling some communication while in fault
  48. // mode, so we don't completely die.
  49. if (SIM_SCGC4 & SIM_SCGC4_USBOTG) usb_isr();
  50. if (SIM_SCGC4 & SIM_SCGC4_UART0) uart0_status_isr();
  51. if (SIM_SCGC4 & SIM_SCGC4_UART1) uart1_status_isr();
  52. if (SIM_SCGC4 & SIM_SCGC4_UART2) uart2_status_isr();
  53. }
  54. }
  55. void unused_isr(void)
  56. {
  57. fault_isr();
  58. }
  59. extern volatile uint32_t systick_millis_count;
  60. void systick_default_isr(void)
  61. {
  62. systick_millis_count++;
  63. }
  64. void nmi_isr(void) __attribute__ ((weak, alias("unused_isr")));
  65. void hard_fault_isr(void) __attribute__ ((weak, alias("unused_isr")));
  66. void memmanage_fault_isr(void) __attribute__ ((weak, alias("unused_isr")));
  67. void bus_fault_isr(void) __attribute__ ((weak, alias("unused_isr")));
  68. void usage_fault_isr(void) __attribute__ ((weak, alias("unused_isr")));
  69. void svcall_isr(void) __attribute__ ((weak, alias("unused_isr")));
  70. void debugmonitor_isr(void) __attribute__ ((weak, alias("unused_isr")));
  71. void pendablesrvreq_isr(void) __attribute__ ((weak, alias("unused_isr")));
  72. void systick_isr(void) __attribute__ ((weak, alias("systick_default_isr")));
  73. void dma_ch0_isr(void) __attribute__ ((weak, alias("unused_isr")));
  74. void dma_ch1_isr(void) __attribute__ ((weak, alias("unused_isr")));
  75. void dma_ch2_isr(void) __attribute__ ((weak, alias("unused_isr")));
  76. void dma_ch3_isr(void) __attribute__ ((weak, alias("unused_isr")));
  77. void dma_error_isr(void) __attribute__ ((weak, alias("unused_isr")));
  78. void flash_cmd_isr(void) __attribute__ ((weak, alias("unused_isr")));
  79. void flash_error_isr(void) __attribute__ ((weak, alias("unused_isr")));
  80. void low_voltage_isr(void) __attribute__ ((weak, alias("unused_isr")));
  81. void wakeup_isr(void) __attribute__ ((weak, alias("unused_isr")));
  82. void watchdog_isr(void) __attribute__ ((weak, alias("unused_isr")));
  83. void i2c0_isr(void) __attribute__ ((weak, alias("unused_isr")));
  84. void spi0_isr(void) __attribute__ ((weak, alias("unused_isr")));
  85. void i2s0_tx_isr(void) __attribute__ ((weak, alias("unused_isr")));
  86. void i2s0_rx_isr(void) __attribute__ ((weak, alias("unused_isr")));
  87. void uart0_lon_isr(void) __attribute__ ((weak, alias("unused_isr")));
  88. void uart0_status_isr(void) __attribute__ ((weak, alias("unused_isr")));
  89. void uart0_error_isr(void) __attribute__ ((weak, alias("unused_isr")));
  90. void uart1_status_isr(void) __attribute__ ((weak, alias("unused_isr")));
  91. void uart1_error_isr(void) __attribute__ ((weak, alias("unused_isr")));
  92. void uart2_status_isr(void) __attribute__ ((weak, alias("unused_isr")));
  93. void uart2_error_isr(void) __attribute__ ((weak, alias("unused_isr")));
  94. void adc0_isr(void) __attribute__ ((weak, alias("unused_isr")));
  95. void cmp0_isr(void) __attribute__ ((weak, alias("unused_isr")));
  96. void cmp1_isr(void) __attribute__ ((weak, alias("unused_isr")));
  97. void ftm0_isr(void) __attribute__ ((weak, alias("unused_isr")));
  98. void ftm1_isr(void) __attribute__ ((weak, alias("unused_isr")));
  99. void cmt_isr(void) __attribute__ ((weak, alias("unused_isr")));
  100. void rtc_alarm_isr(void) __attribute__ ((weak, alias("unused_isr")));
  101. void rtc_seconds_isr(void) __attribute__ ((weak, alias("unused_isr")));
  102. void pit0_isr(void) __attribute__ ((weak, alias("unused_isr")));
  103. void pit1_isr(void) __attribute__ ((weak, alias("unused_isr")));
  104. void pit2_isr(void) __attribute__ ((weak, alias("unused_isr")));
  105. void pit3_isr(void) __attribute__ ((weak, alias("unused_isr")));
  106. void pdb_isr(void) __attribute__ ((weak, alias("unused_isr")));
  107. void usb_isr(void) __attribute__ ((weak, alias("unused_isr")));
  108. void usb_charge_isr(void) __attribute__ ((weak, alias("unused_isr")));
  109. void tsi0_isr(void) __attribute__ ((weak, alias("unused_isr")));
  110. void mcg_isr(void) __attribute__ ((weak, alias("unused_isr")));
  111. void lptmr_isr(void) __attribute__ ((weak, alias("unused_isr")));
  112. void porta_isr(void) __attribute__ ((weak, alias("unused_isr")));
  113. void portb_isr(void) __attribute__ ((weak, alias("unused_isr")));
  114. void portc_isr(void) __attribute__ ((weak, alias("unused_isr")));
  115. void portd_isr(void) __attribute__ ((weak, alias("unused_isr")));
  116. void porte_isr(void) __attribute__ ((weak, alias("unused_isr")));
  117. void software_isr(void) __attribute__ ((weak, alias("unused_isr")));
  118. // TODO: create AVR-stype ISR() macro, with default linkage to undefined handler
  119. //
  120. __attribute__ ((section(".vectors"), used))
  121. void (* const gVectors[])(void) =
  122. {
  123. (void (*)(void))((unsigned long)&_estack), // 0 ARM: Initial Stack Pointer
  124. ResetHandler, // 1 ARM: Initial Program Counter
  125. nmi_isr, // 2 ARM: Non-maskable Interrupt (NMI)
  126. hard_fault_isr, // 3 ARM: Hard Fault
  127. memmanage_fault_isr, // 4 ARM: MemManage Fault
  128. bus_fault_isr, // 5 ARM: Bus Fault
  129. usage_fault_isr, // 6 ARM: Usage Fault
  130. fault_isr, // 7 --
  131. fault_isr, // 8 --
  132. fault_isr, // 9 --
  133. fault_isr, // 10 --
  134. svcall_isr, // 11 ARM: Supervisor call (SVCall)
  135. debugmonitor_isr, // 12 ARM: Debug Monitor
  136. fault_isr, // 13 --
  137. pendablesrvreq_isr, // 14 ARM: Pendable req serv(PendableSrvReq)
  138. systick_isr, // 15 ARM: System tick timer (SysTick)
  139. dma_ch0_isr, // 16 DMA channel 0 transfer complete
  140. dma_ch1_isr, // 17 DMA channel 1 transfer complete
  141. dma_ch2_isr, // 18 DMA channel 2 transfer complete
  142. dma_ch3_isr, // 19 DMA channel 3 transfer complete
  143. dma_error_isr, // 20 DMA error interrupt channel
  144. unused_isr, // 21 DMA --
  145. flash_cmd_isr, // 22 Flash Memory Command complete
  146. flash_error_isr, // 23 Flash Read collision
  147. low_voltage_isr, // 24 Low-voltage detect/warning
  148. wakeup_isr, // 25 Low Leakage Wakeup
  149. watchdog_isr, // 26 Both EWM and WDOG interrupt
  150. i2c0_isr, // 27 I2C0
  151. spi0_isr, // 28 SPI0
  152. i2s0_tx_isr, // 29 I2S0 Transmit
  153. i2s0_rx_isr, // 30 I2S0 Receive
  154. uart0_lon_isr, // 31 UART0 CEA709.1-B (LON) status
  155. uart0_status_isr, // 32 UART0 status
  156. uart0_error_isr, // 33 UART0 error
  157. uart1_status_isr, // 34 UART1 status
  158. uart1_error_isr, // 35 UART1 error
  159. uart2_status_isr, // 36 UART2 status
  160. uart2_error_isr, // 37 UART2 error
  161. adc0_isr, // 38 ADC0
  162. cmp0_isr, // 39 CMP0
  163. cmp1_isr, // 40 CMP1
  164. ftm0_isr, // 41 FTM0
  165. ftm1_isr, // 42 FTM1
  166. cmt_isr, // 43 CMT
  167. rtc_alarm_isr, // 44 RTC Alarm interrupt
  168. rtc_seconds_isr, // 45 RTC Seconds interrupt
  169. pit0_isr, // 46 PIT Channel 0
  170. pit1_isr, // 47 PIT Channel 1
  171. pit2_isr, // 48 PIT Channel 2
  172. pit3_isr, // 49 PIT Channel 3
  173. pdb_isr, // 50 PDB Programmable Delay Block
  174. usb_isr, // 51 USB OTG
  175. usb_charge_isr, // 52 USB Charger Detect
  176. tsi0_isr, // 53 TSI0
  177. mcg_isr, // 54 MCG
  178. lptmr_isr, // 55 Low Power Timer
  179. porta_isr, // 56 Pin detect (Port A)
  180. portb_isr, // 57 Pin detect (Port B)
  181. portc_isr, // 58 Pin detect (Port C)
  182. portd_isr, // 59 Pin detect (Port D)
  183. porte_isr, // 60 Pin detect (Port E)
  184. software_isr, // 61 Software interrupt
  185. };
  186. //void usb_isr(void)
  187. //{
  188. //}
  189. __attribute__ ((section(".flashconfig"), used))
  190. const uint8_t flashconfigbytes[16] = {
  191. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  192. 0xFF, 0xFF, 0xFF, 0xFF, 0xFE, 0xFF, 0xFF, 0xFF
  193. };
  194. // Automatically initialize the RTC. When the build defines the compile
  195. // time, and the user has added a crystal, the RTC will automatically
  196. // begin at the time of the first upload.
  197. #ifndef TIME_T
  198. #define TIME_T 1349049600 // default 1 Oct 2012 (never used, Arduino sets this)
  199. #endif
  200. extern void rtc_set(unsigned long t);
  201. static void startup_unused_hook(void) {}
  202. void startup_early_hook(void) __attribute__ ((weak, alias("startup_unused_hook")));
  203. void startup_late_hook(void) __attribute__ ((weak, alias("startup_unused_hook")));
  204. __attribute__ ((section(".startup")))
  205. void ResetHandler(void)
  206. {
  207. uint32_t *src = &_etext;
  208. uint32_t *dest = &_sdata;
  209. unsigned int i;
  210. WDOG_UNLOCK = WDOG_UNLOCK_SEQ1;
  211. WDOG_UNLOCK = WDOG_UNLOCK_SEQ2;
  212. WDOG_STCTRLH = WDOG_STCTRLH_ALLOWUPDATE;
  213. startup_early_hook();
  214. // enable clocks to always-used peripherals
  215. SIM_SCGC5 = 0x00043F82; // clocks active to all GPIO
  216. SIM_SCGC6 = SIM_SCGC6_RTC | SIM_SCGC6_FTM0 | SIM_SCGC6_FTM1 | SIM_SCGC6_ADC0 | SIM_SCGC6_FTFL;
  217. // if the RTC oscillator isn't enabled, get it started early
  218. if (!(RTC_CR & RTC_CR_OSCE)) {
  219. RTC_SR = 0;
  220. RTC_CR = RTC_CR_SC16P | RTC_CR_SC4P | RTC_CR_OSCE;
  221. }
  222. // release I/O pins hold, if we woke up from VLLS mode
  223. if (PMC_REGSC & PMC_REGSC_ACKISO) PMC_REGSC |= PMC_REGSC_ACKISO;
  224. // TODO: do this while the PLL is waiting to lock....
  225. while (dest < &_edata) *dest++ = *src++;
  226. dest = &_sbss;
  227. while (dest < &_ebss) *dest++ = 0;
  228. SCB_VTOR = 0; // use vector table in flash
  229. // default all interrupts to medium priority level
  230. for (i=0; i < NVIC_NUM_INTERRUPTS; i++) NVIC_SET_PRIORITY(i, 128);
  231. // start in FEI mode
  232. // enable capacitors for crystal
  233. OSC0_CR = OSC_SC8P | OSC_SC2P;
  234. // enable osc, 8-32 MHz range, low power mode
  235. MCG_C2 = MCG_C2_RANGE0(2) | MCG_C2_EREFS;
  236. // switch to crystal as clock source, FLL input = 16 MHz / 512
  237. MCG_C1 = MCG_C1_CLKS(2) | MCG_C1_FRDIV(4);
  238. // wait for crystal oscillator to begin
  239. while ((MCG_S & MCG_S_OSCINIT0) == 0) ;
  240. // wait for FLL to use oscillator
  241. while ((MCG_S & MCG_S_IREFST) != 0) ;
  242. // wait for MCGOUT to use oscillator
  243. while ((MCG_S & MCG_S_CLKST_MASK) != MCG_S_CLKST(2)) ;
  244. // now we're in FBE mode
  245. // config PLL input for 16 MHz Crystal / 4 = 4 MHz
  246. MCG_C5 = MCG_C5_PRDIV0(3);
  247. // config PLL for 96 MHz output
  248. MCG_C6 = MCG_C6_PLLS | MCG_C6_VDIV0(0);
  249. // wait for PLL to start using xtal as its input
  250. while (!(MCG_S & MCG_S_PLLST)) ;
  251. // wait for PLL to lock
  252. while (!(MCG_S & MCG_S_LOCK0)) ;
  253. // now we're in PBE mode
  254. #if F_CPU == 96000000
  255. // config divisors: 96 MHz core, 48 MHz bus, 24 MHz flash
  256. SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIV1(0) | SIM_CLKDIV1_OUTDIV2(1) | SIM_CLKDIV1_OUTDIV4(3);
  257. #elif F_CPU == 48000000
  258. // config divisors: 48 MHz core, 48 MHz bus, 24 MHz flash
  259. SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIV1(1) | SIM_CLKDIV1_OUTDIV2(1) | SIM_CLKDIV1_OUTDIV4(3);
  260. #elif F_CPU == 24000000
  261. // config divisors: 24 MHz core, 24 MHz bus, 24 MHz flash
  262. SIM_CLKDIV1 = SIM_CLKDIV1_OUTDIV1(3) | SIM_CLKDIV1_OUTDIV2(3) | SIM_CLKDIV1_OUTDIV4(3);
  263. #else
  264. #error "Error, F_CPU must be 96000000, 48000000, or 24000000"
  265. #endif
  266. // switch to PLL as clock source, FLL input = 16 MHz / 512
  267. MCG_C1 = MCG_C1_CLKS(0) | MCG_C1_FRDIV(4);
  268. // wait for PLL clock to be used
  269. while ((MCG_S & MCG_S_CLKST_MASK) != MCG_S_CLKST(3)) ;
  270. // now we're in PEE mode
  271. // configure USB for 48 MHz clock
  272. SIM_CLKDIV2 = SIM_CLKDIV2_USBDIV(1); // USB = 96 MHz PLL / 2
  273. // USB uses PLL clock, trace is CPU clock, CLKOUT=OSCERCLK0
  274. SIM_SOPT2 = SIM_SOPT2_USBSRC | SIM_SOPT2_PLLFLLSEL | SIM_SOPT2_TRACECLKSEL | SIM_SOPT2_CLKOUTSEL(6);
  275. // initialize the SysTick counter
  276. SYST_RVR = (F_CPU / 1000) - 1;
  277. SYST_CSR = SYST_CSR_CLKSOURCE | SYST_CSR_TICKINT | SYST_CSR_ENABLE;
  278. //init_pins();
  279. __enable_irq();
  280. _init_Teensyduino_internal_();
  281. if (RTC_SR & RTC_SR_TIF) rtc_set(TIME_T);
  282. __libc_init_array();
  283. /*
  284. for (ptr = &__init_array_start; ptr < &__init_array_end; ptr++) {
  285. (*ptr)();
  286. }
  287. */
  288. startup_late_hook();
  289. main();
  290. while (1) ;
  291. }
  292. // TODO: is this needed for c++ and where does it come from?
  293. /*
  294. void _init(void)
  295. {
  296. }
  297. */
  298. char *__brkval = (char *)&_ebss;
  299. void * _sbrk(int incr)
  300. {
  301. //static char *heap_end = (char *)&_ebss;
  302. //char *prev = heap_end;
  303. //heap_end += incr;
  304. char *prev = __brkval;
  305. __brkval += incr;
  306. return prev;
  307. }
  308. int _read(int file, char *ptr, int len)
  309. {
  310. return 0;
  311. }
  312. int _write(int file, char *ptr, int len)
  313. {
  314. return 0;
  315. }
  316. int _close(int fd)
  317. {
  318. return -1;
  319. }
  320. #include <sys/stat.h>
  321. int _fstat(int fd, struct stat *st)
  322. {
  323. st->st_mode = S_IFCHR;
  324. return 0;
  325. }
  326. int _isatty(int fd)
  327. {
  328. return 1;
  329. }
  330. int _lseek(int fd, long long offset, int whence)
  331. {
  332. return -1;
  333. }
  334. void _exit(int status)
  335. {
  336. while (1);
  337. }
  338. void __cxa_pure_virtual()
  339. {
  340. while (1);
  341. }
  342. int __cxa_guard_acquire (int *g)
  343. {
  344. return 1;
  345. }
  346. void __cxa_guard_release(int *g)
  347. {
  348. }
  349. int nvic_execution_priority(void)
  350. {
  351. int priority=256;
  352. uint32_t primask, faultmask, basepri, ipsr;
  353. // full algorithm in ARM DDI0403D, page B1-639
  354. // this isn't quite complete, but hopefully good enough
  355. asm volatile("mrs %0, faultmask\n" : "=r" (faultmask)::);
  356. if (faultmask) return -1;
  357. asm volatile("mrs %0, primask\n" : "=r" (primask)::);
  358. if (primask) return 0;
  359. asm volatile("mrs %0, ipsr\n" : "=r" (ipsr)::);
  360. if (ipsr) {
  361. if (ipsr < 16) priority = 0; // could be non-zero
  362. else priority = NVIC_GET_PRIORITY(ipsr - 16);
  363. }
  364. asm volatile("mrs %0, basepri\n" : "=r" (basepri)::);
  365. if (basepri > 0 && basepri < priority) priority = basepri;
  366. return priority;
  367. }