psiphi75 / esp-mpu9250

MPU9250 driver for the ESP8266 and the ESP32
Apache License 2.0
33 stars 17 forks source link

don't work for ESP32 #1

Open danylook opened 5 years ago

danylook commented 5 years ago

hi im new in esp32 and c program. i was trying to work with this library but no success until i may this changes. in the file i2c-easy.c i add this lines

define I2C_FREQ_HZ 400000 /!< I2C master clock frequency /

define I2C_TX_BUF_DISABLE 0 /!< I2C master do not need buffer /

define I2C_RX_BUF_DISABLE 0

and change this ones

conf.master.clk_speed = I2C_FREQ_HZ;

ESP_ERROR_CHECK(i2c_param_config(i2c_master_port, &conf)); ESP_ERROR_CHECK(i2c_driver_install(i2c_master_port, conf.mode,I2C_RX_BUF_DISABLE, I2C_TX_BUF_DISABLE,0));

now is working like a charme. hope can help anyone thanks for share

psiphi75 commented 5 years ago

Thanks Danny. You are welcome to make a pull request for this. Otherwise I will get around to fixing it one day. I'm just a bit busy at the moment.

danylook commented 5 years ago

need to learn how do do it ;) i will get there soon. thanks again for you drivers. it help me a lot with my project.

mosantos96 commented 5 years ago

Hello, i'm trying to program the esp32 with this code too. I can flash the code into ESP32, but then ESP32 stucks in a rebooting loop. I didn't change the code. Here is my console log. Hope you can give a little help and btw great work with this library!!

CONSOLE LOG Leaving... Hard resetting via RTS pin... MONITOR --- idf_monitor on /dev/cu.SLAB_USBtoUART 115200 --- --- Quit: Ctrl+] | Menu: Ctrl+T | Help: Ctrl+T followed by Ctrl+H --- ets Jun 8 2016 00:22:57

rst:0x1 (POWERON_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT) configsip: 188777542, SPIWP:0xee clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00 mode:DIO, clock div:2 load:0x3fff0018,len:4 load:0x3fff001c,len:6200 load:0x40078000,len:10168 load:0x40080400,len:6552 entry 0x40080764 I (30) boot: ESP-IDF v3.2-beta1-151-g960c24057-dirty 2nd stage bootloader I (30) boot: compile time 09:35:19 I (31) boot: Enabling RNG early entropy source... I (36) boot: SPI Speed : 40MHz I (41) boot: SPI Mode : DIO I (45) boot: SPI Flash Size : 4MB I (49) boot: Partition Table: I (52) boot: ## Label Usage Type ST Offset Length I (60) boot: 0 nvs WiFi data 01 02 00009000 00006000 I (67) boot: 1 phy_init RF data 01 01 0000f000 00001000 I (74) boot: 2 factory factory app 00 00 00010000 00100000 I (82) boot: End of partition table I (86) esp_image: segment 0: paddr=0x00010020 vaddr=0x3f400020 size=0x0b2c4 ( 45764) map I (111) esp_image: segment 1: paddr=0x0001b2ec vaddr=0x3ff80000 size=0x00000 ( 0) load I (111) esp_image: segment 2: paddr=0x0001b2f4 vaddr=0x3ff80000 size=0x00000 ( 0) load I (118) esp_image: segment 3: paddr=0x0001b2fc vaddr=0x3ffb0000 size=0x01f14 ( 7956) load I (130) esp_image: segment 4: paddr=0x0001d218 vaddr=0x3ffb1f14 size=0x00000 ( 0) load I (136) esp_image: segment 5: paddr=0x0001d220 vaddr=0x40080000 size=0x00400 ( 1024) load 0x40080000: _WindowOverflow4 at /Users/miguelsantos/esp/esp-idf/components/freertos/xtensa_vectors.S:1685

I (145) esp_image: segment 6: paddr=0x0001d628 vaddr=0x40080400 size=0x029e8 ( 10728) load I (158) esp_image: segment 7: paddr=0x00020018 vaddr=0x400d0018 size=0x17704 ( 96004) map 0x400d0018: _flash_cache_start at ??:?

