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

Parents
  • Important information, the error described (processor reset) occurs when running with a SoftDevice (S140).

    After writing the initial post I have tried to make a PWM project without a SoftDevice, mainly to try to debug. In this case the code above runs without error. So it seems that it is related to the SoftDevice. In the original code the playback is initiated by an event from the bsp app_button layer.

    Unfortunately this also means that I am unable to debug the problem as the SoftDevice crashes into app_error_fault_handler as soon as I try to debug.

Reply
  • Important information, the error described (processor reset) occurs when running with a SoftDevice (S140).

    After writing the initial post I have tried to make a PWM project without a SoftDevice, mainly to try to debug. In this case the code above runs without error. So it seems that it is related to the SoftDevice. In the original code the playback is initiated by an event from the bsp app_button layer.

    Unfortunately this also means that I am unable to debug the problem as the SoftDevice crashes into app_error_fault_handler as soon as I try to debug.

Children
Related