This is a fork due to permission issues

Dependencies:   mbed Socket lwip-eth lwip-sys lwip

Fork of 6_songs-from-the-cloud by MakingMusicWorkshop

Committer:
maclobdell
Date:
Wed May 18 19:06:32 2016 +0000
Revision:
0:f7c60d3e7b8a
clean version

Who changed what in which revision?

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