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