ruuvi.drivers.c  ${PROJECT_VERSION}
Drivers for external sensors and peripherals on embedded systems.
Flash storage.

Interface and implementations for storing data into flash in a persistent manner. More...

Files

file  ruuvi_interface_flash_test.h
 
file  ruuvi_interface_flash.h
 Interface functions to persistent flash storage.
 

Macros

#define RI_FLASH_PAGES   (10U)
 Enable implementation selected by application. More...
 

Functions

bool ri_flash_run_integration_test (const rd_test_print_fp printfp)
 Run all flash tests. More...
 
rd_status_t ri_flash_total_size_get (size_t *size)
 Get total size of usable flash, excluding any overhead bytes. More...
 
rd_status_t ri_flash_page_size_get (size_t *size)
 Get size of usable page, excluding any overhead bytes If returned value is N, a record of N bytes must fit in one page. More...
 
rd_status_t ri_flash_free_size_get (size_t *size)
 Get total size of free flash. More...
 
rd_status_t ri_flash_record_delete (const uint32_t file_id, const uint32_t record_id)
 Mark a record for deletion. More...
 
rd_status_t ri_flash_record_set (const uint32_t page_id, const uint32_t record_id, const size_t data_size, const void *const data)
 Set data to record in page. More...
 
rd_status_t ri_flash_record_get (const uint32_t page_id, const uint32_t record_id, const size_t data_size, void *const data)
 Get data from record in page. More...
 
rd_status_t ri_flash_gc_run (void)
 Run garbage collection. More...
 
rd_status_t ri_flash_init (void)
 
rd_status_t ri_flash_uninit (void)
 Unintialize flash. After uninitialization only initialization can be used. More...
 
void ri_flash_purge (void)
 Purge flash. More...
 
bool ri_flash_is_busy ()
 Check if flash is busy. More...
 
rd_status_t ri_flash_protect (const size_t page)
 

Detailed Description

Interface and implementations for storing data into flash in a persistent manner.

Macro Definition Documentation

◆ RI_FLASH_PAGES

#define RI_FLASH_PAGES   (10U)

Enable implementation selected by application.

Definition at line 37 of file ruuvi_interface_flash.h.

Function Documentation

◆ ri_flash_free_size_get()

rd_status_t ri_flash_free_size_get ( size_t *  size)

Get total size of free flash.

Parameters
[out]sizesize of useable storage in bytes.
Return values
RD_SUCCESSon success.
RD_ERROR_NULLif size is null.
RD_ERROR_INVALID_STATEif flash storage is not initialized.
errorcode from stack on other error.

◆ ri_flash_gc_run()

rd_status_t ri_flash_gc_run ( void  )

Run garbage collection.

Return values
RD_SUCCESSon success.
RD_INVALID_STATEif flash is not initialized.
errorcode from stack on other error.

◆ ri_flash_init()

rd_status_t ri_flash_init ( void  )

Initialize flash. After initialization other flash functions can be used.

Return values
RD_SUCCESSon success.
RD_ERROR_INVALID_STATEif flash is already initialized.
RD_ERROR_INVALID_LENGTHif space configured in linker does not match space reserved by implementation.

◆ ri_flash_is_busy()

bool ri_flash_is_busy ( )

Check if flash is busy.

Returns
True if flash is running an operation.
False if flash is idle.

◆ ri_flash_page_size_get()

rd_status_t ri_flash_page_size_get ( size_t *  size)

Get size of usable page, excluding any overhead bytes If returned value is N, a record of N bytes must fit in one page.

Parameters
[out]sizeSize of useable storage in bytes.
Return values
RD_SUCCESSon success.
RD_ERROR_NULLif size is null.
RD_ERROR_INVALID_STATEif flash storage is not initialized.
errorcode from stack on other error.

◆ ri_flash_protect()

rd_status_t ri_flash_protect ( const size_t  page)

Protects a page in flash against overwriting. After protection has been enabled, only reset will clear the protection.

Parameters
pageFlash page to protect
Returns
RD_SUCCESS on success
RD_ERROR_INVALID_PARAM if target chip does not have given page.

◆ ri_flash_purge()

void ri_flash_purge ( void  )

Purge flash.

This function is used to "rescue" flash which cannot be initialized normally, for example after data corruption. Completely erases all data content on flash. Does not erase application files.

This function is blocking, returns once flash is cleared.

◆ ri_flash_record_delete()

rd_status_t ri_flash_record_delete ( const uint32_t  file_id,
const uint32_t  record_id 
)

Mark a record for deletion.

Note that this only marks the record as freed and does not physically overwrite the flash. The function only starts the operation and returns immediately.

Parameters
[in]file_idID of file which contains the record.
[in]record_idID of record to delete.
Return values
RD_SUCCESSif deletion was queued.
RD_ERROR_INVALID_STATEif flash storage is not initialized.
RD_ERROR_BUSYif another operation was ongoing.
RD_ERROR_NOT_FOUNDif give record was not found.

◆ ri_flash_record_get()

rd_status_t ri_flash_record_get ( const uint32_t  page_id,
const uint32_t  record_id,
const size_t  data_size,
void *const  data 
)

Get data from record in page.

Returns after data is read and ready to be used or error has occured.

Parameters
[in]page_idID of a page. Can be random number.
[in]record_idID of a record. Can be a random number.
[in,out]data_sizeinput: Maximum size of data to retrieve. Output: Number of bytes retrieved.
[in]datapointer to memory which will be filled with retrieved data.
Return values
RD_SUCCESSon success.
RD_ERROR_NULLif data is null.
RD_ERROR_INVALID_STATEif flash storage is not initialized.
RD_ERROR_NOT_FOUNDif given page id does not exist or if given record_id does not exist on given page.
errorcode from stack on other error.

◆ ri_flash_record_set()

rd_status_t ri_flash_record_set ( const uint32_t  page_id,
const uint32_t  record_id,
const size_t  data_size,
const void *const  data 
)

Set data to record in page.

Automatically runs garbage collection if record cannot fit on page. Returns after data is successfully written or error has occured.

Parameters
[in]page_idID of a page. Can be random number.
[in]record_idID of a record. Can be a random number.
[in]data_sizesize data to store
[in]datapointer to data to store.
Return values
RD_SUCCESSon success
RD_ERROR_NULLif data is null
RD_ERROR_INVALID_STATEif flash storage is not initialized
RD_ERROR_BUSYif another operation is ongoing.
RD_ERROR_DATA_SIZEif record is too large to fit on page
RD_ERROR_NO_MEMif this record cannot fit on page.
errorcode from stack on other error

◆ ri_flash_run_integration_test()

bool ri_flash_run_integration_test ( const rd_test_print_fp  printfp)

Run all flash tests.

Parameters
[in]printfpFunction pointer to which test result is printed.
Returns
false if test had no errors, true otherwise.

◆ ri_flash_total_size_get()

rd_status_t ri_flash_total_size_get ( size_t *  size)

Get total size of usable flash, excluding any overhead bytes.

Parameters
[out]sizeSize of useable storage in bytes.
Return values
RD_SUCCESSon success.
RD_ERROR_NULLif size is null.
RD_ERROR_INVALID_STATEif flash storage is not initialized.
errorcode from stack on other error.

◆ ri_flash_uninit()

rd_status_t ri_flash_uninit ( void  )

Unintialize flash. After uninitialization only initialization can be used.

Return values
RD_SUCCESSon success.