I have a nested structure like below:
struct stm32fxxx_state {
struct stm32fxxx_gpio_state {
union {
uint32_t regs[10];
struct {
uint32_t MODER;
uint32_t OTYPER;
uint32_t OSPEEDR;
uint32_t PUPDR;
uint32_t IDR;
uint32_t ODR;
uint32_t BSRR;
uint32_t LCKR;
uint32_t AFRL;
uint32_t AFRH;
};
};
} GPIO[STM32FXXX_NUM_GPIOS];
struct stm32fxxx_spi_regs {
union {
uint16_t regs[9];
struct {
uint16_t CR1;
uint16_t CR2;
uint16_t SR;
uint16_t DR;
uint16_t CRCPR;
uint16_t RXCRCR;
uint16_t TXCRCR;
uint16_t I2SCFGR;
uint16_t I2SPR;
};
};
} SPI[STM32FXXX_NUM_SPIS];
uint32_t PWR_CR;
uint32_t PWR_CSR;
};
This structure has been instantiated in the main
function in a structure as below:
struct stm32fxxx_gpio {
SysBusDevice parent;
MemoryRegion mmio;
qemu_irq irq;
uint8_t port_id, _port_id;
struct stm32fxxx_state *state;
struct stm32fxxx_gpio_state *gregs;
};
Somewhere further in the code, the structure is accessed as follows:
uint32_t valx = val ^ self->state->GPIO[self->port_id].MODER;
and
uint32_t valx = val ^ self->gregs->OTYPER;
Where self
is declared as struct stm32fxxx_gpio *self
My question is: how is self->state
different from self->gregs
? How are these two access to the structure is different.
The code is compiled and runs fine. I want to know how these two access return different data? Or what is the use of such nested structures?
I understand state
contains the gpio_state
attributes as well. But state
structure does not have attributes different from gpio_state
structure, then why do we need structures in this case?