Skip to content

Commit 8479550

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 e6300f6 commit 8479550

27 files changed

+516
-85
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/internal/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/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

@@ -37,6 +37,8 @@
3737
using namespace rtos;
3838
#endif
3939

40+
#if MBED_CONF_RTOS_PRESENT || defined(DOXYGEN_ONLY)
41+
4042
namespace rtos {
4143
/** \addtogroup rtos */
4244
/** @{*/
@@ -175,5 +177,5 @@ class Mail : private mbed::NonCopyable<Mail<T, queue_sz> > {
175177

176178
#endif
177179

178-
180+
#endif
179181

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
/** @{*/
@@ -124,4 +125,6 @@ class MemoryPool : private mbed::NonCopyable<MemoryPool<T, pool_sz> > {
124125
}
125126
#endif
126127

128+
#endif
129+
127130

rtos/Mutex.cpp

Lines changed: 13 additions & 8 deletions
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 : "aplication_unnamed_mutex";
@@ -50,9 +53,9 @@ void Mutex::constructor(const char *name)
5053
MBED_ASSERT(_id);
5154
}
5255

53-
osStatus Mutex::lock(void)
56+
osStatus_t Mutex::lock(void)
5457
{
55-
osStatus status = osMutexAcquire(_id, osWaitForever);
58+
osStatus_t status = osMutexAcquire(_id, osWaitForever);
5659
if (osOK == status) {
5760
_count++;
5861
}
@@ -64,9 +67,9 @@ osStatus Mutex::lock(void)
6467
return osOK;
6568
}
6669

67-
osStatus Mutex::lock(uint32_t millisec)
70+
osStatus_t Mutex::lock(uint32_t millisec)
6871
{
69-
osStatus status = osMutexAcquire(_id, millisec);
72+
osStatus_t status = osMutexAcquire(_id, millisec);
7073
if (osOK == status) {
7174
_count++;
7275
}
@@ -89,7 +92,7 @@ bool Mutex::trylock()
8992

9093
bool Mutex::trylock_for(uint32_t millisec)
9194
{
92-
osStatus status = osMutexAcquire(_id, millisec);
95+
osStatus_t status = osMutexAcquire(_id, millisec);
9396
if (status == osOK) {
9497
return true;
9598
}
@@ -119,11 +122,11 @@ bool Mutex::trylock_until(uint64_t millisec)
119122
}
120123
}
121124

122-
osStatus Mutex::unlock()
125+
osStatus_t Mutex::unlock()
123126
{
124127
_count--;
125128

126-
osStatus status = osMutexRelease(_id);
129+
osStatus_t status = osMutexRelease(_id);
127130

128131
if (status != osOK) {
129132
MBED_ERROR1(MBED_MAKE_ERROR(MBED_MODULE_KERNEL, MBED_ERROR_CODE_MUTEX_UNLOCK_FAILED), "Mutex unlock failed", status);
@@ -143,3 +146,5 @@ Mutex::~Mutex()
143146
}
144147

145148
}
149+
150+
#endif

0 commit comments

Comments
 (0)