modelI2c
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
| Type | Name | Default | Description |
|---|---|---|---|
| MCU | mcuA | Controller: Resources/Scripts/MCU/i2c_controller.py (SCL = GP4, SDA = GP5) | |
| MCU | mcuB | Target at 0x42: Resources/Scripts/MCU/i2c_target_mem.py (SCL = GP4, SDA = GP5) | |
| Modelica.Electrical.Analog.Basic.Ground | ground | ||
| Modelica.Electrical.Analog.Sources.ConstantVoltage | vdd | 3.3 V supply of the pull-up resistors | |
| Modelica.Electrical.Analog.Basic.Resistor | pullScl | pull-up of SCL: an I2C bus only rises through its pull-ups | |
| Modelica.Electrical.Analog.Basic.Resistor | pullSda | pull-up of SDA | |
| Modelica.Electrical.Analog.Sources.ConstantVoltage | measured | voltage measured by B on GP2 (ADC), read by A through the bus | |
| Modelica.Electrical.Analog.Basic.Resistor | r3 | limits the current of ledB | |
| MicroPythonMCU.Peripherals.LED | ledB | mcuB GP3: switched on by A, which writes register 4 of B |