1 /* 2 * Copyright (c) 2021-2023 HPMicro 3 * 4 * SPDX-License-Identifier: BSD-3-Clause 5 * 6 */ 7 8 #include "hpm_common.h" 9 #include "hpm_rtc_drv.h" 10 11 rtc_config_time(RTC_Type * base,time_t time)12hpm_stat_t rtc_config_time(RTC_Type *base, time_t time) 13 { 14 base->SECOND = (uint32_t)time; 15 return status_success; 16 } 17 rtc_get_time(RTC_Type * base)18time_t rtc_get_time(RTC_Type *base) 19 { 20 time_t time = (time_t)base->SECOND; 21 return time; 22 } 23 rtc_get_timeval(RTC_Type * base)24struct timeval rtc_get_timeval(RTC_Type *base) 25 { 26 struct timeval tm; 27 28 base->SUB_SNAP = 0; /* Lock shadow registers first */ 29 30 /* Convert sub-second ticks into micro-second */ 31 uint32_t sub_sec = (uint32_t)((base->SUB_SNAP >> 17) * 1.0 * 1000000 / 32768); 32 33 tm.tv_sec = base->SEC_SNAP; 34 tm.tv_usec = sub_sec; 35 36 return tm; 37 } 38 rtc_config_alarm(RTC_Type * base,rtc_alarm_config_t * config)39hpm_stat_t rtc_config_alarm(RTC_Type *base, rtc_alarm_config_t *config) 40 { 41 hpm_stat_t status = status_invalid_argument; 42 do { 43 if ((config == NULL) || (config->index > 1U) || (config->type > RTC_ALARM_TYPE_ABSOLUTE_TIME_ONE_SHOT)) { 44 break; 45 } 46 uint32_t alarm_inc = 0; 47 uint32_t alarm; 48 if (config->type == RTC_ALARM_TYPE_ONE_SHOT) { 49 uint32_t current_sec = base->SECOND; 50 alarm = current_sec + config->period; 51 if (alarm < current_sec) { 52 break; 53 } 54 } else if (config->type == RTC_ALARM_TYPE_PERIODIC) { 55 uint32_t current_sec = base->SECOND; 56 alarm_inc = config->period; 57 alarm = current_sec + config->period; 58 if (alarm < current_sec) { 59 break; 60 } 61 } else { 62 alarm = config->period; 63 } 64 65 if (config->index == 0U) { 66 base->ALARM0 = alarm; 67 base->ALARM0_INC = alarm_inc; 68 } else { 69 base->ALARM1 = alarm; 70 base->ALARM1_INC = alarm_inc; 71 } 72 73 status = status_success; 74 } while (false); 75 76 return status; 77 } 78