Skip to content

FelipeMdeO/BareMetalFW_Arch_Template

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

1 Commit
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Layered Software Architecture - Example Project

This is a demonstrative project of software architecture based on 4 independent layers.

πŸ“ Layer Structure (4 Layers)

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚          APP LAYER                      β”‚
β”‚   (Application / Orchestration)         β”‚
β”‚   - Main logic                          β”‚
β”‚   - Task coordination                   β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
              ↓ can ONLY call
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚        CONTROL LAYER                    β”‚
β”‚   (Control / Business Logic)            β”‚
β”‚   - Algorithms                          β”‚
β”‚   - State machines                      β”‚
β”‚   - Data processing                     β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
         ↓ can call          ↓
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   DRIVER LAYER     β”‚  β”‚ DRIVER LAYER       β”‚
β”‚  (Sensors)         β”‚  β”‚ (Actuators)        β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
         ↓                       ↓
         β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
              ↓ can call
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚      MCU-PERIPHERALS LAYER              β”‚
β”‚   (Microcontroller HAL)                 β”‚
β”‚   - GPIO, ADC, UART, SPI, I2C           β”‚
β”‚   - Vendor libs (emlib, HAL)            β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

πŸ”’ Inter-Layer Access Rules

βœ… Allowed:

  • APP can call CONTROL only
  • CONTROL can call DRIVER and MCU-PERIPHERALS
  • DRIVER can call MCU-PERIPHERALS only

❌ Forbidden:

  • APP CANNOT call DRIVER or MCU-PERIPHERALS directly
  • DRIVER CANNOT call CONTROL or APP
  • MCU-PERIPHERALS CANNOT call anything (base layer)
  • CONTROL CANNOT call APP

πŸ“¦ Layer Responsibilities

APP LAYER (Application)

  • System initialization
  • Main application loop
  • Coordination between CONTROL modules
  • Global state management
  • User interface (if applicable)
  • Never accesses hardware directly

CONTROL LAYER (Control)

  • Control algorithm implementation
  • Business logic
  • Sensor data processing
  • State machines
  • Validations and business rules
  • Bridge between APP and DRIVER

DRIVER LAYER (Device Drivers)

  • External sensor drivers (temperature, pressure, humidity via I2C)
  • Logical actuator drivers (LED, fan, heater, alarm)
  • External module drivers (displays, SD cards, etc)
  • High-level device interface
  • Maps logical concepts (ACTUATOR_FAN) to physical hardware (PORT_B, PIN_0)
  • Uses MCU-PERIPHERALS to access hardware
  • See: docs/actuator_driver_rationale.md - Explains why actuator_driver exists in DRIVER

MCU-PERIPHERALS LAYER (MCU HAL)

  • Lowest layer - microcontroller hardware abstraction
  • Implementation/wrapper of vendor libraries
  • GPIO, ADC, UART, SPI, I2C, PWM, Timers, etc
  • Direct access to MCU registers

πŸ”§ Architecture Advantages

  • ✨ Modularity: Each layer has well-defined responsibility
  • πŸ”„ Reusability: Drivers can be reused in other projects
  • πŸ§ͺ Testability: Layers can be tested independently
  • πŸ“ Maintainability: Easy to locate and fix issues
  • πŸ”Œ Portability: Easy to change hardware - MCU changes affect only MCU-PERIPHERALS layer, external devices affect only DRIVER layer

πŸ“‚ File Structure

