SlotNVM  0.1.0
Public Member Functions | Static Public Attributes | List of all members
SlotNVM< BASE, CLUSTER_SIZE, PROVISION, LAST_SLOT, CRC_FUNC, RND_TYPE, RND_FUNC > Class Template Reference

#include <SlotNVM.h>

Inheritance diagram for SlotNVM< BASE, CLUSTER_SIZE, PROVISION, LAST_SLOT, CRC_FUNC, RND_TYPE, RND_FUNC >:

Public Member Functions

bool begin ()
 Initialize SlotNVM. More...
 
bool isValid () const
 Check if begin is called before and returns true. More...
 
bool isSlotAvailable (uint8_t slot) const
 Check if data is stored for a given slot. More...
 
bool writeSlot (uint8_t slot, const uint8_t *data, nvm_size_t len)
 Write data. More...
 
template<class T >
bool writeSlot (uint8_t slot, T &data)
 Write data. More...
 
bool readSlot (uint8_t slot, uint8_t *data, nvm_size_t &len) const
 Read data If buffer is to small, nothing is read but len is set to needed size and false is returned. More...
 
template<class T >
bool readSlot (uint8_t slot, T &data) const
 Read data If slot stores more or less byte than size of data false is returned and data untouched. More...
 
bool eraseSlot (uint8_t slot)
 Delete slot data. More...
 
nvm_size_t getSize () const
 Get amount of total available user data. More...
 
nvm_size_t getUsableSize () const
 Get amount of total usable user data. More...
 
nvm_size_t getFree () const
 Get amount of still writable user data. More...
 

Static Public Attributes

static const uint16_t S_CLUSTER_CNT = BASE::S_SIZE / CLUSTER_SIZE
 Count of clusters.
 
static const uint8_t S_USER_DATA_PER_CLUSTER = CLUSTER_SIZE - 6 + ((CRC_FUNC == NULL) ? 1 : 0)
 Max size of user data in one cluster.
 
static const uint16_t S_PROVISION = ((PROVISION + S_USER_DATA_PER_CLUSTER - 1) / S_USER_DATA_PER_CLUSTER) * S_USER_DATA_PER_CLUSTER
 Count of reserved user bytes for overwriting slots.
 
static const uint8_t S_FIRST_SLOT = 1
 First allowed slot number.
 
static const uint8_t S_LAST_SLOT = LAST_SLOT == 0 ? (S_CLUSTER_CNT > 250 ? 250 : S_CLUSTER_CNT) : (LAST_SLOT > 250 ? 250 : LAST_SLOT)
 Last allowed slot number.
 

Detailed Description

template<class BASE, nvm_size_t CLUSTER_SIZE, nvm_size_t PROVISION = 0, uint8_t LAST_SLOT = 0, uint8_t(*)(uint8_t crc, uint8_t data) CRC_FUNC = (uint8_t (*)(uint8_t, uint8_t))NULL, typename RND_TYPE = int, RND_TYPE(*)() RND_FUNC = &rand>
class SlotNVM< BASE, CLUSTER_SIZE, PROVISION, LAST_SLOT, CRC_FUNC, RND_TYPE, RND_FUNC >

Template Parameters
BASEBase class handling NVM read and write, see NVMBase as example. This class also specifies the size of the NVM via member S_SIZE.
CLUSTER_SIZESize of a cluster in byte. This should not be lower than 7. Typical values are 16, 32, 64, 128, 256.
PROVISIONBytes that must always be free to ensure that data can safely be rewritten. If your slot data do not exceed this limit is can always be rewritten without deleting other data before. This value is rounded to next multiple of user data of a cluster.
LAST_SLOTNumber of last usable slot. Maximum value is 250. 0 mean number is equal to count of available cluster. The result is stored in S_LAST_SLOT
CRC_FUNCFunction to calculate a 8 bit CRC. Prototype must be: uint8_t CRC8_function(uint8_t crc, uint8_t data). NULL means not CRC is stored in NVM and therefore one extra data byte per cluster is available.
RND_TYPEReturn type of RND_FUNC.
RND_FUNCRandom function for wear leveling. Default is rand() from stdlib.h. Please do not forget to call srand().

