Go Down

Topic: Strange issue with EEPROM (Read 394 times) previous topic - next topic

starinthe

Aug 22, 2019, 11:58 am Last Edit: Aug 22, 2019, 12:34 pm by starinthe
Good morning gentlemen,
    for days I have been trying to save, permanently, in the TTGO ESP32's eeprom.
Immediately after an EEPROM.commit() the hardware crashes (Interrupt wdt timeout on CPU0), the issue not happens ONLY if the value passed to the method is "1"!

Code: [Select]


// IT WORKS!
...
EEPROM.write(0, 1);
EEPROM.commit();

delay(1);
...


Code: [Select]

//IT DOESN'T WORK

...
long val = 5;
byte value = (val & 0xFF); // OR VALUE = 2

EEPROM.write(0, value);
EEPROM.commit();

delay(1);
...


I'm getting crazy... :(

Thank you all

pert

Did you call EEPROM.begin() first?

starinthe

#2
Aug 22, 2019, 12:30 pm Last Edit: Aug 22, 2019, 12:33 pm by starinthe
Yes, I did!

Code: [Select]

#include <lmic.h>
#include <hal/hal.h>
#include <SPI.h>
#include <EEPROM.h>

#define CHANNEL 0
int commandsFromSerial = 0;

static const PROGMEM u1_t NWKSKEY[16] = {  };
static const u1_t PROGMEM APPSKEY[16] = {  };
static const u4_t DEVADDR             = '';

void os_getArtEui (u1_t* buf) { }
void os_getDevEui (u1_t* buf) { }
void os_getDevKey (u1_t* buf) { }

static osjob_t sendjob;
long pulseCount;
long mem_val;
int address = 0;

/*****************************
 * TRASMISSION INTERVAL IN SECONDS
 *****************************/
const unsigned TX_INTERVAL = 10; // 3600 = 1h

/*****************************
 * PIN MAPPING
 *****************************/
const lmic_pinmap lmic_pins = {
    .nss = 18,
    .rxtx = LMIC_UNUSED_PIN,
    .rst = LMIC_UNUSED_PIN,
    .dio = {/*dio0*/ 26, /*dio1*/ 33, /*dio2*/ LMIC_UNUSED_PIN}
};

union ArrayToInteger {
  byte array[4];
  uint32_t integer;
};

long IRAM_ATTR EEPROMReadlong(long address){
  byte four = EEPROM.read(address);
  byte three = EEPROM.read(address + 1);
  byte two = EEPROM.read(address + 2);
  byte one = EEPROM.read(address + 3);

  return ((four << 0) & 0xFF) + ((three << 8) & 0xFFFF) + ((two << 16) & 0XFFFFFF) + ((one << 24) & 0xFFFFFFFF);
}
void IRAM_ATTR EEPROMWritelong(int address, long valu){

  //byte four = (valu & 0xFF);
  //byte three = ((valu >> 8) & 0xFF);
  //byte two = ((valu >> 16) & 0xFF);
  //byte one = ((valu >> 24) & 0xFF);

  //Serial.println(" - - - ");
  //Serial.println(four, HEX);
  //Serial.println(three, HEX);
  //Serial.println(two, HEX);
  //Serial.println(one, HEX);
  //Serial.println(" - - - ");

  portMUX_TYPE myMutex = portMUX_INITIALIZER_UNLOCKED;
  taskENTER_CRITICAL(&myMutex);
 
  //
  //
  //
  // HERE THE ERROR... If the .commit() is not invoked, the data is not saved in the EEPROM. In parallel, if the value is bigger than 1 the ESP32 reboots
  EEPROM.write(address, 0); //EEPROM.write(address, four);
  EEPROM.commit();
  delay(1);
  //
  //
  //
  //

  //EEPROM.write(address + 1, three);
  //EEPROM.commit();
  //EEPROM.write(address + 2, two);
  //EEPROM.commit();
  //EEPROM.write(address + 3, one);
  taskEXIT_CRITICAL(&myMutex);
}

/*****************************
 * do_send
 * TASK JOB
 *****************************/
void IRAM_ATTR do_send(osjob_t* j){
    if (LMIC.opmode & OP_TXRXPEND) {
        Serial.println(F("OP_TXRXPEND, not sending"));
    } else {


        ArrayToInteger converter;
       
        mem_val = EEPROMReadlong(address);

        Serial.println("Seee");
        Serial.println(mem_val);
       
        converter.array[0] = mem_val >> 24;
        converter.array[1] = mem_val >> 16;
        converter.array[2] = mem_val >> 8;
        converter.array[3] = mem_val;

        LMIC_setTxData2(1, converter.array, sizeof(converter.array), 0);
        Serial.println(F("Packet queued"));
    }
}

