furi_hal_rtc.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. #include <furi_hal_rtc.h>
  2. #include <furi_hal_light.h>
  3. #include <furi_hal_debug.h>
  4. #include <stm32wbxx_ll_bus.h>
  5. #include <stm32wbxx_ll_pwr.h>
  6. #include <stm32wbxx_ll_rcc.h>
  7. #include <stm32wbxx_ll_rtc.h>
  8. #include <stm32wbxx_ll_utils.h>
  9. #include <furi.h>
  10. #define TAG "FuriHalRtc"
  11. #define RTC_CLOCK_IS_READY() (LL_RCC_LSE_IsReady() && LL_RCC_LSI1_IsReady())
  12. #define FURI_HAL_RTC_HEADER_MAGIC 0x10F1
  13. #define FURI_HAL_RTC_HEADER_VERSION 0
  14. typedef struct {
  15. uint16_t magic;
  16. uint8_t version;
  17. uint8_t unused;
  18. } FuriHalRtcHeader;
  19. typedef struct {
  20. uint8_t log_level : 4;
  21. uint8_t log_reserved : 4;
  22. uint8_t flags;
  23. uint8_t boot_mode : 4;
  24. uint16_t reserved : 12;
  25. } DeveloperReg;
  26. _Static_assert(sizeof(DeveloperReg) == 4, "DeveloperReg size mismatch");
  27. void furi_hal_rtc_init_early() {
  28. // LSE and RTC
  29. LL_PWR_EnableBkUpAccess();
  30. if(!RTC_CLOCK_IS_READY()) {
  31. LL_RCC_LSI1_Enable();
  32. // Try to start LSE normal way
  33. LL_RCC_LSE_SetDriveCapability(LL_RCC_LSEDRIVE_HIGH);
  34. LL_RCC_LSE_Enable();
  35. uint32_t c = 0;
  36. while(!RTC_CLOCK_IS_READY() && c < 200) {
  37. LL_mDelay(10);
  38. c++;
  39. }
  40. // Plan B: reset backup domain
  41. if(!RTC_CLOCK_IS_READY()) {
  42. furi_hal_light_sequence("rgb R.r.R.r.R");
  43. LL_RCC_ForceBackupDomainReset();
  44. LL_RCC_ReleaseBackupDomainReset();
  45. NVIC_SystemReset();
  46. }
  47. // Set RTC domain clock to LSE
  48. LL_RCC_SetRTCClockSource(LL_RCC_RTC_CLKSOURCE_LSE);
  49. }
  50. // Enable clocking
  51. LL_RCC_EnableRTC();
  52. LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_RTCAPB);
  53. // Verify header register
  54. uint32_t data_reg = furi_hal_rtc_get_register(FuriHalRtcRegisterHeader);
  55. FuriHalRtcHeader* data = (FuriHalRtcHeader*)&data_reg;
  56. if(data->magic != FURI_HAL_RTC_HEADER_MAGIC || data->version != FURI_HAL_RTC_HEADER_VERSION) {
  57. // Reset all our registers to ensure consistency
  58. for(size_t i = 0; i < FuriHalRtcRegisterMAX; i++) {
  59. furi_hal_rtc_set_register(i, 0);
  60. }
  61. data->magic = FURI_HAL_RTC_HEADER_MAGIC;
  62. data->version = FURI_HAL_RTC_HEADER_VERSION;
  63. furi_hal_rtc_set_register(FuriHalRtcRegisterHeader, data_reg);
  64. }
  65. if(furi_hal_rtc_is_flag_set(FuriHalRtcFlagDebug)) {
  66. furi_hal_debug_enable();
  67. } else {
  68. furi_hal_debug_disable();
  69. }
  70. }
  71. void furi_hal_rtc_deinit_early() {
  72. }
  73. void furi_hal_rtc_init() {
  74. if(LL_RCC_GetRTCClockSource() != LL_RCC_RTC_CLKSOURCE_LSE) {
  75. LL_RCC_ForceBackupDomainReset();
  76. LL_RCC_ReleaseBackupDomainReset();
  77. LL_RCC_SetRTCClockSource(LL_RCC_RTC_CLKSOURCE_LSE);
  78. }
  79. LL_RCC_EnableRTC();
  80. LL_RTC_InitTypeDef RTC_InitStruct = {0};
  81. RTC_InitStruct.HourFormat = LL_RTC_HOURFORMAT_24HOUR;
  82. RTC_InitStruct.AsynchPrescaler = 127;
  83. RTC_InitStruct.SynchPrescaler = 255;
  84. LL_RTC_Init(RTC, &RTC_InitStruct);
  85. furi_log_set_level(furi_hal_rtc_get_log_level());
  86. FURI_LOG_I(TAG, "Init OK");
  87. }
  88. uint32_t furi_hal_rtc_get_register(FuriHalRtcRegister reg) {
  89. return LL_RTC_BAK_GetRegister(RTC, reg);
  90. }
  91. void furi_hal_rtc_set_register(FuriHalRtcRegister reg, uint32_t value) {
  92. LL_RTC_BAK_SetRegister(RTC, reg, value);
  93. }
  94. void furi_hal_rtc_set_log_level(uint8_t level) {
  95. uint32_t data_reg = furi_hal_rtc_get_register(FuriHalRtcRegisterSystem);
  96. DeveloperReg* data = (DeveloperReg*)&data_reg;
  97. data->log_level = level;
  98. furi_hal_rtc_set_register(FuriHalRtcRegisterSystem, data_reg);
  99. furi_log_set_level(level);
  100. }
  101. uint8_t furi_hal_rtc_get_log_level() {
  102. uint32_t data_reg = furi_hal_rtc_get_register(FuriHalRtcRegisterSystem);
  103. DeveloperReg* data = (DeveloperReg*)&data_reg;
  104. return data->log_level;
  105. }
  106. void furi_hal_rtc_set_flag(FuriHalRtcFlag flag) {
  107. uint32_t data_reg = furi_hal_rtc_get_register(FuriHalRtcRegisterSystem);
  108. DeveloperReg* data = (DeveloperReg*)&data_reg;
  109. data->flags |= flag;
  110. furi_hal_rtc_set_register(FuriHalRtcRegisterSystem, data_reg);
  111. if(flag & FuriHalRtcFlagDebug) {
  112. furi_hal_debug_enable();
  113. }
  114. }
  115. void furi_hal_rtc_reset_flag(FuriHalRtcFlag flag) {
  116. uint32_t data_reg = furi_hal_rtc_get_register(FuriHalRtcRegisterSystem);
  117. DeveloperReg* data = (DeveloperReg*)&data_reg;
  118. data->flags &= ~flag;
  119. furi_hal_rtc_set_register(FuriHalRtcRegisterSystem, data_reg);
  120. if(flag & FuriHalRtcFlagDebug) {
  121. furi_hal_debug_disable();
  122. }
  123. }
  124. bool furi_hal_rtc_is_flag_set(FuriHalRtcFlag flag) {
  125. uint32_t data_reg = furi_hal_rtc_get_register(FuriHalRtcRegisterSystem);
  126. DeveloperReg* data = (DeveloperReg*)&data_reg;
  127. return data->flags & flag;
  128. }
  129. void furi_hal_rtc_set_boot_mode(FuriHalRtcBootMode mode) {
  130. uint32_t data_reg = furi_hal_rtc_get_register(FuriHalRtcRegisterSystem);
  131. DeveloperReg* data = (DeveloperReg*)&data_reg;
  132. data->boot_mode = mode;
  133. furi_hal_rtc_set_register(FuriHalRtcRegisterSystem, data_reg);
  134. }
  135. FuriHalRtcBootMode furi_hal_rtc_get_boot_mode() {
  136. uint32_t data_reg = furi_hal_rtc_get_register(FuriHalRtcRegisterSystem);
  137. DeveloperReg* data = (DeveloperReg*)&data_reg;
  138. return (FuriHalRtcBootMode)data->boot_mode;
  139. }
  140. void furi_hal_rtc_set_datetime(FuriHalRtcDateTime* datetime) {
  141. furi_assert(datetime);
  142. /* Disable write protection */
  143. LL_RTC_DisableWriteProtection(RTC);
  144. /* Enter Initialization mode and wait for INIT flag to be set */
  145. LL_RTC_EnableInitMode(RTC);
  146. while(!LL_RTC_IsActiveFlag_INIT(RTC)) {
  147. }
  148. /* Set time */
  149. LL_RTC_TIME_Config(
  150. RTC,
  151. LL_RTC_TIME_FORMAT_AM_OR_24,
  152. __LL_RTC_CONVERT_BIN2BCD(datetime->hour),
  153. __LL_RTC_CONVERT_BIN2BCD(datetime->minute),
  154. __LL_RTC_CONVERT_BIN2BCD(datetime->second));
  155. /* Set date */
  156. LL_RTC_DATE_Config(
  157. RTC,
  158. datetime->weekday,
  159. __LL_RTC_CONVERT_BIN2BCD(datetime->day),
  160. __LL_RTC_CONVERT_BIN2BCD(datetime->month),
  161. __LL_RTC_CONVERT_BIN2BCD(datetime->year - 2000));
  162. /* Exit Initialization mode */
  163. LL_RTC_DisableInitMode(RTC);
  164. /* If RTC_CR_BYPSHAD bit = 0, wait for synchro else this check is not needed */
  165. if(!LL_RTC_IsShadowRegBypassEnabled(RTC)) {
  166. LL_RTC_ClearFlag_RS(RTC);
  167. while(!LL_RTC_IsActiveFlag_RS(RTC)) {
  168. };
  169. }
  170. /* Enable write protection */
  171. LL_RTC_EnableWriteProtection(RTC);
  172. }
  173. void furi_hal_rtc_get_datetime(FuriHalRtcDateTime* datetime) {
  174. furi_assert(datetime);
  175. uint32_t time = LL_RTC_TIME_Get(RTC); // 0x00HHMMSS
  176. uint32_t date = LL_RTC_DATE_Get(RTC); // 0xWWDDMMYY
  177. datetime->second = __LL_RTC_CONVERT_BCD2BIN((time >> 0) & 0xFF);
  178. datetime->minute = __LL_RTC_CONVERT_BCD2BIN((time >> 8) & 0xFF);
  179. datetime->hour = __LL_RTC_CONVERT_BCD2BIN((time >> 16) & 0xFF);
  180. datetime->year = __LL_RTC_CONVERT_BCD2BIN((date >> 0) & 0xFF) + 2000;
  181. datetime->month = __LL_RTC_CONVERT_BCD2BIN((date >> 8) & 0xFF);
  182. datetime->day = __LL_RTC_CONVERT_BCD2BIN((date >> 16) & 0xFF);
  183. datetime->weekday = __LL_RTC_CONVERT_BCD2BIN((date >> 24) & 0xFF);
  184. }
  185. bool furi_hal_rtc_validate_datetime(FuriHalRtcDateTime* datetime) {
  186. bool invalid = false;
  187. invalid |= (datetime->second > 59);
  188. invalid |= (datetime->minute > 59);
  189. invalid |= (datetime->hour > 23);
  190. invalid |= (datetime->year < 2000);
  191. invalid |= (datetime->year > 2099);
  192. invalid |= (datetime->month == 0);
  193. invalid |= (datetime->month > 12);
  194. invalid |= (datetime->day == 0);
  195. invalid |= (datetime->day > 31);
  196. invalid |= (datetime->weekday == 0);
  197. invalid |= (datetime->weekday > 7);
  198. return !invalid;
  199. }
  200. void furi_hal_rtc_set_fault_data(uint32_t value) {
  201. furi_hal_rtc_set_register(FuriHalRtcRegisterFaultData, value);
  202. }
  203. uint32_t furi_hal_rtc_get_fault_data() {
  204. return furi_hal_rtc_get_register(FuriHalRtcRegisterFaultData);
  205. }
  206. void furi_hal_rtc_set_pin_fails(uint32_t value) {
  207. furi_hal_rtc_set_register(FuriHalRtcRegisterPinFails, value);
  208. }
  209. uint32_t furi_hal_rtc_get_pin_fails() {
  210. return furi_hal_rtc_get_register(FuriHalRtcRegisterPinFails);
  211. }