cancel
Showing results for 
Search instead for 
Did you mean: 

STM32L053 Nucleo - SPI Issue

matteo239955
Associate III
Posted on October 15, 2014 at 12:03

Hi All,

I'm trying to set-up a simple SPI communication between a STM32L0 Nucleo board and a slave device.

I made the SPI and PINs configuration using the CubeMX tool and seems to work pretty well but the SPI has some problem:

I'm trying to read a register that contains the value 0x21. I can see on my logic analyzer that the byte running on the bus contains the correct value, but the HAL_SPI_Receive function returns 0x20 as received value (as you can see in the attached image).

I've tryed to manually check the SPI initialization but it seems correct...

Here is the Cube-MX initialization:

SPI_HandleTypeDef hspi2;

/* SPI2 init function */

void MX_SPI2_Init(void)

{

  hspi2.Instance = SPI2;

  hspi2.Init.Mode = SPI_MODE_MASTER;

  hspi2.Init.Direction = SPI_DIRECTION_2LINES;

  hspi2.Init.DataSize = SPI_DATASIZE_8BIT;

  hspi2.Init.NSS = SPI_NSS_SOFT;

  hspi2.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_8;

  hspi2.Init.TIMode = SPI_TIMODE_DISABLED;

  hspi2.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLED;

  hspi2.Init.CRCPolynomial     = 7;

  hspi2.Init.FirstBit          = SPI_FIRSTBIT_MSB;

  HAL_SPI_Init(&hspi2);

}

void HAL_SPI_MspInit(SPI_HandleTypeDef* hspi)

{

  GPIO_InitTypeDef GPIO_InitStruct;

  if(hspi->Instance==SPI2)

  {

    /* Peripheral clock enable */

    __SPI2_CLK_ENABLE();

  

    /**SPI2 GPIO Configuration    

    PB13     ------> SPI2_SCK

    PB14     ------> SPI2_MISO

    PB15     ------> SPI2_MOSI 

    */

    GPIO_InitStruct.Pin = GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15;

    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;

    GPIO_InitStruct.Pull = GPIO_NOPULL;

    GPIO_InitStruct.Speed = GPIO_SPEED_LOW;

    GPIO_InitStruct.Alternate = GPIO_AF0_SPI2;

    HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

  }

}

The reading function I'm using is HAL_SPI_Transmit(&hspi2, dataBuffer, dataLength, 1000); where dataBuffer is a uint8_t array and dataLength is set to 1.

The NSS is manually handled and it works correctly (checked with the oscilloscope).

Any ideas?

Thank you!

Mat

#stm32cube-stm32l0xx #stm32l0
2 REPLIES 2
matteo239955
Associate III
Posted on October 16, 2014 at 15:28

Thanks waclawek.jan for your answer!

I think I foud the problem... it seems like that the pin speed was the cause of the problem.

Setting the speed to GPIO_SPEED_HIGH instead of the default GPIO_SPEED_LOW the problem seems to be disappeard.

Thank you for your time!

Mat