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

52833 write flash error

    #define SIZE 10
    int i;
    int8_t ret;
    int main_addr_buff,backup_addr_buff;
    int write_buff[SIZE];
    for(i=0;i<SIZE;i++){
        write_buff[i] = 10+i;
    }
    if((ret = nrf_fstorage_erase(&fstorage, 0x74000, 1, NULL)) != NRF_SUCCESS)
        SLOG_E("nrf_fstorage_erase error,%d",ret);
    waitForFlashReady(&fstorage);

    if((ret = nrf_fstorage_write(&fstorage, 0x74000, write_buff, sizeof(int)*SIZE, NULL)) != NRF_SUCCESS)
        SLOG_E("nrf_fstorage_write error,%d",ret);
    waitForFlashReady(&fstorage);

    for(i = 0;i < 1024;i++){
        nrf_fstorage_read(&fstorage, 0x74000+i*4, &main_addr_buff, sizeof(int));
        DLOG_I("main_addr=0x%x,main_addr_buff=0x%x",0x74000+i*4,main_addr_buff);
    }

i write 40bytes to flash 0x74000 and read 0x74000。i find it actually use 160bytes. I try to write a different number of bytes, it actually use 4*(write bytes)

this is jflash image

Thank you for your reply!!

Parents
  • Hi,

    I do not see anything immediately wrong with the code. A scaling factor of 4 indicates a length error with number of bytes / number of words, but according to the API documentation for fstorage the length fields are in bytes and you provide the lengths in bytes so that should be correct.

    Is your code a modification of the fstorage example in nRF5 SDK v17.0.2?
    Can you share the full project for me to look into and try to reproduce?

    Regards,
    Terje

  • Hi,

    Yes,I refer to the fstorage example in nRF5 SDK v17.0.2.

    This is the full project.

    static void fstorage_evt_handler(nrf_fstorage_evt_t *p_evt)
    {
        if (p_evt->result != NRF_SUCCESS){
            FW_LogDebug("--> Event received: ERROR while executing an fstorage operation.");
            return;
        }
    
        switch (p_evt->id)
        {
            case NRF_FSTORAGE_EVT_WRITE_RESULT:
                FW_LogDebug("--> Event received: wrote %d bytes at address 0x%x.",
                            p_evt->len, p_evt->addr);
                break;
            case NRF_FSTORAGE_EVT_ERASE_RESULT:
                FW_LogDebug("--> Event received: erased %d page from address 0x%x.",
                                p_evt->len, p_evt->addr);
                break;
            default:
                break;
        }
    }
    
    NRF_FSTORAGE_DEF(nrf_fstorage_t fstorage) =
    {
        /* Set a handler for fstorage events. */
        .evt_handler = fstorage_evt_handler,
    
        /* These below are the boundaries of the flash space assigned to this instance of fstorage.
        * You must set these manually, even at runtime, before nrf_fstorage_init() is called.
        * The function nrf5_flash_end_addr_get() can be used to retrieve the last address on the
        * last page of flash available to write data. */
        .start_addr = NV_FS_START_ADDR,
        .end_addr = (NV_FS_START_ADDR + (4096*2)),
    };
    
    
    static void waitForFlashReady(nrf_fstorage_t const *pFstorageHandle)
    {
        /*While fstorage is busy, sleep and wait for an event*/
        while(nrf_fstorage_is_busy(pFstorageHandle)){
        //    sd_app_evt_wait();
        }
    }
    
    static int8_t flash_init()
    {
        nrf_fstorage_api_t *p_fs_api = &nrf_fstorage_sd;
        //For factory para init while power up
        nrf_fstorage_init(&fstorage, p_fs_api, NULL);
    }
    
    static int8_t flash_write_test()
    {
        #define SIZE        10
        #define TEST_ADDR   0x74000
        int i;
        int8_t ret;
        int main_addr_buff,backup_addr_buff;
        int write_buff[SIZE];
        for(i = 0; i < SIZE; i++){
            write_buff[i] = 10 + i;
        }
        if((ret = nrf_fstorage_erase(&fstorage, TEST_ADDR, 1, NULL)) != NRF_SUCCESS){
            DLOG_E("nrf_fstorage_erase error,%d",ret);
            return -1;
        }
        waitForFlashReady(&fstorage);
        DLOG_I("erase 0x74000 success");
        if((ret = nrf_fstorage_write(&fstorage, TEST_ADDR, write_buff, sizeof(int)*SIZE, NULL)) != NRF_SUCCESS){
            DLOG_E("nrf_fstorage_write error,%d",ret);
            return -1;
        }
        waitForFlashReady(&fstorage);
        DLOG_I("write 0x74000 success");
        for(i = 0; i < 1024; i++){
            nrf_fstorage_read(&fstorage, TEST_ADDR + i*4, &main_addr_buff, sizeof(int));
            DLOG_I("addr= 0x%x,main_addr_buff= 0x%x",TEST_ADDR + i*4, main_addr_buff);
        }
        return 0;
    }
    
    int8_t flash_test()
    {
        flash_init();
        flash_write_test();
    }

    Thank you for your reply!!

