Entrega 3er corte - sistemas embebidos

Committer:
Bethory
Date:
Wed May 30 00:01:50 2018 +0000
Revision:
0:6ad07c9019fd
Codigo de tales para todos los pasculaes

Who changed what in which revision?

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