Page 222 - The Art of Designing Embedded Systems
P. 222
A Firmware Standards Manual 209
Memory leak detection
Pointer checking
Out of memory handling
Modules
General
A Module is a single file of source code that contains one or
more functions or routines, as well as the variables needed to support the
functions.
Each module contains a number of reluted functions. For instance, an
A/D converter module may include all A/D drivers in a single file. Group-
ing functions in this manner makes it easier to find relevant sections of
code, and allows more effective encapsulation.
Encapsulation-hiding the details of a function’s operation, and
keeping the variables used by the function local-is absolutely essential.
Though C and assembly language don’t explicitly support encapsulation,
with careful coding you can get all of the benefits of this powerful idea as
do people using OOP languages.
In C and assembly language you can define all variables and RAM
inside the modules that use those values. Encapsulate the data by defining
each variable for the scope of the functions that use these variables only.
Keep them private within the function, or within the module, that uses
them.
Modules tend to grow large enough that they are unmanageable.
Keep module sizes under loo0 lines to insure tools (source debuggers,
compilers, etc.) are not stressed to the point they become slow or unreli-
able, and to ease searching.
Templar fes
To encourage a uniform module look and feel, create module tem-
plates named “module-temp1ate.c” and “module-template.asm,” stored in
the source directory, that become part of the code base maintained by the
VCS. Use one of these files as the base for all new modules. The module
template includes a standardized form for the header (the comment block
preceding all code), a standard spot for file includes and module-wide
declarations, function prototypes and macros. The templates also include
the standard format for functions.
Here’s the template for C code:

