furi_hal_rtc.c 13 KB

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