Furi_hal_rtc: new function (#3294)

* furi_hal_rtc_timestamp_to_datetime added
* hw targets api version sync
* hw targets api version sync, bump
* FuriHal: update rtc docs
* unit tests added

Co-authored-by: hedger <hedger@users.noreply.github.com>
Co-authored-by: Aleksandr Kutuzov <alleteam@gmail.com>
This commit is contained in:
Leptopt1los 2024-01-14 17:07:42 +09:00 committed by MX
parent a7ab4b9c32
commit 1fd4839bb6
No known key found for this signature in database
GPG Key ID: 7CCC66B7DBDD1C83
5 changed files with 81 additions and 1 deletions

View File

@ -1,8 +1,11 @@
#include "furi_hal_rtc.h"
#include <stdint.h>
#include <stdio.h>
#include <furi.h>
#include <furi_hal.h>
#include <lp5562_reg.h>
#include "../minunit.h"
#include <stdlib.h>
#define DATA_SIZE 4
#define EEPROM_ADDRESS 0b10101000
@ -211,6 +214,37 @@ MU_TEST(furi_hal_i2c_ext_eeprom) {
}
}
MU_TEST(furi_hal_rtc_timestamp2datetime_min) {
uint32_t test_value = 0;
FuriHalRtcDateTime min_datetime_expected = {0, 0, 0, 1, 1, 1970, 0};
FuriHalRtcDateTime result = {0};
furi_hal_rtc_timestamp_to_datetime(test_value, &result);
mu_assert_mem_eq(&min_datetime_expected, &result, sizeof(result));
}
MU_TEST(furi_hal_rtc_timestamp2datetime_max) {
uint32_t test_value = UINT32_MAX;
FuriHalRtcDateTime max_datetime_expected = {6, 28, 15, 7, 2, 2106, 0};
FuriHalRtcDateTime result = {0};
furi_hal_rtc_timestamp_to_datetime(test_value, &result);
mu_assert_mem_eq(&max_datetime_expected, &result, sizeof(result));
}
MU_TEST(furi_hal_rtc_timestamp2datetime2timestamp) {
uint32_t test_value = random();
FuriHalRtcDateTime datetime = {0};
furi_hal_rtc_timestamp_to_datetime(test_value, &datetime);
uint32_t result = furi_hal_rtc_datetime_to_timestamp(&datetime);
mu_assert_int_eq(test_value, result);
}
MU_TEST_SUITE(furi_hal_i2c_int_suite) {
MU_SUITE_CONFIGURE(&furi_hal_i2c_int_setup, &furi_hal_i2c_int_teardown);
MU_RUN_TEST(furi_hal_i2c_int_1b);
@ -224,8 +258,15 @@ MU_TEST_SUITE(furi_hal_i2c_ext_suite) {
MU_RUN_TEST(furi_hal_i2c_ext_eeprom);
}
MU_TEST_SUITE(furi_hal_rtc_datetime_suite) {
MU_RUN_TEST(furi_hal_rtc_timestamp2datetime_min);
MU_RUN_TEST(furi_hal_rtc_timestamp2datetime_max);
MU_RUN_TEST(furi_hal_rtc_timestamp2datetime2timestamp);
}
int run_minunit_test_furi_hal() {
MU_RUN_SUITE(furi_hal_i2c_int_suite);
MU_RUN_SUITE(furi_hal_i2c_ext_suite);
MU_RUN_SUITE(furi_hal_rtc_datetime_suite);
return MU_EXIT_CODE;
}

View File

@ -1261,6 +1261,7 @@ Function,+,furi_hal_rtc_set_log_level,void,uint8_t
Function,+,furi_hal_rtc_set_pin_fails,void,uint32_t
Function,+,furi_hal_rtc_set_register,void,"FuriHalRtcRegister, uint32_t"
Function,+,furi_hal_rtc_sync_shadow,void,
Function,+,furi_hal_rtc_timestamp_to_datetime,void,"uint32_t, FuriHalRtcDateTime*"
Function,+,furi_hal_rtc_validate_datetime,_Bool,FuriHalRtcDateTime*
Function,+,furi_hal_sd_get_card_state,FuriStatus,
Function,+,furi_hal_sd_info,FuriStatus,FuriHalSdInfo*

1 entry status name type params
1261 Function + furi_hal_rtc_set_pin_fails void uint32_t
1262 Function + furi_hal_rtc_set_register void FuriHalRtcRegister, uint32_t
1263 Function + furi_hal_rtc_sync_shadow void
1264 Function + furi_hal_rtc_timestamp_to_datetime void uint32_t, FuriHalRtcDateTime*
1265 Function + furi_hal_rtc_validate_datetime _Bool FuriHalRtcDateTime*
1266 Function + furi_hal_sd_get_card_state FuriStatus
1267 Function + furi_hal_sd_info FuriStatus FuriHalSdInfo*

View File

@ -1456,6 +1456,7 @@ Function,+,furi_hal_rtc_set_log_level,void,uint8_t
Function,+,furi_hal_rtc_set_pin_fails,void,uint32_t
Function,+,furi_hal_rtc_set_register,void,"FuriHalRtcRegister, uint32_t"
Function,+,furi_hal_rtc_sync_shadow,void,
Function,+,furi_hal_rtc_timestamp_to_datetime,void,"uint32_t, FuriHalRtcDateTime*"
Function,+,furi_hal_rtc_validate_datetime,_Bool,FuriHalRtcDateTime*
Function,+,furi_hal_sd_get_card_state,FuriStatus,
Function,+,furi_hal_sd_info,FuriStatus,FuriHalSdInfo*

1 entry status name type params
1456 Function + furi_hal_rtc_set_pin_fails void uint32_t
1457 Function + furi_hal_rtc_set_register void FuriHalRtcRegister, uint32_t
1458 Function + furi_hal_rtc_sync_shadow void
1459 Function + furi_hal_rtc_timestamp_to_datetime void uint32_t, FuriHalRtcDateTime*
1460 Function + furi_hal_rtc_validate_datetime _Bool FuriHalRtcDateTime*
1461 Function + furi_hal_sd_get_card_state FuriStatus
1462 Function + furi_hal_sd_info FuriStatus FuriHalSdInfo*

View File

@ -424,6 +424,32 @@ uint32_t furi_hal_rtc_datetime_to_timestamp(FuriHalRtcDateTime* datetime) {
return timestamp;
}
void furi_hal_rtc_timestamp_to_datetime(uint32_t timestamp, FuriHalRtcDateTime* datetime) {
uint32_t days = timestamp / FURI_HAL_RTC_SECONDS_PER_DAY;
uint32_t seconds_in_day = timestamp % FURI_HAL_RTC_SECONDS_PER_DAY;
datetime->year = FURI_HAL_RTC_EPOCH_START_YEAR;
while(days >= furi_hal_rtc_get_days_per_year(datetime->year)) {
days -= furi_hal_rtc_get_days_per_year(datetime->year);
(datetime->year)++;
}
datetime->month = 1;
while(days >= furi_hal_rtc_get_days_per_month(
furi_hal_rtc_is_leap_year(datetime->year), datetime->month)) {
days -= furi_hal_rtc_get_days_per_month(
furi_hal_rtc_is_leap_year(datetime->year), datetime->month);
(datetime->month)++;
}
datetime->day = days + 1;
datetime->hour = seconds_in_day / FURI_HAL_RTC_SECONDS_PER_HOUR;
datetime->minute =
(seconds_in_day % FURI_HAL_RTC_SECONDS_PER_HOUR) / FURI_HAL_RTC_SECONDS_PER_MINUTE;
datetime->second = seconds_in_day % FURI_HAL_RTC_SECONDS_PER_MINUTE;
}
uint16_t furi_hal_rtc_get_days_per_year(uint16_t year) {
return furi_hal_rtc_days_per_year[furi_hal_rtc_is_leap_year(year) ? 1 : 0];
}

View File

@ -252,13 +252,24 @@ uint32_t furi_hal_rtc_get_pin_fails();
uint32_t furi_hal_rtc_get_timestamp();
/** Convert DateTime to UNIX timestamp
*
* @warning Mind timezone when perform conversion
*
* @param datetime The datetime
* @param datetime The datetime (UTC)
*
* @return UNIX Timestamp in seconds from UNIX epoch start
*/
uint32_t furi_hal_rtc_datetime_to_timestamp(FuriHalRtcDateTime* datetime);
/** Convert UNIX timestamp to DateTime
*
* @warning Mind timezone when perform conversion
*
* @param[in] timestamp UNIX Timestamp in seconds from UNIX epoch start
* @param[out] datetime The datetime (UTC)
*/
void furi_hal_rtc_timestamp_to_datetime(uint32_t timestamp, FuriHalRtcDateTime* datetime);
/** Gets the number of days in the year according to the Gregorian calendar.
*
* @param year Input year.