|
27 | 27 | */
|
28 | 28 |
|
29 | 29 | #include <stdio.h>
|
| 30 | +#include <sys/time.h> |
30 | 31 |
|
31 | 32 | #include "freertos/FreeRTOS.h"
|
32 | 33 | #include "freertos/task.h"
|
@@ -78,22 +79,34 @@ void mp_hal_stdout_tx_strn_cooked(const char *str, uint32_t len) {
|
78 | 79 | }
|
79 | 80 |
|
80 | 81 | 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; |
82 | 85 | }
|
83 | 86 |
|
84 | 87 | 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; |
86 | 91 | }
|
87 | 92 |
|
88 | 93 | void mp_hal_delay_ms(uint32_t ms) {
|
| 94 | + struct timeval tv_start; |
| 95 | + gettimeofday(&tv_start, NULL); |
89 | 96 | 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 | + } |
90 | 103 | }
|
91 | 104 |
|
92 | 105 | void mp_hal_delay_us(uint32_t us) {
|
93 |
| - vTaskDelay(us / 1000 / portTICK_PERIOD_MS); |
| 106 | + ets_delay_us(us); |
94 | 107 | }
|
95 | 108 |
|
96 |
| -// this function could do with improvements |
| 109 | +// this function could do with improvements (eg use ets_delay_us) |
97 | 110 | void mp_hal_delay_us_fast(uint32_t us) {
|
98 | 111 | uint32_t delay = ets_get_cpu_frequency() / 19;
|
99 | 112 | while (--us) {
|
|
0 commit comments