Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

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