Skip to content

Commit 61cc471

Browse files
committed
Prefer uintptr_t to void const * for ownership token
1 parent 188e817 commit 61cc471

File tree

2 files changed

+72
-19
lines changed

2 files changed

+72
-19
lines changed

cores/nRF5/HardwarePWM.cpp

Lines changed: 51 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,51 @@ HardwarePWM* HwPWMx[] =
5353
#endif
5454
};
5555

56+
bool can_stringify_token(uintptr_t token)
57+
{
58+
uint8_t * t = (uint8_t *)&token;
59+
for (size_t i = 0; i < sizeof(uintptr_t); ++i, ++t)
60+
{
61+
uint8_t x = *t;
62+
if ((x < 0x20) || (x > 0x7E)) return false;
63+
}
64+
return true;
65+
}
66+
67+
void HardwarePWM::DebugOutput(Stream& logger)
68+
{
69+
const size_t count = arrcount(HwPWMx);
70+
logger.printf("HwPWM Debug:");
71+
for (size_t i = 0; i < count; i++) {
72+
HardwarePWM const * pwm = HwPWMx[i];
73+
uintptr_t token = pwm->_owner_token;
74+
logger.printf(" || %d:", i);
75+
if (can_stringify_token(token)) {
76+
uint8_t * t = (uint8_t*)(&token);
77+
static_assert(sizeof(uintptr_t) == 4);
78+
logger.printf(" \"%c%c%c%c\"", t[0], t[1], t[2], t[3] );
79+
} else {
80+
static_assert(sizeof(uintptr_t) == 4);
81+
logger.printf(" %08x ", token);
82+
}
83+
for (size_t j = 0; j < MAX_CHANNELS; j++) {
84+
uint32_t r = pwm->_pwm->PSEL.OUT[i]; // only read it once
85+
if ( (r & PWM_PSEL_OUT_CONNECT_Msk) != (PWM_PSEL_OUT_CONNECT_Disconnected << PWM_PSEL_OUT_CONNECT_Pos) ) {
86+
logger.printf(" %02x", r & 0x1F);
87+
} else {
88+
logger.printf(" xx");
89+
}
90+
}
91+
}
92+
logger.printf("\n");
93+
}
94+
95+
96+
5697
// returns true ONLY when (1) no PWM channel has a pin, and (2) the owner token is nullptr
57-
bool HardwarePWM::takeOwnership(void const * token)
98+
bool HardwarePWM::takeOwnership(uintptr_t token)
5899
{
100+
if (token == 0) return false; // cannot take ownership with nullptr
59101
if (this->_owner_token != 0) return false; // doesn't matter if it's actually a match ... it's not legal to take ownership twice
60102
if (this->usedChannelCount() != 0) return false; // at least one channel is already in use
61103
if (this->enabled() ) return false; // if it's enabled, do not allow new ownership, even with no pins in use
@@ -64,8 +106,9 @@ bool HardwarePWM::takeOwnership(void const * token)
64106
return __sync_bool_compare_and_swap(&(this->_owner_token), 0, token);
65107
}
66108
// returns true ONLY when (1) no PWM channel has a pin attached, and (2) the owner token matches
67-
bool HardwarePWM::releaseOwnership(void const * token)
109+
bool HardwarePWM::releaseOwnership(uintptr_t token)
68110
{
111+
if (token == 0) return false; // cannot release ownership with nullptr
69112
if (!this->isOwner(token) ) return false; // don't even look at peripheral
70113
if ( this->usedChannelCount() != 0) return false; // fail if any channels still have pins
71114
if ( this->enabled() ) return false; // if it's enabled, do not allow ownership to be released, even with no pins in use
@@ -74,9 +117,9 @@ bool HardwarePWM::releaseOwnership(void const * token)
74117
return __sync_bool_compare_and_swap(&(this->_owner_token), token, 0);
75118
}
76119

