Connecting I2C Modules and Devices
|
|
Note that Scamps are not configured for multi-master I2C. They are configured to be a master (Scamp) communicating with a peripheral (slave), to keep things simple. If you want Scamps to communicate with each other, the easiest way is serial (serial comms on Scamps can be FAST). Alternatively, the Scamp3e and Scamp2e have RS485 for multi-master networking.
Scanning for Connected I2C Modules
You can confirm the presence of individual slaves using the ping word. For example, to check if a device at address $60 is responding:
$60 ping
ping returns either a true (-1) or a false (0) to the stack indicating the presence of the device, or lack thereof.
The word modules will use ping to scan through all the possible I2C addresses (both 7-bit and 10-bit), showing which devices were found.
Communicating with I2C modules
I2C packets are generated by a sequence of Forth words.
The word start will send a start sequence on the I2C bus. The word stop will send an I2C stop.
The words write and read will take an I2C address from the stack, and send a write or read command respectively to that I2C device. Note that both write and read are transmissions from the master to the slave. You are not reading from the slave (yet), you are sending it a read command indicating that you will read from it subsequently. If the address is less than $77, then 7-bit addressing is used, otherwise 10-bit addressing is used. write and read leave a boolean on the stack indicating whether an ACK (acknowledge) was received from the slave device. The word repstart sends a repeated start command on the I2C bus. A repeated start is used, for example, when you need to write to a slave, then read back from it.
The word start will send a start sequence on the I2C bus. The word stop will send an I2C stop.
The words write and read will take an I2C address from the stack, and send a write or read command respectively to that I2C device. Note that both write and read are transmissions from the master to the slave. You are not reading from the slave (yet), you are sending it a read command indicating that you will read from it subsequently. If the address is less than $77, then 7-bit addressing is used, otherwise 10-bit addressing is used. write and read leave a boolean on the stack indicating whether an ACK (acknowledge) was received from the slave device. The word repstart sends a repeated start command on the I2C bus. A repeated start is used, for example, when you need to write to a slave, then read back from it.
The word send will transmit a byte on the I2C bus, leaving a boolean indicating whether an ACK was received.
The word receive will accept a byte transmitted by slave, leaving that value on the stack. If more data is to be received, then use the word ack to send an acknowledge to the slave. If no more data is to be received, then use the word nack to send a NACK to the slave. receive must be followed by either an ack or nack. Failing to do this can put the slave device into an unknown state, and may cause it to hang.
The word receive will accept a byte transmitted by slave, leaving that value on the stack. If more data is to be received, then use the word ack to send an acknowledge to the slave. If no more data is to be received, then use the word nack to send a NACK to the slave. receive must be followed by either an ack or nack. Failing to do this can put the slave device into an unknown state, and may cause it to hang.
As an example of using the I2C words to communicate with an I2C slave, let's say we have connected a GPIO module which has an NXP PCA9554D chip. (Refer to the NXP PCA9554D datasheet for specifics of how this chip works.) The module is configured using its address selection switch to have an address of $24. The PCA9554D has four registers (0..3). Register 3 is the Configuration Register. To write the value $55 to register 3, we need to send a start, then tell slave $24 we are writing to it. Then we can select the Configuration Register by sending a 3 to the slave, followed by the value. We finish by sending a stop. In Forth, we use the following sequence:
start $24 write 3 send $55 send stop
If this succeeded, there will be three true (-1) values on the stack. If these booleans are not required, they can be dropped.
To read the value back from register 3 of the PCA9554D at address $24:
To read the value back from register 3 of the PCA9554D at address $24:
start $24 write 3 send repstart $24 read receive nack stop
Since we are only reading one byte back from the slave, we send a nack after the receive. If everything worked as it should, two values will be on the stack: a boolean indicating that read was acknowledged and the value of $55 left by the receive word.
Note the use of the repeated start (repstart) after the send, prior to the read command. This allows us to follow on from the write command with a read, without releasing the I2C bus.
Note the use of the repeated start (repstart) after the send, prior to the read command. This allows us to follow on from the write command with a read, without releasing the I2C bus.
Learn : Interfacing : I2C Commands