I (196) esp_image: segment 8: paddr=0x00037724 vaddr=0x40082de8 size=0x07aa0 ( 31392) load 0x40082de8: esp_cpu_stall at /Users/miguelsantos/esp/esp-idf/components/soc/esp32/cpu_util.c:26

I (209) esp_image: segment 9: paddr=0x0003f1cc vaddr=0x400c0000 size=0x00000 ( 0) load I (210) esp_image: segment 10: paddr=0x0003f1d4 vaddr=0x50000000 size=0x00000 ( 0) load I (216) esp_image: segment 11: paddr=0x0003f1dc vaddr=0x50000000 size=0x00000 ( 0) load I (232) boot: Loaded app from partition at offset 0x10000 I (232) boot: Disabling RNG early entropy source... I (237) cpu_start: Pro cpu up. I (241) cpu_start: Starting app cpu, entry point is 0x40080f30 0x40080f30: call_start_cpu1 at /Users/miguelsantos/esp/esp-idf/components/esp32/cpu_start.c:245

I (0) cpu_start: App cpu up. I (251) heap_init: Initializing. RAM available for dynamic allocation: I (258) heap_init: At 3FFAE6E0 len 00001920 (6 KiB): DRAM I (264) heap_init: At 3FFB2F90 len 0002D070 (180 KiB): DRAM I (270) heap_init: At 3FFE0440 len 00003AE0 (14 KiB): D/IRAM I (277) heap_init: At 3FFE4350 len 0001BCB0 (111 KiB): D/IRAM I (283) heap_init: At 4008A888 len 00015778 (85 KiB): IRAM I (289) cpu_start: Pro cpu start user code I (307) cpu_start: Starting scheduler on PRO CPU. I (0) cpu_start: Starting scheduler on APP CPU. I (0) mpu9250: Initializating MPU9250

ESP_ERROR_CHECK failed: esp_err_t 0x107 (ESP_ERR_TIMEOUT) at 0x40088cec 0x40088cec: _esp_error_check_failed at /Users/miguelsantos/esp/esp-idf/components/esp32/panic.c:685

file: "/Users/miguelsantos/esp/mpu9250_test/components/mpu9250/mpu9250.c" line 57 func: i2c_mpu9250_init expression: i2c_write_bit(I2C_MASTER_NUM, MPU9250_I2C_ADDR, MPU9250_RA_PWR_MGMT_1, MPU9250_PWR1_DEVICE_RESET_BIT, 1)

Backtrace: 0x400888b0:0x3ffb6f80 0x40088cef:0x3ffb6fa0 0x400d32e3:0x3ffb6fc0 0x400d25c6:0x3ffb6fe0 0x400d274b:0x3ffb7070 0x40085615:0x3ffb7090 0x400888b0: invoke_abort at /Users/miguelsantos/esp/esp-idf/components/esp32/panic.c:680

0x40088cef: _esp_error_check_failed at /Users/miguelsantos/esp/esp-idf/components/esp32/panic.c:686

0x400d32e3: i2c_mpu9250_init at /Users/miguelsantos/esp/mpu9250_test/components/mpu9250/mpu9250.c:57 (discriminator 1)

0x400d25c6: run_imu at /Users/miguelsantos/esp/mpu9250_test/main/main.c:90

0x400d274b: imu_task at /Users/miguelsantos/esp/mpu9250_test/main/main.c:137

0x40085615: vPortTaskWrapper at /Users/miguelsantos/esp/esp-idf/components/freertos/port.c:403

Rebooting... ets Jun 8 2016 00:22:57 (Stuck in reboot loop :/ )

psiphi75 commented 5 years ago

This looks like it's not wired up correctly. Have you connected the mpu9250 correctly?

mosantos96 commented 5 years ago

Yes, it's connected correctly. I just changed these code lines: conf.sda_io_num = GPIO_NUM_21; conf.scl_io_num = GPIO_NUM_22;

