Make events use RTOS API

Switch from CMSIS-RTOS to mbed C++ API, which are available in bare
metal build.

Other minor tidies, like removing unnecessary volatile.
pull/10104/head
Kevin Bracey 2019-03-15 12:23:16 +02:00
parent a522dcfa0a
commit c94d6a8cc7
3 changed files with 37 additions and 27 deletions

View File

@ -22,6 +22,7 @@
#include <stdbool.h> #include <stdbool.h>
#include <string.h> #include <string.h>
#include "cmsis.h"
#include "platform/mbed_critical.h" #include "platform/mbed_critical.h"
#include "drivers/Timer.h" #include "drivers/Timer.h"
#include "drivers/Ticker.h" #include "drivers/Ticker.h"
@ -33,11 +34,13 @@
using namespace mbed; using namespace mbed;
// Ticker operations // Ticker operations
#if MBED_CONF_RTOS_PRESENT #if MBED_CONF_RTOS_API_PRESENT
#include "rtos/Kernel.h"
unsigned equeue_tick() unsigned equeue_tick()
{ {
return osKernelGetTickCount(); return rtos::Kernel::get_ms_count();
} }
#else #else
@ -120,27 +123,28 @@ void equeue_mutex_unlock(equeue_mutex_t *m)
// Semaphore operations // Semaphore operations
#ifdef MBED_CONF_RTOS_PRESENT #ifdef MBED_CONF_RTOS_API_PRESENT
#include "rtos/EventFlags.h"
MBED_STATIC_ASSERT(sizeof(equeue_sema_t) == sizeof(rtos::EventFlags), "equeue_sema_t / rtos::EventFlags mismatch");
int equeue_sema_create(equeue_sema_t *s) int equeue_sema_create(equeue_sema_t *s)
{ {
osEventFlagsAttr_t attr; new (s) rtos::EventFlags("equeue");
memset(&attr, 0, sizeof(attr)); return 0;
attr.cb_mem = &s->mem;
attr.cb_size = sizeof(s->mem);
s->id = osEventFlagsNew(&attr);
return !s->id ? -1 : 0;
} }
void equeue_sema_destroy(equeue_sema_t *s) void equeue_sema_destroy(equeue_sema_t *s)
{ {
osEventFlagsDelete(s->id); rtos::EventFlags *ef = reinterpret_cast<rtos::EventFlags *>(s);
ef->~EventFlags();
} }
void equeue_sema_signal(equeue_sema_t *s) void equeue_sema_signal(equeue_sema_t *s)
{ {
osEventFlagsSet(s->id, 1); rtos::EventFlags *ef = reinterpret_cast<rtos::EventFlags *>(s);
ef->set(1);
} }
bool equeue_sema_wait(equeue_sema_t *s, int ms) bool equeue_sema_wait(equeue_sema_t *s, int ms)
@ -149,7 +153,8 @@ bool equeue_sema_wait(equeue_sema_t *s, int ms)
ms = osWaitForever; ms = osWaitForever;
} }
return (osEventFlagsWait(s->id, 1, osFlagsWaitAny, ms) == 1); rtos::EventFlags *ef = reinterpret_cast<rtos::EventFlags *>(s);
return ef->wait_any(1, ms) == 1;
} }
#else #else
@ -157,7 +162,7 @@ bool equeue_sema_wait(equeue_sema_t *s, int ms)
// Semaphore operations // Semaphore operations
int equeue_sema_create(equeue_sema_t *s) int equeue_sema_create(equeue_sema_t *s)
{ {
*s = false; *s = 0;
return 0; return 0;
} }
@ -177,23 +182,21 @@ static void equeue_sema_timeout(equeue_sema_t *s)
bool equeue_sema_wait(equeue_sema_t *s, int ms) bool equeue_sema_wait(equeue_sema_t *s, int ms)
{ {
int signal = 0;
ALIAS_TIMEOUT timeout; ALIAS_TIMEOUT timeout;
if (ms == 0) { if (ms > 0) {
return false;
} else if (ms > 0) {
timeout.attach_us(callback(equeue_sema_timeout, s), (us_timestamp_t)ms * 1000); timeout.attach_us(callback(equeue_sema_timeout, s), (us_timestamp_t)ms * 1000);
} }
core_util_critical_section_enter(); core_util_critical_section_enter();
while (!*s) { while (!*s && ms != 0) {
sleep(); sleep();
core_util_critical_section_exit(); core_util_critical_section_exit();
__ISB();
core_util_critical_section_enter(); core_util_critical_section_enter();
} }
signal = *s; int signal = *s;
*s = false; *s = 0;
core_util_critical_section_exit(); core_util_critical_section_exit();
return (signal > 0); return (signal > 0);

View File

@ -26,6 +26,7 @@ extern "C" {
#endif #endif
#include <stdbool.h> #include <stdbool.h>
#include <stdint.h>
// Currently supported platforms // Currently supported platforms
// //
@ -114,13 +115,19 @@ typedef struct equeue_sema {
pthread_cond_t cond; pthread_cond_t cond;
bool signal; bool signal;
} equeue_sema_t; } equeue_sema_t;
#elif defined(EQUEUE_PLATFORM_MBED) && defined(MBED_CONF_RTOS_PRESENT) #elif defined(EQUEUE_PLATFORM_MBED) && MBED_CONF_RTOS_API_PRESENT
typedef struct equeue_sema { typedef struct equeue_sema {
osEventFlagsId_t id; // We will actually store a C++ rtos:EventQueue in here;
mbed_rtos_storage_event_flags_t mem; // attempt to match layout for storage, and assert size in equeue_mbed.cpp
#if MBED_CONF_RTOS_PRESENT
osEventFlagsId_t _id;
mbed_rtos_storage_event_flags_t _obj_mem;
#else
uint32_t _flags;
#endif
} equeue_sema_t; } equeue_sema_t;
#elif defined(EQUEUE_PLATFORM_MBED) #elif defined(EQUEUE_PLATFORM_MBED)
typedef volatile int equeue_sema_t; typedef int equeue_sema_t;
#endif #endif
// Platform semaphore operations // Platform semaphore operations

View File

@ -30,7 +30,7 @@ namespace rtos {
EventFlags::EventFlags() EventFlags::EventFlags()
{ {
constructor(); constructor("application_unnamed_event_flags");
} }
EventFlags::EventFlags(const char *name) EventFlags::EventFlags(const char *name)
@ -42,7 +42,7 @@ void EventFlags::constructor(const char *name)
{ {
#if MBED_CONF_RTOS_PRESENT #if MBED_CONF_RTOS_PRESENT
osEventFlagsAttr_t attr = { 0 }; osEventFlagsAttr_t attr = { 0 };
attr.name = name ? name : "application_unnamed_event_flags"; attr.name = name;
attr.cb_mem = &_obj_mem; attr.cb_mem = &_obj_mem;
attr.cb_size = sizeof(_obj_mem); attr.cb_size = sizeof(_obj_mem);
_id = osEventFlagsNew(&attr); _id = osEventFlagsNew(&attr);