Simulink Bench Example

Simulink Bench

An independent CD6 bench example, not derived from a customer application. It conditions two views of Analogue 1 with result = clamp(voltage * gain + offset, 0, 10 V): one received through normal Dash channel evaluation/Model IPC, the other acquired through the fast ADC API. Both paths execute in one 1 ms Simulink step.

Use an isolated bench CAN bus only. This example transmits diagnostic data, not an actuator command. It sends a classic eight-byte CAN1 frame every millisecond. Check bus load, identifiers, wiring and termination before use. No hardware is programmed automatically.

Features

Feature Demonstration
Regular Dash channel passing Analogue 1 is evaluated by Dash and sent on Model IPC 0x321 every 10 ms; model results return on 0x322 every 10 steps.
Model step integration The runtime adapter calls the generated initialize/step functions serially, using MATLAB-staged timing metadata; it rejects periods other than 1000 us.
Nonvolatile memory Firmware declares an eight-byte calibration layout/defaults. CM7 restores compatible stored gain/offset before model initialization. An explicit XCP store saves them; initialization never overwrites them.
XCP Ginmake creates matching EPK/A2L descriptions for writable BenchGain and BenchOffset, plus read-only runtime identity.
Low-latency A2Ds Exactly one cached input refresh precedes each step; input 1 uses nominal 0.001421 V/count conversion and a 5000 us stale limit.
Urgent CAN Tx The adapter packs and enqueues physical CAN1 0x520 immediately after every step, with a 2000 us lifetime. Normal Dash Output transmission is not involved.

The small fixed IPC layout is packed explicitly in main\main.c; Ginmake imports the DBCs for their compatibility hash but generates only calibration/ identity descriptions, not unused CAN buffers or inaccessible XCP measurements. The physical diagnostic frame is intentionally separate from the IPC DBC set.

Create And Build

Use a runtime release containing this example and compatible CD6 firmware. Install the release’s Arm compiler, CMake/Ninja, licensed Ginmake (2.86 or newer), and MATLAB with Simulink, Simulink Coder and Embedded Coder. No special Simulink blocksets or customer files are needed. The model builder uses stock blocks and is intended for R2023b or newer; MATLAB execution remains a verification gate on a machine with those products.

  1. Extract the runtime ZIP into a new project directory, creating cdx-runtime. Copy the contents of cdx-runtime\examples\simulink-bench into that project directory, including .gitignore. Do not work inside the runtime dependency or overlay the handwritten project template.

  2. In MATLAB, make the project root the current folder and run:

    1addpath(fullfile(pwd, 'model'));
    2run(fullfile('model', 'initialize.m'));
    3create_model;
    4test_model;
    5cdx.workflow('full-build', 'cdx-project.json');

    create_model produces and opens model\APP_bench.slx. It refuses to overwrite a file or loaded model. It is a one-time bootstrap, deliberately separate from the repeatable initialization script. Keep the generated model when editing it and version-control the .slx as model source. Embedded Coder output directories and simulation caches are ignored. There is no hand-authored C substitute in the production build.

  3. For later builds with MATLAB installed on the build PC:

    1.\cdx-runtime\bin\cdx.exe build
    2.\cdx-runtime\bin\cdx.exe verify-image --image build\cm4-release\MDL_FIRM.bin

Outputs include:

  • build\cm4-release\CDX-Simulink-Bench.cdxmodel
  • build\cm4-release\MDL_FIRM.bin (finalized image)
  • ginmake-generated\a2l\CDX-Simulink-Bench.a2l

Never deploy an intermediate raw binary. Debug uses --preset cm4-debug. Changing the model base period also requires intentional adapter/rate/timeout changes; do not replace the staged timing header with a hand-written constant.

Remote MATLAB / Local Compile

Keep the same complete project and runtime release on both PCs. On the MATLAB PC, run create_model once, then test_model and:

1cdx.codegen('cdx-project.json');
2cdx.cli('stage', 'cdx-project.json');

This still requires Ginmake, CMake and the configured compiler on the MATLAB PC for the IPC/configure stage. Copy the resulting entire model-generated directory and the matching project sources/model back to the local project. Do not copy CMake caches, build outputs or .cdx machine-local state. On the local PC run cdx-runtime\bin\cdx.exe compile. It does not invoke MATLAB. Regenerate/stage after any model or interface change; compilation alone cannot prove that staged C is up to date.

Dash Setup

Open dash\bench.cd6.gdd in a compatible Dash Design release. It is a basic editable setup, with no model firmware embedded initially. Load/import the newly built .cdxmodel using Dash Design’s Load Model operation; retain the matching packaged DBCs and existing channel mappings. Save a separate deployment copy, for example dash\bench-model.cd6.gdd, before loading onto a CD6.

If an older copy reports that Bench_* receive channel names already exist, replace the starter with this version of dash\bench.cd6.gdd, then close and reopen it in Dash Design before embedding into the current setup. The corrected native setup preserves identical receive source/output names; older XML imports renamed the sources to Bench_*:Source, preventing model import from reusing them. Copy the updated IPC DBCs too and recompile: their explicit 10 ms cycle time keeps Dash-to-model transmission at 100 Hz after model import replaces the starter transmitter. Missing DBC cycle times default to 2 Hz in Dash Design.

The source is dash\bench.xml. To recreate the editable setup using Dash Design’s unattended CLI, choose a new output filename:

1$app = 'C:\Program Files\GEMS\Dash Design\DashDesign.exe' # Use your installed path.
2$p = Start-Process $app -ArgumentList '--convert-setup setup "dash\bench.xml" "dash\bench-new.cd6.gdd"' -Wait -PassThru
3if ($p.ExitCode -ne 0) { throw 'Dash setup conversion failed' }

