From c190318bb9af4d342333c22fb708065effd63e2b Mon Sep 17 00:00:00 2001 From: Henry Gabryjelski Date: Sun, 3 May 2020 20:23:13 -0700 Subject: [PATCH] Use const void* for ownership token --- cores/nRF5/HardwarePWM.cpp | 8 ++++---- cores/nRF5/HardwarePWM.h | 9 +++++---- cores/nRF5/Tone.cpp | 2 +- cores/nRF5/wiring_analog.cpp | 10 +++++----- libraries/Servo/src/nrf52/Servo.cpp | 2 +- 5 files changed, 16 insertions(+), 15 deletions(-) diff --git a/cores/nRF5/HardwarePWM.cpp b/cores/nRF5/HardwarePWM.cpp index fc8ceb00..85db2961 100644 --- a/cores/nRF5/HardwarePWM.cpp +++ b/cores/nRF5/HardwarePWM.cpp @@ -54,21 +54,21 @@ HardwarePWM* HwPWMx[] = }; // returns true ONLY when (1) no PWM channel has a pin, and (2) the owner token is nullptr -bool HardwarePWM::takeOwnership(uintptr_t token) +bool HardwarePWM::takeOwnership(void const * 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 - if (this->enabled ) return false; // if it's enabled, do not allow new ownership, even with no pins in use + if (this->enabled() ) return false; // if it's enabled, do not allow new ownership, even with no pins 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) +bool HardwarePWM::releaseOwnership(void const * 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 - if ( this->enabled ) return false; // if it's enabled, do not allow ownership to be released, even with no pins in use + if ( this->enabled() ) return false; // if it's enabled, do not allow ownership to be released, even with no pins 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), token, 0); diff --git a/cores/nRF5/HardwarePWM.h b/cores/nRF5/HardwarePWM.h index 5bfe6977..59751466 100644 --- a/cores/nRF5/HardwarePWM.h +++ b/cores/nRF5/HardwarePWM.h @@ -50,7 +50,7 @@ class HardwarePWM private: enum { MAX_CHANNELS = 4 }; // Max channel per group NRF_PWM_Type* _pwm; - uintptr_t _owner_token = 0; + void const * _owner_token = nullptr; uint16_t _seq0[MAX_CHANNELS]; @@ -71,11 +71,12 @@ class HardwarePWM // Cooperative ownership sharing // returns true ONLY when (1) no PWM channel has a pin, and (2) the owner token is nullptr - bool takeOwnership(uintptr_t token); + bool takeOwnership (void const * token); // returns true ONLY when (1) no PWM channel has a pin attached, and (2) the owner token matches - bool releaseOwnership(uintptr_t token); + bool releaseOwnership(void const * token); + // allows caller to verify that they own the peripheral - __INLINE bool isOwner(uintptr_t token) __attribute__((__always_inline__)) + __INLINE bool isOwner(void const * token) __attribute__((__always_inline__)) { return this->_owner_token == token; } diff --git a/cores/nRF5/Tone.cpp b/cores/nRF5/Tone.cpp index 20def523..5f073d4e 100644 --- a/cores/nRF5/Tone.cpp +++ b/cores/nRF5/Tone.cpp @@ -41,7 +41,7 @@ Version Modified By Date Comments unsigned long int count_duration=0; volatile bool no_stop = false; uint8_t pin_sound=0; -static uintptr_t _toneToken = (uintptr_t)(&_toneToken); +static char const * _toneToken = "Tone"; void tone(uint8_t pin, unsigned int frequency, unsigned long duration) diff --git a/cores/nRF5/wiring_analog.cpp b/cores/nRF5/wiring_analog.cpp index e7d7b2f4..79ccf408 100644 --- a/cores/nRF5/wiring_analog.cpp +++ b/cores/nRF5/wiring_analog.cpp @@ -47,7 +47,7 @@ extern "C" { static uint8_t _lastAnalogWriteResolution; -static const uintptr_t _token = (uintptr_t)(&_lastAnalogWriteResolution); +static char const * _analogToken = "analog"; /** * This will apply to all PWM Hardware currently used by analogWrite(), @@ -59,7 +59,7 @@ void analogWriteResolution( uint8_t res ) _lastAnalogWriteResolution = res; for (int i = 0; iisOwner(_token)) continue; + if (!HwPWMx[i]->isOwner(_analogToken)) continue; HwPWMx[i]->setResolution(res); } } @@ -80,7 +80,7 @@ void analogWrite( uint32_t pin, uint32_t value ) // first, handle the case where the pin is already in use by analogWrite() for(int i=0; iisOwner(_token)) { + if (!HwPWMx[i]->isOwner(_analogToken)) { LOG_LV3("ANA", "not currently owner of PWM %d", i); continue; // skip if not owner of this PWM instance } @@ -98,7 +98,7 @@ void analogWrite( uint32_t pin, uint32_t value ) // Next, handle the case where can add the pin to a PWM instance already owned by analogWrite() for(int i=0; iisOwner(_token)) { + if (!HwPWMx[i]->isOwner(_analogToken)) { LOG_LV3("ANA", "not currently owner of PWM %d", i); continue; } @@ -118,7 +118,7 @@ void analogWrite( uint32_t pin, uint32_t value ) // 2. it currently has no pins in use. for(int i=0; itakeOwnership(_token)) { + if (!HwPWMx[i]->takeOwnership(_analogToken)) { LOG_LV3("ANA", "Could not take ownership of PWM %d", i); continue; } diff --git a/libraries/Servo/src/nrf52/Servo.cpp b/libraries/Servo/src/nrf52/Servo.cpp index bd3584d4..adbcf455 100644 --- a/libraries/Servo/src/nrf52/Servo.cpp +++ b/libraries/Servo/src/nrf52/Servo.cpp @@ -21,7 +21,7 @@ #include #include -static uintptr_t _servoToken = (uintptr_t)(&_servoToken); +static char const * _servoToken = "Servo"; static servo_t servos[MAX_SERVOS]; // static array of servo structures uint8_t ServoCount = 0; // the total number of attached servos