problem with standard libraries on STM32F100RB

Asked by dunk

hi and thanks for the toolchain!

i appear to have a a problem including any of the C standard libraries in my project.
apologies in advance if this is caused by my lack of understanding rather than this toolchain.

the target:
i am using an old STM32VLDISCOVERY dev board from my parts bin.
http://www.st.com/st-web-ui/static/active/en/resource/technical/document/user_manual/CD00267113.pdf
this uses the STM32F100RB core.

i am using ST's source files, linker script, etc from here:
http://www.st.com/web/en/catalog/tools/PF257914#

i am building on Linux and using https://github.com/texane/stlink for flashing firmware, GDB, etc.

the problem:
lets use memset() as an example.
when i include string.h and use memset() to set more than 1 byte in my code everything appears to build ok,
however the target STM32 goes to the HardFault_Handler interrupt handler.

minimal code example, main.c:

#include "stm32f10x.h"
#include "STM32vldiscovery.h"
#include <string.h>

int main(void){
    STM32vldiscovery_LEDInit(LED3);
    STM32vldiscovery_LEDInit(LED4);

    STM32vldiscovery_LEDOn(LED3);
    STM32vldiscovery_LEDOff(LED4);

    char test[12];
    memset(test, 0, 2);

    STM32vldiscovery_LEDOn(LED4);
    while(1){
    }
}

linker command line:

duncan@duncan:~/Working/EmbeddedArm/test_the_compiler$ make TOOLCHAIN_PATH=~/Working/EmbeddedArm/gcc-arm-none-eabi-4_8-2014q1/bin/
/home/duncan/Working/EmbeddedArm/gcc-arm-none-eabi-4_8-2014q1/bin//arm-none-eabi-gcc -o Demo.elf -O0 -g -Wall -Werror -fno-exceptions -ffunction-sections -fdata-sections -nostartfiles -Wl,--gc-sections,-T"/home/duncan/Working/EmbeddedArm/test_the_compiler/stm32.ld" -lc -lnosys -specs=nosys.specs main.o stm32f10x_it.o system_stm32f10x.o STM32vldiscovery.o stm32f10x_rcc.o misc.o stm32f10x_exti.o stm32f10x_gpio.o startup_stm32f10x_md_vl.o
/home/duncan/Working/EmbeddedArm/gcc-arm-none-eabi-4_8-2014q1/bin//arm-none-eabi-objcopy -O ihex Demo.elf Demo.hex
/home/duncan/Working/EmbeddedArm/gcc-arm-none-eabi-4_8-2014q1/bin//arm-none-eabi-objcopy -O binary Demo.elf Demo.bin

gdb output and source file snippets:

(gdb) run
Starting program: /home/duncan/Working/EmbeddedArm/test_the_compiler/Demo.elf
^CHardFault_Handler () at stm32f10x_it.c:59
59 }
(gdb) bt
#0 HardFault_Handler () at stm32f10x_it.c:59
#1 <signal handler called>
#2 0x080012fe in GPIO_Init (GPIOx=0x20001ff0, GPIO_InitStruct=0x118840ab) at stm32f10x_gpio.c:183
#3 0x08001e36 in ?? ()
#4 0x08001e36 in ?? ()
Backtrace stopped: previous frame identical to this frame (corrupt stack?)
(gdb)

duncan@duncan:~/Working/EmbeddedArm/test_the_compiler$ cat -n stm32f10x_it.c | head -n60 |tail -n12
    49 /**
    50 * @brief This function handles Hard Fault exception.
    51 * @param None
    52 * @retval None
    53 */
    54 void HardFault_Handler(void)
    55 {
    56 /* Go to infinite loop when Hard Fault exception occurs */
    57 while (1)
    58 {
    59 }
    60 }

duncan@duncan:~/Working/EmbeddedArm/test_the_compiler$ cat -n stm32f10x_gpio.c | head -n189 |tail -n9
   181 /*---------------------------- GPIO Mode Configuration -----------------------*/
   182 currentmode = ((uint32_t)GPIO_InitStruct->GPIO_Mode) & ((uint32_t)0x0F);
   183 if ((((uint32_t)GPIO_InitStruct->GPIO_Mode) & ((uint32_t)0x10)) != 0x00)
   184 {
   185 /* Check the parameters */
   186 assert_param(IS_GPIO_SPEED(GPIO_InitStruct->GPIO_Speed));
   187 /* Output mode */
   188 currentmode |= (uint32_t)GPIO_InitStruct->GPIO_Speed;
   189 }

TBH, i'm a little out of my depth here.
it appears to me reading the value of "GPIO_InitStruct->GPIO_Mode" after having called memset() causes a HardFault?

interestingly, if i only memset() a single byte (eg, "memset(test, 0, 1)") the program runs as intended...

i suspect my Makefile is including the wrong standard libraries at link time...

any thoughts appreciated.
thanks,
dunk.

Question information

Language:
English Edit question
Status:
Solved
For:
GNU Arm Embedded Toolchain Edit question
Assignee:
No assignee Edit question
Solved by:
dunk
Solved:
Last query:
Last reply:
Revision history for this message
dunk (mrdunk) said :
#1

arg. i'm an eedjit.
after typing all that it made me look more closely at my includes...

my Makefile was not passing "-mthumb -march=armv7-m" to the linker.

the following exerts from the Makefile solves my problem:
  MCUFLAGS=-mthumb -march=armv7-m
  LDFLAGS=$(COMMONFLAGS) $(MCUFLAGS) -fno-exceptions -ffunction-sections -fdata-sections \
        -nostartfiles -Wl,--gc-sections,-T$(LINKER_SCRIPT) \
        -lc -lnosys -specs=nosys.specs

which invokes the following at link time:
  /home/duncan/Working/EmbeddedArm/gcc-arm-none-eabi-4_8-2014q1/bin//arm-none-eabi-gcc -o Demo.elf -O0 -g -Wall -Werror -mthumb -march=armv7-m -fno-exceptions -ffunction-sections -fdata-sections -nostartfiles -Wl,--gc-sections,-T"/home/duncan/Working/EmbeddedArm/test_the_compiler/stm32.ld" -lc -lnosys -specs=nosys.specs main.o stm32f10x_it.o system_stm32f10x.o STM32vldiscovery.o stm32f10x_rcc.o misc.o stm32f10x_exti.o stm32f10x_gpio.o startup_stm32f10x_md_vl.o