Rtos API example

Committer:
marcozecchini
Date:
Sat Feb 23 12:13:36 2019 +0000
Revision:
0:9fca2b23d0ba
final commit

Who changed what in which revision?

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