Wrong readings from FRAM via SPI after switching the Bitrate on STM32H743
Hi All,
In my system (STM32H743) SPI6 is used to address 2 slaves with different bitrates. So I have to reconfigure the bitrate every time I want to address the other unit.
One of the slaves is an FRAM so reading from / writing to the FRAM is preceeded by following code to reconfigure the SPI: (I used STM32CubeMX version 5.0.1 to create the code)
spiSwitchToFRAM
{
if (spi6Is == SPI_IS_FRAM)
return;
HAL_SPI_DeInit(&hspi6);
hspi6.Instance = SPI6;
hspi6.Init.Mode = SPI_MODE_MASTER;
hspi6.Init.Direction = SPI_DIRECTION_2LINES;
hspi6.Init.DataSize = SPI_DATASIZE_8BIT;
hspi6.Init.CLKPolarity = SPI_POLARITY_HIGH; // HIGH oder LOW
hspi6.Init.CLKPhase = SPI_PHASE_2EDGE; // 2EDGE 1EDGE
hspi6.Init.NSS = SPI_NSS_SOFT;
hspi6.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_8; // 8 = 12,5 MHz / 4 = 25 MHz
hspi6.Init.FirstBit = SPI_FIRSTBIT_MSB;
hspi6.Init.TIMode = SPI_TIMODE_DISABLE;
hspi6.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
hspi6.Init.CRCPolynomial = 7;
hspi6.Init.NSSPMode = SPI_NSS_PULSE_ENABLE;
hspi6.Init.NSSPolarity = SPI_NSS_POLARITY_LOW;
hspi6.Init.FifoThreshold = SPI_FIFO_THRESHOLD_01DATA;
hspi6.Init.TxCRCInitializationPattern = SPI_CRC_INITIALIZATION_ALL_ZERO_PATTERN;
hspi6.Init.RxCRCInitializationPattern = SPI_CRC_INITIALIZATION_ALL_ZERO_PATTERN;
hspi6.Init.MasterSSIdleness = SPI_MASTER_SS_IDLENESS_00CYCLE;
hspi6.Init.MasterInterDataIdleness = SPI_MASTER_INTERDATA_IDLENESS_00CYCLE;
hspi6.Init.MasterReceiverAutoSusp = SPI_MASTER_RX_AUTOSUSP_DISABLE;
hspi6.Init.MasterKeepIOState = SPI_MASTER_KEEP_IO_STATE_DISABLE;
hspi6.Init.IOSwap = SPI_IO_SWAP_DISABLE;
if (HAL_SPI_Init(&hspi6) != HAL_OK)
{
Error_Handler();
}
spi6Is = SPI_IS_FRAM;
}
I used this "bitrate switching" method in other STM32F... controllers without any issues.
The problem on the STM32H7 project is, that I needed to "wait for 100 ms" after switching the SPI in order to read correct data from the FRAM.
When reading the data immediately without a pause then data read from FRAM somtimes is wrong. Not always, but at 10-20% of the time.
Does anyone else use an SPI on two different slaves with different SPI configurations and if so, how could you manage to get this working?
Is there any other way to change only the clock rate - all other paramters are same -using the HAL?
Any hints are very much appreciated!
