nrf52810操作flash fstorge

具體移植可以參考例程E:\nRF5_SDK\nRF5_SDK_15.2.0_9412b96\examples\peripheral\flash_fstorage


static void fstorage_evt_handler(nrf_fstorage_evt_t * p_evt)
{
    if (p_evt->result != NRF_SUCCESS)
    {
//        NRF_LOG_INFO("--> Event received: ERROR while executing an fstorage operation.");
        return;
    }

    switch (p_evt->id)
    {
        case NRF_FSTORAGE_EVT_WRITE_RESULT:
        {
//            NRF_LOG_INFO("--> Event received: wrote %d bytes at address 0x%x.",
//                         p_evt->len, p_evt->addr);
        } break;

        case NRF_FSTORAGE_EVT_ERASE_RESULT:
        {
//            NRF_LOG_INFO("--> 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 = FSTORAGE_START_ADDR,
    .end_addr   = FSTORAGE_END_ADDR,
};

/**@brief   Helper function to obtain the last address on the last page of the on-chip flash that
 *          can be used to write user data.
 */
static uint32_t nrf5_flash_end_addr_get()
{
    uint32_t const bootloader_addr = NRF_UICR->NRFFW[0];
    uint32_t const page_sz         = NRF_FICR->CODEPAGESIZE;
    uint32_t const code_sz         = NRF_FICR->CODESIZE;

    return (bootloader_addr != 0xFFFFFFFF ?
            bootloader_addr : (code_sz * page_sz));
}

void wait_for_flash_ready(nrf_fstorage_t const * p_fstorage)
{
    /* While fstorage is busy, sleep and wait for an event. */
    while (nrf_fstorage_is_busy(p_fstorage))
    {
//        power_manage();
    }
}

static void print_flash_info(nrf_fstorage_t * p_fstorage)
{
//    NRF_LOG_INFO("========| flash info |========");
//    NRF_LOG_INFO("erase unit: \t%d bytes",      p_fstorage->p_flash_info->erase_unit);
//    NRF_LOG_INFO("program unit: \t%d bytes",    p_fstorage->p_flash_info->program_unit);
//    NRF_LOG_INFO("==============================");
}

void ble_flash_init(void)
{
    ret_code_t err_code;
    nrf_fstorage_api_t * p_fs_api;
    p_fs_api = &nrf_fstorage_sd;
    
    err_code = nrf_fstorage_init(&fstorage, p_fs_api, NULL);
  APP_ERROR_CHECK(err_code);
    print_flash_info(&fstorage);

    /* It is possible to set the start and end addresses of an fstorage instance at runtime.
     * They can be set multiple times, should it be needed. The helper function below can
     * be used to determine the last address on the last page of flash memory available to
     * store data. */
    (void) nrf5_flash_end_addr_get();    
//    ble_flash_erase_page(FSTORAGE_START_ADDR);
}

void ble_flash_erase_page(uint32_t fs_addr)
{
    ret_code_t err_code;
    uint32_t start_addr = FSTORAGE_END_ADDR - fs_addr;
    if(start_addr != 4096)
    {
        fs_addr = FSTORAGE_START_ADDR;
    }
    /* Let's write to flash. */
//    NRF_LOG_INFO("Writing \"%x\" to flash.", p_data);
    err_code = nrf_fstorage_erase(&fstorage, fs_addr, 1, NULL);//erase one page
    APP_ERROR_CHECK(err_code);
    wait_for_flash_ready(&fstorage);
}

void ble_flash_write(uint32_t fs_addr,uint32_t *p_data,uint32_t length)
{
    ret_code_t err_code;
    uint32_t start_addr = FSTORAGE_END_ADDR - fs_addr;
    if(start_addr != 4096)
    {
        fs_addr = FSTORAGE_START_ADDR;
    }
    /* Let's write to flash. */
//    NRF_LOG_INFO("Writing \"%x\" to flash.", p_data);
    err_code = nrf_fstorage_erase(&fstorage, fs_addr, 1, NULL);//erase one page
    APP_ERROR_CHECK(err_code);
    wait_for_flash_ready(&fstorage);
    err_code = nrf_fstorage_write(&fstorage, fs_addr, p_data, length, NULL);
    APP_ERROR_CHECK(err_code);

    wait_for_flash_ready(&fstorage);
//    NRF_LOG_INFO("Done.");

}

void ble_flash_read(uint32_t fs_addr,uint32_t *p_data,uint32_t length)
{

    ret_code_t err_code;
    /* Let's write to flash. */
//    NRF_LOG_INFO("Writing \"%x\" to flash.", p_data);
    err_code = nrf_fstorage_read(&fstorage, fs_addr, p_data, length);
    APP_ERROR_CHECK(err_code);

    wait_for_flash_ready(&fstorage);
//    NRF_LOG_INFO("Done.");

}

這裏需要注意的幾點:

1、寫flash的時候,必須先操除,然後再寫,不然會出錯,因爲flash的bit只能寫0,不能寫1.

2、操除與寫的操作直接影響到flash的使用壽命,讀則沒影響,芯片可以操除多少次可以看芯片說明書

3、注意這裏的寫和讀都是32bit的,如你寫入0x123456,在芯片的存入是這樣的 56 34 12 00,所以你可以將8bit的轉成32bit的再存,高低位可以自己控制

4、讀數據的時候,我們可以不用nrf_fstorage_read(&fstorage, fs_addr, p_data, length);函數,有的時候讀的數據多了,它會直接報錯,所以我們直接用memcpy()函數,直接讀地址,將地址裏的數copy出來,這樣就不會報錯了。

5、sdk_config.h 中需要修改:#define NRF_FSTORAGE_ENABLED 1

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章