diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..4a67f19 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,5 @@ +{ + "files.associations": { + "*.tpp": "cpp" + } +} \ No newline at end of file diff --git a/include/sta/rtos/mempool.hpp b/include/sta/rtos/mempool.hpp new file mode 100644 index 0000000..1baa1e0 --- /dev/null +++ b/include/sta/rtos/mempool.hpp @@ -0,0 +1,63 @@ +/** + * @file + * @brief RTOS MemPool implementation. + */ +#ifndef STA_RTOS_MEMPOOL_HPP +#define STA_RTOS_MEMPOOL_HPP + +#include + +#include + + +namespace sta +{ + /** + * @brief Interface object for using CMSIS RTOS2 MemPool. + * + * @tparam Message type + * + * @ingroup STA_RTOS_API + */ + template + class RtosMemPool + { + public: + using Message = T; /**< Queue message type */ + + public: + // Default Constructor + RtosMemPool(); + + /** + * @brief Create a memory pool. + * + * @param block_count names the amount of blocks. + * @param block_size names the size of each block. + */ + RtosMemPool(uint32_t block_count, uint32_t block_size); + + /** + * @brief Allocate a new Memory Block. + * + * @param timeout names the timeout in milliseconds. + */ + void * alloc(uint32_t timeout); + + /** + * @brief Free a Memory Block. + * + * @param block names the block to free. + */ + void free(void *block); + + private: + osMemoryPoolId_t handle_; /**< CMSIS RTOS2 queue handle */ + }; +} // namespace sta + + +#include + + +#endif // STA_RTOS_MEMPOOL_HPP diff --git a/include/sta/rtos/mempool.tpp b/include/sta/rtos/mempool.tpp new file mode 100644 index 0000000..fe13a7d --- /dev/null +++ b/include/sta/rtos/mempool.tpp @@ -0,0 +1,43 @@ +#ifndef STA_RTOS_MEMPOOL_TPP +#define STA_RTOS_MEMPOOL_TPP + +#ifndef STA_RTOS_MEMPOOL_HPP +# error "Internal header. Use instead." +#endif // !STA_RTOS_MEMPOOL_HPP + +#include + + +namespace sta +{ + template + RtosMemPool::RtosMemPool() + { + } + + template + RtosMemPool::RtosMemPool(uint32_t block_count, uint32_t block_size) + { + osMemoryPoolAttr_t mp_attr = { .name = "MemoryPool" }; + handle_ = osMemoryPoolNew(block_size, block_count, &mp_attr); + STA_ASSERT_MSG(handle_ != nullptr, "Failed to Create RtosMemPool"); + } + + template + void * RtosMemPool::alloc(uint32_t timeout /* = osWaitForever */) + { + void * addr = osMemoryPoolAlloc(handle_, timeout); + STA_ASSERT_MSG(addr != nullptr, "Failed to Alloc RtosMemPool Block"); + return addr; + } + + template + void RtosMemPool::free(void * block) + { + STA_ASSERT_MSG(osMemoryPoolFree(handle_, block) == osOK, + "Failed to Free RtosMemPool Block"); + } +} // namespace sta + + +#endif // STA_RTOS_MEMPOOL_TPP diff --git a/include/sta/rtos/queue.hpp b/include/sta/rtos/queue.hpp index e2b809a..f731c91 100644 --- a/include/sta/rtos/queue.hpp +++ b/include/sta/rtos/queue.hpp @@ -26,10 +26,13 @@ namespace sta using Message = T; /**< Queue message type */ public: + // Default Constructor + RtosQueue(); + /** * @param handle CMSIS RTOS2 queue handle */ - RtosQueue(osMessageQueueId_t * handle); + RtosQueue(osMessageQueueId_t handle); /** * @brief Place message in queue. @@ -56,7 +59,7 @@ namespace sta uint32_t available() const; private: - osMessageQueueId_t * handle_; /**< CMSIS RTOS2 queue handle */ + osMessageQueueId_t handle_; /**< CMSIS RTOS2 queue handle */ }; } // namespace sta diff --git a/include/sta/rtos/queue.tpp b/include/sta/rtos/queue.tpp index 35d14fe..0ca5676 100644 --- a/include/sta/rtos/queue.tpp +++ b/include/sta/rtos/queue.tpp @@ -5,13 +5,18 @@ # error "Internal header. Use instead." #endif // !STA_RTOS_QUEUE_HPP -#include +#include namespace sta { template - RtosQueue::RtosQueue(osMessageQueueId_t * handle) + RtosQueue::RtosQueue() + { + } + + template + RtosQueue::RtosQueue(osMessageQueueId_t handle) : handle_{handle} { STA_ASSERT(handle != nullptr); @@ -20,20 +25,20 @@ namespace sta template bool RtosQueue::put(const Message & msg, uint32_t timeout /* = osWaitForever */) { - return (osOK == osMessageQueuePut(*handle_, &msg, 0, timeout)); + return (osOK == osMessageQueuePut(handle_, &msg, 0, timeout)); } template bool RtosQueue::get(Message * outMsg, uint32_t timeout /* = osWaitForever */) { uint8_t prio; - return (osOK == osMessageQueueGet(*handle_, outMsg, &prio, timeout)); + return (osOK == osMessageQueueGet(handle_, outMsg, &prio, timeout)); } template - uint32 RtosQueue::available() const + uint32_t RtosQueue::available() const { - return osMessageQueueGetCount(*handle_); + return osMessageQueueGetCount(handle_); } } // namespace sta diff --git a/include/sta/rtos/sharedmem.hpp b/include/sta/rtos/sharedmem.hpp new file mode 100644 index 0000000..d39141d --- /dev/null +++ b/include/sta/rtos/sharedmem.hpp @@ -0,0 +1,69 @@ +/** + * @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 RtosSharedMem + { + public: + using Message = T; /**< message type */ + + public: + // Default Constructor + RtosSharedMem(); + + /** + * @brief Create a memory pool. And allocates exavlty one block. + * + * @param timeout names the timeout in milliseconds. + */ + RtosSharedMem(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..5460374 --- /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 + RtosSharedMem::RtosSharedMem() + { + } + + template + RtosSharedMem::RtosSharedMem(uint32_t timeout /* = osWaitForever */) + { + handle_ = osMemoryPoolNew(sizeof(T), 1, NULL); + STA_ASSERT_MSG(handle_ != nullptr, "Failed to Create RtosMemPool for sharedmem"); + + shared_mem_ = (T*) osMemoryPoolAlloc(handle_, timeout); + STA_ASSERT_MSG(shared_mem_ != nullptr, "Failed to Alloc RtosMemPool Block"); + } + + template + RtosSharedMem::~RtosSharedMem() + { + osMemoryPoolFree(handle_, shared_mem_); + osMemoryPoolDelete(handle_); + } + + template + void RtosSharedMem::write(T _message) + { + *shared_mem_ = _message; + } + + template + T RtosSharedMem::read() + { + return *shared_mem_; + } +} // namespace sta + + +#endif // STA_RTOS_SHAREDMEM_TPP diff --git a/include/sta/rtos/timer.hpp b/include/sta/rtos/timer.hpp index 2fe4bc0..2fc06bd 100644 --- a/include/sta/rtos/timer.hpp +++ b/include/sta/rtos/timer.hpp @@ -25,6 +25,7 @@ namespace sta void start(uint32_t millis) override; void stop() override; + bool isRunning() override; private: osTimerId_t timer_id_; /**< CMSIS RTOS2 Timer */ diff --git a/src/timer.cpp b/src/timer.cpp index 7e42d1c..180286a 100644 --- a/src/timer.cpp +++ b/src/timer.cpp @@ -6,12 +6,8 @@ namespace sta { RtosTimer::RtosTimer(){} RtosTimer::RtosTimer(void (*callback)(void *arg), void *arg) { - timer_attr_.name = "Timer"; - timer_attr_.attr_bits = osTimerOnce; - timer_attr_.cb_size = sizeof(osTimerAttr_t); - - timer_id_ = osTimerNew(callback, osTimerOnce, arg, &timer_attr_); - STA_ASSERT_MSG(timer_id_ != 0, "Failed to initialize timer"); + timer_id_ = osTimerNew(callback, osTimerOnce, arg, NULL); + STA_ASSERT_MSG(timer_id_ != NULL, "Failed to initialize timer"); } RtosTimer::~RtosTimer() { @@ -29,4 +25,8 @@ namespace sta { if (status != osOK) STA_DEBUG_PRINTLN("Timer stop failed"); } + + bool RtosTimer::isRunning() { + return osTimerIsRunning(timer_id_); + } } // namespace sta