cancel
Showing results for 
Search instead for 
Did you mean: 

stm32f4 discovery lis3dsh communication problem

wersalec
Associate
Posted on November 10, 2014 at 00:19

Hi!

I have stm32f4 discovery board MB997C.

I`m trying to communicate with LIS3DSH accelerometer. I have a problem - MISO line (port PA6) is always high.

I`m just trying to get the value of WHO_AM_I register and as response I always get 0xFF. Here is my configuration code:

 

#define CS_HIGH GPIO_SetBits(GPIOE, GPIO_Pin_3)

#define CS_LOW  GPIO_ResetBits(GPIOE, GPIO_Pin_3)

GPIO_InitTypeDef GPIOA_InitStructure;

GPIO_InitTypeDef GPIOE_InitStructure;

NVIC_InitTypeDef NVIC_InitStructure;

SPI_InitTypeDef  SPI_InitStructure;

uint8_t receivedByte;

unsigned int TransactionFlag = 0x0000;

unsigned int TransmitCounter = 0x0000;

void main(void)

{

  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);

  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE);

  RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);

 

 

  GPIOA_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;

  GPIOA_InitStructure.GPIO_Mode = GPIO_Mode_AF;

  GPIOA_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

  GPIOA_InitStructure.GPIO_OType = GPIO_OType_PP;

  GPIOA_InitStructure.GPIO_PuPd  = GPIO_PuPd_NOPULL;

 

  GPIO_Init(GPIOA, &GPIOA_InitStructure);

 

 

  GPIO_PinAFConfig(GPIOA,GPIO_PinSource5,GPIO_AF_SPI1);

  GPIO_PinAFConfig(GPIOA,GPIO_PinSource6,GPIO_AF_SPI1);

  GPIO_PinAFConfig(GPIOA,GPIO_PinSource7,GPIO_AF_SPI1);

 

  GPIOE_InitStructure.GPIO_Pin = GPIO_Pin_3;

  GPIOE_InitStructure.GPIO_Mode = GPIO_Mode_OUT;

  GPIOE_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

  GPIOE_InitStructure.GPIO_OType = GPIO_OType_PP;

  GPIOE_InitStructure.GPIO_PuPd  = GPIO_PuPd_UP;

  GPIO_Init(GPIOE, &GPIOE_InitStructure);

 

  CS_HIGH;

 

  SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;

  SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;

  SPI_InitStructure.SPI_CPOL = SPI_CPOL_High;

  SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;

  SPI_InitStructure.SPI_NSS = SPI_NSS_Soft | SPI_NSSInternalSoft_Set;

  SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_64;

  SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;

  SPI_InitStructure.SPI_CRCPolynomial = 7;

  SPI_InitStructure.SPI_Mode = SPI_Mode_Master;

  SPI_Init(SPI1, &SPI_InitStructure);

 

  NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);

  NVIC_InitStructure.NVIC_IRQChannel = SPI1_IRQn;

  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;

  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;

  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

  NVIC_Init(&NVIC_InitStructure);

 

  SPI_Cmd(SPI1, ENABLE);

//  SPI_I2S_ITConfig(SPI1, SPI_I2S_IT_RXNE, ENABLE);

//  SPI_I2S_ITConfig(SPI1, SPI_I2S_IT_TXE, ENABLE);

 

  while(1)

  {

      unsigned long i;

      CS_LOW;

      for(i = 0; i < 10; i++);

     

      SPI_I2S_SendData(SPI1, 0x8F);

      //Wait for transmission to complete

      while (!SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE));

      //Wait for received data to complete

      while (!SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE));

      //Wait for SPI to be ready

//      while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_BSY));

     

      receivedByte = SPI_I2S_ReceiveData(SPI1);

     

      SPI_I2S_SendData(SPI1, 0x00);

      //Wait for transmission to complete

      while (!SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE));

      //Wait for received data to complete

      while (!SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE));

     

      receivedByte = SPI_I2S_ReceiveData(SPI1);

     

      //Wait for transmission to complete

//      while (!SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE));

      //Wait for SPI to be ready

//      while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_BSY));

     

      for(i = 0; i < 10; i++);

      CS_HIGH;

      for(i = 0; i < 100; i++);

  }

}

 

Help me please, what am i doing wrong?

0 REPLIES 0