From 4d63870e8110f0b3a0d73d477bf80271da81e7ea Mon Sep 17 00:00:00 2001 From: Christopher Haster Date: Sun, 14 Aug 2016 21:38:26 -0500 Subject: [PATCH] Added rudimentary FreeRTOS implementation Mostly based on the *FromISR set of functions to be compatible with critical sections/interrupt contexts. equeue_tick = xTaskGetTickCountFromISR equeue_mutex = taskENTER_CRITICAL_FROM_ISR equeue_sema = xSemaphoreCreateBinaryStatic --- equeue_freertos.c | 61 +++++++++++++++++++++++++++++++++++++++++++++++ equeue_mutex.h | 3 +++ equeue_sema.h | 7 ++++++ 3 files changed, 71 insertions(+) create mode 100644 equeue_freertos.c diff --git a/equeue_freertos.c b/equeue_freertos.c new file mode 100644 index 0000000..4cb3baf --- /dev/null +++ b/equeue_freertos.c @@ -0,0 +1,61 @@ +/* + * Implementation for the mbed library + * https://github.com/mbedmicro/mbed + * + * Copyright (c) 2016 Christopher Haster + * Distributed under the MIT license + */ +#include "equeue_tick.h" +#include "equeue_sema.h" +#include "equeue_mutex.h" + +#if defined(EQUEUE_PLATFORM_FREERTOS) + +#include "task.h" + + +// Ticker operations +unsigned equeue_tick(void) { + return xTaskGetTickCountFromISR() * portTICK_PERIOD_MS; +} + + +// Mutex operations +int equeue_mutex_create(equeue_mutex_t *m) { return 0; } +void equeue_mutex_destroy(equeue_mutex_t *m) { } + +void equeue_mutex_lock(equeue_mutex_t *m) { + *m = taskENTER_CRITICAL_FROM_ISR(); +} + +void equeue_mutex_unlock(equeue_mutex_t *m) { + taskEXIT_CRITICAL_FROM_ISR(*m); +} + + +// Semaphore operations +int equeue_sema_create(equeue_sema_t *s) { + s->handle = xSemaphoreCreateBinaryStatic(&s->buffer); + return s->handle ? 0 : -1; +} + +void equeue_sema_destroy(equeue_sema_t *s) { + vSemaphoreDelete(s->handle); +} + +void equeue_sema_signal(equeue_sema_t *s) { + xSemaphoreGiveFromISR(s->handle, NULL); +} + +bool equeue_sema_wait(equeue_sema_t *s, int ms) { + if (ms < 0) { + ms = portMAX_DELAY; + } else { + ms = ms / portTICK_PERIOD_MS; + } + + return xSemaphoreTake(s->handle, ms); +} + + +#endif diff --git a/equeue_mutex.h b/equeue_mutex.h index 67afc3f..b6325b2 100644 --- a/equeue_mutex.h +++ b/equeue_mutex.h @@ -27,6 +27,9 @@ typedef pthread_mutex_t equeue_mutex_t; typedef CRITICAL_SECTION equeue_mutex_t; #elif defined(__MBED__) typedef unsigned equeue_mutex_t; +#elif defined(EQUEUE_PLATFORM_FREERTOS) +#include "FreeRTOS.h" +typedef UBaseType_t equeue_mutex_t; #endif diff --git a/equeue_sema.h b/equeue_sema.h index 2fe50d0..23ecc26 100644 --- a/equeue_sema.h +++ b/equeue_sema.h @@ -39,6 +39,13 @@ typedef HANDLE equeue_sema_t; typedef unsigned equeue_sema_t[8]; #elif defined(__MBED__) typedef volatile int equeue_sema_t; +#elif defined(EQUEUE_PLATFORM_FREERTOS) +#include "FreeRTOS.h" +#include "semphr.h" +typedef struct equeue_sema { + SemaphoreHandle_t handle; + StaticSemaphore_t buffer; +} equeue_sema_t; #endif