cancel
Showing results for 
Search instead for 
Did you mean: 

Stm32f103rbt6 spi stm32cubeide 1.14 optimization

Hello

I have encountered a problem that when I change the optimization level from "None" to "Optimize for speed" or even "O1", the spi is not working as expected. It gives me all zero in reading mode. Any idea where could be the problem?

5 REPLIES 5

> spi is not working as expected.

How do you use it, exactly?

> It gives me all zero in reading mode.

Do you use DMA? Tag the buffer "volatile".

JW

@waclawek.jan Thank you for your follow Up. I have a sensor which emit a rising data ready signal and this has been tied to external interrupt. In the external interrupt routine, I read 27 bytes of data in a blocking mode way(not using dma). Moreover the program has been built on top of HAL  library f1 1.8.5. I wonder if it is possible at all to trigger spi dma data transfer with external gpio interrupt?

> I read 27 bytes of data in a blocking mode way(not using dma).

Show. (I don't use Cube/HAL but others here maybe can spot something, if the problem is there. It may be elsewhere, see following lines).

Also tell us, how do you check the received data.

Also, verify using oscilloscope/LA, that MISO is not stuck low during the transfer.

Also, read out and check relevant GPIO and AFIO registers, that they are set properly for AF/SPI

JW

 

 

@waclawek.jan Thank you for your response. It seems my question is a bit misleading and incomplete. So to complete my question, I have two SPIs. One is a full duplex master and the other one is a full duplex slave. When the sensor transmit data ready signal, the spi transmit start in external interrupt routine and put data into a circular buffer.

 

 

 

void EXTI1_IRQHandler(void)
{
  /* USER CODE BEGIN EXTI1_IRQn 0 */
	uint8_t pDatar[27],pDatat[27];
	HAL_StatusTypeDef spiTransCheck = HAL_SPI_TransmitReceive(&hspi1, pDatat,pDatar, 27);
	if (spiTransCheck == HAL_OK)
		circularbufput(circbuf,pDatar);
	else
		printf("Oh no SPi transfer failed")
  /* USER CODE END EXTI1_IRQn 0 */
  HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_1);
  /* USER CODE BEGIN EXTI1_IRQn 1 */

  /* USER CODE END EXTI1_IRQn 1 */
}

 

 

This works in general. If I change

 

 

HAL_SPI_TransmitReceive(&hspi1, pDatat,pDatar, 27,100);

 

 

to

 

 

HAL_SPI_TransmitReceive_DMA(&hspi1, pDatat,pDatar, 27);

 

 

It fails and jump to HardFault_Handler. Sad.

Then when data is processed, the first MCU which is in contact with the sensor sends a data ready pulse to the second MCU and says, hey master, processed data is ready, send clock to get the data. This is in the main loop of the program

 

 

HAL_SPI_TransmitReceive_DMA(&hspi2, pTxData, pRxData, 30);
HAL_GPIO_WritePin(DRDY_GPIO_Port, DRDY_Pin, GPIO_PIN_SET);
HAL_GPIO_WritePin(DRDY_GPIO_Port, DRDY_Pin, GPIO_PIN_RESET);

 

 

What I suspect the most is that since cube generate dma interrupt priority to be of highest priority (0), then if a simultaneous interrupt and DMA request come in, The priority would have been given to DMA not external interrupt

This is the full story.

As I've said, I don't use Cube/HAL so can't help with issues there.

JW