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:
		
							parent
							
								
									1f9fd4c42a
								
							
						
					
					
						commit
						3fd5f15e7f
					
				@ -1,8 +1,11 @@
 | 
				
			|||||||
 | 
					#include "furi_hal_rtc.h"
 | 
				
			||||||
 | 
					#include <stdint.h>
 | 
				
			||||||
#include <stdio.h>
 | 
					#include <stdio.h>
 | 
				
			||||||
#include <furi.h>
 | 
					#include <furi.h>
 | 
				
			||||||
#include <furi_hal.h>
 | 
					#include <furi_hal.h>
 | 
				
			||||||
#include <lp5562_reg.h>
 | 
					#include <lp5562_reg.h>
 | 
				
			||||||
#include "../minunit.h"
 | 
					#include "../minunit.h"
 | 
				
			||||||
 | 
					#include <stdlib.h>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#define DATA_SIZE 4
 | 
					#define DATA_SIZE 4
 | 
				
			||||||
#define EEPROM_ADDRESS 0b10101000
 | 
					#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_TEST_SUITE(furi_hal_i2c_int_suite) {
 | 
				
			||||||
    MU_SUITE_CONFIGURE(&furi_hal_i2c_int_setup, &furi_hal_i2c_int_teardown);
 | 
					    MU_SUITE_CONFIGURE(&furi_hal_i2c_int_setup, &furi_hal_i2c_int_teardown);
 | 
				
			||||||
    MU_RUN_TEST(furi_hal_i2c_int_1b);
 | 
					    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_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() {
 | 
					int run_minunit_test_furi_hal() {
 | 
				
			||||||
    MU_RUN_SUITE(furi_hal_i2c_int_suite);
 | 
					    MU_RUN_SUITE(furi_hal_i2c_int_suite);
 | 
				
			||||||
    MU_RUN_SUITE(furi_hal_i2c_ext_suite);
 | 
					    MU_RUN_SUITE(furi_hal_i2c_ext_suite);
 | 
				
			||||||
 | 
					    MU_RUN_SUITE(furi_hal_rtc_datetime_suite);
 | 
				
			||||||
    return MU_EXIT_CODE;
 | 
					    return MU_EXIT_CODE;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
@ -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_pin_fails,void,uint32_t
 | 
				
			||||||
Function,+,furi_hal_rtc_set_register,void,"FuriHalRtcRegister, uint32_t"
 | 
					Function,+,furi_hal_rtc_set_register,void,"FuriHalRtcRegister, uint32_t"
 | 
				
			||||||
Function,+,furi_hal_rtc_sync_shadow,void,
 | 
					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_rtc_validate_datetime,_Bool,FuriHalRtcDateTime*
 | 
				
			||||||
Function,+,furi_hal_sd_get_card_state,FuriStatus,
 | 
					Function,+,furi_hal_sd_get_card_state,FuriStatus,
 | 
				
			||||||
Function,+,furi_hal_sd_info,FuriStatus,FuriHalSdInfo*
 | 
					Function,+,furi_hal_sd_info,FuriStatus,FuriHalSdInfo*
 | 
				
			||||||
 | 
				
			|||||||
		
		
			
  | 
@ -1427,6 +1427,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_pin_fails,void,uint32_t
 | 
				
			||||||
Function,+,furi_hal_rtc_set_register,void,"FuriHalRtcRegister, uint32_t"
 | 
					Function,+,furi_hal_rtc_set_register,void,"FuriHalRtcRegister, uint32_t"
 | 
				
			||||||
Function,+,furi_hal_rtc_sync_shadow,void,
 | 
					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_rtc_validate_datetime,_Bool,FuriHalRtcDateTime*
 | 
				
			||||||
Function,+,furi_hal_sd_get_card_state,FuriStatus,
 | 
					Function,+,furi_hal_sd_get_card_state,FuriStatus,
 | 
				
			||||||
Function,+,furi_hal_sd_info,FuriStatus,FuriHalSdInfo*
 | 
					Function,+,furi_hal_sd_info,FuriStatus,FuriHalSdInfo*
 | 
				
			||||||
 | 
				
			|||||||
		
		
			
  | 
@ -424,6 +424,32 @@ uint32_t furi_hal_rtc_datetime_to_timestamp(FuriHalRtcDateTime* datetime) {
 | 
				
			|||||||
    return timestamp;
 | 
					    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) {
 | 
					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];
 | 
					    return furi_hal_rtc_days_per_year[furi_hal_rtc_is_leap_year(year) ? 1 : 0];
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
@ -253,12 +253,23 @@ uint32_t furi_hal_rtc_get_timestamp();
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
/** Convert DateTime to UNIX timestamp
 | 
					/** Convert DateTime to UNIX timestamp
 | 
				
			||||||
 * 
 | 
					 * 
 | 
				
			||||||
 * @param      datetime  The datetime
 | 
					 * @warning    Mind timezone when perform conversion
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * @param      datetime  The datetime (UTC)
 | 
				
			||||||
 *
 | 
					 *
 | 
				
			||||||
 * @return     UNIX Timestamp in seconds from UNIX epoch start
 | 
					 * @return     UNIX Timestamp in seconds from UNIX epoch start
 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
uint32_t furi_hal_rtc_datetime_to_timestamp(FuriHalRtcDateTime* datetime);
 | 
					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.
 | 
					/** Gets the number of days in the year according to the Gregorian calendar.
 | 
				
			||||||
 *
 | 
					 *
 | 
				
			||||||
 * @param year Input year.
 | 
					 * @param year Input year.
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user