Skip to content

Commit 6f2adf4

Browse files
committed
fix possible race condition
1 parent 0205da5 commit 6f2adf4

File tree

2 files changed

+21
-7
lines changed

2 files changed

+21
-7
lines changed

cores/nRF5/HardwarePWM.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -289,10 +289,13 @@ bool HardwarePWM::takeOwnership(uintptr_t token)
289289

290290
// This function must not be called within ISR
291291
taskENTER_CRITICAL();
292-
_owner_token = token;
292+
if ( this->_owner_token == 0 )
293+
{
294+
_owner_token = token;
295+
}
293296
taskEXIT_CRITICAL();
294297

295-
return true;
298+
return _owner_token == token;
296299
}
297300

298301
// returns true ONLY when (1) no PWM channel has a pin attached, and (2) the owner token matches
@@ -320,8 +323,11 @@ bool HardwarePWM::releaseOwnership(uintptr_t token)
320323

321324
// This function must not be called within ISR
322325
taskENTER_CRITICAL();
323-
_owner_token = 0;
326+
if ( this->_owner_token == token )
327+
{
328+
_owner_token = 0;
329+
}
324330
taskEXIT_CRITICAL();
325331

326-
return true;
332+
return _owner_token == 0;
327333
}

libraries/Servo/src/nrf52/Servo.cpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,19 @@ uint8_t Servo::attach(int pin, int min, int max)
4848
}
4949
bool succeeded = false;
5050

51-
if (min < MIN_PULSE_WIDTH) min = MIN_PULSE_WIDTH;
52-
if (max > MAX_PULSE_WIDTH) max = MAX_PULSE_WIDTH;
51+
if (min < MIN_PULSE_WIDTH)
52+
{
53+
min = MIN_PULSE_WIDTH;
54+
}
55+
56+
if (max > MAX_PULSE_WIDTH)
57+
{
58+
max = MAX_PULSE_WIDTH;
59+
}
5360

5461
//fix min if conversion to pulse cycle value is too low
55-
if ( (min/DUTY_CYCLE_RESOLUTION) * DUTY_CYCLE_RESOLUTION < min) {
62+
if ( (min / DUTY_CYCLE_RESOLUTION) * DUTY_CYCLE_RESOLUTION < min )
63+
{
5664
min += DUTY_CYCLE_RESOLUTION;
5765
}
5866

0 commit comments

Comments
 (0)