libpropeller
Making PropellerGCC Easier
 All Classes Files Functions Variables Enumerations Enumerator Macros Pages
Public Member Functions | Public Attributes | List of all members
I2C Class Reference

A basic I2C driver. More...

#include <i2c.h>

Collaboration diagram for I2C:
Collaboration graph
[legend]

Public Member Functions

void Init (const int scl=28, const int sda=29, const int frequency=400000)
 Setup the DIRA, OUTA, and INA registers for scl and sda. More...
 
bool Ping (const unsigned char device)
 Test for the Acknowledge of a device by sending start and the slave address. More...
 
bool Put (const unsigned char device, const unsigned char address, const char byte)
 Put a single byte with the following format: More...
 
unsigned char Get (const unsigned char device, const unsigned char address)
 Get a single byte with the following format: More...
 
bool Put (const unsigned char device, const unsigned char address, const char *bytes, const int size)
 Put multiple bytes with the following format: More...
 
bool Get (const unsigned char device, const unsigned char address, char *bytes, const int size)
 Get multiple bytes with the following format: More...
 
bool Put (const unsigned char device, const char byte)
 Put a single byte, no register address, on the bus with the following format : More...
 
bool Get (const unsigned char device, char *bytes, const int size)
 Get multiple bytes, no register address, on the bus with the following format: More...
 
void Start ()
 Passthrough to base. More...
 
void Stop ()
 Passthrough to base. More...
 
int SendByte (const unsigned char byte)
 Passthrough to base. More...
 
unsigned char ReadByte (const int acknowledge)
 Passthrough to base. More...
 

Public Attributes

I2CBase base_
 

Detailed Description

A basic I2C driver.

All "device" fields should be the 7 bit address of the device, with the low bit set to 0 (the 7 addres bits are the upper bits). This applies to both the Put (write) and Get (read) cases.

Put and Get are based on I2C communication specification as described by ST in the LSM303DLHC and L3GD20 datasheets. Terms:

I2C differs based on the device that you use. For functions that might be device specific, there is a reference indicator. These references are:

If you're using the multibyte Get and Put with ST based devices, be sure to bitwise OR the register address with 0x80 (the MSb to 1) in order to turn on the auto-increment function (see datasheet for L3GD20 for example). This is not done automatically by this library.

Author
SRLM

Member Function Documentation

unsigned char I2C::Get ( const unsigned char  device,
const unsigned char  address 
)
inline

Get a single byte with the following format:

+-----—+-—+----—+--—+--—+--—+-—+----—+--—+---—+---—+-—+ | Master | ST | SAD+W | | SUB | | ST | SAD+R | | | NMAK | SP | | Slave | | | SAK | | SAK | | | SAK | DATA | | | +-----—+-—+----—+--—+--—+--—+-—+----—+--—+---—+---—+-—+

Reference: ST

Parameters
devicethe 7 bit slave I2C address (in bits 7-1, with bit 0 set to 0)
addressthe 8 bit slave register address
Returns
the 8 bits of data read from register address.

Here is the call graph for this function:

bool I2C::Get ( const unsigned char  device,
const unsigned char  address,
char *  bytes,
const int  size 
)
inline

Get multiple bytes with the following format:

                                                        |Repeat for # of bytes -1 | Last byte   |            

+-----—+-—+----—+--—+--—+--—+-—+----—+--—+---—+--—+---—+--—+---—+---—+-—+ | Master | ST | SAD+W | | SUB | | ST | SAD+R | | | MAK | | MAK | | NMAK | SP | | Slave | | | SAK | | SAK | | | SAK | DATA | | DATA | | DATA | | | +-----—+-—+----—+--—+--—+--—+-—+----—+--—+---—+--—+---—+--—+---—+---—+-—+

Reference: ST

Parameters
devicethe 7 bit slave I2C address (in bits 7-1, with bit 0 set to 0).
addressthe 8 bit slave register address.
bytesthe address to begin storing the read bytes at.
sizethe number of bytes to read.
Returns
false if one or more nAcks is received, true otherwise

Here is the call graph for this function:

bool I2C::Get ( const unsigned char  device,
char *  bytes,
const int  size 
)
inline

Get multiple bytes, no register address, on the bus with the following format:

                         |Repeat      |

