Merge pull request 'ipc-additions' (#8) from ipc-additions into main

Reviewed-on: https://git.intern.spaceteamaachen.de/ALPAKA/rtos2-utils/pulls/8
This commit is contained in:
dario
2023-09-27 10:14:53 +00:00
9 changed files with 252 additions and 14 deletions

5
.vscode/settings.json vendored Normal file
View File

@@ -0,0 +1,5 @@
{
"files.associations": {
"*.tpp": "cpp"
}
}

View File

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

View File

@@ -0,0 +1,43 @@
#ifndef STA_RTOS_MEMPOOL_TPP
#define STA_RTOS_MEMPOOL_TPP
#ifndef STA_RTOS_MEMPOOL_HPP
# error "Internal header. Use <sta/rtos/mempool.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_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 <typename T>
void * RtosMemPool<T>::alloc(uint32_t timeout /* = osWaitForever */)
{
void * addr = osMemoryPoolAlloc(handle_, timeout);
STA_ASSERT_MSG(addr != nullptr, "Failed to Alloc RtosMemPool Block");
return addr;
}
template <typename T>
void RtosMemPool<T>::free(void * block)
{
STA_ASSERT_MSG(osMemoryPoolFree(handle_, block) == osOK,
"Failed to Free RtosMemPool Block");
}
} // namespace sta
#endif // STA_RTOS_MEMPOOL_TPP

View File

@@ -26,10 +26,13 @@ namespace sta
using Message = T; /**< Queue message type */ using Message = T; /**< Queue message type */
public: public:
// Default Constructor
RtosQueue();
/** /**
* @param handle CMSIS RTOS2 queue handle * @param handle CMSIS RTOS2 queue handle
*/ */
RtosQueue(osMessageQueueId_t * handle); RtosQueue(osMessageQueueId_t handle);
/** /**
* @brief Place message in queue. * @brief Place message in queue.
@@ -56,7 +59,7 @@ namespace sta
uint32_t available() const; uint32_t available() const;
private: private:
osMessageQueueId_t * handle_; /**< CMSIS RTOS2 queue handle */ osMessageQueueId_t handle_; /**< CMSIS RTOS2 queue handle */
}; };
} // namespace sta } // namespace sta

View File

@@ -5,13 +5,18 @@
# error "Internal header. Use <sta/rtos/queue.hpp> instead." # error "Internal header. Use <sta/rtos/queue.hpp> instead."
#endif // !STA_RTOS_QUEUE_HPP #endif // !STA_RTOS_QUEUE_HPP
#include <sta/assert.hpp> #include <sta/debug/assert.hpp>
namespace sta namespace sta
{ {
template <typename T> template <typename T>
RtosQueue<T>::RtosQueue(osMessageQueueId_t * handle) RtosQueue<T>::RtosQueue()
{
}
template <typename T>
RtosQueue<T>::RtosQueue(osMessageQueueId_t handle)
: handle_{handle} : handle_{handle}
{ {
STA_ASSERT(handle != nullptr); STA_ASSERT(handle != nullptr);
@@ -20,20 +25,20 @@ namespace sta
template <typename T> template <typename T>
bool RtosQueue<T>::put(const Message & msg, uint32_t timeout /* = osWaitForever */) bool RtosQueue<T>::put(const Message & msg, uint32_t timeout /* = osWaitForever */)
{ {
return (osOK == osMessageQueuePut(*handle_, &msg, 0, timeout)); return (osOK == osMessageQueuePut(handle_, &msg, 0, timeout));
} }
template <typename T> template <typename T>
bool RtosQueue<T>::get(Message * outMsg, uint32_t timeout /* = osWaitForever */) bool RtosQueue<T>::get(Message * outMsg, uint32_t timeout /* = osWaitForever */)
{ {
uint8_t prio; uint8_t prio;
return (osOK == osMessageQueueGet(*handle_, outMsg, &prio, timeout)); return (osOK == osMessageQueueGet(handle_, outMsg, &prio, timeout));
} }
template <typename T> template <typename T>
uint32 RtosQueue<T>::available() const uint32_t RtosQueue<T>::available() const
{ {
return osMessageQueueGetCount(*handle_); return osMessageQueueGetCount(handle_);
} }
} // namespace sta } // namespace sta

View File

@@ -0,0 +1,69 @@
/**
* @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 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 <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>
RtosSharedMem<T>::RtosSharedMem()
{
}
template <typename T>
RtosSharedMem<T>::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 <typename T>
RtosSharedMem<T>::~RtosSharedMem()
{
osMemoryPoolFree(handle_, shared_mem_);
osMemoryPoolDelete(handle_);
}
template <typename T>
void RtosSharedMem<T>::write(T _message)
{
*shared_mem_ = _message;
}
template <typename T>
T RtosSharedMem<T>::read()
{
return *shared_mem_;
}
} // namespace sta
#endif // STA_RTOS_SHAREDMEM_TPP

View File

@@ -25,6 +25,7 @@ namespace sta
void start(uint32_t millis) override; void start(uint32_t millis) override;
void stop() override; void stop() override;
bool isRunning() override;
private: private:
osTimerId_t timer_id_; /**< CMSIS RTOS2 Timer */ osTimerId_t timer_id_; /**< CMSIS RTOS2 Timer */

View File

@@ -6,12 +6,8 @@ namespace sta {
RtosTimer::RtosTimer(){} RtosTimer::RtosTimer(){}
RtosTimer::RtosTimer(void (*callback)(void *arg), void *arg) { RtosTimer::RtosTimer(void (*callback)(void *arg), void *arg) {
timer_attr_.name = "Timer"; timer_id_ = osTimerNew(callback, osTimerOnce, arg, NULL);
timer_attr_.attr_bits = osTimerOnce; STA_ASSERT_MSG(timer_id_ != NULL, "Failed to initialize timer");
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");
} }
RtosTimer::~RtosTimer() { RtosTimer::~RtosTimer() {
@@ -29,4 +25,8 @@ namespace sta {
if (status != osOK) STA_DEBUG_PRINTLN("Timer stop failed"); if (status != osOK) STA_DEBUG_PRINTLN("Timer stop failed");
} }
bool RtosTimer::isRunning() {
return osTimerIsRunning(timer_id_);
}
} // namespace sta } // namespace sta