Teensy 4.1 core updated for C++20
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

416 lines
11KB

  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. uint32_t set_arm_clock(uint32_t frequency);
  26. __attribute__((section(".startup"), optimize("no-tree-loop-distribute-patterns")))
  27. void ResetHandler(void)
  28. {
  29. unsigned int i;
  30. //force the stack to begin at some arbitrary location
  31. //__asm__ volatile("mov sp, %0" : : "r" (0x20010000) : );
  32. // pin 13 - if startup crashes, use this to turn on the LED early for troubleshooting
  33. //IOMUXC_SW_MUX_CTL_PAD_GPIO_B0_03 = 5;
  34. //IOMUXC_SW_PAD_CTL_PAD_GPIO_B0_03 = IOMUXC_PAD_DSE(7);
  35. //GPIO2_GDIR |= (1<<3);
  36. //GPIO2_DR_SET = (1<<3); // digitalWrite(13, HIGH);
  37. // Initialize memory
  38. memory_copy(&_stext, &_stextload, &_etext);
  39. memory_copy(&_sdata, &_sdataload, &_edata);
  40. memory_clear(&_sbss, &_ebss);
  41. // enable FPU
  42. SCB_CPACR = 0x00F00000;
  43. // set up blank interrupt & exception vector table
  44. for (i=0; i < NVIC_NUM_INTERRUPTS + 16; i++) _VectorsRam[i] = &unused_interrupt_vector;
  45. for (i=0; i < NVIC_NUM_INTERRUPTS; i++) NVIC_SET_PRIORITY(i, 128);
  46. SCB_VTOR = (uint32_t)_VectorsRam;
  47. // Configure clocks
  48. // TODO: make sure all affected peripherals are turned off!
  49. // PIT & GPT timers to run from 24 MHz clock (independent of CPU speed)
  50. CCM_CSCMR1 = (CCM_CSCMR1 & ~CCM_CSCMR1_PERCLK_PODF(0x3F)) | CCM_CSCMR1_PERCLK_CLK_SEL;
  51. // UARTs run from 24 MHz clock (works if PLL3 off or bypassed)
  52. CCM_CSCDR1 = (CCM_CSCDR1 & ~CCM_CSCDR1_UART_CLK_PODF(0x3F)) | CCM_CSCDR1_UART_CLK_SEL;
  53. // must enable PRINT_DEBUG_STUFF in debug/print.h
  54. printf_debug_init();
  55. printf("\n***********IMXRT Startup**********\n");
  56. printf("test %d %d %d\n", 1, -1234567, 3);
  57. configure_cache();
  58. configure_systick();
  59. usb_pll_start();
  60. set_arm_clock(600000000);
  61. //set_arm_clock(984000000); Ludicrous Speed
  62. uint32_t armpll = CCM_ANALOG_PLL_ARM;
  63. uint32_t armdiv = CCM_CACRR;
  64. uint32_t cbcdr = CCM_CBCDR;
  65. uint32_t cbcmr = CCM_CBCMR;
  66. printf("ARM PLL = %u MHz\n", (armpll & 0x7F) * 12);
  67. printf("ARM divisor = %u\n", armdiv + 1);
  68. printf("AHB divisor = %u\n", ((cbcdr >> 10) & 7) + 1);
  69. printf("IPG divisor = %u\n", ((cbcdr >> 8) & 3) + 1);
  70. while (millis() < 20) ; // wait at least 20ms before starting USB
  71. usb_init();
  72. analog_init();
  73. pwm_init();
  74. while (millis() < 300) ; // wait at least 300ms before calling user code
  75. printf("before C++ constructors\n");
  76. __libc_init_array();
  77. printf("after C++ constructors\n");
  78. printf("before setup\n");
  79. setup();
  80. printf("after setup\n");
  81. while (1) {
  82. //printf("loop\n");
  83. loop();
  84. }
  85. }
  86. // ARM SysTick is used for most Ardiuno timing functions, delay(), millis(),
  87. // micros(). SysTick can run from either the ARM core clock, or from an
  88. // "external" clock. NXP documents it as "24 MHz XTALOSC can be the external
  89. // clock source of SYSTICK" (RT1052 ref manual, rev 1, page 411). However,
  90. // NXP actually hid an undocumented divide-by-240 circuit in the hardware, so
  91. // the external clock is really 100 kHz. We use this clock rather than the
  92. // ARM clock, to allow SysTick to maintain correct timing even when we change
  93. // the ARM clock to run at different speeds.
  94. #define SYSTICK_EXT_FREQ 100000
  95. static void configure_systick(void)
  96. {
  97. _VectorsRam[15] = systick_isr;
  98. SYST_RVR = (SYSTICK_EXT_FREQ / 1000) - 1;
  99. SYST_CVR = 0;
  100. SYST_CSR = SYST_CSR_TICKINT | SYST_CSR_ENABLE;
  101. SCB_SHPR3 = 0x20000000; // Systick = priority 32
  102. ARM_DEMCR |= ARM_DEMCR_TRCENA;
  103. ARM_DWT_CTRL |= ARM_DWT_CTRL_CYCCNTENA; // turn on cycle counter
  104. }
  105. // concise defines for SCB_MPU_RASR and SCB_MPU_RBAR, ARM DDI0403E, pg 696
  106. #define NOEXEC SCB_MPU_RASR_XN
  107. #define READONLY SCB_MPU_RASR_AP(7)
  108. #define READWRITE SCB_MPU_RASR_AP(3)
  109. #define NOACCESS SCB_MPU_RASR_AP(0)
  110. #define MEM_CACHE_WT SCB_MPU_RASR_TEX(0) | SCB_MPU_RASR_C
  111. #define MEM_CACHE_WB SCB_MPU_RASR_TEX(0) | SCB_MPU_RASR_C | SCB_MPU_RASR_B
  112. #define MEM_CACHE_WBWA SCB_MPU_RASR_TEX(1) | SCB_MPU_RASR_C | SCB_MPU_RASR_B
  113. #define MEM_NOCACHE SCB_MPU_RASR_TEX(1)
  114. #define DEV_NOCACHE SCB_MPU_RASR_TEX(2)
  115. #define SIZE_128K (SCB_MPU_RASR_SIZE(16) | SCB_MPU_RASR_ENABLE)
  116. #define SIZE_256K (SCB_MPU_RASR_SIZE(17) | SCB_MPU_RASR_ENABLE)
  117. #define SIZE_512K (SCB_MPU_RASR_SIZE(18) | SCB_MPU_RASR_ENABLE)
  118. #define SIZE_1M (SCB_MPU_RASR_SIZE(19) | SCB_MPU_RASR_ENABLE)
  119. #define SIZE_2M (SCB_MPU_RASR_SIZE(20) | SCB_MPU_RASR_ENABLE)
  120. #define SIZE_4M (SCB_MPU_RASR_SIZE(21) | SCB_MPU_RASR_ENABLE)
  121. #define SIZE_8M (SCB_MPU_RASR_SIZE(22) | SCB_MPU_RASR_ENABLE)
  122. #define SIZE_16M (SCB_MPU_RASR_SIZE(23) | SCB_MPU_RASR_ENABLE)
  123. #define SIZE_32M (SCB_MPU_RASR_SIZE(24) | SCB_MPU_RASR_ENABLE)
  124. #define SIZE_64M (SCB_MPU_RASR_SIZE(25) | SCB_MPU_RASR_ENABLE)
  125. #define REGION(n) (SCB_MPU_RBAR_REGION(n) | SCB_MPU_RBAR_VALID)
  126. __attribute__((section(".progmem")))
  127. void configure_cache(void)
  128. {
  129. //printf("MPU_TYPE = %08lX\n", SCB_MPU_TYPE);
  130. //printf("CCR = %08lX\n", SCB_CCR);
  131. // TODO: check if caches already active - skip?
  132. SCB_MPU_CTRL = 0; // turn off MPU
  133. SCB_MPU_RBAR = 0x00000000 | REGION(0); // ITCM
  134. SCB_MPU_RASR = MEM_NOCACHE | READWRITE | SIZE_512K;
  135. SCB_MPU_RBAR = 0x00200000 | REGION(1); // Boot ROM
  136. SCB_MPU_RASR = MEM_CACHE_WT | READONLY | SIZE_128K;
  137. SCB_MPU_RBAR = 0x20000000 | REGION(2); // DTCM
  138. SCB_MPU_RASR = MEM_NOCACHE | READWRITE | NOEXEC | SIZE_512K;
  139. SCB_MPU_RBAR = 0x20200000 | REGION(3); // RAM (AXI bus)
  140. SCB_MPU_RASR = MEM_CACHE_WBWA | READWRITE | NOEXEC | SIZE_1M;
  141. SCB_MPU_RBAR = 0x40000000 | REGION(4); // Peripherals
  142. SCB_MPU_RASR = DEV_NOCACHE | READWRITE | NOEXEC | SIZE_64M;
  143. SCB_MPU_RBAR = 0x60000000 | REGION(5); // QSPI Flash
  144. SCB_MPU_RASR = MEM_CACHE_WBWA | READONLY | SIZE_16M;
  145. // TODO: 32 byte sub-region at 0x00000000 with NOACCESS, to trap NULL pointer deref
  146. // TODO: protect access to power supply config
  147. // TODO: 32 byte sub-region at end of .bss section with NOACCESS, to trap stack overflow
  148. SCB_MPU_CTRL = SCB_MPU_CTRL_ENABLE;
  149. // cache enable, ARM DDI0403E, pg 628
  150. asm("dsb");
  151. asm("isb");
  152. SCB_CACHE_ICIALLU = 0;
  153. asm("dsb");
  154. asm("isb");
  155. SCB_CCR |= (SCB_CCR_IC | SCB_CCR_DC);
  156. }
  157. __attribute__((section(".progmem")))
  158. void usb_pll_start()
  159. {
  160. while (1) {
  161. uint32_t n = CCM_ANALOG_PLL_USB1; // pg 759
  162. printf("CCM_ANALOG_PLL_USB1=%08lX\n", n);
  163. if (n & CCM_ANALOG_PLL_USB1_DIV_SELECT) {
  164. printf(" ERROR, 528 MHz mode!\n"); // never supposed to use this mode!
  165. CCM_ANALOG_PLL_USB1_CLR = 0xC000; // bypass 24 MHz
  166. CCM_ANALOG_PLL_USB1_SET = CCM_ANALOG_PLL_USB1_BYPASS; // bypass
  167. CCM_ANALOG_PLL_USB1_CLR = CCM_ANALOG_PLL_USB1_POWER | // power down
  168. CCM_ANALOG_PLL_USB1_DIV_SELECT | // use 480 MHz
  169. CCM_ANALOG_PLL_USB1_ENABLE | // disable
  170. CCM_ANALOG_PLL_USB1_EN_USB_CLKS; // disable usb
  171. continue;
  172. }
  173. if (!(n & CCM_ANALOG_PLL_USB1_ENABLE)) {
  174. printf(" enable PLL\n");
  175. // TODO: should this be done so early, or later??
  176. CCM_ANALOG_PLL_USB1_SET = CCM_ANALOG_PLL_USB1_ENABLE;
  177. continue;
  178. }
  179. if (!(n & CCM_ANALOG_PLL_USB1_POWER)) {
  180. printf(" power up PLL\n");
  181. CCM_ANALOG_PLL_USB1_SET = CCM_ANALOG_PLL_USB1_POWER;
  182. continue;
  183. }
  184. if (!(n & CCM_ANALOG_PLL_USB1_LOCK)) {
  185. printf(" wait for lock\n");
  186. continue;
  187. }
  188. if (n & CCM_ANALOG_PLL_USB1_BYPASS) {
  189. printf(" turn off bypass\n");
  190. CCM_ANALOG_PLL_USB1_CLR = CCM_ANALOG_PLL_USB1_BYPASS;
  191. continue;
  192. }
  193. if (!(n & CCM_ANALOG_PLL_USB1_EN_USB_CLKS)) {
  194. printf(" enable USB clocks\n");
  195. CCM_ANALOG_PLL_USB1_SET = CCM_ANALOG_PLL_USB1_EN_USB_CLKS;
  196. continue;
  197. }
  198. return; // everything is as it should be :-)
  199. }
  200. }
  201. // Stack frame
  202. // xPSR
  203. // ReturnAddress
  204. // LR (R14) - typically FFFFFFF9 for IRQ or Exception
  205. // R12
  206. // R3
  207. // R2
  208. // R1
  209. // R0
  210. void unused_interrupt_vector(void)
  211. {
  212. // TODO: polling Serial to complete buffered transmits
  213. #ifdef PRINT_DEBUG_STUFF
  214. uint32_t addr;
  215. asm volatile("mrs %0, ipsr\n" : "=r" (addr)::);
  216. printf("\nirq %d\n", addr & 0x1FF);
  217. asm("ldr %0, [sp, #52]" : "=r" (addr) ::);
  218. printf(" %x\n", addr);
  219. asm("ldr %0, [sp, #48]" : "=r" (addr) ::);
  220. printf(" %x\n", addr);
  221. asm("ldr %0, [sp, #44]" : "=r" (addr) ::);
  222. printf(" %x\n", addr);
  223. asm("ldr %0, [sp, #40]" : "=r" (addr) ::);
  224. printf(" %x\n", addr);
  225. asm("ldr %0, [sp, #36]" : "=r" (addr) ::);
  226. printf(" %x\n", addr);
  227. asm("ldr %0, [sp, #33]" : "=r" (addr) ::);
  228. printf(" %x\n", addr);
  229. asm("ldr %0, [sp, #34]" : "=r" (addr) ::);
  230. printf(" %x\n", addr);
  231. asm("ldr %0, [sp, #28]" : "=r" (addr) ::);
  232. printf(" %x\n", addr);
  233. asm("ldr %0, [sp, #24]" : "=r" (addr) ::);
  234. printf(" %x\n", addr);
  235. asm("ldr %0, [sp, #20]" : "=r" (addr) ::);
  236. printf(" %x\n", addr);
  237. asm("ldr %0, [sp, #16]" : "=r" (addr) ::);
  238. printf(" %x\n", addr);
  239. asm("ldr %0, [sp, #12]" : "=r" (addr) ::);
  240. printf(" %x\n", addr);
  241. asm("ldr %0, [sp, #8]" : "=r" (addr) ::);
  242. printf(" %x\n", addr);
  243. asm("ldr %0, [sp, #4]" : "=r" (addr) ::);
  244. printf(" %x\n", addr);
  245. asm("ldr %0, [sp, #0]" : "=r" (addr) ::);
  246. printf(" %x\n", addr);
  247. #endif
  248. #if 1
  249. IOMUXC_SW_MUX_CTL_PAD_GPIO_B0_03 = 5; // pin 13
  250. IOMUXC_SW_PAD_CTL_PAD_GPIO_B0_03 = IOMUXC_PAD_DSE(7);
  251. GPIO2_GDIR |= (1<<3);
  252. GPIO2_DR_SET = (1<<3);
  253. while (1) {
  254. volatile uint32_t n;
  255. GPIO2_DR_SET = (1<<3); //digitalWrite(13, HIGH);
  256. for (n=0; n < 2000000; n++) ;
  257. GPIO2_DR_CLEAR = (1<<3); //digitalWrite(13, LOW);
  258. for (n=0; n < 1500000; n++) ;
  259. }
  260. #else
  261. while (1) {
  262. }
  263. #endif
  264. }
  265. __attribute__((section(".startup"), optimize("no-tree-loop-distribute-patterns")))
  266. static void memory_copy(uint32_t *dest, const uint32_t *src, uint32_t *dest_end)
  267. {
  268. if (dest == src) return;
  269. while (dest < dest_end) {
  270. *dest++ = *src++;
  271. }
  272. }
  273. __attribute__((section(".startup"), optimize("no-tree-loop-distribute-patterns")))
  274. static void memory_clear(uint32_t *dest, uint32_t *dest_end)
  275. {
  276. while (dest < dest_end) {
  277. *dest++ = 0;
  278. }
  279. }
  280. // syscall functions need to be in the same C file as the entry point "ResetVector"
  281. // otherwise the linker will discard them in some cases.
  282. #include <errno.h>
  283. // from the linker script
  284. extern unsigned long _heap_start;
  285. extern unsigned long _heap_end;
  286. char *__brkval = (char *)&_heap_start;
  287. void * _sbrk(int incr)
  288. {
  289. char *prev = __brkval;
  290. if (incr != 0) {
  291. if (prev + incr > (char *)&_heap_end) {
  292. errno = ENOMEM;
  293. return (void *)-1;
  294. }
  295. __brkval = prev + incr;
  296. }
  297. return prev;
  298. }
  299. __attribute__((weak))
  300. int _read(int file, char *ptr, int len)
  301. {
  302. return 0;
  303. }
  304. __attribute__((weak))
  305. int _close(int fd)
  306. {
  307. return -1;
  308. }
  309. #include <sys/stat.h>
  310. __attribute__((weak))
  311. int _fstat(int fd, struct stat *st)
  312. {
  313. st->st_mode = S_IFCHR;
  314. return 0;
  315. }
  316. __attribute__((weak))
  317. int _isatty(int fd)
  318. {
  319. return 1;
  320. }
  321. __attribute__((weak))
  322. int _lseek(int fd, long long offset, int whence)
  323. {
  324. return -1;
  325. }
  326. __attribute__((weak))
  327. void _exit(int status)
  328. {
  329. while (1);
  330. }
  331. __attribute__((weak))
  332. void __cxa_pure_virtual()
  333. {
  334. while (1);
  335. }
  336. __attribute__((weak))
  337. int __cxa_guard_acquire (char *g)
  338. {
  339. return !(*g);
  340. }
  341. __attribute__((weak))
  342. void __cxa_guard_release(char *g)
  343. {
  344. *g = 1;
  345. }
  346. __attribute__((weak))
  347. void abort(void)
  348. {
  349. while (1) ;
  350. }