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