BBR 1 Ebene

Committer:
borlanic
Date:
Mon May 14 11:29:06 2018 +0000
Revision:
0:fbdae7e6d805
BBR

Who changed what in which revision?

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