logo elektroda
logo elektroda
X
logo elektroda

[Solved] [RTOS][esp-idf] ESP32 interrupts and shuffles some basic questions

NIXIE_123 6108 56
Best answers

How should I use ESP32 interrupts with FreeRTOS: should the ISR only wake a task, what is the lightest way to wake a task without passing data, and why do very short periodic timer callbacks seem limited to about 1 ms or clash with Wi‑Fi?

Use the ISR only for very short, time-critical work; if the work is not tiny, let the ISR just wake a task that does the rest [#19448979] The lightest wake-up method without passing data is a queue, while `vTaskSuspend()`/`xTaskResumeFromISR()` was not recommended here [#19448979] The 1 ms FreeRTOS tick only limits scheduler timeout resolution, not whether an interrupt can trigger work sooner, but a task you wake may still run only when the scheduler gives it CPU time [#19448979] For `esp_timer`, ESP-IDF normally dispatches callbacks through the high-priority `esp_timer` task rather than as a raw ISR, and periodic software timers below about 50 µs are considered impractical; for 35–111 µs work, a hardware timer or a peripheral like RMT is a better fit [#19448979][#19448941][#19571581] If you stay with a timer ISR, keep the handler and all accessed data in IRAM/internal RAM and prefer direct GPIO register writes (`W1TS`/`W1TC`) over slower helpers like `gpio_set_level()`, because flash access and Wi‑Fi can block or break non-IRAM handlers [#19545217][#19550193]
Generated by the language model.
ADVERTISEMENT
Treść została przetłumaczona polish » english Zobacz oryginalną wersję tematu
  • #31 19550902
    Anonymous
    Level 1  
  • ADVERTISEMENT
  • #32 19550949
    NIXIE_123
    Level 34  
    Posts: 2087
    Help: 288
    Rate: 606
    [RTOS][esp-idf] ESP32 interrupts and shuffles some basic questions .
    this is the library
    https://github.com/craftmetrics/esp32-button

    I have repeated the definitions
    #define BUTTON_DOWN (1)
    #define BUTTON_UP (2)
    #define BUTTON_HELD (3)


    in button.c and main.c
    eclipse did not throw a multiple definition error just the underscores disappeared.
    The program uploaded, it restarts again.
    I had a new eclipse uploaded when I upgraded, and it's the one with these actions on it. I think I will try it with what I had before.

    Project settings. There seems to be something missing here
    [RTOS][esp-idf] ESP32 interrupts and shuffles some basic questions .

    Added after 18 [minutes]:

    Everything installed from scratch including c:/esp-idf and still the same (on new eclipse)
  • ADVERTISEMENT
  • #33 19551324
    Anonymous
    Level 1  
  • ADVERTISEMENT
  • #34 19551683
    NIXIE_123
    Level 34  
    Posts: 2087
    Help: 288
    Rate: 606
    I went back to esp-idf v4.2
    Eclipse continued to underline but after uploading the program works as before.

    I went back to eclipse 2020-12
    It no longer underlines anything and everything works as it should.
  • #35 19551748
    Anonymous
    Level 1  
  • #36 19551762
    NIXIE_123
    Level 34  
    Posts: 2087
    Help: 288
    Rate: 606
    khoam wrote:
    IDE can affect the operation of the code in ESP
    .
    Well, not exactly.
    On the new eclipse it underlined but it depended on the idf version whether it worked or not.
    Because on the new eclipse but old idf it underlined but the programme worked.

    khoam wrote:
    But in which version of the callback function?
    .
    Everything works I mean I am back to square one. That is, a working clock without wifi and with interrupts the old way. I am now going to play around with converting just the callback.

    Added after 4 [hours] 38 [minutes]: .

    Just to be sure, I still tried on the old eclipse (2020-12) and idf v4.3
    It does not highlight errors the program loads and does not work. Something messed up in the new idf version
    [RTOS][esp-idf] ESP32 interrupts and shuffles some basic questions .
    [RTOS][esp-idf] ESP32 interrupts and shuffles some basic questions
  • #37 19552245
    Anonymous
    Level 1  
  • #38 19552251
    NIXIE_123
    Level 34  
    Posts: 2087
    Help: 288
    Rate: 606
    Code: C / C++
    Log in, to see the code
    .

    Compiles the same project all the time. 4.2 works 4.3 does not work

    EDIT:
    Problem solved. Need to add
    conf.clk_flags = 0;
    .

    Now I can get on with the actual problem i.e. interrupts. I will let you know when I have sculpted something.

    I am running on esp-idf v4.3 and eclipse 2020-12
  • #39 19552286
    Anonymous
    Level 1  
  • #40 19556736
    NIXIE_123
    Level 34  
    Posts: 2087
    Help: 288
    Rate: 606
    In order to assign a function handler to a core, it must be initialised in the shuffle assigned to that core.
    So I want to handle an interrupt on core 1.
    I create a task assigned to it and in it I initiate an interrupt from the timer
    The interrupt does not start, the rest is running
    It does not even start on core 0
    I moved the gpio initialisation to the task, no change

    Code: C / C++
    Log in, to see the code
    .
  • #41 19556976
    Anonymous
    Level 1  
  • #42 19558056
    NIXIE_123
    Level 34  
    Posts: 2087
    Help: 288
    Rate: 606
    It worked for me too, through these combinations I removed the IRAM_ATTR before the function name
    It didn't do anything so I tried it:
    khoam wrote:
    directly manipulate GPIO W1TS registers once GPIO W1TC8923e5bb6 .

    And it works! I have no panic. I've made myself 4 "map" arrays in RAM and in the interrupt just writes values from them to w1ts
    I removed the set_catode() function entirely

    It normally connects to the wifi even at 35µs. There is only one side effect
    Solution as above without wifi:
    -everything works I have no comments
    Solution as above with wifi:
    -no adc works when code from wifi starts. Clock works normally just doesn't dim (well, because there is no adc)

    So I paste in the code the diagnostics(last line):
    Code: C / C++
    Log in, to see the code
    .
    And the processor has a panic exactly when the last line is called. I remove it it works without a working adc
    [RTOS][esp-idf] ESP32 interrupts and shuffles some basic questions

    Initialisation of adc:
    Code: C / C++
    Log in, to see the code
    .

    https://www.esp32.com/viewtopic.php?t=9972

    Solution:
    you need to disable wifi by
    Code: C / C++
    Log in, to see the code
    .

    Only RMT(led_strip) does not work, not sure why
    Oh, it's working now. Apparently it was still trying to connect in the background and delayed calling esp_wifi_stop()
  • #43 19558136
    Anonymous
    Level 1  
  • #44 19558199
    NIXIE_123
    Level 34  
    Posts: 2087
    Help: 288
    Rate: 606
    I use one channel but it is connected to gpio14
    [RTOS][esp-idf] ESP32 interrupts and shuffles some basic questions
  • #45 19558242
    Anonymous
    Level 1  
  • ADVERTISEMENT
  • #46 19558263
    NIXIE_123
    Level 34  
    Posts: 2087
    Help: 288
    Rate: 606
    Of course, the problem here is that I can't change the pcb. Adc won't work for a few seconds every half hour. Don't bother
  • #47 19560875
    NIXIE_123
    Level 34  
    Posts: 2087
    Help: 288
    Rate: 606
    I download the time elegantly via sntp example.
    However, it only uses seconds timestamp and in ntp there are still microseconds.
    As a result, I have an accuracy of 1 second.
    I would also like to download microseconds, i.e. the whole timeval structure (seconds + microseconds)
  • Helpful post
    #48 19560898
    Anonymous
    Level 1  
  • #49 19561136
    NIXIE_123
    Level 34  
    Posts: 2087
    Help: 288
    Rate: 606
    However, the example uses microseconds and the internal time is accurate
    such a code:
    Code: C / C++
    Log in, to see the code
    and we have the exact time to the second in ds3231 : )
  • #50 19563512
    Anonymous
    Level 1  
  • #51 19566175
    NIXIE_123
    Level 34  
    Posts: 2087
    Help: 288
    Rate: 606
    There is one last thing left to do. Setting a few variables from the browser.
    What I want to achieve:
    I plug in the device without holding the button - in the old way, my code executes

    I power on the device while holding the button - my code does not execute.
    The Esp32 is in AP mode. I connect to its wifi by typing 192.168.4.1 into the browser. I see a simple page where I can enter ssid password and 2 variables. All this is written into non-volatile memory

    There is already a topic about this Link but those libraries are on arduino :| .
    There is also Link but you can only set what network it should connect to and it supervises that connection and I'm not interested in that. I just want a simple entry of 4 variables into memory and that's all

    How do I achieve this? There are some libraries or maybe do the AP mode server and nvs support myself just why break down an open door. Having learnt from experience that esp can be capricious and requires tricks I prefer something ready made rather than sitting on a simple thing for half a year

    Smart config is also out
  • #52 19566232
    Anonymous
    Level 1  
  • #53 19571581
    Nagus
    Level 27  
    Posts: 757
    Help: 99
    Rate: 97
    On the ESP32, the ADC2 is used by the WiFi module. So, you either use WiFi or you use ADC2. Never both at the same time. The documentation states. ADC1 is always available.
    The minimum HW timer interrupt is 50us, but this gyrates the CPU a lot. You could try using the rmt module i.e., in this case, let the 0 and 1 sequence go hard. This will definitely put less load on the CPU.
    Hardware timer interrupts should be in RAM. And it usually quickly turns out that some function called in an interrupt does not have a RAM version.
  • #54 19571752
    NIXIE_123
    Level 34  
    Posts: 2087
    Help: 288
    Rate: 606
    I already have everything running even on 35us.
    I am now doing AP + http server and html page with settings.
    I already have the page and by pressing send in the browser I can see the input data in the console.
    Now I just split them up save them to nvs and I have what I want.
  • #55 19571973
    Anonymous
    Level 1  
  • #56 19576769
    NIXIE_123
    Level 34  
    Posts: 2087
    Help: 288
    Rate: 606
    khoam wrote:
    And how long does it take to execute an ISR callback?
    .

    I repeated the test from post #9 the result is 4-5µs

    I did my own way from scratch and got what I wanted I am satisfied
    [RTOS][esp-idf] ESP32 interrupts and shuffles some basic questions
  • #57 19582456
    NIXIE_123
    Level 34  
    Posts: 2087
    Help: 288
    Rate: 606
    Happy end
    [RTOS][esp-idf] ESP32 interrupts and shuffles some basic questions [RTOS][esp-idf] ESP32 interrupts and shuffles some basic questions [RTOS][esp-idf] ESP32 interrupts and shuffles some basic questions .

