Microchip PIC18F2520 Bedienungsanleitung


Lesen Sie kostenlos die đź“– deutsche Bedienungsanleitung fĂĽr Microchip PIC18F2520 (16 Seiten) in der Kategorie Nicht kategorisiert. Dieser Bedienungsanleitung war fĂĽr 3 Personen hilfreich und wurde von 2 Benutzern mit durchschnittlich 4.5 Sternen bewertet

Seite 1/16
 2004 Microchip Technology Inc. DS00914A-page 1
AN914
INTRODUCTION
Dynamic memory allocation is a nice functionality that
is provided with virtually all PC-based compilers. How-
ever, not all microcontroller compilers have such capa-
bility, most likely due to the lack of a sophisticated
operating system with memory management. Although
most applications are static in nature, there are cases
where a need for dynamic allocation of memory
resources exists. Examples include any number of net-
work protocols that have a dynamically specified
nature. This application note presents a simple and effi-
cient method for dynamic memory allocation without
the need of an operating system.
THE MODEL
The model is based on a simple form of a linked list. A
block of memory referred to as the dynamic heap is
split into segments. Each segment has a single-byte
header that references the next segment in the list via
an offset, as well as indicating whether the segment is
allocated. Allocation is specified by a single bit.
Figure 1 shows an example. Consequently, the
reference implicitly identifies the length of the segment.
The heap is terminated with a special header that
references itself, referred to as the “tail”.
Why use single-byte headers? The segment headers
are specifically designed to be a single byte wide to
achieve excellent execution performance, reduce code
size and minimize loss of memory space to segment
control information. Essentially, one byte references are
easier and faster to manipulate than multi-byte relative
or absolute references. Plus, they do not consume as
much space. However, some fundamental limits are
imposed by this methodology. The maximum segment
size is 126 bytes, or the size of the heap, whichever is
smaller. The smallest segment size is one byte, resulting
in a maximum number of segments of one-half of the
number of bytes in the heap minus one. For example, in
a 512-byte heap, one could expect to dynamically
allocate as many as 255 single-byte segments.
FIGURE 1: SIMPLE HEAP EXAMPLE
Although this model will allow dynamic allocation down
to a single byte, doing so sacrifices performance and
memory. With more segments within the heap, more
time is required to attempt to allocate memory. In addi-
tion, every segment requires a header byte; therefore,
a large number of smaller segments require more
memory than a small number of large segments. In the
255-segment example mentioned previously, 50% of
the heap is lost to segment header information.
There is also one other potential problem, especially
with smaller segments: memory fragmentation. Frag-
mentation could ultimately doom an application by
reducing the largest allocatable block of memory. Thus,
dynamic allocation should be restricted to larger blocks
to maintain efficiency and effective use of the heap.
Applications that are likely to encounter fragmentation
issues should provide a method to handle allocation
failures. The implementation depends on the complex-
ity of the application. For some applications, a system
Reset may be sufficient. For applications with more
advanced memory requirements, it may be necessary
to provide allocation management functions. An exam-
ple might be to force non-critical tasks to give up their
memory allocations as needed, then re-allocate
memory to them as required.
Author: Ross M. Fosler
Microchip Technology Incorporated Allocation Bit
Memory Heap
Segment
Length/Reference
Segment 1
Segment x
Tail
Dynamic Memory Allocation for the MPLAB® C18 C Compiler
AN914
DS00914A-page 2  2004 Microchip Technology Inc.
SUPPORTING FUNCTIONS
There are three functions that manage the heap:
•SRAMAlloc: Allocate memory
•SRAMFree: Free previously allocated memory
•SRAMInitHeap: Initialize the dynamic heap
SRAMAlloc
unsigned char * NEAR SRAMAlloc(NEAR unsigned
char nBytes)
SRAMAlloc is used to allocate a segment of memory
within the heap. When it is called, a new segment is
created in the heap. Essentially, larger non-allocated
segments are split to achieve the requested segment
size. If there are a number of smaller non-allocated
segments, they will be merged together to create a
single larger segment. If a segment of sufficient size
cannot be allocated, then an error is returned to the
calling application; otherwise, a 16-bit pointer to the
segment is returned, which is the next address after the
stored segment header. Figure 2 outlines the basic pro-
gram flow. The application must remember the pointer
to successfully free the memory.
SRAMFree
void SRAMFree(unsigned char * NEAR pSRAM)
This function is used to free a previously allocated
memory segment. It allows future calls to SRAMAlloc
to merge or split this segment as necessary. The
pointer returned from allocation must be passed to
successfully free the block of memory.
SRAMInitHeap
void SRAMInitHeap(void)
This function must be called at least one time to initial-
ize the heap with the minimum number of segment
headers and the tail. This function could also be called
to initialize the heap. The minimum number is always
the value of (MAX_HEAP_SIZE/126), rounded up for
any remainder. For example, a 256-byte heap will be
initialized with three segments.
FIGURE 2: SEGMENT ALLOCATION FLOWCHART
Start
Scan for Next
Non-allocated
Segment
Segment too
large?
Found tail?
Segment too
small?
Next
allocated?
Merge Adjacent
Segments
Split Segment
Return Pointer
Return NULL
Finish
Yes
No
Yes
No
No
No
Yes
Yes
segment
 2004 Microchip Technology Inc. DS00914A-page 3
AN914
SETTING UP
Compile Time Options
There are only two compile time options to be set:
•NEAR_MODEL: This specifies whether the code
uses access registers or normal data space for
general processing. There is some small
performance improvement using access memory.
•MAX_HEAP_SIZE: This specifies the size of the
dynamic heap. This value should correlate with
the section size specified in the linker script.
The Linker Script
The source code reserves a block of memory specified
by a section in the linker file named
_SRAM_ALLOC_HEAP. Refer to the attached linker
script in Appendix D: “Linker Script” for an example.
PERFORMANCE
The performance of dynamic allocation varies signifi-
cantly depending on the build options, the number of
segments in the heap, the positions and sizes of the
segments and the size of the heap. In the example
code, with the build options selected in the example
project, allocation can occur in as little as 100 instruc-
tion cycles. In other basic tests, with 4 to 5 segments
previously allocated, allocation can occur in as much
as 450 instruction cycles.
Freeing allocated segments is relatively fixed
compared to allocation. In the example code, with the
build options selected in the example project, freeing
allocated memory only requires 18 instruction cycles.
MEMORY USAGE
The memory usage varies depending on the build
options, the number of segments in the heap, the posi-
tions and sizes of the segments and the size of the
heap. In the example code presented here and with the
build options selected in the example project, only
452 bytes of program memory and 20 bytes of data
memory are used. In addition, another 512 bytes of
data memory are reserved for the dynamic heap. Note
that the heap size can be increased or decreased to
meet the needs of the application.


Produktspezifikationen

Marke: Microchip
Kategorie: Nicht kategorisiert
Modell: PIC18F2520

Brauchst du Hilfe?

Wenn Sie Hilfe mit Microchip PIC18F2520 benötigen, stellen Sie unten eine Frage und andere Benutzer werden Ihnen antworten




Bedienungsanleitung Nicht kategorisiert Microchip

Bedienungsanleitung Nicht kategorisiert

Neueste Bedienungsanleitung fĂĽr -Kategorien-