forked from l29ah/libmpsse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
README.I2C
42 lines (31 loc) · 2.16 KB
/
README.I2C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
Libmpsse supports configuring FTDI chips as I2C master devices in order to communicate with I2C slave devices.
When initializing libmpsse, you must specify:
1) That you wish to use the I2C mode
2) The I2C clock frequency
3) The data transfer endianess (I2C is always MSB first)
After initialization, you can communicate with any connected I2C slave device. A standard I2C transaction involves:
1) Sending a start condition to indicate the beginning of a transaction
2) Reading / writing data to and from the slave device
3) Sending a re-start condition, if necessary
4) Sending and recieving ACK/NACK bits
5) Sending a stop condition to indicate the end of a transaction
Finally, once you are finished talking to your I2C slave device you must close the connection to the FTDI chip.
The following is a simple example of using I2C in Python:
i2c = MPSSE(I2C, FOUR_HUNDRED_KHZ) # Initialize libmpsse for I2C operations at 400kHz
i2c.Start() # Send the start condition
i2c.Write("\xA0") # Write one byte to the I2C slave
if i2c.GetAck() == ACK: # Make sure the last written byte was acknowledged
i2c.Start() # Send a re-start condition
data = i2c.Read(1) # Read one byte from the I2C slave
i2c.SendNacks() # Respond with a NACK for all subsequent reads
i2c.Read(1) # Read one last "dummy" byte from the I2C slave in order to send the NACK
i2c.Stop() # Send I2C stop condition
i2c.Close() # Deinitialize libmpsse
A few notes regarding the above code:
1) Acknowledge bits are read/sent automatically by the Read and Write functions. The GetAck() function does not
need to be called in order to generate or read acknowledgement bits; it simply returns the state of the last
received acknowledgement bit (0 == ACK, 1 == NACK).
2) By default, Read will acknowledge all bytes that it receives. You can instruct Read to not acknowledge received
bytes by using the SendNack function, as shown in the example above. Likewise, calling SendAcks will restore the
default behavior of acknowledging all received bytes.
3) Repeated start conditions are generated the same way that the first start condition was generated: by calling Start.