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