feat(sync): added periodicDelay function to threads

This commit is contained in:
@CarlWachter 2024-06-16 10:44:24 +02:00 committed by carlwachter
parent e03f624c5b
commit 40727728d0
2 changed files with 34 additions and 1 deletions

View File

@ -128,6 +128,14 @@ namespace sta
*/
void sleep(uint32_t ticks);
/**
* @brief Sleep until next period. Sets itself to WAITING if the watchdog is enabled, preventing
* the watchdog from restarting this thread.
*
* @param frequency
*/
void periodicDelay(uint32_t frequency);
#ifdef STA_CAN_BUS_ENABLE
/**
* @brief Set the ID of the CAN bus this thread is associated with.
@ -216,6 +224,7 @@ namespace sta
uint32_t canID_;
#endif // STA_TACOS_WATCHDOG_ENABLED
bool terminate_;
uint32_t previous_tick_;
};
}
}

View File

@ -13,7 +13,11 @@
#include <functional>
#include <cstring>
#include <cmsis_os2.h>
// Include FreeRTOS configuration for the system tick rate.
typedef uint32_t TickType_t;
#include <FreeRTOSConfig.h>
namespace sta
{
@ -31,7 +35,8 @@ namespace sta
CAN_queue_{STA_RTOS_CAN_BUS_QUEUE_LENGTH},
canID_{0},
#endif // STA_CAN_BUS_ENABLE
terminate_{false}
terminate_{false},
previous_tick_{0}
{
STA_ASSERT(stack_size >= 0);
STA_ASSERT(cb_size >= 0);
@ -110,6 +115,25 @@ namespace sta
osDelay(ticks);
#ifdef STA_TACOS_WATCHDOG_ENABLED
heartbeat();
#endif // STA_TACOS_WATCHDOG_ENABLED
}
void TacosThread::periodicDelay(uint32_t frequency){
if (previous_tick_ == 0)
{
previous_tick_ = osKernelGetTickCount();
}
previous_tick_ += configTICK_RATE_HZ/frequency;
#ifdef STA_TACOS_WATCHDOG_ENABLED
waiting();
#endif // STA_TACOS_WATCHDOG_ENABLED
osDelayUntil(previous_tick_);
#ifdef STA_TACOS_WATCHDOG_ENABLED
heartbeat();
#endif // STA_TACOS_WATCHDOG_ENABLED