camera: reinit after the change of the resolution in the photo task, repeat the failed init

- the reinit from the web server released the frame buffer during the upload and the save to the SD card (crash)
- the init is repeated when the driver fails to set the frame size (OV2640 SCCB write error)
release/1.1.3
Miroslav Pivovarsky 2026-11-02 10:29:27 +01:00
parent 2f0f4edf3e
commit 28ce7cf67f
4 changed files with 35 additions and 4 deletions

View File

@ -54,6 +54,7 @@ Camera::Camera(Configuration* i_conf, Logs* i_log, int8_t i_FlashPin) {
PhotoExifData.len = 0;
PhotoExifData.offset = 0;
PhotoSending = false;
ReinitRequest = false;
CameraCaptureFailedCounter = 0;
}
@ -156,6 +157,14 @@ void Camera::InitCameraModule() {
/* Camera init */
err = esp_camera_init(&CameraConfig);
/* OV2640 sometimes does not acknowledge the register write after the register 0x39 in set_framesize() of the driver (SCCB_Write Failed, ret 259).
The next init with the same frame size usually works, esp_camera_init() calls esp_camera_deinit() after the error */
for (uint8_t i = 1; (err == ESP_ERR_CAMERA_FAILED_TO_SET_FRAME_SIZE) && (i < CAMERA_INIT_ATTEMPTS); i++) {
log->AddEvent(LogLevel_Warning, F("Camera failed to set frame size, init again. Attempt: "), String(i + 1));
delay(CAMERA_INIT_RETRY_DELAY);
err = esp_camera_init(&CameraConfig);
}
/* the sensor does not support the frame size from the configuration (MEGA_CCM supports only some of them). Use the factory frame size, otherwise the MCU restarts after every start */
if ((err == ESP_ERR_CAMERA_FAILED_TO_SET_FRAME_SIZE) && (FrameSize != FACTORY_CFG_FRAME_SIZE)) {
log->AddEvent(LogLevel_Warning, F("Camera does not support frame size: "), String(FrameSize));
@ -447,6 +456,21 @@ void Camera::ReinitCameraModule() {
xSemaphoreGive(frameBufferSemaphore);
}
/**
@brief Reinit the camera module after the change of the frame size or the photo quality (web, serial console). Called by the photo task
before sending the photo. The photo task sends the photo to Prusa Connect and saves it to the SD card from the frame buffer
of the driver without frameBufferSemaphore, the reinit from the web (async_tcp) released the frame buffer during it (crash in SavePhotoToSdCard()).
@param none
@return none
*/
void Camera::ProcessReinitRequest() {
if (true == ReinitRequest) {
ReinitRequest = false;
log->AddEvent(LogLevel_Info, F("Reinit camera module, new frame size or photo quality"));
ReinitCameraModule();
}
}
/**
@brief Function for get camera model and type
@param void
@ -1015,18 +1039,18 @@ int Camera::GetPhotoSize() {
}
/**
@brief Set Photo Quality
@brief Set Photo Quality. The camera module is reinitialized by the photo task (ProcessReinitRequest())
@param uint8_t - photo quality
@return none
*/
void Camera::SetPhotoQuality(uint8_t i_data) {
config->SavePhotoQuality(i_data);
PhotoQuality = i_data;
ReinitCameraModule();
ReinitRequest = true;
}
/**
@brief Set Frame Size
@brief Set Frame Size. The camera module is reinitialized by the photo task (ProcessReinitRequest())
@param uint8_t - frame size
@return none
*/
@ -1034,7 +1058,7 @@ void Camera::SetFrameSize(uint8_t i_data) {
config->SaveFrameSize(i_data);
FrameSize = i_data;
TFrameSize = TransformFrameSizeDataType(i_data);
ReinitCameraModule();
ReinitRequest = true;
}
/**

View File

@ -63,6 +63,7 @@ private:
bool CameraCaptureSuccess; ///< camera capture success
bool PhotoSending; ///< photo sending
bool ReinitRequest; ///< reinit of the camera module requested (frame size, photo quality), done by the photo task
/* OV2640 camera module pinout and cfg*/
camera_config_t CameraConfig; ///< camera configuration
@ -98,6 +99,7 @@ public:
void ApplyCameraCfg();
void LoadCameraCfgFromEeprom();
void ReinitCameraModule();
void ProcessReinitRequest();
void GetCameraModel();
void CapturePhoto();
bool CaptureStream(camera_fb_t *);

View File

@ -33,6 +33,8 @@ extern char SwBuild[SW_BUILD_MAX_LEN]; ///< build string, v
#define CERT_DIAGNOSTIC false ///< enable/disable one-time diagnostic of the TLS certificate chain
#define DEVICE_HOSTNAME "Prusa-ESP32cam" ///< device hostname
#define CAMERA_MAX_FAIL_CAPTURE 5 ///< maximum count for failed capture
#define CAMERA_INIT_ATTEMPTS 3 ///< maximum count of the camera init with the same frame size, when the driver fails to set the frame size
#define CAMERA_INIT_RETRY_DELAY 100 ///< delay before the next camera init with the same frame size [ms]
#define TLS_PSRAM_ALLOC_MIN_SIZE 1024 ///< mbedTLS allocations from this size (TLS buffers, certificates, handshake) are in PSRAM, smaller allocations in internal RAM [B]
/* ------------ PRUSA BACKEND CFG --------------*/

View File

@ -595,6 +595,9 @@ void System_TaskCaptureAndSendPhoto(void *pvParameters) {
TickType_t xLastWakeTime = xTaskGetTickCount();
while (1) {
/* reinit camera module after the change of the frame size or the photo quality. Here, not during sending and saving the photo */
SystemCamera.ProcessReinitRequest();
if (Connect.CheckSendingIntervalExpired()) {
Connect.SetSendingIntervalCounter(0);
/* send network information to backend */