Skip to content

Commit

Permalink
Remove use of internal RTX types
Browse files Browse the repository at this point in the history
Make calls to cmsis-os to get thread state, stack size, and max stack
usage rather than accessing internal RTX data directly. Wrap RTX5
specific code in OS_BACKEND_RTX5.

Also refactor the code to use mbed types rather than RTX types:
os_timer_t -> mbed_rtos_storage_timer_t
os_event_flags_t -> mbed_rtos_storage_event_flags_t
osRtxMutex_t -> mbed_rtos_storage_thread_t
  • Loading branch information
c1728p9 committed Sep 4, 2017
1 parent 5bddd88 commit b2384b1
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 10 deletions.
4 changes: 2 additions & 2 deletions events/equeue/equeue_platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ extern "C" {
#include <pthread.h>
#elif defined(EQUEUE_PLATFORM_MBED)
#include "cmsis_os2.h"
#include "rtx_lib.h"
#include "mbed_rtos_storage.h"
#endif


Expand Down Expand Up @@ -117,7 +117,7 @@ typedef struct equeue_sema {
#elif defined(EQUEUE_PLATFORM_MBED) && defined(MBED_CONF_RTOS_PRESENT)
typedef struct equeue_sema {
osEventFlagsId_t id;
os_event_flags_t mem;
mbed_rtos_storage_event_flags_t mem;
} equeue_sema_t;
#elif defined(EQUEUE_PLATFORM_MBED)
typedef volatile int equeue_sema_t;
Expand Down
2 changes: 1 addition & 1 deletion rtos/Mail.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
#include "Queue.h"
#include "MemoryPool.h"
#include "cmsis_os2.h"
#include "rtx_lib.h"
#include "mbed_rtos_storage.h"
#include "mbed_rtos1_types.h"

#include "platform/NonCopyable.h"
Expand Down
4 changes: 2 additions & 2 deletions rtos/RtosTimer.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

#include <stdint.h>
#include "cmsis_os2.h"
#include "rtx_lib.h"
#include "mbed_rtos_storage.h"
#include "platform/Callback.h"
#include "platform/NonCopyable.h"
#include "platform/mbed_toolchain.h"
Expand Down Expand Up @@ -150,7 +150,7 @@ class RtosTimer : private mbed::NonCopyable<RtosTimer> {

osTimerId_t _id;
osTimerAttr_t _attr;
os_timer_t _obj_mem;
mbed_rtos_storage_timer_t _obj_mem;
mbed::Callback<void()> _function;
};

Expand Down
1 change: 1 addition & 0 deletions rtos/TARGET_CORTEX/mbed_rtos_storage.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ extern "C" {
*/

#include "rtx_lib.h"
#include "mbed_rtx_conf.h"

typedef os_mutex_t mbed_rtos_storage_mutex_t;
typedef os_semaphore_t mbed_rtos_storage_semaphore_t;
Expand Down
3 changes: 3 additions & 0 deletions rtos/TARGET_CORTEX/mbed_rtx_conf.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@

#include "mbed_rtx.h"

/** Any access to RTX5 specific data structures used in common code should be wrapped in ifdef MBED_OS_BACKEND_RTX5 */
#define MBED_OS_BACKEND_RTX5

/** The thread's stack size can be configured by the application, if not explicitly specified it'll default to 4K */
#ifndef MBED_CONF_APP_THREAD_STACK_SIZE
#define MBED_CONF_APP_THREAD_STACK_SIZE 4096
Expand Down
17 changes: 15 additions & 2 deletions rtos/Thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,11 @@ Thread::State Thread::get_state() {
_mutex.lock();

if (_tid != NULL) {
#if defined(MBED_OS_BACKEND_RTX5)
state = _obj_mem.state;
#else
state = osThreadGetState(_tid);
#endif
}

_mutex.unlock();
Expand All @@ -185,6 +189,7 @@ Thread::State Thread::get_state() {
case osThreadRunning:
user_state = Running;
break;
#if defined(MBED_OS_BACKEND_RTX5)
case osRtxThreadWaitingDelay:
user_state = WaitingDelay;
break;
Expand Down Expand Up @@ -212,6 +217,7 @@ Thread::State Thread::get_state() {
case osRtxThreadWaitingMessagePut:
user_state = WaitingMessagePut;
break;
#endif
case osThreadTerminated:
default:
user_state = Deleted;
Expand All @@ -226,8 +232,7 @@ uint32_t Thread::stack_size() {
_mutex.lock();

if (_tid != NULL) {
os_thread_t *thread = (os_thread_t *)_tid;
size = thread->stack_size;
size = osThreadGetStackSize(_tid);
}

_mutex.unlock();
Expand All @@ -238,10 +243,12 @@ uint32_t Thread::free_stack() {
uint32_t size = 0;
_mutex.lock();

#if defined(MBED_OS_BACKEND_RTX5)
if (_tid != NULL) {
os_thread_t *thread = (os_thread_t *)_tid;
size = (uint32_t)thread->sp - (uint32_t)thread->stack_mem;
}
#endif

_mutex.unlock();
return size;
Expand All @@ -251,10 +258,12 @@ uint32_t Thread::used_stack() {
uint32_t size = 0;
_mutex.lock();

#if defined(MBED_OS_BACKEND_RTX5)
if (_tid != NULL) {
os_thread_t *thread = (os_thread_t *)_tid;
size = ((uint32_t)thread->stack_mem + thread->stack_size) - thread->sp;
}
#endif

_mutex.unlock();
return size;
Expand All @@ -265,11 +274,15 @@ uint32_t Thread::max_stack() {
_mutex.lock();

if (_tid != NULL) {
#if defined(MBED_OS_BACKEND_RTX5)
os_thread_t *thread = (os_thread_t *)_tid;
uint32_t high_mark = 0;
while (((uint32_t *)(thread->stack_mem))[high_mark] == 0xE25A2EA5)
high_mark++;
size = thread->stack_size - (high_mark * sizeof(uint32_t));
#else
size = osThreadGetStackSize(_tid) - osThreadGetStackSpace(_tid);
#endif
}

_mutex.unlock();
Expand Down
1 change: 0 additions & 1 deletion rtos/Thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
#include "cmsis_os2.h"
#include "mbed_rtos1_types.h"
#include "mbed_rtos_storage.h"
#include "mbed_rtx_conf.h"
#include "platform/Callback.h"
#include "platform/mbed_toolchain.h"
#include "platform/NonCopyable.h"
Expand Down
2 changes: 0 additions & 2 deletions rtos/rtos.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@
#ifndef RTOS_H
#define RTOS_H

#include "mbed_rtx.h"
#include "mbed_rtx_conf.h"
#include "mbed_rtos_storage.h"
#include "rtos/Thread.h"
#include "rtos/Mutex.h"
Expand Down

0 comments on commit b2384b1

Please sign in to comment.