This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

CC2340R5-Q1: we can write data into NVS only once

Part Number: CC2340R5-Q1
Other Parts Discussed in Thread: CC2340R5

Tool/software:

Hi team,

Currently I'm working on CC2340R5 device in that we are using NVS memory for read and write data from the nonvolatile memory, but we can write data into the only one after that we tried to write data into the NVS but NVS not accept data more than one times.

Write_data[0] = 0x24;  //example 
NVS_write(nvsHandle,0, (void *)Write data, 1, NVS_WRITE_POST_VERIFY);  // write data into the memory 

for read data we are using below function

NVS_read(nvsHandle, 0, (void *)read_data, size);

is there any settings needs to do for write & read data multiple time into the NVS memory.

please suggest any way to shoutout this problem.

SDK : simplelink_lowpower_f3_sdk_8_40_00_61

CCS ver : 12.7

Thanks & regards

Balaji Wankhede 

  • Hello,

    What is the behavior that you see when it fails to write? 

    Where in the code are you issuing these read/writes (BLE stack thread?)?

    Best,

    Nima Behmanesh

  • Hello Nima,

    I see the behavior when I'm write into the memory first time data write into the memory successfully but again same memory i want to overwrite data but it's not happened.

    for write I'm using below functions:

    NVS_write(nvsHandle,17, (void *) Write data, 1, NVS_WRITE_POST_VERIFY); // WRITE INTO THE MEMOEY

    I want to write into the 0x4011 memory location in that one byte of data we are writing continuous.

    Thanks & Regards

    Balaji Wankhede

  • Hello,

    I believe you must also erase before you write, if that section is already written. 

    Try this:

    NVS_write(nvsHandle,17, (void *) Write data, 1, NVS_WRITE_POST_VERIFY | NVS_WRITE_ERASE);

    I want to write into the 0x4011 memory location in that one byte of data we are writing continuous

    I'm a little concerned if you are continuously writing data to non-volatile flash, since this will wear the flash down much faster and is not recommended.

    Best,

    Nima Behmanesh

  • Hello Nima,

    Thank you for your response.

    I have already tried the suggested approach, but I'm encountering an issue. When I use the function, the entire sector gets erased before writing the data. However, I do not want to overwrite existing data in the same memory — only update the specific byte without affecting the rest of the sector.

    Here is the function I'm using:

    NVS_write(nvsHandle, 17, (void *)Write_data, 1, NVS_WRITE_POST_VERIFY | NVS_WRITE_ERASE);

    As shown above, I'm only writing 1 byte of data. However, all other previously written data in the same sector gets erased, which is not the intended behavior.

    Is there a way to update a single byte without erasing the rest of the sector?

    Thanks & regards

    Balaji Wankhede

  • Hello,

    When writing to non-volatile flash, the erase is necessary because writing is an AND operation (note, this is not a limitation of TI's APIs, but a limitation of flash). In order to update, you would need to read the sector out (or just the data stored in that sector), make whatever updates to that buffer, erase the sector, then write back the buffer with the updates desired. Alternatively, you could write the data to the same sector, but with an incrementing offset for each write (that is managed), until that sector is full. Once the sector is full, read out the data, erase, reset the offset, and re-write. Using the NVS driver means managing this process yourself.

    However, we usually recommend using the osal_snv wrapper that does this for you. osal_snv wraps the NV functions (NVINTF, NVS, and NVOCMP) to make writing to non-volatile memory easier:

    NVINTF:

    The NVINTF is an abstraction layer that defines a common set of APIs for interacting with non-volatile memory using an ID system. This common set of APIs allow for new methods of NV storage to be implemented without changing the API calls in the stack and application. The ID system is most efficient because it decouples the data stored from its address in flash. This is necessary because flash banks must have the entire sector erased before writing again (with the exception of clearing a bit). Using the ID system, when an NV item needs to be updated, it can simply be invalidated and stored again at a different address. Once the NV system becomes full of unused items, a compaction will occur. A compaction is the removal of unused items.

    For each NV item that is added or updated in NV storage, the item is written to the next lowest available memory address in the active flash page. If the NV item is being updated, the old NV item will be marked as inactive. Inactive items are removed from memory when a memory compaction takes place.

    I recommend reading the entirety of the documentation I provided above. Note that frequent writes to the non-volatile memory region will result as wear on flash and other limitations, and although the osal_snv will result in less erases, it may incur more latency when compaction occurs (removing inactive items, covered in the documentation above).

    Best,

    Nima Behmanesh