Lee Shen / FTHR_USB_serial_qSPI
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-2017 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 "Queue.h"
00029 #include "MemoryPool.h"
00030 #include "cmsis_os2.h"
00031 #include "mbed_rtos_storage.h"
00032 #include "mbed_rtos1_types.h"
00033 
00034 #include "platform/NonCopyable.h"
00035 
00036 using namespace rtos;
00037 
00038 namespace rtos {
00039 /** \addtogroup rtos */
00040 /** @{*/
00041 
00042 /** The Mail class allow to control, send, receive, or wait for mail.
00043  A mail is a memory block that is send to a thread or interrupt service routine.
00044   @tparam  T         data type of a single message element.
00045   @tparam  queue_sz  maximum number of messages in queue.
00046 
00047  @note
00048  Memory considerations: The mail data store and control structures will be created on current thread's stack,
00049  both for the mbed OS and underlying RTOS objects (static or dynamic RTOS memory pools are not being used).
00050 */
00051 template<typename T, uint32_t queue_sz>
00052 class Mail : private mbed::NonCopyable<Mail<T, queue_sz> > {
00053 public:
00054     /** Create and Initialise Mail queue. */
00055     Mail() { };
00056 
00057     /** Allocate a memory block of type T
00058       @param   millisec  timeout value or 0 in case of no time-out. (default: 0).
00059       @return  pointer to memory block that can be filled with mail or NULL in case error.
00060     */
00061     T* alloc(uint32_t millisec=0) {
00062         return _pool.alloc();
00063     }
00064 
00065     /** Allocate a memory block of type T and set memory block to zero.
00066       @param   millisec  timeout value or 0 in case of no time-out.  (default: 0).
00067       @return  pointer to memory block that can be filled with mail or NULL in case error.
00068     */
00069     T* calloc(uint32_t millisec=0) {
00070         return _pool.calloc();
00071     }
00072 
00073     /** Put a mail in the queue.
00074       @param   mptr  memory block previously allocated with Mail::alloc or Mail::calloc.
00075       @return  status code that indicates the execution status of the function.
00076     */
00077     osStatus put(T *mptr) {
00078         return _queue.put(mptr);
00079     }
00080 
00081     /** Get a mail from a queue.
00082       @param   millisec  timeout value or 0 in case of no time-out. (default: osWaitForever).
00083       @return  event that contains mail information or error code.
00084     */
00085     osEvent get(uint32_t millisec=osWaitForever) {
00086         osEvent evt = _queue.get(millisec);
00087         if (evt.status == osEventMessage) {
00088             evt.status = osEventMail;
00089         }
00090         return evt;
00091     }
00092 
00093     /** Free a memory block from a mail.
00094       @param   mptr  pointer to the memory block that was obtained with Mail::get.
00095       @return  status code that indicates the execution status of the function.
00096     */
00097     osStatus free(T *mptr) {
00098         return _pool.free(mptr);
00099     }
00100 
00101 private:
00102     Queue<T, queue_sz> _queue;
00103     MemoryPool<T, queue_sz>  _pool;
00104 };
00105 
00106 }
00107 
00108 #endif
00109 
00110 
00111 /** @}*/