Skip to content

Commit b9bfd0f

Browse files
RTC best practices (#97)
* rtc manager best practices * Move rtc first in topology configure --------- Co-authored-by: Sam S. Yu <[email protected]>
1 parent da683fb commit b9bfd0f

File tree

6 files changed

+35
-14
lines changed

6 files changed

+35
-14
lines changed

FprimeZephyrReference/Components/Drv/RtcManager/RtcManager.cpp

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,25 @@ namespace Drv {
1111
// Component construction and destruction
1212
// ----------------------------------------------------------------------
1313

14-
RtcManager ::RtcManager(const char* const compName) : RtcManagerComponentBase(compName), m_console_throttled(false) {
15-
// Initialize device
16-
this->dev = device_get_binding("RV3028");
17-
}
14+
RtcManager ::RtcManager(const char* const compName) : RtcManagerComponentBase(compName) {}
1815

1916
RtcManager ::~RtcManager() {}
2017

18+
// ----------------------------------------------------------------------
19+
// Public helper methods
20+
// ----------------------------------------------------------------------
21+
22+
void RtcManager ::configure(const struct device* dev) {
23+
this->m_dev = dev;
24+
}
25+
2126
// ----------------------------------------------------------------------
2227
// Handler implementations for typed input ports
2328
// ----------------------------------------------------------------------
2429

2530
void RtcManager ::timeGetPort_handler(FwIndexType portNum, Fw::Time& time) {
2631
// Check device readiness
27-
if (!device_is_ready(this->dev)) {
32+
if (!device_is_ready(this->m_dev)) {
2833
// Use logger instead of events since this fn is in a critical path for FPrime
2934
// to get time. Events require time, if this method fails an event will fail.
3035
//
@@ -38,7 +43,7 @@ void RtcManager ::timeGetPort_handler(FwIndexType portNum, Fw::Time& time) {
3843

3944
// Get time from RTC
4045
struct rtc_time time_rtc = {};
41-
rtc_get_time(this->dev, &time_rtc);
46+
rtc_get_time(this->m_dev, &time_rtc);
4247

4348
// Convert to generic tm struct
4449
struct tm* time_tm = rtc_time_to_tm(&time_rtc);
@@ -67,7 +72,7 @@ void RtcManager ::timeGetPort_handler(FwIndexType portNum, Fw::Time& time) {
6772

6873
void RtcManager ::TIME_SET_cmdHandler(FwOpcodeType opCode, U32 cmdSeq, Drv::TimeData t) {
6974
// Check device readiness
70-
if (!device_is_ready(this->dev)) {
75+
if (!device_is_ready(this->m_dev)) {
7176
// Emit device not ready event
7277
this->log_WARNING_HI_DeviceNotReady();
7378

@@ -104,7 +109,7 @@ void RtcManager ::TIME_SET_cmdHandler(FwOpcodeType opCode, U32 cmdSeq, Drv::Time
104109
};
105110

106111
// Set time on RTC
107-
const int status = rtc_set_time(this->dev, &time_rtc);
112+
const int status = rtc_set_time(this->m_dev, &time_rtc);
108113

109114
if (status != 0) {
110115
// Emit time not set event

FprimeZephyrReference/Components/Drv/RtcManager/RtcManager.hpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,14 @@ class RtcManager final : public RtcManagerComponentBase {
3232
//! Destroy RtcManager object
3333
~RtcManager();
3434

35+
public:
36+
// ----------------------------------------------------------------------
37+
// Public helper methods
38+
// ----------------------------------------------------------------------
39+
40+
//! Configure the RTC device
41+
void configure(const struct device* dev);
42+
3543
private:
3644
// ----------------------------------------------------------------------
3745
// Handler implementations for typed input ports
@@ -67,7 +75,7 @@ class RtcManager final : public RtcManagerComponentBase {
6775
bool timeDataIsValid(Drv::TimeData t);
6876

6977
//! device stores the initialized Zephyr RTC device
70-
const struct device* dev;
78+
const struct device* m_dev;
7179
};
7280

7381
} // namespace Drv

FprimeZephyrReference/ReferenceDeployment/Main.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,13 @@
1010
#include <zephyr/kernel.h>
1111
#include <zephyr/sys/printk.h>
1212

13-
const struct device* ina219Sys = device_get_binding("INA219 sys");
14-
const struct device* ina219Sol = device_get_binding("INA219 sol");
13+
const struct device* ina219Sys = DEVICE_DT_GET(DT_NODELABEL(ina219_0));
14+
const struct device* ina219Sol = DEVICE_DT_GET(DT_NODELABEL(ina219_1));
1515
const struct device* serial = DEVICE_DT_GET(DT_NODELABEL(cdc_acm_uart0));
1616
const struct device* lora = DEVICE_DT_GET(DT_NODELABEL(lora0));
1717
const struct device* lsm6dso = DEVICE_DT_GET(DT_NODELABEL(lsm6dso0));
1818
const struct device* lis2mdl = DEVICE_DT_GET(DT_NODELABEL(lis2mdl0));
19+
const struct device* rtc = DEVICE_DT_GET(DT_NODELABEL(rtc0));
1920

2021
int main(int argc, char* argv[]) {
2122
// ** DO NOT REMOVE **//
@@ -32,6 +33,7 @@ int main(int argc, char* argv[]) {
3233
inputs.uartDevice = serial;
3334
inputs.lsm6dsoDevice = lsm6dso;
3435
inputs.lis2mdlDevice = lis2mdl;
36+
inputs.rtcDevice = rtc;
3537
inputs.baudRate = 115200;
3638

3739
// Setup, cycle, and teardown topology

FprimeZephyrReference/ReferenceDeployment/Top/ReferenceDeploymentTopology.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,9 @@ void setupTopology(const TopologyState& state) {
103103
// Autocoded task kick-off (active components). Function provided by autocoder.
104104
startTasks(state);
105105

106+
// Try to configure the RTC device first because all other components need time
107+
rtcManager.configure(state.rtcDevice);
108+
106109
// We have a pipeline for both the LoRa and UART drive to allow for ground harness debugging an
107110
// for over-the-air communications.
108111
lora.start(state.loraDevice, Zephyr::TransmitState::DISABLED);

FprimeZephyrReference/ReferenceDeployment/Top/ReferenceDeploymentTopologyDefs.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ struct TopologyState {
7979
const device* ina219SolDevice; //!< device path for solar panel ina219
8080
const device* lsm6dsoDevice; //!< LSM6DSO device path for accelerometer/gyroscope
8181
const device* lis2mdlDevice; //!< LIS2MDL device path for magnetometer
82+
const device* rtcDevice; //!< RTC device path
8283
};
8384

8485
namespace PingEntries = ::PingEntries;

boards/bronco_space/proves_flight_control_board_v5/proves_flight_control_board_v5.dtsi

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ zephyr_udc0: &usbd {
141141
pinctrl-names = "default";
142142
clock-frequency = <100000>;
143143

144-
ina219Sys: ina219Sys@40 {
144+
ina219_0: ina219_0@40 {
145145
compatible = "ti,ina219";
146146
reg = <0x40>;
147147
brng = <0>;
@@ -152,7 +152,8 @@ zephyr_udc0: &usbd {
152152
lsb-microamp = <61>;
153153
label = "INA219 sys";
154154
};
155-
ina219Sol: ina219Sol@41 {
155+
156+
ina219_1: ina219_1@41 {
156157
compatible = "ti,ina219";
157158
reg = <0x41>;
158159
brng = <0>;
@@ -183,7 +184,8 @@ zephyr_udc0: &usbd {
183184
reg = <0x1e>;
184185
label = "LIS2MDL";
185186
};
186-
rv3028: rv3028@52 {
187+
188+
rtc0: rtc0@52 {
187189
compatible = "microcrystal,rv3028";
188190
reg = <0x52>;
189191
int-gpios = <&gpio0 27 GPIO_ACTIVE_HIGH>;

0 commit comments

Comments
 (0)