esp_camera.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  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 defined(ARDUINO_ARCH_ESP32) && defined(CONFIG_ARDUHAL_ESP_LOG)
  70. #include "esp32-hal-log.h"
  71. #define TAG ""
  72. #else
  73. #include "esp_log.h"
  74. static const char *TAG = "camera";
  75. #endif
  76. typedef struct {
  77. sensor_t sensor;
  78. camera_fb_t fb;
  79. } camera_state_t;
  80. static const char *CAMERA_SENSOR_NVS_KEY = "sensor";
  81. static const char *CAMERA_PIXFORMAT_NVS_KEY = "pixformat";
  82. static camera_state_t *s_state = NULL;
  83. #if CONFIG_IDF_TARGET_ESP32S3 // LCD_CAM module of ESP32-S3 will generate xclk
  84. #define CAMERA_ENABLE_OUT_CLOCK(v)
  85. #define CAMERA_DISABLE_OUT_CLOCK()
  86. #else
  87. #define CAMERA_ENABLE_OUT_CLOCK(v) camera_enable_out_clock((v))
  88. #define CAMERA_DISABLE_OUT_CLOCK() camera_disable_out_clock()
  89. #endif
  90. typedef struct {
  91. int (*detect)(int slv_addr, sensor_id_t *id);
  92. int (*init)(sensor_t *sensor);
  93. } sensor_func_t;
  94. static const sensor_func_t g_sensors[] = {
  95. #if CONFIG_OV7725_SUPPORT
  96. {ov7725_detect, ov7725_init},
  97. #endif
  98. #if CONFIG_OV7670_SUPPORT
  99. {ov7670_detect, ov7670_init},
  100. #endif
  101. #if CONFIG_OV2640_SUPPORT
  102. {ov2640_detect, ov2640_init},
  103. #endif
  104. #if CONFIG_OV3660_SUPPORT
  105. {ov3660_detect, ov3660_init},
  106. #endif
  107. #if CONFIG_OV5640_SUPPORT
  108. {ov5640_detect, ov5640_init},
  109. #endif
  110. #if CONFIG_NT99141_SUPPORT
  111. {nt99141_detect, nt99141_init},
  112. #endif
  113. #if CONFIG_GC2145_SUPPORT
  114. {gc2145_detect, gc2145_init},
  115. #endif
  116. #if CONFIG_GC032A_SUPPORT
  117. {gc032a_detect, gc032a_init},
  118. #endif
  119. #if CONFIG_GC0308_SUPPORT
  120. {gc0308_detect, gc0308_init},
  121. #endif
  122. #if CONFIG_BF3005_SUPPORT
  123. {bf3005_detect, bf3005_init},
  124. #endif
  125. #if CONFIG_BF20A6_SUPPORT
  126. {bf20a6_detect, bf20a6_init},
  127. #endif
  128. #if CONFIG_SC101IOT_SUPPORT
  129. {sc101iot_detect, sc101iot_init},
  130. #endif
  131. #if CONFIG_SC030IOT_SUPPORT
  132. {sc030iot_detect, sc030iot_init},
  133. #endif
  134. };
  135. static esp_err_t camera_probe(const camera_config_t *config, camera_model_t *out_camera_model)
  136. {
  137. esp_err_t ret = ESP_OK;
  138. *out_camera_model = CAMERA_NONE;
  139. if (s_state != NULL) {
  140. return ESP_ERR_INVALID_STATE;
  141. }
  142. s_state = (camera_state_t *) calloc(sizeof(camera_state_t), 1);
  143. if (!s_state) {
  144. return ESP_ERR_NO_MEM;
  145. }
  146. if (config->pin_xclk >= 0) {
  147. ESP_LOGD(TAG, "Enabling XCLK output");
  148. CAMERA_ENABLE_OUT_CLOCK(config);
  149. }
  150. if (config->pin_sccb_sda != -1) {
  151. ESP_LOGD(TAG, "Initializing SCCB");
  152. ret = SCCB_Init(config->pin_sccb_sda, config->pin_sccb_scl);
  153. } else {
  154. ESP_LOGD(TAG, "Using existing I2C port");
  155. ret = SCCB_Use_Port(config->sccb_i2c_port);
  156. }
  157. if(ret != ESP_OK) {
  158. ESP_LOGE(TAG, "sccb init err");
  159. goto err;
  160. }
  161. if (config->pin_pwdn >= 0) {
  162. ESP_LOGD(TAG, "Resetting camera by power down line");
  163. gpio_config_t conf = { 0 };
  164. conf.pin_bit_mask = 1LL << config->pin_pwdn;
  165. conf.mode = GPIO_MODE_OUTPUT;
  166. gpio_config(&conf);
  167. // carefull, logic is inverted compared to reset pin
  168. gpio_set_level(config->pin_pwdn, 1);
  169. vTaskDelay(10 / portTICK_PERIOD_MS);
  170. gpio_set_level(config->pin_pwdn, 0);
  171. vTaskDelay(10 / portTICK_PERIOD_MS);
  172. }
  173. if (config->pin_reset >= 0) {
  174. ESP_LOGD(TAG, "Resetting camera");
  175. gpio_config_t conf = { 0 };
  176. conf.pin_bit_mask = 1LL << config->pin_reset;
  177. conf.mode = GPIO_MODE_OUTPUT;
  178. gpio_config(&conf);
  179. gpio_set_level(config->pin_reset, 0);
  180. vTaskDelay(10 / portTICK_PERIOD_MS);
  181. gpio_set_level(config->pin_reset, 1);
  182. vTaskDelay(10 / portTICK_PERIOD_MS);
  183. }
  184. ESP_LOGD(TAG, "Searching for camera address");
  185. vTaskDelay(10 / portTICK_PERIOD_MS);
  186. uint8_t slv_addr = SCCB_Probe();
  187. if (slv_addr == 0) {
  188. ret = ESP_ERR_NOT_FOUND;
  189. goto err;
  190. }
  191. ESP_LOGI(TAG, "Detected camera at address=0x%02x", slv_addr);
  192. s_state->sensor.slv_addr = slv_addr;
  193. s_state->sensor.xclk_freq_hz = config->xclk_freq_hz;
  194. /**
  195. * Read sensor ID and then initialize sensor
  196. * Attention: Some sensors have the same SCCB address. Therefore, several attempts may be made in the detection process
  197. */
  198. sensor_id_t *id = &s_state->sensor.id;
  199. for (size_t i = 0; i < sizeof(g_sensors) / sizeof(sensor_func_t); i++) {
  200. if (g_sensors[i].detect(slv_addr, id)) {
  201. camera_sensor_info_t *info = esp_camera_sensor_get_info(id);
  202. if (NULL != info) {
  203. *out_camera_model = info->model;
  204. ESP_LOGI(TAG, "Detected %s camera", info->name);
  205. g_sensors[i].init(&s_state->sensor);
  206. break;
  207. }
  208. }
  209. }
  210. if (CAMERA_NONE == *out_camera_model) { //If no supported sensors are detected
  211. ESP_LOGE(TAG, "Detected camera not supported.");
  212. ret = ESP_ERR_NOT_SUPPORTED;
  213. goto err;
  214. }
  215. ESP_LOGI(TAG, "Camera PID=0x%02x VER=0x%02x MIDL=0x%02x MIDH=0x%02x",
  216. id->PID, id->VER, id->MIDH, id->MIDL);
  217. ESP_LOGD(TAG, "Doing SW reset of sensor");
  218. vTaskDelay(10 / portTICK_PERIOD_MS);
  219. return s_state->sensor.reset(&s_state->sensor);
  220. err :
  221. CAMERA_DISABLE_OUT_CLOCK();
  222. return ret;
  223. }
  224. #if CONFIG_CAMERA_CONVERTER_ENABLED
  225. static pixformat_t get_output_data_format(camera_conv_mode_t conv_mode)
  226. {
  227. pixformat_t format = PIXFORMAT_RGB565;
  228. switch (conv_mode) {
  229. case YUV422_TO_YUV420:
  230. format = PIXFORMAT_YUV420;
  231. break;
  232. case YUV422_TO_RGB565: // default format is RGB565
  233. default:
  234. break;
  235. }
  236. ESP_LOGD(TAG, "Convert to %d format enabled", format);
  237. return format;
  238. }
  239. #endif
  240. esp_err_t esp_camera_init(const camera_config_t *config)
  241. {
  242. esp_err_t err;
  243. err = cam_init(config);
  244. if (err != ESP_OK) {
  245. ESP_LOGE(TAG, "Camera init failed with error 0x%x", err);
  246. return err;
  247. }
  248. camera_model_t camera_model = CAMERA_NONE;
  249. err = camera_probe(config, &camera_model);
  250. if (err != ESP_OK) {
  251. ESP_LOGE(TAG, "Camera probe failed with error 0x%x(%s)", err, esp_err_to_name(err));
  252. goto fail;
  253. }
  254. framesize_t frame_size = (framesize_t) config->frame_size;
  255. pixformat_t pix_format = (pixformat_t) config->pixel_format;
  256. if (PIXFORMAT_JPEG == pix_format && (!camera_sensor[camera_model].support_jpeg)) {
  257. ESP_LOGE(TAG, "JPEG format is not supported on this sensor");
  258. err = ESP_ERR_NOT_SUPPORTED;
  259. goto fail;
  260. }
  261. if (frame_size > camera_sensor[camera_model].max_size) {
  262. ESP_LOGW(TAG, "The frame size exceeds the maximum for this sensor, it will be forced to the maximum possible value");
  263. frame_size = camera_sensor[camera_model].max_size;
  264. }
  265. err = cam_config(config, frame_size, s_state->sensor.id.PID);
  266. if (err != ESP_OK) {
  267. ESP_LOGE(TAG, "Camera config failed with error 0x%x", err);
  268. goto fail;
  269. }
  270. s_state->sensor.status.framesize = frame_size;
  271. s_state->sensor.pixformat = pix_format;
  272. ESP_LOGD(TAG, "Setting frame size to %dx%d", resolution[frame_size].width, resolution[frame_size].height);
  273. if (s_state->sensor.set_framesize(&s_state->sensor, frame_size) != 0) {
  274. ESP_LOGE(TAG, "Failed to set frame size");
  275. err = ESP_ERR_CAMERA_FAILED_TO_SET_FRAME_SIZE;
  276. goto fail;
  277. }
  278. s_state->sensor.set_pixformat(&s_state->sensor, pix_format);
  279. #if CONFIG_CAMERA_CONVERTER_ENABLED
  280. if(config->conv_mode) {
  281. s_state->sensor.pixformat = get_output_data_format(config->conv_mode); // If conversion enabled, change the out data format by conversion mode
  282. }
  283. #endif
  284. if (s_state->sensor.id.PID == OV2640_PID) {
  285. s_state->sensor.set_gainceiling(&s_state->sensor, GAINCEILING_2X);
  286. s_state->sensor.set_bpc(&s_state->sensor, false);
  287. s_state->sensor.set_wpc(&s_state->sensor, true);
  288. s_state->sensor.set_lenc(&s_state->sensor, true);
  289. }
  290. if (pix_format == PIXFORMAT_JPEG) {
  291. s_state->sensor.set_quality(&s_state->sensor, config->jpeg_quality);
  292. }
  293. s_state->sensor.init_status(&s_state->sensor);
  294. cam_start();
  295. return ESP_OK;
  296. fail:
  297. esp_camera_deinit();
  298. return err;
  299. }
  300. esp_err_t esp_camera_deinit()
  301. {
  302. esp_err_t ret = cam_deinit();
  303. CAMERA_DISABLE_OUT_CLOCK();
  304. if (s_state) {
  305. SCCB_Deinit();
  306. free(s_state);
  307. s_state = NULL;
  308. }
  309. return ret;
  310. }
  311. #define FB_GET_TIMEOUT (4000 / portTICK_PERIOD_MS)
  312. camera_fb_t *esp_camera_fb_get()
  313. {
  314. if (s_state == NULL) {
  315. return NULL;
  316. }
  317. camera_fb_t *fb = cam_take(FB_GET_TIMEOUT);
  318. //set the frame properties
  319. if (fb) {
  320. fb->width = resolution[s_state->sensor.status.framesize].width;
  321. fb->height = resolution[s_state->sensor.status.framesize].height;
  322. fb->format = s_state->sensor.pixformat;
  323. }
  324. return fb;
  325. }
  326. void esp_camera_fb_return(camera_fb_t *fb)
  327. {
  328. if (s_state == NULL) {
  329. return;
  330. }
  331. cam_give(fb);
  332. }
  333. sensor_t *esp_camera_sensor_get()
  334. {
  335. if (s_state == NULL) {
  336. return NULL;
  337. }
  338. return &s_state->sensor;
  339. }
  340. esp_err_t esp_camera_save_to_nvs(const char *key)
  341. {
  342. #if ESP_IDF_VERSION_MAJOR > 3
  343. nvs_handle_t handle;
  344. #else
  345. nvs_handle handle;
  346. #endif
  347. esp_err_t ret = nvs_open(key, NVS_READWRITE, &handle);
  348. if (ret == ESP_OK) {
  349. sensor_t *s = esp_camera_sensor_get();
  350. if (s != NULL) {
  351. ret = nvs_set_blob(handle, CAMERA_SENSOR_NVS_KEY, &s->status, sizeof(camera_status_t));
  352. if (ret == ESP_OK) {
  353. uint8_t pf = s->pixformat;
  354. ret = nvs_set_u8(handle, CAMERA_PIXFORMAT_NVS_KEY, pf);
  355. }
  356. return ret;
  357. } else {
  358. return ESP_ERR_CAMERA_NOT_DETECTED;
  359. }
  360. nvs_close(handle);
  361. return ret;
  362. } else {
  363. return ret;
  364. }
  365. }
  366. esp_err_t esp_camera_load_from_nvs(const char *key)
  367. {
  368. #if ESP_IDF_VERSION_MAJOR > 3
  369. nvs_handle_t handle;
  370. #else
  371. nvs_handle handle;
  372. #endif
  373. uint8_t pf;
  374. esp_err_t ret = nvs_open(key, NVS_READWRITE, &handle);
  375. if (ret == ESP_OK) {
  376. sensor_t *s = esp_camera_sensor_get();
  377. camera_status_t st;
  378. if (s != NULL) {
  379. size_t size = sizeof(camera_status_t);
  380. ret = nvs_get_blob(handle, CAMERA_SENSOR_NVS_KEY, &st, &size);
  381. if (ret == ESP_OK) {
  382. s->set_ae_level(s, st.ae_level);
  383. s->set_aec2(s, st.aec2);
  384. s->set_aec_value(s, st.aec_value);
  385. s->set_agc_gain(s, st.agc_gain);
  386. s->set_awb_gain(s, st.awb_gain);
  387. s->set_bpc(s, st.bpc);
  388. s->set_brightness(s, st.brightness);
  389. s->set_colorbar(s, st.colorbar);
  390. s->set_contrast(s, st.contrast);
  391. s->set_dcw(s, st.dcw);
  392. s->set_denoise(s, st.denoise);
  393. s->set_exposure_ctrl(s, st.aec);
  394. s->set_framesize(s, st.framesize);
  395. s->set_gain_ctrl(s, st.agc);
  396. s->set_gainceiling(s, st.gainceiling);
  397. s->set_hmirror(s, st.hmirror);
  398. s->set_lenc(s, st.lenc);
  399. s->set_quality(s, st.quality);
  400. s->set_raw_gma(s, st.raw_gma);
  401. s->set_saturation(s, st.saturation);
  402. s->set_sharpness(s, st.sharpness);
  403. s->set_special_effect(s, st.special_effect);
  404. s->set_vflip(s, st.vflip);
  405. s->set_wb_mode(s, st.wb_mode);
  406. s->set_whitebal(s, st.awb);
  407. s->set_wpc(s, st.wpc);
  408. }
  409. ret = nvs_get_u8(handle, CAMERA_PIXFORMAT_NVS_KEY, &pf);
  410. if (ret == ESP_OK) {
  411. s->set_pixformat(s, pf);
  412. }
  413. } else {
  414. return ESP_ERR_CAMERA_NOT_DETECTED;
  415. }
  416. nvs_close(handle);
  417. return ret;
  418. } else {
  419. ESP_LOGW(TAG, "Error (%d) opening nvs key \"%s\"", ret, key);
  420. return ret;
  421. }
  422. }