AnsweredAssumed Answered

STM32L053 Nucleo - SPI Issue

Question asked by Mat on Oct 15, 2014
Latest reply on Oct 16, 2014 by Mat
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.CRCPolynomial     = 7;
  hspi2.Init.FirstBit          = SPI_FIRSTBIT_MSB;

void HAL_SPI_MspInit(SPI_HandleTypeDef* hspi)
  GPIO_InitTypeDef GPIO_InitStruct;
    /* Peripheral clock 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!