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