dimanche 7 octobre 2018

constexpr pointer to processor fixed address

I have read all the posting regarding this subject. It is understandable that you can not use static variables in constexpr since they are not known until link time. However I do not understand why the compiler will not allow the creation of a static pointer to a truly static address.

We have been converting our code to C++11, due to the "many advantages for embedded programming". This particular issue is troubling. Here are the various attempts to get this to work.

#define PERIPH_BASE   0x40000000U
#define GPIOH_BASE    (AHB1PERIPH_BASE + 0x1C00U)
#define GPIOH         ((GPIO_TypeDef *) GPIOH_BASE)

#define KBATH_NFAULT_GPIO_Port GPIOH

// Attempt #1
const  GPIO_TypeDef* KBATH_NFAULT_GPIO_Port = (GPIO_TypeDef*)GPIOH_BASE;
constexpr DigitalInput BathNFaultK {const_cast<GPIO_TypeDef*> 
    (KBATH_NFAULT_GPIO_Port), BATH_NFAULT_Pin};

// Attempt #2
constexpr GPIO_TypeDef*  KBATH_NFAULT_GPIO_Port = (GPIO_TypeDef*)GPIOH_BASE;
constexpr DigitalInput BathNFaultK {KBATH_NFAULT_GPIO_Port, BATH_NFAULT_Pin};

// Attempt #3
#define STM_PERIPH(a) __builtin_constant_p (a) ? a : a
constexpr DigitalInput BathNFaultK {
    STM_PERIPH((GPIO_TypeDef*)GPIOH_BASE), BATH_NFAULT_Pin};

// Attempt #4
static constexpr inline GPIO_TypeDef* STM_GPIO_PORT(GPIO_TypeDef* p){
    return __builtin_constant_p((p))?(p):(p);
}
constexpr DigitalInput BathNFaultK {
    const_cast<GPIO_TypeDef*>(STM_GPIO_PORT(BATH_NFAULT_GPIO_Port)), 
    BATH_NFAULT_Pin
};

In regular "C", this is acceptable. Is there a solution? Is this fixed in C++17?

Thanks

Aucun commentaire:

Enregistrer un commentaire