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.

424 lines
12KB

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