AnsweredAssumed Answered

The example flexnvm code is not working on our processor : MK50DX256CLL10

Question asked by Shridhar rao on Aug 6, 2015
Latest reply on Aug 6, 2015 by Daniel Chen

Actually the example section in MQX 4.2 doesn't have support for this processor but we modified the code accordingly to our processor - when we run the code, the very first  fopen("flashx:flexram0", NULL);. fails. And we are unable to do anything after that. So we tried writing our own code.


The data written to FlexNVM is disappearing after a power reset if we try our own code  (just a simple sequence of commands to partition the FlexNVM and enable EEE and use direct read/writes to FlexRAM memory). I have tried opening Service requests but I only got answers that work on barebones but don't work with MQX.


Our need is to use FlexRAM to back some critical data inside E-Flash (FlexNVM) with MQX on MK50DX256CLL10. I suspect that MQX is erasing the Flash during boot. Because with our code, we are able to read and write to FlexNVM portion (through FlexRAM/EEE) when the power is retained. But after the processor boots after a power cycle, the data written to FlexNVM is lost. Our code is pasted below:


#include "derivative.h" /* include peripheral declarations */



#define LONGWORD_COUNTER_ADDR 0x14000000

#define WORD_COUNTER_ADDR 0x14000004

#define BYTE_COUNTER_ADDR 0x14000006



#define REG_READ(address)             ((unsigned char)(*(volatile unsigned char*)(address)))

#define WRITE8(address,value)         (*(volatile unsigned char*)(address) = (value))

#define READ8(address)                ((unsigned char)(*(volatile unsigned char*)(address)))



int partition_flash(int eeprom_size, int dflash_size)


      if ((SIM_FCFG1 & SIM_FCFG1_DEPART(0xF)) != 0x00000F00)


          return 0;



      /* clear RDCOLERR & ACCERR & FPVIOL flag in flash status register */

      FTFL_FSTAT = (0x40|0x20|0x10);

      /* Write the FCCOB registers */

      FTFL_FCCOB0 = FTFL_FCCOB0_CCOBn(0x80);


   FTFL_FCCOB1 = 0x00;

      FTFL_FCCOB2 = 0x00;

      FTFL_FCCOB3 = 0x00;



      FTFL_FCCOB4 = eeprom_size;

      FTFL_FCCOB5 = dflash_size;







      return 1;




void EEEWrite(unsigned long destination,unsigned long size,unsigned long source)


    if((FTFL_FCNFG & 0x01)==0x01)


            while(size > 0)


                while(0 == (FTFL_FCNFG & 0x01))






                if(REG_READ(FTFL_FSTAT & 0x10))




                destination += 1;

                size -= 1;

                source += 1;






void EEE_read_data(char *buffer, int length,int offset) {


  uint32_t receive_byte = 0;


  for (receive_byte = 0; receive_byte < length; receive_byte++) {

         buffer[receive_byte] = *((uint8_t *)(offset + receive_byte));






uint32_t temp_val_long_word = 0;

uint16_t temp_val_word = 0;

uint8_t temp_val_byte = 0;



int main(void)


    unsigned long source;

    unsigned long address;

    unsigned long size;

    unsigned char buffer[0x20] = {0x33,0x44,0x55,0x66,0x77,0x88};

    unsigned char dataread[0x20] = {0};


    //address = 0x14000000;





  if (partition_flash( 0x38, 0x5))


  *((uint32_t *)(LONGWORD_COUNTER_ADDR)) = 0x0;






  *((uint16_t *)(WORD_COUNTER_ADDR)) = 0x0;






  *((uint8_t *)(BYTE_COUNTER_ADDR)) = 0x0;







    address = 0x14000000;

    size = 0x06;

    source = (unsigned int)buffer;

    EEEWrite(address, size, source);

   // EEE_read_data(dataread,6,address);


    return 0;