3

how to stop pwm and set pin to clear

wuvo.leo gravatar image

asked 2015-06-12 08:27:00 +0200

Aryan gravatar image

updated 2015-06-12 08:47:50 +0200

Hi,all.i use pwm with sdk8.1 sample (SDK8.1\example\peripheral\pwm),my question is how to stop pwm and set the pin to low level? is my code correct? my pwm init code:


static uint32_t init_pwm(uint32_t perior,uint8_t pin)
 {   
        uint32_t err_code = 0; 
    /* 2-channel PWM, 200Hz, output on DK LED pins. */
      app_pwm_config_t pwm1_cfg = APP_PWM_DEFAULT_CONFIG_1CH(perior, pin);

     pwm1_cfg.pin_polarity[0] = APP_PWM_POLARITY_ACTIVE_LOW;

    /* Initialize and enable PWM. */
    err_code = app_pwm_init(&PWM1,&pwm1_cfg,pwm_ready_callback);
    APP_ERROR_CHECK(err_code);

    return err_code;
}
------------------------------------------------------------------------------

pwm stop code:
void pwm_stop( void )
{
   app_pwm_disable(&PWM1);
}
----------------------------------------------------------------------------

pwm start code:
void pwm_start( void )
{   
   app_pwm_enable(&PWM1);
}
edit retag flag offensive close delete report spam

Comments

Using SDK 9, SD110, app_pwm driver, also app_scheduler, I am also trying to find an answer/workaround to the very same problem after trying unsuccessfully the solutions in this thread. So far I can't disable a pin left high by the PWM, using the app_pwm driver. The typical period I need is 250 us. I also tried setting duty to 0% first before disabling, but this did not help, hinting at an inversion from the toggle. I am hoping for a successful workaround before considering rewriting a PWM driver. Any news Nordic, Mahesh? Thanks!

Mike ( 2015-09-09 02:42:37 +0200 )editconvert to answer

4 answers

Sort by » oldest newest most voted
3
Aryan gravatar image

answered 2015-06-12 09:40:21 +0200

updated 2015-08-10 10:25:28 +0200

Hi wuvo, I have noticed that app_pwm library does not disable a GPIOTE config, which means that it does not free the pin from gpiote after you are done. I created a ticket for it for the team.

For your code you can do this.

1) before initializing pwm library, you configure the pin through gpio interface

nrf_gpio_pin_dir_set(pin_number, NRF_GPIO_PIN_DIR_OUTPUT);
nrf_gpio_pin_clear(pin_number);

2) Then you initialize PWM using app_pwm library, do your stuff

3) in your pwm_stop add three lines to it

void pwm_stop( void )
{
   app_pwm_disable(&PWM1);
   nrf_drv_gpiote_out_task_disable(pin_number);
   nrf_gpio_cfg_output(pin_number)
   nrf_gpio_pin_clear(pin_number);
}

Now as soon as the pin is freed by nrf_drv_gpiote_out_task_disable then gpio configuration will take into effect.

EDIT: 10.08.2015

PAN-73 workaround is enabled by default in app_pwm_init

PAN-73 workaround is disabled in app_pwm_uninit

This means there will be increase in power consumption (~50uA) if the PAN-72 workaround is enabled.

Check here

edit flag offensive delete publish link more

Comments

Thanks! And after stop pwm,how to restart it?

wuvo.leo ( 2015-06-12 14:06:00 +0200 )editconvert to answer

void pwm_start( void ) {
nrf_drv_gpiote_out_task_enable(pin_number); app_pwm_enable(&PWM1); }

Aryan ( 2015-06-12 14:25:27 +0200 )editconvert to answer

That should have worked, If this does not work then it means that somewhere GPIOTE still owns the pin.

nrf_drv_gpiote_out_task_disable(SPEAKER_PIN);
nrf_gpio_cfg_output(SPEAKER_PIN);
nrf_gpio_pin_clear(SPEAKER_PIN);
Aryan ( 2015-06-12 17:52:24 +0200 )editconvert to answer
1

Thanks Aryan! i solved this problem as what you said.

wuvo.leo ( 2015-06-24 10:36:01 +0200 )editconvert to answer

Just want to mention that this is still a problem with 9.0.0.

Mahesh Venkitachalam ( 2015-07-24 17:42:58 +0200 )editconvert to answer

Yes, still unresolved.

Aryan ( 2015-07-31 08:01:28 +0200 )editconvert to answer

Thanks,Aryan! i found a new problem:after i stop pwm and uninit pwm,my device keep current about 300uA,and can't go back to start pwm before.

wuvo.leo ( 2015-08-02 06:03:26 +0200 )editconvert to answer

I did not really understand what you mean by "can't go back to start pwm before". Is this problem related to the fix we described here?

Aryan ( 2015-08-02 13:15:16 +0200 )editconvert to answer

