I'm investigating how I can implement a custom C++ HAL which targets multiple microcontrollers, potentially of differing architectures (ARM, AVR, PIC, etc) while keeping things sane.
I've inherited several large, messy code-bases which are un-maintainable in their current state hence the need for something more structured.
Having picked through a number of good articles and design guides, I'm considering a PIMPL
implementation.
Consider the following example for a UART/serial port:
// -----------------------------
// High-level HAL
// -----------------------------
// serialport.h
class SerialPortPrivate;
class SerialPort {
public:
SerialPort(uint8_t portNumber);
~SerialPort();
bool open();
void close();
void setBaudRate(uint32_t baudRate = 115200);
private:
SerialPortPrivate *_impl;
};
// serialport_p.h
class SerialPort;
class SerialPortPrivate {
public:
SerialPortPrivate(uint8_t portNumber, SerialPort *parent) {
// Store the parent (q_ptr)
_parent = parent;
// Store the port number, this is used to access UART
// specific registers UART->D[portNumber] = 0x10;
_portNumber = portNumber;
}
~SerialPortPrivate();
bool open() = 0;
void close() = 0;
void setBaudRate(uint32_t baudRate) = 0;
protected:
uint8_t _portNumber;
private:
SerialPort *_parent;
};
// serialport.cpp
#include "serialport.h"
#include "serialport_p.h"
#include "stm32serialport_p.h"
#include "avr32serialport_p.h"
#include "nrf52serialport_p.h"
#include "kinetisserialport_p.h"
SerialPort::SerialPort(uint8_t portNumber) {
#if MCU_STM32
_impl = new Stm32SerialPortPrivate(portNumber, this);
#elif MCU_AVR32
_impl = new Avr32SerialPortPrivate(portNumber, this);
#elif MCU_NRF52
_impl = new Nrf52SerialPortPrivate(portNumber, this);
#elif MCU_KINETIS
_impl = new KinetisSerialPortPrivate(portNumber, this);
#endif
}
void SerialPort::setBaudRate(uint32_t baudRate) {
_impl->setBaudRate(baudRate);
}
// -----------------------------
// Low-level BSP
// Hardware-specific overrides
// -----------------------------
// stm32serialport_p.h
class Stm32SerialPortPrivate : public SerialPortPrivate {
};
// nrf52serialport_p.h
class Nrf52SerialPortPrivate : public SerialPortPrivate {
};
// kinetisserialport_p.h
class KinetisSerialPortPrivate : public SerialPortPrivate {
};
The above code only has one set of #if/#endif
statements within the constructor of the high-level interface (SerialPort
) and the hardware-specific code (register accesses, etc) is done within the private implementation.
Taking the above further, I can see the above implementation working well for classes like I2cPort
, SpiPort
, UsbSerialPort
but for other non-port related peripheral sets like clocks, hardware timers.
I'm sure there are some holes in the above concept, can anyone suggest from experience things to avoid or if there is a better way of abstracting?