Contents

Stateflow Chart Considerations

hisf_0064: Shift operations for Stateflow data to improve MISRA-C:2004 compliance

ID: Titlehisf_0064: Shift operations for Stateflow® data to improve MISRA-C:2004 compliance
DescriptionTo improve MISRA-C:2004 compliance of the generated code with Stateflow bit-shifting operations, do not perform:
A

Right-shift operations greater than the bit-width of the input type, or by a negative value.

B

Left-shift operations greater than the bit-width of the output type, or by a negative value.

Note

If you follow this and other modeling guidelines, you increase the likelihood of generating code that complies with the MISRA-C:2004 standard.

RationaleA,BTo avoid shift operations in the generated code that might be a MISRA-C:2004 violation.
References
  • MISRA-C:2004 Rule 12.8

Prerequisiteshisl_0060: Configuration parameters that improve MISRA-C:2004 compliance
Last ChangedR2011a
ExampleIn the first equation, shifting 17 bits to the right pushes data stored in a 16–bit word out of range. The resulting output is zero. In the second equation, shifting the data 33 bits pushes data beyond the range of storage for a 32–bit word. Again, the resulting output is zero.

hisf_0065: Type cast operations in Stateflow to improve MISRA-C:2004 compliance

ID: Titlehisf_0065: Type cast operations in Stateflow to improve MISRA-C:2004 compliance
DescriptionTo improve MISRA-C:2004 compliance of the generated code, protect against Stateflow casting integer and fixed-point calculations to wider data types than the input data types by:
A

Explicitly type casting the calculations

B

Using the := notation in Stateflow

Note

If you follow this and other modeling guidelines, you increase the likelihood of generating code that complies with the MISRA-C:2004 standard.

RationaleA,BTo avoid shift operations in the generated code that might be a MISRA-C:2004 violation.
References
  • MISRA-C:2004 Rule 10.1

  • MISRA-C:2004 Rule 10.4

Prerequisiteshisl_0060: Configuration parameters that improve MISRA-C:2004 compliance
Last ChangedR2011a
ExampleThe example shows the default behavior and both methods of controlling the casting (explicitly type casting and using the colon operator).

hisf_0211: Protect against use of unary operators in Stateflow Charts to improve MISRA-C:2004 compliance

ID: Titlehisf_0211: Protect against use of unary operators in Stateflow Charts to improve MISRA-C:2004 compliance
DescriptionTo improve MISRA-C:2004 compliance of the generated code:
A

Do not use unary minus operators on unsigned data types

Note

The MATLAB® and C action languages do not restrict the use of unary minus operators on unsigned expressions.

RationaleAImprove MISRA-C:2004 compliance of the generated code.
References
  • MISRA-C:2004 Rule 12.9

Last ChangedR2014b
Example

Not Recommended:

Applying the unary minus operator to the unsigned integer results in a MISRA-C:2004 Rule 12.9 violation. The resulting output wraps around the maximum value of 256 (uint8). In this example, if the input variable In_SF_uint8 equals 7, then the output variable varOut_uint8 equals 256 – (7 * 3), or 235. The simulation and code generation values are in agreement.

hisf_0212: Data type of Stateflow for loop control variables to improve MISRA-C: 2004 compliance

ID: Titlehisf_0212: Data type of Stateflow for loop control variables to improve MISRA-C: 2004 compliance
DescriptionTo improve MISRA-C:2004 compliance of the generated code:
A

Explicitly select an integer data type as the control variable in a Stateflow for loop

Note

The default data type in Simulink® and Stateflow is double. Explicitly select an integer data type.

RationaleAImprove MISRA-C:2004 compliance of the generated code
References
  • MISRA-C:2004 Rule 13.4

Last ChangedR2011b

hisf_0213: Protect against divide-by-zero calculations in Stateflow charts to improve MISRA-C: 2004 compliance

ID: Titlehisf_0213: Protect against divide-by-zero calculations in Stateflow charts to improve MISRA-C: 2004 compliance
DescriptionTo improve MISRA-C:2004 compliance of the generated code for floating point and integer-based operations, do one of the following:
A

Perform static analysis of the model to prove that division by zero is not possible

B

Provide run-time error checking in the generated C code by explicitly modeling the error checking in Stateflow

C

Modify the code generation process using Code Replacement Libraries (CRLs) to protect against division by zero

D

For integer-based operations, in the Configuration Parameters dialog box, on the Optimization pane, clear Remove code that protects against division arithmetic exceptions

Note

Using run-time error checking introduces additional computational and memory overhead in the generated code. It is preferable to use static analysis tools to limit errors in the generated code. You can use Simulink Design Verifier™ or Polyspace® Code Prover™ to perform the static analysis.

If static analysis determines that sections of the code can have a division by zero, then add run-time protection into that section of the model (see example). Using a modified CRL or selecting the parameter Remove code that protects against division arithmetic exceptions protects division operations against divide-by-zero operations. However, this action does introduce additional computational and memory overhead.

Use only one of the run-time protections (B, C or D) in a model. Using more than one option can result in redundant protection operations.

RationaleA,B,
C,D
Improve MISRA-C:2004 compliance of the generated code

References
  • MISRA-C:2004 Rule 21.1

See Also
Last ChangedR2011b
ExampleRun-time divide by zero protection can be realized using a graphical function. Unique functions should be provided for each data type.

Was this topic helpful?