Skip to content

micros() overflow fix #267

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions cores/esp32/esp32-hal-misc.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,22 @@ void yield()

uint32_t IRAM_ATTR micros()
{
//The ESP32's micro's function overflows too quickly becuase the 32bit
//cycle counter is used.
static uint32_t ccount_overflows = 0;
static uint32_t previous_ccount = 0;
uint32_t ccount;
__asm__ __volatile__ ( "rsr %0, ccount" : "=a" (ccount) );
return ccount / CONFIG_ESP32_DEFAULT_CPU_FREQ_MHZ;
ccount = xthal_get_ccount();

if(previous_ccount>ccount)
{
previous_ccount = ccount;
ccount_overflows++;
}

previous_ccount = ccount;
return (4294967296 / CONFIG_ESP32_DEFAULT_CPU_FREQ_MHZ)*ccount_overflows
+(ccount / CONFIG_ESP32_DEFAULT_CPU_FREQ_MHZ);
}

uint32_t IRAM_ATTR millis()
Expand Down