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.

357 lines
10KB

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