MPU9250 gives garbage when spiExchange is used but not when spiSend

Discussions and support about ChibiOS/HAL, the MCU Hardware Abstraction Layer.
KalRal
Posts: 4
Joined: Tue Feb 12, 2019 9:08 pm
Has thanked: 1 time

MPU9250 gives garbage when spiExchange is used but not when spiSend

Postby KalRal » Sat Jun 01, 2019 9:21 pm

Hi,

I am using MPU9250 to become familiar with Accelerometers and Gyroscope and eventually apply that knowledge by implementing Sensor fusion. I am using SPI to communicate with that chip.

Strangely I noticed that when

Code: Select all

spiExchange
is used to get the data from the chip, it returns garbage but if I use

Code: Select all

spiSend
and

Code: Select all

spiReceive
I am getting correct data.

If I send and receive only one byte

Code: Select all

 spiExchange
also works. It gives garbage when I use

Code: Select all

spiExchange
to receive multiple bytes.

Here is a code that works with

Code: Select all

spiExcange
. It just reads the

Code: Select all

WHO_AM_I
register of the board.

Code: Select all

spiAcquireBus(&SPID2);
spiStart(&SPID2, &spiConfig);
spiSelect(&SPID2);
spiExchange(&SPID2, 2, &txbuf, &rxbuf);
chprintf(chp, "Sending data over SPI %x\n", txbuf[0]);
chprintf(chp, "Second byte : %x\n", txbuf[1]);
chprintf(chp, "Received data over SPI %x\n", rxbuf[1]);
spiUnselect(&SPID2);
spiReleaseBus(&SPID2);
chThdSleepMilliseconds(10);


The above code works as I am just reading one byte, but If i want to get Accelerometer data, then I should read 6 bytes and it only works If I use

Code: Select all

spiSend
.

Has anybody worked with this chip(MPU9250) and will I get any advantages if I switch to

Code: Select all

spiExchange
?
If further code is needed, I can share it.
Please let me know whether I need to spend time on this or I can let it go by thinking that it might be MPU9250s fault?

User avatar
Giovanni
Site Admin
Posts: 11860
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 524 times
Been thanked: 436 times
Contact:

Re: MPU9250 gives garbage when spiExchange is used but not when spiSend

Postby Giovanni » Sun Jun 02, 2019 5:17 am

Hi,

spiExchange() transmits and receives at same time, perhaps it is not what you want to do, just an hypothesis.

Giovanni

KalRal
Posts: 4
Joined: Tue Feb 12, 2019 9:08 pm
Has thanked: 1 time

Re: MPU9250 gives garbage when spiExchange is used but not when spiSend

Postby KalRal » Sun Jun 02, 2019 10:59 am

Maybe you are right. I even tried it with an Arduino and comparing both using a Logic analyzer. The chi(MPU9250) replies when there is a timegap of 6.4microseconds between the question(register I want to read) and its answer(contents of the register). But I am no expert in SPI and I am just starting off with all this stuff. There is no mention about simultaneous transmit and receive in MPU9250 data sheet. Thanks for your time.

User avatar
alex31
Posts: 268
Joined: Fri May 25, 2012 10:23 am
Location: toulouse, france
Has thanked: 16 times
Been thanked: 32 times
Contact:

Re: MPU9250 gives garbage when spiExchange is used but not when spiSend

Postby alex31 » Mon Jun 03, 2019 10:03 am

Hello,

I have no experience of using MPU9250 with SPI, but i use ICM20600 with SPI and theses chip from invensense are fairly similar.

With the ICM20600, i have no problem using spiExchange, with a spi clock @ 6.25Mhz

What STM MPU are you using ?
What Polarity and Phase are you using ? (post your SPIConfig structure)
How do you declare your arrays txbuf and rxbuf when you try to get the 6 bytes of accelerometer data ?

Alexandre

mikeprotts
Posts: 10
Joined: Wed Jan 09, 2019 12:37 pm
Been thanked: 2 times

Re: MPU9250 gives garbage when spiExchange is used but not when spiSend

Postby mikeprotts » Thu Jun 06, 2019 12:34 pm

I've had issues when I didn't setup the registers correctly - migrating from ChibiOS 3 to 18, and with different boards having different configuration requirements, so my first suggestion is to check the configuration again. It's possible you've ended up with a wrong data size setting.

The second problem I had was with spiExchange where the slave behaviour was changed by the value in the TX buffer. In my case, the slave stopped sending data when it encountered any bit set in the TX buffer, so all the low bits ended up as the same value as this doesn't show as an error at SPI transfer level. Simple solution was to zero the TX buffer before use.

Mike


Return to “ChibiOS/HAL”

Who is online

Users browsing this forum: No registered users and 1 guest