VSF Documented
timers.h
Go to the documentation of this file.
1/*****************************************************************************
2 * Copyright(C)2009-2026 by VSF Team *
3 * *
4 * Licensed under the Apache License, Version 2.0 (the "License"); *
5 * you may not use this file except in compliance with the License. *
6 * You may obtain a copy of the License at *
7 * *
8 * http://www.apache.org/licenses/LICENSE-2.0 *
9 * *
10 * Unless required by applicable law or agreed to in writing, software *
11 * distributed under the License is distributed on an "AS IS" BASIS, *
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
13 * See the License for the specific language governing permissions and *
14 * limitations under the License. *
15 * *
16 ****************************************************************************/
17
18// Clean-room FreeRTOS timers.h shim for VSF.
19//
20// Model:
21// - Each software timer is backed by a vsf_callback_timer_t whose
22// on_timer hook posts the timer pointer to a shared service queue.
23// - A single "timer daemon task" (lazily spawned on first create) drains
24// that queue and invokes the user-supplied TimerCallbackFunction_t in
25// a regular vsf_thread context, so callbacks may call blocking APIs.
26// - Auto-reload timers are re-armed by the daemon after the callback
27// returns, honouring any xTimerChangePeriod / xTimerStop made during
28// the callback body.
29
30#ifndef __VSF_FREERTOS_TIMERS_H__
31#define __VSF_FREERTOS_TIMERS_H__
32
33#include "FreeRTOS.h"
34
35#if defined(__VSF_FREERTOS_TIMERS_CLASS_IMPLEMENT)
36# undef __VSF_FREERTOS_TIMERS_CLASS_IMPLEMENT
37# define __VSF_CLASS_IMPLEMENT__
38#endif
39
40#include "utilities/ooc_class.h"
41
42#ifdef __cplusplus
43extern "C" {
44#endif
45
46/*============================ TYPES =========================================*/
47
48// Forward-declared vsf_class. StaticTimer_t is both the caller-visible
49// opaque-but-correctly-sized backing buffer and the internal control
50// block; TimerHandle_t is an alias pointer into it.
53typedef void (*TimerCallbackFunction_t)(TimerHandle_t xTimer);
54
56 private_member(
57 // vsf_callback_timer_t MUST stay first: __frt_timer_on_cb
58 // upcasts from vsf_callback_timer_t * back to StaticTimer_t *
59 // through a plain cast (offset 0).
60 vsf_callback_timer_t cb_timer;
61 TickType_t period_ticks;
62 UBaseType_t auto_reload;
63 void * pvTimerID;
64 TimerCallbackFunction_t pxCallbackFunction;
65 const char * name;
66 bool is_active;
67 bool is_static;
68 )
69};
70
71/*============================ API ===========================================*/
72
73// Creates a software timer in the "dormant" state. The caller must then
74// call xTimerStart to actually arm it. uxAutoReload == pdTRUE selects a
75// periodic timer; pdFALSE selects one-shot.
76// pxCallbackFunction runs in the timer daemon task context.
77// Returns NULL on allocation failure.
78extern TimerHandle_t xTimerCreate(const char * const pcTimerName,
79 const TickType_t xTimerPeriodInTicks,
80 const UBaseType_t uxAutoReload,
81 void * const pvTimerID,
82 TimerCallbackFunction_t pxCallbackFunction);
83
84// Zero-heap variant. The caller owns pxTimerBuffer; xTimerDelete will
85// stop the timer but will NOT release the buffer.
86extern TimerHandle_t xTimerCreateStatic(const char * const pcTimerName,
87 const TickType_t xTimerPeriodInTicks,
88 const UBaseType_t uxAutoReload,
89 void * const pvTimerID,
90 TimerCallbackFunction_t pxCallbackFunction,
91 StaticTimer_t *pxTimerBuffer);
92
93// Start the timer (or re-arm it from the current tick). xTicksToWait is
94// ignored (there is no command queue to block on in this shim).
95// Returns pdPASS.
96extern BaseType_t xTimerStart(TimerHandle_t xTimer, TickType_t xTicksToWait);
98 BaseType_t *pxHigherPriorityTaskWoken);
99
100// Stop an armed timer. Already-stopped timers are a no-op.
101extern BaseType_t xTimerStop(TimerHandle_t xTimer, TickType_t xTicksToWait);
103 BaseType_t *pxHigherPriorityTaskWoken);
104
105// Stop + start with the same period. Auto-reload and ID are preserved.
106extern BaseType_t xTimerReset(TimerHandle_t xTimer, TickType_t xTicksToWait);
108 BaseType_t *pxHigherPriorityTaskWoken);
109
110// Update the period and re-arm from the current tick. If the timer was
111// not previously running, it is started.
113 TickType_t xNewPeriod,
114 TickType_t xTicksToWait);
116 TickType_t xNewPeriod,
117 BaseType_t *pxHigherPriorityTaskWoken);
118
119// Stop (if running) and free the timer control block.
120extern BaseType_t xTimerDelete(TimerHandle_t xTimer, TickType_t xTicksToWait);
121
122// Queries.
125extern void * pvTimerGetTimerID(const TimerHandle_t xTimer);
126extern void vTimerSetTimerID(TimerHandle_t xTimer, void *pvNewID);
127extern const char *pcTimerGetName(TimerHandle_t xTimer);
128
129#ifdef __cplusplus
130}
131#endif
132
133#endif // __VSF_FREERTOS_TIMERS_H__
Definition timers.h:55
Definition vsf_eda.h:851
long BaseType_t
Definition esp_ringbuf.h:50
#define vsf_dcl_class
Definition ooc_class.h:50
#define vsf_class(__name)
Definition ooc_class.h:52
uint32_t TickType_t
Definition rtos_al.h:59
uint32_t UBaseType_t
Definition rtos_al.h:60
Definition rtos_al.c:114
BaseType_t xTimerStartFromISR(TimerHandle_t xTimer, BaseType_t *pxHigherPriorityTaskWoken)
Definition freertos_timers_port.c:276
BaseType_t xTimerChangePeriod(TimerHandle_t xTimer, TickType_t xNewPeriod, TickType_t xTicksToWait)
Definition freertos_timers_port.c:327
void vTimerSetTimerID(TimerHandle_t xTimer, void *pvNewID)
Definition freertos_timers_port.c:397
TimerHandle_t xTimerCreate(const char *const pcTimerName, const TickType_t xTimerPeriodInTicks, const UBaseType_t uxAutoReload, void *const pvTimerID, TimerCallbackFunction_t pxCallbackFunction)
Definition freertos_timers_port.c:219
BaseType_t xTimerStart(TimerHandle_t xTimer, TickType_t xTicksToWait)
Definition freertos_timers_port.c:260
BaseType_t xTimerStopFromISR(TimerHandle_t xTimer, BaseType_t *pxHigherPriorityTaskWoken)
Definition freertos_timers_port.c:302
TimerHandle_t xTimerCreateStatic(const char *const pcTimerName, const TickType_t xTimerPeriodInTicks, const UBaseType_t uxAutoReload, void *const pvTimerID, TimerCallbackFunction_t pxCallbackFunction, StaticTimer_t *pxTimerBuffer)
Definition freertos_timers_port.c:241
BaseType_t xTimerReset(TimerHandle_t xTimer, TickType_t xTicksToWait)
Definition freertos_timers_port.c:315
void(* TimerCallbackFunction_t)(TimerHandle_t xTimer)
Definition timers.h:53
const char * pcTimerGetName(TimerHandle_t xTimer)
Definition freertos_timers_port.c:403
BaseType_t xTimerStop(TimerHandle_t xTimer, TickType_t xTicksToWait)
Definition freertos_timers_port.c:292
BaseType_t xTimerDelete(TimerHandle_t xTimer, TickType_t xTicksToWait)
Definition freertos_timers_port.c:361
TickType_t xTimerGetPeriod(TimerHandle_t xTimer)
Definition freertos_timers_port.c:385
void * pvTimerGetTimerID(const TimerHandle_t xTimer)
Definition freertos_timers_port.c:391
BaseType_t xTimerIsTimerActive(TimerHandle_t xTimer)
Definition freertos_timers_port.c:378
BaseType_t xTimerChangePeriodFromISR(TimerHandle_t xTimer, TickType_t xNewPeriod, BaseType_t *pxHigherPriorityTaskWoken)
Definition freertos_timers_port.c:343
BaseType_t xTimerResetFromISR(TimerHandle_t xTimer, BaseType_t *pxHigherPriorityTaskWoken)
Definition freertos_timers_port.c:321
Generated from commit: vsfteam/vsf@015f4d1