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

#include <SPI.hh>

Collaboration diagram for 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 ()
 
bool attach (Driver *dev)
 
void acquire (Driver *dev)
 
void release ()
 
void begin ()
 
void end ()
 
uint8_t transfer (uint8_t data)
 
void transfer_start (uint8_t data)
 
uint8_t transfer_await ()
 
uint8_t transfer_next (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 void *buf, size_t count)
 
void write (const iovec_t *vec)
 

Static Public Member Functions

static void powerup ()
 
static void powerdown ()
 

Detailed Description

Serial Peripheral Interface (SPI) device class. A device driver should inherit from SPI::Driver and defined SPI commands and higher level functions. The SPI::Driver class supports multiple SPI devices with possible different configuration (clock, bit order, mode) and integrates with both device chip select and possible interrupt pins.

Circuit

SPI slave circuit with chip select and interrupt pin. Note that Tiny uses USI but the software interface is the same but MOSI/MISO pins are DI/DO. Do not confuse with SPI chip programming pins on Tiny.

SPI Slave
+------------+
(Dn)----------------1-|CSN |
(D11/MOSI)----------2-|MOSI |
(D12/MISO)----------3-|MISO |
(D13/SCK)-----------4-|SCK |
(EXTn)--------------5-|IRQ(opt) |
(VCC)---------------6-|VCC |
(GND)---------------7-|GND |
+------------+

Definition at line 57 of file SPI.hh.

Member Enumeration Documentation

enum SPI::Clock

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 60 of file SPI.hh.

enum SPI::Order

Bit order selectors.

Enumerator
MSB_ORDER 

Most significant bit first.

LSB_ORDER 

Least significant bit first.

DEFAULT_ORDER 

Default is MSB.

Definition at line 72 of file SPI.hh.

enum SPI::Pulse

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 79 of file SPI.hh.

Constructor & Destructor Documentation

SPI::SPI ( )

Construct serial peripheral interface for master.

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 255 of file 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 246 of file SPI.cpp.

void 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 232 of file SPI.hh.

void SPI::end ( )
inline

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

Definition at line 242 of file SPI.hh.

static void SPI::powerdown ( )
inlinestatic

Powerdown SPI.

Definition at line 383 of file SPI.hh.

static void SPI::powerup ( )
inlinestatic

Powerup SPI.

Definition at line 375 of file SPI.hh.

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

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 160 of file SPI.cpp.

void SPI::release ( )

Release the SPI device driver. Enable SPI interrupt sources.

Definition at line 281 of file SPI.cpp.

uint8_t SPI::transfer ( uint8_t  data)
inline

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

Parameters
[in]datato send.
Returns
value received.

Definition at line 326 of file SPI.hh.

void 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.

Definition at line 127 of file SPI.cpp.

void SPI::transfer ( void *  dst,
const void *  src,
size_t  count 
)

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 143 of file SPI.cpp.

uint8_t 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 350 of file SPI.hh.

uint8_t 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 363 of file SPI.hh.

void 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 339 of file SPI.hh.

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

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

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

Definition at line 170 of file SPI.cpp.

void 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 437 of file SPI.hh.

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

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

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

Definition at line 184 of file SPI.cpp.


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