Daniel Vizcaya
/
04_RTOS_Embebidos
Entrega 3er corte - sistemas embebidos
mbed-os/rtos/Mail.h@1:fcdb45ee95b9, 2018-05-30 (annotated)
- Committer:
- Bethory
- Date:
- Wed May 30 04:46:28 2018 +0000
- Revision:
- 1:fcdb45ee95b9
- Parent:
- 0:6ad07c9019fd
Entrega Final
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Bethory | 0:6ad07c9019fd | 1 | /* mbed Microcontroller Library |
Bethory | 0:6ad07c9019fd | 2 | * Copyright (c) 2006-2017 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 MAIL_H |
Bethory | 0:6ad07c9019fd | 23 | #define MAIL_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 "Queue.h" |
Bethory | 0:6ad07c9019fd | 29 | #include "MemoryPool.h" |
Bethory | 0:6ad07c9019fd | 30 | #include "cmsis_os2.h" |
Bethory | 0:6ad07c9019fd | 31 | #include "mbed_rtos_storage.h" |
Bethory | 0:6ad07c9019fd | 32 | #include "mbed_rtos1_types.h" |
Bethory | 0:6ad07c9019fd | 33 | |
Bethory | 0:6ad07c9019fd | 34 | #include "platform/NonCopyable.h" |
Bethory | 0:6ad07c9019fd | 35 | |
Bethory | 0:6ad07c9019fd | 36 | using namespace rtos; |
Bethory | 0:6ad07c9019fd | 37 | |
Bethory | 0:6ad07c9019fd | 38 | namespace rtos { |
Bethory | 0:6ad07c9019fd | 39 | /** \addtogroup rtos */ |
Bethory | 0:6ad07c9019fd | 40 | /** @{*/ |
Bethory | 0:6ad07c9019fd | 41 | /** |
Bethory | 0:6ad07c9019fd | 42 | * \defgroup rtos_Mail Mail class |
Bethory | 0:6ad07c9019fd | 43 | * @{ |
Bethory | 0:6ad07c9019fd | 44 | */ |
Bethory | 0:6ad07c9019fd | 45 | |
Bethory | 0:6ad07c9019fd | 46 | /** The Mail class allow to control, send, receive, or wait for mail. |
Bethory | 0:6ad07c9019fd | 47 | A mail is a memory block that is send to a thread or interrupt service routine. |
Bethory | 0:6ad07c9019fd | 48 | @tparam T data type of a single message element. |
Bethory | 0:6ad07c9019fd | 49 | @tparam queue_sz maximum number of messages in queue. |
Bethory | 0:6ad07c9019fd | 50 | |
Bethory | 0:6ad07c9019fd | 51 | @note |
Bethory | 0:6ad07c9019fd | 52 | Memory considerations: The mail data store and control structures will be created on current thread's stack, |
Bethory | 0:6ad07c9019fd | 53 | both for the mbed OS and underlying RTOS objects (static or dynamic RTOS memory pools are not being used). |
Bethory | 0:6ad07c9019fd | 54 | */ |
Bethory | 0:6ad07c9019fd | 55 | template<typename T, uint32_t queue_sz> |
Bethory | 0:6ad07c9019fd | 56 | class Mail : private mbed::NonCopyable<Mail<T, queue_sz> > { |
Bethory | 0:6ad07c9019fd | 57 | public: |
Bethory | 0:6ad07c9019fd | 58 | /** Create and Initialize Mail queue. |
Bethory | 0:6ad07c9019fd | 59 | * |
Bethory | 0:6ad07c9019fd | 60 | * @note You cannot call this function from ISR context. |
Bethory | 0:6ad07c9019fd | 61 | */ |
Bethory | 0:6ad07c9019fd | 62 | Mail() { }; |
Bethory | 0:6ad07c9019fd | 63 | |
Bethory | 0:6ad07c9019fd | 64 | /** Check if the mail queue is empty |
Bethory | 0:6ad07c9019fd | 65 | * |
Bethory | 0:6ad07c9019fd | 66 | * @return True if the mail queue is empty, false if not |
Bethory | 0:6ad07c9019fd | 67 | * |
Bethory | 0:6ad07c9019fd | 68 | * @note You may call this function from ISR context. |
Bethory | 0:6ad07c9019fd | 69 | */ |
Bethory | 0:6ad07c9019fd | 70 | bool empty() const { |
Bethory | 0:6ad07c9019fd | 71 | return _queue.empty(); |
Bethory | 0:6ad07c9019fd | 72 | } |
Bethory | 0:6ad07c9019fd | 73 | |
Bethory | 0:6ad07c9019fd | 74 | /** Check if the mail queue is full |
Bethory | 0:6ad07c9019fd | 75 | * |
Bethory | 0:6ad07c9019fd | 76 | * @return True if the mail queue is full, false if not |
Bethory | 0:6ad07c9019fd | 77 | * |
Bethory | 0:6ad07c9019fd | 78 | * @note You may call this function from ISR context. |
Bethory | 0:6ad07c9019fd | 79 | */ |
Bethory | 0:6ad07c9019fd | 80 | bool full() const { |
Bethory | 0:6ad07c9019fd | 81 | return _queue.full(); |
Bethory | 0:6ad07c9019fd | 82 | } |
Bethory | 0:6ad07c9019fd | 83 | |
Bethory | 0:6ad07c9019fd | 84 | /** Allocate a memory block of type T |
Bethory | 0:6ad07c9019fd | 85 | @param millisec timeout value or 0 in case of no time-out. (default: 0). |
Bethory | 0:6ad07c9019fd | 86 | @return pointer to memory block that can be filled with mail or NULL in case error. |
Bethory | 0:6ad07c9019fd | 87 | |
Bethory | 0:6ad07c9019fd | 88 | @note You may call this function from ISR context if the millisec parameter is set to 0. |
Bethory | 0:6ad07c9019fd | 89 | */ |
Bethory | 0:6ad07c9019fd | 90 | T* alloc(uint32_t millisec=0) { |
Bethory | 0:6ad07c9019fd | 91 | return _pool.alloc(); |
Bethory | 0:6ad07c9019fd | 92 | } |
Bethory | 0:6ad07c9019fd | 93 | |
Bethory | 0:6ad07c9019fd | 94 | /** Allocate a memory block of type T and set memory block to zero. |
Bethory | 0:6ad07c9019fd | 95 | @param millisec timeout value or 0 in case of no time-out. (default: 0). |
Bethory | 0:6ad07c9019fd | 96 | @return pointer to memory block that can be filled with mail or NULL in case error. |
Bethory | 0:6ad07c9019fd | 97 | |
Bethory | 0:6ad07c9019fd | 98 | @note You may call this function from ISR context if the millisec parameter is set to 0. |
Bethory | 0:6ad07c9019fd | 99 | */ |
Bethory | 0:6ad07c9019fd | 100 | T* calloc(uint32_t millisec=0) { |
Bethory | 0:6ad07c9019fd | 101 | return _pool.calloc(); |
Bethory | 0:6ad07c9019fd | 102 | } |
Bethory | 0:6ad07c9019fd | 103 | |
Bethory | 0:6ad07c9019fd | 104 | /** Put a mail in the queue. |
Bethory | 0:6ad07c9019fd | 105 | @param mptr memory block previously allocated with Mail::alloc or Mail::calloc. |
Bethory | 0:6ad07c9019fd | 106 | @return status code that indicates the execution status of the function. |
Bethory | 0:6ad07c9019fd | 107 | |
Bethory | 0:6ad07c9019fd | 108 | @note You may call this function from ISR context. |
Bethory | 0:6ad07c9019fd | 109 | */ |
Bethory | 0:6ad07c9019fd | 110 | osStatus put(T *mptr) { |
Bethory | 0:6ad07c9019fd | 111 | return _queue.put(mptr); |
Bethory | 0:6ad07c9019fd | 112 | } |
Bethory | 0:6ad07c9019fd | 113 | |
Bethory | 0:6ad07c9019fd | 114 | /** Get a mail from a queue. |
Bethory | 0:6ad07c9019fd | 115 | @param millisec timeout value or 0 in case of no time-out. (default: osWaitForever). |
Bethory | 0:6ad07c9019fd | 116 | @return event that contains mail information or error code. |
Bethory | 0:6ad07c9019fd | 117 | |
Bethory | 0:6ad07c9019fd | 118 | @note You may call this function from ISR context if the millisec parameter is set to 0. |
Bethory | 0:6ad07c9019fd | 119 | */ |
Bethory | 0:6ad07c9019fd | 120 | osEvent get(uint32_t millisec=osWaitForever) { |
Bethory | 0:6ad07c9019fd | 121 | osEvent evt = _queue.get(millisec); |
Bethory | 0:6ad07c9019fd | 122 | if (evt.status == osEventMessage) { |
Bethory | 0:6ad07c9019fd | 123 | evt.status = osEventMail; |
Bethory | 0:6ad07c9019fd | 124 | } |
Bethory | 0:6ad07c9019fd | 125 | return evt; |
Bethory | 0:6ad07c9019fd | 126 | } |
Bethory | 0:6ad07c9019fd | 127 | |
Bethory | 0:6ad07c9019fd | 128 | /** Free a memory block from a mail. |
Bethory | 0:6ad07c9019fd | 129 | @param mptr pointer to the memory block that was obtained with Mail::get. |
Bethory | 0:6ad07c9019fd | 130 | @return status code that indicates the execution status of the function. |
Bethory | 0:6ad07c9019fd | 131 | |
Bethory | 0:6ad07c9019fd | 132 | @note You may call this function from ISR context. |
Bethory | 0:6ad07c9019fd | 133 | */ |
Bethory | 0:6ad07c9019fd | 134 | osStatus free(T *mptr) { |
Bethory | 0:6ad07c9019fd | 135 | return _pool.free(mptr); |
Bethory | 0:6ad07c9019fd | 136 | } |
Bethory | 0:6ad07c9019fd | 137 | |
Bethory | 0:6ad07c9019fd | 138 | private: |
Bethory | 0:6ad07c9019fd | 139 | Queue<T, queue_sz> _queue; |
Bethory | 0:6ad07c9019fd | 140 | MemoryPool<T, queue_sz> _pool; |
Bethory | 0:6ad07c9019fd | 141 | }; |
Bethory | 0:6ad07c9019fd | 142 | |
Bethory | 0:6ad07c9019fd | 143 | /** @}*/ |
Bethory | 0:6ad07c9019fd | 144 | /** @}*/ |
Bethory | 0:6ad07c9019fd | 145 | |
Bethory | 0:6ad07c9019fd | 146 | } |
Bethory | 0:6ad07c9019fd | 147 | |
Bethory | 0:6ad07c9019fd | 148 | #endif |
Bethory | 0:6ad07c9019fd | 149 | |
Bethory | 0:6ad07c9019fd | 150 | |
Bethory | 0:6ad07c9019fd | 151 |