Configuring ITM on STM32U5
We use ITM, locally configured by the embedded code at startup, for our trace output. We have code for this that works with STM32F4 (see below), as advised by Segger; this advice doesn't seem to have changed since we adopted it. Using the same code with STM32U5 we end up in a hard fault after a few calls to ITM_SendChar(). What do we need to add to this code for the STM32U5 case? I have seen a post on this topic here but that person did not succeed, hence asking this narrower question.
Working ITM set-up code for STM32F4 (where U_CFG_HW_SWO_CLOCK_HZ is 125000 to guarantee reliability):
uint32_t stimulusRegs;
// Enable access to SWO registers
DEMCR |= (1 << 24);
ITM_LSR = 0xC5ACCE55;
// Initially disable ITM and stimulus port
// To make sure that nothing is transferred via SWO
// when changing the SWO prescaler etc.
stimulusRegs = ITM_ENA;
stimulusRegs &= ~(1 << 0); // Disable stimulus port 0
ITM_ENA = stimulusRegs;
ITM_TCR = 0; // Disable ITM
// Initialize SWO (prescaler, etc.)
TPIU_SPPR = 0x00000002; // Select NRZ mode
TPIU_ACPR = (SystemCoreClock / U_CFG_HW_SWO_CLOCK_HZ) - 1;
ITM_TPR = 0x00000000;
DWT_CTRL = 0x400003FE;
FFCR = 0x00000100;
// Enable ITM and stimulus port
ITM_TCR = 0x1000D; // Enable ITM
ITM_ENA = stimulusRegs | (1 << 0); // Enable stimulus port 0
In case it matters, I am testing this on a Nucleo 575ZI board with OpenOCD.
