Skip to content

Commit 681cb0d

Browse files
committed
esp32/mphalport: Improve delay and ticks functions.
1 parent 4df414b commit 681cb0d

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

esp32/mphalport.c

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
*/
2828

2929
#include <stdio.h>
30+
#include <sys/time.h>
3031

3132
#include "freertos/FreeRTOS.h"
3233
#include "freertos/task.h"
@@ -78,22 +79,34 @@ void mp_hal_stdout_tx_strn_cooked(const char *str, uint32_t len) {
7879
}
7980

8081
uint32_t mp_hal_ticks_ms(void) {
81-
return xTaskGetTickCount() * (1000 / configTICK_RATE_HZ);
82+
struct timeval tv;
83+
gettimeofday(&tv, NULL);
84+
return tv.tv_sec * 1000 + tv.tv_usec / 1000;
8285
}
8386

8487
uint32_t mp_hal_ticks_us(void) {
85-
return mp_hal_ticks_ms() * 1000;
88+
struct timeval tv;
89+
gettimeofday(&tv, NULL);
90+
return tv.tv_sec * 1000000 + tv.tv_usec;
8691
}
8792

8893
void mp_hal_delay_ms(uint32_t ms) {
94+
struct timeval tv_start;
95+
gettimeofday(&tv_start, NULL);
8996
vTaskDelay(ms / portTICK_PERIOD_MS);
97+
struct timeval tv_end;
98+
gettimeofday(&tv_end, NULL);
99+
uint64_t dt = (tv_end.tv_sec - tv_start.tv_sec) * 1000 + (tv_end.tv_usec - tv_start.tv_usec) / 1000;
100+
if (dt < ms) {
101+
ets_delay_us((ms - dt) * 1000);
102+
}
90103
}
91104

92105
void mp_hal_delay_us(uint32_t us) {
93-
vTaskDelay(us / 1000 / portTICK_PERIOD_MS);
106+
ets_delay_us(us);
94107
}
95108

96-
// this function could do with improvements
109+
// this function could do with improvements (eg use ets_delay_us)
97110
void mp_hal_delay_us_fast(uint32_t us) {
98111
uint32_t delay = ets_get_cpu_frequency() / 19;
99112
while (--us) {

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy