Arduino-TWI
Two-Wire Interface (TWI) library for Arduino
Hardware::TWI Class Reference

#include <TWI.h>

Inheritance diagram for Hardware::TWI:
Inheritance graph
Collaboration diagram for Hardware::TWI:
Collaboration graph

Public Member Functions

 TWI (uint32_t freq=DEFAULT_FREQ)
 
virtual bool acquire ()
 
virtual bool release ()
 
virtual int read (uint8_t addr, void *buf, size_t count)
 
virtual int write (uint8_t addr, iovec_t *vp)
 
 TWI (uint32_t freq=DEFAULT_FREQ)
 
virtual bool acquire ()
 
virtual bool release ()
 
virtual int read (uint8_t addr, void *buf, size_t count)
 
virtual int write (uint8_t addr, iovec_t *vp)
 
virtual int write (uint8_t addr, const void *buf, size_t count)
 

Static Public Attributes

static const uint32_t DEFAULT_FREQ = 100000L
 

Protected Types

enum  {
  START = 0x08, REP_START = 0x10, ARB_LOST = 0x38, MT_SLA_ACK = 0x18,
  MT_SLA_NACK = 0x20, MT_DATA_ACK = 0x28, MT_DATA_NACK = 0x30, MR_SLA_ACK = 0x40,
  MR_SLA_NACK = 0x48, MR_DATA_ACK = 0x50, MR_DATA_NACK = 0x58, MASK = 0xF8,
  BUS_ERROR = 0x00
}
 
enum  state_t { IDLE_STATE, BUSY_STATE, WRITE_STATE }
 

Protected Member Functions

bool iowait (uint8_t status)
 
bool stop_condition ()
 
void lock ()
 
void unlock ()
 

Protected Attributes

bool m_start
 
Twi * m_twi
 
state_t m_state
 
volatile bool m_busy
 

Static Protected Attributes

static const uint32_t RETRY_MAX = 100000
 

Detailed Description

Definition at line 28 of file TWI.h.

Member Enumeration Documentation

anonymous enum
protected

Status codes for Master Transmitter Mode.

Enumerator
START 

Start condition transmitted.

REP_START 

Repeated start transmitted.

ARB_LOST 

Arbitration lost.

MT_SLA_ACK 

Slave address/write sent, ACK received.

MT_SLA_NACK 

dito, NACK received.

MT_DATA_ACK 

Data write sent, ACK received.

MT_DATA_NACK 

dito, NACK received.

MR_SLA_ACK 

Slave address/read sent, ACK received.

MR_SLA_NACK 

dito, NACK received.

MR_DATA_ACK 

Data received, ACK sent.

MR_DATA_NACK 

dito, NACK sent.

MASK 

Mask status code.

BUS_ERROR 

Bus error state.

Definition at line 155 of file TWI.h.

enum Hardware::TWI::state_t
protected

Device driver states.

Enumerator
IDLE_STATE 
BUSY_STATE 
WRITE_STATE 

Definition at line 180 of file TWI.h.

Constructor & Destructor Documentation

Hardware::TWI::TWI ( uint32_t  freq = DEFAULT_FREQ)
inline

Construct Two-Wire Interface (TWI).

Parameters
[in]freqbus manager clock frequency (HZ).

Definition at line 34 of file TWI.h.

Hardware::TWI::TWI ( uint32_t  freq = DEFAULT_FREQ)
inline

Construct Two-Wire Interface (TWI).

Parameters
[in]freqbus manager clock frequency (HZ).

Definition at line 36 of file TWI.h.

Member Function Documentation

virtual bool Hardware::TWI::acquire ( )
inlinevirtual

Start transaction for given device driver. Return true(1) if successful otherwise false(0).

Returns
bool.

Implements TWI.

Definition at line 48 of file TWI.h.

virtual bool Hardware::TWI::acquire ( )
inlinevirtual

Start transaction for given device driver. Return true(1) if successful otherwise false(0).

Returns
bool.

Implements TWI.

Definition at line 59 of file TWI.h.

bool Hardware::TWI::iowait ( uint8_t  status)
inlineprotected

Wait for command to complete and check status. Return true(1) if correct status has been reached otherwise false(0).

Parameters
[in]statusto be reached.
Returns
bool.

Definition at line 178 of file TWI.h.

void TWI::lock ( )
inlineprotectedinherited

Lock bus manager.

Definition at line 176 of file TWI.h.

virtual int Hardware::TWI::read ( uint8_t  addr,
void *  buf,
size_t  count 
)
inlinevirtual

Read data from device with given address into given buffer.

Parameters
[in]addrdevice address.
[in]bufbuffer pointer.
[in]countbuffer size in bytes.
Returns
number of bytes read or negative error code.

Implements TWI.

Definition at line 85 of file TWI.h.

virtual int Hardware::TWI::read ( uint8_t  addr,
void *  buf,
size_t  count 
)
inlinevirtual

Read data from device with given address into given buffer.

Parameters
[in]addrdevice address.
[in]bufbuffer pointer.
[in]countbuffer size in bytes.
Returns
number of bytes read or negative error code.

Implements TWI.

Definition at line 94 of file TWI.h.

virtual bool Hardware::TWI::release ( )
inlinevirtual

Stop transaction. Mark the bus as available. Return true(1) if successful otherwise false(0).

Returns
bool.

Implements TWI.

Definition at line 63 of file TWI.h.

virtual bool Hardware::TWI::release ( )
inlinevirtual

Stop transaction. Mark the bus as available. Return true(1) if successful otherwise false(0).

Returns
bool.

Implements TWI.

Definition at line 73 of file TWI.h.

bool Hardware::TWI::stop_condition ( )
inlineprotected

Definition at line 187 of file TWI.h.

void TWI::unlock ( )
inlineprotectedinherited

Unlock bus manager.

Definition at line 185 of file TWI.h.

virtual int Hardware::TWI::write ( uint8_t  addr,
iovec_t *  vp 
)
inlinevirtual

Write data to device with from given io vector.

Parameters
[in]addrdevice address.
[in]vpio vector pointer.
Returns
number of bytes written or negative error code.

Implements TWI.

Definition at line 123 of file TWI.h.

virtual int Hardware::TWI::write ( uint8_t  addr,
iovec_t *  vp 
)
inlinevirtual

Write data to device with from given io vector.

Parameters
[in]addrdevice address.
[in]vpio vector pointer.
Returns
number of bytes written or negative error code.

Implements TWI.

Definition at line 134 of file TWI.h.

virtual int TWI::write ( uint8_t  addr,
const void *  buf,
size_t  count 
)
inlinevirtualinherited

Write data to device with given address from given buffer.

Parameters
[in]addrdevice address.
[in]bufbuffer pointer.
[in]countbuffer size in bytes.
Returns
number of bytes written or negative error code.

Definition at line 151 of file TWI.h.

Member Data Documentation

const uint32_t TWI::DEFAULT_FREQ = 100000L
staticinherited

Default Two-Wire Interface clock: 100 KHz.

Definition at line 108 of file TWI.h.

volatile bool TWI::m_busy
protectedinherited

Bus manager semaphore.

Definition at line 171 of file TWI.h.

bool Hardware::TWI::m_start
protected

Start condition issued flag.

Definition at line 185 of file TWI.h.

state_t Hardware::TWI::m_state
protected

Definition at line 185 of file TWI.h.

Twi* Hardware::TWI::m_twi
protected

TWI instance (libsam/twi).

Definition at line 177 of file TWI.h.

const uint32_t Hardware::TWI::RETRY_MAX = 100000
staticprotected

Maximum number of retries.

Definition at line 174 of file TWI.h.


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