Skip to content

Commit 2a28ae7

Browse files
committed
Use const void* for ownership token
1 parent bbaeadf commit 2a28ae7

File tree

5 files changed

+16
-15
lines changed

5 files changed

+16
-15
lines changed

cores/nRF5/HardwarePWM.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,21 +54,21 @@ HardwarePWM* HwPWMx[] =
5454
};
5555

5656
// returns true ONLY when (1) no PWM channel has a pin, and (2) the owner token is nullptr
57-
bool HardwarePWM::takeOwnership(uintptr_t token)
57+
bool HardwarePWM::takeOwnership(void const * 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
61+
if (this->enabled() ) return false; // if it's enabled, do not allow new ownership, even with no pins in use
6262
// use gcc built-in intrinsic to ensure atomicity
6363
// See https://gcc.gnu.org/onlinedocs/gcc/_005f_005fsync-Builtins.html
6464
return __sync_bool_compare_and_swap(&(this->_owner_token), 0, token);
6565
}
6666
// returns true ONLY when (1) no PWM channel has a pin attached, and (2) the owner token matches
67-
bool HardwarePWM::releaseOwnership(uintptr_t token)
67+
bool HardwarePWM::releaseOwnership(void const * token)
6868
{
6969
if (!this->isOwner(token) ) return false; // don't even look at peripheral
7070
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
71+
if ( this->enabled() ) return false; // if it's enabled, do not allow ownership to be released, even with no pins in use
7272
// use gcc built-in intrinsic to ensure atomicity
7373
// See https://gcc.gnu.org/onlinedocs/gcc/_005f_005fsync-Builtins.html
7474
return __sync_bool_compare_and_swap(&(this->_owner_token), token, 0);

cores/nRF5/HardwarePWM.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class HardwarePWM
5050
private:
5151
enum { MAX_CHANNELS = 4 }; // Max channel per group
5252
NRF_PWM_Type* _pwm;
53-
uintptr_t _owner_token = 0;
53+
void const * _owner_token = nullptr;
5454

5555
uint16_t _seq0[MAX_CHANNELS];
5656

@@ -71,11 +71,12 @@ class HardwarePWM
7171
// Cooperative ownership sharing
7272

7373
// returns true ONLY when (1) no PWM channel has a pin, and (2) the owner token is nullptr
74-
bool takeOwnership(uintptr_t token);
74+
bool takeOwnership (void const * token);
7575
// returns true ONLY when (1) no PWM channel has a pin attached, and (2) the owner token matches
76-
bool releaseOwnership(uintptr_t token);
76+
bool releaseOwnership(void const * token);
77+
7778
// allows caller to verify that they own the peripheral
78-
__INLINE bool isOwner(uintptr_t token) __attribute__((__always_inline__))
79+
__INLINE bool isOwner(void const * token) __attribute__((__always_inline__))
7980
{
8081
return this->_owner_token == token;
8182
}

cores/nRF5/Tone.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ Version Modified By Date Comments
4141
unsigned long int count_duration=0;
4242
volatile bool no_stop = false;
4343
uint8_t pin_sound=0;
44-
static uintptr_t _toneToken = (uintptr_t)(&_toneToken);
44+
static char const * _toneToken = "Tone";
4545

4646

4747
void tone(uint8_t pin, unsigned int frequency, unsigned long duration)

cores/nRF5/wiring_analog.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ extern "C"
4747
{
4848

4949
static uint8_t _lastAnalogWriteResolution;
50-
static const uintptr_t _token = (uintptr_t)(&_lastAnalogWriteResolution);
50+
static char const * _analogToken = "analog";
5151

5252
/**
5353
* This will apply to all PWM Hardware currently used by analogWrite(),
@@ -59,7 +59,7 @@ void analogWriteResolution( uint8_t res )
5959
_lastAnalogWriteResolution = res;
6060
for (int i = 0; i<HWPWM_MODULE_NUM; i++)
6161
{
62-
if (!HwPWMx[i]->isOwner(_token)) continue;
62+
if (!HwPWMx[i]->isOwner(_analogToken)) continue;
6363
HwPWMx[i]->setResolution(res);
6464
}
6565
}
@@ -80,7 +80,7 @@ void analogWrite( uint32_t pin, uint32_t value )
8080
// first, handle the case where the pin is already in use by analogWrite()
8181
for(int i=0; i<HWPWM_MODULE_NUM; i++)
8282
{
83-
if (!HwPWMx[i]->isOwner(_token)) {
83+
if (!HwPWMx[i]->isOwner(_analogToken)) {
8484
LOG_LV3("ANA", "not currently owner of PWM %d", i);
8585
continue; // skip if not owner of this PWM instance
8686
}
@@ -98,7 +98,7 @@ void analogWrite( uint32_t pin, uint32_t value )
9898
// Next, handle the case where can add the pin to a PWM instance already owned by analogWrite()
9999
for(int i=0; i<HWPWM_MODULE_NUM; i++)
100100
{
101-
if (!HwPWMx[i]->isOwner(_token)) {
101+
if (!HwPWMx[i]->isOwner(_analogToken)) {
102102
LOG_LV3("ANA", "not currently owner of PWM %d", i);
103103
continue;
104104
}
@@ -118,7 +118,7 @@ void analogWrite( uint32_t pin, uint32_t value )
118118
// 2. it currently has no pins in use.
119119
for(int i=0; i<HWPWM_MODULE_NUM; i++)
120120
{
121-
if (!HwPWMx[i]->takeOwnership(_token)) {
121+
if (!HwPWMx[i]->takeOwnership(_analogToken)) {
122122
LOG_LV3("ANA", "Could not take ownership of PWM %d", i);
123123
continue;
124124
}

libraries/Servo/src/nrf52/Servo.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
#include <Arduino.h>
2222
#include <Servo.h>
2323

24-
static uintptr_t _servoToken = (uintptr_t)(&_servoToken);
24+
static char const * _servoToken = "Servo";
2525

2626
static servo_t servos[MAX_SERVOS]; // static array of servo structures
2727
uint8_t ServoCount = 0; // the total number of attached servos

0 commit comments

Comments
 (0)