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

Disable and enable the PWM on a Thingy:91

Hello,

I'm having a bit of difficulty disabling the PWM peripheral when it is no longer required. From the manual it states that "To completely stop the PWM generation and force the associated pins to a defined state, a STOP task can be triggered at any time.", so I have triggered a TASKS_STOP call followed by disabling the peripheral with ENABLE but it doesn't seem to stop the PWM driven pins.

void pwm_control(CTRL *pCtrl, bool enable)
{
  #if CONFIG_PWM
  {
    if(enable && !pCtrl->System_PWM.bPWMEnable)
    {
      printk("Enabling PWM\n");

      NRF_PWM1_NS->ENABLE = 1;
      NRF_PWM1_NS->TASKS_SEQSTART[0] = 1;

      pCtrl->System_PWM.bPWMEnable = true;
    }
    else if (!enable && pCtrl->System_PWM.bPWMEnable)
    {
      printk("Disabling PWM\n");

      NRF_PWM1_NS->TASKS_STOP = 1;
      NRF_PWM1_NS->ENABLE = 0;

      pCtrl->System_PWM.bPWMEnable = false;
    }
    else
    {
      printk("Error, PWM control undetermined\n");
    }
  }
  #endif
}

What I see is that if I place the above function after a call to pwm_pin_set_usec(), the PWM driven pin is still driven, e.g. the Thingy:91 RGB LED is still illuminated, and any future calls to pwm_pin_set_usec() run as normal with no issues. What is the correct method to disable the PWM perpiheral on the nRF9160 during runtime?

  • Hi Heldi,

    Ok so that's strange that the function still can operate. I am having bizarre results in handling the PWM peripheral.

    I have implimented the disable and re-enable code into my application and find that the LED control functions do not engage the LEDs after the PWM is restarted, all I see is the debug lines in the control functions printing to the terminal showing that pwm_pin_set_usec() is not returning an err value and thus should be setting the LED to a specific colour, i.e. blue.

    These control functions are embedded into an operation function that compress tasks like GPS locking or NB-IoT connections into a single function call. I have to make a direct call to my PWM functions (either for the LEDs or the Buzzer) that contain the pwm_pin_set_usec() commands (or just call that command once in the main.c file) in order for the LEDs to be driven normally by the operational functions.

    What I mean is shown below:

    void operational_function(void)
    {
        /*some code*/
        led_control(); /*does not operate LEDs after the PWM is disabled and restarted*/
    }
    

Related