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