Prefer uintptr_t to void const * for ownership token

This commit is contained in:
Henry Gabryjelski committed 2020-07-28 23:05:11 -07:00
1 parent 85e41afe27
commit a6e0d2c925
2 files changed
+73 -20

No files matched your search

+52 -9
View File
@@ -53,9 +53,51 @@ HardwarePWM* HwPWMx[] =
#endif
};
// returns true ONLY when (1) no PWM channel has a pin, and (2) the owner token is nullptr
bool HardwarePWM::takeOwnership(void const * token)
bool can_stringify_token(uintptr_t token)
{
uint8_t * t = (uint8_t *)&token;
for (size_t i = 0; i < sizeof(uintptr_t); ++i, ++t)
{
uint8_t x = *t;
if ((x < 0x20) || (x > 0x7E)) return false;
}
return true;
}
void HardwarePWM::DebugOutput(Stream& logger)
{
const size_t count = arrcount(HwPWMx);
logger.printf("HwPWM Debug:");
for (size_t i = 0; i < count; i++) {
HardwarePWM const * pwm = HwPWMx[i];
uintptr_t token = pwm->_owner_token;
logger.printf(" || %d:", i);
if (can_stringify_token(token)) {
uint8_t * t = (uint8_t*)(&token);
static_assert(sizeof(uintptr_t) == 4);
logger.printf(" \"%c%c%c%c\"", t[0], t[1], t[2], t[3] );
} else {
static_assert(sizeof(uintptr_t) == 4);
logger.printf(" %08x ", token);
}
for (size_t j = 0; j < MAX_CHANNELS; j++) {
uint32_t r = pwm->_pwm->PSEL.OUT[i]; // only read it once
if ( (r & PWM_PSEL_OUT_CONNECT_Msk) != (PWM_PSEL_OUT_CONNECT_Disconnected << PWM_PSEL_OUT_CONNECT_Pos) ) {
logger.printf(" %02x", r & 0x1F);
} else {
logger.printf(" xx");
}
}
}
logger.printf("\n");
}
// 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 (token == 0) return false; // cannot take ownership with nullptr
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
@@ -64,8 +106,9 @@ bool HardwarePWM::takeOwnership(void const * 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
bool HardwarePWM::releaseOwnership(void const * token)
bool HardwarePWM::releaseOwnership(uintptr_t token)
{
if (token == 0) return false; // cannot release ownership with nullptr
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
@@ -74,9 +117,9 @@ bool HardwarePWM::releaseOwnership(void const * token)
return __sync_bool_compare_and_swap(&(this->_owner_token), token, 0);
}
HardwarePWM::HardwarePWM(NRF_PWM_Type* pwm)
HardwarePWM::HardwarePWM(NRF_PWM_Type* pwm) :
_pwm(pwm)
{
_pwm = pwm;
arrclr(_seq0);
_max_value = 255;
@@ -225,7 +268,7 @@ bool HardwarePWM::writePin(uint8_t pin, uint16_t value, bool inverted)
return writeChannel(ch, value, inverted);
}
uint16_t HardwarePWM::readPin(uint8_t pin)
uint16_t HardwarePWM::readPin(uint8_t pin) const
{
int ch = pin2channel(pin);
VERIFY( ch >= 0, 0);
@@ -233,13 +276,13 @@ uint16_t HardwarePWM::readPin(uint8_t pin)
return readChannel(ch);
}
uint16_t HardwarePWM::readChannel(uint8_t ch)
uint16_t HardwarePWM::readChannel(uint8_t ch) const
{
// remove inverted bit
return (_seq0[ch] & 0x7FFF);
}
uint8_t HardwarePWM::usedChannelCount(void)
uint8_t HardwarePWM::usedChannelCount(void) const
{
uint8_t usedChannels = 0;
for(int i=0; i<MAX_CHANNELS; i++)
@@ -252,7 +295,7 @@ uint8_t HardwarePWM::usedChannelCount(void)
return usedChannels;
}
uint8_t HardwarePWM::freeChannelCount(void)
uint8_t HardwarePWM::freeChannelCount(void) const
{
return MAX_CHANNELS - usedChannelCount();
}
+21 -11
View File
@@ -49,8 +49,8 @@ class HardwarePWM
{
private:
enum { MAX_CHANNELS = 4 }; // Max channel per group
NRF_PWM_Type* _pwm;
void const * _owner_token = nullptr;
NRF_PWM_Type * const _pwm;
uintptr_t _owner_token = 0;
uint16_t _seq0[MAX_CHANNELS];
@@ -71,20 +71,27 @@ 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 (void const * token);
bool takeOwnership (uintptr_t token);
// returns true ONLY when (1) no PWM channel has a pin attached, and (2) the owner token matches
bool releaseOwnership(void const * token);
bool releaseOwnership(uintptr_t token);
// allows caller to verify that they own the peripheral
__INLINE bool isOwner(void const * token) __attribute__((__always_inline__))
__INLINE bool isOwner(uintptr_t token) const __attribute__((__always_inline__))
{
return this->_owner_token == token;
}
__INLINE bool takeOwnership (void const * token)
{ return takeOwnership((uintptr_t)token); }
__INLINE bool releaseOwnership(void const * token)
{ return releaseOwnership((uintptr_t)token); }
__INLINE bool isOwner (void const * token) const
__attribute__ ((__always_inline__))
{ return isOwner((uintptr_t)token); }
bool addPin (uint8_t pin);
bool removePin (uint8_t pin);
int pin2channel(uint8_t pin)
int pin2channel(uint8_t pin) const
{
pin = g_ADigitalPinMap[pin];
for(int i=0; i<MAX_CHANNELS; i++)
@@ -94,7 +101,7 @@ class HardwarePWM
return (-1);
}
bool checkPin(uint8_t pin)
bool checkPin(uint8_t pin) const
{
return pin2channel(pin) >= 0;
}
@@ -108,12 +115,15 @@ class HardwarePWM
bool writeChannel(uint8_t ch , uint16_t value, bool inverted = false);
// Read current set value
uint16_t readPin (uint8_t pin);
uint16_t readChannel (uint8_t ch);
uint16_t readPin (uint8_t pin) const;
uint16_t readChannel (uint8_t ch) const;
// Get count of used / free channels
uint8_t usedChannelCount();
uint8_t freeChannelCount();
uint8_t usedChannelCount() const;
uint8_t freeChannelCount() const;
// for debug/validation
static void DebugOutput(Stream& logger);
};
extern HardwarePWM HwPWM0;