rtc.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /**
  2. ******************************************************************************
  3. * @file rtc.c
  4. * @brief This file provides code for the configuration
  5. * of the RTC instances.
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * <h2><center>&copy; Copyright (c) 2023 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. /* Includes ------------------------------------------------------------------*/
  20. #include "rtc.h"
  21. /* USER CODE BEGIN 0 */
  22. /* USER CODE END 0 */
  23. RTC_HandleTypeDef hrtc;
  24. /* RTC init function */
  25. void MX_RTC_Init(void)
  26. {
  27. RTC_TimeTypeDef sTime = {0};
  28. RTC_DateTypeDef DateToUpdate = {0};
  29. /** Initialize RTC Only
  30. */
  31. hrtc.Instance = RTC;
  32. hrtc.Init.AsynchPrediv = RTC_AUTO_1_SECOND;
  33. hrtc.Init.OutPut = RTC_OUTPUTSOURCE_NONE;
  34. if (HAL_RTC_Init(&hrtc) != HAL_OK)
  35. {
  36. Error_Handler();
  37. }
  38. /* USER CODE BEGIN Check_RTC_BKUP */
  39. /* USER CODE END Check_RTC_BKUP */
  40. /** Initialize RTC and set the Time and Date
  41. */
  42. sTime.Hours = 0;
  43. sTime.Minutes = 0;
  44. sTime.Seconds = 0;
  45. if (HAL_RTC_SetTime(&hrtc, &sTime, RTC_FORMAT_BIN) != HAL_OK)
  46. {
  47. Error_Handler();
  48. }
  49. DateToUpdate.WeekDay = RTC_WEEKDAY_MONDAY;
  50. DateToUpdate.Month = RTC_MONTH_JANUARY;
  51. DateToUpdate.Date = 1;
  52. DateToUpdate.Year = 0;
  53. if (HAL_RTC_SetDate(&hrtc, &DateToUpdate, RTC_FORMAT_BIN) != HAL_OK)
  54. {
  55. Error_Handler();
  56. }
  57. }
  58. void HAL_RTC_MspInit(RTC_HandleTypeDef* rtcHandle)
  59. {
  60. if(rtcHandle->Instance==RTC)
  61. {
  62. /* USER CODE BEGIN RTC_MspInit 0 */
  63. /* USER CODE END RTC_MspInit 0 */
  64. HAL_PWR_EnableBkUpAccess();
  65. /* Enable BKP CLK enable for backup registers */
  66. __HAL_RCC_BKP_CLK_ENABLE();
  67. /* RTC clock enable */
  68. __HAL_RCC_RTC_ENABLE();
  69. /* USER CODE BEGIN RTC_MspInit 1 */
  70. /* USER CODE END RTC_MspInit 1 */
  71. }
  72. }
  73. void HAL_RTC_MspDeInit(RTC_HandleTypeDef* rtcHandle)
  74. {
  75. if(rtcHandle->Instance==RTC)
  76. {
  77. /* USER CODE BEGIN RTC_MspDeInit 0 */
  78. /* USER CODE END RTC_MspDeInit 0 */
  79. /* Peripheral clock disable */
  80. __HAL_RCC_RTC_DISABLE();
  81. /* USER CODE BEGIN RTC_MspDeInit 1 */
  82. /* USER CODE END RTC_MspDeInit 1 */
  83. }
  84. }
  85. /* USER CODE BEGIN 1 */
  86. /* USER CODE END 1 */
  87. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/