cancel
Showing results for 
Search instead for 
Did you mean: 

When transmitting via SPI, a bit offset occurs after the first transmission.

User.5
Associate

Hello community.

There was a problem with sending data via SPI. After the successful transmission of the first package, the second and further package arrive shifted by one bit. The whole package shifts. Now I fixed the problem by reinitializing the SPI after sending the package, but this is a bad solution to the problem.

I also noticed that after shifting the most significant bit, the MISO bus changes (seen in the video 4 sec and 28 sec). I tried changing CPOL and CPHA, but it did not produce results.

I generate the code on CubeMX. I use the HAL_SPI_Transmit function for maximum simplicity.

Board stm32f030f4p6.

I attach a video demonstrating the displacement, also a project on CubeMX.

0690X00000BufHTQAZ.jpg0690X00000BufHOQAZ.jpg

sample code:

...
#define CS_in_Pin GPIO_PIN_7
#define CS_in_GPIO_Port GPIOA
 
SPI_HandleTypeDef hspi1;
static uint8_t isTransmiteFinish = 0;
...
 
void Func_SPI_Trans()
{
  HAL_StatusTypeDef hal_res;
  static uint16_t data = 1;
  static uint16_t dataSize = 1;
  static int timing = 500;
 
  hal_res = HAL_SPI_Transmit( &hspi1, (uint8_t *)&data , dataSize, timing ); 
  if ( hal_res == HAL_OK )
  {
    isTransmiteFinish = 0;
    int debug = 0;
  }
  if ( hal_res != HAL_OK )
  {
    int debug = 0;
  }
}
 
int main(void)
{
  ...
  MX_GPIO_Init();
  MX_SPI1_Init();
  MX_DMA_Init();
 
    GPIO_PinState cs_pinState = HAL_GPIO_ReadPin( CS_in_GPIO_Port, CS_in_Pin );
    if ( cs_pinState == GPIO_PIN_RESET )
    {
      if ( isTransmiteFinish )
      {
        Func_SPI_Trans();
      }
    }
    if ( cs_pinState == GPIO_PIN_SET )
    {
      isTransmiteFinish = 1;
      int debug = 0;
    }
}

Could you help? What could be the problem?

0 REPLIES 0