Skip to content

Commit dabe49e

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 0bda0c5 commit dabe49e

29 files changed

+525
-113
lines changed

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)
@@ -151,3 +153,5 @@ ConditionVariable::~ConditionVariable()
151153
}
152154

153155
}
156+
157+
#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 "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: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
#define EVENT_FLAG_H
2424

2525
#include <stdint.h>
26-
#include "cmsis_os2.h"
26+
#include "mbed_rtos_types.h"
2727
#include "mbed_rtos1_types.h"
2828
#include "mbed_rtos_storage.h"
2929

@@ -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
/** @}*/

rtos/Kernel.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,17 @@
2020
* SOFTWARE.
2121
*/
2222

23-
#include "cmsis_os2.h"
2423
#include "rtos/Kernel.h"
2524
#include "rtos/rtos_idle.h"
2625
#include "rtos/rtos_handlers.h"
2726
#include "platform/mbed_critical.h"
27+
#include "platform/mbed_os_timer.h"
2828

2929
namespace rtos {
3030

3131
uint64_t Kernel::get_ms_count()
3232
{
33+
#if MBED_CONF_RTOS_PRESENT
3334
// CMSIS-RTOS 2.1.0 and 2.1.1 differ in the time type. We assume
3435
// our header at least matches the implementation, so we don't try looking
3536
// at the run-time version report. (There's no compile-time version report)
@@ -61,8 +62,12 @@ uint64_t Kernel::get_ms_count()
6162
core_util_critical_section_exit();
6263
return ret;
6364
}
65+
#else
66+
return mbed::internal::init_os_timer()->update_and_get_tick();
67+
#endif
6468
}
6569

70+
#if MBED_CONF_RTOS_PRESENT
6671
void Kernel::attach_idle_hook(void (*fptr)(void))
6772
{
6873
rtos_attach_idle_hook(fptr);
@@ -72,5 +77,6 @@ void Kernel::attach_thread_terminate_hook(void (*fptr)(osThreadId_t id))
7277
{
7378
rtos_attach_thread_terminate_hook(fptr);
7479
}
80+
#endif
7581

7682
}

rtos/Kernel.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
#define KERNEL_H
2424

2525
#include <stdint.h>
26-
#include "cmsis_os2.h"
26+
#include "mbed_rtos_types.h"
2727

