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