My Marlin configs for Fabrikator Mini and CTC i3 Pro B
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.

variant.cpp 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. #include "pins_arduino.h"
  2. #ifdef __cplusplus
  3. extern "C" {
  4. #endif
  5. // Pin number
  6. const PinName digitalPin[] = {
  7. PE_2, // D0
  8. PE_3, // D1
  9. PE_4, // D2
  10. PE_5, // D3
  11. PE_6, // D4
  12. PI_8, // D5
  13. PC_13, // D6
  14. PC_14, // D7
  15. PC_15, // D8
  16. PI_9, // D9
  17. PI_10, // D10
  18. PI_11, // D11
  19. PF_0, // D12
  20. PF_1, // D13
  21. PF_2, // D14
  22. PH_0, // D15
  23. PH_1, // D16
  24. PB_2, // D17
  25. PF_15, // D18
  26. PG_0, // D19
  27. PG_1, // D20
  28. PE_7, // D21
  29. PE_8, // D22
  30. PE_9, // D23
  31. PE_10, // D24
  32. PE_11, // D25
  33. PE_12, // D26
  34. PE_13, // D27
  35. PE_14, // D28
  36. PE_15, // D29
  37. PB_10, // D30
  38. PB_11, // D31
  39. PH_6, // D32
  40. PH_7, // D33
  41. PH_8, // D34
  42. PH_9, // D35
  43. PH_10, // D36
  44. PH_11, // D37
  45. PH_12, // D38
  46. PB_12, // D39
  47. PB_13, // D40
  48. PB_14, // D41
  49. PB_15, // D42
  50. PD_8, // D43
  51. PD_9, // D44
  52. PD_10, // D45
  53. PD_11, // D46
  54. PD_12, // D47
  55. PD_13, // D48
  56. PD_14, // D49
  57. PD_15, // D50
  58. PG_2, // D51
  59. PG_3, // D52
  60. PG_4, // D53
  61. PG_5, // D54
  62. PG_6, // D55
  63. PG_7, // D56
  64. PG_8, // D57
  65. PC_6, // D58
  66. PC_7, // D59
  67. PC_8, // D60
  68. PC_9, // D61
  69. PA_8, // D62
  70. PA_9, // D63
  71. PA_10, // D64
  72. PA_11, // D65
  73. PA_12, // D66
  74. PA_13, // D67
  75. PH_13, // D68
  76. PH_14, // D69
  77. PH_15, // D70
  78. PI_0, // D71
  79. PI_1, // D72
  80. PI_2, // D73
  81. PI_3, // D74
  82. PA_14, // D75
  83. PA_15, // D76
  84. PC_10, // D77
  85. PC_11, // D78
  86. PC_12, // D79
  87. PD_0, // D80
  88. PD_1, // D81
  89. PD_2, // D82
  90. PD_3, // D83
  91. PD_4, // D84
  92. PD_5, // D85
  93. PD_6, // D86
  94. PD_7, // D87
  95. PG_9, // D88
  96. PG_10, // D89
  97. PG_11, // D90
  98. PG_12, // D91
  99. PG_13, // D92
  100. PG_14, // D93
  101. PG_15, // D94
  102. PB_3, // D95
  103. PB_4, // D96
  104. PB_5, // D97
  105. PB_6, // D98
  106. PB_7, // D99
  107. PB_8, // D100
  108. PB_9, // D101
  109. PE_0, // D102
  110. PE_1, // D103
  111. PI_4, // D104
  112. PI_5, // D105
  113. PI_6, // D106
  114. PI_7, // D107
  115. PA_0, // D108 / A0
  116. PA_1, // D109 / A1
  117. PA_2, // D110 / A2
  118. PA_3, // D111 / A3
  119. PA_4, // D112 / A4
  120. PA_5, // D113 / A5
  121. PA_6, // D114 / A6
  122. PA_7, // D115 / A7
  123. PB_0, // D116 / A8
  124. PB_1, // D117 / A9
  125. PH_2, // D118 / A10
  126. PH_3, // D119 / A11
  127. PH_4, // D120 / A12
  128. PH_5, // D121 / A13
  129. PC_0, // D122 / A14
  130. PC_1, // D123 / A15
  131. PC_2, // D124 / A16
  132. PC_3, // D125 / A17
  133. PC_4, // D126 / A18
  134. PC_5, // D127 / A19
  135. PF_3, // D128 / A20
  136. PF_4, // D129 / A21
  137. PF_5, // D130 / A22
  138. PF_6, // D131 / A23
  139. PF_7, // D132 / A24
  140. PF_8, // D133 / A25
  141. PF_9, // D134 / A26
  142. PF_10, // D135 / A27
  143. PF_11, // D136 / A28
  144. PF_12, // D137 / A29
  145. PF_13, // D138 / A30
  146. PF_14, // D139 / A31
  147. };
  148. #ifdef __cplusplus
  149. }
  150. #endif
  151. // ----------------------------------------------------------------------------
  152. #ifdef __cplusplus
  153. extern "C" {
  154. #endif
  155. void SystemClockStartupInit() {
  156. // Confirm is called only once time to avoid hang up caused by repeated calls in USB wakeup interrupt
  157. static bool first_call = true;
  158. if (!first_call) return;
  159. first_call = false;
  160. // Clear all clock setting register
  161. RCC->CR = 0x00000001;
  162. RCC->CFGR = 0x00000000;
  163. RCC->D1CFGR = 0x00000000;
  164. RCC->D2CFGR = 0x00000000;
  165. RCC->D3CFGR = 0x00000000;
  166. RCC->PLLCKSELR = 0x00000000;
  167. RCC->PLLCFGR = 0x00000000;
  168. RCC->CIER = 0x00000000;
  169. // AXI_TARG7_FN_MOD for SRAM
  170. *((volatile uint32_t*)0x51008108)=0x00000001;
  171. // Enable L1-Cache
  172. SCB_EnableICache();
  173. SCB_EnableDCache();
  174. SCB->CACR |= 1<<2;
  175. PWR->CR3 &= ~(1 << 2); // SCUEN=0
  176. PWR->D3CR |= 3 << 14; // VOS=3,Scale1,1.15~1.26V core voltage
  177. while((PWR->D3CR & (1 << 13)) == 0); // Wait for the voltage to stabilize
  178. RCC->CR |= 1<<16; // Enable HSE
  179. uint16_t timeout = 0;
  180. while(((RCC->CR & (1 << 17)) == 0) && (timeout < 0X7FFF)) {
  181. timeout++; // Wait for HSE RDY
  182. }
  183. if(timeout == 0X7FFF) {
  184. Error_Handler();
  185. } else {
  186. RCC->PLLCKSELR |= 2 << 0; // PLLSRC[1:0] = 2, HSE for PLL clock source
  187. RCC->PLLCKSELR |= 5 << 4; // DIVM1[5:0] = pllm, Prescaler for PLL1
  188. RCC->PLL1DIVR |= (160 - 1) << 0; // DIVN1[8:0] = plln - 1, Multiplication factor for PLL1 VCO
  189. RCC->PLL1DIVR |= (2 - 1) << 9; // DIVP1[6:0] = pllp - 1, PLL1 DIVP division factor
  190. RCC->PLL1DIVR |= (4 - 1) << 16; // DIVQ1[6:0] = pllq - 1, PLL1 DIVQ division factor
  191. RCC->PLL1DIVR |= 1 << 24; // DIVR1[6:0] = pllr - 1, PLL1 DIVR division factor
  192. RCC->PLLCFGR |= 2 << 2; // PLL1 input (ref1_ck) clock range frequency is between 4 and 8 MHz
  193. RCC->PLLCFGR |= 0 << 1; // PLL1 VCO selection, 0: 192 to 836 MHz, 1 : 150 to 420 MHz
  194. RCC->PLLCFGR |= 3 << 16; // pll1_q_ck and pll1_p_ck output is enabled
  195. RCC->CR |= 1 << 24; // PLL1 enable
  196. while((RCC->CR & (1 << 25)) == 0); // PLL1 clock ready flag
  197. // PLL2 DIVR clock frequency = 220MHz, so that SDRAM clock can be set to 110MHz
  198. RCC->PLLCKSELR |= 25 << 12; // DIVM2[5:0] = 25, Prescaler for PLL2
  199. RCC->PLL2DIVR |= (440 - 1) << 0; // DIVN2[8:0] = 440 - 1, Multiplication factor for PLL2 VCO
  200. RCC->PLL2DIVR |= (2 - 1) << 9; // DIVP2[6:0] = 2-1, PLL2 DIVP division factor
  201. RCC->PLL2DIVR |= (2 - 1) << 24; // DIVR2[6:0] = 2-1, PLL2 DIVR division factor
  202. RCC->PLLCFGR |= 0 << 6; // PLL2RGE[1:0]=0, PLL2 input (ref2_ck) clock range frequency is between 1 and 2 MHz
  203. RCC->PLLCFGR |= 0 << 5; // PLL2 VCO selection, 0: 192 to 836 MHz, 1: 150 to 420 MHz
  204. RCC->PLLCFGR |= 1 << 19; // pll2_p_ck output is enabled
  205. RCC->PLLCFGR |= 1 << 21; // pll2_r_ck output is enabled
  206. RCC->D1CCIPR &= ~(3 << 0); // clear FMC kernel clock source selection
  207. RCC->D1CCIPR |= 2 << 0; // pll2_r_ck clock selected as kernel peripheral clock
  208. RCC->CR |= 1 << 26; // PLL2 enable
  209. while((RCC->CR&(1<<27)) == 0); // PLL2 clock ready flag
  210. RCC->D1CFGR |= 8 << 0; // rcc_hclk3 = sys_d1cpre_ck / 2 = 400 / 2 = 200MHz. AHB1/2/3/4
  211. RCC->D1CFGR |= 0 << 8; // sys_ck not divided, sys_d1cpre_ck = sys_clk / 1 = 400 / 1 = 400MHz, System Clock = 400MHz
  212. RCC->CFGR |= 3 << 0; // PLL1 selected as system clock (pll1_p_ck). 400MHz
  213. while(1) {
  214. timeout = (RCC->CFGR & (7 << 3)) >> 3; // System clock switch status
  215. if(timeout == 3) break; // Wait for SW[2:0] = 3 (011: PLL1 selected as system clock (pll1_p_ck))
  216. }
  217. FLASH->ACR |= 2 << 0; // LATENCY[2:0] = 2 (@VOS1 Level,maxclock=210MHz)
  218. FLASH->ACR |= 2 << 4; // WRHIGHFREQ[1:0] = 2, flash access frequency < 285MHz
  219. RCC->D1CFGR |= 4 << 4; // D1PPRE[2:0] = 4, rcc_pclk3 = rcc_hclk3 / 2 = 100MHz, APB3.
  220. RCC->D2CFGR |= 4 << 4; // D2PPRE1[2:0] = 4, rcc_pclk1 = rcc_hclk1 / 2 = 100MHz, APB1.
  221. RCC->D2CFGR |= 4 << 8; // D2PPRE2[2:0] = 4, rcc_pclk2 = rcc_hclk1 / 2 = 100MHz, APB2.
  222. RCC->D3CFGR |= 4 << 4; // D3PPRE[2:0] = 4, rcc_pclk4 = rcc_hclk4 / 2 = 100MHz, APB4.
  223. RCC->CR |= 1 << 7; // CSI clock enable
  224. RCC->APB4ENR |= 1 << 1; // SYSCFG peripheral clock enable
  225. SYSCFG->CCCSR |= 1 << 0;
  226. }
  227. // USB clock, (use HSI48 clock)
  228. RCC->CR |= 1 << 12; // HSI48 clock enabl
  229. while((RCC->CR & (1 << 13)) == 0);// 1: HSI48 clock is ready
  230. RCC->APB1HENR |= 1 << 1; // CRS peripheral clock enabled
  231. RCC->APB1HRSTR |= 1 << 1; // Resets CRS
  232. RCC->APB1HRSTR &= ~(1 << 1); // Does not reset CRS
  233. CRS->CFGR &= ~(3 << 28); // USB2 SOF selected as SYNC signal source
  234. CRS->CR |= 3 << 5; // Automatic trimming and Frequency error counter enabled
  235. RCC->D2CCIP2R &= ~(3 << 20); // Clear USBOTG 1 and 2 kernel clock source selection
  236. RCC->D2CCIP2R |= 3 << 20; // HSI48_ck clock is selected as kernel clock
  237. }
  238. uint8_t MPU_Convert_Bytes_To_POT(uint32_t nbytes)
  239. {
  240. uint8_t count = 0;
  241. while(nbytes != 1)
  242. {
  243. nbytes >>= 1;
  244. count++;
  245. }
  246. return count;
  247. }
  248. uint8_t MPU_Set_Protection(uint32_t baseaddr, uint32_t size, uint32_t rnum, uint8_t ap, uint8_t sen, uint8_t cen, uint8_t ben)
  249. {
  250. uint32_t tempreg = 0;
  251. uint8_t rnr = 0;
  252. if ((size % 32) || size == 0) return 1;
  253. rnr = MPU_Convert_Bytes_To_POT(size) - 1;
  254. SCB->SHCSR &= ~(1 << 16); //disable MemManage
  255. MPU->CTRL &= ~(1 << 0); //disable MPU
  256. MPU->RNR = rnum;
  257. MPU->RBAR = baseaddr;
  258. tempreg |= 0 << 28;
  259. tempreg |= ((uint32_t)ap) << 24;
  260. tempreg |= 0 << 19;
  261. tempreg |= ((uint32_t)sen) << 18;
  262. tempreg |= ((uint32_t)cen) << 17;
  263. tempreg |= ((uint32_t)ben) << 16;
  264. tempreg |= 0 << 8;
  265. tempreg |= rnr << 1;
  266. tempreg |= 1 << 0;
  267. MPU->RASR = tempreg;
  268. MPU->CTRL = (1 << 2) | (1 << 0); //enable PRIVDEFENA
  269. SCB->SHCSR |= 1 << 16; //enable MemManage
  270. return 0;
  271. }
  272. void MPU_Memory_Protection(void)
  273. {
  274. MPU_Set_Protection(0x20000000, 128 * 1024, 1, MPU_REGION_FULL_ACCESS, 0, 1, 1); // protect DTCM 128k, Sharing is prohibited, cache is allowed, and buffering is allowed
  275. MPU_Set_Protection(0x24000000, 512 * 1024, 2, MPU_REGION_FULL_ACCESS, 0, 1, 1); // protect AXI SRAM, Sharing is prohibited, cache is allowed, and buffering is allowed
  276. MPU_Set_Protection(0x30000000, 512 * 1024, 3, MPU_REGION_FULL_ACCESS, 0, 1, 1); // protect SRAM1~SRAM3, Sharing is prohibited, cache is allowed, and buffering is allowed
  277. MPU_Set_Protection(0x38000000, 64 * 1024, 4, MPU_REGION_FULL_ACCESS, 0, 1, 1); // protect SRAM4, Sharing is prohibited, cache is allowed, and buffering is allowed
  278. MPU_Set_Protection(0x60000000, 64 * 1024 * 1024, 5, MPU_REGION_FULL_ACCESS, 0, 0, 0); // protect LCD FMC 64M, No sharing, no cache, no buffering
  279. MPU_Set_Protection(0XC0000000, 32 * 1024 * 1024, 6, MPU_REGION_FULL_ACCESS, 0, 1, 1); // protect SDRAM 32M, Sharing is prohibited, cache is allowed, and buffering is allowed
  280. MPU_Set_Protection(0X80000000, 256 * 1024 * 1024, 7, MPU_REGION_FULL_ACCESS, 0, 0, 0); // protect NAND FLASH 256M, No sharing, no cache, no buffering
  281. }
  282. /**
  283. * @brief System Clock Configuration
  284. * @param None
  285. * @retval None
  286. */
  287. WEAK void SystemClock_Config(void)
  288. {
  289. SystemClockStartupInit();
  290. MPU_Memory_Protection();
  291. /* Update current SystemCoreClock value */
  292. SystemCoreClockUpdate();
  293. /* Configure the Systick interrupt time */
  294. HAL_SYSTICK_Config(SystemCoreClock/1000);
  295. /* Configure the Systick */
  296. HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
  297. /* SysTick_IRQn interrupt configuration */
  298. HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
  299. }
  300. #ifdef __cplusplus
  301. }
  302. #endif