Jump to content

Recommended Posts

Posted (edited)

Hello,

after finally getting the EEPROM working which now holds the calibration data for the touchpad I get a hardfault. When the data is retrieved with ginput_lld_mouse_calibration_load(). The function returns a const char* to the previously saved calibration data.

The code now casts to the struct Calibration* and stores the pointer in pc. Then pc is dereferenced using the [] operator (same as *pc);

This causes the compiler to generate code which copies the struct completely. While executing this code I get a hardfault on a STM32F103ZET.

Exchanging the compiler generated code with memcpy all works fine.


if (MouseConfig.fnloadcal && (pc = (Calibration *)MouseConfig.fnloadcal(instance)))
{
//MouseConfig.caldata = pc[0];
memcpy(&MouseConfig.caldata,pc,sizeof(MouseConfig.caldata));
MouseConfig.flags |= (FLG_CAL_OK|FLG_CAL_SAVED);
if ((MouseConfig.flags & FLG_CAL_FREE))

Anybody here who can explain this?

My compiler ist arm-none-eabi-gcc.exe (GCC) 4.7.2.

Thanks for your help.

Regards

Michael

Edited by Guest
Posted

Interesting problem as the two methods should be completely equivalent.

FYI - I am currently using the same compiler with no troubles but with a different processor.

To give you some background...

  • There are a number of other places in the code where structure copy is used (although not all that commonly)
  • We generally try to avoid using memcpy() to avoid dependence on system header files and system libraries (which are not always 100% compatible)

Some things to look at:

  • Stack - perhaps the stack is being overrun?
  • Does changing the compiler optimiser flags change the code generated (ie could this be an optimiser problem)?
  • Exactly what hard-fault is being generated as that might indicate what is going on?
  • Perhaps it is the compiler flags for code generation are not quite right for your processor?
  • If it is using an implicit system library call to do the structure copy, perhaps the system library has been compiled incorrectly for your processor.
  • Is it a bus alignment issue - ie the memory returned by the load function is non-bus aligned and the structure copy is assuming bus aligned data.

If you can please investigate these things that would be appreciated as I would prefer to avoid the use of memcpy unless necessary. This problem may also affect other area's of the code if it is a systemic problem that we need to handle.

Thanks for bringing this to our attention.

Posted

Hi,

thank you very much for your detailed answer.

You nailed it with your last line. It is a alignment problem.

I was using a static buffer allocated by the compiler of course it was nicely aligned.

I am storing the calibration data in that buffer plus a header infront. This header is 3 bytes which is, I agree not very smart ;-)

I will change the headersize then the return pointer is aligned again.

Thanks again.

Michael

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...