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.

850 satır
29KB

  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 configure_external_ram(void);
  27. void unused_interrupt_vector(void);
  28. void usb_pll_start();
  29. extern void analog_init(void); // analog.c
  30. extern void pwm_init(void); // pwm.c
  31. extern void tempmon_init(void); //tempmon.c
  32. uint32_t set_arm_clock(uint32_t frequency); // clockspeed.c
  33. extern void __libc_init_array(void); // C++ standard library
  34. extern int main (void);
  35. void startup_default_early_hook(void) {}
  36. void startup_early_hook(void) __attribute__ ((weak, alias("startup_default_early_hook")));
  37. void startup_default_late_hook(void) {}
  38. void startup_late_hook(void) __attribute__ ((weak, alias("startup_default_late_hook")));
  39. __attribute__((section(".startup"), optimize("no-tree-loop-distribute-patterns"), naked))
  40. void ResetHandler(void)
  41. {
  42. unsigned int i;
  43. #if defined(__IMXRT1062__)
  44. IOMUXC_GPR_GPR17 = (uint32_t)&_flexram_bank_config;
  45. IOMUXC_GPR_GPR16 = 0x00200007;
  46. IOMUXC_GPR_GPR14 = 0x00AA0000;
  47. __asm__ volatile("mov sp, %0" : : "r" ((uint32_t)&_estack) : );
  48. #endif
  49. PMU_MISC0_SET = 1<<3; //Use bandgap-based bias currents for best performance (Page 1175)
  50. // pin 13 - if startup crashes, use this to turn on the LED early for troubleshooting
  51. //IOMUXC_SW_MUX_CTL_PAD_GPIO_B0_03 = 5;
  52. //IOMUXC_SW_PAD_CTL_PAD_GPIO_B0_03 = IOMUXC_PAD_DSE(7);
  53. //IOMUXC_GPR_GPR27 = 0xFFFFFFFF;
  54. //GPIO7_GDIR |= (1<<3);
  55. //GPIO7_DR_SET = (1<<3); // digitalWrite(13, HIGH);
  56. // Initialize memory
  57. memory_copy(&_stext, &_stextload, &_etext);
  58. memory_copy(&_sdata, &_sdataload, &_edata);
  59. memory_clear(&_sbss, &_ebss);
  60. // enable FPU
  61. SCB_CPACR = 0x00F00000;
  62. // set up blank interrupt & exception vector table
  63. for (i=0; i < NVIC_NUM_INTERRUPTS + 16; i++) _VectorsRam[i] = &unused_interrupt_vector;
  64. for (i=0; i < NVIC_NUM_INTERRUPTS; i++) NVIC_SET_PRIORITY(i, 128);
  65. SCB_VTOR = (uint32_t)_VectorsRam;
  66. reset_PFD();
  67. // Configure clocks
  68. // TODO: make sure all affected peripherals are turned off!
  69. // PIT & GPT timers to run from 24 MHz clock (independent of CPU speed)
  70. CCM_CSCMR1 = (CCM_CSCMR1 & ~CCM_CSCMR1_PERCLK_PODF(0x3F)) | CCM_CSCMR1_PERCLK_CLK_SEL;
  71. // UARTs run from 24 MHz clock (works if PLL3 off or bypassed)
  72. CCM_CSCDR1 = (CCM_CSCDR1 & ~CCM_CSCDR1_UART_CLK_PODF(0x3F)) | CCM_CSCDR1_UART_CLK_SEL;
  73. #if defined(__IMXRT1062__)
  74. // Use fast GPIO6, GPIO7, GPIO8, GPIO9
  75. IOMUXC_GPR_GPR26 = 0xFFFFFFFF;
  76. IOMUXC_GPR_GPR27 = 0xFFFFFFFF;
  77. IOMUXC_GPR_GPR28 = 0xFFFFFFFF;
  78. IOMUXC_GPR_GPR29 = 0xFFFFFFFF;
  79. #endif
  80. // must enable PRINT_DEBUG_STUFF in debug/print.h
  81. printf_debug_init();
  82. printf("\n***********IMXRT Startup**********\n");
  83. printf("test %d %d %d\n", 1, -1234567, 3);
  84. configure_cache();
  85. configure_systick();
  86. usb_pll_start();
  87. reset_PFD(); //TODO: is this really needed?
  88. #ifdef F_CPU
  89. set_arm_clock(F_CPU);
  90. #endif
  91. asm volatile("nop\n nop\n nop\n nop": : :"memory"); // why oh why?
  92. // Undo PIT timer usage by ROM startup
  93. CCM_CCGR1 |= CCM_CCGR1_PIT(CCM_CCGR_ON);
  94. PIT_MCR = 0;
  95. PIT_TCTRL0 = 0;
  96. PIT_TCTRL1 = 0;
  97. PIT_TCTRL2 = 0;
  98. PIT_TCTRL3 = 0;
  99. // initialize RTC
  100. if (!(SNVS_LPCR & SNVS_LPCR_SRTC_ENV)) {
  101. // if SRTC isn't running, start it with default Jan 1, 2019
  102. SNVS_LPSRTCLR = 1546300800u << 15;
  103. SNVS_LPSRTCMR = 1546300800u >> 17;
  104. SNVS_LPCR |= SNVS_LPCR_SRTC_ENV;
  105. }
  106. SNVS_HPCR |= SNVS_HPCR_RTC_EN | SNVS_HPCR_HP_TS;
  107. #ifdef ARDUINO_TEENSY41
  108. configure_external_ram();
  109. #endif
  110. startup_early_hook();
  111. while (millis() < 20) ; // wait at least 20ms before starting USB
  112. usb_init();
  113. analog_init();
  114. pwm_init();
  115. tempmon_init();
  116. startup_late_hook();
  117. while (millis() < 300) ; // wait at least 300ms before calling user code
  118. //printf("before C++ constructors\n");
  119. __libc_init_array();
  120. //printf("after C++ constructors\n");
  121. //printf("before setup\n");
  122. main();
  123. while (1) ;
  124. }
  125. // ARM SysTick is used for most Ardiuno timing functions, delay(), millis(),
  126. // micros(). SysTick can run from either the ARM core clock, or from an
  127. // "external" clock. NXP documents it as "24 MHz XTALOSC can be the external
  128. // clock source of SYSTICK" (RT1052 ref manual, rev 1, page 411). However,
  129. // NXP actually hid an undocumented divide-by-240 circuit in the hardware, so
  130. // the external clock is really 100 kHz. We use this clock rather than the
  131. // ARM clock, to allow SysTick to maintain correct timing even when we change
  132. // the ARM clock to run at different speeds.
  133. #define SYSTICK_EXT_FREQ 100000
  134. extern volatile uint32_t systick_cycle_count;
  135. static void configure_systick(void)
  136. {
  137. _VectorsRam[14] = pendablesrvreq_isr;
  138. _VectorsRam[15] = systick_isr;
  139. SYST_RVR = (SYSTICK_EXT_FREQ / 1000) - 1;
  140. SYST_CVR = 0;
  141. SYST_CSR = SYST_CSR_TICKINT | SYST_CSR_ENABLE;
  142. SCB_SHPR3 = 0x20200000; // Systick, pendablesrvreq_isr = priority 32;
  143. ARM_DEMCR |= ARM_DEMCR_TRCENA;
  144. ARM_DWT_CTRL |= ARM_DWT_CTRL_CYCCNTENA; // turn on cycle counter
  145. systick_cycle_count = ARM_DWT_CYCCNT; // compiled 0, corrected w/1st systick
  146. }
  147. // concise defines for SCB_MPU_RASR and SCB_MPU_RBAR, ARM DDI0403E, pg 696
  148. #define NOEXEC SCB_MPU_RASR_XN
  149. #define READONLY SCB_MPU_RASR_AP(7)
  150. #define READWRITE SCB_MPU_RASR_AP(3)
  151. #define NOACCESS SCB_MPU_RASR_AP(0)
  152. #define MEM_CACHE_WT SCB_MPU_RASR_TEX(0) | SCB_MPU_RASR_C
  153. #define MEM_CACHE_WB SCB_MPU_RASR_TEX(0) | SCB_MPU_RASR_C | SCB_MPU_RASR_B
  154. #define MEM_CACHE_WBWA SCB_MPU_RASR_TEX(1) | SCB_MPU_RASR_C | SCB_MPU_RASR_B
  155. #define MEM_NOCACHE SCB_MPU_RASR_TEX(1)
  156. #define DEV_NOCACHE SCB_MPU_RASR_TEX(2)
  157. #define SIZE_32B (SCB_MPU_RASR_SIZE(4) | SCB_MPU_RASR_ENABLE)
  158. #define SIZE_64B (SCB_MPU_RASR_SIZE(5) | SCB_MPU_RASR_ENABLE)
  159. #define SIZE_128B (SCB_MPU_RASR_SIZE(6) | SCB_MPU_RASR_ENABLE)
  160. #define SIZE_256B (SCB_MPU_RASR_SIZE(7) | SCB_MPU_RASR_ENABLE)
  161. #define SIZE_512B (SCB_MPU_RASR_SIZE(8) | SCB_MPU_RASR_ENABLE)
  162. #define SIZE_1K (SCB_MPU_RASR_SIZE(9) | SCB_MPU_RASR_ENABLE)
  163. #define SIZE_2K (SCB_MPU_RASR_SIZE(10) | SCB_MPU_RASR_ENABLE)
  164. #define SIZE_4K (SCB_MPU_RASR_SIZE(11) | SCB_MPU_RASR_ENABLE)
  165. #define SIZE_8K (SCB_MPU_RASR_SIZE(12) | SCB_MPU_RASR_ENABLE)
  166. #define SIZE_16K (SCB_MPU_RASR_SIZE(13) | SCB_MPU_RASR_ENABLE)
  167. #define SIZE_32K (SCB_MPU_RASR_SIZE(14) | SCB_MPU_RASR_ENABLE)
  168. #define SIZE_64K (SCB_MPU_RASR_SIZE(15) | SCB_MPU_RASR_ENABLE)
  169. #define SIZE_128K (SCB_MPU_RASR_SIZE(16) | SCB_MPU_RASR_ENABLE)
  170. #define SIZE_256K (SCB_MPU_RASR_SIZE(17) | SCB_MPU_RASR_ENABLE)
  171. #define SIZE_512K (SCB_MPU_RASR_SIZE(18) | SCB_MPU_RASR_ENABLE)
  172. #define SIZE_1M (SCB_MPU_RASR_SIZE(19) | SCB_MPU_RASR_ENABLE)
  173. #define SIZE_2M (SCB_MPU_RASR_SIZE(20) | SCB_MPU_RASR_ENABLE)
  174. #define SIZE_4M (SCB_MPU_RASR_SIZE(21) | SCB_MPU_RASR_ENABLE)
  175. #define SIZE_8M (SCB_MPU_RASR_SIZE(22) | SCB_MPU_RASR_ENABLE)
  176. #define SIZE_16M (SCB_MPU_RASR_SIZE(23) | SCB_MPU_RASR_ENABLE)
  177. #define SIZE_32M (SCB_MPU_RASR_SIZE(24) | SCB_MPU_RASR_ENABLE)
  178. #define SIZE_64M (SCB_MPU_RASR_SIZE(25) | SCB_MPU_RASR_ENABLE)
  179. #define SIZE_128M (SCB_MPU_RASR_SIZE(26) | SCB_MPU_RASR_ENABLE)
  180. #define SIZE_256M (SCB_MPU_RASR_SIZE(27) | SCB_MPU_RASR_ENABLE)
  181. #define SIZE_512M (SCB_MPU_RASR_SIZE(28) | SCB_MPU_RASR_ENABLE)
  182. #define SIZE_1G (SCB_MPU_RASR_SIZE(29) | SCB_MPU_RASR_ENABLE)
  183. #define SIZE_2G (SCB_MPU_RASR_SIZE(30) | SCB_MPU_RASR_ENABLE)
  184. #define SIZE_4G (SCB_MPU_RASR_SIZE(31) | SCB_MPU_RASR_ENABLE)
  185. #define REGION(n) (SCB_MPU_RBAR_REGION(n) | SCB_MPU_RBAR_VALID)
  186. FLASHMEM void configure_cache(void)
  187. {
  188. //printf("MPU_TYPE = %08lX\n", SCB_MPU_TYPE);
  189. //printf("CCR = %08lX\n", SCB_CCR);
  190. // TODO: check if caches already active - skip?
  191. SCB_MPU_CTRL = 0; // turn off MPU
  192. uint32_t i = 0;
  193. 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
  194. SCB_MPU_RASR = SCB_MPU_RASR_TEX(0) | NOACCESS | NOEXEC | SIZE_4G;
  195. SCB_MPU_RBAR = 0x00000000 | REGION(i++); // ITCM
  196. SCB_MPU_RASR = MEM_NOCACHE | READWRITE | SIZE_512K;
  197. // TODO: trap regions should be created last, because the hardware gives
  198. // priority to the higher number ones.
  199. SCB_MPU_RBAR = 0x00000000 | REGION(i++); // trap NULL pointer deref
  200. SCB_MPU_RASR = DEV_NOCACHE | NOACCESS | SIZE_32B;
  201. SCB_MPU_RBAR = 0x00200000 | REGION(i++); // Boot ROM
  202. SCB_MPU_RASR = MEM_CACHE_WT | READONLY | SIZE_128K;
  203. SCB_MPU_RBAR = 0x20000000 | REGION(i++); // DTCM
  204. SCB_MPU_RASR = MEM_NOCACHE | READWRITE | NOEXEC | SIZE_512K;
  205. SCB_MPU_RBAR = ((uint32_t)&_ebss) | REGION(i++); // trap stack overflow
  206. SCB_MPU_RASR = SCB_MPU_RASR_TEX(0) | NOACCESS | NOEXEC | SIZE_32B;
  207. SCB_MPU_RBAR = 0x20200000 | REGION(i++); // RAM (AXI bus)
  208. SCB_MPU_RASR = MEM_CACHE_WBWA | READWRITE | NOEXEC | SIZE_1M;
  209. SCB_MPU_RBAR = 0x40000000 | REGION(i++); // Peripherals
  210. SCB_MPU_RASR = DEV_NOCACHE | READWRITE | NOEXEC | SIZE_64M;
  211. SCB_MPU_RBAR = 0x60000000 | REGION(i++); // QSPI Flash
  212. SCB_MPU_RASR = MEM_CACHE_WBWA | READONLY | SIZE_16M;
  213. SCB_MPU_RBAR = 0x70000000 | REGION(i++); // FlexSPI2
  214. SCB_MPU_RASR = MEM_CACHE_WBWA | READONLY | NOEXEC | SIZE_256M;
  215. SCB_MPU_RBAR = 0x70000000 | REGION(i++); // FlexSPI2
  216. SCB_MPU_RASR = MEM_CACHE_WBWA | READWRITE | NOEXEC | SIZE_16M;
  217. // TODO: protect access to power supply config
  218. SCB_MPU_CTRL = SCB_MPU_CTRL_ENABLE;
  219. // cache enable, ARM DDI0403E, pg 628
  220. asm("dsb");
  221. asm("isb");
  222. SCB_CACHE_ICIALLU = 0;
  223. asm("dsb");
  224. asm("isb");
  225. SCB_CCR |= (SCB_CCR_IC | SCB_CCR_DC);
  226. }
  227. #ifdef ARDUINO_TEENSY41
  228. #define LUT0(opcode, pads, operand) (FLEXSPI_LUT_INSTRUCTION((opcode), (pads), (operand)))
  229. #define LUT1(opcode, pads, operand) (FLEXSPI_LUT_INSTRUCTION((opcode), (pads), (operand)) << 16)
  230. #define CMD_SDR FLEXSPI_LUT_OPCODE_CMD_SDR
  231. #define ADDR_SDR FLEXSPI_LUT_OPCODE_RADDR_SDR
  232. #define READ_SDR FLEXSPI_LUT_OPCODE_READ_SDR
  233. #define WRITE_SDR FLEXSPI_LUT_OPCODE_WRITE_SDR
  234. #define DUMMY_SDR FLEXSPI_LUT_OPCODE_DUMMY_SDR
  235. #define PINS1 FLEXSPI_LUT_NUM_PADS_1
  236. #define PINS4 FLEXSPI_LUT_NUM_PADS_4
  237. FLASHMEM static void flexspi2_command(uint32_t index, uint32_t addr)
  238. {
  239. FLEXSPI2_IPCR0 = addr;
  240. FLEXSPI2_IPCR1 = FLEXSPI_IPCR1_ISEQID(index);
  241. FLEXSPI2_IPCMD = FLEXSPI_IPCMD_TRG;
  242. while (!(FLEXSPI2_INTR & FLEXSPI_INTR_IPCMDDONE)); // wait
  243. FLEXSPI2_INTR = FLEXSPI_INTR_IPCMDDONE;
  244. }
  245. FLASHMEM static uint32_t flexspi2_psram_id(uint32_t addr)
  246. {
  247. FLEXSPI2_IPCR0 = addr;
  248. FLEXSPI2_IPCR1 = FLEXSPI_IPCR1_ISEQID(3) | FLEXSPI_IPCR1_IDATSZ(4);
  249. FLEXSPI2_IPCMD = FLEXSPI_IPCMD_TRG;
  250. while (!(FLEXSPI2_INTR & FLEXSPI_INTR_IPCMDDONE)); // wait
  251. uint32_t id = FLEXSPI2_RFDR0;
  252. FLEXSPI2_INTR = FLEXSPI_INTR_IPCMDDONE | FLEXSPI_INTR_IPRXWA;
  253. return id & 0xFFFF;
  254. }
  255. FLASHMEM void configure_external_ram()
  256. {
  257. // initialize pins
  258. IOMUXC_SW_PAD_CTL_PAD_GPIO_EMC_22 = 0xB0E1; // 100K pullup, medium drive, max speed
  259. IOMUXC_SW_PAD_CTL_PAD_GPIO_EMC_23 = 0x10E1; // keeper, medium drive, max speed
  260. IOMUXC_SW_PAD_CTL_PAD_GPIO_EMC_24 = 0xB0E1; // 100K pullup, medium drive, max speed
  261. IOMUXC_SW_PAD_CTL_PAD_GPIO_EMC_25 = 0x00E1; // medium drive, max speed
  262. IOMUXC_SW_PAD_CTL_PAD_GPIO_EMC_26 = 0x70E1; // 47K pullup, medium drive, max speed
  263. IOMUXC_SW_PAD_CTL_PAD_GPIO_EMC_27 = 0x70E1; // 47K pullup, medium drive, max speed
  264. IOMUXC_SW_PAD_CTL_PAD_GPIO_EMC_28 = 0x70E1; // 47K pullup, medium drive, max speed
  265. IOMUXC_SW_PAD_CTL_PAD_GPIO_EMC_29 = 0x70E1; // 47K pullup, medium drive, max speed
  266. IOMUXC_SW_MUX_CTL_PAD_GPIO_EMC_22 = 8 | 0x10; // ALT1 = FLEXSPI2_A_SS1_B (Flash)
  267. IOMUXC_SW_MUX_CTL_PAD_GPIO_EMC_23 = 8 | 0x10; // ALT1 = FLEXSPI2_A_DQS
  268. IOMUXC_SW_MUX_CTL_PAD_GPIO_EMC_24 = 8 | 0x10; // ALT1 = FLEXSPI2_A_SS0_B (RAM)
  269. IOMUXC_SW_MUX_CTL_PAD_GPIO_EMC_25 = 8 | 0x10; // ALT1 = FLEXSPI2_A_SCLK
  270. IOMUXC_SW_MUX_CTL_PAD_GPIO_EMC_26 = 8 | 0x10; // ALT1 = FLEXSPI2_A_DATA0
  271. IOMUXC_SW_MUX_CTL_PAD_GPIO_EMC_27 = 8 | 0x10; // ALT1 = FLEXSPI2_A_DATA1
  272. IOMUXC_SW_MUX_CTL_PAD_GPIO_EMC_28 = 8 | 0x10; // ALT1 = FLEXSPI2_A_DATA2
  273. IOMUXC_SW_MUX_CTL_PAD_GPIO_EMC_29 = 8 | 0x10; // ALT1 = FLEXSPI2_A_DATA3
  274. IOMUXC_FLEXSPI2_IPP_IND_DQS_FA_SELECT_INPUT = 1; // GPIO_EMC_23 for Mode: ALT8, pg 986
  275. IOMUXC_FLEXSPI2_IPP_IND_IO_FA_BIT0_SELECT_INPUT = 1; // GPIO_EMC_26 for Mode: ALT8
  276. IOMUXC_FLEXSPI2_IPP_IND_IO_FA_BIT1_SELECT_INPUT = 1; // GPIO_EMC_27 for Mode: ALT8
  277. IOMUXC_FLEXSPI2_IPP_IND_IO_FA_BIT2_SELECT_INPUT = 1; // GPIO_EMC_28 for Mode: ALT8
  278. IOMUXC_FLEXSPI2_IPP_IND_IO_FA_BIT3_SELECT_INPUT = 1; // GPIO_EMC_29 for Mode: ALT8
  279. IOMUXC_FLEXSPI2_IPP_IND_SCK_FA_SELECT_INPUT = 1; // GPIO_EMC_25 for Mode: ALT8
  280. // turn on clock (TODO: increase clock speed later, slow & cautious for first release)
  281. CCM_CBCMR = (CCM_CBCMR & (CCM_CBCMR_FLEXSPI2_PODF_MASK | CCM_CBCMR_FLEXSPI2_CLK_SEL_MASK))
  282. | CCM_CBCMR_FLEXSPI2_PODF(7) | CCM_CBCMR_FLEXSPI2_CLK_SEL(0); // 49.5 MHz
  283. CCM_CCGR7 |= CCM_CCGR7_FLEXSPI2(CCM_CCGR_ON);
  284. FLEXSPI2_MCR0 |= FLEXSPI_MCR0_MDIS;
  285. FLEXSPI2_MCR0 = (FLEXSPI2_MCR0 & ~(FLEXSPI_MCR0_AHBGRANTWAIT_MASK
  286. | FLEXSPI_MCR0_IPGRANTWAIT_MASK | FLEXSPI_MCR0_SCKFREERUNEN
  287. | FLEXSPI_MCR0_COMBINATIONEN | FLEXSPI_MCR0_DOZEEN
  288. | FLEXSPI_MCR0_HSEN | FLEXSPI_MCR0_ATDFEN | FLEXSPI_MCR0_ARDFEN
  289. | FLEXSPI_MCR0_RXCLKSRC_MASK | FLEXSPI_MCR0_SWRESET))
  290. | FLEXSPI_MCR0_AHBGRANTWAIT(0xFF) | FLEXSPI_MCR0_IPGRANTWAIT(0xFF)
  291. | FLEXSPI_MCR0_RXCLKSRC(1) | FLEXSPI_MCR0_MDIS;
  292. FLEXSPI2_MCR1 = FLEXSPI_MCR1_SEQWAIT(0xFFFF) | FLEXSPI_MCR1_AHBBUSWAIT(0xFFFF);
  293. FLEXSPI2_MCR2 = (FLEXSPI_MCR2 & ~(FLEXSPI_MCR2_RESUMEWAIT_MASK
  294. | FLEXSPI_MCR2_SCKBDIFFOPT | FLEXSPI_MCR2_SAMEDEVICEEN
  295. | FLEXSPI_MCR2_CLRLEARNPHASE | FLEXSPI_MCR2_CLRAHBBUFOPT))
  296. | FLEXSPI_MCR2_RESUMEWAIT(0x20) /*| FLEXSPI_MCR2_SAMEDEVICEEN*/;
  297. FLEXSPI2_AHBCR = FLEXSPI2_AHBCR & ~(FLEXSPI_AHBCR_READADDROPT | FLEXSPI_AHBCR_PREFETCHEN
  298. | FLEXSPI_AHBCR_BUFFERABLEEN | FLEXSPI_AHBCR_CACHABLEEN);
  299. uint32_t mask = (FLEXSPI_AHBRXBUFCR0_PREFETCHEN | FLEXSPI_AHBRXBUFCR0_PRIORITY_MASK
  300. | FLEXSPI_AHBRXBUFCR0_MSTRID_MASK | FLEXSPI_AHBRXBUFCR0_BUFSZ_MASK);
  301. FLEXSPI2_AHBRXBUF0CR0 = (FLEXSPI2_AHBRXBUF0CR0 & ~mask)
  302. | FLEXSPI_AHBRXBUFCR0_PREFETCHEN | FLEXSPI_AHBRXBUFCR0_BUFSZ(64);
  303. FLEXSPI2_AHBRXBUF1CR0 = (FLEXSPI2_AHBRXBUF0CR0 & ~mask)
  304. | FLEXSPI_AHBRXBUFCR0_PREFETCHEN | FLEXSPI_AHBRXBUFCR0_BUFSZ(64);
  305. FLEXSPI2_AHBRXBUF2CR0 = mask;
  306. FLEXSPI2_AHBRXBUF3CR0 = mask;
  307. // RX watermark = one 64 bit line
  308. FLEXSPI2_IPRXFCR = (FLEXSPI_IPRXFCR & 0xFFFFFFC0) | FLEXSPI_IPRXFCR_CLRIPRXF;
  309. // TX watermark = one 64 bit line
  310. FLEXSPI2_IPTXFCR = (FLEXSPI_IPTXFCR & 0xFFFFFFC0) | FLEXSPI_IPTXFCR_CLRIPTXF;
  311. FLEXSPI2_INTEN = 0;
  312. FLEXSPI2_FLSHA1CR0 = 0x2000; // 8 MByte
  313. FLEXSPI2_FLSHA1CR1 = FLEXSPI_FLSHCR1_CSINTERVAL(2)
  314. | FLEXSPI_FLSHCR1_TCSH(3) | FLEXSPI_FLSHCR1_TCSS(3);
  315. FLEXSPI2_FLSHA1CR2 = FLEXSPI_FLSHCR2_AWRSEQID(6) | FLEXSPI_FLSHCR2_AWRSEQNUM(0)
  316. | FLEXSPI_FLSHCR2_ARDSEQID(5) | FLEXSPI_FLSHCR2_ARDSEQNUM(0);
  317. FLEXSPI2_FLSHA2CR0 = 0x2000; // 8 MByte
  318. FLEXSPI2_FLSHA2CR1 = FLEXSPI_FLSHCR1_CSINTERVAL(2)
  319. | FLEXSPI_FLSHCR1_TCSH(3) | FLEXSPI_FLSHCR1_TCSS(3);
  320. FLEXSPI2_FLSHA2CR2 = FLEXSPI_FLSHCR2_AWRSEQID(6) | FLEXSPI_FLSHCR2_AWRSEQNUM(0)
  321. | FLEXSPI_FLSHCR2_ARDSEQID(5) | FLEXSPI_FLSHCR2_ARDSEQNUM(0);
  322. FLEXSPI2_MCR0 &= ~FLEXSPI_MCR0_MDIS;
  323. FLEXSPI2_LUTKEY = FLEXSPI_LUTKEY_VALUE;
  324. FLEXSPI2_LUTCR = FLEXSPI_LUTCR_UNLOCK;
  325. volatile uint32_t *luttable = &FLEXSPI2_LUT0;
  326. for (int i=0; i < 64; i++) luttable[i] = 0;
  327. FLEXSPI2_MCR0 |= FLEXSPI_MCR0_SWRESET;
  328. while (FLEXSPI2_MCR0 & FLEXSPI_MCR0_SWRESET) ; // wait
  329. FLEXSPI2_LUTKEY = FLEXSPI_LUTKEY_VALUE;
  330. FLEXSPI2_LUTCR = FLEXSPI_LUTCR_UNLOCK;
  331. // cmd index 0 = exit QPI mode
  332. FLEXSPI2_LUT0 = LUT0(CMD_SDR, PINS4, 0xF5);
  333. // cmd index 1 = reset enable
  334. FLEXSPI2_LUT4 = LUT0(CMD_SDR, PINS1, 0x66);
  335. // cmd index 2 = reset
  336. FLEXSPI2_LUT8 = LUT0(CMD_SDR, PINS1, 0x99);
  337. // cmd index 3 = read ID bytes
  338. FLEXSPI2_LUT12 = LUT0(CMD_SDR, PINS1, 0x9F) | LUT1(DUMMY_SDR, PINS1, 24);
  339. FLEXSPI2_LUT13 = LUT0(READ_SDR, PINS1, 1);
  340. // cmd index 4 = enter QPI mode
  341. FLEXSPI2_LUT16 = LUT0(CMD_SDR, PINS1, 0x35);
  342. // cmd index 5 = read QPI
  343. FLEXSPI2_LUT20 = LUT0(CMD_SDR, PINS4, 0xEB) | LUT1(ADDR_SDR, PINS4, 24);
  344. FLEXSPI2_LUT21 = LUT0(DUMMY_SDR, PINS4, 6) | LUT1(READ_SDR, PINS4, 1);
  345. // cmd index 6 = write QPI
  346. FLEXSPI2_LUT24 = LUT0(CMD_SDR, PINS4, 0x38) | LUT1(ADDR_SDR, PINS4, 24);
  347. FLEXSPI2_LUT25 = LUT0(WRITE_SDR, PINS4, 1);
  348. // look for the first PSRAM chip
  349. flexspi2_command(0, 0); // exit quad mode
  350. flexspi2_command(1, 0); // reset enable
  351. flexspi2_command(2, 0); // reset (is this really necessary?)
  352. if (flexspi2_psram_id(0) == 0x5D0D) {
  353. // first PSRAM chip is present, look for a second PSRAM chip
  354. flexspi2_command(4, 0);
  355. flexspi2_command(0, 0x800000); // exit quad mode
  356. flexspi2_command(1, 0x800000); // reset enable
  357. flexspi2_command(2, 0x800000); // reset (is this really necessary?)
  358. if (flexspi2_psram_id(0x800000) == 0x5D0D) {
  359. flexspi2_command(4, 0x800000);
  360. // Two PSRAM chips are present, 16 MByte
  361. } else {
  362. // One PSRAM chip is present, 8 MByte
  363. }
  364. // TODO: zero uninitialized EXTMEM variables
  365. // TODO: copy from flash to initialize EXTMEM variables
  366. // TODO: set up for malloc_extmem()
  367. } else {
  368. // No PSRAM
  369. }
  370. }
  371. #endif // ARDUINO_TEENSY41
  372. FLASHMEM void usb_pll_start()
  373. {
  374. while (1) {
  375. uint32_t n = CCM_ANALOG_PLL_USB1; // pg 759
  376. printf("CCM_ANALOG_PLL_USB1=%08lX\n", n);
  377. if (n & CCM_ANALOG_PLL_USB1_DIV_SELECT) {
  378. printf(" ERROR, 528 MHz mode!\n"); // never supposed to use this mode!
  379. CCM_ANALOG_PLL_USB1_CLR = 0xC000; // bypass 24 MHz
  380. CCM_ANALOG_PLL_USB1_SET = CCM_ANALOG_PLL_USB1_BYPASS; // bypass
  381. CCM_ANALOG_PLL_USB1_CLR = CCM_ANALOG_PLL_USB1_POWER | // power down
  382. CCM_ANALOG_PLL_USB1_DIV_SELECT | // use 480 MHz
  383. CCM_ANALOG_PLL_USB1_ENABLE | // disable
  384. CCM_ANALOG_PLL_USB1_EN_USB_CLKS; // disable usb
  385. continue;
  386. }
  387. if (!(n & CCM_ANALOG_PLL_USB1_ENABLE)) {
  388. printf(" enable PLL\n");
  389. // TODO: should this be done so early, or later??
  390. CCM_ANALOG_PLL_USB1_SET = CCM_ANALOG_PLL_USB1_ENABLE;
  391. continue;
  392. }
  393. if (!(n & CCM_ANALOG_PLL_USB1_POWER)) {
  394. printf(" power up PLL\n");
  395. CCM_ANALOG_PLL_USB1_SET = CCM_ANALOG_PLL_USB1_POWER;
  396. continue;
  397. }
  398. if (!(n & CCM_ANALOG_PLL_USB1_LOCK)) {
  399. printf(" wait for lock\n");
  400. continue;
  401. }
  402. if (n & CCM_ANALOG_PLL_USB1_BYPASS) {
  403. printf(" turn off bypass\n");
  404. CCM_ANALOG_PLL_USB1_CLR = CCM_ANALOG_PLL_USB1_BYPASS;
  405. continue;
  406. }
  407. if (!(n & CCM_ANALOG_PLL_USB1_EN_USB_CLKS)) {
  408. printf(" enable USB clocks\n");
  409. CCM_ANALOG_PLL_USB1_SET = CCM_ANALOG_PLL_USB1_EN_USB_CLKS;
  410. continue;
  411. }
  412. return; // everything is as it should be :-)
  413. }
  414. }
  415. FLASHMEM void reset_PFD()
  416. {
  417. //Reset PLL2 PFDs, set default frequencies:
  418. CCM_ANALOG_PFD_528_SET = (1 << 31) | (1 << 23) | (1 << 15) | (1 << 7);
  419. CCM_ANALOG_PFD_528 = 0x2018101B; // PFD0:352, PFD1:594, PFD2:396, PFD3:297 MHz
  420. //PLL3:
  421. CCM_ANALOG_PFD_480_SET = (1 << 31) | (1 << 23) | (1 << 15) | (1 << 7);
  422. CCM_ANALOG_PFD_480 = 0x13110D0C; // PFD0:720, PFD1:664, PFD2:508, PFD3:454 MHz
  423. }
  424. // Stack frame
  425. // xPSR
  426. // ReturnAddress
  427. // LR (R14) - typically FFFFFFF9 for IRQ or Exception
  428. // R12
  429. // R3
  430. // R2
  431. // R1
  432. // R0
  433. // Code from :: https://community.nxp.com/thread/389002
  434. __attribute__((naked))
  435. void unused_interrupt_vector(void)
  436. {
  437. __asm( ".syntax unified\n"
  438. "MOVS R0, #4 \n"
  439. "MOV R1, LR \n"
  440. "TST R0, R1 \n"
  441. "BEQ _MSP \n"
  442. "MRS R0, PSP \n"
  443. "B HardFault_HandlerC \n"
  444. "_MSP: \n"
  445. "MRS R0, MSP \n"
  446. "B HardFault_HandlerC \n"
  447. ".syntax divided\n") ;
  448. }
  449. __attribute__((weak))
  450. void HardFault_HandlerC(unsigned int *hardfault_args)
  451. {
  452. volatile unsigned int nn ;
  453. #ifdef PRINT_DEBUG_STUFF
  454. volatile unsigned int stacked_r0 ;
  455. volatile unsigned int stacked_r1 ;
  456. volatile unsigned int stacked_r2 ;
  457. volatile unsigned int stacked_r3 ;
  458. volatile unsigned int stacked_r12 ;
  459. volatile unsigned int stacked_lr ;
  460. volatile unsigned int stacked_pc ;
  461. volatile unsigned int stacked_psr ;
  462. volatile unsigned int _CFSR ;
  463. volatile unsigned int _HFSR ;
  464. volatile unsigned int _DFSR ;
  465. volatile unsigned int _AFSR ;
  466. volatile unsigned int _BFAR ;
  467. volatile unsigned int _MMAR ;
  468. volatile unsigned int addr ;
  469. stacked_r0 = ((unsigned int)hardfault_args[0]) ;
  470. stacked_r1 = ((unsigned int)hardfault_args[1]) ;
  471. stacked_r2 = ((unsigned int)hardfault_args[2]) ;
  472. stacked_r3 = ((unsigned int)hardfault_args[3]) ;
  473. stacked_r12 = ((unsigned int)hardfault_args[4]) ;
  474. stacked_lr = ((unsigned int)hardfault_args[5]) ;
  475. stacked_pc = ((unsigned int)hardfault_args[6]) ;
  476. stacked_psr = ((unsigned int)hardfault_args[7]) ;
  477. // Configurable Fault Status Register
  478. // Consists of MMSR, BFSR and UFSR
  479. //(n & ( 1 << k )) >> k
  480. _CFSR = (*((volatile unsigned int *)(0xE000ED28))) ;
  481. // Hard Fault Status Register
  482. _HFSR = (*((volatile unsigned int *)(0xE000ED2C))) ;
  483. // Debug Fault Status Register
  484. _DFSR = (*((volatile unsigned int *)(0xE000ED30))) ;
  485. // Auxiliary Fault Status Register
  486. _AFSR = (*((volatile unsigned int *)(0xE000ED3C))) ;
  487. // Read the Fault Address Registers. These may not contain valid values.
  488. // Check BFARVALID/MMARVALID to see if they are valid values
  489. // MemManage Fault Address Register
  490. _MMAR = (*((volatile unsigned int *)(0xE000ED34))) ;
  491. // Bus Fault Address Register
  492. _BFAR = (*((volatile unsigned int *)(0xE000ED38))) ;
  493. //__asm("BKPT #0\n") ; // Break into the debugger // NO Debugger here.
  494. asm volatile("mrs %0, ipsr\n" : "=r" (addr)::);
  495. printf("\nFault irq %d\n", addr & 0x1FF);
  496. printf(" stacked_r0 :: %x\n", stacked_r0);
  497. printf(" stacked_r1 :: %x\n", stacked_r1);
  498. printf(" stacked_r2 :: %x\n", stacked_r2);
  499. printf(" stacked_r3 :: %x\n", stacked_r3);
  500. printf(" stacked_r12 :: %x\n", stacked_r12);
  501. printf(" stacked_lr :: %x\n", stacked_lr);
  502. printf(" stacked_pc :: %x\n", stacked_pc);
  503. printf(" stacked_psr :: %x\n", stacked_psr);
  504. printf(" _CFSR :: %x\n", _CFSR);
  505. if(_CFSR > 0){
  506. //Memory Management Faults
  507. if((_CFSR & 1) == 1){
  508. printf(" (IACCVIOL) Instruction Access Violation\n");
  509. } else if(((_CFSR & (0x02))>>1) == 1){
  510. printf(" (DACCVIOL) Data Access Violation\n");
  511. } else if(((_CFSR & (0x08))>>3) == 1){
  512. printf(" (MUNSTKERR) MemMange Fault on Unstacking\n");
  513. } else if(((_CFSR & (0x10))>>4) == 1){
  514. printf(" (MSTKERR) MemMange Fault on stacking\n");
  515. } else if(((_CFSR & (0x20))>>5) == 1){
  516. printf(" (MLSPERR) MemMange Fault on FP Lazy State\n");
  517. }
  518. if(((_CFSR & (0x80))>>7) == 1){
  519. printf(" (MMARVALID) MemMange Fault Address Valid\n");
  520. }
  521. //Bus Fault Status Register
  522. if(((_CFSR & 0x100)>>8) == 1){
  523. printf(" (IBUSERR) Instruction Bus Error\n");
  524. } else if(((_CFSR & (0x200))>>9) == 1){
  525. printf(" (PRECISERR) Data bus error(address in BFAR)\n");
  526. } else if(((_CFSR & (0x400))>>10) == 1){
  527. printf(" (IMPRECISERR) Data bus error but address not related to instruction\n");
  528. } else if(((_CFSR & (0x800))>>11) == 1){
  529. printf(" (UNSTKERR) Bus Fault on unstacking for a return from exception \n");
  530. } else if(((_CFSR & (0x1000))>>12) == 1){
  531. printf(" (STKERR) Bus Fault on stacking for exception entry\n");
  532. } else if(((_CFSR & (0x2000))>>13) == 1){
  533. printf(" (LSPERR) Bus Fault on FP lazy state preservation\n");
  534. }
  535. if(((_CFSR & (0x8000))>>15) == 1){
  536. printf(" (BFARVALID) Bus Fault Address Valid\n");
  537. }
  538. //Usuage Fault Status Register
  539. if(((_CFSR & 0x10000)>>16) == 1){
  540. printf(" (UNDEFINSTR) Undefined instruction\n");
  541. } else if(((_CFSR & (0x20000))>>17) == 1){
  542. printf(" (INVSTATE) Instruction makes illegal use of EPSR)\n");
  543. } else if(((_CFSR & (0x40000))>>18) == 1){
  544. printf(" (INVPC) Usage fault: invalid EXC_RETURN\n");
  545. } else if(((_CFSR & (0x80000))>>19) == 1){
  546. printf(" (NOCP) No Coprocessor \n");
  547. } else if(((_CFSR & (0x1000000))>>24) == 1){
  548. printf(" (UNALIGNED) Unaligned access UsageFault\n");
  549. } else if(((_CFSR & (0x2000000))>>25) == 1){
  550. printf(" (DIVBYZERO) Divide by zero\n");
  551. }
  552. }
  553. printf(" _HFSR :: %x\n", _HFSR);
  554. if(_HFSR > 0){
  555. //Memory Management Faults
  556. if(((_HFSR & (0x02))>>1) == 1){
  557. printf(" (VECTTBL) Bus Fault on Vec Table Read\n");
  558. } else if(((_HFSR & (0x40000000))>>30) == 1){
  559. printf(" (FORCED) Forced Hard Fault\n");
  560. } else if(((_HFSR & (0x80000000))>>31) == 31){
  561. printf(" (DEBUGEVT) Reserved for Debug\n");
  562. }
  563. }
  564. printf(" _DFSR :: %x\n", _DFSR);
  565. printf(" _AFSR :: %x\n", _AFSR);
  566. printf(" _BFAR :: %x\n", _BFAR);
  567. printf(" _MMAR :: %x\n", _MMAR);
  568. #endif
  569. IOMUXC_SW_MUX_CTL_PAD_GPIO_B0_03 = 5; // pin 13
  570. IOMUXC_SW_PAD_CTL_PAD_GPIO_B0_03 = IOMUXC_PAD_DSE(7);
  571. GPIO2_GDIR |= (1 << 3);
  572. GPIO2_DR_SET = (1 << 3);
  573. GPIO2_DR_CLEAR = (1 << 3); //digitalWrite(13, LOW);
  574. if ( F_CPU_ACTUAL >= 600000000 )
  575. set_arm_clock(300000000);
  576. while (1)
  577. {
  578. GPIO2_DR_SET = (1 << 3); //digitalWrite(13, HIGH);
  579. // digitalWrite(13, HIGH);
  580. for (nn = 0; nn < 2000000/2; nn++) ;
  581. GPIO2_DR_CLEAR = (1 << 3); //digitalWrite(13, LOW);
  582. // digitalWrite(13, LOW);
  583. for (nn = 0; nn < 18000000/2; nn++) ;
  584. }
  585. }
  586. __attribute__((weak))
  587. void userDebugDump(){
  588. volatile unsigned int nn;
  589. printf("\nuserDebugDump() in startup.c ___ \n");
  590. while (1)
  591. {
  592. GPIO2_DR_SET = (1 << 3); //digitalWrite(13, HIGH);
  593. // digitalWrite(13, HIGH);
  594. for (nn = 0; nn < 2000000; nn++) ;
  595. GPIO2_DR_CLEAR = (1 << 3); //digitalWrite(13, LOW);
  596. // digitalWrite(13, LOW);
  597. for (nn = 0; nn < 18000000; nn++) ;
  598. GPIO2_DR_SET = (1 << 3); //digitalWrite(13, HIGH);
  599. // digitalWrite(13, HIGH);
  600. for (nn = 0; nn < 20000000; nn++) ;
  601. GPIO2_DR_CLEAR = (1 << 3); //digitalWrite(13, LOW);
  602. // digitalWrite(13, LOW);
  603. for (nn = 0; nn < 10000000; nn++) ;
  604. }
  605. }
  606. __attribute__((weak))
  607. void PJRCunused_interrupt_vector(void)
  608. {
  609. // TODO: polling Serial to complete buffered transmits
  610. #ifdef PRINT_DEBUG_STUFF
  611. uint32_t addr;
  612. asm volatile("mrs %0, ipsr\n" : "=r" (addr)::);
  613. printf("\nirq %d\n", addr & 0x1FF);
  614. asm("ldr %0, [sp, #52]" : "=r" (addr) ::);
  615. printf(" %x\n", addr);
  616. asm("ldr %0, [sp, #48]" : "=r" (addr) ::);
  617. printf(" %x\n", addr);
  618. asm("ldr %0, [sp, #44]" : "=r" (addr) ::);
  619. printf(" %x\n", addr);
  620. asm("ldr %0, [sp, #40]" : "=r" (addr) ::);
  621. printf(" %x\n", addr);
  622. asm("ldr %0, [sp, #36]" : "=r" (addr) ::);
  623. printf(" %x\n", addr);
  624. asm("ldr %0, [sp, #33]" : "=r" (addr) ::);
  625. printf(" %x\n", addr);
  626. asm("ldr %0, [sp, #34]" : "=r" (addr) ::);
  627. printf(" %x\n", addr);
  628. asm("ldr %0, [sp, #28]" : "=r" (addr) ::);
  629. printf(" %x\n", addr);
  630. asm("ldr %0, [sp, #24]" : "=r" (addr) ::);
  631. printf(" %x\n", addr);
  632. asm("ldr %0, [sp, #20]" : "=r" (addr) ::);
  633. printf(" %x\n", addr);
  634. asm("ldr %0, [sp, #16]" : "=r" (addr) ::);
  635. printf(" %x\n", addr);
  636. asm("ldr %0, [sp, #12]" : "=r" (addr) ::);
  637. printf(" %x\n", addr);
  638. asm("ldr %0, [sp, #8]" : "=r" (addr) ::);
  639. printf(" %x\n", addr);
  640. asm("ldr %0, [sp, #4]" : "=r" (addr) ::);
  641. printf(" %x\n", addr);
  642. asm("ldr %0, [sp, #0]" : "=r" (addr) ::);
  643. printf(" %x\n", addr);
  644. #endif
  645. #if 1
  646. if ( F_CPU_ACTUAL >= 600000000 )
  647. set_arm_clock(100000000);
  648. IOMUXC_SW_MUX_CTL_PAD_GPIO_B0_03 = 5; // pin 13
  649. IOMUXC_SW_PAD_CTL_PAD_GPIO_B0_03 = IOMUXC_PAD_DSE(7);
  650. GPIO2_GDIR |= (1<<3);
  651. GPIO2_DR_SET = (1<<3);
  652. while (1) {
  653. volatile uint32_t n;
  654. GPIO2_DR_SET = (1<<3); //digitalWrite(13, HIGH);
  655. for (n=0; n < 2000000/6; n++) ;
  656. GPIO2_DR_CLEAR = (1<<3); //digitalWrite(13, LOW);
  657. for (n=0; n < 1500000/6; n++) ;
  658. }
  659. #else
  660. if ( F_CPU_ACTUAL >= 600000000 )
  661. set_arm_clock(100000000);
  662. while (1) asm ("WFI");
  663. #endif
  664. }
  665. __attribute__((section(".startup"), optimize("no-tree-loop-distribute-patterns")))
  666. static void memory_copy(uint32_t *dest, const uint32_t *src, uint32_t *dest_end)
  667. {
  668. if (dest == src) return;
  669. while (dest < dest_end) {
  670. *dest++ = *src++;
  671. }
  672. }
  673. __attribute__((section(".startup"), optimize("no-tree-loop-distribute-patterns")))
  674. static void memory_clear(uint32_t *dest, uint32_t *dest_end)
  675. {
  676. while (dest < dest_end) {
  677. *dest++ = 0;
  678. }
  679. }
  680. // syscall functions need to be in the same C file as the entry point "ResetVector"
  681. // otherwise the linker will discard them in some cases.
  682. #include <errno.h>
  683. // from the linker script
  684. extern unsigned long _heap_start;
  685. extern unsigned long _heap_end;
  686. char *__brkval = (char *)&_heap_start;
  687. void * _sbrk(int incr)
  688. {
  689. char *prev = __brkval;
  690. if (incr != 0) {
  691. if (prev + incr > (char *)&_heap_end) {
  692. errno = ENOMEM;
  693. return (void *)-1;
  694. }
  695. __brkval = prev + incr;
  696. }
  697. return prev;
  698. }
  699. __attribute__((weak))
  700. int _read(int file, char *ptr, int len)
  701. {
  702. return 0;
  703. }
  704. __attribute__((weak))
  705. int _close(int fd)
  706. {
  707. return -1;
  708. }
  709. #include <sys/stat.h>
  710. __attribute__((weak))
  711. int _fstat(int fd, struct stat *st)
  712. {
  713. st->st_mode = S_IFCHR;
  714. return 0;
  715. }
  716. __attribute__((weak))
  717. int _isatty(int fd)
  718. {
  719. return 1;
  720. }
  721. __attribute__((weak))
  722. int _lseek(int fd, long long offset, int whence)
  723. {
  724. return -1;
  725. }
  726. __attribute__((weak))
  727. void _exit(int status)
  728. {
  729. while (1) asm ("WFI");
  730. }
  731. __attribute__((weak))
  732. void __cxa_pure_virtual()
  733. {
  734. while (1) asm ("WFI");
  735. }
  736. __attribute__((weak))
  737. int __cxa_guard_acquire (char *g)
  738. {
  739. return !(*g);
  740. }
  741. __attribute__((weak))
  742. void __cxa_guard_release(char *g)
  743. {
  744. *g = 1;
  745. }
  746. __attribute__((weak))
  747. void abort(void)
  748. {
  749. while (1) asm ("WFI");
  750. }