Page 156 - Embedded Microprocessor Systems Real World Design
P. 156
initialized. If the code that initializes -LCS, for example, is a subroutine call,
an attempt is made to push the return address onto the stack before the RAM
(where the stack is located) can be accessed. The return at the end of the sub-
routine ends up with a garbage address and the processor goes off into the weeds.
The same arguments apply to any programmable chip select that affects PROM or
RAM.
RAM
Most high-level languages allow local variables, which are completely local within a
subroutine or function. Other processes know nothing of those variables and have
no access to them. In a PC, memory-management hardware can even tell the oper-
ating system if an ill-behaved program steps outside its predetermined boundaries.
In an embedded system, local variables often are an illusion. As already pointed
out, embedded systems have RAM size constraints. A variable is just one or more
memory locations. All variables are stored in the same RAM space, often in the
same RAM IC. Each variable has a unique location, but nothing will prevent a
berserk piece of code or an incorrect pointer from changing the wrong location
or even writing all through memory and trashing everything. Assembler program-
mers are familiar with this concept since variables in assembly language (especially
with simple assemblers) are often global. Unless there is hardware memory man-
agement, all variables are potentially global to incorrect code.
When using languages that support reentrant code and genuine local vari-
ables saved on the stack, be sure there is enough stack space and initialize all
variables. In some nonembedded applications, you can initialize variables such as
tables when the code is loaded. This is not true in an embedded system. Initialize
everything.
One last note about RAM and PROM: HLL compilers for embedded use often
require that you tell the compiler (or, actually, the linker) where the RAM and
PROM are located, what size they are, and where the stack is to be (if used). This
information usually is used by the linker, a program that links together various code
modules and produces a single output file. Some compiler/linkers are capable of
calculating the required stack size (or at least the worst-case stack size) and
automatically setting the stack pointer appropriately.
uo Ports
Programming hardware 1/0 ports for a microcontroller varies depending on the
particular part used. Some microcontrollers, such as the PIC17Cxx, have control
registers that control the direction of each port bit. Others, such as the 8051, make
every port bit an input at reset and the software makes the port bit an output by
writing to it. 1/0 ports for LSI 1/0 ICs usually have a direction register.
Sofhoare Design 137