cancel
Showing results for 
Search instead for 
Did you mean: 

RXNE is never set.SPI always read 0

EMEYD.1
Associate

I am trying to use the SPI interface with a SPI ADXL345 sensor and the STM32F072 Nucleo board.I want to make a very basic read operation.Now the read function is blocked in waiting the the RXNE signal.But the RXNE is never set . SPI always read 0. I use IAR here is my codes, I use these functions too write and read SPI. 

  int main()

  {

  USART2_Init();

  SPIx_Init();

  

  while(1)

  {

  adxl_baslat();

  receiveData[0] = readData(0x32) ;

  receiveData[1] = readData(0x33) ;

  receiveData[2] = readData(0x34) ;

  receiveData[3] = readData(0x35) ;

  receiveData[4] = readData(0x36) ;

  receiveData[5] = readData(0x37) ;

  

  x=( receiveData[1] << 8) | (receiveData[0]);

  y=( receiveData[3] << 8) | ( receiveData[2]);

  z=( receiveData[5] << 8) | ( receiveData[4]);

 xg=(float)x*0.0078;

yg=(float)x*0.0078;

zg=(float)x*0.0078;

  sprintf(buffer,"xg: %f yg: %f zg: %f \r\n", xg,yg,zg);

  USART_PutString( buffer);

   }

   } 

   void SPIx_Init()

   {

   

   SPI_InitTypeDef SPI_InitStruct;

  GPIO_InitTypeDef GPIO_InitStruct;

  RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);

  SPI_InitStruct.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_16;

  SPI_InitStruct.SPI_CPHA = SPI_CPHA_2Edge;

  SPI_InitStruct.SPI_CPOL = SPI_CPOL_High;

  SPI_InitStruct.SPI_DataSize = SPI_DataSize_8b;

  SPI_InitStruct.SPI_Direction = SPI_Direction_2Lines_FullDuplex;

  SPI_InitStruct.SPI_FirstBit = SPI_FirstBit_MSB;

  SPI_InitStruct.SPI_Mode = SPI_Mode_Master;

  SPI_InitStruct.SPI_NSS = SPI_NSS_Soft | SPI_NSSInternalSoft_Set;

  SPI_Init(SPI1, &SPI_InitStruct); 

  SPI_Cmd(SPI1, ENABLE);

  SPI_DataSizeConfig(SPI1, ENABLE);

  SPI_RxFIFOThresholdConfig(SPI1, SPI_RxFIFOThreshold_QF);

  // Step 2: Initialize GPIO

  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE); 

  // GPIO pins for MOSI, MISO, and SCK

  GPIO_InitStruct.GPIO_Pin = SPI_PIN_MOSI | SPI_PIN_MISO | SPI_PIN_SCK;

  GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;

  GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;

    

  GPIO_InitStruct.GPIO_Speed = GPIO_Speed_10MHz;

  GPIO_Init(GPIOA, &GPIO_InitStruct);

   

  GPIO_PinAFConfig(GPIOA, GPIO_PinSource5, GPIO_AF_1);

  GPIO_PinAFConfig(GPIOA, GPIO_PinSource6, GPIO_AF_1);

  GPIO_PinAFConfig(GPIOA, GPIO_PinSource7, GPIO_AF_1);

  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE); 

  GPIO_InitStruct.GPIO_Pin = SPI_PIN_SS;

  GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT;

  GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;

  GPIO_InitStruct.GPIO_Speed = GPIO_Speed_10MHz;

   

  GPIO_Init(GPIOB, &GPIO_InitStruct);

   

  

  }

   

  uint8_t sendByte(uint8_t byteToSend)

  {

   timeout = TIMEOUT_TIME;

   while ((SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET)& 

  (timeout!=0))

  {

    timeout--;

   }

  SPI_SendData8(SPI1, byteToSend);

  timeout = TIMEOUT_TIME; 

  while ((SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE)==RESET)&(timeout!=0))

  {

   timeout--;

  }

  return (uint8_t)SPI_ReceiveData8(SPI1);

   }

 void writeData(uint8_t address, uint8_t dataToWrite)

{

   

  DATA2 = address | 0x40;

  GPIO_ResetBits(GPIOB,GPIO_Pin_9);

   sendByte(DATA2);

  sendByte(dataToWrite);

  

   GPIO_SetBits(GPIOB,GPIO_Pin_9);

 }

  

   

uint8_t readData(uint8_t address)

{

   address |= 0x40;

   address |= 0x80;

   GPIO_ResetBits(GPIOB,GPIO_Pin_9);

  sendByte(address);

  tempByte = sendByte(address);

   GPIO_SetBits(GPIOB,GPIO_Pin_9);

   return tempByte; 

}

void adxl_baslat(void)

{

writeData(0x31, 0x01);

writeData(0x2d, 0x00);

writeData(0x2d, 0x08);

}

   

2 REPLIES 2

Observe SCK pin using oscilloscope /LA- are clocks output there?

Read out and check the SPI and relevant GPIO registers content.

Initialize all fields in the init structs.

JW

TDK
Guru
  while ((SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE)==RESET)&(timeout!=0))
  {
   timeout--;
  }

For all the crap that's in HAL, at least they don't do junk like this where it checks for a timeout then just silently ignores when it happens.

Note that if you're observing the SPI->DR register in a debugger, you will reset the RXNE flag before it has a chance to take effect.

If you feel a post has answered your question, please click "Accept as Solution".