The code in I2CBASE.FTH provides the following words for constructing drivers for I2C devices:
START_I2C | Generate a START condition |
STOP_I2C | Generate a STOP condition |
READ_BYTE_I2C | Read a byte from the I2C lines |
WRITE_BYTE_I2C | Write a byte to the I2C lines |
+ACK_I2C | Generate and test the ACK bit |
-ACK_I2C | Do not generate and test the ACK bit. |
The code assumes that bit banging primitives are used to generate the I2C signals, and that these are provided by low level code. Examples of the low level code are provided in files named I2CxxxDRV.FTH where xxx indicates the hardware.
: start_i2c \ --
Define a start condition, bring SDA high-low while SCL is high
: stop_i2c \ --
Define a stop condition, bring SDA low-high while SCL is high
1 value ack_i2c? \ -- n
If a non-zero value is used, the system will generate the ACK bit on SDA for reads, otherwise SDA is left high. For a non-zero value on writes, the system will check the ACK bit and THROW if it is non-zero.
: +ack_i2c \ --
check ACK cycles.
: -ack_i2c \ --
Do not check ACK cycles.
: read_byte_i2c \ -- in_byte
Read a byte, MSB first. If the value I2C_ACK? is non-zero, pull SDA low during the ACK bit, otherwise leave it high
: write_byte_i2c \ byte --
Write a byte by shifting, MSB first. If ACK_I2C? is set, read ack bit on SDA, and THROW if the ACK bit is high.