distortos  v0.7.0
object-oriented C++ RTOS for microcontrollers
distortos::devices::SpiEeprom Class Reference

#include "distortos/devices/memory/SpiEeprom.hpp"

Inheritance diagram for distortos::devices::SpiEeprom:
[legend]
Collaboration diagram for distortos::devices::SpiEeprom:
[legend]

Public Types

enum  Type : uint8_t {
  Type::_128Bytes8BytesPerPage = 0 << sizeShift_ | 0 << pageSizeShift_, Type::_128Bytes16BytesPerPage = 0 << sizeShift_ | 1 << pageSizeShift_, Type::_256Bytes8BytesPerPage = 1 << sizeShift_ | 0 << pageSizeShift_, Type::_256Bytes16BytesPerPage = 1 << sizeShift_ | 1 << pageSizeShift_,
  Type::_512Bytes8BytesPerPage = 2 << sizeShift_ | 0 << pageSizeShift_, Type::_512Bytes16BytesPerPage = 2 << sizeShift_ | 1 << pageSizeShift_, Type::_1KBytes16BytesPerPage = 3 << sizeShift_ | 1 << pageSizeShift_, Type::_1KBytes32BytesPerPage = 3 << sizeShift_ | 2 << pageSizeShift_,
  Type::_2KBytes16BytesPerPage = 4 << sizeShift_ | 1 << pageSizeShift_, Type::_2KBytes32BytesPerPage = 4 << sizeShift_ | 2 << pageSizeShift_, Type::_4KBytes32BytesPerPage = 5 << sizeShift_ | 2 << pageSizeShift_, Type::_8KBytes32BytesPerPage = 6 << sizeShift_ | 2 << pageSizeShift_,
  Type::_8KBytes64BytesPerPage = 6 << sizeShift_ | 3 << pageSizeShift_, Type::_16KBytes64BytesPerPage = 7 << sizeShift_ | 3 << pageSizeShift_, Type::_32KBytes64BytesPerPage = 8 << sizeShift_ | 3 << pageSizeShift_, Type::_64KBytes128BytesPerPage = 9 << sizeShift_ | 4 << pageSizeShift_,
  Type::_128KBytes256BytesPerPage = 10 << sizeShift_ | 5 << pageSizeShift_, Type::_256KBytes256BytesPerPage = 11 << sizeShift_ | 5 << pageSizeShift_, Type::at25010 = _128Bytes8BytesPerPage, Type::at25020 = _256Bytes8BytesPerPage,
  Type::at25040 = _512Bytes8BytesPerPage, Type::at25080 = _1KBytes32BytesPerPage, Type::at25160 = _2KBytes32BytesPerPage, Type::at25320 = _4KBytes32BytesPerPage,
  Type::at25640 = _8KBytes32BytesPerPage, Type::at25128 = _16KBytes64BytesPerPage, Type::at25256 = _32KBytes64BytesPerPage, Type::at25512 = _64KBytes128BytesPerPage,
  Type::at25m01 = _128KBytes256BytesPerPage, Type::at25m02 = _256KBytes256BytesPerPage, Type::cat25010 = _128Bytes16BytesPerPage, Type::cat25020 = _256Bytes16BytesPerPage,
  Type::cat25040 = _512Bytes16BytesPerPage, Type::cat25080 = _1KBytes32BytesPerPage, Type::cat25160 = _2KBytes32BytesPerPage, Type::cat25320 = _4KBytes32BytesPerPage,
  Type::cat25640 = _8KBytes64BytesPerPage, Type::cat25128 = _16KBytes64BytesPerPage, Type::cat25256 = _32KBytes64BytesPerPage, Type::cat25512 = _64KBytes128BytesPerPage,
  Type::cat25m01 = _128KBytes256BytesPerPage, Type::cat25m02 = _256KBytes256BytesPerPage, Type::m95010 = _128Bytes16BytesPerPage, Type::m95020 = _256Bytes16BytesPerPage,
  Type::m95040 = _512Bytes16BytesPerPage, Type::m95080 = _1KBytes32BytesPerPage, Type::m95160 = _2KBytes32BytesPerPage, Type::m95320 = _4KBytes32BytesPerPage,
  Type::m95640 = _8KBytes32BytesPerPage, Type::m95128 = _16KBytes64BytesPerPage, Type::m95256 = _32KBytes64BytesPerPage, Type::m95512 = _64KBytes128BytesPerPage,
  Type::m95m01 = _128KBytes256BytesPerPage, Type::m95m02 = _256KBytes256BytesPerPage, Type::_25xx010 = _128Bytes16BytesPerPage, Type::_25xx020 = _256Bytes16BytesPerPage,
  Type::_25xx040 = _512Bytes16BytesPerPage, Type::_25xx080 = _1KBytes16BytesPerPage, Type::_25xx080a = _25xx080, Type::_25xx080b = _1KBytes32BytesPerPage,
  Type::_25xx080c = _25xx080, Type::_25xx080d = _25xx080b, Type::_25xx160 = _2KBytes16BytesPerPage, Type::_25xx160a = _25xx160,
  Type::_25xx160b = _2KBytes32BytesPerPage, Type::_25xx160c = _25xx160, Type::_25xx160d = _25xx160b, Type::_25xx320 = _4KBytes32BytesPerPage,
  Type::_25xx640 = _8KBytes32BytesPerPage, Type::_25xx128 = _16KBytes64BytesPerPage, Type::_25xx256 = _32KBytes64BytesPerPage, Type::_25xx512 = _64KBytes128BytesPerPage,
  Type::_25xx1024 = _128KBytes256BytesPerPage
}
 type of device - determines size and page size More...
 

