main.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  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. IWDG_HandleTypeDef hiwdg;
  56. RTC_HandleTypeDef hrtc;
  57. SPI_HandleTypeDef hspi1;
  58. TIM_HandleTypeDef htim3;
  59. UART_HandleTypeDef huart2;
  60. UART_HandleTypeDef huart6;
  61. /* USER CODE BEGIN PV */
  62. /* USER CODE END PV */
  63. /* Private function prototypes -----------------------------------------------*/
  64. void SystemClock_Config(void);
  65. static void MX_GPIO_Init(void);
  66. static void MX_USART2_UART_Init(void);
  67. static void MX_TIM3_Init(void);
  68. static void MX_USART6_UART_Init(void);
  69. static void MX_RTC_Init(void);
  70. static void MX_IWDG_Init(void);
  71. static void MX_SPI1_Init(void);
  72. /* USER CODE BEGIN PFP */
  73. /* USER CODE END PFP */
  74. /* Private user code ---------------------------------------------------------*/
  75. /* USER CODE BEGIN 0 */
  76. /* USER CODE END 0 */
  77. /**
  78. * @brief The application entry point.
  79. * @retval int
  80. */
  81. int main(void)
  82. {
  83. /* USER CODE BEGIN 1 */
  84. /* USER CODE END 1 */
  85. /* MCU Configuration--------------------------------------------------------*/
  86. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  87. HAL_Init();
  88. /* USER CODE BEGIN Init */
  89. /* USER CODE END Init */
  90. /* Configure the system clock */
  91. SystemClock_Config();
  92. /* USER CODE BEGIN SysInit */
  93. /* USER CODE END SysInit */
  94. /* Initialize all configured peripherals */
  95. MX_GPIO_Init();
  96. MX_USART2_UART_Init();
  97. MX_TIM3_Init();
  98. MX_USART6_UART_Init();
  99. MX_RTC_Init();
  100. MX_IWDG_Init();
  101. MX_SPI1_Init();
  102. /* USER CODE BEGIN 2 */
  103. /* USER CODE END 2 */
  104. /* Infinite loop */
  105. /* USER CODE BEGIN WHILE */
  106. while (1)
  107. {
  108. /* USER CODE END WHILE */
  109. /* USER CODE BEGIN 3 */
  110. }
  111. /* USER CODE END 3 */
  112. }
  113. /**
  114. * @brief System Clock Configuration
  115. * @retval None
  116. */
  117. void SystemClock_Config(void)
  118. {
  119. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  120. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  121. RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0};
  122. /**Configure the main internal regulator output voltage
  123. */
  124. __HAL_RCC_PWR_CLK_ENABLE();
  125. __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
  126. /**Initializes the CPU, AHB and APB busses clocks
  127. */
  128. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI|RCC_OSCILLATORTYPE_HSE;
  129. RCC_OscInitStruct.HSEState = RCC_HSE_BYPASS;
  130. RCC_OscInitStruct.LSIState = RCC_LSI_ON;
  131. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  132. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  133. RCC_OscInitStruct.PLL.PLLM = 4;
  134. RCC_OscInitStruct.PLL.PLLN = 84;
  135. RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  136. RCC_OscInitStruct.PLL.PLLQ = 4;
  137. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  138. {
  139. Error_Handler();
  140. }
  141. /**Initializes the CPU, AHB and APB busses clocks
  142. */
  143. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  144. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  145. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  146. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  147. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  148. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  149. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  150. {
  151. Error_Handler();
  152. }
  153. PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_RTC;
  154. PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSI;
  155. if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK)
  156. {
  157. Error_Handler();
  158. }
  159. }
  160. /**
  161. * @brief IWDG Initialization Function
  162. * @param None
  163. * @retval None
  164. */
  165. static void MX_IWDG_Init(void)
  166. {
  167. /* USER CODE BEGIN IWDG_Init 0 */
  168. /* USER CODE END IWDG_Init 0 */
  169. /* USER CODE BEGIN IWDG_Init 1 */
  170. /* USER CODE END IWDG_Init 1 */
  171. hiwdg.Instance = IWDG;
  172. hiwdg.Init.Prescaler = IWDG_PRESCALER_4;
  173. hiwdg.Init.Reload = 4095;
  174. if (HAL_IWDG_Init(&hiwdg) != HAL_OK)
  175. {
  176. Error_Handler();
  177. }
  178. /* USER CODE BEGIN IWDG_Init 2 */
  179. /* USER CODE END IWDG_Init 2 */
  180. }
  181. /**
  182. * @brief RTC Initialization Function
  183. * @param None
  184. * @retval None
  185. */
  186. static void MX_RTC_Init(void)
  187. {
  188. /* USER CODE BEGIN RTC_Init 0 */
  189. /* USER CODE END RTC_Init 0 */
  190. /* USER CODE BEGIN RTC_Init 1 */
  191. /* USER CODE END RTC_Init 1 */
  192. /**Initialize RTC Only
  193. */
  194. hrtc.Instance = RTC;
  195. hrtc.Init.HourFormat = RTC_HOURFORMAT_24;
  196. hrtc.Init.AsynchPrediv = 127;
  197. hrtc.Init.SynchPrediv = 255;
  198. hrtc.Init.OutPut = RTC_OUTPUT_DISABLE;
  199. hrtc.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
  200. hrtc.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;
  201. if (HAL_RTC_Init(&hrtc) != HAL_OK)
  202. {
  203. Error_Handler();
  204. }
  205. /* USER CODE BEGIN RTC_Init 2 */
  206. /* USER CODE END RTC_Init 2 */
  207. }
  208. /**
  209. * @brief SPI1 Initialization Function
  210. * @param None
  211. * @retval None
  212. */
  213. static void MX_SPI1_Init(void)
  214. {
  215. /* USER CODE BEGIN SPI1_Init 0 */
  216. /* USER CODE END SPI1_Init 0 */
  217. /* USER CODE BEGIN SPI1_Init 1 */
  218. /* USER CODE END SPI1_Init 1 */
  219. /* SPI1 parameter configuration*/
  220. hspi1.Instance = SPI1;
  221. hspi1.Init.Mode = SPI_MODE_MASTER;
  222. hspi1.Init.Direction = SPI_DIRECTION_2LINES;
  223. hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
  224. hspi1.Init.CLKPolarity = SPI_POLARITY_LOW;
  225. hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;
  226. hspi1.Init.NSS = SPI_NSS_SOFT;
  227. hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_2;
  228. hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
  229. hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
  230. hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
  231. hspi1.Init.CRCPolynomial = 10;
  232. if (HAL_SPI_Init(&hspi1) != HAL_OK)
  233. {
  234. Error_Handler();
  235. }
  236. /* USER CODE BEGIN SPI1_Init 2 */
  237. /* USER CODE END SPI1_Init 2 */
  238. }
  239. /**
  240. * @brief TIM3 Initialization Function
  241. * @param None
  242. * @retval None
  243. */
  244. static void MX_TIM3_Init(void)
  245. {
  246. /* USER CODE BEGIN TIM3_Init 0 */
  247. /* USER CODE END TIM3_Init 0 */
  248. TIM_ClockConfigTypeDef sClockSourceConfig = {0};
  249. TIM_MasterConfigTypeDef sMasterConfig = {0};
  250. TIM_OC_InitTypeDef sConfigOC = {0};
  251. /* USER CODE BEGIN TIM3_Init 1 */
  252. /* USER CODE END TIM3_Init 1 */
  253. htim3.Instance = TIM3;
  254. htim3.Init.Prescaler = 0;
  255. htim3.Init.CounterMode = TIM_COUNTERMODE_UP;
  256. htim3.Init.Period = 0;
  257. htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  258. if (HAL_TIM_Base_Init(&htim3) != HAL_OK)
  259. {
  260. Error_Handler();
  261. }
  262. sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
  263. if (HAL_TIM_ConfigClockSource(&htim3, &sClockSourceConfig) != HAL_OK)
  264. {
  265. Error_Handler();
  266. }
  267. if (HAL_TIM_PWM_Init(&htim3) != HAL_OK)
  268. {
  269. Error_Handler();
  270. }
  271. sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
  272. sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  273. if (HAL_TIMEx_MasterConfigSynchronization(&htim3, &sMasterConfig) != HAL_OK)
  274. {
  275. Error_Handler();
  276. }
  277. sConfigOC.OCMode = TIM_OCMODE_PWM1;
  278. sConfigOC.Pulse = 0;
  279. sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
  280. sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
  281. if (HAL_TIM_PWM_ConfigChannel(&htim3, &sConfigOC, TIM_CHANNEL_1) != HAL_OK)
  282. {
  283. Error_Handler();
  284. }
  285. if (HAL_TIM_PWM_ConfigChannel(&htim3, &sConfigOC, TIM_CHANNEL_2) != HAL_OK)
  286. {
  287. Error_Handler();
  288. }
  289. if (HAL_TIM_PWM_ConfigChannel(&htim3, &sConfigOC, TIM_CHANNEL_3) != HAL_OK)
  290. {
  291. Error_Handler();
  292. }
  293. if (HAL_TIM_PWM_ConfigChannel(&htim3, &sConfigOC, TIM_CHANNEL_4) != HAL_OK)
  294. {
  295. Error_Handler();
  296. }
  297. /* USER CODE BEGIN TIM3_Init 2 */
  298. /* USER CODE END TIM3_Init 2 */
  299. HAL_TIM_MspPostInit(&htim3);
  300. }
  301. /**
  302. * @brief USART2 Initialization Function
  303. * @param None
  304. * @retval None
  305. */
  306. static void MX_USART2_UART_Init(void)
  307. {
  308. /* USER CODE BEGIN USART2_Init 0 */
  309. /* USER CODE END USART2_Init 0 */
  310. /* USER CODE BEGIN USART2_Init 1 */
  311. /* USER CODE END USART2_Init 1 */
  312. huart2.Instance = USART2;
  313. huart2.Init.BaudRate = 115200;
  314. huart2.Init.WordLength = UART_WORDLENGTH_8B;
  315. huart2.Init.StopBits = UART_STOPBITS_1;
  316. huart2.Init.Parity = UART_PARITY_NONE;
  317. huart2.Init.Mode = UART_MODE_TX_RX;
  318. huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  319. huart2.Init.OverSampling = UART_OVERSAMPLING_16;
  320. if (HAL_UART_Init(&huart2) != HAL_OK)
  321. {
  322. Error_Handler();
  323. }
  324. /* USER CODE BEGIN USART2_Init 2 */
  325. /* USER CODE END USART2_Init 2 */
  326. }
  327. /**
  328. * @brief USART6 Initialization Function
  329. * @param None
  330. * @retval None
  331. */
  332. static void MX_USART6_UART_Init(void)
  333. {
  334. /* USER CODE BEGIN USART6_Init 0 */
  335. /* USER CODE END USART6_Init 0 */
  336. /* USER CODE BEGIN USART6_Init 1 */
  337. /* USER CODE END USART6_Init 1 */
  338. huart6.Instance = USART6;
  339. huart6.Init.BaudRate = 115200;
  340. huart6.Init.WordLength = UART_WORDLENGTH_8B;
  341. huart6.Init.StopBits = UART_STOPBITS_1;
  342. huart6.Init.Parity = UART_PARITY_NONE;
  343. huart6.Init.Mode = UART_MODE_TX_RX;
  344. huart6.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  345. huart6.Init.OverSampling = UART_OVERSAMPLING_16;
  346. if (HAL_UART_Init(&huart6) != HAL_OK)
  347. {
  348. Error_Handler();
  349. }
  350. /* USER CODE BEGIN USART6_Init 2 */
  351. /* USER CODE END USART6_Init 2 */
  352. }
  353. /**
  354. * @brief GPIO Initialization Function
  355. * @param None
  356. * @retval None
  357. */
  358. static void MX_GPIO_Init(void)
  359. {
  360. GPIO_InitTypeDef GPIO_InitStruct = {0};
  361. /* GPIO Ports Clock Enable */
  362. __HAL_RCC_GPIOC_CLK_ENABLE();
  363. __HAL_RCC_GPIOH_CLK_ENABLE();
  364. __HAL_RCC_GPIOA_CLK_ENABLE();
  365. __HAL_RCC_GPIOB_CLK_ENABLE();
  366. /*Configure GPIO pin : B1_Pin */
  367. GPIO_InitStruct.Pin = B1_Pin;
  368. GPIO_InitStruct.Mode = GPIO_MODE_IT_FALLING;
  369. GPIO_InitStruct.Pull = GPIO_NOPULL;
  370. HAL_GPIO_Init(B1_GPIO_Port, &GPIO_InitStruct);
  371. }
  372. /* USER CODE BEGIN 4 */
  373. /* USER CODE END 4 */
  374. /**
  375. * @brief This function is executed in case of error occurrence.
  376. * @retval None
  377. */
  378. void Error_Handler(void)
  379. {
  380. /* USER CODE BEGIN Error_Handler_Debug */
  381. /* User can add his own implementation to report the HAL error return state */
  382. /* USER CODE END Error_Handler_Debug */
  383. }
  384. #ifdef USE_FULL_ASSERT
  385. /**
  386. * @brief Reports the name of the source file and the source line number
  387. * where the assert_param error has occurred.
  388. * @param file: pointer to the source file name
  389. * @param line: assert_param error line source number
  390. * @retval None
  391. */
  392. void assert_failed(uint8_t *file, uint32_t line)
  393. {
  394. /* USER CODE BEGIN 6 */
  395. /* User can add his own implementation to report the file name and line number,
  396. tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  397. /* USER CODE END 6 */
  398. }
  399. #endif /* USE_FULL_ASSERT */
  400. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/