Black Lives Matter - Action and Equality. ... Adafruit is open and shipping.
0

Feather esp8266 + LIS3DH not getting interrupts
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Feather esp8266 + LIS3DH not getting interrupts

by vdonovan2000 on Mon Jun 29, 2020 11:48 pm

Hi friends, I'm sure this is a simple problem, but it's driving me MAD. I have an LIS3DH sensor connected to a Feather ESP8266 using SPI. The sample code works exactly as it should, which tells me the SPI is working:
Code: Select all | TOGGLE FULL SIZE
// Basic demo for tap/doubletap readings from Adafruit LIS3DH

#include <Wire.h>
#include <SPI.h>
#include <Adafruit_LIS3DH.h>
#include <Adafruit_Sensor.h>

// Used for hardware & software SPI
#define LIS3DH_CS 16

// hardware SPI
Adafruit_LIS3DH lis = Adafruit_LIS3DH(LIS3DH_CS);

// Adjust this number for the sensitivity of the 'click' force
// this strongly depend on the range! for 16G, try 5-10
// for 8G, try 10-20. for 4G try 20-40. for 2G try 40-80
#define CLICKTHRESHHOLD 80

int started = 1;

void setup(void) {

  Serial.begin(9600);
  delay(500);
  Serial.println("Adafruit LIS3DH Tap Test!");
 
  if (! lis.begin()) {   // change this to 0x19 for alternative i2c address
    Serial.println("Couldnt start");
    started = 0;
    exit;
  }
 
  Serial.println("LIS3DH found!");
  lis.setRange(LIS3DH_RANGE_2_G);   // 2, 4, 8 or 16 G!
 
  Serial.print("Range = "); Serial.print(2 << lis.getRange()); 
  Serial.println("G");

  lis.setClick(1, CLICKTHRESHHOLD);
  delay(100);
}


void loop() {
  if (started == 0) {
    Serial.println("Not started");
    exit;
  }
  uint8_t click = lis.getClick();
  if (click == 0) return;
  if (! (click & 0x30)) return;
  Serial.print("Click detected (0x"); Serial.print(click, HEX); Serial.print("): ");
  if (click & 0x10) Serial.print(" single click");
  if (click & 0x20) Serial.print(" double click");
  Serial.println();

  delay(100);
  return;
}


However, when I try to use this same-ish code to handle interrupts, the Feather doesn't seem to detect them.
I'm connecting INT on the LIS3DH to GPIO 2 on the Feather, and running the following code, which never seems to receive an interrupt:
Code: Select all | TOGGLE FULL SIZE
// Basic demo for tap/doubletap readings from Adafruit LIS3DH

#include <Wire.h>
#include <SPI.h>
#include <Adafruit_LIS3DH.h>
#include <Adafruit_Sensor.h>

// Used for hardware & software SPI
#define LIS3DH_CS 16

// hardware SPI
Adafruit_LIS3DH lis = Adafruit_LIS3DH(LIS3DH_CS);

// Adjust this number for the sensitivity of the 'click' force
// this strongly depend on the range! for 16G, try 5-10
// for 8G, try 10-20. for 4G try 20-40. for 2G try 40-80
#define CLICKTHRESHHOLD 80

int started = 1;

//interrupt stuff
int intPin = 2;
volatile bool interruptFlag = false;
void ICACHE_RAM_ATTR LISinterrupt();

void setup(void) {

  Serial.begin(9600);
  delay(500);
  Serial.println("Adafruit LIS3DH Tap Test!");
 
  if (! lis.begin()) {   // change this to 0x19 for alternative i2c address
    Serial.println("Couldnt start");
    started = 0;
    exit;
  }
 
  Serial.println("LIS3DH found!");
  lis.setRange(LIS3DH_RANGE_2_G);   // 2, 4, 8 or 16 G!
 
  Serial.print("Range = "); Serial.print(2 << lis.getRange()); 
  Serial.println("G");

  lis.setClick(1, CLICKTHRESHHOLD);
  attachInterrupt(digitalPinToInterrupt(intPin), LISinterrupt, RISING);
  delay(100);
}


void loop() {
  if (started == 0) {
    Serial.println("Not started");
    exit;
  }
  Serial.print("Interrupt flag = ");
  Serial.println(interruptFlag);
  if (interruptFlag == true){
    interruptFlag = false;
  }
  /*
  uint8_t click = lis.getClick();
  if (click == 0) return;
  if (! (click & 0x30)) return;
  Serial.print("Click detected (0x"); Serial.print(click, HEX); Serial.print("): ");
  if (click & 0x10) Serial.print(" single click");
  if (click & 0x20) Serial.print(" double click");
  Serial.println();
*/
  delay(100);
}

void LISinterrupt()
{
  interruptFlag = true;
}


Any guidance, hints, or suggestions to try would be most welcome.

vdonovan2000
 
Posts: 18
Joined: Tue Mar 05, 2019 8:38 pm

Re: Feather esp8266 + LIS3DH not getting interrupts

by pneumatic on Mon Jul 06, 2020 12:18 am

If I understand correctly, you're trying to move the sampling code into an interrupt service routine (ISR). The problem is likely that while in an ISR, all interrupts are disabled, and a lot of hardware libraries depend on interrupts for communicating and signaling with their peripherals. Also things like delay() and millis() won't work because they depend on interrupts as well.

One way to approach this is to have the ISR just set a flag that you can check in your main code. This isn't ideal, because if there's a delay or long library call going on in your main routine, then the sampling might not happen in a timely manner. You can restructure your own code so that it will detect the flag and either bail-out of what it's doing to briefly call your sampling routine (this can make for some very complex and messy code though). However, if your code is stuck waiting for a library call to return (say waiting for a network event or something like that), then it might not be possible.

A second way to approach this is to re-enable interrupts in your ISR. This can be dangerous, since having an ISR interrupted by another interrupt can have weird side-effects, but if you're careful and keep the actions in the sub-calls relatively simple, then you might get away with it, but you also might brick your device.

pneumatic
 
Posts: 130
Joined: Sun Jul 26, 2009 3:59 pm
Location: Newark, CA

Re: Feather esp8266 + LIS3DH not getting interrupts

by pneumatic on Mon Jul 06, 2020 12:23 am

Nevermind.. I read your code and it looks like you've already implemented the first option, so I'm not sure what's going on, unless you've unwittingly broken the interrupt mechanism for the lis library somehow.

pneumatic
 
Posts: 130
Joined: Sun Jul 26, 2009 3:59 pm
Location: Newark, CA

Re: Feather esp8266 + LIS3DH not getting interrupts

by pneumatic on Mon Jul 06, 2020 12:47 am

Or... pin 2 isn't an interrupt pin. Actually according to this diagram, none of the exposed pins are interrupt pins.

pneumatic
 
Posts: 130
Joined: Sun Jul 26, 2009 3:59 pm
Location: Newark, CA

Re: Feather esp8266 + LIS3DH not getting interrupts

by vdonovan2000 on Tue Jul 07, 2020 1:14 am

Hi pneumatic, thanks for looking in to this. According to this post, all of the pins except 16 are interrupt pins:
viewtopic.php?f=19&p=704930

One of the commenters also notes that the diagram seems to be wrong.

I still haven't figured this out! I've revised the code to poll the sensor, but I'd much rather process interrupts.

thanks again,
Vince

vdonovan2000
 
Posts: 18
Joined: Tue Mar 05, 2019 8:38 pm

Please be positive and constructive with your questions and comments.