main.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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. SPI_HandleTypeDef hspi2;
  36. UART_HandleTypeDef huart1;
  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_SPI2_Init(void);
  43. static void MX_USART1_UART_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_SPI2_Init();
  69. MX_USART1_UART_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_DIV7;
  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_DIV1;
  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_USART1;
  125. PeriphClkInit.Usart1ClockSelection = RCC_USART1CLKSOURCE_PCLK2;
  126. if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
  127. {
  128. Error_Handler();
  129. }
  130. /** Configure the main internal regulator output voltage
  131. */
  132. if (HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1) != HAL_OK)
  133. {
  134. Error_Handler();
  135. }
  136. /** Enable MSI Auto calibration
  137. */
  138. HAL_RCCEx_EnableMSIPLLMode();
  139. }
  140. /**
  141. * @brief SPI2 Initialization Function
  142. * @param None
  143. * @retval None
  144. */
  145. static void MX_SPI2_Init(void)
  146. {
  147. /* USER CODE BEGIN SPI2_Init 0 */
  148. /* USER CODE END SPI2_Init 0 */
  149. /* USER CODE BEGIN SPI2_Init 1 */
  150. /* USER CODE END SPI2_Init 1 */
  151. /* SPI2 parameter configuration*/
  152. hspi2.Instance = SPI2;
  153. hspi2.Init.Mode = SPI_MODE_MASTER;
  154. hspi2.Init.Direction = SPI_DIRECTION_1LINE;
  155. hspi2.Init.DataSize = SPI_DATASIZE_4BIT;
  156. hspi2.Init.CLKPolarity = SPI_POLARITY_LOW;
  157. hspi2.Init.CLKPhase = SPI_PHASE_1EDGE;
  158. hspi2.Init.NSS = SPI_NSS_SOFT;
  159. hspi2.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_2;
  160. hspi2.Init.FirstBit = SPI_FIRSTBIT_MSB;
  161. hspi2.Init.TIMode = SPI_TIMODE_DISABLE;
  162. hspi2.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
  163. hspi2.Init.CRCPolynomial = 7;
  164. hspi2.Init.CRCLength = SPI_CRC_LENGTH_DATASIZE;
  165. hspi2.Init.NSSPMode = SPI_NSS_PULSE_ENABLE;
  166. if (HAL_SPI_Init(&hspi2) != HAL_OK)
  167. {
  168. Error_Handler();
  169. }
  170. /* USER CODE BEGIN SPI2_Init 2 */
  171. /* USER CODE END SPI2_Init 2 */
  172. }
  173. /**
  174. * @brief USART1 Initialization Function
  175. * @param None
  176. * @retval None
  177. */
  178. static void MX_USART1_UART_Init(void)
  179. {
  180. /* USER CODE BEGIN USART1_Init 0 */
  181. /* USER CODE END USART1_Init 0 */
  182. /* USER CODE BEGIN USART1_Init 1 */
  183. /* USER CODE END USART1_Init 1 */
  184. huart1.Instance = USART1;
  185. huart1.Init.BaudRate = 115200;
  186. huart1.Init.WordLength = UART_WORDLENGTH_8B;
  187. huart1.Init.StopBits = UART_STOPBITS_1;
  188. huart1.Init.Parity = UART_PARITY_NONE;
  189. huart1.Init.Mode = UART_MODE_TX_RX;
  190. huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  191. huart1.Init.OverSampling = UART_OVERSAMPLING_16;
  192. huart1.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
  193. huart1.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
  194. if (HAL_UART_Init(&huart1) != HAL_OK)
  195. {
  196. Error_Handler();
  197. }
  198. /* USER CODE BEGIN USART1_Init 2 */
  199. /* USER CODE END USART1_Init 2 */
  200. }
  201. /**
  202. * @brief GPIO Initialization Function
  203. * @param None
  204. * @retval None
  205. */
  206. static void MX_GPIO_Init(void)
  207. {
  208. /* GPIO Ports Clock Enable */
  209. __HAL_RCC_GPIOC_CLK_ENABLE();
  210. __HAL_RCC_GPIOH_CLK_ENABLE();
  211. __HAL_RCC_GPIOB_CLK_ENABLE();
  212. __HAL_RCC_GPIOA_CLK_ENABLE();
  213. }
  214. /* USER CODE BEGIN 4 */
  215. /* USER CODE END 4 */
  216. /**
  217. * @brief This function is executed in case of error occurrence.
  218. * @retval None
  219. */
  220. void Error_Handler(void)
  221. {
  222. /* USER CODE BEGIN Error_Handler_Debug */
  223. /* User can add his own implementation to report the HAL error return state */
  224. /* USER CODE END Error_Handler_Debug */
  225. }
  226. #ifdef USE_FULL_ASSERT
  227. /**
  228. * @brief Reports the name of the source file and the source line number
  229. * where the assert_param error has occurred.
  230. * @param file: pointer to the source file name
  231. * @param line: assert_param error line source number
  232. * @retval None
  233. */
  234. void assert_failed(uint8_t *file, uint32_t line)
  235. {
  236. /* USER CODE BEGIN 6 */
  237. /* User can add his own implementation to report the file name and line number,
  238. tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  239. /* USER CODE END 6 */
  240. }
  241. #endif /* USE_FULL_ASSERT */
  242. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/