您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

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