Use const void* for ownership token
This commit is contained in:
1 parent
c58c01df68
commit
c190318bb9
5 files changed
+16
-15
No files matched your search
@@ -54,21 +54,21 @@ HardwarePWM* HwPWMx[] =
|
|||||||
};
|
};
|
||||||
|
|
||||||
// returns true ONLY when (1) no PWM channel has a pin, and (2) the owner token is nullptr
|
// 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->_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->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
|
// use gcc built-in intrinsic to ensure atomicity
|
||||||
// See https://gcc.gnu.org/onlinedocs/gcc/_005f_005fsync-Builtins.html
|
// See https://gcc.gnu.org/onlinedocs/gcc/_005f_005fsync-Builtins.html
|
||||||
return __sync_bool_compare_and_swap(&(this->_owner_token), 0, token);
|
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
|
// 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->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->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
|
// use gcc built-in intrinsic to ensure atomicity
|
||||||
// See https://gcc.gnu.org/onlinedocs/gcc/_005f_005fsync-Builtins.html
|
// See https://gcc.gnu.org/onlinedocs/gcc/_005f_005fsync-Builtins.html
|
||||||
return __sync_bool_compare_and_swap(&(this->_owner_token), token, 0);
|
return __sync_bool_compare_and_swap(&(this->_owner_token), token, 0);
|
||||||
|
|||||||
@@ -50,7 +50,7 @@ class HardwarePWM
|
|||||||
private:
|
private:
|
||||||
enum { MAX_CHANNELS = 4 }; // Max channel per group
|
enum { MAX_CHANNELS = 4 }; // Max channel per group
|
||||||
NRF_PWM_Type* _pwm;
|
NRF_PWM_Type* _pwm;
|
||||||
uintptr_t _owner_token = 0;
|
void const * _owner_token = nullptr;
|
||||||
|
|
||||||
uint16_t _seq0[MAX_CHANNELS];
|
uint16_t _seq0[MAX_CHANNELS];
|
||||||
|
|
||||||
@@ -71,11 +71,12 @@ class HardwarePWM
|
|||||||
// Cooperative ownership sharing
|
// Cooperative ownership sharing
|
||||||
|
|
||||||
// returns true ONLY when (1) no PWM channel has a pin, and (2) the owner token is nullptr
|
// 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
|
// 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
|
// 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;
|
return this->_owner_token == token;
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-1
@@ -41,7 +41,7 @@ Version Modified By Date Comments
|
|||||||
unsigned long int count_duration=0;
|
unsigned long int count_duration=0;
|
||||||
volatile bool no_stop = false;
|
volatile bool no_stop = false;
|
||||||
uint8_t pin_sound=0;
|
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)
|
void tone(uint8_t pin, unsigned int frequency, unsigned long duration)
|
||||||
|
|||||||
@@ -47,7 +47,7 @@ extern "C"
|
|||||||
{
|
{
|
||||||
|
|
||||||
static uint8_t _lastAnalogWriteResolution;
|
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(),
|
* This will apply to all PWM Hardware currently used by analogWrite(),
|
||||||
@@ -59,7 +59,7 @@ void analogWriteResolution( uint8_t res )
|
|||||||
_lastAnalogWriteResolution = res;
|
_lastAnalogWriteResolution = res;
|
||||||
for (int i = 0; i<HWPWM_MODULE_NUM; i++)
|
for (int i = 0; i<HWPWM_MODULE_NUM; i++)
|
||||||
{
|
{
|
||||||
if (!HwPWMx[i]->isOwner(_token)) continue;
|
if (!HwPWMx[i]->isOwner(_analogToken)) continue;
|
||||||
HwPWMx[i]->setResolution(res);
|
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()
|
// first, handle the case where the pin is already in use by analogWrite()
|
||||||
for(int i=0; i<HWPWM_MODULE_NUM; i++)
|
for(int i=0; i<HWPWM_MODULE_NUM; i++)
|
||||||
{
|
{
|
||||||
if (!HwPWMx[i]->isOwner(_token)) {
|
if (!HwPWMx[i]->isOwner(_analogToken)) {
|
||||||
LOG_LV3("ANA", "not currently owner of PWM %d", i);
|
LOG_LV3("ANA", "not currently owner of PWM %d", i);
|
||||||
continue; // skip if not owner of this PWM instance
|
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()
|
// Next, handle the case where can add the pin to a PWM instance already owned by analogWrite()
|
||||||
for(int i=0; i<HWPWM_MODULE_NUM; i++)
|
for(int i=0; i<HWPWM_MODULE_NUM; i++)
|
||||||
{
|
{
|
||||||
if (!HwPWMx[i]->isOwner(_token)) {
|
if (!HwPWMx[i]->isOwner(_analogToken)) {
|
||||||
LOG_LV3("ANA", "not currently owner of PWM %d", i);
|
LOG_LV3("ANA", "not currently owner of PWM %d", i);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@@ -118,7 +118,7 @@ void analogWrite( uint32_t pin, uint32_t value )
|
|||||||
// 2. it currently has no pins in use.
|
// 2. it currently has no pins in use.
|
||||||
for(int i=0; i<HWPWM_MODULE_NUM; i++)
|
for(int i=0; i<HWPWM_MODULE_NUM; i++)
|
||||||
{
|
{
|
||||||
if (!HwPWMx[i]->takeOwnership(_token)) {
|
if (!HwPWMx[i]->takeOwnership(_analogToken)) {
|
||||||
LOG_LV3("ANA", "Could not take ownership of PWM %d", i);
|
LOG_LV3("ANA", "Could not take ownership of PWM %d", i);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,7 +21,7 @@
|
|||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
#include <Servo.h>
|
#include <Servo.h>
|
||||||
|
|
||||||
static uintptr_t _servoToken = (uintptr_t)(&_servoToken);
|
static char const * _servoToken = "Servo";
|
||||||
|
|
||||||
static servo_t servos[MAX_SERVOS]; // static array of servo structures
|
static servo_t servos[MAX_SERVOS]; // static array of servo structures
|
||||||
uint8_t ServoCount = 0; // the total number of attached servos
|
uint8_t ServoCount = 0; // the total number of attached servos
|
||||||
|
|||||||
Reference in new issue
Block a user