fork
rtos/Mail.h@126:5713cbbdb706, 2017-07-04 (annotated)
- Committer:
- Kojto
- Date:
- Tue Jul 04 13:32:20 2017 +0100
- Revision:
- 126:5713cbbdb706
- Parent:
- 123:58563e6cba1e
replace mbed_rtx by mbed_rtx4
Not causing a conflict with mbed_rtx that is for newer rtos
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
emilmont | 8:88a1a9c26ae3 | 1 | /* mbed Microcontroller Library |
emilmont | 8:88a1a9c26ae3 | 2 | * Copyright (c) 2006-2012 ARM Limited |
emilmont | 8:88a1a9c26ae3 | 3 | * |
emilmont | 8:88a1a9c26ae3 | 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
emilmont | 8:88a1a9c26ae3 | 5 | * of this software and associated documentation files (the "Software"), to deal |
emilmont | 8:88a1a9c26ae3 | 6 | * in the Software without restriction, including without limitation the rights |
emilmont | 8:88a1a9c26ae3 | 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
emilmont | 8:88a1a9c26ae3 | 8 | * copies of the Software, and to permit persons to whom the Software is |
emilmont | 8:88a1a9c26ae3 | 9 | * furnished to do so, subject to the following conditions: |
emilmont | 8:88a1a9c26ae3 | 10 | * |
emilmont | 8:88a1a9c26ae3 | 11 | * The above copyright notice and this permission notice shall be included in |
emilmont | 8:88a1a9c26ae3 | 12 | * all copies or substantial portions of the Software. |
emilmont | 8:88a1a9c26ae3 | 13 | * |
emilmont | 8:88a1a9c26ae3 | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
emilmont | 8:88a1a9c26ae3 | 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
emilmont | 8:88a1a9c26ae3 | 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
emilmont | 8:88a1a9c26ae3 | 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
emilmont | 8:88a1a9c26ae3 | 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
emilmont | 8:88a1a9c26ae3 | 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
emilmont | 8:88a1a9c26ae3 | 20 | * SOFTWARE. |
emilmont | 8:88a1a9c26ae3 | 21 | */ |
emilmont | 8:88a1a9c26ae3 | 22 | #ifndef MAIL_H |
emilmont | 8:88a1a9c26ae3 | 23 | #define MAIL_H |
emilmont | 8:88a1a9c26ae3 | 24 | |
emilmont | 8:88a1a9c26ae3 | 25 | #include <stdint.h> |
emilmont | 8:88a1a9c26ae3 | 26 | #include <string.h> |
emilmont | 8:88a1a9c26ae3 | 27 | |
emilmont | 8:88a1a9c26ae3 | 28 | #include "cmsis_os.h" |
emilmont | 8:88a1a9c26ae3 | 29 | |
emilmont | 8:88a1a9c26ae3 | 30 | namespace rtos { |
c1728p9 | 123:58563e6cba1e | 31 | /** \addtogroup rtos */ |
c1728p9 | 123:58563e6cba1e | 32 | /** @{*/ |
emilmont | 8:88a1a9c26ae3 | 33 | |
emilmont | 8:88a1a9c26ae3 | 34 | /** The Mail class allow to control, send, receive, or wait for mail. |
emilmont | 8:88a1a9c26ae3 | 35 | A mail is a memory block that is send to a thread or interrupt service routine. |
emilmont | 8:88a1a9c26ae3 | 36 | @tparam T data type of a single message element. |
emilmont | 8:88a1a9c26ae3 | 37 | @tparam queue_sz maximum number of messages in queue. |
emilmont | 8:88a1a9c26ae3 | 38 | */ |
emilmont | 8:88a1a9c26ae3 | 39 | template<typename T, uint32_t queue_sz> |
emilmont | 8:88a1a9c26ae3 | 40 | class Mail { |
emilmont | 8:88a1a9c26ae3 | 41 | public: |
emilmont | 8:88a1a9c26ae3 | 42 | /** Create and Initialise Mail queue. */ |
emilmont | 8:88a1a9c26ae3 | 43 | Mail() { |
emilmont | 8:88a1a9c26ae3 | 44 | #ifdef CMSIS_OS_RTX |
emilmont | 8:88a1a9c26ae3 | 45 | memset(_mail_q, 0, sizeof(_mail_q)); |
emilmont | 8:88a1a9c26ae3 | 46 | _mail_p[0] = _mail_q; |
mbed_official | 31:015df9e602b6 | 47 | |
emilmont | 8:88a1a9c26ae3 | 48 | memset(_mail_m, 0, sizeof(_mail_m)); |
emilmont | 8:88a1a9c26ae3 | 49 | _mail_p[1] = _mail_m; |
mbed_official | 31:015df9e602b6 | 50 | |
emilmont | 8:88a1a9c26ae3 | 51 | _mail_def.pool = _mail_p; |
emilmont | 8:88a1a9c26ae3 | 52 | _mail_def.queue_sz = queue_sz; |
emilmont | 8:88a1a9c26ae3 | 53 | _mail_def.item_sz = sizeof(T); |
emilmont | 8:88a1a9c26ae3 | 54 | #endif |
emilmont | 8:88a1a9c26ae3 | 55 | _mail_id = osMailCreate(&_mail_def, NULL); |
emilmont | 8:88a1a9c26ae3 | 56 | } |
mbed_official | 31:015df9e602b6 | 57 | |
emilmont | 8:88a1a9c26ae3 | 58 | /** Allocate a memory block of type T |
emilmont | 8:88a1a9c26ae3 | 59 | @param millisec timeout value or 0 in case of no time-out. (default: 0). |
emilmont | 8:88a1a9c26ae3 | 60 | @return pointer to memory block that can be filled with mail or NULL in case error. |
emilmont | 8:88a1a9c26ae3 | 61 | */ |
emilmont | 8:88a1a9c26ae3 | 62 | T* alloc(uint32_t millisec=0) { |
emilmont | 8:88a1a9c26ae3 | 63 | return (T*)osMailAlloc(_mail_id, millisec); |
emilmont | 8:88a1a9c26ae3 | 64 | } |
mbed_official | 31:015df9e602b6 | 65 | |
mbed_official | 31:015df9e602b6 | 66 | /** Allocate a memory block of type T and set memory block to zero. |
emilmont | 8:88a1a9c26ae3 | 67 | @param millisec timeout value or 0 in case of no time-out. (default: 0). |
emilmont | 8:88a1a9c26ae3 | 68 | @return pointer to memory block that can be filled with mail or NULL in case error. |
emilmont | 8:88a1a9c26ae3 | 69 | */ |
emilmont | 8:88a1a9c26ae3 | 70 | T* calloc(uint32_t millisec=0) { |
emilmont | 8:88a1a9c26ae3 | 71 | return (T*)osMailCAlloc(_mail_id, millisec); |
emilmont | 8:88a1a9c26ae3 | 72 | } |
mbed_official | 31:015df9e602b6 | 73 | |
emilmont | 8:88a1a9c26ae3 | 74 | /** Put a mail in the queue. |
emilmont | 8:88a1a9c26ae3 | 75 | @param mptr memory block previously allocated with Mail::alloc or Mail::calloc. |
mbed_official | 31:015df9e602b6 | 76 | @return status code that indicates the execution status of the function. |
emilmont | 8:88a1a9c26ae3 | 77 | */ |
emilmont | 8:88a1a9c26ae3 | 78 | osStatus put(T *mptr) { |
emilmont | 8:88a1a9c26ae3 | 79 | return osMailPut(_mail_id, (void*)mptr); |
emilmont | 8:88a1a9c26ae3 | 80 | } |
mbed_official | 31:015df9e602b6 | 81 | |
emilmont | 8:88a1a9c26ae3 | 82 | /** Get a mail from a queue. |
emilmont | 8:88a1a9c26ae3 | 83 | @param millisec timeout value or 0 in case of no time-out. (default: osWaitForever). |
emilmont | 8:88a1a9c26ae3 | 84 | @return event that contains mail information or error code. |
emilmont | 8:88a1a9c26ae3 | 85 | */ |
emilmont | 8:88a1a9c26ae3 | 86 | osEvent get(uint32_t millisec=osWaitForever) { |
emilmont | 8:88a1a9c26ae3 | 87 | return osMailGet(_mail_id, millisec); |
emilmont | 8:88a1a9c26ae3 | 88 | } |
mbed_official | 31:015df9e602b6 | 89 | |
emilmont | 8:88a1a9c26ae3 | 90 | /** Free a memory block from a mail. |
mbed_official | 31:015df9e602b6 | 91 | @param mptr pointer to the memory block that was obtained with Mail::get. |
emilmont | 8:88a1a9c26ae3 | 92 | @return status code that indicates the execution status of the function. |
emilmont | 8:88a1a9c26ae3 | 93 | */ |
emilmont | 8:88a1a9c26ae3 | 94 | osStatus free(T *mptr) { |
emilmont | 8:88a1a9c26ae3 | 95 | return osMailFree(_mail_id, (void*)mptr); |
emilmont | 8:88a1a9c26ae3 | 96 | } |
emilmont | 8:88a1a9c26ae3 | 97 | |
emilmont | 8:88a1a9c26ae3 | 98 | private: |
emilmont | 8:88a1a9c26ae3 | 99 | osMailQId _mail_id; |
emilmont | 8:88a1a9c26ae3 | 100 | osMailQDef_t _mail_def; |
emilmont | 8:88a1a9c26ae3 | 101 | #ifdef CMSIS_OS_RTX |
emilmont | 8:88a1a9c26ae3 | 102 | uint32_t _mail_q[4+(queue_sz)]; |
emilmont | 8:88a1a9c26ae3 | 103 | uint32_t _mail_m[3+((sizeof(T)+3)/4)*(queue_sz)]; |
emilmont | 8:88a1a9c26ae3 | 104 | void *_mail_p[2]; |
emilmont | 8:88a1a9c26ae3 | 105 | #endif |
emilmont | 8:88a1a9c26ae3 | 106 | }; |
emilmont | 8:88a1a9c26ae3 | 107 | |
emilmont | 8:88a1a9c26ae3 | 108 | } |
emilmont | 8:88a1a9c26ae3 | 109 | |
emilmont | 8:88a1a9c26ae3 | 110 | #endif |
emilmont | 8:88a1a9c26ae3 | 111 | |
c1728p9 | 123:58563e6cba1e | 112 | |
c1728p9 | 123:58563e6cba1e | 113 | /** @}*/ |