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
- Set up your Ceedling project
- Open your terminal and create a new Ceedling project with:
- Navigate into your project folder:
ceedling new MyProject
cd MyProject
- Configure your project
- Edit the `project.yml` file to include necessary paths for your STM32 libraries.
- Include any required headers for GPIO manipulation.
- 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 }
- 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
- Set up the Unity test file
Create a test file named
test_gpio.c
in thetest
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 }
- Run the tests
- In your terminal, run the tests with:
- Check the output for any failed tests and debug accordingly.
ceedling test:all
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!