You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

main.c 8.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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) 2020 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. RTC_HandleTypeDef hrtc;
  36. UART_HandleTypeDef huart2;
  37. RTC_TimeTypeDef sTime;
  38. RTC_DateTypeDef sDate;
  39. /* USER CODE BEGIN PV */
  40. /* USER CODE END PV */
  41. /* Private function prototypes -----------------------------------------------*/
  42. void SystemClock_Config(void);
  43. static void MX_GPIO_Init(void);
  44. static void MX_USART2_UART_Init(void);
  45. static void MX_RTC_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_USART2_UART_Init();
  71. MX_RTC_Init();
  72. /* USER CODE BEGIN 2 */
  73. uint8_t hours = 0;
  74. uint8_t minutes = 0;
  75. uint8_t seconds = 0;
  76. uint8_t weekDay = 0;
  77. uint8_t month = 0;
  78. uint8_t date = 0;
  79. uint8_t year = 0;
  80. /* USER CODE END 2 */
  81. /* Infinite loop */
  82. /* USER CODE BEGIN WHILE */
  83. while (1)
  84. {
  85. /* USER CODE END WHILE */
  86. if (HAL_RTC_GetTime(&hrtc, &sTime, RTC_FORMAT_BIN) == HAL_OK)
  87. {
  88. hours = sTime.Hours;
  89. minutes = sTime.Minutes;
  90. seconds = sTime.Seconds;
  91. }
  92. if (HAL_RTC_GetDate(&hrtc, &sDate, RTC_FORMAT_BIN) == HAL_OK)
  93. {
  94. weekDay = sDate.WeekDay;
  95. month = sDate.Month;
  96. date = sDate.Date;
  97. year = sDate.Year;
  98. }
  99. HAL_Delay(200);
  100. }
  101. /* USER CODE END 3 */
  102. }
  103. /**
  104. * @brief System Clock Configuration
  105. * @retval None
  106. */
  107. void SystemClock_Config(void)
  108. {
  109. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  110. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  111. RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
  112. /** Configure the main internal regulator output voltage
  113. */
  114. __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
  115. /** Initializes the RCC Oscillators according to the specified parameters
  116. * in the RCC_OscInitTypeDef structure.
  117. */
  118. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI|RCC_OSCILLATORTYPE_LSE;
  119. RCC_OscInitStruct.LSEState = RCC_LSE_ON;
  120. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  121. RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  122. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  123. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
  124. RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL6;
  125. RCC_OscInitStruct.PLL.PLLDIV = RCC_PLL_DIV3;
  126. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  127. {
  128. Error_Handler();
  129. }
  130. /** Initializes the CPU, AHB and APB buses clocks
  131. */
  132. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  133. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  134. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  135. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  136. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  137. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  138. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
  139. {
  140. Error_Handler();
  141. }
  142. PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_RTC;
  143. PeriphClkInit.RTCClockSelection = RCC_RTCCLKSOURCE_LSE;
  144. if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
  145. {
  146. Error_Handler();
  147. }
  148. }
  149. /**
  150. * @brief RTC Initialization Function
  151. * @param None
  152. * @retval None
  153. */
  154. static void MX_RTC_Init(void)
  155. {
  156. /* USER CODE BEGIN RTC_Init 0 */
  157. /* USER CODE END RTC_Init 0 */
  158. /* USER CODE BEGIN RTC_Init 1 */
  159. /* USER CODE END RTC_Init 1 */
  160. /** Initialize RTC Only
  161. */
  162. hrtc.Instance = RTC;
  163. hrtc.Init.HourFormat = RTC_HOURFORMAT_24;
  164. hrtc.Init.AsynchPrediv = 127;
  165. hrtc.Init.SynchPrediv = 255;
  166. hrtc.Init.OutPut = RTC_OUTPUT_DISABLE;
  167. hrtc.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
  168. hrtc.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;
  169. if (HAL_RTC_Init(&hrtc) != HAL_OK)
  170. {
  171. Error_Handler();
  172. }
  173. /* USER CODE BEGIN Check_RTC_BKUP */
  174. /* USER CODE END Check_RTC_BKUP */
  175. /** Initialize RTC and set the Time and Date
  176. */
  177. sTime.Hours = 23;
  178. sTime.Minutes = 59;
  179. sTime.Seconds = 45;
  180. sTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
  181. sTime.StoreOperation = RTC_STOREOPERATION_RESET;
  182. if (HAL_RTC_SetTime(&hrtc, &sTime, RTC_FORMAT_BIN) != HAL_OK)
  183. {
  184. Error_Handler();
  185. }
  186. sDate.WeekDay = RTC_WEEKDAY_SUNDAY;
  187. sDate.Month = RTC_MONTH_DECEMBER;
  188. sDate.Date = 31;
  189. sDate.Year = 17;
  190. if (HAL_RTC_SetDate(&hrtc, &sDate, RTC_FORMAT_BIN) != HAL_OK)
  191. {
  192. Error_Handler();
  193. }
  194. /* USER CODE BEGIN RTC_Init 2 */
  195. /* USER CODE END RTC_Init 2 */
  196. }
  197. /**
  198. * @brief USART2 Initialization Function
  199. * @param None
  200. * @retval None
  201. */
  202. static void MX_USART2_UART_Init(void)
  203. {
  204. /* USER CODE BEGIN USART2_Init 0 */
  205. /* USER CODE END USART2_Init 0 */
  206. /* USER CODE BEGIN USART2_Init 1 */
  207. /* USER CODE END USART2_Init 1 */
  208. huart2.Instance = USART2;
  209. huart2.Init.BaudRate = 115200;
  210. huart2.Init.WordLength = UART_WORDLENGTH_8B;
  211. huart2.Init.StopBits = UART_STOPBITS_1;
  212. huart2.Init.Parity = UART_PARITY_NONE;
  213. huart2.Init.Mode = UART_MODE_TX_RX;
  214. huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  215. huart2.Init.OverSampling = UART_OVERSAMPLING_16;
  216. if (HAL_UART_Init(&huart2) != HAL_OK)
  217. {
  218. Error_Handler();
  219. }
  220. /* USER CODE BEGIN USART2_Init 2 */
  221. /* USER CODE END USART2_Init 2 */
  222. }
  223. /**
  224. * @brief GPIO Initialization Function
  225. * @param None
  226. * @retval None
  227. */
  228. static void MX_GPIO_Init(void)
  229. {
  230. GPIO_InitTypeDef GPIO_InitStruct = {0};
  231. /* GPIO Ports Clock Enable */
  232. __HAL_RCC_GPIOC_CLK_ENABLE();
  233. __HAL_RCC_GPIOH_CLK_ENABLE();
  234. __HAL_RCC_GPIOA_CLK_ENABLE();
  235. __HAL_RCC_GPIOB_CLK_ENABLE();
  236. /*Configure GPIO pin Output Level */
  237. HAL_GPIO_WritePin(LD2_GPIO_Port, LD2_Pin, GPIO_PIN_RESET);
  238. /*Configure GPIO pin : B1_Pin */
  239. GPIO_InitStruct.Pin = B1_Pin;
  240. GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
  241. GPIO_InitStruct.Pull = GPIO_NOPULL;
  242. HAL_GPIO_Init(B1_GPIO_Port, &GPIO_InitStruct);
  243. /*Configure GPIO pin : LD2_Pin */
  244. GPIO_InitStruct.Pin = LD2_Pin;
  245. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  246. GPIO_InitStruct.Pull = GPIO_NOPULL;
  247. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  248. HAL_GPIO_Init(LD2_GPIO_Port, &GPIO_InitStruct);
  249. }
  250. /* USER CODE BEGIN 4 */
  251. /* USER CODE END 4 */
  252. /**
  253. * @brief This function is executed in case of error occurrence.
  254. * @retval None
  255. */
  256. void Error_Handler(void)
  257. {
  258. /* USER CODE BEGIN Error_Handler_Debug */
  259. /* User can add his own implementation to report the HAL error return state */
  260. /* USER CODE END Error_Handler_Debug */
  261. }
  262. #ifdef USE_FULL_ASSERT
  263. /**
  264. * @brief Reports the name of the source file and the source line number
  265. * where the assert_param error has occurred.
  266. * @param file: pointer to the source file name
  267. * @param line: assert_param error line source number
  268. * @retval None
  269. */
  270. void assert_failed(uint8_t *file, uint32_t line)
  271. {
  272. /* USER CODE BEGIN 6 */
  273. /* User can add his own implementation to report the file name and line number,
  274. tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  275. /* USER CODE END 6 */
  276. }
  277. #endif /* USE_FULL_ASSERT */
  278. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/