PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

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