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);

    
}

  • Hello again, ram

    RAM_MS said:

    AND after commenting legacy definations, this error message coming during building the project.

    #error "No enabled TIMER instances. Check <nrfx_config.h>."

    Thank you for elaborating. The no TIMER instance error means that you have to make sure that the timer instance you are trying to use is enabled in sdk_config. I see from your code that you have not defined any of the NRFX_TIMER*_ENABLED definitions to 1.
    Please define NRFX_TIMER0_ENABLED 1 if you intend to use the TIMER0 instance. Please also note that TIMER0 is blocked by the SoftDevice - I just mention this in case you intend to add the SoftDevice to your project down the line.

    RAM_MS said:
    As my pwm algorithm working with legacy defines ,So its better to remove nrfx_timer_driver .
    RAM_MS said:
    So if you guide me to generate delay from legacy timer then it may be immediate solution for my issue as per my understanding .

    I would advice you to give it one more try with the nrfx driver, since it did not compile when you had no NRFX_TIMER instances enabled.

    RAM_MS said:
    yes ,I tried but not see any difference.

    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.

    Best regards,
    Karl

  • 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

    85253.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.pwm_timer_based - Copy.7z

    Best regards,
    Ram

  • Hello Karl,

                    I am waiting for your support .

    Best regards,
    Ram

Related