cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F767 SPI not sending same data twice.

bradleybare
Associate II
Posted on July 12, 2016 at 05:18

Hello,

Here is my code

static void SPI_EnableMaster(SPI_TypeDef *SPIx,
uint16_t SPI_CLOCKDIV, uint16_t SPI_MODE,
uint16_t SPI_DATA_SIZE, uint16_t SPI_LSBFIRST) 
{
SPI_DATA_SIZE--; 
SPIx->CR2 = (SPI_DATA_SIZE << 
8
) | SPI_CR2_FRXTH;
SPIx->I2SCFGR = 0;
SPIx->CR1 = (SPI_CR1_SPE | SPI_CR1_MSTR | SPI_MODE | SPI_LSBFIRST | 
SPI_CLOCKDIV | SPI_CR1_SSM | SPI_CR1_SSI);
}
static uint8_t SPI_Transfer8bit(SPI_TypeDef *SPIx, uint8_t SPI_DATA)
{
(*(uint8_t *)&(SPIx->DR)) = SPI_DATA;
//send Data
while((SPIx->SR & SPI_SR_RXNE) == 0)
asm('''');
//wait to receive data
return SPIx->DR;
}
static void SPI_WaitTransfer(SPI_TypeDef *SPIx)
{ 
while((SPIx->SR & SPI_SR_BSY) != 0) 
asm(''''); 
}
int main(void)
{
//All GPIO pins are set as alt function except for CS
SPI_EnableClockAPB2(SPI_CLOCK_SPI1);
SPI_EnableMaster(SPI1, SPI_CLOCKDIV_256, SPI_MODE_0, 8, SPI_LSBFIRST_MSB);

while(1)
{
GPIO_ToggleOutput(GPIOA,GPIO_PIN_4); //CS LOW
SPI_Transfer8bit(SPI1,0b10101010);
SPI_Transfer8bit(SPI1,0b10101010);
GPIO_ToggleOutput(GPIOA,GPIO_PIN_4); //CS HIGH
DelayMilli(1);
}
return 1;
}

I am having a problem with the SPI not sending the second data if it is the same. I have pinned it down to only that problem. Basically what is happening is the SPI transfer is never started the second time. So the program gets stuck in the while loop that waits for RXNE to be high. I have noticed though that if I remove the second SPI_Transfer8bit then the program will work fine. And send the same data after looping around. I have no idea what is causing this problem. I though it could be the cache but the peripheral memory area is not cacheable based on what I read. So I came here hoping someone could jump in and help. Thanks for looking.
3 REPLIES 3
Posted on July 12, 2016 at 18:40

I'd probably wait for TXE before writing, and have read DR to clear any hanging RXNE

Does the F7 SPI use a FIFO?

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
slimen
Senior
Posted on July 13, 2016 at 09:44

Hi,

You'll probably want to review SPI examples as you can find under STM32CubeF7: STM32Cube_FW_F7_V1.4.0\Projects\STM32F767ZI-Nucleo\Examples\SPI

You can compare the code there with yours and identify what you missed.

Regards

bradleybare
Associate II
Posted on July 15, 2016 at 17:06

Yes it does use a FIFO. A TXFIFO AND RXFIFO. How can I fix this problem knowing that the transmission register has a fifo?

Thank you a bunch!