PropWare
C++ objects and CMake build system for Parallax Propeller
|
#include <i2c.h>
Public Member Functions | |
I2C (const Pin::Mask scl=DEFAULT_SCL_MASK, const Pin::Mask sda=DEFAULT_SDA_MASK, const unsigned int frequency=DEFAULT_FREQUENCY) | |
Create a basic I2C instance. 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... | |
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... | |
Static Public Attributes | |
static const Pin::Mask | DEFAULT_SCL_MASK = Pin::P28 |
static const Pin::Mask | DEFAULT_SDA_MASK = Pin::P29 |
static const unsigned int | DEFAULT_FREQUENCY = 400000 |
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.
PropWare::I2C::I2C | ( | const Pin::Mask | scl = DEFAULT_SCL_MASK , |
const Pin::Mask | sda = DEFAULT_SDA_MASK , |
||
const unsigned int | frequency = DEFAULT_FREQUENCY |
||
) |
uint8_t PropWare::I2C::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
[in] | device | 7 bit slave I2C address (in bits 7-1, with bit 0 set to 0) |
[in] | address | slave register address |
Definition at line 140 of file i2c.h.
bool PropWare::I2C::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
[in] | device | the 7 bit slave I2C address (in bits 7-1, with bit 0 set to 0). |
[in] | address | the 8 bit slave register address. |
[out] | bytes | the address to begin storing the read bytes at. |
[in] | size | the number of bytes to read. |
Definition at line 208 of file i2c.h.
bool PropWare::I2C::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 | | | +-----—+-—+----—+--—+---—+--—+---—+---—+-—+
[in] | device | the 7 bit slave I2C address (in bits 7-1, with bit 0 set to 0) |
[out] | bytes | the address to begin storing the read bytes at. |
[in] | size | the number of bytes to read. |
Definition at line 276 of file i2c.h.
bool PropWare::I2C::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 | | +-----—+-—+----—+--—+-—+
[in] | device | 7-bit shifted address device (in bits 7-1, not 6-0) |
Definition at line 88 of file i2c.h.
bool PropWare::I2C::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
[in] | device | 7 bit slave I2C address (in bits 7-1, with bit 0 set to 0) |
[in] | address | slave register address |
[in] | byte | 8 bits of data to store at address |
Definition at line 112 of file i2c.h.
bool PropWare::I2C::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
[in] | device | 7 bit slave I2C address (in bits 7-1, with bit 0 set to 0) |
[in] | address | slave register address |
[in] | bytes | set of bytes to store on device, starting at register address. |
[in] | size | number of bytes to write |
Definition at line 173 of file i2c.h.
bool PropWare::I2C::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
[in] | device | the 7 bit slave I2C address (in bits 7-1, with bit 0 set to 0) |
[in] | byte | the 8 bits of data to send to device |
Definition at line 244 of file i2c.h.
|
inherited |
|
inherited |
|
inherited |