skeleton for lab1

Dependencies:   AvailableMemory mbed-rtos mbed

Fork of helloaabbc by 32314 mbed

Committer:
y7jin
Date:
Thu Apr 03 22:56:32 2014 +0000
Revision:
0:1c8f2727e9f5
hello

Who changed what in which revision?

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