cancel
Showing results for 
Search instead for 
Did you mean: 

STM32L011K4+LSM9ds1tr SPI wrong readings

ArturasV
Senior
Posted on January 26, 2017 at 13:31

Hello, we are having problems with communicating to sensor LSM9ds1tr via SPI with sm32l011k4 mcu. The problem is that SPI seems to work good and looking with logic analyzer proves that everything is okey, we are seeing exactly the same thing as mcu reads. We are trying to read the WHO_AM_I register of accelerometer/gyroscope and magnetometer and for some reason we read wrong values compared to the datasheet. According to the data sheet, then reading ACC/Gyro  WHO_AM_I register the value should be 0x68 and then reading Magnetometer WHO_AM_I register the value should be 0x3D. However, when reading these, we always get 0x40 when reading accelerometer/gyro register and we always get 0x38 when reading magnetometer WHO_AM_I register. Could anyone help us to solve the problem and point out what we are doing wrong? I attach the schematics of the sensor, spi initialization code and our reading function + snapshots from logic analyzer. 0690X00000605wbQAA.png0690X0000060698QAA.png0690X000006069DQAQ.png

/*

* @brief: function to configure SPI1 peripheral.

* (1) Set MCU as a master, configure baud rate to fpclk/256

* (2) Slave select output enable

* (3) Enable SPI1 peripheral.

*/

void spi_config(void)

{

SPI1->CR1 = SPI_CR1_MSTR | SPI_CR1_BR; /* (1) */

SPI1->CR2 = SPI_CR2_SSOE; /* (2) */

SPI1->CR1 |= SPI_CR1_SPE; /* (3) */

}

/*

* @brief: function to read data from slave via SPI.

* @params: arg1 - reg_ptr: address of the register which you want to read

* arg2 - b_count: number of bytes to read starting from the address given by reg_ptr.

* (1) Create local variable called index, to follow read byte number

* (2) Chip select goes to low level

* (3) Write value to the SPI Tx buffer

* (4) Wait until data is transmitted and Tx fifo is empty

* (5) Wait until Rx fifo is not empty (means some data is received).

* (6) Read received Data.

* (7) Start a loop with number of iterations equal to arg2.

* (8) Send dummy byte so master generates clock for the communication

* (9) Small delay before setting CS back to high

* (10) Chip select goes to high level

* ----------------------------------------------------------------------------------------------

* @Note: Delay (9) will change when changing mcu clock settings

*/

void spi_read_acc(uint8_t reg_ptr, uint8_t b_count)

{

uint8_t index = 0; /* (1) */

GPIOB->ODR &= ~0x20; /* (2) */

SPI1->DR = reg_ptr; /* (3) */

while(!(SPI1->SR & 0x02)); /* (4) */

while(!(SPI1->SR & 0x01)); /* (5) */

spi_rx_data[index++] = SPI1->DR; /* (6) */

for(; index < b_count + 1; index++) /* (7) */

{

SPI1->DR = 0xFF; /* (8) */

while(!(SPI1->SR & 0x02)); /* (4) */

while(!(SPI1->SR & 0x01)); /* (5) */

spi_rx_data[index] = SPI1->DR; /* (6) */

}

for(index = 0; index < 20; index++); /* (9) */

GPIOB->ODR |= 0x20; /* (10) */

}

&sharpdefine LSM9DS_WHO_AM_I 0x0F

int main(void)

{

hardware init(); // pseudo code

// ...

spi_read_acc(LSM9DS_WHO_AM_I | 0x80, 1);

while(1);

}

#stm32l011k4 #lsm9ds1
15 REPLIES 15
Posted on January 31, 2017 at 22:54

Nick,

Can you see the data on the Logic analayser ?

Look at the original post.

Arth,

Until somebody more knowledgeable from ST chimes in:

I went through publicly available code and all I could find were the same values as in the DS. I also browsed through all the ST MEMS datasheets and compiled the WHO_AM_I responses for various parts (www.efton.sk/STM32/st_mems_whoami.txt) but there is nothing resembling your combination of responses.

What exactly is your hardware? Is it a genuine ST part?

JW

Posted on January 31, 2017 at 21:55

Hi,

Can you see the data on the Logic analayser ?

did you check the CPOL and CPHA bits ?

my code works for the bxCAN embedded peripheral used in the '091.

Posted on January 31, 2017 at 23:16

Wow, when you zoom into the logic analyzer image there is much more detail...

over 55 now, eyesight with focusing is difficult.

I can see on the data sheet, that address 0x1F looks suspiciously like your data...

try reading registers 0x21,0x22,0x23...

your CPOL and CPHA look good.

how long is the data cable ?

what speed are you running, 10khz ? this should be no problem.

where did you buy the chips ?

do you think they could be counterfeit ?

I have been touched by counterfeit product a few times...

Pins 7 and 8 should be low, did you check that ?

what is pin 13 all about ?  no details in this sheet, looks like it needs to go high.

Posted on February 01, 2017 at 15:15

Thanks for the answer jan. We are doing this on on nucleo development board. 

http://www.st.com/en/evaluation-tools/nucleo-l011k4.html

  For accelerometer, we made our own custom board with headers and connected it to the nucleo board with connection wires. Their length is about 10, maybe 15 centimeters. 
Posted on February 01, 2017 at 15:19

Hey marsh.Nick, thanks for your answer. We bought the chips from Mouser so we don't think they could be counterfeit. And yes we checked everything you have said. We are now thinking that maybe we could have damaged the chips when soldering them on the pcb, because today we tried the same software with another accelerometer and that one works perfectly. Anyway new chips are on their way, i'll give you a feedback as soon as we test the new ones. They should reach us this friday/monday.

ArturasV
Senior
Posted on February 08, 2017 at 19:17

Just wanted to thank you guys for your ideas and time. We believe that we may have damaged the chip while soldering these sensors on pcb. Received a new ones yesterday, soldered them on the same pcb and everything is working as expected, we are getting correct values now. We do not think that the part was fake since we bought it from Mouser, so probably it is our fault. Thank you everyone again, regards.