rtc.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /* USER CODE BEGIN Header */
  2. /**
  3. ******************************************************************************
  4. * @file rtc.c
  5. * @brief This file provides code for the configuration
  6. * of the RTC instances.
  7. ******************************************************************************
  8. * @attention
  9. *
  10. * Copyright (c) 2024 STMicroelectronics.
  11. * All rights reserved.
  12. *
  13. * This software is licensed under terms that can be found in the LICENSE file
  14. * in the root directory of this software component.
  15. * If no LICENSE file comes with this software, it is provided AS-IS.
  16. *
  17. ******************************************************************************
  18. */
  19. /* USER CODE END Header */
  20. /* Includes ------------------------------------------------------------------*/
  21. #include "rtc.h"
  22. /* USER CODE BEGIN 0 */
  23. RTC_TimeTypeDef sTime = {0};
  24. RTC_DateTypeDef DateToUpdate = {0};
  25. /* USER CODE END 0 */
  26. RTC_HandleTypeDef hrtc;
  27. /* RTC init function */
  28. void MX_RTC_Init(void)
  29. {
  30. /* USER CODE BEGIN RTC_Init 0 */
  31. /* USER CODE END RTC_Init 0 */
  32. RTC_TimeTypeDef sTime = {0};
  33. RTC_DateTypeDef DateToUpdate = {0};
  34. /* USER CODE BEGIN RTC_Init 1 */
  35. /* USER CODE END RTC_Init 1 */
  36. /** Initialize RTC Only
  37. */
  38. hrtc.Instance = RTC;
  39. hrtc.Init.AsynchPrediv = RTC_AUTO_1_SECOND;
  40. hrtc.Init.OutPut = RTC_OUTPUTSOURCE_NONE;
  41. if (HAL_RTC_Init(&hrtc) != HAL_OK)
  42. {
  43. Error_Handler();
  44. }
  45. /* USER CODE BEGIN Check_RTC_BKUP */
  46. /* USER CODE END Check_RTC_BKUP */
  47. /** Initialize RTC and set the Time and Date
  48. */
  49. sTime.Hours = 0x13;
  50. sTime.Minutes = 0x0;
  51. sTime.Seconds = 0x0;
  52. if (HAL_RTC_SetTime(&hrtc, &sTime, RTC_FORMAT_BCD) != HAL_OK)
  53. {
  54. Error_Handler();
  55. }
  56. DateToUpdate.WeekDay = RTC_WEEKDAY_THURSDAY;
  57. DateToUpdate.Month = RTC_MONTH_JANUARY;
  58. DateToUpdate.Date = 0x18;
  59. DateToUpdate.Year = 0x23;
  60. if (HAL_RTC_SetDate(&hrtc, &DateToUpdate, RTC_FORMAT_BCD) != HAL_OK)
  61. {
  62. Error_Handler();
  63. }
  64. /* USER CODE BEGIN RTC_Init 2 */
  65. /* USER CODE END RTC_Init 2 */
  66. }
  67. void HAL_RTC_MspInit(RTC_HandleTypeDef* rtcHandle)
  68. {
  69. if(rtcHandle->Instance==RTC)
  70. {
  71. /* USER CODE BEGIN RTC_MspInit 0 */
  72. /* USER CODE END RTC_MspInit 0 */
  73. HAL_PWR_EnableBkUpAccess();
  74. /* Enable BKP CLK enable for backup registers */
  75. __HAL_RCC_BKP_CLK_ENABLE();
  76. /* RTC clock enable */
  77. __HAL_RCC_RTC_ENABLE();
  78. /* RTC interrupt Init */
  79. HAL_NVIC_SetPriority(RTC_IRQn, 5, 0);
  80. HAL_NVIC_EnableIRQ(RTC_IRQn);
  81. /* USER CODE BEGIN RTC_MspInit 1 */
  82. /* USER CODE END RTC_MspInit 1 */
  83. }
  84. }
  85. void HAL_RTC_MspDeInit(RTC_HandleTypeDef* rtcHandle)
  86. {
  87. if(rtcHandle->Instance==RTC)
  88. {
  89. /* USER CODE BEGIN RTC_MspDeInit 0 */
  90. /* USER CODE END RTC_MspDeInit 0 */
  91. /* Peripheral clock disable */
  92. __HAL_RCC_RTC_DISABLE();
  93. /* RTC interrupt Deinit */
  94. HAL_NVIC_DisableIRQ(RTC_IRQn);
  95. /* USER CODE BEGIN RTC_MspDeInit 1 */
  96. /* USER CODE END RTC_MspDeInit 1 */
  97. }
  98. }
  99. /* USER CODE BEGIN 1 */
  100. void SyncTime(uint8_t year, uint8_t month, uint8_t day, uint8_t hour, uint8_t minutes, uint8_t seconds)
  101. {
  102. sTime.Hours = hour;
  103. sTime.Minutes = minutes;
  104. sTime.Seconds = seconds;
  105. if (HAL_RTC_SetTime(&hrtc, &sTime, RTC_FORMAT_BCD) != HAL_OK)
  106. {
  107. Error_Handler();
  108. }
  109. DateToUpdate.WeekDay = RTC_WEEKDAY_THURSDAY;
  110. DateToUpdate.Month = month;
  111. DateToUpdate.Date = day;
  112. DateToUpdate.Year = year;
  113. if (HAL_RTC_SetDate(&hrtc, &DateToUpdate, RTC_FORMAT_BCD) != HAL_OK)
  114. {
  115. Error_Handler();
  116. }
  117. }
  118. /* USER CODE END 1 */