Create an Arduino ESP8266 library

0

I would like to create a Arduino library for an ESP8266or ESP32 microcontroller. I wrote a test library which running on an Arduino Nano board with no problem. Here the library cpp file:

#include "Test.h"

Test::Test(){
}

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

Here's the the header file :

#include <Arduino.h>

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

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

And last but not least the Arduino ino file:

#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);
}

Every compile fine with an Arduino Nano board as well as with ESP8266/ESP32 boards. When I run it on the Nano Board i get the expected result:

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

When I run it on the ESP8266 board I get the following crash result:

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
...

And last but not least on the ESP Development board I receive:

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.

So my question is: What I am doing wrong. What do i miss. What I haven't understood with Arduino IDE, cpp, pointers, etc.

Sorry I forgot: I use Arduino IDE 1.8.2

c++
arduino
esp8266
asked on Stack Overflow Apr 30, 2017 by plexidj • edited Apr 30, 2017 by plexidj

2 Answers

0

strcttest *tt; is your problem. You're not allocating memory for and creating an object of type strcttest - you're merely allocating memory for a pointer to an object of that type. Basically, the code should crash everywhere when your code gets to the line tt->a=1; The fact that it doesn't when run on the Nano is basically dumb luck..

Think of the case where you have a char* variable and then try to copy a string to it - it will crash too, since you dont have any storage space for the string itself - you only have a few bytes allocated that store the address of the string.

The following is a more reasonable implementation of your void loop() function:

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);
}

Another (slower, due to use of new and delete) implementation may look like this:

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);
}
answered on Stack Overflow Apr 30, 2017 by enhzflep
0

For ESP32 and ESP8266, have a excellent tool to help in crashes situations, like that You report.

This integrates to Arduino IDE

See it in: https://github.com/me-no-dev/EspExceptionDecoder

answered on Stack Overflow Mar 2, 2019 by JoaoLopesF

User contributions licensed under CC BY-SA 3.0