mbed-os5 only for TYBLE16
Dependents: TYBLE16_simple_data_logger TYBLE16_MP3_Air
rtos/Mail.h@0:5b88d5760320, 2019-12-17 (annotated)
- 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?
User | Revision | Line number | New contents of line |
---|---|---|---|
kenjiArai | 0:5b88d5760320 | 1 | /* mbed Microcontroller Library |
kenjiArai | 0:5b88d5760320 | 2 | * Copyright (c) 2006-2017 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 MAIL_H |
kenjiArai | 0:5b88d5760320 | 23 | #define MAIL_H |
kenjiArai | 0:5b88d5760320 | 24 | |
kenjiArai | 0:5b88d5760320 | 25 | #include <stdint.h> |
kenjiArai | 0:5b88d5760320 | 26 | #include <string.h> |
kenjiArai | 0:5b88d5760320 | 27 | |
kenjiArai | 0:5b88d5760320 | 28 | #include "Queue.h" |
kenjiArai | 0:5b88d5760320 | 29 | #include "MemoryPool.h" |
kenjiArai | 0:5b88d5760320 | 30 | #include "cmsis_os2.h" |
kenjiArai | 0:5b88d5760320 | 31 | #include "mbed_rtos_storage.h" |
kenjiArai | 0:5b88d5760320 | 32 | #include "mbed_rtos1_types.h" |
kenjiArai | 0:5b88d5760320 | 33 | |
kenjiArai | 0:5b88d5760320 | 34 | #include "platform/mbed_toolchain.h" |
kenjiArai | 0:5b88d5760320 | 35 | #include "platform/NonCopyable.h" |
kenjiArai | 0:5b88d5760320 | 36 | |
kenjiArai | 0:5b88d5760320 | 37 | #ifndef MBED_NO_GLOBAL_USING_DIRECTIVE |
kenjiArai | 0:5b88d5760320 | 38 | using namespace rtos; |
kenjiArai | 0:5b88d5760320 | 39 | #endif |
kenjiArai | 0:5b88d5760320 | 40 | |
kenjiArai | 0:5b88d5760320 | 41 | namespace rtos { |
kenjiArai | 0:5b88d5760320 | 42 | /** \addtogroup rtos */ |
kenjiArai | 0:5b88d5760320 | 43 | /** @{*/ |
kenjiArai | 0:5b88d5760320 | 44 | /** |
kenjiArai | 0:5b88d5760320 | 45 | * \defgroup rtos_Mail Mail class |
kenjiArai | 0:5b88d5760320 | 46 | * @{ |
kenjiArai | 0:5b88d5760320 | 47 | */ |
kenjiArai | 0:5b88d5760320 | 48 | |
kenjiArai | 0:5b88d5760320 | 49 | /** The Mail class allows you to control, send, receive or wait for mail. |
kenjiArai | 0:5b88d5760320 | 50 | * A mail is a memory block that is sent to a thread or interrupt service routine (ISR). |
kenjiArai | 0:5b88d5760320 | 51 | * @tparam T Data type of a single mail message element. |
kenjiArai | 0:5b88d5760320 | 52 | * @tparam queue_sz Maximum number of mail messages in queue. |
kenjiArai | 0:5b88d5760320 | 53 | * |
kenjiArai | 0:5b88d5760320 | 54 | * @note |
kenjiArai | 0:5b88d5760320 | 55 | * Memory considerations: The mail data store and control structures are part of this class - they do not (themselves) |
kenjiArai | 0:5b88d5760320 | 56 | * allocate memory on the heap, both for the Mbed OS and underlying RTOS objects (static or dynamic RTOS memory |
kenjiArai | 0:5b88d5760320 | 57 | * pools are not being used). |
kenjiArai | 0:5b88d5760320 | 58 | */ |
kenjiArai | 0:5b88d5760320 | 59 | template<typename T, uint32_t queue_sz> |
kenjiArai | 0:5b88d5760320 | 60 | class Mail : private mbed::NonCopyable<Mail<T, queue_sz> > { |
kenjiArai | 0:5b88d5760320 | 61 | public: |
kenjiArai | 0:5b88d5760320 | 62 | /** Create and initialize Mail queue. |
kenjiArai | 0:5b88d5760320 | 63 | * |
kenjiArai | 0:5b88d5760320 | 64 | * @note You cannot call this function from ISR context. |
kenjiArai | 0:5b88d5760320 | 65 | */ |
kenjiArai | 0:5b88d5760320 | 66 | Mail() { }; |
kenjiArai | 0:5b88d5760320 | 67 | |
kenjiArai | 0:5b88d5760320 | 68 | /** Check if the mail queue is empty. |
kenjiArai | 0:5b88d5760320 | 69 | * |
kenjiArai | 0:5b88d5760320 | 70 | * @return State of queue. |
kenjiArai | 0:5b88d5760320 | 71 | * @retval true Mail queue is empty. |
kenjiArai | 0:5b88d5760320 | 72 | * @retval false Mail queue contains mail. |
kenjiArai | 0:5b88d5760320 | 73 | * |
kenjiArai | 0:5b88d5760320 | 74 | * @note You may call this function from ISR context. |
kenjiArai | 0:5b88d5760320 | 75 | */ |
kenjiArai | 0:5b88d5760320 | 76 | bool empty() const |
kenjiArai | 0:5b88d5760320 | 77 | { |
kenjiArai | 0:5b88d5760320 | 78 | return _queue.empty(); |
kenjiArai | 0:5b88d5760320 | 79 | } |
kenjiArai | 0:5b88d5760320 | 80 | |
kenjiArai | 0:5b88d5760320 | 81 | /** Check if the mail queue is full. |
kenjiArai | 0:5b88d5760320 | 82 | * |
kenjiArai | 0:5b88d5760320 | 83 | * @return State of queue. |
kenjiArai | 0:5b88d5760320 | 84 | * @retval true Mail queue is full. |
kenjiArai | 0:5b88d5760320 | 85 | * @retval false Mail queue is not full. |
kenjiArai | 0:5b88d5760320 | 86 | * |
kenjiArai | 0:5b88d5760320 | 87 | * @note You may call this function from ISR context. |
kenjiArai | 0:5b88d5760320 | 88 | */ |
kenjiArai | 0:5b88d5760320 | 89 | bool full() const |
kenjiArai | 0:5b88d5760320 | 90 | { |
kenjiArai | 0:5b88d5760320 | 91 | return _queue.full(); |
kenjiArai | 0:5b88d5760320 | 92 | } |
kenjiArai | 0:5b88d5760320 | 93 | |
kenjiArai | 0:5b88d5760320 | 94 | /** Allocate a memory block of type T, without blocking. |
kenjiArai | 0:5b88d5760320 | 95 | * |
kenjiArai | 0:5b88d5760320 | 96 | * @param millisec Not used (see note). |
kenjiArai | 0:5b88d5760320 | 97 | * |
kenjiArai | 0:5b88d5760320 | 98 | * @return Pointer to memory block that you can fill with mail or NULL in case error. |
kenjiArai | 0:5b88d5760320 | 99 | * |
kenjiArai | 0:5b88d5760320 | 100 | * @note You may call this function from ISR context. |
kenjiArai | 0:5b88d5760320 | 101 | * @note If blocking is required, use Mail::alloc_for or Mail::alloc_until |
kenjiArai | 0:5b88d5760320 | 102 | */ |
kenjiArai | 0:5b88d5760320 | 103 | T *alloc(MBED_UNUSED uint32_t millisec = 0) |
kenjiArai | 0:5b88d5760320 | 104 | { |
kenjiArai | 0:5b88d5760320 | 105 | return _pool.alloc(); |
kenjiArai | 0:5b88d5760320 | 106 | } |
kenjiArai | 0:5b88d5760320 | 107 | |
kenjiArai | 0:5b88d5760320 | 108 | /** Allocate a memory block of type T, optionally blocking. |
kenjiArai | 0:5b88d5760320 | 109 | * |
kenjiArai | 0:5b88d5760320 | 110 | * @param millisec Timeout value, or osWaitForever. |
kenjiArai | 0:5b88d5760320 | 111 | * |
kenjiArai | 0:5b88d5760320 | 112 | * @return Pointer to memory block that you can fill with mail or NULL in case error. |
kenjiArai | 0:5b88d5760320 | 113 | * |
kenjiArai | 0:5b88d5760320 | 114 | * @note You may call this function from ISR context if the millisec parameter is set to 0. |
kenjiArai | 0:5b88d5760320 | 115 | */ |
kenjiArai | 0:5b88d5760320 | 116 | T *alloc_for(uint32_t millisec) |
kenjiArai | 0:5b88d5760320 | 117 | { |
kenjiArai | 0:5b88d5760320 | 118 | return _pool.alloc_for(millisec); |
kenjiArai | 0:5b88d5760320 | 119 | } |
kenjiArai | 0:5b88d5760320 | 120 | |
kenjiArai | 0:5b88d5760320 | 121 | /** Allocate a memory block of type T, blocking. |
kenjiArai | 0:5b88d5760320 | 122 | * |
kenjiArai | 0:5b88d5760320 | 123 | * @param millisec Absolute timeout time, referenced to Kernel::get_ms_count(). |
kenjiArai | 0:5b88d5760320 | 124 | * |
kenjiArai | 0:5b88d5760320 | 125 | * @return Pointer to memory block that you can fill with mail or NULL in case error. |
kenjiArai | 0:5b88d5760320 | 126 | * |
kenjiArai | 0:5b88d5760320 | 127 | * @note You cannot call this function from ISR context. |
kenjiArai | 0:5b88d5760320 | 128 | * @note the underlying RTOS may have a limit to the maximum wait time |
kenjiArai | 0:5b88d5760320 | 129 | * due to internal 32-bit computations, but this is guaranteed to work if the |
kenjiArai | 0:5b88d5760320 | 130 | * wait is <= 0x7fffffff milliseconds (~24 days). If the limit is exceeded, |
kenjiArai | 0:5b88d5760320 | 131 | * the wait will time out earlier than specified. |
kenjiArai | 0:5b88d5760320 | 132 | */ |
kenjiArai | 0:5b88d5760320 | 133 | T *alloc_until(uint64_t millisec) |
kenjiArai | 0:5b88d5760320 | 134 | { |
kenjiArai | 0:5b88d5760320 | 135 | return _pool.alloc_until(millisec); |
kenjiArai | 0:5b88d5760320 | 136 | } |
kenjiArai | 0:5b88d5760320 | 137 | |
kenjiArai | 0:5b88d5760320 | 138 | /** Allocate a memory block of type T, and set memory block to zero. |
kenjiArai | 0:5b88d5760320 | 139 | * |
kenjiArai | 0:5b88d5760320 | 140 | * @param millisec Not used (see note). |
kenjiArai | 0:5b88d5760320 | 141 | * |
kenjiArai | 0:5b88d5760320 | 142 | * @return Pointer to memory block that you can fill with mail or NULL in case error. |
kenjiArai | 0:5b88d5760320 | 143 | * |
kenjiArai | 0:5b88d5760320 | 144 | * @note You may call this function from ISR context if the millisec parameter is set to 0. |
kenjiArai | 0:5b88d5760320 | 145 | * @note If blocking is required, use Mail::calloc_for or Mail::calloc_until |
kenjiArai | 0:5b88d5760320 | 146 | */ |
kenjiArai | 0:5b88d5760320 | 147 | T *calloc(MBED_UNUSED uint32_t millisec = 0) |
kenjiArai | 0:5b88d5760320 | 148 | { |
kenjiArai | 0:5b88d5760320 | 149 | return _pool.calloc(); |
kenjiArai | 0:5b88d5760320 | 150 | } |
kenjiArai | 0:5b88d5760320 | 151 | |
kenjiArai | 0:5b88d5760320 | 152 | /** Allocate a memory block of type T, optionally blocking, and set memory block to zero. |
kenjiArai | 0:5b88d5760320 | 153 | * |
kenjiArai | 0:5b88d5760320 | 154 | * @param millisec Timeout value, or osWaitForever. |
kenjiArai | 0:5b88d5760320 | 155 | * |
kenjiArai | 0:5b88d5760320 | 156 | * @return Pointer to memory block that you can fill with mail or NULL in case error. |
kenjiArai | 0:5b88d5760320 | 157 | * |
kenjiArai | 0:5b88d5760320 | 158 | * @note You may call this function from ISR context if the millisec parameter is set to 0. |
kenjiArai | 0:5b88d5760320 | 159 | */ |
kenjiArai | 0:5b88d5760320 | 160 | T *calloc_for(uint32_t millisec) |
kenjiArai | 0:5b88d5760320 | 161 | { |
kenjiArai | 0:5b88d5760320 | 162 | return _pool.calloc_for(millisec); |
kenjiArai | 0:5b88d5760320 | 163 | } |
kenjiArai | 0:5b88d5760320 | 164 | |
kenjiArai | 0:5b88d5760320 | 165 | /** Allocate a memory block of type T, blocking, and set memory block to zero. |
kenjiArai | 0:5b88d5760320 | 166 | * |
kenjiArai | 0:5b88d5760320 | 167 | * @param millisec Absolute timeout time, referenced to Kernel::get_ms_count(). |
kenjiArai | 0:5b88d5760320 | 168 | * |
kenjiArai | 0:5b88d5760320 | 169 | * @return Pointer to memory block that you can fill with mail or NULL in case error. |
kenjiArai | 0:5b88d5760320 | 170 | * |
kenjiArai | 0:5b88d5760320 | 171 | * @note You cannot call this function from ISR context. |
kenjiArai | 0:5b88d5760320 | 172 | * @note the underlying RTOS may have a limit to the maximum wait time |
kenjiArai | 0:5b88d5760320 | 173 | * due to internal 32-bit computations, but this is guaranteed to work if the |
kenjiArai | 0:5b88d5760320 | 174 | * wait is <= 0x7fffffff milliseconds (~24 days). If the limit is exceeded, |
kenjiArai | 0:5b88d5760320 | 175 | * the wait will time out earlier than specified. |
kenjiArai | 0:5b88d5760320 | 176 | */ |
kenjiArai | 0:5b88d5760320 | 177 | T *calloc_until(uint64_t millisec) |
kenjiArai | 0:5b88d5760320 | 178 | { |
kenjiArai | 0:5b88d5760320 | 179 | return _pool.calloc_until(millisec); |
kenjiArai | 0:5b88d5760320 | 180 | } |
kenjiArai | 0:5b88d5760320 | 181 | |
kenjiArai | 0:5b88d5760320 | 182 | /** Put a mail in the queue. |
kenjiArai | 0:5b88d5760320 | 183 | * |
kenjiArai | 0:5b88d5760320 | 184 | * @param mptr Memory block previously allocated with Mail::alloc or Mail::calloc. |
kenjiArai | 0:5b88d5760320 | 185 | * |
kenjiArai | 0:5b88d5760320 | 186 | * @return Status code that indicates the execution status of the function (osOK on success). |
kenjiArai | 0:5b88d5760320 | 187 | * |
kenjiArai | 0:5b88d5760320 | 188 | * @note You may call this function from ISR context. |
kenjiArai | 0:5b88d5760320 | 189 | */ |
kenjiArai | 0:5b88d5760320 | 190 | osStatus put(T *mptr) |
kenjiArai | 0:5b88d5760320 | 191 | { |
kenjiArai | 0:5b88d5760320 | 192 | return _queue.put(mptr); |
kenjiArai | 0:5b88d5760320 | 193 | } |
kenjiArai | 0:5b88d5760320 | 194 | |
kenjiArai | 0:5b88d5760320 | 195 | /** Get a mail from the queue. |
kenjiArai | 0:5b88d5760320 | 196 | * |
kenjiArai | 0:5b88d5760320 | 197 | * @param millisec Timeout value (default: osWaitForever). |
kenjiArai | 0:5b88d5760320 | 198 | * |
kenjiArai | 0:5b88d5760320 | 199 | * @return Event that contains mail information or error code. |
kenjiArai | 0:5b88d5760320 | 200 | * @retval osEventMessage Message received. |
kenjiArai | 0:5b88d5760320 | 201 | * @retval osOK No mail is available (and no timeout was specified). |
kenjiArai | 0:5b88d5760320 | 202 | * @retval osEventTimeout No mail has arrived during the given timeout period. |
kenjiArai | 0:5b88d5760320 | 203 | * @retval osErrorParameter A parameter is invalid or outside of a permitted range. |
kenjiArai | 0:5b88d5760320 | 204 | * |
kenjiArai | 0:5b88d5760320 | 205 | * @note You may call this function from ISR context if the millisec parameter is set to 0. |
kenjiArai | 0:5b88d5760320 | 206 | */ |
kenjiArai | 0:5b88d5760320 | 207 | osEvent get(uint32_t millisec = osWaitForever) |
kenjiArai | 0:5b88d5760320 | 208 | { |
kenjiArai | 0:5b88d5760320 | 209 | osEvent evt = _queue.get(millisec); |
kenjiArai | 0:5b88d5760320 | 210 | if (evt.status == osEventMessage) { |
kenjiArai | 0:5b88d5760320 | 211 | evt.status = osEventMail; |
kenjiArai | 0:5b88d5760320 | 212 | } |
kenjiArai | 0:5b88d5760320 | 213 | return evt; |
kenjiArai | 0:5b88d5760320 | 214 | } |
kenjiArai | 0:5b88d5760320 | 215 | |
kenjiArai | 0:5b88d5760320 | 216 | /** Free a memory block from a mail. |
kenjiArai | 0:5b88d5760320 | 217 | * |
kenjiArai | 0:5b88d5760320 | 218 | * @param mptr Pointer to the memory block that was obtained with Mail::get. |
kenjiArai | 0:5b88d5760320 | 219 | * |
kenjiArai | 0:5b88d5760320 | 220 | * @return Status code that indicates the execution status of the function (osOK on success). |
kenjiArai | 0:5b88d5760320 | 221 | * |
kenjiArai | 0:5b88d5760320 | 222 | * @note You may call this function from ISR context. |
kenjiArai | 0:5b88d5760320 | 223 | */ |
kenjiArai | 0:5b88d5760320 | 224 | osStatus free(T *mptr) |
kenjiArai | 0:5b88d5760320 | 225 | { |
kenjiArai | 0:5b88d5760320 | 226 | return _pool.free(mptr); |
kenjiArai | 0:5b88d5760320 | 227 | } |
kenjiArai | 0:5b88d5760320 | 228 | |
kenjiArai | 0:5b88d5760320 | 229 | private: |
kenjiArai | 0:5b88d5760320 | 230 | Queue<T, queue_sz> _queue; |
kenjiArai | 0:5b88d5760320 | 231 | MemoryPool<T, queue_sz> _pool; |
kenjiArai | 0:5b88d5760320 | 232 | }; |
kenjiArai | 0:5b88d5760320 | 233 | |
kenjiArai | 0:5b88d5760320 | 234 | /** @}*/ |
kenjiArai | 0:5b88d5760320 | 235 | /** @}*/ |
kenjiArai | 0:5b88d5760320 | 236 | |
kenjiArai | 0:5b88d5760320 | 237 | } |
kenjiArai | 0:5b88d5760320 | 238 | |
kenjiArai | 0:5b88d5760320 | 239 | #endif |
kenjiArai | 0:5b88d5760320 | 240 | |
kenjiArai | 0:5b88d5760320 | 241 | |
kenjiArai | 0:5b88d5760320 | 242 |