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