danielljeon.github.io

C Code (STM32 Target) Conventions


Table of Contents

1 Background

Writing code for STM32 targets can be difficult and time-consuming. The following conventions are designed to work with common development environments and toolchains:


2 Language Rules

2.1 Lint

Use Clang-Tidy.

2.1.1 Pros

Built into most IDEs including JetBrains. Good simple error catching, typos, etc.

2.1.2 Cons

Imperfect and slight variations.


3 Style Rules

3.1 Line Length

Max line length = 80.

3.1.1 Exceptions

Exceptions to the limit are:

3.1.2 Pros

Matches most code generation tools, fits A4 print papers and typical maximum in industry.

3.1.3 Cons

None, mostly subjective.

3.3 File Structure

Use the file structure generated by code generation tools targeted for ST's official STM32CubeIDE Toolchain. Always write within the "user code" sections specified by the comments in generated code.

3.3.1 Pros

Ensures CubeMX compatability, allowing for convent initialization, HAL setup and code generation. Also is the official supported format.

3.3.2 Cons

Can be restrictive in certain scenarios when dependent on HAL.

3.4 Naming Convention

Note: All STM32CubeMX code generated is kept as is. The following is regarding developer code.

3.4.1 Directory Names

Use a word, all lowercase characters.

3.4.2 Module and File Names

Use snake_case.

3.4.3 Variable Names

Use snake_case.

3.4.3.1 Constants

Use UPPER_SNAKE_CASE.

3.4.4 Macro and Define Names

Use UPPER_SNAKE_CASE.

3.4.5 typedef Struct Names

Use snake_case.

3.4.5.1 Enum Members

Use UPPER_SNAKE_CASE.

3.4.6 Function Names

Use snake_case.