Daiki Kato / mbed-os-lychee

Dependents:   mbed-os-example-blinky-gr-lychee GR-Boads_Camera_sample GR-Boards_Audio_Recoder GR-Boads_Camera_DisplayApp ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers equeue_mbed.cpp Source File

equeue_mbed.cpp

00001 /*
00002  * Implementation for the mbed library
00003  * https://github.com/mbedmicro/mbed
00004  *
00005  * Copyright (c) 2016 Christopher Haster
00006  *
00007  * Licensed under the Apache License, Version 2.0 (the "License");
00008  * you may not use this file except in compliance with the License.
00009  * You may obtain a copy of the License at
00010  *
00011  *     http://www.apache.org/licenses/LICENSE-2.0
00012  *
00013  * Unless required by applicable law or agreed to in writing, software
00014  * distributed under the License is distributed on an "AS IS" BASIS,
00015  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00016  * See the License for the specific language governing permissions and
00017  * limitations under the License.
00018  */
00019 #include "equeue/equeue_platform.h"
00020 
00021 #if defined(EQUEUE_PLATFORM_MBED)
00022 
00023 #include <stdbool.h>
00024 #include "mbed.h"
00025 
00026 
00027 // Ticker operations
00028 static bool equeue_tick_inited = false;
00029 static volatile unsigned equeue_minutes = 0;
00030 static unsigned equeue_timer[
00031         (sizeof(Timer)+sizeof(unsigned)-1)/sizeof(unsigned)];
00032 static unsigned equeue_ticker[
00033         (sizeof(Ticker)+sizeof(unsigned)-1)/sizeof(unsigned)];
00034 
00035 static void equeue_tick_update() {
00036     equeue_minutes += reinterpret_cast<Timer*>(equeue_timer)->read_ms();
00037     reinterpret_cast<Timer*>(equeue_timer)->reset();
00038 }
00039 
00040 static void equeue_tick_init() {
00041     MBED_STATIC_ASSERT(sizeof(equeue_timer) >= sizeof(Timer),
00042             "The equeue_timer buffer must fit the class Timer");
00043     MBED_STATIC_ASSERT(sizeof(equeue_ticker) >= sizeof(Ticker),
00044             "The equeue_ticker buffer must fit the class Ticker");
00045     new (equeue_timer) Timer;
00046     new (equeue_ticker) Ticker;
00047 
00048     equeue_minutes = 0;
00049     reinterpret_cast<Timer*>(equeue_timer)->start();
00050     reinterpret_cast<Ticker*>(equeue_ticker)
00051             ->attach_us(equeue_tick_update, 1000 << 16);
00052 
00053     equeue_tick_inited = true;
00054 }
00055 
00056 unsigned equeue_tick() {
00057     if (!equeue_tick_inited) {
00058         equeue_tick_init();
00059     }
00060 
00061     unsigned minutes;
00062     unsigned ms;
00063 
00064     do {
00065         minutes = equeue_minutes;
00066         ms = reinterpret_cast<Timer*>(equeue_timer)->read_ms();
00067     } while (minutes != equeue_minutes);
00068 
00069     return minutes + ms;
00070 }
00071 
00072 
00073 // Mutex operations
00074 int equeue_mutex_create(equeue_mutex_t *m) { return 0; }
00075 void equeue_mutex_destroy(equeue_mutex_t *m) { }
00076 
00077 void equeue_mutex_lock(equeue_mutex_t *m) {
00078     core_util_critical_section_enter();
00079 }
00080 
00081 void equeue_mutex_unlock(equeue_mutex_t *m) {
00082     core_util_critical_section_exit();
00083 }
00084 
00085 
00086 // Semaphore operations
00087 #ifdef MBED_CONF_RTOS_PRESENT
00088 
00089 int equeue_sema_create(equeue_sema_t *s) {
00090     MBED_STATIC_ASSERT(sizeof(equeue_sema_t) >= sizeof(Semaphore),
00091             "The equeue_sema_t must fit the class Semaphore");
00092     new (s) Semaphore(0);
00093     return 0;
00094 }
00095 
00096 void equeue_sema_destroy(equeue_sema_t *s) {
00097     reinterpret_cast<Semaphore*>(s)->~Semaphore();
00098 }
00099 
00100 void equeue_sema_signal(equeue_sema_t *s) {
00101     reinterpret_cast<Semaphore*>(s)->release();
00102 }
00103 
00104 bool equeue_sema_wait(equeue_sema_t *s, int ms) {
00105     if (ms < 0) {
00106         ms = osWaitForever;
00107     }
00108 
00109     return (reinterpret_cast<Semaphore*>(s)->wait(ms) > 0);
00110 }
00111 
00112 #else
00113 
00114 // Semaphore operations
00115 int equeue_sema_create(equeue_sema_t *s) {
00116     *s = false;
00117     return 0;
00118 }
00119 
00120 void equeue_sema_destroy(equeue_sema_t *s) {
00121 }
00122 
00123 void equeue_sema_signal(equeue_sema_t *s) {
00124     *s = 1;
00125 }
00126 
00127 static void equeue_sema_timeout(equeue_sema_t *s) {
00128     *s = -1;
00129 }
00130 
00131 bool equeue_sema_wait(equeue_sema_t *s, int ms) {
00132     int signal = 0;
00133     Timeout timeout;
00134     if (ms > 0) {
00135         timeout.attach_us(callback(equeue_sema_timeout, s), ms*1000);
00136     }
00137 
00138     core_util_critical_section_enter();
00139     while (!*s) {
00140         sleep();
00141         core_util_critical_section_exit();
00142         core_util_critical_section_enter();
00143     }
00144 
00145     signal = *s;
00146     *s = false;
00147     core_util_critical_section_exit();
00148 
00149     return (signal > 0);
00150 }
00151 
00152 #endif
00153 
00154 #endif