slasher53
slasher53

Reputation: 141

C++ Setting Struct Address

With the following Code I'm trying to get the struct in hwPort to start at the memory address 0x48001000. How would I do this as I'm currently stuck

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

Upvotes: 0

Views: 469

Answers (1)

Paul R
Paul R

Reputation: 213059

Just initialise the GPIO member variable in your constructor, as follows:

        GPIO = (PortGPIOs *) 0x48001000;

[Note: use a C++-style cast if you prefer - the end result is the same.]

Upvotes: 7

Related Questions