local fork

Dependents:   Encrypted

Fork of mbed-rtos by mbed official

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Queue.h Source File

Queue.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 QUEUE_H
00023 #define QUEUE_H
00024 
00025 #include <stdint.h>
00026 #include <string.h>
00027 
00028 #include "cmsis_os.h"
00029 #include "error.h"
00030 
00031 namespace rtos {
00032 
00033 /** The Queue class allow to control, send, receive, or wait for messages.
00034  A message can be a integer or pointer value  to a certain type T that is send
00035  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 Queue {
00041 public:
00042     /** Create and initialise a message Queue. */
00043     Queue() {
00044     #ifdef CMSIS_OS_RTX
00045         memset(_queue_q, 0, sizeof(_queue_q));
00046         _queue_def.pool = _queue_q;
00047         _queue_def.queue_sz = queue_sz;
00048     #endif
00049         _queue_id = osMessageCreate(&_queue_def, NULL);
00050         if (_queue_id == NULL) {
00051             error("Error initialising the queue object\n");
00052         }
00053     }
00054     
00055     /** Put a message in a Queue.
00056       @param   data      message pointer.
00057       @param   millisec  timeout value or 0 in case of no time-out. (default: 0)
00058       @return  status code that indicates the execution status of the function. 
00059     */
00060     osStatus put(T* data, uint32_t millisec=0) {
00061         return osMessagePut(_queue_id, (uint32_t)data, millisec);
00062     }
00063     
00064     /** Get a message or Wait for a message from a Queue.
00065       @param   millisec  timeout value or 0 in case of no time-out. (default: osWaitForever).
00066       @return  event information that includes the message and the status code.
00067     */
00068     osEvent get(uint32_t millisec=osWaitForever) {
00069         return osMessageGet(_queue_id, millisec);
00070     }
00071 
00072 private:
00073     osMessageQId    _queue_id;
00074     osMessageQDef_t _queue_def;
00075 #ifdef CMSIS_OS_RTX
00076     uint32_t        _queue_q[4+(queue_sz)];
00077 #endif
00078 };
00079 
00080 }
00081 #endif