This example shows how to specify whether to generate default cases for switch-case statements in the code for Stateflow® charts. Generated code that does not contain default cases conserves ROM consumption and enables better code coverage because every branch in the generated code is falsifiable.
Some coding standards, such as MISRA, require the default case for switch-case statements. If you want to increase your chances of producing MISRA C compliant code, generate default cases for unreachable Stateflow switch statements.
Figures 1, 2, and 3 show relevant portions of the sldemo_fuelsys
model, a closed-loop system containing a plant and controller. The Air-fuel rate controller
logic is a Stateflow chart that specifies the different operation modes.
Figure 1: Top-level model of the plant and controller
Figure 2: Fuel rate controller subsystem
Figure 3: Fuel rate controller logic
In the MATLAB Command Window, to open sldemo_fuelsys
via
rtwdemo_fuelsys
enter:
rtwdemo_fuelsys
Open the Model Configuration parameters dialog box. On the Code Generation > Code Style tab, clear the Suppress generation of default cases for Stateflow statements if unreachable parameter.
In the MATLAB Command Window, to build the model, enter:
rtwbuild('sldemo_fuelsys/fuel_rate_control');
For the different operation modes, the fuel_rate_control.c
file
contains default cases for unreachable switch statements. For example, for the Shutdown
operation mode, the generated code contains this default statement:
default: /* Unreachable state, for coverage only */ rtDWork.bitsForTID0.is_Fuel_Disabled = IN_NO_ACTIVE_CHILD; break;
For the Warmup operation mode, the generated code contains this default statement:
default: /* Unreachable state, for coverage only */ rtDWork.bitsForTID0.is_Low_Emissions = IN_NO_ACTIVE_CHILD; break;
Open the Configuration Parameters dialog box. On the Code Generation > Code Style tab, select the Suppress generation of default cases for Stateflow statements if unreachable parameter.
Build the model.
Read through the fuel_rate_control.c
file. The default cases for
unreachable switch statements are not in the generated code.