Skip to content

Commit f6e8eca

Browse files
committed
Add Chrono support to EventFlags
1 parent 4f2fa53 commit f6e8eca

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
@@ -25,6 +25,7 @@
2525

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

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

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

117158
private:
118159
void constructor(const char *name = nullptr);
119-
uint32_t wait(uint32_t flags, uint32_t opt, uint32_t millisec, bool clear);
160+
uint32_t wait_for(uint32_t flags, uint32_t opt, Kernel::Clock::duration_u32 rel_time, bool clear);
161+
uint32_t wait_until(uint32_t flags, uint32_t opt, Kernel::Clock::time_point abs_time, bool clear);
162+
120163
#if MBED_CONF_RTOS_PRESENT
121164
osEventFlagsId_t _id;
122165
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
@@ -27,6 +27,9 @@
2727
#include "platform/mbed_error.h"
2828
#include "platform/mbed_assert.h"
2929

30+
using std::milli;
31+
using std::chrono::duration;
32+
3033
namespace rtos {
3134

3235
EventFlags::EventFlags()
@@ -82,12 +85,32 @@ uint32_t EventFlags::get() const
8285

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

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

93116
EventFlags::~EventFlags()
@@ -97,30 +120,47 @@ EventFlags::~EventFlags()
97120
#endif
98121
}
99122

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

106129
#if MBED_CONF_RTOS_PRESENT
107-
return osEventFlagsWait(_id, flags, opt, millisec);
130+
return osEventFlagsWait(_id, flags, opt, rel_time.count());
108131
#else
109132
rtos::internal::flags_check_capture check;
110133
check.flags = &_flags;
111134
check.options = opt;
112135
check.flags_wanted = flags;
113136
check.result = 0;
114137
check.match = false;
115-
mbed::internal::do_timed_sleep_relative_or_forever(millisec, rtos::internal::non_rtos_check_flags, &check);
138+
mbed::internal::do_timed_sleep_relative_or_forever(rel_time, rtos::internal::non_rtos_check_flags, &check);
116139
if (check.match) {
117140
return check.result;
118-
} else if (millisec == 0) {
141+
} else if (rel_time == rel_time.zero()) {
119142
return osErrorResource;
120143
} else {
121144
return osErrorTimeout;
122145
}
123146
#endif
124147
}
125148

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

0 commit comments

Comments
 (0)