cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F107 and mems LIS3DH spi data exchange problems

danashevskiy
Associate II
Posted on June 10, 2016 at 13:29

I'm trying to configure LIS3DH chip via SPI interface.

Here is my code:

void init(void)
{ 
SPI_InitTypeDef SPI_InitStructure;
// SPI configuration 
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
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_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_8; //4;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStructure.SPI_CRCPolynomial = 7;
SPI_Init(SPI_INT, &SPI_InitStructure);
// Enable SPI 
SPI_Cmd(SPI_INT, ENABLE);
}
void set_up(void)
{
uint16_t buf = 0, msg = 0;
msg = READ + DEVICE_TYPE_REG;
//msg = 0x80 + 0x0F;
LIS3DHTR_CHIP_SELECT();
while (1) {
while (SPI_I2S_GetFlagStatus(SPI_INT, SPI_I2S_FLAG_BSY) == SET);
SPI_I2S_SendData(SPI_INT, msg);
while (SPI_I2S_GetFlagStatus(SPI_INT, SPI_I2S_FLAG_BSY) == SET);
if (SPI_I2S_GetFlagStatus(SPI_INT, SPI_I2S_FLAG_RXNE) == SET) {
buf = SPI_I2S_ReceiveData(SPI_INT);
if (buf == 51)
break;
}
}
LIS3DHTR_CHIP_DESELECT();
}

Problem: the lis3dh register with address 0x0F keeps constant 51 (type of device of something). When i send request to read this register (reg address plus set MSB bit) via spi interface stm32f107 spi RX register gives me 0xFF at first. And only on second iteration of cycle (while(1)) i get true This problem appears on every my request to lis3dh chip. Is it normal or have i missed something? #stm32f107 #lis3dh #mems
0 REPLIES 0