Public Member Functions

constexpr SpiEeprom (SpiMaster &spiMaster, OutputPin &slaveSelectPin, const Type type, const bool mode3={}, const uint32_t clockFrequency=1000000)
 SpiEeprom's constructor. More...
 
 ~SpiEeprom () override
 SpiEeprom's destructor. More...
 
int close () override
 Closes SPI EEPROM. More...
 
int erase (uint64_t address, uint64_t size) override
 Erases blocks on SPI EEPROM. More...
 
size_t getBlockSize () const override
 
uint64_t getSize () const override
 
void lock () override
 Locks SPI EEPROM for exclusive use by current thread. More...
 
int open () override
 Opens SPI EEPROM. More...
 
int read (uint64_t address, void *buffer, size_t size) override
 Reads data from SPI EEPROM. More...
 
int synchronize () override
 Synchronizes state of SPI EEPROM, ensuring all cached writes are finished. More...
 
void unlock () override
 Unlocks SPI EEPROM which was previously locked by current thread. More...
 
int write (uint64_t address, const void *buffer, size_t size) override
 Writes data to SPI EEPROM. More...
 
- Public Member Functions inherited from distortos::devices::BlockDevice
virtual ~BlockDevice ()=default
 BlockDevice's destructor. More...
 
 BlockDevice (const BlockDevice &)=delete
 
BlockDeviceoperator= (const BlockDevice &)=delete
 

Private Member Functions

int eraseOrWrite (uint64_t address, const void *buffer, uint64_t size)
 Implementation of erase() and write() More...
 
std::pair< int, size_t > eraseOrWritePage (uint32_t address, const void *buffer, size_t size)
 Erases or writes single page. More...
 
int executeTransaction (SpiMasterTransfersRange transfersRange) const
 Executes series of transfers as a single atomic transaction. More...
 
size_t getPageSize () const
 
std::pair< int, bool > isWriteInProgress ()
 Checks whether any write operation is currently in progress. More...
 
std::pair< int, uint8_t > readStatusRegister () const
 Reads value of status register of SPI EEPROM. More...
 
int waitWhileWriteInProgress ()
 Waits while any write operation is currently in progress. More...
 
int writeEnable () const
 Enables writes in SPI EEPROM. More...
 

Private Attributes

Mutex mutex_
 mutex used to serialize access to this object More...
 
