nRF5 SDK v17.0.2
Modules | Data Structures | Macros | Typedefs | Enumerations | Functions
Flash storage (fstorage)

Flash abstraction library that provides basic read, write, and erase operations. More...

Modules

 Flash abstraction library configuration
 
 NVMC implementation
 API implementation of fstorage that uses the non-volatile memory controller (NVMC).
 
 SoftDevice implementation
 API implementation of fstorage that uses the SoftDevice.
 

Data Structures

struct  nrf_fstorage_evt_t
 An fstorage event. More...
 
struct  nrf_fstorage_info_t
 Information about the implementation and the flash peripheral. More...
 
struct  nrf_fstorage_t
 An fstorage instance. More...
 
struct  nrf_fstorage_api_s
 Functions provided by the API implementation. More...
 

Macros

#define NRF_FSTORAGE_DEF(inst)   NRF_SECTION_ITEM_REGISTER(fs_data, inst)
 Macro for defining an fstorage instance. More...
 
#define NRF_FSTORAGE_INSTANCE_GET(i)   NRF_SECTION_ITEM_GET(fs_data, nrf_fstorage_t, (i))
 Macro for retrieving an fstorage instance.
 
#define NRF_FSTORAGE_INSTANCE_CNT   NRF_SECTION_ITEM_COUNT(fs_data, nrf_fstorage_t)
 Macro for retrieving the total number of fstorage instances.
 

Typedefs

typedef void(* nrf_fstorage_evt_handler_t )(nrf_fstorage_evt_t *p_evt)
 Event handler function prototype. More...
 
typedef struct nrf_fstorage_api_s nrf_fstorage_api_t
 Functions provided by the API implementation.
 

Enumerations

enum  nrf_fstorage_evt_id_t {
  NRF_FSTORAGE_EVT_READ_RESULT,
  NRF_FSTORAGE_EVT_WRITE_RESULT,
  NRF_FSTORAGE_EVT_ERASE_RESULT
}
 Event IDs. More...
 

Functions

ret_code_t nrf_fstorage_init (nrf_fstorage_t *p_fs, nrf_fstorage_api_t *p_api, void *p_param)
 Function for initializing fstorage. More...
 
ret_code_t nrf_fstorage_uninit (nrf_fstorage_t *p_fs, void *p_param)
 Function for uninitializing an fstorage instance. More...
 
ret_code_t nrf_fstorage_read (nrf_fstorage_t const *p_fs, uint32_t addr, void *p_dest, uint32_t len)
 Function for reading data from flash. More...
 
ret_code_t nrf_fstorage_write (nrf_fstorage_t const *p_fs, uint32_t dest, void const *p_src, uint32_t len, void *p_param)
 Function for writing data to flash. More...
 
ret_code_t nrf_fstorage_erase (nrf_fstorage_t const *p_fs, uint32_t page_addr, uint32_t len, void *p_param)
 Function for erasing flash pages. More...
 
uint8_t const * nrf_fstorage_rmap (nrf_fstorage_t const *p_fs, uint32_t addr)
 Map a flash address to a pointer in the MCU address space that can be dereferenced. More...
 
uint8_t * nrf_fstorage_wmap (nrf_fstorage_t const *p_fs, uint32_t addr)
 Map a flash address to a pointer in the MCU address space that can be written to. More...
 
bool nrf_fstorage_is_busy (nrf_fstorage_t const *p_fs)
 Function for querying the status of fstorage. More...
 

Detailed Description

Flash abstraction library that provides basic read, write, and erase operations.

The fstorage library can be implemented in different ways. Two implementations are provided:

You can select the implementation that should be used independently for each instance of fstorage.

Macro Definition Documentation

#define NRF_FSTORAGE_DEF (   inst)    NRF_SECTION_ITEM_REGISTER(fs_data, inst)

Macro for defining an fstorage instance.

Users of fstorage must define an instance variable by using this macro. Each instance is tied to an API implementation and contains information such as the program and erase units for the target flash peripheral. Instance variables are placed in the "fs_data" section of the binary.

Parameters
[in]instA definition of an nrf_fstorage_t variable.

Typedef Documentation

typedef void(* nrf_fstorage_evt_handler_t)(nrf_fstorage_evt_t *p_evt)

Event handler function prototype.

Parameters
[in]p_evtThe event.

Enumeration Type Documentation

Event IDs.

Enumerator
NRF_FSTORAGE_EVT_READ_RESULT 

Unused event reserved for a possible future feature.

NRF_FSTORAGE_EVT_WRITE_RESULT 

Event for nrf_fstorage_write.

NRF_FSTORAGE_EVT_ERASE_RESULT 

Event for nrf_fstorage_erase.

Function Documentation

ret_code_t nrf_fstorage_erase ( nrf_fstorage_t const *  p_fs,
uint32_t  page_addr,
uint32_t  len,
void *  p_param 
)

Function for erasing flash pages.

This function erases len pages starting from the page at address page_addr. The erase operation must be initiated on a page boundary.

