camera: support of the MEGA_CCM sensor (PY260, M5Stack Unit CamS3 5MP)

- the sensor is detected at the first init, one firmware for OV2640 and 5MP
- unsupported resolution switches to the factory resolution instead of a restart loop
release/1.1.3
Miroslav Pivovarsky 2026-10-28 14:07:05 +01:00
parent 6ff72f6005
commit 12a8209b1f
3 changed files with 118 additions and 12 deletions

View File

@ -13,6 +13,9 @@
*/
#include "camera.h"
/* SCCB_Write16() from the esp32-camera driver (private sccb.h). The driver of the MEGA_CCM sensor has no set_reg() */
extern "C" int SCCB_Write16(uint8_t slv_addr, uint16_t reg, uint8_t data);
Camera SystemCamera(&SystemConfig, &SystemLog, FLASH_GPIO_NUM);
/**
@ -29,6 +32,7 @@ Camera::Camera(Configuration* i_conf, Logs* i_log, int8_t i_FlashPin) {
CameraFlashPin = i_FlashPin;
StreamOnOff = false;
FrameBufferHeld = false;
MegaCcmSensor = false;
frameBufferSemaphore = xSemaphoreCreateMutex();
/* Allocate memory for the frame buffer */
@ -142,14 +146,78 @@ void Camera::InitCameraModule() {
log->AddEvent(LogLevel_Verbose, F("Camera frame buffer location: Unknown"));
}
/* MEGA_CCM sends bigger JPEG frames than the driver buffer for the frame size (width * height / 5) and the photo is lost (cam_hal: FB-OVF).
The driver is initialized with 5MP (1 MB buffer) and the resolution is set in the sensor. The M5Stack demo uses buffer 2592 * 1944 */
if (true == MegaCcmSensor) {
CameraConfig.frame_size = FRAMESIZE_5MP;
}
/* Camera init */
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));
FrameSize = FACTORY_CFG_FRAME_SIZE;
TFrameSize = TransformFrameSizeDataType(FrameSize);
config->SaveFrameSize(FrameSize);
CameraConfig.frame_size = TFrameSize;
err = esp_camera_init(&CameraConfig);
}
/* first init with the MEGA_CCM sensor: init again with the 5MP frame buffer */
if ((err == ESP_OK) && (false == MegaCcmSensor) && (MEGA_CCM_PID == esp_camera_sensor_get()->id.PID)) {
log->AddEvent(LogLevel_Info, F("Camera MEGA_CCM: init with 5MP frame buffer"));
MegaCcmSensor = true;
esp_camera_deinit();
CameraConfig.frame_size = FRAMESIZE_5MP;
err = esp_camera_init(&CameraConfig);
}
if (err != ESP_OK) {
log->AddEvent(LogLevel_Warning, F("Camera init failed. Error: "), String(err, HEX));
log->AddEvent(LogLevel_Warning, F("Reset ESP32-cam!"));
log->LoggerFlush(LOGS_RESTART_FLUSH_TIMEOUT);
ESP.restart();
}
if (true == MegaCcmSensor) {
MegaCcmSetup();
}
}
/**
@brief Setup of the MEGA_CCM sensor (PY260, M5Stack Unit CamS3 5MP) after the driver init, same as the M5Stack demo for the sensor.
Software reset with the delay, without the delay the old hardware version of the sensor does not send frames.
The resolution is set in the sensor, the driver is initialized with 5MP for the bigger frame buffer.
The sensor supports only 320x240, 640x480 and 1600x1200 from the web.
@param none
@return none
*/
void Camera::MegaCcmSetup() {
uint8_t slv_addr = esp_camera_sensor_get()->slv_addr;
uint8_t resolution_reg = 0x01; /* RESOLUTION_REG: 0x01 - 320x240, 0x02 - 640x480, 0x04 - 1600x1200 */
if (FRAMESIZE_VGA == TFrameSize) {
resolution_reg = 0x02;
} else if (FRAMESIZE_UXGA == TFrameSize) {
resolution_reg = 0x04;
} else if (FRAMESIZE_QVGA != TFrameSize) {
/* factory frame size is 320x240 */
log->AddEvent(LogLevel_Warning, F("Camera does not support frame size: "), String(FrameSize));
FrameSize = FACTORY_CFG_FRAME_SIZE;
TFrameSize = TransformFrameSizeDataType(FrameSize);
config->SaveFrameSize(FrameSize);
}
/* CAMERA_RST_REG 0x0102: 0, 100 ms, 1, 1 s for the sensor init. The reset sets the default values */
int ret = SCCB_Write16(slv_addr, 0x0102, 0x00);
delay(100);
ret += SCCB_Write16(slv_addr, 0x0102, 0x01);
delay(1000);
ret += SCCB_Write16(slv_addr, 0x0120, 0x01); /* PIXEL_FMT_REG: JPEG */
ret += SCCB_Write16(slv_addr, 0x0121, resolution_reg); /* RESOLUTION_REG */
log->AddEvent(LogLevel_Info, F("Camera MEGA_CCM setup. Ret: "), String(ret));
}
/**
@ -302,6 +370,21 @@ void Camera::ApplyCameraCfg() {
/* sensor configuration */
sensor = esp_camera_sensor_get();
/* MEGA_CCM: levels are 0-based with the neutral value in the middle (brightness 4, contrast 3, saturation 3), JPEG quality only 0 - high, 1 - default, 2 - low.
Exposure and gain are automatic in the sensor, the manual settings of the driver (aec_value, agc_gain) are not used. Other settings are not supported by the sensor */
if (true == MegaCcmSensor) {
sensor->set_brightness(sensor, brightness + 4);
sensor->set_contrast(sensor, contrast + 3);
sensor->set_saturation(sensor, saturation + 3);
sensor->set_special_effect(sensor, 0);
sensor->set_wb_mode(sensor, wb_mode);
sensor->set_hmirror(sensor, hmirror);
sensor->set_vflip(sensor, vflip);
sensor->set_quality(sensor, (PhotoQuality <= 20) ? 0 : ((PhotoQuality <= 40) ? 1 : 2));
return;
}
sensor->set_brightness(sensor, brightness); // -2 to 2
sensor->set_contrast(sensor, contrast); // -2 to 2
sensor->set_saturation(sensor, saturation); // -2 to 2
@ -434,6 +517,12 @@ void Camera::CapturePhoto() {
break;
}
/* MEGA_CCM: the driver is initialized with 5MP, the photo has the resolution set in the sensor */
if (true == MegaCcmSensor) {
FrameBuffer->width = resolution[TFrameSize].width;
FrameBuffer->height = resolution[TFrameSize].height;
}
char buf[150] = { '\0' };
uint8_t ControlFlag = (uint8_t)FrameBuffer->buf[15];
sprintf(buf, "The picture has been saved. Size: %d bytes, Photo resolution: %zu x %zu", FrameBuffer->len, FrameBuffer->width, FrameBuffer->height);

