Track microseconds overflow

Closes: https://github.com/espressif/arduino-esp32/pull/267
This commit is contained in:
me-no-dev 2017-03-20 11:21:56 +02:00
parent e81d4d3f72
commit 786425509e
2 changed files with 18 additions and 4 deletions

View File

@ -18,17 +18,28 @@
#include "freertos/task.h" #include "freertos/task.h"
#include "esp_attr.h" #include "esp_attr.h"
#include "nvs_flash.h" #include "nvs_flash.h"
#include <sys/time.h>
void yield() void yield()
{ {
vPortYield(); vPortYield();
} }
portMUX_TYPE microsMux = portMUX_INITIALIZER_UNLOCKED;
uint32_t IRAM_ATTR micros() uint32_t IRAM_ATTR micros()
{ {
static uint32_t lccount = 0;
static uint32_t overflow = 0;
uint32_t ccount; uint32_t ccount;
portENTER_CRITICAL_ISR(&microsMux);
__asm__ __volatile__ ( "rsr %0, ccount" : "=a" (ccount) ); __asm__ __volatile__ ( "rsr %0, ccount" : "=a" (ccount) );
return ccount / CONFIG_ESP32_DEFAULT_CPU_FREQ_MHZ; 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);
} }
uint32_t IRAM_ATTR millis() uint32_t IRAM_ATTR millis()
@ -45,7 +56,7 @@ void IRAM_ATTR delayMicroseconds(uint32_t us)
{ {
uint32_t m = micros(); uint32_t m = micros();
if(us){ if(us){
uint32_t e = (m + us) % ((0xFFFFFFFF / CONFIG_ESP32_DEFAULT_CPU_FREQ_MHZ) + 1); uint32_t e = (m + us);
if(m > e){ //overflow if(m > e){ //overflow
while(micros() > e){ while(micros() > e){
NOP(); NOP();
@ -63,14 +74,16 @@ void initVariant() {}
void init() __attribute__((weak)); void init() __attribute__((weak));
void init() {} void init() {}
void initArduino(){ void initArduino()
{
nvs_flash_init(); nvs_flash_init();
init(); init();
initVariant(); initVariant();
} }
//used by hal log //used by hal log
const char * IRAM_ATTR pathToFileName(const char * path){ const char * IRAM_ATTR pathToFileName(const char * path)
{
size_t i = 0; size_t i = 0;
size_t pos = 0; size_t pos = 0;
char * p = (char *)path; char * p = (char *)path;

View File

@ -14,6 +14,7 @@ void loopTask(void *pvParameters)
{ {
setup(); setup();
for(;;) { for(;;) {
micros(); //update overflow
loop(); loop();
} }
} }