I'm working on software for a Cortex-M4 based microcontroller in C++. I have a lot of code (drivers, etc.) that is highly machine dependent. And I have higher level code which is closely dependant on the low level code by using the drivers directly. Example: a low-level part is eg. a UART driver which is very hardware-specific, and a high-level part is a communication protocol which is based on UART. (This software runs on "bare-metal", ie. there is no operating system underneath.)
This code is currently tightly coupled, thus not unit testable.
I'd like to make it testable.
So I figured that I'd create an abstraction of the low-level parts, and make the high-level parts depend only on the abstraction. I could then create mocks of the abstraction which would be used by the unit tests, and a real implementation which would run on the microcontroller.
- Is this a correct approach?
- How can I create such an abstraction?
Most of the sources I've found strongly discourage the use of inheritance andvirtualfunctions in embedded systems. What other ways are there?
So, in summary, I'd like to create a hardware abstraction layer (HAL), but I'm asking how to do it? Should I use virtual inheritance in C++, or is there another, better way?