-2

Starting up a STM32 i try to allocate memory for a struture pointed to by a pointer.

TLxbEvents *LxbEvents
memset((void*)LxbEvents, 0, sizeof(TLxbEvents));

Looking into the disassembly, it crashes always on the line

STMCS r0!,{r2-r3,r12,lr}

I could not find a document describing the STMCS instruction nether on ARM website or Google or elsewhere... The registers at that point are

r0  0x2000D694
r2  0x00000000
r3  0x00000000
r12 0x00000000
lr  0x00000000

I tried to move the call to another routine, without any changes, checked the alignment and that also seems to be okay. Everytime the program runs into that line it crashes with a HardFault and according to some debug variables, it is caused by a watchdog reset, what i do not believe...

What does this line do and has someone an idea, what is causing the hard fault?

Aeonos
  • 365
  • 1
  • 13
  • 5
    You have a pointer `LxbEvents`, but where does it point? – Some programmer dude Apr 21 '17 at 11:51
  • `r0`, so 0x2000D694 somewhere in SRAM and using the Memory Viewer i can see that everything there is zeroed – Aeonos Apr 21 '17 at 11:52
  • Declare before using! – CinCout Apr 21 '17 at 11:52
  • 2
    I mean, you *do* make it actually *point* somewhere? You do initialize the variable, or assign to it? Otherwise you either dereference a null pointer (if the variable is global) or it will have an indeterminate value (if it's a local variable). In both those cases that will lead to *undefined behavior*. – Some programmer dude Apr 21 '17 at 11:54
  • i do ... but, when i try to do something like `LxbEvents = (TLxbEvents*)malloc(sizeof(TLxbEvents))` that also results in a hard fault. I am absolutly clueless at this point why this happens – Aeonos Apr 21 '17 at 12:07
  • It is in all the arm architectural reference manuals CS is carry set STM is store multiple STMCS store multiple if carry set. This is probably an alignment issue, and/or you are using an arm instruction in thumb mode or on a thumb only core (which does not support arm instructions like that). – old_timer Apr 21 '17 at 13:06
  • were you trying to zero the pointer itself using memset? just declare it zero. If you want to zero memory then you need to allocate some first, set the pointer to point at it then you can use the pointer. arm and memset have nothing to do with it at this point, it is simply incorrect use of a pointer. – old_timer Apr 21 '17 at 13:08
  • It is not documentet in all reference manual, i have checked at least 3 without finding any reference to STMCS, stmcs or STM_CS, stm_cs. Thanks to the guy for the down vote without giving any reason! – Aeonos Apr 21 '17 at 13:16

1 Answers1

2

STMCS is an ARM instruction (base instruction is STM and CS is the conditional instruction suffix) It seems you are compiling your code in ARM mode, but STM32 is a Cortex-M core and only supports Thumb-2 instruction set variant. Double-check you build settings and compilation switches.

Igor Skochinsky
  • 24,629
  • 2
  • 72
  • 109
  • Thanks for the info. I compile using the `--cpu Cortex-M3 ` flag in Keil µVision using the ARM Compiler v6.6 – Aeonos Apr 24 '17 at 04:47