AnsweredAssumed Answered

K20 and K60 vs flash erase and most likely cache

Question asked by Adam Rudziński on Jul 22, 2019
Latest reply on Jul 29, 2019 by Adam Rudziński

Dear NXP,

I'm developing a firmware which is supposed to store some data in internal flash. The target hardware used by the customer is based on MK20DN512ZVMD10, while for development I have your tower system with MK60DN512VMD10. We both have a recent MCUXpresso and SDK for K20.
I expected that these two microcontrollers would be compatible, but there are some differences in their operation regarding flash. Could you, please, help me with that?


1) Erasing flash
a) On my K60 the function fsl_flash_erase works fine, but on the target K20 it causes a reset.

b) We've also tried a "custom" erase function according to the description in the manual. In pseudocode it goes like this:


wait for completion of previous operation (CCIF -> 0)
delay (for loop)

clear ACCERR and FPVIOL fields - just in case

load values to FCCOBx registers

set CCIF to start erase

delay (for loop)

wait for completion of the operation (CCIF -> 0)


Note the two delay loops. On my K60 the code runs fine without them, but they turned out to be needed for the target K20. Without them a reset occurs.
The delay loop works just like this:

volatile unsigned int i=0, tmp=0;

for (i=0 ;i<1000000 ; ++i) { tmp += 0; }

Also, note that the function at the end waits for the operation to complete.


2) Reading flash after write

This was not working well on both K20 and K60 until we applied some mechanism to refresh the cache.

On the K60 is seems that functions from the SDK handle that correctly.
On the target K20 that was not the case, in particular flash_cache_clear did not help. What did work for us was a number of "dummy reads":

static uint32_t read_val(void)


       uint32_t v = 0;

       // ugly hack to ensure correct reading of flash memory

       for (int i=0 ; i<10 ; ++i)


             v = data.val;


       return v;


Above, the structure "data" is located in the flash range of interest. It seems that reading twice is sufficient.


If you could shed any light on this case, please, help me understand what is happening.


Best regards,
Adam Rudziński