My mpu9250's SDA and SCL pins are connected to ESP32's GPIO_NUM_21 and GPIO_NUM_22. Do I have to change any configuration in menuconfig file? The console log says that te error is in this function call line, inside i2c_mpu9250_init function: ESP_ERROR_CHECK(i2c_write_bit(I2C_MASTER_NUM, MPU9250_I2C_ADDR, MPU9250_RA_PWR_MGMT_1, MPU9250_PWR1_DEVICE_RESET_BIT, 1))

mosantos96 commented 5 years ago

@psiphi75 problem solved!!! After some research on esp-idf documentation i realized that you have to configure your i2c device parameters before install the i2c driver. I swapped these two lines inside "i2c_master_init" function in i2c-easy.c: ESP_ERROR_CHECK(i2c_driver_install(i2c_master_port, conf.mode,0,0, ESP_INTR_FLAG_IRAM)); ESP_ERROR_CHECK(i2c_param_config(i2c_master_port, &conf));

Now the program is running without any problem.

@danylook i don't know if you have already solved you problem. If not, you can try this. Hope it will be helpful!

psiphi75 commented 5 years ago

Thanks @mosantos96. That's strange, it was working for me on an ESP8266, I've never tested it on an ESP32 though. I don't have time to test it on an ESP8266, so I'll leave this issue open for now.

salocinx commented 4 years ago

Hi. Thanks for sharing this project. I have another problem with the ESP32 and the MPU9250 sensor using this library. I applied the above fixes and the program compiles fine but is constantly rebooting. I am using the standard GPIO 22 for SCL and GPIO21 for SDA and I have configured these pins accordingly in the mpu9250.c file. Moreover I have adjusted MPU9250_I2C_ADDR to 0x76, the current i2c address of my MPU-9250 sensor.

Here's the console output of the serial terminal:

