furi-hal-rtc.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. #include <furi-hal-rtc.h>
  2. #include <stm32wbxx_ll_rcc.h>
  3. #include <stm32wbxx_ll_rtc.h>
  4. #include <furi.h>
  5. #define TAG "FuriHalRtc"
  6. typedef struct {
  7. uint8_t log_level:4;
  8. uint8_t log_reserved:4;
  9. uint8_t flags;
  10. uint16_t reserved;
  11. } DeveloperReg;
  12. _Static_assert(sizeof(DeveloperReg) == 4, "DeveloperReg size mismatch");
  13. void furi_hal_rtc_init() {
  14. if(LL_RCC_GetRTCClockSource() != LL_RCC_RTC_CLKSOURCE_LSE) {
  15. LL_RCC_ForceBackupDomainReset();
  16. LL_RCC_ReleaseBackupDomainReset();
  17. LL_RCC_SetRTCClockSource(LL_RCC_RTC_CLKSOURCE_LSE);
  18. }
  19. LL_RCC_EnableRTC();
  20. LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_RTCAPB);
  21. LL_RTC_InitTypeDef RTC_InitStruct = {0};
  22. RTC_InitStruct.HourFormat = LL_RTC_HOURFORMAT_24HOUR;
  23. RTC_InitStruct.AsynchPrescaler = 127;
  24. RTC_InitStruct.SynchPrescaler = 255;
  25. LL_RTC_Init(RTC, &RTC_InitStruct);
  26. furi_log_set_level(furi_hal_rtc_get_log_level());
  27. FURI_LOG_I(TAG, "Init OK");
  28. }
  29. uint32_t furi_hal_rtc_get_register(FuriHalRtcRegister reg) {
  30. return LL_RTC_BAK_GetRegister(RTC, reg);
  31. }
  32. void furi_hal_rtc_set_register(FuriHalRtcRegister reg, uint32_t value) {
  33. LL_RTC_BAK_SetRegister(RTC, reg, value);
  34. }
  35. void furi_hal_rtc_set_log_level(uint8_t level) {
  36. uint32_t data_reg = furi_hal_rtc_get_register(FuriHalRtcRegisterSystem);
  37. DeveloperReg* data = (DeveloperReg*)&data_reg;
  38. data->log_level = level;
  39. furi_hal_rtc_set_register(FuriHalRtcRegisterSystem, data_reg);
  40. furi_log_set_level(level);
  41. }
  42. uint8_t furi_hal_rtc_get_log_level() {
  43. uint32_t data_reg = furi_hal_rtc_get_register(FuriHalRtcRegisterSystem);
  44. DeveloperReg* data = (DeveloperReg*)&data_reg;
  45. return data->log_level;
  46. }
  47. void furi_hal_rtc_set_flag(FuriHalRtcFlag flag) {
  48. uint32_t data_reg = furi_hal_rtc_get_register(FuriHalRtcRegisterSystem);
  49. DeveloperReg* data = (DeveloperReg*)&data_reg;
  50. data->flags |= flag;
  51. furi_hal_rtc_set_register(FuriHalRtcRegisterSystem, data_reg);
  52. }
  53. void furi_hal_rtc_reset_flag(FuriHalRtcFlag flag) {
  54. uint32_t data_reg = furi_hal_rtc_get_register(FuriHalRtcRegisterSystem);
  55. DeveloperReg* data = (DeveloperReg*)&data_reg;
  56. data->flags &= ~flag;
  57. furi_hal_rtc_set_register(FuriHalRtcRegisterSystem, data_reg);
  58. }
  59. bool furi_hal_rtc_is_flag_set(FuriHalRtcFlag flag) {
  60. uint32_t data_reg = furi_hal_rtc_get_register(FuriHalRtcRegisterSystem);
  61. DeveloperReg* data = (DeveloperReg*)&data_reg;
  62. return data->flags & flag;
  63. }
  64. void furi_hal_rtc_set_datetime(FuriHalRtcDateTime* datetime) {
  65. furi_assert(datetime);
  66. /* Disable write protection */
  67. LL_RTC_DisableWriteProtection(RTC);
  68. /* Enter Initialization mode and wait for INIT flag to be set */
  69. LL_RTC_EnableInitMode(RTC);
  70. while(!LL_RTC_IsActiveFlag_INIT(RTC)) {}
  71. /* Set time */
  72. LL_RTC_TIME_Config(RTC,
  73. LL_RTC_TIME_FORMAT_AM_OR_24,
  74. __LL_RTC_CONVERT_BIN2BCD(datetime->hour),
  75. __LL_RTC_CONVERT_BIN2BCD(datetime->minute),
  76. __LL_RTC_CONVERT_BIN2BCD(datetime->second)
  77. );
  78. /* Set date */
  79. LL_RTC_DATE_Config(RTC,
  80. datetime->weekday,
  81. __LL_RTC_CONVERT_BIN2BCD(datetime->day),
  82. __LL_RTC_CONVERT_BIN2BCD(datetime->month),
  83. __LL_RTC_CONVERT_BIN2BCD(datetime->year - 2000)
  84. );
  85. /* Exit Initialization mode */
  86. LL_RTC_DisableInitMode(RTC);
  87. /* If RTC_CR_BYPSHAD bit = 0, wait for synchro else this check is not needed */
  88. if (!LL_RTC_IsShadowRegBypassEnabled(RTC)) {
  89. LL_RTC_ClearFlag_RS(RTC);
  90. while(!LL_RTC_IsActiveFlag_RS(RTC)) {};
  91. }
  92. /* Enable write protection */
  93. LL_RTC_EnableWriteProtection(RTC);
  94. }
  95. void furi_hal_rtc_get_datetime(FuriHalRtcDateTime* datetime) {
  96. furi_assert(datetime);
  97. uint32_t time = LL_RTC_TIME_Get(RTC); // 0x00HHMMSS
  98. uint32_t date = LL_RTC_DATE_Get(RTC); // 0xWWDDMMYY
  99. datetime->second = __LL_RTC_CONVERT_BCD2BIN((time>>0) & 0xFF);
  100. datetime->minute = __LL_RTC_CONVERT_BCD2BIN((time>>8) & 0xFF);
  101. datetime->hour = __LL_RTC_CONVERT_BCD2BIN((time>>16) & 0xFF);
  102. datetime->year = __LL_RTC_CONVERT_BCD2BIN((date >> 0) & 0xFF) + 2000;
  103. datetime->month = __LL_RTC_CONVERT_BCD2BIN((date >> 8) & 0xFF);
  104. datetime->day = __LL_RTC_CONVERT_BCD2BIN((date >> 16) & 0xFF);
  105. datetime->weekday = __LL_RTC_CONVERT_BCD2BIN((date >> 24) & 0xFF);
  106. }
  107. bool furi_hal_rtc_validate_datetime(FuriHalRtcDateTime* datetime) {
  108. bool invalid = false;
  109. invalid |= (datetime->second > 59);
  110. invalid |= (datetime->minute > 59);
  111. invalid |= (datetime->hour > 23);
  112. invalid |= (datetime->year < 2000);
  113. invalid |= (datetime->year > 2099);
  114. invalid |= (datetime->month == 0);
  115. invalid |= (datetime->month > 12);
  116. invalid |= (datetime->day == 0);
  117. invalid |= (datetime->day > 31);
  118. invalid |= (datetime->weekday == 0);
  119. invalid |= (datetime->weekday > 7);
  120. return !invalid;
  121. }