Configure Service Interface
Embedded Coder® simplifies code interface configuration by enabling you to configure service code interfaces. A service code interface configuration specifies interfaces for:
Entry-point functions that external target application code can call
Access function interfaces that the generated entry-point functions can use to call target platform services
The shared Embedded Coder dictionary configured for a model can define multiple code interfaces for each type of model interface element (for example, functions, inports, and data transfers). The dictionary specifies a default code interface for each type of element. Applying default interfaces saves time and reduces the risk of introducing errors in code, especially for larger models and models from which you generate multi-instance code. You can selectively override the default settings for individual functions and data elements.
Service Interface Code Mappings
You can change a code interface configuration for model elements by using the Code Mappings editor or code mappings programming interface. These tools map model interface elements to service code interfaces that are defined in the configured Embedded Coder Dictionary. This example shows how to change the code mappings for a model by using the Code Mappings editor.
If not already open, in the Embedded Coder app, open example model
ComponentDeploymentFcn
. Then open in the Code Mappings editor. In the
Embedded Coder app, below the model diagram, click Code Mappings - Component
Interface.
This figure shows the Code Mappings editor with the Functions tab selected. The entry-point functions listed can be called by external target platform code. You can change the function customization template and function name that the code generator applies when producing the interface code for entry-point functions. The entry-point functions have a void-void interface (they do not pass arguments). They exchange data with external application code by calling target platform services.
You map model data interface elements to service interfaces. To configure service interfaces for inports, outports, data transfers, parameters, data stores, and signals, use the corresponding tabs. For instance, to change the interface definitions that the code generator uses for inports, select the Inports tab.
Each interface for inports, outports, and data transfers specifies a data communication method (outside execution, during execution, or direct access) and access function naming rules. Interfaces for parameters, data stores, signals, and states can specify a storage class that, for example, enables variable tuning or measurement. You have the option of configuring these model data elements as not tunable or not measurable. When a data element is not tunable or measurable, the code generator applies relevant code optimizations.
Timer service interfaces are associated with specific periodic and aperiodic entry-point functions. Each interface specifies a data communication method and access function naming rule. To change the timer service interface that the code generator applies for a function, on the Functions tab, select the function and click the pencil icon. In the dialog box that appears, select a timer service interface.
Override Default Function Names
Another interface customization is overriding the default entry-point function name
defined by a function customization template in the configured Embedded Coder Dictionary.
For this example, the code generator uses function customization templates
PeriodicAperiodicInterface
and
InitTermInterface
by default, which apply naming rules that
produce function names CD_Aperiodic
, CD_Periodic
,
CD_initialize
, and CD_terminate
. Assume that the
target platform calls execution functions CD_integrator
and
CD_accumulator
. To align with the target environment code, in the
Function Names column, the example model code mapping specifies
function names CD_integrator
and CD_accumulator
.
These names override the default names CD_Aperiodic
and
CD_Periodic
.
Configure Alternative Service Interface
The code mappings for model ComponentDeploymentFcn
apply service
interfaces that use outside-execution data communication. Outside-execution data
communication occurs before and after function execution. The values of the input data
remain unchanged as the function executes. Each time the function accesses the data, the
code uses the same value.
Change the service interface configuration such that the code generator applies the during-execution data communication method for service requests. A best practice is to configure the data elements in a model to use service interfaces that use the same data communication method.
In the Embedded Coder app, open the Code Mappings editor.
Click the Functions tab.
Change the timer service interface associated with the aperiodic and periodic entry-point functions. For each function:
Select the row for the function.
Click the pencil icon.
In the dialog box that appears, set Timer Service to
get_tick_during
.
Change the receiver service interface for the inports. Click the Inports tab. For each inport, in the Receiver Service column, select
ReceiverDuringExe
.Change the sender service interface for the outports. Click the Outports tab. For each outport, in the Sender Service column, select
SenderDuringExe
.Change the data transfer service interfaces. Click the Data Transfers tab. For the data transfer, in the Data Transfers column, select
DataTransferDuringExe
.Save the model.
Generate and Inspect the Generated Code
Generate the code.
In the Code view, in the file
ComponentDeploymentFcn.c
, inspect the code generated for entry-point functionCD_accumulator
.void CD_accumulator(void) { real_T rtb_Sum[10]; real_T rtb_Sum_0; int32_T i; get_CD_accumulator_DataTransfer(rtb_Sum); for (i = 0; i < 10; i++) { rtb_Sum_0 = rtb_Sum[i] + CD_measured.delay[i]; rtb_Sum[i] = rtb_Sum_0; rtDwork.OutBus_y[i] = CD_tunable.k * rtb_Sum_0; CD_measured.delay[i] = rtb_Sum_0; } set_CD_accumulator_OutBus_y(&rtDwwork.OutBus_y[0]); }
The entry-point function calls the target platform data transfer service function
get_CD_accumulator_DataTransfer
to read the output value transferred from the integrator function. For each element of the bus signal, the function applies a delay and gain value and writes the output by calling target platform sender service functiongetref_CD_accumulator_OutBus_y
. The data communication method and function names that the code generator applies for the service calls are defined by the service interfaces selected in the Code Mappings editor. You changed the service interface mappings to interfaces that communicate data during function execution and apply data concurrency safeguards. Data is communicated to other functions immediately during function execution. Each time the function accesses the data, the code uses updated values. This method of data communication favors data freshness over memory usage.Observe differences in the generated code for other entry-point functions.
Next, configure a model parameter for tuning and state data for measurement during run time.