Skip to content
This repository was archived by the owner on Apr 16, 2021. It is now read-only.

analogWrite with negative value to delete pwm objects #105

Closed
wants to merge 1 commit into from

Conversation

polldo
Copy link
Contributor

@polldo polldo commented Sep 30, 2020

This commit allows to deactivate an active PWM attached to a pin N by passing a negative value to the function analogWrite(N, value) .

Necessary fix in mbed-os:
Because of how this mbed function is implemented, there is the need to add a piece of code that sets nordic_internal_pwm[index] == NC when an active pwm is deinitialized (see here ). Otherwise, after having initialized all the 4 pwm instances, the nordic_internal_pwm array will be full. At this point, when a pwm is deactivated, its entry in the array will not be cleared. This is a problem because in this way when a new pwm is initialized, here, no pwm instance will be found free and the default instance (the last one) will be returned. This means that no matter what pwm is deactivated, the new init pwm will try to use the default pwm instance even if it is not free. This results in a failure.

test sketch:

void setup()
{}

void loop() {
    static unsigned long time = millis();
    static uint8_t analogValue = 0;
    if (millis() - time >= 2000) {
      time = millis();
      analogValue += 10; 
      if (analogValue >= 250)
        analogValue = 0;
      
      analogWrite(2, analogValue);
      analogWrite(3, analogValue);
      analogWrite(4, analogValue);
      analogWrite(5, analogValue);
      // deinit the last one 
      analogWrite(5, -1);
      // OK because the last pwm instance has been freed
      analogWrite(9, analogValue);
      // deinit a pwm with an instance that is not the last one
      analogWrite(2, -1);
      // Error: the last pwm instance is busy
      analogWrite(9, analogValue);
    }
}

@facchinm
Copy link
Member

Closing in favour of arduino/ArduinoCore-mbed@1efbfac

@facchinm facchinm closed this Oct 26, 2020
sabas1080 added a commit to ElectronicCats/ArduinoCore-mbed-backup that referenced this pull request Feb 7, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants