Arduino-TWI
Two-Wire Interface (TWI) library for Arduino
Software::TWI< SDA_PIN, SCL_PIN > Class Template Reference

#include <TWI.h>

Inheritance diagram for Software::TWI< SDA_PIN, SCL_PIN >:
Inheritance graph
Collaboration diagram for Software::TWI< SDA_PIN, SCL_PIN >:
Collaboration graph

Public Member Functions

 TWI ()
 
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 Member Functions

bool clock_stretching ()
 
bool start_condition ()
 
bool repeated_start_condition ()
 
bool stop_condition ()
 
bool write_bit (bool value)
 
bool read_bit (bool &value)
 
bool write_byte (uint8_t byte, bool &nack)
 
bool read_byte (uint8_t &byte, bool ack)
 
void lock ()
 
void unlock ()
 

Protected Attributes

GPIO< SDA_PIN > m_sda
 
GPIO< SCL_PIN > m_scl
 
bool m_start
 
volatile bool m_busy
 

Static Protected Attributes

static const int T1 = 4
 
static const int T2 = 5
 
static const int CLOCK_STRETCHING_RETRY_MAX = 25
 

Detailed Description

template<BOARD::pin_t SDA_PIN, BOARD::pin_t SCL_PIN>
class Software::TWI< SDA_PIN, SCL_PIN >

Definition at line 32 of file TWI.h.

Constructor & Destructor Documentation

template<BOARD::pin_t SDA_PIN, BOARD::pin_t SCL_PIN>
Software::TWI< SDA_PIN, SCL_PIN >::TWI ( )
inline

Construct Two-Wire Interface (TWI) instance with given template parameters. Initiate GPIO pins for data and clock for open drain mode.

Definition at line 39 of file TWI.h.

Member Function Documentation

template<BOARD::pin_t SDA_PIN, BOARD::pin_t SCL_PIN>
virtual bool Software::TWI< SDA_PIN, SCL_PIN >::acquire ( )
inlinevirtual

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

Returns
bool.

Implements TWI.

Definition at line 51 of file TWI.h.

template<BOARD::pin_t SDA_PIN, BOARD::pin_t SCL_PIN>
bool Software::TWI< SDA_PIN, SCL_PIN >::clock_stretching ( )
inlineprotected

Allow device to stretch clock signal. Return true(1) if successful otherwise false(0).

Returns
bool.

Definition at line 158 of file TWI.h.

void TWI::lock ( )
inlineprotectedinherited

Lock bus manager.

Definition at line 176 of file TWI.h.

template<BOARD::pin_t SDA_PIN, BOARD::pin_t SCL_PIN>
virtual int Software::TWI< SDA_PIN, SCL_PIN >::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 80 of file TWI.h.

template<BOARD::pin_t SDA_PIN, BOARD::pin_t SCL_PIN>
bool Software::TWI< SDA_PIN, SCL_PIN >::read_bit ( bool &  value)
inlineprotected

Read bit to device. Return true(1) if successful otherwise false(0).

Parameters
[out]valueread from device.
Returns
bool.

Definition at line 239 of file TWI.h.

template<BOARD::pin_t SDA_PIN, BOARD::pin_t SCL_PIN>
bool Software::TWI< SDA_PIN, SCL_PIN >::read_byte ( uint8_t &  byte,
bool  ack 
)
inlineprotected

Read byte to device. Return true(1) if successful otherwise false(0). The parameter ack signals if additional read with follow.

Parameters
[in]byteto write to device.
[out]ackto device.
Returns
bool.

Definition at line 275 of file TWI.h.

template<BOARD::pin_t SDA_PIN, BOARD::pin_t SCL_PIN>
virtual bool Software::TWI< SDA_PIN, SCL_PIN >::release ( )
inlinevirtual

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

Returns
bool.

Implements TWI.

Definition at line 65 of file TWI.h.

template<BOARD::pin_t SDA_PIN, BOARD::pin_t SCL_PIN>
bool Software::TWI< SDA_PIN, SCL_PIN >::repeated_start_condition ( )
inlineprotected

Generate repeated start condition. Return true(1) if successful otherwise false(0).

Returns
bool.

Definition at line 187 of file TWI.h.

template<BOARD::pin_t SDA_PIN, BOARD::pin_t SCL_PIN>
bool Software::TWI< SDA_PIN, SCL_PIN >::start_condition ( )
inlineprotected

Generate start condition. Return true(1) if successful otherwise false(0).

Returns
bool.

Definition at line 172 of file TWI.h.

template<BOARD::pin_t SDA_PIN, BOARD::pin_t SCL_PIN>
bool Software::TWI< SDA_PIN, SCL_PIN >::stop_condition ( )
inlineprotected

Generate stop condition. Return true(1) if successful otherwise false(0).

Returns
bool.

Definition at line 205 of file TWI.h.

void TWI::unlock ( )
inlineprotectedinherited

Unlock bus manager.

Definition at line 185 of file TWI.h.

template<BOARD::pin_t SDA_PIN, BOARD::pin_t SCL_PIN>
virtual int Software::TWI< SDA_PIN, SCL_PIN >::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 109 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.

template<BOARD::pin_t SDA_PIN, BOARD::pin_t SCL_PIN>
bool Software::TWI< SDA_PIN, SCL_PIN >::write_bit ( bool  value)
inlineprotected

Write bit to device. Return true(1) if successful otherwise false(0).

Parameters
[in]valueto write to device.
Returns
bool.

Definition at line 222 of file TWI.h.

template<BOARD::pin_t SDA_PIN, BOARD::pin_t SCL_PIN>
bool Software::TWI< SDA_PIN, SCL_PIN >::write_byte ( uint8_t  byte,
bool &  nack 
)
inlineprotected

Write byte to device. Return true(1) and nack bit if successful otherwise false(0).

Parameters
[in]byteto write to device.
[out]nackfrom device.
Returns
bool.

Definition at line 258 of file TWI.h.

Member Data Documentation

template<BOARD::pin_t SDA_PIN, BOARD::pin_t SCL_PIN>
const int Software::TWI< SDA_PIN, SCL_PIN >::CLOCK_STRETCHING_RETRY_MAX = 25
staticprotected

Maximum number of clock stretching retries: 100 us

Definition at line 142 of file TWI.h.

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.

template<BOARD::pin_t SDA_PIN, BOARD::pin_t SCL_PIN>
GPIO<SCL_PIN> Software::TWI< SDA_PIN, SCL_PIN >::m_scl
protected

Clock signal pin.

Definition at line 148 of file TWI.h.

template<BOARD::pin_t SDA_PIN, BOARD::pin_t SCL_PIN>
GPIO<SDA_PIN> Software::TWI< SDA_PIN, SCL_PIN >::m_sda
protected

Data signal pin.

Definition at line 145 of file TWI.h.

template<BOARD::pin_t SDA_PIN, BOARD::pin_t SCL_PIN>
bool Software::TWI< SDA_PIN, SCL_PIN >::m_start
protected

Transaction state; start or repeated start condition.

Definition at line 151 of file TWI.h.

template<BOARD::pin_t SDA_PIN, BOARD::pin_t SCL_PIN>
const int Software::TWI< SDA_PIN, SCL_PIN >::T1 = 4
staticprotected

Start condition delay time: 4.0 us (100 kHz)

Definition at line 136 of file TWI.h.

template<BOARD::pin_t SDA_PIN, BOARD::pin_t SCL_PIN>
const int Software::TWI< SDA_PIN, SCL_PIN >::T2 = 5
staticprotected

Basic clock delay time: 4.7 us (100 kHz)

Definition at line 139 of file TWI.h.


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