C ++设置结构地址

时间:2015-02-04 15:25:33

标签: c++ memory

使用以下代码我试图让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;
    };

1 个答案:

答案 0 :(得分:7)

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

        GPIO = (PortGPIOs *) 0x48001000;

[注意:如果您愿意,请使用C ++风格的演员表 - 结果是相同的。]