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

Proper sequence to initialize PWM?

I'm having difficulty with using PWM library with softdevice. The app_pwm_init function returns

NRF_SUCCESS but call to app_pwm_enable does not generate an output.

//PWM sound generation
APP_PWM_INSTANCE(PWMpiezo,1);

void pwm_ready_callback(uint32_t pwm_id)    // PWM callback function
{

}

void UI_pwm_init()
{
    ret_code_t err_code;

    app_pwm_config_t piezo_config = APP_PWM_DEFAULT_CONFIG_1CH(250UL,PIEZO); //4KHz 
    // Initialize the PWM library
    err_code = app_pwm_init(&PWMpiezo,&piezo_config,pwm_ready_callback);
    if(err_code == NRF_SUCCESS)
    {
	app_pwm_enable(&PWMpiezo);
     }
}

Documentation doesn't detail where init should take place but an answer to a question here says it should be before the ble_stack_init. Are there other restrictions on where it should be done?

Or do I have an error in the code that I can't see? Thanks! Neil

  • Hi nszmnsky

    Where does it say that it should be before ble_stack_init? I do not think that there is such dependency. you code works fine when i tried it here. Make sure you are sampling the right pin

  • The before ble_stack_init comment was in "Pwm and softdevice on nRF5822" but on re-reading saw later comment from submitter that said after - after everything in fact. So is there a dependency/conflict that I'm not aware of?

  • It's not mentioned in the documentation but PWM should be initialized after SoftDevice. The thing is that PWM is using PPI driver which if softdevice is present access PPI through softdevice. It's recommended to initialize softdevice as one of the first things in the main().

  • nszmnsky, This dependency is there. I missed to see it. Krzysztof agreed to investigate if it is needed and probably remove the dependency in the future. I think this case can rest until then as he knows most about it.

  • Thanks for the update. However even moving pwm_init to end of main after all other inits doesn't seem to work. I've been trying to check out peripheral register values between the PWM example (modified to match my settings) and my app. I'm looking at GPIO, GPIOTE, PPI, & TIMER2. So far I'm not seeing a difference but I'm doing it manually by writing values seen in the two apps so may not be reliable.

1 2