# MISRA C:2012 check and deviation list of the generated code

`codegen.misra` generates the control code as `tools.codeGeneration` does (same params) and runs cppcheck's MISRA C:2012
addon on it, when the machine has cppcheck 2.x (`CPPCHECK` names another binary). It reports each finding by rule
number, file and line, and marks it against the list below. MISRA's rule texts belong to the standard, so neither
Voltecx nor cppcheck prints them: read the rule in your licensed copy of MISRA C:2012.

- `status: "passed"`: no finding outside the deviations below and the unresolved vendor calls.
- `status: "failed"`: `violations` findings are not deviated. They are the generator's to fix, or yours to deviate in
  your own project with a reason.
- `status: "skipped"`: cppcheck or its MISRA addon is not on this machine, or could not run. This is never a pass.
- `unresolved`: a rule 17.3 finding on a call into the STM32Cube LL library (`LL_...`). The LL headers are not part of
  the check; your build, with them, settles it.

This is a check by an open-source tool, not a certification: cppcheck's addon covers part of the rule set, and a
finding it does not report is not proof of compliance.

## Deviations

Each deviation covers one rule, in the named files, on lines that hold the named construct (`{prefix}` is the code's
`prefix`, `{PREFIX}` its upper case). Any other finding of the same rule is still reported.

| ID | Rule | Scope | Reason |
|---|---|---|---|
| VX-MISRA-D1 | 2.5 | `#define {PREFIX}_...` in the generated headers | The headers publish the sample time, the PWM frequency, the clocks and the scales for the integrator's code; the generated code need not use each one itself. |
| VX-MISRA-D2 | 8.7 | `{prefix}_reset` and `{prefix}_stm32g474_adc_isr` | They are the code's interface: the integrator's code calls the reset on a restart and the interrupt entry from its vector, which a check of the generated files alone cannot see. |
| VX-MISRA-D3 | 8.4 | interrupt handlers named by the vendor's vector table (`..._IRQHandler`) | The vendor's startup file declares the handler in its vector table, outside the generated files. |
| VX-MISRA-D4 | 11.4 | the XMC4200 register macros `{PREFIX}_SCU`, `_CCU80`, `_CC8` and `_NVIC_...` | The XMC4200 target writes its peripheral registers at their fixed addresses (XMC4200 reference manual) without the vendor's headers, so the address is an integer cast to a volatile pointer. |
| VX-MISRA-D5 | 21.15 | the copy between a float and its 32-bit word in `{prefix}_tune.c` and `{prefix}_log.c` | The tuning and serial-log frames carry a value as its IEEE 754 binary32 bits. Copying the object's bytes with memcpy is how C99 reads and writes that representation without breaking the aliasing rules; the float and the word have the same size. |

## Findings not deviated (2026-11-03, cppcheck 2.13.0, `buckClosedLoop`)

In float, with `fullScale` 20 on the MCU targets, `buckClosedLoop` has no finding that is not deviated on the
portable, stm32g474, f28379d and cortexm targets: the generator writes explicit parentheses (12.1), one exit in the
duty-to-compare helpers and the STM32G474 interrupt entry (15.5), the interrupts' inputs and outputs as their own
statics (8.9), no cast of a composite expression (10.8) and `(void)memcpy` (17.7) (#3780).

In fixed point (`q15`, which is `q4.11` here, and `q31`) the portable, stm32g474 and f28379d targets have no
finding that is not deviated either (before: 9 of 10.1 and 5 of 12.1 on portable `q15`, 3 more on each MCU
target). The format's macros carry their values (`((int64_t)2048)  /* 2^11 */`) instead of shifting a signed `1`
(10.1); the saturating helpers parenthesize every condition (12.1) and keep their result in a local for one exit
(15.5); and the targets' ADC conversion to the format rounds into a local before its cast (15.5, 10.8). The
cortexm and xmc4200 targets run float only.

With the supervisor (soft start of `Ref`, UVLO and OVP, latched; float, `fullScale` 20) the portable, stm32g474 and
f28379d targets have no finding that is not deviated either (before: 7, 12 and 10). Every function of
`{prefix}_supervisor.c` and the supervised STM32G474 step has one exit, at its end (15.5): the PWM-off branch and
the control law are an `if` and its `else`. Every condition is parenthesized (12.1); the supervisor's inputs are the
step's own static (8.9); and the targets' enable functions store 1 or 0 in an `if` instead of a conditional
expression (10.6) (#3780).

With run-time tuning (`tune: true`; float, double and `q7.24`), `{prefix}_tune.c` and `{prefix}_tune.h` have no
finding that is not deviated (before: 22 in the `.c` file, 24 in fixed point, and 4 of 2.5 in the header). Every
function has one exit, at its end (15.5); every condition is parenthesized and the CRC step is an `if` (12.1);
`memcpy` and `memmove` are `(void)` (17.7); the queue's length is updated before the move, so no composite
expression is cast (10.8); the fixed-point saturation's `else if` ends in an `else` (15.7). Without
`-D{PREFIX}_TUNE` the four functions are real functions that do nothing instead of empty macros in their names, which
removes 5.5, 2.5 and the unused typedef (2.3). The copy between the float and its frame word is VX-MISRA-D5 (#3780).

With the serial log (`log`; float and `q7.24`), `{prefix}_log.c` and `{prefix}_log.h` have no finding that is not
deviated (before: 11 in the `.c` file and 5 of 2.5 on the header's macros). `{prefix}_log_value` drops a frame that
does not fit in an `if` and queues it in its `else`, so it has one exit (15.5); the CRC step is an `if` and every
condition is parenthesized (12.1); `memcpy` is `(void)` (17.7). Without `-D{PREFIX}_LOG` the five functions are real
functions that do nothing instead of empty macros in their names, which removes 5.5, 2.5 and the unused typedef
(2.3). The copy between the float and its frame word is VX-MISRA-D5 (#3780).

Still open in the generator (#3780), not deviated:

- xmc4200: 15.5 on the init's early return when `{prefix}_port_init` fails (and in the VADC calibration wait).
- The F28379D modules (5.5 on the SCI names), measured 2026-11-04 but not fixed yet.
- The supervisor on xmc4200 and cortexm is not measured yet.
