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