nordic52832 _ SDK_15.0---flash实现

1.参考\examples\peripheral\flash_fstorage\pca10040进行flash前期移植

2后续代码实现:

#define FS_STORAGE_START_ADDR 0x0007F000

static void fstorage_evt_handler(nrf_fstorage_evt_t * p_evt);

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 = FS_STORAGE_START_ADDR,
    .end_addr   = 0x0007FFFF,
};

/**@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));
}


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

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

        case NRF_FSTORAGE_EVT_ERASE_RESULT:
        {
            opt_debug("--> Event received: erased %d page from address 0x%x.\r\n",
                         p_evt->len, p_evt->addr);
        } break;

        default:
            break;
    }
}

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

/**@brief   Sleep until an event is received. */
static void power_manage(void)
{
#ifdef SOFTDEVICE_PRESENT
    (void) sd_app_evt_wait();
#else
    __WFE();
#endif
}

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();
    }
}

void opt_fstorage_init(void)
{
    ret_code_t rc;
    
    nrf_fstorage_api_t * p_fs_api;
#ifdef SOFTDEVICE_PRESENT
    /* Initialize an fstorage instance using the nrf_fstorage_sd backend.
     * nrf_fstorage_sd uses the SoftDevice to write to flash. This implementation can safely be
     * used whenever there is a SoftDevice, regardless of its status (enabled/disabled). */
    p_fs_api = &nrf_fstorage_sd;
#else
    /* Initialize an fstorage instance using the nrf_fstorage_nvmc backend.
     * nrf_fstorage_nvmc uses the NVMC peripheral. This implementation can be used when the
     * SoftDevice is disabled or not present.
     *
     * Using this implementation when the SoftDevice is enabled results in a hardfault. */
    p_fs_api = &nrf_fstorage_nvmc;
#endif

    rc = nrf_fstorage_init(&fstorage, p_fs_api, NULL);
    APP_ERROR_CHECK(rc);

    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. */
    rc = nrf5_flash_end_addr_get();        
}

//void opt_fstorage_write(nrf_fstorage_t const * p_fs,
//                                                void           const * p_src,
//                        uint32_t               len,)
void opt_fstorage_write(void *src,uint32_t src_size)
{
        ret_code_t rc;
        rc = nrf_fstorage_erase(&fstorage, FS_STORAGE_START_ADDR, 1, NULL);
        rc = nrf_fstorage_write(&fstorage, FS_STORAGE_START_ADDR, src, src_size, NULL);
    APP_ERROR_CHECK(rc);
        wait_for_flash_ready(&fstorage);
}

void opt_fstorage_read(void *dest,uint32_t dest_size)
{
    ret_code_t rc;
    rc = nrf_fstorage_read(&fstorage,FS_STORAGE_START_ADDR,dest,dest_size);
    APP_ERROR_CHECK(rc);

}

你可能感兴趣的:(nordic52832 _ SDK_15.0---flash实现)