Skip to content

Commit 7bef4fc

Browse files
added const qualifiers, gave register access macro function format, broke down long formulas
1 parent c653486 commit 7bef4fc

File tree

1 file changed

+18
-13
lines changed

1 file changed

+18
-13
lines changed

cores/arduino/time.cpp

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,16 @@ __attribute__((weak)) void yield() {
2020

2121
static FspTimer main_timer;
2222
// specifying these details as constants makes micros() faster !
23-
#define _timer_type AGT_TIMER
24-
#define _timer_index 0
25-
#define _timer_underflow_bit R_AGT0->AGTCR_b.TUNDF
26-
#define _timer_clock_divider TIMER_SOURCE_DIV_8 // dividers 1, 2, 4 and 8 work because _timer_period is < 16-bit. bug in R4 1.0.2: divider 4 acts as 1 !?
27-
#define _timer_clock_freq 24000000UL
28-
#define _timer_ticks_per_us (_timer_clock_freq / ((1 << _timer_clock_divider) * 1000000UL))
29-
#define _timer_period (_timer_ticks_per_us * 1000UL)
30-
#define TIMER_PRIORITY 8
31-
32-
static void timer_micros_callback(timer_callback_args_t __attribute((unused)) *p_args) {
23+
#define _timer_type AGT_TIMER
24+
#define _timer_index 0
25+
#define _timer_get_underflow_bit() R_AGT0->AGTCR_b.TUNDF
26+
#define _timer_clock_divider TIMER_SOURCE_DIV_8 // dividers 1, 2 and 8 work because _timer_period is < 16-bit. the divider 4 seems not supported: acts as 1
27+
#define _timer_clock_freq 24000000UL
28+
#define _timer_counts_per_us (_timer_clock_freq / ((1 << _timer_clock_divider) * 1000000UL))
29+
#define _timer_period (_timer_counts_per_us * 1000UL)
30+
#define TIMER_PRIORITY 8
31+
32+
static void timer_micros_callback(timer_callback_args_t __attribute((unused))* p_args) {
3333
agt_time_ms += 1;
3434
}
3535

@@ -53,8 +53,13 @@ unsigned long micros() {
5353
// Return time in us
5454
NVIC_DisableIRQ(main_timer.get_cfg()->cycle_end_irq);
5555
uint32_t ms = agt_time_ms;
56-
uint32_t cnt = main_timer.get_counter();
57-
if (_timer_underflow_bit && (cnt > (_timer_period / 2))) ++ms; // the counter wrapped arround just before it was read
56+
uint32_t const down_counts = main_timer.get_counter();
57+
if (_timer_get_underflow_bit() && (down_counts > (_timer_period / 2)))
58+
{
59+
// the counter wrapped arround just before it was read
60+
++ms;
61+
}
5862
NVIC_EnableIRQ(main_timer.get_cfg()->cycle_end_irq);
59-
return ms * 1000 + (((_timer_period - 1) - cnt) / _timer_ticks_per_us);
63+
uint32_t const up_counts = (_timer_period - 1) - down_counts;
64+
return (ms * 1000) + (up_counts / _timer_counts_per_us);
6065
}

0 commit comments

Comments
 (0)