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

How to start pwm sequence after particular time delay of gpio event?

I am working on an application where I need to start pwm sequence after particular delay (accurate delay of multiple of 100us).

I am quite new to nordic environment.

Here is my code . Can please guide me what is wrong here??

#define    Period    20000 //20ms

const nrf_drv_timer_t TIMER_LED = NRF_DRV_TIMER_INSTANCE(0);

APP_PWM_INSTANCE(PWM1,1); 

static int gpio_flag =0;

void pwm_update(void)
{
      uint32_t value;
      value = duty_1;
      ready_flag = false;
      /* Set the duty cycle - keep trying until PWM is ready... */
      
      

      while ((app_pwm_channel_duty_set(&PWM1, 0, value))&(app_pwm_channel_duty_set(&PWM1, 1, value)) == NRF_ERROR_BUSY);
 
      /* ... or wait for callback. */
      while (!ready_flag);
      APP_ERROR_CHECK(app_pwm_channel_duty_set(&PWM1, 1, value));
      APP_ERROR_CHECK(app_pwm_channel_duty_set(&PWM1, 0, value));

}


void in_pin_handler(nrf_drv_gpiote_pin_t pin, nrf_gpiote_polarity_t action)
{
   gpio_flag =1;
   nrf_drv_timer_enable(&TIMER_LED);

   
}

void timer0_handler(nrf_timer_event_t event_type, void* p_context)
{
  switch (event_type)
    {
        case NRF_TIMER_EVENT_COMPARE0:
             timer_flg =1;
             nrf_drv_timer_clear(&TIMER_LED);
             gpio_flag=0;
             pwm_update();
            break;

        default:
            //Do nothing.
            break;
    }
}


void timer_init(void)
{
    uint32_t time_us = 500; 
    uint32_t time_ticks;
    uint32_t err_code = NRF_SUCCESS;

    nrf_drv_timer_config_t timer_cfg = NRF_DRV_TIMER_DEFAULT_CONFIG;
    err_code = nrf_drv_timer_init(&TIMER_LED, &timer_cfg, timer0_handler);
    APP_ERROR_CHECK(err_code);

    time_ticks = nrf_drv_timer_us_to_ticks(&TIMER_LED, time_us);
    nrf_drv_timer_compare(
         &TIMER_LED, NRF_TIMER_CC_CHANNEL0, time_ticks, true);

    
}

Parents
  • Hello,

    I am quite new to nordic environment.

    Welcome! Please do not hesitate to ask if you should have any questions.

    I am working on an application where I need to start pwm sequence after particular delay (accurate delay of multiple of 100us).

    Could you show me the entirety of your main.c code?
    Right now I do not see where you setup your gpio for triggering the enabling of the timer, so I am not sure that the timer is ever started, for instance.

    Could you also elaborate on what behavior you are seeing, and how this differs from what you would expect? Does the device reset for example, or is it just that 'nothing' is happening, for example?

    Best regards,
    Karl

  • Hello Karl,

    Right now I do not see where you setup your gpio for triggering the enabling of the timer, so I am not sure that the timer is ever started, for instance.

    void in_pin_handler(nrf_drv_gpiote_pin_t pin, nrf_gpiote_polarity_t action)
    {
       gpio_flag =1;
       nrf_drv_timer_enable(&TIMER_LED);
    
       
    }

    Could you also elaborate on what behavior you are seeing, and how this differs from what you would expect? Does the device reset for example, or is it just that 'nothing' is happening, for example?

    I am not getting the delay I am expecting.

    I think I have done some wrong configuration in linking to timer from gpio interrupt routine. 

    Best regards,
    Ram



  • Thank you for providing the .zip file.

    The first thing I notice in your code is that you are both making use of the drivers / libraries, and manipulating the registers directly - you should only ever do one of the two, since direct modifying of registers while also using the drivers can break them or force them into invalid states with undefined behavior.

    I will try to replicate the behavior you describe on my end with the constant 98 µs CC events - which SDK version are you using?

    Best regards,
    Karl

  • Hello Karl,

    The first thing I notice in your code is that you are both making use of the drivers / libraries, and manipulating the registers directly - you should only ever do one of the two, since direct modifying of registers while also using the drivers can break them or force them into invalid states with undefined behavior.

     Okay .As I am quite new to Nordic environment ,so take multiple Nordic sdk example  into account to create my application.

     You can also suggest me ,how to implement this as complete scenario is known to you( I am expecting ).

    I will try to replicate the behavior you describe on my end with the constant 98 µs CC events - which SDK version are you using?

    I am using SDK 17.02 version.

    Please guide me to resolve this.

    Best regards,
    Ram

  • Hello Karl,

                   Waiting for your valuable inputs.

     

    Thanks & regards,
    Ram

  • Hello Karl,

                   Waiting for your response.

     

    Thanks & regards,
    Ram

  • Hello again, Ram

    Thank you for your continued patience with this. I am sorry that it has taken so long to get back to you on this.

    RAM_MS said:
     Okay .As I am quite new to Nordic environment ,so take multiple Nordic sdk example  into account to create my application.

    Welcome!
    Absolutely, merging different examples could absolutely be a challenge if ones not already familiar with the SDK and how it works.

    RAM_MS said:
    You can also suggest me ,how to implement this as complete scenario is known to you( I am expecting ).

    Yes, sure, I'll take a look at this today. I'll update you by the end of day.

    Best regards,
    Karl

Reply
  • Hello again, Ram

    Thank you for your continued patience with this. I am sorry that it has taken so long to get back to you on this.

    RAM_MS said:
     Okay .As I am quite new to Nordic environment ,so take multiple Nordic sdk example  into account to create my application.

    Welcome!
    Absolutely, merging different examples could absolutely be a challenge if ones not already familiar with the SDK and how it works.

    RAM_MS said:
    You can also suggest me ,how to implement this as complete scenario is known to you( I am expecting ).

    Yes, sure, I'll take a look at this today. I'll update you by the end of day.

    Best regards,
    Karl

Children
Related