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

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 "rtos/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: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@
2525
#include <stdint.h>
2626
#include <string.h>
2727

28-
#include "Queue.h"
29-
#include "MemoryPool.h"
30-
#include "cmsis_os2.h"
31-
#include "mbed_rtos_storage.h"
32-
#include "mbed_rtos1_types.h"
28+
#include "rtos/Queue.h"
29+
#include "rtos/MemoryPool.h"
30+
#include "rtos/mbed_rtos_types.h"
31+
#include "rtos/mbed_rtos_storage.h"
32+
#include "rtos/mbed_rtos1_types.h"
3333

3434
#include "platform/mbed_toolchain.h"
3535
#include "platform/NonCopyable.h"
@@ -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: 5 additions & 3 deletions
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"
29-
#include "mbed_rtos1_types.h"
30-
#include "mbed_rtos_storage.h"
28+
#include "rtos/mbed_rtos_types.h"
29+
#include "rtos/mbed_rtos1_types.h"
30+
#include "rtos/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
/** @{*/
@@ -191,3 +192,4 @@ class MemoryPool : private mbed::NonCopyable<MemoryPool<T, pool_sz> > {
191192

192193
}
193194
#endif
195+
#endif

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

0 commit comments

Comments
 (0)