Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache disabled but cached memory region accessed #1907

Closed
anandpanchal opened this issue Sep 28, 2018 · 4 comments
Closed

Cache disabled but cached memory region accessed #1907

anandpanchal opened this issue Sep 28, 2018 · 4 comments
Labels
Status: Stale Issue is stale stage (outdated/stuck)

Comments

@anandpanchal
Copy link

anandpanchal commented Sep 28, 2018

whatsapp image 2018-09-27 at 3 54 46 pm

Hello Sir @stickbreaker

i am getting the same error. i am reading two ir sensors on two gpios of esp32 using interrupts.
i am using wifi, interrupts, isr, spifs in my code.
can you please help me in this regard by reviewing my code.

#include <WiFi.h>
#include <NTPClient.h>
#include <WiFiUdp.h>

#include "SPIFFS.h"
// Replace with your network credentials
const char* ssid = "=====";
const char* password = "======";

// Define NTP Client to get time
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP);

// Variables to save date and time
String formattedDate;
String dayStamp;
String timeStamp;

volatile unsigned long fall_time1;
volatile unsigned long fall_time2;
volatile bool fall_flag1 = false;
volatile bool fall_flag2 = false;
volatile bool button_flag = false;

unsigned long previousMillis = 0; // last time update
unsigned long interval ; // interval at which to do something (milliseconds)

unsigned long interval_button ;
int led = 5;
int count = 0;

int button = 4;

int ir1 = 19;
int ir2 = 21;
int tr = 23;

#define duration 1500

volatile unsigned long duration_button = 0;
int wait = 10000;

unsigned long interval_1 = 0;
unsigned long interval_2 = 0;

hw_timer_t *timer0 = NULL;
portMUX_TYPE timerMux0 = portMUX_INITIALIZER_UNLOCKED;

volatile bool ledstat = 0;

void IRAM_ATTR onTimer0() //===================ESP32 TIMER ISR=====================================================//
{
portENTER_CRITICAL_ISR(&timerMux0);
ledstat = 1-ledstat;
digitalWrite(23, ledstat);
portEXIT_CRITICAL_ISR(&timerMux0);

}

void IRAM_ATTR fall_isr1() //=======================ISR IR1===========================================================//
{

unsigned long last_1 = millis();
if ((last_1 - interval_1) > duration)
{
fall_flag1 = true;

fall_time1 = last_1;
interval_1 = last_1;
//Serial.println("=====isr1====");
}
}

void IRAM_ATTR fall_isr2() //=========================ISR IR2===========================================================//
{

unsigned long last_2 = millis();
if((last_2 - interval_2) > duration)
{
fall_flag2 = true;
fall_time2 = last_2;
interval_2 = last_2;
//Serial.println("=======isr2=====");
}
}

void IRAM_ATTR button_isr()
{
interval_button = millis();
button_flag = true;

}

void setup()
{
Serial.begin(115200);

Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
// Print local IP address and start web server
Serial.println("");
Serial.println("WiFi connected.");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());

// Initialize a NTPClient to get time
timeClient.begin();
// Set offset time in seconds to adjust for your timezone, for example:
// GMT +1 = 3600
// GMT +8 = 28800
// GMT -1 = -3600
// GMT 0 = 0
//GMT +5:30 = 19900 Indian Time
timeClient.setTimeOffset(19900);

pinMode(led, OUTPUT); //=====================RELAY PIN====================================================//
pinMode(tr, OUTPUT); //=====================TRANSMITTER PIN===============================================//
digitalWrite(tr , LOW);
Serial.println("Monitoring interrupts: ");
pinMode(ir2, INPUT_PULLUP); //======================IR 2=========================================================//
pinMode(ir1, INPUT_PULLUP); //======================IR 1=========================================================//
pinMode(button, INPUT_PULLUP); //======================BUTTON=======================================================//
digitalWrite(button, HIGH);
attachInterrupt(digitalPinToInterrupt(21), fall_isr2, FALLING);
attachInterrupt(digitalPinToInterrupt(19), fall_isr1, FALLING);
attachInterrupt(digitalPinToInterrupt(button), button_isr, FALLING);

timer0 = timerBegin(0, 80, true); //===================ESP32 TIMER0=============================================//
timerAttachInterrupt(timer0, &onTimer0, true); // edge triggered
timerAlarmWrite(timer0, 14, true); // 14 * 1us, autoreload true.
timerAlarmEnable(timer0);

//SPIFFS.begin();
if(!SPIFFS.begin(true))
{
Serial.println("An Error has occurred while mounting SPIFFS");
return;
}

File file = SPIFFS.open("/test.txt", FILE_WRITE); // file open /create

if(!file)
{
Serial.println("There was an error opening the file for writing");
return;
}

}

