Skip to content

cam_hal: shrink ISR stack, silence spam, strip logs at low levels #765

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

Merged

Conversation

RubenKelevra
Copy link
Contributor

@RubenKelevra RubenKelevra commented Jul 7, 2025

Description

Requires #758, #759 and #760 to be merged first, thus containing their commits here.

  • Replace ESP_LOGx in ISRs/tight loops with CAM_WARN_THROTTLE(counter,msg)
    → uses ROM-resident ets_printf(); ~300 B less stack per hit.
  • At CONFIG_LOG_DEFAULT_LEVEL < 2 the macro compiles to a no-op,
    so all warning code is dropped from the binary.
  • First miss logs immediately, then every 100th; counter wraps at 10 000
    (reset to 1 to skip the “first miss” banner after wrap).
  • New static uint16_t counters per call-site keep totals without globals.
  • switch CAM_LOG_SPAM_EVERY_FRAME (=0) restores old per-frame debug.

No functional change to capture path—just less stack, fewer cycles,
smaller image, and a much quieter UART.

Related

Crashes reported by @turenkomv here due to excessive stack usage.

Checklist

Before submitting a Pull Request, please ensure the following:

  • 🚨 This PR does not introduce breaking changes.
  • All CI checks (GH Actions) pass.
  • Documentation is updated as needed.
  • Tests are updated or added as necessary.
  • Code is well-commented, especially in complex areas.
  • Git history is clean — commits are squashed to the minimum necessary.

@RubenKelevra
Copy link
Contributor Author

RubenKelevra commented Jul 7, 2025

@turenkomv feel free to test. The goal here is to be less crashy without increasing the standard stack size.

@turenkomv
Copy link

turenkomv commented Jul 7, 2025

Without increasing the stack size, I’m getting a crash like this:

[19:51:57]Guru Meditation Error: Core  1 panic'ed (Unhandled debug exception). 
[19:51:57]Debug exception reason: Stack canary watchpoint triggered (framebuffer_tas) 
[19:51:57]Core  1 register dump:
[19:51:57]PC      : 0x4037ef9e  PS      : 0x00060836  A0      : 0x820494aa  A1      : 0x3fcb4780
WARNING Decoded 0x4037ef9e: xPortSetInterruptMaskFromISR at C:\Users\turen\.platformio\packages\framework-espidf\components\freertos\FreeRTOS-Kernel\portable\xtensa\include\freertos/portmacro.h:552
 (inlined by) xPortEnterCriticalTimeout at C:\Users\turen\.platformio\packages\framework-espidf\components\freertos\FreeRTOS-Kernel\portable\xtensa/port.c:478
[19:51:57]A2      : 0x3fc9ae6c  A3      : 0xffffffff  A4      : 0x00000007  A5      : 0x3fcb47be  
[19:51:57]A6      : 0x00000066  A7      : 0x00000064  A8      : 0x00060820  A9      : 0x3fcb4780
[19:51:57]A10     : 0x00060823  A11     : 0x00000000  A12     : 0x00060820  A13     : 0x00060223
[19:51:57]A14     : 0x00000000  A15     : 0x0000abab  SAR     : 0x00000018  EXCCAUSE: 0x00000001
[19:51:57]EXCVADDR: 0x00000000  LBEG    : 0x40056fc5  LEND    : 0x40056fe7  LCOUNT  : 0xffffffff


[19:51:57]Backtrace: 0x4037ef9b:0x3fcb4780 0x420494a7:0x3fcb47b0 0x420495cd:0x3fcb47d0 0x4200f9ed:0x3fcb47f0 0x42016ada:0x3fcb48d0 0x420a5a61:0x3fcb4910 0x40384606:0x3fcb4940 0x4201a84a:0x3fcb4990 0x42019d6a:0x3fcb49d0 0x4200bc76:0x3fcb49f0
WARNING Found stack trace! Trying to decode it
WARNING Decoded 0x4037ef9b: xPortSetInterruptMaskFromISR at C:\Users\turen\.platformio\packages\framework-espidf\components\freertos\FreeRTOS-Kernel\portable\xtensa\include\freertos/portmacro.h:552
 (inlined by) xPortEnterCriticalTimeout at C:\Users\turen\.platformio\packages\framework-espidf\components\freertos\FreeRTOS-Kernel\portable\xtensa/port.c:478
WARNING Decoded 0x420494a7: vPortEnterCritical at C:\Users\turen\.platformio\packages\framework-espidf\components\freertos\FreeRTOS-Kernel\portable\xtensa\include\freertos/portmacro.h:567
 (inlined by) find_key at C:\Users\turen\.platformio\packages\framework-espidf\components\pthread/pthread_local_storage.c:77
