Understanding fastio.h
May 07, 2014 06:07AM
Hi everybody,

I am trying to understand in deep the fastio.h file from Marlin. Although I know what finally happens ("magic I/O routines"), I don't know the meaning of each step, and in fact the meaning of this head (in my projects, I use direct assignment for pins). I want to know the meaning and the utility. For example:

#define TXD DIO1

and then

#define DIO0_PIN PINE0
#define DIO0_RPORT PINE
#define DIO0_WPORT PORTE
#define DIO0_DDR DDRE
#define DIO0_PWM NULL

and after

#define _WRITE(IO, v) do { if (&(DIO ## IO ## _RPORT) >= (uint8_t *)0x100) {_WRITE_C(IO, v); } else {_WRITE_NC(IO, v); }; } while (0)

finally

#define WRITE(IO, v) _WRITE(IO, v)

What is the utility for using DIOn definition?

Thank you very much,
regards.

Antonio
Re: Understanding fastio.h
May 07, 2014 02:12PM
Pointing to the source at [github.com] and thereabouts....

The fastio stuff uses C preprocessing macros to translate an arbitrary DIOn operation into a fast bit operation on the associated port/register, which is quicker than a call to the digitalWrite() function (See [github.com] ). Essentially, if you want to set a particular pin to high, you need to read the whole port's register and set the appropriate bit, and then write the whole register. It replaces WRITE(DIO9,0) (or WRITE(PB1,0) )with PORTB &= ~MASK(0b00000010) which . Some 32bit ARM chips have a way of providing a certain parts of address space for magically setting and clearing a particular bit of an output register, which could make the process a couple cycles faster by eliminating the READ and AND operations

One of the confusing things about the preprocessing is the seemingly redundant MACRO and _MACRO definitions which is done to let the macro expand or 'stringify' the parameters. See [stackoverflow.com] for an explanation of this.
Re: Understanding fastio.h
May 09, 2014 06:17AM
Dear MarkT,

thank you very much for your answer, you have clarified to me many things. This file is like the fast way to implement pins functions as read or write. But why these instruction are written? Only for avoiding possible errors in other section code where use PA0, PA1....?

#undef PA0
#undef PA1
#undef PA2
....


Regards,
Antonio
Re: Understanding fastio.h
May 11, 2014 06:36AM
Marlin copied this from Teacup firmware. You might better understand it when reading the original. Preprocessor based algorithms are substantially faster and smaller at runtime (where it matters).


Generation 7 Electronics Teacup Firmware RepRap DIY
     
Re: Understanding fastio.h
May 11, 2014 10:48AM
Quote
Traumflug
Marlin copied this from Teacup firmware. You might better understand it when reading the original. Preprocessor based algorithms are substantially faster and smaller at runtime (where it matters).


In Teacup the sources are [github.com] with supporting files like [github.com] or [github.com]

I think the #undef PA0 are to make sure a that pin def doesn't get expanded into something else and is taken literally during the macro expansion into PA0_PIN, PA0_RPORT, ....etc.
Re: Understanding fastio.h
January 21, 2015 05:59PM
during compile, I never get past this message;

In file included from Marlin.h:21:0,
from stepper.cpp:24:
fastio.cpp:53:29: error: pasting "DIO" and "-" does not give a valid preprocessing token
#define _SET_OUTPUT(IO) do {DIO ## IO ## _DDR |= MASK(DIO ## IO ## _PIN); } while (0)

It has been too long since I've dealt with bitmasks and preproccessing to even consider editing something like this.

I've gone from frustrated to ultrafrustrated as undefined, cast, uninitialized, and recursive calls start popping up (not in this section).

Also, does anyone have a solution to getting the Arduino compiler to scroll the file list? Neither my windows or Ubuntu machine will allow it, and I'm going crosseyed. Codeblocks is a little too strict (probably have something set wrong, but it's been 20 years since I've had to fiddle with this).
Sorry, only registered users may post in this forum.

Click here to login