Skip to content

Commit

Permalink
[nrf fromtree] 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 <krzysztof.chruscinski@nordicsemi.no>
(cherry picked from commit c3a33cf)
  • Loading branch information
nordic-krch committed Oct 21, 2024
1 parent b0c042a commit e578365
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions drivers/pwm/pwm_nrfx.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,13 @@ LOG_MODULE_REGISTER(pwm_nrfx, CONFIG_PWM_LOG_LEVEL);
* to 0 or 1, hence the use of #if IS_ENABLED().
*/
#if IS_ENABLED(NRFX_PWM_NRF52_ANOMALY_109_WORKAROUND_ENABLED)
#define ANOMALY_109_IRQ_CONNECT(...) IRQ_CONNECT(__VA_ARGS__)
#define ANOMALY_109_EGU_IRQ_CONNECT(idx) _EGU_IRQ_CONNECT(idx)
#define _EGU_IRQ_CONNECT(idx) \
extern void nrfx_egu_##idx##_irq_handler(void); \
IRQ_CONNECT(DT_IRQN(DT_NODELABEL(egu##idx)), \
DT_IRQ(DT_NODELABEL(egu##idx), priority), \
nrfx_isr, nrfx_egu_##idx##_irq_handler, 0)
#else
#define ANOMALY_109_IRQ_CONNECT(...)
#define ANOMALY_109_EGU_IRQ_CONNECT(idx)
#endif

Expand Down Expand Up @@ -63,6 +61,12 @@ 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)
{
ARG_UNUSED(event_type);
ARG_UNUSED(p_context);
}

static bool pwm_period_check_and_set(const struct device *dev,
uint32_t channel, uint32_t period_cycles)
{
Expand Down Expand Up @@ -229,7 +233,8 @@ static int pwm_nrfx_set_cycles(const struct device *dev, uint32_t channel,
* 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);
nrfx_pwm_simple_playback(&config->pwm, &config->seq, 1,
NRFX_PWM_FLAG_NO_EVT_FINISHED);
}

return 0;
Expand All @@ -256,6 +261,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 +290,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 +380,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 e578365

Please sign in to comment.