main.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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) 2019 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. ADC_HandleTypeDef hadc1;
  36. SPI_HandleTypeDef hspi1;
  37. UART_HandleTypeDef huart1;
  38. /* USER CODE BEGIN PV */
  39. /* USER CODE END PV */
  40. /* Private function prototypes -----------------------------------------------*/
  41. void SystemClock_Config(void);
  42. static void MX_GPIO_Init(void);
  43. static void MX_USART1_UART_Init(void);
  44. static void MX_ADC1_Init(void);
  45. static void MX_SPI1_Init(void);
  46. /* USER CODE BEGIN PFP */
  47. /* USER CODE END PFP */
  48. /* Private user code ---------------------------------------------------------*/
  49. /* USER CODE BEGIN 0 */
  50. /* USER CODE END 0 */
  51. /**
  52. * @brief The application entry point.
  53. * @retval int
  54. */
  55. int main(void)
  56. {
  57. /* USER CODE BEGIN 1 */
  58. /* USER CODE END 1 */
  59. /* MCU Configuration--------------------------------------------------------*/
  60. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  61. HAL_Init();
  62. /* USER CODE BEGIN Init */
  63. /* USER CODE END Init */
  64. /* Configure the system clock */
  65. SystemClock_Config();
  66. /* USER CODE BEGIN SysInit */
  67. /* USER CODE END SysInit */
  68. /* Initialize all configured peripherals */
  69. MX_GPIO_Init();
  70. MX_USART1_UART_Init();
  71. MX_ADC1_Init();
  72. MX_SPI1_Init();
  73. /* USER CODE BEGIN 2 */
  74. /* USER CODE END 2 */
  75. /* Infinite loop */
  76. /* USER CODE BEGIN WHILE */
  77. while (1)
  78. {
  79. /* USER CODE END WHILE */
  80. /* USER CODE BEGIN 3 */
  81. }
  82. /* USER CODE END 3 */
  83. }
  84. /**
  85. * @brief System Clock Configuration
  86. * @retval None
  87. */
  88. void SystemClock_Config(void)
  89. {
  90. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  91. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  92. RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
  93. /** Initializes the CPU, AHB and APB busses clocks
  94. */
  95. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  96. RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  97. RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
  98. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  99. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  100. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  101. RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL2;
  102. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  103. {
  104. Error_Handler();
  105. }
  106. /** Initializes the CPU, AHB and APB busses clocks
  107. */
  108. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  109. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  110. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  111. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  112. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  113. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  114. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
  115. {
  116. Error_Handler();
  117. }
  118. PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_ADC;
  119. PeriphClkInit.AdcClockSelection = RCC_ADCPCLK2_DIV2;
  120. if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
  121. {
  122. Error_Handler();
  123. }
  124. }
  125. /**
  126. * @brief ADC1 Initialization Function
  127. * @param None
  128. * @retval None
  129. */
  130. static void MX_ADC1_Init(void)
  131. {
  132. /* USER CODE BEGIN ADC1_Init 0 */
  133. /* USER CODE END ADC1_Init 0 */
  134. ADC_ChannelConfTypeDef sConfig = {0};
  135. /* USER CODE BEGIN ADC1_Init 1 */
  136. /* USER CODE END ADC1_Init 1 */
  137. /** Common config
  138. */
  139. hadc1.Instance = ADC1;
  140. hadc1.Init.ScanConvMode = ADC_SCAN_DISABLE;
  141. hadc1.Init.ContinuousConvMode = DISABLE;
  142. hadc1.Init.DiscontinuousConvMode = DISABLE;
  143. hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
  144. hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
  145. hadc1.Init.NbrOfConversion = 1;
  146. if (HAL_ADC_Init(&hadc1) != HAL_OK)
  147. {
  148. Error_Handler();
  149. }
  150. /** Configure Regular Channel
  151. */
  152. sConfig.Channel = ADC_CHANNEL_1;
  153. sConfig.Rank = ADC_REGULAR_RANK_1;
  154. sConfig.SamplingTime = ADC_SAMPLETIME_1CYCLE_5;
  155. if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
  156. {
  157. Error_Handler();
  158. }
  159. /* USER CODE BEGIN ADC1_Init 2 */
  160. /* USER CODE END ADC1_Init 2 */
  161. }
  162. /**
  163. * @brief SPI1 Initialization Function
  164. * @param None
  165. * @retval None
  166. */
  167. static void MX_SPI1_Init(void)
  168. {
  169. /* USER CODE BEGIN SPI1_Init 0 */
  170. /* USER CODE END SPI1_Init 0 */
  171. /* USER CODE BEGIN SPI1_Init 1 */
  172. /* USER CODE END SPI1_Init 1 */
  173. /* SPI1 parameter configuration*/
  174. hspi1.Instance = SPI1;
  175. hspi1.Init.Mode = SPI_MODE_MASTER;
  176. hspi1.Init.Direction = SPI_DIRECTION_2LINES;
  177. hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
  178. hspi1.Init.CLKPolarity = SPI_POLARITY_LOW;
  179. hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;
  180. hspi1.Init.NSS = SPI_NSS_SOFT;
  181. hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_8;
  182. hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
  183. hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
  184. hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
  185. hspi1.Init.CRCPolynomial = 10;
  186. if (HAL_SPI_Init(&hspi1) != HAL_OK)
  187. {
  188. Error_Handler();
  189. }
  190. /* USER CODE BEGIN SPI1_Init 2 */
  191. /* USER CODE END SPI1_Init 2 */
  192. }
  193. /**
  194. * @brief USART1 Initialization Function
  195. * @param None
  196. * @retval None
  197. */
  198. static void MX_USART1_UART_Init(void)
  199. {
  200. /* USER CODE BEGIN USART1_Init 0 */
  201. /* USER CODE END USART1_Init 0 */
  202. /* USER CODE BEGIN USART1_Init 1 */
  203. /* USER CODE END USART1_Init 1 */
  204. huart1.Instance = USART1;
  205. huart1.Init.BaudRate = 115200;
  206. huart1.Init.WordLength = UART_WORDLENGTH_8B;
  207. huart1.Init.StopBits = UART_STOPBITS_1;
  208. huart1.Init.Parity = UART_PARITY_NONE;
  209. huart1.Init.Mode = UART_MODE_TX_RX;
  210. huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  211. huart1.Init.OverSampling = UART_OVERSAMPLING_16;
  212. if (HAL_UART_Init(&huart1) != HAL_OK)
  213. {
  214. Error_Handler();
  215. }
  216. /* USER CODE BEGIN USART1_Init 2 */
  217. /* USER CODE END USART1_Init 2 */
  218. }
  219. /**
  220. * @brief GPIO Initialization Function
  221. * @param None
  222. * @retval None
  223. */
  224. static void MX_GPIO_Init(void)
  225. {
  226. /* GPIO Ports Clock Enable */
  227. __HAL_RCC_GPIOC_CLK_ENABLE();
  228. __HAL_RCC_GPIOD_CLK_ENABLE();
  229. __HAL_RCC_GPIOA_CLK_ENABLE();
  230. }
  231. /* USER CODE BEGIN 4 */
  232. /* USER CODE END 4 */
  233. /**
  234. * @brief This function is executed in case of error occurrence.
  235. * @retval None
  236. */
  237. void Error_Handler(void)
  238. {
  239. /* USER CODE BEGIN Error_Handler_Debug */
  240. /* User can add his own implementation to report the HAL error return state */
  241. /* USER CODE END Error_Handler_Debug */
  242. }
  243. #ifdef USE_FULL_ASSERT
  244. /**
  245. * @brief Reports the name of the source file and the source line number
  246. * where the assert_param error has occurred.
  247. * @param file: pointer to the source file name
  248. * @param line: assert_param error line source number
  249. * @retval None
  250. */
  251. void assert_failed(uint8_t *file, uint32_t line)
  252. {
  253. /* USER CODE BEGIN 6 */
  254. /* User can add his own implementation to report the file name and line number,
  255. tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  256. /* USER CODE END 6 */
  257. }
  258. #endif /* USE_FULL_ASSERT */
  259. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/