Skip to content

Commit 234e506

Browse files
author
oclyke
committed
add rollover blocking to analogWrite
this ensures that (when the timer is running) at least one pulse is emitted at each desired setting this removes concerns about calling analogWrite too often and changing the duty cycle the tradeoff is that analogWrite is now a blocking function that could take up to one whole timer period
1 parent 7544ff1 commit 234e506

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

cores/arduino/ard_sup/analog/ap3_analog.cpp

+13
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,19 @@ ap3_err_t ap3_pwm_output(uint8_t pin, uint32_t th, uint32_t fw, uint32_t clk)
506506
output,
507507
AM_HAL_GPIO_PIN_DRIVESTRENGTH_12MA); //
508508

509+
// if timer is running wait for timer value to roll over (will indicate that at least one pulse has been emitted)
510+
AM_CRITICAL_BEGIN // critical section when reading / writing config registers
511+
if(*((uint32_t*)CTIMERADDRn(CTIMER, timer, CTRL0)) & (CTIMER_CTRL0_TMRA0EN_Msk | CTIMER_CTRL0_TMRB0EN_Msk)){
512+
uint32_t current = 0;
513+
uint32_t last = 0;
514+
do {
515+
last = current;
516+
current = am_hal_ctimer_read( timer, segment);
517+
}while(current >= last);
518+
}
519+
520+
AM_CRITICAL_END // end critical section
521+
509522
// clear timer (also stops the timer)
510523
am_hal_ctimer_clear(timer, segment);
511524

0 commit comments

Comments
 (0)