ESP32 作为 i2c 从内存泄漏

问题描述 投票:0回答:0

代码适用于收到的前 5 条消息,然后微重置。 ESP32 以树莓派为主从模式工作。 该板是 ESP32 C3 和 Arduino 平台。 我发现的另一个问题是

myData.device_state
值没有被正确存储。

#include <Arduino.h>
#include <Wire.h>

#define DEBUG

#define SLAVE_ADDRESS 0x08
int SDA_PIN = 7;
int SCL_PIN = 6;

unsigned char I2C_COMMAND = 0;
volatile boolean RECEIVE_FLAG = false;
char INCOMING_STR[31];
String SPLITTED_STRING[30];
int SPLITTER_COUNT = 0;

typedef struct struct_message
{
  String room;
  String device_name;
  int device_state;
  int r;
  int g;
  int b;
} struct_message;

struct_message myData;

void receiveEvent(int BYTE_COUNT)
{
  I2C_COMMAND = Wire.read();

  for (int i = 1; i < BYTE_COUNT; i++)
  {
    INCOMING_STR[i] = Wire.read();
    INCOMING_STR[i + 1] = '\0';
  }
  for (int i = 0; i < BYTE_COUNT; ++i)
    INCOMING_STR[i] = INCOMING_STR[i + 1];

  RECEIVE_FLAG = true;
}

void deserialize(String INCOMING_STR)
{
#ifdef DEBUG
    Serial.println(String(INCOMING_STR));
#endif
  while (INCOMING_STR.length() > 0)
  {
    int index = INCOMING_STR.indexOf(',');
    if (index == -1)
    {
      SPLITTED_STRING[SPLITTER_COUNT++] = INCOMING_STR;
      break;
    }
    else
    {
      SPLITTED_STRING[SPLITTER_COUNT++] = INCOMING_STR.substring(0, index);
      INCOMING_STR = INCOMING_STR.substring(index + 1);
    }
  }

  myData.room = SPLITTED_STRING[0];
  myData.device_name = SPLITTED_STRING[1];
  myData.device_state = SPLITTED_STRING[2].toInt();
  myData.r = SPLITTED_STRING[3].toInt();
  myData.g = SPLITTED_STRING[4].toInt();
  myData.b = SPLITTED_STRING[5].toInt();
}

void setup()
{
  Wire.begin(SLAVE_ADDRESS, SDA_PIN, SCL_PIN, 100000);
  Wire.onReceive(receiveEvent);
  Serial.begin(9600);

#ifdef DEBUG
  Serial.println("");
  Serial.println("ESP32 HAT!");
  Serial.println("I2C Ready!");
#endif
}

void loop()
{
  if (RECEIVE_FLAG == true)
  {
    deserialize(INCOMING_STR);
    RECEIVE_FLAG = false;
  }
}

这是错误信息。

assert failed: heap_caps_free heap_caps.c:339 (heap != NULL && "free() target pointer is outside heap areas")
Core  0 register dump:
MEPC    : 0x40381cf0  RA      : 0x40384c04  SP      : 0x3fc93ee0  GP      : 0x3fc8b600  
TP      : 0x3fc89f10  T0      : 0x37363534  T1      : 0x7271706f  T2      : 0x33323130  
S0/FP   : 0x3fc9407d  S1      : 0x0000005a  A0      : 0x3fc93f44  A1      : 0x3fc8b4cd  
A2      : 0x00000001  A3      : 0x00000029  A4      : 0x00000001  A5      : 0x3fc8d000  
A6      : 0x7a797877  A7      : 0x76757473  S2      : 0x3fc93f38  S3      : 0x00000001  
S4      : 0x3fc93f38  S5      : 0x4038206c  S6      : 0x3fc8d000  S7      : 0x00000000  
S8      : 0x00000000  S9      : 0x00000000  S10     : 0x00000000  S11     : 0x00000000  
T3      : 0x6e6d6c6b  T4      : 0x6a696867  T5      : 0x66656463  T6      : 0x62613938  
MSTATUS : 0x00001801  MTVEC   : 0x40380001  MCAUSE  : 0x00000007  MTVAL   : 0x00000000  
MHARTID : 0x00000000  

Stack memory:
3fc93ee0: 0xa5a5a5a5 0xa5a5a5a5 0x3c0348a4 0x40389ab6 0x3fc8d7c0 0x00000000 0x0000000c 0x3fc8b4cc
3fc93f00: 0x00000010 0x0000000c 0x00000804 0x00393333 0xa5a5a5a5 0x3fc8bdf4 0x3c0348a4 0x3fc8bdd8
3fc93f20: 0x3c03491d 0x3fc8be04 0x3fc93f0c 0x3fc8be08 0x3c034974 0x3fc8b4cc 0x00000000 0x00000000
3fc93f40: 0x00000000 0x65737361 0x66207472 0x656c6961 0x68203a64 0x5f706165 0x73706163 0x6572665f
3fc93f60: 0x65682065 0x635f7061 0x2e737061 0x33333a63 0x68282039 0x20706165 0x4e203d21 0x204c4c55
3fc93f80: 0x22202626 0x65657266 0x74202928 0x65677261 0x6f702074 0x65746e69 0x73692072 0x74756f20
3fc93fa0: 0x65646973 0x61656820 0x72612070 0x22736165 0x00000029 0x00000000 0x3fc95e3c 0x40385362
3fc93fc0: 0x3fc8d5d8 0x00000000 0x00000000 0x00000000 0x3fc8d7c0 0x00000000 0x00000002 0x3c030550
3fc93fe0: 0x00000000 0x00000000 0x00000002 0x40386b46 0x00000000 0x00000000 0x3fc8d5d8 0x00000000
3fc94000: 0x00000024 0x00000020 0x3fc95964 0xa83865a8 0x00000000 0xffffffff 0x00000000 0x00000000
3fc94020: 0x3c030140 0x3fc8c670 0x3fc9408c 0x4038206c 0x3c033e74 0x00000000 0x3fc9408c 0x3c030140
3fc94040: 0x00000000 0xffffffff 0x3fc9408c 0x420016a6 0x3fc9408c 0x00000000 0x00000021 0x3fc8c460
3fc94060: 0x3fc8d000 0x00000001 0x3fc8c670 0x42001782 0x3fc8d000 0x00000001 0x3fc940cc 0x420000ca
3fc94080: 0xfa000000 0x00000000 0x00000000 0x00000031 0x00000000 0x00000000 0x81c94000 0xa83865a8
3fc940a0: 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x3fc8d5a8 0x3fc8d000 0x420001f0
3fc940c0: 0x00000000 0x00000000 0x00000000 0x3fc8fda4 0x0000001f 0x00000005 0x42000c9a 0xa83865a8
3fc940e0: 0x00000000 0x3fc8d000 0x42000c9a 0x4200356c 0x00000000 0x00000000 0x00000000 0x40386fd2
3fc94100: 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5
3fc94120: 0xa5a5a5a5 0xbaad5678 0x00000160 0xabba1234 0x00000154 0x3fc94000 0x000101a9 0x3fc8d14c
3fc94140: 0x3fc8d14c 0x3fc94134 0x3fc8d144 0x00000018 0xdd59b7b7 0xe736a17d 0x3fc94134 0x00000000
3fc94160: 0x00000001 0x3fc92124 0x706f6f6c 0x6b736154 0x96fa5f00 0x00ce3434 0x00000000 0x3fc94120
3fc94180: 0x00000001 0x00000000 0x00000000 0x00000000 0x00000000 0x3fc8e1b4 0x3fc8e21c 0x3fc8e284
3fc941a0: 0x00000000 0x00000000 0x00000001 0x00000000 0x00000000 0x00000000 0x42017f4e 0x00000000
3fc941c0: 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000
3fc941e0: 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000
3fc94200: 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000
3fc94220: 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000
3fc94240: 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000
3fc94260: 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000
3fc94280: 0x00000000 0x88000000 0xbaad5678 0x00000060 0xabba1234 0x00000054 0x00000000 0x3fc94298
3fc942a0: 0x00000000 0x00000000 0x00000000 0x3fc942b0 0xffffffff 0x3fc942b0 0x3fc942b0 0x00000000
3fc942c0: 0x3fc942c4 0xffffffff 0x3fc942c4 0x3fc942c4 0x00000001 0x00000001 0x00000000 0xbf00ffff



ELF file SHA256: 0000000000000000

Rebooting...
ESP-ROM:esp32c3-api1-20210207
Build:Feb  7 2021
rst:0x3 (RTC_SW_SYS_RST),boot:0xc (SPI_FAST_FLASH_BOOT)
Saved PC:0x403818b2
SPIWP:0xee
mode:DIO, clock div:1
load:0x3fcd6100,len:0x438
load:0x403ce000,len:0x918
load:0x403d0000,len:0x24e4
entry 0x403ce000

我在 I2C 端口上标记了一个来自 NodeRed 节点 (node-red-contrib-i2c) 的 20 字节数据包。

我试过

Wire.flush()
但没有任何改变。

arduino esp32 i2c
© www.soinside.com 2019 - 2024. All rights reserved.