From a38f678d41eccf9f32fcde2d6c16162d13e68e98 Mon Sep 17 00:00:00 2001 From: CarlWachter Date: Mon, 18 Sep 2023 20:45:09 +0200 Subject: [PATCH 1/7] fixes + timer isRunning --- include/sta/rtos/timer.hpp | 1 + src/timer.cpp | 12 ++++++------ 2 files changed, 7 insertions(+), 6 deletions(-) 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 From 327005a4b674fd25abe5133a80ad20563229437e Mon Sep 17 00:00:00 2001 From: CarlWachter Date: Fri, 22 Sep 2023 16:54:20 +0200 Subject: [PATCH 2/7] fixes + defualt constructor --- include/sta/rtos/queue.hpp | 7 +++++-- include/sta/rtos/queue.tpp | 17 +++++++++++------ 2 files changed, 16 insertions(+), 8 deletions(-) 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 From f813e7b6eacdde978bf77ed9a1bce7fb38ab4c1d Mon Sep 17 00:00:00 2001 From: CarlWachter Date: Fri, 22 Sep 2023 17:53:04 +0200 Subject: [PATCH 3/7] Added Mempool --- include/sta/rtos/mempool.hpp | 63 ++++++++++++++++++++++++++++++++++++ include/sta/rtos/mempool.tpp | 40 +++++++++++++++++++++++ 2 files changed, 103 insertions(+) create mode 100644 include/sta/rtos/mempool.hpp create mode 100644 include/sta/rtos/mempool.tpp diff --git a/include/sta/rtos/mempool.hpp b/include/sta/rtos/mempool.hpp new file mode 100644 index 0000000..e4656a0 --- /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. + */ + osStatus_t 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..bd3ab29 --- /dev/null +++ b/include/sta/rtos/mempool.tpp @@ -0,0 +1,40 @@ +#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 */) + { + return osMemoryPoolAlloc(handle_, timeout); + } + + template + osStatus_t RtosMemPool::free(void * block) + { + return osMemoryPoolFree(handle_, block); + } +} // namespace sta + + +#endif // STA_RTOS_MEMPOOL_TPP From 7c8cf2e1e2df6ad0f3f07e7948ed232121172866 Mon Sep 17 00:00:00 2001 From: CarlWachter Date: Sat, 23 Sep 2023 11:02:35 +0200 Subject: [PATCH 4/7] Asserts on free and alloc --- .vscode/settings.json | 5 +++++ include/sta/rtos/mempool.hpp | 2 +- include/sta/rtos/mempool.tpp | 9 ++++++--- 3 files changed, 12 insertions(+), 4 deletions(-) create mode 100644 .vscode/settings.json 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 index e4656a0..1baa1e0 100644 --- a/include/sta/rtos/mempool.hpp +++ b/include/sta/rtos/mempool.hpp @@ -49,7 +49,7 @@ namespace sta * * @param block names the block to free. */ - osStatus_t free(void *block); + void free(void *block); private: osMemoryPoolId_t handle_; /**< CMSIS RTOS2 queue handle */ diff --git a/include/sta/rtos/mempool.tpp b/include/sta/rtos/mempool.tpp index bd3ab29..5dbae60 100644 --- a/include/sta/rtos/mempool.tpp +++ b/include/sta/rtos/mempool.tpp @@ -26,13 +26,16 @@ namespace sta template void * RtosMemPool::alloc(uint32_t timeout /* = osWaitForever */) { - return osMemoryPoolAlloc(handle_, timeout); + void * addr = osMemoryPoolAlloc(handle_, timeout) + STA_ASSERT_MSG(addr != nullptr, "Failed to Alloc RtosMemPool Block"); + return addr; } template - osStatus_t RtosMemPool::free(void * block) + void RtosMemPool::free(void * block) { - return osMemoryPoolFree(handle_, block); + STA_ASSERT_MSG(osMemoryPoolFree(handle_, block) == osOK, + "Failed to Free RtosMemPool Block"); } } // namespace sta From 1bd528c5cb25b9787caa3031e7feee83e7b8a910 Mon Sep 17 00:00:00 2001 From: CarlWachter Date: Sat, 23 Sep 2023 11:14:22 +0200 Subject: [PATCH 5/7] Added SharedMem impl --- include/sta/rtos/sharedmem.hpp | 70 ++++++++++++++++++++++++++++++++++ include/sta/rtos/sharedmem.tpp | 49 ++++++++++++++++++++++++ 2 files changed, 119 insertions(+) create mode 100644 include/sta/rtos/sharedmem.hpp create mode 100644 include/sta/rtos/sharedmem.tpp 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 From 6f64a0616466c59cbafd2b2da70c7e12b3aa4eaa Mon Sep 17 00:00:00 2001 From: CarlWachter Date: Sat, 23 Sep 2023 11:20:53 +0200 Subject: [PATCH 6/7] Added mssing semicolon --- include/sta/rtos/mempool.tpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/sta/rtos/mempool.tpp b/include/sta/rtos/mempool.tpp index 5dbae60..fe13a7d 100644 --- a/include/sta/rtos/mempool.tpp +++ b/include/sta/rtos/mempool.tpp @@ -26,7 +26,7 @@ namespace sta template void * RtosMemPool::alloc(uint32_t timeout /* = osWaitForever */) { - void * addr = osMemoryPoolAlloc(handle_, timeout) + void * addr = osMemoryPoolAlloc(handle_, timeout); STA_ASSERT_MSG(addr != nullptr, "Failed to Alloc RtosMemPool Block"); return addr; } From 3e28326798e7c6681f47ec1cbe2393cfe2461128 Mon Sep 17 00:00:00 2001 From: CarlWachter Date: Sat, 23 Sep 2023 11:31:59 +0200 Subject: [PATCH 7/7] SharedMemFixes --- include/sta/rtos/sharedmem.hpp | 5 ++--- include/sta/rtos/sharedmem.tpp | 14 +++++++------- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/include/sta/rtos/sharedmem.hpp b/include/sta/rtos/sharedmem.hpp index be11a40..d39141d 100644 --- a/include/sta/rtos/sharedmem.hpp +++ b/include/sta/rtos/sharedmem.hpp @@ -20,7 +20,7 @@ namespace sta * @ingroup STA_RTOS_API */ template - class RtosMemPool + class RtosSharedMem { public: using Message = T; /**< message type */ @@ -32,10 +32,9 @@ namespace sta /** * @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); + RtosSharedMem(uint32_t timeout); /** * @brief Destroy the Rtos Mem Pool object and frees the memory. diff --git a/include/sta/rtos/sharedmem.tpp b/include/sta/rtos/sharedmem.tpp index 233bc99..5460374 100644 --- a/include/sta/rtos/sharedmem.tpp +++ b/include/sta/rtos/sharedmem.tpp @@ -11,35 +11,35 @@ namespace sta { template - RtosMemPool::RtosMemPool() + RtosSharedMem::RtosSharedMem() { } template - RtosMemPool::RtosMemPool(uint32_t block_size, uint32_t timeout /* = osWaitForever */) + RtosSharedMem::RtosSharedMem(uint32_t timeout /* = osWaitForever */) { - handle_ = osMemoryPoolNew(block_size, block_count, NULL); + handle_ = osMemoryPoolNew(sizeof(T), 1, NULL); STA_ASSERT_MSG(handle_ != nullptr, "Failed to Create RtosMemPool for sharedmem"); - shared_mem_ = osMemoryPoolAlloc(handle_, timeout) + shared_mem_ = (T*) osMemoryPoolAlloc(handle_, timeout); STA_ASSERT_MSG(shared_mem_ != nullptr, "Failed to Alloc RtosMemPool Block"); } template - RtosMemPool::~RtosSharedMem() + RtosSharedMem::~RtosSharedMem() { osMemoryPoolFree(handle_, shared_mem_); osMemoryPoolDelete(handle_); } template - void RtosMemPool::write(T _message) + void RtosSharedMem::write(T _message) { *shared_mem_ = _message; } template - T RtosMemPool::read(void * block) + T RtosSharedMem::read() { return *shared_mem_; }