View File

@ -68,6 +68,7 @@ private:
camera_config_t CameraConfig; ///< camera configuration
camera_fb_t *FrameBuffer; ///< frame buffer
bool FrameBufferHeld; ///< FrameBuffer is taken from the camera driver (esp_camera_fb_get) and not returned yet
bool MegaCcmSensor; ///< sensor MEGA_CCM (PY260, M5Stack Unit CamS3 5MP), detected at the first init of the camera
camera_fb_t *FrameBufferDuplicate; ///< frame buffer duplicate
camera_fb_t *FrameBufferExif; ///< frame buffer with exif data
sensor_t* sensor; ///< sensor
@ -87,6 +88,7 @@ private:
void InitCameraModule();
void ReturnFrameBufferToDriver();
void MegaCcmSetup();
public:
Camera(Configuration*, Logs*, int8_t);

View File

@ -2,7 +2,7 @@
What we need for functionality
- M5Stack Unit CamS3 board with OV2640 camera module [here](#esp32)
- M5Stack Unit CamS3 board with OV2640 camera module, or Unit CamS3-5MP with PY260 camera module [here](#esp32)
- Supported camera modules [here](#cam_modules)
- How to upload firmware to the board [here](#flash_fw)
- How to compile software in the Arduino IDE [here](#arduino_cfg)
@ -20,13 +20,13 @@ Basic information:
- ESP32-S3-WROOM-1-N16R8 processor module
- 16MB FLASH and 8MB PSRAM
- OV2640 2MP camera
- OV2640 2MP camera (Unit CamS3) or PY260 5MP camera (Unit CamS3-5MP)
- Internal WiFi antenna
- Micro SD card slot and PDM microphone
- Programmable status LED on GPIO14
- USB programming through the Grove2USB-C adapter
This guide covers the **Unit CamS3 (U174)** with the OV2640 camera. Manufacturer documentation is available [here](https://docs.m5stack.com/en/unit/Unit_CamS3).
This guide covers the **Unit CamS3 (U174)** with the OV2640 camera and the **Unit CamS3-5MP** with the PY260 camera. Both use the same firmware, the camera is detected automatically. Manufacturer documentation is available [here](https://docs.m5stack.com/en/unit/Unit_CamS3) and [here](https://docs.m5stack.com/en/unit/Unit-CAMS3%205MP).
<img src="board.webp" alt="M5Stack Unit CamS3 board and hardware features" width=50%>
@ -37,9 +37,17 @@ The hardware images below are saved locally. Their original M5Stack URLs and ret
| Camera chip | FOV | Resolution | Tested | Works | Description |
|-------------|-----|------------|--------|-------|-------------|
| OV2640 | 66.5° | 2MP | Yes | Yes | Standard camera module |
| OV2640 | 66.5° | 2MP | Yes | Yes | Standard camera module, Unit CamS3 |
| PY260 (MEGA_CCM) | - | 5MP | Yes | Yes | Unit CamS3-5MP, see the limitations below |
The CamS3-5MP is a different camera variant and is not covered by this configuration.
Limitations of the Unit CamS3-5MP (PY260):
- Supported resolutions are **320x240**, **640x480** and **1600x1200**. Other resolutions are switched to 320x240.
- The camera has only 3 JPEG quality levels. Quality 10-20 is high, 21-40 is default, 41-63 is low.
- Brightness, contrast, saturation, white balance mode, horizontal mirror and vertical flip are supported. Other camera settings have no effect, exposure and gain are automatic.
- The photos are bigger than with the OV2640 (640x480 about 200 KB, 1600x1200 about 0.8-1 MB). The upload to Prusa Connect and the stream are slower: about 3.4 FPS at 640x480 and 0.8 FPS at 1600x1200.
- At 1600x1200 some photos are larger than the frame buffer (1 MB) and the camera drops them.
- Tested with the old hardware version of the Unit CamS3-5MP.
<a name="flash_fw"></a>
## How to upload firmware to the board
@ -58,7 +66,7 @@ Use the **Grove2USB-C adapter**, a Grove cable, a USB data cable and a jumper wi
For later updates, set **Erase All Flash Before Sketch Upload -> Disabled** to preserve the camera settings. Repeat the download-mode procedure if the uploader cannot connect. If uploading at 921600 fails, try 115200.
This board is not yet included in `fw_build.sh`, so a precompiled release archive is not available. Build and upload from source using the instructions below.
The precompiled firmware for this board is published with the release: `m5stack-unit-cams3.zip`, the file for the firmware update is `ESP32-M5-Unit-CamS3.bin`.
<a name="arduino_cfg"></a>
## How to compile software in the Arduino IDE
@ -81,7 +89,7 @@ In [mcu_cfg.h](../../ESP32_PrusaConnectCam/mcu_cfg.h), enable only the M5Stack b
Board configuration in the Arduino IDE, matching the [board configuration header](../../ESP32_PrusaConnectCam/module_ESP32-M5_Unit_CamS3.h):
- Tools -> Board -> ESP32 Arduino -> ESP32S3 Dev Module
- Tools -> USB CDC on BOOT -> Disabled
- Tools -> USB CDC on BOOT -> Enabled
- Tools -> CPU Frequency -> 240MHz (WiFi/BT)
- Tools -> Core debug level -> None
- Tools -> USB DFU on BOOT -> Disable
@ -101,6 +109,8 @@ Board configuration in the Arduino IDE, matching the [board configuration header
The physical board has **16MB flash**. The **8MB** setting above follows the current board configuration and does not use all of the physical flash.
The serial console is available on the USB of the Grove port (USB CDC on BOOT -> Enabled).
<a name="prusa_connect"></a>
## How to connect camera board to Prusa Connect
@ -114,9 +124,15 @@ Find the camera's IP address in the router's client list; it may appear as `esp3
<a name="factory_cfg"></a>
## How to reset configuration to factory settings
The current board configuration uses **GPIO0** for configuration reset. However, grounding this pin before powering on selects download mode, so the generic reset procedure does not clear the camera settings on this board.
To reset the settings to factory defaults, follow these instructions. **G0 (GPIO0)** is not used for the reset, because G0 connected to GND at power-on selects the download mode.
To reset the settings, repeat the [firmware upload procedure](#flash_fw) with **Erase All Flash Before Sketch Upload -> Enabled**. This removes saved WiFi details, the camera token and other settings. After uploading, disable flash erasing, remove the jumper, restart the camera and configure it again.
- Connect **G8 (GPIO8)** to **GND** on the exposed pin header.
- **Plug in** the power supply.
- Wait for **10 seconds**.
- After 10 seconds, the **STATUS LED will start flashing**.
- **Disconnect** **G8** from **GND** (but don't disconnect the power supply).
- The **STATUS LED** will **stop flashing**, and the MCU will **automatically reboot**.
- Now the MCU is in the factory settings.
<a name="status_led"></a>
## Status LED
@ -144,7 +160,7 @@ Hardware connections from the manufacturer documentation and schematic:
| Micro SD CS / MOSI / CLK / MISO | 9 / 38 / 39 / 40 |
| PDM microphone clock / data | 47 / 48 |
These are hardware connections; firmware feature support is described [below](#issue). The current camera configuration leaves `RESET_GPIO_NUM` set to `-1`.
These are hardware connections; firmware feature support is described [below](#issue). The configuration reset uses **GPIO8** on the pin header.
The dimensions drawing is saved [here](dimensions.jpg).
@ -158,7 +174,6 @@ Supply **5V USB power** through the Grove2USB-C adapter and Grove cable. Leave *
<a name="issue"></a>
## Potential issue with this board
- Micro SD card operation has not been verified on this board.
- Camera flash and DHT sensor support are disabled in the current configuration. The PDM microphone is not supported by this firmware.
- Cloud updates require a board-specific release binary, which is not yet produced by `fw_build.sh`. For manual OTA updates, upload the application binary from a matching CamS3 build through the web interface's **System** tab.
- Cloud updates use the release file `ESP32-M5-Unit-CamS3.bin`. For manual updates, upload the file through the web interface's **System** tab.
- The manufacturer's factory firmware has different WiFi defaults and features. Follow this project's setup instructions after uploading PrusaConnect firmware.