Two I2C devices on separate I2C pins

Can someone help me port this library?
FunkyD_
Posts: 5
Joined: Wed Aug 16, 2017 1:56 pm

Two I2C devices on separate I2C pins

Post by FunkyD_ » Wed Aug 16, 2017 2:13 pm

Hi there !

I am working with an MPU6050 IMU and a STM32 microcontroller. I am getting information from the IMU using jrowberg's implementation (https://github.com/jrowberg/i2cdevlib/t ... no/MPU6050). So far I had one MPU connected and it was working using HWire.

However, I would now need to use 2 MPUs and not on the same I2C (So, using I2C1 and I2C2 pins on maple mini board)
I first thought that we could have only one HWire working but I checked with a simple I2CScanner using 2 HWires and it is working. but when it comes to get it working with the I2Cdevlib implementation (that can be found also by following the link above), I have some troubles.

I also had a look here and from what I read I understand that there actually are some conflicts or a design ?

So did anyone manage to have 2 IMUs working with this library ?

Using HardWire seems good --> it was fast and I could use jrowberg's implementation to get only the data I need (I would like to get MPU's measurements at 1 KHz witout using FIFO buffer if possible)

Thanks for your help ! :mrgreen:

victor_pv
Posts: 1781
Joined: Mon Apr 27, 2015 12:12 pm

Re: 2 MPU6050 for STM32

Post by victor_pv » Wed Aug 16, 2017 2:31 pm

Could you link to the post about the conflict?

Also that thread is from 2015, so it's possible it was using softwaire.

The hardware i2c has a limitation with interrupts. If using interrupts, the i2c ISR needs to have the highest priority or data may get corrupted.
The libmaple implementation sets the i2c interrupts to the higher priority, but also currently does not use the interrupts in anyway, so that should not affect you.

There may be conflicts with other pins. A good place to confirm any problem with the peripheral and possible work arounds is the errata sheets (there are several depending on the version of the MCU (flash size, manufacture data and what not).
This is the one for he xB MCUs (128KB of flash):
http://www.st.com/content/ccc/resource/ ... 190234.pdf

You can also use DMA with i2c to reduce CPU overhead and skip using the i2c interrupts (you would instead use the DMA interrups, which do not have the same problem).

User avatar
Pito
Posts: 1690
Joined: Sat Mar 26, 2016 3:26 pm
Location: Rapa Nui

Re: 2 MPU6050 for STM32

Post by Pito » Wed Aug 16, 2017 2:44 pm

What would be the data throughput from both I2C1 and I2C2 at 1K samples per second?
How big (in bytes) is 1 sample?
Is that even feasible via 400kHz I2C clocked buses?
Pukao Hats Cleaning Services Ltd.

FunkyD_
Posts: 5
Joined: Wed Aug 16, 2017 1:56 pm

Re: 2 MPU6050 for STM32

Post by FunkyD_ » Wed Aug 16, 2017 2:48 pm

I called it conflict but it actually only talks about issues.
Here it is : viewtopic.php?f=15&t=365#p3591

I am not using interruptions. I am just reading values from the MPU at 1 KHz (and I do not think that the code related to the IMU was using interruptions anyway).

I will come back to this post soon.
I found out that if you define an HardWire object which name is not HWire then Warnings appear at compilation time and uploading to Maple Mini makes it not responding (at least not detectable by Arduino IDE).

Si I don't know yet whether a second HWire object (which would be called HWire2 for example) can work.

Anyway I will come back to this post :)

FunkyD_
Posts: 5
Joined: Wed Aug 16, 2017 1:56 pm

Re: 2 MPU6050 for STM32

Post by FunkyD_ » Wed Aug 16, 2017 2:52 pm

Pito wrote:
Wed Aug 16, 2017 2:44 pm
What would be the data throughput from both I2C1 and I2C2 at 1K samples per second?
How big (in bytes) is 1 sample?
Is that even feasible via 400kHz I2C clocked buses?
So there are 12 bytes for Acceleration and gyroscope (Ax, Ay, Az, Gx, Gy, Gz)
I was thinking about reading all of these for both IMUs and send through USB to computer for post-processing in a single Serial.write() as binaries.

