Teensy 4.1 core updated for C++20
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

851 lines
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. uint8_t external_psram_size = 0;
  35. extern int main (void);
  36. void startup_default_early_hook(void) {}
  37. void startup_early_hook(void) __attribute__ ((weak, alias("startup_default_early_hook")));
  38. void startup_default_late_hook(void) {}
  39. void startup_late_hook(void) __attribute__ ((weak, alias("startup_default_late_hook")));
  40. __attribute__((section(".startup"), optimize("no-tree-loop-distribute-patterns")))
  41. void ResetHandler(void)
  42. {
  43. unsigned int i;
  44. #if defined(__IMXRT1062__)
  45. IOMUXC_GPR_GPR17 = (uint32_t)&_flexram_bank_config;
  46. IOMUXC_GPR_GPR16 = 0x00200007;
  47. IOMUXC_GPR_GPR14 = 0x00AA0000;
  48. __asm__ volatile("mov sp, %0" : : "r" ((uint32_t)&_estack) : );
  49. #endif
  50. PMU_MISC0_SET = 1<<3; //Use bandgap-based bias currents for best performance (Page 1175)
  51. // pin 13 - if startup crashes, use this to turn on the LED early for troubleshooting
  52. //IOMUXC_SW_MUX_CTL_PAD_GPIO_B0_03 = 5;
  53. //IOMUXC_SW_PAD_CTL_PAD_GPIO_B0_03 = IOMUXC_PAD_DSE(7);
  54. //IOMUXC_GPR_GPR27 = 0xFFFFFFFF;
  55. //GPIO7_GDIR |= (1<<3);
  56. //GPIO7_DR_SET = (1<<3); // digitalWrite(13, HIGH);
  57. // Initialize memory
  58. memory_copy(&_stext, &_stextload, &_etext);
  59. memory_copy(&_sdata, &_sdataload, &_edata);
  60. memory_clear(&_sbss, &_ebss);
  61. // enable FPU
  62. SCB_CPACR = 0x00F00000;
  63. // set up blank interrupt & exception vector table
  64. for (i=0; i < NVIC_NUM_INTERRUPTS + 16; i++) _VectorsRam[i] = &unused_interrupt_vector;
  65. for (i=0; i < NVIC_NUM_INTERRUPTS; i++) NVIC_SET_PRIORITY(i, 128);
  66. SCB_VTOR = (uint32_t)_VectorsRam;
  67. reset_PFD();
  68. // Configure clocks
  69. // TODO: make sure all affected peripherals are turned off!
  70. // PIT & GPT timers to run from 24 MHz clock (independent of CPU speed)
  71. CCM_CSCMR1 = (CCM_CSCMR1 & ~CCM_CSCMR1_PERCLK_PODF(0x3F)) | CCM_CSCMR1_PERCLK_CLK_SEL;
  72. // UARTs run from 24 MHz clock (works if PLL3 off or bypassed)
  73. CCM_CSCDR1 = (CCM_CSCDR1 & ~CCM_CSCDR1_UART_CLK_PODF(0x3F)) | CCM_CSCDR1_UART_CLK_SEL;
  74. #if defined(__IMXRT1062__)
  75. // Use fast GPIO6, GPIO7, GPIO8, GPIO9
  76. IOMUXC_GPR_GPR26 = 0xFFFFFFFF;
  77. IOMUXC_GPR_GPR27 = 0xFFFFFFFF;
  78. IOMUXC_GPR_GPR28 = 0xFFFFFFFF;
  79. IOMUXC_GPR_GPR29 = 0xFFFFFFFF;
  80. #endif
  81. // must enable PRINT_DEBUG_STUFF in debug/print.h
  82. printf_debug_init();
  83. printf("\n***********IMXRT Startup**********\n");
  84. printf("test %d %d %d\n", 1, -1234567, 3);
  85. configure_cache();
  86. configure_systick();
  87. usb_pll_start();
  88. reset_PFD(); //TODO: is this really needed?
  89. #ifdef F_CPU
  90. set_arm_clock(F_CPU);
  91. #endif
  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 = 0x1B0F9; // 100K pullup, strong drive, max speed, hyst
  259. IOMUXC_SW_PAD_CTL_PAD_GPIO_EMC_23 = 0x110F9; // keeper, strong drive, max speed, hyst
  260. IOMUXC_SW_PAD_CTL_PAD_GPIO_EMC_24 = 0x1B0F9; // 100K pullup, strong drive, max speed, hyst
  261. IOMUXC_SW_PAD_CTL_PAD_GPIO_EMC_25 = 0x100F9; // strong drive, max speed, hyst
  262. IOMUXC_SW_PAD_CTL_PAD_GPIO_EMC_26 = 0x170F9; // 47K pullup, strong drive, max speed, hyst
  263. IOMUXC_SW_PAD_CTL_PAD_GPIO_EMC_27 = 0x170F9; // 47K pullup, strong drive, max speed, hyst
  264. IOMUXC_SW_PAD_CTL_PAD_GPIO_EMC_28 = 0x170F9; // 47K pullup, strong drive, max speed, hyst
  265. IOMUXC_SW_PAD_CTL_PAD_GPIO_EMC_29 = 0x170F9; // 47K pullup, strong drive, max speed, hyst
  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(5) | CCM_CBCMR_FLEXSPI2_CLK_SEL(3); // 88 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. external_psram_size = 16;
  362. } else {
  363. // One PSRAM chip is present, 8 MByte
  364. external_psram_size = 8;
  365. }
  366. // TODO: zero uninitialized EXTMEM variables
  367. // TODO: copy from flash to initialize EXTMEM variables
  368. // TODO: set up for malloc_extmem()
  369. } else {
  370. // No PSRAM
  371. }
  372. }
  373. #endif // ARDUINO_TEENSY41
  374. FLASHMEM void usb_pll_start()
  375. {
  376. while (1) {
  377. uint32_t n = CCM_ANALOG_PLL_USB1; // pg 759
  378. printf("CCM_ANALOG_PLL_USB1=%08lX\n", n);
  379. if (n & CCM_ANALOG_PLL_USB1_DIV_SELECT) {
  380. printf(" ERROR, 528 MHz mode!\n"); // never supposed to use this mode!
  381. CCM_ANALOG_PLL_USB1_CLR = 0xC000; // bypass 24 MHz
  382. CCM_ANALOG_PLL_USB1_SET = CCM_ANALOG_PLL_USB1_BYPASS; // bypass
  383. CCM_ANALOG_PLL_USB1_CLR = CCM_ANALOG_PLL_USB1_POWER | // power down
  384. CCM_ANALOG_PLL_USB1_DIV_SELECT | // use 480 MHz
  385. CCM_ANALOG_PLL_USB1_ENABLE | // disable
  386. CCM_ANALOG_PLL_USB1_EN_USB_CLKS; // disable usb
  387. continue;
  388. }
  389. if (!(n & CCM_ANALOG_PLL_USB1_ENABLE)) {
  390. printf(" enable PLL\n");
  391. // TODO: should this be done so early, or later??
  392. CCM_ANALOG_PLL_USB1_SET = CCM_ANALOG_PLL_USB1_ENABLE;
  393. continue;
  394. }
  395. if (!(n & CCM_ANALOG_PLL_USB1_POWER)) {
  396. printf(" power up PLL\n");
  397. CCM_ANALOG_PLL_USB1_SET = CCM_ANALOG_PLL_USB1_POWER;
  398. continue;
  399. }
  400. if (!(n & CCM_ANALOG_PLL_USB1_LOCK)) {
  401. printf(" wait for lock\n");
  402. continue;
  403. }
  404. if (n & CCM_ANALOG_PLL_USB1_BYPASS) {
  405. printf(" turn off bypass\n");
  406. CCM_ANALOG_PLL_USB1_CLR = CCM_ANALOG_PLL_USB1_BYPASS;
  407. continue;
  408. }
  409. if (!(n & CCM_ANALOG_PLL_USB1_EN_USB_CLKS)) {
  410. printf(" enable USB clocks\n");
  411. CCM_ANALOG_PLL_USB1_SET = CCM_ANALOG_PLL_USB1_EN_USB_CLKS;
  412. continue;
  413. }
  414. return; // everything is as it should be :-)
  415. }
  416. }
  417. FLASHMEM void reset_PFD()
  418. {
  419. //Reset PLL2 PFDs, set default frequencies:
  420. CCM_ANALOG_PFD_528_SET = (1 << 31) | (1 << 23) | (1 << 15) | (1 << 7);
  421. CCM_ANALOG_PFD_528 = 0x2018101B; // PFD0:352, PFD1:594, PFD2:396, PFD3:297 MHz
  422. //PLL3:
  423. CCM_ANALOG_PFD_480_SET = (1 << 31) | (1 << 23) | (1 << 15) | (1 << 7);
  424. CCM_ANALOG_PFD_480 = 0x13110D0C; // PFD0:720, PFD1:664, PFD2:508, PFD3:454 MHz
  425. }
  426. // Stack frame
  427. // xPSR
  428. // ReturnAddress
  429. // LR (R14) - typically FFFFFFF9 for IRQ or Exception
  430. // R12
  431. // R3
  432. // R2
  433. // R1
  434. // R0
  435. // Code from :: https://community.nxp.com/thread/389002
  436. __attribute__((naked))
  437. void unused_interrupt_vector(void)
  438. {
  439. __asm( ".syntax unified\n"
  440. "MOVS R0, #4 \n"
  441. "MOV R1, LR \n"
  442. "TST R0, R1 \n"
  443. "BEQ _MSP \n"
  444. "MRS R0, PSP \n"
  445. "B HardFault_HandlerC \n"
  446. "_MSP: \n"
  447. "MRS R0, MSP \n"
  448. "B HardFault_HandlerC \n"
  449. ".syntax divided\n") ;
  450. }
  451. __attribute__((weak))
  452. void HardFault_HandlerC(unsigned int *hardfault_args)
  453. {
  454. volatile unsigned int nn ;
  455. #ifdef PRINT_DEBUG_STUFF
  456. volatile unsigned int stacked_r0 ;
  457. volatile unsigned int stacked_r1 ;
  458. volatile unsigned int stacked_r2 ;
  459. volatile unsigned int stacked_r3 ;
  460. volatile unsigned int stacked_r12 ;
  461. volatile unsigned int stacked_lr ;
  462. volatile unsigned int stacked_pc ;
  463. volatile unsigned int stacked_psr ;
  464. volatile unsigned int _CFSR ;
  465. volatile unsigned int _HFSR ;
  466. volatile unsigned int _DFSR ;
  467. volatile unsigned int _AFSR ;
  468. volatile unsigned int _BFAR ;
  469. volatile unsigned int _MMAR ;
  470. volatile unsigned int addr ;
  471. stacked_r0 = ((unsigned int)hardfault_args[0]) ;
  472. stacked_r1 = ((unsigned int)hardfault_args[1]) ;
  473. stacked_r2 = ((unsigned int)hardfault_args[2]) ;
  474. stacked_r3 = ((unsigned int)hardfault_args[3]) ;
  475. stacked_r12 = ((unsigned int)hardfault_args[4]) ;
  476. stacked_lr = ((unsigned int)hardfault_args[5]) ;
  477. stacked_pc = ((unsigned int)hardfault_args[6]) ;
  478. stacked_psr = ((unsigned int)hardfault_args[7]) ;
  479. // Configurable Fault Status Register
  480. // Consists of MMSR, BFSR and UFSR
  481. //(n & ( 1 << k )) >> k
  482. _CFSR = (*((volatile unsigned int *)(0xE000ED28))) ;
  483. // Hard Fault Status Register
  484. _HFSR = (*((volatile unsigned int *)(0xE000ED2C))) ;
  485. // Debug Fault Status Register
  486. _DFSR = (*((volatile unsigned int *)(0xE000ED30))) ;
  487. // Auxiliary Fault Status Register
  488. _AFSR = (*((volatile unsigned int *)(0xE000ED3C))) ;
  489. // Read the Fault Address Registers. These may not contain valid values.
  490. // Check BFARVALID/MMARVALID to see if they are valid values
  491. // MemManage Fault Address Register
  492. _MMAR = (*((volatile unsigned int *)(0xE000ED34))) ;
  493. // Bus Fault Address Register
  494. _BFAR = (*((volatile unsigned int *)(0xE000ED38))) ;
  495. //__asm("BKPT #0\n") ; // Break into the debugger // NO Debugger here.
  496. asm volatile("mrs %0, ipsr\n" : "=r" (addr)::);
  497. printf("\nFault irq %d\n", addr & 0x1FF);
  498. printf(" stacked_r0 :: %x\n", stacked_r0);
  499. printf(" stacked_r1 :: %x\n", stacked_r1);
  500. printf(" stacked_r2 :: %x\n", stacked_r2);
  501. printf(" stacked_r3 :: %x\n", stacked_r3);
  502. printf(" stacked_r12 :: %x\n", stacked_r12);
  503. printf(" stacked_lr :: %x\n", stacked_lr);
  504. printf(" stacked_pc :: %x\n", stacked_pc);
  505. printf(" stacked_psr :: %x\n", stacked_psr);
  506. printf(" _CFSR :: %x\n", _CFSR);
  507. if(_CFSR > 0){
  508. //Memory Management Faults
  509. if((_CFSR & 1) == 1){
  510. printf(" (IACCVIOL) Instruction Access Violation\n");
  511. } else if(((_CFSR & (0x02))>>1) == 1){
  512. printf(" (DACCVIOL) Data Access Violation\n");
  513. } else if(((_CFSR & (0x08))>>3) == 1){
  514. printf(" (MUNSTKERR) MemMange Fault on Unstacking\n");
  515. } else if(((_CFSR & (0x10))>>4) == 1){
  516. printf(" (MSTKERR) MemMange Fault on stacking\n");
  517. } else if(((_CFSR & (0x20))>>5) == 1){
  518. printf(" (MLSPERR) MemMange Fault on FP Lazy State\n");
  519. }
  520. if(((_CFSR & (0x80))>>7) == 1){
  521. printf(" (MMARVALID) MemMange Fault Address Valid\n");
  522. }
  523. //Bus Fault Status Register
  524. if(((_CFSR & 0x100)>>8) == 1){
  525. printf(" (IBUSERR) Instruction Bus Error\n");
  526. } else if(((_CFSR & (0x200))>>9) == 1){
  527. printf(" (PRECISERR) Data bus error(address in BFAR)\n");
  528. } else if(((_CFSR & (0x400))>>10) == 1){
  529. printf(" (IMPRECISERR) Data bus error but address not related to instruction\n");
  530. } else if(((_CFSR & (0x800))>>11) == 1){
  531. printf(" (UNSTKERR) Bus Fault on unstacking for a return from exception \n");
  532. } else if(((_CFSR & (0x1000))>>12) == 1){
  533. printf(" (STKERR) Bus Fault on stacking for exception entry\n");
  534. } else if(((_CFSR & (0x2000))>>13) == 1){
  535. printf(" (LSPERR) Bus Fault on FP lazy state preservation\n");
  536. }
  537. if(((_CFSR & (0x8000))>>15) == 1){
  538. printf(" (BFARVALID) Bus Fault Address Valid\n");
  539. }
  540. //Usuage Fault Status Register
  541. if(((_CFSR & 0x10000)>>16) == 1){
  542. printf(" (UNDEFINSTR) Undefined instruction\n");
  543. } else if(((_CFSR & (0x20000))>>17) == 1){
  544. printf(" (INVSTATE) Instruction makes illegal use of EPSR)\n");
  545. } else if(((_CFSR & (0x40000))>>18) == 1){
  546. printf(" (INVPC) Usage fault: invalid EXC_RETURN\n");
  547. } else if(((_CFSR & (0x80000))>>19) == 1){
  548. printf(" (NOCP) No Coprocessor \n");
  549. } else if(((_CFSR & (0x1000000))>>24) == 1){
  550. printf(" (UNALIGNED) Unaligned access UsageFault\n");
  551. } else if(((_CFSR & (0x2000000))>>25) == 1){
  552. printf(" (DIVBYZERO) Divide by zero\n");
  553. }
  554. }
  555. printf(" _HFSR :: %x\n", _HFSR);
  556. if(_HFSR > 0){
  557. //Memory Management Faults
  558. if(((_HFSR & (0x02))>>1) == 1){
  559. printf(" (VECTTBL) Bus Fault on Vec Table Read\n");
  560. } else if(((_HFSR & (0x40000000))>>30) == 1){
  561. printf(" (FORCED) Forced Hard Fault\n");
  562. } else if(((_HFSR & (0x80000000))>>31) == 31){
  563. printf(" (DEBUGEVT) Reserved for Debug\n");
  564. }
  565. }
  566. printf(" _DFSR :: %x\n", _DFSR);
  567. printf(" _AFSR :: %x\n", _AFSR);
  568. printf(" _BFAR :: %x\n", _BFAR);
  569. printf(" _MMAR :: %x\n", _MMAR);
  570. #endif
  571. IOMUXC_SW_MUX_CTL_PAD_GPIO_B0_03 = 5; // pin 13
  572. IOMUXC_SW_PAD_CTL_PAD_GPIO_B0_03 = IOMUXC_PAD_DSE(7);
  573. GPIO2_GDIR |= (1 << 3);
  574. GPIO2_DR_SET = (1 << 3);
  575. GPIO2_DR_CLEAR = (1 << 3); //digitalWrite(13, LOW);
  576. if ( F_CPU_ACTUAL >= 600000000 )
  577. set_arm_clock(300000000);
  578. while (1)
  579. {
  580. GPIO2_DR_SET = (1 << 3); //digitalWrite(13, HIGH);
  581. // digitalWrite(13, HIGH);
  582. for (nn = 0; nn < 2000000/2; nn++) ;
  583. GPIO2_DR_CLEAR = (1 << 3); //digitalWrite(13, LOW);
  584. // digitalWrite(13, LOW);
  585. for (nn = 0; nn < 18000000/2; nn++) ;
  586. }
  587. }
  588. __attribute__((weak))
  589. void userDebugDump(){
  590. volatile unsigned int nn;
  591. printf("\nuserDebugDump() in startup.c ___ \n");
  592. while (1)
  593. {
  594. GPIO2_DR_SET = (1 << 3); //digitalWrite(13, HIGH);
  595. // digitalWrite(13, HIGH);
  596. for (nn = 0; nn < 2000000; nn++) ;
  597. GPIO2_DR_CLEAR = (1 << 3); //digitalWrite(13, LOW);
  598. // digitalWrite(13, LOW);
  599. for (nn = 0; nn < 18000000; nn++) ;
  600. GPIO2_DR_SET = (1 << 3); //digitalWrite(13, HIGH);
  601. // digitalWrite(13, HIGH);
  602. for (nn = 0; nn < 20000000; nn++) ;
  603. GPIO2_DR_CLEAR = (1 << 3); //digitalWrite(13, LOW);
  604. // digitalWrite(13, LOW);
  605. for (nn = 0; nn < 10000000; nn++) ;
  606. }
  607. }
  608. __attribute__((weak))
  609. void PJRCunused_interrupt_vector(void)
  610. {
  611. // TODO: polling Serial to complete buffered transmits
  612. #ifdef PRINT_DEBUG_STUFF
  613. uint32_t addr;
  614. asm volatile("mrs %0, ipsr\n" : "=r" (addr)::);
  615. printf("\nirq %d\n", addr & 0x1FF);
  616. asm("ldr %0, [sp, #52]" : "=r" (addr) ::);
  617. printf(" %x\n", addr);
  618. asm("ldr %0, [sp, #48]" : "=r" (addr) ::);
  619. printf(" %x\n", addr);
  620. asm("ldr %0, [sp, #44]" : "=r" (addr) ::);
  621. printf(" %x\n", addr);
  622. asm("ldr %0, [sp, #40]" : "=r" (addr) ::);
  623. printf(" %x\n", addr);
  624. asm("ldr %0, [sp, #36]" : "=r" (addr) ::);
  625. printf(" %x\n", addr);
  626. asm("ldr %0, [sp, #33]" : "=r" (addr) ::);
  627. printf(" %x\n", addr);
  628. asm("ldr %0, [sp, #34]" : "=r" (addr) ::);
  629. printf(" %x\n", addr);
  630. asm("ldr %0, [sp, #28]" : "=r" (addr) ::);
  631. printf(" %x\n", addr);
  632. asm("ldr %0, [sp, #24]" : "=r" (addr) ::);
  633. printf(" %x\n", addr);
  634. asm("ldr %0, [sp, #20]" : "=r" (addr) ::);
  635. printf(" %x\n", addr);
  636. asm("ldr %0, [sp, #16]" : "=r" (addr) ::);
  637. printf(" %x\n", addr);
  638. asm("ldr %0, [sp, #12]" : "=r" (addr) ::);
  639. printf(" %x\n", addr);
  640. asm("ldr %0, [sp, #8]" : "=r" (addr) ::);
  641. printf(" %x\n", addr);
  642. asm("ldr %0, [sp, #4]" : "=r" (addr) ::);
  643. printf(" %x\n", addr);
  644. asm("ldr %0, [sp, #0]" : "=r" (addr) ::);
  645. printf(" %x\n", addr);
  646. #endif
  647. #if 1
  648. if ( F_CPU_ACTUAL >= 600000000 )
  649. set_arm_clock(100000000);
  650. IOMUXC_SW_MUX_CTL_PAD_GPIO_B0_03 = 5; // pin 13
  651. IOMUXC_SW_PAD_CTL_PAD_GPIO_B0_03 = IOMUXC_PAD_DSE(7);
  652. GPIO2_GDIR |= (1<<3);
  653. GPIO2_DR_SET = (1<<3);
  654. while (1) {
  655. volatile uint32_t n;
  656. GPIO2_DR_SET = (1<<3); //digitalWrite(13, HIGH);
  657. for (n=0; n < 2000000/6; n++) ;
  658. GPIO2_DR_CLEAR = (1<<3); //digitalWrite(13, LOW);
  659. for (n=0; n < 1500000/6; n++) ;
  660. }
  661. #else
  662. if ( F_CPU_ACTUAL >= 600000000 )
  663. set_arm_clock(100000000);
  664. while (1) asm ("WFI");
  665. #endif
  666. }
  667. __attribute__((section(".startup"), optimize("no-tree-loop-distribute-patterns")))
  668. static void memory_copy(uint32_t *dest, const uint32_t *src, uint32_t *dest_end)
  669. {
  670. if (dest == src) return;
  671. while (dest < dest_end) {
  672. *dest++ = *src++;
  673. }
  674. }
  675. __attribute__((section(".startup"), optimize("no-tree-loop-distribute-patterns")))
  676. static void memory_clear(uint32_t *dest, uint32_t *dest_end)
  677. {
  678. while (dest < dest_end) {
  679. *dest++ = 0;
  680. }
  681. }
  682. // syscall functions need to be in the same C file as the entry point "ResetVector"
  683. // otherwise the linker will discard them in some cases.
  684. #include <errno.h>
  685. // from the linker script
  686. extern unsigned long _heap_start;
  687. extern unsigned long _heap_end;
  688. char *__brkval = (char *)&_heap_start;
  689. void * _sbrk(int incr)
  690. {
  691. char *prev = __brkval;
  692. if (incr != 0) {
  693. if (prev + incr > (char *)&_heap_end) {
  694. errno = ENOMEM;
  695. return (void *)-1;
  696. }
  697. __brkval = prev + incr;
  698. }
  699. return prev;
  700. }
  701. __attribute__((weak))
  702. int _read(int file, char *ptr, int len)
  703. {
  704. return 0;
  705. }
  706. __attribute__((weak))
  707. int _close(int fd)
  708. {
  709. return -1;
  710. }
  711. #include <sys/stat.h>
  712. __attribute__((weak))
  713. int _fstat(int fd, struct stat *st)
  714. {
  715. st->st_mode = S_IFCHR;
  716. return 0;
  717. }
  718. __attribute__((weak))
  719. int _isatty(int fd)
  720. {
  721. return 1;
  722. }
  723. __attribute__((weak))
  724. int _lseek(int fd, long long offset, int whence)
  725. {
  726. return -1;
  727. }
  728. __attribute__((weak))
  729. void _exit(int status)
  730. {
  731. while (1) asm ("WFI");
  732. }
  733. __attribute__((weak))
  734. void __cxa_pure_virtual()
  735. {
  736. while (1) asm ("WFI");
  737. }
  738. __attribute__((weak))
  739. int __cxa_guard_acquire (char *g)
  740. {
  741. return !(*g);
  742. }
  743. __attribute__((weak))
  744. void __cxa_guard_release(char *g)
  745. {
  746. *g = 1;
  747. }
  748. __attribute__((weak))
  749. void abort(void)
  750. {
  751. while (1) asm ("WFI");
  752. }