Skip to content

Commit bbaeadf

Browse files
committed
Add check for PWM being ENABLED
1 parent b86db18 commit bbaeadf

File tree

2 files changed

+6
-1
lines changed

2 files changed

+6
-1
lines changed

cores/nRF5/HardwarePWM.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ bool HardwarePWM::takeOwnership(uintptr_t token)
5858
{
5959
if (this->_owner_token != 0) return false; // doesn't matter if it's actually a match ... it's not legal to take ownership twice
6060
if (this->usedChannelCount() != 0) return false; // at least one channel is already in use
61+
if (this->enabled ) return false; // if it's enabled, do not allow new ownership, even with no pins in use
6162
// use gcc built-in intrinsic to ensure atomicity
6263
// See https://gcc.gnu.org/onlinedocs/gcc/_005f_005fsync-Builtins.html
6364
return __sync_bool_compare_and_swap(&(this->_owner_token), 0, token);
@@ -67,6 +68,7 @@ bool HardwarePWM::releaseOwnership(uintptr_t token)
6768
{
6869
if (!this->isOwner(token) ) return false; // don't even look at peripheral
6970
if ( this->usedChannelCount() != 0) return false; // fail if any channels still have pins
71+
if ( this->enabled ) return false; // if it's enabled, do not allow ownership to be released, even with no pins in use
7072
// use gcc built-in intrinsic to ensure atomicity
7173
// See https://gcc.gnu.org/onlinedocs/gcc/_005f_005fsync-Builtins.html
7274
return __sync_bool_compare_and_swap(&(this->_owner_token), token, 0);

libraries/Servo/src/nrf52/Servo.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,10 @@ void Servo::detach()
108108
HardwarePWM * pwm = this->pwm;
109109
this->pwm = nullptr;
110110
pwm->removePin(pin);
111-
pwm->releaseOwnership(_servoToken); // ignore failure ... which happens if a pin is still in use, for example
111+
if (pwm->usedChannelCount() == 0) {
112+
pwm->stop(); // disables peripheral so can release ownership
113+
pwm->releaseOwnership(_servoToken);
114+
}
112115
}
113116

114117
void Servo::write(int value)

0 commit comments

Comments
 (0)