2019-08-01 10:15 AM
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!
2019-08-01 10:26 AM
The HAL uses the kitchen sink approach. If you need to do something more laser focused you might have to code it yourself based on the macros, or cutting-n-pasting the code you need from the library.
Perhaps the LL (Low Level Library) has examples.
Usually you have to disable the peripheral, change the clock, and reenable.
H7 might have additional Peripheral Clock routing options.
A logic-analyzer might help understand why the data reads are wrong, build some test cases, trigger with a GPIO when you observe failure condition. With a clear understanding of what's wrong you might be able to resolve it or get ST's attention.
2019-08-01 11:27 AM
After recording, you need to check the status of the FRAM memory. The algorithm is very similar to checking the status of a regular flash memory.