cancel
Showing results for 
Search instead for 
Did you mean: 

SPI receive

hdemi.1
Associate III

SPI receive funtion not working correctly. Only one time is working, where is my mistake ?

thanks

void SPITransReceive(SPI_TypeDef* SPIx,uint8_t data,uint8_t *pbuff,uint8_t dataSize){

while(dataSize > 0){

 while((SPIx->SR & SPI_I2S_FLAG_TXE) == RESET) {} ;

//SPI_SendData8(SPIx,data);

//SPIx->DR = data ;

*(__IO uint8_t *)(&SPIx->DR) = data;//16 bit type cast ;

 while((SPIx->SR & SPI_I2S_FLAG_RXNE) == RESET) {} ;

SPI_I2S_ClearFlag(SPIx,SPI_I2S_FLAG_RXNE);

 *pbuff = *(__IO uint8_t *)(&SPIx->DR);

++pbuff ;

--dataSize ;

}

}

6 REPLIES 6
S.Ma
Principal

Have you tried 4 wire bidir mode with mosi pin unassigned (spi signal staying withing the chip)? Also, usually, end of communication should only rely on rxne flag. Writing to DR generates the number of sck bit clocks defined in spi config registers.

Which STM32?

> ​Only one time is working

​How do you know? How do you observe it?

JW​

Firstly I read device id correctly with spi than i read status register, device send status info (0x02)

but receive function turn 0x00 or 0xff

S.Ma
Principal

You need to write dummy bytr to.the DR to generate the sck clock pulses, then wait for RXNE to read.

I did but not working correctly

gbm
Lead III
  1. To receive anything you must send something. The basic SPI function should SEND and receive a byte. You cannot receive without sending. Send a dummy value like 0 or 0xff if the value is not needed by slave.
  2. How is the NSS/CS line handled? With STM32 in most cases you must do it in software as GPIO, not via SPI hardware control.
  3. This doesn't work and is not needed, the flag is cleared by reading the data register.
  4. If your MCU has the famous FRXTH bit in SPI control register, it must be set for 8-bit frames.
My STM32 stuff on github - compact USB device stack and more: https://github.com/gbm-ii/gbmUSBdevice