modelI2c

I2C between two microcontrollers: A is the controller, B is a target that behaves as a memory (machine.I2CTarget with mem=)

Extends from Modelica.Icons.Example (Icon for runnable examples).

Information

Electrical I2C bus (open drain, 100 kHz) between two MCU blocks: SCL and SDA are shared by both boards and only rise through the two 4.7 kΩ pull-up resistors, as on a real bus. Board A (i2c_controller.py) is the controller (machine.I2C); board B (i2c_target_mem.py) is a target at address 0x42 (machine.I2CTarget) that exposes a bytearray as a memory: it needs no handler, the library answers the controller on its own while the program of B only keeps the memory up to date.

Register map of B: bytes 0-1 = last ADC reading of its GP2 (2 V here), byte 4 = its LED. A scans the bus (it finds 0x42), writes 1 to register 4 (writeto_mem: ledB lights up), reads back registers 0-1 (readfrom_mem) and prints the voltage measured by B; its GP7 goes high if everything is right. Variant where B answers from an IRQ handler: Examples.MultiMcu.I2cIrq.

Components

TypeNameDefaultDescription
MCUmcuAController: Resources/Scripts/MCU/i2c_controller.py (SCL = GP4, SDA = GP5)
MCUmcuBTarget at 0x42: Resources/Scripts/MCU/i2c_target_mem.py (SCL = GP4, SDA = GP5)
Modelica.Electrical.Analog.Basic.Groundground
Modelica.Electrical.Analog.Sources.ConstantVoltagevdd3.3 V supply of the pull-up resistors
Modelica.Electrical.Analog.Basic.ResistorpullSclpull-up of SCL: an I2C bus only rises through its pull-ups
Modelica.Electrical.Analog.Basic.ResistorpullSdapull-up of SDA
Modelica.Electrical.Analog.Sources.ConstantVoltagemeasuredvoltage measured by B on GP2 (ADC), read by A through the bus
Modelica.Electrical.Analog.Basic.Resistorr3limits the current of ledB
MicroPythonMCU.Peripherals.LEDledBmcuB GP3: switched on by A, which writes register 4 of B