ESP32-S3模组上跑通esp32-camera(11)
接前一篇文章:ESP32-S3模组上跑通esp32-camera(10)
本文内容参考:
esp32-camera入门(基于ESP-IDF)_esp32 camera-CSDN博客
OV5640手册解读-CSDN博客
ESP32_CAM CameraWebServer例程源码解析笔记(一)_void startcameraserver();-CSDN博客
特此致谢!
一、OV5640初始化
1. 配置接线和驱动
#include "esp_camera.h"//WROVER-KIT PIN Map
#define CAM_PIN_PWDN -1 //power down is not used
#define CAM_PIN_RESET -1 //software reset will be performed
#define CAM_PIN_XCLK 21
#define CAM_PIN_SIOD 26
#define CAM_PIN_SIOC 27#define CAM_PIN_D7 35
#define CAM_PIN_D6 34
#define CAM_PIN_D5 39
#define CAM_PIN_D4 36
#define CAM_PIN_D3 19
#define CAM_PIN_D2 18
#define CAM_PIN_D1 5
#define CAM_PIN_D0 4
#define CAM_PIN_VSYNC 25
#define CAM_PIN_HREF 23
#define CAM_PIN_PCLK 22static camera_config_t camera_config = {.pin_pwdn = CAM_PIN_PWDN,.pin_reset = CAM_PIN_RESET,.pin_xclk = CAM_PIN_XCLK,.pin_sccb_sda = CAM_PIN_SIOD,.pin_sccb_scl = CAM_PIN_SIOC,.pin_d7 = CAM_PIN_D7,.pin_d6 = CAM_PIN_D6,.pin_d5 = CAM_PIN_D5,.pin_d4 = CAM_PIN_D4,.pin_d3 = CAM_PIN_D3,.pin_d2 = CAM_PIN_D2,.pin_d1 = CAM_PIN_D1,.pin_d0 = CAM_PIN_D0,.pin_vsync = CAM_PIN_VSYNC,.pin_href = CAM_PIN_HREF,.pin_pclk = CAM_PIN_PCLK,.xclk_freq_hz = 20000000,//EXPERIMENTAL: Set to 16MHz on ESP32-S2 or ESP32-S3 to enable EDMA mode.ledc_timer = LEDC_TIMER_0,.ledc_channel = LEDC_CHANNEL_0,.pixel_format = PIXFORMAT_JPEG,//YUV422,GRAYSCALE,RGB565,JPEG.frame_size = FRAMESIZE_UXGA,//QQVGA-UXGA, For ESP32, do not use sizes above QVGA when not JPEG. The performance of the ESP32-S series has improved a lot, but JPEG mode always gives better frame rates..jpeg_quality = 12, //0-63, for OV series camera sensors, lower number means higher quality.fb_count = 1, //When jpeg mode is used, if fb_count more than one, the driver will work in continuous mode..grab_mode = CAMERA_GRAB_WHEN_EMPTY//CAMERA_GRAB_LATEST. Sets when buffers should be filled
};esp_err_t camera_init(){//power up the camera if PWDN pin is definedif(CAM_PIN_PWDN != -1){pinMode(CAM_PIN_PWDN, OUTPUT);digitalWrite(CAM_PIN_PWDN, LOW);}//initialize the cameraesp_err_t err = esp_camera_init(&camera_config);if (err != ESP_OK) {ESP_LOGE(TAG, "Camera Init Failed");return err;}return ESP_OK;
}esp_err_t camera_capture(){//acquire a framecamera_fb_t * fb = esp_camera_fb_get();if (!fb) {ESP_LOGE(TAG, "Camera Capture Failed");return ESP_FAIL;}//replace this with your own functionprocess_image(fb->width, fb->height, fb->format, fb->buf, fb->len);//return the frame buffer back to the driver for reuseesp_camera_fb_return(fb);return ESP_OK;
}
上一回解析了camera_config_t结构及其实例camera_config的最后一段代码,按计划应该开始解析函数的具体代码了,但是在此还有一个问题要明确一下。
上边代码段中的这一部分:
.pin_pwdn = CAM_PIN_PWDN,.pin_reset = CAM_PIN_RESET,.pin_xclk = CAM_PIN_XCLK,.pin_sccb_sda = CAM_PIN_SIOD,.pin_sccb_scl = CAM_PIN_SIOC,.pin_d7 = CAM_PIN_D7,.pin_d6 = CAM_PIN_D6,.pin_d5 = CAM_PIN_D5,.pin_d4 = CAM_PIN_D4,.pin_d3 = CAM_PIN_D3,.pin_d2 = CAM_PIN_D2,.pin_d1 = CAM_PIN_D1,.pin_d0 = CAM_PIN_D0,.pin_vsync = CAM_PIN_VSYNC,.pin_href = CAM_PIN_HREF,.pin_pclk = CAM_PIN_PCLK,
其中的CAM_PIN_xxx宏定义在前边文章笔者说,都是根据自己实际板子的接线而定的,代码开头处也给出了这些引脚的宏定义:
//WROVER-KIT PIN Map
#define CAM_PIN_PWDN -1 //power down is not used
#define CAM_PIN_RESET -1 //software reset will be performed
#define CAM_PIN_XCLK 21
#define CAM_PIN_SIOD 26
#define CAM_PIN_SIOC 27#define CAM_PIN_D7 35
#define CAM_PIN_D6 34
#define CAM_PIN_D5 39
#define CAM_PIN_D4 36
#define CAM_PIN_D3 19
#define CAM_PIN_D2 18
#define CAM_PIN_D1 5
#define CAM_PIN_D0 4
#define CAM_PIN_VSYNC 25
#define CAM_PIN_HREF 23
#define CAM_PIN_PCLK 22
从宏定义上边的注释就能看出,这些引脚是匹配WROVER-KIT这个开发板的。如果是自己做的板子,可以直接对这些宏的值进行修改,改为自己的就可以(注意,上边宏的数字是对应ESP32-S3的GPIO号的,并不是引脚号)。
接下来是重点。除了直接修改宏定义的方法外,乐鑫ESP-IDF中也提供了通过menuconfig配置的方式进行指定。
在ESP-IDF工程项目中搜索以上任一关键字(如CAMERA_PIN_XCLK),会得到很多结果,其中有两处关键的地方:
- app_camera.hpp文件中
app_camera.hpp文件中定义了不同开发板的camera相关的引脚定义:
#if CONFIG_CAMERA_MODULE_WROVER_KIT
#define CAMERA_MODULE_NAME "Wrover Kit"
#define CAMERA_PIN_PWDN -1
#define CAMERA_PIN_RESET -1
#define CAMERA_PIN_XCLK 21
#define CAMERA_PIN_SIOD 26
#define CAMERA_PIN_SIOC 27#define CAMERA_PIN_D7 35
#define CAMERA_PIN_D6 34
#define CAMERA_PIN_D5 39
#define CAMERA_PIN_D4 36
#define CAMERA_PIN_D3 19
#define CAMERA_PIN_D2 18
#define CAMERA_PIN_D1 5
#define CAMERA_PIN_D0 4
#define CAMERA_PIN_VSYNC 25
#define CAMERA_PIN_HREF 23
#define CAMERA_PIN_PCLK 22#elif CONFIG_CAMERA_MODULE_ESP_EYE
#define CAMERA_MODULE_NAME "ESP-EYE"
#define CAMERA_PIN_PWDN -1
#define CAMERA_PIN_RESET -1
#define CAMERA_PIN_XCLK 4
#define CAMERA_PIN_SIOD 18
#define CAMERA_PIN_SIOC 23#define CAMERA_PIN_D7 36
#define CAMERA_PIN_D6 37
#define CAMERA_PIN_D5 38
#define CAMERA_PIN_D4 39
#define CAMERA_PIN_D3 35
#define CAMERA_PIN_D2 14
#define CAMERA_PIN_D1 13
#define CAMERA_PIN_D0 34
#define CAMERA_PIN_VSYNC 5
#define CAMERA_PIN_HREF 27
#define CAMERA_PIN_PCLK 25#elif CONFIG_CAMERA_MODULE_ESP_S2_KALUGA
#define CAMERA_MODULE_NAME "ESP-S2-KALUGA"
#define CAMERA_PIN_PWDN -1
#define CAMERA_PIN_RESET -1
#define CAMERA_PIN_XCLK 1
#define CAMERA_PIN_SIOD 8
#define CAMERA_PIN_SIOC 7#define CAMERA_PIN_D7 38
#define CAMERA_PIN_D6 21
#define CAMERA_PIN_D5 40
#define CAMERA_PIN_D4 39
#define CAMERA_PIN_D3 42
#define CAMERA_PIN_D2 41
#define CAMERA_PIN_D1 37
#define CAMERA_PIN_D0 36
#define CAMERA_PIN_VSYNC 2
#define CAMERA_PIN_HREF 3
#define CAMERA_PIN_PCLK 33#elif CONFIG_CAMERA_MODULE_ESP_S3_EYE
#define CAMERA_MODULE_NAME "ESP-S3-EYE"
#define CAMERA_PIN_PWDN -1
#define CAMERA_PIN_RESET -1#define CAMERA_PIN_VSYNC 6
#define CAMERA_PIN_HREF 7
#define CAMERA_PIN_PCLK 13
#define CAMERA_PIN_XCLK 15#define CAMERA_PIN_SIOD 4
#define CAMERA_PIN_SIOC 5#define CAMERA_PIN_D0 11
#define CAMERA_PIN_D1 9
#define CAMERA_PIN_D2 8
#define CAMERA_PIN_D3 10
#define CAMERA_PIN_D4 12
#define CAMERA_PIN_D5 18
#define CAMERA_PIN_D6 17
#define CAMERA_PIN_D7 16#elif CONFIG_CAMERA_MODEL_ESP32_CAM_BOARD
#define CAMERA_MODULE_NAME "ESP-DEVCAM"
#define CAMERA_PIN_PWDN 32
#define CAMERA_PIN_RESET 33#define CAMERA_PIN_XCLK 4
#define CAMERA_PIN_SIOD 18
#define CAMERA_PIN_SIOC 23#define CAMERA_PIN_D7 36
#define CAMERA_PIN_D6 19
#define CAMERA_PIN_D5 21
#define CAMERA_PIN_D4 39
#define CAMERA_PIN_D3 35
#define CAMERA_PIN_D2 14
#define CAMERA_PIN_D1 13
#define CAMERA_PIN_D0 34
#define CAMERA_PIN_VSYNC 5
#define CAMERA_PIN_HREF 27
#define CAMERA_PIN_PCLK 25#elif CONFIG_CAMERA_MODULE_M5STACK_PSRAM
#define CAMERA_MODULE_NAME "M5STACK-PSRAM"
#define CAMERA_PIN_PWDN -1
#define CAMERA_PIN_RESET 15#define CAMERA_PIN_XCLK 27
#define CAMERA_PIN_SIOD 25
#define CAMERA_PIN_SIOC 23#define CAMERA_PIN_D7 19
#define CAMERA_PIN_D6 36
#define CAMERA_PIN_D5 18
#define CAMERA_PIN_D4 39
#define CAMERA_PIN_D3 5
#define CAMERA_PIN_D2 34
#define CAMERA_PIN_D1 35
#define CAMERA_PIN_D0 32
#define CAMERA_PIN_VSYNC 22
#define CAMERA_PIN_HREF 26
#define CAMERA_PIN_PCLK 21#elif CONFIG_CAMERA_MODULE_M5STACK_WIDE
#define CAMERA_MODULE_NAME "M5STACK-WIDE"
#define CAMERA_PIN_PWDN -1
#define CAMERA_PIN_RESET 15
#define CAMERA_PIN_XCLK 27
#define CAMERA_PIN_SIOD 22
#define CAMERA_PIN_SIOC 23#define CAMERA_PIN_D7 19
#define CAMERA_PIN_D6 36
#define CAMERA_PIN_D5 18
#define CAMERA_PIN_D4 39
#define CAMERA_PIN_D3 5
#define CAMERA_PIN_D2 34
#define CAMERA_PIN_D1 35
#define CAMERA_PIN_D0 32
#define CAMERA_PIN_VSYNC 25
#define CAMERA_PIN_HREF 26
#define CAMERA_PIN_PCLK 21#elif CONFIG_CAMERA_MODULE_AI_THINKER
#define CAMERA_MODULE_NAME "AI-THINKER"
#define CAMERA_PIN_PWDN 32
#define CAMERA_PIN_RESET -1
#define CAMERA_PIN_XCLK 0
#define CAMERA_PIN_SIOD 26
#define CAMERA_PIN_SIOC 27#define CAMERA_PIN_D7 35
#define CAMERA_PIN_D6 34
#define CAMERA_PIN_D5 39
#define CAMERA_PIN_D4 36
#define CAMERA_PIN_D3 21
#define CAMERA_PIN_D2 19
#define CAMERA_PIN_D1 18
#define CAMERA_PIN_D0 5
#define CAMERA_PIN_VSYNC 25
#define CAMERA_PIN_HREF 23
#define CAMERA_PIN_PCLK 22#elif CONFIG_CAMERA_MODULE_CUSTOM
#define CAMERA_MODULE_NAME "CUSTOM"
#define CAMERA_PIN_PWDN CONFIG_CAMERA_PIN_PWDN
#define CAMERA_PIN_RESET CONFIG_CAMERA_PIN_RESET
#define CAMERA_PIN_XCLK CONFIG_CAMERA_PIN_XCLK
#define CAMERA_PIN_SIOD CONFIG_CAMERA_PIN_SIOD
#define CAMERA_PIN_SIOC CONFIG_CAMERA_PIN_SIOC#define CAMERA_PIN_D7 CONFIG_CAMERA_PIN_Y9
#define CAMERA_PIN_D6 CONFIG_CAMERA_PIN_Y8
#define CAMERA_PIN_D5 CONFIG_CAMERA_PIN_Y7
#define CAMERA_PIN_D4 CONFIG_CAMERA_PIN_Y6
#define CAMERA_PIN_D3 CONFIG_CAMERA_PIN_Y5
#define CAMERA_PIN_D2 CONFIG_CAMERA_PIN_Y4
#define CAMERA_PIN_D1 CONFIG_CAMERA_PIN_Y3
#define CAMERA_PIN_D0 CONFIG_CAMERA_PIN_Y2
#define CAMERA_PIN_VSYNC CONFIG_CAMERA_PIN_VSYNC
#define CAMERA_PIN_HREF CONFIG_CAMERA_PIN_HREF
#define CAMERA_PIN_PCLK CONFIG_CAMERA_PIN_PCLK
#endif
可以看到,一共包括了Wrover Kit、ESP-EYE、AI-THINKER等流行开发板的情况。如果你不是使用的以上开发板,而是自己开发的板子(如笔者的情况),那么上边的最后给出了自定义的方法,片段如下:
#elif CONFIG_CAMERA_MODULE_CUSTOM
#define CAMERA_MODULE_NAME "CUSTOM"
#define CAMERA_PIN_PWDN CONFIG_CAMERA_PIN_PWDN
#define CAMERA_PIN_RESET CONFIG_CAMERA_PIN_RESET
#define CAMERA_PIN_XCLK CONFIG_CAMERA_PIN_XCLK
#define CAMERA_PIN_SIOD CONFIG_CAMERA_PIN_SIOD
#define CAMERA_PIN_SIOC CONFIG_CAMERA_PIN_SIOC#define CAMERA_PIN_D7 CONFIG_CAMERA_PIN_Y9
#define CAMERA_PIN_D6 CONFIG_CAMERA_PIN_Y8
#define CAMERA_PIN_D5 CONFIG_CAMERA_PIN_Y7
#define CAMERA_PIN_D4 CONFIG_CAMERA_PIN_Y6
#define CAMERA_PIN_D3 CONFIG_CAMERA_PIN_Y5
#define CAMERA_PIN_D2 CONFIG_CAMERA_PIN_Y4
#define CAMERA_PIN_D1 CONFIG_CAMERA_PIN_Y3
#define CAMERA_PIN_D0 CONFIG_CAMERA_PIN_Y2
#define CAMERA_PIN_VSYNC CONFIG_CAMERA_PIN_VSYNC
#define CAMERA_PIN_HREF CONFIG_CAMERA_PIN_HREF
#define CAMERA_PIN_PCLK CONFIG_CAMERA_PIN_PCLK
#endif
可以看到,在自定义引脚的情况下,这些CAMERA_PIN_xxx都被定义为了CONFIG_CAMERA_PIN_xxx。而这些CONFIG_CAMERA_PIN_xxx又是在哪里定义的呢?这就要说第2个文件——sdkconfigs了。
- sdkconfigs文件中
sdkconfigs文件中的相关代码如下:
#
# Camera Configuration
#
# CONFIG_CAMERA_MODULE_WROVER_KIT is not set
# CONFIG_CAMERA_MODULE_ESP_EYE is not set
# CONFIG_CAMERA_MODULE_ESP_S2_KALUGA is not set
# CONFIG_CAMERA_MODULE_ESP_S3_EYE is not set
# CONFIG_CAMERA_MODULE_ESP32_CAM_BOARD is not set
# CONFIG_CAMERA_MODULE_M5STACK_PSRAM is not set
# CONFIG_CAMERA_MODULE_M5STACK_WIDE is not set
# CONFIG_CAMERA_MODULE_AI_THINKER is not set
CONFIG_CAMERA_MODULE_CUSTOM=y
CONFIG_CAMERA_PIN_PWDN=-1
CONFIG_CAMERA_PIN_RESET=-1
CONFIG_CAMERA_PIN_XCLK=11
CONFIG_CAMERA_PIN_SIOD=18
CONFIG_CAMERA_PIN_SIOC=21
CONFIG_CAMERA_PIN_VSYNC=14
CONFIG_CAMERA_PIN_HREF=13
CONFIG_CAMERA_PIN_PCLK=8
CONFIG_CAMERA_PIN_Y2=6
CONFIG_CAMERA_PIN_Y3=4
CONFIG_CAMERA_PIN_Y4=3
CONFIG_CAMERA_PIN_Y5=5
CONFIG_CAMERA_PIN_Y6=7
CONFIG_CAMERA_PIN_Y7=9
CONFIG_CAMERA_PIN_Y8=10
CONFIG_CAMERA_PIN_Y9=12
# end of Camera Configuration
sdkconfigs是编译时系统自动生成的,其内容需要通过menuconfig配置而得到。
在PowerShell或者直接在VSCode中执行idf.py menuconfig,在其中搜索“CAM_PIN_PWDN”,可以进入到以下界面:
选中某一项后按回车,即可设置其值:
修改后注意保存就可以了(其它引脚的修改配置也是同样的方法)。重新编译后,就修改了相应宏的值。
这样,关于引脚配置的内容就全都讲完了,下一回开始解析camera的初始化及配置。