mbed-os for GR-LYCHEE

Dependents:   mbed-os-example-blinky-gr-lychee GR-Boads_Camera_sample GR-Boards_Audio_Recoder GR-Boads_Camera_DisplayApp ... more

Committer:
dkato
Date:
Fri Feb 02 05:42:23 2018 +0000
Revision:
0:f782d9c66c49
mbed-os for GR-LYCHEE

Who changed what in which revision?

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