2828
namespace rtos {
2929
/** \addtogroup rtos */

rtos/LICENSE.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ as can be found: LICENSE-apache-2.0.txt.
33

44
Files licensed under MIT:
55

6-
- TARGET_CORTEX/mbed_rtos1_types.h
76
- TARGET_CORTEX/mbed_rtos_storage.h
87
- TARGET_CORTEX/mbed_rtx_conf.h
98
- TARGET_CORTEX/mbed_rtx_idle.cpp

rtos/Mail.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727

2828
#include "Queue.h"
2929
#include "MemoryPool.h"
30-
#include "cmsis_os2.h"
30+
#include "mbed_rtos_types.h"
3131
#include "mbed_rtos_storage.h"
3232
#include "mbed_rtos1_types.h"
3333

@@ -38,6 +38,8 @@
3838
using namespace rtos;
3939
#endif
4040

41+
#if MBED_CONF_RTOS_PRESENT || defined(DOXYGEN_ONLY)
42+
4143
namespace rtos {
4244
/** \addtogroup rtos */
4345
/** @{*/
@@ -238,5 +240,5 @@ class Mail : private mbed::NonCopyable<Mail<T, queue_sz> > {
238240

239241
#endif
240242

241-
243+
#endif
242244

rtos/MemoryPool.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,12 @@
2525
#include <stdint.h>
2626
#include <string.h>
2727

28-
#include "cmsis_os2.h"
28+
#include "mbed_rtos_types.h"
2929
#include "mbed_rtos1_types.h"
3030
#include "mbed_rtos_storage.h"
3131
#include "platform/NonCopyable.h"
3232

33+
#if MBED_CONF_RTOS_PRESENT || defined(DOXYGEN_ONLY)
3334
namespace rtos {
3435
/** \addtogroup rtos */
3536
/** @{*/
@@ -193,4 +194,6 @@ class MemoryPool : private mbed::NonCopyable<MemoryPool<T, pool_sz> > {
193194
}
194195
#endif
195196

197+
#endif
198+
196199

rtos/Mutex.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,11 @@
2626
#include "mbed_error.h"
2727
#include "mbed_assert.h"
2828

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

31-
Mutex::Mutex(): _count(0)
33+
Mutex::Mutex()
3234
{
3335
constructor();
3436
}
@@ -40,6 +42,7 @@ Mutex::Mutex(const char *name)
4042

4143
void Mutex::constructor(const char *name)
4244
{
45+
_count = 0;
4346
osMutexAttr_t attr =
4447
{ 0 };
4548
attr.name = name ? name : "application_unnamed_mutex";
@@ -147,3 +150,5 @@ Mutex::~Mutex()
147150
}
148151

149152
}
153+
154+
#endif

rtos/Mutex.h

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

2525
#include <stdint.h>
26-
#include "cmsis_os2.h"
26+
#include "mbed_rtos_types.h"
2727
#include "mbed_rtos1_types.h"
2828
#include "mbed_rtos_storage.h"
2929

@@ -56,6 +56,8 @@ typedef mbed::ScopedLock<Mutex> ScopedMutexLock;
5656
/** The Mutex class is used to synchronize the execution of threads.
5757
This is, for example, used to protect access to a shared resource.
5858
59+
In bare-metal builds, the Mutex class is a dummy, so lock() and unlock() are no-ops.
60+
5961
@note You cannot use member functions of this class in ISR context. If you require Mutex functionality within
6062
ISR handler, consider using @a Semaphore.
6163
@@ -88,7 +90,11 @@ class Mutex : private mbed::NonCopyable<Mutex> {
8890
@note This function treats RTOS errors as fatal system errors, so it can only return osOK.
8991
Use of the return value is deprecated, as the return is expected to become void in the future.
9092
*/
91-
osStatus lock(void);
93+
#if MBED_CONF_RTOS_PRESENT
94+
osStatus lock();
95+
#else
96+
void lock(); // Value return backwards compatibility not required for non-RTOS
97+
#endif
9298

9399
/**
94100
Wait until a Mutex becomes available.
@@ -150,14 +156,18 @@ class Mutex : private mbed::NonCopyable<Mutex> {
150156
@note This function treats RTOS errors as fatal system errors, so it can only return osOK.
151157
Use of the return value is deprecated, as the return is expected to become void in the future.
152158
*/
159+
#if MBED_CONF_RTOS_PRESENT
153160
osStatus unlock();
161+
#else
162+
void unlock(); // Value return backwards compatibility not required for non-RTOS
163+
#endif
154164

155165
/** Get the owner the this mutex
156166
@return the current owner of this mutex.
157167
158168
@note You cannot call this function from ISR context.
159169
*/
160-
osThreadId get_owner();
170+
osThreadId_t get_owner();
161171

162172
/** Mutex destructor
163173
*
@@ -166,13 +176,53 @@ class Mutex : private mbed::NonCopyable<Mutex> {
166176
~Mutex();
167177

168178
private:
179+
#if MBED_CONF_RTOS_PRESENT
169180
void constructor(const char *name = NULL);
170181
friend class ConditionVariable;
171182

172183
osMutexId_t _id;
173184
mbed_rtos_storage_mutex_t _obj_mem;
174185
uint32_t _count;
186+
#endif
175187
};
188+
189+
#if !MBED_CONF_RTOS_PRESENT
190+
inline Mutex::Mutex()
191+
{
192+
}
193+
194+
inline Mutex::Mutex(const char *)
195+
{
196+
}
197+
198+
inline Mutex::~Mutex()
199+
{
200+
}
201+
202+
inline void Mutex::lock()
203+
{
204+
}
205+
206+
inline bool Mutex::trylock()
207+
{
208+
return true;
209+
}
210+
211+
inline bool Mutex::trylock_for(uint32_t)
212+
{
213+
return true;
214+
}
215+
216+
inline bool Mutex::trylock_until(uint64_t)
217+
{
218+
return true;
219+
}
220+
221+
inline void Mutex::unlock()
222+
{
223+
}
224+
#endif
225+
176226
/** @}*/
177227
/** @}*/
178228
}

0 commit comments

Comments
 (0)