can i store negative values in internal flash memory

hello,

i am using nrf52832 with sdk 15.2 i want to  store signed int values in to nvs (like rssi and tx power) so that i can change these in run time 

typedef struct {
  
  uint32_t s_no;
  int8_t rssi;
  int8_t tx_pwr;
  
  
} my_config_t;

my_config_t my_config_data =

    {

        .s_no=200000000,
        .rssi=-60,
        .tx_pwr=-4,



};

ret_code_t store_entire_data() {
  erase_flash_config();
  ret_code_t rc;
  rc = nrf_fstorage_write(&fstorage, NVS_START_ADDRESS, &my_config_data, sizeof(my_config_data), NULL);

  APP_ERROR_CHECK(rc);
  wait_for_flash_ready(&fstorage);
  return rc;
}
void fetch_config_data(void) {
  ret_code_t rc;
  my_config_data instance_data;
  rc = nrf_fstorage_read(&fstorage, NVS_START_ADDRESS, &instance_data, sizeof(instance_data));
  APP_ERROR_CHECK(rc);
    if (instance_data.s_no != 0xFFFFFFFF) {
    patient_tag_config_data.s_no = instance_data.s_no;

    printf("instance data s_no=%d\n", instance_data.s_no);
  }
  if (instance_data.rssi != #whatshould imput here) {
    patient_tag_config_data.rssi = instance_data.rssi;

    
  }
  if (instance_data.tx_pwr != #whatshould imput here) {
    patient_tag_config_data.tx_pwr = instance_data.tx_pwr;

     
  }
 in main()
 {
  fstorage_init(NVS_START_ADDRESS, NVS_END_ADDRESS);
    store_entire_data();
    fetch_config_data();
    fstorage_uninit();
 
 }

i can able to change the unsigned int variable but could not do the same for signed and also one more thing i am using this command to write on a particular address nrfjprog -f NRF52 --memwr 0x00071000 --val 0x0bebedaa --verify during flash time is there any way to change the content of these address during run time 

thanks and regards

manikandan v

  • Hi Manikandan, 

    When storing data with fds (or with fstorage) the struct with any data type will be stored as an array of byte. 

    So it doesn't matter if it's unsigned int or int. 
    What's important is that when you read the data out you need to assign the data to the correct struct variable.

    Please have a look at the example I provided here.  I edited configuration_t struct to add a "int test" 

    And in each reboot the test value will be decreased by 1. 

    Notice how the data is read out from the record and then memcpy to the truct at line 329 in main.c : 

            memcpy(&m_dummy_cfg, config.p_data, sizeof(configuration_t))

    If you open UART log you will see this: 

    Regarding your question about updating flash directly, it's possible to do so but it's not recommended to access flash directly if you are using fds. But you can do so if you want. You can either use nrf_fstorage_write() or you can use nrf_nvmc_write_words() to write to an address in flash. 

Related