User manual
Interfacing to external I
2
C slave devices
Technically, the correct name for this two-wire serial bus is Inter-IC bus or I
2
C, though it is somemes wrien as
I2C. Informaon on this popular two-wire hardware interface is readily available on the Web;
hp:/www.i2c-
bus.org/ is one starng point you could use. In parcular look for a document called “The I2C-bus and how to
use it (including specificaons).”
I
2
C uses two pins:
• SCL – Serial Clock Line
• SDA – Serial Data line (bidireconal)
Because both the value and direcon (input versus output) of the SCL and SDA pins must be rapidly and
precisely controlled, dedicated I
2
C support funcons have been added to SNAPpy.
NOTE– Parameters shown below in red are only available on selected hardware plaorms and/or
soware versions, beginning with release 2.5. See the SNAP Reference Manual for more details.
• i2cInit(enablePullups, SCL_pin, SDA_pin) – Prepare for I
2
C operaons (call this to set up for I
2
C)
• i2cWrite(byteStr, retries, ignoreFirstAck, endWithRestart) – Send data over I
2
C to another device
• i2cRead(byteStr, numToRead, retries, ignoreFirstAck) – Read data from device
• getI2cResult() – Check the result of the other funcons
These rounes are covered in more detail in the SNAP Reference Manual.
Using these rounes, your SNAPpy script can operate as an I
2
C bus master, and can interact with I
2
C slave
devices.
When performing I
2
C interacons, fixed IO pin assignments are usually used. For example, on an RF100 the
following IO pins are used:
• GPIO 17 is always used as the I
2
C SDA (data) line
• GPIO 18 is always used as the I
2
C SCL (clock) line
Excepons are the STM32W108xB plaorm, and (as of SNAP version 2.5) the ATmega128RFxx plaorms. On
these plaorms, the i2cInit() funcon takes two addional plaorms that specify which IO pins to use for I
2
C
clock (SCL) and data (SDA). (For backwards compability, you do not have to specify values for the SCL_pin and
SDA_pin parameters on ATmega128RFxx plaorms. If you do not, SNAP will default the pins to GPIO_18 and
GPIO_17, respecvely.)
NOTE – These pins are only dedicated if you are actually using the I
2
C funcons. If not, they remain
available for other funcons.
Refer to the plaorm-specific secon for your hardware (located at the back of the SNAP Reference Manual) for
the pin assignments for your plaorm.
Unlike CBUS and SPI, I
2
C does not use separate “chip select” lines. The inial data bytes of each I
2
C transacon
specify an “I
2
C address.” Only the addressed device will respond. So, no addional GPIO pins are needed.
The specifics of which bytes to send to a given I
2
C slave device (and what the response will look like) depend on
the I
2
C device itself. You will have to refer to the manufacturer’s data sheet for any given device to which you
wish to interface.
For examples of using the new SNAPpy I
2
C funcons to interface to external devices, look at the following scripts
that are bundled with Portal:
SNAP® Network Operang System 41