cancel
Showing results for 
Search instead for 
Did you mean: 

STM32CubeF0 - SPI functions problem

yilmazkircicek
Associate III
Posted on July 13, 2016 at 09:27

Hi All,

We are using STM32F072B and faced with SPI comm. problem.

I have seen that Cube SPI functions have some bugs. HAL_SPI_Receive does not reading first three byte and loosing last three byte. I have seen several bug reports about Cube SPI functions but could not find a similar one. Also tried HAL_SPI_TransmitReceive function and stiation is the same. Logic analyzer shows that valid data avaliable on bus but code does not read it.

Error is related with total number of byte transactions. For eg. it is OK for 1 byte TX and three byte RX. But not for 4 bytes of TX and 1 byte of RX.

Is there any workarounds or bug fix ?

Regards.

0690X00000602xaQAA.jpg

0690X000006036gQAA.jpg

#stm32cubef0-spi
18 REPLIES 18
yilmazkircicek
Associate III
Posted on August 15, 2016 at 13:42

Why no one care about this ?

qwer.asdf
Senior
Posted on August 15, 2016 at 15:03

Can you show your SPI initialization code? The more code you show the more chance somebody can find the problem.

yilmazkircicek
Associate III
Posted on August 15, 2016 at 15:14

Hi,

I am using CubeMX for initialization codes. And scope (logic analyser) view seem as expected. I can also read external Flash JEDEC ID accurately. But sometimes it returns 00 or FF for single byte read operation.

SPI_HandleTypeDef hspi1;
/* 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_32;
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();
}
}
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 */
/* Peripheral 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_NOPULL;
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 */
}
}

Posted on August 15, 2016 at 15:16

Why no one care about this ?

This is primarily a user forum, consider ones own level of participation when wondering about that of others. Cube/HAL and Cortex-M0 issues really don't interest me, and the pool here is rather shallow.

SPI is symmetrical, on the F0 there is also a FIFO, doing separate transmit and receive phases you're likely to see the kind of issues described.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
qwer.asdf
Senior
Posted on August 15, 2016 at 15:18

...

hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
 ...

Are you sure the data size is 8 bit and not 16? I mean, look at your logic analyzer's output.
yilmazkircicek
Associate III
Posted on August 15, 2016 at 15:23

Yes, sure.

I have checked several frames from the logic analyser. 

Electrical data is correct every time but code does not read it correctly. 

I can see the appropriate data on bus but HAL does not return correct datas. 

yilmazkircicek
Associate III
Posted on August 15, 2016 at 15:27

Today I am strugling to write only single byte of data. (0x59) And reading it back. 

All analyser data seems OK. 

But it returns 00 when I tried to read even with the correct data on bus. 

Then I tried to read 5 bytes from the same addres result is;

0x00 0x59 0x00 0x59 0xXX

Weird but true

I am using HAL functions. 

yilmazkircicek
Associate III
Posted on August 15, 2016 at 15:29

This code allways return correct data;

void Ext_Flash_Read_JEDEC_ID(Ext_Flash_JEDEC_ID *pData)
{ 
EXT_FLASH_CS_Low(); 
SPI_Tx_Buf[0] = CMD_READ_JEDEC_ID;
HAL_SPI_Transmit(&hspi1, (uint8_t*)SPI_Tx_Buf, 1, 100);
HAL_SPI_Receive(&hspi1, (uint8_t*)SPI_Rx_Buf, 3, 100);
EXT_FLASH_CS_High(); 
(*pData).Manufacturer_ID = SPI_Rx_Buf[0];
(*pData).Device_Type = SPI_Rx_Buf[1];
(*pData).Device_ID = SPI_Rx_Buf[2];
}

yilmazkircicek
Associate III
Posted on August 15, 2016 at 15:31

But these are not

uint8_t Ext_Flash_Read_Byte(unsigned int address)
{
EXT_FLASH_CS_Low(); 
SPI_Tx_Buf[0] = CMD_READ;
SPI_Tx_Buf[1] = ((address >> 16) & 0xFF);
SPI_Tx_Buf[2] = ((address >> 8) & 0xFF);
SPI_Tx_Buf[3] = (address & 0xFF);
HAL_SPI_Transmit(&hspi1, (uint8_t*)SPI_Tx_Buf, 4, 100);
HAL_SPI_Receive(&hspi1, (uint8_t*)SPI_Rx_Buf, 1, 100);
EXT_FLASH_CS_High(); 
return SPI_Rx_Buf[0];
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void Ext_Flash_Read(unsigned int address, uint8_t*pData, uint16_t n) 
{
EXT_FLASH_CS_Low(); 
SPI_Tx_Buf[0] = CMD_READ;
SPI_Tx_Buf[1] = ((address >> 16) & 0xFF);
SPI_Tx_Buf[2] = ((address >> 8) & 0xFF);
SPI_Tx_Buf[3] = (address & 0xFF);
HAL_SPI_Transmit(&hspi1, (uint8_t*)SPI_Tx_Buf, 4, 100);
HAL_SPI_Receive(&hspi1, (uint8_t*)pData, n, 100);
EXT_FLASH_CS_High(); 
}