Skip to main content
Explorer
September 4, 2025
Solved

Compiling/Linking stm32h753xx.h with errors

  • September 4, 2025
  • 1 reply
  • 498 views

I am trying to compile in stm32h753xx.h into a project.  It has a number of lines like this:

#define SPI3 ((SPI_TypeDef *) SPI3_BASE)

I am getting this error on that line:

In file included from ../src/libraries/CMSIS/Device/ST/STM32H7xx/Include/stm32h7xx.h:124,
 from ../src/rt-arm/STM32F4/i2cComm.hh:17,
 from ../src/rt-arm/busses/i2c.cc:12:
../src/libraries/CMSIS/Device/ST/STM32H7xx/Include/stm32h753xx.h:2510:30: error: 'reinterpret_cast<SPI_TypeDef*>(1073757184)' is not a constant expression
 2510 | #define SPI3 ((SPI_TypeDef *) SPI3_BASE)
 | ~^~~~~~~~~~~~~~~~~~~~~~~~~~
../src/rt-arm/STM32F4/dma.hh:178:64: note: in expansion of macro 'SPI3'
 178 | plate<> struct dma_perph<1,0,0> { enum {addr = (uint64_t)(&SPI3->RXDR)}; }; //"SPI3_Rx"
 | ^~~~

Other lines like that one also get a similar error.  What I don't understand is that the SPI3_BASE is a constant and therefore I would think everything in ((SPI_TypeDef *) SPI3_BASE) would be considered a constant.  This code is directly from STM and therefore I'd expect the code to be correct and compilable.  Is the fact that the compiler is trying to change the c style cast to a reinterpret_cast<SPI_TypeDef*> causing it to think that it is not really a constant expression?  If so, what can be done to deal with this?  Given that it is a #define it seems that there shouldn't even be the requirement that the substituted value is constant.  In any case, do you have any ideas about the core problem here and how to fix it?

Note that I am not compiling this within STM32CubeIDE.  I am compiling with the arm-none-eabi-g++ compiler with this version info:

arm-none-eabi-g++ (15:13.2.rel1-2) 13.2.1 20231009
Copyright (C) 2023 Free Software Foundation, Inc.

 

    This topic has been closed for replies.
    Best answer by TDK

    The problem is not with the header.

    The problem is that where you're using it requires a constexpr, but SPI3 is not a constexpr, so you can't use it in that manner.

    A #define is just the preprocessor. It gets filtered out before compiling even takes place. Take out SPI3 and put in ((SPI_TypeDef *) SPI3_BASE) and you will get the same error.

    1 reply

    Super User
    September 4, 2025
    Explorer
    September 4, 2025

    There's some aspects of that stack overflow article that don't seem to quite fit.  One is that the code I referenced uses a #define instead of a constexpr.  A #define shouldn't require a constant expression as I understand it.  Additionally, although the error calls out reinterpret_cast<>, the original code actually uses a c style cast in it.  I don't understand why it seems to be converting the c style cast into a reinterpret_cast in the error.  

    TDKAnswer
    Super User
    September 4, 2025

    The problem is not with the header.

    The problem is that where you're using it requires a constexpr, but SPI3 is not a constexpr, so you can't use it in that manner.

    A #define is just the preprocessor. It gets filtered out before compiling even takes place. Take out SPI3 and put in ((SPI_TypeDef *) SPI3_BASE) and you will get the same error.