furi_hal_rtc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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 FURI_HAL_RTC_LSE_STARTUP_TIME 300
  12. #define FURI_HAL_RTC_CLOCK_IS_READY() (LL_RCC_LSE_IsReady() && LL_RCC_LSI1_IsReady())
  13. #define FURI_HAL_RTC_HEADER_MAGIC 0x10F1
  14. #define FURI_HAL_RTC_HEADER_VERSION 0
  15. typedef struct {
  16. uint16_t magic;
  17. uint8_t version;
  18. uint8_t unused;
  19. } FuriHalRtcHeader;
  20. typedef struct {
  21. uint8_t log_level : 4;
  22. uint8_t log_reserved : 4;
  23. uint8_t flags;
  24. uint8_t boot_mode : 4;
  25. uint16_t reserved : 12;
  26. } DeveloperReg;
  27. _Static_assert(sizeof(DeveloperReg) == 4, "DeveloperReg size mismatch");
  28. #define FURI_HAL_RTC_SECONDS_PER_MINUTE 60
  29. #define FURI_HAL_RTC_SECONDS_PER_HOUR (FURI_HAL_RTC_SECONDS_PER_MINUTE * 60)
  30. #define FURI_HAL_RTC_SECONDS_PER_DAY (FURI_HAL_RTC_SECONDS_PER_HOUR * 24)
  31. #define FURI_HAL_RTC_MONTHS_COUNT 12
  32. #define FURI_HAL_RTC_EPOCH_START_YEAR 1970
  33. #define FURI_HAL_RTC_IS_LEAP_YEAR(year) \
  34. ((((year) % 4 == 0) && ((year) % 100 != 0)) || ((year) % 400 == 0))
  35. static const uint8_t furi_hal_rtc_days_per_month[][FURI_HAL_RTC_MONTHS_COUNT] = {
  36. {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
  37. {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}};
  38. static const uint16_t furi_hal_rtc_days_per_year[] = {365, 366};
  39. static void furi_hal_rtc_reset() {
  40. LL_RCC_ForceBackupDomainReset();
  41. LL_RCC_ReleaseBackupDomainReset();
  42. }
  43. static bool furi_hal_rtc_start_clock_and_switch() {
  44. // Clock operation require access to Backup Domain
  45. LL_PWR_EnableBkUpAccess();
  46. // Enable LSI and LSE
  47. LL_RCC_LSI1_Enable();
  48. LL_RCC_LSE_SetDriveCapability(LL_RCC_LSEDRIVE_HIGH);
  49. LL_RCC_LSE_Enable();
  50. // Wait for LSI and LSE startup
  51. uint32_t c = 0;
  52. while(!FURI_HAL_RTC_CLOCK_IS_READY() && c < FURI_HAL_RTC_LSE_STARTUP_TIME) {
  53. LL_mDelay(1);
  54. c++;
  55. }
  56. if(FURI_HAL_RTC_CLOCK_IS_READY()) {
  57. LL_RCC_SetRTCClockSource(LL_RCC_RTC_CLKSOURCE_LSE);
  58. LL_RCC_EnableRTC();
  59. return LL_RCC_GetRTCClockSource() == LL_RCC_RTC_CLKSOURCE_LSE;
  60. } else {
  61. return false;
  62. }
  63. }
  64. static void furi_hal_rtc_recover() {
  65. FuriHalRtcDateTime datetime = {0};
  66. // Handle fixable LSE failure
  67. if(LL_RCC_LSE_IsCSSDetected()) {
  68. furi_hal_light_sequence("rgb B");
  69. // Shutdown LSE and LSECSS
  70. LL_RCC_LSE_DisableCSS();
  71. LL_RCC_LSE_Disable();
  72. } else {
  73. furi_hal_light_sequence("rgb R");
  74. }
  75. // Temporary switch to LSI
  76. LL_RCC_SetRTCClockSource(LL_RCC_RTC_CLKSOURCE_LSI);
  77. if(LL_RCC_GetRTCClockSource() == LL_RCC_RTC_CLKSOURCE_LSI) {
  78. // Get datetime before RTC Domain reset
  79. furi_hal_rtc_get_datetime(&datetime);
  80. }
  81. // Reset RTC Domain
  82. furi_hal_rtc_reset();
  83. // Start Clock
  84. if(!furi_hal_rtc_start_clock_and_switch()) {
  85. // Plan C: reset RTC and restart
  86. furi_hal_light_sequence("rgb R.r.R.r.R.r");
  87. furi_hal_rtc_reset();
  88. NVIC_SystemReset();
  89. }
  90. // Set date if it valid
  91. if(datetime.year != 0) {
  92. furi_hal_rtc_set_datetime(&datetime);
  93. }
  94. }
  95. void furi_hal_rtc_init_early() {
  96. // Enable RTCAPB clock
  97. LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_RTCAPB);
  98. // Prepare clock
  99. if(!furi_hal_rtc_start_clock_and_switch()) {
  100. // Plan B: try to recover
  101. furi_hal_rtc_recover();
  102. }
  103. // Verify header register
  104. uint32_t data_reg = furi_hal_rtc_get_register(FuriHalRtcRegisterHeader);
  105. FuriHalRtcHeader* data = (FuriHalRtcHeader*)&data_reg;
  106. if(data->magic != FURI_HAL_RTC_HEADER_MAGIC || data->version != FURI_HAL_RTC_HEADER_VERSION) {
  107. // Reset all our registers to ensure consistency
  108. for(size_t i = 0; i < FuriHalRtcRegisterMAX; i++) {
  109. furi_hal_rtc_set_register(i, 0);
  110. }
  111. data->magic = FURI_HAL_RTC_HEADER_MAGIC;
  112. data->version = FURI_HAL_RTC_HEADER_VERSION;
  113. furi_hal_rtc_set_register(FuriHalRtcRegisterHeader, data_reg);
  114. }
  115. if(furi_hal_rtc_is_flag_set(FuriHalRtcFlagDebug)) {
  116. furi_hal_debug_enable();
  117. } else {
  118. furi_hal_debug_disable();
  119. }
  120. }
  121. void furi_hal_rtc_deinit_early() {
  122. }
  123. void furi_hal_rtc_init() {
  124. LL_RTC_InitTypeDef RTC_InitStruct = {0};
  125. RTC_InitStruct.HourFormat = LL_RTC_HOURFORMAT_24HOUR;
  126. RTC_InitStruct.AsynchPrescaler = 127;
  127. RTC_InitStruct.SynchPrescaler = 255;
  128. LL_RTC_Init(RTC, &RTC_InitStruct);
  129. furi_log_set_level(furi_hal_rtc_get_log_level());
  130. FURI_LOG_I(TAG, "Init OK");
  131. }
  132. uint32_t furi_hal_rtc_get_register(FuriHalRtcRegister reg) {
  133. return LL_RTC_BAK_GetRegister(RTC, reg);
  134. }
  135. void furi_hal_rtc_set_register(FuriHalRtcRegister reg, uint32_t value) {
  136. LL_RTC_BAK_SetRegister(RTC, reg, value);
  137. }
  138. void furi_hal_rtc_set_log_level(uint8_t level) {
  139. uint32_t data_reg = furi_hal_rtc_get_register(FuriHalRtcRegisterSystem);
  140. DeveloperReg* data = (DeveloperReg*)&data_reg;
  141. data->log_level = level;
  142. furi_hal_rtc_set_register(FuriHalRtcRegisterSystem, data_reg);
  143. furi_log_set_level(level);
  144. }
  145. uint8_t furi_hal_rtc_get_log_level() {
  146. uint32_t data_reg = furi_hal_rtc_get_register(FuriHalRtcRegisterSystem);
  147. DeveloperReg* data = (DeveloperReg*)&data_reg;
  148. return data->log_level;
  149. }
  150. void furi_hal_rtc_set_flag(FuriHalRtcFlag flag) {
  151. uint32_t data_reg = furi_hal_rtc_get_register(FuriHalRtcRegisterSystem);
  152. DeveloperReg* data = (DeveloperReg*)&data_reg;
  153. data->flags |= flag;
  154. furi_hal_rtc_set_register(FuriHalRtcRegisterSystem, data_reg);
  155. if(flag & FuriHalRtcFlagDebug) {
  156. furi_hal_debug_enable();
  157. }
  158. }
  159. void furi_hal_rtc_reset_flag(FuriHalRtcFlag flag) {
  160. uint32_t data_reg = furi_hal_rtc_get_register(FuriHalRtcRegisterSystem);
  161. DeveloperReg* data = (DeveloperReg*)&data_reg;
  162. data->flags &= ~flag;
  163. furi_hal_rtc_set_register(FuriHalRtcRegisterSystem, data_reg);
  164. if(flag & FuriHalRtcFlagDebug) {
  165. furi_hal_debug_disable();
  166. }
  167. }
  168. bool furi_hal_rtc_is_flag_set(FuriHalRtcFlag flag) {
  169. uint32_t data_reg = furi_hal_rtc_get_register(FuriHalRtcRegisterSystem);
  170. DeveloperReg* data = (DeveloperReg*)&data_reg;
  171. return data->flags & flag;
  172. }
  173. void furi_hal_rtc_set_boot_mode(FuriHalRtcBootMode mode) {
  174. uint32_t data_reg = furi_hal_rtc_get_register(FuriHalRtcRegisterSystem);
  175. DeveloperReg* data = (DeveloperReg*)&data_reg;
  176. data->boot_mode = mode;
  177. furi_hal_rtc_set_register(FuriHalRtcRegisterSystem, data_reg);
  178. }
  179. FuriHalRtcBootMode furi_hal_rtc_get_boot_mode() {
  180. uint32_t data_reg = furi_hal_rtc_get_register(FuriHalRtcRegisterSystem);
  181. DeveloperReg* data = (DeveloperReg*)&data_reg;
  182. return (FuriHalRtcBootMode)data->boot_mode;
  183. }
  184. void furi_hal_rtc_set_datetime(FuriHalRtcDateTime* datetime) {
  185. furi_assert(datetime);
  186. /* Disable write protection */
  187. LL_RTC_DisableWriteProtection(RTC);
  188. /* Enter Initialization mode and wait for INIT flag to be set */
  189. LL_RTC_EnableInitMode(RTC);
  190. while(!LL_RTC_IsActiveFlag_INIT(RTC)) {
  191. }
  192. /* Set time */
  193. LL_RTC_TIME_Config(
  194. RTC,
  195. LL_RTC_TIME_FORMAT_AM_OR_24,
  196. __LL_RTC_CONVERT_BIN2BCD(datetime->hour),
  197. __LL_RTC_CONVERT_BIN2BCD(datetime->minute),
  198. __LL_RTC_CONVERT_BIN2BCD(datetime->second));
  199. /* Set date */
  200. LL_RTC_DATE_Config(
  201. RTC,
  202. datetime->weekday,
  203. __LL_RTC_CONVERT_BIN2BCD(datetime->day),
  204. __LL_RTC_CONVERT_BIN2BCD(datetime->month),
  205. __LL_RTC_CONVERT_BIN2BCD(datetime->year - 2000));
  206. /* Exit Initialization mode */
  207. LL_RTC_DisableInitMode(RTC);
  208. /* If RTC_CR_BYPSHAD bit = 0, wait for synchro else this check is not needed */
  209. if(!LL_RTC_IsShadowRegBypassEnabled(RTC)) {
  210. LL_RTC_ClearFlag_RS(RTC);
  211. while(!LL_RTC_IsActiveFlag_RS(RTC)) {
  212. };
  213. }
  214. /* Enable write protection */
  215. LL_RTC_EnableWriteProtection(RTC);
  216. }
  217. void furi_hal_rtc_get_datetime(FuriHalRtcDateTime* datetime) {
  218. furi_assert(datetime);
  219. uint32_t time = LL_RTC_TIME_Get(RTC); // 0x00HHMMSS
  220. uint32_t date = LL_RTC_DATE_Get(RTC); // 0xWWDDMMYY
  221. datetime->second = __LL_RTC_CONVERT_BCD2BIN((time >> 0) & 0xFF);
  222. datetime->minute = __LL_RTC_CONVERT_BCD2BIN((time >> 8) & 0xFF);
  223. datetime->hour = __LL_RTC_CONVERT_BCD2BIN((time >> 16) & 0xFF);
  224. datetime->year = __LL_RTC_CONVERT_BCD2BIN((date >> 0) & 0xFF) + 2000;
  225. datetime->month = __LL_RTC_CONVERT_BCD2BIN((date >> 8) & 0xFF);
  226. datetime->day = __LL_RTC_CONVERT_BCD2BIN((date >> 16) & 0xFF);
  227. datetime->weekday = __LL_RTC_CONVERT_BCD2BIN((date >> 24) & 0xFF);
  228. }
  229. bool furi_hal_rtc_validate_datetime(FuriHalRtcDateTime* datetime) {
  230. bool invalid = false;
  231. invalid |= (datetime->second > 59);
  232. invalid |= (datetime->minute > 59);
  233. invalid |= (datetime->hour > 23);
  234. invalid |= (datetime->year < 2000);
  235. invalid |= (datetime->year > 2099);
  236. invalid |= (datetime->month == 0);
  237. invalid |= (datetime->month > 12);
  238. invalid |= (datetime->day == 0);
  239. invalid |= (datetime->day > 31);
  240. invalid |= (datetime->weekday == 0);
  241. invalid |= (datetime->weekday > 7);
  242. return !invalid;
  243. }
  244. void furi_hal_rtc_set_fault_data(uint32_t value) {
  245. furi_hal_rtc_set_register(FuriHalRtcRegisterFaultData, value);
  246. }
  247. uint32_t furi_hal_rtc_get_fault_data() {
  248. return furi_hal_rtc_get_register(FuriHalRtcRegisterFaultData);
  249. }
  250. void furi_hal_rtc_set_pin_fails(uint32_t value) {
  251. furi_hal_rtc_set_register(FuriHalRtcRegisterPinFails, value);
  252. }
  253. uint32_t furi_hal_rtc_get_pin_fails() {
  254. return furi_hal_rtc_get_register(FuriHalRtcRegisterPinFails);
  255. }
  256. uint32_t furi_hal_rtc_datetime_to_timestamp(FuriHalRtcDateTime* datetime) {
  257. uint32_t timestamp = 0;
  258. uint8_t years = 0;
  259. uint8_t leap_years = 0;
  260. for(uint16_t y = FURI_HAL_RTC_EPOCH_START_YEAR; y < datetime->year; y++) {
  261. if(FURI_HAL_RTC_IS_LEAP_YEAR(y)) {
  262. leap_years++;
  263. } else {
  264. years++;
  265. }
  266. }
  267. timestamp +=
  268. ((years * furi_hal_rtc_days_per_year[0]) + (leap_years * furi_hal_rtc_days_per_year[1])) *
  269. FURI_HAL_RTC_SECONDS_PER_DAY;
  270. uint8_t year_index = (FURI_HAL_RTC_IS_LEAP_YEAR(datetime->year)) ? 1 : 0;
  271. for(uint8_t m = 0; m < (datetime->month - 1); m++) {
  272. timestamp += furi_hal_rtc_days_per_month[year_index][m] * FURI_HAL_RTC_SECONDS_PER_DAY;
  273. }
  274. timestamp += (datetime->day - 1) * FURI_HAL_RTC_SECONDS_PER_DAY;
  275. timestamp += datetime->hour * FURI_HAL_RTC_SECONDS_PER_HOUR;
  276. timestamp += datetime->minute * FURI_HAL_RTC_SECONDS_PER_MINUTE;
  277. timestamp += datetime->second;
  278. return timestamp;
  279. }