Main Content

Generate Efficient Code for Bus Signals

In a model, you use bus signals to package multiple signals together into a single signal line. You can create virtual or nonvirtual bus signals. The representation in the generated code depends on:

  • For a virtual bus, the generated code appears as if the bus did not exist.

  • Generated code for a nonvirtual bus represents the bus data with a structure. When you want to trace the correspondence between the model and the code, the use of a structure in the generated code can be helpful. To generate structures using nonvirtual bus signals, see Organize Data into Structures in Generated Code.

For general information about buses, see and Composite Interface Guidelines.

To generate efficient code from models that contain bus signals, eliminate unnecessary data copies by following best practices as you construct the model.

Code Efficiency for Bus Signals

When you use buses in a model for which you intend to generate code:

  • Setting bus diagnostic configuration parameters can make model development easier.

  • The bus implementation techniques, and the choice of a nonvirtual or virtual bus, can influence the speed, size, and clarity of the generated code.

  • Some useful bus implementation techniques are not immediately obvious.

When you work with buses, these guidelines help you to improve the results. The guidelines describe techniques to:

  • Simplify the layout of the model.

  • Increase the efficiency of generated code.

  • Define data structures for function (subsystem) interfaces.

  • Define data structures that match existing data structures in external C code.

There are some trade-offs among speed, size, and clarity. For example, the code for nonvirtual buses is easier to read because the buses appear in the code as structures, but the code for virtual buses is faster because virtual buses do not require copying signal data. Apply some of the guidelines based on where you are in the application development process.

Set Bus Diagnostics

Simulink® provides diagnostics that you can use to optimize bus usage. On the Configuration Parameters > Diagnostics > Connectivity pane, use these configuration parameters:

Optimize Virtual and Nonvirtual Buses

Virtual buses are graphical conveniences that do not affect generated code. As a result, the code generation engine is able to fully optimize the signals in the bus. Use virtual buses rather than nonvirtual buses to generate efficient code. You can convert between virtual and nonvirtual buses by using Signal Conversion blocks. In some cases, Simulink automatically converts a virtual bus to a nonvirtual bus when required. For example, a Stateflow chart converts an input virtual bus to a nonvirtual bus.

To bundle function-call signals, you must use a virtual bus.

You must use nonvirtual buses for:

  • Nonauto storage classes

  • Generating a specific structure from the bus

  • Root-level Inport or Outport blocks when the bus has mixed data types

Avoid Nonlocal Nested Buses in Nonvirtual Buses

Buses can contain subordinate buses. To generate efficient code, set the storage classes of subordinate buses to Auto. Setting the storage class to Auto eliminates:

  • Allocation of redundant memory for the subordinate bus signal and for the parent bus signal

  • Additional copy operations (copying data to the subordinate bus, and then copying from the subordinate bus to the final bus)

This model contains nonvirtual bus signals. The subordinate bus signals Sub_Bus_1 and Sub_Bus_2 use the storage class Auto.

The generated code algorithm efficiently assigns the input signal data to the bus signals.

void ex_nonvirtual_buses_step(void)
  Nonvirtual_In_One.SimpleBus_1.A1 = A1;
  Nonvirtual_In_One.SimpleBus_1.A2 = A2;
  Nonvirtual_In_One.SimpleBus_2.A3 = A3;
  Nonvirtual_In_One.SimpleBus_2.A4 = A4;
  Nonvirtual_In_One.A5 = A5;

See Also

Related Topics