main.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /* USER CODE BEGIN Header */
  2. /**
  3. ******************************************************************************
  4. * @file : main.c
  5. * @brief : Main program body
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * Copyright (c) 2025 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. SPI_HandleTypeDef hspi1;
  35. UART_HandleTypeDef huart1;
  36. /* USER CODE BEGIN PV */
  37. /* USER CODE END PV */
  38. /* Private function prototypes -----------------------------------------------*/
  39. void SystemClock_Config(void);
  40. static void MX_GPIO_Init(void);
  41. static void MX_USART1_UART_Init(void);
  42. static void MX_SPI1_Init(void);
  43. /* USER CODE BEGIN PFP */
  44. /* USER CODE END PFP */
  45. /* Private user code ---------------------------------------------------------*/
  46. /* USER CODE BEGIN 0 */
  47. /* USER CODE END 0 */
  48. /**
  49. * @brief The application entry point.
  50. * @retval int
  51. */
  52. int main(void)
  53. {
  54. /* USER CODE BEGIN 1 */
  55. /* USER CODE END 1 */
  56. /* MCU Configuration--------------------------------------------------------*/
  57. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  58. HAL_Init();
  59. /* USER CODE BEGIN Init */
  60. /* USER CODE END Init */
  61. /* Configure the system clock */
  62. SystemClock_Config();
  63. /* USER CODE BEGIN SysInit */
  64. /* USER CODE END SysInit */
  65. /* Initialize all configured peripherals */
  66. MX_GPIO_Init();
  67. MX_USART1_UART_Init();
  68. MX_SPI1_Init();
  69. /* USER CODE BEGIN 2 */
  70. /* USER CODE END 2 */
  71. /* Infinite loop */
  72. /* USER CODE BEGIN WHILE */
  73. while (1)
  74. {
  75. /* USER CODE END WHILE */
  76. /* USER CODE BEGIN 3 */
  77. }
  78. /* USER CODE END 3 */
  79. }
  80. /**
  81. * @brief System Clock Configuration
  82. * @retval None
  83. */
  84. void SystemClock_Config(void)
  85. {
  86. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  87. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  88. /** Configure the main internal regulator output voltage
  89. */
  90. __HAL_RCC_PWR_CLK_ENABLE();
  91. __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
  92. /** Initializes the RCC Oscillators according to the specified parameters
  93. * in the RCC_OscInitTypeDef structure.
  94. */
  95. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  96. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  97. RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  98. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  99. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
  100. RCC_OscInitStruct.PLL.PLLM = 8;
  101. RCC_OscInitStruct.PLL.PLLN = 168;
  102. RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  103. RCC_OscInitStruct.PLL.PLLQ = 4;
  104. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  105. {
  106. Error_Handler();
  107. }
  108. /** Initializes the CPU, AHB and APB buses clocks
  109. */
  110. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  111. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  112. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  113. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  114. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
  115. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
  116. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK)
  117. {
  118. Error_Handler();
  119. }
  120. }
  121. /**
  122. * @brief SPI1 Initialization Function
  123. * @param None
  124. * @retval None
  125. */
  126. static void MX_SPI1_Init(void)
  127. {
  128. /* USER CODE BEGIN SPI1_Init 0 */
  129. /* USER CODE END SPI1_Init 0 */
  130. /* USER CODE BEGIN SPI1_Init 1 */
  131. /* USER CODE END SPI1_Init 1 */
  132. /* SPI1 parameter configuration*/
  133. hspi1.Instance = SPI1;
  134. hspi1.Init.Mode = SPI_MODE_MASTER;
  135. hspi1.Init.Direction = SPI_DIRECTION_2LINES;
  136. hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
  137. hspi1.Init.CLKPolarity = SPI_POLARITY_LOW;
  138. hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;
  139. hspi1.Init.NSS = SPI_NSS_SOFT;
  140. hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_2;
  141. hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
  142. hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
  143. hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
  144. hspi1.Init.CRCPolynomial = 10;
  145. if (HAL_SPI_Init(&hspi1) != HAL_OK)
  146. {
  147. Error_Handler();
  148. }
  149. /* USER CODE BEGIN SPI1_Init 2 */
  150. /* USER CODE END SPI1_Init 2 */
  151. }
  152. /**
  153. * @brief USART1 Initialization Function
  154. * @param None
  155. * @retval None
  156. */
  157. static void MX_USART1_UART_Init(void)
  158. {
  159. /* USER CODE BEGIN USART1_Init 0 */
  160. /* USER CODE END USART1_Init 0 */
  161. /* USER CODE BEGIN USART1_Init 1 */
  162. /* USER CODE END USART1_Init 1 */
  163. huart1.Instance = USART1;
  164. huart1.Init.BaudRate = 115200;
  165. huart1.Init.WordLength = UART_WORDLENGTH_8B;
  166. huart1.Init.StopBits = UART_STOPBITS_1;
  167. huart1.Init.Parity = UART_PARITY_NONE;
  168. huart1.Init.Mode = UART_MODE_TX_RX;
  169. huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  170. huart1.Init.OverSampling = UART_OVERSAMPLING_16;
  171. if (HAL_UART_Init(&huart1) != HAL_OK)
  172. {
  173. Error_Handler();
  174. }
  175. /* USER CODE BEGIN USART1_Init 2 */
  176. /* USER CODE END USART1_Init 2 */
  177. }
  178. /**
  179. * @brief GPIO Initialization Function
  180. * @param None
  181. * @retval None
  182. */
  183. static void MX_GPIO_Init(void)
  184. {
  185. GPIO_InitTypeDef GPIO_InitStruct = {0};
  186. /* USER CODE BEGIN MX_GPIO_Init_1 */
  187. /* USER CODE END MX_GPIO_Init_1 */
  188. /* GPIO Ports Clock Enable */
  189. __HAL_RCC_GPIOC_CLK_ENABLE();
  190. __HAL_RCC_GPIOH_CLK_ENABLE();
  191. __HAL_RCC_GPIOA_CLK_ENABLE();
  192. /*Configure GPIO pin Output Level */
  193. HAL_GPIO_WritePin(SPI1_NSS_GPIO_Port, SPI1_NSS_Pin, GPIO_PIN_RESET);
  194. /*Configure GPIO pin : SPI1_NSS_Pin */
  195. GPIO_InitStruct.Pin = SPI1_NSS_Pin;
  196. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  197. GPIO_InitStruct.Pull = GPIO_NOPULL;
  198. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  199. HAL_GPIO_Init(SPI1_NSS_GPIO_Port, &GPIO_InitStruct);
  200. /* USER CODE BEGIN MX_GPIO_Init_2 */
  201. /* USER CODE END MX_GPIO_Init_2 */
  202. }
  203. /* USER CODE BEGIN 4 */
  204. /* USER CODE END 4 */
  205. /**
  206. * @brief This function is executed in case of error occurrence.
  207. * @retval None
  208. */
  209. void Error_Handler(void)
  210. {
  211. /* USER CODE BEGIN Error_Handler_Debug */
  212. /* User can add his own implementation to report the HAL error return state */
  213. __disable_irq();
  214. while (1)
  215. {
  216. }
  217. /* USER CODE END Error_Handler_Debug */
  218. }
  219. #ifdef USE_FULL_ASSERT
  220. /**
  221. * @brief Reports the name of the source file and the source line number
  222. * where the assert_param error has occurred.
  223. * @param file: pointer to the source file name
  224. * @param line: assert_param error line source number
  225. * @retval None
  226. */
  227. void assert_failed(uint8_t *file, uint32_t line)
  228. {
  229. /* USER CODE BEGIN 6 */
  230. /* User can add his own implementation to report the file name and line number,
  231. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  232. /* USER CODE END 6 */
  233. }
  234. #endif /* USE_FULL_ASSERT */