...
Example usage of memory segments
gives Listing - Memory Segment Usage Example gives an example usage of memory segments. In this example, we attempt to create a memory segment and to allocate three buffers of 20 bytes from it using the three different segment allocation functions available. Following the listing is an explanation of the differences between these three buffers. .
Anchor | ||||
---|---|---|---|---|
|
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
#define CACHE_LINE_LEN 32u
static CPU_INT08U MemSegData[4096u];
static MEM_SEG MemorySegment;
static void Mem_SegExample (void)
{
CPU_INT08U *p_general_purpose_buf;
CPU_INT08U *p_general_purpose_buf_with_align;
CPU_INT08U *p_hardware_buffer;
LIB_ERR err_lib;
/* ------------ CREATION OF MEMORY SEGMENT ------------ */ (1)
Mem_SegCreate( "Name", /* Name of mem seg (for debugging purposes). */
&MemorySegment, /* Pointer to memory segment structure. */
(CPU_ADDR)MemSegData, /* Base address of memory segment data. */
4096u, /* Length, in byte, of the memory segment. */
CACHE_LINE_LEN, /* Padding alignment value. */
&err_lib);
if (err_lib != LIB_MEM_ERR_NONE) { /* Validate memory segment creation is successful. */
/* Handle error case. */
return;
}
/* ------- ALLOCATION OF GENERAL PURPOSE BUFFER ------- */ (2)
p_general_purpose_buf = Mem_SegAlloc("General purpose buffer",
&MemorySegment,
20u, /* Requested buffer length. */
&err_lib);
if (err_lib != LIB_MEM_ERR_NONE) { /* Validate memory segment allocation is successful. */
/* Handle error case. */
return;
}
/* --- ALLOCATION OF ALIGNED GENERAL PURPOSE BUFFER --- */ (3)
p_general_purpose_buf_with_align = Mem_SegAllocExt("General purpose buffer aligned",
&MemorySegment,
20u,
8u, /* Request 8 bytes boundary alignment. */
DEF_NULL,
&err_lib);
if (err_lib != LIB_MEM_ERR_NONE) { /* Validate memory segment allocation is successful. */
/* Handle error case. */
return;
}
/* ---------- ALLOCATION OF HARDWARE BUFFER ----------- */ (4)
p_hardware_buffer = Mem_SegAllocHW("Buffer read/written via DMA engine",
&MemorySegment,
20u,
1024u, /* DMA engine required alignment. */
DEF_NULL,
&err_lib);
if (err_lib != LIB_MEM_ERR_NONE) { /* Validate memory segment allocation is successful. */
/* Handle error case. */
return;
}
/* ... */
} |
Panel | ||
---|---|---|
| ||
(1) Creation of the memory segment. The memory segment will be created on a statically allocated buffer of 4096 bytes. The base address of the memory segment data can also point to a controller dedicated memory. (2) Allocation of a general-purpose buffer. (3) Allocation of a general-purpose buffer with specified alignment. (4) Allocation of an hardware buffer (a buffer that can be read/written from a DMA engine). |