cancel
Showing results for 
Search instead for 
Did you mean: 

stm32f0 SPI does not receive data

hdemi.1
Associate III

When data size equal to 3 rx buffer last element is true only others are not. I expected all of them is true data. Why it is working like that ? I examine the result on debug mode

static void Init_SPI2(void)
{
   SPI_InitTypeDef  SPI_InitStructure;
 
	  SPI_InitStructure.SPI_Direction         = SPI_Direction_2Lines_FullDuplex;
    SPI_InitStructure.SPI_Mode              = SPI_Mode_Master;
    SPI_InitStructure.SPI_DataSize          = SPI_DataSize_8b;
    SPI_InitStructure.SPI_CPOL              = SPI_CPOL_Low ;
    SPI_InitStructure.SPI_CPHA              = SPI_CPHA_1Edge;
    SPI_InitStructure.SPI_NSS               = SPI_NSS_Soft| SPI_NSSInternalSoft_Set;
    SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256;
    SPI_InitStructure.SPI_FirstBit          = SPI_FirstBit_MSB;
    SPI_InitStructure.SPI_CRCPolynomial     = 7;
 
    SPI_Init(flashSPI,&SPI_InitStructure);
    SPI_Cmd(flashSPI,ENABLE) ;
}
 
 
void SPITransReceive(SPI_TypeDef* SPIx,uint8_t data,uint8_t *pbuff,uint8_t dataSize){
 
    //writeCsLow();
 
		while(dataSize > 0){
			
		  while((SPIx->SR & SPI_I2S_FLAG_TXE) == RESET) {} ;
				
				//SPI_SendData8(SPIx,data);
			//SPIx->DR = data ;
			*(__IO uint8_t *)(&SPIx->DR) = data;
 
		  while((SPIx->SR & SPI_I2S_FLAG_RXNE) == RESET) {} ;
			SPI_I2S_ClearFlag(SPIx,SPI_I2S_FLAG_RXNE);		
 
		  *pbuff = *(__IO uint8_t *)(&SPIx->DR);
				
			++pbuff ;
			--dataSize ;
		
			}
		
		 //writeCsHigh();
 
 
}

4 REPLIES 4
S.Ma
Principal

In SPI Master mode, write DR when TXE is set to start the transaction in 4 wire mode, it is used also to generate the SCK pulses.

Then once RXNE is set, the bit shifting on the bus has ended.

Fix the above code and maybe things will get better.

Is my code not like that ? What do you think is the wrong part ? thanks

S.Ma
Principal

Have you made sure the GPIO are in alternate function mode?

Have you tried the code without clearing the RXNE flag (reading the DR should be enough) ?

KnarfB
Principal III

RXNE and other flags in SPI status register are read-only, don't try to clear them.

Otherwise, your code looks good if RESET is defined to ???

Tx and Rx happen simultaneously on the wires. Many peripherals need a command/address first and answer in the cycles following that. What's your protocol look like?

Hooking-up a cheap USB logic analayzer is a life saver for such kind of analysis.

hth

KnarfB