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

639 行
20KB

  1. #include "imxrt.h"
  2. #include "wiring.h"
  3. #include "usb_dev.h"
  4. #include "debug/printf.h"
  5. // from the linker
  6. extern unsigned long _stextload;
  7. extern unsigned long _stext;
  8. extern unsigned long _etext;
  9. extern unsigned long _sdataload;
  10. extern unsigned long _sdata;
  11. extern unsigned long _edata;
  12. extern unsigned long _sbss;
  13. extern unsigned long _ebss;
  14. extern unsigned long _flexram_bank_config;
  15. extern unsigned long _estack;
  16. __attribute__ ((used, aligned(1024)))
  17. void (* _VectorsRam[NVIC_NUM_INTERRUPTS+16])(void);
  18. static void memory_copy(uint32_t *dest, const uint32_t *src, uint32_t *dest_end);
  19. static void memory_clear(uint32_t *dest, uint32_t *dest_end);
  20. static void configure_systick(void);
  21. static void reset_PFD();
  22. extern void systick_isr(void);
  23. extern void pendablesrvreq_isr(void);
  24. void configure_cache(void);
  25. void unused_interrupt_vector(void);
  26. void usb_pll_start();
  27. extern void analog_init(void); // analog.c
  28. extern void pwm_init(void); // pwm.c
  29. extern void tempmon_init(void); //tempmon.c
  30. uint32_t set_arm_clock(uint32_t frequency); // clockspeed.c
  31. extern void __libc_init_array(void); // C++ standard library
  32. void startup_default_early_hook(void) {}
  33. void startup_early_hook(void) __attribute__ ((weak, alias("startup_default_early_hook")));
  34. void startup_default_late_hook(void) {}
  35. void startup_late_hook(void) __attribute__ ((weak, alias("startup_default_late_hook")));
  36. __attribute__((section(".startup"), optimize("no-tree-loop-distribute-patterns"), naked))
  37. void ResetHandler(void)
  38. {
  39. unsigned int i;
  40. #if defined(__IMXRT1062__)
  41. IOMUXC_GPR_GPR17 = (uint32_t)&_flexram_bank_config;
  42. IOMUXC_GPR_GPR16 = 0x00000007;
  43. IOMUXC_GPR_GPR14 = 0x00AA0000;
  44. __asm__ volatile("mov sp, %0" : : "r" ((uint32_t)&_estack) : );
  45. #endif
  46. // pin 13 - if startup crashes, use this to turn on the LED early for troubleshooting
  47. //IOMUXC_SW_MUX_CTL_PAD_GPIO_B0_03 = 5;
  48. //IOMUXC_SW_PAD_CTL_PAD_GPIO_B0_03 = IOMUXC_PAD_DSE(7);
  49. //IOMUXC_GPR_GPR27 = 0xFFFFFFFF;
  50. //GPIO7_GDIR |= (1<<3);
  51. //GPIO7_DR_SET = (1<<3); // digitalWrite(13, HIGH);
  52. // Initialize memory
  53. memory_copy(&_stext, &_stextload, &_etext);
  54. memory_copy(&_sdata, &_sdataload, &_edata);
  55. memory_clear(&_sbss, &_ebss);
  56. // enable FPU
  57. SCB_CPACR = 0x00F00000;
  58. // set up blank interrupt & exception vector table
  59. for (i=0; i < NVIC_NUM_INTERRUPTS + 16; i++) _VectorsRam[i] = &unused_interrupt_vector;
  60. for (i=0; i < NVIC_NUM_INTERRUPTS; i++) NVIC_SET_PRIORITY(i, 128);
  61. SCB_VTOR = (uint32_t)_VectorsRam;
  62. reset_PFD();
  63. // Configure clocks
  64. // TODO: make sure all affected peripherals are turned off!
  65. // PIT & GPT timers to run from 24 MHz clock (independent of CPU speed)
  66. CCM_CSCMR1 = (CCM_CSCMR1 & ~CCM_CSCMR1_PERCLK_PODF(0x3F)) | CCM_CSCMR1_PERCLK_CLK_SEL;
  67. // UARTs run from 24 MHz clock (works if PLL3 off or bypassed)
  68. CCM_CSCDR1 = (CCM_CSCDR1 & ~CCM_CSCDR1_UART_CLK_PODF(0x3F)) | CCM_CSCDR1_UART_CLK_SEL;
  69. #if defined(__IMXRT1062__)
  70. // Use fast GPIO6, GPIO7, GPIO8, GPIO9
  71. IOMUXC_GPR_GPR26 = 0xFFFFFFFF;
  72. IOMUXC_GPR_GPR27 = 0xFFFFFFFF;
  73. IOMUXC_GPR_GPR28 = 0xFFFFFFFF;
  74. IOMUXC_GPR_GPR29 = 0xFFFFFFFF;
  75. #endif
  76. // must enable PRINT_DEBUG_STUFF in debug/print.h
  77. printf_debug_init();
  78. printf("\n***********IMXRT Startup**********\n");
  79. printf("test %d %d %d\n", 1, -1234567, 3);
  80. configure_cache();
  81. configure_systick();
  82. usb_pll_start();
  83. reset_PFD(); //TODO: is this really needed?
  84. set_arm_clock(600000000);
  85. //set_arm_clock(984000000); Ludicrous Speed
  86. // initialize RTC
  87. if (!(SNVS_LPCR & SNVS_LPCR_SRTC_ENV)) {
  88. // if SRTC isn't running, start it with default Jan 1, 2019
  89. SNVS_LPSRTCLR = 1546300800u << 15;
  90. SNVS_LPSRTCMR = 1546300800u >> 17;
  91. SNVS_LPCR |= SNVS_LPCR_SRTC_ENV;
  92. }
  93. SNVS_HPCR |= SNVS_HPCR_RTC_EN | SNVS_HPCR_HP_TS;
  94. startup_early_hook();
  95. while (millis() < 20) ; // wait at least 20ms before starting USB
  96. usb_init();
  97. analog_init();
  98. pwm_init();
  99. tempmon_init();
  100. startup_late_hook();
  101. while (millis() < 300) ; // wait at least 300ms before calling user code
  102. //printf("before C++ constructors\n");
  103. __libc_init_array();
  104. //printf("after C++ constructors\n");
  105. //printf("before setup\n");
  106. setup();
  107. //printf("after setup\n");
  108. while (1) {
  109. //printf("loop\n");
  110. loop();
  111. yield();
  112. }
  113. }
  114. // ARM SysTick is used for most Ardiuno timing functions, delay(), millis(),
  115. // micros(). SysTick can run from either the ARM core clock, or from an
  116. // "external" clock. NXP documents it as "24 MHz XTALOSC can be the external
  117. // clock source of SYSTICK" (RT1052 ref manual, rev 1, page 411). However,
  118. // NXP actually hid an undocumented divide-by-240 circuit in the hardware, so
  119. // the external clock is really 100 kHz. We use this clock rather than the
  120. // ARM clock, to allow SysTick to maintain correct timing even when we change
  121. // the ARM clock to run at different speeds.
  122. #define SYSTICK_EXT_FREQ 100000
  123. extern volatile uint32_t systick_cycle_count;
  124. static void configure_systick(void)
  125. {
  126. _VectorsRam[14] = pendablesrvreq_isr;
  127. _VectorsRam[15] = systick_isr;
  128. SYST_RVR = (SYSTICK_EXT_FREQ / 1000) - 1;
  129. SYST_CVR = 0;
  130. SYST_CSR = SYST_CSR_TICKINT | SYST_CSR_ENABLE;
  131. SCB_SHPR3 = 0x20200000; // Systick, pendablesrvreq_isr = priority 32;
  132. ARM_DEMCR |= ARM_DEMCR_TRCENA;
  133. ARM_DWT_CTRL |= ARM_DWT_CTRL_CYCCNTENA; // turn on cycle counter
  134. systick_cycle_count = ARM_DWT_CYCCNT; // compiled 0, corrected w/1st systick
  135. }
  136. // concise defines for SCB_MPU_RASR and SCB_MPU_RBAR, ARM DDI0403E, pg 696
  137. #define NOEXEC SCB_MPU_RASR_XN
  138. #define READONLY SCB_MPU_RASR_AP(7)
  139. #define READWRITE SCB_MPU_RASR_AP(3)
  140. #define NOACCESS SCB_MPU_RASR_AP(0)
  141. #define MEM_CACHE_WT SCB_MPU_RASR_TEX(0) | SCB_MPU_RASR_C
  142. #define MEM_CACHE_WB SCB_MPU_RASR_TEX(0) | SCB_MPU_RASR_C | SCB_MPU_RASR_B
  143. #define MEM_CACHE_WBWA SCB_MPU_RASR_TEX(1) | SCB_MPU_RASR_C | SCB_MPU_RASR_B
  144. #define MEM_NOCACHE SCB_MPU_RASR_TEX(1)
  145. #define DEV_NOCACHE SCB_MPU_RASR_TEX(2)
  146. #define SIZE_128K (SCB_MPU_RASR_SIZE(16) | SCB_MPU_RASR_ENABLE)
  147. #define SIZE_256K (SCB_MPU_RASR_SIZE(17) | SCB_MPU_RASR_ENABLE)
  148. #define SIZE_512K (SCB_MPU_RASR_SIZE(18) | SCB_MPU_RASR_ENABLE)
  149. #define SIZE_1M (SCB_MPU_RASR_SIZE(19) | SCB_MPU_RASR_ENABLE)
  150. #define SIZE_2M (SCB_MPU_RASR_SIZE(20) | SCB_MPU_RASR_ENABLE)
  151. #define SIZE_4M (SCB_MPU_RASR_SIZE(21) | SCB_MPU_RASR_ENABLE)
  152. #define SIZE_8M (SCB_MPU_RASR_SIZE(22) | SCB_MPU_RASR_ENABLE)
  153. #define SIZE_16M (SCB_MPU_RASR_SIZE(23) | SCB_MPU_RASR_ENABLE)
  154. #define SIZE_32M (SCB_MPU_RASR_SIZE(24) | SCB_MPU_RASR_ENABLE)
  155. #define SIZE_64M (SCB_MPU_RASR_SIZE(25) | SCB_MPU_RASR_ENABLE)
  156. #define REGION(n) (SCB_MPU_RBAR_REGION(n) | SCB_MPU_RBAR_VALID)
  157. __attribute__((section(".progmem")))
  158. void configure_cache(void)
  159. {
  160. //printf("MPU_TYPE = %08lX\n", SCB_MPU_TYPE);
  161. //printf("CCR = %08lX\n", SCB_CCR);
  162. // TODO: check if caches already active - skip?
  163. SCB_MPU_CTRL = 0; // turn off MPU
  164. SCB_MPU_RBAR = 0x00000000 | REGION(0); // ITCM
  165. SCB_MPU_RASR = MEM_NOCACHE | READWRITE | SIZE_512K;
  166. SCB_MPU_RBAR = 0x00200000 | REGION(1); // Boot ROM
  167. SCB_MPU_RASR = MEM_CACHE_WT | READONLY | SIZE_128K;
  168. SCB_MPU_RBAR = 0x20000000 | REGION(2); // DTCM
  169. SCB_MPU_RASR = MEM_NOCACHE | READWRITE | NOEXEC | SIZE_512K;
  170. SCB_MPU_RBAR = 0x20200000 | REGION(3); // RAM (AXI bus)
  171. SCB_MPU_RASR = MEM_CACHE_WBWA | READWRITE | NOEXEC | SIZE_1M;
  172. SCB_MPU_RBAR = 0x40000000 | REGION(4); // Peripherals
  173. SCB_MPU_RASR = DEV_NOCACHE | READWRITE | NOEXEC | SIZE_64M;
  174. SCB_MPU_RBAR = 0x60000000 | REGION(5); // QSPI Flash
  175. SCB_MPU_RASR = MEM_CACHE_WBWA | READONLY | SIZE_16M;
  176. // TODO: 32 byte sub-region at 0x00000000 with NOACCESS, to trap NULL pointer deref
  177. // TODO: protect access to power supply config
  178. // TODO: 32 byte sub-region at end of .bss section with NOACCESS, to trap stack overflow
  179. SCB_MPU_CTRL = SCB_MPU_CTRL_ENABLE;
  180. // cache enable, ARM DDI0403E, pg 628
  181. asm("dsb");
  182. asm("isb");
  183. SCB_CACHE_ICIALLU = 0;
  184. asm("dsb");
  185. asm("isb");
  186. SCB_CCR |= (SCB_CCR_IC | SCB_CCR_DC);
  187. }
  188. __attribute__((section(".progmem")))
  189. void usb_pll_start()
  190. {
  191. while (1) {
  192. uint32_t n = CCM_ANALOG_PLL_USB1; // pg 759
  193. printf("CCM_ANALOG_PLL_USB1=%08lX\n", n);
  194. if (n & CCM_ANALOG_PLL_USB1_DIV_SELECT) {
  195. printf(" ERROR, 528 MHz mode!\n"); // never supposed to use this mode!
  196. CCM_ANALOG_PLL_USB1_CLR = 0xC000; // bypass 24 MHz
  197. CCM_ANALOG_PLL_USB1_SET = CCM_ANALOG_PLL_USB1_BYPASS; // bypass
  198. CCM_ANALOG_PLL_USB1_CLR = CCM_ANALOG_PLL_USB1_POWER | // power down
  199. CCM_ANALOG_PLL_USB1_DIV_SELECT | // use 480 MHz
  200. CCM_ANALOG_PLL_USB1_ENABLE | // disable
  201. CCM_ANALOG_PLL_USB1_EN_USB_CLKS; // disable usb
  202. continue;
  203. }
  204. if (!(n & CCM_ANALOG_PLL_USB1_ENABLE)) {
  205. printf(" enable PLL\n");
  206. // TODO: should this be done so early, or later??
  207. CCM_ANALOG_PLL_USB1_SET = CCM_ANALOG_PLL_USB1_ENABLE;
  208. continue;
  209. }
  210. if (!(n & CCM_ANALOG_PLL_USB1_POWER)) {
  211. printf(" power up PLL\n");
  212. CCM_ANALOG_PLL_USB1_SET = CCM_ANALOG_PLL_USB1_POWER;
  213. continue;
  214. }
  215. if (!(n & CCM_ANALOG_PLL_USB1_LOCK)) {
  216. printf(" wait for lock\n");
  217. continue;
  218. }
  219. if (n & CCM_ANALOG_PLL_USB1_BYPASS) {
  220. printf(" turn off bypass\n");
  221. CCM_ANALOG_PLL_USB1_CLR = CCM_ANALOG_PLL_USB1_BYPASS;
  222. continue;
  223. }
  224. if (!(n & CCM_ANALOG_PLL_USB1_EN_USB_CLKS)) {
  225. printf(" enable USB clocks\n");
  226. CCM_ANALOG_PLL_USB1_SET = CCM_ANALOG_PLL_USB1_EN_USB_CLKS;
  227. continue;
  228. }
  229. return; // everything is as it should be :-)
  230. }
  231. }
  232. __attribute__((section(".progmem")))
  233. void reset_PFD()
  234. {
  235. //Reset PLL2 PFDs, set default frequencies:
  236. CCM_ANALOG_PFD_528_SET = (1 << 31) | (1 << 23) | (1 << 15) | (1 << 7);
  237. CCM_ANALOG_PFD_528 = 0x2018101B; // PFD0:352, PFD1:594, PFD2:396, PFD3:297 MHz
  238. //PLL3:
  239. CCM_ANALOG_PFD_480_SET = (1 << 31) | (1 << 23) | (1 << 15) | (1 << 7);
  240. CCM_ANALOG_PFD_480 = 0x13110D0C; // PFD0:720, PFD1:664, PFD2:508, PFD3:454 MHz
  241. }
  242. // Stack frame
  243. // xPSR
  244. // ReturnAddress
  245. // LR (R14) - typically FFFFFFF9 for IRQ or Exception
  246. // R12
  247. // R3
  248. // R2
  249. // R1
  250. // R0
  251. // Code from :: https://community.nxp.com/thread/389002
  252. __attribute__((naked))
  253. void unused_interrupt_vector(void)
  254. {
  255. __asm( ".syntax unified\n"
  256. "MOVS R0, #4 \n"
  257. "MOV R1, LR \n"
  258. "TST R0, R1 \n"
  259. "BEQ _MSP \n"
  260. "MRS R0, PSP \n"
  261. "B HardFault_HandlerC \n"
  262. "_MSP: \n"
  263. "MRS R0, MSP \n"
  264. "B HardFault_HandlerC \n"
  265. ".syntax divided\n") ;
  266. }
  267. __attribute__((weak))
  268. void HardFault_HandlerC(unsigned int *hardfault_args) {
  269. volatile unsigned int stacked_r0 ;
  270. volatile unsigned int stacked_r1 ;
  271. volatile unsigned int stacked_r2 ;
  272. volatile unsigned int stacked_r3 ;
  273. volatile unsigned int stacked_r12 ;
  274. volatile unsigned int stacked_lr ;
  275. volatile unsigned int stacked_pc ;
  276. volatile unsigned int stacked_psr ;
  277. volatile unsigned int _CFSR ;
  278. volatile unsigned int _HFSR ;
  279. volatile unsigned int _DFSR ;
  280. volatile unsigned int _AFSR ;
  281. volatile unsigned int _BFAR ;
  282. volatile unsigned int _MMAR ;
  283. volatile unsigned int addr ;
  284. volatile unsigned int nn ;
  285. stacked_r0 = ((unsigned int)hardfault_args[0]) ;
  286. stacked_r1 = ((unsigned int)hardfault_args[1]) ;
  287. stacked_r2 = ((unsigned int)hardfault_args[2]) ;
  288. stacked_r3 = ((unsigned int)hardfault_args[3]) ;
  289. stacked_r12 = ((unsigned int)hardfault_args[4]) ;
  290. stacked_lr = ((unsigned int)hardfault_args[5]) ;
  291. stacked_pc = ((unsigned int)hardfault_args[6]) ;
  292. stacked_psr = ((unsigned int)hardfault_args[7]) ;
  293. // Configurable Fault Status Register
  294. // Consists of MMSR, BFSR and UFSR
  295. //(n & ( 1 << k )) >> k
  296. _CFSR = (*((volatile unsigned int *)(0xE000ED28))) ;
  297. // Hard Fault Status Register
  298. _HFSR = (*((volatile unsigned int *)(0xE000ED2C))) ;
  299. // Debug Fault Status Register
  300. _DFSR = (*((volatile unsigned int *)(0xE000ED30))) ;
  301. // Auxiliary Fault Status Register
  302. _AFSR = (*((volatile unsigned int *)(0xE000ED3C))) ;
  303. // Read the Fault Address Registers. These may not contain valid values.
  304. // Check BFARVALID/MMARVALID to see if they are valid values
  305. // MemManage Fault Address Register
  306. _MMAR = (*((volatile unsigned int *)(0xE000ED34))) ;
  307. // Bus Fault Address Register
  308. _BFAR = (*((volatile unsigned int *)(0xE000ED38))) ;
  309. //__asm("BKPT #0\n") ; // Break into the debugger // NO Debugger here.
  310. asm volatile("mrs %0, ipsr\n" : "=r" (addr)::);
  311. printf("\nFault irq %d\n", addr & 0x1FF);
  312. printf(" stacked_r0 :: %x\n", stacked_r0);
  313. printf(" stacked_r1 :: %x\n", stacked_r1);
  314. printf(" stacked_r2 :: %x\n", stacked_r2);
  315. printf(" stacked_r3 :: %x\n", stacked_r3);
  316. printf(" stacked_r12 :: %x\n", stacked_r12);
  317. printf(" stacked_lr :: %x\n", stacked_lr);
  318. printf(" stacked_pc :: %x\n", stacked_pc);
  319. printf(" stacked_psr :: %x\n", stacked_psr);
  320. printf(" _CFSR :: %x\n", _CFSR);
  321. if(_CFSR > 0){
  322. //Memory Management Faults
  323. if((_CFSR & 1) == 1){
  324. printf(" (IACCVIOL) Instruction Access Violation\n");
  325. } else if(((_CFSR & (0x02))>>1) == 1){
  326. printf(" (DACCVIOL) Data Access Violation\n");
  327. } else if(((_CFSR & (0x08))>>3) == 1){
  328. printf(" (MUNSTKERR) MemMange Fault on Unstacking\n");
  329. } else if(((_CFSR & (0x10))>>4) == 1){
  330. printf(" (MSTKERR) MemMange Fault on stacking\n");
  331. } else if(((_CFSR & (0x20))>>5) == 1){
  332. printf(" (MLSPERR) MemMange Fault on FP Lazy State\n");
  333. }
  334. if(((_CFSR & (0x80))>>7) == 1){
  335. printf(" (MMARVALID) MemMange Fault Address Valid\n");
  336. }
  337. //Bus Fault Status Register
  338. if(((_CFSR & 0x100)>>8) == 1){
  339. printf(" (IBUSERR) Instruction Bus Error\n");
  340. } else if(((_CFSR & (0x200))>>9) == 1){
  341. printf(" (PRECISERR) Data bus error(address in BFAR)\n");
  342. } else if(((_CFSR & (0x400))>>10) == 1){
  343. printf(" (IMPRECISERR) Data bus error but address not related to instruction\n");
  344. } else if(((_CFSR & (0x800))>>11) == 1){
  345. printf(" (UNSTKERR) Bus Fault on unstacking for a return from exception \n");
  346. } else if(((_CFSR & (0x1000))>>12) == 1){
  347. printf(" (STKERR) Bus Fault on stacking for exception entry\n");
  348. } else if(((_CFSR & (0x2000))>>13) == 1){
  349. printf(" (LSPERR) Bus Fault on FP lazy state preservation\n");
  350. }
  351. if(((_CFSR & (0x8000))>>15) == 1){
  352. printf(" (BFARVALID) Bus Fault Address Valid\n");
  353. }
  354. //Usuage Fault Status Register
  355. if(((_CFSR & 0x10000)>>16) == 1){
  356. printf(" (UNDEFINSTR) Undefined instruction\n");
  357. } else if(((_CFSR & (0x20000))>>17) == 1){
  358. printf(" (INVSTATE) Instruction makes illegal use of EPSR)\n");
  359. } else if(((_CFSR & (0x40000))>>18) == 1){
  360. printf(" (INVPC) Usage fault: invalid EXC_RETURN\n");
  361. } else if(((_CFSR & (0x80000))>>19) == 1){
  362. printf(" (NOCP) No Coprocessor \n");
  363. } else if(((_CFSR & (0x1000000))>>24) == 1){
  364. printf(" (UNALIGNED) Unaligned access UsageFault\n");
  365. } else if(((_CFSR & (0x2000000))>>25) == 1){
  366. printf(" (DIVBYZERO) Divide by zero\n");
  367. }
  368. }
  369. printf(" _HFSR :: %x\n", _HFSR);
  370. if(_HFSR > 0){
  371. //Memory Management Faults
  372. if(((_HFSR & (0x02))>>1) == 1){
  373. printf(" (VECTTBL) Bus Fault on Vec Table Read\n");
  374. } else if(((_HFSR & (0x40000000))>>30) == 1){
  375. printf(" (FORCED) Forced Hard Fault\n");
  376. } else if(((_HFSR & (0x80000000))>>31) == 31){
  377. printf(" (DEBUGEVT) Reserved for Debug\n");
  378. }
  379. }
  380. printf(" _DFSR :: %x\n", _DFSR);
  381. printf(" _AFSR :: %x\n", _AFSR);
  382. printf(" _BFAR :: %x\n", _BFAR);
  383. printf(" _MMAR :: %x\n", _MMAR);
  384. IOMUXC_SW_MUX_CTL_PAD_GPIO_B0_03 = 5; // pin 13
  385. IOMUXC_SW_PAD_CTL_PAD_GPIO_B0_03 = IOMUXC_PAD_DSE(7);
  386. GPIO2_GDIR |= (1 << 3);
  387. GPIO2_DR_SET = (1 << 3);
  388. GPIO2_DR_CLEAR = (1 << 3); //digitalWrite(13, LOW);
  389. if ( F_CPU_ACTUAL >= 600000000 )
  390. set_arm_clock(300000000);
  391. while (1)
  392. {
  393. GPIO2_DR_SET = (1 << 3); //digitalWrite(13, HIGH);
  394. // digitalWrite(13, HIGH);
  395. for (nn = 0; nn < 2000000/2; nn++) ;
  396. GPIO2_DR_CLEAR = (1 << 3); //digitalWrite(13, LOW);
  397. // digitalWrite(13, LOW);
  398. for (nn = 0; nn < 18000000/2; nn++) ;
  399. }
  400. }
  401. __attribute__((weak))
  402. void userDebugDump(){
  403. volatile unsigned int nn;
  404. printf("\nuserDebugDump() in startup.c ___ \n");
  405. while (1)
  406. {
  407. GPIO2_DR_SET = (1 << 3); //digitalWrite(13, HIGH);
  408. // digitalWrite(13, HIGH);
  409. for (nn = 0; nn < 2000000; nn++) ;
  410. GPIO2_DR_CLEAR = (1 << 3); //digitalWrite(13, LOW);
  411. // digitalWrite(13, LOW);
  412. for (nn = 0; nn < 18000000; nn++) ;
  413. GPIO2_DR_SET = (1 << 3); //digitalWrite(13, HIGH);
  414. // digitalWrite(13, HIGH);
  415. for (nn = 0; nn < 20000000; nn++) ;
  416. GPIO2_DR_CLEAR = (1 << 3); //digitalWrite(13, LOW);
  417. // digitalWrite(13, LOW);
  418. for (nn = 0; nn < 10000000; nn++) ;
  419. }
  420. }
  421. __attribute__((weak))
  422. void PJRCunused_interrupt_vector(void)
  423. {
  424. // TODO: polling Serial to complete buffered transmits
  425. #ifdef PRINT_DEBUG_STUFF
  426. uint32_t addr;
  427. asm volatile("mrs %0, ipsr\n" : "=r" (addr)::);
  428. printf("\nirq %d\n", addr & 0x1FF);
  429. asm("ldr %0, [sp, #52]" : "=r" (addr) ::);
  430. printf(" %x\n", addr);
  431. asm("ldr %0, [sp, #48]" : "=r" (addr) ::);
  432. printf(" %x\n", addr);
  433. asm("ldr %0, [sp, #44]" : "=r" (addr) ::);
  434. printf(" %x\n", addr);
  435. asm("ldr %0, [sp, #40]" : "=r" (addr) ::);
  436. printf(" %x\n", addr);
  437. asm("ldr %0, [sp, #36]" : "=r" (addr) ::);
  438. printf(" %x\n", addr);
  439. asm("ldr %0, [sp, #33]" : "=r" (addr) ::);
  440. printf(" %x\n", addr);
  441. asm("ldr %0, [sp, #34]" : "=r" (addr) ::);
  442. printf(" %x\n", addr);
  443. asm("ldr %0, [sp, #28]" : "=r" (addr) ::);
  444. printf(" %x\n", addr);
  445. asm("ldr %0, [sp, #24]" : "=r" (addr) ::);
  446. printf(" %x\n", addr);
  447. asm("ldr %0, [sp, #20]" : "=r" (addr) ::);
  448. printf(" %x\n", addr);
  449. asm("ldr %0, [sp, #16]" : "=r" (addr) ::);
  450. printf(" %x\n", addr);
  451. asm("ldr %0, [sp, #12]" : "=r" (addr) ::);
  452. printf(" %x\n", addr);
  453. asm("ldr %0, [sp, #8]" : "=r" (addr) ::);
  454. printf(" %x\n", addr);
  455. asm("ldr %0, [sp, #4]" : "=r" (addr) ::);
  456. printf(" %x\n", addr);
  457. asm("ldr %0, [sp, #0]" : "=r" (addr) ::);
  458. printf(" %x\n", addr);
  459. #endif
  460. #if 1
  461. if ( F_CPU_ACTUAL >= 600000000 )
  462. set_arm_clock(100000000);
  463. IOMUXC_SW_MUX_CTL_PAD_GPIO_B0_03 = 5; // pin 13
  464. IOMUXC_SW_PAD_CTL_PAD_GPIO_B0_03 = IOMUXC_PAD_DSE(7);
  465. GPIO2_GDIR |= (1<<3);
  466. GPIO2_DR_SET = (1<<3);
  467. while (1) {
  468. volatile uint32_t n;
  469. GPIO2_DR_SET = (1<<3); //digitalWrite(13, HIGH);
  470. for (n=0; n < 2000000/6; n++) ;
  471. GPIO2_DR_CLEAR = (1<<3); //digitalWrite(13, LOW);
  472. for (n=0; n < 1500000/6; n++) ;
  473. }
  474. #else
  475. if ( F_CPU_ACTUAL >= 600000000 )
  476. set_arm_clock(100000000);
  477. while (1) asm ("WFI");
  478. #endif
  479. }
  480. __attribute__((section(".startup"), optimize("no-tree-loop-distribute-patterns")))
  481. static void memory_copy(uint32_t *dest, const uint32_t *src, uint32_t *dest_end)
  482. {
  483. if (dest == src) return;
  484. while (dest < dest_end) {
  485. *dest++ = *src++;
  486. }
  487. }
  488. __attribute__((section(".startup"), optimize("no-tree-loop-distribute-patterns")))
  489. static void memory_clear(uint32_t *dest, uint32_t *dest_end)
  490. {
  491. while (dest < dest_end) {
  492. *dest++ = 0;
  493. }
  494. }
  495. // syscall functions need to be in the same C file as the entry point "ResetVector"
  496. // otherwise the linker will discard them in some cases.
  497. #include <errno.h>
  498. // from the linker script
  499. extern unsigned long _heap_start;
  500. extern unsigned long _heap_end;
  501. char *__brkval = (char *)&_heap_start;
  502. void * _sbrk(int incr)
  503. {
  504. char *prev = __brkval;
  505. if (incr != 0) {
  506. if (prev + incr > (char *)&_heap_end) {
  507. errno = ENOMEM;
  508. return (void *)-1;
  509. }
  510. __brkval = prev + incr;
  511. }
  512. return prev;
  513. }
  514. __attribute__((weak))
  515. int _read(int file, char *ptr, int len)
  516. {
  517. return 0;
  518. }
  519. __attribute__((weak))
  520. int _close(int fd)
  521. {
  522. return -1;
  523. }
  524. #include <sys/stat.h>
  525. __attribute__((weak))
  526. int _fstat(int fd, struct stat *st)
  527. {
  528. st->st_mode = S_IFCHR;
  529. return 0;
  530. }
  531. __attribute__((weak))
  532. int _isatty(int fd)
  533. {
  534. return 1;
  535. }
  536. __attribute__((weak))
  537. int _lseek(int fd, long long offset, int whence)
  538. {
  539. return -1;
  540. }
  541. __attribute__((weak))
  542. void _exit(int status)
  543. {
  544. while (1) asm ("WFI");
  545. }
  546. __attribute__((weak))
  547. void __cxa_pure_virtual()
  548. {
  549. while (1) asm ("WFI");
  550. }
  551. __attribute__((weak))
  552. int __cxa_guard_acquire (char *g)
  553. {
  554. return !(*g);
  555. }
  556. __attribute__((weak))
  557. void __cxa_guard_release(char *g)
  558. {
  559. *g = 1;
  560. }
  561. __attribute__((weak))
  562. void abort(void)
  563. {
  564. while (1) asm ("WFI");
  565. }