florent ollivier / Mbed 2 deprecated Gyro

Dependencies:   mbed

Committer:
flo__
Date:
Mon Mar 28 15:54:19 2022 +0000
Revision:
0:b435eadf76b4
28/03/2022

Who changed what in which revision?

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