Skip to content

Commit

Permalink
RTOS API for bare metal
Browse files Browse the repository at this point in the history
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
  • Loading branch information
kjbracey committed Jul 15, 2019
1 parent 3816003 commit 83b329c
Show file tree
Hide file tree
Showing 35 changed files with 580 additions and 136 deletions.
3 changes: 3 additions & 0 deletions UNITTESTS/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ endif(COVERAGE)
# 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"
Expand Down
3 changes: 2 additions & 1 deletion UNITTESTS/target_h/rtos/Mutex.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
#define __MUTEX_H__

#include <inttypes.h>
#include "cmsis_os2.h"
#include "mbed_rtos_types.h"
#include "mbed_rtos1_types.h"

namespace rtos {

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion mbed.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 4 additions & 0 deletions rtos/ConditionVariable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -150,3 +152,5 @@ ConditionVariable::~ConditionVariable()
}

}

#endif
6 changes: 5 additions & 1 deletion rtos/ConditionVariable.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,14 @@
#define CONDITIONVARIABLE_H

#include <stdint.h>
#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 */
/** @{*/
Expand Down Expand Up @@ -328,4 +330,6 @@ class ConditionVariable : private mbed::NonCopyable<ConditionVariable> {
}
#endif

#endif

/** @}*/
37 changes: 37 additions & 0 deletions rtos/EventFlags.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
* SOFTWARE.
*/
#include "rtos/EventFlags.h"
#include "rtos/ThisThread.h"
#include "mbed_os_timer.h"
#include <string.h>
#include "mbed_error.h"
#include "mbed_assert.h"
Expand All @@ -38,27 +40,43 @@ EventFlags::EventFlags(const char *name)

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)
Expand All @@ -73,7 +91,9 @@ uint32_t EventFlags::wait_any(uint32_t flags, uint32_t millisec, bool clear)

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)
Expand All @@ -82,7 +102,24 @@ uint32_t EventFlags::wait(uint32_t flags, uint32_t opt, uint32_t millisec, bool
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
}

}
10 changes: 7 additions & 3 deletions rtos/EventFlags.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
#define EVENT_FLAG_H

#include <stdint.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/NonCopyable.h"

Expand Down Expand Up @@ -114,8 +114,12 @@ class EventFlags : private mbed::NonCopyable<EventFlags> {
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
};

/** @}*/
Expand Down
8 changes: 7 additions & 1 deletion rtos/Kernel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -61,8 +62,12 @@ uint64_t Kernel::get_ms_count()
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);
Expand All @@ -72,5 +77,6 @@ void Kernel::attach_thread_terminate_hook(void (*fptr)(osThreadId_t id))
{
rtos_attach_thread_terminate_hook(fptr);
}
#endif

}
2 changes: 1 addition & 1 deletion rtos/Kernel.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
#define KERNEL_H

#include <stdint.h>
#include "cmsis_os2.h"
#include "rtos/mbed_rtos_types.h"

namespace rtos {
/** \addtogroup rtos */
Expand Down
1 change: 0 additions & 1 deletion rtos/LICENSE.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ as can be found: LICENSE-apache-2.0.txt.

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
Expand Down
14 changes: 8 additions & 6 deletions rtos/Mail.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@
#include <stdint.h>
#include <string.h>

#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"
Expand All @@ -38,6 +38,8 @@
using namespace rtos;
#endif

#if MBED_CONF_RTOS_PRESENT || defined(DOXYGEN_ONLY)

namespace rtos {
/** \addtogroup rtos */
/** @{*/
Expand Down Expand Up @@ -238,5 +240,5 @@ class Mail : private mbed::NonCopyable<Mail<T, queue_sz> > {

#endif


#endif

8 changes: 5 additions & 3 deletions rtos/MemoryPool.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,12 @@
#include <stdint.h>
#include <string.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/NonCopyable.h"

#if MBED_CONF_RTOS_PRESENT || defined(DOXYGEN_ONLY)
namespace rtos {
/** \addtogroup rtos */
/** @{*/
Expand Down Expand Up @@ -191,3 +192,4 @@ class MemoryPool : private mbed::NonCopyable<MemoryPool<T, pool_sz> > {

}
#endif
#endif
7 changes: 6 additions & 1 deletion rtos/Mutex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand All @@ -40,6 +42,7 @@ Mutex::Mutex(const char *name)

void Mutex::constructor(const char *name)
{
_count = 0;
osMutexAttr_t attr =
{ 0 };
attr.name = name ? name : "application_unnamed_mutex";
Expand Down Expand Up @@ -147,3 +150,5 @@ Mutex::~Mutex()
}

}

#endif
Loading

0 comments on commit 83b329c

Please sign in to comment.