02.03.2014 Views

BSP Developer's Guide

BSP Developer's Guide

BSP Developer's Guide

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

5<br />

Driver <strong>Guide</strong>lines<br />

To fix this situation, create a new macro that is the value of the argument. Define it<br />

at the head of the file in its proper place, then refer to it at the proper place in the<br />

code. This results in a subroutine that is much easier to understand at first sight.<br />

For example:<br />

/* A better way */<br />

#ifdef INCL_FOO<br />

# define THE_FOO myFoo<br />

#else<br />

# define THE_FOO yourFoo<br />

#endif<br />

. . .<br />

fooReset (&THE_FOO, arg2, arg3, arg4);<br />

5<br />

The general rule shall be to use conditional compilation if it really changes the flow<br />

of execution within the routine. If only argument values, variable names, or<br />

subroutine names are changed then use the technique above to define an<br />

intermediate macro.<br />

Be careful with macro names, neither too simple nor too complex. Both of the<br />

following examples are unsatisfactory:<br />

SIO_READ<br />

NS16550_CONTROL_REGISTER_BIT_7_CLEAR<br />

Do not use structures to declare hardware registers, or memory control blocks. Use<br />

macros for offset constants or to convert a base address into a register address.<br />

Structures are inherently non-portable. This is the single greatest source of failure<br />

when porting drivers across architectures. Even using the same toolchain, the<br />

default structure creation can vary from architecture to architecture. The C<br />

language specification does not specify a standard for structure element<br />

alignments.<br />

typedef struct { /* I8250_DEV */<br />

char CSR;<br />

char DATA;<br />

char MBR;<br />

} I8250_DEV;<br />

/* A better way */<br />

#define I8250_CSR 0<br />

#define I8250_DATA 1<br />

#define I8250_MBR 2<br />

99

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!