zmemw16
Posts: 1577
Joined: Wed Jul 08, 2015 2:09 pm
Location: St Annes, Lancs,UK

Re: 2 MPU6050 for STM32

Post by zmemw16 » Wed Aug 16, 2017 3:12 pm

sorry, i got the maple mini bit, but we need info
which platform win/mac/linux ?
which core, Arduino_STM32, ST_F1(??) ?
libmaple or hal preference ? :)
what ide version ? 1.6.x 1.8.x
odd thoughts occur, which module are you using, post a link maybe?
i'm wondering if it has an alternate address available?
have you confirmed that each is working on i2c1?

for myself, i look for sample code that shows how it's done, not necessarily a cut and paste one; it doesn't really matter whether it's one from spl, hal or unicore-mx examples.
it's the one that pops up with that 'oh that's how it's done' realisation having read it. :D

my way forward would be to look at the interrupt driven i2c hal examples.

stephen

FunkyD_
Posts: 5
Joined: Wed Aug 16, 2017 1:56 pm

Re: 2 MPU6050 for STM32

Post by FunkyD_ » Wed Aug 16, 2017 3:36 pm

OK so problem partially solved :

So using 2 HWires is easy. but you need to name at least one of them 'HWire' otherwise you have the strange behaviours described above. This is certainly due to the implementation in i2cdevlib.
However, reading both acc x2 + gyro x2 and sending it through USb takes 1.25 ms approximately. (I can see if it is possible to optimize this but getting under 1ms will be hard if not impossible I think :? )
zmemw16 wrote:
Wed Aug 16, 2017 3:12 pm
sorry, i got the maple mini bit, but we need info
which platform win/mac/linux ?
which core, Arduino_STM32, ST_F1(??) ?
libmaple or hal preference ? :)
what ide version ? 1.6.x 1.8.x
odd thoughts occur, which module are you using, post a link maybe?
i'm wondering if it has an alternate address available?
have you confirmed that each is working on i2c1?
stephen
I am working on Linux with arduino 1.6.11, the core is Arduino_STM32 !

Thanks ! :)

WindyYam
Posts: 17
Joined: Mon Aug 07, 2017 2:39 am

Re: 2 MPU6050 for STM32

Post by WindyYam » Wed Aug 16, 2017 3:56 pm

the mpu6050 have a pin to select i2c address 0x68 or 0x69. I think there are no need to use 2 wire devices as with Arduino wire lib you can use only one wire object to communicate at one time, there are no anything like multi-threading. instead, try connect them on the same bus, give them different address and init 2 mpu objects on different address params

FunkyD_
Posts: 5
Joined: Wed Aug 16, 2017 1:56 pm

Re: 2 MPU6050 for STM32

Post by FunkyD_ » Thu Aug 17, 2017 8:14 am

WindyYam wrote:
Wed Aug 16, 2017 3:56 pm
the mpu6050 have a pin to select i2c address 0x68 or 0x69. I think there are no need to use 2 wire devices as with Arduino wire lib you can use only one wire object to communicate at one time, there are no anything like multi-threading. instead, try connect them on the same bus, give them different address and init 2 mpu objects on different address params
Yes you're right some manipulation on the MPU switches the address from 0x68 to 0x69.
But I am not sure that this can save you 0.20ms.

Also I thought that the MPU was storing the last values of Acceleration and Gyroscope it produced and that I was just asking to read those values with the microcontroller but it might be that both acceleration and gyroscope data are produced at request time from the microcontroller. This could save me enough time.

User avatar
RogerClark
Posts: 7537
Joined: Mon Apr 27, 2015 10:36 am
Location: Melbourne, Australia
Contact:

Re: Two I2C devices on separate I2C pins

Post by RogerClark » Thu Aug 17, 2017 10:59 am

If you need faster data rates, you should probably use SPI, however I don't think that devices supports SPI, but AFIK the 9150 and 9250 support SPI

Post Reply