Official mbed Real Time Operating System based on the RTX implementation of the CMSIS-RTOS API open standard.

Dependents:   denki-yohou_b TestY201 Network-RTOS NTPClient_HelloWorld ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Mail.h Source File

Mail.h

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2006-2012 ARM Limited
00003  *
00004  * Permission is hereby granted, free of charge, to any person obtaining a copy
00005  * of this software and associated documentation files (the "Software"), to deal
00006  * in the Software without restriction, including without limitation the rights
00007  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00008  * copies of the Software, and to permit persons to whom the Software is
00009  * furnished to do so, subject to the following conditions:
00010  *
00011  * The above copyright notice and this permission notice shall be included in
00012  * all copies or substantial portions of the Software.
00013  *
00014  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00015  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00016  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00017  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00018  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00019  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
00020  * SOFTWARE.
00021  */
00022 #ifndef MAIL_H
00023 #define MAIL_H
00024 
00025 #include <stdint.h>
00026 #include <string.h>
00027 
00028 #include "cmsis_os.h"
00029 
00030 namespace rtos {
00031 /** \addtogroup rtos */
00032 /** @{*/
00033 
00034 /** The Mail class allow to control, send, receive, or wait for mail.
00035  A mail is a memory block that is send to a thread or interrupt service routine.
00036   @tparam  T         data type of a single message element.
00037   @tparam  queue_sz  maximum number of messages in queue.
00038 */
00039 template<typename T, uint32_t queue_sz>
00040 class Mail {
00041 public:
00042     /** Create and Initialise Mail queue. */
00043     Mail() {
00044     #ifdef CMSIS_OS_RTX
00045         memset(_mail_q, 0, sizeof(_mail_q));
00046         _mail_p[0] = _mail_q;
00047 
00048         memset(_mail_m, 0, sizeof(_mail_m));
00049         _mail_p[1] = _mail_m;
00050 
00051         _mail_def.pool = _mail_p;
00052         _mail_def.queue_sz = queue_sz;
00053         _mail_def.item_sz = sizeof(T);
00054     #endif
00055         _mail_id = osMailCreate(&_mail_def, NULL);
00056     }
00057 
00058     /** Allocate a memory block of type T
00059       @param   millisec  timeout value or 0 in case of no time-out. (default: 0).
00060       @return  pointer to memory block that can be filled with mail or NULL in case error.
00061     */
00062     T* alloc(uint32_t millisec=0) {
00063         return (T*)osMailAlloc(_mail_id, millisec);
00064     }
00065 
00066     /** Allocate a memory block of type T and set memory block to zero.
00067       @param   millisec  timeout value or 0 in case of no time-out.  (default: 0).
00068       @return  pointer to memory block that can be filled with mail or NULL in case error.
00069     */
00070     T* calloc(uint32_t millisec=0) {
00071         return (T*)osMailCAlloc(_mail_id, millisec);
00072     }
00073 
00074     /** Put a mail in the queue.
00075       @param   mptr  memory block previously allocated with Mail::alloc or Mail::calloc.
00076       @return  status code that indicates the execution status of the function.
00077     */
00078     osStatus put(T *mptr) {
00079         return osMailPut(_mail_id, (void*)mptr);
00080     }
00081 
00082     /** Get a mail from a queue.
00083       @param   millisec  timeout value or 0 in case of no time-out. (default: osWaitForever).
00084       @return  event that contains mail information or error code.
00085     */
00086     osEvent get(uint32_t millisec=osWaitForever) {
00087         return osMailGet(_mail_id, millisec);
00088     }
00089 
00090     /** Free a memory block from a mail.
00091       @param   mptr  pointer to the memory block that was obtained with Mail::get.
00092       @return  status code that indicates the execution status of the function.
00093     */
00094     osStatus free(T *mptr) {
00095         return osMailFree(_mail_id, (void*)mptr);
00096     }
00097 
00098 private:
00099     osMailQId    _mail_id;
00100     osMailQDef_t _mail_def;
00101 #ifdef CMSIS_OS_RTX
00102     uint32_t     _mail_q[4+(queue_sz)];
00103     uint32_t     _mail_m[3+((sizeof(T)+3)/4)*(queue_sz)];
00104     void        *_mail_p[2];
00105 #endif
00106 };
00107 
00108 }
00109 
00110 #endif
00111 
00112 
00113 /** @}*/