[fpc-pascal] AVR Busy Wait implementation

Marc Santhoff M.Santhoff at web.de
Sat Jan 16 12:48:36 CET 2016

On Sa, 2016-01-16 at 01:26 +0100, Simon Ameis wrote:
> Am 14.01.2016 um 23:57 schrieb Marc Santhoff:
> > On Do, 2016-01-14 at 21:33 +0100, Simon Ameis wrote:
> >> In general I already know these documents; what I'm looking for is an
> >> implementation of a busy wait, not how to use it.
> >> For C/C++ it's easy to get the code files. But I don't know where to get
> >> compiler built-in procedures for gcc or other language's compilers.
> > If you want to write Pascal code and don not need nano seconds accuracy,
> > you have no need to now compiler internals. Only use time values and the
> > divider factors.
> I don't talk of FPC compiler internals but of gcc compiler internals.
> E.g. the C implementation uses a compiler specific procedure
> __builtin_avr_delay_cycles; I can't find a source to get known how it is
> inlined in the resulting assembler code.
> Please see for reference:
> https://gcc.gnu.org/onlinedocs/gcc-4.7.1/gcc/AVR-Built_002din-Functions.html

Well, this is the fpc mailing list ...
How about asking the gcc guys that question?

You could also easily fetch the gcc source code and 'grep' it.

$ cd gcc-src
$ grep -r * "_builtin_avr_delay_cycles"

And if that doesn't help, write a short C code snippet using that
function and then call the compiler like this:

$ arm-gcc -nostdinc -c -S yoursnippet.c

That will leave the asm (.s or .S) file for you to look at.

> >> For point 3 there is a very good explanation in German at
> >> http://www.avr-asm-tutorial.net/avr_de/zeitschleifen/index.html.
> >>
> >> The wicked thing is the calculation of the cpu cycles. The C
> >> implementation uses floating point arithmetics wich is evaluated at
> >> compile time. It doesn't work with wait times which are calculated at
> >> runtime.
> > Why not use calculated constants? The compiler can evaluate simple
> > arithmetics at compile time:
> >
> > const
> >   sclk = int(round(SYSTEM_CLOCK / 42));
> >
> > The resulting number will be as close to the float value as possible.
> FPC doesn't support floating point on AVR; thus no floating point
> constants are allowed, even if they can be evaluated to an integer
> expression at compile time. FPC obviously first checks types and then
> does optimization on known types.
> Thus only this code is valid on AVR:
> const
>   sclk = SYSTEM_CLOCK div 42;

OK, good to know.

> The question isn't how to calculate this but how to fit it into a word
> boundary for function call without loosing required accuracy.

I don't understand. Fitting in a word boundary would be done using a
compiler switch in code, IIRC "{$align 2}" or similar. If that's true
for function implementations isn't clear, I cannot image it is. If you
want to use Assembler code in Pascal there is documentation in the
programmers manual, chapter 3.

Marc Santhoff <M.Santhoff at web.de>

More information about the fpc-pascal mailing list