Skip to content

Commit 3cc5b5b

Browse files
committed
revert to use C11 atomic which is implemented by LDREX/STREX on M4
1 parent 6f2adf4 commit 3cc5b5b

File tree

2 files changed

+7
-19
lines changed

2 files changed

+7
-19
lines changed

cores/nRF5/HardwarePWM.cpp

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -287,15 +287,9 @@ bool HardwarePWM::takeOwnership(uintptr_t token)
287287
if ( this->usedChannelCount() != 0 ) return false;
288288
if ( this->enabled() ) return false;
289289

290-
// This function must not be called within ISR
291-
taskENTER_CRITICAL();
292-
if ( this->_owner_token == 0 )
293-
{
294-
_owner_token = token;
295-
}
296-
taskEXIT_CRITICAL();
297-
298-
return _owner_token == token;
290+
// Use C++11 atomic CAS operation
291+
uintptr_t expectedValue = 0U;
292+
return this->_owner_token.compare_exchange_strong(expectedValue, token);
299293
}
300294

301295
// returns true ONLY when (1) no PWM channel has a pin attached, and (2) the owner token matches
@@ -321,13 +315,6 @@ bool HardwarePWM::releaseOwnership(uintptr_t token)
321315
return false; // if it's enabled, do not allow ownership to be released, even with no pins in use
322316
}
323317

324-
// This function must not be called within ISR
325-
taskENTER_CRITICAL();
326-
if ( this->_owner_token == token )
327-
{
328-
_owner_token = 0;
329-
}
330-
taskEXIT_CRITICAL();
331-
332-
return _owner_token == 0;
318+
// Use C++11 atomic CAS operation
319+
return this->_owner_token.compare_exchange_strong(token, 0U);
333320
}

cores/nRF5/HardwarePWM.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838

3939
#include "common_inc.h"
4040
#include "nrf.h"
41+
#include <atomic>
4142

4243
#ifdef NRF52840_XXAA
4344
#define HWPWM_MODULE_NUM 4
@@ -50,7 +51,7 @@ class HardwarePWM
5051
private:
5152
enum { MAX_CHANNELS = 4 }; // Max channel per group
5253
NRF_PWM_Type * const _pwm;
53-
uintptr_t _owner_token;
54+
std::atomic_uintptr_t _owner_token;
5455

5556
uint16_t _seq0[MAX_CHANNELS];
5657

0 commit comments

Comments
 (0)