Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

619 lines
19KB

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