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

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

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

Public Member Functions

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

Static Public Attributes

static constexpr size_t blockSize {512}
 size of block, bytes More...
 

Private Member Functions

void deinitialize ()
 Deinitializes SD card connected via SPI. More...
 
int initialize ()
 Initializes SD card connected via SPI. More...
 

Private Attributes

Mutex mutex_
 mutex used to serialize access to this object More...
 
size_t blocksCount_
 number of blocks available on SD card More...
 
uint32_t auSize_
 size of AU, bytes More...
 
uint32_t clockFrequency_
 desired clock frequency of SD card, Hz More...
 
OutputPinslaveSelectPin_
 reference to slave select pin of this SD card More...
 
SpiMasterspiMaster_
 reference to SPI master to which this SD card is connected More...
 
uint16_t eraseTimeoutMs_
 timeout of erase operation of single AU, milliseconds More...
 
uint16_t readTimeoutMs_
 timeout of read operation, milliseconds More...
 
uint16_t writeTimeoutMs_
 timeout of write operation, milliseconds More...
 
bool blockAddressing_
 selects whether card uses byte (false) or block (true) addressing More...
 
uint8_t openCount_
 number of times this device was opened but not yet closed More...
 

Detailed Description

SdCardSpiBased class is a SD card connected via SPI.

This class supports SD version 2.0 cards only.

Constructor & Destructor Documentation

◆ SdCardSpiBased()

constexpr distortos::devices::SdCardSpiBased::SdCardSpiBased ( SpiMaster spiMaster,
OutputPin slaveSelectPin,
const uint32_t  clockFrequency = 25000000 
)
inline

SdCardSpiBased's constructor.

Parameters
[in]spiMasteris a reference to SPI master to which this SD card is connected
[in]slaveSelectPinis a reference to slave select pin of this SD card
[in]clockFrequencyis the desired clock frequency of SD card, Hz, default - 25 MHz

◆ ~SdCardSpiBased()

distortos::devices::SdCardSpiBased::~SdCardSpiBased ( )
override

SdCardSpiBased's destructor.

Precondition
Device is closed.

Member Function Documentation

◆ close()

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

Closes SD card connected via SPI.

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:

◆ deinitialize()

void distortos::devices::SdCardSpiBased::deinitialize ( )
private

Deinitializes SD card connected via SPI.

Here is the caller graph for this function:

◆ erase()

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

Erases blocks on a SD card connected via SPI.

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, must be a multiple of block size
[in]sizeis the size of erased range, bytes, must be a multiple of block size
Returns
0 on success, error code otherwise:

Implements distortos::devices::BlockDevice.

Here is the call graph for this function:

◆ getBlockSize()

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

Implements distortos::devices::BlockDevice.

◆ getSize()

uint64_t distortos::devices::SdCardSpiBased::getSize ( ) const
overridevirtual
Returns
size of SD card connected via SPI, bytes

Implements distortos::devices::BlockDevice.

Here is the caller graph for this function:

◆ initialize()

int distortos::devices::SdCardSpiBased::initialize ( )
private

Initializes SD card connected via SPI.

Algorithm is based on ChaN's How to Use MMC/SDC: Initialization Procedure for SPI Mode.

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

◆ lock()

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

Locks the device 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::SdCardSpiBased::open ( )
overridevirtual

Opens SD card connected via SPI.

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::SdCardSpiBased::read ( uint64_t  address,
void *  buffer,
size_t  size 
)
overridevirtual

Reads data from SD card connected via SPI.

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, must be a multiple of block size
[out]bufferis the buffer into which the data will be read, must be valid
[in]sizeis the size of buffer, bytes, must be a multiple of block size
Returns
0 on success, error code otherwise:

Implements distortos::devices::BlockDevice.

Here is the call graph for this function:

◆ synchronize()

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

Synchronizes state of SD card connected via SPI, ensuring all cached writes are finished.

Precondition
Device is opened.
Returns
always 0

Implements distortos::devices::BlockDevice.

◆ unlock()

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

Unlocks the device 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:

◆ write()

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

Writes data to SD card connected via SPI.

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, must be a multiple of block size
[in]bufferis the buffer with data that will be written, must be valid
[in]sizeis the size of buffer, bytes, must be a multiple of block size
Returns
0 on success, error code otherwise:

Implements distortos::devices::BlockDevice.

Here is the call graph for this function:

Member Data Documentation

◆ auSize_

uint32_t distortos::devices::SdCardSpiBased::auSize_
private

size of AU, bytes

◆ blockAddressing_

bool distortos::devices::SdCardSpiBased::blockAddressing_
private

selects whether card uses byte (false) or block (true) addressing

◆ blocksCount_

size_t distortos::devices::SdCardSpiBased::blocksCount_
private

number of blocks available on SD card

◆ blockSize

constexpr size_t distortos::devices::SdCardSpiBased::blockSize {512}
static

size of block, bytes

◆ clockFrequency_

uint32_t distortos::devices::SdCardSpiBased::clockFrequency_
private

desired clock frequency of SD card, Hz

◆ eraseTimeoutMs_

uint16_t distortos::devices::SdCardSpiBased::eraseTimeoutMs_
private

timeout of erase operation of single AU, milliseconds

◆ mutex_

Mutex distortos::devices::SdCardSpiBased::mutex_
private

mutex used to serialize access to this object

◆ openCount_

uint8_t distortos::devices::SdCardSpiBased::openCount_
private

number of times this device was opened but not yet closed

◆ readTimeoutMs_

uint16_t distortos::devices::SdCardSpiBased::readTimeoutMs_
private

timeout of read operation, milliseconds

◆ slaveSelectPin_

OutputPin& distortos::devices::SdCardSpiBased::slaveSelectPin_
private

reference to slave select pin of this SD card

◆ spiMaster_

SpiMaster& distortos::devices::SdCardSpiBased::spiMaster_
private

reference to SPI master to which this SD card is connected

◆ writeTimeoutMs_

uint16_t distortos::devices::SdCardSpiBased::writeTimeoutMs_
private

timeout of write operation, milliseconds


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