Skip to content

Commit

Permalink
Fix wait_msec when CNTFRQ_EL0 loops around zero
Browse files Browse the repository at this point in the history
  • Loading branch information
petemoore committed Feb 2, 2023
1 parent d3fa67c commit 9fd38ec
Show file tree
Hide file tree
Showing 10 changed files with 46 additions and 36 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.o
9 changes: 5 additions & 4 deletions 07_delays/delays.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,10 @@ void wait_msec(unsigned int n)
asm volatile ("mrs %0, cntfrq_el0" : "=r"(f));
// read the current counter
asm volatile ("mrs %0, cntpct_el0" : "=r"(t));
// calculate expire value for counter
t+=((f/1000)*n)/1000;
do{asm volatile ("mrs %0, cntpct_el0" : "=r"(r));}while(r<t);
// calculate required count increase
unsigned long i=((f/1000)*n)/1000
// loop while counter increase is less than i
do{asm volatile ("mrs %0, cntpct_el0" : "=r"(r));}while(r-t<i);
}

/**
Expand Down Expand Up @@ -77,5 +78,5 @@ void wait_msec_st(unsigned int n)
unsigned long t=get_system_timer();
// we must check if it's non-zero, because qemu does not emulate
// system timer, and returning constant zero would mean infinite loop
if(t) while(get_system_timer() < t+n);
if(t) while(get_system_timer()-t < n);
}
9 changes: 5 additions & 4 deletions 08_power/delays.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,10 @@ void wait_msec(unsigned int n)
asm volatile ("mrs %0, cntfrq_el0" : "=r"(f));
// read the current counter
asm volatile ("mrs %0, cntpct_el0" : "=r"(t));
// calculate expire value for counter
t+=((f/1000)*n)/1000;
do{asm volatile ("mrs %0, cntpct_el0" : "=r"(r));}while(r<t);
// calculate required count increase
unsigned long i=((f/1000)*n)/1000
// loop while counter increase is less than i
do{asm volatile ("mrs %0, cntpct_el0" : "=r"(r));}while(r-t<i);
}

/**
Expand Down Expand Up @@ -77,5 +78,5 @@ void wait_msec_st(unsigned int n)
unsigned long t=get_system_timer();
// we must check if it's non-zero, because qemu does not emulate
// system timer, and returning constant zero would mean infinite loop
if(t) while(get_system_timer() < t+n);
if(t) while(get_system_timer()-t < n);
}
9 changes: 5 additions & 4 deletions 09_framebuffer/delays.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,10 @@ void wait_msec(unsigned int n)
asm volatile ("mrs %0, cntfrq_el0" : "=r"(f));
// read the current counter
asm volatile ("mrs %0, cntpct_el0" : "=r"(t));
// calculate expire value for counter
t+=((f/1000)*n)/1000;
do{asm volatile ("mrs %0, cntpct_el0" : "=r"(r));}while(r<t);
// calculate required count increase
unsigned long i=((f/1000)*n)/1000
// loop while counter increase is less than i
do{asm volatile ("mrs %0, cntpct_el0" : "=r"(r));}while(r-t<i);
}

/**
Expand Down Expand Up @@ -77,5 +78,5 @@ void wait_msec_st(unsigned int n)
unsigned long t=get_system_timer();
// we must check if it's non-zero, because qemu does not emulate
// system timer, and returning constant zero would mean infinite loop
if(t) while(get_system_timer() < t+n);
if(t) while(get_system_timer()-t < n);
}
9 changes: 5 additions & 4 deletions 0A_pcscreenfont/delays.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,10 @@ void wait_msec(unsigned int n)
asm volatile ("mrs %0, cntfrq_el0" : "=r"(f));
// read the current counter
asm volatile ("mrs %0, cntpct_el0" : "=r"(t));
// calculate expire value for counter
t+=((f/1000)*n)/1000;
do{asm volatile ("mrs %0, cntpct_el0" : "=r"(r));}while(r<t);
// calculate required count increase
unsigned long i=((f/1000)*n)/1000
// loop while counter increase is less than i
do{asm volatile ("mrs %0, cntpct_el0" : "=r"(r));}while(r-t<i);
}

/**
Expand Down Expand Up @@ -77,5 +78,5 @@ void wait_msec_st(unsigned int n)
unsigned long t=get_system_timer();
// we must check if it's non-zero, because qemu does not emulate
// system timer, and returning constant zero would mean infinite loop
if(t) while(get_system_timer() < t+n);
if(t) while(get_system_timer()-t < n);
}
9 changes: 5 additions & 4 deletions 0B_readsector/delays.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,10 @@ void wait_msec(unsigned int n)
asm volatile ("mrs %0, cntfrq_el0" : "=r"(f));
// read the current counter
asm volatile ("mrs %0, cntpct_el0" : "=r"(t));
// calculate expire value for counter
t+=((f/1000)*n)/1000;
do{asm volatile ("mrs %0, cntpct_el0" : "=r"(r));}while(r<t);
// calculate required count increase
unsigned long i=((f/1000)*n)/1000
// loop while counter increase is less than i
do{asm volatile ("mrs %0, cntpct_el0" : "=r"(r));}while(r-t<i);
}

/**
Expand Down Expand Up @@ -77,5 +78,5 @@ void wait_msec_st(unsigned int n)
unsigned long t=get_system_timer();
// we must check if it's non-zero, because qemu does not emulate
// system timer, and returning constant zero would mean infinite loop
if(t) while(get_system_timer() < t+n);
if(t) while(get_system_timer()-t < n);
}
9 changes: 5 additions & 4 deletions 0C_directory/delays.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,10 @@ void wait_msec(unsigned int n)
asm volatile ("mrs %0, cntfrq_el0" : "=r"(f));
// read the current counter
asm volatile ("mrs %0, cntpct_el0" : "=r"(t));
// calculate expire value for counter
t+=((f/1000)*n)/1000;
do{asm volatile ("mrs %0, cntpct_el0" : "=r"(r));}while(r<t);
// calculate required count increase
unsigned long i=((f/1000)*n)/1000
// loop while counter increase is less than i
do{asm volatile ("mrs %0, cntpct_el0" : "=r"(r));}while(r-t<i);
}

/**
Expand Down Expand Up @@ -77,5 +78,5 @@ void wait_msec_st(unsigned int n)
unsigned long t=get_system_timer();
// we must check if it's non-zero, because qemu does not emulate
// system timer, and returning constant zero would mean infinite loop
if(t) while(get_system_timer() < t+n);
if(t) while(get_system_timer()-t < n);
}
9 changes: 5 additions & 4 deletions 0D_readfile/delays.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,10 @@ void wait_msec(unsigned int n)
asm volatile ("mrs %0, cntfrq_el0" : "=r"(f));
// read the current counter
asm volatile ("mrs %0, cntpct_el0" : "=r"(t));
// calculate expire value for counter
t+=((f/1000)*n)/1000;
do{asm volatile ("mrs %0, cntpct_el0" : "=r"(r));}while(r<t);
// calculate required count increase
unsigned long i=((f/1000)*n)/1000
// loop while counter increase is less than i
do{asm volatile ("mrs %0, cntpct_el0" : "=r"(r));}while(r-t<i);
}

/**
Expand Down Expand Up @@ -77,5 +78,5 @@ void wait_msec_st(unsigned int n)
unsigned long t=get_system_timer();
// we must check if it's non-zero, because qemu does not emulate
// system timer, and returning constant zero would mean infinite loop
if(t) while(get_system_timer() < t+n);
if(t) while(get_system_timer()-t < n);
}
9 changes: 5 additions & 4 deletions 0E_initrd/delays.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,10 @@ void wait_msec(unsigned int n)
asm volatile ("mrs %0, cntfrq_el0" : "=r"(f));
// read the current counter
asm volatile ("mrs %0, cntpct_el0" : "=r"(t));
// calculate expire value for counter
t+=((f/1000)*n)/1000;
do{asm volatile ("mrs %0, cntpct_el0" : "=r"(r));}while(r<t);
// calculate required count increase
unsigned long i=((f/1000)*n)/1000
// loop while counter increase is less than i
do{asm volatile ("mrs %0, cntpct_el0" : "=r"(r));}while(r-t<i);
}

/**
Expand Down Expand Up @@ -77,5 +78,5 @@ void wait_msec_st(unsigned int n)
unsigned long t=get_system_timer();
// we must check if it's non-zero, because qemu does not emulate
// system timer, and returning constant zero would mean infinite loop
if(t) while(get_system_timer() < t+n);
if(t) while(get_system_timer()-t < n);
}
9 changes: 5 additions & 4 deletions 15_writesector/delays.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,10 @@ void wait_msec(unsigned int n)
asm volatile ("mrs %0, cntfrq_el0" : "=r"(f));
// read the current counter
asm volatile ("mrs %0, cntpct_el0" : "=r"(t));
// calculate expire value for counter
t+=((f/1000)*n)/1000;
do{asm volatile ("mrs %0, cntpct_el0" : "=r"(r));}while(r<t);
// calculate required count increase
unsigned long i=((f/1000)*n)/1000
// loop while counter increase is less than i
do{asm volatile ("mrs %0, cntpct_el0" : "=r"(r));}while(r-t<i);
}

/**
Expand Down Expand Up @@ -77,5 +78,5 @@ void wait_msec_st(unsigned int n)
unsigned long t=get_system_timer();
// we must check if it's non-zero, because qemu does not emulate
// system timer, and returning constant zero would mean infinite loop
if(t) while(get_system_timer() < t+n);
if(t) while(get_system_timer()-t < n);
}

0 comments on commit 9fd38ec

Please sign in to comment.