uint32_t clockFrequency_
 desired clock frequency of SPI EEPROM, Hz More...
 
OutputPinslaveSelectPin_
 reference to slave select pin of this SPI EEPROM More...
 
SpiMasterspiMaster_
 reference to SPI master to which this SPI EEPROM is connected More...
 
SpiMode mode_
 SPI mode used by SPI EEPROM. More...
 
uint8_t openCount_
 number of times this device was opened but not yet closed More...
 
Type type_
 type of SPI EEPROM More...
 

Static Private Attributes

static constexpr size_t pageSizeShift_ {0}
 bit shift of field with page size encoded in device's type More...
 
static constexpr size_t pageSizeWidth_ {3}
 bit width of field with page size encoded in device's type More...
 
static constexpr size_t pageSizeMask_ {((1 << pageSizeWidth_) - 1) << pageSizeShift_}
 bit mask of field with page size encoded in device's type More...
 
static constexpr size_t sizeShift_ {pageSizeWidth_}
 bit shift of field with size encoded in device's type More...
 
static constexpr size_t sizeWidth_ {8 - pageSizeWidth_}
 bit width of field with size encoded in device's type More...
 
static constexpr size_t sizeMask_ {((1 << sizeWidth_) - 1) << sizeShift_}
 bit mask of field with size encoded in device's type More...
 

Detailed Description

SpiEeprom class is a SPI EEPROM memory: Atmel AT25xxx, ON Semiconductor CAT25xxx, ST M95xxx, Microchip 25xxxxx or similar.

Member Enumeration Documentation

◆ Type

enum distortos::devices::SpiEeprom::Type : uint8_t
strong

type of device - determines size and page size

Enumerator
_128Bytes8BytesPerPage 

128 B (1 kb) size, 8 bytes per page

_128Bytes16BytesPerPage 

128 B (1 kb) size, 16 bytes per page

_256Bytes8BytesPerPage 

256 B (2 kb) size, 8 bytes per page

_256Bytes16BytesPerPage 

256 B (2 kb) size, 16 bytes per page

_512Bytes8BytesPerPage 

512 B (4 kb) size, 8 bytes per page

_512Bytes16BytesPerPage 

512 B (4 kb) size, 16 bytes per page

_1KBytes16BytesPerPage 

1 kB (8 kb) size, 16 bytes per page

_1KBytes32BytesPerPage 

1 kB (8 kb) size, 32 bytes per page

_2KBytes16BytesPerPage 

2 kB (16 kb) size, 16 bytes per page

_2KBytes32BytesPerPage 

2 kB (16 kb) size, 32 bytes per page

_4KBytes32BytesPerPage 

4 kB (32 kb) size, 32 bytes per page

_8KBytes32BytesPerPage 

8 kB (64 kb) size, 32 bytes per page

_8KBytes64BytesPerPage 

8 kB (64 kb) size, 64 bytes per page

_16KBytes64BytesPerPage 

16 kB (128 kb) size, 64 bytes per page

_32KBytes64BytesPerPage 

32 kB (256 kb) size, 64 bytes per page

_64KBytes128BytesPerPage 

64 kB (512 kb) size, 128 bytes per page

_128KBytes256BytesPerPage 

128 kB (1 Mb) size, 256 bytes per page

_256KBytes256BytesPerPage 

256 kB (2 Mb) size, 256 bytes per page

at25010 

Atmel AT25010.

at25020 

Atmel AT25020.

at25040 

Atmel AT25040.

at25080 

Atmel AT25080.

at25160 

Atmel AT25160.

at25320 

Atmel AT25320.

at25640 

Atmel AT25640.

at25128 

Atmel AT25128.

at25256 

Atmel AT25256.

at25512 

Atmel AT25512.

at25m01 

Atmel AT25M01.

at25m02 

Atmel AT25M02.

cat25010 

ON Semiconductor CAT25010.

cat25020 

ON Semiconductor CAT25020.

cat25040 

ON Semiconductor CAT25040.

cat25080 

ON Semiconductor CAT25080.

