esp_camera.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. // Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #include "time.h"
  18. #include "sys/time.h"
  19. #include "freertos/FreeRTOS.h"
  20. #include "freertos/task.h"
  21. #include "driver/gpio.h"
  22. #include "esp_system.h"
  23. #include "nvs_flash.h"
  24. #include "nvs.h"
  25. #include "sensor.h"
  26. #include "sccb.h"
  27. #include "cam_hal.h"
  28. #include "esp_camera.h"
  29. #include "xclk.h"
  30. #if CONFIG_OV2640_SUPPORT
  31. #include "ov2640.h"
  32. #endif
  33. #if CONFIG_OV7725_SUPPORT
  34. #include "ov7725.h"
  35. #endif
  36. #if CONFIG_OV3660_SUPPORT
  37. #include "ov3660.h"
  38. #endif
  39. #if CONFIG_OV5640_SUPPORT
  40. #include "ov5640.h"
  41. #endif
  42. #if CONFIG_NT99141_SUPPORT
  43. #include "nt99141.h"
  44. #endif
  45. #if CONFIG_OV7670_SUPPORT
  46. #include "ov7670.h"
  47. #endif
  48. #if CONFIG_GC2145_SUPPORT
  49. #include "gc2145.h"
  50. #endif
  51. #if CONFIG_GC032A_SUPPORT
  52. #include "gc032a.h"
  53. #endif
  54. #if CONFIG_GC0308_SUPPORT
  55. #include "gc0308.h"
  56. #endif
  57. #if CONFIG_BF3005_SUPPORT
  58. #include "bf3005.h"
  59. #endif
  60. #if CONFIG_BF20A6_SUPPORT
  61. #include "bf20a6.h"
  62. #endif
  63. #if CONFIG_SC101IOT_SUPPORT
  64. #include "sc101iot.h"
  65. #endif
  66. #if CONFIG_SC030IOT_SUPPORT
  67. #include "sc030iot.h"
  68. #endif
  69. #if CONFIG_SC031GS_SUPPORT
  70. #include "sc031gs.h"
  71. #endif
  72. #if CONFIG_MEGA_CCM_SUPPORT
  73. #include "mega_ccm.h"
  74. #endif
  75. #if defined(ARDUINO_ARCH_ESP32) && defined(CONFIG_ARDUHAL_ESP_LOG)
  76. #include "esp32-hal-log.h"
  77. #define TAG ""
  78. #else
  79. #include "esp_log.h"
  80. static const char *TAG = "camera";
  81. #endif
  82. typedef struct {
  83. sensor_t sensor;
  84. camera_fb_t fb;
  85. } camera_state_t;
  86. static const char *CAMERA_SENSOR_NVS_KEY = "sensor";
  87. static const char *CAMERA_PIXFORMAT_NVS_KEY = "pixformat";
  88. static camera_state_t *s_state = NULL;
  89. #if CONFIG_IDF_TARGET_ESP32S3 // LCD_CAM module of ESP32-S3 will generate xclk
  90. #define CAMERA_ENABLE_OUT_CLOCK(v)
  91. #define CAMERA_DISABLE_OUT_CLOCK()
  92. #else
  93. #define CAMERA_ENABLE_OUT_CLOCK(v) camera_enable_out_clock((v))
  94. #define CAMERA_DISABLE_OUT_CLOCK() camera_disable_out_clock()
  95. #endif
  96. typedef struct {
  97. int (*detect)(int slv_addr, sensor_id_t *id);
  98. int (*init)(sensor_t *sensor);
  99. } sensor_func_t;
  100. static const sensor_func_t g_sensors[] = {
  101. #if CONFIG_OV7725_SUPPORT
  102. {ov7725_detect, ov7725_init},
  103. #endif
  104. #if CONFIG_OV7670_SUPPORT
  105. {ov7670_detect, ov7670_init},
  106. #endif
  107. #if CONFIG_OV2640_SUPPORT
  108. {ov2640_detect, ov2640_init},
  109. #endif
  110. #if CONFIG_OV3660_SUPPORT
  111. {ov3660_detect, ov3660_init},
  112. #endif
  113. #if CONFIG_OV5640_SUPPORT
  114. {ov5640_detect, ov5640_init},
  115. #endif
  116. #if CONFIG_NT99141_SUPPORT
  117. {nt99141_detect, nt99141_init},
  118. #endif
  119. #if CONFIG_GC2145_SUPPORT
  120. {gc2145_detect, gc2145_init},
  121. #endif
  122. #if CONFIG_GC032A_SUPPORT
  123. {gc032a_detect, gc032a_init},
  124. #endif
  125. #if CONFIG_GC0308_SUPPORT
  126. {gc0308_detect, gc0308_init},
  127. #endif
  128. #if CONFIG_BF3005_SUPPORT
  129. {bf3005_detect, bf3005_init},
  130. #endif
  131. #if CONFIG_BF20A6_SUPPORT
  132. {bf20a6_detect, bf20a6_init},
  133. #endif
  134. #if CONFIG_SC101IOT_SUPPORT
  135. {sc101iot_detect, sc101iot_init},
  136. #endif
  137. #if CONFIG_SC030IOT_SUPPORT
  138. {sc030iot_detect, sc030iot_init},
  139. #endif
  140. #if CONFIG_SC031GS_SUPPORT
  141. {sc031gs_detect, sc031gs_init},
  142. #endif
  143. #if CONFIG_MEGA_CCM_SUPPORT
  144. {mega_ccm_detect, mega_ccm_init},
  145. #endif
  146. };
  147. static esp_err_t camera_probe(const camera_config_t *config, camera_model_t *out_camera_model)
  148. {
  149. esp_err_t ret = ESP_OK;
  150. *out_camera_model = CAMERA_NONE;
  151. if (s_state != NULL) {
  152. return ESP_ERR_INVALID_STATE;
  153. }
  154. s_state = (camera_state_t *) calloc(1, sizeof(camera_state_t));
  155. if (!s_state) {
  156. return ESP_ERR_NO_MEM;
  157. }
  158. if (config->pin_xclk >= 0) {
  159. ESP_LOGD(TAG, "Enabling XCLK output");
  160. CAMERA_ENABLE_OUT_CLOCK(config);
  161. }
  162. if (config->pin_sccb_sda != -1) {
  163. ESP_LOGD(TAG, "Initializing SCCB");
  164. ret = SCCB_Init(config->pin_sccb_sda, config->pin_sccb_scl);
  165. } else {
  166. ESP_LOGD(TAG, "Using existing I2C port");
  167. ret = SCCB_Use_Port(config->sccb_i2c_port);
  168. }
  169. if(ret != ESP_OK) {
  170. ESP_LOGE(TAG, "sccb init err");
  171. goto err;
  172. }
  173. if (config->pin_pwdn >= 0) {
  174. ESP_LOGD(TAG, "Resetting camera by power down line");
  175. gpio_config_t conf = { 0 };
  176. conf.pin_bit_mask = 1LL << config->pin_pwdn;
  177. conf.mode = GPIO_MODE_OUTPUT;
  178. gpio_config(&conf);
  179. // carefull, logic is inverted compared to reset pin
  180. gpio_set_level(config->pin_pwdn, 1);
  181. vTaskDelay(10 / portTICK_PERIOD_MS);
  182. gpio_set_level(config->pin_pwdn, 0);
  183. vTaskDelay(10 / portTICK_PERIOD_MS);
  184. }
  185. if (config->pin_reset >= 0) {
  186. ESP_LOGD(TAG, "Resetting camera");
  187. gpio_config_t conf = { 0 };
  188. conf.pin_bit_mask = 1LL << config->pin_reset;
  189. conf.mode = GPIO_MODE_OUTPUT;
  190. gpio_config(&conf);
  191. gpio_set_level(config->pin_reset, 0);
  192. vTaskDelay(10 / portTICK_PERIOD_MS);
  193. gpio_set_level(config->pin_reset, 1);
  194. vTaskDelay(10 / portTICK_PERIOD_MS);
  195. }
  196. ESP_LOGD(TAG, "Searching for camera address");
  197. vTaskDelay(10 / portTICK_PERIOD_MS);
  198. uint8_t slv_addr = SCCB_Probe();
  199. if (slv_addr == 0) {
  200. ret = ESP_ERR_NOT_FOUND;
  201. goto err;
  202. }
  203. ESP_LOGI(TAG, "Detected camera at address=0x%02x", slv_addr);
  204. s_state->sensor.slv_addr = slv_addr;
  205. s_state->sensor.xclk_freq_hz = config->xclk_freq_hz;
  206. /**
  207. * Read sensor ID and then initialize sensor
  208. * Attention: Some sensors have the same SCCB address. Therefore, several attempts may be made in the detection process
  209. */
  210. sensor_id_t *id = &s_state->sensor.id;
  211. for (size_t i = 0; i < sizeof(g_sensors) / sizeof(sensor_func_t); i++) {
  212. if (g_sensors[i].detect(slv_addr, id)) {
  213. camera_sensor_info_t *info = esp_camera_sensor_get_info(id);
  214. if (NULL != info) {
  215. *out_camera_model = info->model;
  216. ESP_LOGI(TAG, "Detected %s camera", info->name);
  217. g_sensors[i].init(&s_state->sensor);
  218. break;
  219. }
  220. }
  221. }
  222. if (CAMERA_NONE == *out_camera_model) { //If no supported sensors are detected
  223. ESP_LOGE(TAG, "Detected camera not supported.");
  224. ret = ESP_ERR_NOT_SUPPORTED;
  225. goto err;
  226. }
  227. ESP_LOGI(TAG, "Camera PID=0x%02x VER=0x%02x MIDL=0x%02x MIDH=0x%02x",
  228. id->PID, id->VER, id->MIDH, id->MIDL);
  229. ESP_LOGD(TAG, "Doing SW reset of sensor");
  230. vTaskDelay(10 / portTICK_PERIOD_MS);
  231. return s_state->sensor.reset(&s_state->sensor);
  232. err :
  233. CAMERA_DISABLE_OUT_CLOCK();
  234. return ret;
  235. }
  236. #if CONFIG_CAMERA_CONVERTER_ENABLED
  237. static pixformat_t get_output_data_format(camera_conv_mode_t conv_mode)
  238. {
  239. pixformat_t format = PIXFORMAT_RGB565;
  240. switch (conv_mode) {
  241. case YUV422_TO_YUV420:
  242. format = PIXFORMAT_YUV420;
  243. break;
  244. case YUV422_TO_RGB565: // default format is RGB565
  245. default:
  246. break;
  247. }
  248. ESP_LOGD(TAG, "Convert to %d format enabled", format);
  249. return format;
  250. }
  251. #endif
  252. esp_err_t esp_camera_init(const camera_config_t *config)
  253. {
  254. esp_err_t err;
  255. err = cam_init(config);
  256. if (err != ESP_OK) {
  257. ESP_LOGE(TAG, "Camera init failed with error 0x%x", err);
  258. return err;
  259. }
  260. camera_model_t camera_model = CAMERA_NONE;
  261. err = camera_probe(config, &camera_model);
  262. if (err != ESP_OK) {
  263. ESP_LOGE(TAG, "Camera probe failed with error 0x%x(%s)", err, esp_err_to_name(err));
  264. goto fail;
  265. }
  266. framesize_t frame_size = (framesize_t) config->frame_size;
  267. pixformat_t pix_format = (pixformat_t) config->pixel_format;
  268. if (PIXFORMAT_JPEG == pix_format && (!camera_sensor[camera_model].support_jpeg)) {
  269. ESP_LOGE(TAG, "JPEG format is not supported on this sensor");
  270. err = ESP_ERR_NOT_SUPPORTED;
  271. goto fail;
  272. }
  273. if (frame_size > camera_sensor[camera_model].max_size) {
  274. ESP_LOGW(TAG, "The frame size exceeds the maximum for this sensor, it will be forced to the maximum possible value");
  275. frame_size = camera_sensor[camera_model].max_size;
  276. }
  277. err = cam_config(config, frame_size, s_state->sensor.id.PID);
  278. if (err != ESP_OK) {
  279. ESP_LOGE(TAG, "Camera config failed with error 0x%x", err);
  280. goto fail;
  281. }
  282. s_state->sensor.status.framesize = frame_size;
  283. s_state->sensor.pixformat = pix_format;
  284. ESP_LOGD(TAG, "Setting frame size to %dx%d", resolution[frame_size].width, resolution[frame_size].height);
  285. if (s_state->sensor.set_framesize(&s_state->sensor, frame_size) != 0) {
  286. ESP_LOGE(TAG, "Failed to set frame size");
  287. err = ESP_ERR_CAMERA_FAILED_TO_SET_FRAME_SIZE;
  288. goto fail;
  289. }
  290. s_state->sensor.set_pixformat(&s_state->sensor, pix_format);
  291. #if CONFIG_CAMERA_CONVERTER_ENABLED
  292. if(config->conv_mode) {
  293. s_state->sensor.pixformat = get_output_data_format(config->conv_mode); // If conversion enabled, change the out data format by conversion mode
  294. }
  295. #endif
  296. if (s_state->sensor.id.PID == OV2640_PID) {
  297. s_state->sensor.set_gainceiling(&s_state->sensor, GAINCEILING_2X);
  298. s_state->sensor.set_bpc(&s_state->sensor, false);
  299. s_state->sensor.set_wpc(&s_state->sensor, true);
  300. s_state->sensor.set_lenc(&s_state->sensor, true);
  301. }
  302. if (pix_format == PIXFORMAT_JPEG) {
  303. s_state->sensor.set_quality(&s_state->sensor, config->jpeg_quality);
  304. }
  305. s_state->sensor.init_status(&s_state->sensor);
  306. cam_start();
  307. return ESP_OK;
  308. fail:
  309. esp_camera_deinit();
  310. return err;
  311. }
  312. esp_err_t esp_camera_deinit()
  313. {
  314. esp_err_t ret = cam_deinit();
  315. CAMERA_DISABLE_OUT_CLOCK();
  316. if (s_state) {
  317. SCCB_Deinit();
  318. free(s_state);
  319. s_state = NULL;
  320. }
  321. return ret;
  322. }
  323. #define FB_GET_TIMEOUT (10*4000 / portTICK_PERIOD_MS)
  324. camera_fb_t *esp_camera_fb_get()
  325. {
  326. if (s_state == NULL) {
  327. return NULL;
  328. }
  329. camera_fb_t *fb = cam_take(FB_GET_TIMEOUT);
  330. ESP_LOGI(TAG, "测试");
  331. //set the frame properties
  332. if (fb) {
  333. fb->width = resolution[s_state->sensor.status.framesize].width;
  334. fb->height = resolution[s_state->sensor.status.framesize].height;
  335. fb->format = s_state->sensor.pixformat;
  336. return fb;
  337. }else{
  338. return NULL;
  339. }
  340. }
  341. void esp_camera_fb_return(camera_fb_t *fb)
  342. {
  343. if (s_state == NULL) {
  344. return;
  345. }
  346. cam_give(fb);
  347. }
  348. sensor_t *esp_camera_sensor_get()
  349. {
  350. if (s_state == NULL) {
  351. return NULL;
  352. }
  353. return &s_state->sensor;
  354. }
  355. esp_err_t esp_camera_save_to_nvs(const char *key)
  356. {
  357. #if ESP_IDF_VERSION_MAJOR > 3
  358. nvs_handle_t handle;
  359. #else
  360. nvs_handle handle;
  361. #endif
  362. esp_err_t ret = nvs_open(key, NVS_READWRITE, &handle);
  363. if (ret == ESP_OK) {
  364. sensor_t *s = esp_camera_sensor_get();
  365. if (s != NULL) {
  366. ret = nvs_set_blob(handle, CAMERA_SENSOR_NVS_KEY, &s->status, sizeof(camera_status_t));
  367. if (ret == ESP_OK) {
  368. uint8_t pf = s->pixformat;
  369. ret = nvs_set_u8(handle, CAMERA_PIXFORMAT_NVS_KEY, pf);
  370. }
  371. return ret;
  372. } else {
  373. return ESP_ERR_CAMERA_NOT_DETECTED;
  374. }
  375. nvs_close(handle);
  376. return ret;
  377. } else {
  378. return ret;
  379. }
  380. }
  381. esp_err_t esp_camera_load_from_nvs(const char *key)
  382. {
  383. #if ESP_IDF_VERSION_MAJOR > 3
  384. nvs_handle_t handle;
  385. #else
  386. nvs_handle handle;
  387. #endif
  388. uint8_t pf;
  389. esp_err_t ret = nvs_open(key, NVS_READWRITE, &handle);
  390. if (ret == ESP_OK) {
  391. sensor_t *s = esp_camera_sensor_get();
  392. camera_status_t st;
  393. if (s != NULL) {
  394. size_t size = sizeof(camera_status_t);
  395. ret = nvs_get_blob(handle, CAMERA_SENSOR_NVS_KEY, &st, &size);
  396. if (ret == ESP_OK) {
  397. s->set_ae_level(s, st.ae_level);
  398. s->set_aec2(s, st.aec2);
  399. s->set_aec_value(s, st.aec_value);
  400. s->set_agc_gain(s, st.agc_gain);
  401. s->set_awb_gain(s, st.awb_gain);
  402. s->set_bpc(s, st.bpc);
  403. s->set_brightness(s, st.brightness);
  404. s->set_colorbar(s, st.colorbar);
  405. s->set_contrast(s, st.contrast);
  406. s->set_dcw(s, st.dcw);
  407. s->set_denoise(s, st.denoise);
  408. s->set_exposure_ctrl(s, st.aec);
  409. s->set_framesize(s, st.framesize);
  410. s->set_gain_ctrl(s, st.agc);
  411. s->set_gainceiling(s, st.gainceiling);
  412. s->set_hmirror(s, st.hmirror);
  413. s->set_lenc(s, st.lenc);
  414. s->set_quality(s, st.quality);
  415. s->set_raw_gma(s, st.raw_gma);
  416. s->set_saturation(s, st.saturation);
  417. s->set_sharpness(s, st.sharpness);
  418. s->set_special_effect(s, st.special_effect);
  419. s->set_vflip(s, st.vflip);
  420. s->set_wb_mode(s, st.wb_mode);
  421. s->set_whitebal(s, st.awb);
  422. s->set_wpc(s, st.wpc);
  423. }
  424. ret = nvs_get_u8(handle, CAMERA_PIXFORMAT_NVS_KEY, &pf);
  425. if (ret == ESP_OK) {
  426. s->set_pixformat(s, pf);
  427. }
  428. } else {
  429. return ESP_ERR_CAMERA_NOT_DETECTED;
  430. }
  431. nvs_close(handle);
  432. return ret;
  433. } else {
  434. ESP_LOGW(TAG, "Error (%d) opening nvs key \"%s\"", ret, key);
  435. return ret;
  436. }
  437. }
  438. void esp_camera_return_all(void) {
  439. if (s_state == NULL) {
  440. return;
  441. }
  442. cam_give_all();
  443. }