Bluenet
5.7.0
Bluenet, firmware for nRF52 smart home devices
|
Pulse Wide Modulation class. More...
#include <cs_PWM.h>
Public Member Functions | |
uint32_t | init (const pwm_config_t &config) |
Initialize the PWM settings. More... | |
uint32_t | deinit () |
De-Initialize the PWM instance, i.e. free allocated resources. More... | |
uint32_t | start (bool onZeroCrossing) |
Start PWM. More... | |
void | setValue (uint8_t channel, uint8_t value, uint8_t speed) |
Set the value of a specific channel. More... | |
uint8_t | getValue (uint8_t channel) |
Get current value of a specific channel. More... | |
void | onPeriodEnd () |
Function to be called on the end of the PWM period. More... | |
void | onZeroCrossingInterrupt () |
Function to be called on a zero crossing interrupt. More... | |
void | _handleInterrupt () |
Interrupt handler: internal function, implementation specific. More... | |
Static Public Member Functions | |
static PWM & | getInstance () |
Gets a static singleton (no dynamic memory allocation) of the PWM class. More... | |
Static Public Attributes | |
static const uint8_t | _maxValue = 100 |
Private Member Functions | |
PWM () | |
Private PWM constructor. More... | |
PWM (PWM const &) | |
Private PWM copy constructor. More... | |
void | operator= (PWM const &) |
Private PWM copy assignment definition. More... | |
uint32_t | initChannel (uint8_t index, pwm_channel_config_t &config) |
Init a channel. More... | |
void | start () |
Start the PWM timer, and mark PWM as started. More... | |
void | updateValues () |
Check if the current value with the target value. More... | |
void | setValue (uint8_t channel, uint8_t newValue) |
Actually set the value in the peripheral. More... | |
void | gpioteConfig (uint8_t channel, bool initOn) |
Config gpiote. More... | |
void | gpioteUnconfig (uint8_t channel) |
Unconfig gpiote. More... | |
void | gpioteEnable (uint8_t channel) |
Enable gpiote. More... | |
void | gpioteDisable (uint8_t channel) |
Disable gpiote. More... | |
void | gpioteForce (uint8_t channel, bool initOn) |
Force gpiote state. More... | |
void | gpioOn (uint8_t channel) |
Turn on with gpio. More... | |
void | gpioOff (uint8_t channel) |
Turn off with gpio. More... | |
void | writeCC (uint8_t channelIdx, uint32_t ticks) |
Write CC of timer. More... | |
uint32_t | readCC (uint8_t channelIdx) |
Read CC of timer. More... | |
void | enableInterrupt () |
Enables the timer interrupt, to change the pwm value. More... | |
nrf_timer_cc_channel_t | getTimerChannel (uint8_t index) |
Helper function to get the timer channel, given the index. More... | |
nrf_gpiote_tasks_t | getGpioteTaskOut (uint8_t index) |
Helper function to get the gpiote task out, given the index. More... | |
nrf_gpiote_tasks_t | getGpioteTaskSet (uint8_t index) |
Helper function to get the gpiote task out, given the index. More... | |
nrf_gpiote_tasks_t | getGpioteTaskClear (uint8_t index) |
Helper function to get the gpiote task out, given the index. More... | |
nrf_ppi_channel_t | getPpiChannel (uint8_t index) |
Helper function to get the ppi channel, given the index. More... | |
nrf_ppi_channel_group_t | getPpiGroup (uint8_t index) |
Helper function to get the ppi group, given the index. More... | |
nrf_ppi_task_t | getPpiTaskEnable (uint8_t index) |
Helper function to get the ppi enable task, given the group index. More... | |
nrf_ppi_task_t | getPpiTaskDisable (uint8_t index) |
Helper function to get the ppi disable task, given the group index. More... | |
Private Attributes | |
pwm_config_t | _config |
Config of the PWM. More... | |
uint8_t | _values [CS_PWM_MAX_CHANNELS] = {0} |
Duty cycle values of the channels in percentage. More... | |
bool | _initialized |
Flag to indicate that the init function has been successfully performed. More... | |
bool | _started |
Flag to indicate that the start functions has been successfully performed. More... | |
bool | _startOnZeroCrossing |
Flag to indicate whether to wait for a zero crossing to start. More... | |
uint32_t | _maxTickVal |
Max value of channel, in ticks. Set at init. More... | |
uint32_t | _adjustedMaxTickVal |
Max value of channel, in ticks. Adjusted to sync with zero crossings. More... | |
uint8_t | _targetValues [CS_PWM_MAX_CHANNELS] = {0} |
Target duty cycle values of the channels in percentage. More... | |
uint8_t | _stepSize [CS_PWM_MAX_CHANNELS] = {0} |
Step size to move actual value towards target value. More... | |
uint8_t | _updateValuesCountdown = 0 |
Current number of periods to wait before next value update. More... | |
uint32_t | _tickValues [CS_PWM_MAX_CHANNELS] = {0} |
Duty cycle values of the channels in ticks. More... | |
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. More... | |
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. More... | |
nrf_ppi_channel_t | _ppiTransitionChannel |
PPI channel to be used for duty cycle transitions. More... | |
nrf_gpiote_outinit_t | _gpioteInitStatesOn [CS_PWM_MAX_CHANNELS] |
GPIOTE init states cache. More... | |
nrf_gpiote_outinit_t | _gpioteInitStatesOff [CS_PWM_MAX_CHANNELS] |
bool | _isPwmEnabled [CS_PWM_MAX_CHANNELS] |
Returns whether a channel is currently dimming (value > 0 and < max). More... | |
uint32_t | _zeroCrossingCounter |
Counter to keep up the number of zero crossing callbacks. More... | |
int64_t | _zeroCrossTicksDeviationAvg |
Moving average amount of timer ticks deviation compared to when the zero crossing callback was called. More... | |
int64_t | _zeroCrossDeviationIntegral |
Integral of the tick deviations. More... | |
Static Private Attributes | |
static const uint8_t | numPeriodsBeforeValueUpdate = 3 |
Only update values every so many PWM periods. More... | |
Pulse Wide Modulation class.
To turn on/off the power, as well as all intermediate stages, for example with dimming, the PWM class is used.
TODO: The PWM class concerns dimming in general. It should be called "Dimmer", not just "PWM".
void PWM::_handleInterrupt | ( | ) |
Interrupt handler: internal function, implementation specific.
uint32_t PWM::deinit | ( | ) |
De-Initialize the PWM instance, i.e. free allocated resources.
|
private |
Enables the timer interrupt, to change the pwm value.
|
private |
Helper function to get the gpiote task out, given the index.
|
private |
Helper function to get the gpiote task out, given the index.
|
private |
Helper function to get the gpiote task out, given the index.
|
inlinestatic |
Gets a static singleton (no dynamic memory allocation) of the PWM class.
|
private |
Helper function to get the ppi channel, given the index.
|
private |
Helper function to get the ppi group, given the index.
|
private |
Helper function to get the ppi disable task, given the group index.
|
private |
Helper function to get the ppi enable task, given the group index.
|
private |
Helper function to get the timer channel, given the index.
uint8_t PWM::getValue | ( | uint8_t | channel | ) |
Get current value of a specific channel.
|
private |
Turn off with gpio.
|
private |
Turn on with gpio.
|
private |
Config gpiote.
|
private |
Disable gpiote.
|
private |
Enable gpiote.
|
private |
Force gpiote state.
|
private |
Unconfig gpiote.
uint32_t PWM::init | ( | const pwm_config_t & | config | ) |
Initialize the PWM settings.
config can be safely deleted after calling this function.
|
private |
Init a channel.
void PWM::onPeriodEnd | ( | ) |
Function to be called on the end of the PWM period.
Decoupled from interrupt.
void PWM::onZeroCrossingInterrupt | ( | ) |
Function to be called on a zero crossing interrupt.
|
private |
Read CC of timer.
|
private |
Actually set the value in the peripheral.
void PWM::setValue | ( | uint8_t | channel, |
uint8_t | value, | ||
uint8_t | speed | ||
) |
Set the value of a specific channel.
Each tick, the value will go towards target value by increasing or decreasing the actual value with 'speed'.
[in] | channel | Channel to set. |
[in] | value | Target value to set the channel to (0-100). |
[in] | speed | Speed at which to go to target value (1-100). |
|
private |
uint32_t PWM::start | ( | bool | onZeroCrossing | ) |
Start PWM.
[in] | onZeroCrossing | Set to true to make it start at the first zero crossing. |
|
private |
Check if the current value with the target value.
If not equal, set the value.
|
private |
Write CC of timer.
|
private |
Max value of channel, in ticks. Adjusted to sync with zero crossings.
|
private |
Config of the PWM.
|
private |
|
private |
GPIOTE init states cache.
|
private |
Flag to indicate that the init function has been successfully performed.
|
private |
Returns whether a channel is currently dimming (value > 0 and < max).
|
private |
Max value of channel, in ticks. Set at init.
|
static |
|
private |
PPI channels to be used to trigger GPIOTE tasks from timer compare events. Turning the switch off.
|
private |
PPI channels to be used to trigger GPIOTE tasks from timer compare events. Turning the switch on.
|
private |
PPI channel to be used for duty cycle transitions.
|
private |
Flag to indicate that the start functions has been successfully performed.
|
private |
Flag to indicate whether to wait for a zero crossing to start.
|
private |
Step size to move actual value towards target value.
|
private |
Target duty cycle values of the channels in percentage.
|
private |
Duty cycle values of the channels in ticks.
|
private |
Current number of periods to wait before next value update.
|
private |
Duty cycle values of the channels in percentage.
|
private |
Integral of the tick deviations.
|
private |
Counter to keep up the number of zero crossing callbacks.
|
private |
Moving average amount of timer ticks deviation compared to when the zero crossing callback was called.
|
staticprivate |
Only update values every so many PWM periods.