my device's current is about 7uA before call pwm_start(),and call it,current is about 300uA.

wuvo.leo ( 2015-08-03 03:32:54 +0200 )editconvert to answer

I will try it out today and see what we can do

Aryan ( 2015-08-03 11:25:33 +0200 )editconvert to answer

wuvo, i am not able to reproduce it, can you give me the code and the setup details on which you tried this.

Aryan ( 2015-08-03 12:27:22 +0200 )editconvert to answer

Thanks Aryan! How to upload my code here?

wuvo.leo ( 2015-08-09 05:39:24 +0200 )editconvert to answer

I suggest creating a github gist and posting a link.

Mahesh Venkitachalam ( 2015-08-09 05:44:06 +0200 )editconvert to answer

Sorry,what's mean "PAN-73"?

wuvo.leo ( 2015-08-11 11:01:26 +0200 )editconvert to answer

73. TIMER

Use of an EVENT from any TIMER module to trigger a TASK in GPIOTE or RTC using the PPI could fail under, But this workaround is enabled by default app_pwm. And it can be found here certain conditions.

Aryan ( 2015-08-11 11:53:29 +0200 )editconvert to answer

this library is a piece

Lucas ( 2015-08-13 03:20:13 +0200 )editconvert to answer
0
wuvo.leo gravatar image

answered 2015-06-12 15:27:28 +0200

joice_markose gravatar image

updated 2017-07-27 08:17:49 +0200

Thanks,Aryan.i tested as what your say,but the pin is high level always.no effects.i want to controll buzzer used pwm.what's wrong?i reconfig pwm before i start(restart) pwm using code as follow:

-------------------------------------------------------------------------
uint32_t reconfig_pwm(uint32_t perior,uint8_t pin)
{
    uint32_t err_code = 0;

    err_code = app_pwm_uninit(&PWM1);   
    APP_ERROR_CHECK(err_code);

    if( err_code == NRF_SUCCESS)
    {
      err_code =    init_pwm(perior,pin);
        APP_ERROR_CHECK(err_code);
    }

    //while (app_pwm_channel_duty_set(&PWM1, 0, 50) == NRF_ERROR_BUSY);

    return err_code;

}

static uint32_t init_pwm(uint32_t perior,uint8_t pin)
 {   
        uint32_t err_code = 0;

        nrf_gpio_pin_dir_set(pin, NRF_GPIO_PIN_DIR_OUTPUT);
        nrf_gpio_pin_clear(pin);

    /* 2-channel PWM, 200Hz, output on DK LED pins. */
      app_pwm_config_t pwm1_cfg = APP_PWM_DEFAULT_CONFIG_1CH(perior, pin);

     pwm1_cfg.pin_polarity[0] = APP_PWM_POLARITY_ACTIVE_LOW;

    /* Initialize and enable PWM. */
    err_code = app_pwm_init(&PWM1,&pwm1_cfg,pwm_ready_callback);
    APP_ERROR_CHECK(err_code);

    return err_code;
}
------------------------------------------------------------------------------------------------
then,start it:
void pwm_start( void )
{   
   app_pwm_enable(&PWM1);
     while (app_pwm_channel_duty_set(&PWM1, 0, 50) == NRF_ERROR_BUSY);
}
-----------------------------------------------------------------------------------------------
stop as this:
void pwm_stop( void )
{ 
      app_pwm_disable(&PWM1);
      nrf_drv_gpiote_out_task_disable(SPEAKER_PIN);
          nrf_gpio_pin_clear(SPEAKER_PIN);

//    while (app_pwm_channel_duty_set(&PWM1, 0, 0) == NRF_ERROR_BUSY);
      pos = 0;
      time = 0;
      k = 0;
      freq = 0;
}
edit flag offensive delete publish link more
0
wuvo.leo gravatar image

answered 2015-08-03 03:30:49 +0200

Milk gravatar image

updated 2015-11-24 09:02:17 +0200

The device's current can't decrease after call pwm_start() even if call pwm_stop().

edit flag offensive delete publish link more

Comments

I also met this situation by SDK9.0,still do not know how to solve this problem.

Milk ( 2015-11-24 09:03:58 +0200 )editconvert to answer

did you try using sdk10?

Aryan ( 2015-12-02 20:32:41 +0200 )editconvert to answer
0
Ali Shokri gravatar image

answered 2016-05-29 13:41:28 +0200

I face with this problem too. I'm using SDK11.0 :( after I start PWM but I can't stop it. I have try every single top steps but I didn't get any good result.

edit flag offensive delete publish link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer. Do not ask a new question or reply to an answer here.

[hide preview]

User menu

    or sign up

Recent questions

Question Tools

1 follower

Stats

Asked: 2015-06-12 08:27:00 +0200

Seen: 1,904 times

Last updated: May 29 '16