cat25160 

ON Semiconductor CAT25160.

cat25320 

ON Semiconductor CAT25320.

cat25640 

ON Semiconductor CAT25640.

cat25128 

ON Semiconductor CAT25128.

cat25256 

ON Semiconductor CAT25256.

cat25512 

ON Semiconductor CAT25512.

cat25m01 

ON Semiconductor CAT25M01.

cat25m02 

ON Semiconductor CAT25M02.

m95010 

ST M95010.

m95020 

ST M95020.

m95040 

ST M95040.

m95080 

ST M95080.

m95160 

ST M95160.

m95320 

ST M95320.

m95640 

ST M95640.

m95128 

ST M95128.

m95256 

ST M95256.

m95512 

ST M95512.

m95m01 

ST M95M01.

m95m02 

ST M95M02.

_25xx010 

Microchip 25xx010.

_25xx020 

Microchip 25xx020.

_25xx040 

Microchip 25xx040.

_25xx080 

Microchip 25xx080.

_25xx080a 

Microchip 25xx080A.

_25xx080b 

Microchip 25xx080B.

_25xx080c 

Microchip 25xx080C.

_25xx080d 

Microchip 25xx080D.

_25xx160 

Microchip 25xx160.

_25xx160a 

Microchip 25xx160A.

_25xx160b 

Microchip 25xx160B.

_25xx160c 

Microchip 25xx160C.

_25xx160d 

Microchip 25xx160D.

_25xx320 

Microchip 25xx320.

_25xx640 

Microchip 25xx640.

_25xx128 

Microchip 25xx128.

_25xx256 

Microchip 25xx256.

_25xx512 

Microchip 25xx512.

_25xx1024 

Microchip 25xx1024.

Constructor & Destructor Documentation

◆ SpiEeprom()

constexpr distortos::devices::SpiEeprom::SpiEeprom ( SpiMaster spiMaster,
OutputPin slaveSelectPin,
const Type  type,
const bool  mode3 = {},
const uint32_t  clockFrequency = 1000000 
)
inline

SpiEeprom's constructor.

Parameters
[in]spiMasteris a reference to SPI master to which this SPI EEPROM is connected
[in]slaveSelectPinis a reference to slave select pin of this SPI EEPROM
[in]typeis the type of SPI EEPROM
[in]mode3selects whether SPI mode 0 - CPOL == 0, CPHA == 0 - (false) or SPI mode 3 - CPOL == 1, CPHA == 1 - (true) will be used, default - SPI mode 0 (false)
[in]clockFrequencyis the desired clock frequency of SPI EEPROM, Hz, default - 1 MHz

◆ ~SpiEeprom()

distortos::devices::SpiEeprom::~SpiEeprom ( )
override

SpiEeprom's destructor.

Precondition
Device is closed.

Member Function Documentation

◆ close()

int distortos::devices::SpiEeprom::close ( )
overridevirtual

Closes SPI EEPROM.

Note
Even if error code is returned, the device must not be used from the context which opened it (until it is successfully opened again).
Warning
This function must not be called from interrupt context!
Precondition
Device is opened.
Returns
0 on success, error code otherwise

Implements distortos::devices::BlockDevice.

Here is the call graph for this function:

◆ erase()

int distortos::devices::SpiEeprom::erase ( uint64_t  address,
uint64_t  size 
)
overridevirtual

Erases blocks on SPI EEPROM.

Warning
This function must not be called from interrupt context!
Precondition
Device is opened.
address and size are valid.
Selected range is within address space of device.
Parameters
[in]addressis the address of range that will be erased
[in]sizeis the size of erased range, bytes
Returns
0 on success, error code otherwise:

Implements distortos::devices::BlockDevice.

Here is the call graph for this function:

◆ eraseOrWrite()

int distortos::devices::SpiEeprom::eraseOrWrite ( uint64_t  address,
const void *  buffer,
uint64_t  size 
)
private

Implementation of erase() and write()

