Simulink provides a mechanism to integrate existing or custom C code with a model, usually with little change to the C code itself. It may be beneficial to mix Simulink and C code for a number of reasons. For instance:
when there is an existing body of C code which would take a significant amount of effort to re-create as a Simulink model;
when an existing algorithm written in C has been tested and proven to work correctly — the code is at a mature stage and re-creating the code in Simulink means additional testing is required;
when an algorithm written in C code might be more efficient and easier to understand than the equivalent created using Simulink blocks.
Existing or custom C code can be incorporated into a Simulink model using S-functions. S-functions can be written in MATLAB, C, C++, Ada, or Fortran and except for the MATLAB type function, the rest are compiled as a MEX-function using the MATLAB mex utility. An S-function can be used with Simulink Coder and the code generated by Simulink Coder for S-functions can be customized by writing a Target Language Compiler (TLC) file.
This section explains how to write and integrate a C type S-function into an OpenECU Simulink model (OpenECU does not support C++, Ada or Fortran S-functions, or non-inlined S-functions). S-functions with TLC files, are called inlined S-functions, while S-functions without TLC files are non-inlined. For OpenECU models, a TLC file is required, which improves the efficiency of code generation and helps reduce RAM usage.
See “S-Functions and Code Generation” in MATLAB's help section for more information.