mxtommy / SigkSens

ESP8266 based SignalK Wifi Sensors
Apache License 2.0
38 stars 8 forks source link

Esp32 interups #64

Open dmdelorme opened 5 years ago

dmdelorme commented 5 years ago

https://github.com/mxtommy/SigkSens/blob/0e13d46a73b21332cc6ed4821a5dd390d4842143/SigkSens/src/sensors/digitalIn/digitalIn.cpp#L106

This may or may not work as hoped/expected depending on where and when the counter values change. ie if a interrupt is generated while changing value.. in a different area of code

I have included a snippet of code. for esp32 external interrupt.


`const byte interruptPin = 25;
volatile int interruptCounter = 0;
int numberOfInterrupts = 0;

portMUX_TYPE mux = portMUX_INITIALIZER_UNLOCKED;

void IRAM_ATTR handleInterrupt() {
  portENTER_CRITICAL_ISR(&mux); // this prevent collisions during a write
  interruptCounter++;
  portEXIT_CRITICAL_ISR(&mux); // release the lock
}

void setup() {

  Serial.begin(115200);
  Serial.println("Monitoring interrupts: ");
  pinMode(interruptPin, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(interruptPin), handleInterrupt, FALLING);

}

void loop() {

  if(interruptCounter>0){

      portENTER_CRITICAL(&mux); //lock to prevent write collision while we change the value
      interruptCounter--;
      portEXIT_CRITICAL(&mux);// unlock

      numberOfInterrupts++;
      Serial.print("An interrupt has occurred. Total: ");
      Serial.println(numberOfInterrupts);
  }
}`
dmdelorme commented 5 years ago

ICACHE_RAM_ATTR vs IRAM_ATTR on an esp32 when ICACHE is used the code is stored in flash slower when IRAM is used the code is stored in RAM faster.. https://docs.espressif.com/projects/esp-idf/en/latest/api-guides/general-notes.html

Not a hard fix.

I not 100% on this ReactESP does this different and i do not full understand what they are doing.

mxtommy commented 5 years ago

Yup need to update that stuff for esp32 :)

ReactESP is a cool little library to help with timing and stuff. rather than having a bunch of timers running ReactESP handles it.