ese519

Dependencies:   mbed

Committer:
Jing_Qiu
Date:
Sat Mar 21 02:54:25 2015 +0000
Revision:
0:95aa779116f0
ese519

Who changed what in which revision?

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