variant.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /*
  2. *******************************************************************************
  3. * Copyright (c) 2019, 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. #include "pins_arduino.h"
  14. #ifdef __cplusplus
  15. extern "C" {
  16. #endif
  17. // Digital PinName array
  18. const PinName digitalPin[] = {
  19. PA_0, //D0
  20. PA_1, //D1
  21. PA_2, //D2
  22. PA_3, //D3
  23. PA_4, //D4
  24. PA_5, //D5
  25. PA_6, //D6
  26. PA_7, //D7
  27. PA_8, //D8
  28. PA_9, //D9
  29. PA_10, //D10
  30. PA_11, //D11
  31. PA_12, //D12
  32. PA_13, //D13
  33. PA_14, //D14
  34. PA_15, //D15
  35. PB_0, //D16
  36. PB_1, //D17
  37. PB_2, //D18
  38. PB_3, //D19
  39. PB_4, //D20
  40. PB_5, //D21
  41. PB_6, //D22
  42. PB_7, //D23
  43. PB_8, //D24
  44. PB_9, //D25
  45. PB_10, //D26
  46. PB_11, //D27
  47. PB_12, //D28
  48. PB_13, //D29
  49. PB_14, //D30
  50. PB_15, //D31
  51. PC_0, //D32
  52. PC_1, //D33
  53. PC_2, //D34
  54. PC_3, //D35
  55. PC_4, //D36
  56. PC_5, //D37
  57. PC_6, //D38
  58. PC_7, //D39
  59. PC_8, //D40
  60. PC_9, //D41
  61. PC_10, //D42
  62. PC_11, //D43
  63. PC_12, //D44
  64. PC_13, //D45
  65. PC_14, //D46
  66. PC_15, //D47
  67. PD_0, //D48
  68. PD_1, //D49
  69. PD_2, //D50
  70. PD_3, //D51
  71. PD_4, //D52
  72. PD_5, //D53
  73. PD_6, //D54
  74. PD_7, //D55
  75. PD_8, //D56
  76. PD_9, //D57
  77. PD_10, //D58
  78. PD_11, //D59
  79. PD_12, //D60
  80. PD_13, //D61
  81. PD_14, //D62
  82. PD_15, //D63
  83. PE_0, //D64
  84. PE_1, //D65
  85. PE_2, //D66
  86. PE_3, //D67
  87. PE_4, //D68
  88. PE_5, //D69
  89. PE_6, //D70
  90. PE_7, //D71
  91. PE_8, //D72
  92. PE_9, //D73
  93. PE_10, //D74
  94. PE_11, //D75
  95. PE_12, //D76
  96. PE_13, //D77
  97. PE_14, //D78
  98. PE_15, //D79
  99. };
  100. // Analog (Ax) pin number array
  101. const uint32_t analogInputPin[] = {
  102. 0, // A0, PA0
  103. 1, // A1, PA1
  104. 2, // A2, PA2
  105. 3, // A3, PA3
  106. 4, // A4, PA4
  107. 5, // A5, PA5
  108. 6, // A6, PA6
  109. 7, // A7, PA7
  110. 16, // A8, PB0
  111. 17, // A9, PB1
  112. 32, // A10, PC0
  113. 33, // A11, PC1
  114. 34, // A12, PC2
  115. 35, // A13, PC3
  116. 36, // A14, PC4
  117. 37, // A15, PC5
  118. };
  119. /******************************************************************************/
  120. /* PLL (clocked by HSE) used as System clock source */
  121. /******************************************************************************/
  122. static bool SetSysClock_PLL_HSE(bool bypass)
  123. {
  124. RCC_OscInitTypeDef RCC_OscInitStruct = {};
  125. RCC_ClkInitTypeDef RCC_ClkInitStruct = {};
  126. RCC_PeriphCLKInitTypeDef PeriphClkInit = {};
  127. bool ret = false;
  128. // Initializes the CPU, AHB and APB busses clocks
  129. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  130. if (bypass == false) {
  131. RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  132. } else {
  133. RCC_OscInitStruct.HSEState = RCC_HSE_BYPASS;
  134. }
  135. RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
  136. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  137. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  138. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  139. RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9; // 8Mhz x 9 = 72MHz
  140. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) == HAL_OK) {
  141. // Initializes the CPU, AHB and APB busses clocks
  142. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK
  143. | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
  144. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  145. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  146. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  147. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  148. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) == HAL_OK) {
  149. PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_ADC | RCC_PERIPHCLK_USB;
  150. PeriphClkInit.AdcClockSelection = RCC_ADCPCLK2_DIV6;
  151. PeriphClkInit.UsbClockSelection = RCC_USBCLKSOURCE_PLL_DIV1_5; // 72/1.5 = 48MHz
  152. #ifndef USBCON
  153. PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_ADC;
  154. #endif
  155. if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) == HAL_OK) {
  156. ret = true;
  157. }
  158. }
  159. }
  160. return ret;
  161. }
  162. /******************************************************************************/
  163. /* PLL (clocked by HSI) used as System clock source (64MHz max) */
  164. /******************************************************************************/
  165. bool SetSysClock_PLL_HSI(void)
  166. {
  167. RCC_OscInitTypeDef RCC_OscInitStruct = {};
  168. RCC_ClkInitTypeDef RCC_ClkInitStruct = {};
  169. RCC_PeriphCLKInitTypeDef PeriphClkInit = {};
  170. bool ret = false;
  171. // Initializes the CPU, AHB and APB busses clocks
  172. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI | RCC_OSCILLATORTYPE_HSE;
  173. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  174. RCC_OscInitStruct.HSEState = RCC_HSE_OFF;
  175. RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  176. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  177. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI_DIV2; // 4 MHz
  178. RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL12; // 48 MHz
  179. #ifndef USBCON
  180. // When the HSI is used as a PLL clock input, the maximum
  181. // system clock frequency that can be achieved is 64 MHz.
  182. RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL16; // 64 MHz, stay close to 72 for delay()
  183. #endif
  184. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) == HAL_OK) {
  185. // Initializes the CPU, AHB and APB busses clocks
  186. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK
  187. | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
  188. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  189. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  190. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  191. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  192. // FLASH_LATENCY_1 may cause boot loops
  193. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) == HAL_OK) {
  194. PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_ADC | RCC_PERIPHCLK_USB;
  195. PeriphClkInit.AdcClockSelection = RCC_ADCPCLK2_DIV4;
  196. PeriphClkInit.UsbClockSelection = RCC_USBCLKSOURCE_PLL; // requires 48 MHz
  197. #ifndef USBCON
  198. PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_ADC;// No USB, RTC nor I2S
  199. PeriphClkInit.AdcClockSelection = RCC_ADCPCLK2_DIV6; // 2 4 6 8
  200. #endif
  201. if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) == HAL_OK) {
  202. ret = true;
  203. }
  204. }
  205. }
  206. return ret;
  207. }
  208. void SystemClock_Config(void)
  209. {
  210. /*
  211. * If HSE_VALUE is not 8MHz and you want use it, then:
  212. * - Redefine HSE_VALUE to the correct HSE_VALUE
  213. * - Redefine SystemClock_Config() with the correct settings
  214. */
  215. #if HSE_VALUE == 8000000U
  216. // 1- Try to start with HSE and external 8MHz xtal
  217. if (SetSysClock_PLL_HSE(false) == false) {
  218. // 2- If fail try to start with HSE and external clock
  219. if (SetSysClock_PLL_HSE(true) == false) {
  220. #endif
  221. // 3- If fail start with HSI clock
  222. if (SetSysClock_PLL_HSI() == false) {
  223. Error_Handler();
  224. }
  225. #if HSE_VALUE == 8000000U
  226. }
  227. }
  228. #endif
  229. }
  230. #ifdef __cplusplus
  231. }
  232. #endif