CP Express and attachInterrupt()

Play with it! Please tell us which board you're using.
For CircuitPython issues, ask in the Adafruit CircuitPython forum.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
Halbaffe
 
Posts: 4
Joined: Wed Nov 01, 2017 5:29 am

CP Express and attachInterrupt()

Post by Halbaffe »

Hello,

can i try to use attachInterrupt() with the CP Express for the internal IR ?
Like:

Code: Select all

  attachInterrupt(digitalPinToInterrupt(39), irDoSomething, CHANGE); // internal IR 
Many thanks

User avatar
adafruit_support_mike
 
Posts: 67446
Joined: Thu Feb 11, 2010 2:51 pm

Re: CP Express and attachInterrupt()

Post by adafruit_support_mike »

That should work.. the Circuit Playground Express uses the same SAMD21 microcontroller as the M0 Feather, and we know interrupts work there.

Arduino-level programming of the Circuit Express Playground is still kind of limited though. The best thing to do is try the code and see if it works.

User avatar
Halbaffe
 
Posts: 4
Joined: Wed Nov 01, 2017 5:29 am

Re: CP Express and attachInterrupt()

Post by Halbaffe »

after trying many things (and learning a lot) i reckognized that there is no way to try the interrupt on the left button or the IR Receiver.

Code: Select all

  
  attachInterrupt(digitalPinToInterrupt(39), irpause, CHANGE); // externer IR Empfänger
  attachInterrupt(digitalPinToInterrupt(4), buttonlinks, CHANGE); // Button links
  attachInterrupt(digitalPinToInterrupt(5), buttonrechts, CHANGE); // Button rechts
Of course i tried them alone. But only button Right (5) accepts the interrupt. The left button does not and the interrupt on (39) leads into an total fail. The program then simply doesnt start.
Is that a Bug? Or a feature? Do you know a solution with Arduino IDE?

Thx a lot

User avatar
adafruit_support_mike
 
Posts: 67446
Joined: Thu Feb 11, 2010 2:51 pm

Re: CP Express and attachInterrupt()

Post by adafruit_support_mike »

Try using pin number 26 instead of 39.

Looking through the board support package, it seems that IDs like D39 are only used in the Circuit Playground library. As far as the interrupt system is concerned, the IR recceiver is connected to pin D26 (shown at the top of the CPX pinout diagram):

https://cdn-learn.adafruit.com/assets/a ... 1507829017

User avatar
ScarletCleo
 
Posts: 3
Joined: Sat Sep 08, 2018 3:15 am

Re: CP Express and attachInterrupt()

Post by ScarletCleo »

I don't see any info about attachInterrupt with the left button. Does this mean that the left button cannot be monitored for presses via an interrupt?

User avatar
adafruit_support_mike
 
Posts: 67446
Joined: Thu Feb 11, 2010 2:51 pm

Re: CP Express and attachInterrupt()

Post by adafruit_support_mike »

If you're using a Circuit Playground Express, all of the microcontroller's pins support interrupts. You just have to give attachInterrupt() the correct pin number, which will be the same one used by digitalRead().

User avatar
ScarletCleo
 
Posts: 3
Joined: Sat Sep 08, 2018 3:15 am

Re: CP Express and attachInterrupt()

Post by ScarletCleo »

adafruit_support_mike wrote:If you're using a Circuit Playground Express, all of the microcontroller's pins support interrupts. You just have to give attachInterrupt() the correct pin number, which will be the same one used by digitalRead().
This seems to be false:

Here's my test case sketch:

Code: Select all

const uint8_t BUTTONA_PIN = 4;
const uint8_t BUTTONB_PIN = 5;

void setup() {
  Serial.begin(9600);
  pinMode(BUTTONA_PIN, INPUT_PULLDOWN);
  pinMode(BUTTONB_PIN, INPUT_PULLDOWN);

  attachInterrupt(
    digitalPinToInterrupt(BUTTONA_PIN),
    [](){
      Serial.println("button A interrupt");
    },
    RISING);
  attachInterrupt(
    digitalPinToInterrupt(BUTTONB_PIN),
    [](){
      Serial.println("button B interrupt");
    },
    RISING);
}

void loop() {
  if (digitalRead(BUTTONA_PIN)) {
    Serial.println("Polling-Button A pressed");
  }
  if (digitalRead(BUTTONB_PIN)) {
    Serial.println("Polling-Button B pressed");
  }
  delay(50);
}
Button B fires it's interrupt. Here's output when pressing B:

Code: Select all

button B interrupt
Polling-Button B pressed
Polling-Button B pressed
Polling-Button B pressed
Button A never fires. Here's output when pressing A:

Code: Select all

Polling-Button A pressed
Polling-Button A pressed
Polling-Button A pressed
You can clearly see that the buttonA interrupt is never fired while polling works. Also, I am 99% sure that ButtonA (the left one) does not fire any interrupt as I tried a number of other values in place of 4.

Do you see anything wrong with the above?

Thanks!

User avatar
adafruit_support_mike
 
Posts: 67446
Joined: Thu Feb 11, 2010 2:51 pm

Re: CP Express and attachInterrupt()

Post by adafruit_support_mike »

Hmm.. digging deeper, that seems to be more of a logical issue than a physical one.

The pin connected to button A does have an external interrupt connection (EXTINT[8]), but that interrupt can also be mapped to the pin connected to the Circuit Playground's TX pad.

The variant.cpp file for the Circuit Playground Express defines that pin as not having an interrupt:

Code: Select all

  { PORTA, 28, PIO_COM, PIN_ATTR_NONE, No_ADC_Channel, NOT_ON_PWM, NOT_ON_TIMER, EXTERNAL_INT_NONE },                           // GPIO D4 / Left Button
and instead maps EXTINT[8] to the TX pin:

Code: Select all

  { PORTB,  8, PIO_SERCOM_ALT, (PIN_ATTR_PWM|PIN_ATTR_TIMER), ADC_Channel2, PWM4_CH0, TC4_CH0, EXTERNAL_INT_8 }, // GPIO 1 / A7 / UART TX
With those limits, the pin can only work with digitalRead() and digitalWrite().

User avatar
ScarletCleo
 
Posts: 3
Joined: Sat Sep 08, 2018 3:15 am

Re: CP Express and attachInterrupt()

Post by ScarletCleo »

First of. Thank you for the honest information.

Is this a bug that can be fixed? If not, this is a huge limitation that should be called out on the product page and in documentation, and I feel like I've been lied to.

And I mean lied to by Adafruit, not by you. Thanks again for the expert analysis of the issue.

User avatar
adafruit_support_mike
 
Posts: 67446
Joined: Thu Feb 11, 2010 2:51 pm

Re: CP Express and attachInterrupt()

Post by adafruit_support_mike »

It's mostly an example of the limits you run into when working with 32-bit microcontrollers. They're highly configurable, but many of the options are mutually exclusive because of low-level details in the peripherals.

In this case, it's possible to configure the pin connected to button A to handle interrupts, but doing so would disable the Serial1 interface on the TX/RX pins. The SERCOM peripheral requires interrupt handling, and there's an interrupt collision between those two pins.

We certainly didn't intend it that way, and it isn't an obvious conflict.. I had to spend a couple hours comparing the schematics, board support package code, and datasheets to find out why interrupts aren't available on that pin in the default configuration.

Locked
Please be positive and constructive with your questions and comments.

Return to “Circuit Playground Classic, Circuit Playground Express, Circuit Playground Bluefruit”