Skip to content

Commit c532a9a

Browse files
YarivColbulislaw
authored andcommitted
RTOS: Add EventFlags class
EventFlags class is a wrapper for Event Flag functionality introduced in RTOS2/RTX5.
1 parent 99a8467 commit c532a9a

File tree

3 files changed

+163
-0
lines changed

3 files changed

+163
-0
lines changed

rtos/EventFlags.cpp

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/* mbed Microcontroller Library
2+
* Copyright (c) 2006-2017 ARM Limited
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy
5+
* of this software and associated documentation files (the "Software"), to deal
6+
* in the Software without restriction, including without limitation the rights
7+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
* copies of the Software, and to permit persons to whom the Software is
9+
* furnished to do so, subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in
12+
* all copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20+
* SOFTWARE.
21+
*/
22+
#include "rtos/EventFlags.h"
23+
#include <string.h>
24+
#include "mbed_error.h"
25+
#include "mbed_assert.h"
26+
27+
namespace rtos {
28+
29+
EventFlags::EventFlags() {
30+
constructor();
31+
}
32+
33+
EventFlags::EventFlags(const char *name) {
34+
constructor(name);
35+
}
36+
37+
void EventFlags::constructor(const char *name) {
38+
memset(&_obj_mem, 0, sizeof(_obj_mem));
39+
memset(&_attr, 0, sizeof(_attr));
40+
_attr.name = name ? name : "application_unnamed_event_flags";
41+
_attr.cb_mem = &_obj_mem;
42+
_attr.cb_size = sizeof(_obj_mem);
43+
_id = osEventFlagsNew(&_attr);
44+
MBED_ASSERT(_id);
45+
}
46+
47+
uint32_t EventFlags::set(uint32_t flags) {
48+
return osEventFlagsSet(_id, flags);
49+
}
50+
51+
uint32_t EventFlags::clear(uint32_t flags) {
52+
return osEventFlagsClear(_id, flags);
53+
}
54+
55+
uint32_t EventFlags::get() {
56+
return osEventFlagsGet(_id);
57+
}
58+
59+
uint32_t EventFlags::wait(uint32_t flags, uint32_t timeout) {
60+
if(flags == 0) {
61+
return osEventFlagsWait(_id, 0x7fffffff, osFlagsWaitAny | osFlagsNoClear, timeout);
62+
}
63+
return osEventFlagsWait(_id, flags, osFlagsWaitAll, timeout);
64+
}
65+
66+
EventFlags::~EventFlags() {
67+
osEventFlagsDelete(_id);
68+
}
69+
70+
}

rtos/EventFlags.h

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/* mbed Microcontroller Library
2+
* Copyright (c) 2006-2017 ARM Limited
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy
5+
* of this software and associated documentation files (the "Software"), to deal
6+
* in the Software without restriction, including without limitation the rights
7+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
* copies of the Software, and to permit persons to whom the Software is
9+
* furnished to do so, subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in
12+
* all copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20+
* SOFTWARE.
21+
*/
22+
#ifndef EVENT_FLAG_H
23+
#define EVENT_FLAG_H
24+
25+
#include <stdint.h>
26+
#include "cmsis_os2.h"
27+
#include "mbed_rtos1_types.h"
28+
#include "mbed_rtos_storage.h"
29+
30+
#include "platform/NonCopyable.h"
31+
32+
namespace rtos {
33+
/** \addtogroup rtos */
34+
/** @{*/
35+
36+
/** The EventFlags class is used to signal to whom it may concern about an event has occured.
37+
@note
38+
EventFlags support 31 flags so the MSB flag is ignored, it is used to return error code (osFlagsError)
39+
@note
40+
Memory considerations: The EventFlags control structures will be created on current thread's stack, both for the mbed OS
41+
and underlying RTOS objects (static or dynamic RTOS memory pools are not being used).
42+
*/
43+
class EventFlags : private mbed::NonCopyable<EventFlags> {
44+
public:
45+
/** Create and Initialize a EventFlags object */
46+
EventFlags();
47+
48+
/** Create and Initialize a EventFlags object
49+
50+
@param name name to be used for this EventFlags. It has to stay allocated for the lifetime of the thread.
51+
*/
52+
EventFlags(const char *name);
53+
54+
/** Set the specified Event Flags.
55+
@param flags specifies the flags that shall be set. (default: 0x7fffffff)
56+
@return event flags after setting or error code if highest bit set (osFlagsError).
57+
*/
58+
uint32_t set(uint32_t flags = 0x7fffffff);
59+
60+
/** Clear the specified Event Flags.
61+
@param flags specifies the flags that shall be cleared. (default: 0x7fffffff0)
62+
@return event flags before clearing or error code if highest bit set (osFlagsError).
63+
*/
64+
uint32_t clear(uint32_t flags = 0x7fffffff);
65+
66+
/** Get the current Event Flags.
67+
@return current event flags.
68+
*/
69+
uint32_t get();
70+
71+
/** Wait for one or more Event Flags to become signaled.
72+
@param flags specifies the flags to wait for. (default: 0)
73+
@param timeout timeout value or 0 in case of no time-out. (default: osWaitForever)
74+
@return event flags before clearing or error code if highest bit set (osFlagsError).
75+
@note incase of flags 0 the function will wait to any flag and will not clear the flags,
76+
the user must clear the flags. otherwise the function to wait all specified flags and clear them.
77+
*/
78+
uint32_t wait(uint32_t flags = 0, uint32_t timeout = osWaitForever);
79+
80+
~EventFlags();
81+
82+
private:
83+
void constructor(const char *name = NULL);
84+
osEventFlagsId_t _id;
85+
osEventFlagsAttr_t _attr;
86+
mbed_rtos_storage_event_flags_t _obj_mem;
87+
};
88+
89+
}
90+
#endif
91+
92+
/** @}*/

rtos/rtos.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#include "rtos/Mail.h"
3636
#include "rtos/MemoryPool.h"
3737
#include "rtos/Queue.h"
38+
#include "rtos/EventFlags.h"
3839

3940
using namespace rtos;
4041

0 commit comments

Comments
 (0)