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