Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Fix: Clean-up previous state when alternating between GPIO and PWM functionality. #620

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions cores/arduino/wiring_analog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,17 @@ void analogWrite(pin_size_t pin, int val)
#endif
float percent = (float)val/(float)((1 << write_resolution)-1);
mbed::PwmOut* pwm = digitalPinToPwm(pin);

/* Check if this pin has been previously configured as a GPIO pin.
* If it has, delete the GPIO allocation as well as the PWM allocation
* to enforce a full re-initialisation of the PWM module.
*/
mbed::DigitalInOut * gpio = digitalPinToGpio(pin);
if ((gpio != NULL) && (pwm != NULL)) {
delete gpio; gpio = NULL; digitalPinToGpio(pin) = NULL;
delete pwm; pwm = NULL; digitalPinToPwm(pin) = NULL;
}

if (pwm == NULL) {
pwm = new mbed::PwmOut(digitalPinToPinName(pin));
digitalPinToPwm(pin) = pwm;
Expand Down
11 changes: 11 additions & 0 deletions cores/arduino/wiring_digital.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,17 @@ void digitalWrite(pin_size_t pin, PinStatus val)
return;
}
mbed::DigitalInOut* gpio = digitalPinToGpio(pin);

/* Check if this pin has been previously configured as a PWM pin.
* If it has, delete the GPIO allocation as well as the PWM allocation
* to enforce a full re-initialisation of the GPIO module.
*/
mbed::PwmOut * pwm = digitalPinToPwm(pin);
if ((gpio != NULL) && (pwm != NULL)) {
delete gpio; gpio = NULL; digitalPinToGpio(pin) = NULL;
delete pwm; pwm = NULL; digitalPinToPwm(pin) = NULL;
}

if (gpio == NULL) {
gpio = new mbed::DigitalInOut(digitalPinToPinName(pin), PIN_OUTPUT, PullNone, val);
digitalPinToGpio(pin) = gpio;
Expand Down