AnsweredAssumed Answered

STM32F767 SPI not sending same data twice.

Question asked by Bare.Bradley on Jul 12, 2016
Latest reply on Jul 15, 2016 by Bare.Bradley
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.

Outcomes