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 QUEUE_H
kenjiArai 0:5b88d5760320 23 #define QUEUE_H
kenjiArai 0:5b88d5760320 24
kenjiArai 0:5b88d5760320 25 #include "cmsis_os2.h"
kenjiArai 0:5b88d5760320 26 #include "mbed_rtos1_types.h"
kenjiArai 0:5b88d5760320 27 #include "mbed_rtos_storage.h"
kenjiArai 0:5b88d5760320 28 #include "platform/mbed_error.h"
kenjiArai 0:5b88d5760320 29 #include "platform/NonCopyable.h"
kenjiArai 0:5b88d5760320 30
kenjiArai 0:5b88d5760320 31 namespace rtos {
kenjiArai 0:5b88d5760320 32 /** \addtogroup rtos */
kenjiArai 0:5b88d5760320 33 /** @{*/
kenjiArai 0:5b88d5760320 34 /**
kenjiArai 0:5b88d5760320 35 * \defgroup rtos_Queue Queue class
kenjiArai 0:5b88d5760320 36 * @{
kenjiArai 0:5b88d5760320 37 */
kenjiArai 0:5b88d5760320 38
kenjiArai 0:5b88d5760320 39 /** The Queue class represents a collection of objects that are stored first by
kenjiArai 0:5b88d5760320 40 * order of priority, and then in first-in, first-out (FIFO) order.
kenjiArai 0:5b88d5760320 41 *
kenjiArai 0:5b88d5760320 42 * You can use a queue when you need to store data and then access it in the same
kenjiArai 0:5b88d5760320 43 * order that it has been stored. The order in which you retrieve the data is in
kenjiArai 0:5b88d5760320 44 * order of descending priority. If multiple elements have the same priority,
kenjiArai 0:5b88d5760320 45 * they are retrieved in FIFO order.
kenjiArai 0:5b88d5760320 46 *
kenjiArai 0:5b88d5760320 47 * The object type stored in the queue can be an integer, pointer or a generic
kenjiArai 0:5b88d5760320 48 * type given by the template parameter T.
kenjiArai 0:5b88d5760320 49 *
kenjiArai 0:5b88d5760320 50 * @tparam T Specifies the type of elements stored in the queue.
kenjiArai 0:5b88d5760320 51 * @tparam queue_sz Maximum number of messages that you can store in the queue.
kenjiArai 0:5b88d5760320 52 *
kenjiArai 0:5b88d5760320 53 * @note Memory considerations: The queue control structures are created on the
kenjiArai 0:5b88d5760320 54 * current thread's stack, both for the Mbed OS and underlying RTOS
kenjiArai 0:5b88d5760320 55 * objects (static or dynamic RTOS memory pools are not being used).
kenjiArai 0:5b88d5760320 56 *
kenjiArai 0:5b88d5760320 57 */
kenjiArai 0:5b88d5760320 58 template<typename T, uint32_t queue_sz>
kenjiArai 0:5b88d5760320 59 class Queue : private mbed::NonCopyable<Queue<T, queue_sz> > {
kenjiArai 0:5b88d5760320 60 public:
kenjiArai 0:5b88d5760320 61 /** Create and initialize a message Queue of objects of the parameterized
kenjiArai 0:5b88d5760320 62 * type `T` and maximum capacity specified by `queue_sz`.
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 Queue()
kenjiArai 0:5b88d5760320 67 {
kenjiArai 0:5b88d5760320 68 osMessageQueueAttr_t attr = { 0 };
kenjiArai 0:5b88d5760320 69 attr.mq_mem = _queue_mem;
kenjiArai 0:5b88d5760320 70 attr.mq_size = sizeof(_queue_mem);
kenjiArai 0:5b88d5760320 71 attr.cb_mem = &_obj_mem;
kenjiArai 0:5b88d5760320 72 attr.cb_size = sizeof(_obj_mem);
kenjiArai 0:5b88d5760320 73 _id = osMessageQueueNew(queue_sz, sizeof(T *), &attr);
kenjiArai 0:5b88d5760320 74 MBED_ASSERT(_id);
kenjiArai 0:5b88d5760320 75 }
kenjiArai 0:5b88d5760320 76
kenjiArai 0:5b88d5760320 77 /** Queue destructor
kenjiArai 0:5b88d5760320 78 *
kenjiArai 0:5b88d5760320 79 * @note You cannot call this function from ISR context.
kenjiArai 0:5b88d5760320 80 */
kenjiArai 0:5b88d5760320 81 ~Queue()
kenjiArai 0:5b88d5760320 82 {
kenjiArai 0:5b88d5760320 83 osMessageQueueDelete(_id);
kenjiArai 0:5b88d5760320 84 }
kenjiArai 0:5b88d5760320 85
kenjiArai 0:5b88d5760320 86 /** Check if the queue is empty.
kenjiArai 0:5b88d5760320 87 *
kenjiArai 0:5b88d5760320 88 * @return True if the queue is empty, false if not
kenjiArai 0:5b88d5760320 89 *
kenjiArai 0:5b88d5760320 90 * @note You may call this function from ISR context.
kenjiArai 0:5b88d5760320 91 */
kenjiArai 0:5b88d5760320 92 bool empty() const
kenjiArai 0:5b88d5760320 93 {
kenjiArai 0:5b88d5760320 94 return osMessageQueueGetCount(_id) == 0;
kenjiArai 0:5b88d5760320 95 }
kenjiArai 0:5b88d5760320 96
kenjiArai 0:5b88d5760320 97 /** Check if the queue is full.
kenjiArai 0:5b88d5760320 98 *
kenjiArai 0:5b88d5760320 99 * @return True if the queue is full, false if not
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 full() const
kenjiArai 0:5b88d5760320 104 {
kenjiArai 0:5b88d5760320 105 return osMessageQueueGetSpace(_id) == 0;
kenjiArai 0:5b88d5760320 106 }
kenjiArai 0:5b88d5760320 107
kenjiArai 0:5b88d5760320 108 /** Inserts the given element to the end of the queue.
kenjiArai 0:5b88d5760320 109 *
kenjiArai 0:5b88d5760320 110 * This function puts the message pointed to by `data` into the queue. The
kenjiArai 0:5b88d5760320 111 * parameter `prio` is used to sort the message according to their priority
kenjiArai 0:5b88d5760320 112 * (higher numbers indicate higher priority) on insertion.
kenjiArai 0:5b88d5760320 113 *
kenjiArai 0:5b88d5760320 114 * The timeout indicated by the parameter `millisec` specifies how long the
kenjiArai 0:5b88d5760320 115 * function blocks waiting for the message to be inserted into the
kenjiArai 0:5b88d5760320 116 * queue.
kenjiArai 0:5b88d5760320 117 *
kenjiArai 0:5b88d5760320 118 * The parameter `millisec` can have the following values:
kenjiArai 0:5b88d5760320 119 * - When the timeout is 0 (the default), the function returns instantly.
kenjiArai 0:5b88d5760320 120 * - When the timeout is osWaitForever, the function waits for an
kenjiArai 0:5b88d5760320 121 * infinite time.
kenjiArai 0:5b88d5760320 122 * - For all other values, the function waits for the given number of
kenjiArai 0:5b88d5760320 123 * milliseconds.
kenjiArai 0:5b88d5760320 124 *
kenjiArai 0:5b88d5760320 125 * @param data Pointer to the element to insert into the queue.
kenjiArai 0:5b88d5760320 126 * @param millisec Timeout for the operation to be executed, or 0 in case
kenjiArai 0:5b88d5760320 127 * of no timeout. (default: 0)
kenjiArai 0:5b88d5760320 128 * @param prio Priority of the operation or 0 in case of default.
kenjiArai 0:5b88d5760320 129 * (default: 0)
kenjiArai 0:5b88d5760320 130 *
kenjiArai 0:5b88d5760320 131 * @return Status code that indicates the execution status of the function:
kenjiArai 0:5b88d5760320 132 * @a osOK The message has been successfully inserted
kenjiArai 0:5b88d5760320 133 * into the queue.
kenjiArai 0:5b88d5760320 134 * @a osErrorTimeout The message could not be inserted into the
kenjiArai 0:5b88d5760320 135 * queue in the given time.
kenjiArai 0:5b88d5760320 136 * @a osErrorResource The message could not be inserted because
kenjiArai 0:5b88d5760320 137 * the queue is full.
kenjiArai 0:5b88d5760320 138 * @a osErrorParameter Internal error or nonzero timeout specified
kenjiArai 0:5b88d5760320 139 * in an ISR.
kenjiArai 0:5b88d5760320 140 *
kenjiArai 0:5b88d5760320 141 * @note You may call this function from ISR context if the millisec
kenjiArai 0:5b88d5760320 142 * parameter is set to 0.
kenjiArai 0:5b88d5760320 143 *
kenjiArai 0:5b88d5760320 144 */
kenjiArai 0:5b88d5760320 145 osStatus put(T *data, uint32_t millisec = 0, uint8_t prio = 0)
kenjiArai 0:5b88d5760320 146 {
kenjiArai 0:5b88d5760320 147 return osMessageQueuePut(_id, &data, prio, millisec);
kenjiArai 0:5b88d5760320 148 }
kenjiArai 0:5b88d5760320 149
kenjiArai 0:5b88d5760320 150 /** Get a message or wait for a message from the queue.
kenjiArai 0:5b88d5760320 151 *
kenjiArai 0:5b88d5760320 152 * This function retrieves a message from the queue. The message is stored
kenjiArai 0:5b88d5760320 153 * in the value field of the returned `osEvent` object.
kenjiArai 0:5b88d5760320 154 *
kenjiArai 0:5b88d5760320 155 * The timeout specified by the parameter `millisec` specifies how long the
kenjiArai 0:5b88d5760320 156 * function waits to retrieve the message from the queue.
kenjiArai 0:5b88d5760320 157 *
kenjiArai 0:5b88d5760320 158 * The timeout parameter can have the following values:
kenjiArai 0:5b88d5760320 159 * - When the timeout is 0, the function returns instantly.
kenjiArai 0:5b88d5760320 160 * - When the timeout is osWaitForever (default), the function waits
kenjiArai 0:5b88d5760320 161 * infinite time until the message is retrieved.
kenjiArai 0:5b88d5760320 162 * - When the timeout is any other value, the function waits for the
kenjiArai 0:5b88d5760320 163 * specified time before returning a timeout error.
kenjiArai 0:5b88d5760320 164 *
kenjiArai 0:5b88d5760320 165 * Messages are retrieved in descending priority order. If two messages
kenjiArai 0:5b88d5760320 166 * share the same priority level, they are retrieved in first-in, first-out
kenjiArai 0:5b88d5760320 167 * (FIFO) order.
kenjiArai 0:5b88d5760320 168 *
kenjiArai 0:5b88d5760320 169 * @param millisec Timeout value.
kenjiArai 0:5b88d5760320 170 * (default: osWaitForever).
kenjiArai 0:5b88d5760320 171 *
kenjiArai 0:5b88d5760320 172 * @return Event information that includes the message in event. Message
kenjiArai 0:5b88d5760320 173 * value and the status code in event.status:
kenjiArai 0:5b88d5760320 174 * @a osEventMessage Message successfully received.
kenjiArai 0:5b88d5760320 175 * @a osOK No message is available in the queue, and no
kenjiArai 0:5b88d5760320 176 * timeout was specified.
kenjiArai 0:5b88d5760320 177 * @a osEventTimeout No message was received before a timeout
kenjiArai 0:5b88d5760320 178 * event occurred.
kenjiArai 0:5b88d5760320 179 * @a osErrorParameter A parameter is invalid or outside of a
kenjiArai 0:5b88d5760320 180 * permitted range.
kenjiArai 0:5b88d5760320 181 *
kenjiArai 0:5b88d5760320 182 * @note You may call this function from ISR context if the millisec
kenjiArai 0:5b88d5760320 183 * parameter is set to 0.
kenjiArai 0:5b88d5760320 184 */
kenjiArai 0:5b88d5760320 185 osEvent get(uint32_t millisec = osWaitForever)
kenjiArai 0:5b88d5760320 186 {
kenjiArai 0:5b88d5760320 187 osEvent event;
kenjiArai 0:5b88d5760320 188 T *data = NULL;
kenjiArai 0:5b88d5760320 189 osStatus_t res = osMessageQueueGet(_id, &data, NULL, millisec);
kenjiArai 0:5b88d5760320 190
kenjiArai 0:5b88d5760320 191 switch (res) {
kenjiArai 0:5b88d5760320 192 case osOK:
kenjiArai 0:5b88d5760320 193 event.status = (osStatus)osEventMessage;
kenjiArai 0:5b88d5760320 194 event.value.p = data;
kenjiArai 0:5b88d5760320 195 break;
kenjiArai 0:5b88d5760320 196 case osErrorResource:
kenjiArai 0:5b88d5760320 197 event.status = osOK;
kenjiArai 0:5b88d5760320 198 break;
kenjiArai 0:5b88d5760320 199 case osErrorTimeout:
kenjiArai 0:5b88d5760320 200 event.status = (osStatus)osEventTimeout;
kenjiArai 0:5b88d5760320 201 break;
kenjiArai 0:5b88d5760320 202 case osErrorParameter:
kenjiArai 0:5b88d5760320 203 default:
kenjiArai 0:5b88d5760320 204 event.status = osErrorParameter;
kenjiArai 0:5b88d5760320 205 break;
kenjiArai 0:5b88d5760320 206 }
kenjiArai 0:5b88d5760320 207 event.def.message_id = _id;
kenjiArai 0:5b88d5760320 208
kenjiArai 0:5b88d5760320 209 return event;
kenjiArai 0:5b88d5760320 210 }
kenjiArai 0:5b88d5760320 211
kenjiArai 0:5b88d5760320 212 private:
kenjiArai 0:5b88d5760320 213 osMessageQueueId_t _id;
kenjiArai 0:5b88d5760320 214 char _queue_mem[queue_sz * (sizeof(T *) + sizeof(mbed_rtos_storage_message_t))];
kenjiArai 0:5b88d5760320 215 mbed_rtos_storage_msg_queue_t _obj_mem;
kenjiArai 0:5b88d5760320 216 };
kenjiArai 0:5b88d5760320 217 /** @}*/
kenjiArai 0:5b88d5760320 218 /** @}*/
kenjiArai 0:5b88d5760320 219
kenjiArai 0:5b88d5760320 220 } // namespace rtos
kenjiArai 0:5b88d5760320 221
kenjiArai 0:5b88d5760320 222 #endif // QUEUE_H