Dependents:   sensomed

Committer:
switches
Date:
Tue Nov 08 18:27:11 2016 +0000
Revision:
0:0e018d759a2a
Initial commit

Who changed what in which revision?

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