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.

300 linhas
8.5KB

  1. #include "imxrt.h"
  2. #include "wiring.h"
  3. #include "debug/printf.h"
  4. // from the linker
  5. extern unsigned long _stextload;
  6. extern unsigned long _stext;
  7. extern unsigned long _etext;
  8. extern unsigned long _sdataload;
  9. extern unsigned long _sdata;
  10. extern unsigned long _edata;
  11. extern unsigned long _sbss;
  12. extern unsigned long _ebss;
  13. __attribute__ ((used, aligned(1024)))
  14. void (* _VectorsRam[160+16])(void);
  15. static void memory_copy(uint32_t *dest, const uint32_t *src, uint32_t *dest_end);
  16. static void memory_clear(uint32_t *dest, uint32_t *dest_end);
  17. static void configure_systick(void);
  18. extern void systick_isr(void);
  19. void configure_cache(void);
  20. void unused_interrupt_vector(void);
  21. __attribute__((section(".startup")))
  22. void ResetHandler(void)
  23. {
  24. unsigned int i;
  25. //force the stack to begin at some arbitrary location
  26. //__asm__ volatile("mov sp, %0" : : "r" (0x20010000) : );
  27. // pin 13 - if startup crashes, use this to turn on the LED early for troubleshooting
  28. //IOMUXC_SW_MUX_CTL_PAD_GPIO_B0_03 = 5;
  29. //IOMUXC_SW_PAD_CTL_PAD_GPIO_B0_03 = IOMUXC_PAD_DSE(7);
  30. //GPIO2_GDIR |= (1<<3);
  31. //GPIO2_DR_SET = (1<<3);
  32. memory_copy(&_stext, &_stextload, &_etext);
  33. memory_copy(&_sdata, &_sdataload, &_edata);
  34. memory_clear(&_sbss, &_ebss);
  35. for (i=0; i < 176; i++) _VectorsRam[i] = &unused_interrupt_vector;
  36. SCB_VTOR = (uint32_t)_VectorsRam;
  37. // must enable PRINT_DEBUG_STUFF in debug/print.h
  38. printf_init();
  39. printf("\n***********IMXRT Startup**********\n");
  40. printf("test %d %d %d\n", 1, -1234567, 3);
  41. configure_cache();
  42. configure_systick();
  43. #if 1
  44. //uint32_t pll1;
  45. //uint32_t n =
  46. //pll = CCM_ANALOG_PLL_ARM;
  47. printf("ARM PLL = %08lX\n", CCM_ANALOG_PLL_ARM);
  48. uint32_t cdcdr = CCM_CBCDR;
  49. uint32_t cbcmr = CCM_CBCMR;
  50. printf("AHB divisor = %ld\n", ((cdcdr >> 10) & 7) + 1);
  51. printf("IPG divisor = %ld\n", ((cdcdr >> 8) & 3) + 1);
  52. if (cdcdr & CCM_CBCDR_PERIPH_CLK_SEL) {
  53. printf("using periph_clk2_clk_divided\n");
  54. } else {
  55. printf("using pre_periph_clk_sel\n");
  56. uint32_t n = (cbcmr >> 19) & 3;
  57. if (n == 0) {
  58. printf("using PLL2\n");
  59. } else if (n == 1) {
  60. printf("using PLL2 PFD2\n");
  61. } else if (n == 2) {
  62. printf("using PLL2 PFD0\n");
  63. } else {
  64. printf("using PLL1\n");
  65. }
  66. }
  67. //set_arm_clock(300000000);
  68. #endif
  69. printf("before setup\n");
  70. setup();
  71. printf("after setup\n");
  72. while (1) {
  73. printf("loop\n");
  74. loop();
  75. }
  76. }
  77. // ARM SysTick is used for most Ardiuno timing functions, delay(), millis(),
  78. // micros(). SysTick can run from either the ARM core clock, or from an
  79. // "external" clock. NXP documents it as "24 MHz XTALOSC can be the external
  80. // clock source of SYSTICK" (RT1052 ref manual, rev 1, page 411). However,
  81. // NXP actually hid an undocumented divide-by-240 circuit in the hardware, so
  82. // the external clock is really 100 kHz. We use this clock rather than the
  83. // ARM clock, to allow SysTick to maintain correct timing even when we change
  84. // the ARM clock to run at different speeds.
  85. #define SYSTICK_EXT_FREQ 100000
  86. static void configure_systick(void)
  87. {
  88. _VectorsRam[15] = systick_isr;
  89. SYST_RVR = (SYSTICK_EXT_FREQ / 1000) - 1;
  90. SYST_CVR = 0;
  91. SYST_CSR = SYST_CSR_TICKINT | SYST_CSR_ENABLE;
  92. ARM_DEMCR |= ARM_DEMCR_TRCENA;
  93. ARM_DWT_CTRL |= ARM_DWT_CTRL_CYCCNTENA; // turn on cycle counter
  94. }
  95. // concise defines for SCB_MPU_RASR and SCB_MPU_RBAR, ARM DDI0403E, pg 696
  96. #define NOEXEC SCB_MPU_RASR_XN
  97. #define READONLY SCB_MPU_RASR_AP(7)
  98. #define READWRITE SCB_MPU_RASR_AP(3)
  99. #define NOACCESS SCB_MPU_RASR_AP(0)
  100. #define MEM_CACHE_WT SCB_MPU_RASR_TEX(0) | SCB_MPU_RASR_C
  101. #define MEM_CACHE_WB SCB_MPU_RASR_TEX(0) | SCB_MPU_RASR_C | SCB_MPU_RASR_B
  102. #define MEM_CACHE_WBWA SCB_MPU_RASR_TEX(1) | SCB_MPU_RASR_C | SCB_MPU_RASR_B
  103. #define MEM_NOCACHE SCB_MPU_RASR_TEX(1)
  104. #define DEV_NOCACHE SCB_MPU_RASR_TEX(2)
  105. #define SIZE_128K (SCB_MPU_RASR_SIZE(16) | SCB_MPU_RASR_ENABLE)
  106. #define SIZE_256K (SCB_MPU_RASR_SIZE(17) | SCB_MPU_RASR_ENABLE)
  107. #define SIZE_512K (SCB_MPU_RASR_SIZE(18) | SCB_MPU_RASR_ENABLE)
  108. #define SIZE_1M (SCB_MPU_RASR_SIZE(19) | SCB_MPU_RASR_ENABLE)
  109. #define SIZE_2M (SCB_MPU_RASR_SIZE(20) | SCB_MPU_RASR_ENABLE)
  110. #define SIZE_4M (SCB_MPU_RASR_SIZE(21) | SCB_MPU_RASR_ENABLE)
  111. #define SIZE_8M (SCB_MPU_RASR_SIZE(22) | SCB_MPU_RASR_ENABLE)
  112. #define SIZE_16M (SCB_MPU_RASR_SIZE(23) | SCB_MPU_RASR_ENABLE)
  113. #define SIZE_32M (SCB_MPU_RASR_SIZE(24) | SCB_MPU_RASR_ENABLE)
  114. #define SIZE_64M (SCB_MPU_RASR_SIZE(25) | SCB_MPU_RASR_ENABLE)
  115. #define REGION(n) (SCB_MPU_RBAR_REGION(n) | SCB_MPU_RBAR_VALID)
  116. __attribute__((section(".progmem")))
  117. void configure_cache(void)
  118. {
  119. //printf("MPU_TYPE = %08lX\n", SCB_MPU_TYPE);
  120. //printf("CCR = %08lX\n", SCB_CCR);
  121. // TODO: check if caches already active - skip?
  122. SCB_MPU_CTRL = 0; // turn off MPU
  123. SCB_MPU_RBAR = 0x00000000 | REGION(0); // ITCM
  124. SCB_MPU_RASR = MEM_NOCACHE | READWRITE | SIZE_512K;
  125. SCB_MPU_RBAR = 0x00200000 | REGION(1); // Boot ROM
  126. SCB_MPU_RASR = MEM_CACHE_WT | READONLY | SIZE_128K;
  127. SCB_MPU_RBAR = 0x20000000 | REGION(2); // DTCM
  128. SCB_MPU_RASR = MEM_NOCACHE | READWRITE | NOEXEC | SIZE_512K;
  129. SCB_MPU_RBAR = 0x20200000 | REGION(3); // RAM (AXI bus)
  130. SCB_MPU_RASR = MEM_CACHE_WBWA | READWRITE | NOEXEC | SIZE_1M;
  131. SCB_MPU_RBAR = 0x40000000 | REGION(4); // Peripherals
  132. SCB_MPU_RASR = DEV_NOCACHE | READWRITE | NOEXEC | SIZE_64M;
  133. SCB_MPU_RBAR = 0x60000000 | REGION(5); // QSPI Flash
  134. SCB_MPU_RASR = MEM_CACHE_WBWA | READONLY | SIZE_16M;
  135. // TODO: 32 byte sub-region at 0x00000000 with NOACCESS, to trap NULL pointer deref
  136. // TODO: protect access to power supply config
  137. // TODO: 32 byte sub-region at end of .bss section with NOACCESS, to trap stack overflow
  138. SCB_MPU_CTRL = SCB_MPU_CTRL_ENABLE;
  139. // cache enable, ARM DDI0403E, pg 628
  140. asm("dsb");
  141. asm("isb");
  142. SCB_CACHE_ICIALLU = 0;
  143. asm("dsb");
  144. asm("isb");
  145. SCB_CCR |= (SCB_CCR_IC | SCB_CCR_DC);
  146. }
  147. uint32_t set_arm_clock(uint32_t frequency)
  148. {
  149. if (!(CCM_CBCDR & CCM_CBCDR_PERIPH_CLK_SEL)) {
  150. //print("need to switch to stable clock while reconfigure of ARM PLL\n");
  151. const uint32_t need1s = CCM_ANALOG_PLL_USB1_ENABLE | CCM_ANALOG_PLL_USB1_POWER |
  152. CCM_ANALOG_PLL_USB1_LOCK | CCM_ANALOG_PLL_USB1_EN_USB_CLKS;
  153. if ((CCM_ANALOG_PLL_USB1 & need1s) == need1s) {
  154. //print(" run temporarily from USB/4 (120 MHz)\n");
  155. } else {
  156. //print(" run temporarily from crystal (24 MHz)\n");
  157. }
  158. } else {
  159. //print("already running from an alternate clock, ok to mess with ARM PLL\n");
  160. }
  161. // if SYS PLL running at 528 MHz
  162. // if frequency == 528
  163. // if frequency == 396
  164. // if frequency == 352
  165. //
  166. return frequency;
  167. }
  168. __attribute__((section(".progmem")))
  169. void usb_pll_start()
  170. {
  171. while (1) {
  172. uint32_t n = CCM_ANALOG_PLL_USB1; // pg 759
  173. //printf("CCM_ANALOG_PLL_USB1=%08lX\r\n", n);
  174. if (n & CCM_ANALOG_PLL_USB1_DIV_SELECT) {
  175. //print(" ERROR, 528 MHz mode!\r\n"); // never supposed to use this mode!
  176. CCM_ANALOG_PLL_USB1_CLR = 0xC000; // bypass 24 MHz
  177. CCM_ANALOG_PLL_USB1_SET = CCM_ANALOG_PLL_USB1_BYPASS; // bypass
  178. CCM_ANALOG_PLL_USB1_CLR = CCM_ANALOG_PLL_USB1_POWER | // power down
  179. CCM_ANALOG_PLL_USB1_DIV_SELECT | // use 480 MHz
  180. CCM_ANALOG_PLL_USB1_ENABLE | // disable
  181. CCM_ANALOG_PLL_USB1_EN_USB_CLKS; // disable usb
  182. continue;
  183. }
  184. if (!(n & CCM_ANALOG_PLL_USB1_ENABLE)) {
  185. //print(" enable PLL\r\n");
  186. // TODO: should this be done so early, or later??
  187. CCM_ANALOG_PLL_USB1_SET = CCM_ANALOG_PLL_USB1_ENABLE;
  188. continue;
  189. }
  190. if (!(n & CCM_ANALOG_PLL_USB1_POWER)) {
  191. //print(" power up PLL\r\n");
  192. CCM_ANALOG_PLL_USB1_SET = CCM_ANALOG_PLL_USB1_POWER;
  193. continue;
  194. }
  195. if (!(n & CCM_ANALOG_PLL_USB1_LOCK)) {
  196. //print(" wait for lock\r\n");
  197. continue;
  198. }
  199. if (n & CCM_ANALOG_PLL_USB1_BYPASS) {
  200. //print(" turn off bypass\r\n");
  201. CCM_ANALOG_PLL_USB1_CLR = CCM_ANALOG_PLL_USB1_BYPASS;
  202. continue;
  203. }
  204. if (!(n & CCM_ANALOG_PLL_USB1_EN_USB_CLKS)) {
  205. //print(" enable USB clocks\r\n");
  206. CCM_ANALOG_PLL_USB1_SET = CCM_ANALOG_PLL_USB1_EN_USB_CLKS;
  207. continue;
  208. }
  209. return; // everything is as it should be :-)
  210. }
  211. }
  212. void unused_interrupt_vector(void)
  213. {
  214. // TODO: polling Serial to complete buffered transmits
  215. #if 1
  216. IOMUXC_SW_MUX_CTL_PAD_GPIO_B0_03 = 5; // pin 13
  217. IOMUXC_SW_PAD_CTL_PAD_GPIO_B0_03 = IOMUXC_PAD_DSE(7);
  218. GPIO2_GDIR |= (1<<3);
  219. GPIO2_DR_SET = (1<<3);
  220. while (1) {
  221. volatile uint32_t n;
  222. GPIO2_DR_SET = (1<<3); //digitalWrite(13, HIGH);
  223. for (n=0; n < 5000000; n++) ;
  224. GPIO2_DR_CLEAR = (1<<3); //digitalWrite(13, LOW);
  225. for (n=0; n < 4000000; n++) ;
  226. }
  227. #else
  228. while (1) {
  229. }
  230. #endif
  231. }
  232. static void memory_copy(uint32_t *dest, const uint32_t *src, uint32_t *dest_end)
  233. {
  234. if (dest == src) return;
  235. while (dest < dest_end) {
  236. *dest++ = *src++;
  237. }
  238. }
  239. static void memory_clear(uint32_t *dest, uint32_t *dest_end)
  240. {
  241. while (dest < dest_end) {
  242. *dest++ = 0;
  243. }
  244. }