how-to-create-unity-ceedling-firmware-unit-tests-for-stm32-gpio-toggle.png

How to Create Unity/Ceedling Firmware Unit Tests for STM32 GPIO Toggle

Creating a Unity/Ceedling Firmware Unit Test for STM32 GPIO Toggle Functionality with Mock Dependencies

This tutorial will guide you through the process of creating a unit test for the GPIO toggle functionality of an STM32 microcontroller using the Unity testing framework and Ceedling. We will cover the prerequisites, necessary tools, and step-by-step instructions to set up and run the test.

Prerequisites

  • Basic knowledge of C programming
  • Familiarity with STM32 microcontrollers
  • Installed Unity and Ceedling frameworks
  • STM32 development environment (e.g., STM32CubeIDE or similar)

Parts/Tools

  • Computer with STM32 development environment
  • Unity testing framework
  • Ceedling build system
  • STM32 microcontroller

Steps

  1. Set up your Ceedling project
    1. Open your terminal and create a new Ceedling project with:
    2. ceedling new MyProject
    3. Navigate into your project folder:
    4. cd MyProject
  2. Configure your project
    1. Edit the `project.yml` file to include necessary paths for your STM32 libraries.
    2. Include any required headers for GPIO manipulation.
  3. Write the GPIO toggle function

    Create a new source file named gpio.c and implement the GPIO toggle functionality:

    
    #include "gpio.h"
    
    void toggle_gpio(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin) {
        GPIOx->ODR ^= GPIO_Pin; // Toggle the specified pin
    }
            
  4. Create a header file for GPIO

    In the same directory, create gpio.h with the following content:

    
    #ifndef GPIO_H
    #define GPIO_H
    
    #include "stm32f4xx.h" // Adjust according to your STM32 series
    
    void toggle_gpio(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);
    
    #endif // GPIO_H
            
  5. Set up the Unity test file

    Create a test file named test_gpio.c in the test directory with:

    
    #include "unity.h"
    #include "gpio.h"
    
    void test_toggle_gpio(void) {
        // Mock GPIO structure
        GPIO_TypeDef mock_gpio;
        mock_gpio.ODR = 0x00; // Initial state
    
        toggle_gpio(&mock_gpio, 0x01); // Toggle pin 0
        TEST_ASSERT_EQUAL(0x01, mock_gpio.ODR); // Check if pin is toggled
    }
            
  6. Run the tests
    1. In your terminal, run the tests with:
    2. ceedling test:all
    3. Check the output for any failed tests and debug accordingly.

Troubleshooting

  • Test fails: Verify that your GPIO structure is correctly mocked and that the toggle function is implemented correctly.
  • Include path errors: Ensure your project.yml includes paths to all necessary STM32 headers.
  • Ceedling setup issues: Revisit the installation steps for Ceedling and ensure it’s correctly set up in your environment.

Conclusion

In this tutorial, you learned how to create a unit test for the GPIO toggle functionality of an STM32 microcontroller using Unity and Ceedling. By following these steps, you can ensure that your firmware is robust and functions as intended. Happy coding!

Leave a Comment

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