Knight KE / Mbed OS Game_Master
Committer:
WFKnight
Date:
Thu Jun 21 13:51:43 2018 +0000
Revision:
0:9b3d4731edbb
UART, RTOS, LED

Who changed what in which revision?

UserRevisionLine numberNew contents of line
WFKnight 0:9b3d4731edbb 1 /* mbed Microcontroller Library
WFKnight 0:9b3d4731edbb 2 * Copyright (c) 2006-2017 ARM Limited
WFKnight 0:9b3d4731edbb 3 *
WFKnight 0:9b3d4731edbb 4 * Permission is hereby granted, free of charge, to any person obtaining a copy
WFKnight 0:9b3d4731edbb 5 * of this software and associated documentation files (the "Software"), to deal
WFKnight 0:9b3d4731edbb 6 * in the Software without restriction, including without limitation the rights
WFKnight 0:9b3d4731edbb 7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
WFKnight 0:9b3d4731edbb 8 * copies of the Software, and to permit persons to whom the Software is
WFKnight 0:9b3d4731edbb 9 * furnished to do so, subject to the following conditions:
WFKnight 0:9b3d4731edbb 10 *
WFKnight 0:9b3d4731edbb 11 * The above copyright notice and this permission notice shall be included in
WFKnight 0:9b3d4731edbb 12 * all copies or substantial portions of the Software.
WFKnight 0:9b3d4731edbb 13 *
WFKnight 0:9b3d4731edbb 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
WFKnight 0:9b3d4731edbb 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
WFKnight 0:9b3d4731edbb 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
WFKnight 0:9b3d4731edbb 17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
WFKnight 0:9b3d4731edbb 18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
WFKnight 0:9b3d4731edbb 19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
WFKnight 0:9b3d4731edbb 20 * SOFTWARE.
WFKnight 0:9b3d4731edbb 21 */
WFKnight 0:9b3d4731edbb 22 #ifndef MAIL_H
WFKnight 0:9b3d4731edbb 23 #define MAIL_H
WFKnight 0:9b3d4731edbb 24
WFKnight 0:9b3d4731edbb 25 #include <stdint.h>
WFKnight 0:9b3d4731edbb 26 #include <string.h>
WFKnight 0:9b3d4731edbb 27
WFKnight 0:9b3d4731edbb 28 #include "Queue.h"
WFKnight 0:9b3d4731edbb 29 #include "MemoryPool.h"
WFKnight 0:9b3d4731edbb 30 #include "cmsis_os2.h"
WFKnight 0:9b3d4731edbb 31 #include "mbed_rtos_storage.h"
WFKnight 0:9b3d4731edbb 32 #include "mbed_rtos1_types.h"
WFKnight 0:9b3d4731edbb 33
WFKnight 0:9b3d4731edbb 34 #include "platform/NonCopyable.h"
WFKnight 0:9b3d4731edbb 35
WFKnight 0:9b3d4731edbb 36 using namespace rtos;
WFKnight 0:9b3d4731edbb 37
WFKnight 0:9b3d4731edbb 38 namespace rtos {
WFKnight 0:9b3d4731edbb 39 /** \addtogroup rtos */
WFKnight 0:9b3d4731edbb 40 /** @{*/
WFKnight 0:9b3d4731edbb 41 /**
WFKnight 0:9b3d4731edbb 42 * \defgroup rtos_Mail Mail class
WFKnight 0:9b3d4731edbb 43 * @{
WFKnight 0:9b3d4731edbb 44 */
WFKnight 0:9b3d4731edbb 45
WFKnight 0:9b3d4731edbb 46 /** The Mail class allow to control, send, receive, or wait for mail.
WFKnight 0:9b3d4731edbb 47 A mail is a memory block that is send to a thread or interrupt service routine.
WFKnight 0:9b3d4731edbb 48 @tparam T data type of a single message element.
WFKnight 0:9b3d4731edbb 49 @tparam queue_sz maximum number of messages in queue.
WFKnight 0:9b3d4731edbb 50
WFKnight 0:9b3d4731edbb 51 @note
WFKnight 0:9b3d4731edbb 52 Memory considerations: The mail data store and control structures will be created on current thread's stack,
WFKnight 0:9b3d4731edbb 53 both for the mbed OS and underlying RTOS objects (static or dynamic RTOS memory pools are not being used).
WFKnight 0:9b3d4731edbb 54 */
WFKnight 0:9b3d4731edbb 55 template<typename T, uint32_t queue_sz>
WFKnight 0:9b3d4731edbb 56 class Mail : private mbed::NonCopyable<Mail<T, queue_sz> > {
WFKnight 0:9b3d4731edbb 57 public:
WFKnight 0:9b3d4731edbb 58 /** Create and Initialize Mail queue.
WFKnight 0:9b3d4731edbb 59 *
WFKnight 0:9b3d4731edbb 60 * @note You cannot call this function from ISR context.
WFKnight 0:9b3d4731edbb 61 */
WFKnight 0:9b3d4731edbb 62 Mail() { };
WFKnight 0:9b3d4731edbb 63
WFKnight 0:9b3d4731edbb 64 /** Check if the mail queue is empty
WFKnight 0:9b3d4731edbb 65 *
WFKnight 0:9b3d4731edbb 66 * @return True if the mail queue is empty, false if not
WFKnight 0:9b3d4731edbb 67 *
WFKnight 0:9b3d4731edbb 68 * @note You may call this function from ISR context.
WFKnight 0:9b3d4731edbb 69 */
WFKnight 0:9b3d4731edbb 70 bool empty() const {
WFKnight 0:9b3d4731edbb 71 return _queue.empty();
WFKnight 0:9b3d4731edbb 72 }
WFKnight 0:9b3d4731edbb 73
WFKnight 0:9b3d4731edbb 74 /** Check if the mail queue is full
WFKnight 0:9b3d4731edbb 75 *
WFKnight 0:9b3d4731edbb 76 * @return True if the mail queue is full, false if not
WFKnight 0:9b3d4731edbb 77 *
WFKnight 0:9b3d4731edbb 78 * @note You may call this function from ISR context.
WFKnight 0:9b3d4731edbb 79 */
WFKnight 0:9b3d4731edbb 80 bool full() const {
WFKnight 0:9b3d4731edbb 81 return _queue.full();
WFKnight 0:9b3d4731edbb 82 }
WFKnight 0:9b3d4731edbb 83
WFKnight 0:9b3d4731edbb 84 /** Allocate a memory block of type T
WFKnight 0:9b3d4731edbb 85 @param millisec timeout value or 0 in case of no time-out. (default: 0).
WFKnight 0:9b3d4731edbb 86 @return pointer to memory block that can be filled with mail or NULL in case error.
WFKnight 0:9b3d4731edbb 87
WFKnight 0:9b3d4731edbb 88 @note You may call this function from ISR context if the millisec parameter is set to 0.
WFKnight 0:9b3d4731edbb 89 */
WFKnight 0:9b3d4731edbb 90 T* alloc(uint32_t millisec=0) {
WFKnight 0:9b3d4731edbb 91 return _pool.alloc();
WFKnight 0:9b3d4731edbb 92 }
WFKnight 0:9b3d4731edbb 93
WFKnight 0:9b3d4731edbb 94 /** Allocate a memory block of type T and set memory block to zero.
WFKnight 0:9b3d4731edbb 95 @param millisec timeout value or 0 in case of no time-out. (default: 0).
WFKnight 0:9b3d4731edbb 96 @return pointer to memory block that can be filled with mail or NULL in case error.
WFKnight 0:9b3d4731edbb 97
WFKnight 0:9b3d4731edbb 98 @note You may call this function from ISR context if the millisec parameter is set to 0.
WFKnight 0:9b3d4731edbb 99 */
WFKnight 0:9b3d4731edbb 100 T* calloc(uint32_t millisec=0) {
WFKnight 0:9b3d4731edbb 101 return _pool.calloc();
WFKnight 0:9b3d4731edbb 102 }
WFKnight 0:9b3d4731edbb 103
WFKnight 0:9b3d4731edbb 104 /** Put a mail in the queue.
WFKnight 0:9b3d4731edbb 105 @param mptr memory block previously allocated with Mail::alloc or Mail::calloc.
WFKnight 0:9b3d4731edbb 106 @return status code that indicates the execution status of the function.
WFKnight 0:9b3d4731edbb 107
WFKnight 0:9b3d4731edbb 108 @note You may call this function from ISR context.
WFKnight 0:9b3d4731edbb 109 */
WFKnight 0:9b3d4731edbb 110 osStatus put(T *mptr) {
WFKnight 0:9b3d4731edbb 111 return _queue.put(mptr);
WFKnight 0:9b3d4731edbb 112 }
WFKnight 0:9b3d4731edbb 113
WFKnight 0:9b3d4731edbb 114 /** Get a mail from a queue.
WFKnight 0:9b3d4731edbb 115 @param millisec timeout value or 0 in case of no time-out. (default: osWaitForever).
WFKnight 0:9b3d4731edbb 116 @return event that contains mail information or error code.
WFKnight 0:9b3d4731edbb 117
WFKnight 0:9b3d4731edbb 118 @note You may call this function from ISR context if the millisec parameter is set to 0.
WFKnight 0:9b3d4731edbb 119 */
WFKnight 0:9b3d4731edbb 120 osEvent get(uint32_t millisec=osWaitForever) {
WFKnight 0:9b3d4731edbb 121 osEvent evt = _queue.get(millisec);
WFKnight 0:9b3d4731edbb 122 if (evt.status == osEventMessage) {
WFKnight 0:9b3d4731edbb 123 evt.status = osEventMail;
WFKnight 0:9b3d4731edbb 124 }
WFKnight 0:9b3d4731edbb 125 return evt;
WFKnight 0:9b3d4731edbb 126 }
WFKnight 0:9b3d4731edbb 127
WFKnight 0:9b3d4731edbb 128 /** Free a memory block from a mail.
WFKnight 0:9b3d4731edbb 129 @param mptr pointer to the memory block that was obtained with Mail::get.
WFKnight 0:9b3d4731edbb 130 @return status code that indicates the execution status of the function.
WFKnight 0:9b3d4731edbb 131
WFKnight 0:9b3d4731edbb 132 @note You may call this function from ISR context.
WFKnight 0:9b3d4731edbb 133 */
WFKnight 0:9b3d4731edbb 134 osStatus free(T *mptr) {
WFKnight 0:9b3d4731edbb 135 return _pool.free(mptr);
WFKnight 0:9b3d4731edbb 136 }
WFKnight 0:9b3d4731edbb 137
WFKnight 0:9b3d4731edbb 138 private:
WFKnight 0:9b3d4731edbb 139 Queue<T, queue_sz> _queue;
WFKnight 0:9b3d4731edbb 140 MemoryPool<T, queue_sz> _pool;
WFKnight 0:9b3d4731edbb 141 };
WFKnight 0:9b3d4731edbb 142
WFKnight 0:9b3d4731edbb 143 /** @}*/
WFKnight 0:9b3d4731edbb 144 /** @}*/
WFKnight 0:9b3d4731edbb 145
WFKnight 0:9b3d4731edbb 146 }
WFKnight 0:9b3d4731edbb 147
WFKnight 0:9b3d4731edbb 148 #endif
WFKnight 0:9b3d4731edbb 149
WFKnight 0:9b3d4731edbb 150
WFKnight 0:9b3d4731edbb 151