Skip to content

Commit 43da173

Browse files
committed
Update noTone() to be safe to call from ISR
1 parent a2a4371 commit 43da173

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

cores/nRF5/Tone.cpp

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,12 @@ void tone(uint8_t pin, unsigned int frequency, unsigned long duration)
127127

128128
void noTone(uint8_t pin)
129129
{
130+
bool notInIsr = !isInISR();
131+
130132
if (!_HwPWM->isOwner(_toneToken)) {
131-
LOG_LV1("TON", "Attempt to set noTone when not the owner of the PWM peripheral. Ignoring call....");
133+
if (notInIsr) {
134+
LOG_LV1("TON", "Attempt to set noTone when not the owner of the PWM peripheral. Ignoring call....");
135+
}
132136
return;
133137
}
134138
nrf_pwm_task_trigger(_PWMInstance, NRF_PWM_TASK_STOP);
@@ -137,7 +141,9 @@ void noTone(uint8_t pin)
137141
NVIC_DisableIRQ(_IntNo);
138142
_HwPWM->releaseOwnership(_toneToken);
139143
if (_HwPWM->isOwner(_toneToken)) {
140-
LOG_LV1("TON", "stopped tone, but failed to release ownership of PWM peripheral?");
144+
if (notInIsr) {
145+
LOG_LV1("TON", "stopped tone, but failed to release ownership of PWM peripheral?");
146+
}
141147
return;
142148
}
143149
}
@@ -151,11 +157,8 @@ void PWM2_IRQHandler(void){
151157
if(!no_stop){
152158
count_duration--;
153159
if(count_duration == 0) {
154-
nrf_pwm_task_trigger(NRF_PWM2, NRF_PWM_TASK_STOP);
155-
nrf_pwm_disable(NRF_PWM2);
156-
_PWMInstance->PSEL.OUT[0] = NRF_PWM_PIN_NOT_CONNECTED;
157-
NVIC_DisableIRQ(PWM2_IRQn);
158-
_HwPWM->releaseOwnership(_toneToken);
160+
uint8_t pin = _PWMInstance->PSEL.OUT[0];
161+
noTone(pin);
159162
} else {
160163
nrf_pwm_task_trigger(NRF_PWM2, NRF_PWM_TASK_SEQSTART0);
161164
}

0 commit comments

Comments
 (0)