+-----—+-—+----—+--—+---—+--—+---—+---—+-—+ | Master | ST | SAD+R | | | MAK | | NMAK | SP | | Slave | | | SAK | DATA | | DATA | | | +-----—+-—+----—+--—+---—+--—+---—+---—+-—+

Warning
Notice the lack of a specified register!

Reference: MS

Parameters
devicethe 7 bit slave I2C address (in bits 7-1, with bit 0 set to 0)
bytesthe address to begin storing the read bytes at.
sizethe number of bytes to read.
Returns
false if one or more nAcks is received, true otherwise

Here is the call graph for this function:

void I2C::Init ( const int  scl = 28,
const int  sda = 29,
const int  frequency = 400000 
)
inline

Setup the DIRA, OUTA, and INA registers for scl and sda.

Parameters
sclThe I2C SCL pin. Defaults to the Propeller default SCL pin.
sdaThe I2C SDA pin. Defaults to the Propeller default SDA pin.
frequencyThe frequency in hz to run the bus at.

Here is the call graph for this function:

bool I2C::Ping ( const unsigned char  device)
inline

Test for the Acknowledge of a device by sending start and the slave address.

Useful for polling the bus and seeing what devices are available. Ping uses the following format:

+-----—+-—+----—+--—+-—+ | Master | ST | SAD+W | | SP | | Slave | | | SAK | | +-----—+-—+----—+--—+-—+

Returns
true if ack was received, false otherwise.

Here is the call graph for this function:

bool I2C::Put ( const unsigned char  device,
const unsigned char  address,
const char  byte 
)
inline

Put a single byte with the following format:

+-----—+-—+----—+--—+--—+--—+---—+--—+-—+ | Master | ST | SAD+W | | SUB | | BYTE | | SP | | Slave | | | SAK | | SAK | | SAK | | +-----—+-—+----—+--—+--—+--—+---—+--—+-—+

Reference: ST

Parameters
devicethe 7 bit slave I2C address (in bits 7-1, with bit 0 set to 0)
addressthe 8 bit slave register address
bytethe 8 bits of data to store at address.
Returns
false if one or more nAcks is received, true otherwise

Here is the call graph for this function:

bool I2C::Put ( const unsigned char  device,
const unsigned char  address,
const char *  bytes,
const int  size 
)
inline

Put multiple bytes with the following format:

                                     |Repeat for # of bytes    |

+-----—+-—+----—+--—+--—+--—+---—+--—+---—+--—+-—+ | Master | ST | SAD+W | | SUB | | DATA | | DATA | | SP | | Slave | | | SAK | | SAK | | SAK | | SAK | | +-----—+-—+----—+--—+--—+--—+---—+--—+---—+--—+-—+

Reference: ST

Parameters
devicethe 7 bit slave I2C address (in bits 7-1, with bit 0 set to 0)
addressthe 8 bit slave register address
bytesthe set of bytes to store on device, starting at register address.
sizethe number of bytes to write
Returns
false if one or more nAcks is received, true otherwise

Here is the call graph for this function:

bool I2C::Put ( const unsigned char  device,
const char  byte 
)
inline

Put a single byte, no register address, on the bus with the following format :

+-----—+-—+----—+--—+---—+--—+-—+ | Master | ST | SAD+W | | DATA | | SP | | Slave | | | SAK | | SAK | | +-----—+-—+----—+--—+---—+--—+-—+

Warning
Notice the lack of a specified register!

Reference: MS

Parameters
devicethe 7 bit slave I2C address (in bits 7-1, with bit 0 set to 0)
bytethe 8 bits of data to send to device
Returns
false if one or more nAcks is received, true otherwise

Here is the call graph for this function:

unsigned char I2C::ReadByte ( const int  acknowledge)
inline

Passthrough to base.

See i2cBase::ReadByte(int) for more details.

Here is the call graph for this function:

int I2C::SendByte ( const unsigned char  byte)
inline

Passthrough to base.

See i2cBase::SendByte(unsigned char) for more details.

Here is the call graph for this function:

void I2C::Start ( )
inline

Passthrough to base.

See i2cBase::Start() for more details.

Here is the call graph for this function:

void I2C::Stop ( void  )
inline

Passthrough to base.

See i2cBase::Stop() for more details.

Here is the call graph for this function:

Member Data Documentation

I2CBase I2C::base_

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