Skip to content

Commit

Permalink
drivers: pwm: nrfx: Disable PWM peripheral when not used
Browse files Browse the repository at this point in the history
Shim was not correctly disabling PWM when it was not used. Task
STOP was triggered but PWM->ENABLE remained set which caused
increased current. Added interrupt and enabled event handler in
the nrfx driver to allow disabling PWM on STOPPED event.

Signed-off-by: Krzysztof Chruściński <[email protected]>
  • Loading branch information
nordic-krch committed Sep 24, 2024
1 parent 119b238 commit 0aafdd5
Showing 1 changed file with 28 additions and 21 deletions.
49 changes: 28 additions & 21 deletions drivers/pwm/pwm_nrfx.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ struct pwm_nrfx_data {
/* Bit mask indicating channels that need the PWM generation. */
uint8_t pwm_needed;
uint8_t prescaler;
bool stop_requested;
volatile bool stop_requested;
volatile bool active;
};
/* Ensure the pwm_needed bit mask can accommodate all available channels. */
#if (NRF_PWM_CHANNEL_COUNT > 8)
Expand All @@ -63,6 +64,15 @@ static uint16_t *seq_values_ptr_get(const struct device *dev)
return (uint16_t *)config->seq.values.p_raw;
}

static void pwm_handler(nrfx_pwm_evt_type_t event_type, void *p_context)
{
__ASSERT_NO_MSG(event_type == NRFX_PWM_EVT_STOPPED);
struct pwm_nrfx_data *data = p_context;

data->active = false;
data->stop_requested = false;
}

static bool pwm_period_check_and_set(const struct device *dev,
uint32_t channel, uint32_t period_cycles)
{
Expand Down Expand Up @@ -209,27 +219,27 @@ static int pwm_nrfx_set_cycles(const struct device *dev, uint32_t channel,
/* Don't wait here for the peripheral to actually stop. Instead,
* ensure it is stopped before starting the next playback.
*/
nrfx_pwm_stop(&config->pwm, false);
data->stop_requested = true;
if (!data->stop_requested) {
data->stop_requested = true;
nrfx_pwm_stop(&config->pwm, false);
}
} else {
if (data->stop_requested) {
data->stop_requested = false;

/* After a stop is requested, the PWM peripheral stops
* pulse generation at the end of the current period,
* and till that moment, it ignores any start requests,
* so ensure here that it is stopped.
*/
while (!nrfx_pwm_stopped_check(&config->pwm)) {
}
/* After a stop is requested, the PWM peripheral stops
* pulse generation at the end of the current period,
* and till that moment, it ignores any start requests,
* so ensure here that it is stopped.
*/
while (data->stop_requested) {
}

/* It is sufficient to play the sequence once without looping.
* The PWM generation will continue with the loaded values
* until another playback is requested (new values will be
* loaded then) or the PWM peripheral is stopped.
*/
nrfx_pwm_simple_playback(&config->pwm, &config->seq, 1, 0);
data->active = true;
nrfx_pwm_simple_playback(&config->pwm, &config->seq, 1,
NRFX_PWM_FLAG_NO_EVT_FINISHED);
}

return 0;
Expand All @@ -256,6 +266,7 @@ static int pwm_nrfx_init(const struct device *dev)
{
const struct pwm_nrfx_config *config = dev->config;
uint8_t initially_inverted = 0;
nrfx_err_t result;

int ret = pinctrl_apply_state(config->pcfg, PINCTRL_STATE_DEFAULT);

Expand Down Expand Up @@ -284,10 +295,7 @@ static int pwm_nrfx_init(const struct device *dev)
seq_values_ptr_get(dev)[i] = PWM_NRFX_CH_VALUE(0, inverted);
}

nrfx_err_t result = nrfx_pwm_init(&config->pwm,
&config->initial_config,
NULL,
NULL);
result = nrfx_pwm_init(&config->pwm, &config->initial_config, pwm_handler, dev->data);
if (result != NRFX_SUCCESS) {
LOG_ERR("Failed to initialize device: %s", dev->name);
return -EBUSY;
Expand Down Expand Up @@ -377,9 +385,8 @@ static int pwm_nrfx_pm_action(const struct device *dev,
}; \
static int pwm_nrfx_init##idx(const struct device *dev) \
{ \
ANOMALY_109_IRQ_CONNECT( \
DT_IRQN(PWM(idx)), DT_IRQ(PWM(idx), priority), \
nrfx_isr, nrfx_pwm_##idx##_irq_handler, 0); \
IRQ_CONNECT(DT_IRQN(PWM(idx)), DT_IRQ(PWM(idx), priority), \
nrfx_isr, nrfx_pwm_##idx##_irq_handler, 0); \
return pwm_nrfx_init(dev); \
}; \
PM_DEVICE_DT_DEFINE(PWM(idx), pwm_nrfx_pm_action); \
Expand Down

0 comments on commit 0aafdd5

Please sign in to comment.