furi_hal_rtc.c 9.5 KB

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