Skip to content

Commit 698b52d

Browse files
Merge pull request #4517 from YarivCol/read_write_mutex
Add cpp API for CMSIS OS 2 EventFlags
2 parents c72d60a + b0ad73e commit 698b52d

File tree

4 files changed

+253
-0
lines changed

4 files changed

+253
-0
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Copyright (c) 2013-2017, ARM Limited, All Rights Reserved
3+
* SPDX-License-Identifier: Apache-2.0
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License"); you may
6+
* not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
#include "mbed.h"
19+
#include "greentea-client/test_env.h"
20+
#include "rtos.h"
21+
22+
#if defined(MBED_RTOS_SINGLE_THREAD)
23+
#error [NOT_SUPPORTED] test not supported
24+
#endif
25+
26+
#define TEST_STACK_SIZE 512
27+
28+
#define EVENT_SET_VALUE 0x01
29+
const int EVENT_TO_EMIT = 100;
30+
const int EVENT_HANDLE_DELAY = 25;
31+
32+
DigitalOut led(LED1);
33+
EventFlags event_flags;
34+
35+
int events_counter = 0;
36+
37+
void led_thread() {
38+
while (true) {
39+
event_flags.wait_all(EVENT_SET_VALUE);
40+
led = !led;
41+
events_counter++;
42+
}
43+
}
44+
45+
int main (void) {
46+
GREENTEA_SETUP(10, "default_auto");
47+
48+
Thread thread(osPriorityNormal, TEST_STACK_SIZE);
49+
thread.start(led_thread);
50+
51+
bool result = false;
52+
53+
while (true) {
54+
Thread::wait(2 * EVENT_HANDLE_DELAY);
55+
event_flags.set(EVENT_SET_VALUE);
56+
if (events_counter == EVENT_TO_EMIT) {
57+
result = true;
58+
break;
59+
}
60+
}
61+
GREENTEA_TESTSUITE_RESULT(result);
62+
return 0;
63+
}

rtos/EventFlags.cpp

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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+
{
31+
constructor();
32+
}
33+
34+
EventFlags::EventFlags(const char *name)
35+
{
36+
constructor(name);
37+
}
38+
39+
void EventFlags::constructor(const char *name)
40+
{
41+
memset(&_obj_mem, 0, sizeof(_obj_mem));
42+
memset(&_attr, 0, sizeof(_attr));
43+
_attr.name = name ? name : "application_unnamed_event_flags";
44+
_attr.cb_mem = &_obj_mem;
45+
_attr.cb_size = sizeof(_obj_mem);
46+
_id = osEventFlagsNew(&_attr);
47+
MBED_ASSERT(_id);
48+
}
49+
50+
uint32_t EventFlags::set(uint32_t flags)
51+
{
52+
return osEventFlagsSet(_id, flags);
53+
}
54+
55+
uint32_t EventFlags::clear(uint32_t flags)
56+
{
57+
return osEventFlagsClear(_id, flags);
58+
}
59+
60+
uint32_t EventFlags::get() const
61+
{
62+
return osEventFlagsGet(_id);
63+
}
64+
65+
uint32_t EventFlags::wait_all(uint32_t flags, uint32_t timeout, bool clear)
66+
{
67+
return wait(flags, osFlagsWaitAll, timeout, clear);
68+
}
69+
70+
uint32_t EventFlags::wait_any(uint32_t flags, uint32_t timeout, bool clear)
71+
{
72+
return wait(flags, osFlagsWaitAny, timeout, clear);
73+
}
74+
75+
EventFlags::~EventFlags()
76+
{
77+
osEventFlagsDelete(_id);
78+
}
79+
80+
uint32_t EventFlags::wait(uint32_t flags, uint32_t opt, uint32_t timeout, bool clear)
81+
{
82+
if (clear == false) {
83+
opt |= osFlagsNoClear;
84+
}
85+
86+
return osEventFlagsWait(_id, flags, opt, timeout);
87+
}
88+
89+
}

rtos/EventFlags.h

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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 or wait for an arbitrary event or events.
37+
@note
38+
EventFlags support 31 flags so the MSB flag is ignored, it is used to return an error code (@a 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.
56+
@return event flags after setting or error code if highest bit set (@a osFlagsError).
57+
*/
58+
uint32_t set(uint32_t flags);
59+
60+
/** Clear the specified Event Flags.
61+
@param flags specifies the flags that shall be cleared. (default: 0x7fffffff - all flags)
62+
@return event flags before clearing or error code if highest bit set (@a osFlagsError).
63+
*/
64+
uint32_t clear(uint32_t flags = 0x7fffffff);
65+
66+
/** Get the currently set Event Flags.
67+
@return set event flags.
68+
*/
69+
uint32_t get() const;
70+
71+
/** Wait for all of the specified event flags to become signaled.
72+
@param flags specifies the flags to wait for.
73+
@param timeout timeout value or 0 in case of no time-out. (default: osWaitForever)
74+
@param clear specifies wether to clear the flags after waiting for them. (default: true)
75+
@return event flags before clearing or error code if highest bit set (@a osFlagsError).
76+
*/
77+
uint32_t wait_all(uint32_t flags = 0, uint32_t timeout = osWaitForever, bool clear = true);
78+
79+
/** Wait for any of the specified event flags to become signaled.
80+
@param flags specifies the flags to wait for. (default: 0)
81+
@param timeout timeout value or 0 in case of no time-out. (default: osWaitForever)
82+
@param clear specifies wether to clear the flags after waiting for them. (default: true)
83+
@return event flags before clearing or error code if highest bit set (@a osFlagsError).
84+
*/
85+
uint32_t wait_any(uint32_t flags = 0, uint32_t timeout = osWaitForever, bool clear = true);
86+
87+
~EventFlags();
88+
89+
private:
90+
void constructor(const char *name = NULL);
91+
uint32_t wait(uint32_t flags, uint32_t opt, uint32_t timeout, bool clear);
92+
osEventFlagsId_t _id;
93+
osEventFlagsAttr_t _attr;
94+
mbed_rtos_storage_event_flags_t _obj_mem;
95+
};
96+
97+
}
98+
#endif
99+
100+
/** @}*/

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)