Counter

Dependencies:   EthernetInterface NTPClient SDFileSystem TextLCD WebSocketClient mbed-rtos mbed Socket lwip-eth lwip-sys lwip FATFileSystem

Committer:
Tuxitheone
Date:
Mon Feb 29 18:59:15 2016 +0000
Revision:
0:ecaf3e593122
TankCounter

Who changed what in which revision?

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