Last active
November 9, 2017 23:35
-
-
Save DominikPalo/c52926bc4452958ae5036db888644499 to your computer and use it in GitHub Desktop.
Function to return the number of microseconds since the ESP32 board began running
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include "freertos/FreeRTOS.h" | |
#include "esp_system.h" | |
#include "esp_attr.h" | |
#include "sdkconfig.h" | |
portMUX_TYPE microsMux = portMUX_INITIALIZER_UNLOCKED; | |
unsigned long IRAM_ATTR micros() | |
{ | |
static unsigned long lccount = 0; | |
static unsigned long overflow = 0; | |
unsigned long ccount; | |
portENTER_CRITICAL_ISR(µsMux); | |
__asm__ __volatile__ ( "rsr %0, ccount" : "=a" (ccount) ); | |
if(ccount < lccount){ | |
overflow += UINT32_MAX / CONFIG_ESP32_DEFAULT_CPU_FREQ_MHZ; | |
} | |
lccount = ccount; | |
portEXIT_CRITICAL_ISR(µsMux); | |
return overflow + (ccount / CONFIG_ESP32_DEFAULT_CPU_FREQ_MHZ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment