Skip to main content
Explorer II
September 4, 2023
Solved

One bit shift in SPI communication on STM32G4

  • September 4, 2023
  • 5 replies
  • 5128 views

Hi,

I am working on SPI communication between STM32G491 and ATM90E26. and I am using the SPI polling API with HAL_SPI_Transmit and HAL_SPI_Receive.

My System clock is 96Mhz and I am using prescaler of 64 for SPI.

CLKPolarity=Low,

CLKPhase=1Edge.

Communication is happening between the devices but I am not getting the correct data.
there is only a shift of one bit.

For example
in binary:

expected: 0001 0011 1001 0100

received: 0000 1001 1100 1010
f I shift the received bitstream to the left I get exactly the right result.

I found one solution in the forum for similar type of question(for stm32H7) It was solved with
hspi1.Init.MasterKeepIOState = SPI_MASTER_KEEP_IO_STATE_ENABLE;

But I am using STM32G4 with SPI Poll method
there is no MasterKeepIOState parameter  available its giving me the error I use it.
Is it due to bad configutation?

or anything do I need to configure to solve it. Can anyone please suggest.

Thanks

 

 

    This topic has been closed for replies.
    Best answer by David Littell

    Note that SPI is a bit exchange protocol.  For every bit you send you receive a bit.  So, given the "SPI sequence" you show you must transmit 3 bytes in order to properly set the register address and transfer the 2 data bytes (for either a read or write operation).  You must use a 3-byte HAL_SPI_TransmitReceive() for the register read operation.

    5 replies

    Graduate II
    September 4, 2023

    Hello @sireevenkat1

    I suggest you to try to compare your exemple to one of those exemples.

    Else, you can take a look at the UM2570 to find all SPI commands.

    Best regards

    II

    Graduate II
    September 4, 2023

    Normally this problem is due to a SPI clock signal integrity issue.  Ensure the SPI clock is clean with minimal switching artifacts, reflections, etc.

    Explorer II
    September 5, 2023

    Thanks for the replies @David Littell @Issamos ,

    I changed SPI prescaler from 64 to 256 ,Now I can able to read correct values from few registers(ATM90E26 is energy meter IC).Now I can able to read frequency & meter status registers correctly. But If I read voltage and current registers I am always getting value zero values.
    In ATM90E26 datasheet SPI Interface Bit Rate  max is 160K Bps.

    In my configuration 96Mhz(system clock)/256(prescaler)=375 kbps.
    Is the issue with Bit rate? Can anyone clarify me ?
    Because I can able to read few registers of ATM90E26 correctly & few registers I can't able to read.
    Thanks

    Graduate II
    September 5, 2023

    Hello again

    I suggest you to reduce your bit rate and retry.

    Best regards.

    II

    Explorer II
    September 5, 2023

    Hi,

    I did that still same issue.
    Thanks

    Graduate II
    September 5, 2023

    Note that SPI is a bit exchange protocol.  For every bit you send you receive a bit.  So, given the "SPI sequence" you show you must transmit 3 bytes in order to properly set the register address and transfer the 2 data bytes (for either a read or write operation).  You must use a 3-byte HAL_SPI_TransmitReceive() for the register read operation.

    Visitor II
    December 24, 2023

    Try enabling the SPI unit before selecting the slave