mbed-os

Dependents:   cobaLCDJoyMotor_Thread odometry_omni_3roda_v3 odometry_omni_3roda_v1 odometry_omni_3roda_v2 ... more

Committer:
be_bryan
Date:
Mon Dec 11 17:54:04 2017 +0000
Revision:
0:b74591d5ab33
motor ++

Who changed what in which revision?

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