Member Function Documentation

◆ begin()

template<class BASE , nvm_size_t CLUSTER_SIZE, nvm_size_t PROVISION, uint8_t LAST_SLOT, uint8_t(*)(uint8_t, uint8_t) CRC_FUNC, typename RND_TYPE , RND_TYPE(*)() RND_FUNC>
bool SlotNVM< BASE, CLUSTER_SIZE, PROVISION, LAST_SLOT, CRC_FUNC, RND_TYPE, RND_FUNC >::begin ( )

Initialize SlotNVM.

Call this once before every other call to SlotNVM. This will check current data of NVM and fix wrong data if found some.

Returns
true if NVM data is readable and data structure is OK or fixed.
false if NVM data is not readable or data structure is corrupt and can not be fixed or begin() is called twice.

◆ eraseSlot()

template<class BASE , nvm_size_t CLUSTER_SIZE, nvm_size_t PROVISION, uint8_t LAST_SLOT, uint8_t(*)(uint8_t, uint8_t) CRC_FUNC, typename RND_TYPE , RND_TYPE(*)() RND_FUNC>
bool SlotNVM< BASE, CLUSTER_SIZE, PROVISION, LAST_SLOT, CRC_FUNC, RND_TYPE, RND_FUNC >::eraseSlot ( uint8_t  slot)

Delete slot data.

Parameters
slotSlot number
Returns
true on success else false

◆ getFree()

template<class BASE , nvm_size_t CLUSTER_SIZE, nvm_size_t PROVISION, uint8_t LAST_SLOT, uint8_t(*)(uint8_t, uint8_t) CRC_FUNC, typename RND_TYPE , RND_TYPE(*)() RND_FUNC>
nvm_size_t SlotNVM< BASE, CLUSTER_SIZE, PROVISION, LAST_SLOT, CRC_FUNC, RND_TYPE, RND_FUNC >::getFree ( ) const

Get amount of still writable user data.

Returns
Free bytes

◆ getSize()

template<class BASE, nvm_size_t CLUSTER_SIZE, nvm_size_t PROVISION = 0, uint8_t LAST_SLOT = 0, uint8_t(*)(uint8_t crc, uint8_t data) CRC_FUNC = (uint8_t (*)(uint8_t, uint8_t))NULL, typename RND_TYPE = int, RND_TYPE(*)() RND_FUNC = &rand>
nvm_size_t SlotNVM< BASE, CLUSTER_SIZE, PROVISION, LAST_SLOT, CRC_FUNC, RND_TYPE, RND_FUNC >::getSize ( ) const
inline

Get amount of total available user data.

Some of this might be reserved as provision for safe overwriting.

Returns
Total size in bytes

◆ getUsableSize()

template<class BASE, nvm_size_t CLUSTER_SIZE, nvm_size_t PROVISION = 0, uint8_t LAST_SLOT = 0, uint8_t(*)(uint8_t crc, uint8_t data) CRC_FUNC = (uint8_t (*)(uint8_t, uint8_t))NULL, typename RND_TYPE = int, RND_TYPE(*)() RND_FUNC = &rand>
nvm_size_t SlotNVM< BASE, CLUSTER_SIZE, PROVISION, LAST_SLOT, CRC_FUNC, RND_TYPE, RND_FUNC >::getUsableSize ( ) const
inline

Get amount of total usable user data.

Returns
Usable data size in bytes

◆ isSlotAvailable()

template<class BASE, nvm_size_t CLUSTER_SIZE, nvm_size_t PROVISION = 0, uint8_t LAST_SLOT = 0, uint8_t(*)(uint8_t crc, uint8_t data) CRC_FUNC = (uint8_t (*)(uint8_t, uint8_t))NULL, typename RND_TYPE = int, RND_TYPE(*)() RND_FUNC = &rand>
bool SlotNVM< BASE, CLUSTER_SIZE, PROVISION, LAST_SLOT, CRC_FUNC, RND_TYPE, RND_FUNC >::isSlotAvailable ( uint8_t  slot) const
inline

