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

650 行
20KB

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