PropWare
3.0.0.229
C++ objects and CMake build system for Parallax Propeller
|
#include <PropWare/serial/i2c/i2cmaster.h>
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 |
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.
Definition at line 64 of file i2cmaster.h.
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.
[in] | sclMask | Pin mask for the SCL pin (default value uses the EEPROM SCL line) |
[in] | sdaMask | Pin mask for the SDA pin (default value uses the EEPROM SDA line) |
[in] | frequency | Frequency to run the bus (default is highest standard I2C frequency of 400 kHz) |
Definition at line 78 of file i2cmaster.h.
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
[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 327 of file i2cmaster.h.
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
[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 408 of file i2cmaster.h.
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 | | | +-----—+-—+----—+--—+---—+--—+---—+---—+-—+
[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 483 of file i2cmaster.h.
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 | | +-----—+-—+----—+--—+-—+
[in] | device | 7-bit shifted address device (in bits 7-1, not 6-0) |
Definition at line 272 of file i2cmaster.h.
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
[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 296 of file i2cmaster.h.
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
[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 367 of file i2cmaster.h.
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
[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 450 of file i2cmaster.h.
uint8_t PropWare::I2CMaster::read_byte | ( | const bool | acknowledge | ) | const |
Get a byte from the bus.
[in] | acknowledge | true to acknowledge the byte received, false otherwise |
Definition at line 197 of file i2cmaster.h.
bool PropWare::I2CMaster::send_byte | ( | const uint8_t | byte | ) | const |
Output a byte on the I2C bus.
[in] | byte | 8 bits to send on the bus |
Definition at line 134 of file i2cmaster.h.
void PropWare::I2CMaster::set_frequency | ( | const unsigned int | frequency | ) |
Set the bus frequency.
[in] | frequency | Frequency in Hz to run the bus |
Definition at line 94 of file i2cmaster.h.