cancel
Showing results for 
Search instead for 
Did you mean: 

SPI data Issue with SPI_DATASIZE_8BIT

ZMohd.7
Associate III

Hi,

I'm facing an issue after configuring the SPI1 of STM32G491REI6 MCU using STm32CubeMX for SPI_DATASIZE_8BIT. I have captured the signals on the Oscilloscope after Transmitting the followed three-byte (0x03, 0x00, 0x50) from master(MCU) to slave. you can see clearly in the image for each byte transmit MCU is generating a clock two times, which is undesired. Clock (Yellow) and data(Green)

For SPI_DATASIZE_9BIT is working fine, see the second photo.

0693W00000DmPTpQAN.png 

0693W00000DmPd6QAF.png

static void MX_SPI1_Init(void)
{
hspi1.Instance = SPI1;
 
 hspi1.Init.Mode = SPI_MODE_MASTER;
 
 hspi1.Init.Direction = SPI_DIRECTION_2LINES;
 
 hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
 
 hspi1.Init.CLKPolarity = SPI_POLARITY_LOW;
 
 hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;
 
 hspi1.Init.NSS = SPI_NSS_SOFT;
 
 hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_16;
 
 hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
 
 hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
 
 hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
 
 hspi1.Init.CRCPolynomial = 7;
 
 hspi1.Init.CRCLength = SPI_CRC_LENGTH_DATASIZE;
 
 hspi1.Init.NSSPMode = SPI_NSS_PULSE_ENABLE;
 
 if (HAL_SPI_Init(&hspi1) != HAL_OK)
 {
  Error_Handler();
 }
}

@

I have tested the same code SPI_DATASIZE_8BIT for the STM32F446RE MCU and that's working fine, See Photo.

0693W00000DmPdaQAF.png 

The only issue is with STM32G491REI6 using SPI_DATASIZE_8BIT. Please provide the scalable solution as per the mentioned query.

Thanks in Advance.

4 REPLIES 4
TDK
Guru

Show the code you're using to write the data to the SPI.

Possibly you're using a 16-bit instruction, so sending two bytes instead of one. The G4 has a FIFO here whereas the F4 does not.

If you feel a post has answered your question, please click "Accept as Solution".

It's probably not FIFO but data packing.

JW

ZMohd.7
Associate III

Hi,

I am placing two type code snippet I tried-

In main routine, NSS is Reset forcefully by firmware (GPIO control), after that "SPI_Transmit" function is called.

when you set debugger breakpoint at line4 or line3 of functions, still 2 Bytes sent by the controller (Image attached)  

Case 1:-

void SPI_Transmit (uint8_t data) {
 
line1: while (!((SPI1->SR) & (1 << 1))) {}; // wait for TXE bit to set 
 
line2:  SPI1->DR = data;
 
line3:  while (((SPI1->SR)&(1<<7))) {};
 
line4:  while (((SPI1->SR)&(1<<7))) {}; // wait for BSY bit to Reset 
}

Case 2:

void SPI_Transmit (uint8_t data) { 
 
line1: while (!((SPI1->SR) & (1 << 1))) { }; // wait for TXE bit to set 
 
line2: SPI1->DR = data;
 
line3: while (((SPI1->SR)&(1<<7))) {};
 
line4: while (((SPI1->SR)&(1<<7))) {}; // wait for BSY bit to Reset 
 
line5: uint8_t temp = SPI1->DR; // dummy Read SPI1->DR 
 
line6: temp = SPI1->SR; // dummy Read SPI1->SR 
}

0693W00000DmXMrQAN.png 

Thanks

TDK
Guru

> SPI1->DR = data;

If you want to write a byte (as opposed to a word), you need to force the compiler to use a byte access instruction.

*((volatile uint8_t *) &SPI1->DR) = data

https://github.com/STMicroelectronics/STM32CubeG4/blob/e762fe2ce800cf6c18a1868a3aabc7e9351751bd/Drivers/STM32G4xx_HAL_Driver/Src/stm32g4xx_hal_spi.c#L932

If you feel a post has answered your question, please click "Accept as Solution".