Software based PWMs are the perfect location to add custom events when PWM drivers changes the matching output pin from high to low and vice versa.
The idea behind is to be able to trigger custom events (in the form of function pointers) using a standard function signature, which will be called whenever a certain event is triggered by the output signal.
For instance, we can think about a car's turn signal. We need to send the PWM signal to a relay/solid state relay in order to toggle the turning lights.
At the same time, as modern car do not rely on electromechanical, self-oscillating devices to provide this feature anymore, we can generate an event whenever output light is toggled which will use the car's audio system in order to play the usual turn signal sound (a clicky sound in facts).
This architecture will simplify application code as we don't need keep track of time explicitely with an event that should be in sync with the software based PWM using external code.
Directly using software based PWM with integrated event generation will simply call the embedded functions, run the custom code (that needs to be short) and resume back to its regular operation.
Rising edge event :
Falling edge event :
api could look like this :
void (pwm_event_t*)(void);
typedef enum
{
PWM_EVENT_RISING_EDGE,
PWM_EVENT_FALLING_EDGE
} pwm_event_kind_t;
/**
* @brief Configures a single event (callback function) when the targeted pwm instance
* reaches the selected state described with the `kind` parameter
* @param index : index of pwm software instance (as per referenced in pwm_static_config array)
* @param event : callback function called when the targeted even is detected
* @param kind : encodes the kind of event (either rising or falling edge)
*/
pwm_error_t pwm_soft_set_event(const uint8_t index, pwm_event_t event, pwm_event_kind_t kind);
Software based PWMs are the perfect location to add custom events when PWM drivers changes the matching output pin from high to low and vice versa.
The idea behind is to be able to trigger custom events (in the form of function pointers) using a standard function signature, which will be called whenever a certain event is triggered by the output signal.
For instance, we can think about a car's turn signal. We need to send the PWM signal to a relay/solid state relay in order to toggle the turning lights.
At the same time, as modern car do not rely on electromechanical, self-oscillating devices to provide this feature anymore, we can generate an event whenever output light is toggled which will use the car's audio system in order to play the usual turn signal sound (a clicky sound in facts).
This architecture will simplify application code as we don't need keep track of time explicitely with an event that should be in sync with the software based PWM using external code.
Directly using software based PWM with integrated event generation will simply call the embedded functions, run the custom code (that needs to be short) and resume back to its regular operation.
Rising edge event :
Falling edge event :
api could look like this :