mbed-os

Fork of mbed-os by erkin yucel

Committer:
xuaner
Date:
Thu Jul 20 14:26:57 2017 +0000
Revision:
1:3deb71413561
Parent:
0:f269e3021894
mbed_os

Who changed what in which revision?

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