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