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:
- URLs, filepaths, etc.
- Long constants not containing whitespace that would be inconvenient to split.
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.
Toolchain / IDE
=STM32CubeIDE
setting takes supremacy at all times unless specifically stated.
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 Directory Names
Use a single word, all lowercase characters.
3.5 Module Names
Use snake_case.
3.6 Variable Names
Use snake_case.