Home Learning Paths ECU Lab Assessments Interview Preparation Arena Pricing Log In Sign Up

MISRA-C in Embedded Coder Generated Code

MISRA-C:2012 Compliance Goal

Embedded Coder is designed to generate MISRA-C:2012 compliant code when the model is configured correctly. Most MISRA violations in generated code trace back to one of three model issues:

  • Untyped data: signals or parameters without explicit types generate casts or promotions that violate MISRA Rule 10.x (essential type model)
  • Continuous-time blocks: integrators, derivatives using double by default violate Rule 10.1 (inappropriate essential operands)
  • Wrap overflow mode: integer wrap generates code that violates Rule 12.1 (signed integer overflow)

The MISRA-C checker in Embedded Coder Code Generation Report highlights violations with traceable links back to the model block that caused them.

Common MISRA Violations and Model Fixes

MATLABmisra_fixes.m
% MISRA Rule 10.1: Inappropriate essential operands
% Cause: Block with "Inherit" data type receiving int + double
% Fix: Set explicit type on all Gain/Add blocks
set_param("MyModel/PID/P_Gain", "OutDataTypeStr", "single");
set_param("MyModel/PID/I_Sum",  "OutDataTypeStr", "single");

% MISRA Rule 12.1: Operator precedence unclear
% Cause: Complex arithmetic expressions in MATLAB Function blocks
% Fix: Add explicit parentheses in MATLAB code
% Bad:  y = a + b * c;    (ambiguous to reviewer)
% Good: y = a + (b * c);  (explicit precedence)

% MISRA Rule 14.4: Non-boolean condition in if
% Cause: Stateflow condition using integer comparison as boolean
% Fix: Use explicit boolean expression
% Bad:  [speed]           (int used as bool)
% Good: [speed > 0.0f]    (explicit comparison)

% MISRA Rule 17.7: Return value discarded
% Cause: MATLAB Function block calling function without using return
% Fix: Assign return value: result = myFunc(); % even if unused

% Run MISRA check in code gen report:
set_param(model, "MisraC3Version", "MISRA C:2012");
set_param(model, "GenerateMisraReport", "on");
slbuild(model);
% Open: <model>_ert_rtw/html/misra_report.html

Integration with Static Analysis Tools

ToolMISRA CoverageIntegration Method
Polyspace Bug FinderMISRA C:2012 full rule setEmbedded Coder: Analysis > Polyspace > Run Bug Finder
Polyspace Code ProverMISRA + runtime error proof (overflow, div-by-zero)Embedded Coder: Analysis > Polyspace > Run Code Prover
LDRA TBvisionMISRA C:2012 + custom rulesImport generated .c/.h files; run LDRA analysis
PC-lint PlusMISRA C:2012 warningsAdd to makefile: lint generated files as part of build
Embedded Coder built-inSubset of MISRA C:2012 (key rules)Enable in Config Params > Code Generation > MISRA

Summary

Generated code MISRA compliance is almost entirely a model quality issue, not a code generation issue. Embedded Coder is designed to generate MISRA-compliant code from a well-typed, well-structured model. When MISRA violations appear, they are signals pointing to specific model problems: an untyped signal generating a prohibited cast, an overflow wrap mode generating unchecked integer arithmetic, or a MATLAB Function block with implicit type promotions. The workflow is to use the Code Generation Report's traceable links to jump from the MISRA violation in the C code directly to the Simulink block that caused it, fix the model, regenerate, and verify the violation is gone - not to manually edit the generated C code to suppress the violation.

🔬 Deep Dive — Core Concepts Expanded

This section builds on the foundational concepts covered above with additional technical depth, edge cases, and configuration nuances that separate competent engineers from experts. When working on production ECU projects, the details covered here are the ones most commonly responsible for integration delays and late-phase defects.

Key principles to reinforce:

  • Configuration over coding: In AUTOSAR and automotive middleware environments, correctness is largely determined by ARXML configuration, not application code. A correctly implemented algorithm can produce wrong results due to a single misconfigured parameter.
  • Traceability as a first-class concern: Every configuration decision should be traceable to a requirement, safety goal, or architecture decision. Undocumented configuration choices are a common source of regression defects when ECUs are updated.
  • Cross-module dependencies: In tightly integrated automotive software stacks, changing one module's configuration often requires corresponding updates in dependent modules. Always perform a dependency impact analysis before submitting configuration changes.

🏭 How This Topic Appears in Production Projects

  • Project integration phase: The concepts covered in this lesson are most commonly encountered during ECU integration testing — when multiple software components from different teams are combined for the first time. Issues that were invisible in unit tests frequently surface at this stage.
  • Supplier/OEM interface: This is a topic that frequently appears in technical discussions between Tier-1 ECU suppliers and OEM system integrators. Engineers who can speak fluently about these details earn credibility and are often brought into critical design review meetings.
  • Automotive tool ecosystem: Vector CANoe/CANalyzer, dSPACE tools, and ETAS INCA are the standard tools used to validate and measure the correct behaviour of the systems described in this lesson. Familiarity with these tools alongside the conceptual knowledge dramatically accelerates debugging in real projects.

⚠️ Common Mistakes and How to Avoid Them

  1. Assuming default configuration is correct: Automotive software tools ship with default configurations that are designed to compile and link, not to meet project-specific requirements. Every configuration parameter needs to be consciously set. 'It compiled' is not the same as 'it is correctly configured'.
  2. Skipping documentation of configuration rationale: In a 3-year ECU project with team turnover, undocumented configuration choices become tribal knowledge that disappears when engineers leave. Document why a parameter is set to a specific value, not just what it is set to.
  3. Testing only the happy path: Automotive ECUs must behave correctly under fault conditions, voltage variations, and communication errors. Always test the error handling paths as rigorously as the nominal operation. Many production escapes originate in untested error branches.
  4. Version mismatches between teams: In a multi-team project, the BSW team, SWC team, and system integration team may use different versions of the same ARXML file. Version management of all ARXML files in a shared repository is mandatory, not optional.

📊 Industry Note

Engineers who master both the theoretical concepts and the practical toolchain skills covered in this course are among the most sought-after professionals in the automotive software industry. The combination of AUTOSAR standards knowledge, safety engineering understanding, and hands-on configuration experience commands premium salaries at OEMs and Tier-1 suppliers globally.

← PreviousAUTOSAR Code Generation: ARXML and CodeNext →Custom Code Integration: Legacy Code Tool