variant_MARLIN_STM32F401RC.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*
  2. *******************************************************************************
  3. * Copyright (c) 2020-2021, STMicroelectronics
  4. * All rights reserved.
  5. *
  6. * This software component is licensed by ST under BSD 3-Clause license,
  7. * the "License"; You may not use this file except in compliance with the
  8. * License. You may obtain a copy of the License at:
  9. * opensource.org/licenses/BSD-3-Clause
  10. *
  11. *******************************************************************************
  12. */
  13. #if defined(STM32F401xC)
  14. #include "pins_arduino.h"
  15. // Digital PinName array
  16. const PinName digitalPin[] = {
  17. PA_0, // D0/A0
  18. PA_1, // D1/A1
  19. PA_2, // D2/A2
  20. PA_3, // D3/A3
  21. PA_4, // D4/A4
  22. PA_5, // D5/A5
  23. PA_6, // D6/A6
  24. PA_7, // D7/A7
  25. PA_8, // D8
  26. PA_9, // D9
  27. PA_10, // D10
  28. PA_11, // D11
  29. PA_12, // D12
  30. PA_13, // D13
  31. PA_14, // D14
  32. PA_15, // D15
  33. PB_0, // D16/A8
  34. PB_1, // D17/A9
  35. PB_2, // D18
  36. PB_3, // D19
  37. PB_4, // D20
  38. PB_5, // D21
  39. PB_6, // D22
  40. PB_7, // D23
  41. PB_8, // D24
  42. PB_9, // D25
  43. PB_10, // D26
  44. PB_12, // D27
  45. PB_13, // D28
  46. PB_14, // D29
  47. PB_15, // D30
  48. PC_0, // D31/A10
  49. PC_1, // D32/A11
  50. PC_2, // D33/A12
  51. PC_3, // D34/A13
  52. PC_4, // D35/A14
  53. PC_5, // D36/A15
  54. PC_6, // D37
  55. PC_7, // D38
  56. PC_8, // D39
  57. PC_9, // D40
  58. PC_10, // D41
  59. PC_11, // D42
  60. PC_12, // D43
  61. PC_13, // D44
  62. PC_14, // D45
  63. PC_15, // D46
  64. PD_2, // D47
  65. PH_0, // D48
  66. PH_1 // D49
  67. };
  68. // Analog (Ax) pin number array
  69. const uint32_t analogInputPin[] = {
  70. 0, // A0, PA0
  71. 1, // A1, PA1
  72. 2, // A2, PA2
  73. 3, // A3, PA3
  74. 4, // A4, PA4
  75. 5, // A5, PA5
  76. 6, // A6, PA6
  77. 7, // A7, PA7
  78. 16, // A8, PB0
  79. 17, // A9, PB1
  80. 31, // A10, PC0
  81. 32, // A11, PC1
  82. 33, // A12, PC2
  83. 34, // A13, PC3
  84. 35, // A14, PC4
  85. 36 // A15, PC5
  86. };
  87. // ----------------------------------------------------------------------------
  88. #ifdef __cplusplus
  89. extern "C" {
  90. #endif
  91. /*
  92. * @brief Configures the System clock source, PLL Multiplier and Divider factors,
  93. * AHB/APBx prescalers and Flash settings
  94. * @note This function should be called only once the RCC clock configuration
  95. * is reset to the default reset state (done in SystemInit() function).
  96. * @param None
  97. * @retval None
  98. */
  99. /******************************************************************************/
  100. /* PLL (clocked by HSE) used as System clock source */
  101. /******************************************************************************/
  102. static uint8_t SetSysClock_PLL_HSE(uint8_t bypass)
  103. {
  104. RCC_OscInitTypeDef RCC_OscInitStruct;
  105. RCC_ClkInitTypeDef RCC_ClkInitStruct;
  106. /* The voltage scaling allows optimizing the power consumption when the device is
  107. clocked below the maximum system frequency, to update the voltage scaling value
  108. regarding system frequency refer to product datasheet. */
  109. __HAL_RCC_PWR_CLK_ENABLE();
  110. __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE2);
  111. // Enable HSE oscillator and activate PLL with HSE as source
  112. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  113. if (bypass == 0) {
  114. RCC_OscInitStruct.HSEState = RCC_HSE_ON; // External 8 MHz xtal on OSC_IN/OSC_OUT
  115. } else {
  116. RCC_OscInitStruct.HSEState = RCC_HSE_BYPASS; // External 8 MHz clock on OSC_IN
  117. }
  118. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  119. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  120. RCC_OscInitStruct.PLL.PLLM = HSE_VALUE / 1000000L; // Expects an 8 MHz external clock by default. Redefine HSE_VALUE if not
  121. RCC_OscInitStruct.PLL.PLLN = 336; // VCO output clock = 336 MHz (1 MHz * 336)
  122. RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV4; // PLLCLK = 84 MHz (336 MHz / 4)
  123. RCC_OscInitStruct.PLL.PLLQ = 7; // USB clock = 48 MHz (336 MHz / 7) --> OK for USB
  124. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
  125. return 0; // FAIL
  126. }
  127. // Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2 clocks dividers
  128. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
  129. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; // 84 MHz
  130. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; // 84 MHz
  131. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2; // 42 MHz
  132. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; // 84 MHz
  133. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK) {
  134. return 0; // FAIL
  135. }
  136. /* Output clock on MCO1 pin(PA8) for debugging purpose */
  137. /*
  138. if (bypass == 0)
  139. HAL_RCC_MCOConfig(RCC_MCO1, RCC_MCO1SOURCE_HSE, RCC_MCODIV_2); // 4 MHz
  140. else
  141. HAL_RCC_MCOConfig(RCC_MCO1, RCC_MCO1SOURCE_HSE, RCC_MCODIV_1); // 8 MHz
  142. */
  143. return 1; // OK
  144. }
  145. /******************************************************************************/
  146. /* PLL (clocked by HSI) used as System clock source */
  147. /******************************************************************************/
  148. uint8_t SetSysClock_PLL_HSI(void)
  149. {
  150. RCC_OscInitTypeDef RCC_OscInitStruct;
  151. RCC_ClkInitTypeDef RCC_ClkInitStruct;
  152. /* The voltage scaling allows optimizing the power consumption when the device is
  153. clocked below the maximum system frequency, to update the voltage scaling value
  154. regarding system frequency refer to product datasheet. */
  155. __HAL_RCC_PWR_CLK_ENABLE();
  156. __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE2);
  157. // Enable HSI oscillator and activate PLL with HSI as source
  158. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI | RCC_OSCILLATORTYPE_HSE;
  159. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  160. RCC_OscInitStruct.HSEState = RCC_HSE_OFF;
  161. RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  162. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  163. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
  164. RCC_OscInitStruct.PLL.PLLM = 16; // VCO input clock = 1 MHz (16 MHz / 16)
  165. RCC_OscInitStruct.PLL.PLLN = 336; // VCO output clock = 336 MHz (1 MHz * 336)
  166. RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV4; // PLLCLK = 84 MHz (336 MHz / 4)
  167. RCC_OscInitStruct.PLL.PLLQ = 7; // USB clock = 48 MHz (336 MHz / 7) --> freq is ok but not precise enough
  168. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
  169. return 0; // FAIL
  170. }
  171. /* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2 clocks dividers */
  172. RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
  173. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; // 84 MHz
  174. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; // 84 MHz
  175. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2; // 42 MHz
  176. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; // 84 MHz
  177. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK) {
  178. return 0; // FAIL
  179. }
  180. /* Output clock on MCO1 pin(PA8) for debugging purpose */
  181. //HAL_RCC_MCOConfig(RCC_MCO1, RCC_MCO1SOURCE_HSI, RCC_MCODIV_1); // 16 MHz
  182. return 1; // OK
  183. }
  184. WEAK void SystemClock_Config(void)
  185. {
  186. /* 1- If fail try to start with HSE and external xtal */
  187. if (SetSysClock_PLL_HSE(0) == 0) {
  188. /* 2- Try to start with HSE and external clock */
  189. if (SetSysClock_PLL_HSE(1) == 0) {
  190. /* 3- If fail start with HSI clock */
  191. if (SetSysClock_PLL_HSI() == 0) {
  192. Error_Handler();
  193. }
  194. }
  195. }
  196. /* Output clock on MCO2 pin(PC9) for debugging purpose */
  197. //HAL_RCC_MCOConfig(RCC_MCO2, RCC_MCO2SOURCE_SYSCLK, RCC_MCODIV_4);
  198. }
  199. #ifdef __cplusplus
  200. }
  201. #endif
  202. #endif /* STM32F401xC */