ESP32 与多个 I2C 设备偶尔崩溃,堆问题?

如何解决ESP32 与多个 I2C 设备偶尔崩溃,堆问题?

我有一个 ESP32(M5Stack Core Grey),它通过 I2C 连接到 PMIC(带有 I2C 寄存器的定制 IP5306)和加速度计 (MPU9250)。两个库大部分时间同时独立工作。有时,我会遇到 2 个错误之一,我怀疑这是我尝试调用这两个设备的代码中的某种类型的冲突:

assertion "head != NULL" failed: file "/home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/heap/multi_heap_poisoning.c",line 245,function: multi_heap_realloc
Decoding stack results
0x4008d774: invoke_abort at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/esp32/panic.c line 155
0x4008d9a5: abort at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/esp32/panic.c line 170
0x400ec7ef: __assert_func at ../../../.././newlib/libc/stdlib/assert.c line 63
0x4008d401: multi_heap_free at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/heap/multi_heap_poisoning.c line 214
0x40085456: heap_caps_free at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/heap/heap_caps.c line 268
0x40085c2d: _free_r at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/newlib/syscalls.c line 42
0x400e24fb: i2cFlush at C:\users\quan\.platformio\packages\framework-arduinoespressif32\cores\esp32\esp32-hal-i2c.c line 1604
0x400e2e0c: i2cRead at C:\users\quan\.platformio\packages\framework-arduinoespressif32\cores\esp32\esp32-hal-i2c.c line 1649
0x400dc025: TwoWire::readTransmission(unsigned short,unsigned char*,unsigned short,bool,unsigned int*) at C:\users\quan\.platformio\packages\framework-arduinoespressif32\libraries\Wire\src\Wire.cpp line 139
0x400dc0d9: TwoWire::requestFrom(unsigned short,unsigned char,bool) at C:\users\quan\.platformio\packages\framework-arduinoespressif32\libraries\Wire\src\Wire.cpp line 193
0x400dc155: TwoWire::requestFrom(int,int) at C:\users\quan\.platformio\packages\framework-arduinoespressif32\libraries\Wire\src\Wire.cpp line 296
0x400dafcc: getBatteryGauge() at src\myPower.cpp line 16
0x400daff8: powerLoop(void*) at src\myPower.cpp line 37
0x40089eb1: vPortTaskWrapper at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/freertos/port.c line 143

然后有时我会得到以下信息:

assertion "head != NULL" failed: file "/home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/heap/multi_heap_poisoning.c",function: multi_heap_realloc
Decoding stack results
0x4008d774: invoke_abort at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/esp32/panic.c line 155
0x4008d9a5: abort at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/esp32/panic.c line 170
0x400ec7eb: __assert_func at ../../../.././newlib/libc/stdlib/assert.c line 59
0x4008d459: multi_heap_realloc at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/heap/multi_heap_poisoning.c line 245
0x400854c6: heap_caps_realloc at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/heap/heap_caps.c line 297
0x4008552a: heap_caps_realloc_default at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/heap/heap_caps.c line 163
0x40085c3d: _realloc_r at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/newlib/syscalls.c line 47
0x400e22ca: i2cAddQueue at C:\users\quan\.platformio\packages\framework-arduinoespressif32\cores\esp32\esp32-hal-i2c.c line 1057
0x400e234f: i2cAddQueueWrite at C:\users\quan\.platformio\packages\framework-arduinoespressif32\cores\esp32\esp32-hal-i2c.c line 1081
0x400e2d4c: i2cWrite at C:\users\quan\.platformio\packages\framework-arduinoespressif32\cores\esp32\esp32-hal-i2c.c line 1617
0x400dc001: TwoWire::writeTransmission(unsigned short,bool) at C:\users\quan\.platformio\packages\framework-arduinoespressif32\libraries\Wire\src\Wire.cpp line 133
0x400dc055: TwoWire::endTransmission(bool) at C:\users\quan\.platformio\packages\framework-arduinoespressif32\libraries\Wire\src\Wire.cpp line 158
0x400dc558: CommUtil::readByte(unsigned char,unsigned char*) at C:\users\quan\.platformio\lib\M5Stack_ID1851\src\utility\CommUtil.cpp line 98
0x400dfcf6: MPU9250::readByte(unsigned char,unsigned char) at C:\users\quan\.platformio\lib\M5Stack_ID1851\src\utility\MPU9250.cpp line 438
0x400d1bce: readIMU(void*) at src\accelerometer.cpp line 75
0x40089eb1: vPortTaskWrapper at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/freertos/port.c line 143

这两个错误看起来很相似,但它们是从不同的地方调用的。我已将 PMIC 和 IMU 读取放在单独的 FreeRTOS 任务中。我是否需要确保在每次 I2C 读取之前获取一个信号量并在完成后将其返回?我已经把它放在我显式读取值的代码中,但可能遗漏了一些地方。大多数 M5Stack 代码一次仅演示一个模块,因此当您组合多个传感器时,我不确定最佳实践。非常感谢任何指导。

解决方法

您需要使用互斥锁来访问来自不同任务的硬件。

  //GLOBAL
  SemaphoreHandle_t xI2CSemaphore;            //Shared I2C Resource Mutex
  
  //PROGRAM SETUP
  if ( xI2CSemaphore == NULL )
  {
    xI2CSemaphore = xSemaphoreCreateMutex();
    if ( ( xI2CSemaphore ) != NULL )
      xSemaphoreGive( ( xI2CSemaphore ) );
  }



 //WHEN USING IN A TASK...
 if ( xSemaphoreTake( xI2CSemaphore,( TickType_t ) 250 ) == pdTRUE )
 {

 xSemaphoreGive( xI2CSemaphore ); // Now free or "Give" the Serial Port for others.
 }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 <select id="xxx"> SELECT di.id, di.name, di.work_type, di.updated... <where> <if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 <property name="dynamic.classpath" value="tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams['font.sans-serif'] = ['SimHei'] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -> systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping("/hires") public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate<String
使用vite构建项目报错 C:\Users\ychen\work>npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-