furi_hal_rtc.c 11 KB

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