WARNING Decoded 0x420495cd: pthread_setspecific at C:\Users\turen\.platformio\packages\framework-espidf\components\pthread/pthread_local_storage.c:199
WARNING Decoded 0x4200f9ed: esphome::logger::Logger::check_and_set_task_log_recursion_(bool) at C:\Projects\m5stack-camera-1\.esphome\build\m5stack-camera-1/src/esphome/components/logger/logger.h:298
 (inlined by) esphome::logger::Logger::check_and_set_task_log_recursion_(bool) at C:\Projects\m5stack-camera-1\.esphome\build\m5stack-camera-1/src/esphome/components/logger/logger.h:287    
 (inlined by) esphome::logger::Logger::log_vprintf_(unsigned char, char const*, int, char const*, __va_list_tag) at C:\Projects\m5stack-camera-1\.esphome\build\m5stack-camera-1/src/esphome/components/logger/logger.cpp:35
WARNING Decoded 0x42016ada: esphome::esp_idf_log_vprintf_(char const*, __va_list_tag) at C:\Projects\m5stack-camera-1\.esphome\build\m5stack-camera-1/src/esphome/core/log.cpp:56
WARNING Decoded 0x420a5a61: esp_log_writev at C:\Users\turen\.platformio\packages\framework-espidf\components\log/log.c:210
WARNING Decoded 0x40384606: esp_log_write at C:\Users\turen\.platformio\packages\framework-espidf\components\log/log.c:220
WARNING Decoded 0x4201a84a: cam_take at C:\Projects\m5stack-camera-1\.esphome\build\m5stack-camera-1/managed_components/espressif__esp32-camera/driver/cam_hal.c:541 (discriminator 1)
WARNING Decoded 0x42019d6a: esp_camera_fb_get at C:\Projects\m5stack-camera-1\.esphome\build\m5stack-camera-1/managed_components/espressif__esp32-camera/driver/esp_camera.c:384
WARNING Decoded 0x4200bc76: esphome::esp32_camera::ESP32Camera::framebuffer_task(void*) at C:\Projects\m5stack-camera-1\.esphome\build\m5stack-camera-1/src/esphome/components/esp32_camera/esp32_camera.cpp:412

Increasing the stack size for framebuffer_task as shown here helps avoid the crash:
https://github.com/esphome/esphome/blob/dev/esphome/components/esp32_camera/esp32_camera.cpp#L43

  xTaskCreatePinnedToCore(&ESP32Camera::framebuffer_task,
                          "framebuffer_task",  // name
                          8192,                // stack size
                          nullptr,             // task pv params
                          1,                   // priority
                          nullptr,             // handle
                          1                    // core
  );

After that, the device runs stably — no more crashes — but still doesn’t receive any frames.

@RubenKelevra
Copy link
Contributor Author

@turenkomv

thanks!

Sorry for the delayed response. I'll wait for further development that I got the hardware, which arrived in my country today. So given the usual delay it should be here on monday.

@me-no-dev
Copy link
Member

please resolve the conflicts, do not use sizeof for SOI and EOI (they are constants) and let's see how this PR will look after that

@RubenKelevra
Copy link
Contributor Author

Hey, the conflict is just due to me including the commits of the 3 other PRs here, that's why I started the description with:

Requires #758, #759 and #760 to be merged first, thus containing their commits here.

I'll rebase and do the requested change.

@RubenKelevra RubenKelevra force-pushed the throttle-cam-hal-isr-logging branch from 33e6180 to 3d093a9 Compare July 14, 2025 10:32
@RubenKelevra RubenKelevra marked this pull request as draft July 14, 2025 10:33
@me-no-dev
Copy link
Member

ok, let's get this one ready now :)

@RubenKelevra RubenKelevra force-pushed the throttle-cam-hal-isr-logging branch from 3d093a9 to df7dcec Compare July 14, 2025 12:08
@RubenKelevra RubenKelevra marked this pull request as ready for review July 14, 2025 12:08
* Replace ESP_LOGx in ISRs/tight loops with CAM_WARN_THROTTLE(counter,msg)
  → uses ROM-resident ets_printf(); ~300 B less stack per hit.

* At CONFIG_LOG_DEFAULT_LEVEL < 2 the macro compiles to a no-op,
  so *all* warning code is dropped from the binary.

* First miss logs immediately, then every 100th; counter wraps at 10 000
  (reset to 1 to skip the “first miss” banner after wrap).

* New static uint16_t counters per call-site keep totals without globals.

* Kconfig switch CAM_LOG_SPAM_EVERY_FRAME (=0) restores old per-frame debug.

No functional change to capture path—just less stack, fewer cycles,
smaller image, and a much quieter UART.
@RubenKelevra RubenKelevra force-pushed the throttle-cam-hal-isr-logging branch from df7dcec to f8fad50 Compare July 14, 2025 12:22
@RubenKelevra
Copy link
Contributor Author

Woops, thanks for catching that!

@me-no-dev me-no-dev merged commit 3e25105 into espressif:master Jul 14, 2025
30 checks passed
@me-no-dev
Copy link
Member

all done :)

@RubenKelevra RubenKelevra deleted the throttle-cam-hal-isr-logging branch July 14, 2025 12:28
@RubenKelevra
Copy link
Contributor Author

Thanks for the quick review process! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants