cancel
Showing results for 
Search instead for 
Did you mean: 

stm32f4 discovery lis3dsh communication

wersalec
Associate
Posted on November 09, 2014 at 23:41

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?
3 REPLIES 3
Konstantin Maslennikov
Associate II
Posted on February 23, 2018 at 18:10

Can you run just blink on PA6?

Konstantin Maslennikov
Associate II
Posted on February 23, 2018 at 18:21

My code with StdPeriph:

#include 'stm32f4xx.h'

#include 'main.h'

#include 'spi.h'

#define LIS3DSH

#define LIS3DSH_OUT_X_L 0x28

#define LIS3DSH_OUT_X_H 0x29

#define LIS3DSH_OUT_Y_L 0x2a

#define LIS3DSH_OUT_Y_H 0x2b

#define LIS3DSH_OUT_Z_L 0x2c

#define LIS3DSH_OUT_Z_D 0x2c

#define HYSTERESIS 6000

int16_t AxisX = 0;

int16_t AxisY = 0;

//int16_t AxisZ = 0;

uint8_t temp_x_l = 0;

uint8_t temp_x_h = 0;

uint8_t temp_y_l = 0;

uint8_t temp_y_h = 0;

uint8_t writeData(uint8_t data)

{

while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_BSY) == SET) ;

SPI_I2S_SendData(SPI1, data);

while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_BSY) == SET) ;

return SPI_I2S_ReceiveData(SPI1);

}

int getReg(uint8_t address)

{

uint8_t data=0;

address|=(1<<7);

GPIO_ResetBits(GPIOE,GPIO_Pin_3);

writeData(address);

data = writeData(0x00);

GPIO_SetBits(GPIOE,GPIO_Pin_3);

return data;

}

void setReg(uint8_t address, uint8_t value)

{

GPIO_ResetBits(GPIOE,GPIO_Pin_3);

writeData(address);

writeData(value);

GPIO_SetBits(GPIOE,GPIO_Pin_3);

}

int main(void)

{

Led_init();

SPI_init();

// getReg(0x0f); // get ID

setReg(0x20, 0x97);

setReg(0x23, 0xA0);

while(1)

{

temp_x_h = getReg(LIS3DSH_OUT_X_H);

temp_x_l = getReg(LIS3DSH_OUT_X_L);

temp_y_h = getReg(LIS3DSH_OUT_Y_H);

temp_y_l = getReg(LIS3DSH_OUT_Y_L);

AxisX = (temp_x_h) << 8 | temp_x_l;

AxisY = (temp_y_h) << 8 | temp_y_l;

if (AxisX > HYSTERESIS)

{ R_ON(); }

else if (AxisX < -HYSTERESIS)

{ G_ON(); }

else

{ R_OFF();

G_OFF(); }

if (AxisY > HYSTERESIS)

{ Y_ON(); }

else if (AxisY < -HYSTERESIS)

{ B_ON(); }

else

{ B_OFF();

Y_OFF(); }

}

}

AvaTar
Lead
Posted on February 23, 2018 at 18:24

I would try the MEMS example from the F4 disco firmware package.

I used to start my project with working examples, and added my code then later.

And the example would be a good functional test.