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.

startup.c 16KB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  1. #include "imxrt.h"
  2. #include "wiring.h"
  3. #include "usb_dev.h"
  4. #include "debug/printf.h"
  5. // from the linker
  6. extern unsigned long _stextload;
  7. extern unsigned long _stext;
  8. extern unsigned long _etext;
  9. extern unsigned long _sdataload;
  10. extern unsigned long _sdata;
  11. extern unsigned long _edata;
  12. extern unsigned long _sbss;
  13. extern unsigned long _ebss;
  14. __attribute__ ((used, aligned(1024)))
  15. void (* _VectorsRam[NVIC_NUM_INTERRUPTS+16])(void);
  16. static void memory_copy(uint32_t *dest, const uint32_t *src, uint32_t *dest_end);
  17. static void memory_clear(uint32_t *dest, uint32_t *dest_end);
  18. static void configure_systick(void);
  19. static void reset_PFD();
  20. extern void systick_isr(void);
  21. extern void pendablesrvreq_isr(void);
  22. void configure_cache(void);
  23. void unused_interrupt_vector(void);
  24. void usb_pll_start();
  25. extern void analog_init(void); // analog.c
  26. extern void pwm_init(void); // pwm.c
  27. extern void tempmon_init(void); //tempmon.c
  28. uint32_t set_arm_clock(uint32_t frequency); // clockspeed.c
  29. extern void __libc_init_array(void); // C++ standard library
  30. __attribute__((section(".startup"), optimize("no-tree-loop-distribute-patterns")))
  31. void ResetHandler(void)
  32. {
  33. unsigned int i;
  34. //force the stack to begin at some arbitrary location
  35. //__asm__ volatile("mov sp, %0" : : "r" (0x20010000) : );
  36. // pin 13 - if startup crashes, use this to turn on the LED early for troubleshooting
  37. //IOMUXC_SW_MUX_CTL_PAD_GPIO_B0_03 = 5;
  38. //IOMUXC_SW_PAD_CTL_PAD_GPIO_B0_03 = IOMUXC_PAD_DSE(7);
  39. //GPIO2_GDIR |= (1<<3);
  40. //GPIO2_DR_SET = (1<<3); // digitalWrite(13, HIGH);
  41. // Initialize memory
  42. memory_copy(&_stext, &_stextload, &_etext);
  43. memory_copy(&_sdata, &_sdataload, &_edata);
  44. memory_clear(&_sbss, &_ebss);
  45. // enable FPU
  46. SCB_CPACR = 0x00F00000;
  47. // set up blank interrupt & exception vector table
  48. for (i=0; i < NVIC_NUM_INTERRUPTS + 16; i++) _VectorsRam[i] = &unused_interrupt_vector;
  49. for (i=0; i < NVIC_NUM_INTERRUPTS; i++) NVIC_SET_PRIORITY(i, 128);
  50. SCB_VTOR = (uint32_t)_VectorsRam;
  51. reset_PFD();
  52. // Configure clocks
  53. // TODO: make sure all affected peripherals are turned off!
  54. // PIT & GPT timers to run from 24 MHz clock (independent of CPU speed)
  55. CCM_CSCMR1 = (CCM_CSCMR1 & ~CCM_CSCMR1_PERCLK_PODF(0x3F)) | CCM_CSCMR1_PERCLK_CLK_SEL;
  56. // UARTs run from 24 MHz clock (works if PLL3 off or bypassed)
  57. CCM_CSCDR1 = (CCM_CSCDR1 & ~CCM_CSCDR1_UART_CLK_PODF(0x3F)) | CCM_CSCDR1_UART_CLK_SEL;
  58. // must enable PRINT_DEBUG_STUFF in debug/print.h
  59. printf_debug_init();
  60. printf("\n***********IMXRT Startup**********\n");
  61. printf("test %d %d %d\n", 1, -1234567, 3);
  62. configure_cache();
  63. configure_systick();
  64. usb_pll_start();
  65. reset_PFD(); //TODO: is this really needed?
  66. set_arm_clock(600000000);
  67. //set_arm_clock(984000000); Ludicrous Speed
  68. while (millis() < 20) ; // wait at least 20ms before starting USB
  69. usb_init();
  70. analog_init();
  71. pwm_init();
  72. tempmon_init();
  73. while (millis() < 300) ; // wait at least 300ms before calling user code
  74. printf("before C++ constructors\n");
  75. __libc_init_array();
  76. printf("after C++ constructors\n");
  77. printf("before setup\n");
  78. setup();
  79. printf("after setup\n");
  80. while (1) {
  81. //printf("loop\n");
  82. loop();
  83. }
  84. }
  85. // ARM SysTick is used for most Ardiuno timing functions, delay(), millis(),
  86. // micros(). SysTick can run from either the ARM core clock, or from an
  87. // "external" clock. NXP documents it as "24 MHz XTALOSC can be the external
  88. // clock source of SYSTICK" (RT1052 ref manual, rev 1, page 411). However,
  89. // NXP actually hid an undocumented divide-by-240 circuit in the hardware, so
  90. // the external clock is really 100 kHz. We use this clock rather than the
  91. // ARM clock, to allow SysTick to maintain correct timing even when we change
  92. // the ARM clock to run at different speeds.
  93. #define SYSTICK_EXT_FREQ 100000
  94. extern volatile uint32_t systick_cycle_count;
  95. static void configure_systick(void)
  96. {
  97. _VectorsRam[14] = pendablesrvreq_isr;
  98. _VectorsRam[15] = systick_isr;
  99. SYST_RVR = (SYSTICK_EXT_FREQ / 1000) - 1;
  100. SYST_CVR = 0;
  101. SYST_CSR = SYST_CSR_TICKINT | SYST_CSR_ENABLE;
  102. SCB_SHPR3 = 0x20200000; // Systick, pendablesrvreq_isr = priority 32;
  103. ARM_DEMCR |= ARM_DEMCR_TRCENA;
  104. ARM_DWT_CTRL |= ARM_DWT_CTRL_CYCCNTENA; // turn on cycle counter
  105. systick_cycle_count = ARM_DWT_CYCCNT; // compiled 0, corrected w/1st systick
  106. }
  107. // concise defines for SCB_MPU_RASR and SCB_MPU_RBAR, ARM DDI0403E, pg 696
  108. #define NOEXEC SCB_MPU_RASR_XN
  109. #define READONLY SCB_MPU_RASR_AP(7)
  110. #define READWRITE SCB_MPU_RASR_AP(3)
  111. #define NOACCESS SCB_MPU_RASR_AP(0)
  112. #define MEM_CACHE_WT SCB_MPU_RASR_TEX(0) | SCB_MPU_RASR_C
  113. #define MEM_CACHE_WB SCB_MPU_RASR_TEX(0) | SCB_MPU_RASR_C | SCB_MPU_RASR_B
  114. #define MEM_CACHE_WBWA SCB_MPU_RASR_TEX(1) | SCB_MPU_RASR_C | SCB_MPU_RASR_B
  115. #define MEM_NOCACHE SCB_MPU_RASR_TEX(1)
  116. #define DEV_NOCACHE SCB_MPU_RASR_TEX(2)
  117. #define SIZE_128K (SCB_MPU_RASR_SIZE(16) | SCB_MPU_RASR_ENABLE)
  118. #define SIZE_256K (SCB_MPU_RASR_SIZE(17) | SCB_MPU_RASR_ENABLE)
  119. #define SIZE_512K (SCB_MPU_RASR_SIZE(18) | SCB_MPU_RASR_ENABLE)
  120. #define SIZE_1M (SCB_MPU_RASR_SIZE(19) | SCB_MPU_RASR_ENABLE)
  121. #define SIZE_2M (SCB_MPU_RASR_SIZE(20) | SCB_MPU_RASR_ENABLE)
  122. #define SIZE_4M (SCB_MPU_RASR_SIZE(21) | SCB_MPU_RASR_ENABLE)
  123. #define SIZE_8M (SCB_MPU_RASR_SIZE(22) | SCB_MPU_RASR_ENABLE)
  124. #define SIZE_16M (SCB_MPU_RASR_SIZE(23) | SCB_MPU_RASR_ENABLE)
  125. #define SIZE_32M (SCB_MPU_RASR_SIZE(24) | SCB_MPU_RASR_ENABLE)
  126. #define SIZE_64M (SCB_MPU_RASR_SIZE(25) | SCB_MPU_RASR_ENABLE)
  127. #define REGION(n) (SCB_MPU_RBAR_REGION(n) | SCB_MPU_RBAR_VALID)
  128. __attribute__((section(".progmem")))
  129. void configure_cache(void)
  130. {
  131. //printf("MPU_TYPE = %08lX\n", SCB_MPU_TYPE);
  132. //printf("CCR = %08lX\n", SCB_CCR);
  133. // TODO: check if caches already active - skip?
  134. SCB_MPU_CTRL = 0; // turn off MPU
  135. SCB_MPU_RBAR = 0x00000000 | REGION(0); // ITCM
  136. SCB_MPU_RASR = MEM_NOCACHE | READWRITE | SIZE_512K;
  137. SCB_MPU_RBAR = 0x00200000 | REGION(1); // Boot ROM
  138. SCB_MPU_RASR = MEM_CACHE_WT | READONLY | SIZE_128K;
  139. SCB_MPU_RBAR = 0x20000000 | REGION(2); // DTCM
  140. SCB_MPU_RASR = MEM_NOCACHE | READWRITE | NOEXEC | SIZE_512K;
  141. SCB_MPU_RBAR = 0x20200000 | REGION(3); // RAM (AXI bus)
  142. SCB_MPU_RASR = MEM_CACHE_WBWA | READWRITE | NOEXEC | SIZE_1M;
  143. SCB_MPU_RBAR = 0x40000000 | REGION(4); // Peripherals
  144. SCB_MPU_RASR = DEV_NOCACHE | READWRITE | NOEXEC | SIZE_64M;
  145. SCB_MPU_RBAR = 0x60000000 | REGION(5); // QSPI Flash
  146. SCB_MPU_RASR = MEM_CACHE_WBWA | READONLY | SIZE_16M;
  147. // TODO: 32 byte sub-region at 0x00000000 with NOACCESS, to trap NULL pointer deref
  148. // TODO: protect access to power supply config
  149. // TODO: 32 byte sub-region at end of .bss section with NOACCESS, to trap stack overflow
  150. SCB_MPU_CTRL = SCB_MPU_CTRL_ENABLE;
  151. // cache enable, ARM DDI0403E, pg 628
  152. asm("dsb");
  153. asm("isb");
  154. SCB_CACHE_ICIALLU = 0;
  155. asm("dsb");
  156. asm("isb");
  157. SCB_CCR |= (SCB_CCR_IC | SCB_CCR_DC);
  158. }
  159. __attribute__((section(".progmem")))
  160. void usb_pll_start()
  161. {
  162. while (1) {
  163. uint32_t n = CCM_ANALOG_PLL_USB1; // pg 759
  164. printf("CCM_ANALOG_PLL_USB1=%08lX\n", n);
  165. if (n & CCM_ANALOG_PLL_USB1_DIV_SELECT) {
  166. printf(" ERROR, 528 MHz mode!\n"); // never supposed to use this mode!
  167. CCM_ANALOG_PLL_USB1_CLR = 0xC000; // bypass 24 MHz
  168. CCM_ANALOG_PLL_USB1_SET = CCM_ANALOG_PLL_USB1_BYPASS; // bypass
  169. CCM_ANALOG_PLL_USB1_CLR = CCM_ANALOG_PLL_USB1_POWER | // power down
  170. CCM_ANALOG_PLL_USB1_DIV_SELECT | // use 480 MHz
  171. CCM_ANALOG_PLL_USB1_ENABLE | // disable
  172. CCM_ANALOG_PLL_USB1_EN_USB_CLKS; // disable usb
  173. continue;
  174. }
  175. if (!(n & CCM_ANALOG_PLL_USB1_ENABLE)) {
  176. printf(" enable PLL\n");
  177. // TODO: should this be done so early, or later??
  178. CCM_ANALOG_PLL_USB1_SET = CCM_ANALOG_PLL_USB1_ENABLE;
  179. continue;
  180. }
  181. if (!(n & CCM_ANALOG_PLL_USB1_POWER)) {
  182. printf(" power up PLL\n");
  183. CCM_ANALOG_PLL_USB1_SET = CCM_ANALOG_PLL_USB1_POWER;
  184. continue;
  185. }
  186. if (!(n & CCM_ANALOG_PLL_USB1_LOCK)) {
  187. printf(" wait for lock\n");
  188. continue;
  189. }
  190. if (n & CCM_ANALOG_PLL_USB1_BYPASS) {
  191. printf(" turn off bypass\n");
  192. CCM_ANALOG_PLL_USB1_CLR = CCM_ANALOG_PLL_USB1_BYPASS;
  193. continue;
  194. }
  195. if (!(n & CCM_ANALOG_PLL_USB1_EN_USB_CLKS)) {
  196. printf(" enable USB clocks\n");
  197. CCM_ANALOG_PLL_USB1_SET = CCM_ANALOG_PLL_USB1_EN_USB_CLKS;
  198. continue;
  199. }
  200. return; // everything is as it should be :-)
  201. }
  202. }
  203. __attribute__((section(".progmem")))
  204. void reset_PFD()
  205. {
  206. //Reset PLL2 PFDs, set default frequencies:
  207. CCM_ANALOG_PFD_528_SET = (1 << 31) | (1 << 23) | (1 << 15) | (1 << 7);
  208. CCM_ANALOG_PFD_528 = 0x2018101B; // PFD0:352, PFD1:594, PFD2:396, PFD3:297 MHz
  209. //PLL3:
  210. CCM_ANALOG_PFD_480_SET = (1 << 31) | (1 << 23) | (1 << 15) | (1 << 7);
  211. CCM_ANALOG_PFD_480 = 0x13110D0C; // PFD0:720, PFD1:664, PFD2:508, PFD3:454 MHz
  212. }
  213. // Stack frame
  214. // xPSR
  215. // ReturnAddress
  216. // LR (R14) - typically FFFFFFF9 for IRQ or Exception
  217. // R12
  218. // R3
  219. // R2
  220. // R1
  221. // R0
  222. // Code from :: https://community.nxp.com/thread/389002
  223. __attribute__((naked))
  224. void unused_interrupt_vector(void)
  225. {
  226. __asm( ".syntax unified\n"
  227. "MOVS R0, #4 \n"
  228. "MOV R1, LR \n"
  229. "TST R0, R1 \n"
  230. "BEQ _MSP \n"
  231. "MRS R0, PSP \n"
  232. "B HardFault_HandlerC \n"
  233. "_MSP: \n"
  234. "MRS R0, MSP \n"
  235. "B HardFault_HandlerC \n"
  236. ".syntax divided\n") ;
  237. }
  238. __attribute__((weak))
  239. void HardFault_HandlerC(unsigned int *hardfault_args) {
  240. volatile unsigned int stacked_r0 ;
  241. volatile unsigned int stacked_r1 ;
  242. volatile unsigned int stacked_r2 ;
  243. volatile unsigned int stacked_r3 ;
  244. volatile unsigned int stacked_r12 ;
  245. volatile unsigned int stacked_lr ;
  246. volatile unsigned int stacked_pc ;
  247. volatile unsigned int stacked_psr ;
  248. volatile unsigned int _CFSR ;
  249. volatile unsigned int _HFSR ;
  250. volatile unsigned int _DFSR ;
  251. volatile unsigned int _AFSR ;
  252. volatile unsigned int _BFAR ;
  253. volatile unsigned int _MMAR ;
  254. volatile unsigned int addr ;
  255. volatile unsigned int nn ;
  256. stacked_r0 = ((unsigned int)hardfault_args[0]) ;
  257. stacked_r1 = ((unsigned int)hardfault_args[1]) ;
  258. stacked_r2 = ((unsigned int)hardfault_args[2]) ;
  259. stacked_r3 = ((unsigned int)hardfault_args[3]) ;
  260. stacked_r12 = ((unsigned int)hardfault_args[4]) ;
  261. stacked_lr = ((unsigned int)hardfault_args[5]) ;
  262. stacked_pc = ((unsigned int)hardfault_args[6]) ;
  263. stacked_psr = ((unsigned int)hardfault_args[7]) ;
  264. // Configurable Fault Status Register
  265. // Consists of MMSR, BFSR and UFSR
  266. _CFSR = (*((volatile unsigned int *)(0xE000ED28))) ;
  267. // Hard Fault Status Register
  268. _HFSR = (*((volatile unsigned int *)(0xE000ED2C))) ;
  269. // Debug Fault Status Register
  270. _DFSR = (*((volatile unsigned int *)(0xE000ED30))) ;
  271. // Auxiliary Fault Status Register
  272. _AFSR = (*((volatile unsigned int *)(0xE000ED3C))) ;
  273. // Read the Fault Address Registers. These may not contain valid values.
  274. // Check BFARVALID/MMARVALID to see if they are valid values
  275. // MemManage Fault Address Register
  276. _MMAR = (*((volatile unsigned int *)(0xE000ED34))) ;
  277. // Bus Fault Address Register
  278. _BFAR = (*((volatile unsigned int *)(0xE000ED38))) ;
  279. //__asm("BKPT #0\n") ; // Break into the debugger // NO Debugger here.
  280. asm volatile("mrs %0, ipsr\n" : "=r" (addr)::);
  281. printf_debug("\nFault irq %d\n", addr & 0x1FF);
  282. printf_debug(" stacked_r0 :: %x\n", stacked_r0);
  283. printf_debug(" stacked_r1 :: %x\n", stacked_r1);
  284. printf_debug(" stacked_r2 :: %x\n", stacked_r2);
  285. printf_debug(" stacked_r3 :: %x\n", stacked_r3);
  286. printf_debug(" stacked_r12 :: %x\n", stacked_r12);
  287. printf_debug(" stacked_lr :: %x\n", stacked_lr);
  288. printf_debug(" stacked_pc :: %x\n", stacked_pc);
  289. printf_debug(" stacked_psr :: %x\n", stacked_psr);
  290. printf_debug(" _CFSR :: %x\n", _CFSR);
  291. printf_debug(" _HFSR :: %x\n", _HFSR);
  292. printf_debug(" _DFSR :: %x\n", _DFSR);
  293. printf_debug(" _AFSR :: %x\n", _AFSR);
  294. printf_debug(" _BFAR :: %x\n", _BFAR);
  295. printf_debug(" _MMAR :: %x\n", _MMAR);
  296. IOMUXC_SW_MUX_CTL_PAD_GPIO_B0_03 = 5; // pin 13
  297. IOMUXC_SW_PAD_CTL_PAD_GPIO_B0_03 = IOMUXC_PAD_DSE(7);
  298. GPIO2_GDIR |= (1 << 3);
  299. GPIO2_DR_SET = (1 << 3);
  300. GPIO2_DR_CLEAR = (1 << 3); //digitalWrite(13, LOW);
  301. if ( F_CPU_ACTUAL >= 600000000 )
  302. set_arm_clock(300000000);
  303. while (1)
  304. {
  305. GPIO2_DR_SET = (1 << 3); //digitalWrite(13, HIGH);
  306. // digitalWrite(13, HIGH);
  307. for (nn = 0; nn < 2000000; nn++) ;
  308. GPIO2_DR_CLEAR = (1 << 3); //digitalWrite(13, LOW);
  309. // digitalWrite(13, LOW);
  310. for (nn = 0; nn < 18000000; nn++) ;
  311. }
  312. }
  313. __attribute__((weak))
  314. void userDebugDump(){
  315. volatile unsigned int nn;
  316. printf_debug("\nuserDebugDump() in startup.c ___ \n");
  317. while (1)
  318. {
  319. GPIO2_DR_SET = (1 << 3); //digitalWrite(13, HIGH);
  320. // digitalWrite(13, HIGH);
  321. for (nn = 0; nn < 2000000; nn++) ;
  322. GPIO2_DR_CLEAR = (1 << 3); //digitalWrite(13, LOW);
  323. // digitalWrite(13, LOW);
  324. for (nn = 0; nn < 18000000; nn++) ;
  325. GPIO2_DR_SET = (1 << 3); //digitalWrite(13, HIGH);
  326. // digitalWrite(13, HIGH);
  327. for (nn = 0; nn < 20000000; nn++) ;
  328. GPIO2_DR_CLEAR = (1 << 3); //digitalWrite(13, LOW);
  329. // digitalWrite(13, LOW);
  330. for (nn = 0; nn < 10000000; nn++) ;
  331. }
  332. }
  333. __attribute__((weak))
  334. void PJRCunused_interrupt_vector(void)
  335. {
  336. // TODO: polling Serial to complete buffered transmits
  337. #ifdef PRINT_DEBUG_STUFF
  338. uint32_t addr;
  339. asm volatile("mrs %0, ipsr\n" : "=r" (addr)::);
  340. printf("\nirq %d\n", addr & 0x1FF);
  341. asm("ldr %0, [sp, #52]" : "=r" (addr) ::);
  342. printf(" %x\n", addr);
  343. asm("ldr %0, [sp, #48]" : "=r" (addr) ::);
  344. printf(" %x\n", addr);
  345. asm("ldr %0, [sp, #44]" : "=r" (addr) ::);
  346. printf(" %x\n", addr);
  347. asm("ldr %0, [sp, #40]" : "=r" (addr) ::);
  348. printf(" %x\n", addr);
  349. asm("ldr %0, [sp, #36]" : "=r" (addr) ::);
  350. printf(" %x\n", addr);
  351. asm("ldr %0, [sp, #33]" : "=r" (addr) ::);
  352. printf(" %x\n", addr);
  353. asm("ldr %0, [sp, #34]" : "=r" (addr) ::);
  354. printf(" %x\n", addr);
  355. asm("ldr %0, [sp, #28]" : "=r" (addr) ::);
  356. printf(" %x\n", addr);
  357. asm("ldr %0, [sp, #24]" : "=r" (addr) ::);
  358. printf(" %x\n", addr);
  359. asm("ldr %0, [sp, #20]" : "=r" (addr) ::);
  360. printf(" %x\n", addr);
  361. asm("ldr %0, [sp, #16]" : "=r" (addr) ::);
  362. printf(" %x\n", addr);
  363. asm("ldr %0, [sp, #12]" : "=r" (addr) ::);
  364. printf(" %x\n", addr);
  365. asm("ldr %0, [sp, #8]" : "=r" (addr) ::);
  366. printf(" %x\n", addr);
  367. asm("ldr %0, [sp, #4]" : "=r" (addr) ::);
  368. printf(" %x\n", addr);
  369. asm("ldr %0, [sp, #0]" : "=r" (addr) ::);
  370. printf(" %x\n", addr);
  371. #endif
  372. #if 1
  373. if ( F_CPU_ACTUAL >= 600000000 )
  374. set_arm_clock(100000000);
  375. IOMUXC_SW_MUX_CTL_PAD_GPIO_B0_03 = 5; // pin 13
  376. IOMUXC_SW_PAD_CTL_PAD_GPIO_B0_03 = IOMUXC_PAD_DSE(7);
  377. GPIO2_GDIR |= (1<<3);
  378. GPIO2_DR_SET = (1<<3);
  379. while (1) {
  380. volatile uint32_t n;
  381. GPIO2_DR_SET = (1<<3); //digitalWrite(13, HIGH);
  382. for (n=0; n < 2000000/6; n++) ;
  383. GPIO2_DR_CLEAR = (1<<3); //digitalWrite(13, LOW);
  384. for (n=0; n < 1500000/6; n++) ;
  385. }
  386. #else
  387. if ( F_CPU_ACTUAL >= 600000000 )
  388. set_arm_clock(100000000);
  389. while (1) {
  390. }
  391. #endif
  392. }
  393. __attribute__((section(".startup"), optimize("no-tree-loop-distribute-patterns")))
  394. static void memory_copy(uint32_t *dest, const uint32_t *src, uint32_t *dest_end)
  395. {
  396. if (dest == src) return;
  397. while (dest < dest_end) {
  398. *dest++ = *src++;
  399. }
  400. }
  401. __attribute__((section(".startup"), optimize("no-tree-loop-distribute-patterns")))
  402. static void memory_clear(uint32_t *dest, uint32_t *dest_end)
  403. {
  404. while (dest < dest_end) {
  405. *dest++ = 0;
  406. }
  407. }
  408. // syscall functions need to be in the same C file as the entry point "ResetVector"
  409. // otherwise the linker will discard them in some cases.
  410. #include <errno.h>
  411. // from the linker script
  412. extern unsigned long _heap_start;
  413. extern unsigned long _heap_end;
  414. char *__brkval = (char *)&_heap_start;
  415. void * _sbrk(int incr)
  416. {
  417. char *prev = __brkval;
  418. if (incr != 0) {
  419. if (prev + incr > (char *)&_heap_end) {
  420. errno = ENOMEM;
  421. return (void *)-1;
  422. }
  423. __brkval = prev + incr;
  424. }
  425. return prev;
  426. }
  427. __attribute__((weak))
  428. int _read(int file, char *ptr, int len)
  429. {
  430. return 0;
  431. }
  432. __attribute__((weak))
  433. int _close(int fd)
  434. {
  435. return -1;
  436. }
  437. #include <sys/stat.h>
  438. __attribute__((weak))
  439. int _fstat(int fd, struct stat *st)
  440. {
  441. st->st_mode = S_IFCHR;
  442. return 0;
  443. }
  444. __attribute__((weak))
  445. int _isatty(int fd)
  446. {
  447. return 1;
  448. }
  449. __attribute__((weak))
  450. int _lseek(int fd, long long offset, int whence)
  451. {
  452. return -1;
  453. }
  454. __attribute__((weak))
  455. void _exit(int status)
  456. {
  457. while (1);
  458. }
  459. __attribute__((weak))
  460. void __cxa_pure_virtual()
  461. {
  462. while (1);
  463. }
  464. __attribute__((weak))
  465. int __cxa_guard_acquire (char *g)
  466. {
  467. return !(*g);
  468. }
  469. __attribute__((weak))
  470. void __cxa_guard_release(char *g)
  471. {
  472. *g = 1;
  473. }
  474. __attribute__((weak))
  475. void abort(void)
  476. {
  477. while (1) ;
  478. }