Firmware Development7 min read

How to Structure Maintainable Firmware Projects

Md Shahed Alam
Md Shahed Alam
March 8, 2025
How to Structure Maintainable Firmware Projects

Firmware projects that start as one file with everything in main.c become nightmares fast. Here is how to structure them so they stay manageable as they grow.

The layered architecture

Think in layers. Each layer only talks to the layer below it. This prevents dependencies from spreading everywhere.

  • Application layer: Your business logic. State machines, algorithms, application behavior.
  • Middleware layer: Protocol implementations, RTOS wrappers, storage managers.
  • HAL (Hardware Abstraction Layer): Abstracts the specific hardware. A uart_send() function that works the same regardless of whether you are on STM32 or ESP32.
  • Drivers: Direct hardware register access. One driver file per peripheral or chip.

Directory structure

project/
├── App/
│   ├── main.c
│   ├── state_machine.c
│   └── config.h
├── Drivers/
│   ├── uart/
│   ├── spi/
│   └── gpio/
├── Middleware/
│   ├── FreeRTOS/
│   └── lwIP/
├── BSP/            (Board Support Package)
└── Core/           (Startup, linker scripts)

Header file discipline

Every .c file gets a matching .h file. The header declares the public interface. The .c file contains the implementation and private functions (declared static). Use include guards on every header:

#ifndef UART_H
#define UART_H

void uart_init(uint32_t baud_rate);
void uart_send_byte(uint8_t byte);
uint8_t uart_receive_byte(void);

#endif // UART_H

Avoid global variables

Global variables make code hard to reason about and test. Use module-level static variables instead. Pass state through function parameters or use well-defined handles.

Use a build system

CMake or Makefiles manage your build. For STM32 projects, STM32CubeIDE or PlatformIO handle this for you. For custom projects, learning CMake is time well spent — it handles cross-compilation, include paths, and linker settings cleanly.

A well-structured firmware project is easier to understand, easier to test, and easier to port to a different hardware platform when needed.

Firmware Development

Ready to build something great?

Let's talk about your project. We will give you honest advice, a clear plan, and a fair price. No pressure, no sales pitch.