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.

862 lines
30KB

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