ON Semiconductor / mbed-os

Dependents:   mbed-TFT-example-NCS36510 mbed-Accelerometer-example-NCS36510 mbed-Accelerometer-example-NCS36510

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 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     reinterpret_cast<Timer*>(equeue_timer)->reset();
00037     equeue_minutes += 1;
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, (1 << 16)*1000);
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 equeue_ms = reinterpret_cast<Timer*>(equeue_timer)->read_ms();
00062     return (equeue_minutes << 16) + equeue_ms;
00063 }
00064 
00065 
00066 // Mutex operations
00067 int equeue_mutex_create(equeue_mutex_t *m) { return 0; }
00068 void equeue_mutex_destroy(equeue_mutex_t *m) { }
00069 
00070 void equeue_mutex_lock(equeue_mutex_t *m) {
00071     core_util_critical_section_enter();
00072 }
00073 
00074 void equeue_mutex_unlock(equeue_mutex_t *m) {
00075     core_util_critical_section_exit();
00076 }
00077 
00078 
00079 // Semaphore operations
00080 #ifdef MBED_CONF_RTOS_PRESENT
00081 
00082 int equeue_sema_create(equeue_sema_t *s) {
00083     MBED_STATIC_ASSERT(sizeof(equeue_sema_t) >= sizeof(Semaphore),
00084             "The equeue_sema_t must fit the class Semaphore");
00085     new (s) Semaphore(0);
00086     return 0;
00087 }
00088 
00089 void equeue_sema_destroy(equeue_sema_t *s) {
00090     reinterpret_cast<Semaphore*>(s)->~Semaphore();
00091 }
00092 
00093 void equeue_sema_signal(equeue_sema_t *s) {
00094     reinterpret_cast<Semaphore*>(s)->release();
00095 }
00096 
00097 bool equeue_sema_wait(equeue_sema_t *s, int ms) {
00098     if (ms < 0) {
00099         ms = osWaitForever;
00100     }
00101 
00102     return (reinterpret_cast<Semaphore*>(s)->wait(ms) > 0);
00103 }
00104 
00105 #else
00106 
00107 // Semaphore operations
00108 int equeue_sema_create(equeue_sema_t *s) {
00109     *s = false;
00110     return 0;
00111 }
00112 
00113 void equeue_sema_destroy(equeue_sema_t *s) {
00114 }
00115 
00116 void equeue_sema_signal(equeue_sema_t *s) {
00117     *s = 1;
00118 }
00119 
00120 static void equeue_sema_timeout(equeue_sema_t *s) {
00121     *s = -1;
00122 }
00123 
00124 bool equeue_sema_wait(equeue_sema_t *s, int ms) {
00125     int signal = 0;
00126     Timeout timeout;
00127     if (ms > 0) {
00128         timeout.attach_us(callback(equeue_sema_timeout, s), ms*1000);
00129     }
00130 
00131     core_util_critical_section_enter();
00132     while (!*s) {
00133         sleep();
00134         core_util_critical_section_exit();
00135         core_util_critical_section_enter();
00136     }
00137 
00138     signal = *s;
00139     *s = false;
00140     core_util_critical_section_exit();
00141 
00142     return (signal > 0);
00143 }
00144 
00145 #endif
00146 
00147 #endif