void loop()
{

//volatile unsigned long interval_button = millis();
//File file = SPIFFS.open("/tes.txt", FILE_WRITE);
while(!timeClient.update())
{
timeClient.forceUpdate();
}
// The formattedDate comes with the following format:
// 2018-05-28T16:00:13Z
// We need to extract date and time
formattedDate = timeClient.getFormattedDate();
//Serial.println(formattedDate);

// Extract date
int splitT = formattedDate.indexOf("T");
dayStamp = formattedDate.substring(0, splitT);
//Serial.print("DATE: ");
//Serial.println(dayStamp);
// Extract time
timeStamp = formattedDate.substring(splitT+1, formattedDate.length()-1);
//Serial.print("HOUR: ");
//Serial.println(timeStamp);
//delay(1000);

unsigned long currentMillis = millis();

if (fall_flag1 && fall_flag2)
{
fall_flag1 = false;
fall_flag2 = false;

   if ((fall_time2 > fall_time1))   //================ENTRY ON==================================================//
   {
    digitalWrite(led, HIGH);
    interval = millis();
  //  Serial.println("======HIGH======");
    count = count + 1;
   // Serial.println("=====count=====");
   // Serial.println(count);

//=========================================================================================================================
File file = SPIFFS.open("/test.txt", FILE_WRITE); // file open /create

if(!file)
{
Serial.println("There was an error opening the file for writing");
return;
}

   if(file.print(dayStamp))                  // file write
        {
            Serial.println("Day File was written");;
        }
   else 
       {
            Serial.println("File write failed");
       }

       if(file.print(timeStamp))                  // file write
        {
            Serial.println("Time File was written");;
        }
   else 
       {
            Serial.println("File write failed");
       }

  file.close();


  File fileToAppend = SPIFFS.open("/test.txt", FILE_APPEND);     // file append

if(!fileToAppend){
    Serial.println("There was an error opening the file for appending");
    return;
}

if(fileToAppend.println(dayStamp)){
    Serial.println("Day File content was appended");
} else {
    Serial.println("File append failed");
}


if(fileToAppend.println(timeStamp)){
    Serial.println("Time File content was appended");
} else {
    Serial.println("File append failed");
}

fileToAppend.close();



File file2 = SPIFFS.open("/test.txt");                //===============file read ====================//
 
if(!file2){
    Serial.println("Failed to open file for reading");
    return;
}

Serial.println("File Content:");
Serial.write(file2.read()); 
while(file2.available())

{

    Serial.write(file2.read());
}

file2.close();

//=========================================================================================================
Serial.println ("===ON TIME===");
Serial.print("DATE: ");
Serial.println(dayStamp);
Serial.print("HOUR: ");
Serial.println(timeStamp);

   }
   if (fall_time1 > fall_time2)                  //=====================EXIT OFF=================================================================//
   {
    digitalWrite(led, LOW);
  //  Serial.println("=======LOW======");
    
    }

}

  if (button_flag)
  {
   //button_flag = false; 
   digitalWrite(led, HIGH); 
   //Serial.println("=======================================");
  }
  
  if (currentMillis - interval_button > wait)
  {
    //Serial.println("=========");
    button_flag = false;
    //digitalWrite(led, LOW);
  }

   if(currentMillis - interval == wait)             //====================TIMEOUT FUNCTION==============================================================//
 {
 
   Serial.println("====OFF TIME===");
   Serial.print("DATE: ");
   Serial.println(dayStamp);
   Serial.print("HOUR: ");
   Serial.println(timeStamp);
   delay(500);

 
   }
  if(currentMillis - interval > wait)             //====================TIMEOUT FUNCTION==============================================================//
 {
 
 digitalWrite(led , LOW);
 
 
   }  
  if(currentMillis - interval == 20000)             //====================TIMEOUT FUNCTION==============================================================//
 {
 
 fall_flag1 = false;
 fall_flag2 = false;
 Serial.println ("==========flag clear=====");

}

}

@atanisoft
Copy link
Collaborator

instead of volatile for each variable you are using in the ISRs try using DRAM_ATTR instead. volatile doesn't move it to IRAM/DRAM which is required for access in ISRs.

@anandpanchal
Copy link
Author

anandpanchal commented Oct 1, 2018

Hi @atanisoft and @stickbreaker

Thnaks for the comment.
rest of the details are same. the issue is if both the flags are true in ISR, ESP should comapre the time values and if time2 > time1, it should on the led and if time 2 < time 1, it should off the led . but it is not happening all the times. some times when time 2> time1, esp turn off the led and if time1 > time2, esp turns on the led . this is the cause of false cases.
can you please let me know what things i am doing wrong , here in the code. do i need to maintain some flags or any other thongs inside if's.

HOW CAN I CLEAR THE INTERRUPTS AFTER SOME INTERVAL OF TIME , LET SAY 10 SECONDS

Thanks in advance.

void loop()
{

   unsigned long currentMillis = millis();

  if (fall_flag1 && fall_flag2)                                                           //  if interrupts occurs at both the pins

{
fall_flag1 = false;
fall_flag2 = false;

   if ((fall_time2 > fall_time1) )           // if second beam has broken later, it means someone has entered inside and turn on the lights.
   {
    digitalWrite(5, HIGH);
    interval = millis();
    
   }
   if (fall_time1 > fall_time2)                                                       //  if first beam has broken later, it means someone has left and turn off the lights.
   {
    digitalWrite(5, LOW);
    
            
   }

}

@stale
Copy link

stale bot commented Aug 1, 2019

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the Status: Stale Issue is stale stage (outdated/stuck) label Aug 1, 2019
@stale
Copy link

stale bot commented Aug 15, 2019

This stale issue has been automatically closed. Thank you for your contributions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Status: Stale Issue is stale stage (outdated/stuck)
Projects
None yet
Development

No branches or pull requests

2 participants