This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

PWM Driver problems

I am having problem with the PWM driver.

I need PWM for a very simple application where I need it to drive a buzzer with a series of 50% duty cycle pulses.

Trying to set it up gives me problems with the documentation level, so I am trying to guess the functions of the pwm and experiment with the values. 

I initialize with the following:

#define SCALED_RANGE    366      // 1 MHz scaled to 2.730 Hz
#define DUTY_CYCLE_50   (SCALED_RANGE/2)

void Initialize(void)
{
   nrfx_pwm_config_t const config0 = //NRFX_PWM_DEFAULT_CONFIG;
   {
    .output_pins  = { BUZZER_PIN,                                          \
                      NRFX_PWM_PIN_NOT_USED,                               \
                      NRFX_PWM_PIN_NOT_USED,                               \
                      NRFX_PWM_PIN_NOT_USED },                             \
    .irq_priority = NRFX_PWM_DEFAULT_CONFIG_IRQ_PRIORITY,                  \
    .base_clock   = (nrf_pwm_clk_t)NRFX_PWM_DEFAULT_CONFIG_BASE_CLOCK,     \
    .count_mode   = (nrf_pwm_mode_t)NRFX_PWM_DEFAULT_CONFIG_COUNT_MODE,    \
    .top_value    = SCALED_RANGE,                                          \
    .load_mode    = NRF_PWM_LOAD_COMMON, // (nrf_pwm_dec_load_t)NRFX_PWM_DEFAULT_CONFIG_LOAD_MODE, \
    .step_mode    = (nrf_pwm_dec_step_t)NRFX_PWM_DEFAULT_CONFIG_STEP_MODE
   };

   // Init PWM without error handler
   // returns nrfx_err_t 
   APP_ERROR_CHECK(nrfx_pwm_init(&m_pwm0, &config0, PwmEventHandler));
   }

But when using it, I do not understand repeat and loop counts values.

First if I set the repeats to 100

nrf_pwm_values_individual_t seq_values[] = {DUTY_CYCLE_50, DUTY_CYCLE_50, DUTY_CYCLE_50, DUTY_CYCLE_50};
nrf_pwm_sequence_t const seq =
{
    .values.p_individual = seq_values,
    .length          = 1, //NRF_PWM_VALUES_LENGTH(seq_values),
    .repeats         = 100,
    .end_delay       = 0
};

And call 

      nrfx_pwm_simple_playback(&m_pwm0, &seq, 1, NRFX_PWM_FLAG_STOP | NRFX_PWM_FLAG_NO_EVT_FINISHED);

I would expect the PWM to generate 100 pulses with 50% duty cycle.

This does not happen.

Only if I use a loop counter > 1 it seems that the repeats are being used.

So I would expect with a loop of 2 and a repeat of 100, I would get 200 pulses, but again no....

Documentation lacking information about the use of these values!

Anyway without fully understanding the values, I can get what I want by experimenting.

Now the second problem.....

With the simple code above, my applications reboots when trying to run the playback a second time. I will run the first time, but at second attemt the processor reboots (not hitting error handler).

I have been searching documentation, but no clues. Looking for documentation if I should stop it in any way, also a check using nrfx_pwm_is_stopped does not help...

Related