Bluenet  5.7.0
Bluenet, firmware for nRF52 smart home devices
Loading...
Searching...
No Matches
PWM Class Reference

Pulse Wide Modulation class. More...

#include <cs_PWM.h>

Collaboration diagram for PWM:

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 PWMgetInstance ()
 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...
 

Detailed Description

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".

Constructor & Destructor Documentation

◆ PWM() [1/2]

PWM::PWM ( )
private

Private PWM constructor.

◆ PWM() [2/2]

PWM::PWM ( PWM const &  )
private

Private PWM copy constructor.

Member Function Documentation

◆ _handleInterrupt()

void PWM::_handleInterrupt ( )

Interrupt handler: internal function, implementation specific.

◆ deinit()

uint32_t PWM::deinit ( )

De-Initialize the PWM instance, i.e. free allocated resources.

◆ enableInterrupt()

void PWM::enableInterrupt ( )
private

Enables the timer interrupt, to change the pwm value.

◆ getGpioteTaskClear()

nrf_gpiote_tasks_t PWM::getGpioteTaskClear ( uint8_t  index)
private

Helper function to get the gpiote task out, given the index.

◆ getGpioteTaskOut()

nrf_gpiote_tasks_t PWM::getGpioteTaskOut ( uint8_t  index)
private

Helper function to get the gpiote task out, given the index.

◆ getGpioteTaskSet()

nrf_gpiote_tasks_t PWM::getGpioteTaskSet ( uint8_t  index)
private

Helper function to get the gpiote task out, given the index.

◆ getInstance()

static PWM & PWM::getInstance ( )
inlinestatic

Gets a static singleton (no dynamic memory allocation) of the PWM class.

◆ getPpiChannel()

nrf_ppi_channel_t PWM::getPpiChannel ( uint8_t  index)
private

Helper function to get the ppi channel, given the index.

◆ getPpiGroup()

nrf_ppi_channel_group_t PWM::getPpiGroup ( uint8_t  index)
private

Helper function to get the ppi group, given the index.

◆ getPpiTaskDisable()

nrf_ppi_task_t PWM::getPpiTaskDisable ( uint8_t  index)
private

Helper function to get the ppi disable task, given the group index.

◆ getPpiTaskEnable()

nrf_ppi_task_t PWM::getPpiTaskEnable ( uint8_t  index)
private

Helper function to get the ppi enable task, given the group index.

◆ getTimerChannel()

nrf_timer_cc_channel_t PWM::getTimerChannel ( uint8_t  index)
private

Helper function to get the timer channel, given the index.

◆ getValue()

uint8_t PWM::getValue ( uint8_t  channel)

Get current value of a specific channel.

◆ gpioOff()

void PWM::gpioOff ( uint8_t  channel)
private

Turn off with gpio.

◆ gpioOn()

void PWM::gpioOn ( uint8_t  channel)
private

Turn on with gpio.

◆ gpioteConfig()

void PWM::gpioteConfig ( uint8_t  channel,
bool  initOn 
)
private

Config gpiote.

◆ gpioteDisable()

void PWM::gpioteDisable ( uint8_t  channel)
private

Disable gpiote.

◆ gpioteEnable()

void PWM::gpioteEnable ( uint8_t  channel)
private

Enable gpiote.

◆ gpioteForce()

void PWM::gpioteForce ( uint8_t  channel,
bool  initOn 
)
private

Force gpiote state.

◆ gpioteUnconfig()

void PWM::gpioteUnconfig ( uint8_t  channel)
private

Unconfig gpiote.

◆ init()

uint32_t PWM::init ( const pwm_config_t config)

Initialize the PWM settings.

config can be safely deleted after calling this function.

◆ initChannel()

uint32_t PWM::initChannel ( uint8_t  index,
pwm_channel_config_t config 
)
private

Init a channel.

◆ onPeriodEnd()

void PWM::onPeriodEnd ( )

Function to be called on the end of the PWM period.

Decoupled from interrupt.

◆ onZeroCrossingInterrupt()

void PWM::onZeroCrossingInterrupt ( )

Function to be called on a zero crossing interrupt.

◆ operator=()

void PWM::operator= ( PWM const &  )
private

Private PWM copy assignment definition.

◆ readCC()

uint32_t PWM::readCC ( uint8_t  channelIdx)
private

Read CC of timer.

◆ setValue() [1/2]

