Skip to content

cam_hal: PSRAM: forward-scan last dma node to find JPEG EOI reliably #785

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 Aug 4, 2025

PR requires the following PRs to be merged first, and thus contains their commits:

Issue

I've noticed that my previous implementation of the EOI search isn't
optimal, as it will not find the EOI, depending on where the image ends
in the last buffer due to padding.

Description

When psram_mode is active, cam_take() used to copy only 32 bytes
from PSRAM and scan backward for the JPEG EOI (FF D9). Because DMA
rounds the captured length up to a half-buffer, the true EOI often
falls earlier and was missed, leading to repeated "NO-EOI" resets.

This change:

  • Drops the 32-byte SRAM copy; scan directly in PSRAM after cache
    invalidation.
  • Searches the last DMA half-buffer plus 1 byte (marker_len-1),
    starting 1 byte before the final block to catch split EOIs.
  • Scans forward so we pick the earliest EOI in the current tail and
    avoid stale markers from a larger previous frame.
  • Uses a fast byte-hunt for 0xFF with a 2-byte verification for "FF D9".

SRAM mode keeps the simple backward check (we only accept EOI at offset
0 there), so no extra complexity or perf work is added.

Net effect: robust EOI detection without garbage tails and fewer
spurious "NO-EOI" resets in PSRAM captures.

Testing

Does fix some crashes, but not everything in psram_mode with JPEGs.

It will now reliably fetch one frame and then the frame capture stalls somewhere until the watchdog kills it (after 4 seconds) and the device reboots. I'm investigating.


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 RubenKelevra marked this pull request as draft August 4, 2025 01:02
@RubenKelevra RubenKelevra marked this pull request as ready for review August 5, 2025 04:10
@RubenKelevra RubenKelevra force-pushed the fix/EOI_expand_checks_search_view branch from 9e27574 to e9a5394 Compare August 5, 2025 04:15
@me-no-dev
Copy link
Member

@RubenKelevra please fix the conflicts and errors

@RubenKelevra RubenKelevra force-pushed the fix/EOI_expand_checks_search_view branch from e9a5394 to 38f8740 Compare August 5, 2025 12:24
@RubenKelevra RubenKelevra changed the title cam_hal: PSRAM: forward-scan last half-buffer to find JPEG EOI reliably cam_hal: PSRAM: forward-scan last dma node to find JPEG EOI reliably Aug 5, 2025
@RubenKelevra RubenKelevra marked this pull request as draft August 5, 2025 12:26
When `psram_mode` is active, `cam_take()` used to copy only 32 bytes
from PSRAM and scan backward for the JPEG EOI (FF D9). Because DMA
copies in dma_node_buffer_size length, the true EOI often
falls earlier and was missed, leading to repeated "NO-EOI" resets.

This change:
- Drops the 32-byte SRAM copy; scan directly in PSRAM after cache
  invalidation.
- Searches the last dma_node_buffer_size **plus 1 byte** (marker_len-1),
  starting 1 byte before the final block to catch split EOIs.
- Scans **forward** so we pick the earliest EOI in the current tail and
  avoid stale markers from a larger previous frame.
- Uses a fast byte-hunt for 0xFF with a 2-byte verification for "FF D9".

SRAM DMA mode keeps the simple backward check (we only accept EOI at
offset 0 there), so no extra complexity or perf work is added.

Net effect: robust EOI detection without garbage tails and fewer
spurious "NO-EOI" resets in PSRAM captures.
@RubenKelevra RubenKelevra force-pushed the fix/EOI_expand_checks_search_view branch from 38f8740 to 38bb948 Compare August 5, 2025 12:56
@RubenKelevra
Copy link
Contributor Author

Minor fix added, that dma_node_buffer_size is used instead of dma_half_buffer_size as search window, as the DMA operations are done with the former. No functional change in most situations, as both are usually the same for JPEG operations.

@RubenKelevra RubenKelevra marked this pull request as ready for review August 5, 2025 12:58
@me-no-dev me-no-dev merged commit 89e8d4d into espressif:master Aug 5, 2025
30 checks passed
@RubenKelevra RubenKelevra deleted the fix/EOI_expand_checks_search_view branch August 5, 2025 19:11
@RubenKelevra
Copy link
Contributor Author

Thanks @me-no-dev !

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.

2 participants