Skip to content

Commit 7287ead

Browse files
committed
Use fprime getTime() to get the time
1 parent fe5b6e8 commit 7287ead

File tree

2 files changed

+22
-45
lines changed

2 files changed

+22
-45
lines changed

FprimeZephyrReference/Components/Drv/RtcManager/RtcManager.cpp

Lines changed: 22 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,28 @@ void RtcManager ::timeGetPort_handler(FwIndexType portNum, Fw::Time& time) {
3232
}
3333

3434
// Get time from RTC
35-
U32 posix_time;
36-
U32 u_secs;
37-
this->timeGet(posix_time, u_secs);
35+
struct rtc_time time_rtc = {};
36+
rtc_get_time(this->dev, &time_rtc);
37+
38+
// Convert to generic tm struct
39+
struct tm* time_tm = rtc_time_to_tm(&time_rtc);
40+
41+
// Convert to time_t (seconds since epoch)
42+
errno = 0;
43+
time_t seconds = timeutil_timegm(time_tm);
44+
if (errno == ERANGE) {
45+
Fw::Logger::log("RTC returned invalid time");
46+
return;
47+
}
48+
49+
// Get microseconds from system clock cycles
50+
// Note: RV3028 does not provide sub-second precision, so this is
51+
// just an approximation based on system cycles.
52+
// FPrime expects microseconds in the range [0, 999999]
53+
uint32_t useconds = k_cyc_to_us_near32(k_cycle_get_32()) % 1000000;
3854

3955
// Set FPrime time object
40-
time.set(TimeBase::TB_WORKSTATION_TIME, 0, posix_time, u_secs);
56+
time.set(TimeBase::TB_WORKSTATION_TIME, 0, static_cast<U32>(seconds), static_cast<U32>(useconds));
4157
}
4258

4359
// ----------------------------------------------------------------------
@@ -66,9 +82,7 @@ void RtcManager ::TIME_SET_cmdHandler(FwOpcodeType opCode, U32 cmdSeq, Drv::Time
6682
};
6783

6884
// Store current time for logging
69-
U32 posix_time;
70-
U32 u_secs;
71-
this->timeGet(posix_time, u_secs);
85+
Fw::Time time_before_set = this->getTime();
7286

7387
// Set time on RTC
7488
const int status = rtc_set_time(this->dev, &time_rtc);
@@ -83,40 +97,10 @@ void RtcManager ::TIME_SET_cmdHandler(FwOpcodeType opCode, U32 cmdSeq, Drv::Time
8397
}
8498

8599
// Emit time set event, include previous time for reference
86-
this->log_ACTIVITY_HI_TimeSet(posix_time, u_secs);
100+
this->log_ACTIVITY_HI_TimeSet(time_before_set.getSeconds(), time_before_set.getUSeconds());
87101

88102
// Send command response
89103
this->cmdResponse_out(opCode, cmdSeq, Fw::CmdResponse::OK);
90104
}
91105

92-
// ----------------------------------------------------------------------
93-
// Private helper methods
94-
// ----------------------------------------------------------------------
95-
96-
void RtcManager ::timeGet(U32& posix_time, U32& u_secs) {
97-
// Read time from RTC
98-
struct rtc_time time_rtc = {};
99-
rtc_get_time(this->dev, &time_rtc);
100-
101-
// Convert time to POSIX time_t format
102-
struct tm* time_tm = rtc_time_to_tm(&time_rtc);
103-
104-
errno = 0;
105-
time_t time_pt = timeutil_timegm(time_tm);
106-
if (errno == ERANGE) {
107-
Fw::Logger::log("RV2038 returned invalid time");
108-
return;
109-
}
110-
111-
// Get microseconds from system clock cycles
112-
// Note: RV3028 does not provide sub-second precision, so this is
113-
// just an approximation based on system cycles.
114-
// FPrime expects microseconds in the range [0, 999999]
115-
uint32_t time_usecs = k_cyc_to_us_near32(k_cycle_get_32()) % 1000000;
116-
117-
// Set output parameters
118-
posix_time = static_cast<U32>(time_pt);
119-
u_secs = static_cast<U32>(time_usecs);
120-
}
121-
122106
} // namespace Drv

FprimeZephyrReference/Components/Drv/RtcManager/RtcManager.hpp

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,6 @@ class RtcManager final : public RtcManagerComponentBase {
5858
Drv::TimeData t //!< Set the time
5959
) override;
6060

61-
// ----------------------------------------------------------------------
62-
// Private helper methods
63-
// ----------------------------------------------------------------------
64-
65-
//! Helper method to get time from RTC
66-
void timeGet(U32& posix_time, U32& u_secs);
67-
6861
//! device stores the initialized Zephyr RTC device
6962
const struct device* dev;
7063
};

0 commit comments

Comments
 (0)