Add support new target: ST Nucleo-L152RE Official mbed Real Time Operating System based on the RTX implementation of the CMSIS-RTOS API open standard.

Fork of mbed-rtos by mbed official

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 
00032 /** The Mail class allow to control, send, receive, or wait for mail.
00033  A mail is a memory block that is send to a thread or interrupt service routine.
00034   @tparam  T         data type of a single message element.
00035   @tparam  queue_sz  maximum number of messages in queue.
00036 */
00037 template<typename T, uint32_t queue_sz>
00038 class Mail {
00039 public:
00040     /** Create and Initialise Mail queue. */
00041     Mail() {
00042     #ifdef CMSIS_OS_RTX
00043         memset(_mail_q, 0, sizeof(_mail_q));
00044         _mail_p[0] = _mail_q;
00045         
00046         memset(_mail_m, 0, sizeof(_mail_m));
00047         _mail_p[1] = _mail_m;
00048         
00049         _mail_def.pool = _mail_p;
00050         _mail_def.queue_sz = queue_sz;
00051         _mail_def.item_sz = sizeof(T);
00052     #endif
00053         _mail_id = osMailCreate(&_mail_def, NULL);
00054     }
00055     
00056     /** Allocate a memory block of type T
00057       @param   millisec  timeout value or 0 in case of no time-out. (default: 0).
00058       @return  pointer to memory block that can be filled with mail or NULL in case error.
00059     */
00060     T* alloc(uint32_t millisec=0) {
00061         return (T*)osMailAlloc(_mail_id, millisec);
00062     }
00063     
00064     /** Allocate a memory block of type T and set memory block to zero. 
00065       @param   millisec  timeout value or 0 in case of no time-out.  (default: 0).
00066       @return  pointer to memory block that can be filled with mail or NULL in case error.
00067     */
00068     T* calloc(uint32_t millisec=0) {
00069         return (T*)osMailCAlloc(_mail_id, millisec);
00070     }
00071     
00072     /** Put a mail in the queue.
00073       @param   mptr  memory block previously allocated with Mail::alloc or Mail::calloc.
00074       @return  status code that indicates the execution status of the function. 
00075     */
00076     osStatus put(T *mptr) {
00077         return osMailPut(_mail_id, (void*)mptr);
00078     }
00079     
00080     /** Get a mail from a queue.
00081       @param   millisec  timeout value or 0 in case of no time-out. (default: osWaitForever).
00082       @return  event that contains mail information or error code.
00083     */
00084     osEvent get(uint32_t millisec=osWaitForever) {
00085         return osMailGet(_mail_id, millisec);
00086     }
00087     
00088     /** Free a memory block from a mail.
00089       @param   mptr  pointer to the memory block that was obtained with Mail::get. 
00090       @return  status code that indicates the execution status of the function.
00091     */
00092     osStatus free(T *mptr) {
00093         return osMailFree(_mail_id, (void*)mptr);
00094     }
00095 
00096 private:
00097     osMailQId    _mail_id;
00098     osMailQDef_t _mail_def;
00099 #ifdef CMSIS_OS_RTX
00100     uint32_t     _mail_q[4+(queue_sz)];
00101     uint32_t     _mail_m[3+((sizeof(T)+3)/4)*(queue_sz)];
00102     void        *_mail_p[2];
00103 #endif
00104 };
00105 
00106 }
00107 
00108 #endif
00109