Skip to content

Commit 83b329c

Browse files
committed
RTOS API for bare metal
Provide partial RTOS API for bare metal builds - things that can be done in a single threaded environment. Allows more code to work in both RTOS and bare metal builds without change, and in particular gives easy access to the ability to efficiently wait for something occurring in interrupt. Available in bare-metal: * ThisThread * osThreadFlagsSet to set flags on main thread (can be set from IRQ) * EventFlags (can be set from IRQ) * Semaphores (can be released from IRQ) * Mutex (dummy implementation) Not useful: * ConditionVariable (could only be signalled from 2nd thread) * RtosTimer (calls in a second thread context) * Thread Unimplemented: * Mail, Queue, MemoryPool Possible future work: * ConditionVariableCS to act as IRQ signalled ConditionVariable
1 parent 3816003 commit 83b329c

35 files changed

+580
-136
lines changed

UNITTESTS/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,9 @@ endif(COVERAGE)
8686
# UNIT TESTS
8787
####################
8888

89+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DUNITTEST")
90+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DUNITTEST")
91+
8992
# Set include dirs.
9093
set(unittest-includes-base
9194
"${PROJECT_SOURCE_DIR}/target_h"

UNITTESTS/target_h/rtos/Mutex.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
#define __MUTEX_H__
1919

2020
#include <inttypes.h>
21-
#include "cmsis_os2.h"
21+
#include "mbed_rtos_types.h"
22+
#include "mbed_rtos1_types.h"
2223

2324
namespace rtos {
2425

File renamed without changes.

mbed.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
#include "platform/mbed_version.h"
2020

21-
#if MBED_CONF_RTOS_PRESENT
21+
#if MBED_CONF_RTOS_API_PRESENT
2222
#include "rtos/rtos.h"
2323
#endif
2424

rtos/ConditionVariable.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
#include "mbed_error.h"
2727
#include "mbed_assert.h"
2828

29+
#if MBED_CONF_RTOS_PRESENT
30+
2931
namespace rtos {
3032

3133
ConditionVariable::Waiter::Waiter(): sem(0), prev(NULL), next(NULL), in_list(false)
@@ -150,3 +152,5 @@ ConditionVariable::~ConditionVariable()
150152
}
151153

152154
}
155+
156+
#endif

rtos/ConditionVariable.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,14 @@
2323
#define CONDITIONVARIABLE_H
2424

2525
#include <stdint.h>
26-
#include "cmsis_os.h"
26+
#include "rtos/mbed_rtos_types.h"
2727
#include "rtos/Mutex.h"
2828
#include "rtos/Semaphore.h"
2929

3030
#include "platform/NonCopyable.h"
3131

32+
#if MBED_CONF_RTOS_PRESENT || defined(DOXYGEN_ONLY)
33+
3234
namespace rtos {
3335
/** \addtogroup rtos */
3436
/** @{*/
@@ -328,4 +330,6 @@ class ConditionVariable : private mbed::NonCopyable<ConditionVariable> {
328330
}
329331
#endif
330332

333+
#endif
334+
331335
/** @}*/

rtos/EventFlags.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
* SOFTWARE.
2121
*/
2222
#include "rtos/EventFlags.h"
23+
#include "rtos/ThisThread.h"
24+
#include "mbed_os_timer.h"
2325
#include <string.h>
2426
#include "mbed_error.h"
2527
#include "mbed_assert.h"
@@ -38,27 +40,43 @@ EventFlags::EventFlags(const char *name)
3840

3941
void EventFlags::constructor(const char *name)
4042
{
43+
#if MBED_CONF_RTOS_PRESENT
4144
osEventFlagsAttr_t attr = { 0 };
4245
attr.name = name ? name : "application_unnamed_event_flags";
4346
attr.cb_mem = &_obj_mem;
4447
attr.cb_size = sizeof(_obj_mem);
4548
_id = osEventFlagsNew(&attr);
4649
MBED_ASSERT(_id);
50+
#else
51+
_flags = 0;
52+
#endif
4753
}
4854

4955
uint32_t EventFlags::set(uint32_t flags)
5056
{
57+
#if MBED_CONF_RTOS_PRESENT
5158
return osEventFlagsSet(_id, flags);
59+
#else
60+
return core_util_atomic_fetch_or_u32(&_flags, flags) | flags;
61+
#endif
5262
}
5363

5464
uint32_t EventFlags::clear(uint32_t flags)
5565
{
66+
#if MBED_CONF_RTOS_PRESENT
5667
return osEventFlagsClear(_id, flags);
68+
#else
69+
return core_util_atomic_fetch_and_u32(&_flags, ~flags);
70+
#endif
5771
}
5872

5973
uint32_t EventFlags::get() const
6074
{
75+
#if MBED_CONF_RTOS_PRESENT
6176
return osEventFlagsGet(_id);
77+
#else
78+
return core_util_atomic_load_u32(&_flags);
79+
#endif
6280
}
6381

6482
uint32_t EventFlags::wait_all(uint32_t flags, uint32_t millisec, bool clear)
@@ -73,7 +91,9 @@ uint32_t EventFlags::wait_any(uint32_t flags, uint32_t millisec, bool clear)
7391

7492
EventFlags::~EventFlags()
7593
{
94+
#if MBED_CONF_RTOS_PRESENT
7695
osEventFlagsDelete(_id);
96+
#endif
7797
}
7898

7999
uint32_t EventFlags::wait(uint32_t flags, uint32_t opt, uint32_t millisec, bool clear)
@@ -82,7 +102,24 @@ uint32_t EventFlags::wait(uint32_t flags, uint32_t opt, uint32_t millisec, bool
82102
opt |= osFlagsNoClear;
83103
}
84104

105+
#if MBED_CONF_RTOS_PRESENT
85106
return osEventFlagsWait(_id, flags, opt, millisec);
107+
#else
108+
rtos::internal::flags_check_capture check;
109+
check.flags = &_flags;
110+
check.options = opt;
111+
check.flags_wanted = flags;
112+
check.result = 0;
113+
check.match = false;
114+
mbed::internal::do_timed_sleep_relative_or_forever(millisec, rtos::internal::non_rtos_check_flags, &check);
115+
if (check.match) {
116+
return check.result;
117+
} else if (millisec == 0) {
118+
return osErrorResource;
119+
} else {
120+
return osErrorTimeout;
121+
}
122+
#endif
86123
}
87124

88125
}

rtos/EventFlags.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@
2323
#define EVENT_FLAG_H
2424

2525
#include <stdint.h>
26-
#include "cmsis_os2.h"
27-
#include "mbed_rtos1_types.h"
28-
#include "mbed_rtos_storage.h"
26+
#include "rtos/mbed_rtos_types.h"
27+
#include "rtos/mbed_rtos1_types.h"
28+
#include "rtos/mbed_rtos_storage.h"
2929

3030
#include "platform/NonCopyable.h"
3131

@@ -114,8 +114,12 @@ class EventFlags : private mbed::NonCopyable<EventFlags> {
114114
private:
115115
void constructor(const char *name = NULL);
116116
uint32_t wait(uint32_t flags, uint32_t opt, uint32_t millisec, bool clear);
117+
#if MBED_CONF_RTOS_PRESENT
117118
osEventFlagsId_t _id;
118119
mbed_rtos_storage_event_flags_t _obj_mem;
120+
#else
121+
uint32_t _flags;
122+
#endif
119123
};
120124

121125
/** @}*/

0 commit comments

Comments
 (0)