opt-in ownership of PWM peripheral

This commit is contained in:
Henry Gabryjelski committed 2020-07-28 23:05:09 -07:00
1 parent a771af3ed6
commit ab1b90490c
2 files changed
+32

No files matched your search

+19
View File
@@ -53,6 +53,25 @@ HardwarePWM* HwPWMx[] =
#endif
};
// returns true ONLY when (1) no PWM channel has a pin, and (2) the owner token is nullptr
bool HardwarePWM::takeOwnership(uintptr_t token)
{
if (this->_owner_token != 0) return false; // doesn't matter if it's actually a match ... it's not legal to take ownership twice
if (this->usedChannelCount() != 0) return false; // at least one channel is already in use
// use gcc built-in intrinsic to ensure atomicity
// See https://gcc.gnu.org/onlinedocs/gcc/_005f_005fsync-Builtins.html
return __sync_bool_compare_and_swap(&(this->_owner_token), 0, token);
}
// returns true ONLY when (1) no PWM channel has a pin attached, and (2) the owner token matches
bool HardwarePWM::releaseOwnership(uintptr_t token)
{
if (!this->isOwner(token) ) return false; // don't even look at peripheral
if ( this->usedChannelCount() != 0) return false; // fail if any channels still have pins
// use gcc built-in intrinsic to ensure atomicity
// See https://gcc.gnu.org/onlinedocs/gcc/_005f_005fsync-Builtins.html
return __sync_bool_compare_and_swap(&(this->_owner_token), token, 0);
}
HardwarePWM::HardwarePWM(NRF_PWM_Type* pwm)
{
_pwm = pwm;