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_base_impl.h
Go to the documentation of this file.
1
28#ifndef CAFFEINE_HAL_HAL_BASE_IMPL_H
29#define CAFFEINE_HAL_HAL_BASE_IMPL_H
30
31#ifdef __cplusplus
32extern "C"
33{
34#endif
35
36/* Includes ---------------------------------------------------------*/
37#include "cfn_hal_base.h"
38
39/* Functions Implementation -----------------------------------------*/
45#define CFN_HAL_POPULATE_DRIVER( \
46 driver_ptr, periph_type, periph_id, clock_ptr, dep_ptr, api_ptr, phy_ptr, cfg_ptr, cb_func, cb_arg) \
47 do \
48 { \
49 if (driver_ptr) \
50 { \
51 cfn_hal_base_populate( \
52 &(driver_ptr)->base, (periph_type), (periph_id), &(api_ptr)->base, (clock_ptr), (dep_ptr)); \
53 (driver_ptr)->api = (api_ptr); \
54 (driver_ptr)->phy = (phy_ptr); \
55 (driver_ptr)->config = (cfg_ptr); \
56 (driver_ptr)->cb = (cb_func); \
57 (driver_ptr)->cb_user_arg = (cb_arg); \
58 } \
59 } while (0)
60
63 uint32_t peripheral_id,
64 const struct cfn_hal_api_base_s *vmt,
65 struct cfn_hal_clock_s *clock,
66 void *dependency)
67{
68 if (!base)
69 {
70 return;
71 }
72 base->type = type;
73 base->peripheral_id = peripheral_id;
74 base->vmt = vmt;
75 base->clock_driver = clock;
78 base->on_config = NULL;
79 base->on_config_arg = NULL;
80 base->dependency = dependency;
81 base->extension = NULL;
82 base->flags = 0;
83}
84
86{
87 if (!base || base->type != expected_type || !base->vmt)
88 {
90 }
91
93 {
95 }
96
98
99 /* Board-level Initialization Hook (Phase A)
100 * This must happen BEFORE the VMT init. It typically handles clock gating
101 * and pin muxing via the 'on_config' callback provided by the BSP.
102 */
103 if (base->on_config)
104 {
105 error = base->on_config(base, base->on_config_arg, CFN_HAL_CONFIG_PHASE_INIT);
106 if (error != CFN_HAL_ERROR_OK)
107 {
108 return error;
109 }
110 }
111
112 /* Hardware-specific Peripheral Initialization (Phase B)
113 * We cast the generic VMT to the base API type to access the init hook.
114 */
115 const cfn_hal_api_base_t *api = (const cfn_hal_api_base_t *) base->vmt;
116 if (api->init)
117 {
118 error = api->init(base);
119 }
120 else
121 {
122 // Requires port implmentation
124 }
125 /* 4. Final State Update (Phase C)
126 * If all stages passed, we mark the driver as ready for functional use.
127 */
128 if (error == CFN_HAL_ERROR_OK)
129 {
131 }
132 else if (base->on_config)
133 {
134 /* Roll back board-level config if hardware init failed.
135 * We ignore the error code from the DEINIT phase to ensure
136 * the original Phase B error is returned to the caller.
137 */
138 (void) base->on_config(base, base->on_config_arg, CFN_HAL_CONFIG_PHASE_DEINIT);
139 }
140
141 return error;
142}
143
145 cfn_hal_peripheral_type_t expected_type)
146{
147 if (!base || base->type != expected_type || !base->vmt)
148 {
150 }
151
153 {
154 return CFN_HAL_ERROR_OK;
155 }
156
158 {
160 }
161
163
164 /* Hardware-specific Peripheral Deinitialization (Phase A)
165 * We trigger the VMT deinit first while the clocks/pins are still active.
166 */
167 const cfn_hal_api_base_t *api = (const cfn_hal_api_base_t *) base->vmt;
168 if (api->deinit)
169 {
170 error = api->deinit(base);
171 }
172 else
173 {
174 // Requires port implmentation
176 }
177 /* Board-level Deinitialization Hook (Phase B)
178 * Only after the peripheral logic is stopped do we release clocks and pins.
179 * We use CFN_HAL_CONFIG_PHASE_DEINIT to signify teardown to the hook.
180 */
181 if (error == CFN_HAL_ERROR_OK)
182 {
183 /* Mark as constructed (not live) before releasing clocks to prevent race
184 * conditions */
186
187 if (base->on_config)
188 {
189 error = base->on_config(base, base->on_config_arg, CFN_HAL_CONFIG_PHASE_DEINIT);
190 }
191 }
192
193 return error;
194}
195
197 cfn_hal_peripheral_type_t expected_type,
198 const void *config)
199{
200 if (!base || base->type != expected_type || !config)
201 {
203 }
204
206
208 {
209 const cfn_hal_api_base_t *api = (const cfn_hal_api_base_t *) base->vmt;
210 if (api)
211 {
212 if (api->config_validate)
213 {
214 error = api->config_validate(base, config);
215 }
216
217 if (error == CFN_HAL_ERROR_OK)
218 {
219 if (api->config_set)
220 {
221 error = api->config_set(base, config);
222 }
223 else
224 {
226 }
227 }
228 }
229 }
230
231 return error;
232}
233
235 cfn_hal_peripheral_type_t expected_type,
236 const void *config)
237{
238 if (!base || base->type != expected_type)
239 {
241 }
242
244
245 const cfn_hal_api_base_t *api = (const cfn_hal_api_base_t *) base->vmt;
246 if (api && api->config_validate)
247 {
248 error = api->config_validate(base, config);
249 }
250
251 return error;
252}
253
255 cfn_hal_peripheral_type_t expected_type,
256 cfn_hal_callback_t callback,
257 void *user_arg)
258{
259 if (!base || base->type != expected_type)
260 {
262 }
263
265
267 {
268 const cfn_hal_api_base_t *api = (const cfn_hal_api_base_t *) base->vmt;
269
270 if (api && api->callback_register)
271 {
272 error = api->callback_register(base, callback, user_arg);
273 }
274 }
275
276 return error;
277}
278
280 cfn_hal_peripheral_type_t expected_type,
282{
283 if (!base || base->type != expected_type)
284 {
286 }
287
288 if (base->power_state == state)
289 {
290 return CFN_HAL_ERROR_OK;
291 }
292
294
296 {
297 const cfn_hal_api_base_t *api = (const cfn_hal_api_base_t *) base->vmt;
298
299 if (api && api->power_state_set)
300 {
301 error = api->power_state_set(base, state);
302 if (error == CFN_HAL_ERROR_OK)
303 {
304 base->power_state = state;
305 }
306 }
307 else
308 {
309 // Requires port implmentation
311 }
312 }
313 else
314 {
315 base->power_state = state;
316 }
317
318 return error;
319}
320
322{
323 if (!base)
324 {
326 }
327
328 return base->power_state;
329}
330
332 cfn_hal_peripheral_type_t expected_type,
333 uint32_t event_mask)
334{
335 if (!base || base->type != expected_type || !base->vmt)
336 {
338 }
339
341 {
343 }
344
346
347 const cfn_hal_api_base_t *api = (const cfn_hal_api_base_t *) base->vmt;
348 if (api->event_enable)
349 {
350 error = api->event_enable(base, event_mask);
351 }
352 else
353 {
354 // event enable requires port implmentation
356 }
357
358 return error;
359}
360
362 cfn_hal_peripheral_type_t expected_type,
363 uint32_t event_mask)
364{
365 if (!base || base->type != expected_type || !base->vmt)
366 {
368 }
369
371 {
373 }
374
376
377 const cfn_hal_api_base_t *api = (const cfn_hal_api_base_t *) base->vmt;
378 if (api->event_disable)
379 {
380 error = api->event_disable(base, event_mask);
381 }
382
383 return error;
384}
385
387 cfn_hal_peripheral_type_t expected_type,
388 uint32_t *event_mask)
389{
390 if (!base || base->type != expected_type || !event_mask || !base->vmt)
391 {
393 }
394
396 {
398 }
399
401
402 const cfn_hal_api_base_t *api = (const cfn_hal_api_base_t *) base->vmt;
403 if (api->event_get)
404 {
405 error = api->event_get(base, event_mask);
406 }
407
408 return error;
409}
410
412 cfn_hal_peripheral_type_t expected_type,
413 uint32_t error_mask)
414{
415 if (!base || base->type != expected_type || !base->vmt)
416 {
418 }
419
421 {
423 }
424
426
427 const cfn_hal_api_base_t *api = (const cfn_hal_api_base_t *) base->vmt;
428 if (api->error_enable)
429 {
430 error = api->error_enable(base, error_mask);
431 }
432
433 return error;
434}
435
437 cfn_hal_peripheral_type_t expected_type,
438 uint32_t error_mask)
439{
440 if (!base || base->type != expected_type || !base->vmt)
441 {
443 }
444
446 {
448 }
449
451
452 const cfn_hal_api_base_t *api = (const cfn_hal_api_base_t *) base->vmt;
453 if (api->error_disable)
454 {
455 error = api->error_disable(base, error_mask);
456 }
457
458 return error;
459}
460
462 cfn_hal_peripheral_type_t expected_type,
463 uint32_t *error_mask)
464{
465 if (!base || base->type != expected_type || !error_mask || !base->vmt)
466 {
468 }
469
471 {
473 }
474
476
477 const cfn_hal_api_base_t *api = (const cfn_hal_api_base_t *) base->vmt;
478 if (api->error_get)
479 {
480 error = api->error_get(base, error_mask);
481 }
482
483 return error;
484}
485
486#if (CFN_HAL_USE_LOCK == 1)
487CFN_HAL_BASE_API cfn_hal_error_code_t cfn_hal_base_lock(cfn_hal_driver_t *base, uint32_t timeout)
488{
489 if (!base || !base->vmt)
490 {
492 }
493
494 const cfn_hal_api_base_t *api = (const cfn_hal_api_base_t *) base->vmt;
495 if (!api->lock)
496 {
497 return CFN_HAL_ERROR_OK;
498 }
499
500 return api->lock(base, timeout);
501}
502
504{
505 if (!base || !base->vmt)
506 {
508 }
509
510 const cfn_hal_api_base_t *api = (const cfn_hal_api_base_t *) base->vmt;
511 if (!api->unlock)
512 {
513 return CFN_HAL_ERROR_OK;
514 }
515
516 return api->unlock(base);
517}
518#endif
519
520#ifdef __cplusplus
521}
522#endif
523
524#endif // CAFFEINE_HAL_HAL_BASE_IMPL_H
#define CFN_HAL_INLINE
Macro for inlining HAL wrapper functions. Can be overridden with attribute((always_inline)) for perfo...
Definition cfn_hal.h:61
Base driver Hardware Abstraction Layer declarations.
#define CFN_HAL_BASE_API
API visibility macro for the base driver.
Definition cfn_hal_base.h:53
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_INLINE void cfn_hal_base_populate(cfn_hal_driver_t *base, cfn_hal_peripheral_type_t type, uint32_t peripheral_id, const struct cfn_hal_api_base_s *vmt, struct cfn_hal_clock_s *clock, void *dependency)
Definition cfn_hal_base_impl.h:61
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_power_state_t cfn_hal_power_state_get(const cfn_hal_driver_t *base)
Generic power state getter. Returns the current power state from the software shadow.
Definition cfn_hal_base_impl.h:321
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
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
@ CFN_HAL_CONFIG_PHASE_INIT
Definition cfn_hal_types.h:45
@ CFN_HAL_CONFIG_PHASE_DEINIT
Definition cfn_hal_types.h:44
@ CFN_HAL_DRIVER_STATUS_CONSTRUCTED
Definition cfn_hal_types.h:146
@ CFN_HAL_DRIVER_STATUS_INITIALIZED
Definition cfn_hal_types.h:148
@ CFN_HAL_DRIVER_STATUS_UNKNOWN
Definition cfn_hal_types.h:145
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
uint32_t cfn_hal_peripheral_type_t
Definition cfn_hal_types.h:85
cfn_hal_power_state_t
Definition cfn_hal_types.h:156
@ CFN_HAL_POWER_STATE_UNKNOWN
Definition cfn_hal_types.h:157
@ CFN_HAL_ERROR_OK
Definition cfn_hal_types.h:50
@ CFN_HAL_ERROR_BAD_PARAM
Definition cfn_hal_types.h:53
@ CFN_HAL_ERROR_DRIVER_ALREADY_INIT
Definition cfn_hal_types.h:68
@ CFN_HAL_ERROR_DRIVER_NOT_INIT
Definition cfn_hal_types.h:67
@ CFN_HAL_ERROR_NOT_SUPPORTED
Definition cfn_hal_types.h:57
Base API structure for all peripheral drivers. Every peripheral-specific API struct MUST have this as...
Definition cfn_hal_base.h:63
cfn_hal_error_code_t(* power_state_set)(cfn_hal_driver_t *base, cfn_hal_power_state_t state)
Definition cfn_hal_base.h:67
cfn_hal_error_code_t(* error_disable)(cfn_hal_driver_t *base, uint32_t error_mask)
Definition cfn_hal_base.h:77
cfn_hal_error_code_t(* callback_register)(cfn_hal_driver_t *base, cfn_hal_callback_t callback, void *user_arg)
Definition cfn_hal_base.h:70
cfn_hal_error_code_t(* event_enable)(cfn_hal_driver_t *base, uint32_t event_mask)
Definition cfn_hal_base.h:72
cfn_hal_error_code_t(* error_enable)(cfn_hal_driver_t *base, uint32_t error_mask)
Definition cfn_hal_base.h:76
cfn_hal_error_code_t(* config_set)(cfn_hal_driver_t *base, const void *config)
Definition cfn_hal_base.h:68
cfn_hal_error_code_t(* config_validate)(const cfn_hal_driver_t *base, const void *config)
Definition cfn_hal_base.h:69
cfn_hal_error_code_t(* error_get)(cfn_hal_driver_t *base, uint32_t *error_mask)
Definition cfn_hal_base.h:78
cfn_hal_error_code_t(* event_get)(cfn_hal_driver_t *base, uint32_t *event_mask)
Definition cfn_hal_base.h:74
cfn_hal_error_code_t(* init)(cfn_hal_driver_t *base)
Definition cfn_hal_base.h:64
cfn_hal_error_code_t(* event_disable)(cfn_hal_driver_t *base, uint32_t event_mask)
Definition cfn_hal_base.h:73
cfn_hal_error_code_t(* deinit)(cfn_hal_driver_t *base)
Definition cfn_hal_base.h:65
Base structure for all peripheral drivers. Contains common state and polymorphic interface linkage.
Definition cfn_hal_types.h:171
struct cfn_hal_clock_s * clock_driver
Definition cfn_hal_types.h:176
uint32_t flags
Definition cfn_hal_types.h:197
void * dependency
Definition cfn_hal_types.h:192
void * extension
Definition cfn_hal_types.h:194
cfn_hal_error_code_t(* on_config)(struct cfn_hal_driver_s *base, void *user_arg, cfn_hal_config_phase_t phase)
Board-level configuration hook (BSP). Called by the generic HAL to handle hardware-specific setup lik...
Definition cfn_hal_types.h:188
cfn_hal_power_state_t power_state
Definition cfn_hal_types.h:174
cfn_hal_peripheral_type_t type
Definition cfn_hal_types.h:172
const struct cfn_hal_api_base_s * vmt
Definition cfn_hal_types.h:199
uint32_t peripheral_id
Definition cfn_hal_types.h:198
void * on_config_arg
Definition cfn_hal_types.h:190
cfn_hal_driver_status_t status
Definition cfn_hal_types.h:173