AR #30878 - 10.1 EDK - malloc returns a NULL pointer after a CPU reset

Search Answers Database


 

10.1 EDK - malloc returns a NULL pointer after a CPU reset

AR# 30878
Part EDK-GNU
Last Modified 2008-05-01 00:00:00.0
Status Active
Keywords data, data1, ELF, initial, reboot, objcopy

Description

Keywords: data, data1, ELF, initial, reboot, objcopy

My software application allocates memory by the malloc function. When I perform a CPU reset, the malloc function always returns a NULL pointer. I only need to perform a CPU reset, but I also need malloc to reliably allocate memory.

How can I work around this problem?

Solution

On a CPU reset, the .data sections are not reinitialized. The malloc function relies on variables inside of the .data section in order to operate properly. You will need non-volatile memory to store the .data sections and copy the sections over on a CPU reset. The example below assumes that there are .data and .data1 sections and that these two sections are contiguous. Verify your design and check what data sections exist and if the data sections are contiguous. While this Answer Record focuses on the PowerPC 405 processor, a similar method can be used for MicroBlaze.

1. A 'CopyData' 'C' file to copy data sections from (Xilinx XAPP642):

void CopyData(void){

int *src_rom = (int *)XPAR_FLASH_MEM0_BASEADDR; // pointer src_rom gets the base address of flash
int *dst_ram = &__data_start; // pointer dst_ram gets the value(address) of .data section
int *end_ram = &__data1_end; // pointer end_ram gets the ending address of .data1 section

while (dst_ram < end_ram) { // copy the data from ROM to RAM
*dst_ram++ = *src_rom++;
}
return;
} /* end of copy.c */


2. Copy $EDK/sw/lib/ppc405/src/xil-crt0.s to your project directory.

3. Add bl CopyData right before bl main in the xil-crt0.s file:

:
:
/* Call __init */
bl __init

/* copy data section */
bl CopyData

/* Let her rip */
bl main
:
:

4. Compile xil-crt0.s and CopyData.c in an EDK Shell:
EDK_Shell> powerpc-eabi-gcc -c CopyData.c -c xil-crt0.s

5. Add the -B./<path_to_CopyData.o_and_xil-crt0.o_files>/ extra compile switch for your software application.

6. Compile your software application.

7. Extract the data sections to binary format from you software application's ELF:
powerpc-eabi-objcopy -O binary -j .data -j .data1 executable.elf flash.bin

8. Use flashwriter to write the flash.bin into the flash device starting at the base address.

Now when you press the CPU reset, the CopyData function will execute, copying the data sections from flash into RAM.
 
 
Jobs Events Webcasts News Investors Feedback Legal Privacy Trademarks Sitemap
©  1994-2008 Xilinx, Inc. All Rights Reserved.