How to make unit tests for embedded code?

I am working on a Cortex-M4 microcontroller software in C ++. I have a lot of code (drivers, etc.), which is very machine dependent. And I have a higher-level code, which is closely dependent on low-level code, using drivers directly. Example: low-level part, for example. a UART driver, which is very specific to the hardware, and the higher-level part is a communication protocol based on UART. (This software runs on bare metal, meaning there is no operating system under it.)

This code is currently closely related, so it cannot be tested.
I would like this to be checked.

So, I decided that I would create an abstraction of the low-level parts and make the high-level parts depend only on the abstraction. Then I could create abstraction layouts that will be used by unit tests, and a real implementation that will be executed on the microcontroller.

  • Is this the right approach?
  • How to create such an abstraction? Most of the sources that I found strongly hinder the use of inheritance functions virtualin embedded systems. What other methods exist?

So, in general, I would like to create a hardware abstraction layer (HAL), but I am asking how to do this? Should I use inheritance virtualin C ++, or is there another, better way?

+4
2

++ , , HAL.hpp, , :

class HAL
{
    virtual void func1() = 0;
    virtual void func2() = 0;
};

Mock.cpp, Real.cpp :

Mock.cpp:
class Mock : HAL
{
        virtual void func1(){ }
        virtual void func2(){ }
}

HAL.h :

void func1();
void func2();

HAL.cpp , . HAL. .

. . , , HAL. Mock.cpp, HAL.h . , HAL Mock.

+5

HAL HAL. HAL . unit test HAL.

, , HAL HAL.

, ; , , ( MC/DC, ). .

+3

Source: https://habr.com/ru/post/1681801/


All Articles