Skip to content

m3y54m/avr-dev-container

Repository files navigation

AVR DevContainer Project

Professional containerized AVR development environment with comprehensive build system and testing framework.

Features

  • 🐳 DevContainer: Fully containerized development environment
  • πŸ—οΈ Dual Build System: CMake for firmware + Ceedling for testing
  • πŸ§ͺ Unit Testing: CMock/Unity framework with hardware abstraction mocking
  • πŸ“Š Code Coverage: LLVM-based coverage analysis with detailed reporting
  • πŸ’‘ IntelliSense: Advanced code navigation with clangd configuration
  • πŸ”Œ USB Programming: Direct AVR device programming from container
  • πŸ“ Professional Structure: Clean separation of firmware and test code

Prerequisites

Setup

1. USB Device Setup (One-time)

# Install usbipd-win
winget install usbipd

# Share your AVR programmer (as Administrator)
usbipd list
usbipd bind --busid <BUSID>

2. Before Each Session

# Attach USB device to WSL
usbipd attach --wsl --busid <BUSID>

3. Open Project

  1. Open project in Cursor
  2. Click "Reopen in Container" when prompted
  3. Container builds automatically

Project Structure

avr-dev-container/
β”œβ”€β”€ firmware/                   # Firmware source code
β”‚   β”œβ”€β”€ src/
β”‚   β”‚   β”œβ”€β”€ main.c             # Application entry point
β”‚   β”‚   β”œβ”€β”€ led.c              # LED control module
β”‚   β”‚   └── hardware_interface.c # Hardware abstraction layer
β”‚   └── include/
β”‚       β”œβ”€β”€ led.h              # LED module interface
β”‚       └── hardware_interface.h # Hardware abstraction interface
β”œβ”€β”€ test/                       # Unit testing
β”‚   β”œβ”€β”€ unit/
β”‚   β”‚   └── test_led.c         # LED module unit tests
β”‚   └── support/
β”‚       └── avr_io.h           # AVR header mocks
β”œβ”€β”€ toolchain/                  # CMake toolchain configurations
β”‚   β”œβ”€β”€ avr.cmake              # AVR-GCC toolchain setup
β”‚   └── test.cmake             # Host compiler toolchain for testing
β”œβ”€β”€ .devcontainer/              # DevContainer setup
β”‚   β”œβ”€β”€ devcontainer.json      # Container configuration
β”‚   β”œβ”€β”€ Dockerfile             # Container image definition
β”‚   └── setup-git.sh           # Git configuration script
β”œβ”€β”€ .github/                    # GitHub Actions CI/CD
β”‚   └── workflows/
β”‚       └── ci.yml             # Continuous integration pipeline
β”œβ”€β”€ build/                      # AVR firmware build artifacts (generated)
β”œβ”€β”€ build-test/                 # Test build artifacts (generated)
β”‚   └── mocks/                 # Auto-generated CMock files
β”œβ”€β”€ CMakeLists.txt             # Main build configuration
β”œβ”€β”€ CMakePresets.json          # Build presets for AVR and testing
β”œβ”€β”€ project.yml                # Ceedling test configuration
β”œβ”€β”€ LICENSE                    # Project license
└── README.md                  # Project documentation

Usage

Build Firmware

cmake --preset avr
cmake --build build

Program Device

cmake --build build --target firmware-upload

Unit Tests

# Run all tests
ceedling test:all

# Run specific test
ceedling test:test_led

# Clean test artifacts
ceedling clean

Code Coverage Analysis

# Generate coverage report
ceedling gcov:all

# Display coverage summary (custom script)
./scripts/coverage_summary.sh

# Clean coverage files
./scripts/clean_coverage.sh

# View detailed coverage data
find build-test/gcov/out/test_led -name "*.gcov" -exec cat {} \;

Coverage File Organization:

  • Coverage Data: build-test/gcov/out/test_led/
  • Generated Reports: build-test/artifacts/gcov/
  • Cleanup Script: scripts/clean_coverage.sh
  • Summary Script: scripts/coverage_summary.sh

Current Coverage Results:

  • Source Code: 100% line coverage (15/15 lines)
  • Test Code: 100% line coverage (34/34 lines)

Clean Build

# Clean AVR build artifacts
cmake --build build --target clean

# Clean everything (custom target)
cmake --build build --target clean-all

Build System Features

AVR Firmware Build

  • Toolchain: AVR-GCC with optimized settings for ATmega328p
  • Output Formats: ELF, Intel HEX, and binary files
  • Memory Analysis: Automatic flash/RAM usage reporting

Unit Testing

  • Framework: Unity + CMock for comprehensive testing
  • Mocking: Hardware abstraction layer automatically mocked
  • Mock Prefix: mock_ functions (e.g., mock_hardware_interface_Init())
  • Test Structure: Clean separation of test code and fixtures
  • Build Integration: Seamless CMake and Ceedling integration

Configuration

MCU and Frequency Settings

Override default settings with environment variables:

export AVR_MCU=atmega328p
export F_CPU=16000000UL
cmake --preset avr

Build Presets

  • avr: Release build for AVR firmware (optimized)
  • test: Debug build for unit testing with Ceedling

Test Configuration

  • Mock Generation: Automatic in build-test/mocks/
  • Test Support: AVR headers stubbed for host compilation
  • CMock Settings: Hardware interface mocked with mock_ prefix

Architecture

Hardware Abstraction

The project uses a clean hardware abstraction layer:

  • hardware_interface.h/c: GPIO operations abstracted
  • led.h/c: High-level LED control using hardware interface
  • Testability: Hardware layer mocked for unit testing

Build System Design

  • Dual Toolchains: AVR-GCC for firmware, Clang for tests
  • Separate Build Directories: build/ for firmware, build-test/ for tests
  • IntelliSense Support: .clangd configuration for code navigation

Troubleshooting

USB device not found:

  1. Check: usbipd list (device should show "Attached - WSL")
  2. Re-attach: usbipd attach --wsl --busid <BUSID>
  3. Verify in container: lsusb

Build issues:

cmake --build build --target clean-all
cmake --preset avr && cmake --build build

Container issues: Ctrl+Shift+P β†’ Dev Containers: Rebuild Container

About

AVR development environment in a DevContainer

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published