Skip to content

Commit c1f1ec9

Browse files
committed
reduce time in critical section so we don't miss critical interrupts like incoming serial characters
1 parent 4c1513a commit c1f1ec9

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

cores/arduino/sdk/core-implement/CommonAnalog.cpp

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -570,9 +570,20 @@ ap3_err_t ap3_pwm_output(ap3_gpio_pad_t pad, uint32_t th, uint32_t fw, uint32_t
570570
AM_HAL_GPIO_PIN_DRIVESTRENGTH_12MA); //
571571

572572
// if timer is running wait for timer value to roll over (will indicate that at least one pulse has been emitted)
573+
uint32_t timer_enabled;
574+
573575
AM_CRITICAL_BEGIN // critical section when reading / writing config registers
574-
if ((segment == AM_HAL_CTIMER_TIMERA && *((uint32_t *)CTIMERADDRn(CTIMER, timer, CTRL0)) & (CTIMER_CTRL0_TMRA0EN_Msk)) ||
575-
(segment == AM_HAL_CTIMER_TIMERB && *((uint32_t *)CTIMERADDRn(CTIMER, timer, CTRL0)) & (CTIMER_CTRL0_TMRB0EN_Msk)))
576+
if(segment == AM_HAL_CTIMER_TIMERA)
577+
{
578+
timer_enabled = *((uint32_t *)CTIMERADDRn(CTIMER, timer, CTRL0)) & (CTIMER_CTRL0_TMRA0EN_Msk);
579+
}
580+
else
581+
{
582+
timer_enabled = *((uint32_t *)CTIMERADDRn(CTIMER, timer, CTRL0)) & (CTIMER_CTRL0_TMRB0EN_Msk);
583+
}
584+
AM_CRITICAL_END // end critical section
585+
586+
if (timer_enabled)
576587
{
577588
uint32_t current = 0;
578589
uint32_t last = 0;
@@ -582,7 +593,7 @@ ap3_err_t ap3_pwm_output(ap3_gpio_pad_t pad, uint32_t th, uint32_t fw, uint32_t
582593
current = am_hal_ctimer_read(timer, segment);
583594
} while (current >= last);
584595
}
585-
AM_CRITICAL_END // end critical section
596+
586597

587598
// clear timer (also stops the timer)
588599
am_hal_ctimer_clear(timer, segment);

0 commit comments

Comments
 (0)