objc

pwm.h
Go to the documentation of this file.
1 
34 #pragma once
35 #include <stdbool.h>
36 #include <stdint.h>
37 
39 // TYPES
40 
46 typedef struct {
47  uint32_t wrap;
48  float divider;
50 
56 typedef struct hw_pwm_t {
57  uint8_t unit;
58  uint32_t wrap;
59  bool enabled;
60 } hw_pwm_t;
61 
75 typedef void (*hw_pwm_callback_t)(uint8_t unit, void *userdata);
76 
78 // LIFECYCLE
79 
92 hw_pwm_t hw_pwm_init(uint8_t unit, hw_pwm_config_t *config);
93 
102 uint8_t hw_pwm_count();
103 
113 static inline bool hw_pwm_valid(hw_pwm_t *pwm) {
114  if (pwm == NULL) {
115  return false;
116  }
117  if (pwm->unit == 0xFF) {
118  return false;
119  }
120  if (pwm->unit >= hw_pwm_count()) {
121  return false;
122  }
123  return true;
124 }
125 
135 void hw_pwm_finalize(hw_pwm_t *pwm);
136 
138 // CONFIGURATION
139 
149 uint8_t hw_pwm_gpio_unit(uint8_t gpio);
150 
160 
170 float hw_pwm_get_freq(hw_pwm_config_t *config);
171 
182 void hw_pwm_set_config(hw_pwm_t *pwm, const hw_pwm_config_t *config);
183 
185 // CONTROL
186 
205 bool hw_pwm_start(hw_pwm_t *pwm, uint8_t gpio, float duty_percent);
206 
220 void hw_pwm_stop(hw_pwm_t *pwm);
221 
223 // INTERRUPT HANDLING
224 
238 void hw_pwm_set_callback(hw_pwm_callback_t callback, void *userdata);
239 
250 void hw_pwm_set_irq_enabled(hw_pwm_t *pwm, bool enabled);
void hw_pwm_stop(hw_pwm_t *pwm)
Stop PWM output.
void hw_pwm_finalize(hw_pwm_t *pwm)
Finalize and release a PWM unit.
void hw_pwm_set_callback(hw_pwm_callback_t callback, void *userdata)
Set the PWM interrupt callback handler.
PWM configuration structure for setup parameters.
Definition: pwm.h:46
uint8_t hw_pwm_gpio_unit(uint8_t gpio)
Get the PWM unit for a GPIO pin.
bool hw_pwm_start(hw_pwm_t *pwm, uint8_t gpio, float duty_percent)
Start PWM output on a specific GPIO pin.
hw_pwm_t hw_pwm_init(uint8_t unit, hw_pwm_config_t *config)
Initialize a PWM unit.
uint8_t unit
PWM unit number.
Definition: pwm.h:57
void hw_pwm_set_config(hw_pwm_t *pwm, const hw_pwm_config_t *config)
Apply configuration to a PWM unit.
struct hw_pwm_t hw_pwm_t
PWM structure representing a PWM instance.
PWM structure representing a PWM instance.
Definition: pwm.h:56
bool enabled
PWM unit enabled state.
Definition: pwm.h:59
uint8_t hw_pwm_count()
Get the number of PWM units.
hw_pwm_config_t hw_pwm_get_config(float freq)
Get PWM configuration.
static bool hw_pwm_valid(hw_pwm_t *pwm)
Validate the PWM unit.
Definition: pwm.h:113
void(* hw_pwm_callback_t)(uint8_t unit, void *userdata)
PWM interrupt callback function pointer.
Definition: pwm.h:75
void hw_pwm_set_irq_enabled(hw_pwm_t *pwm, bool enabled)
Enable or disable PWM wrap interrupt for a slice.
uint32_t wrap
Counter wrap value (TOP), determines PWM period.
Definition: pwm.h:47
uint32_t wrap
Current wrap value for duty cycle calculations.
Definition: pwm.h:58
float hw_pwm_get_freq(hw_pwm_config_t *config)
Returns the frequency for a configuration.
float divider
Clock divider.
Definition: pwm.h:48