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_platform.h@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 | /** \addtogroup events */ |
dkato | 0:f782d9c66c49 | 3 | /** @{*/ |
dkato | 0:f782d9c66c49 | 4 | /* |
dkato | 0:f782d9c66c49 | 5 | * System specific implementation |
dkato | 0:f782d9c66c49 | 6 | * |
dkato | 0:f782d9c66c49 | 7 | * Copyright (c) 2016 Christopher Haster |
dkato | 0:f782d9c66c49 | 8 | * |
dkato | 0:f782d9c66c49 | 9 | * Licensed under the Apache License, Version 2.0 (the "License"); |
dkato | 0:f782d9c66c49 | 10 | * you may not use this file except in compliance with the License. |
dkato | 0:f782d9c66c49 | 11 | * You may obtain a copy of the License at |
dkato | 0:f782d9c66c49 | 12 | * |
dkato | 0:f782d9c66c49 | 13 | * http://www.apache.org/licenses/LICENSE-2.0 |
dkato | 0:f782d9c66c49 | 14 | * |
dkato | 0:f782d9c66c49 | 15 | * Unless required by applicable law or agreed to in writing, software |
dkato | 0:f782d9c66c49 | 16 | * distributed under the License is distributed on an "AS IS" BASIS, |
dkato | 0:f782d9c66c49 | 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
dkato | 0:f782d9c66c49 | 18 | * See the License for the specific language governing permissions and |
dkato | 0:f782d9c66c49 | 19 | * limitations under the License. |
dkato | 0:f782d9c66c49 | 20 | */ |
dkato | 0:f782d9c66c49 | 21 | #ifndef EQUEUE_PLATFORM_H |
dkato | 0:f782d9c66c49 | 22 | #define EQUEUE_PLATFORM_H |
dkato | 0:f782d9c66c49 | 23 | |
dkato | 0:f782d9c66c49 | 24 | #ifdef __cplusplus |
dkato | 0:f782d9c66c49 | 25 | extern "C" { |
dkato | 0:f782d9c66c49 | 26 | #endif |
dkato | 0:f782d9c66c49 | 27 | |
dkato | 0:f782d9c66c49 | 28 | #include <stdbool.h> |
dkato | 0:f782d9c66c49 | 29 | |
dkato | 0:f782d9c66c49 | 30 | // Currently supported platforms |
dkato | 0:f782d9c66c49 | 31 | // |
dkato | 0:f782d9c66c49 | 32 | // Uncomment to select a supported platform or reimplement this file |
dkato | 0:f782d9c66c49 | 33 | // for a specific target. |
dkato | 0:f782d9c66c49 | 34 | //#define EQUEUE_PLATFORM_POSIX |
dkato | 0:f782d9c66c49 | 35 | //#define EQUEUE_PLATFORM_MBED |
dkato | 0:f782d9c66c49 | 36 | |
dkato | 0:f782d9c66c49 | 37 | // Try to infer a platform if none was manually selected |
dkato | 0:f782d9c66c49 | 38 | #if !defined(EQUEUE_PLATFORM_POSIX) \ |
dkato | 0:f782d9c66c49 | 39 | && !defined(EQUEUE_PLATFORM_MBED) |
dkato | 0:f782d9c66c49 | 40 | #if defined(__unix__) |
dkato | 0:f782d9c66c49 | 41 | #define EQUEUE_PLATFORM_POSIX |
dkato | 0:f782d9c66c49 | 42 | #elif defined(__MBED__) |
dkato | 0:f782d9c66c49 | 43 | #define EQUEUE_PLATFORM_MBED |
dkato | 0:f782d9c66c49 | 44 | #else |
dkato | 0:f782d9c66c49 | 45 | #warning "Unknown platform! Please update equeue_platform.h" |
dkato | 0:f782d9c66c49 | 46 | #endif |
dkato | 0:f782d9c66c49 | 47 | #endif |
dkato | 0:f782d9c66c49 | 48 | |
dkato | 0:f782d9c66c49 | 49 | // Platform includes |
dkato | 0:f782d9c66c49 | 50 | #if defined(EQUEUE_PLATFORM_POSIX) |
dkato | 0:f782d9c66c49 | 51 | #include <pthread.h> |
dkato | 0:f782d9c66c49 | 52 | #endif |
dkato | 0:f782d9c66c49 | 53 | |
dkato | 0:f782d9c66c49 | 54 | |
dkato | 0:f782d9c66c49 | 55 | // Platform millisecond counter |
dkato | 0:f782d9c66c49 | 56 | // |
dkato | 0:f782d9c66c49 | 57 | // Return a tick that represents the number of milliseconds that have passed |
dkato | 0:f782d9c66c49 | 58 | // since an arbitrary point in time. The granularity does not need to be at |
dkato | 0:f782d9c66c49 | 59 | // the millisecond level, however the accuracy of the equeue library is |
dkato | 0:f782d9c66c49 | 60 | // limited by the accuracy of this tick. |
dkato | 0:f782d9c66c49 | 61 | // |
dkato | 0:f782d9c66c49 | 62 | // Must intentionally overflow to 0 after 2^32-1 |
dkato | 0:f782d9c66c49 | 63 | unsigned equeue_tick(void); |
dkato | 0:f782d9c66c49 | 64 | |
dkato | 0:f782d9c66c49 | 65 | |
dkato | 0:f782d9c66c49 | 66 | // Platform mutex type |
dkato | 0:f782d9c66c49 | 67 | // |
dkato | 0:f782d9c66c49 | 68 | // The equeue library requires at minimum a non-recursive mutex that is |
dkato | 0:f782d9c66c49 | 69 | // safe in interrupt contexts. The mutex section is help for a bounded |
dkato | 0:f782d9c66c49 | 70 | // amount of time, so simply disabling interrupts is acceptable |
dkato | 0:f782d9c66c49 | 71 | // |
dkato | 0:f782d9c66c49 | 72 | // If irq safety is not required, a regular blocking mutex can be used. |
dkato | 0:f782d9c66c49 | 73 | #if defined(EQUEUE_PLATFORM_POSIX) |
dkato | 0:f782d9c66c49 | 74 | typedef pthread_mutex_t equeue_mutex_t; |
dkato | 0:f782d9c66c49 | 75 | #elif defined(EQUEUE_PLATFORM_WINDOWS) |
dkato | 0:f782d9c66c49 | 76 | typedef CRITICAL_SECTION equeue_mutex_t; |
dkato | 0:f782d9c66c49 | 77 | #elif defined(EQUEUE_PLATFORM_MBED) |
dkato | 0:f782d9c66c49 | 78 | typedef unsigned equeue_mutex_t; |
dkato | 0:f782d9c66c49 | 79 | #elif defined(EQUEUE_PLATFORM_FREERTOS) |
dkato | 0:f782d9c66c49 | 80 | typedef UBaseType_t equeue_mutex_t; |
dkato | 0:f782d9c66c49 | 81 | #endif |
dkato | 0:f782d9c66c49 | 82 | |
dkato | 0:f782d9c66c49 | 83 | // Platform mutex operations |
dkato | 0:f782d9c66c49 | 84 | // |
dkato | 0:f782d9c66c49 | 85 | // The equeue_mutex_create and equeue_mutex_destroy manage the lifetime |
dkato | 0:f782d9c66c49 | 86 | // of the mutex. On error, equeue_mutex_create should return a negative |
dkato | 0:f782d9c66c49 | 87 | // error code. |
dkato | 0:f782d9c66c49 | 88 | // |
dkato | 0:f782d9c66c49 | 89 | // The equeue_mutex_lock and equeue_mutex_unlock lock and unlock the |
dkato | 0:f782d9c66c49 | 90 | // underlying mutex. |
dkato | 0:f782d9c66c49 | 91 | int equeue_mutex_create(equeue_mutex_t *mutex); |
dkato | 0:f782d9c66c49 | 92 | void equeue_mutex_destroy(equeue_mutex_t *mutex); |
dkato | 0:f782d9c66c49 | 93 | void equeue_mutex_lock(equeue_mutex_t *mutex); |
dkato | 0:f782d9c66c49 | 94 | void equeue_mutex_unlock(equeue_mutex_t *mutex); |
dkato | 0:f782d9c66c49 | 95 | |
dkato | 0:f782d9c66c49 | 96 | |
dkato | 0:f782d9c66c49 | 97 | // Platform semaphore type |
dkato | 0:f782d9c66c49 | 98 | // |
dkato | 0:f782d9c66c49 | 99 | // The equeue library requires a binary semaphore type that can be safely |
dkato | 0:f782d9c66c49 | 100 | // signaled from interrupt contexts and from inside a equeue_mutex section. |
dkato | 0:f782d9c66c49 | 101 | // |
dkato | 0:f782d9c66c49 | 102 | // The equeue_signal_wait is relied upon by the equeue library to sleep the |
dkato | 0:f782d9c66c49 | 103 | // processor between events. Spurious wakeups have no negative-effects. |
dkato | 0:f782d9c66c49 | 104 | // |
dkato | 0:f782d9c66c49 | 105 | // A counting semaphore will also work, however may cause the event queue |
dkato | 0:f782d9c66c49 | 106 | // dispatch loop to run unnecessarily. For that matter, equeue_signal_wait |
dkato | 0:f782d9c66c49 | 107 | // may even be implemented as a single return statement. |
dkato | 0:f782d9c66c49 | 108 | #if defined(EQUEUE_PLATFORM_POSIX) |
dkato | 0:f782d9c66c49 | 109 | typedef struct equeue_sema { |
dkato | 0:f782d9c66c49 | 110 | pthread_mutex_t mutex; |
dkato | 0:f782d9c66c49 | 111 | pthread_cond_t cond; |
dkato | 0:f782d9c66c49 | 112 | bool signal; |
dkato | 0:f782d9c66c49 | 113 | } equeue_sema_t; |
dkato | 0:f782d9c66c49 | 114 | #elif defined(EQUEUE_PLATFORM_MBED) && defined(MBED_CONF_RTOS_PRESENT) |
dkato | 0:f782d9c66c49 | 115 | typedef unsigned equeue_sema_t[8]; |
dkato | 0:f782d9c66c49 | 116 | #elif defined(EQUEUE_PLATFORM_MBED) |
dkato | 0:f782d9c66c49 | 117 | typedef volatile int equeue_sema_t; |
dkato | 0:f782d9c66c49 | 118 | #endif |
dkato | 0:f782d9c66c49 | 119 | |
dkato | 0:f782d9c66c49 | 120 | // Platform semaphore operations |
dkato | 0:f782d9c66c49 | 121 | // |
dkato | 0:f782d9c66c49 | 122 | // The equeue_sema_create and equeue_sema_destroy manage the lifetime |
dkato | 0:f782d9c66c49 | 123 | // of the semaphore. On error, equeue_sema_create should return a negative |
dkato | 0:f782d9c66c49 | 124 | // error code. |
dkato | 0:f782d9c66c49 | 125 | // |
dkato | 0:f782d9c66c49 | 126 | // The equeue_sema_signal marks a semaphore as signalled such that the next |
dkato | 0:f782d9c66c49 | 127 | // equeue_sema_wait will return true. |
dkato | 0:f782d9c66c49 | 128 | // |
dkato | 0:f782d9c66c49 | 129 | // The equeue_sema_wait waits for a semaphore to be signalled or returns |
dkato | 0:f782d9c66c49 | 130 | // immediately if equeue_sema_signal had been called since the last |
dkato | 0:f782d9c66c49 | 131 | // equeue_sema_wait. The equeue_sema_wait returns true if it detected that |
dkato | 0:f782d9c66c49 | 132 | // equeue_sema_signal had been called. |
dkato | 0:f782d9c66c49 | 133 | int equeue_sema_create(equeue_sema_t *sema); |
dkato | 0:f782d9c66c49 | 134 | void equeue_sema_destroy(equeue_sema_t *sema); |
dkato | 0:f782d9c66c49 | 135 | void equeue_sema_signal(equeue_sema_t *sema); |
dkato | 0:f782d9c66c49 | 136 | bool equeue_sema_wait(equeue_sema_t *sema, int ms); |
dkato | 0:f782d9c66c49 | 137 | |
dkato | 0:f782d9c66c49 | 138 | |
dkato | 0:f782d9c66c49 | 139 | #ifdef __cplusplus |
dkato | 0:f782d9c66c49 | 140 | } |
dkato | 0:f782d9c66c49 | 141 | #endif |
dkato | 0:f782d9c66c49 | 142 | |
dkato | 0:f782d9c66c49 | 143 | #endif |
dkato | 0:f782d9c66c49 | 144 | |
dkato | 0:f782d9c66c49 | 145 | /** @}*/ |