Creating Simulink S-functions by using Rhapsody

Rhapsody® can be used to create Simulink S-functions that can then be plugged into Simulink models.

About this task

This feature is applicable in Rhapsody Developer for C and C++.

Procedure

  1. Create a Rhapsody project.
  2. Import the Simulink profile into your project.
  3. Set the newly created configuration to be the active configuration.
  4. Create a class and apply the S-FunctionBlock stereotype to it.
  5. Add incoming flowports to the class to represent incoming data.
  6. Add outgoing flowports to the class to represent outgoing data.
  7. For each of the flowports you added, add an attribute to the class to represent the flowport. The attribute must have the same name and be of the same type as the corresponding flowport.
  8. Implement a statechart for the class.
  9. Generate code for the configuration you created.
  10. The output directory for the configuration to include the following items:
    • generated source files for the model
    • a Simulink C, or C++, template file called RhapsSFunc_the_name_you_gave_to_the_block.c (for example, RhapsSFunc_BlockSFA.c)
    • a build_sfunction.m
    • a create_sfunction_model.m
  11. Open MATLAB and go to the output directory containing the Rhapsody code.
  12. Run build_sfunction.m.

Results

Output of S-function creation

When you generate code for an S-FunctionConfig configuration, Rhapsody performs the following actions:

  • Completes the sfuntmpl_basic.c template provided by Simulink, and renames it to reflect the name that you assigned to your S-function block.
  • Takes the information you have entered for the S-function block in your project and creates a corresponding Simulink model file, using the name that you assigned to your S-function block.
  • Generates a Simulink model containing the S-Function block, and builds the S-Function using the MEX compiler.

Timing and S-functions

For time-related events, Rhapsody uses the timing mechanism of the target operating system. Since Simulink has its own timing mechanism, Rhapsody takes this into account when generating the S-function code.