Kenji Arai / mbed-os_TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers EventFlags.cpp Source File

EventFlags.cpp

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2006-2017 ARM Limited
00003  *
00004  * Permission is hereby granted, free of charge, to any person obtaining a copy
00005  * of this software and associated documentation files (the "Software"), to deal
00006  * in the Software without restriction, including without limitation the rights
00007  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00008  * copies of the Software, and to permit persons to whom the Software is
00009  * furnished to do so, subject to the following conditions:
00010  *
00011  * The above copyright notice and this permission notice shall be included in
00012  * all copies or substantial portions of the Software.
00013  *
00014  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00015  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00016  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00017  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00018  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00019  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
00020  * SOFTWARE.
00021  */
00022 #include "rtos/EventFlags.h"
00023 #include "rtos/ThisThread.h"
00024 #include "mbed_os_timer.h"
00025 #include <string.h>
00026 #include "platform/mbed_error.h"
00027 #include "platform/mbed_assert.h"
00028 
00029 namespace rtos {
00030 
00031 EventFlags::EventFlags()
00032 {
00033     constructor("application_unnamed_event_flags");
00034 }
00035 
00036 EventFlags::EventFlags(const char *name)
00037 {
00038     constructor(name);
00039 }
00040 
00041 void EventFlags::constructor(const char *name)
00042 {
00043 #if MBED_CONF_RTOS_PRESENT
00044     osEventFlagsAttr_t attr = { 0 };
00045     attr.name = name;
00046     attr.cb_mem = &_obj_mem;
00047     attr.cb_size = sizeof(_obj_mem);
00048     _id = osEventFlagsNew(&attr);
00049     MBED_ASSERT(_id);
00050 #else
00051     _flags = 0;
00052 #endif
00053 }
00054 
00055 uint32_t EventFlags::set(uint32_t flags)
00056 {
00057 #if MBED_CONF_RTOS_PRESENT
00058     return osEventFlagsSet(_id, flags);
00059 #else
00060     return core_util_atomic_fetch_or_u32 (&_flags, flags) | flags;
00061 #endif
00062 }
00063 
00064 uint32_t EventFlags::clear(uint32_t flags)
00065 {
00066 #if MBED_CONF_RTOS_PRESENT
00067     return osEventFlagsClear(_id, flags);
00068 #else
00069     return core_util_atomic_fetch_and_u32 (&_flags, ~flags);
00070 #endif
00071 }
00072 
00073 uint32_t EventFlags::get() const
00074 {
00075 #if MBED_CONF_RTOS_PRESENT
00076     return osEventFlagsGet(_id);
00077 #else
00078     return core_util_atomic_load_u32 (&_flags);
00079 #endif
00080 }
00081 
00082 uint32_t EventFlags::wait_all(uint32_t flags, uint32_t millisec, bool clear)
00083 {
00084     return wait(flags, osFlagsWaitAll, millisec, clear);
00085 }
00086 
00087 uint32_t EventFlags::wait_any(uint32_t flags, uint32_t millisec, bool clear)
00088 {
00089     return wait(flags, osFlagsWaitAny, millisec, clear);
00090 }
00091 
00092 EventFlags::~EventFlags()
00093 {
00094 #if MBED_CONF_RTOS_PRESENT
00095     osEventFlagsDelete(_id);
00096 #endif
00097 }
00098 
00099 uint32_t EventFlags::wait(uint32_t flags, uint32_t opt, uint32_t millisec, bool clear)
00100 {
00101     if (clear == false) {
00102         opt |= osFlagsNoClear;
00103     }
00104 
00105 #if MBED_CONF_RTOS_PRESENT
00106     return osEventFlagsWait(_id, flags, opt, millisec);
00107 #else
00108     rtos::internal::flags_check_capture check;
00109     check.flags = &_flags;
00110     check.options = opt;
00111     check.flags_wanted = flags;
00112     check.result = 0;
00113     check.match = false;
00114     mbed::internal::do_timed_sleep_relative_or_forever(millisec, rtos::internal::non_rtos_check_flags, &check);
00115     if (check.match) {
00116         return check.result;
00117     } else if (millisec == 0) {
00118         return osErrorResource;
00119     } else {
00120         return osErrorTimeout;
00121     }
00122 #endif
00123 }
00124 
00125 }