PropWare  3.0.0.229
C++ objects and CMake build system for Parallax Propeller
Public Member Functions | Static Public Attributes | List of all members
PropWare::I2CMaster Class Reference

Basic I2C driver. More...

#include <PropWare/serial/i2c/i2cmaster.h>

+ Collaboration diagram for PropWare::I2CMaster:

Public Member Functions

 I2CMaster (const Pin::Mask sclMask=DEFAULT_SCL_MASK, const Pin::Mask sdaMask=DEFAULT_SDA_MASK, const unsigned int frequency=DEFAULT_FREQUENCY)
 Create a basic I2C instance. More...
 
void set_frequency (const unsigned int frequency)
 Set the bus frequency. More...
 
void start () const
 Output a start condition on the I2C bus.
 
void stop () const
 Output a stop condition on the I2C bus.
 
bool send_byte (const uint8_t byte) const
 Output a byte on the I2C bus. More...
 
uint8_t read_byte (const bool acknowledge) const
 Get a byte from the bus. More...
 
bool ping (const uint8_t device) const
 Test for the Acknowledge of a device by sending start and the slave address. More...
 
template<typename T >
bool put (const uint8_t device, const T address, const uint8_t byte) const
 Put a single byte with the following format: More...
 
template<typename T >
uint8_t get (const uint8_t device, const T address) const
 Get a single byte with the following format: More...
 
template<typename T >
bool put (const uint8_t device, const T address, const uint8_t bytes[], const size_t size) const
 Put multiple bytes with the following format: More...
 
template<typename T >
bool get (const uint8_t device, const T address, uint8_t bytes[], const size_t size) const
 Read multiple bytes. More...
 
bool put (const uint8_t device, const uint8_t byte) const
 Put a single byte, no register address, on the bus. More...
 
bool get (const uint8_t device, uint8_t bytes[], const size_t size) const
 Get multiple bytes, no register address. More...
 

Static Public Attributes

static const Pin::Mask DEFAULT_SCL_MASK = Pin::Mask::P28
 
static const Pin::Mask DEFAULT_SDA_MASK = Pin::Mask::P29
 
static const unsigned int DEFAULT_FREQUENCY = 400000
 

Detailed Description

Basic I2C driver.

Requires that the SDA and SCL pins have sufficient pullups. These should be selected based on the capacitance of the devices on the I2C bus, and the expected clock speed (400kHz currently).

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.

Examples
I2CSlave_Demo.cpp, and PropWareI2C_Demo.cpp.

Definition at line 64 of file i2cmaster.h.

Constructor & Destructor Documentation

◆ I2CMaster()

PropWare::I2CMaster::I2CMaster ( const Pin::Mask  sclMask = DEFAULT_SCL_MASK,
const Pin::Mask  sdaMask = DEFAULT_SDA_MASK,
const unsigned int  frequency = DEFAULT_FREQUENCY 
)

Create a basic I2C instance.

Parameters
[in]sclMaskPin mask for the SCL pin (default value uses the EEPROM SCL line)
[in]sdaMaskPin mask for the SDA pin (default value uses the EEPROM SDA line)
[in]frequencyFrequency to run the bus (default is highest standard I2C frequency of 400 kHz)

Definition at line 78 of file i2cmaster.h.

+ Here is the call graph for this function:

Member Function Documentation

◆ get() [1/3]

template<typename T >
uint8_t PropWare::I2CMaster::get ( const uint8_t  device,
const T  address 
) const

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
[in]device7 bit slave I2C address (in bits 7-1, with bit 0 set to 0)
[in]addressslave register address
Returns
the 8 bits of data read from register address.

Definition at line 327 of file i2cmaster.h.

+ Here is the caller graph for this function:

◆ get() [2/3]

template<typename T >
bool PropWare::I2CMaster::get ( const uint8_t  device,
const T  address,
uint8_t  bytes[],
const size_t  size 
) const

Read multiple bytes.

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

Definition at line 408 of file i2cmaster.h.

◆ get() [3/3]

bool PropWare::I2CMaster::get ( const uint8_t  device,
uint8_t  bytes[],
const size_t  size 
) const

Get multiple bytes, no register address.

Reference: MS Follows the format:

                         |Repeat      |

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

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

Definition at line 483 of file i2cmaster.h.

+ Here is the call graph for this function:

◆ ping()

bool PropWare::I2CMaster::ping ( const uint8_t  device) const

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 | | +-----—+-—+----—+--—+-—+

Parameters
[in]device7-bit shifted address device (in bits 7-1, not 6-0)
Returns
true if ack was received, false otherwise

Definition at line 272 of file i2cmaster.h.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ put() [1/3]

template<typename T >
bool PropWare::I2CMaster::put ( const uint8_t  device,
const T  address,
const uint8_t  byte 
) const

Put a single byte with the following format:

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

Reference: ST

Parameters
[in]device7 bit slave I2C address (in bits 7-1, with bit 0 set to 0)
[in]addressslave register address
[in]byte8 bits of data to store at address
Returns
false if one or more nAcks is received, true otherwise

Definition at line 296 of file i2cmaster.h.

+ Here is the caller graph for this function:

◆ put() [2/3]

template<typename T >
bool PropWare::I2CMaster::put ( const uint8_t  device,
const T  address,
const uint8_t  bytes[],
const size_t  size 
) const

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
[in]device7 bit slave I2C address (in bits 7-1, with bit 0 set to 0)
[in]addressslave register address
[in]bytesset of bytes to store on device, starting at register address.
[in]sizenumber of bytes to write
Returns
false if one or more nAcks is received, true otherwise

Definition at line 367 of file i2cmaster.h.

◆ put() [3/3]

bool PropWare::I2CMaster::put ( const uint8_t  device,
const uint8_t  byte 
) const

Put a single byte, no register address, on the bus.

Follows the format:

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

Reference: MS

Warning
Notice the lack of a specified register!
Parameters
[in]devicethe 7 bit slave I2C address (in bits 7-1, with bit 0 set to 0)
[in]bytethe 8 bits of data to send to device
Returns
false if one or more nAcks is received, true otherwise

Definition at line 450 of file i2cmaster.h.

+ Here is the call graph for this function:

◆ read_byte()

uint8_t PropWare::I2CMaster::read_byte ( const bool  acknowledge) const

Get a byte from the bus.

Parameters
[in]acknowledgetrue to acknowledge the byte received, false otherwise
Returns
Byte clocked in from the bus

Definition at line 197 of file i2cmaster.h.

◆ send_byte()

bool PropWare::I2CMaster::send_byte ( const uint8_t  byte) const

Output a byte on the I2C bus.

Parameters
[in]byte8 bits to send on the bus
Returns
true if the device acknowledges, false otherwise

Definition at line 134 of file i2cmaster.h.

◆ set_frequency()

void PropWare::I2CMaster::set_frequency ( const unsigned int  frequency)

Set the bus frequency.

Parameters
[in]frequencyFrequency in Hz to run the bus

Definition at line 94 of file i2cmaster.h.

+ Here is the caller graph for this function:

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