当前位置:   article > 正文

ESP32之esp_camera分析(一)_esp32-cam 拍出来的照片偏绿

esp32-cam 拍出来的照片偏绿

初始化分析,首先定义一个camera_config_t类型的结构体变量,然后给结构中的成员赋值,再调用err = esp_camera_init(&camera_config);函数进行初始化,代码如下:

  1. esp_err_t err;
  2. camera_config_t camera_config = {
  3. .pin_pwdn = CAM_PWDN,
  4. .pin_reset = CAM_RET,
  5. .pin_xclk = CAM_XCLK,
  6. .pin_sscb_sda = CAM_SDA,
  7. .pin_sscb_scl = CAM_SCL,
  8. .pin_d7 = CAM_D7,
  9. .pin_d6 = CAM_D6,
  10. .pin_d5 = CAM_D5,
  11. .pin_d4 = CAM_D4,
  12. .pin_d3 = CAM_D3,
  13. .pin_d2 = CAM_D2,
  14. .pin_d1 = CAM_D1,
  15. .pin_d0 = CAM_D0,
  16. .pin_vsync = CAM_VSYNC,
  17. .pin_href = CAM_HSYNC,
  18. .pin_pclk = CAM_PCLK,
  19. //XCLK 20MHz or 10MHz for OV2640 double FPS (Experimental)
  20. .xclk_freq_hz = 24000000,
  21. .ledc_timer = LEDC_TIMER_0,
  22. .ledc_channel = LEDC_CHANNEL_0,
  23. #ifdef CONFIG_CAMERA_JPEG_MODE
  24. .pixel_format = PIXFORMAT_JPEG, //YUV422,GRAYSCALE,RGB565,JPEG
  25. #else
  26. .pixel_format = PIXFORMAT_GRAYSCALE,
  27. #endif
  28. .frame_size = FRAMESIZE_QVGA, //QQVGA-UXGA Do not use sizes above QVGA when not JPEG
  29. .jpeg_quality = 12, //0-63 lower number means higher quality
  30. .fb_count = 2, //if more than one, i2s runs in continuous mode. Use only with JPEG
  31. .grab_mode = CAMERA_GRAB_WHEN_EMPTY//CAMERA_GRAB_LATEST. Sets when buffers should be filled
  32. };
  33. //initialize the camera
  34. err = esp_camera_init(&camera_config);

esp_camera_init()函数过程分析,调用cam_init()初始化camera外设;接着调用camera_probe()检测sensor是否存在,如果存在,则对senor进行初始化,包括硬件复位,通过i2c接口对寄存器初始化;接着调用cam_config(),设置窗口模式,初始化cam_obj结构,创建cam_task任务并启动;最后调用cam_start()使能vsync_pin的中断接收服务,vsync为帧同步信号。

  1. esp_err_t cam_config(const camera_config_t *config, framesize_t frame_size, uint16_t sensor_pid)
  2. {
  3. CAM_CHECK(NULL != config, "config pointer is invalid", ESP_ERR_INVALID_ARG);
  4. esp_err_t ret = ESP_OK;
  5. ret = ll_cam_set_sample_mode(cam_obj, (pixformat_t)config->pixel_format, config->xclk_freq_hz, sensor_pid);
  6. cam_obj->jpeg_mode = config->pixel_format == PIXFORMAT_JPEG;
  7. #if CONFIG_IDF_TARGET_ESP32
  8. cam_obj->psram_mode = false;
  9. #else
  10. cam_obj->psram_mode = (config->xclk_freq_hz == 16000000);
  11. #endif
  12. cam_obj->frame_cnt = config->fb_count;
  13. cam_obj->width = resolution[frame_size].width;
  14. cam_obj->height = resolution[frame_size].height;
  15. if(cam_obj->jpeg_mode){
  16. cam_obj->recv_size = cam_obj->width * cam_obj->height / 5;
  17. cam_obj->fb_size = cam_obj->recv_size;
  18. } else {
  19. cam_obj->recv_size = cam_obj->width * cam_obj->height * cam_obj->in_bytes_per_pixel;
  20. cam_obj->fb_size = cam_obj->width * cam_obj->height * cam_obj->fb_bytes_per_pixel;
  21. }
  22. ret = cam_dma_config(config);
  23. CAM_CHECK_GOTO(ret == ESP_OK, "cam_dma_config failed", err);
  24. cam_obj->event_queue = xQueueCreate(cam_obj->dma_half_buffer_cnt - 1, sizeof(cam_event_t));
  25. CAM_CHECK_GOTO(cam_obj->event_queue != NULL, "event_queue create failed", err);
  26. size_t frame_buffer_queue_len = cam_obj->frame_cnt;
  27. if (config->grab_mode == CAMERA_GRAB_LATEST && cam_obj->frame_cnt > 1) {
  28. frame_buffer_queue_len = cam_obj->frame_cnt - 1;
  29. }
  30. cam_obj->frame_buffer_queue = xQueueCreate(frame_buffer_queue_len, sizeof(camera_fb_t*));
  31. CAM_CHECK_GOTO(cam_obj->frame_buffer_queue != NULL, "frame_buffer_queue create failed", err);
  32. ret = ll_cam_init_isr(cam_obj);
  33. CAM_CHECK_GOTO(ret == ESP_OK, "cam intr alloc failed", err);
  34. #if CONFIG_CAMERA_CORE0
  35. xTaskCreatePinnedToCore(cam_task, "cam_task", 2048, NULL, configMAX_PRIORITIES - 2, &cam_obj->task_handle, 0);
  36. #elif CONFIG_CAMERA_CORE1
  37. xTaskCreatePinnedToCore(cam_task, "cam_task", 2048, NULL, configMAX_PRIORITIES - 2, &cam_obj->task_handle, 1);
  38. #else
  39. xTaskCreate(cam_task, "cam_task", 2048, NULL, configMAX_PRIORITIES - 2, &cam_obj->task_handle);
  40. #endif
  41. ESP_LOGI(TAG, "cam config ok");
  42. return ESP_OK;
  43. err:
  44. cam_deinit();
  45. return ESP_FAIL;
  46. }

cam_config()中有一句代码设置是否使用PSRAM方式,如果是ESP32-S2处理器,xclk_freq_hz为16000000Hz,则使用PSRAM方式。

esp_camera_init()之后,sensor会不断的输出图像,cam_task任务会不断的被唤醒。

ESP32/8266技术交流QQ群:1018891543

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/正经夜光杯/article/detail/896443
推荐阅读
相关标签
  

闽ICP备14008679号