Nicolas Borla / Mbed OS ROME2_Robot_Firmware
Committer:
boro
Date:
Mon Mar 16 13:12:31 2020 +0000
Revision:
0:4beb2ea291ec
a

Who changed what in which revision?

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