HALFRED  0.4.0
hal_tim.h
Go to the documentation of this file.
1 
10 #ifndef HAL_TIM_H
11 #define HAL_TIM_H
12 
13 #include <stdint.h>
14 #include <stdbool.h>
15 #include "hal_compiler_support.h"
16 
136 
137 #ifndef HAL_TIM_USE_INTERRUPT_EVENTS
138 #define HAL_TIM_USE_INTERRUPT_EVENTS 1
140 #endif
141 
142 #ifndef HAL_TIM_USE_TASK_EVENTS
143 #define HAL_TIM_USE_TASK_EVENTS 1
145 #endif
146 
148 #define TIM_NO_EVENT 0xffffffff
149 
150 
151 // -----------------------------------------------------------------------------
152 // PUBLIC TYPEDEFS
153 // -----------------------------------------------------------------------------
154 
156 typedef union {
158  uint32_t id;
159  struct {
161  uint32_t index : 31;
163  uint32_t type : 1;
164  } field;
165 } TIM_EventID;
166 
167 
169 typedef struct {
171  uint32_t counter_periods : 31;
172  uint32_t negative : 1;
174  uint32_t counter_ticks;
175 } TIM_Time;
176 
177 
179 typedef enum
180 {
187 } TIM_EventType;
188 
189 
191 typedef struct
192 {
194  uint32_t MinTickBase;
196  uint32_t MaxTickBase;
198  uint32_t CounterRange;
200 
201 
203 typedef struct TIMDeviceDesc* TIMDevice;
204 
205 
206 // -----------------------------------------------------------------------------
207 // PUBLIC API
208 // -----------------------------------------------------------------------------
209 
216 void TIM_Init(TIMDevice tim, uint32_t TickTimeBase);
217 
223 #define TIM_Deinit(TIM) (TIM)->Deinit(TIM)
224 
230 #define TIM_GetTime(TIM) (TIM)->GetTime(TIM)
231 
237 #define TIM_ResetCounter(TIM) (TIM)->ResetCounter(TIM)
238 
245 #define TIM_GetCapabilities(TIM, Caps) (TIM)->GetCapabilities(TIM, Caps)
246 
254 #define TIM_GetError(TIM, TickTimeBase) (TIM)->GetError(TIM, TickTimeBase)
255 
256 
262 int64_t TIM_GetTimeInNS(TIMDevice tim);
263 
270 int64_t TIM_GetTimeElapsed(TIMDevice tim, TIM_Time since);
271 
278 TIM_Time TIM_NSToTimTime(TIMDevice tim, int64_t ns);
279 
286 int64_t TIM_TimTimeToNS(TIMDevice tim, TIM_Time t);
287 
296 TIM_EventID TIM_InitEvent(TIMDevice tim, void (*handler)(TIMDevice tim, TIM_EventID event_id, TIM_Time expire_time, void* context), TIM_EventType type, void* context);
297 
304 static HAL_INLINE bool TIM_IsEvent(TIM_EventID event_id) { return event_id.id != TIM_NO_EVENT; }
305 
311 void TIM_DeinitEvent(TIMDevice tim, TIM_EventID event_id);
312 
320 HALRESULT TIM_ScheduleEvent(TIMDevice tim, TIM_EventID event_id, TIM_Time delta_time);
321 
329 HALRESULT TIM_ScheduleEventAt(TIMDevice tim, TIM_EventID event_id, TIM_Time abs_time);
330 
337 HALRESULT TIM_UnscheduleEvent(TIMDevice tim, TIM_EventID event_id);
338 
345 bool TIM_IsEventScheduled(TIMDevice tim, TIM_EventID event_id);
346 
355 TIM_EventID TIM_SetEventType(TIMDevice tim, TIM_EventID event_id, TIM_EventType type);
356 
364 
372 TIM_Time TIM_GetTimeToEvent(TIMDevice tim, TIM_EventID event_id);
373 
380 TIM_EventID TIM_GetNextEvent(TIMDevice tim);
381 
390 TIM_Time TIM_GetEventTime(TIMDevice tim, TIM_EventID event_id);
391 
398 void TIM_TaskEventProc(TIMDevice tim);
399 
406 void TIM_InterruptProc(TIMDevice tim);
407 
416 TIM_Time TIM_AddTime(TIMDevice tim, TIM_Time t1, TIM_Time t2);
417 
426 TIM_Time TIM_SubtractTime(TIMDevice tim, TIM_Time t1, TIM_Time t2);
427 
437 int TIM_CompareTime(TIM_Time t1, TIM_Time t2);
438 
443 static HAL_INLINE int TIM_IsEventValid(TIM_EventID event_id) { if (TIM_NO_EVENT == event_id.id) { return 0; } return 1; }
444 
451 #endif /* HAL_TIM_H */
Definition: hal_tim.h:169
uint32_t MinTickBase
Lowest possible time base for a timer tick (in nanoseconds) - for highest timer resolution.
Definition: hal_tim.h:194
void TIM_TaskEventProc(TIMDevice tim)
This type indicates that the event handler is called from within an interrupt service routine...
Definition: hal_tim.h:184
TIM_Time TIM_GetEventTime(TIMDevice tim, TIM_EventID event_id)
HALRESULT
Definition: hal_defs.h:16
int TIM_CompareTime(TIM_Time t1, TIM_Time t2)
This type indicates, that the event handler is called from a task (and NOT directly in an interrupt s...
Definition: hal_tim.h:186
TIM_Time TIM_SubtractTime(TIMDevice tim, TIM_Time t1, TIM_Time t2)
TIM_EventID TIM_GetNextEvent(TIMDevice tim)
TIM_EventID TIM_InitEvent(TIMDevice tim, void(*handler)(TIMDevice tim, TIM_EventID event_id, TIM_Time expire_time, void *context), TIM_EventType type, void *context)
uint32_t counter_ticks
absolute number of ticks
Definition: hal_tim.h:174
uint32_t id
32-bit plain id
Definition: hal_tim.h:158
Definition: hal_tim.h:156
void TIM_InterruptProc(TIMDevice tim)
Uninitialized type of event.
Definition: hal_tim.h:182
void TIM_DeinitEvent(TIMDevice tim, TIM_EventID event_id)
various compiler-dependent definitions
void TIM_Init(TIMDevice tim, uint32_t TickTimeBase)
Definition: hal_tim.h:191
uint32_t TickTimeBase
Definition: hali_tim.h:47
TIM_Time TIM_NSToTimTime(TIMDevice tim, int64_t ns)
int64_t TIM_GetTimeElapsed(TIMDevice tim, TIM_Time since)
uint32_t MaxTickBase
Highest possible time base for a timer tick(in nanoseconds) - for widest time range.
Definition: hal_tim.h:196
HALRESULT TIM_ScheduleEvent(TIMDevice tim, TIM_EventID event_id, TIM_Time delta_time)
int64_t TIM_GetTimeInNS(TIMDevice tim)
Definition: hali_tim.h:42
uint32_t CounterRange
Hardware counter range. This would be for example 0x0000ffff for 16bit timers.
Definition: hal_tim.h:198
#define TIM_NO_EVENT
An identifier representing uninitialized (empty) event.
Definition: hal_tim.h:148
TIM_Time TIM_AddTime(TIMDevice tim, TIM_Time t1, TIM_Time t2)
struct TIMDeviceDesc * TIMDevice
Definition: hal_tim.h:203
HALRESULT TIM_ScheduleEventAt(TIMDevice tim, TIM_EventID event_id, TIM_Time abs_time)
bool TIM_IsEventScheduled(TIMDevice tim, TIM_EventID event_id)
HALRESULT TIM_UnscheduleEvent(TIMDevice tim, TIM_EventID event_id)
TIM_Time TIM_GetTimeToEvent(TIMDevice tim, TIM_EventID event_id)
int64_t TIM_TimTimeToNS(TIMDevice tim, TIM_Time t)
TIM_EventID TIM_SetEventType(TIMDevice tim, TIM_EventID event_id, TIM_EventType type)
TIM_EventType TIM_GetEventType(TIM_EventID event_id)
TIM_EventType
Definition: hal_tim.h:179