Skip to content

034 I2C Sensor Component ​

034-i2c-sensor-component.mc defines one I2C controller component and one I2C sensor component. The same I2C interface is bound with different roles: I2C(Master) for the MCU and I2C(Slave) for the sensor.

Both roles expose SCL and SDA, so matching members join the shared I2C_SCL and I2C_SDA nets. I2C uses open-drain signaling, so each line also has a pull-up resistor to V3V3. This example reuses interface binding and adds the idea that two component definitions can appear before one runnable module main.

bash
eval "$(../mcc/scripts/mcc-slot.sh)"
MCC_SYSTEM_ROOT="$(cd .. && pwd)" "$MCC_BIN" --local parse 03-define-components-and-interfaces/034-i2c-sensor-component.mc --lib mcode --pass1 --pass2
MCC_SYSTEM_ROOT="$(cd .. && pwd)" "$MCC_BIN" --local parse 03-define-components-and-interfaces/034-i2c-sensor-component.mc --lib mcode --viz -o 03-define-components-and-interfaces/034-i2c-sensor-component.html

Source ​

text
// Example: I2C Sensor Component
// Goal: Define two components with matching I2C interfaces.
// Language focus: matching interface roles and shared bus nets.

component MCU_I2C
{
    name = "MCU with I2C"
    pins = [
        io [1:2] = I2C0::I2C(MASTER)
        psnk [3,4] = [VCC, GND]
    ]
}

component SENSOR_I2C
{
    name = "I2C Sensor"
    pins = [
        io [1:2] = I2C0::I2C(SLAVE)
        psnk [3,4] = [VCC, GND]
    ]
}

module main
{
    io GND
    io I2C_SCL
    io I2C_SDA
    io V3V3

    DC.SRC PWR(3.3V, 100mA)
    MCU_I2C U_MCU
    SENSOR_I2C U_SENSOR
    RES R_SCL(4700R, 50V)
    RES R_SDA(4700R, 50V)

    PWR.1 -> V3V3
    PWR.2 -> GND

    V3V3 -> U_MCU.VCC
    V3V3 -> U_SENSOR.VCC
    U_MCU.GND -> GND
    U_SENSOR.GND -> GND

    // Master and Slave expose the same SCL and SDA member names.
    U_MCU.I2C0.SCL -> I2C_SCL
    U_SENSOR.I2C0.SCL -> I2C_SCL
    U_MCU.I2C0.SDA -> I2C_SDA
    U_SENSOR.I2C0.SDA -> I2C_SDA

    // I2C lines are open-drain, so each shared line needs a pull-up.
    V3V3 -> R_SCL -> I2C_SCL
    V3V3 -> R_SDA -> I2C_SDA
}

MCode Bench is the public face of the MCode stack. Closed capabilities are served through the MCode gateway.