Skip to content

Commit c2e2443

Browse files
committed
Fix: Clean-up previous state when alternating between GPIO and PWM functionality.
This fixes #613.
1 parent 12213c4 commit c2e2443

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

cores/arduino/wiring_analog.cpp

+11
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,17 @@ void analogWrite(pin_size_t pin, int val)
6868
#endif
6969
float percent = (float)val/(float)((1 << write_resolution)-1);
7070
mbed::PwmOut* pwm = digitalPinToPwm(pin);
71+
72+
/* Check if this pin has been previously configured as a GPIO pin.
73+
* If it has, delete the GPIO allocation as well as the PWM allocation
74+
* to enforce a full re-initialisation of the PWM module.
75+
*/
76+
mbed::DigitalInOut * gpio = digitalPinToGpio(pin);
77+
if ((gpio != NULL) && (pwm != NULL)) {
78+
delete gpio; gpio = NULL; digitalPinToGpio(pin) = NULL;
79+
delete pwm; pwm = NULL; digitalPinToPwm(pin) = NULL;
80+
}
81+
7182
if (pwm == NULL) {
7283
pwm = new mbed::PwmOut(digitalPinToPinName(pin));
7384
digitalPinToPwm(pin) = pwm;

cores/arduino/wiring_digital.cpp

+11
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,17 @@ void digitalWrite(pin_size_t pin, PinStatus val)
9494
return;
9595
}
9696
mbed::DigitalInOut* gpio = digitalPinToGpio(pin);
97+
98+
/* Check if this pin has been previously configured as a PWM pin.
99+
* If it has, delete the GPIO allocation as well as the PWM allocation
100+
* to enforce a full re-initialisation of the GPIO module.
101+
*/
102+
mbed::PwmOut * pwm = digitalPinToPwm(pin);
103+
if ((gpio != NULL) && (pwm != NULL)) {
104+
delete gpio; gpio = NULL; digitalPinToGpio(pin) = NULL;
105+
delete pwm; pwm = NULL; digitalPinToPwm(pin) = NULL;
106+
}
107+
97108
if (gpio == NULL) {
98109
gpio = new mbed::DigitalInOut(digitalPinToPinName(pin), PIN_OUTPUT, PullNone, val);
99110
digitalPinToGpio(pin) = gpio;

0 commit comments

Comments
 (0)