AnsweredAssumed Answered

Flash memory read/write

Question asked by Eunseok Jung on Feb 12, 2015
Latest reply on Mar 6, 2015 by Jorge_Gonzalez

Hi all,

I'm using MK10 series. I have some problem when i am coding the FlexNVM memory part.

What i want to do is that partition FlexNVM and save data in data flash region.

Below code is shown my FlexMem part.

It doesn't work what i annouced..

It starts from FlexMem_Init(void) function. And flow to erase function - partition function - Program longword function - read function.

I want to write '202' in the address 6FFFC.

 

Could you tell me what is the wrong?

 

Thanks.

 

 

/

*

* FlexMem.c

*

*  Created on: Feb 10, 2015

*      Author: ESJ

*/

#include <MK10D7.h>

#include <string.h>     //memset

#include "GLB_DataTypes.h"   //type define

#include "FlexMem.h"

 

 

uint8_t flashCommandArray[10];

uint16_t error_flash_code=0;  //0bit:partition error(already partitioned)

 

 

void FlexMem_Init(void);

int16_t partition_flash(int16_t eeprom_size_code, int16_t dflash_size_code,uint8_t* flashCommandArray);

void FlashCommandSequence (uint8_t* flashCommandArray);

void FlashEraseAllBlock (uint8_t *flashCommandArray);

void FlashProgramLongword(uint32_t address,uint32_t value);

void ReadResource (uint32_t address);

uint16_t i1, i2, i3, i4;

 

void FlexMem_Init(void)

{

    memset((uint8_t*)flashCommandArray, 0x00, sizeof(flashCommandArray));

    error_flash_code=MSG_FLASH_NORMAL;

 

    FlashEraseAllBlock(flashCommandArray);

    partition_flash(EEPROM_128, DFLASH_SIZE_08K, flashCommandArray);

    FlashProgramLongword(0x86FFFC, 202);

    ReadResource(0x86FFFC);

  

     i1=FTFL_FCCOB7; //for checking the saved value

     i2=FTFL_FCCOB6;

     i3=FTFL_FCCOB5;

     i4=FTFL_FCCOB4;

    

}

 

 

int16_t partition_flash(int16_t eeprom_size_code, int16_t dflash_size_code,uint8_t* flashCommandArray)

{

 

    *(flashCommandArray + 0) = 0x80;

    *(flashCommandArray + 4) = 0x30|eeprom_size_code; //FlexNVM is divided

    *(flashCommandArray + 5) = dflash_size_code; // FlexNVM is divide

    FlashCommandSequence(flashCommandArray);

    return 1;

}

 

 

void FlashCommandSequence (uint8_t * flashCommandArray )

{

    while(!(FTFL_FSTAT & FTFL_FSTAT_CCIF_MASK)){  //always check to write flash register

    }

 

    FTFL_FSTAT = (FTFL_FSTAT_RDCOLERR_MASK|FTFL_FSTAT_ACCERR_MASK|FTFL_FSTAT_FPVIOL_MASK);

    FTFL_FCCOB0 = *(flashCommandArray + 0);

    FTFL_FCCOB1 = *(flashCommandArray + 1);

    FTFL_FCCOB2 = *(flashCommandArray + 2);

    FTFL_FCCOB3 = *(flashCommandArray + 3);

    FTFL_FCCOB4 = *(flashCommandArray + 4);

    FTFL_FCCOB5 = *(flashCommandArray + 5);

    FTFL_FCCOB6 = *(flashCommandArray + 6);

    FTFL_FCCOB7 = *(flashCommandArray + 7);

    FTFL_FCCOB8 = *(flashCommandArray + 8);

    FTFL_FCCOB9 = *(flashCommandArray + 9);

    /* clear CCIF bit , activate command*/

    FTFL_FSTAT = FTFL_FSTAT_CCIF_MASK;

    while(!(FTFL_FSTAT & FTFL_FSTAT_CCIF_MASK)) {

        /* wait till CCIF bit is set */

    }

}

 

 

void FlashEraseAllBlock (uint8_t *flashCommandArray)

{

    *(flashCommandArray + 0)=0x44;

    FlashCommandSequence(flashCommandArray);

}

 

 

void FlashProgramLongword(uint32_t destination,uint32_t value)

{

  *(flashCommandArray + 0) = 0x06;

  *(flashCommandArray + 1) = (uint8_t)(destination >> 16);

  *(flashCommandArray + 2) = (uint8_t)((destination >> 8) & 0xFF);

  *(flashCommandArray + 3) = (uint8_t)(destination & 0xFF);

  *(flashCommandArray + 4) = (uint8_t)((value >> 24) &0xff);

  *(flashCommandArray + 5) = (uint8_t)((value >> 16) &0xff);

  *(flashCommandArray + 6) = (uint8_t)((value >> 8) &0xff);

  *(flashCommandArray + 7) = (uint8_t)((value >> 0) &0xff);

  FlashCommandSequence(flashCommandArray);

}

 

 

void ReadResource (uint32_t address)  //data Flash read(IFC)

{

    *(flashCommandArray+0) = 0x03; //read resource command

    *(flashCommandArray+1) = ((uint8_t)(address >> 16)); //23bit 0:Program Flash, 1:DataFlash

    *(flashCommandArray+2) = ((uint8_t)((address >> 8) & 0xFF));

    *(flashCommandArray+3) = ((uint8_t)(address & 0xFF));

    *(flashCommandArray+8) = 0x00; //0: IFR,1:Version ID

    FlashCommandSequence(flashCommandArray);

    *(flashCommandArray+0)= FTFL_FCCOB7; //addr   data (IFR:FC[3:0] : DEPART)

    *(flashCommandArray+1)= FTFL_FCCOB6; //addr+1 data (IFR:FD[3:0] : EESIZE)

    *(flashCommandArray+2)= FTFL_FCCOB5; //addr+2 data

    *(flashCommandArray+3)= FTFL_FCCOB4; //addr+3 data

}

Outcomes