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.

681 lines
22KB

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