From ca49b7daf2171000d074e16878e5418bc0ea507b Mon Sep 17 00:00:00 2001 From: Stephan Hadinger Date: Thu, 10 Jun 2021 18:55:06 +0200 Subject: [PATCH 1/6] Added `tasmota.memory()` --- lib/libesp32/Berry/default/be_tasmotalib.c | 3 + lib/libesp32/Berry/generate/be_const_strtab.h | 1 + .../Berry/generate/be_const_strtab_def.h | 5 +- .../generate/be_fixed_be_class_tasmota.h | 105 +++++++++--------- tasmota/xdrv_52_3_berry_tasmota.ino | 23 ++++ 5 files changed, 83 insertions(+), 54 deletions(-) diff --git a/lib/libesp32/Berry/default/be_tasmotalib.c b/lib/libesp32/Berry/default/be_tasmotalib.c index 63cb56472..0cdc2ae32 100644 --- a/lib/libesp32/Berry/default/be_tasmotalib.c +++ b/lib/libesp32/Berry/default/be_tasmotalib.c @@ -14,6 +14,7 @@ extern int l_millis(bvm *vm); extern int l_timereached(bvm *vm); extern int l_rtc(bvm *vm); extern int l_time_dump(bvm *vm); +extern int l_memory(bvm *vm); extern int l_yield(bvm *vm); extern int l_delay(bvm *vm); extern int l_scaleuint(bvm *vm); @@ -2488,6 +2489,7 @@ void be_load_tasmota_ntvlib(bvm *vm) { "time_reached", l_timereached }, { "rtc", l_rtc }, { "time_dump", l_time_dump }, + { "memory", l_memory }, { "yield", l_yield }, { "delay", l_delay }, { "scale_uint", l_scaleuint }, @@ -2568,6 +2570,7 @@ class be_class_tasmota (scope: global, name: Tasmota) { time_reached, func(l_timereached) rtc, func(l_rtc) time_dump, func(l_time_dump) + memory, func(l_memory) yield, func(l_yield) delay, func(l_delay) scale_uint, func(l_scaleuint) diff --git a/lib/libesp32/Berry/generate/be_const_strtab.h b/lib/libesp32/Berry/generate/be_const_strtab.h index ec2c4df85..e9a6f9bf1 100644 --- a/lib/libesp32/Berry/generate/be_const_strtab.h +++ b/lib/libesp32/Berry/generate/be_const_strtab.h @@ -34,6 +34,7 @@ extern const bcstring be_const_str_get_free_heap; extern const bcstring be_const_str_lv_msgbox; extern const bcstring be_const_str_publish; extern const bcstring be_const_str_finish; +extern const bcstring be_const_str_memory; extern const bcstring be_const_str_SYMBOL_DUMMY; extern const bcstring be_const_str_SYMBOL_SETTINGS; extern const bcstring be_const_str_content_button; diff --git a/lib/libesp32/Berry/generate/be_const_strtab_def.h b/lib/libesp32/Berry/generate/be_const_strtab_def.h index 597d5dff9..8b1647b10 100644 --- a/lib/libesp32/Berry/generate/be_const_strtab_def.h +++ b/lib/libesp32/Berry/generate/be_const_strtab_def.h @@ -33,7 +33,8 @@ be_define_const_str(log10, "log10", 2346846000u, 0, 5, NULL); be_define_const_str(get_free_heap, "get_free_heap", 625069757u, 0, 13, &be_const_str_lv_msgbox); be_define_const_str(lv_msgbox, "lv_msgbox", 689085206u, 0, 9, &be_const_str_publish); be_define_const_str(publish, "publish", 264247304u, 0, 7, NULL); -be_define_const_str(finish, "finish", 1494643858u, 0, 6, NULL); +be_define_const_str(finish, "finish", 1494643858u, 0, 6, &be_const_str_memory); +be_define_const_str(memory, "memory", 2229924270u, 0, 6, NULL); be_define_const_str(SYMBOL_DUMMY, "SYMBOL_DUMMY", 3621732138u, 0, 12, &be_const_str_SYMBOL_SETTINGS); be_define_const_str(SYMBOL_SETTINGS, "SYMBOL_SETTINGS", 339656335u, 0, 15, &be_const_str_content_button); be_define_const_str(content_button, "content_button", 1956476087u, 0, 14, NULL); @@ -874,6 +875,6 @@ static const bstring* const m_string_table[] = { static const struct bconststrtab m_const_string_table = { .size = 283, - .count = 566, + .count = 567, .table = m_string_table }; diff --git a/lib/libesp32/Berry/generate/be_fixed_be_class_tasmota.h b/lib/libesp32/Berry/generate/be_fixed_be_class_tasmota.h index d744d2a24..34c41ed47 100644 --- a/lib/libesp32/Berry/generate/be_fixed_be_class_tasmota.h +++ b/lib/libesp32/Berry/generate/be_fixed_be_class_tasmota.h @@ -1,67 +1,68 @@ #include "be_constobj.h" static be_define_const_map_slots(be_class_tasmota_map) { - { be_const_key(wire_scan, -1), be_const_closure(wire_scan_closure) }, - { be_const_key(gc, 41), be_const_closure(gc_closure) }, - { be_const_key(resp_cmnd_failed, -1), be_const_func(l_respCmndFailed) }, - { be_const_key(rtc, 23), be_const_func(l_rtc) }, - { be_const_key(time_str, 9), be_const_closure(time_str_closure) }, - { be_const_key(get_free_heap, -1), be_const_func(l_getFreeHeap) }, - { be_const_key(resp_cmnd_str, -1), be_const_func(l_respCmndStr) }, - { be_const_key(get_power, -1), be_const_func(l_getpower) }, - { be_const_key(resp_cmnd_done, -1), be_const_func(l_respCmndDone) }, - { be_const_key(get_light, -1), be_const_closure(get_light_closure) }, - { be_const_key(remove_cmd, -1), be_const_closure(remove_cmd_closure) }, - { be_const_key(set_light, 3), be_const_closure(set_light_closure) }, - { be_const_key(find_op, -1), be_const_closure(find_op_closure) }, - { be_const_key(set_power, 44), be_const_func(l_setpower) }, + { be_const_key(millis, 52), be_const_func(l_millis) }, + { be_const_key(gc, -1), be_const_closure(gc_closure) }, { be_const_key(scale_uint, -1), be_const_func(l_scaleuint) }, - { be_const_key(add_rule, 18), be_const_closure(add_rule_closure) }, - { be_const_key(save, -1), be_const_func(l_save) }, - { be_const_key(_rules, -1), be_const_int(0) }, - { be_const_key(resp_cmnd_error, 50), be_const_func(l_respCmndError) }, - { be_const_key(wire1, 8), be_const_int(1) }, - { be_const_key(_timers, -1), be_const_int(2) }, - { be_const_key(set_timer, -1), be_const_closure(set_timer_closure) }, - { be_const_key(response_append, 46), be_const_func(l_respAppend) }, - { be_const_key(run_deferred, -1), be_const_closure(run_deferred_closure) }, - { be_const_key(remove_rule, 35), be_const_closure(remove_rule_closure) }, - { be_const_key(_drivers, 7), be_const_int(3) }, - { be_const_key(resp_cmnd, -1), be_const_func(l_respCmnd) }, - { be_const_key(add_driver, 22), be_const_closure(add_driver_closure) }, - { be_const_key(exec_rules, 20), be_const_closure(exec_rules_closure) }, - { be_const_key(i2c_enabled, -1), be_const_func(l_i2cenabled) }, + { be_const_key(log, -1), be_const_func(l_logInfo) }, + { be_const_key(add_rule, -1), be_const_closure(add_rule_closure) }, + { be_const_key(get_free_heap, -1), be_const_func(l_getFreeHeap) }, { be_const_key(_cmd, -1), be_const_func(l_cmd) }, - { be_const_key(millis, 1), be_const_func(l_millis) }, - { be_const_key(web_send, 11), be_const_func(l_webSend) }, - { be_const_key(get_option, -1), be_const_func(l_getoption) }, - { be_const_key(yield, -1), be_const_func(l_yield) }, - { be_const_key(delay, -1), be_const_func(l_delay) }, - { be_const_key(web_send_decimal, -1), be_const_func(l_webSendDecimal) }, - { be_const_key(cmd, -1), be_const_closure(cmd_closure) }, - { be_const_key(wire2, 34), be_const_int(4) }, - { be_const_key(event, -1), be_const_closure(event_closure) }, - { be_const_key(load, -1), be_const_closure(load_closure) }, + { be_const_key(web_send, 16), be_const_func(l_webSend) }, + { be_const_key(set_timer, -1), be_const_closure(set_timer_closure) }, + { be_const_key(_cb, -1), be_const_int(0) }, { be_const_key(exec_cmd, -1), be_const_closure(exec_cmd_closure) }, - { be_const_key(_get_cb, -1), be_const_func(l_get_cb) }, - { be_const_key(cb_dispatch, 37), be_const_closure(cb_dispatch_closure) }, - { be_const_key(_ccmd, -1), be_const_int(5) }, - { be_const_key(time_reached, 51), be_const_func(l_timereached) }, - { be_const_key(time_dump, -1), be_const_func(l_time_dump) }, - { be_const_key(add_cmd, -1), be_const_closure(add_cmd_closure) }, - { be_const_key(publish, 12), be_const_func(l_publish) }, - { be_const_key(log, 40), be_const_func(l_logInfo) }, - { be_const_key(_cb, -1), be_const_int(6) }, + { be_const_key(i2c_enabled, 42), be_const_func(l_i2cenabled) }, + { be_const_key(cmd, -1), be_const_closure(cmd_closure) }, + { be_const_key(resp_cmnd_done, 28), be_const_func(l_respCmndDone) }, + { be_const_key(set_light, 1), be_const_closure(set_light_closure) }, + { be_const_key(get_light, -1), be_const_closure(get_light_closure) }, + { be_const_key(_rules, 50), be_const_int(1) }, + { be_const_key(time_reached, -1), be_const_func(l_timereached) }, + { be_const_key(web_send_decimal, -1), be_const_func(l_webSendDecimal) }, + { be_const_key(load, 22), be_const_closure(load_closure) }, + { be_const_key(yield, -1), be_const_func(l_yield) }, + { be_const_key(event, -1), be_const_closure(event_closure) }, + { be_const_key(add_cmd, 40), be_const_closure(add_cmd_closure) }, + { be_const_key(find_key_i, -1), be_const_closure(find_key_i_closure) }, + { be_const_key(delay, 43), be_const_func(l_delay) }, + { be_const_key(remove_cmd, -1), be_const_closure(remove_cmd_closure) }, + { be_const_key(remove_rule, 56), be_const_closure(remove_rule_closure) }, + { be_const_key(resolvecmnd, 29), be_const_func(l_resolveCmnd) }, + { be_const_key(get_option, -1), be_const_func(l_getoption) }, + { be_const_key(resp_cmnd_str, -1), be_const_func(l_respCmndStr) }, + { be_const_key(response_append, -1), be_const_func(l_respAppend) }, + { be_const_key(_timers, -1), be_const_int(2) }, { be_const_key(try_rule, -1), be_const_closure(try_rule_closure) }, + { be_const_key(set_power, 45), be_const_func(l_setpower) }, + { be_const_key(resp_cmnd_error, -1), be_const_func(l_respCmndError) }, + { be_const_key(publish, -1), be_const_func(l_publish) }, + { be_const_key(_ccmd, 13), be_const_int(3) }, + { be_const_key(_get_cb, -1), be_const_func(l_get_cb) }, + { be_const_key(wire1, 15), be_const_int(4) }, + { be_const_key(gen_cb, 6), be_const_closure(gen_cb_closure) }, + { be_const_key(rtc, -1), be_const_func(l_rtc) }, + { be_const_key(time_dump, -1), be_const_func(l_time_dump) }, + { be_const_key(run_deferred, -1), be_const_closure(run_deferred_closure) }, + { be_const_key(memory, 47), be_const_func(l_memory) }, + { be_const_key(time_str, 53), be_const_closure(time_str_closure) }, + { be_const_key(save, -1), be_const_func(l_save) }, + { be_const_key(find_op, 25), be_const_closure(find_op_closure) }, { be_const_key(chars_in_string, -1), be_const_closure(chars_in_string_closure) }, - { be_const_key(resolvecmnd, 5), be_const_func(l_resolveCmnd) }, - { be_const_key(find_key_i, 10), be_const_closure(find_key_i_closure) }, - { be_const_key(gen_cb, -1), be_const_closure(gen_cb_closure) }, + { be_const_key(get_power, -1), be_const_func(l_getpower) }, + { be_const_key(wire2, 8), be_const_int(5) }, + { be_const_key(wire_scan, 55), be_const_closure(wire_scan_closure) }, + { be_const_key(resp_cmnd_failed, -1), be_const_func(l_respCmndFailed) }, + { be_const_key(exec_rules, -1), be_const_closure(exec_rules_closure) }, + { be_const_key(cb_dispatch, -1), be_const_closure(cb_dispatch_closure) }, + { be_const_key(resp_cmnd, -1), be_const_func(l_respCmnd) }, + { be_const_key(_drivers, -1), be_const_int(6) }, + { be_const_key(add_driver, -1), be_const_closure(add_driver_closure) }, }; static be_define_const_map( be_class_tasmota_map, - 56 + 57 ); BE_EXPORT_VARIABLE be_define_const_class( diff --git a/tasmota/xdrv_52_3_berry_tasmota.ino b/tasmota/xdrv_52_3_berry_tasmota.ino index 711c641f2..7f8a04c2d 100644 --- a/tasmota/xdrv_52_3_berry_tasmota.ino +++ b/tasmota/xdrv_52_3_berry_tasmota.ino @@ -168,6 +168,29 @@ extern "C" { be_raise(vm, kTypeError, nullptr); } + // Berry: tasmota.memory(timer:int) -> bool + // + int32_t l_memory(struct bvm *vm); + int32_t l_memory(struct bvm *vm) { + int32_t top = be_top(vm); // Get the number of arguments + if (top == 1) { // no argument (instance only) + be_newobject(vm, "map"); + map_insert_int(vm, "flash", ESP.getFlashChipSize() / 1024); + map_insert_int(vm, "program", ESP_getSketchSize() / 1024); + map_insert_int(vm, "program_free", ESP.getFreeSketchSpace() / 1024); + map_insert_int(vm, "heap_free", ESP_getFreeHeap() / 1024); + int32_t freeMaxMem = 100 - (int32_t)(ESP_getMaxAllocHeap() * 100 / ESP_getFreeHeap()); + map_insert_int(vm, "frag", freeMaxMem); + if (psramFound()) { + map_insert_int(vm, "psram", ESP.getPsramSize() / 1024); + map_insert_int(vm, "psram_free", ESP.getFreePsram() / 1024); + } + be_pop(vm, 1); + be_return(vm); + } + be_raise(vm, kTypeError, nullptr); + } + int32_t l_time_dump(bvm *vm) { int32_t top = be_top(vm); // Get the number of arguments if (top == 2 && be_isint(vm, 2)) { From 2d5841af9ca3a4bdee532b4c9bbaef833a605249 Mon Sep 17 00:00:00 2001 From: Stephan Hadinger Date: Thu, 10 Jun 2021 19:07:20 +0200 Subject: [PATCH 2/6] Esp32c3 fix compilation for SPI and uDisplay --- include/fix_esp32c3.h | 14 ++++++++++++++ .../src/internal/DotStarEsp32DmaSpiMethod.h | 4 ++-- lib/lib_display/UDisplay/uDisplay.h | 5 +++++ 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/include/fix_esp32c3.h b/include/fix_esp32c3.h index 1964eb354..96e994159 100644 --- a/include/fix_esp32c3.h +++ b/include/fix_esp32c3.h @@ -38,3 +38,17 @@ #define __UINT32_TYPE__ unsigned int #endif // __riscv + +// fix a bug in esp-idf 4.4 for esp32c3 +#ifndef REG_SPI_BASE +#define REG_SPI_BASE(i) (DR_REG_SPI1_BASE + (((i)>1) ? (((i)* 0x1000) + 0x20000) : (((~(i)) & 1)* 0x1000 ))) +#endif + +#if CONFIG_IDF_TARGET_ESP32C3 +// SPI_MOSI_DLEN_REG is not defined anymore in esp32c3, instead use SPI_MS_DLEN_REG +#define SPI_MOSI_DLEN_REG(x) SPI_MS_DLEN_REG(x) +//alias for different chips, deprecated for the chips after esp32s2 +#define SPI_HOST SPI1_HOST +#define HSPI_HOST SPI2_HOST +#define VSPI_HOST SPI3_HOST +#endif \ No newline at end of file diff --git a/lib/lib_basic/NeoPixelBus/src/internal/DotStarEsp32DmaSpiMethod.h b/lib/lib_basic/NeoPixelBus/src/internal/DotStarEsp32DmaSpiMethod.h index d71cf79d8..a9d149b71 100644 --- a/lib/lib_basic/NeoPixelBus/src/internal/DotStarEsp32DmaSpiMethod.h +++ b/lib/lib_basic/NeoPixelBus/src/internal/DotStarEsp32DmaSpiMethod.h @@ -29,9 +29,9 @@ License along with NeoPixel. If not, see #include "driver/spi_master.h" -#if defined(CONFIG_IDF_TARGET_ESP32C3) +#if defined(CONFIG_IDF_TARGET_ESP32C3) && !defined(HSPI_HOST) // HSPI_HOST depreciated in C3 -#define HSPI_HOST SPI3_HOST +#define HSPI_HOST SPI2_HOST #endif #if !defined(CONFIG_IDF_TARGET_ESP32S2) && !defined(CONFIG_IDF_TARGET_ESP32C3) diff --git a/lib/lib_display/UDisplay/uDisplay.h b/lib/lib_display/UDisplay/uDisplay.h index 78f96ad6c..42b8f47a3 100755 --- a/lib/lib_display/UDisplay/uDisplay.h +++ b/lib/lib_display/UDisplay/uDisplay.h @@ -54,8 +54,13 @@ enum uColorType { uCOLOR_BW, uCOLOR_COLOR }; #undef GPIO_CLR #undef GPIO_SET_SLOW #undef GPIO_CLR_SLOW +#if CONFIG_IDF_TARGET_ESP32C3 +#define GPIO_CLR(A) GPIO.out_w1tc.val = (1 << A) +#define GPIO_SET(A) GPIO.out_w1ts.val = (1 << A) +#else // plain ESP32 #define GPIO_CLR(A) GPIO.out_w1tc = (1 << A) #define GPIO_SET(A) GPIO.out_w1ts = (1 << A) +#endif #define GPIO_CLR_SLOW(A) digitalWrite(A, LOW) #define GPIO_SET_SLOW(A) digitalWrite(A, HIGH) From 22102000ef09b0ce19edc1e8505ade6dbedfb33d Mon Sep 17 00:00:00 2001 From: Stephan Hadinger Date: Thu, 10 Jun 2021 19:15:15 +0200 Subject: [PATCH 3/6] Remove no more needed lib exclusions --- platformio_tasmota_cenv_sample.ini | 4 ---- 1 file changed, 4 deletions(-) diff --git a/platformio_tasmota_cenv_sample.ini b/platformio_tasmota_cenv_sample.ini index 3d7a32bef..37d2e87cd 100644 --- a/platformio_tasmota_cenv_sample.ini +++ b/platformio_tasmota_cenv_sample.ini @@ -74,10 +74,6 @@ lib_extra_dirs = lib/libesp32 lib_ignore = NimBLE-Arduino Micro-RTSP - ILI9488 - SSD3115 - RA8876 - universal display Library ; *** EXPERIMENTAL Tasmota version for ESP32 IDF4.4. [env:tasmota32idf4] From 0e278fe13b830105d37d33d7d65ce4d82de47964 Mon Sep 17 00:00:00 2001 From: Stephan Hadinger Date: Thu, 10 Jun 2021 19:35:33 +0200 Subject: [PATCH 4/6] Remove SPI3_HOST that does not exist on esp32c3 --- include/fix_esp32c3.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/fix_esp32c3.h b/include/fix_esp32c3.h index 96e994159..d1e0bcb80 100644 --- a/include/fix_esp32c3.h +++ b/include/fix_esp32c3.h @@ -50,5 +50,5 @@ //alias for different chips, deprecated for the chips after esp32s2 #define SPI_HOST SPI1_HOST #define HSPI_HOST SPI2_HOST -#define VSPI_HOST SPI3_HOST -#endif \ No newline at end of file +#define VSPI_HOST SPI2_HOST /* No SPI3_host on C3 */ +#endif From 8693f5b0a8839d1ea391791594109a768dd2c220 Mon Sep 17 00:00:00 2001 From: Theo Arends <11044339+arendst@users.noreply.github.com> Date: Fri, 11 Jun 2021 10:04:27 +0200 Subject: [PATCH 5/6] Fix ESP32 bootloop regression --- tasmota/support.ino | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tasmota/support.ino b/tasmota/support.ino index 566d0ea95..76b42f38a 100644 --- a/tasmota/support.ino +++ b/tasmota/support.ino @@ -1191,8 +1191,8 @@ uint32_t ResponseLength(void) { void ResponseClear(void) { // Reset string length to zero #ifdef MQTT_DATA_STRING -// TasmotaGlobal.mqtt_data = ""; - TasmotaGlobal.mqtt_data = (const char*) nullptr; + TasmotaGlobal.mqtt_data = ""; +// TasmotaGlobal.mqtt_data = (const char*) nullptr; // Doesn't work on ESP32 as strlen() (in MqttPublishPayload) will fail (for obvious reasons) #else TasmotaGlobal.mqtt_data[0] = '\0'; #endif From 1c5bcd6dadd45d9e096dce6c02ded484dfa18cbd Mon Sep 17 00:00:00 2001 From: Stephan Hadinger Date: Fri, 11 Jun 2021 13:27:34 +0200 Subject: [PATCH 6/6] I2C extended MPU6886 to also support MPU9250 (found in Legacy M5Stack Fire) --- CHANGELOG.md | 1 + I2CDEVICES.md | 2 +- .../{MPU6886 => MPU_accel}/library.properties | 8 +- .../src/MPU_accel.cpp} | 42 +++++---- .../MPU6886.h => MPU_accel/src/MPU_accel.h} | 9 +- .../drivers/{mpu6886.be => mpu_accel.be} | 31 ++++--- tasmota/my_user_config.h | 4 +- tasmota/tasmota_configurations_ESP32.h | 4 +- tasmota/xsns_85_mpu6886.ino | 93 ++++++++++--------- 9 files changed, 108 insertions(+), 86 deletions(-) rename lib/lib_i2c/{MPU6886 => MPU_accel}/library.properties (52%) rename lib/lib_i2c/{MPU6886/src/MPU6886.cpp => MPU_accel/src/MPU_accel.cpp} (83%) rename lib/lib_i2c/{MPU6886/src/MPU6886.h => MPU_accel/src/MPU_accel.h} (94%) rename tasmota/berry/drivers/{mpu6886.be => mpu_accel.be} (78%) diff --git a/CHANGELOG.md b/CHANGELOG.md index 55316dfb4..d71321407 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ All notable changes to this project will be documented in this file. - Command ``Timers`` layout of JSON message changed to single line - Command ``Gpio`` layout of JSON message changed to single line - Command ``Modules`` layout of JSON message changed to single line +- I2C extended MPU6886 to also support MPU9250 (found in Legacy M5Stack Fire) ## [9.4.0.4] ### Added diff --git a/I2CDEVICES.md b/I2CDEVICES.md index 5ceff0408..84dac460b 100644 --- a/I2CDEVICES.md +++ b/I2CDEVICES.md @@ -91,5 +91,5 @@ Index | Define | Driver | Device | Address(es) | Description 55 | USE_EZOPMP | xsns_78 | EZOPMP | 0x61 - 0x70 | Peristaltic Pump 56 | USE_SEESAW_SOIL | xsns_81 | SEESOIL | 0x36 - 0x39 | Adafruit seesaw soil moisture sensor 57 | USE_TOF10120 | xsns_84 | TOF10120 | 0x52 | Time-of-flight (ToF) distance sensor - 58 | USE_MPU6886 | xsns_85 | MPU6886 | 0x68 | MPU6886 M5Stack + 58 | USE_MPU_ACCEL | xsns_85 | MPU_ACCEL| 0x68 | MPU6886/MPU9250 6-axis MotionTracking sensor from M5Stack 59 | USE_BM8563 | xdrv_56 | BM8563 | 0x51 | BM8563 RTC from M5Stack \ No newline at end of file diff --git a/lib/lib_i2c/MPU6886/library.properties b/lib/lib_i2c/MPU_accel/library.properties similarity index 52% rename from lib/lib_i2c/MPU6886/library.properties rename to lib/lib_i2c/MPU_accel/library.properties index 1a08c8516..583ca0e8f 100644 --- a/lib/lib_i2c/MPU6886/library.properties +++ b/lib/lib_i2c/MPU_accel/library.properties @@ -1,9 +1,9 @@ -name=MPU6886 +name=MPU_accel version= author=M5StickC maintainer=Stephan Hadinger -sentence=Support for MPU6886 -paragraph=Support for MPU6886 +sentence=Support for MPU6886, MPU9250 +paragraph=Support for MPU6886, MPU9250 category= url=https://github.com/m5stack/M5StickC/blob/master/src/utility/ -architectures=esp32,esp8266 +architectures=* diff --git a/lib/lib_i2c/MPU6886/src/MPU6886.cpp b/lib/lib_i2c/MPU_accel/src/MPU_accel.cpp similarity index 83% rename from lib/lib_i2c/MPU6886/src/MPU6886.cpp rename to lib/lib_i2c/MPU_accel/src/MPU_accel.cpp index 8774d09f7..439f2562b 100755 --- a/lib/lib_i2c/MPU6886/src/MPU6886.cpp +++ b/lib/lib_i2c/MPU_accel/src/MPU_accel.cpp @@ -1,8 +1,8 @@ -#include "MPU6886.h" +#include "MPU_accel.h" #include #include -void MPU6886::I2C_Read_NBytes(uint8_t driver_Addr, uint8_t start_Addr, uint8_t number_Bytes, uint8_t *read_Buffer){ +void MPU_accel::I2C_Read_NBytes(uint8_t driver_Addr, uint8_t start_Addr, uint8_t number_Bytes, uint8_t *read_Buffer){ myWire->beginTransmission(driver_Addr); myWire->write(start_Addr); @@ -16,7 +16,7 @@ void MPU6886::I2C_Read_NBytes(uint8_t driver_Addr, uint8_t start_Addr, uint8_t n } } -void MPU6886::I2C_Write_NBytes(uint8_t driver_Addr, uint8_t start_Addr, uint8_t number_Bytes, uint8_t *write_Buffer){ +void MPU_accel::I2C_Write_NBytes(uint8_t driver_Addr, uint8_t start_Addr, uint8_t number_Bytes, uint8_t *write_Buffer){ myWire->beginTransmission(driver_Addr); myWire->write(start_Addr); @@ -25,13 +25,19 @@ void MPU6886::I2C_Write_NBytes(uint8_t driver_Addr, uint8_t start_Addr, uint8_t } -int MPU6886::Init(void){ +int MPU_accel::Init(void){ unsigned char tempdata[1]; unsigned char regdata; I2C_Read_NBytes(MPU6886_ADDRESS, MPU6886_WHOAMI, 1, tempdata); - if(tempdata[0] != 0x19) + const uint8_t whoami = tempdata[0]; + if (whoami == 0x19) { + model = 6886; // MPU6886 + } else if (whoami == 0x71) { + model = 9250; // MPU9250 + } else { return -1; + } delay(1); regdata = 0x00; @@ -91,7 +97,7 @@ int MPU6886::Init(void){ return 0; } -void MPU6886::getAccelAdc(int16_t* ax, int16_t* ay, int16_t* az){ +void MPU_accel::getAccelAdc(int16_t* ax, int16_t* ay, int16_t* az){ uint8_t buf[6]; I2C_Read_NBytes(MPU6886_ADDRESS,MPU6886_ACCEL_XOUT_H,6,buf); @@ -101,7 +107,7 @@ void MPU6886::getAccelAdc(int16_t* ax, int16_t* ay, int16_t* az){ *az=((int16_t)buf[4]<<8)|buf[5]; } -void MPU6886::getGyroAdc(int16_t* gx, int16_t* gy, int16_t* gz){ +void MPU_accel::getGyroAdc(int16_t* gx, int16_t* gy, int16_t* gz){ uint8_t buf[6]; I2C_Read_NBytes(MPU6886_ADDRESS,MPU6886_GYRO_XOUT_H,6,buf); @@ -112,7 +118,7 @@ void MPU6886::getGyroAdc(int16_t* gx, int16_t* gy, int16_t* gz){ } -void MPU6886::getTempAdc(int16_t *t){ +void MPU_accel::getTempAdc(int16_t *t){ uint8_t buf[2]; I2C_Read_NBytes(MPU6886_ADDRESS,MPU6886_TEMP_OUT_H,2,buf); @@ -123,7 +129,7 @@ void MPU6886::getTempAdc(int16_t *t){ // //!俯仰,航向,横滚:pitch,yaw,roll,指三维空间中飞行器的旋转状态。 -// void MPU6886::getAhrsData(float *pitch,float *roll,float *yaw){ +// void MPU_accel::getAhrsData(float *pitch,float *roll,float *yaw){ // float accX = 0; // float accY = 0; @@ -141,7 +147,7 @@ void MPU6886::getTempAdc(int16_t *t){ // } -void MPU6886::getGres(){ +void MPU_accel::getGres(){ switch (Gyscale) { @@ -167,7 +173,7 @@ void MPU6886::getGres(){ } -void MPU6886::getAres(){ +void MPU_accel::getAres(){ switch (Acscale) { // Possible accelerometer scales (and their register bit settings) are: @@ -193,7 +199,7 @@ void MPU6886::getAres(){ } -void MPU6886::SetGyroFsr(Gscale scale) +void MPU_accel::SetGyroFsr(Gscale scale) { //return IIC_Write_Byte(MPU_GYRO_CFG_REG,scale<<3);//设置陀螺仪满量程范围 unsigned char regdata; @@ -205,7 +211,7 @@ void MPU6886::SetGyroFsr(Gscale scale) getGres(); } -void MPU6886::SetAccelFsr(Ascale scale) +void MPU_accel::SetAccelFsr(Ascale scale) { unsigned char regdata; regdata = (scale<<3); @@ -219,7 +225,7 @@ void MPU6886::SetAccelFsr(Ascale scale) // x/y/z are in 1/1000 if g // avoiding costly float calculations -void MPU6886::getAccelDataInt(int16_t* ax, int16_t* ay, int16_t* az) { +void MPU_accel::getAccelDataInt(int16_t* ax, int16_t* ay, int16_t* az) { int16_t accX = 0; int16_t accY = 0; int16_t accZ = 0; @@ -231,7 +237,7 @@ void MPU6886::getAccelDataInt(int16_t* ax, int16_t* ay, int16_t* az) { } -void MPU6886::getAccelData(float* ax, float* ay, float* az){ +void MPU_accel::getAccelData(float* ax, float* ay, float* az){ int16_t accX = 0; @@ -248,7 +254,7 @@ void MPU6886::getAccelData(float* ax, float* ay, float* az){ // x/y/z are in dps - degrees per second // avoiding costly float calculations -void MPU6886::getGyroDataInt(int16_t* ax, int16_t* ay, int16_t* az) { +void MPU_accel::getGyroDataInt(int16_t* ax, int16_t* ay, int16_t* az) { int16_t gyX = 0; int16_t gyY = 0; int16_t gyZ = 0; @@ -260,7 +266,7 @@ void MPU6886::getGyroDataInt(int16_t* ax, int16_t* ay, int16_t* az) { } -void MPU6886::getGyroData(float* gx, float* gy, float* gz){ +void MPU_accel::getGyroData(float* gx, float* gy, float* gz){ int16_t gyroX = 0; int16_t gyroY = 0; int16_t gyroZ = 0; @@ -271,7 +277,7 @@ void MPU6886::getGyroData(float* gx, float* gy, float* gz){ *gz = (float)gyroZ * gRes; } -void MPU6886::getTempData(float *t){ +void MPU_accel::getTempData(float *t){ int16_t temp = 0; getTempAdc(&temp); diff --git a/lib/lib_i2c/MPU6886/src/MPU6886.h b/lib/lib_i2c/MPU_accel/src/MPU_accel.h similarity index 94% rename from lib/lib_i2c/MPU6886/src/MPU6886.h rename to lib/lib_i2c/MPU_accel/src/MPU_accel.h index b4a5541e0..7b522a836 100755 --- a/lib/lib_i2c/MPU6886/src/MPU6886.h +++ b/lib/lib_i2c/MPU_accel/src/MPU_accel.h @@ -5,6 +5,9 @@ library in the Wire.h/twi.c utility file. We are also using the 400 kHz fast I2C mode by setting the TWI_FREQ to 400000L /twi.h utility file. */ + +// Extended to support MPU9250 and other variants + #ifndef _MPU6886_H_ #define _MPU6886_H_ @@ -48,7 +51,7 @@ #define AtR 0.0174533 #define Gyro_Gr 0.0010653 -class MPU6886 { +class MPU_accel { public: enum Ascale { AFS_2G = 0, @@ -68,14 +71,16 @@ class MPU6886 { Ascale Acscale = AFS_8G; int16_t acRange = 8000; // 1/1000 of g int16_t gyRange = 2000; // dps - degree per second + uint32_t model = 6886; // MPU model number public: - MPU6886(void) {}; + MPU_accel(void) {}; #ifdef ESP32 void setBus(uint32_t _bus) { myWire = _bus ? &Wire1 : &Wire; }; #else void setBus(uint32_t _bus) { myWire = &Wire; }; #endif int Init(void); + uint32_t getModel(void) const { return model; } void getAccelAdc(int16_t* ax, int16_t* ay, int16_t* az); void getGyroAdc(int16_t* gx, int16_t* gy, int16_t* gz); void getTempAdc(int16_t *t); diff --git a/tasmota/berry/drivers/mpu6886.be b/tasmota/berry/drivers/mpu_accel.be similarity index 78% rename from tasmota/berry/drivers/mpu6886.be rename to tasmota/berry/drivers/mpu_accel.be index 934d8db7f..7a0e39b4a 100644 --- a/tasmota/berry/drivers/mpu6886.be +++ b/tasmota/berry/drivers/mpu_accel.be @@ -7,6 +7,7 @@ class MPU6886 : Driver var wire #- if wire == nil then the module is not initialized -# + var device var gres, ares var accel, gyro @@ -15,7 +16,10 @@ class MPU6886 : Driver if self.wire var v = self.wire.read(0x68,0x75,1) - if v != 0x19 return end #- wrong device -# + if v == 0x19 self.device = 6886 + elif v == 0x71 self.device = 9250 + else self.wire = nil return #- wrong device -# + end self.wire.write(0x68, 0x6B, 0, 1) tasmota.delay(10) @@ -46,7 +50,7 @@ class MPU6886 : Driver self.gres = 2000.0/32768.0 self.ares = 8.0/32678.0 - print("I2C: MPU6886 detected on bus "+str(self.wire.bus)) + print("I2C:","MPU"+str(self.device),"detected on bus",self.wire.bus) end end @@ -90,13 +94,14 @@ class MPU6886 : Driver if !self.wire return nil end #- exit if not initialized -# import string var msg = string.format( - "{s}MPU6886 acc_x{m}%.3f G{e}".. - "{s}MPU6886 acc_y{m}%.3f G{e}".. - "{s}MPU6886 acc_z{m}%.3f G{e}".. - "{s}MPU6886 gyr_x{m}%i dps{e}".. - "{s}MPU6886 gyr_y{m}%i dps{e}".. - "{s}MPU6886 gyr_z{m}%i dps{e}", - self.accel[0], self.accel[1], self.accel[2], self.gyro[0], self.gyro[1], self.gyro[2]) + "{s}MPU%d acc_x{m}%.3f G{e}".. + "{s}MPU%d acc_y{m}%.3f G{e}".. + "{s}MPU%d acc_z{m}%.3f G{e}".. + "{s}MPU%d gyr_x{m}%i dps{e}".. + "{s}MPU%d gyr_y{m}%i dps{e}".. + "{s}MPU%d gyr_z{m}%i dps{e}", + self.device, self.accel[0], self.device, self.accel[1], self.device, self.accel[2], + self.device, self.gyro[0], self.device, self.gyro[1], self.device, self.gyro[2]) tasmota.web_send_decimal(msg) end @@ -107,11 +112,11 @@ class MPU6886 : Driver var ax = int(self.accel[0] * 1000) var ay = int(self.accel[1] * 1000) var az = int(self.accel[2] * 1000) - var msg = string.format(",\"MPU6886\":{\"AX\":%i,\"AY\":%i,\"AZ\":%i,\"GX\":%i,\"GY\":%i,\"GZ\":%i}", - ax, ay, az, self.gyro[0], self.gyro[1], self.gyro[2]) + var msg = string.format(",\"MPU%d\":{\"AX\":%i,\"AY\":%i,\"AZ\":%i,\"GX\":%i,\"GY\":%i,\"GZ\":%i}", + self.device, ax, ay, az, self.gyro[0], self.gyro[1], self.gyro[2]) tasmota.response_append(msg) end end -mpu6886 = MPU6886() -tasmota.add_driver(mpu6886) \ No newline at end of file +mpu_accel = MPU6886() +tasmota.add_driver(mpu_accel) \ No newline at end of file diff --git a/tasmota/my_user_config.h b/tasmota/my_user_config.h index 9286a7fe0..10343cc3b 100644 --- a/tasmota/my_user_config.h +++ b/tasmota/my_user_config.h @@ -628,8 +628,8 @@ // #define USE_EZORGB // [I2cDriver55] Enable support for EZO's RGB sensor (+0k5 code) - Shared EZO code required for any EZO device (+1k2 code) // #define USE_EZOPMP // [I2cDriver55] Enable support for EZO's PMP sensor (+0k3 code) - Shared EZO code required for any EZO device (+1k2 code) // #define USE_SEESAW_SOIL // [I2cDriver56] Enable Capacitice Soil Moisture & Temperature Sensor (I2C addresses 0x36 - 0x39) (+1k3 code) -// #define USE_MPU6886 // [I2cDriver58] Enable MPU6886 - found in M5Stack - support both I2C buses on ESP32 (I2C address 0x68) (+2k code) -// #define USE_BM8563 // [I2cDriver58] Enable BM8563 RTC - found in M5Stack - support both I2C buses on ESP32 (I2C address 0x51) (+2.5k code) +// #define USE_MPU_ACCEL // [I2cDriver58] Enable MPU6886/MPU9250 - found in M5Stack - support both I2C buses on ESP32 (I2C address 0x68) (+2k code) +// #define USE_BM8563 // [I2cDriver59] Enable BM8563 RTC - found in M5Stack - support both I2C buses on ESP32 (I2C address 0x51) (+2.5k code) // #define USE_DISPLAY // Add I2C Display Support (+2k code) #define USE_DISPLAY_MODES1TO5 // Enable display mode 1 to 5 in addition to mode 0 diff --git a/tasmota/tasmota_configurations_ESP32.h b/tasmota/tasmota_configurations_ESP32.h index 387ed5de9..6d2a40352 100644 --- a/tasmota/tasmota_configurations_ESP32.h +++ b/tasmota/tasmota_configurations_ESP32.h @@ -106,7 +106,7 @@ #define USE_I2C #define USE_BMA423 - #define USE_MPU6886 + #define USE_MPU_ACCEL #define USE_SPI #define USE_DISPLAY #ifdef USE_UNIVERSAL_DISPLAY @@ -233,7 +233,7 @@ //#define USE_MPR121 // [I2cDriver23] Enable MPR121 controller (I2C addresses 0x5A, 0x5B, 0x5C and 0x5D) in input mode for touch buttons (+1k3 code) //#define USE_CCS811 // [I2cDriver24] Enable CCS811 sensor (I2C address 0x5A) (+2k2 code) #define USE_CCS811_V2 // [I2cDriver24] Enable CCS811 sensor (I2C addresses 0x5A and 0x5B) (+2k8 code) -#define USE_MPU6886 // [I2cDriver??] Enable MPU6886 6-axis MotionTracking sensor (I2C address 0x68) +#define USE_MPU_ACCEL // [I2cDriver58] Enable MPU6886, MPU9250 6-axis MotionTracking sensor (I2C address 0x68) //#define USE_MPU6050 // [I2cDriver25] Enable MPU6050 sensor (I2C address 0x68 AD0 low or 0x69 AD0 high) (+3K3 of code and 188 Bytes of RAM) //#define USE_DS3231 // [I2cDriver26] Enable DS3231 external RTC in case no Wifi is avaliable. See docs in the source file (+1k2 code) //#define USE_MGC3130 // [I2cDriver27] Enable MGC3130 Electric Field Effect Sensor (I2C address 0x42) (+2k7 code, 0k3 mem) diff --git a/tasmota/xsns_85_mpu6886.ino b/tasmota/xsns_85_mpu6886.ino index 5a288fd2f..9f77efc0d 100644 --- a/tasmota/xsns_85_mpu6886.ino +++ b/tasmota/xsns_85_mpu6886.ino @@ -18,11 +18,11 @@ */ #ifdef USE_I2C -#ifdef USE_MPU6886 +#if defined(USE_MPU6886) || defined(USE_MPU_ACCEL) // USE_MPU6886 deprecated, prefer USE_MPU_ACCEL -#include +#include /*********************************************************************************************\ - * MPU6886 + * MPU6886/MPU9250 * Internal chip found in M5Stack devices, using `Wire1` internal I2C bus * * I2C Address: 0x68 @@ -32,65 +32,70 @@ #define XSNS_85 85 #define XI2C_58 58 // See I2CDEVICES.md -#define MPU6886_ADDRESS 0x68 +#define MPU_ACCEL_ADDRESS 0x68 struct { - MPU6886 Mpu; - bool ready = false; + MPU_accel Mpu; + uint32_t mpu_model; int16_t ax=0, ay=0, az=0; // accelerator data int16_t gyx=0, gyy=0, gyz=0; // accelerator data -} mpu6886_sensor; + uint8_t bus = 0; // I2C bus + bool ready = false; +} mpu_sensor; /********************************************************************************************/ const char HTTP_MPU6686[] PROGMEM = - "{s}MPU6886 acc_x" "{m}%3_f G" "{e}" - "{s}MPU6886 acc_y" "{m}%3_f G" "{e}" - "{s}MPU6886 acc_z" "{m}%3_f G" "{e}" - "{s}MPU6886 gyr_x" "{m}%i dps" "{e}" - "{s}MPU6886 gyr_y" "{m}%i dps" "{e}" - "{s}MPU6886 gyr_z" "{m}%i dps" "{e}" + "{s}MPU%d acc_x" "{m}%3_f G" "{e}" + "{s}MPU%d acc_y" "{m}%3_f G" "{e}" + "{s}MPU%d acc_z" "{m}%3_f G" "{e}" + "{s}MPU%d gyr_x" "{m}%i dps" "{e}" + "{s}MPU%d gyr_y" "{m}%i dps" "{e}" + "{s}MPU%d gyr_z" "{m}%i dps" "{e}" ; -void MPU6686_Show(uint32_t json) { +void MPU_Show(uint32_t json) { if (json) { - ResponseAppend_P(PSTR(",\"MPU6886\":{\"AX\":%i,\"AY\":%i,\"AZ\":%i,\"GX\":%i,\"GY\":%i,\"GZ\":%i}"), - mpu6886_sensor.ax, mpu6886_sensor.ay, mpu6886_sensor.az, - mpu6886_sensor.gyx, mpu6886_sensor.gyy, mpu6886_sensor.gyz); + ResponseAppend_P(PSTR(",\"MPU%d\":{\"AX\":%i,\"AY\":%i,\"AZ\":%i,\"GX\":%i,\"GY\":%i,\"GZ\":%i}"), + mpu_sensor.mpu_model, + mpu_sensor.ax, mpu_sensor.ay, mpu_sensor.az, + mpu_sensor.gyx, mpu_sensor.gyy, mpu_sensor.gyz); } else { - float ax = mpu6886_sensor.ax / 1000.0f; - float ay = mpu6886_sensor.ay / 1000.0f; - float az = mpu6886_sensor.az / 1000.0f; - WSContentSend_PD(HTTP_MPU6686, &ax, &ay, &az, - mpu6886_sensor.gyx, mpu6886_sensor.gyy, mpu6886_sensor.gyz); + float ax = mpu_sensor.ax / 1000.0f; + float ay = mpu_sensor.ay / 1000.0f; + float az = mpu_sensor.az / 1000.0f; + WSContentSend_PD(HTTP_MPU6686, mpu_sensor.mpu_model, &ax, mpu_sensor.mpu_model, &ay, mpu_sensor.mpu_model, &az, + mpu_sensor.mpu_model, mpu_sensor.gyx, mpu_sensor.mpu_model, mpu_sensor.gyy, mpu_sensor.mpu_model, mpu_sensor.gyz); } } -void MPU6686Detect(void) { +void MPU_Detect(void) { #ifdef ESP32 - if (!I2cSetDevice(MPU6886_ADDRESS, 0)) { - if (!I2cSetDevice(MPU6886_ADDRESS, 1)) { return; } // check on bus 1 - mpu6886_sensor.Mpu.setBus(1); // switch to bus 1 - I2cSetActiveFound(MPU6886_ADDRESS, "MPU6886", 1); - } else { - I2cSetActiveFound(MPU6886_ADDRESS, "MPU6886", 0); + if (!I2cSetDevice(MPU_ACCEL_ADDRESS, 0)) { + if (!I2cSetDevice(MPU_ACCEL_ADDRESS, 1)) { return; } // check on bus 1 + mpu_sensor.bus = 1; + mpu_sensor.Mpu.setBus(mpu_sensor.bus); // switch to bus 1 } #else - if (!I2cSetDevice(MPU6886_ADDRESS)) { return; } - I2cSetActiveFound(MPU6886_ADDRESS, "MPU6886"); + if (!I2cSetDevice(MPU_ACCEL_ADDRESS)) { return; } #endif - mpu6886_sensor.Mpu.Init(); - mpu6886_sensor.ready = true; + if (mpu_sensor.Mpu.Init() == 0) { + char model_name[16]; + mpu_sensor.mpu_model = mpu_sensor.Mpu.getModel(); + snprintf_P(model_name, sizeof(model_name), PSTR("MPU%04d"), mpu_sensor.mpu_model); + I2cSetActiveFound(MPU_ACCEL_ADDRESS, model_name, mpu_sensor.bus); + mpu_sensor.ready = true; + } } -void MPU6886Every_Second(void) { - mpu6886_sensor.Mpu.getAccelDataInt(&mpu6886_sensor.ax, &mpu6886_sensor.ay, &mpu6886_sensor.az); - mpu6886_sensor.Mpu.getGyroDataInt(&mpu6886_sensor.gyx, &mpu6886_sensor.gyy, &mpu6886_sensor.gyz); +void MPU_Every_Second(void) { + mpu_sensor.Mpu.getAccelDataInt(&mpu_sensor.ax, &mpu_sensor.ay, &mpu_sensor.az); + mpu_sensor.Mpu.getGyroDataInt(&mpu_sensor.gyx, &mpu_sensor.gyy, &mpu_sensor.gyz); - // AddLog(LOG_LEVEL_DEBUG, PSTR(">> Acc x=%i y=%i z=%i gx=%i gy=%i gz=%i"), mpu6886_sensor.ax, mpu6886_sensor.ay, mpu6886_sensor.az, - // mpu6886_sensor.gyx, mpu6886_sensor.gyy, mpu6886_sensor.gyz); + // AddLog(LOG_LEVEL_DEBUG, PSTR(">> Acc x=%i y=%i z=%i gx=%i gy=%i gz=%i"), mpu_sensor.ax, mpu_sensor.ay, mpu_sensor.az, + // mpu_sensor.gyx, mpu_sensor.gyy, mpu_sensor.gyz); } @@ -104,19 +109,19 @@ bool Xsns85(uint8_t function) { bool result = false; if (FUNC_INIT == function) { - MPU6686Detect(); + MPU_Detect(); } - else if (mpu6886_sensor.ready) { + else if (mpu_sensor.ready) { switch (function) { case FUNC_EVERY_SECOND: - MPU6886Every_Second(); + MPU_Every_Second(); break; case FUNC_JSON_APPEND: - MPU6686_Show(1); + MPU_Show(1); break; #ifdef USE_WEBSERVER case FUNC_WEB_SENSOR: - MPU6686_Show(0); + MPU_Show(0); break; #endif // USE_WEBSERVER } @@ -124,5 +129,5 @@ bool Xsns85(uint8_t function) { return result; } -#endif // USE_MPU6886 +#endif // USE_MPU6886 USE_MPU_ACCEL #endif // USE_I2C \ No newline at end of file