77-
HardwarePWM::HardwarePWM(NRF_PWM_Type* pwm)
120+
HardwarePWM::HardwarePWM(NRF_PWM_Type* pwm) :
121+
_pwm(pwm)
78122
{
79-
_pwm = pwm;
80123
arrclr(_seq0);
81124

82125
_max_value = 255;
@@ -225,21 +268,21 @@ bool HardwarePWM::writePin(uint8_t pin, uint16_t value, bool inverted)
225268
return writeChannel(ch, value, inverted);
226269
}
227270

228-
uint16_t HardwarePWM::readPin(uint8_t pin)
271+
uint16_t HardwarePWM::readPin(uint8_t pin) const
229272
{
230273
int ch = pin2channel(pin);
231274
VERIFY( ch >= 0, 0);
232275

233276
return readChannel(ch);
234277
}
235278

236-
uint16_t HardwarePWM::readChannel(uint8_t ch)
279+
uint16_t HardwarePWM::readChannel(uint8_t ch) const
237280
{
238281
// remove inverted bit
239282
return (_seq0[ch] & 0x7FFF);
240283
}
241284

242-
uint8_t HardwarePWM::usedChannelCount(void)
285+
uint8_t HardwarePWM::usedChannelCount(void) const
243286
{
244287
uint8_t usedChannels = 0;
245288
for(int i=0; i<MAX_CHANNELS; i++)
@@ -252,7 +295,7 @@ uint8_t HardwarePWM::usedChannelCount(void)
252295
return usedChannels;
253296
}
254297

255-
uint8_t HardwarePWM::freeChannelCount(void)
298+
uint8_t HardwarePWM::freeChannelCount(void) const
256299
{
257300
return MAX_CHANNELS - usedChannelCount();
258301
}

cores/nRF5/HardwarePWM.h

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

5555
uint16_t _seq0[MAX_CHANNELS];
5656

@@ -71,20 +71,27 @@ 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 (void const * token);
74+
bool takeOwnership (uintptr_t token);
7575
// returns true ONLY when (1) no PWM channel has a pin attached, and (2) the owner token matches
76-
bool releaseOwnership(void const * token);
76+
bool releaseOwnership(uintptr_t token);
7777

7878
// allows caller to verify that they own the peripheral
79-
__INLINE bool isOwner(void const * token) __attribute__((__always_inline__))
79+
__INLINE bool isOwner(uintptr_t token) const __attribute__((__always_inline__))
8080
{
8181
return this->_owner_token == token;
8282
}
83+
__INLINE bool takeOwnership (void const * token)
84+
{ return takeOwnership((uintptr_t)token); }
85+
__INLINE bool releaseOwnership(void const * token)
86+
{ return releaseOwnership((uintptr_t)token); }
87+
__INLINE bool isOwner (void const * token) const
88+
__attribute__ ((__always_inline__))
89+
{ return isOwner((uintptr_t)token); }
8390

8491
bool addPin (uint8_t pin);
8592
bool removePin (uint8_t pin);
8693

87-
int pin2channel(uint8_t pin)
94+
int pin2channel(uint8_t pin) const
8895
{
8996
pin = g_ADigitalPinMap[pin];
9097
for(int i=0; i<MAX_CHANNELS; i++)
@@ -94,7 +101,7 @@ class HardwarePWM
94101
return (-1);
95102
}
96103

97-
bool checkPin(uint8_t pin)
104+
bool checkPin(uint8_t pin) const
98105
{
99106
return pin2channel(pin) >= 0;
100107
}
@@ -108,12 +115,15 @@ class HardwarePWM
108115
bool writeChannel(uint8_t ch , uint16_t value, bool inverted = false);
109116

110117
// Read current set value
111-
uint16_t readPin (uint8_t pin);
112-
uint16_t readChannel (uint8_t ch);
118+
uint16_t readPin (uint8_t pin) const;
119+
uint16_t readChannel (uint8_t ch) const;
113120

114121
// Get count of used / free channels
115-
uint8_t usedChannelCount();
116-
uint8_t freeChannelCount();
122+
uint8_t usedChannelCount() const;
123+
uint8_t freeChannelCount() const;
124+
125+
// for debug/validation
126+
static void DebugOutput(Stream& logger);
117127
};
118128

119129
extern HardwarePWM HwPWM0;

0 commit comments

Comments
 (0)