Parameters
[in]p_fsThe fstorage instance.
[in]page_addrAddress of the page to erase.
[in]lenNumber of pages to erase.
[in]p_paramUser-defined parameter passed to the event handler (may be NULL).
Return values
NRF_SUCCESSIf the operation was accepted.
NRF_ERROR_NULLIf p_fs is NULL.
NRF_ERROR_INVALID_STATEIf the module is not initialized.
NRF_ERROR_INVALID_LENGTHIf len is zero.
NRF_ERROR_INVALID_ADDRIf the address page_addr is outside the flash memory boundaries specified in p_fs, or if it is unaligned.
NRF_ERROR_NO_MEMIf no memory is available to accept the operation. When using the SoftDevice implementation, this error indicates that the internal queue of operations is full.
ret_code_t nrf_fstorage_init ( nrf_fstorage_t p_fs,
nrf_fstorage_api_t p_api,
void *  p_param 
)

Function for initializing fstorage.

Parameters
[in]p_fsThe fstorage instance to initialize.
[in]p_apiThe API implementation to use.
[in]p_paramAn optional parameter to pass to the implementation-specific API call.
Return values
NRF_SUCCESSIf initialization was successful.
NRF_ERROR_NULLIf p_fs or p_api field in p_fs is NULL.
NRF_ERROR_INTERNALIf another error occurred.
bool nrf_fstorage_is_busy ( nrf_fstorage_t const *  p_fs)

Function for querying the status of fstorage.

An uninitialized instance of fstorage is treated as not busy.

Parameters
[in]p_fsThe fstorage instance. Pass NULL to query all instances.
Returns
If p_fs is NULL, this function returns true if any fstorage instance is busy or false otherwise.
If p_fs is not NULL, this function returns true if the fstorage instance is busy or false otherwise.
ret_code_t nrf_fstorage_read ( nrf_fstorage_t const *  p_fs,
uint32_t  addr,
void *  p_dest,
uint32_t  len 
)

Function for reading data from flash.

Copy len bytes from addr to p_dest.

Parameters
[in]p_fsThe fstorage instance.
[in]addrAddress in flash where to read from.
[in]p_destBuffer where the data should be copied.
[in]lenLength of the data to be copied (in bytes).
Return values
NRF_SUCCESSIf the operation was successful.
NRF_ERROR_NULLIf p_fs or p_dest is NULL.
NRF_ERROR_INVALID_STATEIf the module is not initialized.
NRF_ERROR_INVALID_LENGTHIf len is zero or otherwise invalid.
NRF_ERROR_INVALID_ADDRIf the address addr is outside the flash memory boundaries specified in p_fs, or if it is unaligned.
uint8_t const* nrf_fstorage_rmap ( nrf_fstorage_t const *  p_fs,
uint32_t  addr 
)

Map a flash address to a pointer in the MCU address space that can be dereferenced.

Parameters
p_fsThe fstorage instance.
addrThe address to map.
Return values
Apointer to the specified address, or NULL if the address cannot be mapped or if p_fs is NULL.
ret_code_t nrf_fstorage_uninit ( nrf_fstorage_t p_fs,
void *  p_param 
)

Function for uninitializing an fstorage instance.

Parameters
[in]p_fsThe fstorage instance to uninitialize.
[in]p_paramAn optional parameter to pass to the implementation-specific API call.
Return values
NRF_SUCCESSIf uninitialization was successful.
NRF_ERROR_NULLIf p_fs is NULL.
NRF_ERROR_INVALID_STATEIf the module is not initialized.
NRF_ERROR_INTERNALIf another error occurred.
uint8_t* nrf_fstorage_wmap ( nrf_fstorage_t const *  p_fs,
uint32_t  addr 
)

Map a flash address to a pointer in the MCU address space that can be written to.

Parameters
p_fsThe fstorage instance.
addrThe address to map.
Return values
Apointer to the specified address, or NULL if the address cannot be mapped or if p_fs is NULL.
ret_code_t nrf_fstorage_write ( nrf_fstorage_t const *  p_fs,
uint32_t  dest,
void const *  p_src,
uint32_t  len,
void *  p_param 
)

Function for writing data to flash.

Write len bytes from p_src to dest.

When using SoftDevice implementation, the data is written by several calls to sd_flash_write if the length of the data exceeds NRF_FSTORAGE_SD_MAX_WRITE_SIZE bytes. Only one event is sent upon completion.

Note
The data to be written to flash must be kept in memory until the operation has terminated and an event is received.
Parameters
[in]p_fsThe fstorage instance.
[in]destAddress in flash memory where to write the data.
[in]p_srcData to be written.
[in]lenLength of the data (in bytes).
[in]p_paramUser-defined parameter passed to the event handler (may be NULL).
Return values
NRF_SUCCESSIf the operation was accepted.
NRF_ERROR_NULLIf p_fs or p_src is NULL.
NRF_ERROR_INVALID_STATEIf the module is not initialized.
NRF_ERROR_INVALID_LENGTHIf len is zero or not a multiple of the program unit, or if it is otherwise invalid.
NRF_ERROR_INVALID_ADDRIf the address dest is outside the flash memory boundaries specified in p_fs, or if it is unaligned.
NRF_ERROR_NO_MEMIf no memory is available to accept the operation. When using the SoftDevice implementation, this error indicates that the internal queue of operations is full.

Documentation feedback | Developer Zone | Subscribe | Updated