Skip to content

Instantly share code, notes, and snippets.

@DominikPalo
Last active November 9, 2017 23:35
Show Gist options
  • Save DominikPalo/c52926bc4452958ae5036db888644499 to your computer and use it in GitHub Desktop.
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
#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(&microsMux);
__asm__ __volatile__ ( "rsr %0, ccount" : "=a" (ccount) );
if(ccount < lccount){
overflow += UINT32_MAX / CONFIG_ESP32_DEFAULT_CPU_FREQ_MHZ;
}
lccount = ccount;
portEXIT_CRITICAL_ISR(&microsMux);
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