C++ 设置结构地址

C++ Setting Struct Address

使用以下代码,我试图让 hwPort 中的结构从内存地址 0x48001000 开始。我现在卡住了,我该怎么做

    struct PortGPIOs
    {
            volatile uint32_t MODER;        /*!< GPIO port mode register,               Address offset: 0x00      */
            volatile uint32_t OTYPER;       /*!< GPIO port output type register,        Address offset: 0x04      */
            volatile uint32_t OSPEEDR;      /*!< GPIO port output speed register,       Address offset: 0x08      */
            volatile uint32_t PUPDR;        /*!< GPIO port pull-up/pull-down register,  Address offset: 0x0C      */
            volatile uint32_t IDR;          /*!< GPIO port input data register,         Address offset: 0x10      */
            volatile uint32_t ODR;          /*!< GPIO port output data register,        Address offset: 0x14      */
            volatile uint16_t BSRRL;        /*!< GPIO port bit set/reset low register,  Address offset: 0x18      */
            volatile uint16_t BSRRH;        /*!< GPIO port bit set/reset high register, Address offset: 0x1A      */
            volatile uint32_t LCKR;         /*!< GPIO port configuration lock register, Address offset: 0x1C      */
            volatile uint32_t AFR[2];       /*!< GPIO alternate function registers,     Address offset: 0x20-0x24 */
            volatile uint32_t BRR;          /*!< GPIO bit reset register,               Address offset: 0x28 */
    };

    class hwPort {
        public:
            hwPort(PortGPIOs *, uint32_t, uint32_t, uint32_t);
            uint32_t read();
            void readOr(uint32_t);
            void readAnd(uint32_t);
            void write(uint32_t);
            void writeOr(uint32_t);
            void writeAnd(uint32_t);
        private:
            PortGPIOs *GPIO;
    };

只需在构造函数中初始化GPIO成员变量,如下所示:

        GPIO = (PortGPIOs *) 0x48001000;

[注意:如果您愿意,可以使用 C++ 风格的转换 - 最终结果是一样的。]