简体   繁体   English

访问嵌套结构

[英]Accessing a nested structure

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:这个结构体已经在main函数中实例化了一个结构体,如下所示:

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其中self被声明为struct stm32fxxx_gpio *self

My question is: how is self->state different from self->gregs ?我的问题是: self->stateself->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.我知道state包含gpio_state属性。 But state structure does not have attributes different from gpio_state structure, then why do we need structures in this case?但是state结构并没有与gpio_state结构不同的属性,那么为什么我们在这种情况下需要结构呢?

self->state and self->regs are 2 different pointers. self->stateself->regs是两个不同的指针。 The code probably initializes these fields to point to parts of the same structure.代码可能会初始化这些字段以指向相同结构的部分。

You try to reinvent the wheel and you did it wrong.你试图重新发明轮子,但你做错了。

  1. you have defined structures with the hardware registers.你已经用硬件寄存器定义了结构。
  2. Your declarations are not "generic" as many families have different registers.您的声明不是“通用的”,因为许多系列都有不同的寄存器。 For example F3xx has additional BRR register.例如 F3xx 有额外的BRR寄存器。
  3. Order of the registers is wrong.寄存器的顺序是错误的。
  4. Some peripherals have unused space between the registers.一些外设在寄存器之间有未使用的空间。 For example例如
     typedef struct
    {
      __IO uint32_t ACR;          /*!< FLASH access control register,              Address offset: 0x00 */
      __IO uint32_t KEYR;         /*!< FLASH key register,                         Address offset: 0x04 */
      __IO uint32_t OPTKEYR;      /*!< FLASH option key register,                  Address offset: 0x08 */
      __IO uint32_t SR;           /*!< FLASH status register,                      Address offset: 0x0C */
      __IO uint32_t CR;           /*!< FLASH control register,                     Address offset: 0x10 */
      __IO uint32_t AR;           /*!< FLASH address register,                     Address offset: 0x14 */
      uint32_t      RESERVED;     /*!< Reserved, 0x18                                                   */
      __IO uint32_t OBR;          /*!< FLASH Option byte register,                 Address offset: 0x1C */
      __IO uint32_t WRPR;         /*!< FLASH Write register,                       Address offset: 0x20 */

    } FLASH_TypeDef;

If your idea is to save registers in the RAM it is enough to如果您的想法是将寄存器保存在 RAM 中,则足以

GPIO_TypeDef savedGPIOs[NUMBER_OF_GPIOS];

and

savedGPIOs[0] = *GPIOA;

but I do not see too much sense in it.但我看不出有太多意义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM