cancel
Showing results for 
Search instead for 
Did you mean: 

One bit shift in SPI communication on STM32G4

sireevenkat1
Senior

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

 

 

1 ACCEPTED SOLUTION

Accepted Solutions

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.

View solution in original post

9 REPLIES 9
Issamos
Lead II

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

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

sireevenkat1
Senior

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

Hello again

I suggest you to reduce your bit rate and retry.

Best regards.

II

Hi,

I did that still same issue.
Thanks

Then the issue is not caused by the baud rate. I suggest you to share your code here so the community members may help you finding your problem.

Best regards.

II

Hi,
Fully I can't share
I am sharing the logic
//write logic

void ATM90E26_SPI_Write(uint8_t addr, uint16_t data)

{

uint8_t txData[3];

txData[0] = addr & 0x7F;

txData[1] = (data >>8) & 0xFF;

txData[2] = data & 0xFF;

HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_RESET); // Pull CS/SS low

HAL_SPI_Transmit(&hspi1, txData, 3,100);

HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_SET); // Pull CS/SS high

}
//Read logic

uint16_t ATM90E26_SPI_Read(uint8_t addr)

{

uint8_t txData= addr | 0x80;

uint8_t rxData[2];

HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_RESET); // Pull CS/SS low

HAL_SPI_Transmit(&hspi1, &txData, 1,100);

HAL_SPI_Receive(&hspi1, rxData, 2,100);

// HAL_SPI_TransmitReceive(&hspi1, txData, rxData, 2, 200);

HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_SET); // Pull CS/SS high

return (uint16_t)((rxData[0] << 8)| rxData[1]);

}
//ATM90E26 SPI sequence

sireevenkat1_0-1693903427712.png

 

 

 

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.

SHIPANGEL
Associate

Try enabling the SPI unit before selecting the slave