Skip to content

Commit 8ad4dd3

Browse files
read counter direct from AGT0 count register: micros() is now 2x faster
1 parent 46d5403 commit 8ad4dd3

File tree

1 file changed

+2
-1
lines changed

1 file changed

+2
-1
lines changed

cores/arduino/time.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ static FspTimer main_timer;
2222
const uint8_t _timer_type = AGT_TIMER;
2323
const uint8_t _timer_index = 0;
2424
inline uint8_t _timer_get_underflow_bit() { return R_AGT0->AGTCR_b.TUNDF; }
25+
inline uint16_t _timer_get_counter() { return R_AGT0->AGT; }
2526
// clock divider 8 works for the Uno R4 and Portenta C33 both because _timer_period is < 16-bit.
2627
// on the Uno R4 the AGT clock is 24 MHz / 8 -> 3000 ticks per ms
2728
// on the Portenta C33 the AGT clock is 50 Mhz / 8 -> 6250 ticks per ms
@@ -55,7 +56,7 @@ unsigned long micros() {
5556
// Return time in us
5657
NVIC_DisableIRQ(main_timer.get_cfg()->cycle_end_irq);
5758
uint32_t ms = agt_time_ms;
58-
uint32_t const down_counts = main_timer.get_counter();
59+
uint32_t const down_counts = _timer_get_counter();
5960
if (_timer_get_underflow_bit() && (down_counts > (_timer_period / 2)))
6061
{
6162
// the counter wrapped around just before it was read

0 commit comments

Comments
 (0)