COSA
An Object-Oriented Platform for Arduino Programming
Soft::SPI Class Reference

#include <SPI.hh>

Collaboration diagram for Soft::SPI:
Collaboration graph

Classes

class  Driver
 

Public Types

enum  Clock {
  DIV2_CLOCK = 0x04, DIV4_CLOCK = 0x00, DIV8_CLOCK = 0x05, DIV16_CLOCK = 0x01,
  DIV32_CLOCK = 0x06, DIV64_CLOCK = 0x02, DIV128_CLOCK = 0x03, DEFAULT_CLOCK = DIV4_CLOCK
}
 
enum  Order { MSB_ORDER = 0, LSB_ORDER = 1, DEFAULT_ORDER = MSB_ORDER }
 
enum  Pulse {
  ACTIVE_LOW = 0, ACTIVE_HIGH = 1, PULSE_LOW = 2, PULSE_HIGH = 3,
  DEFAULT_PULSE = ACTIVE_LOW
}
 

Public Member Functions

 SPI (Board::DigitalPin miso, Board::DigitalPin mosi, Board::DigitalPin sck)
 
bool attach (Driver *dev)
 
void acquire (Driver *dev)
 
void release ()
 
void begin ()
 
void end ()
 
void transfer_start (uint8_t data)
 
uint8_t transfer_await ()
 
uint8_t transfer_next (uint8_t data)
 
uint8_t transfer (uint8_t data)
 
void transfer (void *buf, size_t count)
 
void transfer (void *dst, const void *src, size_t count)
 
void read (void *buf, size_t count)
 
void write (const void *buf, size_t count)
 
void write_P (const uint8_t *buf, size_t count)
 
void write (const iovec_t *vec)
 

Detailed Description

Soft Serial Peripheral Interface (SPI) device class.

Definition at line 37 of file SPI.hh.

Member Enumeration Documentation

Clock selectors.

Enumerator
DIV2_CLOCK 

Divide system clock by 2.

DIV4_CLOCK 

Divide system clock by 4.

DIV8_CLOCK 

Divide system clock by 8.

DIV16_CLOCK 

Divide system clock by 16.

DIV32_CLOCK 

Divide system clock by 32.

DIV64_CLOCK 

Divide system clock by 64.

DIV128_CLOCK 

Divide system clock by 128.

DEFAULT_CLOCK 

Default clock rate.

Definition at line 40 of file SPI.hh.

Bit order selectors.

Enumerator
MSB_ORDER 

Most significant bit first.

LSB_ORDER 

Least significant bit first.

DEFAULT_ORDER 

Default is MSB.

Definition at line 52 of file SPI.hh.

Chip select mode.

Enumerator
ACTIVE_LOW 

Active low logic during transaction.

ACTIVE_HIGH 

Active high logic during transaction.

PULSE_LOW 

Pulse low on end of transaction.

PULSE_HIGH 

Pulse high on end of transaction.

DEFAULT_PULSE 

Default is low logic.

Definition at line 59 of file SPI.hh.

Constructor & Destructor Documentation

Soft::SPI::SPI ( Board::DigitalPin  miso,
Board::DigitalPin  mosi,
Board::DigitalPin  sck 
)
inline

Construct soft serial peripheral interface master.

Definition at line 157 of file SPI.hh.

Member Function Documentation

void SPI::acquire ( Driver dev)

Acquire the SPI device driver. Initiate SPI hardware registers and disable SPI interrupt sources. The function will yield until the device driver has been acquired. Interrupts from SPI devices are disabled until the device driver is released. Used in the below format for a device driver:

spi.acquire(this)
res = spi.transfer(data);
spi.end();

Each transfer that requires chip select should be enclosed in a block with begin() and end(). There may be several transfer blocks in a transaction. The transaction is terminated with release().

Parameters
[in]devdevice driver context.

Definition at line 43 of file SOFT_SPI.cpp.

bool SPI::attach ( Driver dev)

Attach given SPI device driver context.

Parameters
[in]devdevice driver context.
Returns
true(1) if successful otherwise false(0)

Definition at line 78 of file SOFT_SPI.cpp.

void Soft::SPI::begin ( )
inline

Mark the beginning of a transfer block. Select the device by asserting the chip select pin according to the pulse pattern. Used in the format:

spi.acquire(this)
res = spi.transfer(data);
spi.end();

The transfer block should be terminated with end(). Typically the transfer block is a command (read/write) with a possible parameter block.

Definition at line 216 of file SPI.hh.

void Soft::SPI::end ( )
inline

Mark the end of a transfer block. Deselect the device chip according to the pulse pattern.

Definition at line 226 of file SPI.hh.

void Soft::SPI::read ( void *  buf,
size_t  count 
)
inline

Read package from the device slave. Should only be used within a SPI transfer; begin()-end() block.

Parameters
[in]bufbuffer for read data.
[in]countnumber of bytes to read.

Definition at line 308 of file SPI.hh.

void SPI::release ( )

Release the SPI device driver. Enable SPI interrupt sources.

Definition at line 64 of file SOFT_SPI.cpp.

uint8_t SPI::transfer ( uint8_t  data)

Exchange data with slave. Slave select must be done before exchange of data. Returns always zero(0) in soft variant.

Parameters
[in]datato send.
Returns
zero

Definition at line 87 of file SOFT_SPI.cpp.

void Soft::SPI::transfer ( void *  buf,
size_t  count 
)

Exchange package with slave. Received data from slave is stored in given buffer. Should only be used within a SPI transfer; begin()-end() block.

Parameters
[in]bufwith data to transfer (send/receive).
[in]countsize of buffer.
void Soft::SPI::transfer ( void *  dst,
const void *  src,
size_t  count 
)
inline

Exchange package with slave. Received data from slave is stored in given destination buffer. Should only be used within a SPI transfer; begin()-end() block.

Parameters
[in]dstdestination buffer for received data.
[in]srcsource buffer with data to send.
[in]countsize of buffers.

Definition at line 294 of file SPI.hh.

uint8_t Soft::SPI::transfer_await ( )
inline

Wait for exchange with slave. Should only be used within a SPI transaction; begin()-end() block. Return received value.

Returns
value received.

Definition at line 249 of file SPI.hh.

uint8_t Soft::SPI::transfer_next ( uint8_t  data)
inline

Next data to exchange with slave. Should only be used within a SPI transaction; begin()-end() block.

Parameters
[in]datato send.
Returns
value received.

Definition at line 261 of file SPI.hh.

void Soft::SPI::transfer_start ( uint8_t  data)
inline

Start exchange data with slave. Should only be used within a SPI transaction; begin()-end() block.

Parameters
[in]datato send.

Definition at line 238 of file SPI.hh.

void Soft::SPI::write ( const void *  buf,
size_t  count 
)
inline

Write package to the device slave. Should only be used within a SPI transaction; begin()-end() block.

Parameters
[in]bufbuffer with data to write.
[in]countnumber of bytes to write.

Definition at line 321 of file SPI.hh.

void Soft::SPI::write ( const iovec_t vec)
inline

Write null terminated io buffer vector to the device slave. Should only be used within a SPI transfer; begin()-end() block.

Parameters
[in]vecnull terminated io buffer vector pointer.

Definition at line 345 of file SPI.hh.

void Soft::SPI::write_P ( const uint8_t *  buf,
size_t  count 
)
inline

Write package to the device slave. Should only be used within a SPI transaction; begin()-end() block.

Parameters
[in]bufbuffer with data to write.
[in]countnumber of bytes to write.

Definition at line 334 of file SPI.hh.


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