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

Pulsewidth modulation with app_pwm results in NRF_ERROR_INVALID_STATE

I have a nrf51-DK. I am trying to get a PWM signal on one pin. Here is my code:

static volatile bool ready_flag;
void pwm_ready_callback(uint32_t pwm_id){ ready_flag = true; }      // PWM callback function

void test_pwm(){
    APP_PWM_INSTANCE(PWM1,2);                   // Create the instance "PWM1" using TIMER2.
    ret_code_t err_code;
    app_pwm_config_t pwm1_cfg = APP_PWM_DEFAULT_CONFIG_1CH(5000L, 17);
    pwm1_cfg.pin_polarity[0] = APP_PWM_POLARITY_ACTIVE_HIGH;
    err_code = app_pwm_init(&PWM1,&pwm1_cfg,pwm_ready_callback);
    APP_ERROR_CHECK(err_code);
    app_pwm_enable(&PWM1);
    app_pwm_channel_duty_set(&PWM1, 0, 50);
}

The error code returned by init is 8 NRF_ERROR_INVALID_STATE. It happens with whatever TimerX (0,1,2) I use. I am not using a SoftDevice, nor am I using the Timers in another part of the code (except one SPI part, which is commented out atm).

What can I do to further debug the issue?

Parents
  • Hi,

    The function app_pwm_init() will return NRF_ERROR_INVALID_STATE if the timer/PWM is already in use or if initialization failed. Are you calling the function app_pwm_init() more than once?

  • #include "variables.h" #include "DAC.h" #include "DDC.h" #include "boards.h" #include "sdk_config.h" #include "setup.h" #include "nrf_gpio.h" #include "nrf_log.h" #include "nrf_delay.h"

    int main(void){
        setup();
        DDC m;
        m.test_pwm();
        return 0;
    }
    

    DDC is a class which (in the future) controls a current measurement chip (this is what I need the periodic signal for). At the moment everything in that class is commented out except for some LED control which helps me debug:

    void DDC::set_success(int err){
        if (err == NRF_SUCCESS){
            nrf_gpio_pin_write(18, HIGH);
            nrf_delay_ms(500);
        } else {
            nrf_gpio_pin_write(19, HIGH);
            nrf_delay_ms(500);
          }
      }
    
Reply
  • #include "variables.h" #include "DAC.h" #include "DDC.h" #include "boards.h" #include "sdk_config.h" #include "setup.h" #include "nrf_gpio.h" #include "nrf_log.h" #include "nrf_delay.h"

    int main(void){
        setup();
        DDC m;
        m.test_pwm();
        return 0;
    }
    

    DDC is a class which (in the future) controls a current measurement chip (this is what I need the periodic signal for). At the moment everything in that class is commented out except for some LED control which helps me debug:

    void DDC::set_success(int err){
        if (err == NRF_SUCCESS){
            nrf_gpio_pin_write(18, HIGH);
            nrf_delay_ms(500);
        } else {
            nrf_gpio_pin_write(19, HIGH);
            nrf_delay_ms(500);
          }
      }
    
Children
No Data
Related