roo_threads
API Documentation for roo_threads
Loading...
Searching...
No Matches
timeutil.h
Go to the documentation of this file.
1#pragma once
2
4
5#ifdef ROO_THREADS_USE_FREERTOS
6
7#include "freertos/FreeRTOS.h"
8#include "freertos/task.h"
10#include "roo_time.h"
11
12namespace roo_threads {
13namespace freertos {
14
15namespace internal {
16
17// Truncate to half of the representation range, to avoid overflow in the
18// FreeRTOS API, taking into account that tick counter overflows.
19constexpr TickType_t kMaxTicksDelay = portMAX_DELAY / 2;
20
21inline constexpr TickType_t ToTicks(roo_time::Duration duration) {
22 int64_t micros = duration.inMicros();
23 if (micros <= 0) return 0;
24 uint64_t ms = (micros + 999) / 1000;
25 uint64_t ticks = (ms + portTICK_PERIOD_MS - 1) / portTICK_PERIOD_MS;
26 return (ticks <= kMaxTicksDelay) ? static_cast<TickType_t>(ticks)
27 : kMaxTicksDelay;
28}
29
30inline roo_time::Uptime CalculateDeadlineFromDuration(
31 const roo_time::Duration& duration) {
32 auto now = roo_time::Uptime::Now();
33 return (duration <= roo_time::Uptime::Max() - now) ? now + duration
34 : roo_time::Uptime::Max();
35}
36
37} // namespace internal
38
39} // namespace freertos
40} // namespace roo_threads
41
42#endif // ROO_THREADS_USE_FREERTOS