Rtos API example

Committer:
marcozecchini
Date:
Sat Feb 23 12:13:36 2019 +0000
Revision:
0:9fca2b23d0ba
final commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
marcozecchini 0:9fca2b23d0ba 1 /*
marcozecchini 0:9fca2b23d0ba 2 * Implementation for the mbed library
marcozecchini 0:9fca2b23d0ba 3 * https://github.com/mbedmicro/mbed
marcozecchini 0:9fca2b23d0ba 4 *
marcozecchini 0:9fca2b23d0ba 5 * Copyright (c) 2016 Christopher Haster
marcozecchini 0:9fca2b23d0ba 6 *
marcozecchini 0:9fca2b23d0ba 7 * Licensed under the Apache License, Version 2.0 (the "License");
marcozecchini 0:9fca2b23d0ba 8 * you may not use this file except in compliance with the License.
marcozecchini 0:9fca2b23d0ba 9 * You may obtain a copy of the License at
marcozecchini 0:9fca2b23d0ba 10 *
marcozecchini 0:9fca2b23d0ba 11 * http://www.apache.org/licenses/LICENSE-2.0
marcozecchini 0:9fca2b23d0ba 12 *
marcozecchini 0:9fca2b23d0ba 13 * Unless required by applicable law or agreed to in writing, software
marcozecchini 0:9fca2b23d0ba 14 * distributed under the License is distributed on an "AS IS" BASIS,
marcozecchini 0:9fca2b23d0ba 15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
marcozecchini 0:9fca2b23d0ba 16 * See the License for the specific language governing permissions and
marcozecchini 0:9fca2b23d0ba 17 * limitations under the License.
marcozecchini 0:9fca2b23d0ba 18 */
marcozecchini 0:9fca2b23d0ba 19 #include "equeue/equeue_platform.h"
marcozecchini 0:9fca2b23d0ba 20
marcozecchini 0:9fca2b23d0ba 21 #if defined(EQUEUE_PLATFORM_MBED)
marcozecchini 0:9fca2b23d0ba 22
marcozecchini 0:9fca2b23d0ba 23 #include <stdbool.h>
marcozecchini 0:9fca2b23d0ba 24 #include "mbed.h"
marcozecchini 0:9fca2b23d0ba 25
marcozecchini 0:9fca2b23d0ba 26 #if MBED_CONF_EVENTS_USE_LOWPOWER_TIMER_TICKER
marcozecchini 0:9fca2b23d0ba 27 #define ALIAS_TIMER LowPowerTimer
marcozecchini 0:9fca2b23d0ba 28 #define ALIAS_TICKER LowPowerTicker
marcozecchini 0:9fca2b23d0ba 29 #define ALIAS_TIMEOUT LowPowerTimeout
marcozecchini 0:9fca2b23d0ba 30 #else
marcozecchini 0:9fca2b23d0ba 31 #define ALIAS_TIMER Timer
marcozecchini 0:9fca2b23d0ba 32 #define ALIAS_TICKER Ticker
marcozecchini 0:9fca2b23d0ba 33 #define ALIAS_TIMEOUT Timeout
marcozecchini 0:9fca2b23d0ba 34 #endif
marcozecchini 0:9fca2b23d0ba 35
marcozecchini 0:9fca2b23d0ba 36 // Ticker operations
marcozecchini 0:9fca2b23d0ba 37 static bool equeue_tick_inited = false;
marcozecchini 0:9fca2b23d0ba 38 static volatile unsigned equeue_minutes = 0;
marcozecchini 0:9fca2b23d0ba 39 static unsigned equeue_timer[
marcozecchini 0:9fca2b23d0ba 40 (sizeof(ALIAS_TIMER)+sizeof(unsigned)-1)/sizeof(unsigned)];
marcozecchini 0:9fca2b23d0ba 41 static unsigned equeue_ticker[
marcozecchini 0:9fca2b23d0ba 42 (sizeof(ALIAS_TICKER)+sizeof(unsigned)-1)/sizeof(unsigned)];
marcozecchini 0:9fca2b23d0ba 43
marcozecchini 0:9fca2b23d0ba 44 static void equeue_tick_update() {
marcozecchini 0:9fca2b23d0ba 45 equeue_minutes += reinterpret_cast<ALIAS_TIMER*>(equeue_timer)->read_ms();
marcozecchini 0:9fca2b23d0ba 46 reinterpret_cast<ALIAS_TIMER*>(equeue_timer)->reset();
marcozecchini 0:9fca2b23d0ba 47 }
marcozecchini 0:9fca2b23d0ba 48
marcozecchini 0:9fca2b23d0ba 49 static void equeue_tick_init() {
marcozecchini 0:9fca2b23d0ba 50 MBED_STATIC_ASSERT(sizeof(equeue_timer) >= sizeof(ALIAS_TIMER),
marcozecchini 0:9fca2b23d0ba 51 "The equeue_timer buffer must fit the class Timer");
marcozecchini 0:9fca2b23d0ba 52 MBED_STATIC_ASSERT(sizeof(equeue_ticker) >= sizeof(ALIAS_TICKER),
marcozecchini 0:9fca2b23d0ba 53 "The equeue_ticker buffer must fit the class Ticker");
marcozecchini 0:9fca2b23d0ba 54 ALIAS_TIMER *timer = new (equeue_timer) ALIAS_TIMER;
marcozecchini 0:9fca2b23d0ba 55 ALIAS_TICKER *ticker = new (equeue_ticker) ALIAS_TICKER;
marcozecchini 0:9fca2b23d0ba 56
marcozecchini 0:9fca2b23d0ba 57 equeue_minutes = 0;
marcozecchini 0:9fca2b23d0ba 58 timer->start();
marcozecchini 0:9fca2b23d0ba 59 ticker->attach_us(equeue_tick_update, 1000 << 16);
marcozecchini 0:9fca2b23d0ba 60
marcozecchini 0:9fca2b23d0ba 61 equeue_tick_inited = true;
marcozecchini 0:9fca2b23d0ba 62 }
marcozecchini 0:9fca2b23d0ba 63
marcozecchini 0:9fca2b23d0ba 64 unsigned equeue_tick() {
marcozecchini 0:9fca2b23d0ba 65 if (!equeue_tick_inited) {
marcozecchini 0:9fca2b23d0ba 66 equeue_tick_init();
marcozecchini 0:9fca2b23d0ba 67 }
marcozecchini 0:9fca2b23d0ba 68
marcozecchini 0:9fca2b23d0ba 69 unsigned minutes;
marcozecchini 0:9fca2b23d0ba 70 unsigned ms;
marcozecchini 0:9fca2b23d0ba 71
marcozecchini 0:9fca2b23d0ba 72 do {
marcozecchini 0:9fca2b23d0ba 73 minutes = equeue_minutes;
marcozecchini 0:9fca2b23d0ba 74 ms = reinterpret_cast<ALIAS_TIMER*>(equeue_timer)->read_ms();
marcozecchini 0:9fca2b23d0ba 75 } while (minutes != equeue_minutes);
marcozecchini 0:9fca2b23d0ba 76
marcozecchini 0:9fca2b23d0ba 77 return minutes + ms;
marcozecchini 0:9fca2b23d0ba 78 }
marcozecchini 0:9fca2b23d0ba 79
marcozecchini 0:9fca2b23d0ba 80
marcozecchini 0:9fca2b23d0ba 81 // Mutex operations
marcozecchini 0:9fca2b23d0ba 82 int equeue_mutex_create(equeue_mutex_t *m) { return 0; }
marcozecchini 0:9fca2b23d0ba 83 void equeue_mutex_destroy(equeue_mutex_t *m) { }
marcozecchini 0:9fca2b23d0ba 84
marcozecchini 0:9fca2b23d0ba 85 void equeue_mutex_lock(equeue_mutex_t *m) {
marcozecchini 0:9fca2b23d0ba 86 core_util_critical_section_enter();
marcozecchini 0:9fca2b23d0ba 87 }
marcozecchini 0:9fca2b23d0ba 88
marcozecchini 0:9fca2b23d0ba 89 void equeue_mutex_unlock(equeue_mutex_t *m) {
marcozecchini 0:9fca2b23d0ba 90 core_util_critical_section_exit();
marcozecchini 0:9fca2b23d0ba 91 }
marcozecchini 0:9fca2b23d0ba 92
marcozecchini 0:9fca2b23d0ba 93
marcozecchini 0:9fca2b23d0ba 94 // Semaphore operations
marcozecchini 0:9fca2b23d0ba 95 #ifdef MBED_CONF_RTOS_PRESENT
marcozecchini 0:9fca2b23d0ba 96
marcozecchini 0:9fca2b23d0ba 97 int equeue_sema_create(equeue_sema_t *s) {
marcozecchini 0:9fca2b23d0ba 98 osEventFlagsAttr_t attr;
marcozecchini 0:9fca2b23d0ba 99 memset(&attr, 0, sizeof(attr));
marcozecchini 0:9fca2b23d0ba 100 attr.cb_mem = &s->mem;
marcozecchini 0:9fca2b23d0ba 101 attr.cb_size = sizeof(s->mem);
marcozecchini 0:9fca2b23d0ba 102
marcozecchini 0:9fca2b23d0ba 103 s->id = osEventFlagsNew(&attr);
marcozecchini 0:9fca2b23d0ba 104 return !s->id ? -1 : 0;
marcozecchini 0:9fca2b23d0ba 105 }
marcozecchini 0:9fca2b23d0ba 106
marcozecchini 0:9fca2b23d0ba 107 void equeue_sema_destroy(equeue_sema_t *s) {
marcozecchini 0:9fca2b23d0ba 108 osEventFlagsDelete(s->id);
marcozecchini 0:9fca2b23d0ba 109 }
marcozecchini 0:9fca2b23d0ba 110
marcozecchini 0:9fca2b23d0ba 111 void equeue_sema_signal(equeue_sema_t *s) {
marcozecchini 0:9fca2b23d0ba 112 osEventFlagsSet(s->id, 1);
marcozecchini 0:9fca2b23d0ba 113 }
marcozecchini 0:9fca2b23d0ba 114
marcozecchini 0:9fca2b23d0ba 115 bool equeue_sema_wait(equeue_sema_t *s, int ms) {
marcozecchini 0:9fca2b23d0ba 116 if (ms < 0) {
marcozecchini 0:9fca2b23d0ba 117 ms = osWaitForever;
marcozecchini 0:9fca2b23d0ba 118 }
marcozecchini 0:9fca2b23d0ba 119
marcozecchini 0:9fca2b23d0ba 120 return (osEventFlagsWait(s->id, 1, osFlagsWaitAny, ms) == 1);
marcozecchini 0:9fca2b23d0ba 121 }
marcozecchini 0:9fca2b23d0ba 122
marcozecchini 0:9fca2b23d0ba 123 #else
marcozecchini 0:9fca2b23d0ba 124
marcozecchini 0:9fca2b23d0ba 125 // Semaphore operations
marcozecchini 0:9fca2b23d0ba 126 int equeue_sema_create(equeue_sema_t *s) {
marcozecchini 0:9fca2b23d0ba 127 *s = false;
marcozecchini 0:9fca2b23d0ba 128 return 0;
marcozecchini 0:9fca2b23d0ba 129 }
marcozecchini 0:9fca2b23d0ba 130
marcozecchini 0:9fca2b23d0ba 131 void equeue_sema_destroy(equeue_sema_t *s) {
marcozecchini 0:9fca2b23d0ba 132 }
marcozecchini 0:9fca2b23d0ba 133
marcozecchini 0:9fca2b23d0ba 134 void equeue_sema_signal(equeue_sema_t *s) {
marcozecchini 0:9fca2b23d0ba 135 *s = 1;
marcozecchini 0:9fca2b23d0ba 136 }
marcozecchini 0:9fca2b23d0ba 137
marcozecchini 0:9fca2b23d0ba 138 static void equeue_sema_timeout(equeue_sema_t *s) {
marcozecchini 0:9fca2b23d0ba 139 *s = -1;
marcozecchini 0:9fca2b23d0ba 140 }
marcozecchini 0:9fca2b23d0ba 141
marcozecchini 0:9fca2b23d0ba 142 bool equeue_sema_wait(equeue_sema_t *s, int ms) {
marcozecchini 0:9fca2b23d0ba 143 int signal = 0;
marcozecchini 0:9fca2b23d0ba 144 ALIAS_TIMEOUT timeout;
marcozecchini 0:9fca2b23d0ba 145 if (ms == 0) {
marcozecchini 0:9fca2b23d0ba 146 return false;
marcozecchini 0:9fca2b23d0ba 147 } else if (ms > 0) {
marcozecchini 0:9fca2b23d0ba 148 timeout.attach_us(callback(equeue_sema_timeout, s), ms*1000);
marcozecchini 0:9fca2b23d0ba 149 }
marcozecchini 0:9fca2b23d0ba 150
marcozecchini 0:9fca2b23d0ba 151 core_util_critical_section_enter();
marcozecchini 0:9fca2b23d0ba 152 while (!*s) {
marcozecchini 0:9fca2b23d0ba 153 sleep();
marcozecchini 0:9fca2b23d0ba 154 core_util_critical_section_exit();
marcozecchini 0:9fca2b23d0ba 155 core_util_critical_section_enter();
marcozecchini 0:9fca2b23d0ba 156 }
marcozecchini 0:9fca2b23d0ba 157
marcozecchini 0:9fca2b23d0ba 158 signal = *s;
marcozecchini 0:9fca2b23d0ba 159 *s = false;
marcozecchini 0:9fca2b23d0ba 160 core_util_critical_section_exit();
marcozecchini 0:9fca2b23d0ba 161
marcozecchini 0:9fca2b23d0ba 162 return (signal > 0);
marcozecchini 0:9fca2b23d0ba 163 }
marcozecchini 0:9fca2b23d0ba 164
marcozecchini 0:9fca2b23d0ba 165 #endif
marcozecchini 0:9fca2b23d0ba 166
marcozecchini 0:9fca2b23d0ba 167 #endif