Teensy 4.1 core updated for C++20
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.

647 lines
20KB

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