You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

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