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

Reply
  • 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

Children
  • 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



  • Hello Ram,

    I saw your in_pin_handler function before, but I still do not see where / if it is ever being called.
    Please show me the section of your code in which you initialize your GPIOTE and provide the in_pin_handler as its callback.
    Could you also confirm that you ever enter into the in_pin_handler function as expected?

    RAM_MS said:
    I am not getting the delay I am expecting.

    Please elaborate at much as possible. The more specific you are, the easier it will be for us to help you resolve your issue.
    So you are getting a delay, but it is not what you expect? From your code I assume that you are trying to see a 500 µs delay. What delay are you currently seeing?

    Best regards,
    Karl

  • Hello Karl,

    I saw your in_pin_handler function before, but I still do not see where / if it is ever being called.
    Please show me the section of your code in which you initialize your GPIOTE and provide the in_pin_handler as its callback.
    Could you also confirm that you ever enter into the in_pin_handler function as expected?

    #define    PIN_IN    22
    
    static void gpio_init(void)
    {
        ret_code_t err_code;
    
        err_code = nrf_drv_gpiote_init();
        APP_ERROR_CHECK(err_code);
    
        nrf_drv_gpiote_in_config_t in_config = GPIOTE_CONFIG_IN_SENSE_LOTOHI(true);
        in_config.pull = NRF_GPIO_PIN_PULLDOWN;
    
        err_code = nrf_drv_gpiote_in_init(PIN_IN, &in_config, in_pin_handler);
        APP_ERROR_CHECK(err_code);
    
        nrf_drv_gpiote_in_event_enable(PIN_IN, true);
    }

    Could you also confirm that you ever enter into the in_pin_handler function as expected?

    Yes ,my in_pin_handler function working as expected. and its not a button like interrupt but it is a square wave signal interrupt with 20ms period and 50% duty cycle.  

    So you are getting a delay, but it is not what you expect? From your code I assume that you are trying to see a 500 µs delay. What delay are you currently seeing?

    Yes, I am getting a delay but which is a random value around 98 µs irrespective of my input .

  • RAM_MS said:
    Yes ,my in_pin_handler function working as expected. and its not a button like interrupt but it is a square wave signal interrupt with 20ms period and 50% duty cycle.  
    RAM_MS said:
    Yes, I am getting a delay but which is a random value around 98 µs irrespective of my input .

    I see, thank you for clarifying.
    It seems very strange to me that you do not see any difference when you change the time_us variable.

    Could I also ask why you are using an external periodic signal to enable the Timer? Is this placeholder functionality for something you will implement later? Is the gpio_flag being set every 10 ms?

    It would also be helpful if you could share with me your sdk_config.h file, so I may take a look at your GPIOTE and TIMER configuration.

    Best regards,
    Karl

  • Could I also ask why you are using an external periodic signal to enable the Timer? Is this placeholder functionality for something you will implement later?

    I want to follow external periodic signal with some user defined delay that why I am starting timer from interrupt.

    My aim is here to create a same periodic signal of 50% duty cycle and 20ms period but with user defined delay(that is vary from 0 to 10ms ).

    Is the gpio_flag being set every 10 ms?

    For now I configured for 20ms but definitely aim is to get in every 10ms.

    It would also be helpful if you could share with me your sdk_config.h file, so I may take a look at your GPIOTE and TIMER configuration.

    3718.sdk_config.h

    Best regards,
    Ram

Related