diff --git a/UNITTESTS/CMakeLists.txt b/UNITTESTS/CMakeLists.txt index 4116426..875f024 100644 --- a/UNITTESTS/CMakeLists.txt +++ b/UNITTESTS/CMakeLists.txt @@ -86,6 +86,9 @@ # UNIT TESTS #################### +set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DUNITTEST") +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DUNITTEST") + # Set include dirs. set(unittest-includes-base "${PROJECT_SOURCE_DIR}/target_h" diff --git a/UNITTESTS/target_h/mbed_rtos1_types.h b/UNITTESTS/target_h/mbed_rtos1_types.h deleted file mode 100644 index 4ff0189..0000000 --- a/UNITTESTS/target_h/mbed_rtos1_types.h +++ /dev/null @@ -1,24 +0,0 @@ -/* - * Copyright (c) 2017, Arm Limited and affiliates. - * 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. - */ - -#ifndef MBED_RTOS1_TYPES_H -#define MBED_RTOS1_TYPES_H - -#include "cmsis_os.h" -#include "mbed_rtos_storage.h" - -#endif // MBED_RTOS1_TYPES_H diff --git a/UNITTESTS/target_h/mbed_rtos_storage.h b/UNITTESTS/target_h/mbed_rtos_storage.h deleted file mode 100644 index e1b1276..0000000 --- a/UNITTESTS/target_h/mbed_rtos_storage.h +++ /dev/null @@ -1,38 +0,0 @@ -/* - * Copyright (c) 2017, Arm Limited and affiliates. - * 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. - */ - -#ifndef MBED_RTOS_STORAGE_H -#define MBED_RTOS_STORAGE_H - -#ifdef __cplusplus -extern "C" { -#endif - -#include "cmsis_os2.h" -#include "rtx_os.h" -#include "rtx_lib.h" -#include "mbed_rtx_conf.h" - -typedef os_semaphore_t mbed_rtos_storage_semaphore_t; -typedef os_thread_t mbed_rtos_storage_thread_t; -typedef osRtxEventFlags_t mbed_rtos_storage_event_flags_t; - -#ifdef __cplusplus -} -#endif - -#endif diff --git a/UNITTESTS/target_h/rtos.h b/UNITTESTS/target_h/rtos.h deleted file mode 100644 index b624678..0000000 --- a/UNITTESTS/target_h/rtos.h +++ /dev/null @@ -1,26 +0,0 @@ -/* - * Copyright (c) 2018, Arm Limited and affiliates. - * 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. - */ - -#ifndef RTOS_H -#define RTOS_H - -typedef enum { - osTimerOnce = 0, ///< One-shot timer. - osTimerPeriodic = 1 ///< Repeating timer. -} os_timer_type; - -#endif /* RTOS_H */ diff --git a/UNITTESTS/target_h/rtos/Mutex.h b/UNITTESTS/target_h/rtos/Mutex.h index 4f38c37..5a14ed4 100644 --- a/UNITTESTS/target_h/rtos/Mutex.h +++ b/UNITTESTS/target_h/rtos/Mutex.h @@ -18,7 +18,8 @@ #define __MUTEX_H__ #include -#include "cmsis_os2.h" +#include "mbed_rtos_types.h" +#include "mbed_rtos1_types.h" namespace rtos { diff --git a/UNITTESTS/target_h/rtos/mbed_rtos1_types.h b/UNITTESTS/target_h/rtos/mbed_rtos1_types.h new file mode 100644 index 0000000..4ff0189 --- /dev/null +++ b/UNITTESTS/target_h/rtos/mbed_rtos1_types.h @@ -0,0 +1,24 @@ +/* + * Copyright (c) 2017, Arm Limited and affiliates. + * 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. + */ + +#ifndef MBED_RTOS1_TYPES_H +#define MBED_RTOS1_TYPES_H + +#include "cmsis_os.h" +#include "mbed_rtos_storage.h" + +#endif // MBED_RTOS1_TYPES_H diff --git a/UNITTESTS/target_h/rtos/mbed_rtos_storage.h b/UNITTESTS/target_h/rtos/mbed_rtos_storage.h new file mode 100644 index 0000000..e1b1276 --- /dev/null +++ b/UNITTESTS/target_h/rtos/mbed_rtos_storage.h @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2017, Arm Limited and affiliates. + * 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. + */ + +#ifndef MBED_RTOS_STORAGE_H +#define MBED_RTOS_STORAGE_H + +#ifdef __cplusplus +extern "C" { +#endif + +#include "cmsis_os2.h" +#include "rtx_os.h" +#include "rtx_lib.h" +#include "mbed_rtx_conf.h" + +typedef os_semaphore_t mbed_rtos_storage_semaphore_t; +typedef os_thread_t mbed_rtos_storage_thread_t; +typedef osRtxEventFlags_t mbed_rtos_storage_event_flags_t; + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/UNITTESTS/target_h/rtos/rtos.h b/UNITTESTS/target_h/rtos/rtos.h new file mode 100644 index 0000000..b624678 --- /dev/null +++ b/UNITTESTS/target_h/rtos/rtos.h @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2018, Arm Limited and affiliates. + * 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. + */ + +#ifndef RTOS_H +#define RTOS_H + +typedef enum { + osTimerOnce = 0, ///< One-shot timer. + osTimerPeriodic = 1 ///< Repeating timer. +} os_timer_type; + +#endif /* RTOS_H */ diff --git a/mbed.h b/mbed.h index e59813d..4f6f6f6 100644 --- a/mbed.h +++ b/mbed.h @@ -18,7 +18,7 @@ #include "platform/mbed_version.h" -#if MBED_CONF_RTOS_PRESENT +#if MBED_CONF_RTOS_API_PRESENT #include "rtos/rtos.h" #endif diff --git a/rtos/ConditionVariable.cpp b/rtos/ConditionVariable.cpp index cb3bf80..e1087fe 100644 --- a/rtos/ConditionVariable.cpp +++ b/rtos/ConditionVariable.cpp @@ -26,6 +26,8 @@ #include "mbed_error.h" #include "mbed_assert.h" +#if MBED_CONF_RTOS_PRESENT + namespace rtos { ConditionVariable::Waiter::Waiter(): sem(0), prev(NULL), next(NULL), in_list(false) @@ -150,3 +152,5 @@ } } + +#endif diff --git a/rtos/ConditionVariable.h b/rtos/ConditionVariable.h index 543efc7..35ecbf5 100644 --- a/rtos/ConditionVariable.h +++ b/rtos/ConditionVariable.h @@ -23,12 +23,14 @@ #define CONDITIONVARIABLE_H #include -#include "cmsis_os.h" +#include "rtos/mbed_rtos_types.h" #include "rtos/Mutex.h" #include "rtos/Semaphore.h" #include "platform/NonCopyable.h" +#if MBED_CONF_RTOS_PRESENT || defined(DOXYGEN_ONLY) + namespace rtos { /** \addtogroup rtos */ /** @{*/ @@ -328,4 +330,6 @@ } #endif +#endif + /** @}*/ diff --git a/rtos/EventFlags.cpp b/rtos/EventFlags.cpp index 1167d76..5382ff9 100644 --- a/rtos/EventFlags.cpp +++ b/rtos/EventFlags.cpp @@ -20,6 +20,8 @@ * SOFTWARE. */ #include "rtos/EventFlags.h" +#include "rtos/ThisThread.h" +#include "mbed_os_timer.h" #include #include "mbed_error.h" #include "mbed_assert.h" @@ -38,27 +40,43 @@ void EventFlags::constructor(const char *name) { +#if MBED_CONF_RTOS_PRESENT osEventFlagsAttr_t attr = { 0 }; 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); +#else + _flags = 0; +#endif } uint32_t EventFlags::set(uint32_t flags) { +#if MBED_CONF_RTOS_PRESENT return osEventFlagsSet(_id, flags); +#else + return core_util_atomic_fetch_or_u32(&_flags, flags) | flags; +#endif } uint32_t EventFlags::clear(uint32_t flags) { +#if MBED_CONF_RTOS_PRESENT return osEventFlagsClear(_id, flags); +#else + return core_util_atomic_fetch_and_u32(&_flags, ~flags); +#endif } uint32_t EventFlags::get() const { +#if MBED_CONF_RTOS_PRESENT return osEventFlagsGet(_id); +#else + return core_util_atomic_load_u32(&_flags); +#endif } uint32_t EventFlags::wait_all(uint32_t flags, uint32_t millisec, bool clear) @@ -73,7 +91,9 @@ EventFlags::~EventFlags() { +#if MBED_CONF_RTOS_PRESENT osEventFlagsDelete(_id); +#endif } uint32_t EventFlags::wait(uint32_t flags, uint32_t opt, uint32_t millisec, bool clear) @@ -82,7 +102,24 @@ opt |= osFlagsNoClear; } +#if MBED_CONF_RTOS_PRESENT return osEventFlagsWait(_id, flags, opt, millisec); +#else + rtos::internal::flags_check_capture check; + check.flags = &_flags; + check.options = opt; + check.flags_wanted = flags; + check.result = 0; + check.match = false; + mbed::internal::do_timed_sleep_relative_or_forever(millisec, rtos::internal::non_rtos_check_flags, &check); + if (check.match) { + return check.result; + } else if (millisec == 0) { + return osErrorResource; + } else { + return osErrorTimeout; + } +#endif } } diff --git a/rtos/EventFlags.h b/rtos/EventFlags.h index bac58ea..0c2798d 100644 --- a/rtos/EventFlags.h +++ b/rtos/EventFlags.h @@ -23,9 +23,9 @@ #define EVENT_FLAG_H #include -#include "cmsis_os2.h" -#include "mbed_rtos1_types.h" -#include "mbed_rtos_storage.h" +#include "rtos/mbed_rtos_types.h" +#include "rtos/mbed_rtos1_types.h" +#include "rtos/mbed_rtos_storage.h" #include "platform/NonCopyable.h" @@ -114,8 +114,12 @@ private: void constructor(const char *name = NULL); uint32_t wait(uint32_t flags, uint32_t opt, uint32_t millisec, bool clear); +#if MBED_CONF_RTOS_PRESENT osEventFlagsId_t _id; mbed_rtos_storage_event_flags_t _obj_mem; +#else + uint32_t _flags; +#endif }; /** @}*/ diff --git a/rtos/Kernel.cpp b/rtos/Kernel.cpp index a620c0c..4751bee 100644 --- a/rtos/Kernel.cpp +++ b/rtos/Kernel.cpp @@ -20,16 +20,17 @@ * SOFTWARE. */ -#include "cmsis_os2.h" #include "rtos/Kernel.h" #include "rtos/rtos_idle.h" #include "rtos/rtos_handlers.h" #include "platform/mbed_critical.h" +#include "platform/mbed_os_timer.h" namespace rtos { uint64_t Kernel::get_ms_count() { +#if MBED_CONF_RTOS_PRESENT // CMSIS-RTOS 2.1.0 and 2.1.1 differ in the time type. We assume // our header at least matches the implementation, so we don't try looking // at the run-time version report. (There's no compile-time version report) @@ -61,8 +62,12 @@ core_util_critical_section_exit(); return ret; } +#else + return mbed::internal::init_os_timer()->update_and_get_tick(); +#endif } +#if MBED_CONF_RTOS_PRESENT void Kernel::attach_idle_hook(void (*fptr)(void)) { rtos_attach_idle_hook(fptr); @@ -72,5 +77,6 @@ { rtos_attach_thread_terminate_hook(fptr); } +#endif } diff --git a/rtos/Kernel.h b/rtos/Kernel.h index 851dfb6..5a73aad 100644 --- a/rtos/Kernel.h +++ b/rtos/Kernel.h @@ -23,7 +23,7 @@ #define KERNEL_H #include -#include "cmsis_os2.h" +#include "rtos/mbed_rtos_types.h" namespace rtos { /** \addtogroup rtos */ diff --git a/rtos/LICENSE.md b/rtos/LICENSE.md index a99cc40..01cb36e 100644 --- a/rtos/LICENSE.md +++ b/rtos/LICENSE.md @@ -3,7 +3,6 @@ Files licensed under MIT: -- TARGET_CORTEX/mbed_rtos1_types.h - TARGET_CORTEX/mbed_rtos_storage.h - TARGET_CORTEX/mbed_rtx_conf.h - TARGET_CORTEX/mbed_rtx_idle.cpp diff --git a/rtos/Mail.h b/rtos/Mail.h index dd10509..85607dc 100644 --- a/rtos/Mail.h +++ b/rtos/Mail.h @@ -25,11 +25,11 @@ #include #include -#include "Queue.h" -#include "MemoryPool.h" -#include "cmsis_os2.h" -#include "mbed_rtos_storage.h" -#include "mbed_rtos1_types.h" +#include "rtos/Queue.h" +#include "rtos/MemoryPool.h" +#include "rtos/mbed_rtos_types.h" +#include "rtos/mbed_rtos_storage.h" +#include "rtos/mbed_rtos1_types.h" #include "platform/mbed_toolchain.h" #include "platform/NonCopyable.h" @@ -38,6 +38,8 @@ using namespace rtos; #endif +#if MBED_CONF_RTOS_PRESENT || defined(DOXYGEN_ONLY) + namespace rtos { /** \addtogroup rtos */ /** @{*/ @@ -238,5 +240,5 @@ #endif - +#endif diff --git a/rtos/MemoryPool.h b/rtos/MemoryPool.h index 431b5d6..6764d12 100644 --- a/rtos/MemoryPool.h +++ b/rtos/MemoryPool.h @@ -25,11 +25,12 @@ #include #include -#include "cmsis_os2.h" -#include "mbed_rtos1_types.h" -#include "mbed_rtos_storage.h" +#include "rtos/mbed_rtos_types.h" +#include "rtos/mbed_rtos1_types.h" +#include "rtos/mbed_rtos_storage.h" #include "platform/NonCopyable.h" +#if MBED_CONF_RTOS_PRESENT || defined(DOXYGEN_ONLY) namespace rtos { /** \addtogroup rtos */ /** @{*/ @@ -191,3 +192,4 @@ } #endif +#endif diff --git a/rtos/Mutex.cpp b/rtos/Mutex.cpp index 24f3085..8316168 100644 --- a/rtos/Mutex.cpp +++ b/rtos/Mutex.cpp @@ -26,9 +26,11 @@ #include "mbed_error.h" #include "mbed_assert.h" +#if MBED_CONF_RTOS_PRESENT + namespace rtos { -Mutex::Mutex(): _count(0) +Mutex::Mutex() { constructor(); } @@ -40,6 +42,7 @@ void Mutex::constructor(const char *name) { + _count = 0; osMutexAttr_t attr = { 0 }; attr.name = name ? name : "application_unnamed_mutex"; @@ -147,3 +150,5 @@ } } + +#endif diff --git a/rtos/Mutex.h b/rtos/Mutex.h index a4955a5..aeddf02 100644 --- a/rtos/Mutex.h +++ b/rtos/Mutex.h @@ -23,9 +23,9 @@ #define MUTEX_H #include -#include "cmsis_os2.h" -#include "mbed_rtos1_types.h" -#include "mbed_rtos_storage.h" +#include "rtos/mbed_rtos_types.h" +#include "rtos/mbed_rtos1_types.h" +#include "rtos/mbed_rtos_storage.h" #include "platform/NonCopyable.h" #include "platform/ScopedLock.h" @@ -56,6 +56,8 @@ /** The Mutex class is used to synchronize the execution of threads. This is, for example, used to protect access to a shared resource. + In bare-metal builds, the Mutex class is a dummy, so lock() and unlock() are no-ops. + @note You cannot use member functions of this class in ISR context. If you require Mutex functionality within ISR handler, consider using @a Semaphore. @@ -88,7 +90,11 @@ @note This function treats RTOS errors as fatal system errors, so it can only return osOK. Use of the return value is deprecated, as the return is expected to become void in the future. */ - osStatus lock(void); +#if MBED_CONF_RTOS_PRESENT + osStatus lock(); +#else + void lock(); // Value return backwards compatibility not required for non-RTOS +#endif /** Wait until a Mutex becomes available. @@ -150,14 +156,18 @@ @note This function treats RTOS errors as fatal system errors, so it can only return osOK. Use of the return value is deprecated, as the return is expected to become void in the future. */ +#if MBED_CONF_RTOS_PRESENT osStatus unlock(); +#else + void unlock(); // Value return backwards compatibility not required for non-RTOS +#endif /** Get the owner the this mutex @return the current owner of this mutex. @note You cannot call this function from ISR context. */ - osThreadId get_owner(); + osThreadId_t get_owner(); /** Mutex destructor * @@ -166,13 +176,53 @@ ~Mutex(); private: +#if MBED_CONF_RTOS_PRESENT void constructor(const char *name = NULL); friend class ConditionVariable; osMutexId_t _id; mbed_rtos_storage_mutex_t _obj_mem; uint32_t _count; +#endif }; + +#if !MBED_CONF_RTOS_PRESENT +inline Mutex::Mutex() +{ +} + +inline Mutex::Mutex(const char *) +{ +} + +inline Mutex::~Mutex() +{ +} + +inline void Mutex::lock() +{ +} + +inline bool Mutex::trylock() +{ + return true; +} + +inline bool Mutex::trylock_for(uint32_t) +{ + return true; +} + +inline bool Mutex::trylock_until(uint64_t) +{ + return true; +} + +inline void Mutex::unlock() +{ +} +#endif + /** @}*/ /** @}*/ } diff --git a/rtos/Queue.h b/rtos/Queue.h index e31fb61..1ce8eb3 100644 --- a/rtos/Queue.h +++ b/rtos/Queue.h @@ -22,12 +22,14 @@ #ifndef QUEUE_H #define QUEUE_H -#include "cmsis_os2.h" -#include "mbed_rtos1_types.h" -#include "mbed_rtos_storage.h" +#include "rtos/mbed_rtos_types.h" +#include "rtos/mbed_rtos1_types.h" +#include "rtos/mbed_rtos_storage.h" #include "platform/mbed_error.h" #include "platform/NonCopyable.h" +#if MBED_CONF_RTOS_PRESENT || defined(DOXYGEN_ONLY) + namespace rtos { /** \addtogroup rtos */ /** @{*/ @@ -219,4 +221,6 @@ } // namespace rtos +#endif + #endif // QUEUE_H diff --git a/rtos/RtosTimer.cpp b/rtos/RtosTimer.cpp index 4639da0..4779f32 100644 --- a/rtos/RtosTimer.cpp +++ b/rtos/RtosTimer.cpp @@ -26,6 +26,8 @@ #include +#if MBED_CONF_RTOS_PRESENT + namespace rtos { void RtosTimer::constructor(mbed::Callback func, os_timer_type type) @@ -54,3 +56,5 @@ } } + +#endif diff --git a/rtos/RtosTimer.h b/rtos/RtosTimer.h index eb976cd..1686872 100644 --- a/rtos/RtosTimer.h +++ b/rtos/RtosTimer.h @@ -23,12 +23,14 @@ #define RTOS_TIMER_H #include -#include "cmsis_os2.h" -#include "mbed_rtos_storage.h" +#include "rtos/mbed_rtos_types.h" +#include "rtos/mbed_rtos_storage.h" #include "platform/Callback.h" #include "platform/NonCopyable.h" #include "platform/mbed_toolchain.h" -#include "mbed_rtos1_types.h" +#include "rtos/mbed_rtos1_types.h" + +#if MBED_CONF_RTOS_PRESENT || defined(DOXYGEN_ONLY) namespace rtos { /** \addtogroup rtos */ @@ -188,4 +190,4 @@ #endif - +#endif diff --git a/rtos/Semaphore.cpp b/rtos/Semaphore.cpp index 0c69c00..6887ac2 100644 --- a/rtos/Semaphore.cpp +++ b/rtos/Semaphore.cpp @@ -22,7 +22,9 @@ #include "rtos/Semaphore.h" #include "rtos/Kernel.h" #include "platform/mbed_assert.h" +#include "platform/mbed_critical.h" #include "platform/mbed_error.h" +#include "platform/mbed_os_timer.h" #include @@ -40,22 +42,51 @@ void Semaphore::constructor(int32_t count, uint16_t max_count) { +#if MBED_CONF_RTOS_PRESENT osSemaphoreAttr_t attr = { 0 }; attr.cb_mem = &_obj_mem; attr.cb_size = sizeof(_obj_mem); _id = osSemaphoreNew(max_count, count, &attr); MBED_ASSERT(_id != NULL); +#else + _count = count; + _max_count = max_count; +#endif } +#if !MBED_CONF_RTOS_PRESENT +struct sem_wait_capture { + Semaphore *sem; + bool acquired; +}; + +bool Semaphore::semaphore_available(void *handle) +{ + sem_wait_capture *wait = static_cast(handle); + return wait->acquired = wait->sem->try_acquire(); +} +#endif + bool Semaphore::try_acquire() { +#if MBED_CONF_RTOS_PRESENT osStatus_t status = osSemaphoreAcquire(_id, 0); if (status != osOK && status != osErrorResource) { MBED_ERROR1(MBED_MAKE_ERROR(MBED_MODULE_KERNEL, MBED_ERROR_CODE_SEMAPHORE_LOCK_FAILED), "Semaphore acquire failed", status); } return status == osOK; +#else + int32_t old_count = core_util_atomic_load_s32(&_count); + do { + if (old_count == 0) { + return false; + } + } while (!core_util_atomic_cas_s32(&_count, &old_count, old_count - 1)); + return true; +#endif } +#if MBED_CONF_RTOS_PRESENT /* To sidestep deprecation warnings */ int32_t Semaphore::_wait(uint32_t millisec) { @@ -71,14 +102,26 @@ return -1; } } +#endif int32_t Semaphore::wait(uint32_t millisec) { +#if MBED_CONF_RTOS_PRESENT return _wait(millisec); +#else + sem_wait_capture capture = { this, false }; + mbed::internal::do_timed_sleep_relative_or_forever(millisec, semaphore_available, &capture); + if (capture.acquired) { + return core_util_atomic_load_s32(&_count) + 1; + } else { + return 0; + } +#endif } int32_t Semaphore::wait_until(uint64_t millisec) { +#if MBED_CONF_RTOS_PRESENT uint64_t now = Kernel::get_ms_count(); if (now >= millisec) { @@ -89,18 +132,36 @@ } else { return _wait(millisec - now); } +#else + sem_wait_capture capture = { this, false }; + mbed::internal::do_timed_sleep_absolute(millisec, semaphore_available, &capture); + if (capture.acquired) { + return core_util_atomic_load_s32(&_count) + 1; + } else { + return 0; + } +#endif } void Semaphore::acquire() { +#if MBED_CONF_RTOS_PRESENT osStatus_t status = osSemaphoreAcquire(_id, osWaitForever); if (status != osOK) { MBED_ERROR1(MBED_MAKE_ERROR(MBED_MODULE_KERNEL, MBED_ERROR_CODE_SEMAPHORE_LOCK_FAILED), "Semaphore acquire failed", status); } +#else + sem_wait_capture capture = { this, false }; + mbed::internal::do_untimed_sleep(semaphore_available, &capture); + if (!capture.acquired) { + MBED_ERROR(MBED_MAKE_ERROR(MBED_MODULE_KERNEL, MBED_ERROR_CODE_SEMAPHORE_LOCK_FAILED), "Semaphore acquire failed"); + } +#endif } bool Semaphore::try_acquire_for(uint32_t millisec) { +#if MBED_CONF_RTOS_PRESENT osStatus_t status = osSemaphoreAcquire(_id, millisec); if (status == osOK) { return true; @@ -113,10 +174,16 @@ MBED_ERROR1(MBED_MAKE_ERROR(MBED_MODULE_KERNEL, MBED_ERROR_CODE_SEMAPHORE_LOCK_FAILED), "Semaphore acquire failed", status); } return false; +#else + sem_wait_capture capture = { this, false }; + mbed::internal::do_timed_sleep_relative_or_forever(millisec, semaphore_available, &capture); + return capture.acquired; +#endif } bool Semaphore::try_acquire_until(uint64_t millisec) { +#if MBED_CONF_RTOS_PRESENT uint64_t now = Kernel::get_ms_count(); if (now >= millisec) { @@ -127,16 +194,33 @@ } else { return try_acquire_for(millisec - now); } +#else + sem_wait_capture capture = { this, false }; + mbed::internal::do_timed_sleep_absolute(millisec, semaphore_available, &capture); + return capture.acquired; +#endif } osStatus Semaphore::release(void) { +#if MBED_CONF_RTOS_PRESENT return osSemaphoreRelease(_id); +#else + int32_t old_count = core_util_atomic_load_s32(&_count); + do { + if (old_count == _max_count) { + return osErrorResource; + } + } while (!core_util_atomic_cas_s32(&_count, &old_count, old_count + 1)); +#endif + return osOK; } Semaphore::~Semaphore() { +#if MBED_CONF_RTOS_PRESENT osSemaphoreDelete(_id); +#endif } } diff --git a/rtos/Semaphore.h b/rtos/Semaphore.h index 9eaa116..8b89e7b 100644 --- a/rtos/Semaphore.h +++ b/rtos/Semaphore.h @@ -23,9 +23,9 @@ #define SEMAPHORE_H #include -#include "cmsis_os2.h" -#include "mbed_rtos1_types.h" -#include "mbed_rtos_storage.h" +#include "rtos/mbed_rtos_types.h" +#include "rtos/mbed_rtos1_types.h" +#include "rtos/mbed_rtos_storage.h" #include "platform/mbed_toolchain.h" #include "platform/NonCopyable.h" @@ -141,10 +141,16 @@ private: void constructor(int32_t count, uint16_t max_count); +#if MBED_CONF_RTOS_PRESENT int32_t _wait(uint32_t millisec); osSemaphoreId_t _id; mbed_rtos_storage_semaphore_t _obj_mem; +#else + static bool semaphore_available(void *); + int32_t _count; + uint16_t _max_count; +#endif }; /** @}*/ /** @}*/ diff --git a/rtos/TARGET_CORTEX/mbed_lib.json b/rtos/TARGET_CORTEX/mbed_lib.json new file mode 100644 index 0000000..aed253a --- /dev/null +++ b/rtos/TARGET_CORTEX/mbed_lib.json @@ -0,0 +1,57 @@ +{ + "name": "rtos", + "config": { + "present": 1, + "main-thread-stack-size": { + "help": "The size of the main thread's stack", + "value": 4096 + }, + "timer-thread-stack-size": { + "help": "The size of the timer thread's stack", + "value": 768 + }, + "idle-thread-stack-size": { + "help": "The size of the idle thread's stack", + "value": 512 + }, + "thread-stack-size": { + "help": "The default stack size of new threads", + "value": 4096 + }, + "idle-thread-stack-size-tickless-extra": { + "help": "Additional size to add to the idle thread when a specific target or application implementation requires it or in case tickless is enabled and LPTICKER_DELAY_TICKS is used", + "value": 256 + }, + "idle-thread-stack-size-debug-extra": { + "help": "Additional size to add to the idle thread when code compilation optimisation is disabled", + "value": 0 + } + }, + "macros": ["_RTE_"], + "target_overrides": { + "*": { + "target.boot-stack-size": "0x400" + }, + "STM": { + "idle-thread-stack-size-debug-extra": 128 + }, + "STM32L1": { + "idle-thread-stack-size-debug-extra": 512 + }, + "MCU_NRF51": { + "target.boot-stack-size": "0x800" + }, + "MCU_NRF52840": { + "target.boot-stack-size": "0x800" + }, + "MCU_NRF52832": { + "target.boot-stack-size": "0x800" + }, + "MCU_NRF51_UNIFIED": { + "target.boot-stack-size": "0x800" + }, + "NUVOTON": { + "idle-thread-stack-size-debug-extra": 512 + } + } +} diff --git a/rtos/TARGET_CORTEX/mbed_rtos1_types.h b/rtos/TARGET_CORTEX/mbed_rtos1_types.h deleted file mode 100644 index e44fa25..0000000 --- a/rtos/TARGET_CORTEX/mbed_rtos1_types.h +++ /dev/null @@ -1,28 +0,0 @@ -/* mbed Microcontroller Library - * Copyright (c) 2006-2017 ARM Limited - * SPDX-License-Identifier: Apache-2.0 - * - * 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 MBED_RTOS_RTX1_TYPES_H -#define MBED_RTOS_RTX1_TYPES_H - -#include "rtx4/cmsis_os.h" - -#endif diff --git a/rtos/TARGET_CORTEX/mbed_rtos_storage.h b/rtos/TARGET_CORTEX/mbed_rtos_storage.h deleted file mode 100644 index 2a0338b..0000000 --- a/rtos/TARGET_CORTEX/mbed_rtos_storage.h +++ /dev/null @@ -1,64 +0,0 @@ -/* 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 MBED_RTOS_STORAGE_H -#define MBED_RTOS_STORAGE_H - -#ifdef __cplusplus -extern "C" { -#endif - -/** \addtogroup rtos */ -/** @{*/ - -/** @brief RTOS primitives storage types for RTX - - Types defined in this file should be utilized, when the direct RTOS C API usage is required, to provide backing memory - for internal RTX data. Allocated object should be wrapped in attribute struct and passed to os*New call, for details - see CMSIS-RTOS2 documentation. - - @note - This file breaks abstraction layers and uses RTX internal types, but it limits the contamination to single, RTOS - implementation specific, header file, therefore limiting scope of possible changes. - */ - -#include "rtx_os.h" -#include "mbed_rtx_conf.h" - -typedef osRtxMutex_t mbed_rtos_storage_mutex_t; -typedef osRtxSemaphore_t mbed_rtos_storage_semaphore_t; -typedef osRtxThread_t mbed_rtos_storage_thread_t; -typedef osRtxMemoryPool_t mbed_rtos_storage_mem_pool_t; -typedef osRtxMessageQueue_t mbed_rtos_storage_msg_queue_t; -typedef osRtxEventFlags_t mbed_rtos_storage_event_flags_t; -typedef osRtxMessage_t mbed_rtos_storage_message_t; -typedef osRtxTimer_t mbed_rtos_storage_timer_t; - -#define MBED_RTOS_STORAGE_MEM_POOL_MEM_SIZE(block_count, block_size) \ - osRtxMemoryPoolMemSize(block_count, block_size) - -#ifdef __cplusplus -} -#endif - -#endif - -/** @}*/ diff --git a/rtos/TARGET_CORTEX/mbed_rtx_storage.h b/rtos/TARGET_CORTEX/mbed_rtx_storage.h new file mode 100644 index 0000000..d14f78a --- /dev/null +++ b/rtos/TARGET_CORTEX/mbed_rtx_storage.h @@ -0,0 +1,64 @@ +/* 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 MBED_RTX_STORAGE_H +#define MBED_RTX_STORAGE_H + +#ifdef __cplusplus +extern "C" { +#endif + +/** \addtogroup rtos */ +/** @{*/ + +/** @brief RTOS primitives storage types for RTX + + Types defined in this file should be utilized, when the direct RTOS C API usage is required, to provide backing memory + for internal RTX data. Allocated object should be wrapped in attribute struct and passed to os*New call, for details + see CMSIS-RTOS2 documentation. + + @note + This file breaks abstraction layers and uses RTX internal types, but it limits the contamination to single, RTOS + implementation specific, header file, therefore limiting scope of possible changes. + */ + +#include "rtx_os.h" +#include "mbed_rtx_conf.h" + +typedef osRtxMutex_t mbed_rtos_storage_mutex_t; +typedef osRtxSemaphore_t mbed_rtos_storage_semaphore_t; +typedef osRtxThread_t mbed_rtos_storage_thread_t; +typedef osRtxMemoryPool_t mbed_rtos_storage_mem_pool_t; +typedef osRtxMessageQueue_t mbed_rtos_storage_msg_queue_t; +typedef osRtxEventFlags_t mbed_rtos_storage_event_flags_t; +typedef osRtxMessage_t mbed_rtos_storage_message_t; +typedef osRtxTimer_t mbed_rtos_storage_timer_t; + +#define MBED_RTOS_STORAGE_MEM_POOL_MEM_SIZE(block_count, block_size) \ + osRtxMemoryPoolMemSize(block_count, block_size) + +#ifdef __cplusplus +} +#endif + +#endif + +/** @}*/ diff --git a/rtos/ThisThread.cpp b/rtos/ThisThread.cpp index 0e3b89d..cd8af4e 100644 --- a/rtos/ThisThread.cpp +++ b/rtos/ThisThread.cpp @@ -23,39 +23,119 @@ #define __STDC_LIMIT_MACROS #include "rtos/ThisThread.h" +#include "platform/mbed_toolchain.h" #include "rtos/Kernel.h" -#include "rtos/rtos_idle.h" +#include "platform/CriticalSectionLock.h" #include "platform/mbed_assert.h" +#include "platform/mbed_critical.h" +#include "platform/mbed_os_timer.h" + +#if !MBED_CONF_RTOS_PRESENT +static uint32_t thread_flags; + +/* For the flags to be useful, need a way of setting them, but there's only the main + * thread, and that has no Thread object, so Thread class is not provided. Implement + * one CMSIS-RTOS function to provide access. + */ +extern "C" +uint32_t osThreadFlagsSet(osThreadId_t /*thread_id*/, uint32_t flags) +{ + return core_util_atomic_fetch_or_u32(&thread_flags, flags) | flags; +} +#endif namespace rtos { uint32_t ThisThread::flags_clear(uint32_t flags) { +#if MBED_CONF_RTOS_PRESENT flags = osThreadFlagsClear(flags); MBED_ASSERT(!(flags & osFlagsError)); +#else + flags = core_util_atomic_fetch_and_u32(&thread_flags, ~flags); +#endif return flags; } uint32_t ThisThread::flags_get() { +#if MBED_CONF_RTOS_PRESENT return osThreadFlagsGet(); +#else + return core_util_atomic_load_u32(&thread_flags); +#endif } +#if !MBED_CONF_RTOS_PRESENT +namespace internal { +bool non_rtos_check_flags(void *handle) +{ + flags_check_capture *check = static_cast(handle); + uint32_t cur_flags = core_util_atomic_load_u32(check->flags); + uint32_t set_flags; + do { + set_flags = cur_flags & check->flags_wanted; + check->result = set_flags; + if ((check->options & osFlagsWaitAll) ? set_flags == check->flags_wanted : set_flags != 0) { + if (check->options & osFlagsNoClear) { + break; + } + } else { + return false; + } + } while (!core_util_atomic_cas_u32(check->flags, &cur_flags, cur_flags & ~set_flags)); + check->match = true; + return true; +} +} +#endif + static uint32_t flags_wait_for(uint32_t flags, uint32_t millisec, bool clear, uint32_t options) { if (!clear) { options |= osFlagsNoClear; } +#if MBED_CONF_RTOS_PRESENT flags = osThreadFlagsWait(flags, options, millisec); if (flags & osFlagsError) { MBED_ASSERT((flags == osFlagsErrorTimeout && millisec != osWaitForever) || (flags == osFlagsErrorResource && millisec == 0)); flags = ThisThread::flags_get(); } +#else + rtos::internal::flags_check_capture check; + check.flags = &thread_flags; + check.options = options; + check.flags_wanted = flags; + check.result = 0; + mbed::internal::do_timed_sleep_relative_or_forever(millisec, rtos::internal::non_rtos_check_flags, &check); + flags = check.result; +#endif return flags; } +static uint32_t flags_wait(uint32_t flags, bool clear, uint32_t options) +{ +#if MBED_CONF_RTOS_PRESENT + return flags_wait_for(flags, osWaitForever, clear, options); +#else + /* Avoids pulling in timer if not used */ + if (!clear) { + options |= osFlagsNoClear; + } + rtos::internal::flags_check_capture check; + check.flags = &thread_flags; + check.options = options; + check.flags_wanted = flags; + check.result = 0; + mbed::internal::do_untimed_sleep(rtos::internal::non_rtos_check_flags, &check); + flags = check.result; + + return flags; +#endif +} + static uint32_t flags_wait_until(uint32_t flags, uint64_t millisec, bool clear, uint32_t options) { uint64_t now = Kernel::get_ms_count(); @@ -74,7 +154,7 @@ uint32_t ThisThread::flags_wait_all(uint32_t flags, bool clear) { - return flags_wait_for(flags, osWaitForever, clear, osFlagsWaitAll); + return flags_wait(flags, clear, osFlagsWaitAll); } uint32_t ThisThread::flags_wait_all_for(uint32_t flags, uint32_t millisec, bool clear) @@ -89,7 +169,7 @@ uint32_t ThisThread::flags_wait_any(uint32_t flags, bool clear) { - return flags_wait_for(flags, osWaitForever, clear, osFlagsWaitAny); + return flags_wait(flags, clear, osFlagsWaitAny); } uint32_t ThisThread::flags_wait_any_for(uint32_t flags, uint32_t millisec, bool clear) @@ -104,12 +184,18 @@ void ThisThread::sleep_for(uint32_t millisec) { +#if MBED_CONF_RTOS_PRESENT osStatus_t status = osDelay(millisec); MBED_ASSERT(status == osOK); +#else + // Undocumented, but osDelay(UINT32_MAX) does actually sleep forever + mbed::internal::do_timed_sleep_relative_or_forever(millisec); +#endif } void ThisThread::sleep_until(uint64_t millisec) { +#if MBED_CONF_RTOS_PRESENT // CMSIS-RTOS 2.1.0 had 64-bit time and osDelayUntil, but that's been revoked. // Limit ourselves to manual implementation assuming a >=32-bit osDelay. @@ -126,25 +212,42 @@ break; } } +#else + mbed::internal::do_timed_sleep_absolute(millisec); +#endif } void ThisThread::yield() { +#if MBED_CONF_RTOS_PRESENT osThreadYield(); +#else + asm("yield"); +#endif } osThreadId_t ThisThread::get_id() { +#if MBED_CONF_RTOS_PRESENT return osThreadGetId(); +#else + return (osThreadId_t) 1; // dummy non-0 value +#endif } const char *get_name() { +#if MBED_CONF_RTOS_PRESENT osThreadId_t id = osThreadGetId(); if (id == NULL) { return NULL; } return osThreadGetName(id); +#else + return NULL; +#endif } } + + diff --git a/rtos/ThisThread.h b/rtos/ThisThread.h index d566fb5..492ad88 100644 --- a/rtos/ThisThread.h +++ b/rtos/ThisThread.h @@ -23,14 +23,7 @@ #define THIS_THREAD_H #include -#include "cmsis_os2.h" -#include "mbed_rtos1_types.h" -#include "mbed_rtos_storage.h" -#include "platform/Callback.h" -#include "platform/mbed_toolchain.h" -#include "platform/NonCopyable.h" -#include "rtos/Semaphore.h" -#include "rtos/Mutex.h" +#include "rtos/mbed_rtos_types.h" namespace rtos { /** \addtogroup rtos */ @@ -188,6 +181,19 @@ }; /** @}*/ /** @}*/ + +namespace internal { +struct flags_check_capture { + uint32_t *flags; + uint32_t options; + uint32_t flags_wanted; + uint32_t result; + bool match; +}; + +bool non_rtos_check_flags(void *handle); + +} } #endif diff --git a/rtos/Thread.cpp b/rtos/Thread.cpp index 40154af..80827ab 100644 --- a/rtos/Thread.cpp +++ b/rtos/Thread.cpp @@ -26,6 +26,8 @@ #include "platform/mbed_assert.h" #include "platform/mbed_error.h" +#if MBED_CONF_RTOS_PRESENT + #define ALIGN_UP(pos, align) ((pos) % (align) ? (pos) + ((align) - (pos) % (align)) : (pos)) MBED_STATIC_ASSERT(ALIGN_UP(0, 8) == 0, "ALIGN_UP macro error"); MBED_STATIC_ASSERT(ALIGN_UP(1, 8) == 8, "ALIGN_UP macro error"); @@ -432,3 +434,5 @@ } } + +#endif diff --git a/rtos/Thread.h b/rtos/Thread.h index 34da08a..f9e25d6 100644 --- a/rtos/Thread.h +++ b/rtos/Thread.h @@ -23,15 +23,17 @@ #define THREAD_H #include -#include "cmsis_os2.h" -#include "mbed_rtos1_types.h" -#include "mbed_rtos_storage.h" +#include "rtos/mbed_rtos_types.h" +#include "rtos/mbed_rtos1_types.h" +#include "rtos/mbed_rtos_storage.h" #include "platform/Callback.h" #include "platform/mbed_toolchain.h" #include "platform/NonCopyable.h" #include "rtos/Semaphore.h" #include "rtos/Mutex.h" +#if MBED_CONF_RTOS_PRESENT || defined(DOXYGEN_ONLY) || defined(UNITTEST) + namespace rtos { /** \addtogroup rtos */ /** @{*/ @@ -545,4 +547,4 @@ } #endif - +#endif diff --git a/rtos/mbed_lib.json b/rtos/mbed_lib.json index aed253a..43fb3ab 100644 --- a/rtos/mbed_lib.json +++ b/rtos/mbed_lib.json @@ -1,57 +1,6 @@ { - "name": "rtos", - "config": { - "present": 1, - "main-thread-stack-size": { - "help": "The size of the main thread's stack", - "value": 4096 - }, - "timer-thread-stack-size": { - "help": "The size of the timer thread's stack", - "value": 768 - }, - "idle-thread-stack-size": { - "help": "The size of the idle thread's stack", - "value": 512 - }, - "thread-stack-size": { - "help": "The default stack size of new threads", - "value": 4096 - }, - "idle-thread-stack-size-tickless-extra": { - "help": "Additional size to add to the idle thread when a specific target or application implementation requires it or in case tickless is enabled and LPTICKER_DELAY_TICKS is used", - "value": 256 - }, - "idle-thread-stack-size-debug-extra": { - "help": "Additional size to add to the idle thread when code compilation optimisation is disabled", - "value": 0 - } - }, - "macros": ["_RTE_"], - "target_overrides": { - "*": { - "target.boot-stack-size": "0x400" - }, - "STM": { - "idle-thread-stack-size-debug-extra": 128 - }, - "STM32L1": { - "idle-thread-stack-size-debug-extra": 512 - }, - "MCU_NRF51": { - "target.boot-stack-size": "0x800" - }, - "MCU_NRF52840": { - "target.boot-stack-size": "0x800" - }, - "MCU_NRF52832": { - "target.boot-stack-size": "0x800" - }, - "MCU_NRF51_UNIFIED": { - "target.boot-stack-size": "0x800" - }, - "NUVOTON": { - "idle-thread-stack-size-debug-extra": 512 - } - } + "name": "rtos-api", + "config": { + "present": 1 + } } diff --git a/rtos/mbed_rtos1_types.h b/rtos/mbed_rtos1_types.h new file mode 100644 index 0000000..e178b27 --- /dev/null +++ b/rtos/mbed_rtos1_types.h @@ -0,0 +1,30 @@ +/* mbed Microcontroller Library + * Copyright (c) 2006-2019 ARM Limited + * 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. + */ +#ifndef MBED_RTOS_RTX1_TYPES_H +#define MBED_RTOS_RTX1_TYPES_H + +#if MBED_CONF_RTOS_PRESENT || defined(UNITTEST) + +#include "cmsis_os.h" + +#else + +typedef int32_t osStatus; + +#endif + +#endif diff --git a/rtos/mbed_rtos_storage.h b/rtos/mbed_rtos_storage.h new file mode 100644 index 0000000..58eb8f4 --- /dev/null +++ b/rtos/mbed_rtos_storage.h @@ -0,0 +1,28 @@ +/* mbed Microcontroller Library + * Copyright (c) 2006-2019 ARM Limited + * 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. + */ +#ifndef MBED_RTOS_STORAGE_H +#define MBED_RTOS_STORAGE_H + +#if MBED_CONF_RTOS_PRESENT || defined(UNITTEST) + +#include "mbed_rtx_storage.h" + +#endif + +#endif + +/** @}*/ diff --git a/rtos/mbed_rtos_types.h b/rtos/mbed_rtos_types.h new file mode 100644 index 0000000..3584b6d --- /dev/null +++ b/rtos/mbed_rtos_types.h @@ -0,0 +1,76 @@ +/* mbed Microcontroller Library + * Copyright (c) 2006-2019, 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. + */ +#ifndef RTOS_TYPES_H_ +#define RTOS_TYPES_H_ + +#if MBED_CONF_RTOS_PRESENT || defined(DOXYGEN_ONLY) || defined(UNITTEST) +#include "cmsis_os2.h" +#else + +#ifdef __cplusplus +extern "C" { +#endif + +/* Minimal definitions for bare metal form of RTOS */ + +// Timeout value. +#define osWaitForever 0xFFFFFFFFU ///< Wait forever timeout value. + +// Flags options (\ref osThreadFlagsWait and \ref osEventFlagsWait). +#define osFlagsWaitAny 0x00000000U ///< Wait for any flag (default). +#define osFlagsWaitAll 0x00000001U ///< Wait for all flags. +#define osFlagsNoClear 0x00000002U ///< Do not clear flags which have been specified to wait for. + +// Flags errors (returned by osThreadFlagsXxxx and osEventFlagsXxxx). +#define osFlagsError 0x80000000U ///< Error indicator. +#define osFlagsErrorUnknown 0xFFFFFFFFU ///< osError (-1). +#define osFlagsErrorTimeout 0xFFFFFFFEU ///< osErrorTimeout (-2). +#define osFlagsErrorResource 0xFFFFFFFDU ///< osErrorResource (-3). +#define osFlagsErrorParameter 0xFFFFFFFCU ///< osErrorParameter (-4). +#define osFlagsErrorISR 0xFFFFFFFAU ///< osErrorISR (-6). + +// Status code values returned by CMSIS-RTOS functions. +typedef enum { + osOK = 0, ///< Operation completed successfully. + osError = -1, ///< Unspecified RTOS error: run-time error but no other error message fits. + osErrorTimeout = -2, ///< Operation not completed within the timeout period. + osErrorResource = -3, ///< Resource not available. + osErrorParameter = -4, ///< Parameter error. + osErrorNoMemory = -5, ///< System is out of memory: it was impossible to allocate or reserve memory for the operation. + osErrorISR = -6, ///< Not allowed in ISR context: the function cannot be called from interrupt service routines. + osStatusReserved = 0x7FFFFFFF ///< Prevents enum down-size compiler optimization. +} osStatus_t; + + +// \details Thread ID identifies the thread. +typedef void *osThreadId_t; + +// Set the specified Thread Flags of a thread. +// \param[in] thread_id thread ID obtained by \ref osThreadNew or \ref osThreadGetId. +// \param[in] flags specifies the flags of the thread that shall be set. +// \return thread flags after setting or error code if highest bit set. +uint32_t osThreadFlagsSet(osThreadId_t thread_id, uint32_t flags); + +#ifdef __cplusplus +} +#endif + +#endif + + + +#endif /* RTOS_TYPES_H_ */ diff --git a/rtos/rtos.h b/rtos/rtos.h index c2d4ad5..b480d25 100644 --- a/rtos/rtos.h +++ b/rtos/rtos.h @@ -25,7 +25,7 @@ #ifndef RTOS_H #define RTOS_H -#include "mbed_rtos_storage.h" +#include "rtos/mbed_rtos_storage.h" #include "rtos/Kernel.h" #include "rtos/Thread.h" #include "rtos/ThisThread.h" diff --git a/rtos/rtos_handlers.h b/rtos/rtos_handlers.h index e3d4743..351682b 100644 --- a/rtos/rtos_handlers.h +++ b/rtos/rtos_handlers.h @@ -25,7 +25,7 @@ #ifndef RTOS_HANDLERS_H #define RTOS_HANDLERS_H -#include "cmsis_os2.h" +#include "rtos/mbed_rtos_types.h" #ifdef __cplusplus extern "C" {