C++ 设置结构地址

标签 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;
    };

最佳答案

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

        GPIO = (PortGPIOs *) 0x48001000;

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

关于C++ 设置结构地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28325028/

相关文章:

java - 以编程方式获取android设备的所有RAM内存,而不仅仅是分配给用户进程的内容

c - 如何使数据类型小于 32 位

c - posix_memalign/memalign 做什么

c - 与 C 中的 NULL 相比的非空参数

c++ - 删除指向不同指针类型的复制指针会导致内存泄漏吗?

c++ - 通过头文件确定.lib/.dll

c++ - 从 bash 脚本发送一些参数(如 Ctrl-C、3、some_string)到程序

c++ - C99 和 C++ 的内联函数的兼容定义

c++ - 在 C/C++ 中检查 NULL 指针

c++ - 在 cpp 程序之间交换变量