mbed-os5 only for TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Committer:
kenjiArai
Date:
Tue Dec 17 23:23:45 2019 +0000
Revision:
0:5b88d5760320
Child:
1:9db0e321a9f4
mbed-os5 only for TYBLE16

Who changed what in which revision?

UserRevisionLine numberNew contents of line
kenjiArai 0:5b88d5760320 1 /* mbed Microcontroller Library
kenjiArai 0:5b88d5760320 2 * Copyright (c) 2006-2012 ARM Limited
kenjiArai 0:5b88d5760320 3 *
kenjiArai 0:5b88d5760320 4 * Permission is hereby granted, free of charge, to any person obtaining a copy
kenjiArai 0:5b88d5760320 5 * of this software and associated documentation files (the "Software"), to deal
kenjiArai 0:5b88d5760320 6 * in the Software without restriction, including without limitation the rights
kenjiArai 0:5b88d5760320 7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
kenjiArai 0:5b88d5760320 8 * copies of the Software, and to permit persons to whom the Software is
kenjiArai 0:5b88d5760320 9 * furnished to do so, subject to the following conditions:
kenjiArai 0:5b88d5760320 10 *
kenjiArai 0:5b88d5760320 11 * The above copyright notice and this permission notice shall be included in
kenjiArai 0:5b88d5760320 12 * all copies or substantial portions of the Software.
kenjiArai 0:5b88d5760320 13 *
kenjiArai 0:5b88d5760320 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
kenjiArai 0:5b88d5760320 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
kenjiArai 0:5b88d5760320 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
kenjiArai 0:5b88d5760320 17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
kenjiArai 0:5b88d5760320 18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
kenjiArai 0:5b88d5760320 19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
kenjiArai 0:5b88d5760320 20 * SOFTWARE.
kenjiArai 0:5b88d5760320 21 */
kenjiArai 0:5b88d5760320 22 #ifndef SEMAPHORE_H
kenjiArai 0:5b88d5760320 23 #define SEMAPHORE_H
kenjiArai 0:5b88d5760320 24
kenjiArai 0:5b88d5760320 25 #include <stdint.h>
kenjiArai 0:5b88d5760320 26 #include "cmsis_os2.h"
kenjiArai 0:5b88d5760320 27 #include "mbed_rtos1_types.h"
kenjiArai 0:5b88d5760320 28 #include "mbed_rtos_storage.h"
kenjiArai 0:5b88d5760320 29 #include "platform/mbed_toolchain.h"
kenjiArai 0:5b88d5760320 30 #include "platform/NonCopyable.h"
kenjiArai 0:5b88d5760320 31
kenjiArai 0:5b88d5760320 32 namespace rtos {
kenjiArai 0:5b88d5760320 33 /** \addtogroup rtos */
kenjiArai 0:5b88d5760320 34 /** @{*/
kenjiArai 0:5b88d5760320 35 /**
kenjiArai 0:5b88d5760320 36 * \defgroup rtos_Semaphore Semaphore class
kenjiArai 0:5b88d5760320 37 * @{
kenjiArai 0:5b88d5760320 38 */
kenjiArai 0:5b88d5760320 39
kenjiArai 0:5b88d5760320 40 /** The Semaphore class is used to manage and protect access to a set of shared resources.
kenjiArai 0:5b88d5760320 41 *
kenjiArai 0:5b88d5760320 42 * @note
kenjiArai 0:5b88d5760320 43 * Memory considerations: The semaphore control structures will be created on current thread's stack, both for the mbed OS
kenjiArai 0:5b88d5760320 44 * and underlying RTOS objects (static or dynamic RTOS memory pools are not being used).
kenjiArai 0:5b88d5760320 45 */
kenjiArai 0:5b88d5760320 46 class Semaphore : private mbed::NonCopyable<Semaphore> {
kenjiArai 0:5b88d5760320 47 public:
kenjiArai 0:5b88d5760320 48 /** Create and Initialize a Semaphore object used for managing resources.
kenjiArai 0:5b88d5760320 49 @param count number of available resources; maximum index value is (count-1). (default: 0).
kenjiArai 0:5b88d5760320 50
kenjiArai 0:5b88d5760320 51 @note You cannot call this function from ISR context.
kenjiArai 0:5b88d5760320 52 */
kenjiArai 0:5b88d5760320 53 Semaphore(int32_t count = 0);
kenjiArai 0:5b88d5760320 54
kenjiArai 0:5b88d5760320 55 /** Create and Initialize a Semaphore object used for managing resources.
kenjiArai 0:5b88d5760320 56 @param count number of available resources
kenjiArai 0:5b88d5760320 57 @param max_count maximum number of available resources
kenjiArai 0:5b88d5760320 58
kenjiArai 0:5b88d5760320 59 @note You cannot call this function from ISR context.
kenjiArai 0:5b88d5760320 60 */
kenjiArai 0:5b88d5760320 61 Semaphore(int32_t count, uint16_t max_count);
kenjiArai 0:5b88d5760320 62
kenjiArai 0:5b88d5760320 63 /** Wait until a Semaphore resource becomes available.
kenjiArai 0:5b88d5760320 64
kenjiArai 0:5b88d5760320 65 @deprecated Do not use this function. This function has been replaced with acquire(), try_acquire() and try_acquire_for() functions.
kenjiArai 0:5b88d5760320 66
kenjiArai 0:5b88d5760320 67 @param millisec timeout value. (default: osWaitForever).
kenjiArai 0:5b88d5760320 68 @return number of available tokens, before taking one; or -1 in case of incorrect parameters
kenjiArai 0:5b88d5760320 69
kenjiArai 0:5b88d5760320 70 @note You may call this function from ISR context if the millisec parameter is set to 0.
kenjiArai 0:5b88d5760320 71 */
kenjiArai 0:5b88d5760320 72 MBED_DEPRECATED_SINCE("mbed-os-5.13", "Replaced with acquire, try_acquire() and try_acquire_for() functions")
kenjiArai 0:5b88d5760320 73 int32_t wait(uint32_t millisec = osWaitForever);
kenjiArai 0:5b88d5760320 74
kenjiArai 0:5b88d5760320 75 /** Wait until a Semaphore resource becomes available.
kenjiArai 0:5b88d5760320 76
kenjiArai 0:5b88d5760320 77 @deprecated Do not use this function. This function has been replaced with try_acquire_until().
kenjiArai 0:5b88d5760320 78
kenjiArai 0:5b88d5760320 79 @param millisec absolute timeout time, referenced to Kernel::get_ms_count()
kenjiArai 0:5b88d5760320 80 @return number of available tokens, before taking one; or -1 in case of incorrect parameters
kenjiArai 0:5b88d5760320 81 @note the underlying RTOS may have a limit to the maximum wait time
kenjiArai 0:5b88d5760320 82 due to internal 32-bit computations, but this is guaranteed to work if the
kenjiArai 0:5b88d5760320 83 wait is <= 0x7fffffff milliseconds (~24 days). If the limit is exceeded,
kenjiArai 0:5b88d5760320 84 the acquire attempt will time out earlier than specified.
kenjiArai 0:5b88d5760320 85
kenjiArai 0:5b88d5760320 86 @note You cannot call this function from ISR context.
kenjiArai 0:5b88d5760320 87 */
kenjiArai 0:5b88d5760320 88 MBED_DEPRECATED_SINCE("mbed-os-5.13", "Replaced with try_acquire_until()")
kenjiArai 0:5b88d5760320 89 int32_t wait_until(uint64_t millisec);
kenjiArai 0:5b88d5760320 90
kenjiArai 0:5b88d5760320 91 /** Wait until a Semaphore resource becomes available.
kenjiArai 0:5b88d5760320 92
kenjiArai 0:5b88d5760320 93 @note You cannot call this function from ISR context.
kenjiArai 0:5b88d5760320 94 */
kenjiArai 0:5b88d5760320 95 void acquire();
kenjiArai 0:5b88d5760320 96
kenjiArai 0:5b88d5760320 97 /** Try to acquire a Semaphore resource, and return immediately
kenjiArai 0:5b88d5760320 98 @return true if a resource was acquired, false otherwise.
kenjiArai 0:5b88d5760320 99 @note equivalent to try_acquire_for(0)
kenjiArai 0:5b88d5760320 100
kenjiArai 0:5b88d5760320 101 @note You may call this function from ISR context.
kenjiArai 0:5b88d5760320 102 */
kenjiArai 0:5b88d5760320 103 bool try_acquire();
kenjiArai 0:5b88d5760320 104
kenjiArai 0:5b88d5760320 105 /** Wait until a Semaphore resource becomes available.
kenjiArai 0:5b88d5760320 106 @param millisec timeout value.
kenjiArai 0:5b88d5760320 107 @return true if a resource was acquired, false otherwise.
kenjiArai 0:5b88d5760320 108
kenjiArai 0:5b88d5760320 109 @note You may call this function from ISR context if the millisec parameter is set to 0.
kenjiArai 0:5b88d5760320 110 */
kenjiArai 0:5b88d5760320 111 bool try_acquire_for(uint32_t millisec);
kenjiArai 0:5b88d5760320 112
kenjiArai 0:5b88d5760320 113 /** Wait until a Semaphore resource becomes available.
kenjiArai 0:5b88d5760320 114 @param millisec absolute timeout time, referenced to Kernel::get_ms_count()
kenjiArai 0:5b88d5760320 115 @return true if a resource was acquired, false otherwise.
kenjiArai 0:5b88d5760320 116 @note the underlying RTOS may have a limit to the maximum wait time
kenjiArai 0:5b88d5760320 117 due to internal 32-bit computations, but this is guaranteed to work if the
kenjiArai 0:5b88d5760320 118 wait is <= 0x7fffffff milliseconds (~24 days). If the limit is exceeded,
kenjiArai 0:5b88d5760320 119 the acquire attempt will time out earlier than specified.
kenjiArai 0:5b88d5760320 120
kenjiArai 0:5b88d5760320 121 @note You cannot call this function from ISR context.
kenjiArai 0:5b88d5760320 122 */
kenjiArai 0:5b88d5760320 123 bool try_acquire_until(uint64_t millisec);
kenjiArai 0:5b88d5760320 124
kenjiArai 0:5b88d5760320 125 /** Release a Semaphore resource that was obtain with Semaphore::acquire.
kenjiArai 0:5b88d5760320 126 @return status code that indicates the execution status of the function:
kenjiArai 0:5b88d5760320 127 @a osOK the token has been correctly released.
kenjiArai 0:5b88d5760320 128 @a osErrorResource the maximum token count has been reached.
kenjiArai 0:5b88d5760320 129 @a osErrorParameter internal error.
kenjiArai 0:5b88d5760320 130
kenjiArai 0:5b88d5760320 131 @note You may call this function from ISR context.
kenjiArai 0:5b88d5760320 132 */
kenjiArai 0:5b88d5760320 133 osStatus release(void);
kenjiArai 0:5b88d5760320 134
kenjiArai 0:5b88d5760320 135 /** Semaphore destructor
kenjiArai 0:5b88d5760320 136 *
kenjiArai 0:5b88d5760320 137 * @note You cannot call this function from ISR context.
kenjiArai 0:5b88d5760320 138 */
kenjiArai 0:5b88d5760320 139 ~Semaphore();
kenjiArai 0:5b88d5760320 140
kenjiArai 0:5b88d5760320 141 private:
kenjiArai 0:5b88d5760320 142 void constructor(int32_t count, uint16_t max_count);
kenjiArai 0:5b88d5760320 143
kenjiArai 0:5b88d5760320 144 int32_t _wait(uint32_t millisec);
kenjiArai 0:5b88d5760320 145
kenjiArai 0:5b88d5760320 146 osSemaphoreId_t _id;
kenjiArai 0:5b88d5760320 147 mbed_rtos_storage_semaphore_t _obj_mem;
kenjiArai 0:5b88d5760320 148 };
kenjiArai 0:5b88d5760320 149 /** @}*/
kenjiArai 0:5b88d5760320 150 /** @}*/
kenjiArai 0:5b88d5760320 151 }
kenjiArai 0:5b88d5760320 152 #endif
kenjiArai 0:5b88d5760320 153
kenjiArai 0:5b88d5760320 154