Committer:
borlanic
Date:
Fri Mar 30 14:07:05 2018 +0000
Revision:
4:75df35ef4fb6
Parent:
0:380207fcb5c1
commentar

Who changed what in which revision?

UserRevisionLine numberNew contents of line
borlanic 0:380207fcb5c1 1 /* mbed Microcontroller Library
borlanic 0:380207fcb5c1 2 * Copyright (c) 2006-2012 ARM Limited
borlanic 0:380207fcb5c1 3 *
borlanic 0:380207fcb5c1 4 * Permission is hereby granted, free of charge, to any person obtaining a copy
borlanic 0:380207fcb5c1 5 * of this software and associated documentation files (the "Software"), to deal
borlanic 0:380207fcb5c1 6 * in the Software without restriction, including without limitation the rights
borlanic 0:380207fcb5c1 7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
borlanic 0:380207fcb5c1 8 * copies of the Software, and to permit persons to whom the Software is
borlanic 0:380207fcb5c1 9 * furnished to do so, subject to the following conditions:
borlanic 0:380207fcb5c1 10 *
borlanic 0:380207fcb5c1 11 * The above copyright notice and this permission notice shall be included in
borlanic 0:380207fcb5c1 12 * all copies or substantial portions of the Software.
borlanic 0:380207fcb5c1 13 *
borlanic 0:380207fcb5c1 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
borlanic 0:380207fcb5c1 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
borlanic 0:380207fcb5c1 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
borlanic 0:380207fcb5c1 17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
borlanic 0:380207fcb5c1 18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
borlanic 0:380207fcb5c1 19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
borlanic 0:380207fcb5c1 20 * SOFTWARE.
borlanic 0:380207fcb5c1 21 */
borlanic 0:380207fcb5c1 22 #ifndef QUEUE_H
borlanic 0:380207fcb5c1 23 #define QUEUE_H
borlanic 0:380207fcb5c1 24
borlanic 0:380207fcb5c1 25 #include <stdint.h>
borlanic 0:380207fcb5c1 26 #include <string.h>
borlanic 0:380207fcb5c1 27
borlanic 0:380207fcb5c1 28 #include "cmsis_os2.h"
borlanic 0:380207fcb5c1 29 #include "mbed_rtos_storage.h"
borlanic 0:380207fcb5c1 30 #include "platform/mbed_error.h"
borlanic 0:380207fcb5c1 31 #include "platform/NonCopyable.h"
borlanic 0:380207fcb5c1 32 #include "mbed_rtos1_types.h"
borlanic 0:380207fcb5c1 33
borlanic 0:380207fcb5c1 34 namespace rtos {
borlanic 0:380207fcb5c1 35 /** \addtogroup rtos */
borlanic 0:380207fcb5c1 36 /** @{*/
borlanic 0:380207fcb5c1 37 /**
borlanic 0:380207fcb5c1 38 * \defgroup rtos_EventFlags EventFlags class
borlanic 0:380207fcb5c1 39 * @{
borlanic 0:380207fcb5c1 40 */
borlanic 0:380207fcb5c1 41
borlanic 0:380207fcb5c1 42 /** The Queue class allow to control, send, receive, or wait for messages.
borlanic 0:380207fcb5c1 43 A message can be a integer or pointer value to a certain type T that is send
borlanic 0:380207fcb5c1 44 to a thread or interrupt service routine.
borlanic 0:380207fcb5c1 45 @tparam T data type of a single message element.
borlanic 0:380207fcb5c1 46 @tparam queue_sz maximum number of messages in queue.
borlanic 0:380207fcb5c1 47
borlanic 0:380207fcb5c1 48 @note
borlanic 0:380207fcb5c1 49 Memory considerations: The queue control structures will be created on current thread's stack, both for the mbed OS
borlanic 0:380207fcb5c1 50 and underlying RTOS objects (static or dynamic RTOS memory pools are not being used).
borlanic 0:380207fcb5c1 51 */
borlanic 0:380207fcb5c1 52 template<typename T, uint32_t queue_sz>
borlanic 0:380207fcb5c1 53 class Queue : private mbed::NonCopyable<Queue<T, queue_sz> > {
borlanic 0:380207fcb5c1 54 public:
borlanic 0:380207fcb5c1 55 /** Create and initialize a message Queue.
borlanic 0:380207fcb5c1 56 *
borlanic 0:380207fcb5c1 57 * @note You cannot call this function from ISR context.
borlanic 0:380207fcb5c1 58 */
borlanic 0:380207fcb5c1 59 Queue() {
borlanic 0:380207fcb5c1 60 memset(&_obj_mem, 0, sizeof(_obj_mem));
borlanic 0:380207fcb5c1 61 osMessageQueueAttr_t attr = { 0 };
borlanic 0:380207fcb5c1 62 attr.mq_mem = _queue_mem;
borlanic 0:380207fcb5c1 63 attr.mq_size = sizeof(_queue_mem);
borlanic 0:380207fcb5c1 64 attr.cb_mem = &_obj_mem;
borlanic 0:380207fcb5c1 65 attr.cb_size = sizeof(_obj_mem);
borlanic 0:380207fcb5c1 66 _id = osMessageQueueNew(queue_sz, sizeof(T*), &attr);
borlanic 0:380207fcb5c1 67 MBED_ASSERT(_id);
borlanic 0:380207fcb5c1 68 }
borlanic 0:380207fcb5c1 69 /** Queue destructor
borlanic 0:380207fcb5c1 70 *
borlanic 0:380207fcb5c1 71 * @note You cannot call this function from ISR context.
borlanic 0:380207fcb5c1 72 */
borlanic 0:380207fcb5c1 73 ~Queue() {
borlanic 0:380207fcb5c1 74 osMessageQueueDelete(_id);
borlanic 0:380207fcb5c1 75 }
borlanic 0:380207fcb5c1 76
borlanic 0:380207fcb5c1 77 /** Check if the queue is empty
borlanic 0:380207fcb5c1 78 *
borlanic 0:380207fcb5c1 79 * @return True if the queue is empty, false if not
borlanic 0:380207fcb5c1 80 *
borlanic 0:380207fcb5c1 81 * @note You may call this function from ISR context.
borlanic 0:380207fcb5c1 82 */
borlanic 0:380207fcb5c1 83 bool empty() const {
borlanic 0:380207fcb5c1 84 return osMessageQueueGetCount(_id) == 0;
borlanic 0:380207fcb5c1 85 }
borlanic 0:380207fcb5c1 86
borlanic 0:380207fcb5c1 87 /** Check if the queue is full
borlanic 0:380207fcb5c1 88 *
borlanic 0:380207fcb5c1 89 * @return True if the queue is full, false if not
borlanic 0:380207fcb5c1 90 *
borlanic 0:380207fcb5c1 91 * @note You may call this function from ISR context.
borlanic 0:380207fcb5c1 92 */
borlanic 0:380207fcb5c1 93 bool full() const {
borlanic 0:380207fcb5c1 94 return osMessageQueueGetSpace(_id) == 0;
borlanic 0:380207fcb5c1 95 }
borlanic 0:380207fcb5c1 96
borlanic 0:380207fcb5c1 97 /** Put a message in a Queue.
borlanic 0:380207fcb5c1 98 @param data message pointer.
borlanic 0:380207fcb5c1 99 @param millisec timeout value or 0 in case of no time-out. (default: 0)
borlanic 0:380207fcb5c1 100 @param prio priority value or 0 in case of default. (default: 0)
borlanic 0:380207fcb5c1 101 @return status code that indicates the execution status of the function:
borlanic 0:380207fcb5c1 102 @a osOK the message has been put into the queue.
borlanic 0:380207fcb5c1 103 @a osErrorTimeout the message could not be put into the queue in the given time.
borlanic 0:380207fcb5c1 104 @a osErrorResource not enough space in the queue.
borlanic 0:380207fcb5c1 105 @a osErrorParameter internal error or non-zero timeout specified in an ISR.
borlanic 0:380207fcb5c1 106
borlanic 0:380207fcb5c1 107 @note You may call this function from ISR context if the millisec parameter is set to 0.
borlanic 0:380207fcb5c1 108 */
borlanic 0:380207fcb5c1 109 osStatus put(T* data, uint32_t millisec=0, uint8_t prio=0) {
borlanic 0:380207fcb5c1 110 return osMessageQueuePut(_id, &data, prio, millisec);
borlanic 0:380207fcb5c1 111 }
borlanic 0:380207fcb5c1 112
borlanic 0:380207fcb5c1 113 /** Get a message or Wait for a message from a Queue. Messages are retrieved in a descending priority order or
borlanic 0:380207fcb5c1 114 first in first out when the priorities are the same.
borlanic 0:380207fcb5c1 115 @param millisec timeout value or 0 in case of no time-out. (default: osWaitForever).
borlanic 0:380207fcb5c1 116 @return event information that includes the message in event.value and the status code in event.status:
borlanic 0:380207fcb5c1 117 @a osEventMessage message received.
borlanic 0:380207fcb5c1 118 @a osOK no message is available in the queue and no timeout was specified.
borlanic 0:380207fcb5c1 119 @a osEventTimeout no message has arrived during the given timeout period.
borlanic 0:380207fcb5c1 120 @a osErrorParameter a parameter is invalid or outside of a permitted range.
borlanic 0:380207fcb5c1 121
borlanic 0:380207fcb5c1 122 @note You may call this function from ISR context if the millisec parameter is set to 0.
borlanic 0:380207fcb5c1 123 */
borlanic 0:380207fcb5c1 124 osEvent get(uint32_t millisec=osWaitForever) {
borlanic 0:380207fcb5c1 125 osEvent event;
borlanic 0:380207fcb5c1 126 T *data = NULL;
borlanic 0:380207fcb5c1 127 osStatus_t res = osMessageQueueGet(_id, &data, NULL, millisec);
borlanic 0:380207fcb5c1 128
borlanic 0:380207fcb5c1 129 switch (res) {
borlanic 0:380207fcb5c1 130 case osOK:
borlanic 0:380207fcb5c1 131 event.status = (osStatus)osEventMessage;
borlanic 0:380207fcb5c1 132 event.value.p = data;
borlanic 0:380207fcb5c1 133 break;
borlanic 0:380207fcb5c1 134 case osErrorResource:
borlanic 0:380207fcb5c1 135 event.status = osOK;
borlanic 0:380207fcb5c1 136 break;
borlanic 0:380207fcb5c1 137 case osErrorTimeout:
borlanic 0:380207fcb5c1 138 event.status = (osStatus)osEventTimeout;
borlanic 0:380207fcb5c1 139 break;
borlanic 0:380207fcb5c1 140 case osErrorParameter:
borlanic 0:380207fcb5c1 141 default:
borlanic 0:380207fcb5c1 142 event.status = osErrorParameter;
borlanic 0:380207fcb5c1 143 break;
borlanic 0:380207fcb5c1 144 }
borlanic 0:380207fcb5c1 145 event.def.message_id = _id;
borlanic 0:380207fcb5c1 146
borlanic 0:380207fcb5c1 147 return event;
borlanic 0:380207fcb5c1 148 }
borlanic 0:380207fcb5c1 149
borlanic 0:380207fcb5c1 150 private:
borlanic 0:380207fcb5c1 151 osMessageQueueId_t _id;
borlanic 0:380207fcb5c1 152 char _queue_mem[queue_sz * (sizeof(T*) + sizeof(mbed_rtos_storage_message_t))];
borlanic 0:380207fcb5c1 153 mbed_rtos_storage_msg_queue_t _obj_mem;
borlanic 0:380207fcb5c1 154 };
borlanic 0:380207fcb5c1 155 /** @}*/
borlanic 0:380207fcb5c1 156 /** @}*/
borlanic 0:380207fcb5c1 157
borlanic 0:380207fcb5c1 158 }
borlanic 0:380207fcb5c1 159 #endif
borlanic 0:380207fcb5c1 160