Potential problems with memcpy() in ISR

npkz wrote on Monday, March 13, 2017:

Hello people,

are there any known problems on using memcpy() function inside of ISR? I’ve had some really odd problems while using it in ISR (I can get into those if needed), even though buffers that were being used as parameters for memcpy weren’t being shared by other tasks. I decided to manually do the copying instead of using memcpy(), and those strange problems disappeared.

Regards,
Nenad

heinbali01 wrote on Monday, March 13, 2017:

Hello Nenad, recently there was a post in which the standard memcpy() seemed to crash.
Could you try your code using memcpy() but also with the compiler option -fno-builtin-memcpy?

rtel wrote on Monday, March 13, 2017:

In addition to Hein’s valid comments: Are you using a Zynq? If so then
there are some library optimisations that use the floating point
registers even when they are not saved as the task or ISR context.
Hein’s option of creating your own memcpy function is a solution for
that too - but the latest FreeRTOS version also has an options to always
save floating point registers for all tasks and ISRs - it fixes the
problem at the expense of adding RAM and processing time overheads.

npkz wrote on Monday, March 13, 2017:

Hi Hein, thanks for your quick answer!
I have just added that compiler option, but it doesn’t help.
In that previous post you mentioned that there are memcpy() alternatives in /labs sw. I have downloaded it but couldn’t find specific functions, probably I am not searching right. Can you specify in which file I can see those?

npkz wrote on Monday, March 13, 2017:

Yes, I am using Zynq and FreeRTOS v8.2.3. So I am not able to make a use of that funcionality.

heinbali01 wrote on Monday, March 13, 2017:

I pointed to the wrong file. It should be:

    FreeRTOS-Plus/Demo/FreeRTOS_Plus_TCP_and_FAT_Zynq_SDK/RTOSDemo/src/memcpy.c

But don’t worry, I attached that file in this post.

It will replace the standard memcpy().

Regards.

heinbali01 wrote on Monday, March 13, 2017:

Forgot to mention, if you want to use the FPU and have its registers saved saved as the task- or ISR-context, please have a look here

npkz wrote on Monday, March 13, 2017:

Wow, I can’t thank you enough!
And I must say that this forum generally provides most helpful and fastest support!

heinbali01 wrote on Tuesday, March 14, 2017:

Thank you good to hear. But I’m curious about the results:

Which solution did you choose?

● Replace memcpy() with a new implementation
● Make sure the FPU registers are stored/restored as the task- or ISR-context

The reason for not storing FPU registers by default is performance: during each task switch, an extra 260 bytes must be saved and restored.

If you only have a single task that uses the FPU, you can tell the scheduler to store the registers of that task only by calling portTASK_USES_FLOATING_POINT().

It is all described here

npkz wrote on Thursday, March 16, 2017:

Yes, I have replaced memcpy() with a new implementation (and added mentioned compiler options).
Well, since I don’t have a need to use FPU at all in my app, I just decided to set configUSE_TASK_FPU_SUPPORT to 0.

thomask wrote on Tuesday, March 21, 2017:

I’ve run into similar problems, and built my own toolchain with Crosstool-NG.

You might be interested to try it out: https://github.com/thomask77/ct-ng-toolchains

aseris wrote on Monday, March 27, 2017:

Had issue memcpy crash like. The root cause was in incorrect pointer typecast in 3rd party open source code. The chain was: some uint16_t record in structure -> void * type pointer to this record - > uint32_t * type pointer to this record. In case of 32 bit architecture it causes strange behavior during built in memcpy.