Keep the supplied XML as the source of truth. The Dash Design build used for regeneration must preserve CAN source names on XML import (without appending :Source); use the supplied native setup with older builds. XML export omits the transmit frame’s can_id when exporting a native setup back to XML. Before reimporting such an exported XML file, restore <can_id>#321</can_id> on the Model IPC transmit frame. The supplied XML and native setup already contain the correct identifier; this is an exporter round-trip limitation, not a change to the IPC interface.

Link Identifier / settings
Model IPC, Dash to model Standard 0x321, two bytes: unsigned little-endian millivolts.
Model IPC, model to Dash Standard 0x322, eight bytes; see below.
Physical CAN1 urgent Standard 0x520, classic eight-byte frame, 500 kbit/s, every 1 ms. No Dash transmitter should duplicate it.
Physical CAN1 XCP Master to device 0x650, device to master 0x651, classic CAN, 500 kbit/s.

The setup’s gauges show the ordinary input, both returned results, the wrapping step counter, status and urgent rejection count. Nominal ADC scaling is not an individually calibrated voltage measurement. Use the same input scaling when comparing the two paths; their acquisition times are not synchronized.

Result Payloads And Failure Handling

Both result frames use bytes 0..1 for Dash-result millivolts, 2..3 for fast-result millivolts, and 4..5 for the low 16 bits of the step count, all little-endian. The step count wraps after 65.536 seconds; it is not elapsed wall time.

For IPC 0x322, byte 6 is a bit mask:

  • Bit 0: ordinary input received within the last 100 ms.
  • Bit 1: fast input is valid and fresh enough.
  • Bit 2: urgent request rejected on this reporting step (queue full/offline).
  • Bit 3: a previous normal IPC report was rejected because its queue was full. This remains visible until a subsequent report is accepted.

Byte 7 counts urgent enqueue rejections, saturating at 255, and resets at model initialization. Rejected requests are not retried. For urgent 0x520, byte 6 contains only the two input-valid bits and byte 7 is reserved/zero; the result of that same enqueue cannot be reported in the frame being enqueued.

Invalid inputs produce zero transmitted results, even with a nonzero offset. The model still steps with zero for the invalid input. Missing/offline/stale ADC is deliberately a degraded condition, not a model fault. Repeated scans age out through the runtime cache. Other unexpected API errors, invalid calibration (including NaN), a wrong model period, or nonfinite/out-of-range generated outputs return an error to the runtime and fault the model.

Acceptance into an urgent queue is not delivery. Expiry, arbitration, disabled ports, bus errors and resets can discard a request without a completion callback. At 500 kbit/s, budget roughly a quarter to a third of the bus for this traffic, plus XCP and other frames. The native simulator does not emulate fast I/O and therefore shows its invalid/offline diagnostics.

XCP And Nonvolatile Calibration

Load the matching generated A2L, connect using the above CAN1 settings and check the EPK/station name (CDX Simulink Bench) before changing parameters. The A2L describes addresses and types; generic clients may need manual CAN transport configuration. DAQ, STIM, paging and programming are not supported.

Parameter XCP address Type Default Accepted range
BenchGain 0xF000 little-endian IEEE single 1 0..4
BenchOffset 0xF004 little-endian IEEE single, V 0 0..2

For a steady 2 V input, gain 2 and offset 0.5 should produce approximately 4.5 V on both paths. Results saturate at 10 V. Live multi-value updates are not atomic: intermediate gain/offset combinations may be observed. This is a bench demonstration, not a coherent calibration transaction.

An XCP DOWNLOAD changes RAM only. Use the client’s store calibration action (SET_REQUEST(STORE_CAL_REQ)), then wait for EV_STORE_CAL, not just command acceptance, before power cycling. CM7 owns the two-slot eMMC storage; the model does not write files. Storage failure reports EV_USER/IO_ERROR rather than a successful store. Firmware without storage cannot demonstrate persistence. After a completed store and power cycle, the same layout restores gain/offset before the first model initialization. Changing the representation requires a new layout ID/version and matching defaults; rejected records use defaults.

Verification

Run the shipped adapter tests without MATLAB or hardware:

1cmake -S tests -B build\adapter-tests -G "Visual Studio 18 2026" -A x64
2cmake --build build\adapter-tests --config Debug
3ctest --test-dir build\adapter-tests -C Debug --output-on-failure

Select an installed Visual Studio generator (for example 17 2022) if different. Tests compile the real adapter against public CDX headers, with explicit runtime and model doubles confined to tests. They check exact payload bytes, step/refresh/send rates, restored calibration preservation, absent/stale inputs, clock wrap, queue pressure, saturating diagnostics and fatal errors. They do not test Embedded Coder output, eMMC durability or physical CAN latency.

On the MATLAB PC, test_model simulates the actual block diagram using nominal, calibrated, saturation and zero cases and checks the exact 1000 us period. Then generate, stage and build the actual model before deployment.

On hardware:

  1. Verify firmware identity, READY/RUNNING state and matching IPC hash.
  2. Apply safe 0..5 V bench inputs and compare ordinary/fast returned values.
  3. Capture CAN1 0x520: check scaling, counter and input-valid bits. Measure ADC-to-CAN latency and worst-case step execution under representative load.
  4. Check stale/offline/queue-pressure diagnostics without relying on a held input value as a valid sample.
  5. Change gain/offset via XCP, store, wait for completion, power cycle, and verify restored outputs. Separately exercise failed/interrupted stores.

MATLAB creation/simulation/code generation, full generated-model linking, physical deployment, latency and power-cycle persistence require those external tools/hardware and must not be inferred from native adapter test success.