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