|
NetBurner 3.5.8
PDF Version |
Software I2C master driven directly from a pair of GPIO pins. More...
Classes | |
| class | BBI2C |
| Software (bit-banged) I2C master driver. More... | |
Software I2C master driven directly from a pair of GPIO pins.
#include< bb_i2c.h>
The BBI2C class implements an I2C master entirely in software, by toggling two general purpose I/O pins. It does not use any I2C peripheral hardware, so it can put an I2C bus on any pair of GPIO capable pins.
Most applications should use the TwoWire class in Wire.h, which drives the processor's I2C peripheral modules. Reach for BBI2C when:
| BBI2C | TwoWire | |
|---|---|---|
| Implementation | Software, GPIO toggling | I2C peripheral hardware |
| Pins | Any two GPIO capable pins | Fixed sets per platform |
| Controller (master) | Yes | Yes |
| Target (slave) | No | Yes |
| Register address | 0 to 3 bytes | 1 byte |
| Thread safe | No, see below | Yes |
| CPU cost | Busy waits for the whole transaction | Interrupt driven |
Construct the object with the SCL and SDA pins, call setup() once with the bus speed, then use the register level calls. As with any I2C bus, both lines need pull up resistors; the driver configures the pins as open drain and never drives them high.
Devices differ in how many bytes of register address they expect before the data phase. setNumAddressBytes() selects that width, and it applies to every transaction until changed. Address bytes are sent most significant first.
| Call | Bytes sent | Typical device |
|---|---|---|
| setNumAddressBytes(0) | none | Parts with no register pointer, read straight from the device |
| setNumAddressBytes(1) | 1, the default | Most sensors and real time clocks, 8-bit registers |
| setNumAddressBytes(2) | 2 | EEPROMs and switches with 16-bit registers |
| setNumAddressBytes(3) | 3 | Large EEPROMs with 24-bit addressing |
If every device on the bus uses the same width, call it once after setup(). With a mix of devices, set it before each transaction:
ping() reports whether a device acknowledges an address, which is all a bus scan needs. resetBus() clocks SCL until a target that is holding SDA low releases it, then issues a stop, and is the way to recover a hung bus.
I2C_RES_ACK rather than assuming success; I2C_RES_NACK usually means nothing responded at that address.