/*****************************
 * EVENT HANDLER
 *****************************/
void IRAM_ATTR onEvent (ev_t ev) {
    Serial.print(os_getTime());
    Serial.print(": ");
    switch(ev) {
        case EV_SCAN_TIMEOUT:
            Serial.println(F("EV_SCAN_TIMEOUT"));
            break;
        case EV_BEACON_FOUND:
            Serial.println(F("EV_BEACON_FOUND"));
            break;
        case EV_BEACON_MISSED:
            Serial.println(F("EV_BEACON_MISSED"));
            break;
        case EV_BEACON_TRACKED:
            Serial.println(F("EV_BEACON_TRACKED"));
            break;
        case EV_JOINING:
            Serial.println(F("EV_JOINING"));
            break;
        case EV_JOINED:
            Serial.println(F("EV_JOINED"));
            break;
        case EV_RFU1:
            Serial.println(F("EV_RFU1"));
            break;
        case EV_JOIN_FAILED:
            Serial.println(F("EV_JOIN_FAILED"));
            break;
        case EV_REJOIN_FAILED:
            Serial.println(F("EV_REJOIN_FAILED"));
            break;
        case EV_TXCOMPLETE:
            Serial.println(F("EV_TXCOMPLETE (includes waiting for RX windows)"));
            os_setTimedCallback(&sendjob, os_getTime()+sec2osticks(TX_INTERVAL), do_send);
            break;
         case EV_LOST_TSYNC:
            Serial.println(F("EV_LOST_TSYNC"));
            break;
        case EV_RESET:
            Serial.println(F("EV_RESET"));
            break;
        case EV_RXCOMPLETE:
            // data received in ping slot
            Serial.println(F("EV_RXCOMPLETE"));
            break;
        case EV_LINK_DEAD:
            Serial.println(F("EV_LINK_DEAD"));
            break;
        case EV_LINK_ALIVE:
            Serial.println(F("EV_LINK_ALIVE"));
            break;
         default:
            Serial.println(F("Unknown event"));
            break;
    }
}

/*****************************
 * catchPulse
 * Count pulses from GPIO34
 *****************************/
void IRAM_ATTR catchPulse()
{
  static unsigned long last_interrupt_time = 0;
  unsigned long interrupt_time = millis();
  if(interrupt_time - last_interrupt_time > 200)
  {
    ++pulseCount;
    EEPROMWritelong(address, pulseCount);
  }
  last_interrupt_time = interrupt_time;
}

/*****************************
 * SETUP
 *****************************/
void setup()
{
  Serial.begin(115200);
 
  if(!EEPROM.begin(512))
  {
    Serial.println("Failed to initialise EEPROM"); delay(10000);
  }
 
  Serial.println(F("Starting"));

 
  Serial.println("Erase now or die... 3 secs"); // 666 Erase the Flash Memory
  delay(2000);

  if (Serial.available() > 0)
  {
    commandsFromSerial = Serial.read(); // read the incoming byte:
    if (commandsFromSerial == 54) {
      Serial.println("ERASING FLASH MEMORY");
      EEPROMWritelong(address, 500);
    } else {
      Serial.println("Nothing to do.");
    }
  }

  pulseCount = EEPROMReadlong(address);
 
  pinMode(GPIO_NUM_32, INPUT_PULLUP);
  attachInterrupt(GPIO_NUM_32, catchPulse, RISING);

 
  os_init();
  LMIC_reset();

  #ifdef PROGMEM
    uint8_t appskey[sizeof(APPSKEY)];
    uint8_t nwkskey[sizeof(NWKSKEY)];
    memcpy_P(appskey, APPSKEY, sizeof(APPSKEY));
    memcpy_P(nwkskey, NWKSKEY, sizeof(NWKSKEY));
    LMIC_setSession (0x1, DEVADDR, nwkskey, appskey);
  #else
    LMIC_setSession (0x1, DEVADDR, NWKSKEY, APPSKEY);
  #endif

  #if defined(CFG_eu868)
    LMIC_setupChannel(0, 868100000, DR_RANGE_MAP(DR_SF12, DR_SF7),  BAND_CENTI);
  #elif defined(CFG_us915)
    LMIC_selectSubBand(1);
  #endif

  #define CHANNEL 0
  for (uint8_t i = 0; i<9;i++){
    if(i!=CHANNEL){
      LMIC_disableChannel(i);
    }
  }
 
  LMIC_setLinkCheckMode(0);
  LMIC_setDrTxpow(DR_SF12,14); 

  do_send(&sendjob);
}

void loop()
{
  os_runloop_once();
}


Go Up