The Motor Industry Software Reliability Association (MISRA®)[1] has established “Guidelines for the Use of the C Language in Critical Systems” (MISRA C®). When you generate code from MATLAB® code, if you have Embedded Coder®, you can configure certain code generation parameters to increase the likelihood of generating C and C++ code that comply with the MISRA C:2012 and MISRA C++:2008 guidelines.
The following describes the separate command line and app workflows.
Create an Embedded Coder code generation configuration object.
cfg = coder.config('lib', 'ecoder', true);
Set the properties listed in the table to the values for improved MISRA compliance.
Property | Value for Improved MISRA Compliance |
---|---|
CastingMode | 'Standards' |
CppNamespace | Any valid namespace name for C++ |
DataTypeReplacement | 'CoderTypedefs' |
DynamicMemoryAllocation | 'Off' |
EnableRuntimeRecursion | false |
EnableSignedLeftShifts | false |
EnableSignedRightShifts | false |
GenerateDefaultInSwitch | true |
ParenthesesLevel | 'Maximum' |
TargetLangStandard
| 'C99 (ISO)' for C, 'C++03 (ISO)'
for C++ |
To set these properties in one step, use coder.setupMISRAConfig
.
coder.setupMISRAConfig(cfg);
Alternatively, set properties individually. For example:
cfg.CastingMode = 'Standards';
If the CppNamespace
property is unset, and the
TargetLang
property is 'C++'
, then
coder.setupMISRAConfig
sets the CppNamespace
property to a default character vector, 'Codegen'
. Modify this
value to a namespace name that is particular to your project.
Pass the configuration object to codegen
by using the
-config
option.
codegen myFunction -config cfg ...
On the Generate Code page, set Build
type to Source Code
, Static
Library
, Dynamic Library
, or
Executable
.
Click More Settings.
Click MISRA Compliance.
The app displays the settings that might impact MISRA compliance.
To set all of these settings to the recommended values, click Set to Recommended Values.
To modify an individual setting, click All Settings, and then search for the setting.
By default, the C++ code that you generate uses a custom type definition for
bool
to maximize compatibility with certain precompiled libraries. This
custom type definition can cause violations for MISRA C++:2008 Rule 5-0-13 and Rule 5-3-1.
To use the built-in bool
type in generated C++ code, and avoid these
violations, change the Hardware Board setting in the configuration
object from MATLAB Host Computer
. To see how to change this setting in the App, see
Access Hardware Implementation Properties with a Dialog Box (MATLAB Coder). For example, for
code generation and build on a 64-bit Windows® platform, change the Hardware Board to None
- Select device below
, and then set Device vendor to
Intel
and Device type to
x86-64 (Windows64)
.
coder.EmbeddedCodeConfig
| coder.setupMISRAConfig
[1] MISRA and MISRA C are registered trademarks of HORIBA MIRA Ltd., held on behalf of the MISRA Consortium.