Issues with memcpy

ChibiOS public support forum for all topics not covered by a specific support forum.

Moderators: RoccoMarco, lbednarz, utzig, tfAteba, barthess

reportingsjr
Posts: 7
Joined: Thu Jan 22, 2015 2:44 am

Issues with memcpy

Postby reportingsjr » Mon Mar 23, 2015 4:40 pm

Hi,

I've searched and searched and asked on irc channels and I haven't been able to figure out a problem I'm having with my code. I don't think it is related to chibios, but this is probably as good a forum as any to ask.

I'm running Chibios 3 and I've compiled by code both on the default fedora arm-none-eabi gcc toolchain and the linaro toolchain and I get the same/similar problems on both. This is on an stm32 discovery vl board (STM32F100) for reference.

What I'm trying to accomplish is passing a struct I've defined that is basically the GPIO bank and pin number for a certain pin to a function. I want to then store the pin struct locally to the driver for this chip I'm connecting to. I have a static struct defined in the file with this function and I've tried doing both a regular assignment (static pin struct = passed in pin struct) and I am currently trying to do a memcpy as was suggesting in the chibios IRC channel. When I tried doing the regular assignment nothing at all would happen. The static/local struct would stay uninitialized. When I try to memcpy the struct in gdb as I step through the instructions it seems to call memcpy about 4 times randomly and finally the local struct has the right values, but only after I've already tried to use it to toggle a pin state.

My code is here: https://github.com/reportingsjr/rfidBoa ... f_driver.c

It is the first function cr95hf_init that has the code I am trying to figure out.

Thanks for the help! I'll continue searching in the meantime.

User avatar
Giovanni
Site Admin
Posts: 14457
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1076 times
Been thanked: 922 times
Contact:

Re: Issues with memcpy

Postby Giovanni » Mon Mar 23, 2015 4:47 pm

Hi,

Are you sure it is not a compiler problem? you should try an ARM compiler for bare metal like: https://launchpad.net/gcc-arm-embedded

Giovanni

reportingsjr
Posts: 7
Joined: Thu Jan 22, 2015 2:44 am

Re: Issues with memcpy

Postby reportingsjr » Mon Mar 23, 2015 7:16 pm

That is actually one of the two compilers I have tried using (gcc-arm-none-eabi).

User avatar
Giovanni
Site Admin
Posts: 14457
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1076 times
Been thanked: 922 times
Contact:

Re: Issues with memcpy

Postby Giovanni » Mon Mar 23, 2015 7:55 pm

Hi,

Are you trying to debug code optimized with -O2, -O3 or -Os? that makes the debugger behave in a strange way because the code is reordered by the compiler, probably it is what you are seeing. If so then try compiling with -O0 if you need to debug.

Giovanni

reportingsjr
Posts: 7
Joined: Thu Jan 22, 2015 2:44 am

Re: Issues with memcpy

Postby reportingsjr » Mon Mar 23, 2015 9:46 pm

Ah, wonderful! I set optimizations to 0 as suggested and everything works fine now. It wasn't just the debugged that was acting up though. I have a LA hooked up to my dev board and it wasn't acting correctly either.

Thanks for the help Giovanni!

colin
Posts: 149
Joined: Thu Dec 22, 2011 7:44 pm

Re: Issues with memcpy

Postby colin » Wed Mar 25, 2015 9:13 pm

Occasionally you may encounter a compiler or toolchain bug. But most often when changing the compiler optimization level causes a change in behavior from C or C++ code, it indicates that the code is depending on some undefined behavior in the language. (See also A Guide to Undefined Behavior in C and C++.) For instance, in your case maybe there is a memory consistency issue, a place where you need to add a volatile qualifier to a pointer, or something. The compiler is allowed to do certain things to optimize the code, subject to some constraints that should produce the same result, BUT it is easy to forget what the language guarantees and expects from the programmer. Just something to think about.


Return to “General Support”

Who is online

Users browsing this forum: No registered users and 38 guests