Using c++20, I have the following idea to generalize access to multiple chips in a circuit:
An interface should allow reading/writing to all chips with different address bits and register sizes (8, 16, …)
template<unsigned int AddressBits = 9, unsigned int ContentBits = 32>
requires (AddressBits < ContentBits)
class MyInterface {
protected:
std::bitset<AddressBits> mAddress;
std::bitset<ContentBits> mValue;
public:
virtual ~MyInterface() = 0;
virtual std::bitset<ContentBits> read() = 0;
virtual bool write(const std::bitset<ContentBits> &value) = 0;
};
Now, let’s say I need to drive from the above interface for a couple of chips with different addresses and content sizes, i.e. one is <8,16> and one is <9, 32>.
How should I do this? does it even make sense? I wrote the code below but it does not support my initial idea:
class DRV8323 : public MyInterface<8,16> {
explicit DRV8323(unsigned int address, unsigned int resetValue = 0) {
mAddress = address;
mValue = resetValue;
}
~DRV8323() override = default;
std::bitset<16> read() override {
// Somehow read from hardware and return
return mValue;
}
bool write(const std::bitset<16> &value) override {
// Somehow write to hardware and return true/false to indicate result
return true;
}
};
// or vector<unique_ptr<MyInterface>>
std::vector<MyInterface*> AllRegisters = {
{ /* How to add two different derived classes/chips? */ },
{ /* How to add two different derived classes/chips? */ },
// And so on
};
- For the read function, I need to provide the number explicitly…Is there a way to automatically deduct it from the
MyInterface<8,16>
? - Let’s say I add another chip with a 9-bit address and 32-bit content. How can I then have a vector on the global scope that contains registers from both derived classes:
3