1

I am trying to unit test (using unity+ceedling) some STM32 code on my linux machine, but every time I access any register the code fails with this error:

> Produced no final test result counts in $stdout:
Segmentation fault (core dumped)
> And exited with status: [0] (count of failed tests).
> This is often a symptom of a bad memory access in source or test code

For example this code will result in PASSED 1/1 (note that I am testing function that returns a+b and has nothing to do with STM peripherals).

#include "unity.h"
#include "sum2nums.h"
#include "stm32f4xx.h"

void test_Sum(){
    TEST_ASSERT_EQUAL_UINT32(5, Sum(3, 2));
}

But this code will produced the error mentioned above.

#include "unity.h"
#include "sum2nums.h"
#include "stm32f4xx.h"

void test_Sum(){
    GPIOA->MODER = 1U;
    TEST_ASSERT_EQUAL_UINT32(5, Sum(3, 2));
}

Is it even possible to test it this way or do I have to use QEMU (and how to do so without using Eclipse or any other IDE)? Note that Ceedling uses gcc, if I'd use arm-none-eabi it would produce hex file and I couldn't run that on my PC.

nero
  • 23
  • 1
  • 3

2 Answers2

0

If I understand correctly, this test framework is just trying to compile your test cases with the host x86 C compiler and run them directly. That will work where your test code doesn't do anything hardware-specific, which is why your first test case is fine, but as soon as your code tries to touch the hardware, it will just crash if that hardware isn't actually there, which it obviously isn't if you're running it as a normal x86 Linux process.

If you need to run test cases that access the hardware, then you need to run them either:

  • actually on the hardware, e.g. by having a dev board plugged into your PC and using a test framework that knows how to cross-compile the test, copy the resulting test case binary to the dev board, run it and capture the output.

  • or on an emulator or simulator that provides a model of the hardware. QEMU is one possibility here, assuming that it has a model of the board that you are using and that that model is good enough for whatever you're running. Again, your test framework will need to know how to cross-compile the test and how to run that on the simulator and capture the output.

Peter Maydell
  • 9,707
  • 1
  • 19
  • 25
0

there is a simple solution if you want to run it on PC. You simply copy the stm32fxxx.h header from your STM library and then in that file you replace all the addresses with simple variables. For example:

volatile USART_TypeDef    usart0_base;
#define USART0_BASE       (&usart0_base) /**< USART0 base address  */

Now you change the main header where your CPU specific include is added and and add a MOCK or some define, which will swap the hardware addressed header with this header and you can compile with your computer GCC and run as well on linux (much faster). That way you basically mocked the hardware and replaced it by simple memory.

Crt Mori
  • 242
  • 4
  • 11