This is the mail archive of the
ecos-discuss@sources.redhat.com
mailing list for the eCos project.
HAL_DELAY_US() default implementation
- From: Øyvind Harboe <oyvind dot harboe at zylin dot com>
- To: ecos-discuss at sources dot redhat dot com
- Date: Wed, 22 Jun 2005 09:26:46 +0200
- Subject: [ECOS] HAL_DELAY_US() default implementation
Ref
http://ecos.sourceware.org/ml/ecos-patches/2005-06/msg00033.html
Looks good.
Thoughts on a default implementation:
- Add calibration by default on startup. There are so many things
affecting the performance: type of ram/flash, caches, off-frequency
crystals(e.g. 64MHz instead of 66 MHz crystal), etc.
I'm not entirely convinced that the calibration number can
be put in the source and committed to CVS. E.g. for the AT91
there are 5 HALs X 3 types of code memory(internal, external
& flash) = 15 calibration numbers. The number of combinations
for all the eCos HAL's could easy reach thousands.
- Calibration can be disabled by specifying the number in
a CDL option. This number is extracted by running the app in
the debugger and setting a breakpoint at the end of the calibration
loop.
- The calibration algorithm must have some sort of way to estimating
the accuracy to determine when the calibration should terminate.
Integer math.
Some code:
/* this fn takes at least 0.000001342194658704 seconds
* to execute one iteration (EB40a running out of flash).
*
* A number of factors affect this number:
*
* - Is the code in ram/rom, wait states
* - Iterrupts/task switches can slow it down
*/
static void busy(int j)
{
__asm volatile (
".L1: nop\n"
"sub %0,%0,#1\n"
"cmp %0,#0\n"
"bne .L1\n"
: "+r" (j));
}
/* times the busy() function */
void timeIt()
{
for (int j=1000; j<10000000; j=((j*110)/100))
{
// time it.
cyg_tick_count_t lastTime=cyg_current_time();
busy(j);
cyg_tick_count_t current=cyg_current_time();
diag_printf("%d %d\n", j, (int)(current-lastTime));
}
}
/* Waits at least this many us.
*
* Does not require kernel to be running.
*/
void waitus(int us)
{
float oneIteration=0.000001342194658704;
int iterations;
iterations=(int)(us*1000000*oneIteration);
busy(iterations);
}
Example run:
loops ticks time/seconds
1395411 187 0,000001340106964901
1534952 206 0,000001342061510718
1688447 227 0,000001344430710588
1857291 249 0,000001340662287170
2043020 274 0,000001341151824260
2247322 301 0,000001339371928010
2472054 331 0,000001338967514464
2719259 365 0,000001342277436610
2991184 401 0,000001340606261601
3290302 441 0,000001340302501108
3619332 486 0,000001342789221879
3981265 535 0,000001343793995125
4379391 588 0,000001342652437291
4817330 647 0,000001343067632900
5299063 711 0,000001341746644643
5828969 782 0,000001341575156773
6411865 861 0,000001342823031988
7053051 947 0,000001342681344570
7758356 1042 0,000001343068041735
8534191 1145 0,000001341662027485
9387610 1260 0,000001342194658704
--
Øyvind Harboe
http://www.zylin.com
--
Before posting, please read the FAQ: http://ecos.sourceware.org/fom/ecos
and search the list archive: http://ecos.sourceware.org/ml/ecos-discuss