OS2

Dependents:   GYRO_MPU6050 Bluetooth_Powered_Multimeter_Using_STM32F429_and_RTOS fyp

Committer:
guilhemMBED
Date:
Mon Feb 03 13:41:14 2020 +0000
Revision:
0:a7c449cd2d5a
previous version;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
guilhemMBED 0:a7c449cd2d5a 1 /* mbed Microcontroller Library
guilhemMBED 0:a7c449cd2d5a 2 * Copyright (c) 2006-2012 ARM Limited
guilhemMBED 0:a7c449cd2d5a 3 *
guilhemMBED 0:a7c449cd2d5a 4 * Permission is hereby granted, free of charge, to any person obtaining a copy
guilhemMBED 0:a7c449cd2d5a 5 * of this software and associated documentation files (the "Software"), to deal
guilhemMBED 0:a7c449cd2d5a 6 * in the Software without restriction, including without limitation the rights
guilhemMBED 0:a7c449cd2d5a 7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
guilhemMBED 0:a7c449cd2d5a 8 * copies of the Software, and to permit persons to whom the Software is
guilhemMBED 0:a7c449cd2d5a 9 * furnished to do so, subject to the following conditions:
guilhemMBED 0:a7c449cd2d5a 10 *
guilhemMBED 0:a7c449cd2d5a 11 * The above copyright notice and this permission notice shall be included in
guilhemMBED 0:a7c449cd2d5a 12 * all copies or substantial portions of the Software.
guilhemMBED 0:a7c449cd2d5a 13 *
guilhemMBED 0:a7c449cd2d5a 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
guilhemMBED 0:a7c449cd2d5a 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
guilhemMBED 0:a7c449cd2d5a 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
guilhemMBED 0:a7c449cd2d5a 17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
guilhemMBED 0:a7c449cd2d5a 18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
guilhemMBED 0:a7c449cd2d5a 19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
guilhemMBED 0:a7c449cd2d5a 20 * SOFTWARE.
guilhemMBED 0:a7c449cd2d5a 21 */
guilhemMBED 0:a7c449cd2d5a 22 #ifndef QUEUE_H
guilhemMBED 0:a7c449cd2d5a 23 #define QUEUE_H
guilhemMBED 0:a7c449cd2d5a 24
guilhemMBED 0:a7c449cd2d5a 25 #include <stdint.h>
guilhemMBED 0:a7c449cd2d5a 26 #include <string.h>
guilhemMBED 0:a7c449cd2d5a 27
guilhemMBED 0:a7c449cd2d5a 28 #include "cmsis_os.h"
guilhemMBED 0:a7c449cd2d5a 29 #include "platform/mbed_error.h"
guilhemMBED 0:a7c449cd2d5a 30
guilhemMBED 0:a7c449cd2d5a 31 namespace rtos {
guilhemMBED 0:a7c449cd2d5a 32 /** \addtogroup rtos */
guilhemMBED 0:a7c449cd2d5a 33 /** @{*/
guilhemMBED 0:a7c449cd2d5a 34
guilhemMBED 0:a7c449cd2d5a 35 /** The Queue class allow to control, send, receive, or wait for messages.
guilhemMBED 0:a7c449cd2d5a 36 A message can be a integer or pointer value to a certain type T that is send
guilhemMBED 0:a7c449cd2d5a 37 to a thread or interrupt service routine.
guilhemMBED 0:a7c449cd2d5a 38 @tparam T data type of a single message element.
guilhemMBED 0:a7c449cd2d5a 39 @tparam queue_sz maximum number of messages in queue.
guilhemMBED 0:a7c449cd2d5a 40 */
guilhemMBED 0:a7c449cd2d5a 41 template<typename T, uint32_t queue_sz>
guilhemMBED 0:a7c449cd2d5a 42 class Queue {
guilhemMBED 0:a7c449cd2d5a 43 public:
guilhemMBED 0:a7c449cd2d5a 44 /** Create and initialise a message Queue. */
guilhemMBED 0:a7c449cd2d5a 45 Queue() {
guilhemMBED 0:a7c449cd2d5a 46 #ifdef CMSIS_OS_RTX
guilhemMBED 0:a7c449cd2d5a 47 memset(_queue_q, 0, sizeof(_queue_q));
guilhemMBED 0:a7c449cd2d5a 48 _queue_def.pool = _queue_q;
guilhemMBED 0:a7c449cd2d5a 49 _queue_def.queue_sz = queue_sz;
guilhemMBED 0:a7c449cd2d5a 50 #endif
guilhemMBED 0:a7c449cd2d5a 51 _queue_id = osMessageCreate(&_queue_def, NULL);
guilhemMBED 0:a7c449cd2d5a 52 if (_queue_id == NULL) {
guilhemMBED 0:a7c449cd2d5a 53 error("Error initialising the queue object\n");
guilhemMBED 0:a7c449cd2d5a 54 }
guilhemMBED 0:a7c449cd2d5a 55 }
guilhemMBED 0:a7c449cd2d5a 56
guilhemMBED 0:a7c449cd2d5a 57 /** Put a message in a Queue.
guilhemMBED 0:a7c449cd2d5a 58 @param data message pointer.
guilhemMBED 0:a7c449cd2d5a 59 @param millisec timeout value or 0 in case of no time-out. (default: 0)
guilhemMBED 0:a7c449cd2d5a 60 @return status code that indicates the execution status of the function.
guilhemMBED 0:a7c449cd2d5a 61 */
guilhemMBED 0:a7c449cd2d5a 62 osStatus put(T* data, uint32_t millisec=0) {
guilhemMBED 0:a7c449cd2d5a 63 return osMessagePut(_queue_id, (uint32_t)data, millisec);
guilhemMBED 0:a7c449cd2d5a 64 }
guilhemMBED 0:a7c449cd2d5a 65
guilhemMBED 0:a7c449cd2d5a 66 /** Get a message or Wait for a message from a Queue.
guilhemMBED 0:a7c449cd2d5a 67 @param millisec timeout value or 0 in case of no time-out. (default: osWaitForever).
guilhemMBED 0:a7c449cd2d5a 68 @return event information that includes the message and the status code.
guilhemMBED 0:a7c449cd2d5a 69 */
guilhemMBED 0:a7c449cd2d5a 70 osEvent get(uint32_t millisec=osWaitForever) {
guilhemMBED 0:a7c449cd2d5a 71 return osMessageGet(_queue_id, millisec);
guilhemMBED 0:a7c449cd2d5a 72 }
guilhemMBED 0:a7c449cd2d5a 73
guilhemMBED 0:a7c449cd2d5a 74 private:
guilhemMBED 0:a7c449cd2d5a 75 osMessageQId _queue_id;
guilhemMBED 0:a7c449cd2d5a 76 osMessageQDef_t _queue_def;
guilhemMBED 0:a7c449cd2d5a 77 #ifdef CMSIS_OS_RTX
guilhemMBED 0:a7c449cd2d5a 78 uint32_t _queue_q[4+(queue_sz)];
guilhemMBED 0:a7c449cd2d5a 79 #endif
guilhemMBED 0:a7c449cd2d5a 80 };
guilhemMBED 0:a7c449cd2d5a 81
guilhemMBED 0:a7c449cd2d5a 82 }
guilhemMBED 0:a7c449cd2d5a 83 #endif
guilhemMBED 0:a7c449cd2d5a 84
guilhemMBED 0:a7c449cd2d5a 85 /** @}*/