创建一个Arduino ESP8266库

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

我想为ESP8266或ESP32微控制器创建一个Arduino库。我写了一个测试库,它在Arduino Nano板上运行没有问题。这里是库cpp文件:

#include "Test.h"

Test::Test(){
}

uint32_t Test::libTest(strcttest* t){
    uint32_t w;
    w = t->a;
    return w;
}

这是头文件:

#include <Arduino.h>

typedef struct {
    uint32_t a;
    uint32_t b;
}strcttest;

class Test
{
public:
    Test();
    uint32_t libTest(strcttest* t);
private:
};

最后但并非最不重要的是Arduino ino文件:

#include <Test.h>

//Instante Test
Test t;

void setup() {
  // put your setup code here, to run once:
    Serial.begin(9600);
    Serial.println("Start");
}

void loop() {
  // put your main code here, to run repeatedly:
  //Create structure
  strcttest *tt;
  tt->a=1;
  tt->b=2;
  //Output result
  Serial.println (t.libTest(tt));  
  delay(1000);
}

每个编译都可以使用Arduino Nano板以及ESP8266 / ESP32板。当我在Nano Board上运行时,我得到了预期的结果:

Start
1
1
1
1
1
1
1
1
1
...

当我在ESP8266板上运行时,我得到以下崩溃结果:

l*⸮⸮⸮⸮CI>⸮⸮⸮HB⸮⸮Start

Exception (28):
epc1=0x402024f8 epc2=0x00000000 epc3=0x00000000 excvaddr=0x00000000 depc=0x00000000

ctx: cont 
sp: 3ffef7d0 end: 3ffef9a0 offset: 01a0

>>>stack>>>
3ffef970:  feefeffe 00000000 3ffee950 40201eb4  
3ffef980:  feefeffe feefeffe 3ffee96c 40202340  
3ffef990:  feefeffe feefeffe 3ffee980 40100108  
<<<stack<<<
7!a!*6⸮⸮⸮Start

Exception (28):
epc1=0x402024f8 epc2=0x00000000 epc3=0x00000000 excvaddr=0x00000000 depc=0x00000000

ctx: cont 
sp: 3ffef7d0 end: 3ffef9a0 offset: 01a0

>>>stack>>>
3ffef970:  feefeffe 00000000 3ffee950 40201eb4  
3ffef980:  feefeffe feefeffe 3ffee96c 40202340  
3ffef990:  feefeffe feefeffe 3ffee980 40100108  
<<<stack<<<
ĜBs⸮`⸮"⸮⸮Start
...

最后但并非最不重要的是我收到的ESP开发板:

i%M/⸮`⸮i%M7
⸮⸮%Q=qU=\Md⸮aGd<$⸮Start
Guru Meditation Error of type LoadProhibited occurred on core  1. Exception was unhandled.
Register dump:
PC      : 0x400dde93  PS      : 0x00060030  A0      : 0x800d0570  A1      : 0x3ffc7390  
A2      : 0x3ffc1c30  A3      : 0x00000000  A4      : 0x0800001c  A5      : 0xffffffff  
A6      : 0xffffffff  A7      : 0x00060d23  A8      : 0x800832e9  A9      : 0x3ffc7380  
A10     : 0x00000003  A11     : 0x00060023  A12     : 0x00060020  A13     : 0x00000003  
A14     : 0x00000001  A15     : 0x00000000  SAR     : 0x0000001f  EXCCAUSE: 0x0000001c  
EXCVADDR: 0x00000000  LBEG    : 0x400014fd  LEND    : 0x4000150d  LCOUNT  : 0xffffffff  

Backtrace: 0x400dde93:0x3ffc7390 0x400d0570:0x3ffc73b0 0x400d79b0:0x3ffc73d0

CPU halted.

所以我的问题是:我做错了什么。我错过了什么我没有用Arduino IDE,cpp,指针等理解的东西。

对不起,我忘记了:我使用的是Arduino IDE 1.8.2

c++ arduino esp8266
2个回答
0
投票

strcttest *tt;是你的问题。你没有为strcttest分配内存和创建一个类型的对象 - 你只是为指向该类型对象的指针分配内存。基本上,当你的代码到达行tt->a=1;时,代码应该在任何地方崩溃事实上,当它运行在Nano上时,它基本上是愚蠢的运气..

想想你有一个char *变量然后尝试将字符串复制到它的情况 - 它也会崩溃,因为你没有字符串本身的任何存储空间 - 你只分配了几个字节来存储地址字符串。

以下是void loop()函数的更合理的实现:

void loop() {
  // put your main code here, to run repeatedly:
  //Create structure
  strcttest tt;
  tt.a=1;
  tt.b=2;
  //Output result
  Serial.println (t.libTest(&tt));  
  delay(1000);
}

另一个(较慢,由于使用new和delete)实现可能如下所示:

void loop() {
  // put your main code here, to run repeatedly:
  //Create structure
  strcttest *tt = new strcttest;
  tt->a=1;
  tt->b=2;
  //Output result
  Serial.println (t.libTest(tt));
  delete tt;  
  delay(1000);
}

0
投票

对于ESP32和ESP8266,有一个很好的工具来帮助崩溃情况,就像你报告的那样。

这与Arduino IDE集成

见:https://github.com/me-no-dev/EspExceptionDecoder

© www.soinside.com 2019 - 2024. All rights reserved.