Committer:
borlanic
Date:
Tue Apr 24 11:45:18 2018 +0000
Revision:
0:02dd72d1d465
BaBoRo_test2 - backup 1

Who changed what in which revision?

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