-
Notifications
You must be signed in to change notification settings - Fork 3k
Add cpp API for CMSIS OS 2 EventFlags #4517
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
* Copyright (c) 2013-2017, ARM Limited, All Rights Reserved | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#include "mbed.h" | ||
#include "greentea-client/test_env.h" | ||
#include "rtos.h" | ||
|
||
#if defined(MBED_RTOS_SINGLE_THREAD) | ||
#error [NOT_SUPPORTED] test not supported | ||
#endif | ||
|
||
#define TEST_STACK_SIZE 512 | ||
|
||
#define EVENT_SET_VALUE 0x01 | ||
const int EVENT_TO_EMIT = 100; | ||
const int EVENT_HANDLE_DELAY = 25; | ||
|
||
DigitalOut led(LED1); | ||
EventFlags event_flags; | ||
|
||
int events_counter = 0; | ||
|
||
void led_thread() { | ||
while (true) { | ||
event_flags.wait_all(EVENT_SET_VALUE); | ||
led = !led; | ||
events_counter++; | ||
} | ||
} | ||
|
||
int main (void) { | ||
GREENTEA_SETUP(10, "default_auto"); | ||
|
||
Thread thread(osPriorityNormal, TEST_STACK_SIZE); | ||
thread.start(led_thread); | ||
|
||
bool result = false; | ||
|
||
while (true) { | ||
Thread::wait(2 * EVENT_HANDLE_DELAY); | ||
event_flags.set(EVENT_SET_VALUE); | ||
if (events_counter == EVENT_TO_EMIT) { | ||
result = true; | ||
break; | ||
} | ||
} | ||
GREENTEA_TESTSUITE_RESULT(result); | ||
return 0; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/* mbed Microcontroller Library | ||
* Copyright (c) 2006-2017 ARM Limited | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
*/ | ||
#include "rtos/EventFlags.h" | ||
#include <string.h> | ||
#include "mbed_error.h" | ||
#include "mbed_assert.h" | ||
|
||
namespace rtos { | ||
|
||
EventFlags::EventFlags() | ||
{ | ||
constructor(); | ||
} | ||
|
||
EventFlags::EventFlags(const char *name) | ||
{ | ||
constructor(name); | ||
} | ||
|
||
void EventFlags::constructor(const char *name) | ||
{ | ||
memset(&_obj_mem, 0, sizeof(_obj_mem)); | ||
memset(&_attr, 0, sizeof(_attr)); | ||
_attr.name = name ? name : "application_unnamed_event_flags"; | ||
_attr.cb_mem = &_obj_mem; | ||
_attr.cb_size = sizeof(_obj_mem); | ||
_id = osEventFlagsNew(&_attr); | ||
MBED_ASSERT(_id); | ||
} | ||
|
||
uint32_t EventFlags::set(uint32_t flags) | ||
{ | ||
return osEventFlagsSet(_id, flags); | ||
} | ||
|
||
uint32_t EventFlags::clear(uint32_t flags) | ||
{ | ||
return osEventFlagsClear(_id, flags); | ||
} | ||
|
||
uint32_t EventFlags::get() const | ||
{ | ||
return osEventFlagsGet(_id); | ||
} | ||
|
||
uint32_t EventFlags::wait_all(uint32_t flags, uint32_t timeout, bool clear) | ||
{ | ||
return wait(flags, osFlagsWaitAll, timeout, clear); | ||
} | ||
|
||
uint32_t EventFlags::wait_any(uint32_t flags, uint32_t timeout, bool clear) | ||
{ | ||
return wait(flags, osFlagsWaitAny, timeout, clear); | ||
} | ||
|
||
EventFlags::~EventFlags() | ||
{ | ||
osEventFlagsDelete(_id); | ||
} | ||
|
||
uint32_t EventFlags::wait(uint32_t flags, uint32_t opt, uint32_t timeout, bool clear) | ||
{ | ||
if (clear == false) { | ||
opt |= osFlagsNoClear; | ||
} | ||
|
||
return osEventFlagsWait(_id, flags, opt, timeout); | ||
} | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
/* mbed Microcontroller Library | ||
* Copyright (c) 2006-2017 ARM Limited | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
*/ | ||
#ifndef EVENT_FLAG_H | ||
#define EVENT_FLAG_H | ||
|
||
#include <stdint.h> | ||
#include "cmsis_os2.h" | ||
#include "mbed_rtos1_types.h" | ||
#include "mbed_rtos_storage.h" | ||
|
||
#include "platform/NonCopyable.h" | ||
|
||
namespace rtos { | ||
/** \addtogroup rtos */ | ||
/** @{*/ | ||
|
||
/** The EventFlags class is used to signal or wait for an arbitrary event or events. | ||
@note | ||
EventFlags support 31 flags so the MSB flag is ignored, it is used to return an error code (@a osFlagsError) | ||
@note | ||
Memory considerations: The EventFlags control structures will be created on current thread's stack, both for the mbed OS | ||
and underlying RTOS objects (static or dynamic RTOS memory pools are not being used). | ||
*/ | ||
class EventFlags : private mbed::NonCopyable<EventFlags> { | ||
public: | ||
/** Create and Initialize a EventFlags object */ | ||
EventFlags(); | ||
|
||
/** Create and Initialize a EventFlags object | ||
|
||
@param name name to be used for this EventFlags. It has to stay allocated for the lifetime of the thread. | ||
*/ | ||
EventFlags(const char *name); | ||
|
||
/** Set the specified Event Flags. | ||
@param flags specifies the flags that shall be set. | ||
@return event flags after setting or error code if highest bit set (@a osFlagsError). | ||
*/ | ||
uint32_t set(uint32_t flags); | ||
|
||
/** Clear the specified Event Flags. | ||
@param flags specifies the flags that shall be cleared. (default: 0x7fffffff - all flags) | ||
@return event flags before clearing or error code if highest bit set (@a osFlagsError). | ||
*/ | ||
uint32_t clear(uint32_t flags = 0x7fffffff); | ||
|
||
/** Get the currently set Event Flags. | ||
@return set event flags. | ||
*/ | ||
uint32_t get() const; | ||
|
||
/** Wait for all of the specified event flags to become signaled. | ||
@param flags specifies the flags to wait for. | ||
@param timeout timeout value or 0 in case of no time-out. (default: osWaitForever) | ||
@param clear specifies wether to clear the flags after waiting for them. (default: true) | ||
@return event flags before clearing or error code if highest bit set (@a osFlagsError). | ||
*/ | ||
uint32_t wait_all(uint32_t flags = 0, uint32_t timeout = osWaitForever, bool clear = true); | ||
|
||
/** Wait for any of the specified event flags to become signaled. | ||
@param flags specifies the flags to wait for. (default: 0) | ||
@param timeout timeout value or 0 in case of no time-out. (default: osWaitForever) | ||
@param clear specifies wether to clear the flags after waiting for them. (default: true) | ||
@return event flags before clearing or error code if highest bit set (@a osFlagsError). | ||
*/ | ||
uint32_t wait_any(uint32_t flags = 0, uint32_t timeout = osWaitForever, bool clear = true); | ||
|
||
~EventFlags(); | ||
|
||
private: | ||
void constructor(const char *name = NULL); | ||
uint32_t wait(uint32_t flags, uint32_t opt, uint32_t timeout, bool clear); | ||
osEventFlagsId_t _id; | ||
osEventFlagsAttr_t _attr; | ||
mbed_rtos_storage_event_flags_t _obj_mem; | ||
}; | ||
|
||
} | ||
#endif | ||
|
||
/** @}*/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
tests should contain also license header, please add it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we add the license header to this file please?