LL_SPI_SetMode to configure SPI2_CFG2 Master bit not working reliably?
I'm working with a STM32H723ZG and trying to set up SPI2 communication.
I tracked down the issue where my LL_SPI_SetMode(SPI2, LL_SPI_MODE_MASTER) call does not set the Master bit in SPI2_CFG2 to 1, as intended (see Test #1 below). However, if I call the function twice, the second one does set the Master bit to 1 (see Test #2 below).
Does anyone know how that could happen?
Test #1:
/* Configure SPI2 communication */
LL_SPI_SetBaudRatePrescaler(SPI2, LL_SPI_BAUDRATEPRESCALER_DIV4);
LL_SPI_SetTransferDirection(SPI2,LL_SPI_FULL_DUPLEX);
LL_SPI_SetDataWidth(SPI2, LL_SPI_DATAWIDTH_8BIT);
LL_SPI_SetNSSMode(SPI2, LL_SPI_NSS_SOFT);
LL_SPI_SetMode(SPI2, LL_SPI_MODE_MASTER); //this first call does nothing?
Disassembly block when debugging for the last 2 lines:
1629: MODIFY_REG(SPIx->CFG2, SPI_CFG2_SSM | SPI_CFG2_SSOE, NSS);
0x08001D2E 68C1 LDR r1,[r0,#0x0C]
0x08001D30 F0215110 BIC r1,r1,#0x24000000
0x08001D34 F0416180 ORR r1,r1,#0x4000000
0x08001D38 60C1 STR r1,[r0,#0x0C]
665: MODIFY_REG(SPIx->CFG2, SPI_CFG2_MASTER, Mode);
666: }
667:
668: /**
669: * @brief Get SPI Mode (Master or Slave)
670: * @rmtoll CFG2 MASTER LL_SPI_GetMode
671: * SPIx SPI Instance
672: * @retval Returned value can be one of the following values:
673: * @arg @ref LL_SPI_MODE_MASTER
674: * @arg @ref LL_SPI_MODE_SLAVE
675: */
676: __STATIC_INLINE uint32_t LL_SPI_GetMode(SPI_TypeDef *SPIx)
677: {
0x08001D3A 68C1 LDR r1,[r0,#0x0C]
0x08001D3C F4410180 ORR r1,r1,#0x400000
0x08001D40 60C1 STR r1,[r0,#0x0C]
918: }
0x08001D42 BD70 POP {r4-r6,pc}
SPI2_CFG2 after all of this code finishes executing:

Test #2:
/* Configure SPI2 communication */
LL_SPI_SetBaudRatePrescaler(SPI2, LL_SPI_BAUDRATEPRESCALER_DIV4);
LL_SPI_SetTransferDirection(SPI2,LL_SPI_FULL_DUPLEX);
LL_SPI_SetDataWidth(SPI2, LL_SPI_DATAWIDTH_8BIT);
LL_SPI_SetNSSMode(SPI2, LL_SPI_NSS_SOFT);
LL_SPI_SetMode(SPI2, LL_SPI_MODE_MASTER); //this first call does nothing?
LL_SPI_SetMode(SPI2, LL_SPI_MODE_MASTER); //second call actually works??
Disassembly block when debugging for the last 3 lines:
1629: MODIFY_REG(SPIx->CFG2, SPI_CFG2_SSM | SPI_CFG2_SSOE, NSS);
0x08001D2E 68C1 LDR r1,[r0,#0x0C]
0x08001D30 F0215110 BIC r1,r1,#0x24000000
0x08001D34 F0416180 ORR r1,r1,#0x4000000
0x08001D38 60C1 STR r1,[r0,#0x0C]
665: MODIFY_REG(SPIx->CFG2, SPI_CFG2_MASTER, Mode);
666: }
667:
668: /**
669: * @brief Get SPI Mode (Master or Slave)
670: * @rmtoll CFG2 MASTER LL_SPI_GetMode
671: * SPIx SPI Instance
672: * @retval Returned value can be one of the following values:
673: * @arg @ref LL_SPI_MODE_MASTER
674: * @arg @ref LL_SPI_MODE_SLAVE
675: */
676: __STATIC_INLINE uint32_t LL_SPI_GetMode(SPI_TypeDef *SPIx)
677: {
0x08001D3A 68C1 LDR r1,[r0,#0x0C]
0x08001D3C F4410180 ORR r1,r1,#0x400000
0x08001D40 60C1 STR r1,[r0,#0x0C]
665: MODIFY_REG(SPIx->CFG2, SPI_CFG2_MASTER, Mode);
0x08001D42 68C1 LDR r1,[r0,#0x0C]
0x08001D44 F4410180 ORR r1,r1,#0x400000
0x08001D48 60C1 STR r1,[r0,#0x0C]
918: }
0x08001D4A BD70 POP {r4-r6,pc}
SPI2_CFG2 after all of this code finishes executing:

When stepping through in debug mode with the SPI2_CFG2 view open, I can see that the first SetMode call does not turn the Master bit on and the second SetMode call does. It doesn't make sense to me, since the disassembly code blocks are identical (LDR, ORR, STR sequence with identical arguments).
I've tried both with and without CPU_CACHE_Enable() calls and the result is the same.
Although making the call twice appears to be an effective workaround, this behavior is very unsettling and makes me question whether other configuration commands are "getting through". Any help is appreciated!
