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_pwm.h
Go to the documentation of this file.
1
26#ifndef CAFFEINE_HAL_HAL_PWM_H
27#define CAFFEINE_HAL_HAL_PWM_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_gpio.h"
38#include "cfn_hal_types.h"
39
40/* Defines ----------------------------------------------------------*/
41
42/* Types Enums ------------------------------------------------------*/
43
53
63
74
75/* Types Structs ----------------------------------------------------*/
76
80typedef struct
81{
82 void *instance;
83 uint32_t channel;
85 void *user_arg;
87
98
99typedef struct cfn_hal_pwm_s cfn_hal_pwm_t;
101
109typedef void (*cfn_hal_pwm_callback_t)(cfn_hal_pwm_t *driver, uint32_t event_mask, uint32_t error_mask, void *user_arg);
110
115{
117
118 /* PWM Specific Extensions */
121 cfn_hal_error_code_t (*set_frequency)(cfn_hal_pwm_t *driver, uint32_t frequency_hz);
122 cfn_hal_error_code_t (*set_duty_cycle)(cfn_hal_pwm_t *driver, uint32_t duty_percent);
123};
124
126
128/* Functions inline ------------------------------------------------- */
130 uint32_t peripheral_id,
131 struct cfn_hal_clock_s *clock,
132 void *dependency,
133 const cfn_hal_pwm_api_t *api,
134 const cfn_hal_pwm_phy_t *phy,
135 const cfn_hal_pwm_config_t *config,
136 cfn_hal_pwm_callback_t callback,
137 void *user_arg)
138{
140 driver, CFN_HAL_PERIPHERAL_TYPE_PWM, peripheral_id, clock, dependency, api, phy, config, callback, user_arg);
141}
142
150 const cfn_hal_pwm_config_t *config)
151{
152 if (driver == NULL || config == NULL)
153 {
155 }
156
158 {
160 }
161
162 return cfn_hal_base_config_validate(&driver->base, CFN_HAL_PERIPHERAL_TYPE_PWM, config);
163}
164
171{
172 if (!driver)
173 {
175 }
176 driver->base.vmt = (const struct cfn_hal_api_base_s *) driver->api;
177 cfn_hal_error_code_t error = cfn_hal_pwm_config_validate(driver, driver->config);
178 if (error != CFN_HAL_ERROR_OK)
179 {
180 return error;
181 }
182 return cfn_hal_base_init(&driver->base, CFN_HAL_PERIPHERAL_TYPE_PWM);
183}
184
191{
192 if (!driver)
193 {
195 }
197}
198
206{
207 if (!driver)
208 {
210 }
212 if (error != CFN_HAL_ERROR_OK)
213 {
214 return error;
215 }
216 {
217 driver->config = config;
218 }
219 return cfn_hal_base_config_set(&driver->base, CFN_HAL_PERIPHERAL_TYPE_PWM, (const void *) config);
220}
221
229{
230 if (!driver || !config || !driver->config)
231 {
233 }
234 *config = *(driver->config);
235 return CFN_HAL_ERROR_OK;
236}
237
246 const cfn_hal_pwm_callback_t callback,
247 void *user_arg)
248{
249 if (!driver)
250 {
252 }
253 {
254 driver->cb = callback;
255 driver->cb_user_arg = user_arg;
256 }
258 &driver->base, CFN_HAL_PERIPHERAL_TYPE_PWM, (cfn_hal_callback_t) callback, user_arg);
259}
260
275
283{
284 if (!driver)
285 {
287 }
288 return cfn_hal_base_event_enable(&driver->base, CFN_HAL_PERIPHERAL_TYPE_PWM, event_mask);
289}
290
298{
299 if (!driver)
300 {
302 }
303 return cfn_hal_base_event_disable(&driver->base, CFN_HAL_PERIPHERAL_TYPE_PWM, event_mask);
304}
305
313{
314 if (!driver)
315 {
317 }
318 return cfn_hal_base_event_get(&driver->base, CFN_HAL_PERIPHERAL_TYPE_PWM, event_mask);
319}
320
328{
329 if (!driver)
330 {
332 }
333 return cfn_hal_base_error_enable(&driver->base, CFN_HAL_PERIPHERAL_TYPE_PWM, error_mask);
334}
335
343{
344 if (!driver)
345 {
347 }
348 return cfn_hal_base_error_disable(&driver->base, CFN_HAL_PERIPHERAL_TYPE_PWM, error_mask);
349}
350
358{
359 if (!driver)
360 {
362 }
363 return cfn_hal_base_error_get(&driver->base, CFN_HAL_PERIPHERAL_TYPE_PWM, error_mask);
364}
365
366/* PWM Specific Functions ------------------------------------------- */
367
379
391
399{
401 CFN_HAL_CHECK_AND_CALL_FUNC_VARG(CFN_HAL_PERIPHERAL_TYPE_PWM, set_frequency, driver, error, frequency_hz);
402 return error;
403}
404
412{
414 CFN_HAL_CHECK_AND_CALL_FUNC_VARG(CFN_HAL_PERIPHERAL_TYPE_PWM, set_duty_cycle, driver, error, duty_percent);
415 return error;
416}
418 const cfn_hal_pwm_config_t *config,
419 const cfn_hal_pwm_phy_t *phy,
420 struct cfn_hal_clock_s *clock,
421 void *dependency,
422 cfn_hal_pwm_callback_t callback,
423 void *user_arg);
425#ifdef __cplusplus
426}
427#endif
428
429#endif // CAFFEINE_HAL_HAL_PWM_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
General Purpose Input/Output (GPIO) HAL API.
CFN_HAL_INLINE void cfn_hal_pwm_populate(cfn_hal_pwm_t *driver, uint32_t peripheral_id, struct cfn_hal_clock_s *clock, void *dependency, const cfn_hal_pwm_api_t *api, const cfn_hal_pwm_phy_t *phy, const cfn_hal_pwm_config_t *config, cfn_hal_pwm_callback_t callback, void *user_arg)
Definition cfn_hal_pwm.h:129
cfn_hal_error_code_t cfn_hal_pwm_destruct(cfn_hal_pwm_t *driver)
Definition cfn_hal_pwm_port.c:85
CFN_HAL_INLINE cfn_hal_error_code_t cfn_hal_pwm_error_disable(cfn_hal_pwm_t *driver, uint32_t error_mask)
Disables one or more PWM exception errors.
Definition cfn_hal_pwm.h:342
CFN_HAL_INLINE cfn_hal_error_code_t cfn_hal_pwm_set_duty_cycle(cfn_hal_pwm_t *driver, uint32_t duty_percent)
Updates the PWM duty cycle.
Definition cfn_hal_pwm.h:411
void(* cfn_hal_pwm_callback_t)(cfn_hal_pwm_t *driver, uint32_t event_mask, uint32_t error_mask, void *user_arg)
PWM callback signature.
Definition cfn_hal_pwm.h:109
CFN_HAL_INLINE cfn_hal_error_code_t cfn_hal_pwm_set_frequency(cfn_hal_pwm_t *driver, uint32_t frequency_hz)
Updates the PWM output frequency.
Definition cfn_hal_pwm.h:398
CFN_HAL_INLINE cfn_hal_error_code_t cfn_hal_pwm_config_set(cfn_hal_pwm_t *driver, const cfn_hal_pwm_config_t *config)
Sets the PWM configuration.
Definition cfn_hal_pwm.h:205
CFN_HAL_INLINE cfn_hal_error_code_t cfn_hal_pwm_event_disable(cfn_hal_pwm_t *driver, uint32_t event_mask)
Disables one or more PWM nominal events.
Definition cfn_hal_pwm.h:297
CFN_HAL_INLINE cfn_hal_error_code_t cfn_hal_pwm_start(cfn_hal_pwm_t *driver)
Starts PWM signal generation.
Definition cfn_hal_pwm.h:373
CFN_HAL_INLINE cfn_hal_error_code_t cfn_hal_pwm_callback_register(cfn_hal_pwm_t *driver, const cfn_hal_pwm_callback_t callback, void *user_arg)
Registers a callback for PWM events and errors.
Definition cfn_hal_pwm.h:245
CFN_HAL_INLINE cfn_hal_error_code_t cfn_hal_pwm_error_get(cfn_hal_pwm_t *driver, uint32_t *error_mask)
Retrieves the current PWM exception error status.
Definition cfn_hal_pwm.h:357
CFN_HAL_INLINE cfn_hal_error_code_t cfn_hal_pwm_config_get(cfn_hal_pwm_t *driver, cfn_hal_pwm_config_t *config)
Gets the current PWM configuration.
Definition cfn_hal_pwm.h:228
CFN_HAL_INLINE cfn_hal_error_code_t cfn_hal_pwm_power_state_set(cfn_hal_pwm_t *driver, cfn_hal_power_state_t state)
Sets the PWM power state.
Definition cfn_hal_pwm.h:267
CFN_HAL_INLINE cfn_hal_error_code_t cfn_hal_pwm_config_validate(const cfn_hal_pwm_t *driver, const cfn_hal_pwm_config_t *config)
Validates the PWM configuration.
Definition cfn_hal_pwm.h:149
CFN_HAL_INLINE cfn_hal_error_code_t cfn_hal_pwm_error_enable(cfn_hal_pwm_t *driver, uint32_t error_mask)
Enables one or more PWM exception errors.
Definition cfn_hal_pwm.h:327
CFN_HAL_INLINE cfn_hal_error_code_t cfn_hal_pwm_deinit(cfn_hal_pwm_t *driver)
Deinitializes the PWM driver.
Definition cfn_hal_pwm.h:190
struct cfn_hal_pwm_s cfn_hal_pwm_t
Definition cfn_hal_pwm.h:99
cfn_hal_pwm_config_polarity_t
PWM output polarity.
Definition cfn_hal_pwm.h:68
@ CFN_HAL_PWM_CONFIG_POLARITY_NORMAL
Definition cfn_hal_pwm.h:69
@ CFN_HAL_PWM_CONFIG_POLARITY_INVERTED
Definition cfn_hal_pwm.h:71
cfn_hal_pwm_event_t
PWM nominal event flags.
Definition cfn_hal_pwm.h:48
@ CFN_HAL_PWM_EVENT_PULSE_MATCH
Definition cfn_hal_pwm.h:51
@ CFN_HAL_PWM_EVENT_PERIOD_MATCH
Definition cfn_hal_pwm.h:50
@ CFN_HAL_PWM_EVENT_NONE
Definition cfn_hal_pwm.h:49
CFN_HAL_INLINE cfn_hal_error_code_t cfn_hal_pwm_init(cfn_hal_pwm_t *driver)
Initializes the PWM driver.
Definition cfn_hal_pwm.h:170
CFN_HAL_INLINE cfn_hal_error_code_t cfn_hal_pwm_event_get(cfn_hal_pwm_t *driver, uint32_t *event_mask)
Retrieves the current PWM nominal event status.
Definition cfn_hal_pwm.h:312
CFN_HAL_INLINE cfn_hal_error_code_t cfn_hal_pwm_event_enable(cfn_hal_pwm_t *driver, uint32_t event_mask)
Enables one or more PWM nominal events.
Definition cfn_hal_pwm.h:282
cfn_hal_pwm_error_t
PWM exception error flags.
Definition cfn_hal_pwm.h:58
@ CFN_HAL_PWM_ERROR_NONE
Definition cfn_hal_pwm.h:59
@ CFN_HAL_PWM_ERROR_FAULT
Definition cfn_hal_pwm.h:60
@ CFN_HAL_PWM_ERROR_GENERAL
Definition cfn_hal_pwm.h:61
cfn_hal_error_code_t cfn_hal_pwm_construct(cfn_hal_pwm_t *driver, const cfn_hal_pwm_config_t *config, const cfn_hal_pwm_phy_t *phy, struct cfn_hal_clock_s *clock, void *dependency, cfn_hal_pwm_callback_t callback, void *user_arg)
Definition cfn_hal_pwm_port.c:69
CFN_HAL_INLINE cfn_hal_error_code_t cfn_hal_pwm_stop(cfn_hal_pwm_t *driver)
Stops PWM signal generation.
Definition cfn_hal_pwm.h:385
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
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
@ CFN_HAL_ERROR_BAD_CONFIG
Definition cfn_hal_types.h:60
#define CFN_HAL_PERIPHERAL_TYPE_PWM
Definition cfn_hal_types.h:127
Base API structure for all peripheral drivers. Every peripheral-specific API struct MUST have this as...
Definition cfn_hal_base.h:63
Lightweight handle to a specific GPIO pin. Used by other peripherals to reference physical pins.
Definition cfn_hal_gpio.h:173
PWM Virtual Method Table (VMT).
Definition cfn_hal_pwm.h:115
cfn_hal_error_code_t(* set_frequency)(cfn_hal_pwm_t *driver, uint32_t frequency_hz)
Definition cfn_hal_pwm.h:121
cfn_hal_error_code_t(* stop)(cfn_hal_pwm_t *driver)
Definition cfn_hal_pwm.h:120
cfn_hal_error_code_t(* set_duty_cycle)(cfn_hal_pwm_t *driver, uint32_t duty_percent)
Definition cfn_hal_pwm.h:122
cfn_hal_error_code_t(* start)(cfn_hal_pwm_t *driver)
Definition cfn_hal_pwm.h:119
cfn_hal_api_base_t base
Definition cfn_hal_pwm.h:116
PWM configuration structure.
Definition cfn_hal_pwm.h:92
void * custom
Definition cfn_hal_pwm.h:96
cfn_hal_pwm_config_polarity_t polarity
Definition cfn_hal_pwm.h:95
uint32_t duty_cycle_percent
Definition cfn_hal_pwm.h:94
uint32_t frequency_hz
Definition cfn_hal_pwm.h:93
PWM hardware physical mapping.
Definition cfn_hal_pwm.h:81
uint32_t channel
Definition cfn_hal_pwm.h:83
cfn_hal_gpio_pin_handle_t * pin
Definition cfn_hal_pwm.h:84
void * instance
Definition cfn_hal_pwm.h:82
void * user_arg
Definition cfn_hal_pwm.h:85