Skip to content

062 Multi-File Sensor Node ​

The entry file begins with three local imports:

text
use ./power.mc
use ./mcu.mc
use ./sensor.mc

use loads definitions from another MCode file. Each path starts with ./, so it is resolved relative to 062-main.mc, not the shell's current directory. The imported files have distinct jobs:

  • power.mc defines a SENSOR_NODE_POWER module with VIN, V3V3, and GND ports. It contains an LDO and its input and output capacitors.
  • mcu.mc defines SENSOR_NODE_MCU with an I2C Master interface.
  • sensor.mc defines I2C_TEMP_SENSOR with an I2C Slave interface.

Loading a file does not instantiate its definitions. module main creates one power-module instance and one instance of each imported component. The explicit paths U_POWER.VIN, U_POWER.V3V3, and U_POWER.GND connect the parent design to the imported module ports. The MCU and sensor then share V3V3, GND, I2C_SCL, and I2C_SDA; one pull-up resistor is added to each bus line using the circuit pattern from 03-define-components-and-interfaces/034-i2c-sensor-component.mc.

Run MCC only on the entry file; it loads the three dependencies automatically.

bash
eval "$(../mcc/scripts/mcc-slot.sh)"
MCC_SYSTEM_ROOT="$(cd .. && pwd)" "$MCC_BIN" --local parse 06-multi-file-project/062-main.mc --lib mcode --pass1 --pass2 --top main
MCC_SYSTEM_ROOT="$(cd .. && pwd)" "$MCC_BIN" --local parse 06-multi-file-project/062-main.mc --lib mcode --viz --top main -o 06-multi-file-project/062-main.html

Source files ​

text
// Example: Multi-File Sensor Node
// Goal: Import local files and reuse a power module in a small sensor node.
// Language focus: use, cross-file component and module reuse.

// Each use path is resolved relative to this entry file.
use ./power.mc
use ./mcu.mc
use ./sensor.mc

module main
{
    io GND
    io I2C_SCL
    io I2C_SDA
    io V3V3
    io VIN

    SENSOR_NODE_POWER U_POWER
    SENSOR_NODE_MCU U_MCU
    I2C_TEMP_SENSOR U_SENSOR
    RES R_SCL(4700R, 50V)
    RES R_SDA(4700R, 50V)

    VIN -> U_POWER.VIN
    U_POWER.V3V3 -> V3V3
    U_POWER.GND -> GND

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

    U_MCU.I2C0.SCL -> I2C_SCL
    U_SENSOR.I2C0.SCL -> I2C_SCL
    U_MCU.I2C0.SDA -> I2C_SDA
    U_SENSOR.I2C0.SDA -> I2C_SDA

    V3V3 -> R_SCL -> I2C_SCL
    V3V3 -> R_SDA -> I2C_SDA
}
text
// Example: Sensor Node Power File
// Goal: Provide a reusable 5 V to 3.3 V power module for a multi-file example.
// Language focus: cross-file module definition and module ports.

use mcfam.power/reg.mc

module SENSOR_NODE_POWER(in VIN, out V3V3, psnk GND)
{
    REG.LDO U_LDO(3.3V, 500mA, 5V, 0.3V)
    CAP.MLCC C_IN(1uF, 10V)
    CAP.MLCC C_OUT(1uF, 10V)

    VIN -> U_LDO.Vin
    U_LDO.Vout -> V3V3
    U_LDO.GND -> GND
    VIN -> C_IN -> GND
    V3V3 -> C_OUT -> GND
}
text
// Example: Sensor Node MCU File
// Goal: Provide an MCU component for a multi-file example.
// Language focus: cross-file component definition, interface binding.

component SENSOR_NODE_MCU
{
    name = "Sensor Node MCU"
    pins = [
        io [1:2] = I2C0::I2C(MASTER)
        psnk [3,4] = [VCC, GND]
    ]
}
text
// Example: Sensor Node Sensor File
// Goal: Provide an I2C sensor component for a multi-file example.
// Language focus: cross-file component definition, interface binding.

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

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