modelPartialI2cDevice

Base of the I2C slave peripherals: open-drain electrical link, bus decoding, behaviour described by a Python script

Extends from PartialSupplyPin (Supply of a peripheral: optional GND and VCC pins, internal supply rail - GND shown and ideal internal supply VOH by default).

Information

Base class of the I2C slave peripherals. It cannot be instantiated directly: a concrete peripheral extends it and sets its parameters — see Peripherals.I2cEchoDevice, Peripherals.I2cGroveLcdRgb, or Peripherals.I2cGenericDevice to start from a script template.

Electrical. The peripheral is connected to a real open-drain SDA/SCL bus: it can only pull SDA to ground or release it. Several peripherals and the microcontroller share the same two wires; the "wired AND" of the bus simply follows from Kirchhoff's laws. The lines only go back up thanks to the pull-up resistors: the bus needs at least one pair, carried by a component with usePullUp = true. Without them, the lines stay low and the microcontroller raises OSError(ETIMEDOUT), as on a real circuit.

Behaviour: a Python script only, at the transaction level — the script sees neither the bits, nor the START/STOP conditions, nor the acknowledges:

  • on_write(addr, data, t, v): a write phase addressed to it has just ended (STOP or repeated START); data holds all the received bytes.
  • on_read(addr, t, v): the master starts reading; return the bytes to send to it (bytes, str, list of integers or integer). They go out one by one; if the master asks for more, on_read() is called again.
  • outputs(): read again after each handler, feeds valueOut.
  • lines(): optional, read again after each handler, feeds line1/line2 (displays).

The component automatically acknowledges any address of the addresses list and any written byte. It does not answer the other addresses: this is what lets several peripherals share the same bus.

The two dots of the icon light up when the peripheral pulls SDA (amber) and during a phase addressed to it (cyan) — visible while replaying a result with animation in OMEdit. Each completed phase produces a line in the simulation log, prefixed with the component name.

Parameters

TypeNameDefaultDescription
Electrical › Supply
BooleanuseSupplyPin (from PartialSupplyPin)falseShow the VCC supply pin: the peripheral is supplied by the circuit (its high levels follow VCC, its consumption IQ is drawn from VCC) - unchecked: ideal internal supply VOH, nothing to wire
Modelica.Units.SI.VoltageVOH (from PartialSupplyPin)Interfaces.VOHLogic high voltage: ideal internal supply, high level of the outputs and of the pull-ups - with the VCC pin, the voltage of VCC is used instead
Modelica.Units.SI.CurrentIQ (from PartialSupplyPin)1e-3Current drawn from VCC by the peripheral (quiescent consumption, outputs excluded)
BooleanuseGroundPin (from PartialSupplyPin)trueShow the GND pin - unchecked: the peripheral is referenced to the simulation ground (0 V), nothing to wire
I2C bus
Stringaddresses"0x42"7-bit address(es) the peripheral answers to, e.g. "0x42" or "0x3E, 0x62" (at most 4)
BooleanusePullUpfalseCarry the pull-up resistors of SDA and SCL to the supply (at least one component of the bus must do it)
Modelica.Units.SI.ResistanceRPullUp4700Pull-up resistance of each line
Behaviour
StringscriptPath""Script .py describing the behaviour (on_write / on_read / outputs / lines)
Inputs / outputs › Inputs (handler argument v)
BooleanuseValueInputfalseTake the quantities from the valueIn connector rather than from a constant
IntegernIn1Number of quantities received from the model
RealfixedValue0Value used when valueIn is not connected
Inputs / outputs › Outputs (return value of outputs())
IntegernOut1Number of quantities returned to the model by outputs() - leave the connector unconnected if unused
Electrical › Levels
Modelica.Units.SI.VoltageVIHInterfaces.VIHThreshold above which an input reads high
Modelica.Units.SI.VoltageVILInterfaces.VILThreshold below which an input reads low
Electrical › Impedances
Modelica.Units.SI.ResistanceROutInterfaces.ROutResistance of the SDA output transistor when it pulls the line to ground
Modelica.Units.SI.ConductanceGOff1e-9Leakage of the SDA output transistor when off (line released)
Modelica.Units.SI.CapacitanceCIn10e-12Input capacitance of each pin (SDA, SCL)
Modelica.Units.SI.ResistanceRIn10Series resistance of each pin (pad), between the bus wire and the input capacitance

Connectors

TypeNameDefaultDescription
Modelica.Electrical.Analog.Interfaces.NegativePinGND (from PartialSupplyPin)Common reference (ground), to be connected to the microcontroller's one - shown when useGroundPin is checked
Modelica.Electrical.Analog.Interfaces.PositivePinVCC (from PartialSupplyPin)Supply of the peripheral (e.g. 3V3(OUT) of a Raspberry Pi Pico) - shown when useSupplyPin is checked
Modelica.Electrical.Analog.Interfaces.PositivePinSDAI2C bus data - to be connected to the SDA pin of the microcontroller and of the other peripherals
Modelica.Electrical.Analog.Interfaces.PositivePinSCLI2C bus clock - to be connected to the SCL pin of the microcontroller and of the other peripherals
Modelica.Blocks.Interfaces.RealInput[nIn]valueInQuantities supplied by the model, passed to the script's handlers (argument v)
Modelica.Blocks.Interfaces.RealOutput[nOut]valueOutQuantities returned by outputs() - the peripheral then becomes an actuator

Components

TypeNameDefaultDescription
BooleansdaDriveLowThe peripheral pulls SDA to ground (acknowledge, or 0 bit read by the master)
BooleanbusyA phase addressed to this peripheral is in progress
IntegereventSeqIncremented at each completed write or read phase
StringlastEventSummary of the last completed phase (log)
Stringline1First line of text returned by lines() (displays)
Stringline2Second line of text returned by lines()