main.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. /* USER CODE BEGIN Header */
  2. /**
  3. ******************************************************************************
  4. * @file : main.c
  5. * @brief : Main program body
  6. ******************************************************************************
  7. ** This notice applies to any and all portions of this file
  8. * that are not between comment pairs USER CODE BEGIN and
  9. * USER CODE END. Other portions of this file, whether
  10. * inserted by the user or by software development tools
  11. * are owned by their respective copyright owners.
  12. *
  13. * COPYRIGHT(c) 2018 STMicroelectronics
  14. *
  15. * Redistribution and use in source and binary forms, with or without modification,
  16. * are permitted provided that the following conditions are met:
  17. * 1. Redistributions of source code must retain the above copyright notice,
  18. * this list of conditions and the following disclaimer.
  19. * 2. Redistributions in binary form must reproduce the above copyright notice,
  20. * this list of conditions and the following disclaimer in the documentation
  21. * and/or other materials provided with the distribution.
  22. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  23. * may be used to endorse or promote products derived from this software
  24. * without specific prior written permission.
  25. *
  26. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  27. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  28. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  29. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  30. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  31. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  32. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  33. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  34. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  35. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  36. *
  37. ******************************************************************************
  38. */
  39. /* USER CODE END Header */
  40. /* Includes ------------------------------------------------------------------*/
  41. #include "main.h"
  42. /* Private includes ----------------------------------------------------------*/
  43. /* USER CODE BEGIN Includes */
  44. /* USER CODE END Includes */
  45. /* Private typedef -----------------------------------------------------------*/
  46. /* USER CODE BEGIN PTD */
  47. /* USER CODE END PTD */
  48. /* Private define ------------------------------------------------------------*/
  49. /* USER CODE BEGIN PD */
  50. /* USER CODE END PD */
  51. /* Private macro -------------------------------------------------------------*/
  52. /* USER CODE BEGIN PM */
  53. /* USER CODE END PM */
  54. /* Private variables ---------------------------------------------------------*/
  55. ADC_HandleTypeDef hadc1;
  56. I2C_HandleTypeDef hi2c1;
  57. IWDG_HandleTypeDef hiwdg;
  58. RTC_HandleTypeDef hrtc;
  59. SPI_HandleTypeDef hspi2;
  60. UART_HandleTypeDef huart1;
  61. UART_HandleTypeDef huart2;
  62. /* USER CODE BEGIN PV */
  63. /* USER CODE END PV */
  64. /* Private function prototypes -----------------------------------------------*/
  65. void SystemClock_Config(void);
  66. static void MX_GPIO_Init(void);
  67. static void MX_USART1_UART_Init(void);
  68. static void MX_SPI2_Init(void);
  69. static void MX_I2C1_Init(void);
  70. static void MX_ADC1_Init(void);
  71. static void MX_USART2_UART_Init(void);
  72. static void MX_IWDG_Init(void);
  73. static void MX_RTC_Init(void);
  74. /* USER CODE BEGIN PFP */
  75. /* USER CODE END PFP */
  76. /* Private user code ---------------------------------------------------------*/
  77. /* USER CODE BEGIN 0 */
  78. /* USER CODE END 0 */
  79. /**
  80. * @brief The application entry point.
  81. * @retval int
  82. */
  83. int main(void)
  84. {
  85. /* USER CODE BEGIN 1 */
  86. /* USER CODE END 1 */
  87. /* MCU Configuration--------------------------------------------------------*/
  88. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  89. HAL_Init();
  90. /* USER CODE BEGIN Init */
  91. /* USER CODE END Init */
  92. /* Configure the system clock */
  93. SystemClock_Config();
  94. /* USER CODE BEGIN SysInit */
  95. /* USER CODE END SysInit */
  96. /* Initialize all configured peripherals */
  97. MX_GPIO_Init();
  98. MX_USART1_UART_Init();
  99. MX_SPI2_Init();
  100. MX_I2C1_Init();
  101. MX_ADC1_Init();
  102. MX_USART2_UART_Init();
  103. MX_IWDG_Init();
  104. MX_RTC_Init();
  105. /* USER CODE BEGIN 2 */
  106. /* USER CODE END 2 */
  107. /* Infinite loop */
  108. /* USER CODE BEGIN WHILE */
  109. while (1)
  110. {
  111. /* USER CODE END WHILE */
  112. /* USER CODE BEGIN 3 */
  113. }
  114. /* USER CODE END 3 */
  115. }
  116. /**
  117. * @brief System Clock Configuration
  118. * @retval None
  119. */
  120. void SystemClock_Config(void)
  121. {
  122. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  123. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  124. RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0};
  125. /**Configure the main internal regulator output voltage
  126. */
  127. __HAL_RCC_PWR_CLK_ENABLE();
  128. __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
  129. /**Initializes the CPU, AHB and APB busses clocks
  130. */
  131. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI|RCC_OSCILLATORTYPE_LSI;
  132. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  133. RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  134. RCC_OscInitStruct.LSIState = RCC_LSI_ON;
  135. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  136. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
  137. RCC_OscInitStruct.PLL.PLLM = 16;
  138. RCC_OscInitStruct.PLL.PLLN = 192;
  139. RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  140. RCC_OscInitStruct.PLL.PLLQ = 4;
  141. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  142. {
  143. Error_Handler();
  144. }
  145. /**Initializes the CPU, AHB and APB busses clocks
  146. */
  147. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  148. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  149. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  150. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  151. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  152. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  153. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_3) != HAL_OK)
  154. {
  155. Error_Handler();
  156. }
  157. PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_RTC;
  158. PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSI;
  159. if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK)
  160. {
  161. Error_Handler();
  162. }
  163. }
  164. /**
  165. * @brief ADC1 Initialization Function
  166. * @param None
  167. * @retval None
  168. */
  169. static void MX_ADC1_Init(void)
  170. {
  171. /* USER CODE BEGIN ADC1_Init 0 */
  172. /* USER CODE END ADC1_Init 0 */
  173. ADC_ChannelConfTypeDef sConfig = {0};
  174. /* USER CODE BEGIN ADC1_Init 1 */
  175. /* USER CODE END ADC1_Init 1 */
  176. /**Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion)
  177. */
  178. hadc1.Instance = ADC1;
  179. hadc1.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV4;
  180. hadc1.Init.Resolution = ADC_RESOLUTION_12B;
  181. hadc1.Init.ScanConvMode = DISABLE;
  182. hadc1.Init.ContinuousConvMode = DISABLE;
  183. hadc1.Init.DiscontinuousConvMode = DISABLE;
  184. hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
  185. hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
  186. hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
  187. hadc1.Init.NbrOfConversion = 1;
  188. hadc1.Init.DMAContinuousRequests = DISABLE;
  189. hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
  190. if (HAL_ADC_Init(&hadc1) != HAL_OK)
  191. {
  192. Error_Handler();
  193. }
  194. /**Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
  195. */
  196. sConfig.Channel = ADC_CHANNEL_9;
  197. sConfig.Rank = 1;
  198. sConfig.SamplingTime = ADC_SAMPLETIME_3CYCLES;
  199. if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
  200. {
  201. Error_Handler();
  202. }
  203. /* USER CODE BEGIN ADC1_Init 2 */
  204. /* USER CODE END ADC1_Init 2 */
  205. }
  206. /**
  207. * @brief I2C1 Initialization Function
  208. * @param None
  209. * @retval None
  210. */
  211. static void MX_I2C1_Init(void)
  212. {
  213. /* USER CODE BEGIN I2C1_Init 0 */
  214. /* USER CODE END I2C1_Init 0 */
  215. /* USER CODE BEGIN I2C1_Init 1 */
  216. /* USER CODE END I2C1_Init 1 */
  217. hi2c1.Instance = I2C1;
  218. hi2c1.Init.ClockSpeed = 100000;
  219. hi2c1.Init.DutyCycle = I2C_DUTYCYCLE_2;
  220. hi2c1.Init.OwnAddress1 = 0;
  221. hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
  222. hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
  223. hi2c1.Init.OwnAddress2 = 0;
  224. hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
  225. hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
  226. if (HAL_I2C_Init(&hi2c1) != HAL_OK)
  227. {
  228. Error_Handler();
  229. }
  230. /* USER CODE BEGIN I2C1_Init 2 */
  231. /* USER CODE END I2C1_Init 2 */
  232. }
  233. /**
  234. * @brief IWDG Initialization Function
  235. * @param None
  236. * @retval None
  237. */
  238. static void MX_IWDG_Init(void)
  239. {
  240. /* USER CODE BEGIN IWDG_Init 0 */
  241. /* USER CODE END IWDG_Init 0 */
  242. /* USER CODE BEGIN IWDG_Init 1 */
  243. /* USER CODE END IWDG_Init 1 */
  244. hiwdg.Instance = IWDG;
  245. hiwdg.Init.Prescaler = IWDG_PRESCALER_4;
  246. hiwdg.Init.Reload = 4095;
  247. if (HAL_IWDG_Init(&hiwdg) != HAL_OK)
  248. {
  249. Error_Handler();
  250. }
  251. /* USER CODE BEGIN IWDG_Init 2 */
  252. /* USER CODE END IWDG_Init 2 */
  253. }
  254. /**
  255. * @brief RTC Initialization Function
  256. * @param None
  257. * @retval None
  258. */
  259. static void MX_RTC_Init(void)
  260. {
  261. /* USER CODE BEGIN RTC_Init 0 */
  262. /* USER CODE END RTC_Init 0 */
  263. /* USER CODE BEGIN RTC_Init 1 */
  264. /* USER CODE END RTC_Init 1 */
  265. /**Initialize RTC Only
  266. */
  267. hrtc.Instance = RTC;
  268. hrtc.Init.HourFormat = RTC_HOURFORMAT_24;
  269. hrtc.Init.AsynchPrediv = 127;
  270. hrtc.Init.SynchPrediv = 255;
  271. hrtc.Init.OutPut = RTC_OUTPUT_DISABLE;
  272. hrtc.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
  273. hrtc.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;
  274. if (HAL_RTC_Init(&hrtc) != HAL_OK)
  275. {
  276. Error_Handler();
  277. }
  278. /* USER CODE BEGIN RTC_Init 2 */
  279. /* USER CODE END RTC_Init 2 */
  280. }
  281. /**
  282. * @brief SPI2 Initialization Function
  283. * @param None
  284. * @retval None
  285. */
  286. static void MX_SPI2_Init(void)
  287. {
  288. /* USER CODE BEGIN SPI2_Init 0 */
  289. /* USER CODE END SPI2_Init 0 */
  290. /* USER CODE BEGIN SPI2_Init 1 */
  291. /* USER CODE END SPI2_Init 1 */
  292. /* SPI2 parameter configuration*/
  293. hspi2.Instance = SPI2;
  294. hspi2.Init.Mode = SPI_MODE_MASTER;
  295. hspi2.Init.Direction = SPI_DIRECTION_2LINES;
  296. hspi2.Init.DataSize = SPI_DATASIZE_8BIT;
  297. hspi2.Init.CLKPolarity = SPI_POLARITY_LOW;
  298. hspi2.Init.CLKPhase = SPI_PHASE_1EDGE;
  299. hspi2.Init.NSS = SPI_NSS_SOFT;
  300. hspi2.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_2;
  301. hspi2.Init.FirstBit = SPI_FIRSTBIT_MSB;
  302. hspi2.Init.TIMode = SPI_TIMODE_DISABLE;
  303. hspi2.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
  304. hspi2.Init.CRCPolynomial = 10;
  305. if (HAL_SPI_Init(&hspi2) != HAL_OK)
  306. {
  307. Error_Handler();
  308. }
  309. /* USER CODE BEGIN SPI2_Init 2 */
  310. /* USER CODE END SPI2_Init 2 */
  311. }
  312. /**
  313. * @brief USART1 Initialization Function
  314. * @param None
  315. * @retval None
  316. */
  317. static void MX_USART1_UART_Init(void)
  318. {
  319. /* USER CODE BEGIN USART1_Init 0 */
  320. /* USER CODE END USART1_Init 0 */
  321. /* USER CODE BEGIN USART1_Init 1 */
  322. /* USER CODE END USART1_Init 1 */
  323. huart1.Instance = USART1;
  324. huart1.Init.BaudRate = 115200;
  325. huart1.Init.WordLength = UART_WORDLENGTH_8B;
  326. huart1.Init.StopBits = UART_STOPBITS_1;
  327. huart1.Init.Parity = UART_PARITY_NONE;
  328. huart1.Init.Mode = UART_MODE_TX_RX;
  329. huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  330. huart1.Init.OverSampling = UART_OVERSAMPLING_16;
  331. if (HAL_UART_Init(&huart1) != HAL_OK)
  332. {
  333. Error_Handler();
  334. }
  335. /* USER CODE BEGIN USART1_Init 2 */
  336. /* USER CODE END USART1_Init 2 */
  337. }
  338. /**
  339. * @brief USART2 Initialization Function
  340. * @param None
  341. * @retval None
  342. */
  343. static void MX_USART2_UART_Init(void)
  344. {
  345. /* USER CODE BEGIN USART2_Init 0 */
  346. /* USER CODE END USART2_Init 0 */
  347. /* USER CODE BEGIN USART2_Init 1 */
  348. /* USER CODE END USART2_Init 1 */
  349. huart2.Instance = USART2;
  350. huart2.Init.BaudRate = 115200;
  351. huart2.Init.WordLength = UART_WORDLENGTH_8B;
  352. huart2.Init.StopBits = UART_STOPBITS_1;
  353. huart2.Init.Parity = UART_PARITY_NONE;
  354. huart2.Init.Mode = UART_MODE_TX_RX;
  355. huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  356. huart2.Init.OverSampling = UART_OVERSAMPLING_16;
  357. if (HAL_UART_Init(&huart2) != HAL_OK)
  358. {
  359. Error_Handler();
  360. }
  361. /* USER CODE BEGIN USART2_Init 2 */
  362. /* USER CODE END USART2_Init 2 */
  363. }
  364. /**
  365. * @brief GPIO Initialization Function
  366. * @param None
  367. * @retval None
  368. */
  369. static void MX_GPIO_Init(void)
  370. {
  371. /* GPIO Ports Clock Enable */
  372. __HAL_RCC_GPIOC_CLK_ENABLE();
  373. __HAL_RCC_GPIOH_CLK_ENABLE();
  374. __HAL_RCC_GPIOA_CLK_ENABLE();
  375. __HAL_RCC_GPIOB_CLK_ENABLE();
  376. }
  377. /* USER CODE BEGIN 4 */
  378. /* USER CODE END 4 */
  379. /**
  380. * @brief This function is executed in case of error occurrence.
  381. * @retval None
  382. */
  383. void Error_Handler(void)
  384. {
  385. /* USER CODE BEGIN Error_Handler_Debug */
  386. /* User can add his own implementation to report the HAL error return state */
  387. /* USER CODE END Error_Handler_Debug */
  388. }
  389. #ifdef USE_FULL_ASSERT
  390. /**
  391. * @brief Reports the name of the source file and the source line number
  392. * where the assert_param error has occurred.
  393. * @param file: pointer to the source file name
  394. * @param line: assert_param error line source number
  395. * @retval None
  396. */
  397. void assert_failed(uint8_t *file, uint32_t line)
  398. {
  399. /* USER CODE BEGIN 6 */
  400. /* User can add his own implementation to report the file name and line number,
  401. tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  402. /* USER CODE END 6 */
  403. }
  404. #endif /* USE_FULL_ASSERT */
  405. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/