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

Committer:
acarter2
Date:
Fri Apr 29 14:04:02 2016 +0000
Revision:
111:de54bc598ef1
current code 2;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
acarter2 111:de54bc598ef1 1 /* mbed Microcontroller Library
acarter2 111:de54bc598ef1 2 * Copyright (c) 2006-2012 ARM Limited
acarter2 111:de54bc598ef1 3 *
acarter2 111:de54bc598ef1 4 * Permission is hereby granted, free of charge, to any person obtaining a copy
acarter2 111:de54bc598ef1 5 * of this software and associated documentation files (the "Software"), to deal
acarter2 111:de54bc598ef1 6 * in the Software without restriction, including without limitation the rights
acarter2 111:de54bc598ef1 7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
acarter2 111:de54bc598ef1 8 * copies of the Software, and to permit persons to whom the Software is
acarter2 111:de54bc598ef1 9 * furnished to do so, subject to the following conditions:
acarter2 111:de54bc598ef1 10 *
acarter2 111:de54bc598ef1 11 * The above copyright notice and this permission notice shall be included in
acarter2 111:de54bc598ef1 12 * all copies or substantial portions of the Software.
acarter2 111:de54bc598ef1 13 *
acarter2 111:de54bc598ef1 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
acarter2 111:de54bc598ef1 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
acarter2 111:de54bc598ef1 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
acarter2 111:de54bc598ef1 17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
acarter2 111:de54bc598ef1 18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
acarter2 111:de54bc598ef1 19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
acarter2 111:de54bc598ef1 20 * SOFTWARE.
acarter2 111:de54bc598ef1 21 */
acarter2 111:de54bc598ef1 22 #ifndef MAIL_H
acarter2 111:de54bc598ef1 23 #define MAIL_H
acarter2 111:de54bc598ef1 24
acarter2 111:de54bc598ef1 25 #include <stdint.h>
acarter2 111:de54bc598ef1 26 #include <string.h>
acarter2 111:de54bc598ef1 27
acarter2 111:de54bc598ef1 28 #include "cmsis_os.h"
acarter2 111:de54bc598ef1 29
acarter2 111:de54bc598ef1 30 namespace rtos {
acarter2 111:de54bc598ef1 31
acarter2 111:de54bc598ef1 32 /** The Mail class allow to control, send, receive, or wait for mail.
acarter2 111:de54bc598ef1 33 A mail is a memory block that is send to a thread or interrupt service routine.
acarter2 111:de54bc598ef1 34 @tparam T data type of a single message element.
acarter2 111:de54bc598ef1 35 @tparam queue_sz maximum number of messages in queue.
acarter2 111:de54bc598ef1 36 */
acarter2 111:de54bc598ef1 37 template<typename T, uint32_t queue_sz>
acarter2 111:de54bc598ef1 38 class Mail {
acarter2 111:de54bc598ef1 39 public:
acarter2 111:de54bc598ef1 40 /** Create and Initialise Mail queue. */
acarter2 111:de54bc598ef1 41 Mail() {
acarter2 111:de54bc598ef1 42 #ifdef CMSIS_OS_RTX
acarter2 111:de54bc598ef1 43 memset(_mail_q, 0, sizeof(_mail_q));
acarter2 111:de54bc598ef1 44 _mail_p[0] = _mail_q;
acarter2 111:de54bc598ef1 45
acarter2 111:de54bc598ef1 46 memset(_mail_m, 0, sizeof(_mail_m));
acarter2 111:de54bc598ef1 47 _mail_p[1] = _mail_m;
acarter2 111:de54bc598ef1 48
acarter2 111:de54bc598ef1 49 _mail_def.pool = _mail_p;
acarter2 111:de54bc598ef1 50 _mail_def.queue_sz = queue_sz;
acarter2 111:de54bc598ef1 51 _mail_def.item_sz = sizeof(T);
acarter2 111:de54bc598ef1 52 #endif
acarter2 111:de54bc598ef1 53 _mail_id = osMailCreate(&_mail_def, NULL);
acarter2 111:de54bc598ef1 54 }
acarter2 111:de54bc598ef1 55
acarter2 111:de54bc598ef1 56 /** Allocate a memory block of type T
acarter2 111:de54bc598ef1 57 @param millisec timeout value or 0 in case of no time-out. (default: 0).
acarter2 111:de54bc598ef1 58 @return pointer to memory block that can be filled with mail or NULL in case error.
acarter2 111:de54bc598ef1 59 */
acarter2 111:de54bc598ef1 60 T* alloc(uint32_t millisec=0) {
acarter2 111:de54bc598ef1 61 return (T*)osMailAlloc(_mail_id, millisec);
acarter2 111:de54bc598ef1 62 }
acarter2 111:de54bc598ef1 63
acarter2 111:de54bc598ef1 64 /** Allocate a memory block of type T and set memory block to zero.
acarter2 111:de54bc598ef1 65 @param millisec timeout value or 0 in case of no time-out. (default: 0).
acarter2 111:de54bc598ef1 66 @return pointer to memory block that can be filled with mail or NULL in case error.
acarter2 111:de54bc598ef1 67 */
acarter2 111:de54bc598ef1 68 T* calloc(uint32_t millisec=0) {
acarter2 111:de54bc598ef1 69 return (T*)osMailCAlloc(_mail_id, millisec);
acarter2 111:de54bc598ef1 70 }
acarter2 111:de54bc598ef1 71
acarter2 111:de54bc598ef1 72 /** Put a mail in the queue.
acarter2 111:de54bc598ef1 73 @param mptr memory block previously allocated with Mail::alloc or Mail::calloc.
acarter2 111:de54bc598ef1 74 @return status code that indicates the execution status of the function.
acarter2 111:de54bc598ef1 75 */
acarter2 111:de54bc598ef1 76 osStatus put(T *mptr) {
acarter2 111:de54bc598ef1 77 return osMailPut(_mail_id, (void*)mptr);
acarter2 111:de54bc598ef1 78 }
acarter2 111:de54bc598ef1 79
acarter2 111:de54bc598ef1 80 /** Get a mail from a queue.
acarter2 111:de54bc598ef1 81 @param millisec timeout value or 0 in case of no time-out. (default: osWaitForever).
acarter2 111:de54bc598ef1 82 @return event that contains mail information or error code.
acarter2 111:de54bc598ef1 83 */
acarter2 111:de54bc598ef1 84 osEvent get(uint32_t millisec=osWaitForever) {
acarter2 111:de54bc598ef1 85 return osMailGet(_mail_id, millisec);
acarter2 111:de54bc598ef1 86 }
acarter2 111:de54bc598ef1 87
acarter2 111:de54bc598ef1 88 /** Free a memory block from a mail.
acarter2 111:de54bc598ef1 89 @param mptr pointer to the memory block that was obtained with Mail::get.
acarter2 111:de54bc598ef1 90 @return status code that indicates the execution status of the function.
acarter2 111:de54bc598ef1 91 */
acarter2 111:de54bc598ef1 92 osStatus free(T *mptr) {
acarter2 111:de54bc598ef1 93 return osMailFree(_mail_id, (void*)mptr);
acarter2 111:de54bc598ef1 94 }
acarter2 111:de54bc598ef1 95
acarter2 111:de54bc598ef1 96 private:
acarter2 111:de54bc598ef1 97 osMailQId _mail_id;
acarter2 111:de54bc598ef1 98 osMailQDef_t _mail_def;
acarter2 111:de54bc598ef1 99 #ifdef CMSIS_OS_RTX
acarter2 111:de54bc598ef1 100 uint32_t _mail_q[4+(queue_sz)];
acarter2 111:de54bc598ef1 101 uint32_t _mail_m[3+((sizeof(T)+3)/4)*(queue_sz)];
acarter2 111:de54bc598ef1 102 void *_mail_p[2];
acarter2 111:de54bc598ef1 103 #endif
acarter2 111:de54bc598ef1 104 };
acarter2 111:de54bc598ef1 105
acarter2 111:de54bc598ef1 106 }
acarter2 111:de54bc598ef1 107
acarter2 111:de54bc598ef1 108 #endif
acarter2 111:de54bc598ef1 109