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

Writing data in flash

Hello,

I am working in NCS and I am trying to save some data in flash before the SoC turns off.

I am starting by using the code below.

       //erase page 
       NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Een << NVMC_CONFIG_WEN_Pos;
       while (NRF_NVMC->READY == NVMC_READY_READY_Busy){}
       NRF_NVMC->ERASEPAGE = 0x0005A000;
       while (NRF_NVMC->READY == NVMC_READY_READY_Busy){}

  
       //write data in flash's page
       NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Wen << NVMC_CONFIG_WEN_Pos;
       while (NRF_NVMC->READY == NVMC_READY_READY_Busy){}
       *(uint32_t *)0x0005A000 =0x0000000AUL;
       NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Ren << NVMC_CONFIG_WEN_Pos;
       while (NRF_NVMC->READY == NVMC_READY_READY_Busy){}

If I use this code in the main() I can see the data written in flash. Instead, if I write the same code either in a gpio interrupt or in a thread, the SoC goes in Hard Fault and nothing is written in flash.

Can you help me to understand the problem?

Best Regards,

Crescenzo

Parents
  • I was not able to reproduce the hard fault. I ran your snippet from a thread and did not get any hard faults. I wrote 

    0x0000009AUL to address 0x0005A000 and confirmed that it was sucessfully exectuted:
    C:\Users\Simon>nrfjprog --memrd 0x00059FF0 --n 0x30
    0x00059FF0: FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF   |................|
    0x0005A000: 0000009A FFFFFFFF FFFFFFFF FFFFFFFF   |................|
    0x0005A010: FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF   |................|
    Could you test the same code and see if you get it to work:
    Best regards,
    Simon
  • Hi Simon,

    Your code works. However it starts breaking if you add some Bluetooth features. 

    For instance, if in your project, in prj.conf you add the following configurations to enable the bonding

    # Enable bonding
    CONFIG_BT_SETTINGS=y
    CONFIG_FLASH=y
    CONFIG_FLASH_PAGE_LAYOUT=y
    CONFIG_FLASH_MAP=y
    CONFIG_NVS=y
    CONFIG_SETTINGS=y

    and in the main you add:

            bt_enable(NULL);
    
         
    	if (IS_ENABLED(CONFIG_SETTINGS)) {
    		settings_load();
    	}

    The SoC goes in hard fault when attempts to write the flash in the thread.

    I noticed that everything works fine if you remove CONFIG_NVS=y from prj.conf.

    However, even without CONFIG_NVS,  if in the main you add a command to start the advertising, like below

            bt_le_adv_start(BT_LE_ADV_CONN, ad, ARRAY_SIZE(ad), sd,
    			     ARRAY_SIZE(sd));

    The SoC goes in hard fault again when trying to write in flash...

    I don't understand the correlation between Bluetooth Stack and Memory Controller, but something happens when you try to handle both in the same code.

    I attach the prj.cnf file I am working with.

    .myprj.conf

    Thank you.

    Best Regards,

    Crescenzo

Reply
  • Hi Simon,

    Your code works. However it starts breaking if you add some Bluetooth features. 

    For instance, if in your project, in prj.conf you add the following configurations to enable the bonding

    # Enable bonding
    CONFIG_BT_SETTINGS=y
    CONFIG_FLASH=y
    CONFIG_FLASH_PAGE_LAYOUT=y
    CONFIG_FLASH_MAP=y
    CONFIG_NVS=y
    CONFIG_SETTINGS=y

    and in the main you add:

            bt_enable(NULL);
    
         
    	if (IS_ENABLED(CONFIG_SETTINGS)) {
    		settings_load();
    	}

    The SoC goes in hard fault when attempts to write the flash in the thread.

    I noticed that everything works fine if you remove CONFIG_NVS=y from prj.conf.

    However, even without CONFIG_NVS,  if in the main you add a command to start the advertising, like below

            bt_le_adv_start(BT_LE_ADV_CONN, ad, ARRAY_SIZE(ad), sd,
    			     ARRAY_SIZE(sd));

    The SoC goes in hard fault again when trying to write in flash...

    I don't understand the correlation between Bluetooth Stack and Memory Controller, but something happens when you try to handle both in the same code.

    I attach the prj.cnf file I am working with.

    .myprj.conf

    Thank you.

    Best Regards,

    Crescenzo

Children
Related