Precondition
Device is opened.
Selected range is within address space of device.
Parameters
[in]addressis the address of data that will be erased or written
[in]bufferis the buffer with data that will be written, nullptr to erase
[in]sizeis the size of erase (buffer == nullptr) or size of buffer (buffer != nullptr), bytes
Returns
0 on success, error code otherwise:
Here is the call graph for this function:
Here is the caller graph for this function:

◆ eraseOrWritePage()

std::pair< int, size_t > distortos::devices::SpiEeprom::eraseOrWritePage ( uint32_t  address,
const void *  buffer,
size_t  size 
)
private

Erases or writes single page.

Parameters
[in]addressis the address of data that will be erased or written, must be valid!
[in]bufferis the buffer with data that will be written, nullptr to erase
[in]sizeis the size of erase (buffer == nullptr) or size of buffer (buffer != nullptr), bytes
Returns
pair with return code (0 on success, error code otherwise) and number of erased/written bytes; error codes:
Here is the call graph for this function:
Here is the caller graph for this function:

◆ executeTransaction()

int distortos::devices::SpiEeprom::executeTransaction ( SpiMasterTransfersRange  transfersRange) const
private

Executes series of transfers as a single atomic transaction.

Parameters
[in]transfersRangeis the range of transfers that will be executed
Returns
0 on success, error code otherwise:
Here is the call graph for this function:
Here is the caller graph for this function:

◆ getBlockSize()

size_t distortos::devices::SpiEeprom::getBlockSize ( ) const
overridevirtual
Returns
block size, bytes

Implements distortos::devices::BlockDevice.

◆ getPageSize()

size_t distortos::devices::SpiEeprom::getPageSize ( ) const
inlineprivate
Returns
size of single page, bytes
Here is the caller graph for this function:

◆ getSize()

uint64_t distortos::devices::SpiEeprom::getSize ( ) const
overridevirtual
Returns
size of SPI EEPROM, bytes

Implements distortos::devices::BlockDevice.

Here is the caller graph for this function:

◆ isWriteInProgress()

std::pair< int, bool > distortos::devices::SpiEeprom::isWriteInProgress ( )
private

Checks whether any write operation is currently in progress.

Returns
pair with return code (0 on success, error code otherwise) and current status of device: false - device is idle, true - write operation is in progress; error codes:
Here is the call graph for this function:
Here is the caller graph for this function:

◆ lock()

void distortos::devices::SpiEeprom::lock ( )
overridevirtual

Locks SPI EEPROM for exclusive use by current thread.

When the object is locked, any call to any member function from other thread will be blocked until the object is unlocked. Locking is optional, but may be useful when more than one transaction must be done atomically.

Note
Locks are recursive.
Warning
This function must not be called from interrupt context!
Precondition
The number of recursive locks of device is less than 65535.
Postcondition
Device is locked.

Implements distortos::devices::BlockDevice.

Here is the call graph for this function:

◆ open()

int distortos::devices::SpiEeprom::open ( )
overridevirtual

Opens SPI EEPROM.

Warning
This function must not be called from interrupt context!
Precondition
The number of times the device is opened is less than 255.
Returns
0 on success, error code otherwise:

Implements distortos::devices::BlockDevice.

Here is the call graph for this function:

◆ read()

int distortos::devices::SpiEeprom::read ( uint64_t  address,
void *  buffer,
size_t  size 
)
overridevirtual

Reads data from SPI EEPROM.

Warning
This function must not be called from interrupt context!
Precondition
Device is opened.
address and buffer and size are valid.
Selected range is within address space of device.
Parameters
[in]addressis the address of data that will be read
[out]bufferis the buffer into which the data will be read, must be valid
[in]sizeis the size of buffer, bytes
Returns
0 on success, error code otherwise:

Implements distortos::devices::BlockDevice.

Here is the call graph for this function:

◆ readStatusRegister()

std::pair< int, uint8_t > distortos::devices::SpiEeprom::readStatusRegister ( ) const
private

Reads value of status register of SPI EEPROM.

