STM32F103 i2c won't work with MPU6050

Discussions and support about ChibiOS/HAL, the MCU Hardware Abstraction Layer.
Fulcrum Mike
Posts: 14
Joined: Fri Dec 23, 2016 3:33 am

Re: STM32F103 i2c won't work with MPU6050

Postby Fulcrum Mike » Mon Jan 02, 2017 9:11 pm

Hi

Try increasing I2C IRQ priorities above the other sources, 5 for example, and see if it makes a difference.

The IRQ priorities for both I2C1 and I2C2 were already 5. I changed it to 7. Same issue.

Some other common issues are listed in this article: http://wiki.chibios.org/dokuwiki/doku.p ... e_shooting

I already studied this guide before opening this thread.

After numerous resets, I was once able to read the correct values with two different i2c read requests. Output:

Code: Select all

Init error: 0
Add: 34
Sleep: 1


Yet the next reset produced the same error again:

Code: Select all

Init error: 0
Add: 34
I2C STATUS: -1
I2C ERROR: 0
Sleep: 0


Just hooked the sensor module to rpi-3. Works like a charm.

I have read virtually all the posts on this forum about I2C, and still got no idea whats bugging my very simple code.

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

Re: STM32F103 i2c won't work with MPU6050

Postby Giovanni » Mon Jan 02, 2017 9:18 pm

Note that lower numbers are higher priorities. Do you have IRQ priorities numerically below 5?

Giovanni

Fulcrum Mike
Posts: 14
Joined: Fri Dec 23, 2016 3:33 am

Re: STM32F103 i2c won't work with MPU6050

Postby Fulcrum Mike » Mon Jan 02, 2017 9:28 pm

Oopss.. I should have checked before assuming.

No, all other IRQs are above 5.

Koen
Posts: 35
Joined: Mon Dec 21, 2015 12:15 am
Been thanked: 5 times

Re: STM32F103 i2c won't work with MPU6050

Postby Koen » Mon Jan 02, 2017 10:51 pm

If I recall correctly, early STM32 had major I2C hardware problems and ST later released a library named CPAL to help deal with it.

Have you seen this : https://github.com/jevermeister/MPU6050 ... S/issues/2 ?
"To get the library to function and detect the MPU6050, I had to modify the I2CdevreadBytes function to add in specific support for the STM32F103 chip as in Chibios/Hardware, you are unable to read a single byte from the I2C bus."

Koen
Posts: 35
Joined: Mon Dec 21, 2015 12:15 am
Been thanked: 5 times

Re: STM32F103 i2c won't work with MPU6050

Postby Koen » Mon Jan 02, 2017 10:58 pm

Did you enable debug checks in chconf.h ? I checked the repository and the code should trigger an error as you are asking to receive 1 byte :

Code: Select all

msg_t i2c_lld_master_receive_timeout(I2CDriver *i2cp, i2caddr_t addr,
                                     uint8_t *rxbuf, size_t rxbytes,
                                     systime_t timeout) {
[...]
#if defined(STM32F1XX_I2C)
  osalDbgCheck(rxbytes > 1);
#endif
[...]


Code: Select all

msg_t i2c_lld_master_transmit_timeout(I2CDriver *i2cp, i2caddr_t addr,
                                      const uint8_t *txbuf, size_t txbytes,
                                      uint8_t *rxbuf, size_t rxbytes,
                                      systime_t timeout) {
[...]
#if defined(STM32F1XX_I2C)
  osalDbgCheck((rxbytes == 0) || ((rxbytes > 1) && (rxbuf != NULL)));
#endif
[...]

Fulcrum Mike
Posts: 14
Joined: Fri Dec 23, 2016 3:33 am

Re: STM32F103 i2c won't work with MPU6050

Postby Fulcrum Mike » Tue Jan 03, 2017 8:35 am

Have you seen this : https://github.com/jevermeister/MPU6050 ... S/issues/2 ?
"To get the library to function and detect the MPU6050, I had to modify the I2CdevreadBytes function to add in specific support for the STM32F103 chip as in Chibios/Hardware, you are unable to read a single byte from the I2C bus."


A similar thread was opened in this forum too. I thought that the problem would have been solved by now. :oops:

I am now using a very simple approach by manually using i2cMasterTransmitTimeout instead of using MPU6050-ChibiOS. The good thing:By reading in burst mode, I can make several transactions without getting the earlier erros. The bad thing: The data received is not correct. Reading in burst mode and then chprinting the required register returns incorrect value. For example:

Code: Select all

    //READ DATA IN BURST
    chprintf((BaseSequentialStream*)&SD2, "==Read Data:==: \n\r");   
    status = MSG_OK;
    i2cAcquireBus(&I2CD2);
    tx_data[0] = 0x3B; //Accel_data
    status = i2cMasterTransmitTimeout(&I2CD2, 0x68,tx_data, 1, rx_data, 14, tmo);
    i2cReleaseBus(&I2CD2);
    if(status == MSG_TIMEOUT || status == MSG_RESET) {
        chprintf((BaseChannel *)&SD2,"I2C STATUS: %d\n\r", status);
        chprintf((BaseChannel *)&SD2,"I2C ERROR: %d\n\r", i2cGetErrors(&I2CD2));
    }     
    chThdSleepMilliseconds(2000);
    chprintf((BaseSequentialStream*)&SD2, "%d %d %d %d %d %d %d %d %d %d %d %d %d %d\n\r", rx_data[0], rx_data[1],rx_data[2],rx_data[3],rx_data[4],rx_data[5],rx_data[6],rx_data[7],rx_data[8],rx_data[9],rx_data[10],rx_data[11],rx_data[12],rx_data[13]);

This code returns the current value for 3D accel, 3D gyro, and temperature. But all I am getting is this:

Code: Select all

==Read Data:==:       //No 'I2C ERROR' output, meaning the transaction returned MSG_OK
0 0 255 0 0 0 0 0 0 0 0 0 0 0   //The register values. These are very likely incorrect


Won't give up now :evil:

Koen
Posts: 35
Joined: Mon Dec 21, 2015 12:15 am
Been thanked: 5 times

Re: STM32F103 i2c won't work with MPU6050

Postby Koen » Tue Jan 03, 2017 8:46 am

It's an hardware error in F1 : it won't be fixed, ever. You can only work-around it. Anyway, enable debug checks to avoid these easy traps.

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

Re: STM32F103 i2c won't work with MPU6050

Postby Giovanni » Tue Jan 03, 2017 9:20 am

Hi,

Also try avoiding reading a single byte, minimum two, it is another errata of the F1 I2C.

Giovanni

Fulcrum Mike
Posts: 14
Joined: Fri Dec 23, 2016 3:33 am

Re: STM32F103 i2c won't work with MPU6050

Postby Fulcrum Mike » Wed Jan 04, 2017 3:19 pm

Hi,

Thank you all for your help and suggestions.

Finally, I am able to read correct data, and write to configuration registers of the MPU6050 module.

The only difference between my working code and earlier code is that earlier, I was trying to read WHO_AM_I byte (single byte read operation) at startup, which froze the I2C driver, hence making later transactions fail.

Now I am reading the sensor data in burst mode (14 registers at once starting at 0x3B). It's working as expected. To read a single byte, I read multiple bytes and discard the extra data.

Cheers


Return to “ChibiOS/HAL”

Who is online

Users browsing this forum: No registered users and 7 guests