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

#include <TWI.hh>

Collaboration diagram for TWI:
Collaboration graph

Classes

class  Driver
 
class  Slave
 

Public Member Functions

 TWI ()
 
void acquire (TWI::Driver *dev)
 
void release ()
 
bool write_request (void *buf, size_t size)
 
bool write_request (uint8_t header, void *buf, size_t size)
 
bool write_request (uint16_t header, void *buf, size_t size)
 
bool read_request (void *buf, size_t size)
 
int write (void *buf, size_t size)
 
int write (uint8_t header, void *buf=0, size_t size=0)
 
int write (uint16_t header, void *buf=0, size_t size=0)
 
int read (void *buf, size_t size)
 
int await_completed ()
 
void set_freq (uint32_t hz)
 
void powerup ()
 
void powerdown ()
 

Static Public Attributes

static const uint32_t DEFAULT_FREQ = 100000L
 
static const uint32_t MAX_FREQ = (F_CPU / (16 + 2*2))
 

Friends

void TWI_vect (void)
 

Detailed Description

Two wire library. Support for the I2C/TWI bus Master and Slave device drivers. Single-ton, twi, holds bus interaction state. Supporting classes TWI::Driver for device drivers, TWI::Slave for slave devices.

Circuit

TWI slave circuit with internal pullup resistors (4K7). Note that Tiny uses USI but the software interface is the same.

TWI Slave
+------------+
(A4/SDA)------------1-|SDA |
(A5/SCL)------------2-|SCL |
(EXTn/PCIn)---------3-|IRQ(opt) |
(VCC)---------------4-|VCC |
(GND)---------------5-|GND |
+------------+

Definition at line 51 of file TWI.hh.

Constructor & Destructor Documentation

TWI::TWI ( )
inline

Construct two-wire instance. This is actually a single-ton on current supported hardware, i.e. there can only be one unit.

Definition at line 206 of file TWI.hh.

Member Function Documentation

void TWI::acquire ( TWI::Driver dev)

Start TWI logic for a device transaction block. Wait until TWI hardware can be acquired. The given device driver becomes the current driver for addressing, read and write requests.

Parameters
[in]devdevice.

Definition at line 36 of file TWI.cpp.

int TWI::await_completed ( )

Await issued request to complete. Returns number of bytes read/written or negative error code.

Returns
number of bytes or negative error code.

Definition at line 133 of file TWI.cpp.

void TWI::powerdown ( )
inline

Powerdown TWI hardware.

Definition at line 362 of file TWI.hh.

void TWI::powerup ( )
inline

Powerup TWI hardware.

Definition at line 354 of file TWI.hh.

int TWI::read ( void *  buf,
size_t  size 
)
inline

Read data to the current driver. Returns number of bytes read or negative error code.

Parameters
[in]bufpointer to buffer.
[in]sizenumber of bytes.
Returns
number of bytes or negative error code.

Definition at line 326 of file TWI.hh.

bool TWI::read_request ( void *  buf,
size_t  size 
)

Issue a read data request to the current driver. Return true(1) if successful otherwise false(0).

Parameters
[in]bufpointer to buffer.
[in]sizenumber of bytes.
Returns
bool.

Definition at line 124 of file TWI.cpp.

void TWI::release ( )

Release TWI hardware and bus.

Definition at line 58 of file TWI.cpp.

void TWI::set_freq ( uint32_t  hz)
inline

Set bus frequency for device access. Does not adjust for cpu frequency scaling. Compile-time cpu frequency used.

Parameters
[in]hzbus frequency.

Definition at line 345 of file TWI.hh.

int TWI::write ( void *  buf,
size_t  size 
)
inline

Write data to the current driver. Returns number of bytes written or negative error code.

Parameters
[in]bufpointer to buffer.
[in]sizenumber of bytes.
Returns
number of bytes or negative error code.

Definition at line 282 of file TWI.hh.

int TWI::write ( uint8_t  header,
void *  buf = 0,
size_t  size = 0 
)
inline

Write data to the current driver with given byte header. Returns number of bytes written or negative error code.

Parameters
[in]headerto write before buffer.
[in]bufpointer to buffer.
[in]sizenumber of bytes.
Returns
number of bytes or negative error code.

Definition at line 297 of file TWI.hh.

int TWI::write ( uint16_t  header,
void *  buf = 0,
size_t  size = 0 
)
inline

Write data to the current driver with given header. Returns number of bytes written or negative error code.

Parameters
[in]headerto write before buffer.
[in]bufpointer to buffer.
[in]sizenumber of bytes.
Returns
number of bytes or negative error code.

Definition at line 312 of file TWI.hh.

bool TWI::write_request ( void *  buf,
size_t  size 
)

Issue a write data request to the current driver. Return true(1) if successful otherwise false(0).

Parameters
[in]bufpointer to buffer.
[in]sizenumber of bytes.
Returns
bool.

Definition at line 92 of file TWI.cpp.

bool TWI::write_request ( uint8_t  header,
void *  buf,
size_t  size 
)

Issue a write data request to the current driver with given byte header/command. Return true(1) if successful otherwise false(0).

Parameters
[in]headerto write before buffer.
[in]bufpointer to buffer.
[in]sizenumber of bytes.
Returns
bool.

Definition at line 101 of file TWI.cpp.

bool TWI::write_request ( uint16_t  header,
void *  buf,
size_t  size 
)

Issue a write data request to the current driver with given header/command. Return true(1) if successful otherwise false(0).

Parameters
[in]headerto write before buffer.
[in]bufpointer to buffer.
[in]sizenumber of bytes.
Returns
bool.

Definition at line 112 of file TWI.cpp.

Friends And Related Function Documentation

void TWI_vect ( void  )
friend

Interrupt Sevice Routine.

Member Data Documentation

const uint32_t TWI::DEFAULT_FREQ = 100000L
static

Default Two-Wire Interface clock: 100 KHz.

Definition at line 54 of file TWI.hh.

const uint32_t TWI::MAX_FREQ = (F_CPU / (16 + 2*2))
static

Max Two-Wire Interface clock: 800 KHz @ 16 MHz.

Definition at line 57 of file TWI.hh.


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