Final 350 project

Dependencies:   uzair Camera_LS_Y201 F7_Ethernet LCD_DISCO_F746NG NetworkAPI SDFileSystem mbed

Committer:
shoaib_ahmed
Date:
Mon Jul 31 09:16:35 2017 +0000
Revision:
0:791a779d6220
final project;

Who changed what in which revision?

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