This is a fork of the `events` subdirectory of https://github.com/ARMmbed/mbed-os

Dependents:   HelloWorld_CCA01M1 HelloWorld_CCA02M1 CI-data-logger-server HelloWorld_CCA02M1 ... more

This is a fork of the events subdirectory of https://github.com/ARMmbed/mbed-os.

Note, you must import this library with import name: events!!!

Committer:
Wolfgang Betz
Date:
Tue Sep 05 09:09:24 2017 +0200
Revision:
9832:b95afde9ef7e
Parent:
9828:b778d3912beb
Merge branch 'master' of hg::http://developer.mbed.org/teams/ST/code/ST_Events into events-split

Who changed what in which revision?

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