diff --git a/include/sta/rtos/timer.hpp b/include/sta/rtos/timer.hpp new file mode 100644 index 0000000..b576078 --- /dev/null +++ b/include/sta/rtos/timer.hpp @@ -0,0 +1,34 @@ +/** + * @file + * @brief RTOS timer implementation. + */ + +#ifndef STA_RTOS_TIMER_HPP +#define STA_RTOS_TIMER_HPP + +#include +#include + +namespace sta +{ + /** + * @brief Implementation of Timer using CMSIS RTOS2. + * + * @ingroup STA_RTOS_API + */ + class RtosTimer : public Timer + { + public: + RtosTimer(void (*callback)(void *arg), void *arg); + ~RtosTimer(); + + void start(uint32_t millis) override; + void stop() override; + + private: + osTimerId_t timer_id_; /**< CMSIS RTOS2 Timer */ + osTimerAttr_t timer_attr_; /**< CMSIS RTOS2 Timer attributes */ + }; +} // namespace sta + +#endif // STA_RTOS_TIMER_HPP \ No newline at end of file diff --git a/src/timer.cpp b/src/timer.cpp new file mode 100644 index 0000000..398cdd3 --- /dev/null +++ b/src/timer.cpp @@ -0,0 +1,28 @@ +#include +#include + +namespace sta { + 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_); + } + + RtosTimer::~RtosTimer() { + osTimerDelete(timer_id_); + } + + void RtosTimer::start(uint32_t millis) { + osStatus_t status = osTimerStart(timer_id_, millis); + + if (status != osOK) STA_DEBUG_PRINTLN("Timer start failed"); + } + + void RtosTimer::stop() { + osStatus_t status = osTimerStop(timer_id_); + + if (status != osOK) STA_DEBUG_PRINTLN("Timer stop failed"); + } +} // namespace sta \ No newline at end of file