MQX FlashX driver – how to write FlexNVM?

Document created by Radek Sestak Employee on Jun 2, 2015
Version 1Show Document
  • View in full screen mode


1. Few basic questions and answers about FlexNVM and FlashX.

1.1 What is FlexNVM?

FlexNVM is additional separate block of flash memory which could be used as data flash, as non volatile storage for emulated EEPROM or as combination both option. I will focus on first option in this document – FlexNVM will work simply as data flash.

FlexNVM you can find in MCU parts which contain “X” at dedicated place in part number. For example:

MK64FX512VMD12 contains 1 block (512 KB) of program flash and 1 block (128 KB) of FlexNVM

MK64FN1M0VMD12 contains 2 blocks (512 KB each) of program flash only.

For more details about FlexNVM and flash blocks, please see your MCU reference manual. For example chapter “Flash Memory Sizes”.


1.2 What is FlashX?

MQX FlashX driver provide ability to write to and read from internal flash. Unfortunately FlexNVM memory is supported only partially in default state – Some of BSPs has implemented configuration and functions for emulated EEPROM (flexnvm example code). For more details, please check MQX_IO_User_Guide.pdf in c:\Freescale\Freescale_MQX_4_2\doc\mqx folder.


1.3 Can I use BSP for MCU without FlexNVM for my own board which has MCU with FlexNVM?

It is not recommended. However you can use this BSP as base for your own board BSP. Please check MQX_BSP_Cloning_Wizard_Getting_Started.pdf, MQX_BSP_Porting_Guide.pdf  and MQX_BSP_Porting_Example_User_Guide.pdf documents in C:\Freescale\Freescale_MQX_4_2\doc folder.


1.4 Can I use FlashX in my KSDK project?

Unfortunately FlashX driver was not implemented into KSDK. KSDK contains its own Standard Software Driver (SSD) for C90TFS/FTFx Flash family, however this is just low level driver without high level abstraction layer like in case of FlashX driver.


2. Procedure for update MQX FlashX driver to support FlexNVM.


2.1 Please backup these files: user_config.h, <your BSP>.h, init_flashx.c, flash_ftfl.c and flash_ftfe.c files. Note: user_config.h, <your BSP>.h and init_flashx.c are part of your BSP code.


2.2 Enable FlashX in user_config.h file by definition:

#define BSPCFG_ENABLE_FLASHX                      1


2.3 Updates of <your BSP>.h file:

2.3.1 Please check MCU reference manual and update BSP_INTERNAL_FLASH_BASE, BSP_INTERNAL_FLASH_SIZE, BSP_INTERNAL_FLASH_SECTOR_SIZE if necessary. Typically we have to decrease BSP_INTERNAL_FLASH_SIZE in case when BSP without FlexNVM was used as base for own BSP.


2.3.2 Add new macros which will define FlexNVM in memory map. For example:

#define BSP_INTERNAL_FLEXNVM_BASE  0x10000000

#define BSP_FLEXNVM_SECTOR_SIZE         0x400

#define BSP_INTERNAL_FLEXNVM_SIZE  0x00008000



2.4 Update init_flashx.c in your BSP folder:

2.4.1 Add FlexNVM file block into _bsp_flashx_file_blocks[] table. For example:

    // data flash file block


   Parameters are {name of file block, start address, end address}.

   Note: This is pure software interface; range of addresses doesn’t need fit to physical flash block parameters. You can organize file blocks according your needs.


2.4.2 If you used non-FlexNVM BSP as base for your own BSP, you have to change HW block map for KinetisX devices. Please change _flashx_kinetisN_block_map into _flashx_kinetisX_block_map in _bsp_flashx_init structure.


2.5 Update flash_ftfl.c or flash_ftfe.c file:

2.5.1 Look at MCU reference manual whether your MCU has FTFL or FTFE flash memory module and select appropriate file for editing.


2.5.2 Add FlexNVM memory block into _flashx_kinetisX_block_map[] table. For example:


Parameters are {number of sectors, start address, sector size}.

Note: This is description of physical hardware memory block; range of addresses must fit to physical flash block parameters.


2.5.3 Now we have to fix problem with FlexNVM address. Both Program Flash and FlexNVM Flash are programmed trough FTFL_FCCOBn / FTFE_FCCOBn registers where FCCOB1.. FCCOB3 contains address in 24bit format. Therefore we cannot work directly with FlexNVM addresses – they will not fit into 24bit due to FlexNVM base 0x10000000.

FTFL/FTFE modules specifies that most significant bit in 24bit address (bit 23) will be used for distinguish between Program Flash and FlexNVM Flash. We can use for example such code:

//Set 23th bit when FlexNVM flash address

    if (write_addr & BSP_INTERNAL_FLEXNVM_BASE)


write_addr = write_addr | (1 << 23);



and add this code into necessary functions prior write into command_array[] (content of command_array[] will be used for filling FTFL_FCCOBn / FTFE_FCCOBn registers).

For basic work of FlashX example code is necessary update at least ftfl_flash_erase_sector()/ftfe_flash_erase_sector() and ftfl_flash_write_sector()/ftfe_flash_write_sector() functions.


2.6. After these changes, you can try using FlashX example code in flash_demo.c where you simply open FlexNVM file block instead of default program flash file block. For example:

//#define FLASH_NAME "flashx:bank0"

#define FLASH_NAME "flashx:dflash"


In attachment file you can find example of modification for MQX4.2.0 and MK20DX72 MCU.



3. How to rewrite flash - general notes:

Flash data must be in the erased state before being programmed. Cumulative programming of bits (adding more zeros) is not allowed.

In case of both FTFL and FTFE modules we program flash by aligned phrases (typically 64 bits). If we want program by smaller chunks (e.g by bytes), FTFL module will allow you write into this phrase even if it is not recommended. However FTFE module will cause bus fault in case of second write into the same phrase. So, only save way how to change data in FTFE phrase which was already written is erase whole sector and rewrite data back. Therefore please use ioctl command FLASH_IOCTL_ENABLE_SECTOR_CACHE for FTFE module. The sector cache allocation is not required in case of Full sector write and partial sector overwrite when the destination area (aligned to phrases) is blank.


1 person found this helpful