Decoding I2C protocol (from file)
Description
This example demonstrates how to decode the I2C protocol using logic analyser commands. Red Pitaya loads the I2C data from the “i2c_data.bin” file and decodes it. When configuring the example, make sure that the I2C decoder settings match the capture settings.
Here are the possible values for the decoder:
address_format - Shifted =
0, Unshifted =1invert_bit -
0, 1(invert input data signal)decoder lines -
1 - 8(1 == DIO0_P)trigger channel -
LA_T_CHANNEL_1 - LA_T_CHANNEL_8(1 == DIO0_P)
The possible decoded data packet types are:
Start - The start bit of the I2C message (control value 0)
Repeat start - The repeat start bit of the I2C message (control value 1)
Stop -The stop bit of the I2C message (control value 2)
Ack - The acknowledge bit of the I2C message (control value 3)
Nack - The not acknowledge bit of the I2C message (control value 4)
Read address - The address of the I2C device (control value 5)
Write address - The address of the I2C device (control value 6)
Read data - The data read from the I2C device (control value 7)
Write data - The data written to the I2C device (control value 8)
Each decoded data packet has the following parameters:
control - Data identifier.
data - Data contained in the protocol.
line name - Line name according to the protocol for which the data was decoded. Must be specified in the decoder settings, otherwise they will not be present.
sampleStart - Starting position in the data to be recognized in samples. 0 = first sample, 1 = second sample, etc. The value is not an integer, since the bit width can be real depending on the protocol. Includes the pretrigger samples.
length - Length of the recognized block in samples.
\[\text{l_{packet}} = \frac{125~\mathrm{MHz}}{\text{decimation} \cdot \text{i2c_speed}} \cdot \text{bitsInPack}\]bitsInPack - Number of recognized data bits. Multiple of 0.5.
Required hardware
Red Pitaya
Required software
IN DEV - Nightly Build OS
Note
This code is written for IN DEV and will not work with the currently available OS releases. To use this functionality, please install the Nightly Build OS versions.
API Code Examples
Code - Python
#!/usr/bin/python3
""" Example for decoding LA I2C data from file. Settings are also loaded from a file.
"""
import rp_la
rp_cla = rp_la.CLAController()
rp_cla.loadFromFile("i2c_data.bin", True, 0)
rp_cla.addDecoder("I2C", rp_la.LA_DECODER_I2C)
f = open("i2c_settings.json", "r", encoding="utf-8")
rp_cla.setDecoderSettings("I2C", f.read())
print("Settings:")
print(rp_cla.getDecoderSettings("I2C"))
print("\nDecoded data\n")
decode = rp_cla.decode("I2C")
for index in range(len(decode)):
print(rp_cla.getAnnotation(rp_la.LA_DECODER_I2C, decode[index]['control']), " = ", decode[index])
del rp_cla
Modifying decoder settings
#!/usr/bin/python3
"""This script demonstrates how to modify the settings of the I2C decoder in the json files."""
import json
import rp_hw_profiles
import rp_la
# Define callback
class Callback(rp_la.CLACallback):
def captureStatus(self, controller, isTimeout, bytes, samples, preTrig, postTrig):
print("captureStatus timeout =",isTimeout,"bytes =",bytes,"samples =",samples,"preTrig =",preTrig,"postTrig =",postTrig)
def decodeDone(self, controller, name):
print("Decode done ",name)
i2c_decoder = "I2C"
decimation = 16
acq_rate = int(rp_hw_profiles.rp_HPGetBaseSpeedHzOrDefault() / decimation)
# Create controller
rp_cla = rp_la.CLAController()
callback = Callback()
rp_cla.setDelegate(callback.__disown__())
# LA FPGA must be loaded/LA Application must be open
rp_cla.initFpga()
# Add decoders
rp_cla.addDecoder(i2c_decoder, rp_la.LA_DECODER_I2C)
# Get settings from Red Pitaya
i2c_settings = rp_cla.getDecoderSettings(i2c_decoder)
print(f"I2C decoder settings: {i2c_settings}")
# Modify settings
i2c_settings = json.loads(i2c_settings)
i2c_settings["acq_speed" ] = 400000 # Fixed to 400 kHz
i2c_settings["address_format"] = 0 # Shifted = 0, Unshifted = 1
i2c_settings["invert_bit" ] = 0
i2c_settings["scl" ] = 1 # I2C SCL (1 - first line. Valid values are from 1 to 8), 0 == disabled
i2c_settings["sda" ] = 2 # I2C SDA
# Save json data to a file
with open("i2c_settings.json", "w", encoding='utf8') as json_file:
json.dump(i2c_settings, json_file, indent=4)
del rp_cla
Save decoder settings to a json file
#!/usr/bin/python3
"""This script demonstrates how to save the settings of the I2C decoder to a json file."""
import json
import rp_la
# Define callback
class Callback(rp_la.CLACallback):
def captureStatus(self, controller, isTimeout, bytes, samples, preTrig, postTrig):
print("captureStatus timeout =",isTimeout,"bytes =",bytes,"samples =",samples,"preTrig =",preTrig,"postTrig =",postTrig)
def decodeDone(self, controller, name):
print("Decode done ",name)
# Create controller
rp_cla = rp_la.CLAController()
callback = Callback()
rp_cla.setDelegate(callback.__disown__())
# LA FPGA must be loaded/LA Application must be open
rp_cla.initFpga()
# Add decoders
rp_cla.addDecoder("I2C", rp_la.LA_DECODER_I2C)
# Get settings from Red Pitaya
i2c_settings = rp_cla.getDecoderSettings("I2C")
print(f"I2C decoder settings: {i2c_settings}")
# Save json data to a file
i2c_settings = json.loads(i2c_settings)
with open("i2c_settings.json", "w", encoding='utf8') as json_file:
json.dump(i2c_settings, json_file, indent=4)
del rp_cla