Added SharedMem impl

This commit is contained in:
CarlWachter 2023-09-23 11:14:22 +02:00
parent 7c8cf2e1e2
commit 1bd528c5cb
2 changed files with 119 additions and 0 deletions

View File

@ -0,0 +1,70 @@
/**
* @file
* @brief RTOS MemPool implementation for one variable.
*/
#ifndef STA_RTOS_SHAREDMEM_HPP
#define STA_RTOS_SHAREDMEM_HPP
#include <cmsis_os2.h>
#include <cstdint>
namespace sta
{
/**
* @brief Interface object for using CMSIS RTOS2 MemPool for one variable.
*
* @tparam Message type
*
* @ingroup STA_RTOS_API
*/
template <typename T>
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 <sta/rtos/sharedmem.tpp>
#endif // STA_RTOS_SHAREDMEM_HPP

View File

@ -0,0 +1,49 @@
#ifndef STA_RTOS_SHAREDMEM_TPP
#define STA_RTOS_SHAREDMEM_TPP
#ifndef STA_RTOS_SHAREDMEM_HPP
# error "Internal header. Use <sta/rtos/sharedmem.hpp> instead."
#endif // !STA_RTOS_MEMPOOL_HPP
#include <sta/debug/assert.hpp>
namespace sta
{
template <typename T>
RtosMemPool<T>::RtosMemPool()
{
}
template <typename T>
RtosMemPool<T>::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 <typename T>
RtosMemPool<T>::~RtosSharedMem()
{
osMemoryPoolFree(handle_, shared_mem_);
osMemoryPoolDelete(handle_);
}
template <typename T>
void RtosMemPool<T>::write(T _message)
{
*shared_mem_ = _message;
}
template <typename T>
T RtosMemPool<T>::read(void * block)
{
return *shared_mem_;
}
} // namespace sta
#endif // STA_RTOS_SHAREDMEM_TPP