void PWM::setValue ( uint8_t  channel,
uint8_t  newValue 
)
private

Actually set the value in the peripheral.

◆ setValue() [2/2]

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'.

Parameters
[in]channelChannel to set.
[in]valueTarget value to set the channel to (0-100).
[in]speedSpeed at which to go to target value (1-100).

◆ start() [1/2]

void PWM::start ( )
private

Start the PWM timer, and mark PWM as started.

Can be called from zero crossing interrupt.

◆ start() [2/2]

uint32_t PWM::start ( bool  onZeroCrossing)

Start PWM.

Parameters
[in]onZeroCrossingSet to true to make it start at the first zero crossing.

◆ updateValues()

void PWM::updateValues ( )
private

Check if the current value with the target value.

If not equal, set the value.

◆ writeCC()

void PWM::writeCC ( uint8_t  channelIdx,
uint32_t  ticks 
)
private

Write CC of timer.

Member Data Documentation

◆ _adjustedMaxTickVal

uint32_t PWM::_adjustedMaxTickVal
private

Max value of channel, in ticks. Adjusted to sync with zero crossings.

◆ _config

pwm_config_t PWM::_config
private

Config of the PWM.

◆ _gpioteInitStatesOff

nrf_gpiote_outinit_t PWM::_gpioteInitStatesOff[CS_PWM_MAX_CHANNELS]
private

◆ _gpioteInitStatesOn

nrf_gpiote_outinit_t PWM::_gpioteInitStatesOn[CS_PWM_MAX_CHANNELS]
private

GPIOTE init states cache.

◆ _initialized

bool PWM::_initialized
private

Flag to indicate that the init function has been successfully performed.

◆ _isPwmEnabled

bool PWM::_isPwmEnabled[CS_PWM_MAX_CHANNELS]
private

Returns whether a channel is currently dimming (value > 0 and < max).

◆ _maxTickVal

uint32_t PWM::_maxTickVal
private

Max value of channel, in ticks. Set at init.

◆ _maxValue

const uint8_t PWM::_maxValue = 100
static

◆ _ppiChannelsOff

nrf_ppi_channel_t PWM::_ppiChannelsOff[CS_PWM_MAX_CHANNELS]
private

PPI channels to be used to trigger GPIOTE tasks from timer compare events. Turning the switch off.

◆ _ppiChannelsOn

nrf_ppi_channel_t PWM::_ppiChannelsOn[CS_PWM_MAX_CHANNELS]
private

PPI channels to be used to trigger GPIOTE tasks from timer compare events. Turning the switch on.

◆ _ppiTransitionChannel

nrf_ppi_channel_t PWM::_ppiTransitionChannel
private

PPI channel to be used for duty cycle transitions.

◆ _started

bool PWM::_started
private

Flag to indicate that the start functions has been successfully performed.

◆ _startOnZeroCrossing

bool PWM::_startOnZeroCrossing
private

Flag to indicate whether to wait for a zero crossing to start.

◆ _stepSize

uint8_t PWM::_stepSize[CS_PWM_MAX_CHANNELS] = {0}
private

Step size to move actual value towards target value.

◆ _targetValues

uint8_t PWM::_targetValues[CS_PWM_MAX_CHANNELS] = {0}
private

Target duty cycle values of the channels in percentage.

◆ _tickValues

uint32_t PWM::_tickValues[CS_PWM_MAX_CHANNELS] = {0}
private

Duty cycle values of the channels in ticks.

◆ _updateValuesCountdown

uint8_t PWM::_updateValuesCountdown = 0
private

Current number of periods to wait before next value update.

◆ _values

uint8_t PWM::_values[CS_PWM_MAX_CHANNELS] = {0}
private

Duty cycle values of the channels in percentage.

◆ _zeroCrossDeviationIntegral

int64_t PWM::_zeroCrossDeviationIntegral
private

Integral of the tick deviations.

◆ _zeroCrossingCounter

uint32_t PWM::_zeroCrossingCounter
private

Counter to keep up the number of zero crossing callbacks.

◆ _zeroCrossTicksDeviationAvg

int64_t PWM::_zeroCrossTicksDeviationAvg
private

Moving average amount of timer ticks deviation compared to when the zero crossing callback was called.

◆ numPeriodsBeforeValueUpdate

const uint8_t PWM::numPeriodsBeforeValueUpdate = 3
staticprivate

Only update values every so many PWM periods.


The documentation for this class was generated from the following file: