Class NonVolatileMemory

Class Documentation

class NonVolatileMemory

Non-volatile memory model.

Represents a block of non-volatile memory (such as flash or eeprom) of a AVR MCU. It has a memory block which simulates the NVM actual storage. Each byte has a state unprogrammed/programmed, i.e. it is erased or loaded with a meaningful value.

Public Functions

explicit NonVolatileMemory(size_t size)

Construct a non-volatile memory. Initialise the memory block, setting it to unprogrammed and filling it with the default value 0xFF.

Parameters:

size – size of the NVM in bytes

NonVolatileMemory(const NonVolatileMemory &other)
~NonVolatileMemory()

Destroy a non-volatile memory.

inline size_t size() const

Return the size of the NVM.

inline bool programmed(size_t pos) const

Return the unprogrammed/programmed state of one NVM byte.

Parameters:

pos – address of the byte

Returns:

true if the byte is programmed, false if unprogrammed

size_t programmed(unsigned char *buf, size_t base, size_t len) const

Return the unprogrammed/programmed state of the NVM into a buffer. Each byte in the buffer is set a value of 0 for “unprogrammed” and 1 for “programmed”.

Parameters:
  • buf – buffer to copy the NVM programmed state into

  • base – first address to be read

  • len – length of the area to be read, in bytes

Returns:

length of data actually read

inline unsigned char operator[](size_t pos) const

Read a single NVM byte with no boundary checks.

Parameters:

pos – address of the byte to read

Returns:

the byte value

mem_block_t block() const

Return a mem_block_t struct representing the entire NVM.

mem_block_t block(size_t base, size_t size) const

Return a mem_block_t struct representing a block of the NVM.

bool program(const mem_block_t &mem_block, size_t base = 0)

Load the NVM with a ‘program’. The memory block is copied into the NVM bytes and their state are set to programmed.

Parameters:
  • mem_block – memory block with the data to be loaded into the NVM.

  • base – first address where the memory block should be copied.

Returns:

true if the operation was completed.

void erase()

Erase the entire NVM.

void erase(size_t base, size_t size)

Erases a NVM block, overwrite all bytes of the block with the default value 0xFF and set their state to unprogrammed.

Parameters:
  • base – first address to be erased

  • len – length of the block to be erased, in bytes

void erase(const unsigned char *buf, size_t base, size_t len)

Selective erasing of a NVM block. Bytes in the block are erased only if the corresponding byte in the buffer argument is non-zero.

Parameters:
  • buf – buffer for selecting the bytes that should be erased

  • base – first address to be erased

  • len – length of the area to be erased, in bytes

int read(size_t pos) const

Read a single NVM byte

Parameters:

pos – address of the byte to read

Returns:

the byte value or -1 if the address is invalid

size_t read(unsigned char *buf, size_t base, size_t len) const

Read the memory into a buffer.

Parameters:
  • buf – buffer to copy the NVM data into

  • base – first address to be read

  • len – length of the area to be read, in bytes

Returns:

length of data actually read

void write(unsigned char v, size_t pos)

Write a byte of the NVM.

Parameters:
  • v – data to be written

  • pos – address to be written

void write(const unsigned char *buf, size_t base, size_t len)

Write bytes of the NVM.

Parameters:
  • buf – data to be copied into the NVM

  • base – first address to be written

  • len – length of data to write

void spm_write(unsigned char v, size_t pos)

Write a byte to the NVM and set its state to programmed.

Note

The writing is performed by a bitwise AND with the previous content of the byte.

Parameters:
  • v – value to write

  • pos – address to write, in bytes

void spm_write(const unsigned char *buf, const unsigned char *bufset, size_t base, size_t len)

Selectively write bytes to the NVM and set their state to programmed.

Note

The writing of each byte is performed by a bitwise AND with the previous content of the byte.

Note

If buftag is set to nullptr, all bytes in ‘buf’ are copied.

Parameters:
  • buf – data to be copied

  • buftag – tag for the data in ‘buf’. When a tag byte is non-zero, the corresponding byte in buf is copied into the NVM.

  • base – first address to be written

  • len – length of data to write

NonVolatileMemory &operator=(const NonVolatileMemory &other)