2025-08-16 5:42 AM
Hi, Im new to STM32 and Im writing a bare metal spi library however for some reason when I try to change the SSM bit in code it doesnt seem to work. Im able to change the SSM bit in debug mode.
void SPI_Enable(SPI_Com* spi)
{
SPI1_CLK_EN;
//Reset the whole CR1 register if someone decides to enable it using different options
//First disable SPI
spi->spiReg->SPI_CR1 &= ~(1 << 6);
spi->spiReg->SPI_CR1 = 0;
//CPOL and CPHA
spi->spiReg->SPI_CR1 |= (spi->spiConfig.CPHA);
spi->spiReg->SPI_CR1 |= (spi->spiConfig.CPOL << 1);
//Master, Slave mode
spi->spiReg->SPI_CR1 |= (spi->spiConfig.masterMode << 2);
//Set baud rate
spi->spiReg->SPI_CR1 |= (spi->spiConfig.baudRate << 3);
//MSB or LSB first
spi->spiReg->SPI_CR1 |= (spi->spiConfig.isLSB << 7);
//Data format
spi->spiReg->SPI_CR1 |= (spi->spiConfig.dataFormat << 11);
//Software Slave SSM and SSI bits
//spi->spiReg->SPI_CR1 |= (spi->spiConfig.isSoftwareSlave << 9);
spi->spiReg->SPI_CR1 |= (1 << 9);
spi->spiReg->SPI_CR1 |= (1 << 8);
//Set duplex modes
switch(spi->spiConfig.duplexMode)
{
case FULL_DUPLEX_MODE:
//You dont have to change since by default its in full duplex mode
break;
case HALF_DUPLEX_MODE:
spi->spiReg->SPI_CR1 |= (1 << 15);
break;
case RECIEVE_ONLY_MODE:
spi->spiReg->SPI_CR1 |= (1 << 10);
break;
}
//Bidi mode
spi->spiReg->SPI_CR1 |= (spi->spiConfig.bidiMode << 14);
//Set CS to high
SetOutputAdress(spi->CS, 1);
//Set busy to 0
spi->isBusy = 0;
spi->spiReg->SPI_CR1 |= (1 << 6);
}
Solved! Go to Solution.
2025-08-17 6:15 AM
Hi, after testing some stuff it appears there was a problem within the inbuilt debugger causing it to skip some lines of code or something like that. I dont know what seemed to cause it. It seems to be fixed now but when i tried to unplug/restart the cube ide at that time it wasnt working. Sorry to bother
2025-08-16 3:38 PM
Some mode bits can be changed only when the SPI is disabled or transfer not active etc. See the RM.
2025-08-17 2:19 AM
> when I try to change the SSM bit in code it doesnt seem to work
What are exactly the symptoms, how do you check that?
Which STM32 in particular? What are its clock settings?
I would recommend to "assemble" all these bits into one local variable, and then write that variable into SPI_CR1 register in one single straight write, rather than have a sequence of read-modify-writes.
JW
2025-08-17 6:15 AM
Hi, after testing some stuff it appears there was a problem within the inbuilt debugger causing it to skip some lines of code or something like that. I dont know what seemed to cause it. It seems to be fixed now but when i tried to unplug/restart the cube ide at that time it wasnt working. Sorry to bother