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: nvs write and read

Part Number: CC2340R5
Other Parts Discussed in Thread: SYSCONFIG, UNIFLASH

Tool/software:

Hii am writting some data in nvs by using the api


NVS_Handle nvsRegion;
NVS_Attrs regionAttrs;
NVS_Params nvsParams;
uint_fast16_t status;

uint8_t buffer[16];
uint8_t buffer2[1];
extern uint8_t aesKey[16];
extern uint8_t nvmExecutionFlagarray[1];
extern uint8_t nvmExecutionFlag[1];
uint8 flag;

// Initialize NVM
void NVM_INIT() {
// Initialize the NVS driver
NVS_init();

// Initialize NVS parameters
NVS_Params_init(&nvsParams);

// Open the NVS region
nvsRegion = NVS_open(CONFIG_NVS_0, &nvsParams);
if (nvsRegion == NULL) {
// Handle error if the region could not be opened
// Log or implement error handling
return;
}

// Get attributes of the NVS region
NVS_getAttrs(nvsRegion, &regionAttrs);

// Erase the first sector of the NVS region
// status = NVS_erase(nvsRegion, 0, regionAttrs.sectorSize);
if (status != NVS_STATUS_SUCCESS) {
// Handle erase failure
// Log or implement error handling
return;
}
}

// Write AES key to NVM
void NVM_WRITE_AES_KEY()
{


// Write AES key to NVS
status = NVS_write(nvsRegion, 0x00, &aesKey, sizeof(aesKey), NVS_WRITE_POST_VERIFY);
if (status != NVS_STATUS_SUCCESS) {
// Handle write failure
// Log or implement error handling
return;
}
NVS_close(nvsRegion);
}

// Read AES key from NVM
void NVM_READ_AES_KEY()
{


// Read AES key from NVS
status = NVS_read(nvsRegion, 0x00, buffer, sizeof(buffer));
if (status != NVS_STATUS_SUCCESS) {
// Handle read failure
// Log or implement error handling
return;
}
NVS_close(nvsRegion);
}

//// Write execution flag to NVM
void NVM_WRITE_EXECUTION_FLAG()
{


// Write execution flag to NVS
status = NVS_write(nvsRegion, 0x16, &nvmExecutionFlagarray,sizeof(nvmExecutionFlagarray), NVS_WRITE_POST_VERIFY);
if (status != NVS_STATUS_SUCCESS) {
// Handle write failure
// Log or implement error handling
return;
}
flag=1;
NVS_close(nvsRegion);
}
//
//// Read execution flag from NVM
void NVM_READ_EXECUTION_FLAG()
{


// Read execution flag from NVS
if(flag)
{
status = NVS_read(nvsRegion, 0x16, buffer2,sizeof(buffer2));
}
flag=0;
if (nvmExecutionFlag[0] == 0xFF)
{
nvmExecutionFlag[0] = 0x00; // Set the flag to 0x00
status = NVS_write(nvsRegion, 16, nvmExecutionFlag, sizeof(nvmExecutionFlag), NVS_WRITE_POST_VERIFY);
if (status != NVS_STATUS_SUCCESS)
{
// Handle write failure
return;
}

if (status != NVS_STATUS_SUCCESS) {
// Handle read failure
// Log or implement error handling
return;
}

}
NVS_close(nvsRegion);
}


i have created this function to write and read the data from nvs it is working fine , but when the power is shutdown and again power is given it is not able to read the data stored in nvs.

any suggestions

i have also attached the sysconfig setting which i have done.

  • Hi Amrendra,

    Thank you for reaching out! I will look into this and get back to you by end of day tomorrow!

    Regards,

    Tarek

  • Hi Amrendra,

    I believe it would be beneficial to take a look at the "nvsinternal" example provided inside the SDK, as it's a very helpful resource when using the NVS API. It implements reading from and writing to NVS. NVSinternal . 

    Also, if you would like shutdown versatility, please take a look at the "gpioshutdown" example also provided inside the SDK. It demonstrates how to properly shutdown and wake up (release latches) from shutdown. GPIOshutdown

    Hopefully this helps!

    Regards,
    Tarek

  • shutdown issue is resolved.

    And  for read and write , i have gone through the example code and i am able to write and read but the issue is that after power cut they are not having the values

  • Hi Amrendra,

    Can you explain how you're testing this? Please keep in mind, debugging through code composer studio results in re-flashing the board, therefore, the NVS would be automatically erased. To check the memory address to make sure the data is still present, you could download uniflash and use it's memory browser to check the data at a specific location. Based on the sysconfig screenshot you provided, the data would start at address 0x10000. 

    Best Regards,
    Tarek