Reply
  • Hi,

    Yes,I refer to the fstorage example in nRF5 SDK v17.0.2.

    This is the full project.

    static void fstorage_evt_handler(nrf_fstorage_evt_t *p_evt)
    {
        if (p_evt->result != NRF_SUCCESS){
            FW_LogDebug("--> Event received: ERROR while executing an fstorage operation.");
            return;
        }
    
        switch (p_evt->id)
        {
            case NRF_FSTORAGE_EVT_WRITE_RESULT:
                FW_LogDebug("--> Event received: wrote %d bytes at address 0x%x.",
                            p_evt->len, p_evt->addr);
                break;
            case NRF_FSTORAGE_EVT_ERASE_RESULT:
                FW_LogDebug("--> Event received: erased %d page from address 0x%x.",
                                p_evt->len, p_evt->addr);
                break;
            default:
                break;
        }
    }
    
    NRF_FSTORAGE_DEF(nrf_fstorage_t fstorage) =
    {
        /* Set a handler for fstorage events. */
        .evt_handler = fstorage_evt_handler,
    
        /* These below are the boundaries of the flash space assigned to this instance of fstorage.
        * You must set these manually, even at runtime, before nrf_fstorage_init() is called.
        * The function nrf5_flash_end_addr_get() can be used to retrieve the last address on the
        * last page of flash available to write data. */
        .start_addr = NV_FS_START_ADDR,
        .end_addr = (NV_FS_START_ADDR + (4096*2)),
    };
    
    
    static void waitForFlashReady(nrf_fstorage_t const *pFstorageHandle)
    {
        /*While fstorage is busy, sleep and wait for an event*/
        while(nrf_fstorage_is_busy(pFstorageHandle)){
        //    sd_app_evt_wait();
        }
    }
    
    static int8_t flash_init()
    {
        nrf_fstorage_api_t *p_fs_api = &nrf_fstorage_sd;
        //For factory para init while power up
        nrf_fstorage_init(&fstorage, p_fs_api, NULL);
    }
    
    static int8_t flash_write_test()
    {
        #define SIZE        10
        #define TEST_ADDR   0x74000
        int i;
        int8_t ret;
        int main_addr_buff,backup_addr_buff;
        int write_buff[SIZE];
        for(i = 0; i < SIZE; i++){
            write_buff[i] = 10 + i;
        }
        if((ret = nrf_fstorage_erase(&fstorage, TEST_ADDR, 1, NULL)) != NRF_SUCCESS){
            DLOG_E("nrf_fstorage_erase error,%d",ret);
            return -1;
        }
        waitForFlashReady(&fstorage);
        DLOG_I("erase 0x74000 success");
        if((ret = nrf_fstorage_write(&fstorage, TEST_ADDR, write_buff, sizeof(int)*SIZE, NULL)) != NRF_SUCCESS){
            DLOG_E("nrf_fstorage_write error,%d",ret);
            return -1;
        }
        waitForFlashReady(&fstorage);
        DLOG_I("write 0x74000 success");
        for(i = 0; i < 1024; i++){
            nrf_fstorage_read(&fstorage, TEST_ADDR + i*4, &main_addr_buff, sizeof(int));
            DLOG_I("addr= 0x%x,main_addr_buff= 0x%x",TEST_ADDR + i*4, main_addr_buff);
        }
        return 0;
    }
    
    int8_t flash_test()
    {
        flash_init();
        flash_write_test();
    }

    Thank you for your reply!!

Children
  • Hi,

    You tagged the case with nRF52833, but the examples/peripheral/flash_fstorage examples of nRF5 SDK v17.0.2 are only for the boards pca10040 (nRF52832) and pca10056 (nRF52840). The DK for the nRF52833 is pca10100. Have you done anything to port the example to pca10100? If so, what?

    Also, which version (softdevice or blank), and for what board (pca10040 or pca10056) did you start with?

    Regards,
    Terje

Related