main.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. /**
  2. ******************************************************************************
  3. * @file : main.c
  4. * @brief : Main program body
  5. ******************************************************************************
  6. ** This notice applies to any and all portions of this file
  7. * that are not between comment pairs USER CODE BEGIN and
  8. * USER CODE END. Other portions of this file, whether
  9. * inserted by the user or by software development tools
  10. * are owned by their respective copyright owners.
  11. *
  12. * COPYRIGHT(c) 2019 STMicroelectronics
  13. *
  14. * Redistribution and use in source and binary forms, with or without modification,
  15. * are permitted provided that the following conditions are met:
  16. * 1. Redistributions of source code must retain the above copyright notice,
  17. * this list of conditions and the following disclaimer.
  18. * 2. Redistributions in binary form must reproduce the above copyright notice,
  19. * this list of conditions and the following disclaimer in the documentation
  20. * and/or other materials provided with the distribution.
  21. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  22. * may be used to endorse or promote products derived from this software
  23. * without specific prior written permission.
  24. *
  25. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  26. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  27. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  28. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  29. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  30. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  31. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  32. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  33. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  34. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35. *
  36. ******************************************************************************
  37. */
  38. /* Includes ------------------------------------------------------------------*/
  39. #include "main.h"
  40. #include "stm32l4xx_hal.h"
  41. /* USER CODE BEGIN Includes */
  42. /* USER CODE END Includes */
  43. /* Private variables ---------------------------------------------------------*/
  44. IWDG_HandleTypeDef hiwdg;
  45. RTC_HandleTypeDef hrtc;
  46. UART_HandleTypeDef huart2;
  47. /* USER CODE BEGIN PV */
  48. /* Private variables ---------------------------------------------------------*/
  49. /* USER CODE END PV */
  50. /* Private function prototypes -----------------------------------------------*/
  51. void SystemClock_Config(void);
  52. static void MX_GPIO_Init(void);
  53. static void MX_USART2_UART_Init(void);
  54. static void MX_IWDG_Init(void);
  55. static void MX_RTC_Init(void);
  56. /* USER CODE BEGIN PFP */
  57. /* Private function prototypes -----------------------------------------------*/
  58. /* USER CODE END PFP */
  59. /* USER CODE BEGIN 0 */
  60. /* USER CODE END 0 */
  61. /**
  62. * @brief The application entry point.
  63. *
  64. * @retval None
  65. */
  66. int main(void)
  67. {
  68. /* USER CODE BEGIN 1 */
  69. /* USER CODE END 1 */
  70. /* MCU Configuration----------------------------------------------------------*/
  71. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  72. HAL_Init();
  73. /* USER CODE BEGIN Init */
  74. /* USER CODE END Init */
  75. /* Configure the system clock */
  76. SystemClock_Config();
  77. /* USER CODE BEGIN SysInit */
  78. /* USER CODE END SysInit */
  79. /* Initialize all configured peripherals */
  80. MX_GPIO_Init();
  81. MX_USART2_UART_Init();
  82. MX_IWDG_Init();
  83. MX_RTC_Init();
  84. /* USER CODE BEGIN 2 */
  85. /* USER CODE END 2 */
  86. /* Infinite loop */
  87. /* USER CODE BEGIN WHILE */
  88. while (1)
  89. {
  90. /* USER CODE END WHILE */
  91. /* USER CODE BEGIN 3 */
  92. }
  93. /* USER CODE END 3 */
  94. }
  95. /**
  96. * @brief System Clock Configuration
  97. * @retval None
  98. */
  99. void SystemClock_Config(void)
  100. {
  101. RCC_OscInitTypeDef RCC_OscInitStruct;
  102. RCC_ClkInitTypeDef RCC_ClkInitStruct;
  103. RCC_PeriphCLKInitTypeDef PeriphClkInit;
  104. /**Configure LSE Drive Capability
  105. */
  106. HAL_PWR_EnableBkUpAccess();
  107. __HAL_RCC_LSEDRIVE_CONFIG(RCC_LSEDRIVE_LOW);
  108. /**Initializes the CPU, AHB and APB busses clocks
  109. */
  110. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI|RCC_OSCILLATORTYPE_LSE
  111. |RCC_OSCILLATORTYPE_MSI;
  112. RCC_OscInitStruct.LSEState = RCC_LSE_ON;
  113. RCC_OscInitStruct.LSIState = RCC_LSI_ON;
  114. RCC_OscInitStruct.MSIState = RCC_MSI_ON;
  115. RCC_OscInitStruct.MSICalibrationValue = 0;
  116. RCC_OscInitStruct.MSIClockRange = RCC_MSIRANGE_6;
  117. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  118. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_MSI;
  119. RCC_OscInitStruct.PLL.PLLM = 1;
  120. RCC_OscInitStruct.PLL.PLLN = 16;
  121. RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV7;
  122. RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2;
  123. RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2;
  124. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  125. {
  126. _Error_Handler(__FILE__, __LINE__);
  127. }
  128. /**Initializes the CPU, AHB and APB busses clocks
  129. */
  130. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  131. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  132. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  133. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  134. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  135. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  136. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
  137. {
  138. _Error_Handler(__FILE__, __LINE__);
  139. }
  140. PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_RTC|RCC_PERIPHCLK_USART2;
  141. PeriphClkInit.Usart2ClockSelection = RCC_USART2CLKSOURCE_PCLK1;
  142. PeriphClkInit.RTCClockSelection = RCC_RTCCLKSOURCE_LSE;
  143. if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
  144. {
  145. _Error_Handler(__FILE__, __LINE__);
  146. }
  147. /**Configure the main internal regulator output voltage
  148. */
  149. if (HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1) != HAL_OK)
  150. {
  151. _Error_Handler(__FILE__, __LINE__);
  152. }
  153. /**Configure the Systick interrupt time
  154. */
  155. HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
  156. /**Configure the Systick
  157. */
  158. HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
  159. /**Enable MSI Auto calibration
  160. */
  161. HAL_RCCEx_EnableMSIPLLMode();
  162. /* SysTick_IRQn interrupt configuration */
  163. HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
  164. }
  165. /* IWDG init function */
  166. static void MX_IWDG_Init(void)
  167. {
  168. hiwdg.Instance = IWDG;
  169. hiwdg.Init.Prescaler = IWDG_PRESCALER_4;
  170. hiwdg.Init.Window = 4095;
  171. hiwdg.Init.Reload = 4095;
  172. if (HAL_IWDG_Init(&hiwdg) != HAL_OK)
  173. {
  174. _Error_Handler(__FILE__, __LINE__);
  175. }
  176. }
  177. /* RTC init function */
  178. static void MX_RTC_Init(void)
  179. {
  180. /* USER CODE BEGIN RTC_Init 0 */
  181. /* USER CODE END RTC_Init 0 */
  182. RTC_TimeTypeDef sTime;
  183. RTC_DateTypeDef sDate;
  184. /* USER CODE BEGIN RTC_Init 1 */
  185. /* USER CODE END RTC_Init 1 */
  186. /**Initialize RTC Only
  187. */
  188. hrtc.Instance = RTC;
  189. hrtc.Init.HourFormat = RTC_HOURFORMAT_24;
  190. hrtc.Init.AsynchPrediv = 127;
  191. hrtc.Init.SynchPrediv = 255;
  192. hrtc.Init.OutPut = RTC_OUTPUT_DISABLE;
  193. hrtc.Init.OutPutRemap = RTC_OUTPUT_REMAP_NONE;
  194. hrtc.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
  195. hrtc.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;
  196. if (HAL_RTC_Init(&hrtc) != HAL_OK)
  197. {
  198. _Error_Handler(__FILE__, __LINE__);
  199. }
  200. /**Initialize RTC and set the Time and Date
  201. */
  202. sTime.Hours = 0x0;
  203. sTime.Minutes = 0x0;
  204. sTime.Seconds = 0x0;
  205. sTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
  206. sTime.StoreOperation = RTC_STOREOPERATION_RESET;
  207. if (HAL_RTC_SetTime(&hrtc, &sTime, RTC_FORMAT_BCD) != HAL_OK)
  208. {
  209. _Error_Handler(__FILE__, __LINE__);
  210. }
  211. sDate.WeekDay = RTC_WEEKDAY_MONDAY;
  212. sDate.Month = RTC_MONTH_JANUARY;
  213. sDate.Date = 0x1;
  214. sDate.Year = 0x0;
  215. if (HAL_RTC_SetDate(&hrtc, &sDate, RTC_FORMAT_BCD) != HAL_OK)
  216. {
  217. _Error_Handler(__FILE__, __LINE__);
  218. }
  219. }
  220. /* USART2 init function */
  221. static void MX_USART2_UART_Init(void)
  222. {
  223. huart2.Instance = USART2;
  224. huart2.Init.BaudRate = 115200;
  225. huart2.Init.WordLength = UART_WORDLENGTH_8B;
  226. huart2.Init.StopBits = UART_STOPBITS_1;
  227. huart2.Init.Parity = UART_PARITY_NONE;
  228. huart2.Init.Mode = UART_MODE_TX_RX;
  229. huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  230. huart2.Init.OverSampling = UART_OVERSAMPLING_16;
  231. huart2.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
  232. huart2.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
  233. if (HAL_UART_Init(&huart2) != HAL_OK)
  234. {
  235. _Error_Handler(__FILE__, __LINE__);
  236. }
  237. }
  238. /** Configure pins as
  239. * Analog
  240. * Input
  241. * Output
  242. * EVENT_OUT
  243. * EXTI
  244. */
  245. static void MX_GPIO_Init(void)
  246. {
  247. GPIO_InitTypeDef GPIO_InitStruct;
  248. /* GPIO Ports Clock Enable */
  249. __HAL_RCC_GPIOC_CLK_ENABLE();
  250. __HAL_RCC_GPIOA_CLK_ENABLE();
  251. __HAL_RCC_GPIOB_CLK_ENABLE();
  252. /*Configure GPIO pin Output Level */
  253. HAL_GPIO_WritePin(LD3_GPIO_Port, LD3_Pin, GPIO_PIN_RESET);
  254. /*Configure GPIO pin : LD3_Pin */
  255. GPIO_InitStruct.Pin = LD3_Pin;
  256. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  257. GPIO_InitStruct.Pull = GPIO_NOPULL;
  258. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  259. HAL_GPIO_Init(LD3_GPIO_Port, &GPIO_InitStruct);
  260. }
  261. /* USER CODE BEGIN 4 */
  262. /* USER CODE END 4 */
  263. /**
  264. * @brief This function is executed in case of error occurrence.
  265. * @param file: The file name as string.
  266. * @param line: The line in file as a number.
  267. * @retval None
  268. */
  269. void _Error_Handler(char *file, int line)
  270. {
  271. /* USER CODE BEGIN Error_Handler_Debug */
  272. /* User can add his own implementation to report the HAL error return state */
  273. while(1)
  274. {
  275. }
  276. /* USER CODE END Error_Handler_Debug */
  277. }
  278. #ifdef USE_FULL_ASSERT
  279. /**
  280. * @brief Reports the name of the source file and the source line number
  281. * where the assert_param error has occurred.
  282. * @param file: pointer to the source file name
  283. * @param line: assert_param error line source number
  284. * @retval None
  285. */
  286. void assert_failed(uint8_t* file, uint32_t line)
  287. {
  288. /* USER CODE BEGIN 6 */
  289. /* User can add his own implementation to report the file name and line number,
  290. tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  291. /* USER CODE END 6 */
  292. }
  293. #endif /* USE_FULL_ASSERT */
  294. /**
  295. * @}
  296. */
  297. /**
  298. * @}
  299. */
  300. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/