Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

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