Skip to content

Commit 53485bc

Browse files
committed
Add Chrono support to EventFlags
1 parent 9d039ed commit 53485bc

File tree

2 files changed

+90
-7
lines changed

2 files changed

+90
-7
lines changed

rtos/EventFlags.h

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
#include <cstddef>
2626
#include <stdint.h>
27+
#include "rtos/Kernel.h"
2728
#include "rtos/mbed_rtos_types.h"
2829
#include "rtos/mbed_rtos1_types.h"
2930
#include "rtos/mbed_rtos_storage.h"
@@ -97,6 +98,26 @@ class EventFlags : private mbed::NonCopyable<EventFlags> {
9798
*/
9899
uint32_t wait_all(uint32_t flags = 0, uint32_t millisec = osWaitForever, bool clear = true);
99100

101+
/** Wait for all of the specified event flags to become signaled.
102+
@param flags the flags to wait for.
103+
@param rel_time timeout value.
104+
@param clear clear specified event flags after waiting for them (default: true).
105+
@return event flags before clearing or error code if highest bit set (see @a osFlagsError for details).
106+
107+
@note You may call this function from ISR context if the rel_time parameter is set to 0.
108+
*/
109+
uint32_t wait_all_for(uint32_t flags, Kernel::Clock::duration_u32 rel_time, bool clear = true);
110+
111+
/** Wait for all of the specified event flags to become signaled.
112+
@param flags the flags to wait for.
113+
@param abs_time timeout value.
114+
@param clear clear specified event flags after waiting for them (default: true).
115+
@return event flags before clearing or error code if highest bit set (see @a osFlagsError for details).
116+
117+
@note You cannot call this function from ISR context.
118+
*/
119+
uint32_t wait_all_until(uint32_t flags, Kernel::Clock::time_point abs_time, bool clear = true);
120+
100121
/** Wait for any of the specified event flags to become signaled.
101122
@param flags the flags to wait for (default: 0 -- no flags).
102123
@param millisec timeout value (default: osWaitForever).
@@ -107,6 +128,26 @@ class EventFlags : private mbed::NonCopyable<EventFlags> {
107128
*/
108129
uint32_t wait_any(uint32_t flags = 0, uint32_t millisec = osWaitForever, bool clear = true);
109130

131+
/** Wait for any of the specified event flags to become signaled.
132+
@param flags the flags to wait for.
133+
@param rel_time timeout value.
134+
@param clear clear specified event flags after waiting for them (default: true).
135+
@return event flags before clearing or error code if highest bit set (see @a osFlagsError for details).
136+
137+
@note This function may be called from ISR context if the millisec parameter is set to 0.
138+
*/
139+
uint32_t wait_any_for(uint32_t flags, Kernel::Clock::duration_u32 rel_time, bool clear = true);
140+
141+
/** Wait for any of the specified event flags to become signaled.
142+
@param flags the flags to wait for.
143+
@param abs_time timeout value.
144+
@param clear clear specified event flags after waiting for them (default: true).
145+
@return event flags before clearing or error code if highest bit set (see @a osFlagsError for details).
146+
147+
@note You cannot call this function from ISR context.
148+
*/
149+
uint32_t wait_any_until(uint32_t flags, Kernel::Clock::time_point abs_time, bool clear = true);
150+
110151
/** EventFlags destructor.
111152
112153
@note You cannot call this function from ISR context.
@@ -115,7 +156,9 @@ class EventFlags : private mbed::NonCopyable<EventFlags> {
115156

116157
private:
117158
void constructor(const char *name = nullptr);
118-
uint32_t wait(uint32_t flags, uint32_t opt, uint32_t millisec, bool clear);
159+
uint32_t wait_for(uint32_t flags, uint32_t opt, Kernel::Clock::duration_u32 rel_time, bool clear);
160+
uint32_t wait_until(uint32_t flags, uint32_t opt, Kernel::Clock::time_point abs_time, bool clear);
161+
119162
#if MBED_CONF_RTOS_PRESENT
120163
osEventFlagsId_t _id;
121164
mbed_rtos_storage_event_flags_t _obj_mem;

rtos/source/EventFlags.cpp

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@
2626
#include "platform/mbed_error.h"
2727
#include "platform/mbed_assert.h"
2828

29+
using std::milli;
30+
using std::chrono::duration;
31+
2932
namespace rtos {
3033

3134
EventFlags::EventFlags()
@@ -81,12 +84,32 @@ uint32_t EventFlags::get() const
8184

8285
uint32_t EventFlags::wait_all(uint32_t flags, uint32_t millisec, bool clear)
8386
{
84-
return wait(flags, osFlagsWaitAll, millisec, clear);
87+
return wait_all_for(flags, duration<uint32_t, milli>(millisec), clear);
88+
}
89+
90+
uint32_t EventFlags::wait_all_for(uint32_t flags, Kernel::Clock::duration_u32 rel_time, bool clear)
91+
{
92+
return wait_for(flags, osFlagsWaitAll, rel_time, clear);
93+
}
94+
95+
uint32_t EventFlags::wait_all_until(uint32_t flags, Kernel::Clock::time_point abs_time, bool clear)
96+
{
97+
return wait_until(flags, osFlagsWaitAll, abs_time, clear);
8598
}
8699

87100
uint32_t EventFlags::wait_any(uint32_t flags, uint32_t millisec, bool clear)
88101
{
89-
return wait(flags, osFlagsWaitAny, millisec, clear);
102+
return wait_any_for(flags, duration<uint32_t, milli>(millisec), clear);
103+
}
104+
105+
uint32_t EventFlags::wait_any_for(uint32_t flags, Kernel::Clock::duration_u32 rel_time, bool clear)
106+
{
107+
return wait_for(flags, osFlagsWaitAny, rel_time, clear);
108+
}
109+
110+
uint32_t EventFlags::wait_any_until(uint32_t flags, Kernel::Clock::time_point abs_time, bool clear)
111+
{
112+
return wait_until(flags, osFlagsWaitAny, abs_time, clear);
90113
}
91114

92115
EventFlags::~EventFlags()
@@ -96,30 +119,47 @@ EventFlags::~EventFlags()
96119
#endif
97120
}
98121

99-
uint32_t EventFlags::wait(uint32_t flags, uint32_t opt, uint32_t millisec, bool clear)
122+
uint32_t EventFlags::wait_for(uint32_t flags, uint32_t opt, Kernel::Clock::duration_u32 rel_time, bool clear)
100123
{
101124
if (clear == false) {
102125
opt |= osFlagsNoClear;
103126
}
104127

105128
#if MBED_CONF_RTOS_PRESENT
106-
return osEventFlagsWait(_id, flags, opt, millisec);
129+
return osEventFlagsWait(_id, flags, opt, rel_time.count());
107130
#else
108131
rtos::internal::flags_check_capture check;
109132
check.flags = &_flags;
110133
check.options = opt;
111134
check.flags_wanted = flags;
112135
check.result = 0;
113136
check.match = false;
114-
mbed::internal::do_timed_sleep_relative_or_forever(millisec, rtos::internal::non_rtos_check_flags, &check);
137+
mbed::internal::do_timed_sleep_relative_or_forever(rel_time, rtos::internal::non_rtos_check_flags, &check);
115138
if (check.match) {
116139
return check.result;
117-
} else if (millisec == 0) {
140+
} else if (rel_time == rel_time.zero()) {
118141
return osErrorResource;
119142
} else {
120143
return osErrorTimeout;
121144
}
122145
#endif
123146
}
124147

148+
uint32_t EventFlags::wait_until(uint32_t flags, uint32_t opt, Kernel::Clock::time_point abs_time, bool clear)
149+
{
150+
Kernel::Clock::time_point now = Kernel::Clock::now();
151+
152+
Kernel::Clock::duration_u32 rel_time;
153+
if (now >= abs_time) {
154+
rel_time = rel_time.zero();
155+
} else if (abs_time - now > Kernel::wait_for_u32_max) {
156+
// Documentation permits early return for big offsets
157+
rel_time = Kernel::wait_for_u32_max;
158+
} else {
159+
rel_time = abs_time - now;
160+
}
161+
return wait_for(flags, opt, rel_time, clear);
162+
}
163+
164+
125165
}

0 commit comments

Comments
 (0)