Returns
pair with return code (0 on success, error code otherwise) and value of status register of SPI EEPROM; error codes:
Here is the call graph for this function:
Here is the caller graph for this function:

◆ synchronize()

int distortos::devices::SpiEeprom::synchronize ( )
overridevirtual

Synchronizes state of SPI EEPROM, ensuring all cached writes are finished.

Warning
This function must not be called from interrupt context!
Precondition
Device is opened.
Returns
0 on success, error code otherwise:

Implements distortos::devices::BlockDevice.

Here is the call graph for this function:

◆ unlock()

void distortos::devices::SpiEeprom::unlock ( )
overridevirtual

Unlocks SPI EEPROM which was previously locked by current thread.

Note
Locks are recursive.
Warning
This function must not be called from interrupt context!
Precondition
This function is called by the thread that locked the device.

Implements distortos::devices::BlockDevice.

Here is the call graph for this function:

◆ waitWhileWriteInProgress()

int distortos::devices::SpiEeprom::waitWhileWriteInProgress ( )
private

Waits while any write operation is currently in progress.

Returns
0 on success, error code otherwise:
Here is the call graph for this function:
Here is the caller graph for this function:

◆ write()

int distortos::devices::SpiEeprom::write ( uint64_t  address,
const void *  buffer,
size_t  size 
)
overridevirtual

Writes data to SPI EEPROM.

Warning
This function must not be called from interrupt context!
Precondition
Device is opened.
address and buffer and size are valid.
Selected range is within address space of device.
Parameters
[in]addressis the address of data that will be written
[in]bufferis the buffer with data that will be written, must be valid
[in]sizeis the size of buffer, bytes
Returns
0 on success, error code otherwise:

Implements distortos::devices::BlockDevice.

Here is the call graph for this function:

◆ writeEnable()

int distortos::devices::SpiEeprom::writeEnable ( ) const
private

Enables writes in SPI EEPROM.

Returns
0 on success, error code otherwise:
Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ clockFrequency_

uint32_t distortos::devices::SpiEeprom::clockFrequency_
private

desired clock frequency of SPI EEPROM, Hz

◆ mode_

SpiMode distortos::devices::SpiEeprom::mode_
private

SPI mode used by SPI EEPROM.

◆ mutex_

Mutex distortos::devices::SpiEeprom::mutex_
private

mutex used to serialize access to this object

◆ openCount_

uint8_t distortos::devices::SpiEeprom::openCount_
private

number of times this device was opened but not yet closed

◆ pageSizeMask_

constexpr size_t distortos::devices::SpiEeprom::pageSizeMask_ {((1 << pageSizeWidth_) - 1) << pageSizeShift_}
staticprivate

bit mask of field with page size encoded in device's type

◆ pageSizeShift_

constexpr size_t distortos::devices::SpiEeprom::pageSizeShift_ {0}
staticprivate

bit shift of field with page size encoded in device's type

◆ pageSizeWidth_

constexpr size_t distortos::devices::SpiEeprom::pageSizeWidth_ {3}
staticprivate

bit width of field with page size encoded in device's type

◆ sizeMask_

constexpr size_t distortos::devices::SpiEeprom::sizeMask_ {((1 << sizeWidth_) - 1) << sizeShift_}
staticprivate

bit mask of field with size encoded in device's type

◆ sizeShift_

constexpr size_t distortos::devices::SpiEeprom::sizeShift_ {pageSizeWidth_}
staticprivate

bit shift of field with size encoded in device's type

◆ sizeWidth_

constexpr size_t distortos::devices::SpiEeprom::sizeWidth_ {8 - pageSizeWidth_}
staticprivate

bit width of field with size encoded in device's type

◆ slaveSelectPin_

OutputPin& distortos::devices::SpiEeprom::slaveSelectPin_
private

reference to slave select pin of this SPI EEPROM

◆ spiMaster_

SpiMaster& distortos::devices::SpiEeprom::spiMaster_
private

reference to SPI master to which this SPI EEPROM is connected

◆ type_

Type distortos::devices::SpiEeprom::type_
private

type of SPI EEPROM


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