furi_hal_rtc.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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 = {0};
  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. uint32_t furi_hal_rtc_get_register(FuriHalRtcRegister reg) {
  137. return LL_RTC_BAK_GetRegister(RTC, reg);
  138. }
  139. void furi_hal_rtc_set_register(FuriHalRtcRegister reg, uint32_t value) {
  140. LL_RTC_BAK_SetRegister(RTC, reg, value);
  141. }
  142. void furi_hal_rtc_set_log_level(uint8_t level) {
  143. uint32_t data_reg = furi_hal_rtc_get_register(FuriHalRtcRegisterSystem);
  144. SystemReg* data = (SystemReg*)&data_reg;
  145. data->log_level = level;
  146. furi_hal_rtc_set_register(FuriHalRtcRegisterSystem, data_reg);
  147. furi_log_set_level(level);
  148. }
  149. uint8_t furi_hal_rtc_get_log_level() {
  150. uint32_t data_reg = furi_hal_rtc_get_register(FuriHalRtcRegisterSystem);
  151. SystemReg* data = (SystemReg*)&data_reg;
  152. return data->log_level;
  153. }
  154. void furi_hal_rtc_set_flag(FuriHalRtcFlag flag) {
  155. uint32_t data_reg = furi_hal_rtc_get_register(FuriHalRtcRegisterSystem);
  156. SystemReg* data = (SystemReg*)&data_reg;
  157. data->flags |= flag;
  158. furi_hal_rtc_set_register(FuriHalRtcRegisterSystem, data_reg);
  159. if(flag & FuriHalRtcFlagDebug) {
  160. furi_hal_debug_enable();
  161. }
  162. }
  163. void furi_hal_rtc_reset_flag(FuriHalRtcFlag flag) {
  164. uint32_t data_reg = furi_hal_rtc_get_register(FuriHalRtcRegisterSystem);
  165. SystemReg* data = (SystemReg*)&data_reg;
  166. data->flags &= ~flag;
  167. furi_hal_rtc_set_register(FuriHalRtcRegisterSystem, data_reg);
  168. if(flag & FuriHalRtcFlagDebug) {
  169. furi_hal_debug_disable();
  170. }
  171. }
  172. bool furi_hal_rtc_is_flag_set(FuriHalRtcFlag flag) {
  173. uint32_t data_reg = furi_hal_rtc_get_register(FuriHalRtcRegisterSystem);
  174. SystemReg* data = (SystemReg*)&data_reg;
  175. return data->flags & flag;
  176. }
  177. void furi_hal_rtc_set_boot_mode(FuriHalRtcBootMode mode) {
  178. uint32_t data_reg = furi_hal_rtc_get_register(FuriHalRtcRegisterSystem);
  179. SystemReg* data = (SystemReg*)&data_reg;
  180. data->boot_mode = mode;
  181. furi_hal_rtc_set_register(FuriHalRtcRegisterSystem, data_reg);
  182. }
  183. FuriHalRtcBootMode furi_hal_rtc_get_boot_mode() {
  184. uint32_t data_reg = furi_hal_rtc_get_register(FuriHalRtcRegisterSystem);
  185. SystemReg* data = (SystemReg*)&data_reg;
  186. return data->boot_mode;
  187. }
  188. void furi_hal_rtc_set_heap_track_mode(FuriHalRtcHeapTrackMode mode) {
  189. uint32_t data_reg = furi_hal_rtc_get_register(FuriHalRtcRegisterSystem);
  190. SystemReg* data = (SystemReg*)&data_reg;
  191. data->heap_track_mode = mode;
  192. furi_hal_rtc_set_register(FuriHalRtcRegisterSystem, data_reg);
  193. }
  194. FuriHalRtcHeapTrackMode furi_hal_rtc_get_heap_track_mode() {
  195. uint32_t data_reg = furi_hal_rtc_get_register(FuriHalRtcRegisterSystem);
  196. SystemReg* data = (SystemReg*)&data_reg;
  197. return data->heap_track_mode;
  198. }
  199. void furi_hal_rtc_set_locale_units(FuriHalRtcLocaleUnits value) {
  200. uint32_t data_reg = furi_hal_rtc_get_register(FuriHalRtcRegisterSystem);
  201. SystemReg* data = (SystemReg*)&data_reg;
  202. data->locale_units = value;
  203. furi_hal_rtc_set_register(FuriHalRtcRegisterSystem, data_reg);
  204. }
  205. FuriHalRtcLocaleUnits furi_hal_rtc_get_locale_units() {
  206. uint32_t data_reg = furi_hal_rtc_get_register(FuriHalRtcRegisterSystem);
  207. SystemReg* data = (SystemReg*)&data_reg;
  208. return data->locale_units;
  209. }
  210. void furi_hal_rtc_set_locale_timeformat(FuriHalRtcLocaleTimeFormat value) {
  211. uint32_t data_reg = furi_hal_rtc_get_register(FuriHalRtcRegisterSystem);
  212. SystemReg* data = (SystemReg*)&data_reg;
  213. data->locale_timeformat = value;
  214. furi_hal_rtc_set_register(FuriHalRtcRegisterSystem, data_reg);
  215. }
  216. FuriHalRtcLocaleTimeFormat furi_hal_rtc_get_locale_timeformat() {
  217. uint32_t data_reg = furi_hal_rtc_get_register(FuriHalRtcRegisterSystem);
  218. SystemReg* data = (SystemReg*)&data_reg;
  219. return data->locale_timeformat;
  220. }
  221. void furi_hal_rtc_set_locale_dateformat(FuriHalRtcLocaleDateFormat value) {
  222. uint32_t data_reg = furi_hal_rtc_get_register(FuriHalRtcRegisterSystem);
  223. SystemReg* data = (SystemReg*)&data_reg;
  224. data->locale_dateformat = value;
  225. furi_hal_rtc_set_register(FuriHalRtcRegisterSystem, data_reg);
  226. }
  227. FuriHalRtcLocaleDateFormat furi_hal_rtc_get_locale_dateformat() {
  228. uint32_t data_reg = furi_hal_rtc_get_register(FuriHalRtcRegisterSystem);
  229. SystemReg* data = (SystemReg*)&data_reg;
  230. return data->locale_dateformat;
  231. }
  232. void furi_hal_rtc_set_datetime(FuriHalRtcDateTime* datetime) {
  233. furi_assert(datetime);
  234. /* Disable write protection */
  235. LL_RTC_DisableWriteProtection(RTC);
  236. /* Enter Initialization mode and wait for INIT flag to be set */
  237. LL_RTC_EnableInitMode(RTC);
  238. while(!LL_RTC_IsActiveFlag_INIT(RTC)) {
  239. }
  240. /* Set time */
  241. LL_RTC_TIME_Config(
  242. RTC,
  243. LL_RTC_TIME_FORMAT_AM_OR_24,
  244. __LL_RTC_CONVERT_BIN2BCD(datetime->hour),
  245. __LL_RTC_CONVERT_BIN2BCD(datetime->minute),
  246. __LL_RTC_CONVERT_BIN2BCD(datetime->second));
  247. /* Set date */
  248. LL_RTC_DATE_Config(
  249. RTC,
  250. datetime->weekday,
  251. __LL_RTC_CONVERT_BIN2BCD(datetime->day),
  252. __LL_RTC_CONVERT_BIN2BCD(datetime->month),
  253. __LL_RTC_CONVERT_BIN2BCD(datetime->year - 2000));
  254. /* Exit Initialization mode */
  255. LL_RTC_DisableInitMode(RTC);
  256. /* If RTC_CR_BYPSHAD bit = 0, wait for synchro else this check is not needed */
  257. if(!LL_RTC_IsShadowRegBypassEnabled(RTC)) {
  258. LL_RTC_ClearFlag_RS(RTC);
  259. while(!LL_RTC_IsActiveFlag_RS(RTC)) {
  260. };
  261. }
  262. /* Enable write protection */
  263. LL_RTC_EnableWriteProtection(RTC);
  264. }
  265. void furi_hal_rtc_get_datetime(FuriHalRtcDateTime* datetime) {
  266. furi_assert(datetime);
  267. uint32_t time = LL_RTC_TIME_Get(RTC); // 0x00HHMMSS
  268. uint32_t date = LL_RTC_DATE_Get(RTC); // 0xWWDDMMYY
  269. datetime->second = __LL_RTC_CONVERT_BCD2BIN((time >> 0) & 0xFF);
  270. datetime->minute = __LL_RTC_CONVERT_BCD2BIN((time >> 8) & 0xFF);
  271. datetime->hour = __LL_RTC_CONVERT_BCD2BIN((time >> 16) & 0xFF);
  272. datetime->year = __LL_RTC_CONVERT_BCD2BIN((date >> 0) & 0xFF) + 2000;
  273. datetime->month = __LL_RTC_CONVERT_BCD2BIN((date >> 8) & 0xFF);
  274. datetime->day = __LL_RTC_CONVERT_BCD2BIN((date >> 16) & 0xFF);
  275. datetime->weekday = __LL_RTC_CONVERT_BCD2BIN((date >> 24) & 0xFF);
  276. }
  277. bool furi_hal_rtc_validate_datetime(FuriHalRtcDateTime* datetime) {
  278. bool invalid = false;
  279. invalid |= (datetime->second > 59);
  280. invalid |= (datetime->minute > 59);
  281. invalid |= (datetime->hour > 23);
  282. invalid |= (datetime->year < 2000);
  283. invalid |= (datetime->year > 2099);
  284. invalid |= (datetime->month == 0);
  285. invalid |= (datetime->month > 12);
  286. invalid |= (datetime->day == 0);
  287. invalid |= (datetime->day > 31);
  288. invalid |= (datetime->weekday == 0);
  289. invalid |= (datetime->weekday > 7);
  290. return !invalid;
  291. }
  292. void furi_hal_rtc_set_fault_data(uint32_t value) {
  293. furi_hal_rtc_set_register(FuriHalRtcRegisterFaultData, value);
  294. }
  295. uint32_t furi_hal_rtc_get_fault_data() {
  296. return furi_hal_rtc_get_register(FuriHalRtcRegisterFaultData);
  297. }
  298. void furi_hal_rtc_set_pin_fails(uint32_t value) {
  299. furi_hal_rtc_set_register(FuriHalRtcRegisterPinFails, value);
  300. }
  301. uint32_t furi_hal_rtc_get_pin_fails() {
  302. return furi_hal_rtc_get_register(FuriHalRtcRegisterPinFails);
  303. }
  304. uint32_t furi_hal_rtc_get_timestamp() {
  305. FuriHalRtcDateTime datetime = {0};
  306. furi_hal_rtc_get_datetime(&datetime);
  307. return furi_hal_rtc_datetime_to_timestamp(&datetime);
  308. }
  309. uint32_t furi_hal_rtc_datetime_to_timestamp(FuriHalRtcDateTime* datetime) {
  310. uint32_t timestamp = 0;
  311. uint8_t years = 0;
  312. uint8_t leap_years = 0;
  313. for(uint16_t y = FURI_HAL_RTC_EPOCH_START_YEAR; y < datetime->year; y++) {
  314. if(FURI_HAL_RTC_IS_LEAP_YEAR(y)) {
  315. leap_years++;
  316. } else {
  317. years++;
  318. }
  319. }
  320. timestamp +=
  321. ((years * furi_hal_rtc_days_per_year[0]) + (leap_years * furi_hal_rtc_days_per_year[1])) *
  322. FURI_HAL_RTC_SECONDS_PER_DAY;
  323. uint8_t year_index = (FURI_HAL_RTC_IS_LEAP_YEAR(datetime->year)) ? 1 : 0;
  324. for(uint8_t m = 0; m < (datetime->month - 1); m++) {
  325. timestamp += furi_hal_rtc_days_per_month[year_index][m] * FURI_HAL_RTC_SECONDS_PER_DAY;
  326. }
  327. timestamp += (datetime->day - 1) * FURI_HAL_RTC_SECONDS_PER_DAY;
  328. timestamp += datetime->hour * FURI_HAL_RTC_SECONDS_PER_HOUR;
  329. timestamp += datetime->minute * FURI_HAL_RTC_SECONDS_PER_MINUTE;
  330. timestamp += datetime->second;
  331. return timestamp;
  332. }