9#include <modules/nrfx/hal/nrf_gpiote.h>
10#include <modules/nrfx/hal/nrf_ppi.h>
11#include <modules/nrfx/hal/nrf_timer.h>
12#include <nrf_gpiote.h>
19#define ERR_PWM_NOT_ENABLED 1
61 uint32_t
start(
bool onZeroCrossing);
72 void setValue(uint8_t channel, uint8_t value, uint8_t speed);
206 void writeCC(uint8_t channelIdx, uint32_t ticks);
Pulse Wide Modulation class.
Definition: cs_PWM.h:38
void setValue(uint8_t channel, uint8_t value, uint8_t speed)
Set the value of a specific channel.
PWM(PWM const &)
Private PWM copy constructor.
void gpioteDisable(uint8_t channel)
Disable gpiote.
bool _initialized
Flag to indicate that the init function has been successfully performed.
Definition: cs_PWM.h:107
void start()
Start the PWM timer, and mark PWM as started.
uint8_t _stepSize[CS_PWM_MAX_CHANNELS]
Step size to move actual value towards target value.
Definition: cs_PWM.h:151
void gpioOff(uint8_t channel)
Turn off with gpio.
void _handleInterrupt()
Interrupt handler: internal function, implementation specific.
uint32_t deinit()
De-Initialize the PWM instance, i.e. free allocated resources.
void updateValues()
Check if the current value with the target value.
void gpioteUnconfig(uint8_t channel)
Unconfig gpiote.
uint32_t _tickValues[CS_PWM_MAX_CHANNELS]
Duty cycle values of the channels in ticks.
Definition: cs_PWM.h:164
uint32_t _maxTickVal
Max value of channel, in ticks. Set at init.
Definition: cs_PWM.h:138
nrf_gpiote_tasks_t getGpioteTaskSet(uint8_t index)
Helper function to get the gpiote task out, given the index.
int64_t _zeroCrossTicksDeviationAvg
Moving average amount of timer ticks deviation compared to when the zero crossing callback was called...
Definition: cs_PWM.h:186
nrf_gpiote_tasks_t getGpioteTaskClear(uint8_t index)
Helper function to get the gpiote task out, given the index.
void gpioteConfig(uint8_t channel, bool initOn)
Config gpiote.
nrf_ppi_channel_group_t getPpiGroup(uint8_t index)
Helper function to get the ppi group, given the index.
uint32_t initChannel(uint8_t index, pwm_channel_config_t &config)
Init a channel.
uint32_t readCC(uint8_t channelIdx)
Read CC of timer.
uint32_t init(const pwm_config_t &config)
Initialize the PWM settings.
void enableInterrupt()
Enables the timer interrupt, to change the pwm value.
nrf_gpiote_tasks_t getGpioteTaskOut(uint8_t index)
Helper function to get the gpiote task out, given the index.
nrf_ppi_channel_t getPpiChannel(uint8_t index)
Helper function to get the ppi channel, given the index.
uint8_t getValue(uint8_t channel)
Get current value of a specific channel.
uint32_t _adjustedMaxTickVal
Max value of channel, in ticks. Adjusted to sync with zero crossings.
Definition: cs_PWM.h:141
void onZeroCrossingInterrupt()
Function to be called on a zero crossing interrupt.
uint8_t _values[CS_PWM_MAX_CHANNELS]
Duty cycle values of the channels in percentage.
Definition: cs_PWM.h:104
nrf_ppi_channel_t _ppiChannelsOff[CS_PWM_MAX_CHANNELS]
PPI channels to be used to trigger GPIOTE tasks from timer compare events. Turning the switch off.
Definition: cs_PWM.h:170
bool _isPwmEnabled[CS_PWM_MAX_CHANNELS]
Returns whether a channel is currently dimming (value > 0 and < max).
Definition: cs_PWM.h:180
bool _started
Flag to indicate that the start functions has been successfully performed.
Definition: cs_PWM.h:110
int64_t _zeroCrossDeviationIntegral
Integral of the tick deviations.
Definition: cs_PWM.h:189
void gpioteForce(uint8_t channel, bool initOn)
Force gpiote state.
nrf_gpiote_outinit_t _gpioteInitStatesOff[CS_PWM_MAX_CHANNELS]
Definition: cs_PWM.h:177
static const uint8_t numPeriodsBeforeValueUpdate
Only update values every so many PWM periods.
Definition: cs_PWM.h:156
void operator=(PWM const &)
Private PWM copy assignment definition.
nrf_ppi_channel_t _ppiTransitionChannel
PPI channel to be used for duty cycle transitions.
Definition: cs_PWM.h:173
pwm_config_t _config
Config of the PWM.
Definition: cs_PWM.h:101
PWM()
Private PWM constructor.
uint8_t _targetValues[CS_PWM_MAX_CHANNELS]
Target duty cycle values of the channels in percentage.
Definition: cs_PWM.h:146
static const uint8_t _maxValue
Definition: cs_PWM.h:46
uint32_t start(bool onZeroCrossing)
Start PWM.
nrf_timer_cc_channel_t getTimerChannel(uint8_t index)
Helper function to get the timer channel, given the index.
uint8_t _updateValuesCountdown
Current number of periods to wait before next value update.
Definition: cs_PWM.h:161
void setValue(uint8_t channel, uint8_t newValue)
Actually set the value in the peripheral.
nrf_ppi_task_t getPpiTaskDisable(uint8_t index)
Helper function to get the ppi disable task, given the group index.
uint32_t _zeroCrossingCounter
Counter to keep up the number of zero crossing callbacks.
Definition: cs_PWM.h:183
nrf_ppi_channel_t _ppiChannelsOn[CS_PWM_MAX_CHANNELS]
PPI channels to be used to trigger GPIOTE tasks from timer compare events. Turning the switch on.
Definition: cs_PWM.h:167
void onPeriodEnd()
Function to be called on the end of the PWM period.
void gpioOn(uint8_t channel)
Turn on with gpio.
void gpioteEnable(uint8_t channel)
Enable gpiote.
static PWM & getInstance()
Gets a static singleton (no dynamic memory allocation) of the PWM class.
Definition: cs_PWM.h:41
nrf_ppi_task_t getPpiTaskEnable(uint8_t index)
Helper function to get the ppi enable task, given the group index.
void writeCC(uint8_t channelIdx, uint32_t ticks)
Write CC of timer.
nrf_gpiote_outinit_t _gpioteInitStatesOn[CS_PWM_MAX_CHANNELS]
GPIOTE init states cache.
Definition: cs_PWM.h:176
bool _startOnZeroCrossing
Flag to indicate whether to wait for a zero crossing to start.
Definition: cs_PWM.h:113
#define CS_PWM_MAX_CHANNELS
Definition: cs_Config.h:91
bool inverted
Definition: cs_PWM.h:23
uint16_t pin
Definition: cs_PWM.h:22
uint8_t channelCount
Definition: cs_PWM.h:28
uint32_t period_us
Definition: cs_PWM.h:27