File tree Expand file tree Collapse file tree 2 files changed +7
-19
lines changed Expand file tree Collapse file tree 2 files changed +7
-19
lines changed Original file line number Diff line number Diff line change @@ -287,15 +287,9 @@ bool HardwarePWM::takeOwnership(uintptr_t token)
287
287
if ( this ->usedChannelCount () != 0 ) return false ;
288
288
if ( this ->enabled () ) return false ;
289
289
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);
299
293
}
300
294
301
295
// 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)
321
315
return false ; // if it's enabled, do not allow ownership to be released, even with no pins in use
322
316
}
323
317
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 );
333
320
}
Original file line number Diff line number Diff line change 38
38
39
39
#include " common_inc.h"
40
40
#include " nrf.h"
41
+ #include < atomic>
41
42
42
43
#ifdef NRF52840_XXAA
43
44
#define HWPWM_MODULE_NUM 4
@@ -50,7 +51,7 @@ class HardwarePWM
50
51
private:
51
52
enum { MAX_CHANNELS = 4 }; // Max channel per group
52
53
NRF_PWM_Type * const _pwm;
53
- uintptr_t _owner_token;
54
+ std:: atomic_uintptr_t _owner_token;
54
55
55
56
uint16_t _seq0[MAX_CHANNELS];
56
57
You can’t perform that action at this time.
0 commit comments