Teensy 4.1 core updated for C++20
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

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