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



  • Hello Karl,

    Please try to increase the bit depth to 32 again when only using the nrfx driver, with all the legacy TIMER definitions still commented out or removed from the sdk_config. Please do this, and let me know if you then see the constant 98 µs behavior change

    I tried with bit depth 32 and only enabling nrfx_driver ,but still the same issue ..getting constant around 98 µs behaviour.

    here I am attaching my sdk_config.h

    Best regards,
    Ram

  • RAM_MS said:

    I tried with bit depth 32 and only enabling nrfx_driver ,but still the same issue ..getting constant around 98 µs behaviour.

    here I am attaching my sdk_config.h

    Interesting, thank you for checking and confirming this.

    It seems very strange to me that the TIMER is not triggering at the specified interval.
    Could you .zip and send me your entire project as it currently is - using the nrfx_timer driver, and getting the 98 µs CC events - so I may look over the code to see if I perhaps can spot something?

    Best regards,
    Karl

  • Hello Karl,

    Thanks for your valuable guide.

    t seems very strange to me that the TIMER is not triggering at the specified interval.
    Could you .zip and send me your entire project as it currently is - using the nrfx_timer driver, and getting the 98 µs CC events - so I may look over the code to see if I perhaps can spot something?

    Here I am attaching the .zip file of project.

    Best regards,
    Ram

  • Hello Karl,

                    I am waiting for your support .

    Best regards,
    Ram

  • Hello,

    Thank you for your patience with this.
    Could you possibly provide the project as a .zip file, instead of a .7z?
    I am unable to open the supplied file - I'll take a look at it as soon as I get it.

    Best regards,
    Karl

Reply Children
Related