samtal - How can I make it any clearer?
-------- case 1 ------
FLASH TO RAM
This case is extremely similar to RAM to RAM
memcpy(demo_array,flash_array,sizeof(demo_array))
always works for valid addresses.
SerialUSB.println((int)memcpy(demo_array,flash_array,sizeof(demo_array)),HEX);
works for me. It works every time for valid addresses.
I can only think of one way to stop it working.
It just works.
You do not have to do anything at all to make it work.
You do not have to unlock flash, or use the flash keys.
DO NOTHING to the flash controller.
memcpy(ram, flash, sizeof(ram)) works.
In fact, do NOT even TRY to do anything to the flash controller because that is the only thing that can make it fail (with valid addresses)
memcpy FROM flash TO ram WILL ALWAYS WORK.
It never ever fails for valid addresses, unless the flash memory controller is busy.
So for all uses of memcpy() for case 1, where the code does not attempt to use the Flash memory controller, and the addresses are valid, memcpy() always work.
------ case 2 -------
RAM TO FLASH
this is NOT like RAM to RAM, or FLASH to RAM
memcpy(flash_array,demo_array,sizeof(flash_array))
does not work
SerialUSB.println((int)memcpy(flash_array,demo_array,sizeof(demo_array)),HEX);
never works for me.
It does not matter if you use the Flash keys. That will not make it work.
It does not matter if you unlock Flash. That will not make it work
memcpy() will never ever work, no matter what sequence of Flash controller commands are used.
memcpy FROM ram TO flash will NEVER work. (Note 1)
IT WILL NEVER EVER WORK (Note 1)
So for all uses of memcpy() for case 2, no matter what you do to the Flash memory controller, memcpy() will not work
If you do nothing with the Flash memory controller, memcpy() for case 2 will not work.
memcpy() is not useful for copying memory for case 2.
---------------------------------------
You have read the flash memory programming manual. So I assume you understand how it works.
You have written the code to copy from ram to flash in your original program, and you got it to work.
It tests every single write to flash memory. That is necessary for it to work.
memcpy does not do that test.
None of the memxxx functions, builtins or macros are likely to do that test because they are generic memory-copying code designed to work on every CPU that C has ever run on.
I think this is a sufficient explanation of why
memcpy(flash_array,demo_array,sizeof(flash_array))
will not work.
The memxxx functions and builtins assume RAM to RAM.
(and implicitly FLASH to RAM, because the processor can just read Flash without any special machine code instructions)
Those memxxx functions do not know about writing to Flash.
You read in the Flash memory programming manual, you have to do an extra bunch of steps to program Flash.
YOU HAVE TO WRITE THE CODE TO COPY FROM RAM TO FLASH
You have already written the code in your earlier program. There is no work to do. It is done.
----------------
Summary:
memcpy(ram_array, flash_array, sizeof(ram));
will always work, if you leave the flash memory controller alone.
memcpy(flash_array, ram_array, sizeof(flash));
will never work. It will never work no matter what you do with the flash memory controller. It can not work. (Note 1)
Is this clear?
(Note 1: I think there is a way to copy exactly and only two bytes, but IMHO, there is no point wasting time on it. I'm not going to, it only confuses the issue)