cancel
Showing results for 
Search instead for 
Did you mean: 

SPI transmit problem

dominik23
Associate
Posted on June 22, 2015 at 16:08

I am using a STM32F103RBT6 and I'm trying to use SPI in slave fullduplex mode with generated code from CubeMX.

The master is sending a predefined data package and I try to respond. I can receive the whole data package correctly, if I'm not sending anything back during the data transfer. As soon as I begin to answer (I answer each byte), I can receive two byte, but after that the received data begins to make no sense. It looks like sending something isinterfering with receiving further data. I tried using DMA or interrupt for transmit and receive, but I had the same issue. I'm guessing that there is somehow a problem with my initialization of the SPI module, but I can't see where and I'm only using the generated CubeMX code

/* SPI1 init function */
void
MX_SPI1_Init(
void
)
{
hspi1.Instance = SPI1;
hspi1.Init.Mode = SPI_MODE_SLAVE;
hspi1.Init.Direction = SPI_DIRECTION_2LINES;
hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
hspi1.Init.CLKPolarity = SPI_POLARITY_HIGH;
hspi1.Init.CLKPhase = SPI_PHASE_2EDGE;
hspi1.Init.NSS = SPI_NSS_SOFT;
hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_256;
hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
hspi1.Init.TIMode = SPI_TIMODE_DISABLED;
hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLED;
hspi1.Init.CRCPolynomial = 7;
HAL_SPI_Init(&hspi1);
}

#dma #spi
3 REPLIES 3
chrif
Associate II
Posted on June 24, 2015 at 15:42

Hi,

Make sure that the Master has same data size as the slave. But, you can send the initialisation of master to help you more.

dominik23
Associate
Posted on June 26, 2015 at 08:38

I already checked all parameters in the initialization.

But after searching for a while I found the problem. The master is providing a 100khz clock frequency for the SPI communication. As stated somewhere in the comments of the CubeMX generated code, this routine is used to init low level hardware:


void
HAL_SPI_MspInit(SPI_HandleTypeDef* hspi)

{


GPIO_InitTypeDef GPIO_InitStruct;

if
(hspi->Instance==SPI1)

{

/* USER CODE BEGIN SPI1_MspInit 0 */


/* USER CODE END SPI1_MspInit 0 */

/* Peripheral clock enable */

__SPI1_CLK_ENABLE();


/**SPI1 GPIO Configuration 

PA5 ------> SPI1_SCK

PA6 ------> SPI1_MISO

PA7 ------> SPI1_MOSI 

*/

GPIO_InitStruct.Pin = GPIO_PIN_5|GPIO_PIN_7;

GPIO_InitStruct.Mode = GPIO_MODE_INPUT;

GPIO_InitStruct.Pull = GPIO_NOPULL;

HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);


GPIO_InitStruct.Pin = GPIO_PIN_6;

GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;

GPIO_InitStruct.Speed = GPIO_SPEED_MEDIUM;

HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);


/* Peripheral interrupt init*/

HAL_NVIC_SetPriority(SPI1_IRQn, 0, 0);

HAL_NVIC_EnableIRQ(SPI1_IRQn);

/* USER CODE BEGIN SPI1_MspInit 1 */


/* USER CODE END SPI1_MspInit 1 */

}


}

In line 25 the speed for the GPIO pins is set. In the default code from CubeMX was this line:

GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;

I don't quiet understand why, but after changing the speed to

GPIO_SPEED_MEDIUM or

GPIO_SPEED_LOW

everything worked fine.

However I found no possibility to set this during the SPI configuration in CubeMX.
chrif
Associate II
Posted on June 26, 2015 at 11:59

Hello,

If you want to configure the GPIO speed, in the configuration window, click on GPIO, you will have the Pin configuration, choose the pin to configure and choose from three speeds in Maximum output speed. So, the line 25 will be changed to 

GPIO_InitStruct.Speed = GPIO_SPEED_LOW; 

Regards