caffeine-hal 0.6.6
A Header-Defined Interface c library, it provides the hal layer for the Caffeine framework
Loading...
Searching...
No Matches
cfn_hal_timer.h
Go to the documentation of this file.
1
26#ifndef CAFFEINE_HAL_HAL_TIMER_H
27#define CAFFEINE_HAL_HAL_TIMER_H
28
29#ifdef __cplusplus
30extern "C"
31{
32#endif
33
34/* Includes ---------------------------------------------------------*/
35#include "cfn_hal.h"
36#include "cfn_hal_base.h"
37#include "cfn_hal_types.h"
38
39/* Defines ----------------------------------------------------------*/
40
41/* Types Enums ------------------------------------------------------*/
42
56
66
67/* Types Structs ----------------------------------------------------*/
68
72typedef struct
73{
74 uint32_t prescaler;
75 uint32_t period;
76 void *custom;
78
82typedef struct
83{
84 void *instance;
85 void *user_arg;
87
91typedef struct
92{
93 uint32_t channel;
94 uint64_t ticks;
96
97typedef struct cfn_hal_timer_s cfn_hal_timer_t;
99
109 cfn_hal_timer_t *driver, uint32_t event_mask, uint32_t error_mask, uint32_t channel, void *user_arg);
110
115{
117
118 /* Timer Specific Extensions */
121 cfn_hal_error_code_t (*get_ticks)(cfn_hal_timer_t *driver, uint32_t ch, uint32_t *ticks);
122 cfn_hal_error_code_t (*get_ticks_u64)(cfn_hal_timer_t *driver, uint32_t ch, uint64_t *ticks);
124};
125
127
130
131/* Functions inline ------------------------------------------------- */
133 uint32_t peripheral_id,
134 struct cfn_hal_clock_s *clock,
135 void *dependency,
136 const cfn_hal_timer_api_t *api,
137 const cfn_hal_timer_phy_t *phy,
138 const cfn_hal_timer_config_t *config,
140 void *user_arg)
141{
143 driver, CFN_HAL_PERIPHERAL_TYPE_TIMER, peripheral_id, clock, dependency, api, phy, config, callback, user_arg);
144}
152 const cfn_hal_timer_config_t *config)
153{
154 if (driver == NULL || config == NULL)
155 {
157 }
158
159 return cfn_hal_base_config_validate(&driver->base, CFN_HAL_PERIPHERAL_TYPE_TIMER, config);
160}
161
168{
169 if (!driver)
170 {
172 }
173 driver->base.vmt = (const struct cfn_hal_api_base_s *) driver->api;
174 cfn_hal_error_code_t error = cfn_hal_timer_config_validate(driver, driver->config);
175 if (error != CFN_HAL_ERROR_OK)
176 {
177 return error;
178 }
180}
181
195
203 const cfn_hal_timer_config_t *config)
204{
205 if (!driver)
206 {
208 }
209 {
210 driver->config = config;
211 }
212 return cfn_hal_base_config_set(&driver->base, CFN_HAL_PERIPHERAL_TYPE_TIMER, (const void *) config);
213}
214
222{
223 if (!driver || !config || !driver->config)
224 {
226 }
227 *config = *(driver->config);
228 return CFN_HAL_ERROR_OK;
229}
230
239 const cfn_hal_timer_callback_t callback,
240 void *user_arg)
241{
242 if (!driver)
243 {
245 }
246 {
247 driver->cb = callback;
248 driver->cb_user_arg = user_arg;
249 }
251 &driver->base, CFN_HAL_PERIPHERAL_TYPE_TIMER, (cfn_hal_callback_t) callback, user_arg);
252}
253
268
276{
277 if (!driver)
278 {
280 }
281 return cfn_hal_base_event_enable(&driver->base, CFN_HAL_PERIPHERAL_TYPE_TIMER, event_mask);
282}
283
291{
292 if (!driver)
293 {
295 }
296 return cfn_hal_base_event_disable(&driver->base, CFN_HAL_PERIPHERAL_TYPE_TIMER, event_mask);
297}
298
306{
307 if (!driver)
308 {
310 }
311 return cfn_hal_base_event_get(&driver->base, CFN_HAL_PERIPHERAL_TYPE_TIMER, event_mask);
312}
313
321{
322 if (!driver)
323 {
325 }
326 return cfn_hal_base_error_enable(&driver->base, CFN_HAL_PERIPHERAL_TYPE_TIMER, error_mask);
327}
328
336{
337 if (!driver)
338 {
340 }
341 return cfn_hal_base_error_disable(&driver->base, CFN_HAL_PERIPHERAL_TYPE_TIMER, error_mask);
342}
343
351{
352 if (!driver)
353 {
355 }
356 return cfn_hal_base_error_get(&driver->base, CFN_HAL_PERIPHERAL_TYPE_TIMER, error_mask);
357}
358
359/* Timer Specific Functions ----------------------------------------- */
360
372
384
393{
395 CFN_HAL_CHECK_AND_CALL_FUNC_VARG(CFN_HAL_PERIPHERAL_TYPE_TIMER, get_ticks, driver, error, ch, ticks);
396 return error;
397}
398
407{
409 CFN_HAL_CHECK_AND_CALL_FUNC_VARG(CFN_HAL_PERIPHERAL_TYPE_TIMER, get_ticks_u64, driver, error, ch, ticks);
410 return error;
411}
412
426
428 const cfn_hal_timer_config_t *config,
429 const cfn_hal_timer_phy_t *phy,
430 struct cfn_hal_clock_s *clock,
431 void *dependency,
433 void *user_arg);
435#ifdef __cplusplus
436}
437#endif
438
439#endif // CAFFEINE_HAL_HAL_TIMER_H
Core Hardware Abstraction Layer definitions and macros.
#define CFN_HAL_CHECK_AND_CALL_FUNC_VARG(expected_peripheral_type, func, driver, result,...)
Definition cfn_hal.h:134
#define CFN_HAL_CHECK_AND_CALL_FUNC(expected_peripheral_type, func, driver, result)
Definition cfn_hal.h:114
#define CFN_HAL_INLINE
Macro for inlining HAL wrapper functions. Can be overridden with attribute((always_inline)) for perfo...
Definition cfn_hal.h:61
#define CFN_HAL_BIT(x)
Definition cfn_hal.h:79
#define CFN_HAL_CREATE_DRIVER_TYPE(prefix, config_type, api_type, phy_type, cb_type)
Definition cfn_hal.h:102
Base driver Hardware Abstraction Layer declarations.
CFN_HAL_BASE_API cfn_hal_error_code_t cfn_hal_base_error_disable(cfn_hal_driver_t *base, cfn_hal_peripheral_type_t expected_type, uint32_t error_mask)
Generic error disable for any driver. Deactivates exception-flow hardware triggers based on the provi...
Definition cfn_hal_base_impl.h:436
CFN_HAL_BASE_API cfn_hal_error_code_t cfn_hal_base_callback_register(cfn_hal_driver_t *base, cfn_hal_peripheral_type_t expected_type, cfn_hal_callback_t callback, void *user_arg)
Generic callback registration for any driver.
Definition cfn_hal_base_impl.h:254
CFN_HAL_BASE_API cfn_hal_error_code_t cfn_hal_base_error_enable(cfn_hal_driver_t *base, cfn_hal_peripheral_type_t expected_type, uint32_t error_mask)
Generic error enable for any driver. Activates exception-flow hardware triggers based on the provided...
Definition cfn_hal_base_impl.h:411
CFN_HAL_BASE_API cfn_hal_error_code_t cfn_hal_base_event_enable(cfn_hal_driver_t *base, cfn_hal_peripheral_type_t expected_type, uint32_t event_mask)
Generic event enable for any driver. Activates nominal hardware triggers based on the provided mask.
Definition cfn_hal_base_impl.h:331
CFN_HAL_BASE_API cfn_hal_error_code_t cfn_hal_base_event_get(cfn_hal_driver_t *base, cfn_hal_peripheral_type_t expected_type, uint32_t *event_mask)
Generic event status getter for any driver. Retrieves the current nominal hardware triggers/flags.
Definition cfn_hal_base_impl.h:386
CFN_HAL_BASE_API cfn_hal_error_code_t cfn_hal_base_error_get(cfn_hal_driver_t *base, cfn_hal_peripheral_type_t expected_type, uint32_t *error_mask)
Generic error status getter for any driver. Retrieves current exception-flow hardware flags/errors.
Definition cfn_hal_base_impl.h:461
CFN_HAL_BASE_API cfn_hal_error_code_t cfn_hal_base_config_set(cfn_hal_driver_t *base, cfn_hal_peripheral_type_t expected_type, const void *config)
Generic configuration setter for any driver.
Definition cfn_hal_base_impl.h:196
CFN_HAL_BASE_API cfn_hal_error_code_t cfn_hal_power_state_set(cfn_hal_driver_t *base, cfn_hal_peripheral_type_t expected_type, cfn_hal_power_state_t state)
Generic power state transition for any driver.
Definition cfn_hal_base_impl.h:279
CFN_HAL_BASE_API cfn_hal_error_code_t cfn_hal_base_deinit(cfn_hal_driver_t *base, cfn_hal_peripheral_type_t expected_type)
Generic deinitialization for any driver. Returns the driver to the CONSTRUCTED state and releases boa...
Definition cfn_hal_base_impl.h:144
#define CFN_HAL_VMT_CHECK(api_struct_type)
Compile-time check to ensure a peripheral API struct is compatible with the base layer....
Definition cfn_hal_base.h:91
CFN_HAL_BASE_API cfn_hal_error_code_t cfn_hal_base_init(cfn_hal_driver_t *base, cfn_hal_peripheral_type_t expected_type)
Generic initialization for any driver. Handles board-level hooks, type validation,...
Definition cfn_hal_base_impl.h:85
CFN_HAL_BASE_API cfn_hal_error_code_t cfn_hal_base_config_validate(const cfn_hal_driver_t *base, cfn_hal_peripheral_type_t expected_type, const void *config)
Generic validation for a configuration.
Definition cfn_hal_base_impl.h:234
CFN_HAL_BASE_API cfn_hal_error_code_t cfn_hal_base_event_disable(cfn_hal_driver_t *base, cfn_hal_peripheral_type_t expected_type, uint32_t event_mask)
Generic event disable for any driver. Deactivates nominal hardware triggers based on the provided mas...
Definition cfn_hal_base_impl.h:361
#define CFN_HAL_POPULATE_DRIVER( driver_ptr, periph_type, periph_id, clock_ptr, dep_ptr, api_ptr, phy_ptr, cfg_ptr, cb_func, cb_arg)
Standardizes the population of a peripheral driver structure. This macro ensures that the base driver...
Definition cfn_hal_base_impl.h:45
CFN_HAL_INLINE cfn_hal_error_code_t cfn_hal_timer_get_ticks_u64(cfn_hal_timer_t *driver, uint32_t ch, uint64_t *ticks)
Reads the current tick count for a specific channel (64-bit).
Definition cfn_hal_timer.h:406
CFN_HAL_INLINE cfn_hal_error_code_t cfn_hal_timer_start(cfn_hal_timer_t *driver)
Starts the timer counter.
Definition cfn_hal_timer.h:366
CFN_HAL_INLINE cfn_hal_error_code_t cfn_hal_timer_power_state_set(cfn_hal_timer_t *driver, cfn_hal_power_state_t state)
Sets the Timer power state.
Definition cfn_hal_timer.h:260
struct cfn_hal_timer_s cfn_hal_timer_t
Definition cfn_hal_timer.h:97
CFN_HAL_INLINE cfn_hal_error_code_t cfn_hal_timer_set_period(cfn_hal_timer_t *driver, const cfn_hal_timer_period_t *period)
Updates the timer period/reload value for a specific channel.
Definition cfn_hal_timer.h:419
CFN_HAL_INLINE cfn_hal_error_code_t cfn_hal_timer_error_enable(cfn_hal_timer_t *driver, uint32_t error_mask)
Enables one or more Timer exception errors.
Definition cfn_hal_timer.h:320
cfn_hal_error_code_t cfn_hal_timer_destruct(cfn_hal_timer_t *driver)
Definition cfn_hal_timer_port.c:86
CFN_HAL_INLINE cfn_hal_error_code_t cfn_hal_timer_stop(cfn_hal_timer_t *driver)
Stops the timer counter.
Definition cfn_hal_timer.h:378
CFN_HAL_INLINE cfn_hal_error_code_t cfn_hal_timer_error_disable(cfn_hal_timer_t *driver, uint32_t error_mask)
Disables one or more Timer exception errors.
Definition cfn_hal_timer.h:335
CFN_HAL_INLINE cfn_hal_error_code_t cfn_hal_timer_event_enable(cfn_hal_timer_t *driver, uint32_t event_mask)
Enables one or more Timer nominal events.
Definition cfn_hal_timer.h:275
cfn_hal_timer_error_t
Timer exception error flags.
Definition cfn_hal_timer.h:61
@ CFN_HAL_TIMER_ERROR_NONE
Definition cfn_hal_timer.h:62
@ CFN_HAL_TIMER_ERROR_BREAK
Definition cfn_hal_timer.h:63
@ CFN_HAL_TIMER_ERROR_GENERAL
Definition cfn_hal_timer.h:64
CFN_HAL_INLINE void cfn_hal_timer_populate(cfn_hal_timer_t *driver, uint32_t peripheral_id, struct cfn_hal_clock_s *clock, void *dependency, const cfn_hal_timer_api_t *api, const cfn_hal_timer_phy_t *phy, const cfn_hal_timer_config_t *config, cfn_hal_timer_callback_t callback, void *user_arg)
Definition cfn_hal_timer.h:132
CFN_HAL_INLINE cfn_hal_error_code_t cfn_hal_timer_error_get(cfn_hal_timer_t *driver, uint32_t *error_mask)
Retrieves the current Timer exception error status.
Definition cfn_hal_timer.h:350
CFN_HAL_INLINE cfn_hal_error_code_t cfn_hal_timer_init(cfn_hal_timer_t *driver)
Initializes the Timer driver.
Definition cfn_hal_timer.h:167
CFN_HAL_INLINE cfn_hal_error_code_t cfn_hal_timer_callback_register(cfn_hal_timer_t *driver, const cfn_hal_timer_callback_t callback, void *user_arg)
Registers a callback for Timer events and errors.
Definition cfn_hal_timer.h:238
CFN_HAL_INLINE cfn_hal_error_code_t cfn_hal_timer_config_validate(const cfn_hal_timer_t *driver, const cfn_hal_timer_config_t *config)
Validates the Timer configuration.
Definition cfn_hal_timer.h:151
cfn_hal_error_code_t cfn_hal_timer_construct(cfn_hal_timer_t *driver, const cfn_hal_timer_config_t *config, const cfn_hal_timer_phy_t *phy, struct cfn_hal_clock_s *clock, void *dependency, cfn_hal_timer_callback_t callback, void *user_arg)
Definition cfn_hal_timer_port.c:70
CFN_HAL_INLINE cfn_hal_error_code_t cfn_hal_timer_event_disable(cfn_hal_timer_t *driver, uint32_t event_mask)
Disables one or more Timer nominal events.
Definition cfn_hal_timer.h:290
CFN_HAL_INLINE cfn_hal_error_code_t cfn_hal_timer_event_get(cfn_hal_timer_t *driver, uint32_t *event_mask)
Retrieves the current Timer nominal event status.
Definition cfn_hal_timer.h:305
void(* cfn_hal_timer_callback_t)(cfn_hal_timer_t *driver, uint32_t event_mask, uint32_t error_mask, uint32_t channel, void *user_arg)
Timer callback signature.
Definition cfn_hal_timer.h:108
CFN_HAL_INLINE cfn_hal_error_code_t cfn_hal_timer_config_get(cfn_hal_timer_t *driver, cfn_hal_timer_config_t *config)
Gets the current Timer configuration.
Definition cfn_hal_timer.h:221
CFN_HAL_INLINE cfn_hal_error_code_t cfn_hal_timer_config_set(cfn_hal_timer_t *driver, const cfn_hal_timer_config_t *config)
Sets the Timer configuration.
Definition cfn_hal_timer.h:202
cfn_hal_timer_event_t
Timer nominal event flags.
Definition cfn_hal_timer.h:47
@ CFN_HAL_TIMER_EVENT_CC3
Definition cfn_hal_timer.h:52
@ CFN_HAL_TIMER_EVENT_CC4
Definition cfn_hal_timer.h:53
@ CFN_HAL_TIMER_EVENT_CC1
Definition cfn_hal_timer.h:50
@ CFN_HAL_TIMER_EVENT_UPDATE
Definition cfn_hal_timer.h:49
@ CFN_HAL_TIMER_EVENT_TRIGGER
Definition cfn_hal_timer.h:54
@ CFN_HAL_TIMER_EVENT_CC2
Definition cfn_hal_timer.h:51
@ CFN_HAL_TIMER_EVENT_NONE
Definition cfn_hal_timer.h:48
CFN_HAL_INLINE cfn_hal_error_code_t cfn_hal_timer_deinit(cfn_hal_timer_t *driver)
Deinitializes the Timer driver.
Definition cfn_hal_timer.h:187
CFN_HAL_INLINE cfn_hal_error_code_t cfn_hal_timer_get_ticks(cfn_hal_timer_t *driver, uint32_t ch, uint32_t *ticks)
Reads the current tick count for a specific channel (32-bit).
Definition cfn_hal_timer.h:392
Common type definitions and enums for the HAL.
void(* cfn_hal_callback_t)(void)
Generic function pointer for HAL callbacks. Used as a standard-compliant carrier in the base layer.
Definition cfn_hal_types.h:98
enum cfn_hal_error_codes cfn_hal_error_code_t
#define CFN_HAL_PERIPHERAL_TYPE_TIMER
Definition cfn_hal_types.h:130
cfn_hal_power_state_t
Definition cfn_hal_types.h:156
@ CFN_HAL_ERROR_OK
Definition cfn_hal_types.h:50
@ CFN_HAL_ERROR_BAD_PARAM
Definition cfn_hal_types.h:53
Base API structure for all peripheral drivers. Every peripheral-specific API struct MUST have this as...
Definition cfn_hal_base.h:63
Timer Virtual Method Table (VMT).
Definition cfn_hal_timer.h:115
cfn_hal_api_base_t base
Definition cfn_hal_timer.h:116
cfn_hal_error_code_t(* set_period)(cfn_hal_timer_t *driver, const cfn_hal_timer_period_t *period)
Definition cfn_hal_timer.h:123
cfn_hal_error_code_t(* start)(cfn_hal_timer_t *driver)
Definition cfn_hal_timer.h:119
cfn_hal_error_code_t(* get_ticks_u64)(cfn_hal_timer_t *driver, uint32_t ch, uint64_t *ticks)
Definition cfn_hal_timer.h:122
cfn_hal_error_code_t(* stop)(cfn_hal_timer_t *driver)
Definition cfn_hal_timer.h:120
cfn_hal_error_code_t(* get_ticks)(cfn_hal_timer_t *driver, uint32_t ch, uint32_t *ticks)
Definition cfn_hal_timer.h:121
Timer configuration structure.
Definition cfn_hal_timer.h:73
uint32_t period
Definition cfn_hal_timer.h:75
void * custom
Definition cfn_hal_timer.h:76
uint32_t prescaler
Definition cfn_hal_timer.h:74
Timer period and channel mapping.
Definition cfn_hal_timer.h:92
uint64_t ticks
Definition cfn_hal_timer.h:94
uint32_t channel
Definition cfn_hal_timer.h:93
Timer hardware physical mapping.
Definition cfn_hal_timer.h:83
void * instance
Definition cfn_hal_timer.h:84
void * user_arg
Definition cfn_hal_timer.h:85