main.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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 huart2;
  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_USART2_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_USART2_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. /** Initializes the RCC Oscillators according to the specified parameters
  89. * in the RCC_OscInitTypeDef structure.
  90. */
  91. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  92. RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  93. RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
  94. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  95. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  96. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  97. RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL6;
  98. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  99. {
  100. Error_Handler();
  101. }
  102. /** Initializes the CPU, AHB and APB buses clocks
  103. */
  104. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  105. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  106. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  107. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  108. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  109. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  110. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
  111. {
  112. Error_Handler();
  113. }
  114. }
  115. /**
  116. * @brief SPI1 Initialization Function
  117. * @param None
  118. * @retval None
  119. */
  120. static void MX_SPI1_Init(void)
  121. {
  122. /* USER CODE BEGIN SPI1_Init 0 */
  123. /* USER CODE END SPI1_Init 0 */
  124. /* USER CODE BEGIN SPI1_Init 1 */
  125. /* USER CODE END SPI1_Init 1 */
  126. /* SPI1 parameter configuration*/
  127. hspi1.Instance = SPI1;
  128. hspi1.Init.Mode = SPI_MODE_MASTER;
  129. hspi1.Init.Direction = SPI_DIRECTION_2LINES;
  130. hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
  131. hspi1.Init.CLKPolarity = SPI_POLARITY_LOW;
  132. hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;
  133. hspi1.Init.NSS = SPI_NSS_SOFT;
  134. hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_64;
  135. hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
  136. hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
  137. hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
  138. hspi1.Init.CRCPolynomial = 10;
  139. if (HAL_SPI_Init(&hspi1) != HAL_OK)
  140. {
  141. Error_Handler();
  142. }
  143. /* USER CODE BEGIN SPI1_Init 2 */
  144. /* USER CODE END SPI1_Init 2 */
  145. }
  146. /**
  147. * @brief USART2 Initialization Function
  148. * @param None
  149. * @retval None
  150. */
  151. static void MX_USART2_UART_Init(void)
  152. {
  153. /* USER CODE BEGIN USART2_Init 0 */
  154. /* USER CODE END USART2_Init 0 */
  155. /* USER CODE BEGIN USART2_Init 1 */
  156. /* USER CODE END USART2_Init 1 */
  157. huart2.Instance = USART2;
  158. huart2.Init.BaudRate = 9600;
  159. huart2.Init.WordLength = UART_WORDLENGTH_8B;
  160. huart2.Init.StopBits = UART_STOPBITS_1;
  161. huart2.Init.Parity = UART_PARITY_NONE;
  162. huart2.Init.Mode = UART_MODE_TX_RX;
  163. huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  164. huart2.Init.OverSampling = UART_OVERSAMPLING_16;
  165. if (HAL_UART_Init(&huart2) != HAL_OK)
  166. {
  167. Error_Handler();
  168. }
  169. /* USER CODE BEGIN USART2_Init 2 */
  170. /* USER CODE END USART2_Init 2 */
  171. }
  172. /**
  173. * @brief GPIO Initialization Function
  174. * @param None
  175. * @retval None
  176. */
  177. static void MX_GPIO_Init(void)
  178. {
  179. GPIO_InitTypeDef GPIO_InitStruct = {0};
  180. /* GPIO Ports Clock Enable */
  181. __HAL_RCC_GPIOC_CLK_ENABLE();
  182. __HAL_RCC_GPIOD_CLK_ENABLE();
  183. __HAL_RCC_GPIOA_CLK_ENABLE();
  184. /*Configure GPIO pin Output Level */
  185. HAL_GPIO_WritePin(DC_GPIO_Port, DC_Pin, GPIO_PIN_RESET);
  186. /*Configure GPIO pin Output Level */
  187. HAL_GPIO_WritePin(GPIOA, RST_Pin|PWR_Pin, GPIO_PIN_RESET);
  188. /*Configure GPIO pin : DC_Pin */
  189. GPIO_InitStruct.Pin = DC_Pin;
  190. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  191. GPIO_InitStruct.Pull = GPIO_NOPULL;
  192. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  193. HAL_GPIO_Init(DC_GPIO_Port, &GPIO_InitStruct);
  194. /*Configure GPIO pins : RST_Pin PWR_Pin */
  195. GPIO_InitStruct.Pin = RST_Pin|PWR_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(GPIOA, &GPIO_InitStruct);
  200. }
  201. /* USER CODE BEGIN 4 */
  202. /* USER CODE END 4 */
  203. /**
  204. * @brief This function is executed in case of error occurrence.
  205. * @retval None
  206. */
  207. void Error_Handler(void)
  208. {
  209. /* USER CODE BEGIN Error_Handler_Debug */
  210. /* User can add his own implementation to report the HAL error return state */
  211. __disable_irq();
  212. while (1)
  213. {
  214. }
  215. /* USER CODE END Error_Handler_Debug */
  216. }
  217. #ifdef USE_FULL_ASSERT
  218. /**
  219. * @brief Reports the name of the source file and the source line number
  220. * where the assert_param error has occurred.
  221. * @param file: pointer to the source file name
  222. * @param line: assert_param error line source number
  223. * @retval None
  224. */
  225. void assert_failed(uint8_t *file, uint32_t line)
  226. {
  227. /* USER CODE BEGIN 6 */
  228. /* User can add his own implementation to report the file name and line number,
  229. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  230. /* USER CODE END 6 */
  231. }
  232. #endif /* USE_FULL_ASSERT */