automate-stm32-firmware-testing-and-flashing-with-github-actions.png

Automate STM32 Firmware Testing and Flashing with GitHub Actions

Introduction

Automating the process of unit testing and flashing firmware for STM32 microcontrollers can significantly streamline your development workflow. With GitHub Actions, you can set up continuous integration (CI) to run tests and flash the firmware on every push to your repository. This tutorial will guide you through the steps to implement GitHub Actions for automated unit testing and flashing of STM32 firmware.

Prerequisites

  • Basic knowledge of Git and GitHub
  • Familiarity with STM32 microcontroller development
  • A GitHub repository containing your STM32 firmware project
  • Access to a physical STM32 device for flashing
  • Configured toolchain for STM32 development (e.g., STM32CubeIDE or ARM GCC)

Parts/Tools

  • GitHub account
  • STM32 firmware project
  • GitHub Actions
  • OpenOCD or ST-Link for flashing

Steps

  1. Create a GitHub Actions Workflow:

    1. Navigate to your GitHub repository.
    2. Click on the “Actions” tab.
    3. Select “Set up a workflow yourself” or choose a template.
    4. Create a new file called .github/workflows/ci.yml.
  2. Define the Workflow Configuration:

    name: CI
    
    on:
      push:
        branches:
          - main
    
    jobs:
      build:
        runs-on: ubuntu-latest
        steps:
          - name: Checkout code
            uses: actions/checkout@v2
          - name: Set up toolchain
            run: sudo apt-get install -y gcc-arm-none-eabi
          - name: Build the project
            run: make
          - name: Run unit tests
            run: make test
          - name: Flash firmware
            run: ./flash_script.sh
    
  3. Setup a Makefile for Build and Test:

    Create a Makefile in your project root that includes targets for building and testing.

    CC=arm-none-eabi-gcc
    CFLAGS=-mcpu=cortex-m4 -mthumb
    
    all: main.elf
    
    main.elf: main.o
    	$(CC) $(CFLAGS) -o $@ $^
    
    test: main.o
    	# Run unit tests here
    
    clean:
    	rm -f *.o *.elf
    
  4. Create a Flash Script:

    Create a script named flash_script.sh that will handle flashing the firmware to your STM32 device.

    #!/bin/bash
    st-flash write main.elf 0x8000000
    

    Make sure to give execution permission:

    chmod +x flash_script.sh
    
  5. Push Changes to GitHub:

    1. Add your new files to Git:
    2. git add .github/workflows/ci.yml Makefile flash_script.sh
    3. Commit your changes:
    4. git commit -m "Add GitHub Actions workflow for CI"
    5. Push to the main branch:
    6. git push origin main
  6. Monitor the Action Execution:

    1. Go to the “Actions” tab in your GitHub repository.
    2. Select the latest workflow run to see the progress.
    3. Check for any errors in the steps.

Troubleshooting

  • Build Fails: Check the error logs in the Actions tab to diagnose issues with compilation or missing dependencies.
  • Unit Tests Fail: Ensure that your unit tests are correctly configured and that the test framework is included in your project.
  • Flashing Issues: Verify that the st-flash tool is correctly installed and that your STM32 device is connected properly.
  • Permission Denied Errors: Ensure that your scripts have the correct execution permissions.

Conclusion

By following this tutorial, you’ve successfully set up GitHub Actions for automated unit testing and flashing of STM32 firmware. This process not only enhances your development efficiency but also ensures that your code is continuously validated and ready for deployment. With automation in place, you can focus more on coding and less on manual testing and flashing tasks.

Leave a Comment

Your email address will not be published. Required fields are marked *