architecture-example/
β”œβ”€β”€ .clang-format                # Code formatting configuration (Barr C Standard)
β”œβ”€β”€ Makefile                     # Build system with validation
β”œβ”€β”€ README.md                    # Project documentation
β”œβ”€β”€ template.c                   # Template for .c files
β”œβ”€β”€ template.h                   # Template for .h files
β”œβ”€β”€ docs/
β”‚   β”œβ”€β”€ 4-layer-architecture.md  # Architecture documentation
β”‚   β”œβ”€β”€ architecture_diagram.md  # Visual diagrams
β”‚   └── layer_rules.md          # Layer rules reference
β”œβ”€β”€ build/                       # Build artifacts (auto-generated)
β”‚   β”œβ”€β”€ obj/                    # Object files
β”‚   └── architecture-example.elf # Executable
└── src/
    β”œβ”€β”€ app/                     # APPLICATION LAYER
    β”‚   β”œβ”€β”€ inc/
    β”‚   β”‚   └── app.h           # APP layer public interface
    β”‚   └── src/
    β”‚       β”œβ”€β”€ main.c          # Entry point
    β”‚       β”œβ”€β”€ app_manager.c   # Application coordinator
    β”‚       β”œβ”€β”€ app_cooling.c   # Cooling management module
    β”‚       β”œβ”€β”€ app_hmi.c       # HMI (buttons/LEDs) module
    β”‚       └── app_msg.c       # Message/communication module
    β”œβ”€β”€ control/                 # CONTROL LAYER
    β”‚   β”œβ”€β”€ inc/
    β”‚   β”‚   └── control.h       # CONTROL layer public interface
    β”‚   └── src/
    β”‚       β”œβ”€β”€ board_init.c    # Board initialization
    β”‚       β”œβ”€β”€ temperature_control.c  # Temperature control algorithm
    β”‚       β”œβ”€β”€ alarm_control.c        # Alarm management
    β”‚       β”œβ”€β”€ actuator_control.c     # Actuator control abstraction
    β”‚       └── hmi_control.c          # HMI control abstraction
    β”œβ”€β”€ driver/                  # DRIVER LAYER
    β”‚   β”œβ”€β”€ inc/
    β”‚   β”‚   └── driver.h        # DRIVER layer public interface
    β”‚   └── src/
    β”‚       β”œβ”€β”€ temperature_sensor_driver.c  # LM75 I2C sensor
    β”‚       β”œβ”€β”€ humidity_sensor_driver.c     # SHT31 I2C sensor
    β”‚       β”œβ”€β”€ pressure_sensor_driver.c     # BMP280 I2C sensor
    β”‚       β”œβ”€β”€ actuator_driver.c            # LED, fan, heater drivers
    β”‚       β”œβ”€β”€ button_driver.c              # Button event driver (interrupt-based)
    β”‚       └── uart_driver.c                # UART communication
    └── mcu-peripherals/         # MCU-PERIPHERALS LAYER (HAL)
        β”œβ”€β”€ inc/
        β”‚   └── mcu_hal.h       # MCU HAL public interface
        └── src/
            β”œβ”€β”€ mcu_peripheral_init.c     # Peripheral initialization
            β”œβ”€β”€ mcu_peripheral_internal.h # Internal HAL declarations
            β”œβ”€β”€ gpio.c          # GPIO HAL implementation
            β”œβ”€β”€ adc.c           # ADC HAL implementation
            β”œβ”€β”€ uart.c          # UART HAL implementation
            └── i2c.c           # I2C HAL implementation

πŸ’‘ Important Notes

  • This is an example/demonstrative project
  • The code is simplified for learn purposes
  • The architecture can be adapted according to project needs

πŸ”¨ Build Commands (Makefile)

This project uses make to automate compilation and validation. Available commands:

make build

Compiles the entire project and generates the executable.

make build
  • Compiles all .c files from the 4 layers
  • Generates object files in build/obj/
  • Links and creates the executable build/architecture-example.elf

make clean

Removes all compiled files.

make clean
  • Deletes the entire build/ folder
  • Useful for doing a clean build from scratch

make validate

Validates if architecture rules are being respected.

make validate

Checks:

  • βœ… APP does not include DRIVER or MCU-PERIPHERALS directly
  • βœ… CONTROL does not include APP
  • βœ… DRIVER does not include CONTROL or APP
  • βœ… MCU-PERIPHERALS does not include any upper layer

make format

Auto-formats all source files according to Barr C Coding Standard.

make format
  • Applies consistent code formatting across all .c and .h files
  • Uses clang-format with modified Barr C Coding Standard
  • Ensures 2-space indentation, 80 character line limit

make info

Shows project information and dependencies.

make info

Displays:

  • List of source files for each layer
  • Allowed dependencies between layers
  • Project structure overview

make help

Shows help message with all available commands.

make help

Quick Build and Test

# Clean, compile and validate everything
make clean build validate

# Build and validate architecture
make build validate

# Format code and build
make format build

# Full workflow: format, clean, build, validate
make format clean build validate

Running the Program

# Run in simulation mode
./build/architecture-example.elf

# Run for a limited time (3 seconds)
timeout 3 ./build/architecture-example.elf

The program runs in simulation mode (-DSIMULATION) printing messages about hardware operations.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors