EmbDev.net

Forum: ARM programming with GCC/GNU tools help! memcpy causes hardfault on STM32F4


Author: magnetron (Guest)
Posted on:
Attached files:

Rate this post
0 useful
not useful
hello dear forum ,
I am trying to run a TCP/IP demo on a Olimex STM32F4 board
with eclipse + yagarto
however I cannot connect to the board with LAN
when I stop the demo with JTAG to look where it hangs
it shows below code

I tried to set up a breakpoint before the memcpy function
as shown in the attached picture
It stops at the breakpoint just before the memcpy function
however when I rerun from this point
it ends up at hardfault_handler as shown below
please advice how can I proceed ?
void
HardFault_Handler(void) 
{
/* Go to infinite loop when Hard Fault exception occurs */ 
while (1)
{
}

: Locked by Moderator
Author: holger (Guest)
Posted on:

Rate this post
0 useful
not useful
>it ends up at hardfault_handler

You end up in a hard fault if pPacket points out of memory.
Or if RXBuff points out of memory.
Or pPacket + size points out of memory.

Author: magnetron (Guest)
Posted on:

Rate this post
0 useful
not useful
hello forum ,

I tried to write a simple memcpy demo to see what is wrong with my 
original code
int main(void)
{
    char name[40];
    char myname[] = "James Clerk Maxwell";
    memcpy ( name, myname, 10 );
}
when I run this code the programm hangs
when I stop the uC with JTAG I see it is in infinite loop of
hardfault handler

memcpy somehow causes hardfault
I think above memcpy is ok - how much simpler can it be ?

it must be something wrong with the gcc

please how can I proceed ?

Author: huegene (Guest)
Posted on:

Rate this post
0 useful
not useful
linkerscript?

Author: magnetron (Guest)
Posted on:

Rate this post
0 useful
not useful
>thank you here is the linker script
====================0=====================
Default linker script for STM32F4xx_1024K_192K
*/

/* include the common STM32F4xx  sub-script */

/* Common part of the linker scripts for STM32F devices*/

/* default stack sizes.

These are used by the startup in order to allocate stacks for the 
different modes.
*/

__Stack_Size = 1024 ;

PROVIDE ( _Stack_Size = __Stack_Size ) ;

__Stack_Init = _estack  - __Stack_Size ;

/*"PROVIDE" allows to easily override these values from an object file 
or the commmand line.*/
PROVIDE ( _Stack_Init = __Stack_Init ) ;

/*
There will be a link error if there is not this amount of RAM free at 
the end.
*/
_Minimum_Stack_Size = 0x100 ;

/* include the memory spaces definitions sub-script */
/*
Linker subscript for STM32F4xx definitions with 1024 Flash and 192 
Onchip SRAM */

/* Memory Spaces Definitions */

MEMORY
{
  RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 128K
  CCMRAM (xrw) : ORIGIN = 0x10000000, LENGTH = 64K
  FLASH (rx) : ORIGIN = 0x8000000, LENGTH = 1024K
  FLASHB1 (rx) : ORIGIN = 0x00000000, LENGTH = 0
  EXTMEMB0 (rx) : ORIGIN = 0x00000000, LENGTH = 0
  EXTMEMB1 (rx) : ORIGIN = 0x00000000, LENGTH = 0
  EXTMEMB2 (rx) : ORIGIN = 0x00000000, LENGTH = 0
  EXTMEMB3 (rx) : ORIGIN = 0x00000000, LENGTH = 0
  MEMORY_ARRAY (xrw)  : ORIGIN = 0x20002000, LENGTH = 32
}

/* higher address of the user mode stack (end of 128K RAM on AHB bus)*/
_estack = 0x20020000;



/* include the sections management sub-script for FLASH mode */

/* Sections Definitions */

SECTIONS
{
    /* for Cortex devices, the beginning of the startup code is stored 
in the .isr_vector section, which goes to FLASH */
    .isr_vector :
    {
 . = ALIGN(4);
        KEEP(*(.isr_vector))            /* Startup code */
 . = ALIGN(4);
    } >FLASH

    /* for some STRx devices, the beginning of the startup code is 
stored in the .flashtext section, which goes to FLASH */
    .flashtext :
    {
 . = ALIGN(4);
        *(.flashtext)            /* Startup code */
 . = ALIGN(4);
    } >FLASH


    /* the program code is stored in the .text section, which goes to 
Flash */
    .text :
    {
     . = ALIGN(4);

        *(.text)                   /* remaining code */
        *(.text.*)                 /* remaining code */
        *(.rodata)                 /* read-only data (constants) */
        *(.rodata*)
        *(.glue_7)
        *(.glue_7t)

     . = ALIGN(4);
     _etext = .;
     /* This is used by the startup in order to initialize the .data 
secion */
     _sidata = _etext;
    } >FLASH

    /* MEMORY_ARRAY */
    .ROarraySection :
    {
            *(.ROarraySection)
    } >MEMORY_ARRAY


    /* This is the initialized data section
    The program executes knowing that the data is in the RAM
    but the loader puts the initial values in the FLASH (inidata).
    It is one task of the startup to copy the initial values from FLASH 
to RAM. */
    .data  : AT ( _sidata )
    {
     . = ALIGN(4);
        /* This is used by the startup in order to initialize the .data 
secion */
        _sdata = . ;

        *(.data)
        *(.data.*)

     . = ALIGN(4);
     /* This is used by the startup in order to initialize the .data 
secion */
     _edata = . ;
    } >RAM



    /* This is the uninitialized data section */
    .bss :
    {
     . = ALIGN(4);
        /* This is used by the startup in order to initialize the .bss 
secion */
        _sbss = .;

        *(.bss)
        *(COMMON)

     . = ALIGN(4);
     /* This is used by the startup in order to initialize the .bss 
secion */
     _ebss = . ;
    } >RAM

    PROVIDE ( end = _ebss );
    PROVIDE ( _end = _ebss );

    /* This is the user stack section
    This is just to check that there is enough RAM left for the User 
mode stack
    It should generate an error if it's full.
     */
    ._usrstack :
    {
     . = ALIGN(4);
        _susrstack = . ;

        . = . + _Minimum_Stack_Size ;

     . = ALIGN(4);
        _eusrstack = . ;
    } >RAM



    /* this is the FLASH Bank1 */
    /* the C or assembly source must explicitly place the code or data 
there
    using the "section" attribute */
    .b1text :
    {
        *(.b1text)                   /* remaining code */
        *(.b1rodata)                 /* read-only data (constants) */
        *(.b1rodata*)
    } >FLASHB1

    /* this is the EXTMEM */
    /* the C or assembly source must explicitly place the code or data 
there
    using the "section" attribute */

    /* EXTMEM Bank0 */
    .eb0text :
    {
        *(.eb0text)                   /* remaining code */
        *(.eb0rodata)                 /* read-only data (constants) */
        *(.eb0rodata*)
    } >EXTMEMB0

    /* EXTMEM Bank1 */
    .eb1text :
    {
        *(.eb1text)                   /* remaining code */
        *(.eb1rodata)                 /* read-only data (constants) */
        *(.eb1rodata*)
    } >EXTMEMB1

    /* EXTMEM Bank2 */
    .eb2text :
    {
        *(.eb2text)                   /* remaining code */
        *(.eb2rodata)                 /* read-only data (constants) */
        *(.eb2rodata*)
    } >EXTMEMB2

    /* EXTMEM Bank0 */
    .eb3text :
    {
        *(.eb3text)                   /* remaining code */
        *(.eb3rodata)                 /* read-only data (constants) */
        *(.eb3rodata*)
    } >EXTMEMB3



    /* after that it's only debugging information. */

    /* remove the debugging information from the standard libraries */
    DISCARD :
    {
     /* libc.a ( * ) */
     /* libm.a ( * ) */
     libgcc.a ( * )
     }


    /* Stabs debugging sections.  */
    .stab          0 : { *(.stab) }
    .stabstr       0 : { *(.stabstr) }
    .stab.excl     0 : { *(.stab.excl) }
    .stab.exclstr  0 : { *(.stab.exclstr) }
    .stab.index    0 : { *(.stab.index) }
    .stab.indexstr 0 : { *(.stab.indexstr) }
    .comment       0 : { *(.comment) }
    /* DWARF debug sections.
       Symbols in the DWARF debugging sections are relative to the 
beginning
       of the section so we begin them at 0.  */
    /* DWARF 1 */
    .debug          0 : { *(.debug) }
    .line           0 : { *(.line) }
    /* GNU DWARF 1 extensions */
    .debug_srcinfo  0 : { *(.debug_srcinfo) }
    .debug_sfnames  0 : { *(.debug_sfnames) }
    /* DWARF 1.1 and DWARF 2 */
    .debug_aranges  0 : { *(.debug_aranges) }
    .debug_pubnames 0 : { *(.debug_pubnames) }
    /* DWARF 2 */
    .debug_info     0 : { *(.debug_info .gnu.linkonce.wi.*) }
    .debug_abbrev   0 : { *(.debug_abbrev) }
    .debug_line     0 : { *(.debug_line) }
    .debug_frame    0 : { *(.debug_frame) }
    .debug_str      0 : { *(.debug_str) }
    .debug_loc      0 : { *(.debug_loc) }
    .debug_macinfo  0 : { *(.debug_macinfo) }
    /* SGI/MIPS DWARF 2 extensions */
    .debug_weaknames 0 : { *(.debug_weaknames) }
    .debug_funcnames 0 : { *(.debug_funcnames) }
    .debug_typenames 0 : { *(.debug_typenames) }
    .debug_varnames  0 : { *(.debug_varnames) }

}
>*******************************************************************
>=============== and this is the stm32f407xG.icf file ==============
>*******************************************************************
/*###ICF### Section handled by ICF editor, don't touch! ****/
/*-Editor annotation file-*/
/* IcfEditorFile="$TOOLKIT_DIR$\config\ide\IcfEditor\cortex_v1_0.xml" */
/*-Specials-*/
define symbol _ICFEDIT_intvec_start_ = 0x08000000;
/*-Memory Regions-*/
define symbol _ICFEDIT_region_ROM_start_ = 0x08000000;
define symbol _ICFEDIT_region_ROM_end_   = 0x080FFFFF;
define symbol _ICFEDIT_region_RAM_start_ = 0x20000000;
define symbol _ICFEDIT_region_RAM_end_   = 0x2002FFFF;
/*-Sizes-*/
define symbol _ICFEDIT_size_cstack_ = 0x2000;
define symbol _ICFEDIT_size_heap_   = 0x2000;
/**** End of ICF editor section. ###ICF###*/

define memory mem with size = 4G;
define region ROM_region   = mem:[from _ICFEDIT_region_ROM_start_   to 
__ICFEDIT_region_ROM_end__];
define region RAM_region   = mem:[from _ICFEDIT_region_RAM_start_   to 
__ICFEDIT_region_RAM_end__];

define block CSTACK    with alignment = 8, size = 
_ICFEDIT_size_cstack_   { };
define block HEAP      with alignment = 8, size = _ICFEDIT_size_heap_ 
{ };

initialize by copy { readwrite };
do not initialize  { section .noinit };

place at address mem:__ICFEDIT_intvec_start__ { readonly section .intvec 
};

place in ROM_region   { readonly };
place in RAM_region   { readwrite,
                        block CSTACK, block HEAP };

Author: chivas (Guest)
Posted on:

Rate this post
0 useful
not useful
>int main(void)
>{
>    char name[40];
>    char myname[] = "James Clerk Maxwell";
>    memcpy ( name, myname, 10 );
>}

The problem within this code is the string literal. A comment respective 
string literals can be found in the c standard 6.4.5, subitem 909:
"If the program attempts to modify such an array, the behavior is 
undefined."
(http://c0x.coding-guidelines.com/6.4.5.html)

A discussion and solutions concerning this problem is available under 
the following link:

http://stackoverflow.com/questions/7698441/c-mallo...

Author: Helmut (Guest)
Posted on:

Rate this post
0 useful
not useful
Hello magnetron,
according to what I know about memcpy your call
memcpy ( name, myname, 10 ); 
tries to copy the 10 bytes starting from address myname to the 10 bytes 
starting from address name.

Therefore I cannot find anything wrong in your call to memcpy.
name is local on the stack and therefore in RAM.
myname is constant and could be in flash

The problem could be:
 - The call to memcpy.
   Is the code for memcpy really there where
   the processor needs it?
 - What memcpy does.
   Is your memcpy standard or do you have
   changed anything?
   Does your memcpy try to read/write from/to an odd address
   and your processor cannot to that?
 - The read from address myname.
   You can check that if you add an additional
   read before the call to memcpy.
 - The write to address name.
   You can check that if you add an
   additional write before the call to memcpy.

What happens if you try:
// volatile dummybyte to prevent the compiler from optimizing away the reads and writes below
char volatile dummy;

int main(void)
{
char name[40];
char myname[] = "James Clerk Maxwell";

// check if writing to name is possible
name[0] = dummy;
name[9] = dummy;

// check if reading from myname is possible
dummy = myname[0];
dummy = myname[9];

memcpy ( name, myname, 10 );

Best regards
Helmut

Author: magnetron (Guest)
Posted on:

Rate this post
0 useful
not useful
hello thank you all for the answers

can you please comment on my makefile ?
I suspect that compiler cannot build with correct library
#   Makefile for compiling the Getting Started project

#-------------------------------------------------------------------------------
#    User-modifiable options
#-------------------------------------------------------------------------------

# Trace level used for compilation
# (can be overriden by adding TRACE_LEVEL=#number to the command-line)
# TRACE_LEVEL_DEBUG      5
# TRACE_LEVEL_INFO       4
# TRACE_LEVEL_WARNING    3
# TRACE_LEVEL_ERROR      2
# TRACE_LEVEL_FATAL      1
# TRACE_LEVEL_NO_TRACE   0
TRACE_LEVEL = 4

# Optimization level
OPTIMIZATION = -O0

# Output file basename
OUTPUT = main

# Output directories
BIN = .
OBJ = obj

# library dirs
LIBRARYSRC = ./lib/src

STARTUPFILE = ./lib/startup_stm32f4xx.s

#-------------------------------------------------------------------------------
#    Tools
#-------------------------------------------------------------------------------

# Tool suffix when cross-compiling
CROSS_COMPILE = arm-none-eabi-

CC = $(CROSS_COMPILE)gcc
SIZE = $(CROSS_COMPILE)size
STRIP = $(CROSS_COMPILE)strip
OBJCOPY = $(CROSS_COMPILE)objcopy
OBJDUMP = $(CROSS_COMPILE)objdump
LD = $(CROSS_COMPILE)gcc
AS = $(CROSS_COMPILE)gcc

#-------------------------------------------------------------------------------
#    Files
#-------------------------------------------------------------------------------

# include folders
INCLUDES = -I./
INCLUDES += -I./lib/
INCLUDES += -I./lib/inc/
INCLUDES += -I./lib/inc/../../

# Objects built from C source files
C_OBJECTS = $(OBJ)/main.o
C_OBJECTS += $(OBJ)/system_stm32f4xx.o
C_OBJECTS += $(OBJ)/stm32f4xx_gpio.o
C_OBJECTS += $(OBJ)/stm32f4xx_rcc.o
C_OBJECTS += $(OBJ)/stm32f4xx_tim.o
C_OBJECTS += $(OBJ)/stm32f4xx_it.o
C_OBJECTS += $(OBJ)/stm32f4xx_syscfg.o
C_OBJECTS += $(OBJ)/stm32_eth.o
C_OBJECTS += $(OBJ)/ethernet.o
C_OBJECTS += $(OBJ)/clock-arch.o
C_OBJECTS += $(OBJ)/timer.o
C_OBJECTS += $(OBJ)/misc.o
C_OBJECTS += $(OBJ)/uip.o
C_OBJECTS += $(OBJ)/httpd.o
C_OBJECTS += $(OBJ)/uip_arp.o
C_OBJECTS += $(OBJ)/psock.o
C_OBJECTS += $(OBJ)/httpd-fs.o
C_OBJECTS += $(OBJ)/httpd-cgi.o
C_OBJECTS += $(OBJ)/http-strings.o
C_OBJECTS += $(OBJ)/syscall.o

# Objects built from Assembly source files
ASM_OBJECTS = $(OBJ)/startup_stm32f4xx.o

LINKER_SCRIPT = ./lib/stm32f4xx_flash.ld

# Append OBJ and BIN directories to output filename
OUTPUT := $(BIN)/$(OUTPUT)

#-------------------------------------------------------------------------------
#    Rules
#-------------------------------------------------------------------------------

# Flags
CFLAGS = -Wall -fno-common -c -g -mcpu=cortex-m3 -mthumb
CFLAGS += -g $(OPTIMIZATION) $(INCLUDES) -DTRACE_LEVEL=$(TRACE_LEVEL)
ASFLAGS = -g -mapcs-32
LDFLAGS = -g -v -nostartfiles -LC:/OlimexODS/yagarto/lib/gcc/arm-none-eabi/4.5.1
OBJCOPYFLAGS = -O binary
OBJDUMPFLAGS = -x --syms -S

all: $(BIN) $(OBJ) $(OUTPUT).out

$(BIN) $(OBJ):
  mkdir $@

$(OUTPUT).out: $(C_OBJECTS) $(ASM_OBJECTS) $(LINKER_SCRIPT)
  @ echo "..linking"
  $(LD) $(LDFLAGS) -T$(LINKER_SCRIPT) -o $(OUTPUT).out $(C_OBJECTS) $(ASM_OBJECTS) libgcc.a 
  $(OBJCOPY) $(OBJCOPYFLAGS) $(OUTPUT).out $(OUTPUT).bin
  @ echo "...completed."
  
$(C_OBJECTS): main.c system_stm32f4xx.c
  @ echo ".compiling"
  $(CC) $(CFLAGS) -o $(OBJ)/main.o main.c
  $(CC) $(CFLAGS) -o $(OBJ)/system_stm32f4xx.o system_stm32f4xx.c
  $(CC) $(CFLAGS) -o $(OBJ)/stm32f4xx_it.o stm32f4xx_it.c
  $(CC) $(CFLAGS) -o $(OBJ)/stm32_eth.o stm32_eth.c
  $(CC) $(CFLAGS) -o $(OBJ)/ethernet.o ethernet.c
  $(CC) $(CFLAGS) -o $(OBJ)/clock-arch.o clock-arch.c
  $(CC) $(CFLAGS) -o $(OBJ)/timer.o timer.c
  $(CC) $(CFLAGS) -o $(OBJ)/uip.o uip.c
  $(CC) $(CFLAGS) -o $(OBJ)/httpd.o httpd.c
  $(CC) $(CFLAGS) -o $(OBJ)/uip_arp.o uip_arp.c
  $(CC) $(CFLAGS) -o $(OBJ)/psock.o psock.c
  $(CC) $(CFLAGS) -o $(OBJ)/httpd-fs.o httpd-fs.c
  $(CC) $(CFLAGS) -o $(OBJ)/httpd-cgi.o httpd-cgi.c
  $(CC) $(CFLAGS) -o $(OBJ)/http-strings.o http-strings.c
  $(CC) $(CFLAGS) -o $(OBJ)/syscall.o syscall.c
  @ echo ".compiling libraries"
  $(CC) $(CFLAGS) -o $(OBJ)/stm32f4xx_tim.o $(LIBRARYSRC)/stm32f4xx_tim.c 
  $(CC) $(CFLAGS) -o $(OBJ)/stm32f4xx_gpio.o $(LIBRARYSRC)/stm32f4xx_gpio.c 
  $(CC) $(CFLAGS) -o $(OBJ)/stm32f4xx_rcc.o $(LIBRARYSRC)/stm32f4xx_rcc.c 
  $(CC) $(CFLAGS) -o $(OBJ)/stm32f4xx_rcc.o $(LIBRARYSRC)/stm32f4xx_rcc.c 
  $(CC) $(CFLAGS) -o $(OBJ)/stm32f4xx_syscfg.o $(LIBRARYSRC)/stm32f4xx_syscfg.c
  $(CC) $(CFLAGS) -o $(OBJ)/misc.o $(LIBRARYSRC)/misc.c
  
$(ASM_OBJECTS): $(STARTUPFILE)
  @ echo ".assembling"
  $(AS) $(ASFLAGS) -o $(OBJ)/startup_stm32f4xx.o $(STARTUPFILE)

clean:
  -rm -f $(OBJ)/*.o $(BIN)/*.out $(BIN)/*.bin $(BIN)/*.dmp $(BIN)/*.map $(BIN)/*.lss

Author: Manuel (Guest)
Posted on:

Rate this post
0 useful
not useful
Did you solve the problem ? By looking your makefile I think you are 
missing some linker directives such as:
-mcpu=cortex-m3 -mthumb -mthumb-interwork

Author: Spencer Oliver (ntfreak)
Posted on:

Rate this post
0 useful
not useful
Just a couple of comments.

#Manuel - thumb-interwork is not required.

#magnetron - [RANT] please can everybody stop using ld to link, you 
are heading for a world of pain. If not now in the future but it will 
happen.[/RANT]

Use gcc as the link driver and it will sort the correct libs/paths for 
you - it is so easy.

Cheers
Spen

Author: Felix (Guest)
Posted on:

Rate this post
0 useful
not useful
Well i had the same problem with some functions from string.h and an 
stm32f4 and coide.

Turned out i just had to properly include the C standard library. Try 
adding libc.a to your linker (with the right fpu version). Then set 
Library dropdown to "Dont use C Library"

Author: magnetron (Guest)
Posted on:

Rate this post
0 useful
not useful
I changed below line
----------------------------------------------------------
from
LDFLAGS = -g -v -nostartfiles

to
LDFLAGS = -mcpu=cortex-m3 -mthumb -g -v -nostartfiles
----------------------------------------------------------
the answer came from ST's microcontroller forum
and it took me 2 weeks

shouldnot it come from this forum ?

Author: Karsten F. (Company: von Dänemark) (bingo600)
Posted on:

Rate this post
0 useful
not useful
magnetron wrote:
> I changed below line
> ----------------------------------------------------------
> from
> LDFLAGS = -g -v -nostartfiles
>
> to
> LDFLAGS = -mcpu=cortex-m3 -mthumb -g -v -nostartfiles
> ----------------------------------------------------------
> the answer came from ST's microcontroller forum
> and it took me 2 weeks
>
> shouldnot it come from this forum ?

As i read it it already did

Spence wrote :

> #magnetron - [RANT] please can everybody stop using ld to link, you
> are heading for a world of pain. If not now in the future but it will
> happen.[/RANT]

He tells you to stop using ld to link , and use gcc instead.

I totally agree , as gcc will use all the relevant options when linking.

/Bingo

This topic is locked and can not be replied to.