Committer:
donatien
Date:
Thu May 31 15:46:30 2012 +0000
Revision:
0:e6ccf0b3d718

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
donatien 0:e6ccf0b3d718 1 /* Copyright (c) 2012 mbed.org */
donatien 0:e6ccf0b3d718 2 #ifndef MAIL_H
donatien 0:e6ccf0b3d718 3 #define MAIL_H
donatien 0:e6ccf0b3d718 4
donatien 0:e6ccf0b3d718 5 #include <stdint.h>
donatien 0:e6ccf0b3d718 6 #include <string.h>
donatien 0:e6ccf0b3d718 7
donatien 0:e6ccf0b3d718 8 #include "cmsis_os.h"
donatien 0:e6ccf0b3d718 9
donatien 0:e6ccf0b3d718 10 namespace rtos {
donatien 0:e6ccf0b3d718 11
donatien 0:e6ccf0b3d718 12 /*! The Mail class allow to control, send, receive, or wait for mail.
donatien 0:e6ccf0b3d718 13 A mail is a memory block that is send to a thread or interrupt service routine.
donatien 0:e6ccf0b3d718 14 \tparam T data type of a single message element.
donatien 0:e6ccf0b3d718 15 \tparam queue_sz maximum number of messages in queue.
donatien 0:e6ccf0b3d718 16 */
donatien 0:e6ccf0b3d718 17 template<typename T, uint32_t queue_sz>
donatien 0:e6ccf0b3d718 18 class Mail {
donatien 0:e6ccf0b3d718 19 public:
donatien 0:e6ccf0b3d718 20 /*! Create and Initialise Mail queue. */
donatien 0:e6ccf0b3d718 21 Mail() {
donatien 0:e6ccf0b3d718 22 #ifdef CMSIS_OS_RTX
donatien 0:e6ccf0b3d718 23 memset(_mail_q, 0, sizeof(_mail_q));
donatien 0:e6ccf0b3d718 24 _mail_p[0] = _mail_q;
donatien 0:e6ccf0b3d718 25
donatien 0:e6ccf0b3d718 26 memset(_mail_m, 0, sizeof(_mail_m));
donatien 0:e6ccf0b3d718 27 _mail_p[1] = _mail_m;
donatien 0:e6ccf0b3d718 28
donatien 0:e6ccf0b3d718 29 _mail_def.pool = _mail_p;
donatien 0:e6ccf0b3d718 30 _mail_def.queue_sz = queue_sz;
donatien 0:e6ccf0b3d718 31 _mail_def.item_sz = sizeof(T);
donatien 0:e6ccf0b3d718 32 #endif
donatien 0:e6ccf0b3d718 33 _mail_id = osMailCreate(&_mail_def, NULL);
donatien 0:e6ccf0b3d718 34 }
donatien 0:e6ccf0b3d718 35
donatien 0:e6ccf0b3d718 36 /*! Allocate a memory block of type T
donatien 0:e6ccf0b3d718 37 \param millisec timeout value or 0 in case of no time-out. (default: 0).
donatien 0:e6ccf0b3d718 38 \return pointer to memory block that can be filled with mail or NULL in case error.
donatien 0:e6ccf0b3d718 39 */
donatien 0:e6ccf0b3d718 40 T* alloc(uint32_t millisec=0) {
donatien 0:e6ccf0b3d718 41 return (T*)osMailAlloc(_mail_id, millisec);
donatien 0:e6ccf0b3d718 42 }
donatien 0:e6ccf0b3d718 43
donatien 0:e6ccf0b3d718 44 /*! Allocate a memory block of type T and set memory block to zero.
donatien 0:e6ccf0b3d718 45 \param millisec timeout value or 0 in case of no time-out. (default: 0).
donatien 0:e6ccf0b3d718 46 \return pointer to memory block that can be filled with mail or NULL in case error.
donatien 0:e6ccf0b3d718 47 */
donatien 0:e6ccf0b3d718 48 T* calloc(uint32_t millisec=0) {
donatien 0:e6ccf0b3d718 49 return (T*)osMailCAlloc(_mail_id, millisec);
donatien 0:e6ccf0b3d718 50 }
donatien 0:e6ccf0b3d718 51
donatien 0:e6ccf0b3d718 52 /*! Put a mail in the queue.
donatien 0:e6ccf0b3d718 53 \param mptr memory block previously allocated with Mail::alloc or Mail::calloc.
donatien 0:e6ccf0b3d718 54 \return status code that indicates the execution status of the function.
donatien 0:e6ccf0b3d718 55 */
donatien 0:e6ccf0b3d718 56 osStatus put(T *mptr) {
donatien 0:e6ccf0b3d718 57 return osMailPut(_mail_id, (void*)mptr);
donatien 0:e6ccf0b3d718 58 }
donatien 0:e6ccf0b3d718 59
donatien 0:e6ccf0b3d718 60 /*! Get a mail from a queue.
donatien 0:e6ccf0b3d718 61 \param millisec timeout value or 0 in case of no time-out. (default: osWaitForever).
donatien 0:e6ccf0b3d718 62 \return event that contains mail information or error code.
donatien 0:e6ccf0b3d718 63 */
donatien 0:e6ccf0b3d718 64 osEvent get(uint32_t millisec=osWaitForever) {
donatien 0:e6ccf0b3d718 65 return osMailGet(_mail_id, millisec);
donatien 0:e6ccf0b3d718 66 }
donatien 0:e6ccf0b3d718 67
donatien 0:e6ccf0b3d718 68 /*! Free a memory block from a mail.
donatien 0:e6ccf0b3d718 69 \param mptr pointer to the memory block that was obtained with Mail::get.
donatien 0:e6ccf0b3d718 70 \return status code that indicates the execution status of the function.
donatien 0:e6ccf0b3d718 71 */
donatien 0:e6ccf0b3d718 72 osStatus free(T *mptr) {
donatien 0:e6ccf0b3d718 73 return osMailFree(_mail_id, (void*)mptr);
donatien 0:e6ccf0b3d718 74 }
donatien 0:e6ccf0b3d718 75
donatien 0:e6ccf0b3d718 76 private:
donatien 0:e6ccf0b3d718 77 osMailQId _mail_id;
donatien 0:e6ccf0b3d718 78 osMailQDef_t _mail_def;
donatien 0:e6ccf0b3d718 79 #ifdef CMSIS_OS_RTX
donatien 0:e6ccf0b3d718 80 uint32_t _mail_q[4+(queue_sz)];
donatien 0:e6ccf0b3d718 81 uint32_t _mail_m[3+((sizeof(T)+3)/4)*(queue_sz)];
donatien 0:e6ccf0b3d718 82 void *_mail_p[2];
donatien 0:e6ccf0b3d718 83 #endif
donatien 0:e6ccf0b3d718 84 };
donatien 0:e6ccf0b3d718 85
donatien 0:e6ccf0b3d718 86 }
donatien 0:e6ccf0b3d718 87
donatien 0:e6ccf0b3d718 88 #endif
donatien 0:e6ccf0b3d718 89