diff --git a/include/sta/rtos/sharedmem.hpp b/include/sta/rtos/sharedmem.hpp new file mode 100644 index 0000000..be11a40 --- /dev/null +++ b/include/sta/rtos/sharedmem.hpp @@ -0,0 +1,70 @@ +/** + * @file + * @brief RTOS MemPool implementation for one variable. + */ +#ifndef STA_RTOS_SHAREDMEM_HPP +#define STA_RTOS_SHAREDMEM_HPP + +#include + +#include + + +namespace sta +{ + /** + * @brief Interface object for using CMSIS RTOS2 MemPool for one variable. + * + * @tparam Message type + * + * @ingroup STA_RTOS_API + */ + template + class RtosMemPool + { + public: + using Message = T; /**< message type */ + + public: + // Default Constructor + RtosSharedMem(); + + /** + * @brief Create a memory pool. And allocates exavlty one block. + * + * @param block_size names the size of each block. + * @param timeout names the timeout in milliseconds. + */ + RtosSharedMem(uint32_t block_size, uint32_t timeout); + + /** + * @brief Destroy the Rtos Mem Pool object and frees the memory. + * + */ + ~RtosSharedMem(); + + /** + * @brief Allocate a new Memory Block. + * + * @param _message names the message to write. + */ + void write(T _message); + + /** + * @brief Gets the value of the shared memory. + * + * @return T Value of the shared memory. + */ + T read(); + + private: + osMemoryPoolId_t handle_; /**< CMSIS RTOS2 queue handle */ + T * shared_mem_; /**< Pointer to the shared memory */ + }; +} // namespace sta + + +#include + + +#endif // STA_RTOS_SHAREDMEM_HPP diff --git a/include/sta/rtos/sharedmem.tpp b/include/sta/rtos/sharedmem.tpp new file mode 100644 index 0000000..233bc99 --- /dev/null +++ b/include/sta/rtos/sharedmem.tpp @@ -0,0 +1,49 @@ +#ifndef STA_RTOS_SHAREDMEM_TPP +#define STA_RTOS_SHAREDMEM_TPP + +#ifndef STA_RTOS_SHAREDMEM_HPP +# error "Internal header. Use instead." +#endif // !STA_RTOS_MEMPOOL_HPP + +#include + + +namespace sta +{ + template + RtosMemPool::RtosMemPool() + { + } + + template + RtosMemPool::RtosMemPool(uint32_t block_size, uint32_t timeout /* = osWaitForever */) + { + handle_ = osMemoryPoolNew(block_size, block_count, NULL); + STA_ASSERT_MSG(handle_ != nullptr, "Failed to Create RtosMemPool for sharedmem"); + + shared_mem_ = osMemoryPoolAlloc(handle_, timeout) + STA_ASSERT_MSG(shared_mem_ != nullptr, "Failed to Alloc RtosMemPool Block"); + } + + template + RtosMemPool::~RtosSharedMem() + { + osMemoryPoolFree(handle_, shared_mem_); + osMemoryPoolDelete(handle_); + } + + template + void RtosMemPool::write(T _message) + { + *shared_mem_ = _message; + } + + template + T RtosMemPool::read(void * block) + { + return *shared_mem_; + } +} // namespace sta + + +#endif // STA_RTOS_SHAREDMEM_TPP