Skip to content

Commit 33e6180

Browse files
committed
cam_hal: shrink ISR stack, silence spam, strip logs at low levels
* 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.
1 parent 6c4361e commit 33e6180

File tree

1 file changed

+48
-6
lines changed

1 file changed

+48
-6
lines changed

driver/cam_hal.c

Lines changed: 48 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,42 @@
4242
static const char *TAG = "cam_hal";
4343
static cam_obj_t *cam_obj = NULL;
4444

45+
/* At top of cam_hal.c – one switch for noisy ISR prints */
46+
#ifndef CAM_LOG_SPAM_EVERY_FRAME
47+
#define CAM_LOG_SPAM_EVERY_FRAME 0 /* set to 1 to restore old behaviour */
48+
#endif
49+
50+
/* Throttle repeated warnings printed from tight loops / ISRs.
51+
*
52+
* counter – static DRAM/IRAM uint16_t you pass in
53+
* first – literal C string shown on first hit and as prefix of summaries
54+
*/
55+
#if CONFIG_LOG_DEFAULT_LEVEL >= 2
56+
#define CAM_WARN_THROTTLE(counter, first) \
57+
do { \
58+
if (++(counter) == 1) { \
59+
ESP_CAMERA_ETS_PRINTF(DRAM_STR("cam_hal: %s\r\n"), first); \
60+
} else if ((counter) % 100 == 0) { \
61+
ESP_CAMERA_ETS_PRINTF(DRAM_STR("cam_hal: %s - 100 additional misses\r\n"), first); \
62+
} \
63+
if ((counter) == 10000) (counter) = 1; \
64+
} while (0)
65+
#else
66+
#define CAM_WARN_THROTTLE(counter, first) do { (void)(counter); } while (0)
67+
#endif
68+
4569
/* JPEG markers in little-endian order (ESP32). */
4670
static const uint8_t JPEG_SOI_MARKER[] = {0xFF, 0xD8, 0xFF}; /* SOI = FF D8 FF */
4771
static const uint16_t JPEG_EOI_MARKER = 0xD9FF; /* EOI = FF D9 */
4872

4973
static int cam_verify_jpeg_soi(const uint8_t *inbuf, uint32_t length)
5074
{
5175
const size_t soi_len = sizeof(JPEG_SOI_MARKER);
76+
static uint16_t warn_soi_miss_cnt;
5277

5378
if (length < soi_len) {
54-
ESP_LOGW(TAG, "NO-SOI");
79+
CAM_WARN_THROTTLE(warn_soi_miss_cnt,
80+
"NO-SOI - JPEG start marker missing (len < 3b)");
5581
return -1;
5682
}
5783

@@ -61,7 +87,9 @@ static int cam_verify_jpeg_soi(const uint8_t *inbuf, uint32_t length)
6187
return i;
6288
}
6389
}
64-
ESP_LOGW(TAG, "NO-SOI");
90+
91+
CAM_WARN_THROTTLE(warn_soi_miss_cnt,
92+
"NO-SOI - JPEG start marker missing");
6593
return -1;
6694
}
6795

@@ -119,7 +147,13 @@ void IRAM_ATTR ll_cam_send_event(cam_obj_t *cam, cam_event_t cam_event, BaseType
119147
if (xQueueSendFromISR(cam->event_queue, (void *)&cam_event, HPTaskAwoken) != pdTRUE) {
120148
ll_cam_stop(cam);
121149
cam->state = CAM_STATE_IDLE;
122-
ESP_CAMERA_ETS_PRINTF(DRAM_STR("cam_hal: EV-%s-OVF\r\n"), cam_event==CAM_IN_SUC_EOF_EVENT ? DRAM_STR("EOF") : DRAM_STR("VSYNC"));
150+
#if CAM_LOG_SPAM_EVERY_FRAME
151+
ESP_DRAM_LOGD(TAG, "EV-%s-OVF", cam_event==CAM_IN_SUC_EOF_EVENT ? "EOF" : "VSYNC");
152+
#else
153+
static uint16_t ovf_cnt;
154+
CAM_WARN_THROTTLE(ovf_cnt,
155+
cam_event==CAM_IN_SUC_EOF_EVENT ? "EV-EOF-OVF" : "EV-VSYNC-OVF");
156+
#endif
123157
}
124158
}
125159

@@ -493,7 +527,12 @@ camera_fb_t *cam_take(TickType_t timeout)
493527
#if CONFIG_IDF_TARGET_ESP32S3
494528
uint16_t dma_reset_counter = 0;
495529
static const uint8_t MAX_GDMA_RESETS = 3;
530+
#else
531+
/* throttle repeated NULL frame warnings */
532+
static uint16_t warn_null_cnt;
496533
#endif
534+
/* throttle repeated NO-EOI warnings */
535+
static uint16_t warn_eoi_miss_cnt;
497536

498537
for (;;)
499538
{
@@ -518,12 +557,14 @@ camera_fb_t *cam_take(TickType_t timeout)
518557
continue; /* retry with queue timeout */
519558
}
520559
if (dma_reset_counter == MAX_GDMA_RESETS) {
521-
ESP_LOGW(TAG, "Giving up GDMA reset after %u tries", dma_reset_counter);
560+
ESP_CAMERA_ETS_PRINTF(DRAM_STR("cam_hal: Giving up GDMA reset after %u tries\r\n"),
561+
(unsigned) dma_reset_counter);
522562
dma_reset_counter++; /* suppress further logs */
523563
}
524564
#else
525565
/* Early warning for misbehaving sensors on other chips */
526-
ESP_LOGW(TAG, "Unexpected NULL frame on %s", CONFIG_IDF_TARGET);
566+
CAM_WARN_THROTTLE(warn_null_cnt,
567+
"Unexpected NULL frame on " CONFIG_IDF_TARGET);
527568
#endif
528569
vTaskDelay(1); /* immediate yield once resets are done */
529570
continue; /* go to top of loop */
@@ -537,7 +578,8 @@ camera_fb_t *cam_take(TickType_t timeout)
537578
return dma_buffer;
538579
}
539580

540-
ESP_LOGW(TAG, "NO-EOI");
581+
CAM_WARN_THROTTLE(warn_eoi_miss_cnt,
582+
"NO-EOI - JPEG end marker missing");
541583
cam_give(dma_buffer);
542584
continue; /* wait for another frame */
543585
} else if (cam_obj->psram_mode &&

0 commit comments

Comments
 (0)