main.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. /* USER CODE BEGIN Header */
  2. /**
  3. ******************************************************************************
  4. * @file : main.c
  5. * @brief : Main program body
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * <h2><center>&copy; Copyright (c) 2020 STMicroelectronics.
  10. * All rights reserved.</center></h2>
  11. *
  12. * This software component is licensed by ST under BSD 3-Clause license,
  13. * the "License"; You may not use this file except in compliance with the
  14. * License. You may obtain a copy of the License at:
  15. * opensource.org/licenses/BSD-3-Clause
  16. *
  17. ******************************************************************************
  18. */
  19. /* USER CODE END Header */
  20. /* Includes ------------------------------------------------------------------*/
  21. #include "main.h"
  22. /* Private includes ----------------------------------------------------------*/
  23. /* USER CODE BEGIN Includes */
  24. /* USER CODE END Includes */
  25. /* Private typedef -----------------------------------------------------------*/
  26. /* USER CODE BEGIN PTD */
  27. /* USER CODE END PTD */
  28. /* Private define ------------------------------------------------------------*/
  29. /* USER CODE BEGIN PD */
  30. /* USER CODE END PD */
  31. /* Private macro -------------------------------------------------------------*/
  32. /* USER CODE BEGIN PM */
  33. /* USER CODE END PM */
  34. /* Private variables ---------------------------------------------------------*/
  35. UART_HandleTypeDef hlpuart1;
  36. PCD_HandleTypeDef hpcd_USB_OTG_FS;
  37. /* USER CODE BEGIN PV */
  38. /* USER CODE END PV */
  39. /* Private function prototypes -----------------------------------------------*/
  40. void SystemClock_Config(void);
  41. static void MX_GPIO_Init(void);
  42. static void MX_LPUART1_UART_Init(void);
  43. static void MX_USB_OTG_FS_PCD_Init(void);
  44. /* USER CODE BEGIN PFP */
  45. /* USER CODE END PFP */
  46. /* Private user code ---------------------------------------------------------*/
  47. /* USER CODE BEGIN 0 */
  48. /* USER CODE END 0 */
  49. /**
  50. * @brief The application entry point.
  51. * @retval int
  52. */
  53. int main(void)
  54. {
  55. /* USER CODE BEGIN 1 */
  56. /* USER CODE END 1 */
  57. /* MCU Configuration--------------------------------------------------------*/
  58. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  59. HAL_Init();
  60. /* USER CODE BEGIN Init */
  61. /* USER CODE END Init */
  62. /* Configure the system clock */
  63. SystemClock_Config();
  64. /* USER CODE BEGIN SysInit */
  65. /* USER CODE END SysInit */
  66. /* Initialize all configured peripherals */
  67. MX_GPIO_Init();
  68. MX_LPUART1_UART_Init();
  69. MX_USB_OTG_FS_PCD_Init();
  70. /* USER CODE BEGIN 2 */
  71. /* USER CODE END 2 */
  72. /* Infinite loop */
  73. /* USER CODE BEGIN WHILE */
  74. while (1)
  75. {
  76. /* USER CODE END WHILE */
  77. /* USER CODE BEGIN 3 */
  78. }
  79. /* USER CODE END 3 */
  80. }
  81. /**
  82. * @brief System Clock Configuration
  83. * @retval None
  84. */
  85. void SystemClock_Config(void)
  86. {
  87. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  88. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  89. RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
  90. /** Configure LSE Drive Capability
  91. */
  92. HAL_PWR_EnableBkUpAccess();
  93. __HAL_RCC_LSEDRIVE_CONFIG(RCC_LSEDRIVE_LOW);
  94. /** Initializes the CPU, AHB and APB busses clocks
  95. */
  96. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSE|RCC_OSCILLATORTYPE_MSI;
  97. RCC_OscInitStruct.LSEState = RCC_LSE_ON;
  98. RCC_OscInitStruct.MSIState = RCC_MSI_ON;
  99. RCC_OscInitStruct.MSICalibrationValue = 0;
  100. RCC_OscInitStruct.MSIClockRange = RCC_MSIRANGE_6;
  101. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  102. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_MSI;
  103. RCC_OscInitStruct.PLL.PLLM = 1;
  104. RCC_OscInitStruct.PLL.PLLN = 40;
  105. RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  106. RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2;
  107. RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2;
  108. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  109. {
  110. Error_Handler();
  111. }
  112. /** Initializes the CPU, AHB and APB busses clocks
  113. */
  114. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  115. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  116. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  117. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  118. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  119. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  120. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_4) != HAL_OK)
  121. {
  122. Error_Handler();
  123. }
  124. PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_LPUART1|RCC_PERIPHCLK_USB;
  125. PeriphClkInit.Lpuart1ClockSelection = RCC_LPUART1CLKSOURCE_PCLK1;
  126. PeriphClkInit.UsbClockSelection = RCC_USBCLKSOURCE_PLLSAI1;
  127. PeriphClkInit.PLLSAI1.PLLSAI1Source = RCC_PLLSOURCE_MSI;
  128. PeriphClkInit.PLLSAI1.PLLSAI1M = 1;
  129. PeriphClkInit.PLLSAI1.PLLSAI1N = 24;
  130. PeriphClkInit.PLLSAI1.PLLSAI1P = RCC_PLLP_DIV2;
  131. PeriphClkInit.PLLSAI1.PLLSAI1Q = RCC_PLLQ_DIV2;
  132. PeriphClkInit.PLLSAI1.PLLSAI1R = RCC_PLLR_DIV2;
  133. PeriphClkInit.PLLSAI1.PLLSAI1ClockOut = RCC_PLLSAI1_48M2CLK;
  134. if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
  135. {
  136. Error_Handler();
  137. }
  138. /** Configure the main internal regulator output voltage
  139. */
  140. if (HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1) != HAL_OK)
  141. {
  142. Error_Handler();
  143. }
  144. /** Enable MSI Auto calibration
  145. */
  146. HAL_RCCEx_EnableMSIPLLMode();
  147. }
  148. /**
  149. * @brief LPUART1 Initialization Function
  150. * @param None
  151. * @retval None
  152. */
  153. static void MX_LPUART1_UART_Init(void)
  154. {
  155. /* USER CODE BEGIN LPUART1_Init 0 */
  156. /* USER CODE END LPUART1_Init 0 */
  157. /* USER CODE BEGIN LPUART1_Init 1 */
  158. /* USER CODE END LPUART1_Init 1 */
  159. hlpuart1.Instance = LPUART1;
  160. hlpuart1.Init.BaudRate = 115200;
  161. hlpuart1.Init.WordLength = UART_WORDLENGTH_8B;
  162. hlpuart1.Init.StopBits = UART_STOPBITS_1;
  163. hlpuart1.Init.Parity = UART_PARITY_NONE;
  164. hlpuart1.Init.Mode = UART_MODE_TX_RX;
  165. hlpuart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  166. hlpuart1.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
  167. hlpuart1.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
  168. if (HAL_UART_Init(&hlpuart1) != HAL_OK)
  169. {
  170. Error_Handler();
  171. }
  172. /* USER CODE BEGIN LPUART1_Init 2 */
  173. /* USER CODE END LPUART1_Init 2 */
  174. }
  175. /**
  176. * @brief USB_OTG_FS Initialization Function
  177. * @param None
  178. * @retval None
  179. */
  180. static void MX_USB_OTG_FS_PCD_Init(void)
  181. {
  182. /* USER CODE BEGIN USB_OTG_FS_Init 0 */
  183. /* USER CODE END USB_OTG_FS_Init 0 */
  184. /* USER CODE BEGIN USB_OTG_FS_Init 1 */
  185. /* USER CODE END USB_OTG_FS_Init 1 */
  186. hpcd_USB_OTG_FS.Instance = USB_OTG_FS;
  187. hpcd_USB_OTG_FS.Init.dev_endpoints = 6;
  188. hpcd_USB_OTG_FS.Init.speed = PCD_SPEED_FULL;
  189. hpcd_USB_OTG_FS.Init.phy_itface = PCD_PHY_EMBEDDED;
  190. hpcd_USB_OTG_FS.Init.Sof_enable = ENABLE;
  191. hpcd_USB_OTG_FS.Init.low_power_enable = DISABLE;
  192. hpcd_USB_OTG_FS.Init.lpm_enable = DISABLE;
  193. hpcd_USB_OTG_FS.Init.battery_charging_enable = ENABLE;
  194. hpcd_USB_OTG_FS.Init.use_dedicated_ep1 = DISABLE;
  195. hpcd_USB_OTG_FS.Init.vbus_sensing_enable = ENABLE;
  196. if (HAL_PCD_Init(&hpcd_USB_OTG_FS) != HAL_OK)
  197. {
  198. Error_Handler();
  199. }
  200. /* USER CODE BEGIN USB_OTG_FS_Init 2 */
  201. /* USER CODE END USB_OTG_FS_Init 2 */
  202. }
  203. /**
  204. * @brief GPIO Initialization Function
  205. * @param None
  206. * @retval None
  207. */
  208. static void MX_GPIO_Init(void)
  209. {
  210. GPIO_InitTypeDef GPIO_InitStruct = {0};
  211. /* GPIO Ports Clock Enable */
  212. __HAL_RCC_GPIOC_CLK_ENABLE();
  213. __HAL_RCC_GPIOH_CLK_ENABLE();
  214. __HAL_RCC_GPIOB_CLK_ENABLE();
  215. __HAL_RCC_GPIOG_CLK_ENABLE();
  216. HAL_PWREx_EnableVddIO2();
  217. __HAL_RCC_GPIOA_CLK_ENABLE();
  218. /*Configure GPIO pin Output Level */
  219. HAL_GPIO_WritePin(GPIOB, LD3_Pin|LD2_Pin, GPIO_PIN_RESET);
  220. /*Configure GPIO pin Output Level */
  221. HAL_GPIO_WritePin(USB_PowerSwitchOn_GPIO_Port, USB_PowerSwitchOn_Pin, GPIO_PIN_RESET);
  222. /*Configure GPIO pin : B1_Pin */
  223. GPIO_InitStruct.Pin = B1_Pin;
  224. GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
  225. GPIO_InitStruct.Pull = GPIO_NOPULL;
  226. HAL_GPIO_Init(B1_GPIO_Port, &GPIO_InitStruct);
  227. /*Configure GPIO pins : LD3_Pin LD2_Pin */
  228. GPIO_InitStruct.Pin = LD3_Pin|LD2_Pin;
  229. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  230. GPIO_InitStruct.Pull = GPIO_NOPULL;
  231. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  232. HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  233. /*Configure GPIO pin : USB_OverCurrent_Pin */
  234. GPIO_InitStruct.Pin = USB_OverCurrent_Pin;
  235. GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  236. GPIO_InitStruct.Pull = GPIO_NOPULL;
  237. HAL_GPIO_Init(USB_OverCurrent_GPIO_Port, &GPIO_InitStruct);
  238. /*Configure GPIO pin : USB_PowerSwitchOn_Pin */
  239. GPIO_InitStruct.Pin = USB_PowerSwitchOn_Pin;
  240. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  241. GPIO_InitStruct.Pull = GPIO_NOPULL;
  242. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  243. HAL_GPIO_Init(USB_PowerSwitchOn_GPIO_Port, &GPIO_InitStruct);
  244. }
  245. /* USER CODE BEGIN 4 */
  246. /* USER CODE END 4 */
  247. /**
  248. * @brief Period elapsed callback in non blocking mode
  249. * @note This function is called when TIM1 interrupt took place, inside
  250. * HAL_TIM_IRQHandler(). It makes a direct call to HAL_IncTick() to increment
  251. * a global variable "uwTick" used as application time base.
  252. * @param htim : TIM handle
  253. * @retval None
  254. */
  255. void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
  256. {
  257. /* USER CODE BEGIN Callback 0 */
  258. /* USER CODE END Callback 0 */
  259. if (htim->Instance == TIM1) {
  260. HAL_IncTick();
  261. }
  262. /* USER CODE BEGIN Callback 1 */
  263. /* USER CODE END Callback 1 */
  264. }
  265. /**
  266. * @brief This function is executed in case of error occurrence.
  267. * @retval None
  268. */
  269. void Error_Handler(void)
  270. {
  271. /* USER CODE BEGIN Error_Handler_Debug */
  272. /* User can add his own implementation to report the HAL error return state */
  273. /* USER CODE END Error_Handler_Debug */
  274. }
  275. #ifdef USE_FULL_ASSERT
  276. /**
  277. * @brief Reports the name of the source file and the source line number
  278. * where the assert_param error has occurred.
  279. * @param file: pointer to the source file name
  280. * @param line: assert_param error line source number
  281. * @retval None
  282. */
  283. void assert_failed(char *file, uint32_t line)
  284. {
  285. /* USER CODE BEGIN 6 */
  286. /* User can add his own implementation to report the file name and line number,
  287. tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  288. /* USER CODE END 6 */
  289. }
  290. #endif /* USE_FULL_ASSERT */
  291. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/