> For the complete documentation index, see [llms.txt](https://www.opal-rt.com/docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://www.opal-rt.com/docs/cmde-add-on/model-and-simulation/integrate-an-external-i2c-or-spi-slave-device.md).

# Integrate an External I2C or SPI Slave Device

## Introduction

Certain cell monitoring devices can interface with externally connected circuits via **I²C** or **SPI protocols over GPIO lines**. These external circuits—functioning as I²C/SPI slaves—may include sensors, analog multiplexers, serial-to-parallel converters, and other peripherals.&#x20;

While physical GPIO lines are not available in an emulated environment, the I²C/SPI slave functionality can be simulated as an external model running on the target CPU and interfaced with the emulated device on the FPGA. Instead of using traditional I²C or SPI communication, data exchange is performed through VeriStand channels. This guide outlines the required project configuration to integrate the slave model, as well as the command sequence to execute during runtime.

<figure><img src="https://2869406741-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fgtd2gZ5PvjsRaV45Czvu%2Fuploads%2FGiMGMlpfgn21wmlFgjNc%2Fimage.png?alt=media&amp;token=25fab97b-d9c2-4abd-a8ab-31ce478938c6" alt="" width="480"><figcaption><p>Integration of an external I2C or SPI slave device on the real time target.</p></figcaption></figure>

Because the communication occurs on the CPU, the data transfer speed between the external slave and the emulated cell monitoring device is limited by the execution rates of both the external model and the CMDE Add-On.

The **Loop Rate** of the CMDE Add-On can be configured under **Advanced Performance** on the [**CMDE Add-On Custom Device**](https://www.opal-rt.com/docs/cmde-add-on/model-and-simulation/configure-the-cmde-add-on-to-the-system-definition.md) page.

Refer to [**Setting Model Timing**](https://www.ni.com/docs/en-US/bundle/veristand/page/set-model-timing.html) for information on adjusting the execution speed of the external model.

## Configure the project <a href="#project-configuration" id="project-configuration"></a>

{% stepper %}
{% step %}

### Check compatibility

Ensure the emulated cell monitoring device supports **WRCOMM** and **STCOMM** commands. Documentation on supported commands for each **Device** type can be found in this Wiki space.
{% endstep %}

{% step %}

### Add external model

Create a model of the external circuit and add it to the VeriStand project. Refer to [**Integrating and Executing Models**](https://www.ni.com/docs/en-US/bundle/veristand/page/integrate-execute-models.html) for detailed instructions. MathWorks Simulink is commonly used.

The model interface should include:

* **Two inputs** to receive COMM Register data from the emulated cell monitoring device:
  * Input 1: Control bits (ICOMx\[3:0] and FCOMx\[3:0])
  * Input 2: Data bits (Dx\[7:0])
* **Two outputs** to write data to the COMM Register of the emulated cell monitoring device:
  * Output 1: Control bits (ICOMx\[3:0] and FCOMx\[3:0])
  * Output 2: Data bits (Dx\[7:0])
    {% endstep %}

{% step %}

### Map signals

In the VeriStand Editor, connect the signals between the external model and the cell monitoring device:

* Map the **WRCOMM Command** and **WRCOMM Data** device channels to the inputs of the external model.
* Map the outputs of the external model to the **STCOMM Reply Command** and **STCOMM Reply Data** device channels.

<figure><img src="https://2869406741-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fgtd2gZ5PvjsRaV45Czvu%2Fuploads%2FIauwPb3bJ9jiUd4lq1uI%2Fimage.png?alt=media&amp;token=501d2a6d-7514-4a3d-96fb-6c2c427ef163" alt=""><figcaption></figcaption></figure>
{% endstep %}

{% step %}

### Connect SPI Master

Connect the SPI Master to the target DI/O channels to establish SPI communication with the emulated device.

Save and deploy the VeriStand project.
{% endstep %}
{% endstepper %}

## Run the project

During execution, send the following sequence of commands from the **SPI Master** to the emulated device:

1. **WRCOMM**

   Data transmitted by the WRCOMM command is stored in the COMM Register of the emulated cell monitoring device. These values are continually written to the **WRCOMM Command** and **WRCOMM Data** channels, and subsequently received by the external model through the mapped input channels. Note that in a real system, the COMM Register values are transmitted to the I<sup>2</sup>C/SPI slave device only after the STCOMM command is received.
2. **STCOMM**

   When the STCOMM command is received, the emulated device reads the response written to the **STCOMM Reply Command** and **STCOMM Reply Data** channels by the external model. These values are stored in the COMM Register of the emulated device.
3. **RDCOMM**

   The SPI Master reads the values stored in the COMM Register.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://www.opal-rt.com/docs/cmde-add-on/model-and-simulation/integrate-an-external-i2c-or-spi-slave-device.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
