I'm trying to get a variable declared in the main
into the private variables of my class without passing it as an argument for the constructor. I need to link the interrupt controller to multiple hardware interrupts without re initializing the interrupt instance and thus overwriting it.
XScuGic InterruptInstance;
int main()
{
// Initialize interrupt by looking up config and initializing with that config
ConfigPtr = XScuGic_LookupConfig(INTERRUPT_DEVICE_ID);
XScuGic_CfgInitialize(&InterruptInstance, ConfigPtr, ConfigPtr->BaseAddr);
Foo foo;
foo.initializeSpi(deviceID,slaveMask);
return 0;
}
And the implementation of the class Foo:
class Foo
{
// This should be linked to the one in the main
XScuGic InterruptInstance;
public:
// In here, the interrupt is linked to the SPI device
void initializeSpi(uint16_t deviceID, uint32_t slaveMask);
};
The deviceID and slaveMask are defined in a header which is included.
Is there some way to achieve this?
You can initialize a private class reference member with a constructor that uses the global variable, so then there is no need to pass it in the constructor:
XScuGic InterruptInstance_g; // global variable
class Foo {
private:
const XScuGic& InterruptInstance; // This should be linked to the one in the main
public:
Foo() : InterruptInstance{InterruptInstance_g} {}; // private variable is initialized from global variable
void initializeSpi(uint16_t deviceID,uint32_t slaveMask); // In here, the interrupt is linked to the SPI device
};
int main()
{
// Initialize interrupt by looking up config and initializing with that config
ConfigPtr = XScuGic_LookupConfig(INTERRUPT_DEVICE_ID);
XScuGic_CfgInitialize(&InterruptInstance,ConfigPtr,ConfigPtr->BaseAddr);
Foo foo{}; // reference is not required as it will come from the global variable to initialize the private reference member
foo.initializeSpi(deviceID,slaveMask);
return 0;
}