variant.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. /*
  2. *******************************************************************************
  3. * Copyright (c) 2017, STMicroelectronics
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice,
  10. * this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  15. * may be used to endorse or promote products derived from this software
  16. * without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  19. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  20. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  22. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  23. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  24. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  25. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  26. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  27. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *******************************************************************************
  29. */
  30. #include "pins_arduino.h"
  31. #ifdef __cplusplus
  32. extern "C" {
  33. #endif
  34. // Digital PinName array
  35. const PinName digitalPin[] = {
  36. PA_0, // Digital pin 0
  37. PA_1, // Digital pin 1
  38. PA_2, // Digital pin 2
  39. PA_3, // Digital pin 3
  40. PA_4, // Digital pin 4
  41. PA_5, // Digital pin 5
  42. PA_6, // Digital pin 6
  43. PA_7, // Digital pin 7
  44. PA_8, // Digital pin 8
  45. PA_9, // Digital pin 9
  46. PA_10, // Digital pin 10
  47. PA_11, // Digital pin 11
  48. PA_12, // Digital pin 12
  49. PA_13, // Digital pin 13
  50. PA_14, // Digital pin 14
  51. PA_15, // Digital pin 15
  52. PB_0, // Digital pin 16
  53. PB_1, // Digital pin 17
  54. PB_2, // Digital pin 18
  55. PB_3, // Digital pin 19
  56. PB_4, // Digital pin 20
  57. PB_5, // Digital pin 21
  58. PB_6, // Digital pin 22
  59. PB_7, // Digital pin 23
  60. PB_8, // Digital pin 24
  61. PB_9, // Digital pin 25
  62. PB_10, // Digital pin 26
  63. PB_11, // Digital pin 27
  64. PB_12, // Digital pin 28
  65. PB_13, // Digital pin 29
  66. PB_14, // Digital pin 30
  67. PB_15, // Digital pin 31
  68. PC_0, // Digital pin 32
  69. PC_1, // Digital pin 33
  70. PC_2, // Digital pin 34
  71. PC_3, // Digital pin 35
  72. PC_4, // Digital pin 36
  73. PC_5, // Digital pin 37
  74. PC_6, // Digital pin 38
  75. PC_7, // Digital pin 39
  76. PC_8, // Digital pin 40
  77. PC_9, // Digital pin 41
  78. PC_10, // Digital pin 42
  79. PC_11, // Digital pin 43
  80. PC_12, // Digital pin 44
  81. PC_13, // Digital pin 45
  82. PC_14, // Digital pin 46
  83. PC_15, // Digital pin 47
  84. PD_0, // Digital pin 48
  85. PD_1, // Digital pin 49
  86. PD_2, // Digital pin 50
  87. PD_3, // Digital pin 51
  88. PD_4, // Digital pin 52
  89. PD_5, // Digital pin 53
  90. PD_6, // Digital pin 54
  91. PD_7, // Digital pin 55
  92. PD_8, // Digital pin 56
  93. PD_9, // Digital pin 57
  94. PD_10, // Digital pin 58
  95. PD_11, // Digital pin 59
  96. PD_12, // Digital pin 60
  97. PD_13, // Digital pin 61
  98. PD_14, // Digital pin 62
  99. PD_15, // Digital pin 63
  100. PE_0, // Digital pin 64
  101. PE_1, // Digital pin 65
  102. PE_2, // Digital pin 66
  103. PE_3, // Digital pin 67
  104. PE_4, // Digital pin 68
  105. PE_5, // Digital pin 69
  106. PE_6, // Digital pin 70
  107. PE_7, // Digital pin 71
  108. PE_8, // Digital pin 72
  109. PE_9, // Digital pin 73
  110. PE_10, // Digital pin 74
  111. PE_11, // Digital pin 75
  112. PE_12, // Digital pin 76
  113. PE_13, // Digital pin 77
  114. PE_14, // Digital pin 78
  115. PE_15, // Digital pin 79
  116. PH_0, // Digital pin 80, used by the external oscillator
  117. PH_1 // Digital pin 81, used by the external oscillator
  118. };
  119. // Analog (Ax) pin number array
  120. const uint32_t analogInputPin[] = {
  121. 0, // A0, PA0
  122. 1, // A1, PA1
  123. 2, // A2, PA2
  124. 3, // A3, PA3
  125. 4, // A4, PA4
  126. 5, // A5, PA5
  127. 6, // A6, PA6
  128. 7, // A7, PA7
  129. 16, // A8, PB0
  130. 17, // A9, PB1
  131. 32, // A10, PC0
  132. 33, // A11, PC1
  133. 34, // A12, PC2
  134. 35, // A13, PC3
  135. 36, // A14, PC4
  136. 37 // A15, PC5
  137. };
  138. #ifdef __cplusplus
  139. }
  140. #endif
  141. // ----------------------------------------------------------------------------
  142. #ifdef __cplusplus
  143. extern "C" {
  144. #endif
  145. /*
  146. * @brief Configures the System clock source, PLL Multiplier and Divider factors,
  147. * AHB/APBx prescalers and Flash settings
  148. * @note This function should be called only once the RCC clock configuration
  149. * is reset to the default reset state (done in SystemInit() function).
  150. * @param None
  151. * @retval None
  152. */
  153. /******************************************************************************/
  154. /* PLL (clocked by HSE) used as System clock source */
  155. /******************************************************************************/
  156. static uint8_t SetSysClock_PLL_HSE(uint8_t bypass)
  157. {
  158. RCC_OscInitTypeDef RCC_OscInitStruct;
  159. RCC_ClkInitTypeDef RCC_ClkInitStruct;
  160. /* The voltage scaling allows optimizing the power consumption when the device is
  161. clocked below the maximum system frequency, to update the voltage scaling value
  162. regarding system frequency refer to product datasheet. */
  163. __HAL_RCC_PWR_CLK_ENABLE();
  164. __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
  165. // Enable HSE oscillator and activate PLL with HSE as source
  166. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  167. if (bypass == 0) {
  168. RCC_OscInitStruct.HSEState = RCC_HSE_ON; // External 8 MHz xtal on OSC_IN/OSC_OUT
  169. } else {
  170. RCC_OscInitStruct.HSEState = RCC_HSE_BYPASS; // External 8 MHz clock on OSC_IN
  171. }
  172. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  173. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  174. RCC_OscInitStruct.PLL.PLLM = HSE_VALUE / 1000000L; // Expects an 8 MHz external clock by default. Redefine HSE_VALUE if not
  175. RCC_OscInitStruct.PLL.PLLN = 336; // VCO output clock = 336 MHz (1 MHz * 336)
  176. RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2; // PLLCLK = 168 MHz (336 MHz / 2)
  177. RCC_OscInitStruct.PLL.PLLQ = 7;
  178. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
  179. return 0; // FAIL
  180. }
  181. // Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2 clocks dividers
  182. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
  183. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  184. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; // 168 MHz
  185. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4; // 42 MHz
  186. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2; // 84 MHz
  187. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK) {
  188. return 0; // FAIL
  189. }
  190. /* Output clock on MCO1 pin(PA8) for debugging purpose */
  191. /*
  192. if (bypass == 0)
  193. HAL_RCC_MCOConfig(RCC_MCO1, RCC_MCO1SOURCE_HSE, RCC_MCODIV_2); // 4 MHz
  194. else
  195. HAL_RCC_MCOConfig(RCC_MCO1, RCC_MCO1SOURCE_HSE, RCC_MCODIV_1); // 8 MHz
  196. */
  197. return 1; // OK
  198. }
  199. /******************************************************************************/
  200. /* PLL (clocked by HSI) used as System clock source */
  201. /******************************************************************************/
  202. uint8_t SetSysClock_PLL_HSI(void)
  203. {
  204. RCC_OscInitTypeDef RCC_OscInitStruct;
  205. RCC_ClkInitTypeDef RCC_ClkInitStruct;
  206. /* The voltage scaling allows optimizing the power consumption when the device is
  207. clocked below the maximum system frequency, to update the voltage scaling value
  208. regarding system frequency refer to product datasheet. */
  209. __HAL_RCC_PWR_CLK_ENABLE();
  210. __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
  211. // Enable HSI oscillator and activate PLL with HSI as source
  212. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI | RCC_OSCILLATORTYPE_HSE;
  213. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  214. RCC_OscInitStruct.HSEState = RCC_HSE_OFF;
  215. RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  216. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  217. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
  218. RCC_OscInitStruct.PLL.PLLM = 16; // VCO input clock = 1 MHz (16 MHz / 16)
  219. RCC_OscInitStruct.PLL.PLLN = 336; // VCO output clock = 336 MHz (1 MHz * 336)
  220. RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2; // PLLCLK = 168 MHz (336 MHz / 2)
  221. RCC_OscInitStruct.PLL.PLLQ = 7;
  222. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
  223. return 0; // FAIL
  224. }
  225. /* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2 clocks dividers */
  226. RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
  227. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  228. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; // 168 MHz
  229. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4; // 42 MHz
  230. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2; // 84 MHz
  231. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK) {
  232. return 0; // FAIL
  233. }
  234. /* Output clock on MCO1 pin(PA8) for debugging purpose */
  235. //HAL_RCC_MCOConfig(RCC_MCO1, RCC_MCO1SOURCE_HSI, RCC_MCODIV_1); // 16 MHz
  236. return 1; // OK
  237. }
  238. WEAK void SystemClock_Config(void)
  239. {
  240. /* 1- If fail try to start with HSE and external xtal */
  241. if (SetSysClock_PLL_HSE(0) == 0) {
  242. /* 2- Try to start with HSE and external clock */
  243. if (SetSysClock_PLL_HSE(1) == 0) {
  244. /* 3- If fail start with HSI clock */
  245. if (SetSysClock_PLL_HSI() == 0) {
  246. Error_Handler();
  247. }
  248. }
  249. }
  250. /* Ensure CCM RAM clock is enabled */
  251. __HAL_RCC_CCMDATARAMEN_CLK_ENABLE();
  252. /* Output clock on MCO2 pin(PC9) for debugging purpose */
  253. //HAL_RCC_MCOConfig(RCC_MCO2, RCC_MCO2SOURCE_SYSCLK, RCC_MCODIV_4);
  254. }
  255. #ifdef __cplusplus
  256. }
  257. #endif