Skip to content

Commit aa2a710

Browse files
igrrCurclamas
authored andcommitted
Use esp_timer_get_time as time source for micros and mills (espressif#1424)
esp_timer_get_time returns monotonic time in microseconds, as a 64-bit number. It can be called from tasks and interrupts, does not use any critical sections/mutexes, and is thread safe.
1 parent 788874e commit aa2a710

File tree

2 files changed

+3
-41
lines changed

2 files changed

+3
-41
lines changed

Diff for: cores/esp32/esp32-hal-misc.c

+3-40
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
#include "nvs.h"
2222
#include "esp_partition.h"
2323
#include "esp_log.h"
24-
#include "pthread.h"
24+
#include "esp_timer.h"
2525
#include <sys/time.h>
2626

2727
//Undocumented!!! Get chip temperature in Farenheit
@@ -38,51 +38,14 @@ void yield()
3838
vPortYield();
3939
}
4040

41-
portMUX_TYPE microsMux = portMUX_INITIALIZER_UNLOCKED;
42-
static pthread_key_t microsStore=NULL; // Thread Local Storage Handle
43-
44-
void microsStoreDelete(void * storage) { // release thread local data when task is delete.
45-
if(storage) free(storage);
46-
}
47-
4841
unsigned long IRAM_ATTR micros()
4942
{
50-
if (!microsStore) { // first Time Ever thread local not init'd
51-
portENTER_CRITICAL_ISR(&microsMux);
52-
pthread_key_create(&microsStore,microsStoreDelete); // create initial holder
53-
portEXIT_CRITICAL_ISR(&microsMux);
54-
}
55-
56-
uint32_t *ptr;// [0] is lastCount, [1] is overFlow
57-
58-
ptr = pthread_getspecific(microsStore); // get address of storage
59-
60-
if(ptr == NULL) { // first time in this thread, allocate mem, init it.
61-
portENTER_CRITICAL_ISR(&microsMux);
62-
ptr = (uint32_t*)malloc(sizeof(uint32_t)*2);
63-
pthread_setspecific(microsStore,ptr); // store the pointer to this thread's values
64-
ptr[0] = 0; // lastCount value
65-
ptr[1] = 0; // overFlow
66-
portEXIT_CRITICAL_ISR(&microsMux);
67-
}
68-
69-
unsigned long ccount;
70-
71-
portENTER_CRITICAL_ISR(&microsMux);
72-
__asm__ __volatile__ ( "rsr %0, ccount" : "=a" (ccount) ); //get cycle count
73-
if(ccount < ptr[0]) { // overflow occurred
74-
ptr[1] += UINT32_MAX / CONFIG_ESP32_DEFAULT_CPU_FREQ_MHZ;
75-
}
76-
77-
ptr[0] = ccount;
78-
portEXIT_CRITICAL_ISR(&microsMux);
79-
80-
return ptr[1] + (ccount / CONFIG_ESP32_DEFAULT_CPU_FREQ_MHZ);
43+
return (unsigned long) esp_timer_get_time();
8144
}
8245

8346
unsigned long IRAM_ATTR millis()
8447
{
85-
return xTaskGetTickCount() * portTICK_PERIOD_MS;
48+
return (unsigned long) (esp_timer_get_time() / 1000);
8649
}
8750

8851
void delay(uint32_t ms)

Diff for: cores/esp32/main.cpp

-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ void loopTask(void *pvParameters)
1414
{
1515
setup();
1616
for(;;) {
17-
micros(); //update overflow
1817
loop();
1918
}
2019
}

0 commit comments

Comments
 (0)