Rizky Ardi Maulana / mbed-os
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_ASSERT(sizeof(equeue_timer) >= sizeof(Timer));
00042     MBED_ASSERT(sizeof(equeue_ticker) >= sizeof(Ticker));
00043     new (equeue_timer) Timer;
00044     new (equeue_ticker) Ticker;
00045 
00046     equeue_minutes = 0;
00047     reinterpret_cast<Timer*>(equeue_timer)->start();
00048     reinterpret_cast<Ticker*>(equeue_ticker)
00049             ->attach_us(equeue_tick_update, (1 << 16)*1000);
00050 
00051     equeue_tick_inited = true;
00052 }
00053 
00054 unsigned equeue_tick() {
00055     if (!equeue_tick_inited) {
00056         equeue_tick_init();
00057     }
00058 
00059     unsigned equeue_ms = reinterpret_cast<Timer*>(equeue_timer)->read_ms();
00060     return (equeue_minutes << 16) + equeue_ms;
00061 }
00062 
00063 
00064 // Mutex operations
00065 int equeue_mutex_create(equeue_mutex_t *m) { return 0; }
00066 void equeue_mutex_destroy(equeue_mutex_t *m) { }
00067 
00068 void equeue_mutex_lock(equeue_mutex_t *m) {
00069     core_util_critical_section_enter();
00070 }
00071 
00072 void equeue_mutex_unlock(equeue_mutex_t *m) {
00073     core_util_critical_section_exit();
00074 }
00075 
00076 
00077 // Semaphore operations
00078 #ifdef MBED_CONF_RTOS_PRESENT
00079 
00080 int equeue_sema_create(equeue_sema_t *s) {
00081     MBED_ASSERT(sizeof(equeue_sema_t) >= sizeof(Semaphore));
00082     new (s) Semaphore(0);
00083     return 0;
00084 }
00085 
00086 void equeue_sema_destroy(equeue_sema_t *s) {
00087     reinterpret_cast<Semaphore*>(s)->~Semaphore();
00088 }
00089 
00090 void equeue_sema_signal(equeue_sema_t *s) {
00091     reinterpret_cast<Semaphore*>(s)->release();
00092 }
00093 
00094 bool equeue_sema_wait(equeue_sema_t *s, int ms) {
00095     if (ms < 0) {
00096         ms = osWaitForever;
00097     }
00098 
00099     return (reinterpret_cast<Semaphore*>(s)->wait(ms) > 0);
00100 }
00101 
00102 #else
00103 
00104 // Semaphore operations
00105 int equeue_sema_create(equeue_sema_t *s) {
00106     *s = false;
00107     return 0;
00108 }
00109 
00110 void equeue_sema_destroy(equeue_sema_t *s) {
00111 }
00112 
00113 void equeue_sema_signal(equeue_sema_t *s) {
00114     *s = 1;
00115 }
00116 
00117 static void equeue_sema_timeout(equeue_sema_t *s) {
00118     *s = -1;
00119 }
00120 
00121 bool equeue_sema_wait(equeue_sema_t *s, int ms) {
00122     int signal = 0;
00123     Timeout timeout;
00124     timeout.attach_us(s, equeue_sema_timeout, ms*1000);
00125 
00126     core_util_critical_section_enter();
00127     while (!*s) {
00128         sleep();
00129         core_util_critical_section_exit();
00130         core_util_critical_section_enter();
00131     }
00132 
00133     signal = *s;
00134     *s = false;
00135     core_util_critical_section_exit();
00136 
00137     return (signal > 0);
00138 }
00139 
00140 #endif
00141 
00142 #endif