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

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