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

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