Open Model Railroad Network (OpenMRN)
Loading...
Searching...
No Matches
CC32xxEEPROMFlushFlow.hxx
Go to the documentation of this file.
1
34#ifndef FREERTOS_DRIVERS_TI_CC32XXEEPROMFLUSHFLOW_HXX_
35#define FREERTOS_DRIVERS_TI_CC32XXEEPROMFLUSHFLOW_HXX_
36
38
39extern "C" {
40void eeprom_flush();
41}
42
43class EepromTimerFlow : public StateFlowBase, protected Atomic
44{
45public:
47 : StateFlowBase(s)
48 , isWaiting_(0)
49 , isDirty_(0)
50 {
51 start_flow(STATE(test_and_sleep));
52 }
53
54 void wakeup() {
55 AtomicHolder h(this);
56 isDirty_ = 1;
57 if (isWaiting_) {
58 isWaiting_ = 0;
59 notify();
60 }
61 }
62
63private:
64 Action test_and_sleep()
65 {
66 bool need_sleep = false;
67 {
68 AtomicHolder h(this);
69 if (isDirty_)
70 {
71 isDirty_ = 0;
72 need_sleep = true;
73 }
74 else
75 {
76 isWaiting_ = 1;
77 }
78 }
79 if (need_sleep)
80 {
81 return sleep_and_call(
82 &timer_, MSEC_TO_NSEC(1000), STATE(test_and_flush));
83 }
84 else
85 {
86 return wait();
87 }
88 }
89
90 Action test_and_flush()
91 {
92 bool need_sleep = false;
93 {
94 AtomicHolder h(this);
95 if (isDirty_)
96 {
97 // we received another write during the sleep. Go back to sleep.
98 isDirty_ = 0;
99 need_sleep = true;
100 }
101 }
102 if (need_sleep)
103 {
104 return sleep_and_call(
105 &timer_, MSEC_TO_NSEC(1000), STATE(test_and_flush));
106 }
107 eeprom_flush();
108 return call_immediately(STATE(test_and_sleep));
109 }
110
111 StateFlowTimer timer_{this};
112 // 1 if the flow is sleeping and needs to be notified to wake up.
113 unsigned isWaiting_ : 1;
114 // 1 if we received a notification from the eeprom handler.
115 unsigned isDirty_ : 1;
116};
117
118extern EepromTimerFlow eepromTimerFlow_;
119
120#ifndef SKIP_UPDATED_CALLBACK
121extern "C" {
122void eeprom_updated_notification() {
123 eepromTimerFlow_.wakeup();
124}
125}
126#endif
127
128#endif // FREERTOS_DRIVERS_TI_CC32XXEEPROMFLUSHFLOW_HXX_
#define STATE(_fn)
Turns a function name into an argument to be supplied to functions expecting a state.
Definition StateFlow.hxx:61
See OSMutexLock in os/OS.hxx.
Definition Atomic.hxx:153
Lightweight locking class for protecting small critical sections.
Definition Atomic.hxx:130
Collection of related state machines that pend on incoming messages.
Return type for a state flow callback.
Use this timer class to deliver the timeout notification to a stateflow.
Base class for state machines.
void notify() override
Wakeup call arrived.
Definition StateFlow.cxx:97
StateFlowBase()
Default constructor.
void start_flow(Callback c)
Resets the flow to the specified state and starts it.
Action wait()
Wait for an asynchronous call.
Action call_immediately(Callback c)
Imediately call the next state upon return.
Action sleep_and_call(::Timer *timer, long long timeout_nsec, Callback c)
Suspends execution of this control flow for a specified time.
#define MSEC_TO_NSEC(_msec)
Convert a millisecond value to a nanosecond value.
Definition os.h:268