AnsweredAssumed Answered

STM32F030 SPI with long time between bytes and wrong output

Question asked by David Martins on Oct 29, 2017
Latest reply on Nov 9, 2017 by Hugues Libotte

Hello, I'm having some problems with the STM32F030 SPI.
I've read enough about this and I know there are some problems .... but I can not find justification for this.

Why does the SPI send 2 bytes and then stop for 17uS (this time can vary with the main clock, which in my case is 8MHz) and then send the next 2 bytes?


What is the reason for this long break?

 

Why does SPI take so long to start sending after I change the status of CSN pin?

I leave here an image of the signals and part of the code that I'm using ...

 

/* SPI1 init function */
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_4;
  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_DISABLE;
  if (HAL_SPI_Init(&hspi1) != HAL_OK)
  {
    _Error_Handler(__FILE__, __LINE__);
  }

}

void HAL_SPI_MspInit(SPI_HandleTypeDef* spiHandle)
{

  GPIO_InitTypeDef GPIO_InitStruct;
  if(spiHandle->Instance==SPI1)
  {
  /* USER CODE BEGIN SPI1_MspInit 0 */

  /* USER CODE END SPI1_MspInit 0 */
    /* SPI1 clock enable */
    __HAL_RCC_SPI1_CLK_ENABLE();
 
    /**SPI1 GPIO Configuration    
    PA5     ------> SPI1_SCK
    PA6     ------> SPI1_MISO
    PA7     ------> SPI1_MOSI
    */
    GPIO_InitStruct.Pin = GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Pull = GPIO_PULLUP;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
    GPIO_InitStruct.Alternate = GPIO_AF0_SPI1;
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

  /* USER CODE BEGIN SPI1_MspInit 1 */

  /* USER CODE END SPI1_MspInit 1 */
  }
}

void HW_SPI_send(uint8_t cmd, uint8_t* rx_buff, uint8_t lenght)
{
    uint8_t tx_buff[lenght + 1];

    memset(tx_buff, 0xFF, lenght + 1);

    tx_buff[0] = cmd;

    HAL_SPI_TransmitReceive(&hspi1, tx_buff, rx_buff, lenght + 1, 5000);
}

int main(void)
{

    /* USER CODE BEGIN 1 */
    uint8_t RX_buff[5];

    /* USER CODE END 1 */

    /* MCU Configuration----------------------------------------------------------*/

    /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
    HAL_Init();

    /* USER CODE BEGIN Init */

    /* USER CODE END Init */

    /* Configure the system clock */
    SystemClock_Config();

    /* USER CODE BEGIN SysInit */

    /* USER CODE END SysInit */

    /* Initialize all configured peripherals */
    MX_GPIO_Init();
    MX_SPI1_Init();

    /* USER CODE BEGIN 2 */
    SET_CSN_PIN;

    HAL_Delay(1000);

    HAL_SPI_MspInit (&hspi1);

    //clean output buffer
    /************************************************************************ SEE NEXT QUESTION ***/
    HAL_SPI_TransmitReceive(&hspi1, (uint8_t*) 0xFF, (uint8_t*) RX_buff, 1, 5000);
    HAL_Delay(100);

    /***************************************************************************** PROBLEM ******/
    CLEAR_CSN_PIN;
    HW_SPI_send(0x0A, RX_buff, 5);
    SET_CSN_PIN;

    (....)
}

 

 

***********************************************************************************

PS: I have one more question. Why does the first byte, after configuring the SPI, send junk? Look at this example. I send "0xFF", but the MCU sends 0xD1...

 

Outcomes