Beware that this post is related to an SDK in maintenance mode
More Info: Consider nRF Connect SDK for new designs
This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts
This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

How to set duty cycle of two pwm channel in opposite polarity inside gpio interrupt handler?

I am quite new with nordic environment .

My application is to start 2 pwm instance in opposite polarity (i.e 1 high polarity and another is low polarity with same duty cycle) when got interrupt in gpio pin.

I am able to create the same by using PWM_Library example in SDK .

Here is my code 

#define    Period    20000 //20ms
#define    PIN_IN    22
#define    PWM_PIN  (11UL)

int duty_1= 20;

static volatile bool ready_flag;            
static int gpio_flag =0;

APP_PWM_INSTANCE(PWM1,1);                   
APP_PWM_INSTANCE(PWM2,2);

void pwm_ready_callback(uint32_t pwm_id)    // PWM callback function
{
    ready_flag = true;
}

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

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

static void pwm_pin_init(void)
{
 ret_code_t err_code;

    app_pwm_config_t pwm1_cfg = APP_PWM_DEFAULT_CONFIG_1CH(Period, BSP_LED_0);
    app_pwm_config_t pwm2_cfg = APP_PWM_DEFAULT_CONFIG_1CH(Period, BSP_LED_1);

    
    pwm1_cfg.pin_polarity [0]= APP_PWM_POLARITY_ACTIVE_HIGH;
    pwm2_cfg.pin_polarity [0]= APP_PWM_POLARITY_ACTIVE_LOW;


    
    err_code = app_pwm_init(&PWM1,&pwm1_cfg,pwm_ready_callback);

    APP_ERROR_CHECK(err_code);

    err_code = app_pwm_init(&PWM2,&pwm2_cfg,pwm_ready_callback);

    APP_ERROR_CHECK(err_code);
    app_pwm_enable(&PWM1);
    app_pwm_enable(&PWM2);

} 

int main(void)
{
 bsp_board_init(BSP_INIT_LEDS);

    gpio_init();

    pwm_pin_init();

    uint32_t value;

  while(true)
  {

    if(gpio_flag == 1)
    {
      value = duty_1;
      gpio_flag =0;

      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(&PWM2, 0, value)) == NRF_ERROR_BUSY);
 
      /* ... or wait for callback. */
      while (!ready_flag);
      APP_ERROR_CHECK(app_pwm_channel_duty_set(&PWM1, 0, value));
      APP_ERROR_CHECK(app_pwm_channel_duty_set(&PWM2, 0, value));
   
     // nrf_delay_ms(25);
    }

    else
    {
     app_pwm_channel_duty_set(&PWM1, 0, 0);
     app_pwm_channel_duty_set(&PWM2, 0, 0);
    }
  }
}

This code is working properly but I want to set duty cycle inside the interrupt function not in main function .

So how to do so because I tried but without any success.

So guide me .

Related