main.c 9.8 KB

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