Nicolas Borla
/
BBR_1Ebene
BBR 1 Ebene
mbed-os/rtos/Mail.h@0:fbdae7e6d805, 2018-05-14 (annotated)
- Committer:
- borlanic
- Date:
- Mon May 14 11:29:06 2018 +0000
- Revision:
- 0:fbdae7e6d805
BBR
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
borlanic | 0:fbdae7e6d805 | 1 | /* mbed Microcontroller Library |
borlanic | 0:fbdae7e6d805 | 2 | * Copyright (c) 2006-2017 ARM Limited |
borlanic | 0:fbdae7e6d805 | 3 | * |
borlanic | 0:fbdae7e6d805 | 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
borlanic | 0:fbdae7e6d805 | 5 | * of this software and associated documentation files (the "Software"), to deal |
borlanic | 0:fbdae7e6d805 | 6 | * in the Software without restriction, including without limitation the rights |
borlanic | 0:fbdae7e6d805 | 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
borlanic | 0:fbdae7e6d805 | 8 | * copies of the Software, and to permit persons to whom the Software is |
borlanic | 0:fbdae7e6d805 | 9 | * furnished to do so, subject to the following conditions: |
borlanic | 0:fbdae7e6d805 | 10 | * |
borlanic | 0:fbdae7e6d805 | 11 | * The above copyright notice and this permission notice shall be included in |
borlanic | 0:fbdae7e6d805 | 12 | * all copies or substantial portions of the Software. |
borlanic | 0:fbdae7e6d805 | 13 | * |
borlanic | 0:fbdae7e6d805 | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
borlanic | 0:fbdae7e6d805 | 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
borlanic | 0:fbdae7e6d805 | 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
borlanic | 0:fbdae7e6d805 | 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
borlanic | 0:fbdae7e6d805 | 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
borlanic | 0:fbdae7e6d805 | 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
borlanic | 0:fbdae7e6d805 | 20 | * SOFTWARE. |
borlanic | 0:fbdae7e6d805 | 21 | */ |
borlanic | 0:fbdae7e6d805 | 22 | #ifndef MAIL_H |
borlanic | 0:fbdae7e6d805 | 23 | #define MAIL_H |
borlanic | 0:fbdae7e6d805 | 24 | |
borlanic | 0:fbdae7e6d805 | 25 | #include <stdint.h> |
borlanic | 0:fbdae7e6d805 | 26 | #include <string.h> |
borlanic | 0:fbdae7e6d805 | 27 | |
borlanic | 0:fbdae7e6d805 | 28 | #include "Queue.h" |
borlanic | 0:fbdae7e6d805 | 29 | #include "MemoryPool.h" |
borlanic | 0:fbdae7e6d805 | 30 | #include "cmsis_os2.h" |
borlanic | 0:fbdae7e6d805 | 31 | #include "mbed_rtos_storage.h" |
borlanic | 0:fbdae7e6d805 | 32 | #include "mbed_rtos1_types.h" |
borlanic | 0:fbdae7e6d805 | 33 | |
borlanic | 0:fbdae7e6d805 | 34 | #include "platform/NonCopyable.h" |
borlanic | 0:fbdae7e6d805 | 35 | |
borlanic | 0:fbdae7e6d805 | 36 | using namespace rtos; |
borlanic | 0:fbdae7e6d805 | 37 | |
borlanic | 0:fbdae7e6d805 | 38 | namespace rtos { |
borlanic | 0:fbdae7e6d805 | 39 | /** \addtogroup rtos */ |
borlanic | 0:fbdae7e6d805 | 40 | /** @{*/ |
borlanic | 0:fbdae7e6d805 | 41 | /** |
borlanic | 0:fbdae7e6d805 | 42 | * \defgroup rtos_Mail Mail class |
borlanic | 0:fbdae7e6d805 | 43 | * @{ |
borlanic | 0:fbdae7e6d805 | 44 | */ |
borlanic | 0:fbdae7e6d805 | 45 | |
borlanic | 0:fbdae7e6d805 | 46 | /** The Mail class allow to control, send, receive, or wait for mail. |
borlanic | 0:fbdae7e6d805 | 47 | A mail is a memory block that is send to a thread or interrupt service routine. |
borlanic | 0:fbdae7e6d805 | 48 | @tparam T data type of a single message element. |
borlanic | 0:fbdae7e6d805 | 49 | @tparam queue_sz maximum number of messages in queue. |
borlanic | 0:fbdae7e6d805 | 50 | |
borlanic | 0:fbdae7e6d805 | 51 | @note |
borlanic | 0:fbdae7e6d805 | 52 | Memory considerations: The mail data store and control structures will be created on current thread's stack, |
borlanic | 0:fbdae7e6d805 | 53 | both for the mbed OS and underlying RTOS objects (static or dynamic RTOS memory pools are not being used). |
borlanic | 0:fbdae7e6d805 | 54 | */ |
borlanic | 0:fbdae7e6d805 | 55 | template<typename T, uint32_t queue_sz> |
borlanic | 0:fbdae7e6d805 | 56 | class Mail : private mbed::NonCopyable<Mail<T, queue_sz> > { |
borlanic | 0:fbdae7e6d805 | 57 | public: |
borlanic | 0:fbdae7e6d805 | 58 | /** Create and Initialize Mail queue. |
borlanic | 0:fbdae7e6d805 | 59 | * |
borlanic | 0:fbdae7e6d805 | 60 | * @note You cannot call this function from ISR context. |
borlanic | 0:fbdae7e6d805 | 61 | */ |
borlanic | 0:fbdae7e6d805 | 62 | Mail() { }; |
borlanic | 0:fbdae7e6d805 | 63 | |
borlanic | 0:fbdae7e6d805 | 64 | /** Check if the mail queue is empty |
borlanic | 0:fbdae7e6d805 | 65 | * |
borlanic | 0:fbdae7e6d805 | 66 | * @return True if the mail queue is empty, false if not |
borlanic | 0:fbdae7e6d805 | 67 | * |
borlanic | 0:fbdae7e6d805 | 68 | * @note You may call this function from ISR context. |
borlanic | 0:fbdae7e6d805 | 69 | */ |
borlanic | 0:fbdae7e6d805 | 70 | bool empty() const { |
borlanic | 0:fbdae7e6d805 | 71 | return _queue.empty(); |
borlanic | 0:fbdae7e6d805 | 72 | } |
borlanic | 0:fbdae7e6d805 | 73 | |
borlanic | 0:fbdae7e6d805 | 74 | /** Check if the mail queue is full |
borlanic | 0:fbdae7e6d805 | 75 | * |
borlanic | 0:fbdae7e6d805 | 76 | * @return True if the mail queue is full, false if not |
borlanic | 0:fbdae7e6d805 | 77 | * |
borlanic | 0:fbdae7e6d805 | 78 | * @note You may call this function from ISR context. |
borlanic | 0:fbdae7e6d805 | 79 | */ |
borlanic | 0:fbdae7e6d805 | 80 | bool full() const { |
borlanic | 0:fbdae7e6d805 | 81 | return _queue.full(); |
borlanic | 0:fbdae7e6d805 | 82 | } |
borlanic | 0:fbdae7e6d805 | 83 | |
borlanic | 0:fbdae7e6d805 | 84 | /** Allocate a memory block of type T |
borlanic | 0:fbdae7e6d805 | 85 | @param millisec timeout value or 0 in case of no time-out. (default: 0). |
borlanic | 0:fbdae7e6d805 | 86 | @return pointer to memory block that can be filled with mail or NULL in case error. |
borlanic | 0:fbdae7e6d805 | 87 | |
borlanic | 0:fbdae7e6d805 | 88 | @note You may call this function from ISR context if the millisec parameter is set to 0. |
borlanic | 0:fbdae7e6d805 | 89 | */ |
borlanic | 0:fbdae7e6d805 | 90 | T* alloc(uint32_t millisec=0) { |
borlanic | 0:fbdae7e6d805 | 91 | return _pool.alloc(); |
borlanic | 0:fbdae7e6d805 | 92 | } |
borlanic | 0:fbdae7e6d805 | 93 | |
borlanic | 0:fbdae7e6d805 | 94 | /** Allocate a memory block of type T and set memory block to zero. |
borlanic | 0:fbdae7e6d805 | 95 | @param millisec timeout value or 0 in case of no time-out. (default: 0). |
borlanic | 0:fbdae7e6d805 | 96 | @return pointer to memory block that can be filled with mail or NULL in case error. |
borlanic | 0:fbdae7e6d805 | 97 | |
borlanic | 0:fbdae7e6d805 | 98 | @note You may call this function from ISR context if the millisec parameter is set to 0. |
borlanic | 0:fbdae7e6d805 | 99 | */ |
borlanic | 0:fbdae7e6d805 | 100 | T* calloc(uint32_t millisec=0) { |
borlanic | 0:fbdae7e6d805 | 101 | return _pool.calloc(); |
borlanic | 0:fbdae7e6d805 | 102 | } |
borlanic | 0:fbdae7e6d805 | 103 | |
borlanic | 0:fbdae7e6d805 | 104 | /** Put a mail in the queue. |
borlanic | 0:fbdae7e6d805 | 105 | @param mptr memory block previously allocated with Mail::alloc or Mail::calloc. |
borlanic | 0:fbdae7e6d805 | 106 | @return status code that indicates the execution status of the function. |
borlanic | 0:fbdae7e6d805 | 107 | |
borlanic | 0:fbdae7e6d805 | 108 | @note You may call this function from ISR context. |
borlanic | 0:fbdae7e6d805 | 109 | */ |
borlanic | 0:fbdae7e6d805 | 110 | osStatus put(T *mptr) { |
borlanic | 0:fbdae7e6d805 | 111 | return _queue.put(mptr); |
borlanic | 0:fbdae7e6d805 | 112 | } |
borlanic | 0:fbdae7e6d805 | 113 | |
borlanic | 0:fbdae7e6d805 | 114 | /** Get a mail from a queue. |
borlanic | 0:fbdae7e6d805 | 115 | @param millisec timeout value or 0 in case of no time-out. (default: osWaitForever). |
borlanic | 0:fbdae7e6d805 | 116 | @return event that contains mail information or error code. |
borlanic | 0:fbdae7e6d805 | 117 | |
borlanic | 0:fbdae7e6d805 | 118 | @note You may call this function from ISR context if the millisec parameter is set to 0. |
borlanic | 0:fbdae7e6d805 | 119 | */ |
borlanic | 0:fbdae7e6d805 | 120 | osEvent get(uint32_t millisec=osWaitForever) { |
borlanic | 0:fbdae7e6d805 | 121 | osEvent evt = _queue.get(millisec); |
borlanic | 0:fbdae7e6d805 | 122 | if (evt.status == osEventMessage) { |
borlanic | 0:fbdae7e6d805 | 123 | evt.status = osEventMail; |
borlanic | 0:fbdae7e6d805 | 124 | } |
borlanic | 0:fbdae7e6d805 | 125 | return evt; |
borlanic | 0:fbdae7e6d805 | 126 | } |
borlanic | 0:fbdae7e6d805 | 127 | |
borlanic | 0:fbdae7e6d805 | 128 | /** Free a memory block from a mail. |
borlanic | 0:fbdae7e6d805 | 129 | @param mptr pointer to the memory block that was obtained with Mail::get. |
borlanic | 0:fbdae7e6d805 | 130 | @return status code that indicates the execution status of the function. |
borlanic | 0:fbdae7e6d805 | 131 | |
borlanic | 0:fbdae7e6d805 | 132 | @note You may call this function from ISR context. |
borlanic | 0:fbdae7e6d805 | 133 | */ |
borlanic | 0:fbdae7e6d805 | 134 | osStatus free(T *mptr) { |
borlanic | 0:fbdae7e6d805 | 135 | return _pool.free(mptr); |
borlanic | 0:fbdae7e6d805 | 136 | } |
borlanic | 0:fbdae7e6d805 | 137 | |
borlanic | 0:fbdae7e6d805 | 138 | private: |
borlanic | 0:fbdae7e6d805 | 139 | Queue<T, queue_sz> _queue; |
borlanic | 0:fbdae7e6d805 | 140 | MemoryPool<T, queue_sz> _pool; |
borlanic | 0:fbdae7e6d805 | 141 | }; |
borlanic | 0:fbdae7e6d805 | 142 | |
borlanic | 0:fbdae7e6d805 | 143 | /** @}*/ |
borlanic | 0:fbdae7e6d805 | 144 | /** @}*/ |
borlanic | 0:fbdae7e6d805 | 145 | |
borlanic | 0:fbdae7e6d805 | 146 | } |
borlanic | 0:fbdae7e6d805 | 147 | |
borlanic | 0:fbdae7e6d805 | 148 | #endif |
borlanic | 0:fbdae7e6d805 | 149 | |
borlanic | 0:fbdae7e6d805 | 150 | |
borlanic | 0:fbdae7e6d805 | 151 |