better timer emulation

This commit is contained in:
leo 2023-09-18 16:43:34 +02:00
parent 5afb207678
commit 052e7b5a4b
Signed by: leo
GPG Key ID: 0DD993BFB2B307DB
3 changed files with 16 additions and 12 deletions

@ -1 +1 @@
Subproject commit d0877672a3653eeed5fe7b65185ec45d93943f26
Subproject commit bf08fc69ff21c28482ade1eca086867e619b7fea

2
main.c
View File

@ -79,7 +79,7 @@ int main(int argc, char** argv){
exit(-1);
}
window = SDL_CreateWindow( "SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, LCD_WIDTH, LCD_HEIGHT, SDL_WINDOW_SHOWN );
window = SDL_CreateWindow("PSE emulator", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, LCD_WIDTH, LCD_HEIGHT, SDL_WINDOW_SHOWN );
if(window == NULL){
printf("Window could not be created! SDL_Error: %s\n", SDL_GetError());
}

View File

@ -35,19 +35,23 @@ static void* tim_handler(void* arg){
struct timespec start, current;
clock_gettime(CLOCK_MONOTONIC, &start);
while(htim->started){
uint64_t delay_ps = (uint64_t) 15625 * (htim->presc+1) * (htim->period+1);
printf("delay : %lu\n", delay_ps);
uint64_t delay_ps = (uint64_t) 15625 * (htim->presc+1) * (htim->period+1);
int exec_counter = 0;
while(htim->started){
clock_gettime(CLOCK_MONOTONIC, &current);
uint64_t time = (uint64_t) 1000000000 * (current.tv_sec - start.tv_sec) + (current.tv_nsec - start.tv_nsec);
if(delay_ps / 1000 > time)
usleep((delay_ps / 1000 - time) / 1000);
else printf("oups");
clock_gettime(CLOCK_MONOTONIC, &start);
HAL_TIM_PeriodElapsedCallback(htim);
unsigned int nb_exec = time * 1000 / delay_ps;
int backlog = nb_exec - exec_counter;
while(backlog > 0){
if(backlog > 10)
printf("timer simulation lagging behind (%d ticks)\n", backlog);
HAL_TIM_PeriodElapsedCallback(htim);
exec_counter++;
backlog = nb_exec - exec_counter;
}
}
return NULL;
}