rst:0xc (SW_CPU_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:2
load:0x3fff0030,len:4
load:0x3fff0034,len:7264
ho 0 tail 12 room 4
load:0x40078000,len:14352
load:0x40080400,len:4448
entry 0x400806f0
I (31) boot: ESP-IDF v4.2-dev-414-g132cc67c0-dirty 2nd stage bootloader
I (31) boot: compile time 23:11:53
I (32) boot: chip revision: 1
I (36) boot_comm: chip revision: 1, min. bootloader chip revision: 0
I (43) boot.esp32: SPI Speed      : 40MHz
I (47) boot.esp32: SPI Mode       : DIO
I (52) boot.esp32: SPI Flash Size : 2MB
I (56) boot: Enabling RNG early entropy source...
I (62) boot: Partition Table:
I (65) boot: ## Label            Usage          Type ST Offset   Length
I (73) boot:  0 nvs              WiFi data        01 02 00009000 00006000
I (80) boot:  1 phy_init         RF data          01 01 0000f000 00001000
I (88) boot:  2 factory          factory app      00 00 00010000 00100000
I (95) boot: End of partition table
I (99) boot_comm: chip revision: 1, min. application chip revision: 0
I (106) esp_image: segment 0: paddr=0x00010020 vaddr=0x3f400020 size=0x07cac ( 31916) map
I (127) esp_image: segment 1: paddr=0x00017cd4 vaddr=0x3ffb0000 size=0x02224 (  8740) load
I (131) esp_image: segment 2: paddr=0x00019f00 vaddr=0x40080000 size=0x00404 (  1028) load
I (136) esp_image: segment 3: paddr=0x0001a30c vaddr=0x40080404 size=0x05d0c ( 23820) load
I (154) esp_image: segment 4: paddr=0x00020020 vaddr=0x400d0020 size=0x1814c ( 98636) map
I (190) esp_image: segment 5: paddr=0x00038174 vaddr=0x40086110 size=0x04cdc ( 19676) load
I (206) boot: Loaded app from partition at offset 0x10000
I (206) boot: Disabling RNG early entropy source...
I (206) cpu_start: Pro cpu up.
I (210) cpu_start: Application information:
I (215) cpu_start: Project name:     app-template
I (220) cpu_start: App version:      1
I (225) cpu_start: Compile time:     Mar 10 2020 23:11:43
I (231) cpu_start: ELF file SHA256:  1848d9c3c6a88861...
I (237) cpu_start: ESP-IDF:          v4.2-dev-414-g132cc67c0-dirty
I (244) cpu_start: Starting app cpu, entry point is 0x400810b8
I (235) cpu_start: App cpu up.
I (254) heap_init: Initializing. RAM available for dynamic allocation:
I (261) heap_init: At 3FFAE6E0 len 00001920 (6 KiB): DRAM
I (267) heap_init: At 3FFB2A48 len 0002D5B8 (181 KiB): DRAM
I (273) heap_init: At 3FFE0440 len 00003AE0 (14 KiB): D/IRAM
I (280) heap_init: At 3FFE4350 len 0001BCB0 (111 KiB): D/IRAM
I (286) heap_init: At 4008ADEC len 00015214 (84 KiB): IRAM
I (292) cpu_start: Pro cpu start user code
I (310) spi_flash: detected chip: gd
I (311) spi_flash: flash io: dio
W (311) spi_flash: Detected size(4096k) larger than the size in the binary image header(2048k). Using the size in the binary image header.
I (321) cpu_start: Starting scheduler on PRO CPU.
I (0) cpu_start: Starting scheduler on APP CPU.
I (0) mpu9250: Initializating MPU9250

ESP_ERROR_CHECK failed: esp_err_t 0xffffffff (ESP_FAIL) at 0x40086138
file: "../main/mpu9250/mpu9250.c" line 57
func: i2c_mpu9250_init
expression: i2c_write_bit(I2C_MASTER_NUM, MPU9250_I2C_ADDR, MPU9250_RA_PWR_MGMT_1, MPU9250_PWR1_DEVICE_RESET_BIT, 1)

ELF file SHA256: 1848d9c3c6a88861f0159348512b9b3d9f9a304fe4c7187eb4c34200cb2dfed0

Backtrace: 0x40085b8d:0x3ffb6ee0 0x4008613b:0x3ffb6f00 0x400d49e3:0x3ffb6f20 0x400d3cc6:0x3ffb6f40 0x400d3e4b:0x3ffb6fd0 0x40088475:0x3ffb6ff0

Some idea how to fix this?

psiphi75 commented 4 years ago

I don't have an ESP32 at the moment to test this on and I can't see where the error is coming from.

salocinx commented 4 years ago

Thanks for responding. Is it correct if I define the MPU9250 i2c address in mpu9250.h at MPU9250_I2C_ADDR ?

#define MPU9250_I2C_ADDR (0x76)

I found that my MPU9250 is at hex 0x76 by using the i2c_scanner example sketch.

psiphi75 commented 4 years ago

It's strange that it's at 0x76, but I believe it's possible. I believe they will manufacture them if requested at other addresses. But I think it would be rare to come across it. Did you try the original 0x68? Are you using a BMP280 or similar, they are often found alongside the MPU9250 and have an i2c address of 0x76.

If you do use 0x76, then I would expect that you make the following changes to components/mpu9250/mpu9250.h:

#define MPU9250_I2C_ADDRESS_AD0_LOW (0x76)
#define MPU9250_I2C_ADDR MPU9250_I2C_ADDRESS_AD0_LOW
#define MPU9250_I2C_ADDRESS_AD0_HIGH (0x77)
salocinx commented 4 years ago

Hi psiphi75. I am using a MP9250 with BMP280. I meanwhile did quite some testing and as you mentioned, the BMP280 sensor is found at address 0x76. I initially wasn't aware of the fact that two devices should be shown with this particular module. I now reflow soldered the board and tadaaa... Now I get two devices at 0x68 + 0x76 and your code is working fine on the ESP32 (with the fixes discussed above). Thanks for your help!

thomasnugent commented 4 years ago

Thanks to the comments above I got ESP32 working nicely with this forked repo