Erste version der Software für der Prototyp

Committer:
borlanic
Date:
Fri Mar 30 14:07:05 2018 +0000
Revision:
4:75df35ef4fb6
Parent:
0:380207fcb5c1
commentar

Who changed what in which revision?

UserRevisionLine numberNew contents of line
borlanic 0:380207fcb5c1 1 /* mbed Microcontroller Library
borlanic 0:380207fcb5c1 2 * Copyright (c) 2006-2012 ARM Limited
borlanic 0:380207fcb5c1 3 *
borlanic 0:380207fcb5c1 4 * Permission is hereby granted, free of charge, to any person obtaining a copy
borlanic 0:380207fcb5c1 5 * of this software and associated documentation files (the "Software"), to deal
borlanic 0:380207fcb5c1 6 * in the Software without restriction, including without limitation the rights
borlanic 0:380207fcb5c1 7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
borlanic 0:380207fcb5c1 8 * copies of the Software, and to permit persons to whom the Software is
borlanic 0:380207fcb5c1 9 * furnished to do so, subject to the following conditions:
borlanic 0:380207fcb5c1 10 *
borlanic 0:380207fcb5c1 11 * The above copyright notice and this permission notice shall be included in
borlanic 0:380207fcb5c1 12 * all copies or substantial portions of the Software.
borlanic 0:380207fcb5c1 13 *
borlanic 0:380207fcb5c1 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
borlanic 0:380207fcb5c1 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
borlanic 0:380207fcb5c1 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
borlanic 0:380207fcb5c1 17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
borlanic 0:380207fcb5c1 18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
borlanic 0:380207fcb5c1 19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
borlanic 0:380207fcb5c1 20 * SOFTWARE.
borlanic 0:380207fcb5c1 21 */
borlanic 0:380207fcb5c1 22 #ifndef MEMORYPOOL_H
borlanic 0:380207fcb5c1 23 #define MEMORYPOOL_H
borlanic 0:380207fcb5c1 24
borlanic 0:380207fcb5c1 25 #include <stdint.h>
borlanic 0:380207fcb5c1 26 #include <string.h>
borlanic 0:380207fcb5c1 27
borlanic 0:380207fcb5c1 28 #include "cmsis_os2.h"
borlanic 0:380207fcb5c1 29 #include "mbed_rtos1_types.h"
borlanic 0:380207fcb5c1 30 #include "mbed_rtos_storage.h"
borlanic 0:380207fcb5c1 31 #include "platform/NonCopyable.h"
borlanic 0:380207fcb5c1 32
borlanic 0:380207fcb5c1 33 namespace rtos {
borlanic 0:380207fcb5c1 34 /** \addtogroup rtos */
borlanic 0:380207fcb5c1 35 /** @{*/
borlanic 0:380207fcb5c1 36 /**
borlanic 0:380207fcb5c1 37 * \defgroup rtos_MemoryPool MemoryPool class
borlanic 0:380207fcb5c1 38 * @{
borlanic 0:380207fcb5c1 39 */
borlanic 0:380207fcb5c1 40
borlanic 0:380207fcb5c1 41 /** Define and manage fixed-size memory pools of objects of a given type.
borlanic 0:380207fcb5c1 42 @tparam T data type of a single object (element).
borlanic 0:380207fcb5c1 43 @tparam queue_sz maximum number of objects (elements) in the memory pool.
borlanic 0:380207fcb5c1 44
borlanic 0:380207fcb5c1 45 @note
borlanic 0:380207fcb5c1 46 Memory considerations: The memory pool data store and control structures will be created on current thread's stack,
borlanic 0:380207fcb5c1 47 both for the mbed OS and underlying RTOS objects (static or dynamic RTOS memory pools are not being used).
borlanic 0:380207fcb5c1 48 */
borlanic 0:380207fcb5c1 49 template<typename T, uint32_t pool_sz>
borlanic 0:380207fcb5c1 50 class MemoryPool : private mbed::NonCopyable<MemoryPool<T, pool_sz> > {
borlanic 0:380207fcb5c1 51 MBED_STATIC_ASSERT(pool_sz > 0, "Invalid memory pool size. Must be greater than 0.");
borlanic 0:380207fcb5c1 52 public:
borlanic 0:380207fcb5c1 53 /** Create and Initialize a memory pool.
borlanic 0:380207fcb5c1 54 *
borlanic 0:380207fcb5c1 55 * @note You cannot call this function from ISR context.
borlanic 0:380207fcb5c1 56 */
borlanic 0:380207fcb5c1 57 MemoryPool() {
borlanic 0:380207fcb5c1 58 memset(_pool_mem, 0, sizeof(_pool_mem));
borlanic 0:380207fcb5c1 59 memset(&_obj_mem, 0, sizeof(_obj_mem));
borlanic 0:380207fcb5c1 60 osMemoryPoolAttr_t attr = { 0 };
borlanic 0:380207fcb5c1 61 attr.mp_mem = _pool_mem;
borlanic 0:380207fcb5c1 62 attr.mp_size = sizeof(_pool_mem);
borlanic 0:380207fcb5c1 63 attr.cb_mem = &_obj_mem;
borlanic 0:380207fcb5c1 64 attr.cb_size = sizeof(_obj_mem);
borlanic 0:380207fcb5c1 65 _id = osMemoryPoolNew(pool_sz, sizeof(T), &attr);
borlanic 0:380207fcb5c1 66 MBED_ASSERT(_id);
borlanic 0:380207fcb5c1 67 }
borlanic 0:380207fcb5c1 68
borlanic 0:380207fcb5c1 69 /** Destroy a memory pool
borlanic 0:380207fcb5c1 70 *
borlanic 0:380207fcb5c1 71 * @note You cannot call this function from ISR context.
borlanic 0:380207fcb5c1 72 */
borlanic 0:380207fcb5c1 73 ~MemoryPool() {
borlanic 0:380207fcb5c1 74 osMemoryPoolDelete(_id);
borlanic 0:380207fcb5c1 75 }
borlanic 0:380207fcb5c1 76
borlanic 0:380207fcb5c1 77 /** Allocate a memory block of type T from a memory pool.
borlanic 0:380207fcb5c1 78 @return address of the allocated memory block or NULL in case of no memory available.
borlanic 0:380207fcb5c1 79
borlanic 0:380207fcb5c1 80 @note You may call this function from ISR context.
borlanic 0:380207fcb5c1 81 */
borlanic 0:380207fcb5c1 82 T* alloc(void) {
borlanic 0:380207fcb5c1 83 return (T*)osMemoryPoolAlloc(_id, 0);
borlanic 0:380207fcb5c1 84 }
borlanic 0:380207fcb5c1 85
borlanic 0:380207fcb5c1 86 /** Allocate a memory block of type T from a memory pool and set memory block to zero.
borlanic 0:380207fcb5c1 87 @return address of the allocated memory block or NULL in case of no memory available.
borlanic 0:380207fcb5c1 88
borlanic 0:380207fcb5c1 89 @note You may call this function from ISR context.
borlanic 0:380207fcb5c1 90 */
borlanic 0:380207fcb5c1 91 T* calloc(void) {
borlanic 0:380207fcb5c1 92 T *item = (T*)osMemoryPoolAlloc(_id, 0);
borlanic 0:380207fcb5c1 93 if (item != NULL) {
borlanic 0:380207fcb5c1 94 memset(item, 0, sizeof(T));
borlanic 0:380207fcb5c1 95 }
borlanic 0:380207fcb5c1 96 return item;
borlanic 0:380207fcb5c1 97 }
borlanic 0:380207fcb5c1 98
borlanic 0:380207fcb5c1 99 /** Free a memory block.
borlanic 0:380207fcb5c1 100 @param block address of the allocated memory block to be freed.
borlanic 0:380207fcb5c1 101 @return osOK on successful deallocation, osErrorParameter if given memory block id
borlanic 0:380207fcb5c1 102 is NULL or invalid, or osErrorResource if given memory block is in an
borlanic 0:380207fcb5c1 103 invalid memory pool state.
borlanic 0:380207fcb5c1 104
borlanic 0:380207fcb5c1 105 @note You may call this function from ISR context.
borlanic 0:380207fcb5c1 106 */
borlanic 0:380207fcb5c1 107 osStatus free(T *block) {
borlanic 0:380207fcb5c1 108 return osMemoryPoolFree(_id, (void*)block);
borlanic 0:380207fcb5c1 109 }
borlanic 0:380207fcb5c1 110
borlanic 0:380207fcb5c1 111 private:
borlanic 0:380207fcb5c1 112 osMemoryPoolId_t _id;
borlanic 0:380207fcb5c1 113 /* osMemoryPoolNew requires that pool block size is a multiple of 4 bytes. */
borlanic 0:380207fcb5c1 114 char _pool_mem[((sizeof(T) + 3) & ~3) * pool_sz];
borlanic 0:380207fcb5c1 115 mbed_rtos_storage_mem_pool_t _obj_mem;
borlanic 0:380207fcb5c1 116 };
borlanic 0:380207fcb5c1 117 /** @}*/
borlanic 0:380207fcb5c1 118 /** @}*/
borlanic 0:380207fcb5c1 119
borlanic 0:380207fcb5c1 120 }
borlanic 0:380207fcb5c1 121 #endif
borlanic 0:380207fcb5c1 122
borlanic 0:380207fcb5c1 123