简体   繁体   English

如何在 Linux PC 上运行 stm32 单元测试?

[英]How to run stm32 unit tests on Linux PC?

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:我正在尝试在我的 linux 机器上对一些 STM32 代码进行单元测试(使用 unity+ceedling),但每次我访问任何寄存器时,代码都会失败并出现此错误:

> 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).例如,此代码将导致通过 1/1(请注意,我正在测试返回 a+b 且与 STM 外设无关的 function)。

#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)?甚至有可能以这种方式对其进行测试,还是我必须使用 QEMU(以及如何在不使用 Eclipse 或任何其他 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.请注意,Ceedling 使用 gcc,如果我使用 arm-none-eabi,它会生成十六进制文件,而我无法在我的 PC 上运行它。

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.如果我理解正确,这个测试框架只是试图用主机 x86 C 编译器编译你的测试用例并直接运行它们。 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.这将适用于您的测试代码不执行任何特定于硬件的操作,这就是为什么您的第一个测试用例很好的原因,但是一旦您的代码尝试接触硬件,如果该硬件实际上不存在,它就会崩溃,如果您将其作为正常的 x86 Linux 进程运行,则显然不是。

If you need to run test cases that access the hardware, then you need to run them either:如果您需要运行访问硬件的测试用例,那么您需要运行它们:

  • actually on the hardware, eg 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.实际上在硬件上,例如通过将开发板插入您的 PC 并使用知道如何交叉编译测试的测试框架,将生成的测试用例二进制文件复制到开发板,运行它并捕获 output。

  • or on an emulator or simulator that provides a model of the hardware.或在提供硬件 model 的仿真器或模拟器上。 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. QEMU 是这里的一种可能性,假设它具有您正在使用的板的 model 并且 model 对于您正在运行的任何东西都足够好。 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.同样,您的测试框架需要知道如何交叉编译测试以及如何在模拟器上运行它并捕获 output。

there is a simple solution if you want to run it on PC.如果你想在 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.您只需从 STM 库中复制stm32fxxx.h header,然后在该文件中用简单变量替换所有地址。 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).现在,您更改主要的 header,其中添加了您的 CPU 特定包含,并添加了一个 MOCK 或一些定义,这会将地址为 header 的硬件与此 header 交换,您可以使用您的计算机 GCC 进行编译,并在 linux 上运行(更快) . That way you basically mocked the hardware and replaced it by simple memory.这样你基本上模拟了硬件并将其替换为简单的 memory。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM