main.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /* USER CODE BEGIN Header */
  2. /**
  3. ******************************************************************************
  4. * @file : main.c
  5. * @brief : Main program body
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * Copyright (c) 2023 STMicroelectronics.
  10. * All rights reserved.
  11. *
  12. * This software is licensed under terms that can be found in the LICENSE file
  13. * in the root directory of this software component.
  14. * If no LICENSE file comes with this software, it is provided AS-IS.
  15. *
  16. ******************************************************************************
  17. */
  18. /* USER CODE END Header */
  19. /* Includes ------------------------------------------------------------------*/
  20. #include "main.h"
  21. /* Private includes ----------------------------------------------------------*/
  22. /* USER CODE BEGIN Includes */
  23. /* USER CODE END Includes */
  24. /* Private typedef -----------------------------------------------------------*/
  25. /* USER CODE BEGIN PTD */
  26. /* USER CODE END PTD */
  27. /* Private define ------------------------------------------------------------*/
  28. /* USER CODE BEGIN PD */
  29. /* USER CODE END PD */
  30. /* Private macro -------------------------------------------------------------*/
  31. /* USER CODE BEGIN PM */
  32. /* USER CODE END PM */
  33. /* Private variables ---------------------------------------------------------*/
  34. UART_HandleTypeDef huart2;
  35. /* USER CODE BEGIN PV */
  36. /* USER CODE END PV */
  37. /* Private function prototypes -----------------------------------------------*/
  38. void SystemClock_Config(void);
  39. static void MX_GPIO_Init(void);
  40. static void MX_USART2_UART_Init(void);
  41. /* USER CODE BEGIN PFP */
  42. /* USER CODE END PFP */
  43. /* Private user code ---------------------------------------------------------*/
  44. /* USER CODE BEGIN 0 */
  45. /* USER CODE END 0 */
  46. /**
  47. * @brief The application entry point.
  48. * @retval int
  49. */
  50. int main(void)
  51. {
  52. /* USER CODE BEGIN 1 */
  53. /* USER CODE END 1 */
  54. /* MCU Configuration--------------------------------------------------------*/
  55. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  56. HAL_Init();
  57. /* USER CODE BEGIN Init */
  58. /* USER CODE END Init */
  59. /* Configure the system clock */
  60. SystemClock_Config();
  61. /* USER CODE BEGIN SysInit */
  62. /* USER CODE END SysInit */
  63. /* Initialize all configured peripherals */
  64. MX_GPIO_Init();
  65. MX_USART2_UART_Init();
  66. /* USER CODE BEGIN 2 */
  67. /* USER CODE END 2 */
  68. /* Infinite loop */
  69. /* USER CODE BEGIN WHILE */
  70. while (1)
  71. {
  72. /* USER CODE END WHILE */
  73. /* USER CODE BEGIN 3 */
  74. }
  75. /* USER CODE END 3 */
  76. }
  77. /**
  78. * @brief System Clock Configuration
  79. * @retval None
  80. */
  81. void SystemClock_Config(void)
  82. {
  83. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  84. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  85. RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
  86. /** Configure the main internal regulator output voltage
  87. */
  88. __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
  89. /** Initializes the RCC Oscillators according to the specified parameters
  90. * in the RCC_OscInitTypeDef structure.
  91. */
  92. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  93. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  94. RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  95. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  96. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
  97. RCC_OscInitStruct.PLL.PLLMUL = RCC_PLLMUL_4;
  98. RCC_OscInitStruct.PLL.PLLDIV = RCC_PLLDIV_2;
  99. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  100. {
  101. Error_Handler();
  102. }
  103. /** Initializes the CPU, AHB and APB buses clocks
  104. */
  105. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  106. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  107. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  108. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  109. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  110. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  111. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
  112. {
  113. Error_Handler();
  114. }
  115. PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART2;
  116. PeriphClkInit.Usart2ClockSelection = RCC_USART2CLKSOURCE_PCLK1;
  117. if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
  118. {
  119. Error_Handler();
  120. }
  121. }
  122. /**
  123. * @brief USART2 Initialization Function
  124. * @param None
  125. * @retval None
  126. */
  127. static void MX_USART2_UART_Init(void)
  128. {
  129. /* USER CODE BEGIN USART2_Init 0 */
  130. /* USER CODE END USART2_Init 0 */
  131. /* USER CODE BEGIN USART2_Init 1 */
  132. /* USER CODE END USART2_Init 1 */
  133. huart2.Instance = USART2;
  134. huart2.Init.BaudRate = 115200;
  135. huart2.Init.WordLength = UART_WORDLENGTH_8B;
  136. huart2.Init.StopBits = UART_STOPBITS_1;
  137. huart2.Init.Parity = UART_PARITY_NONE;
  138. huart2.Init.Mode = UART_MODE_TX_RX;
  139. huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  140. huart2.Init.OverSampling = UART_OVERSAMPLING_16;
  141. huart2.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
  142. huart2.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
  143. if (HAL_UART_Init(&huart2) != HAL_OK)
  144. {
  145. Error_Handler();
  146. }
  147. /* USER CODE BEGIN USART2_Init 2 */
  148. /* USER CODE END USART2_Init 2 */
  149. }
  150. /**
  151. * @brief GPIO Initialization Function
  152. * @param None
  153. * @retval None
  154. */
  155. static void MX_GPIO_Init(void)
  156. {
  157. GPIO_InitTypeDef GPIO_InitStruct = {0};
  158. /* USER CODE BEGIN MX_GPIO_Init_1 */
  159. /* USER CODE END MX_GPIO_Init_1 */
  160. /* GPIO Ports Clock Enable */
  161. __HAL_RCC_GPIOC_CLK_ENABLE();
  162. __HAL_RCC_GPIOA_CLK_ENABLE();
  163. __HAL_RCC_GPIOB_CLK_ENABLE();
  164. /*Configure GPIO pin Output Level */
  165. HAL_GPIO_WritePin(LD3_GPIO_Port, LD3_Pin, GPIO_PIN_RESET);
  166. /*Configure GPIO pin : LD3_Pin */
  167. GPIO_InitStruct.Pin = LD3_Pin;
  168. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  169. GPIO_InitStruct.Pull = GPIO_NOPULL;
  170. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  171. HAL_GPIO_Init(LD3_GPIO_Port, &GPIO_InitStruct);
  172. /* USER CODE BEGIN MX_GPIO_Init_2 */
  173. /* USER CODE END MX_GPIO_Init_2 */
  174. }
  175. /* USER CODE BEGIN 4 */
  176. /* USER CODE END 4 */
  177. /**
  178. * @brief This function is executed in case of error occurrence.
  179. * @retval None
  180. */
  181. void Error_Handler(void)
  182. {
  183. /* USER CODE BEGIN Error_Handler_Debug */
  184. /* User can add his own implementation to report the HAL error return state */
  185. __disable_irq();
  186. while (1)
  187. {
  188. }
  189. /* USER CODE END Error_Handler_Debug */
  190. }
  191. #ifdef USE_FULL_ASSERT
  192. /**
  193. * @brief Reports the name of the source file and the source line number
  194. * where the assert_param error has occurred.
  195. * @param file: pointer to the source file name
  196. * @param line: assert_param error line source number
  197. * @retval None
  198. */
  199. void assert_failed(uint8_t *file, uint32_t line)
  200. {
  201. /* USER CODE BEGIN 6 */
  202. /* User can add his own implementation to report the file name and line number,
  203. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  204. /* USER CODE END 6 */
  205. }
  206. #endif /* USE_FULL_ASSERT */