desp koval / mbed-rtos

Dependents:   Mecatro_Filtre

Committer:
mecatro_prod
Date:
Mon Mar 15 14:18:11 2021 +0000
Revision:
0:4a2c9c1f5b9e
Pas de changement

Who changed what in which revision?

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