esp-serial-flasher
esp-serial-flasher copied to clipboard
Wrong timeouts with Raspberry Pi port (ESF-5)
These lines of code:
https://github.com/espressif/esp-serial-flasher/blob/e13fa455b0fb00bb039ab5c668985d8aab859418/port/raspberry_port.c#L286-L296
are incorrect. The clock() routine returns how much processor time is used by the application, not how much processor time has passed. An example of checking this is doing (by using a system call to sleep, plus spinning for some "user" time measurement, but measuring across the span of both with clock()):
#include <time.h>
#include <unistd.h>
#include <stdio.h>
double to_seconds(struct timespec *spec)
{
return spec->tv_sec + (double)spec->tv_nsec / 1e9;
}
int main(int argc, char **argv)
{
int i;
clock_t clk_start, clk_end;
struct timespec mono_start, mono_end;
clk_start = clock();
clock_gettime(CLOCK_MONOTONIC, &mono_start);
for(i=0;i<100000000;++i);
usleep(1000 * 1000 * 1);
clk_end = clock();
clock_gettime(CLOCK_MONOTONIC, &mono_end);
printf("clock() duration: %f\n", (float)(clk_end - clk_start) / CLOCKS_PER_SEC);
printf("clock_gettime() duration: %f\n", to_seconds(&mono_end) - to_seconds(&mono_start));
return 0;
}
which outputs:
$ time ./test
clock() duration: 0.167709
clock_gettime() duration: 1.167781
real 0m1.169s
user 0m0.169s
sys 0m0.000s
You can see that clock() isn't suitable for measuring how much real time has elapsed (only "user" time). It'd be better to utilize the POSIX API clock_gettime(CLOCK_MONOTONIC, xxx)
instead.
Thank you for the submission and sorry for the big delay in the reply, we've only recently added Github issue tracking for this project.
This does seem to be correct, I will investigate and test the changes suggested.