NRF com

Dependencies:   mbed nRF24L01P

Committer:
vmihalcut
Date:
Mon May 27 06:06:31 2013 +0000
Revision:
0:fdfe93cb9255
NRF24L01 receiver

Who changed what in which revision?

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