Entrega 3er corte - sistemas embebidos

Committer:
Bethory
Date:
Wed May 30 00:01:50 2018 +0000
Revision:
0:6ad07c9019fd
Codigo de tales para todos los pasculaes

Who changed what in which revision?

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