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.

678 lines
21KB

  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. // 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_32B (SCB_MPU_RASR_SIZE(4) | SCB_MPU_RASR_ENABLE)
  153. #define SIZE_64B (SCB_MPU_RASR_SIZE(5) | SCB_MPU_RASR_ENABLE)
  154. #define SIZE_128B (SCB_MPU_RASR_SIZE(6) | SCB_MPU_RASR_ENABLE)
  155. #define SIZE_256B (SCB_MPU_RASR_SIZE(7) | SCB_MPU_RASR_ENABLE)
  156. #define SIZE_512B (SCB_MPU_RASR_SIZE(8) | SCB_MPU_RASR_ENABLE)
  157. #define SIZE_1K (SCB_MPU_RASR_SIZE(9) | SCB_MPU_RASR_ENABLE)
  158. #define SIZE_2K (SCB_MPU_RASR_SIZE(10) | SCB_MPU_RASR_ENABLE)
  159. #define SIZE_4K (SCB_MPU_RASR_SIZE(11) | SCB_MPU_RASR_ENABLE)
  160. #define SIZE_8K (SCB_MPU_RASR_SIZE(12) | SCB_MPU_RASR_ENABLE)
  161. #define SIZE_16K (SCB_MPU_RASR_SIZE(13) | SCB_MPU_RASR_ENABLE)
  162. #define SIZE_32K (SCB_MPU_RASR_SIZE(14) | SCB_MPU_RASR_ENABLE)
  163. #define SIZE_64K (SCB_MPU_RASR_SIZE(15) | SCB_MPU_RASR_ENABLE)
  164. #define SIZE_128K (SCB_MPU_RASR_SIZE(16) | SCB_MPU_RASR_ENABLE)
  165. #define SIZE_256K (SCB_MPU_RASR_SIZE(17) | SCB_MPU_RASR_ENABLE)
  166. #define SIZE_512K (SCB_MPU_RASR_SIZE(18) | SCB_MPU_RASR_ENABLE)
  167. #define SIZE_1M (SCB_MPU_RASR_SIZE(19) | SCB_MPU_RASR_ENABLE)
  168. #define SIZE_2M (SCB_MPU_RASR_SIZE(20) | SCB_MPU_RASR_ENABLE)
  169. #define SIZE_4M (SCB_MPU_RASR_SIZE(21) | SCB_MPU_RASR_ENABLE)
  170. #define SIZE_8M (SCB_MPU_RASR_SIZE(22) | SCB_MPU_RASR_ENABLE)
  171. #define SIZE_16M (SCB_MPU_RASR_SIZE(23) | SCB_MPU_RASR_ENABLE)
  172. #define SIZE_32M (SCB_MPU_RASR_SIZE(24) | SCB_MPU_RASR_ENABLE)
  173. #define SIZE_64M (SCB_MPU_RASR_SIZE(25) | SCB_MPU_RASR_ENABLE)
  174. #define SIZE_128M (SCB_MPU_RASR_SIZE(26) | SCB_MPU_RASR_ENABLE)
  175. #define SIZE_256M (SCB_MPU_RASR_SIZE(27) | SCB_MPU_RASR_ENABLE)
  176. #define SIZE_512M (SCB_MPU_RASR_SIZE(28) | SCB_MPU_RASR_ENABLE)
  177. #define SIZE_1G (SCB_MPU_RASR_SIZE(29) | SCB_MPU_RASR_ENABLE)
  178. #define SIZE_2G (SCB_MPU_RASR_SIZE(30) | SCB_MPU_RASR_ENABLE)
  179. #define SIZE_4G (SCB_MPU_RASR_SIZE(31) | SCB_MPU_RASR_ENABLE)
  180. #define REGION(n) (SCB_MPU_RBAR_REGION(n) | SCB_MPU_RBAR_VALID)
  181. FLASHMEM void configure_cache(void)
  182. {
  183. //printf("MPU_TYPE = %08lX\n", SCB_MPU_TYPE);
  184. //printf("CCR = %08lX\n", SCB_CCR);
  185. // TODO: check if caches already active - skip?
  186. SCB_MPU_CTRL = 0; // turn off MPU
  187. uint32_t i = 0;
  188. 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
  189. SCB_MPU_RASR = SCB_MPU_RASR_TEX(0) | NOACCESS | NOEXEC | SIZE_4G;
  190. SCB_MPU_RBAR = 0x00000000 | REGION(i++); // ITCM
  191. SCB_MPU_RASR = MEM_NOCACHE | READWRITE | SIZE_512K;
  192. SCB_MPU_RBAR = 0x00000000 | REGION(i++); // trap NULL pointer deref
  193. SCB_MPU_RASR = DEV_NOCACHE | NOACCESS | SIZE_32B;
  194. SCB_MPU_RBAR = 0x00200000 | REGION(i++); // Boot ROM
  195. SCB_MPU_RASR = MEM_CACHE_WT | READONLY | SIZE_128K;
  196. SCB_MPU_RBAR = 0x20000000 | REGION(i++); // DTCM
  197. SCB_MPU_RASR = MEM_NOCACHE | READWRITE | NOEXEC | SIZE_512K;
  198. SCB_MPU_RBAR = ((uint32_t)&_ebss) | REGION(i++); // trap stack overflow
  199. SCB_MPU_RASR = SCB_MPU_RASR_TEX(0) | NOACCESS | NOEXEC | SIZE_32B;
  200. SCB_MPU_RBAR = 0x20200000 | REGION(i++); // RAM (AXI bus)
  201. SCB_MPU_RASR = MEM_CACHE_WBWA | READWRITE | NOEXEC | SIZE_1M;
  202. SCB_MPU_RBAR = 0x40000000 | REGION(i++); // Peripherals
  203. SCB_MPU_RASR = DEV_NOCACHE | READWRITE | NOEXEC | SIZE_64M;
  204. SCB_MPU_RBAR = 0x60000000 | REGION(i++); // QSPI Flash
  205. SCB_MPU_RASR = MEM_CACHE_WBWA | READONLY | SIZE_16M;
  206. SCB_MPU_RBAR = 0x70000000 | REGION(i++); // FlexSPI2
  207. SCB_MPU_RASR = MEM_CACHE_WBWA | READONLY | SIZE_256M;
  208. SCB_MPU_RBAR = 0x70000000 | REGION(i++); // FlexSPI2
  209. SCB_MPU_RASR = MEM_CACHE_WBWA | READWRITE | SIZE_16M;
  210. // TODO: protect access to power supply config
  211. SCB_MPU_CTRL = SCB_MPU_CTRL_ENABLE;
  212. // cache enable, ARM DDI0403E, pg 628
  213. asm("dsb");
  214. asm("isb");
  215. SCB_CACHE_ICIALLU = 0;
  216. asm("dsb");
  217. asm("isb");
  218. SCB_CCR |= (SCB_CCR_IC | SCB_CCR_DC);
  219. }
  220. FLASHMEM void usb_pll_start()
  221. {
  222. while (1) {
  223. uint32_t n = CCM_ANALOG_PLL_USB1; // pg 759
  224. printf("CCM_ANALOG_PLL_USB1=%08lX\n", n);
  225. if (n & CCM_ANALOG_PLL_USB1_DIV_SELECT) {
  226. printf(" ERROR, 528 MHz mode!\n"); // never supposed to use this mode!
  227. CCM_ANALOG_PLL_USB1_CLR = 0xC000; // bypass 24 MHz
  228. CCM_ANALOG_PLL_USB1_SET = CCM_ANALOG_PLL_USB1_BYPASS; // bypass
  229. CCM_ANALOG_PLL_USB1_CLR = CCM_ANALOG_PLL_USB1_POWER | // power down
  230. CCM_ANALOG_PLL_USB1_DIV_SELECT | // use 480 MHz
  231. CCM_ANALOG_PLL_USB1_ENABLE | // disable
  232. CCM_ANALOG_PLL_USB1_EN_USB_CLKS; // disable usb
  233. continue;
  234. }
  235. if (!(n & CCM_ANALOG_PLL_USB1_ENABLE)) {
  236. printf(" enable PLL\n");
  237. // TODO: should this be done so early, or later??
  238. CCM_ANALOG_PLL_USB1_SET = CCM_ANALOG_PLL_USB1_ENABLE;
  239. continue;
  240. }
  241. if (!(n & CCM_ANALOG_PLL_USB1_POWER)) {
  242. printf(" power up PLL\n");
  243. CCM_ANALOG_PLL_USB1_SET = CCM_ANALOG_PLL_USB1_POWER;
  244. continue;
  245. }
  246. if (!(n & CCM_ANALOG_PLL_USB1_LOCK)) {
  247. printf(" wait for lock\n");
  248. continue;
  249. }
  250. if (n & CCM_ANALOG_PLL_USB1_BYPASS) {
  251. printf(" turn off bypass\n");
  252. CCM_ANALOG_PLL_USB1_CLR = CCM_ANALOG_PLL_USB1_BYPASS;
  253. continue;
  254. }
  255. if (!(n & CCM_ANALOG_PLL_USB1_EN_USB_CLKS)) {
  256. printf(" enable USB clocks\n");
  257. CCM_ANALOG_PLL_USB1_SET = CCM_ANALOG_PLL_USB1_EN_USB_CLKS;
  258. continue;
  259. }
  260. return; // everything is as it should be :-)
  261. }
  262. }
  263. FLASHMEM void reset_PFD()
  264. {
  265. //Reset PLL2 PFDs, set default frequencies:
  266. CCM_ANALOG_PFD_528_SET = (1 << 31) | (1 << 23) | (1 << 15) | (1 << 7);
  267. CCM_ANALOG_PFD_528 = 0x2018101B; // PFD0:352, PFD1:594, PFD2:396, PFD3:297 MHz
  268. //PLL3:
  269. CCM_ANALOG_PFD_480_SET = (1 << 31) | (1 << 23) | (1 << 15) | (1 << 7);
  270. CCM_ANALOG_PFD_480 = 0x13110D0C; // PFD0:720, PFD1:664, PFD2:508, PFD3:454 MHz
  271. }
  272. // Stack frame
  273. // xPSR
  274. // ReturnAddress
  275. // LR (R14) - typically FFFFFFF9 for IRQ or Exception
  276. // R12
  277. // R3
  278. // R2
  279. // R1
  280. // R0
  281. // Code from :: https://community.nxp.com/thread/389002
  282. __attribute__((naked))
  283. void unused_interrupt_vector(void)
  284. {
  285. __asm( ".syntax unified\n"
  286. "MOVS R0, #4 \n"
  287. "MOV R1, LR \n"
  288. "TST R0, R1 \n"
  289. "BEQ _MSP \n"
  290. "MRS R0, PSP \n"
  291. "B HardFault_HandlerC \n"
  292. "_MSP: \n"
  293. "MRS R0, MSP \n"
  294. "B HardFault_HandlerC \n"
  295. ".syntax divided\n") ;
  296. }
  297. __attribute__((weak))
  298. void HardFault_HandlerC(unsigned int *hardfault_args)
  299. {
  300. volatile unsigned int nn ;
  301. #ifdef PRINT_DEBUG_STUFF
  302. volatile unsigned int stacked_r0 ;
  303. volatile unsigned int stacked_r1 ;
  304. volatile unsigned int stacked_r2 ;
  305. volatile unsigned int stacked_r3 ;
  306. volatile unsigned int stacked_r12 ;
  307. volatile unsigned int stacked_lr ;
  308. volatile unsigned int stacked_pc ;
  309. volatile unsigned int stacked_psr ;
  310. volatile unsigned int _CFSR ;
  311. volatile unsigned int _HFSR ;
  312. volatile unsigned int _DFSR ;
  313. volatile unsigned int _AFSR ;
  314. volatile unsigned int _BFAR ;
  315. volatile unsigned int _MMAR ;
  316. volatile unsigned int addr ;
  317. stacked_r0 = ((unsigned int)hardfault_args[0]) ;
  318. stacked_r1 = ((unsigned int)hardfault_args[1]) ;
  319. stacked_r2 = ((unsigned int)hardfault_args[2]) ;
  320. stacked_r3 = ((unsigned int)hardfault_args[3]) ;
  321. stacked_r12 = ((unsigned int)hardfault_args[4]) ;
  322. stacked_lr = ((unsigned int)hardfault_args[5]) ;
  323. stacked_pc = ((unsigned int)hardfault_args[6]) ;
  324. stacked_psr = ((unsigned int)hardfault_args[7]) ;
  325. // Configurable Fault Status Register
  326. // Consists of MMSR, BFSR and UFSR
  327. //(n & ( 1 << k )) >> k
  328. _CFSR = (*((volatile unsigned int *)(0xE000ED28))) ;
  329. // Hard Fault Status Register
  330. _HFSR = (*((volatile unsigned int *)(0xE000ED2C))) ;
  331. // Debug Fault Status Register
  332. _DFSR = (*((volatile unsigned int *)(0xE000ED30))) ;
  333. // Auxiliary Fault Status Register
  334. _AFSR = (*((volatile unsigned int *)(0xE000ED3C))) ;
  335. // Read the Fault Address Registers. These may not contain valid values.
  336. // Check BFARVALID/MMARVALID to see if they are valid values
  337. // MemManage Fault Address Register
  338. _MMAR = (*((volatile unsigned int *)(0xE000ED34))) ;
  339. // Bus Fault Address Register
  340. _BFAR = (*((volatile unsigned int *)(0xE000ED38))) ;
  341. //__asm("BKPT #0\n") ; // Break into the debugger // NO Debugger here.
  342. asm volatile("mrs %0, ipsr\n" : "=r" (addr)::);
  343. printf("\nFault irq %d\n", addr & 0x1FF);
  344. printf(" stacked_r0 :: %x\n", stacked_r0);
  345. printf(" stacked_r1 :: %x\n", stacked_r1);
  346. printf(" stacked_r2 :: %x\n", stacked_r2);
  347. printf(" stacked_r3 :: %x\n", stacked_r3);
  348. printf(" stacked_r12 :: %x\n", stacked_r12);
  349. printf(" stacked_lr :: %x\n", stacked_lr);
  350. printf(" stacked_pc :: %x\n", stacked_pc);
  351. printf(" stacked_psr :: %x\n", stacked_psr);
  352. printf(" _CFSR :: %x\n", _CFSR);
  353. if(_CFSR > 0){
  354. //Memory Management Faults
  355. if((_CFSR & 1) == 1){
  356. printf(" (IACCVIOL) Instruction Access Violation\n");
  357. } else if(((_CFSR & (0x02))>>1) == 1){
  358. printf(" (DACCVIOL) Data Access Violation\n");
  359. } else if(((_CFSR & (0x08))>>3) == 1){
  360. printf(" (MUNSTKERR) MemMange Fault on Unstacking\n");
  361. } else if(((_CFSR & (0x10))>>4) == 1){
  362. printf(" (MSTKERR) MemMange Fault on stacking\n");
  363. } else if(((_CFSR & (0x20))>>5) == 1){
  364. printf(" (MLSPERR) MemMange Fault on FP Lazy State\n");
  365. }
  366. if(((_CFSR & (0x80))>>7) == 1){
  367. printf(" (MMARVALID) MemMange Fault Address Valid\n");
  368. }
  369. //Bus Fault Status Register
  370. if(((_CFSR & 0x100)>>8) == 1){
  371. printf(" (IBUSERR) Instruction Bus Error\n");
  372. } else if(((_CFSR & (0x200))>>9) == 1){
  373. printf(" (PRECISERR) Data bus error(address in BFAR)\n");
  374. } else if(((_CFSR & (0x400))>>10) == 1){
  375. printf(" (IMPRECISERR) Data bus error but address not related to instruction\n");
  376. } else if(((_CFSR & (0x800))>>11) == 1){
  377. printf(" (UNSTKERR) Bus Fault on unstacking for a return from exception \n");
  378. } else if(((_CFSR & (0x1000))>>12) == 1){
  379. printf(" (STKERR) Bus Fault on stacking for exception entry\n");
  380. } else if(((_CFSR & (0x2000))>>13) == 1){
  381. printf(" (LSPERR) Bus Fault on FP lazy state preservation\n");
  382. }
  383. if(((_CFSR & (0x8000))>>15) == 1){
  384. printf(" (BFARVALID) Bus Fault Address Valid\n");
  385. }
  386. //Usuage Fault Status Register
  387. if(((_CFSR & 0x10000)>>16) == 1){
  388. printf(" (UNDEFINSTR) Undefined instruction\n");
  389. } else if(((_CFSR & (0x20000))>>17) == 1){
  390. printf(" (INVSTATE) Instruction makes illegal use of EPSR)\n");
  391. } else if(((_CFSR & (0x40000))>>18) == 1){
  392. printf(" (INVPC) Usage fault: invalid EXC_RETURN\n");
  393. } else if(((_CFSR & (0x80000))>>19) == 1){
  394. printf(" (NOCP) No Coprocessor \n");
  395. } else if(((_CFSR & (0x1000000))>>24) == 1){
  396. printf(" (UNALIGNED) Unaligned access UsageFault\n");
  397. } else if(((_CFSR & (0x2000000))>>25) == 1){
  398. printf(" (DIVBYZERO) Divide by zero\n");
  399. }
  400. }
  401. printf(" _HFSR :: %x\n", _HFSR);
  402. if(_HFSR > 0){
  403. //Memory Management Faults
  404. if(((_HFSR & (0x02))>>1) == 1){
  405. printf(" (VECTTBL) Bus Fault on Vec Table Read\n");
  406. } else if(((_HFSR & (0x40000000))>>30) == 1){
  407. printf(" (FORCED) Forced Hard Fault\n");
  408. } else if(((_HFSR & (0x80000000))>>31) == 31){
  409. printf(" (DEBUGEVT) Reserved for Debug\n");
  410. }
  411. }
  412. printf(" _DFSR :: %x\n", _DFSR);
  413. printf(" _AFSR :: %x\n", _AFSR);
  414. printf(" _BFAR :: %x\n", _BFAR);
  415. printf(" _MMAR :: %x\n", _MMAR);
  416. #endif
  417. IOMUXC_SW_MUX_CTL_PAD_GPIO_B0_03 = 5; // pin 13
  418. IOMUXC_SW_PAD_CTL_PAD_GPIO_B0_03 = IOMUXC_PAD_DSE(7);
  419. GPIO2_GDIR |= (1 << 3);
  420. GPIO2_DR_SET = (1 << 3);
  421. GPIO2_DR_CLEAR = (1 << 3); //digitalWrite(13, LOW);
  422. if ( F_CPU_ACTUAL >= 600000000 )
  423. set_arm_clock(300000000);
  424. while (1)
  425. {
  426. GPIO2_DR_SET = (1 << 3); //digitalWrite(13, HIGH);
  427. // digitalWrite(13, HIGH);
  428. for (nn = 0; nn < 2000000/2; nn++) ;
  429. GPIO2_DR_CLEAR = (1 << 3); //digitalWrite(13, LOW);
  430. // digitalWrite(13, LOW);
  431. for (nn = 0; nn < 18000000/2; nn++) ;
  432. }
  433. }
  434. __attribute__((weak))
  435. void userDebugDump(){
  436. volatile unsigned int nn;
  437. printf("\nuserDebugDump() in startup.c ___ \n");
  438. while (1)
  439. {
  440. GPIO2_DR_SET = (1 << 3); //digitalWrite(13, HIGH);
  441. // digitalWrite(13, HIGH);
  442. for (nn = 0; nn < 2000000; nn++) ;
  443. GPIO2_DR_CLEAR = (1 << 3); //digitalWrite(13, LOW);
  444. // digitalWrite(13, LOW);
  445. for (nn = 0; nn < 18000000; nn++) ;
  446. GPIO2_DR_SET = (1 << 3); //digitalWrite(13, HIGH);
  447. // digitalWrite(13, HIGH);
  448. for (nn = 0; nn < 20000000; nn++) ;
  449. GPIO2_DR_CLEAR = (1 << 3); //digitalWrite(13, LOW);
  450. // digitalWrite(13, LOW);
  451. for (nn = 0; nn < 10000000; nn++) ;
  452. }
  453. }
  454. __attribute__((weak))
  455. void PJRCunused_interrupt_vector(void)
  456. {
  457. // TODO: polling Serial to complete buffered transmits
  458. #ifdef PRINT_DEBUG_STUFF
  459. uint32_t addr;
  460. asm volatile("mrs %0, ipsr\n" : "=r" (addr)::);
  461. printf("\nirq %d\n", addr & 0x1FF);
  462. asm("ldr %0, [sp, #52]" : "=r" (addr) ::);
  463. printf(" %x\n", addr);
  464. asm("ldr %0, [sp, #48]" : "=r" (addr) ::);
  465. printf(" %x\n", addr);
  466. asm("ldr %0, [sp, #44]" : "=r" (addr) ::);
  467. printf(" %x\n", addr);
  468. asm("ldr %0, [sp, #40]" : "=r" (addr) ::);
  469. printf(" %x\n", addr);
  470. asm("ldr %0, [sp, #36]" : "=r" (addr) ::);
  471. printf(" %x\n", addr);
  472. asm("ldr %0, [sp, #33]" : "=r" (addr) ::);
  473. printf(" %x\n", addr);
  474. asm("ldr %0, [sp, #34]" : "=r" (addr) ::);
  475. printf(" %x\n", addr);
  476. asm("ldr %0, [sp, #28]" : "=r" (addr) ::);
  477. printf(" %x\n", addr);
  478. asm("ldr %0, [sp, #24]" : "=r" (addr) ::);
  479. printf(" %x\n", addr);
  480. asm("ldr %0, [sp, #20]" : "=r" (addr) ::);
  481. printf(" %x\n", addr);
  482. asm("ldr %0, [sp, #16]" : "=r" (addr) ::);
  483. printf(" %x\n", addr);
  484. asm("ldr %0, [sp, #12]" : "=r" (addr) ::);
  485. printf(" %x\n", addr);
  486. asm("ldr %0, [sp, #8]" : "=r" (addr) ::);
  487. printf(" %x\n", addr);
  488. asm("ldr %0, [sp, #4]" : "=r" (addr) ::);
  489. printf(" %x\n", addr);
  490. asm("ldr %0, [sp, #0]" : "=r" (addr) ::);
  491. printf(" %x\n", addr);
  492. #endif
  493. #if 1
  494. if ( F_CPU_ACTUAL >= 600000000 )
  495. set_arm_clock(100000000);
  496. IOMUXC_SW_MUX_CTL_PAD_GPIO_B0_03 = 5; // pin 13
  497. IOMUXC_SW_PAD_CTL_PAD_GPIO_B0_03 = IOMUXC_PAD_DSE(7);
  498. GPIO2_GDIR |= (1<<3);
  499. GPIO2_DR_SET = (1<<3);
  500. while (1) {
  501. volatile uint32_t n;
  502. GPIO2_DR_SET = (1<<3); //digitalWrite(13, HIGH);
  503. for (n=0; n < 2000000/6; n++) ;
  504. GPIO2_DR_CLEAR = (1<<3); //digitalWrite(13, LOW);
  505. for (n=0; n < 1500000/6; n++) ;
  506. }
  507. #else
  508. if ( F_CPU_ACTUAL >= 600000000 )
  509. set_arm_clock(100000000);
  510. while (1) asm ("WFI");
  511. #endif
  512. }
  513. __attribute__((section(".startup"), optimize("no-tree-loop-distribute-patterns")))
  514. static void memory_copy(uint32_t *dest, const uint32_t *src, uint32_t *dest_end)
  515. {
  516. if (dest == src) return;
  517. while (dest < dest_end) {
  518. *dest++ = *src++;
  519. }
  520. }
  521. __attribute__((section(".startup"), optimize("no-tree-loop-distribute-patterns")))
  522. static void memory_clear(uint32_t *dest, uint32_t *dest_end)
  523. {
  524. while (dest < dest_end) {
  525. *dest++ = 0;
  526. }
  527. }
  528. // syscall functions need to be in the same C file as the entry point "ResetVector"
  529. // otherwise the linker will discard them in some cases.
  530. #include <errno.h>
  531. // from the linker script
  532. extern unsigned long _heap_start;
  533. extern unsigned long _heap_end;
  534. char *__brkval = (char *)&_heap_start;
  535. void * _sbrk(int incr)
  536. {
  537. char *prev = __brkval;
  538. if (incr != 0) {
  539. if (prev + incr > (char *)&_heap_end) {
  540. errno = ENOMEM;
  541. return (void *)-1;
  542. }
  543. __brkval = prev + incr;
  544. }
  545. return prev;
  546. }
  547. __attribute__((weak))
  548. int _read(int file, char *ptr, int len)
  549. {
  550. return 0;
  551. }
  552. __attribute__((weak))
  553. int _close(int fd)
  554. {
  555. return -1;
  556. }
  557. #include <sys/stat.h>
  558. __attribute__((weak))
  559. int _fstat(int fd, struct stat *st)
  560. {
  561. st->st_mode = S_IFCHR;
  562. return 0;
  563. }
  564. __attribute__((weak))
  565. int _isatty(int fd)
  566. {
  567. return 1;
  568. }
  569. __attribute__((weak))
  570. int _lseek(int fd, long long offset, int whence)
  571. {
  572. return -1;
  573. }
  574. __attribute__((weak))
  575. void _exit(int status)
  576. {
  577. while (1) asm ("WFI");
  578. }
  579. __attribute__((weak))
  580. void __cxa_pure_virtual()
  581. {
  582. while (1) asm ("WFI");
  583. }
  584. __attribute__((weak))
  585. int __cxa_guard_acquire (char *g)
  586. {
  587. return !(*g);
  588. }
  589. __attribute__((weak))
  590. void __cxa_guard_release(char *g)
  591. {
  592. *g = 1;
  593. }
  594. __attribute__((weak))
  595. void abort(void)
  596. {
  597. while (1) asm ("WFI");
  598. }