Ken Van Hoeylandt 43c78c69d8
Thread, Timer and flash.sh improvements (#165)
- Various improvements to Thread and Timer:
  - Remove "mark as static" option as it is unused
  - Implemented core pinning for ESP32 platforms
  - Use `TickType_t` consistently (instead of `uint32_t`)
  - Use `enum class` instead of `enum`
- Fix for `flash.sh` not working when using `pip` to install `esptool`
2025-01-13 20:20:43 +01:00

86 lines
2.4 KiB
C++

#include "Timer.h"
#include <utility>
#include "Check.h"
#include "RtosCompat.h"
namespace tt {
static void timer_callback(TimerHandle_t hTimer) {
auto* timer = static_cast<Timer*>(pvTimerGetTimerID(hTimer));
if (timer != nullptr) {
timer->callback(timer->callbackContext);
}
}
Timer::Timer(Type type, Callback callback, std::shared_ptr<void> callbackContext) {
tt_assert((!TT_IS_ISR()) && (callback != nullptr));
this->callback = callback;
this->callbackContext = std::move(callbackContext);
UBaseType_t reload;
if (type == TypeOnce) {
reload = pdFALSE;
} else {
reload = pdTRUE;
}
this->timerHandle = xTimerCreate(nullptr, portMAX_DELAY, (BaseType_t)reload, this, timer_callback);
tt_assert(this->timerHandle);
}
Timer::~Timer() {
tt_assert(!TT_IS_ISR());
tt_check(xTimerDelete(timerHandle, portMAX_DELAY) == pdPASS);
}
bool Timer::start(TickType_t interval) {
tt_assert(!TT_IS_ISR());
tt_assert(interval < portMAX_DELAY);
return xTimerChangePeriod(timerHandle, interval, portMAX_DELAY) == pdPASS;
}
bool Timer::restart(TickType_t interval) {
tt_assert(!TT_IS_ISR());
tt_assert(interval < portMAX_DELAY);
return xTimerChangePeriod(timerHandle, interval, portMAX_DELAY) == pdPASS &&
xTimerReset(timerHandle, portMAX_DELAY) == pdPASS;
}
bool Timer::stop() {
tt_assert(!TT_IS_ISR());
return xTimerStop(timerHandle, portMAX_DELAY) == pdPASS;
}
bool Timer::isRunning() {
tt_assert(!TT_IS_ISR());
return xTimerIsTimerActive(timerHandle) == pdTRUE;
}
TickType_t Timer::getExpireTime() {
tt_assert(!TT_IS_ISR());
return xTimerGetExpiryTime(timerHandle);
}
bool Timer::setPendingCallback(PendingCallback callback, void* callbackContext, uint32_t callbackArg, TickType_t timeout) {
if (TT_IS_ISR()) {
assert(timeout == 0);
return xTimerPendFunctionCallFromISR(callback, callbackContext, callbackArg, nullptr) == pdPASS;
} else {
return xTimerPendFunctionCall(callback, callbackContext, callbackArg, timeout) == pdPASS;
}
}
void Timer::setThreadPriority(Thread::Priority priority) {
tt_assert(!TT_IS_ISR());
TaskHandle_t task_handle = xTimerGetTimerDaemonTaskHandle();
tt_assert(task_handle); // Don't call this method before timer task start
vTaskPrioritySet(task_handle, static_cast<UBaseType_t>(priority));
}
} // namespace