Does flashx manage the writing of data to the flash in phrase-sized chunks?

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Does flashx manage the writing of data to the flash in phrase-sized chunks?

Jump to solution
814 Views
gorakk
Contributor IV

If I want to perform multiple writes to a flashx file - that could each be less than a phrase - does the flashx driver accumulate bytes until it has a phrase and then write that to flash?  Or do I need to manage that myself?  The phrase size for the processor I am using appears to be 8 bytes.

From some experimentation it seems that I have to manage it myself but maybe I am missing a setting?

My experiments are based on the flashx example, flash_demo.c

This works - but obviously leaves gaps in the file:

   fseek(flash_file, 0, IO_SEEK_SET); 

   buffer[0] = 'a'; 

   write(flash_file, buffer, 1); 

   

   fseek(flash_file, 8, IO_SEEK_SET); // move to the next phrase   

   buffer[0] = 'b'; 

   write(flash_file, buffer, 1); 

 

   fseek(flash_file, 16, IO_SEEK_SET); // move to the next phrase 

   buffer[0] = 'c'; 

   write(flash_file, buffer, 1); 

This doesn't work - the second and third write() give a write error:

   fseek(flash_file, 0, IO_SEEK_SET); 

   buffer[0] = 'a'; 

   write(flash_file, buffer, 1); 

   

   fseek(flash_file, 1, IO_SEEK_SET); 

   buffer[0] = 'b'; 

   write(flash_file, buffer, 1); 

 

   fseek(flash_file, 2, IO_SEEK_SET); 

   buffer[0] = 'c'; 

   write(flash_file, buffer, 1);   

TWR-K60F120M, CodeWarrior 10.5, MQX 4.0

0 Kudos
1 Solution
435 Views
RadekS
NXP Employee
NXP Employee

Yes, FTFE module doesn’t allow cumulative write into the same phrase even in case when target bytes are erased (0xFF).

If you want to modify data in already written phrase, please use ioctl commands FLASH_IOCTL_ENABLE_SECTOR_CACHE and FLASH_IOCTL_ENABLE_BUFFERING. This will cause that whole sector will be loaded into RAM, data will be modified by new values and the sector will be erased and newly programmed again. 

Unfortunately MQX 4.1.1 contains bug and cumulative write into the same phrase to empty bytes will not be recognized correctly. As workaround please edit _io_flashx_write_partial_sector() function in flashx.c file and replace part of code:

    /* Is the write area erased?

    * And make sure these is no content in cache. Otherwise, flush cache will lost data programmed here. */

    if (!file_ptr->DIRTY_DATA && !_io_flashx_check_free_space(data_dest_ptr, bytes_to_write)) {

        /* Yes, write data */

        if (!(*dev_ptr->SECTOR_PROGRAM)(dev_ptr, src_ptr, data_dest_ptr, bytes_to_write)) {

            return (_mem_size) IO_ERROR;

        } /* Endif */

    } 

by:

    /* Is the write area erased?

    * And make sure these is no content in cache. Otherwise, flush cache will lost data programmed here. */

    if (!file_ptr->DIRTY_DATA && !_io_flashx_check_free_space(data_dest_ptr, bytes_to_write) \

        && !(file_ptr->FLAGS & FLASHX_FLASH_BUFFER_ENABLED)) {

        /* Yes, write data */

        if (!(*dev_ptr->SECTOR_PROGRAM)(dev_ptr, src_ptr, data_dest_ptr, bytes_to_write)) {

            return (_mem_size) IO_ERROR;

        } /* Endif */

    }

This will be fixed in MQX4.2

Unfortunately flahx driver in MQX 4.0 contains some additional bugs which were already fixed in MQX 4.1.1 therefore I am attaching flashx files from MQX 4.1.1 as reference. Please modify your original MQX 4.0 flashx files according attached files and upper described workaround.

Note: you cannot use MQX 4.1.1 files directly due to incompatibility in used data types/pointers.

I hope it helps you.

Have a great day,
RadekS

-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------

View solution in original post

0 Kudos
2 Replies
436 Views
RadekS
NXP Employee
NXP Employee

Yes, FTFE module doesn’t allow cumulative write into the same phrase even in case when target bytes are erased (0xFF).

If you want to modify data in already written phrase, please use ioctl commands FLASH_IOCTL_ENABLE_SECTOR_CACHE and FLASH_IOCTL_ENABLE_BUFFERING. This will cause that whole sector will be loaded into RAM, data will be modified by new values and the sector will be erased and newly programmed again. 

Unfortunately MQX 4.1.1 contains bug and cumulative write into the same phrase to empty bytes will not be recognized correctly. As workaround please edit _io_flashx_write_partial_sector() function in flashx.c file and replace part of code:

    /* Is the write area erased?

    * And make sure these is no content in cache. Otherwise, flush cache will lost data programmed here. */

    if (!file_ptr->DIRTY_DATA && !_io_flashx_check_free_space(data_dest_ptr, bytes_to_write)) {

        /* Yes, write data */

        if (!(*dev_ptr->SECTOR_PROGRAM)(dev_ptr, src_ptr, data_dest_ptr, bytes_to_write)) {

            return (_mem_size) IO_ERROR;

        } /* Endif */

    } 

by:

    /* Is the write area erased?

    * And make sure these is no content in cache. Otherwise, flush cache will lost data programmed here. */

    if (!file_ptr->DIRTY_DATA && !_io_flashx_check_free_space(data_dest_ptr, bytes_to_write) \

        && !(file_ptr->FLAGS & FLASHX_FLASH_BUFFER_ENABLED)) {

        /* Yes, write data */

        if (!(*dev_ptr->SECTOR_PROGRAM)(dev_ptr, src_ptr, data_dest_ptr, bytes_to_write)) {

            return (_mem_size) IO_ERROR;

        } /* Endif */

    }

This will be fixed in MQX4.2

Unfortunately flahx driver in MQX 4.0 contains some additional bugs which were already fixed in MQX 4.1.1 therefore I am attaching flashx files from MQX 4.1.1 as reference. Please modify your original MQX 4.0 flashx files according attached files and upper described workaround.

Note: you cannot use MQX 4.1.1 files directly due to incompatibility in used data types/pointers.

I hope it helps you.

Have a great day,
RadekS

-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------

0 Kudos
435 Views
gorakk
Contributor IV

RadekS,

Thanks for the answer! :smileyhappy:   In the end I decided to move my project to 4.1.1 and include the fix that you described.

Allen

0 Kudos