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

Non-volatile storage functions. More...

Files

file  ruuvi_task_flash.h
 

Functions

rd_status_t rt_flash_init (void)
 Initialize flash storage. More...
 
rd_status_t rt_flash_store (const uint16_t file_id, const uint16_t record_id, const void *const message, const size_t message_length)
 Store data to flash. More...
 
rd_status_t rt_flash_load (const uint16_t file_id, const uint16_t record_id, void *const message, const size_t message_length)
 Load data from flash. More...
 
rd_status_t rt_flash_free (const uint16_t file_id, const uint16_t record_id)
 Free data from flash. More...
 
rd_status_t rt_flash_gc_run (void)
 Trigger garbage collection. More...
 
bool rt_flash_busy (void)
 Check if flash is running an operation. More...
 

Detailed Description

Non-volatile storage functions.

Function Documentation

◆ rt_flash_busy()

bool rt_flash_busy ( void  )

Check if flash is running an operation.

Return values
trueif flash is busy.
falseif flash is not runnning operation.

Definition at line 209 of file ruuvi_task_flash.c.

◆ rt_flash_free()

rd_status_t rt_flash_free ( const uint16_t  file_id,
const uint16_t  record_id 
)

Free data from flash.

The flash storage implements a simple file system, where data is arranged to files which have records. You can for example have a file for sensor configurations and record for each sensor.

This function does not physically erase the data, it only marks the record as deleteable for garbage collection.

Parameters
[in]file_idID of a file to delete. Valid range 1 ... 0xBFFF
[in]record_idID of a record to delete. Valid range 1 ... 0xFFFF
Return values
RD_SUCCESSon success.
RD_ERROR_INVALID_STATEif flash is not initialized.
RD_ERROR_BUSYif another operation was ongoing.
RD_ERROR_NOT_FOUNDif given record was not found.
Warning
Triggers garbage collection if there is no space available, which leads to long processing time.

◆ rt_flash_gc_run()

rd_status_t rt_flash_gc_run ( void  )

Trigger garbage collection.

Free up space in flash by erasing old records. It's generally a good idea to check remaining space on flash after a write and trigger GC if the space remaining is smaller than expected record sizes. This function physically erases the records which are garbage collected. Waits until GC can be triggered, and returns when GC operation has been queued.

Returns
RD_SUCCESS on success.
RD_ERROR_INVALID_STATE if flash is not initialized.

◆ rt_flash_init()

rd_status_t rt_flash_init ( void  )

Initialize flash storage.

If flash initialization fails, flash is purged and device tries to enter bootloader.

Return values
RD_SUCCESSon success
RD_ERROR_INVALID_STATEif flash is already initialized
Warning
Erases entire flash storage and reboots on failure.

Definition at line 191 of file ruuvi_task_flash.c.

◆ rt_flash_load()

rd_status_t rt_flash_load ( const uint16_t  file_id,
const uint16_t  record_id,
void *const  message,
const size_t  message_length 
)

Load data from flash.

The flash storage implements a simple file system, where data is arranged to files which have records. You can for example have a file for sensor configurations and record for each sensor.

Parameters
[in]file_idID of a file to load. Valid range 1 ... 0xBFFF
[in]record_idID of a record to load. Valid range 1 ... 0xFFFF
[in]messageData to load. Must be aligned to a 4-byte boundary.
[in]message_lengthLength of loaded data. Maximum 4000 bytes per record on nRF52.
Return values
RD_SUCCESSon success.
RD_ERROR_NULLif data pointer is NULL.
RD_ERROR_INVALID_STATEif flash is not initialized.
RD_ERROR_BUSYif another operation was ongoing.
RD_ERROR_NOT_FOUNDif given record was not found.
RD_ERROR_DATA_SIZEif record exceeds maximum size.
Warning
Triggers garbage collection if there is no space available, which leads to long processing time.

Definition at line 203 of file ruuvi_task_flash.c.

◆ rt_flash_store()

rd_status_t rt_flash_store ( const uint16_t  file_id,
const uint16_t  record_id,
const void *const  message,
const size_t  message_length 
)

Store data to flash.

The flash storage implements a simple file system, where data is arranged to files which have records. You can for example have a file for sensor configurations and record for each sensor. Underlying implementation provides wear leveling. Garbage collection may be triggered manually and is tried automatically if there is not enough space in flash. This function only queues the data to be written, you must verify that write was completed before freeing message.

If a record with given file_id and record_id already exists, the record is updated. In case the flash memory is 100 % filled, record cannot be updated as new record has to be created before old is deleted to maintain data over power outages etc.

Parameters
[in]file_idID of a file to store. Valid range 1 ... 0xBFFF
[in]record_idID of a record to store. Valid range 1 ... 0xFFFF
[in]messageData to store. Must be aligned to a 4-byte boundary.
[in]message_lengthLength of stored data. Maximum 4000 bytes per record on nRF52.
Return values
RD_SUCCESSon success.
RD_ERROR_NULLif data pointer is NULL.
RD_ERROR_INVALID_STATEif flash is not initialized.
RD_ERROR_BUSYif another operation was ongoing.
RD_ERROR_NO_MEMif there was no space for the record in flash.
RD_ERROR_DATA_SIZEif record exceeds maximum size.
Warning
triggers garbage collection if there is no space available, which leads to long processing time.

Definition at line 197 of file ruuvi_task_flash.c.