Coder Social home page Coder Social logo

Comments (6)

ladyada avatar ladyada commented on August 29, 2024 1

ooooh thats interesting and not something we knew about but yeah irq's are not as simple as we'd like and arduino uses A LOT of them behind the scenes.
can you submit a PR with this example sketch? call it like "seesaw_ESP32_IRQ" ?

from adafruit_seesaw.

theficus avatar theficus commented on August 29, 2024 1

Yes, absolutely. Hopefully it can save else from falling into this same trap in the future. I’ll try to get a PR done this weekend.

from adafruit_seesaw.

theficus avatar theficus commented on August 29, 2024

I did some extensive debugging into this issue this evening and I believe I understand what's going on. It appears that what is happening is when the ISR is calling into the ESP TwoWire library to read data from the I2C bus, it will ultimately fail with I2C_ERROR_BUSY and this is unrecoverable.

Digging further into this, it appears that there are some known limitations on the ESP platform specific to doing I2C operations from within an ISR due to how its FreeRTOS handles task switching. The recommendation for this seems to be to use a ESP task queue to do the work, and have the ISR simply signal the queue to do something. I played around with this some and actually made some pretty good progress.

Here's some prototype sample code (incomplete -- based on the original sample):

QueueHandle_t joyQueue;

void onButtonPress()
{
    int i = 0;
    xQueueSend(joyQueue, &i, portMAX_DELAY);
}

void joyTaskConsumer(void* p)
{
    Serial.println("In queue!");
    int element;
    size_t i = 0;
    while(true)
    {
        Serial.printf("q=%zu\n", i);
        xQueueReceive(joyQueue, &element, portMAX_DELAY);
        uint32_t v = 0;
        v = ss.digitalReadBulk(s_button_mask);
        Serial.println(v, BIN);
        i++;
    }
    vTaskDelete(NULL);
}

void setup()
{
    Serial.begin(115200);
    joyQueue = xQueueCreate(10, sizeof(int));
    ss.begin(0x49);
    ss.pinModeBulk(s_button_mask, INPUT_PULLUP);
    ss.setGPIOInterrupts(s_button_mask, true);
    pinMode(JOY_IRQ_PIN, INPUT);
    
    xTaskCreate(
        joyTaskConsumer,
        "JoyTaskConsumer",
        10000,
        NULL,
        1,
        NULL);

    attachInterrupt(JOY_IRQ_PIN, onButtonPress, FALLING);
}

This actually seems to work reasonably well -- I am seeing events fire pretty much immediately when the button is depressed and released and on every subsequent button press, and the queue task simply cedes to any other work on the CPU until something gets queued which is basically what I want. In my testing I am running into a new issue where pressing too many buttons at once or too quickly can eventually cause the ESP32 to crash which I'm still troubleshooting (I think this is due to a race with the Serial.println statement), but in all this is working a million times better than before!

It's possible I could also use a semaphore or some other similar approach to do this if I wanted to and I'll do some more experimentation. It seems like the key thing here is to keep the actual work of communicating with the I2C bus out of the ISR.

So in conclusion this seems more like an issue specific to the interrupt use case with the ESP platform rather than a simple bug or PEBKAC problem. This would probably be something useful to call out in the docs or samples as this is something that can come as quite a shock to someone who's trying to use interrupts but is new to the ESP platform.

Further reading:

from adafruit_seesaw.

ladyada avatar ladyada commented on August 29, 2024

awesome thankx :)

from adafruit_seesaw.

theficus avatar theficus commented on August 29, 2024

I figured out a fix to the crashes in my sample and also added support for reading the analog stick in a separate task. This allows the ESP32 to handle all controller inputs without blocking other work. I’ll update the sample with this when I get a chance and will close this Issue out when that’s done.

from adafruit_seesaw.

theficus avatar theficus commented on August 29, 2024

Going to close this after checking in some sample code demonstrating an approach for getting this to work on an ESP32.

Hopefully this saves somebody else some time and headaches. :)

from adafruit_seesaw.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.