diff --git a/cores/arduino/ard_sup/analog/ap3_analog.cpp b/cores/arduino/ard_sup/analog/ap3_analog.cpp index 636116ca..accfd13b 100644 --- a/cores/arduino/ard_sup/analog/ap3_analog.cpp +++ b/cores/arduino/ard_sup/analog/ap3_analog.cpp @@ -538,7 +538,8 @@ ap3_err_t ap3_pwm_output(uint8_t pin, uint32_t th, uint32_t fw, uint32_t clk) // if timer is running wait for timer value to roll over (will indicate that at least one pulse has been emitted) AM_CRITICAL_BEGIN // critical section when reading / writing config registers - if (*((uint32_t *)CTIMERADDRn(CTIMER, timer, CTRL0)) & (CTIMER_CTRL0_TMRA0EN_Msk | CTIMER_CTRL0_TMRB0EN_Msk)) + if ((segment == AM_HAL_CTIMER_TIMERA && *((uint32_t *)CTIMERADDRn(CTIMER, timer, CTRL0)) & (CTIMER_CTRL0_TMRA0EN_Msk)) || + (segment == AM_HAL_CTIMER_TIMERB && *((uint32_t *)CTIMERADDRn(CTIMER, timer, CTRL0)) & (CTIMER_CTRL0_TMRB0EN_Msk))) { uint32_t current = 0; uint32_t last = 0; @@ -548,11 +549,10 @@ ap3_err_t ap3_pwm_output(uint8_t pin, uint32_t th, uint32_t fw, uint32_t clk) current = am_hal_ctimer_read(timer, segment); } while (current >= last); } - AM_CRITICAL_END // end critical section - // clear timer (also stops the timer) - am_hal_ctimer_clear(timer, segment); + // clear timer (also stops the timer) + am_hal_ctimer_clear(timer, segment); // Configure the repeated pulse mode with our clock source am_hal_ctimer_config_single(timer,