cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F4 SPI Clock polarity - How to change it dynamically?

shinseow
Associate
Posted on May 09, 2013 at 22:50

We can initialize the SPI port clock polarity  by :-

SPI_InitStruct.SPI_CPOL = SPI_CPOL_Low;

SPI_InitStruct.SPI_CPHA = SPI_CPHA_1Edge;

If we have several slaves connected to the SPI port, different slave operated using different clock polarity, is there any way to change those 2 parameters easily without using the above method again?

Thanks.

#stm32f4-spi-cpol-cpha
2 REPLIES 2
John F.
Senior
Posted on May 10, 2013 at 08:55

You can write to the SPI_CR1 register to directly change the CPOL/CPHA bits but note that prior to changing the CPOL/CPHA bits the SPI must be disabled by resetting the SPE bit.

I use multiple SPI devices with different configurations and simply call the functions to re-initialise the SPI before selecting a device. You probably will need to be handling the slave select (device CS_N) lines in software anyway. It's all quite fast! Alternatively, use multiple SPIs with each set for a particular configuration.

jj2
Associate II
Posted on May 10, 2013 at 18:13

''is there any way to change those 2 SPI parameters easily?''

 

Forgive - but coming from past, dark days - when we changed parameters w/xacto-blade, wire-cutter, soldering iron - just how much easier can life get?

As responding poster notes - conserving MCU pins forces you to alter some aspects of SPI config.  Multiple SPI employment may enable ''just once'' set-up/config - at the cost of additional pin usage...

Did think of one old, HW method - to achieve your desire.  Simple inverter would be added - fed by SPI clock.  Then both ''normal clock'' and new, ''inverted clock'' feed a data selector IC - which is controlled by single GPIO.  Now - via toggle of that GPIO - normal or inverted clock - at your SW command...  (with a little help from HW add-on)