This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Getting problem to write after 20 times data in flash...

Hello All,

I am working on nRF51822 use SDK nRF5 12.3.0 version in IAR workbench.

I am write data in the flash so for that i follow below steps,

Step 1: press button to start writing data in to flash.

step 1: write 2 data in to flash. and size of each data is 16-bit.

           below I add function(flash_adc_write) of write data in flash,

static void flash_word_write(uint32_t * address, uint32_t value)
{
    // Turn on flash write enable and wait until the NVMC is ready:
    NRF_NVMC->CONFIG = (NVMC_CONFIG_WEN_Wen << NVMC_CONFIG_WEN_Pos);

    while (NRF_NVMC->READY == NVMC_READY_READY_Busy)
    {
        // Do nothing.
    }

    *address = value;

    while (NRF_NVMC->READY == NVMC_READY_READY_Busy)
    {
        // Do nothing.
    }

    // Turn off flash write enable and wait until the NVMC is ready:
    NRF_NVMC->CONFIG = (NVMC_CONFIG_WEN_Ren << NVMC_CONFIG_WEN_Pos);

    while (NRF_NVMC->READY == NVMC_READY_READY_Busy)
    {
        // Do nothing.
    }
}

void flash_adc_write(uint16_t patwr_max, uint16_t patvr_min)
{
    addr = (uint32_t *)(pg_size * pg_num);
 
    // Erase page:
        sd_flash_page_erase(pg_num);
        //flash_page_erase(addr);
        nrf_delay_ms(5);
        flash_count++;

    if (patold != patvr_min)
    {
        patold = patvr_min;
        addr = addr + 2;
        flash_word_write(addr, (uint32_t)patvr_min);
        nrf_delay_us(50);
    }
    if(patold != patwr_max)
    {
        patold = patwr_max;
        addr = addr + 1;
        flash_word_write(addr, (uint32_t)patwr_max);
        nrf_delay_us(50);
    }
    
}

step 2: write 2 array in to flash and in this first array size is 8 byte and second array size is very from 4 to 20 byte.

           below I add function() of write array in flash,

void flash_name_write(char patwr[], char number[])
{
      int i = 0;
      //uint32_t size = 1;
      addr = (uint32_t *)(pg_size * (pg_num - 1));

      sd_flash_page_erase(pg_num - 1);      
      //flash_page_erase(addr);
      nrf_delay_ms(5);
      flash_count++;
      
      flash_word_write(++addr, 1);
      //addr = addr + 1;
      
      for(i=0; patwr[i] != '\0' ;i++)
      //for(i=0; i<8 ;i++)
      {
          addr = addr + 1;
          flash_word_write(addr, (uint32_t)patwr[i]);
          //APP_ERROR_CHECK(sd_flash_write(addr, (uint32_t *)patwr[i], 1));
          nrf_delay_us(50);
      }
      
      /*addr = addr + 1;
      flash_word_write(addr, '\0');*/
      
      for(i=0; i < 20; i++)
      {
          addr = addr + 1;
          flash_word_write(addr, (uint32_t)number[i]);
          //APP_ERROR_CHECK(sd_flash_write(addr, (uint32_t *)number[i], 1));
          nrf_delay_us(50);
      }
}
 

step 3: after this step I do soft reset for reset device.

When start application read all the data from flash for that I add function below,

typedef struct flash_variable
{
    uint16_t adc_max_value;
    uint16_t adc_min_value;
    char DEVICE_NAME[9];
    uint8_t SERIAL_NUMBER[21];
}FV;

void flash_adc_read(FV *fv)
{
        int i = 0;
        
	addr = (uint32_t *)(pg_size * pg_num);
        addr = addr + 2;
	fv->adc_min_value = (uint16_t)*(addr);
	fv->adc_max_value = (uint16_t)*(++addr);
        
        addr = (uint32_t *)(pg_size * (pg_num - 1));
        addr = addr + 1;
        for(i = 0; i<8; i++)
        {
            addr = addr + 1;
            fv->DEVICE_NAME[i] = (char)*(addr);
        }
        addr = addr + 1;
        fv->SERIAL_NUMBER[0] = (char)*(addr);
        for(i = 1;  i < 20; i++)
        {
            addr = addr + 1;
            fv->SERIAL_NUMBER[i] = (char)*(addr);
        }
}

When I follow below step continuous 20 time it work properly.

But after that i follow same step 21st time at this time I am getting problem in to step no 1 only but step 2 work properly.

so after 21st time I read flash in this my data is lost which is written in step no 1 only. 

My question is what is the problem and how can I resolve this issue?

Also i observe that when i am getting below problem in this case i am showing some fix data is present into last page which is not written by me for reference I add below image,

  

So my question is what is indicate flash data mention in image?

Please give me some solution of it as soon as possible.

Thanks & regards,

Urvisha Andani

Related