|
| 1 | +/* |
| 2 | + * SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | +#include <stdio.h> |
| 7 | +#include "sdkconfig.h" |
| 8 | +#include "freertos/FreeRTOS.h" |
| 9 | +#include "unity.h" |
| 10 | +#include "esp_cam_ctlr_csi.h" |
| 11 | +#include "esp_cam_ctlr.h" |
| 12 | +#include "esp_ldo_regulator.h" |
| 13 | +#include "sdkconfig.h" |
| 14 | +#include "driver/isp.h" |
| 15 | +#include "example_sensor_init.h" |
| 16 | +#include "esp_private/esp_cache_private.h" |
| 17 | + |
| 18 | +#define TEST_USED_LDO_CHAN_ID 3 |
| 19 | +#define TEST_USED_LDO_VOLTAGE_MV 2500 |
| 20 | + |
| 21 | +#define TEST_RGB565_BITS_PER_PIXEL 16 |
| 22 | +#define TEST_MIPI_CSI_LANE_BITRATE_MBPS 200 |
| 23 | + |
| 24 | +#define TEST_MIPI_CSI_CAM_SCCB_SCL_IO 8 |
| 25 | +#define TEST_MIPI_CSI_CAM_SCCB_SDA_IO 7 |
| 26 | + |
| 27 | +#define TEST_MIPI_DSI_DISP_HRES 800 |
| 28 | +#define TEST_MIPI_DSI_DISP_VRES 1280 |
| 29 | +#define TEST_MIPI_CSI_DISP_HRES 800 |
| 30 | +#define TEST_MIPI_CSI_DISP_VRES 640 |
| 31 | + |
| 32 | +#define TEST_CAM_FORMAT "MIPI_2lane_24Minput_RAW8_800x640_50fps" |
| 33 | + |
| 34 | +static int new_buffer_count = 0; |
| 35 | +static int trans_finished_count = 0; |
| 36 | + |
| 37 | +static bool IRAM_ATTR camera_get_new_buffer(esp_cam_ctlr_handle_t handle, esp_cam_ctlr_trans_t *trans, void *user_data) |
| 38 | +{ |
| 39 | + esp_cam_ctlr_trans_t *new_trans = (esp_cam_ctlr_trans_t *)user_data; |
| 40 | + trans->buffer = new_trans->buffer; |
| 41 | + trans->buflen = new_trans->buflen; |
| 42 | + new_buffer_count++; |
| 43 | + return false; |
| 44 | +} |
| 45 | + |
| 46 | +static bool IRAM_ATTR camera_trans_finished(esp_cam_ctlr_handle_t handle, esp_cam_ctlr_trans_t *trans, void *user_data) |
| 47 | +{ |
| 48 | + trans_finished_count++; |
| 49 | + return false; |
| 50 | +} |
| 51 | + |
| 52 | +TEST_CASE("TEST esp_cam on ov5647", "[csi][camera][ov5647]") |
| 53 | +{ |
| 54 | + size_t frame_buffer_size = TEST_MIPI_CSI_DISP_HRES * |
| 55 | + TEST_MIPI_DSI_DISP_VRES * |
| 56 | + TEST_RGB565_BITS_PER_PIXEL / 8; |
| 57 | + |
| 58 | + //mipi ldo |
| 59 | + esp_ldo_channel_handle_t ldo_mipi_phy = NULL; |
| 60 | + esp_ldo_channel_config_t ldo_config = { |
| 61 | + .chan_id = TEST_USED_LDO_CHAN_ID, |
| 62 | + .voltage_mv = TEST_USED_LDO_VOLTAGE_MV, |
| 63 | + }; |
| 64 | + TEST_ESP_OK(esp_ldo_acquire_channel(&ldo_config, &ldo_mipi_phy)); |
| 65 | + |
| 66 | + size_t frame_buffer_alignment = 0; |
| 67 | + TEST_ESP_OK(esp_cache_get_alignment(0, &frame_buffer_alignment)); |
| 68 | + void *frame_buffer = heap_caps_aligned_calloc(frame_buffer_alignment, 1, frame_buffer_size, |
| 69 | + MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT); |
| 70 | + |
| 71 | + esp_cam_ctlr_trans_t trans_data = { |
| 72 | + .buffer = frame_buffer, |
| 73 | + .buflen = frame_buffer_size, |
| 74 | + }; |
| 75 | + |
| 76 | + //--------Camera Sensor and SCCB Init-----------// |
| 77 | + example_sensor_handle_t sensor_handle = { |
| 78 | + .sccb_handle = NULL, |
| 79 | + .i2c_bus_handle = NULL, |
| 80 | + }; |
| 81 | + example_sensor_config_t sensor_config = { |
| 82 | + .i2c_port_num = I2C_NUM_0, |
| 83 | + .i2c_sda_io_num = TEST_MIPI_CSI_CAM_SCCB_SDA_IO, |
| 84 | + .i2c_scl_io_num = TEST_MIPI_CSI_CAM_SCCB_SCL_IO, |
| 85 | + .port = ESP_CAM_SENSOR_MIPI_CSI, |
| 86 | + .format_name = TEST_CAM_FORMAT, |
| 87 | + }; |
| 88 | + example_sensor_init(&sensor_config, &sensor_handle); |
| 89 | + |
| 90 | + //---------------CSI Init------------------// |
| 91 | + esp_cam_ctlr_csi_config_t csi_config = { |
| 92 | + .ctlr_id = 0, |
| 93 | + .h_res = TEST_MIPI_CSI_DISP_HRES, |
| 94 | + .v_res = TEST_MIPI_CSI_DISP_VRES, |
| 95 | + .lane_bit_rate_mbps = TEST_MIPI_CSI_LANE_BITRATE_MBPS, |
| 96 | + .input_data_color_type = CAM_CTLR_COLOR_RAW8, |
| 97 | + .output_data_color_type = CAM_CTLR_COLOR_RGB565, |
| 98 | + .data_lane_num = 2, |
| 99 | + .byte_swap_en = false, |
| 100 | + .queue_items = 1, |
| 101 | + }; |
| 102 | + esp_cam_ctlr_handle_t cam_handle = NULL; |
| 103 | + TEST_ESP_OK(esp_cam_new_csi_ctlr(&csi_config, &cam_handle)); |
| 104 | + |
| 105 | + esp_cam_ctlr_evt_cbs_t cbs = { |
| 106 | + .on_get_new_trans = camera_get_new_buffer, |
| 107 | + .on_trans_finished = camera_trans_finished, |
| 108 | + }; |
| 109 | + TEST_ESP_OK(esp_cam_ctlr_register_event_callbacks(cam_handle, &cbs, &trans_data)); |
| 110 | + |
| 111 | + TEST_ESP_OK(esp_cam_ctlr_enable(cam_handle)); |
| 112 | + |
| 113 | + //---------------ISP Init------------------// |
| 114 | + isp_proc_handle_t isp_proc = NULL; |
| 115 | + esp_isp_processor_cfg_t isp_config = { |
| 116 | + .clk_hz = 80 * 1000 * 1000, |
| 117 | + .input_data_source = ISP_INPUT_DATA_SOURCE_CSI, |
| 118 | + .input_data_color_type = ISP_COLOR_RAW8, |
| 119 | + .output_data_color_type = ISP_COLOR_RGB565, |
| 120 | + .has_line_start_packet = false, |
| 121 | + .has_line_end_packet = false, |
| 122 | + .h_res = TEST_MIPI_CSI_DISP_HRES, |
| 123 | + .v_res = TEST_MIPI_CSI_DISP_VRES, |
| 124 | + }; |
| 125 | + TEST_ESP_OK(esp_isp_new_processor(&isp_config, &isp_proc)); |
| 126 | + |
| 127 | + TEST_ESP_OK(esp_isp_enable(isp_proc)); |
| 128 | + TEST_ESP_OK(esp_cam_ctlr_start(cam_handle)); |
| 129 | + TEST_ESP_OK(esp_cam_ctlr_receive(cam_handle, &trans_data, ESP_CAM_CTLR_MAX_DELAY)); |
| 130 | + vTaskDelay(100 / portTICK_PERIOD_MS); |
| 131 | + TEST_ESP_OK(esp_cam_ctlr_stop(cam_handle)); |
| 132 | + |
| 133 | + TEST_ASSERT_GREATER_THAN(2, new_buffer_count); |
| 134 | + TEST_ASSERT_GREATER_THAN(2, trans_finished_count); |
| 135 | + |
| 136 | + TEST_ESP_OK(esp_cam_ctlr_disable(cam_handle)); |
| 137 | + TEST_ESP_OK(esp_cam_ctlr_del(cam_handle)); |
| 138 | + |
| 139 | + TEST_ESP_OK(esp_isp_disable(isp_proc)); |
| 140 | + TEST_ESP_OK(esp_isp_del_processor(isp_proc)); |
| 141 | + |
| 142 | + TEST_ESP_OK(esp_ldo_release_channel(ldo_mipi_phy)); |
| 143 | + |
| 144 | + example_sensor_deinit(sensor_handle); |
| 145 | + |
| 146 | + heap_caps_free(frame_buffer); |
| 147 | +} |
0 commit comments