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