Check if data is stored for a given slot.

Parameters
slotSlot number to check,
Returns
true if there is data for this slot.

◆ isValid()

template<class BASE, nvm_size_t CLUSTER_SIZE, nvm_size_t PROVISION = 0, uint8_t LAST_SLOT = 0, uint8_t(*)(uint8_t crc, uint8_t data) CRC_FUNC = (uint8_t (*)(uint8_t, uint8_t))NULL, typename RND_TYPE = int, RND_TYPE(*)() RND_FUNC = &rand>
bool SlotNVM< BASE, CLUSTER_SIZE, PROVISION, LAST_SLOT, CRC_FUNC, RND_TYPE, RND_FUNC >::isValid ( ) const
inline

Check if begin is called before and returns true.

Returns
true if SlotNVM is ready for use.

◆ readSlot() [1/2]

template<class BASE , nvm_size_t CLUSTER_SIZE, nvm_size_t PROVISION, uint8_t LAST_SLOT, uint8_t(*)(uint8_t, uint8_t) CRC_FUNC, typename RND_TYPE , RND_TYPE(*)() RND_FUNC>
bool SlotNVM< BASE, CLUSTER_SIZE, PROVISION, LAST_SLOT, CRC_FUNC, RND_TYPE, RND_FUNC >::readSlot ( uint8_t  slot,
uint8_t *  data,
nvm_size_t &  len 
) const

Read data If buffer is to small, nothing is read but len is set to needed size and false is returned.

So you can set data to NULL and len to 0 to read data length of a slot.

Parameters
slotSlot number
dataBuffer to read in
[in,out]lenin: Size of data buffer
out: On success count of bytes copied to data. If data buffer was to small the size of slot else value is not changed.
Returns
true on success else false

◆ readSlot() [2/2]

template<class BASE, nvm_size_t CLUSTER_SIZE, nvm_size_t PROVISION = 0, uint8_t LAST_SLOT = 0, uint8_t(*)(uint8_t crc, uint8_t data) CRC_FUNC = (uint8_t (*)(uint8_t, uint8_t))NULL, typename RND_TYPE = int, RND_TYPE(*)() RND_FUNC = &rand>
template<class T >
bool SlotNVM< BASE, CLUSTER_SIZE, PROVISION, LAST_SLOT, CRC_FUNC, RND_TYPE, RND_FUNC >::readSlot ( uint8_t  slot,
T &  data 
) const
inline

Read data If slot stores more or less byte than size of data false is returned and data untouched.

Parameters
slotSlot number
dataData to read in
Returns
true on success else false

◆ writeSlot() [1/2]

template<class BASE , nvm_size_t CLUSTER_SIZE, nvm_size_t PROVISION, uint8_t LAST_SLOT, uint8_t(*)(uint8_t, uint8_t) CRC_FUNC, typename RND_TYPE , RND_TYPE(*)() RND_FUNC>
bool SlotNVM< BASE, CLUSTER_SIZE, PROVISION, LAST_SLOT, CRC_FUNC, RND_TYPE, RND_FUNC >::writeSlot ( uint8_t  slot,
const uint8_t *  data,
nvm_size_t  len 
)

Write data.

Parameters
slotSlot number
dataData to write
lenLenght of data to write
Returns
true on success else false

◆ writeSlot() [2/2]

template<class BASE, nvm_size_t CLUSTER_SIZE, nvm_size_t PROVISION = 0, uint8_t LAST_SLOT = 0, uint8_t(*)(uint8_t crc, uint8_t data) CRC_FUNC = (uint8_t (*)(uint8_t, uint8_t))NULL, typename RND_TYPE = int, RND_TYPE(*)() RND_FUNC = &rand>
template<class T >
bool SlotNVM< BASE, CLUSTER_SIZE, PROVISION, LAST_SLOT, CRC_FUNC, RND_TYPE, RND_FUNC >::writeSlot ( uint8_t  slot,
T &  data 
)
inline

Write data.

Parameters
slotSlot number
dataData to write
Returns
true on success else false

The documentation for this class was generated from the following file: