cancel
Showing results for 
Search instead for 
Did you mean: 

SPI RXNE stays to 0

Mich1
Associate III

Hello,

I have a project configured by cubeMX with a STM32L476 nucleo board.

I have the following SPI configuration function:

/* SPI2 init function */
void MX_SPI2_Init(void)
{
  LL_SPI_InitTypeDef SPI_InitStruct = {0};
 
  LL_GPIO_InitTypeDef GPIO_InitStruct = {0};
  /* Peripheral clock enable */
  LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_SPI2);
  
  LL_AHB2_GRP1_EnableClock(LL_AHB2_GRP1_PERIPH_GPIOB);
  /**SPI2 GPIO Configuration  
  PB13   ------> SPI2_SCK
  PB14   ------> SPI2_MISO
  PB15   ------> SPI2_MOSI 
  */
  GPIO_InitStruct.Pin = LL_GPIO_PIN_13|LL_GPIO_PIN_14|LL_GPIO_PIN_15;
  GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE;
  GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_HIGH;
  GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
  GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;
  GPIO_InitStruct.Alternate = LL_GPIO_AF_5;
  LL_GPIO_Init(GPIOB, &GPIO_InitStruct);
 
  SPI_InitStruct.TransferDirection = LL_SPI_FULL_DUPLEX;
  SPI_InitStruct.Mode = LL_SPI_MODE_MASTER;
  SPI_InitStruct.DataWidth = LL_SPI_DATAWIDTH_8BIT;
  SPI_InitStruct.ClockPolarity = LL_SPI_POLARITY_HIGH;
  SPI_InitStruct.ClockPhase = LL_SPI_PHASE_2EDGE;
  SPI_InitStruct.NSS = LL_SPI_NSS_SOFT;
  SPI_InitStruct.BaudRate = LL_SPI_BAUDRATEPRESCALER_DIV8;
  SPI_InitStruct.BitOrder = LL_SPI_MSB_FIRST;
  SPI_InitStruct.CRCCalculation = LL_SPI_CRCCALCULATION_DISABLE;
  SPI_InitStruct.CRCPoly = 7;
  LL_SPI_Init(SPI2, &SPI_InitStruct);
  LL_SPI_SetStandard(SPI2, LL_SPI_PROTOCOL_MOTOROLA);
  LL_SPI_DisableNSSPulseMgt(SPI2);
}

Then I activate the SPI:

LL_SPI_Enable(SPI2);

and I try to send and read 1 byte:

char spi_read_reg_spi2_withadr(unsigned char adr)
{
	uint8_t tmp, tmp1, tmp3;
 
 
    while ((LL_SPI_IsActiveFlag_BSY(SPI2)));
 
 
    /* Send the adr */
    LL_SPI_TransmitData8(SPI2, adr);
 
   while (!LL_SPI_IsActiveFlag_TXE(SPI2));   // wait while not empty
 
   while (LL_SPI_IsActiveFlag_BSY(SPI2));   // wait while busy,
 
   while (!LL_SPI_IsActiveFlag_RXNE(SPI2));   // wait rx data
 
   //now byte should be ready
   tmp = LL_SPI_ReceiveData8(SPI2);
   
   return (tmp);
}

but the RXNE bit stays to 0 so I never receive a byte despite I can see the correct byte on the MOSI pin...

Do you have any suggestion?

Best regards

Mich

2 REPLIES 2
Mich1
Associate III

I modify the code as following:

char spi_read_reg_spi2_withadr(unsigned char adr)
{
	uint8_t tmp3[16];
    uint32_t n = 0, i = 0;
 
 
    for (i=0; i < 8; i++)
    {
        /* Check TXE flag to transmit data */
        while ( !LL_SPI_IsActiveFlag_TXE(SPI2));
        while (LL_SPI_IsActiveFlag_BSY(SPI2));   // wait while busy,
 
        /* Transmit 8bit Data */
        LL_GPIO_SetOutputPin(TESTA11_GPIO_Port, TESTA11_Pin);
        LL_SPI_TransmitData8(SPI2, adr);
        LL_GPIO_ResetOutputPin(TESTA11_GPIO_Port, TESTA11_Pin);
        
        /* Check RXE flag */
        if(LL_SPI_IsActiveFlag_RXNE(SPI2))
        {
            LL_GPIO_SetOutputPin(TESTA11_GPIO_Port, TESTA11_Pin);
            /* Receive 8bit Data */
            tmp3[n++] = LL_SPI_ReceiveData8(SPI2);
            LL_GPIO_ResetOutputPin(TESTA11_GPIO_Port, TESTA11_Pin);
        }
    }
   
   return (tmp3[0]);
}

and I can see that the RXNE flag is set only after the 3rd TX:

0690X000006CsYoQAK.png

Why must I send 3 bytes before having 1 RX when the SPI is configured as 8 bit data width?

Mich1
Associate III

issue was linked to the FIFO Threshold. It is ok now.