Topic summary

✨ The discussion revolves around the use of interrupts in the ESP32 with the ESP-IDF RTOS. Key points include best practices for handling interrupts, such as using them to wake tasks rather than performing extensive operations within ISRs. The participants explore methods for waking tasks from interrupts, with queues being a recommended approach. Concerns about the system tick rate limiting task execution frequency to 1ms are addressed, along with issues related to WiFi interference when using timer interrupts. The conversation also touches on the importance of managing execution time within ISRs to avoid conflicts with other processes, particularly WiFi operations. Solutions include using direct GPIO manipulation for efficiency and the potential of using the RMT module to reduce CPU load. The discussion concludes with successful implementations of timer interrupts and HTTP server setups for configuration via a web interface.
Generated by the language model.

FAQ

TL;DR: 35 μs timer ISRs run ~5 µs each (“fast enough if kept in IRAM” [Elektroda, khoam, post #19547733]) and an ADC2-Wi-Fi clash causes 100 % failure when both are active [Espressif Docs]. "Keep ISRs tiny" [Elektroda, khoam, post #19448979]

Why it matters: These micro-timing rules decide whether your ESP32 reboots or streams data reliably.

Quick Facts

• Minimum reliable esp_timer period: 50 µs (Espressif v4.2 docs) • esp_timer task priority: 22 (ESP_TASK_PRIO_MAX-3) [Elektroda, khoam, post #19448979] • Default ISR stack: 1 536 bytes (CONFIG_FREERTOS_ISR_STACKSIZE) [Elektroda, khoam, post #19547733] • ADC2 is unavailable while Wi-Fi is on; ADC1 always works (Technical Reference v4.3) • System tick default: 1 ms; can be raised to 1 kHz by setting CONFIG_FREERTOS_HZ=1000 [Elektroda, khoam, post #19546466]

Should I put all my work inside an ESP32 interrupt or wake a task?

Keep the ISR minimal and defer heavy work to a task unless the work must finish inside a few microseconds. "If very short and critical, stay in the interrupt." [Elektroda, khoam, post #19448979]

What is the lightest way to wake a FreeRTOS task from an ISR when no data is sent?

Use xQueueSendFromISR to a zero-length queue or a binary semaphore. Espressif confirms queues are tested and watchdog-safe, whereas xTaskResumeFromISR can trigger panics [Elektroda, NIXIE_123, post #19448833]

Does the 1 ms system tick cap task frequency?

The scheduler can’t unblock a delay shorter than one tick automatically, but an ISR can force a ready state at any time, so tasks may still run faster than 1 kHz when woken by interrupts [Elektroda, khoam, post #19448979]

Why did my 35 µs esp_timer callback slow to 1 ms after I moved code into a task?

Because esp_timer’s default dispatcher runs in a high-priority task. When you defer work, the task waits for the next tick. Using CONFIG_ESP_TIMER_SUPPORTS_ISR_DISPATCH_METHOD or direct hardware timers avoids the 1 ms bottleneck [Elektroda, khoam, post #19448979]

What’s the minimum safe period for esp_timer?

Espressif warns that periods below 50 µs consume “most of the CPU time”; use hardware peripherals or DMA for faster needs [Elektroda, khoam, post #19449941]

Why does Wi-Fi crash when my timer ISR runs?

esp_timer callbacks run at priority 22 on core 0, pre-empting Wi-Fi tasks on the same core. Long ISRs starve the Wi-Fi stack and trigger panics [Elektroda, khoam, post #19545217]

Can I fix the Wi-Fi clash by moving the timer to core 1?

Yes. Create the timer inside a task pinned to core 1 with xTaskCreatePinnedToCore, or call esp_ipc_call to execute setup on core 1 [Elektroda, khoam, post #19563512]

Why does ADC2 stop working when Wi-Fi is enabled?

ADC2 hardware is shared with the Wi-Fi radio. Reading ADC2 while Wi-Fi runs returns ESP_ERR_TIMEOUT or causes panics. Use ADC1 channels (GPIO 34-39) or stop Wi-Fi before sampling [Elektroda, NIXIE_123, post #19558056]

How do I guarantee my ISR code stays in RAM?

Add IRAM_ATTR to the callback and any helper it calls, ensure variables it touches are in DRAM, and register the interrupt with ESP_INTR_FLAG_IRAM; otherwise flash writes will disable the handler [Elektroda, ESP_Sprite, post #19550055]

Edge case: why does timer_spinlock_take disappear after upgrading IDF?

The call is deprecated beyond v4.3; switch to timer_isr_callback_add, which wraps the critical section for you [Elektroda, khoam, post #19550573]

How can I update Wi-Fi credentials via a browser without Arduino libraries?

Run the ESP32 in AP mode, host an HTTP server, parse the POST data, and store SSID, password, and variables to NVS. Only 4 kB flash and 20 kB RAM are needed (typical) [Espressif HTTP Server Guide].

Quick How-To: create a 111 µs hardware timer ISR

  1. Configure divider = 80 and alarm_value = 111.
  2. Register ISR with timer_isr_callback_add(..., ESP_INTR_FLAG_IRAM).
  3. In ISR, clear the interrupt: write 1 to TIMERG0.int_clr_timers.t0. [Elektroda, 19546162]
Generated by the language model.
ADVERTISEMENT