ESP32P4该怎么使用CSI?

cq65617875
Posts: 2
Joined: Tue Sep 03, 2024 7:22 am

ESP32P4该怎么使用CSI?

Postby cq65617875 » Tue Sep 03, 2024 7:30 am

设备是 esp32-p4-function-ev-board
IDF版本是5.0
已经sc2336: Stream=1
然后初始化csi_ctrl之后
怎么获取到buffer呢
  1. #include <esp_cache.h>
  2. #include "esp_heap_caps.h"
  3. #include "string.h"
  4.  
  5. #include "esp_ldo_regulator.h"
  6. #include "esp_cam_ctlr.h"
  7. #include "esp_cam_ctlr_csi.h"
  8.  
  9. #include "esp_sccb_intf.h"
  10. #include "esp_sccb_i2c.h"
  11. #include "esp_cam_sensor.h"
  12. #include "sc2336.h"
  13.  
  14. #define MIPI_SCCB_SDA_IO 7
  15. #define MIPI_SCCB_SCL_IO 8
  16. #define CSI_LDO_UNIT_ID 3
  17. #define CSI_LDO_CFG_VOL_MV 2500
  18.  
  19.  
  20. static bool csi_video_on_get_new_trans(esp_cam_ctlr_handle_t handle, esp_cam_ctlr_trans_t *trans, void *user_data) {
  21.     printf("csi_on_get_new_trans \n");
  22.     esp_cam_ctlr_trans_t csi_trans = *(esp_cam_ctlr_trans_t *)user_data;
  23.     trans->buffer = csi_trans.buffer;
  24.     trans->buflen = csi_trans.buflen;
  25.     return false;
  26. }
  27.  
  28. static bool csi_video_on_trans_finished(esp_cam_ctlr_handle_t handle, esp_cam_ctlr_trans_t *trans, void *user_data) {
  29.     printf("csi_video_on_trans_finished \n");
  30.     return false;
  31. }
  32.  
  33. void app_main(void) {
  34.     //---------------BUFFER Init------------------//
  35.     size_t frame_buffer_size = 1920 * 1080 * 10 / 8;
  36.     void *frame_buffer = heap_caps_malloc(frame_buffer_size, MALLOC_CAP_8BIT | MALLOC_CAP_SPIRAM | MALLOC_CAP_CACHE_ALIGNED);
  37.     esp_cam_ctlr_trans_t csi_trans = {};
  38.     csi_trans.buffer = frame_buffer;
  39.     csi_trans.buflen = frame_buffer_size;
  40.     //---------------LDO Init------------------//
  41.     esp_ldo_channel_handle_t ldo_mipi_phy = NULL;
  42.     esp_ldo_channel_config_t ldo_mipi_phy_config = {
  43.             .chan_id = CSI_LDO_UNIT_ID,
  44.             .voltage_mv = CSI_LDO_CFG_VOL_MV,
  45.     };
  46.     ESP_ERROR_CHECK(esp_ldo_acquire_channel(&ldo_mipi_phy_config, &ldo_mipi_phy));
  47.     //---------------I2C Init------------------//
  48.     i2c_master_bus_config_t i2c_bus_conf = {
  49.             .clk_source = I2C_CLK_SRC_DEFAULT,
  50.             .sda_io_num = MIPI_SCCB_SDA_IO,
  51.             .scl_io_num = MIPI_SCCB_SCL_IO,
  52.             .i2c_port = I2C_NUM_0,
  53.             .flags.enable_internal_pullup = true,
  54.     };
  55.     i2c_master_bus_handle_t bus_handle = NULL;
  56.     ESP_ERROR_CHECK(i2c_new_master_bus(&i2c_bus_conf, &bus_handle));
  57.     //---------------SCCB Init------------------//
  58.     esp_sccb_io_handle_t sc2336_io_handle = NULL;
  59.     sccb_i2c_config_t i2c_config = {
  60.             .scl_speed_hz = 400 * 1000,
  61.             .device_address = SC2336_SCCB_ADDR,
  62.             .dev_addr_length = I2C_ADDR_BIT_LEN_7,
  63.     };
  64.     ESP_ERROR_CHECK(sccb_new_i2c_io(bus_handle, &i2c_config, &sc2336_io_handle));
  65.     //---------------SENSOR Init------------------//
  66.     esp_cam_sensor_config_t cam_config = {
  67.             .sccb_handle = sc2336_io_handle,
  68.             .reset_pin = -1,
  69.             .pwdn_pin = -1,
  70.             .xclk_pin = -1,
  71.             .sensor_port = ESP_CAM_SENSOR_MIPI_CSI,
  72.     };
  73.     esp_cam_sensor_device_t *cam = sc2336_detect(&cam_config);
  74.     //---------------FORMAT Init------------------//
  75.     esp_cam_sensor_format_array_t cam_fmt_array = {0};
  76.     esp_cam_sensor_query_format(cam, &cam_fmt_array);
  77.     const esp_cam_sensor_format_t *parray = cam_fmt_array.format_array;
  78.     for (int i = 0; i < cam_fmt_array.count; i++) {
  79.         printf("fmt[%d].name:%s\n", i, parray[i].name);
  80.     }
  81.     esp_cam_sensor_format_t *cam_cur_fmt = NULL;
  82.     for (int i = 0; i < cam_fmt_array.count; i++) {
  83.         if (!strcmp(parray[i].name, "MIPI_2lane_24Minput_RAW10_1920x1080_30fps")) {
  84.             cam_cur_fmt = (esp_cam_sensor_format_t *) & (parray[i].name);
  85.         }
  86.     }
  87.     ESP_ERROR_CHECK(esp_cam_sensor_set_format(cam, (const esp_cam_sensor_format_t *) cam_cur_fmt));
  88.     //---------------START Init------------------//
  89.     int enable_flag = 1;
  90.     ESP_ERROR_CHECK(esp_cam_sensor_ioctl(cam, ESP_CAM_SENSOR_IOC_S_STREAM, &enable_flag));
  91.     //---------------CSI Init------------------//
  92.     esp_cam_ctlr_csi_config_t csi_config = {
  93.             .ctlr_id = 0,
  94.             .clk_src = MIPI_CSI_PHY_CLK_SRC_DEFAULT,
  95.             .byte_swap_en = false,
  96.             .queue_items = 1,
  97.             .h_res = 1920,
  98.             .v_res = 1080,
  99.             .data_lane_num = 2,
  100.             .input_data_color_type = CAM_CTLR_COLOR_RAW10,
  101.             .output_data_color_type = CAM_CTLR_COLOR_RAW10,
  102.             .lane_bit_rate_mbps = 594,
  103.             .bk_buffer_dis = false,
  104.     };
  105.     esp_cam_ctlr_handle_t cam_handle = NULL;
  106.     ESP_ERROR_CHECK(esp_cam_new_csi_ctlr(&csi_config, &cam_handle));
  107.     esp_cam_ctlr_evt_cbs_t cam_ctrl_cbs = {
  108.             .on_get_new_trans = csi_video_on_get_new_trans,
  109.             .on_trans_finished = csi_video_on_trans_finished
  110.     };
  111.     ESP_ERROR_CHECK(esp_cam_ctlr_register_event_callbacks(cam_handle, &cam_ctrl_cbs, &csi_trans));
  112.     ESP_ERROR_CHECK(esp_cam_ctlr_enable(cam_handle));
  113.  
  114.     memset(frame_buffer, 0xFF, frame_buffer_size);
  115.     esp_cache_msync((void *)frame_buffer, frame_buffer_size, ESP_CACHE_MSYNC_FLAG_DIR_C2M);
  116.     ESP_ERROR_CHECK(esp_cam_ctlr_start(cam_handle));
  117.  
  118.     while (1) {
  119.         ESP_ERROR_CHECK(esp_cam_ctlr_receive(cam_handle, &csi_trans, ESP_CAM_CTLR_MAX_DELAY));
  120.         printf("HERE? \n")l
  121.     }
  122. }

Who is online

Users browsing this forum: No registered users and 75 guests