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

Fix attach_interrupt(...) for esp-idf framework #2416

Merged
merged 5 commits into from Oct 1, 2021

Conversation

mmakaay
Copy link
Member

@mmakaay mmakaay commented Sep 29, 2021

What does this implement/fix?

The attach_interrupt(...) implementation for the esp32 component uses ESP_INTR_FLAG_LEVEL5 for gpio_install_isr_service(...). This interrupt level requires handlers that are written in assembly. Therefore C-code handler are not acceptable and using those results in a failed interrupt setup.

Additionally, the return value of gpio_install_isr_service(...) was not checked, resulting in a LoadProhibited abort, because of a NULL pointer exception. further down the road. I added a check to see if it actually returns ESP_OK.

Note:
Adding some logging in case of a fail would be a nice to have, but this code currently lives in the gpio_idf.h file and not the gpio_idf.cpp file. That makes the structure different from the other .h files and I shouldn't define a TAG in this .h file for logging purposes.
Is there a reason why this code is in the .h file or is it okay if I split declaration and implementation between respectively the .h and the .cpp file here?

Types of changes

  • Bugfix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Other

Related issue or feature (if applicable): fixes esphome/issues#2483

Pull request in esphome-docs with documentation (if applicable): esphome/esphome-docs#

Test Environment

  • ESP32 (unicore, not 100% sure if that matters)
  • ESP8266

Example entry for config.yaml:

# Example config.yaml
esp32:
  board: esp32doit-devkit-v1
  framework:
    type: esp-idf
    sdkconfig_options:
      CONFIG_FREERTOS_UNICORE: y

# Just an example, I saw the behavior with all code that attached a pin interrupt.
sensor:
  - platform: rotary_encoder
    name: "Rotary Encoder"
    pin_a: GPIO16
    pin_b: GPIO17

Checklist:

  • The code change is tested and works locally.
  • Tests have been added to verify that the new code works (under tests/ folder).

If user exposed functionality or configuration variables are added/changed:

@probot-esphome
Copy link

Hey there @esphome/core, mind taking a look at this pull request as it has been labeled with an integration (esp32) you are listed as a code owner for? Thanks!
(message by CodeOwnersMention)

@mmakaay
Copy link
Member Author

mmakaay commented Sep 29, 2021

Tomorrow, I'll dig up a normal dual core ESP32 to see if the fix is only applicable to ESP32 unicore.
If this is the case, then I'll implement something to differentiate the handling between unicore and dual core.

@mmakaay
Copy link
Member Author

mmakaay commented Sep 29, 2021

Okay, this PR is useful for a regular dual core EPS32 as well. I tried flashing one with the rotary encoder for testing the interrupt setup, and this is what I ended up with:

[12:50:26]I (407) cpu_start: Starting scheduler on PRO CPU.
[12:50:26]I (0) cpu_start: Starting scheduler on APP CPU.
[12:50:26][I][app:029]: Running through setup()...
[12:50:26][C][rotary_encoder:127]: Setting up Rotary Encoder 'Rotary Encoder'...
[12:50:26][D][esp-idf:000]: I (210) gpio: GPIO[16]| InputEn: 1| Guru Meditation Error: Core  1 panic'ed (LoadProhibited). Exception was unhandled.
[12:50:26]
[12:50:26]Core  1 register dump:
[12:50:26]PC      : 0x4014e123  PS      : 0x00060533  A0      : 0x800e2b0c  A1      : 0x3ffbf180  
[12:50:26]A2      : 0x00000000  A3      : 0x00000000  A4      : 0x00000010  A5      : 0x3ffb0020  
[12:50:26]A6      : 0x00000028  A7      : 0x00020000  A8      : 0x800e2809  A9      : 0x3ffbf150  
[12:50:26]A10     : 0x3ffb0024  A11     : 0x00000010  A12     : 0x00000010  A13     : 0x00060523  
[12:50:26]A14     : 0x00000001  A15     : 0x00000011  SAR     : 0x00000010  EXCCAUSE: 0x0000001c  
[12:50:26]EXCVADDR: 0x00000000  LBEG    : 0x4000c2e0  LEND    : 0x4000c2f6  LCOUNT  : 0xffffffff  
[12:50:26]
[12:50:26]Backtrace:

0x4014e120: esp_intr_get_cpu at /home/mmakaay1/.platformio/packages/framework-espidf/components/esp_system/intr_alloc.c line 719
0x400e2b09: gpio_isr_handler_add at /home/mmakaay1/.platformio/packages/framework-espidf/components/driver/gpio.c line 484
0x400d5c3b: esphome::esp32::IDFInternalGPIOPin::attach_interrupt(void (*)(void*), void*, esphome::gpio::InterruptType) const at src/esphome/components/esp32/gpio_idf.h line 83
0x400d77ad: esphome::rotary_encoder::RotaryEncoderSensor::setup() at src/esphome/core/gpio.h line 80
0x4014dded: esphome::Component::call_setup() at src/esphome/core/component.cpp line 67
0x4014de65: esphome::Component::call() at src/esphome/core/component.cpp line 78
0x400db955: esphome::Application::setup() at src/esphome/core/application.cpp line 38
0x400dcb01: setup() at src/main.cpp line 174
0x400d5b87: esphome::loop_task(void*) at src/esphome/components/esp32/core.cpp line 71

So the same symptom here.
I'll flag the PR as ready for review, since no further differentiation is required.

@mmakaay mmakaay marked this pull request as ready for review September 29, 2021 10:54
@OttoWinter
Copy link
Member

Is there a reason why this code is in the .h file or is it okay if I split declaration and implementation between respectively the .h and the .cpp file here?

no, only laziness 😝

Copy link
Member

@OttoWinter OttoWinter left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. let me know if you want to move it to cpp, otherwise we can also merge now

ESPHome Dev automation moved this from Needs Review to Reviewer Approved Sep 30, 2021
@mmakaay mmakaay marked this pull request as draft September 30, 2021 22:09
@mmakaay
Copy link
Member Author

mmakaay commented Sep 30, 2021

I will move it to the .cpp file. Moved the PR to draft for the time being.

@probot-esphome probot-esphome bot removed the small-pr label Oct 1, 2021
@mmakaay
Copy link
Member Author

mmakaay commented Oct 1, 2021

Alright, moved the code to the .cpp file, and added an error log now I had access to TAG.

@mmakaay mmakaay marked this pull request as ready for review October 1, 2021 00:48
@mmakaay
Copy link
Member Author

mmakaay commented Oct 1, 2021

Ow, checks failing, that's what you get for committing at 3am 😄 I'll fix the issues tomorrow, and give the code a spin on a few of my devices for testing.

@mmakaay
Copy link
Member Author

mmakaay commented Oct 1, 2021

Okay, the code should be ready now. The checks are running, fingers crossed 🤞

@OttoWinter OttoWinter merged commit 5a2984d into esphome:dev Oct 1, 2021
ESPHome Dev automation moved this from Reviewer Approved to Done Oct 1, 2021
@github-actions github-actions bot locked and limited conversation to collaborators Oct 3, 2021
ESPHome Dev automation moved this from Done to Reviewer Approved Sep 23, 2023
ESPHome Dev automation moved this from Reviewer Approved to Done Sep 23, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
Development

Successfully merging this pull request may close these issues.

Attach pin interrupt with framework esp-idf resulting in LoadProhibited panic
2 participants