esp_camera.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  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. *out_camera_model = CAMERA_NONE;
  138. if (s_state != NULL) {
  139. return ESP_ERR_INVALID_STATE;
  140. }
  141. s_state = (camera_state_t *) calloc(sizeof(camera_state_t), 1);
  142. if (!s_state) {
  143. return ESP_ERR_NO_MEM;
  144. }
  145. if (config->pin_xclk >= 0) {
  146. ESP_LOGD(TAG, "Enabling XCLK output");
  147. CAMERA_ENABLE_OUT_CLOCK(config);
  148. }
  149. if (config->pin_sscb_sda != -1) {
  150. ESP_LOGD(TAG, "Initializing SSCB");
  151. SCCB_Init(config->pin_sscb_sda, config->pin_sscb_scl);
  152. }
  153. if (config->pin_pwdn >= 0) {
  154. ESP_LOGD(TAG, "Resetting camera by power down line");
  155. gpio_config_t conf = { 0 };
  156. conf.pin_bit_mask = 1LL << config->pin_pwdn;
  157. conf.mode = GPIO_MODE_OUTPUT;
  158. gpio_config(&conf);
  159. // carefull, logic is inverted compared to reset pin
  160. gpio_set_level(config->pin_pwdn, 1);
  161. vTaskDelay(10 / portTICK_PERIOD_MS);
  162. gpio_set_level(config->pin_pwdn, 0);
  163. vTaskDelay(10 / portTICK_PERIOD_MS);
  164. }
  165. if (config->pin_reset >= 0) {
  166. ESP_LOGD(TAG, "Resetting camera");
  167. gpio_config_t conf = { 0 };
  168. conf.pin_bit_mask = 1LL << config->pin_reset;
  169. conf.mode = GPIO_MODE_OUTPUT;
  170. gpio_config(&conf);
  171. gpio_set_level(config->pin_reset, 0);
  172. vTaskDelay(10 / portTICK_PERIOD_MS);
  173. gpio_set_level(config->pin_reset, 1);
  174. vTaskDelay(10 / portTICK_PERIOD_MS);
  175. }
  176. ESP_LOGD(TAG, "Searching for camera address");
  177. vTaskDelay(10 / portTICK_PERIOD_MS);
  178. uint8_t slv_addr = SCCB_Probe();
  179. if (slv_addr == 0) {
  180. CAMERA_DISABLE_OUT_CLOCK();
  181. return ESP_ERR_NOT_FOUND;
  182. }
  183. ESP_LOGI(TAG, "Detected camera at address=0x%02x", slv_addr);
  184. s_state->sensor.slv_addr = slv_addr;
  185. s_state->sensor.xclk_freq_hz = config->xclk_freq_hz;
  186. /**
  187. * Read sensor ID and then initialize sensor
  188. * Attention: Some sensors have the same SCCB address. Therefore, several attempts may be made in the detection process
  189. */
  190. sensor_id_t *id = &s_state->sensor.id;
  191. for (size_t i = 0; i < sizeof(g_sensors) / sizeof(sensor_func_t); i++) {
  192. if (g_sensors[i].detect(slv_addr, id)) {
  193. camera_sensor_info_t *info = esp_camera_sensor_get_info(id);
  194. if (NULL != info) {
  195. *out_camera_model = info->model;
  196. ESP_LOGI(TAG, "Detected %s camera", info->name);
  197. g_sensors[i].init(&s_state->sensor);
  198. break;
  199. }
  200. }
  201. }
  202. if (CAMERA_NONE == *out_camera_model) { //If no supported sensors are detected
  203. CAMERA_DISABLE_OUT_CLOCK();
  204. ESP_LOGE(TAG, "Detected camera not supported.");
  205. return ESP_ERR_NOT_SUPPORTED;
  206. }
  207. ESP_LOGI(TAG, "Camera PID=0x%02x VER=0x%02x MIDL=0x%02x MIDH=0x%02x",
  208. id->PID, id->VER, id->MIDH, id->MIDL);
  209. ESP_LOGD(TAG, "Doing SW reset of sensor");
  210. vTaskDelay(10 / portTICK_PERIOD_MS);
  211. s_state->sensor.reset(&s_state->sensor);
  212. return ESP_OK;
  213. }
  214. #if CONFIG_CAMERA_CONVERTER_ENABLED
  215. static pixformat_t get_output_data_format(camera_conv_mode_t conv_mode)
  216. {
  217. pixformat_t format = PIXFORMAT_RGB565;
  218. switch (conv_mode) {
  219. case YUV422_TO_YUV420:
  220. format = PIXFORMAT_YUV420;
  221. break;
  222. case YUV422_TO_RGB565: // default format is RGB565
  223. default:
  224. break;
  225. }
  226. ESP_LOGD(TAG, "Convert to %d format enabled", format);
  227. return format;
  228. }
  229. #endif
  230. esp_err_t esp_camera_init(const camera_config_t *config)
  231. {
  232. esp_err_t err;
  233. err = cam_init(config);
  234. if (err != ESP_OK) {
  235. ESP_LOGE(TAG, "Camera init failed with error 0x%x", err);
  236. return err;
  237. }
  238. camera_model_t camera_model = CAMERA_NONE;
  239. err = camera_probe(config, &camera_model);
  240. if (err != ESP_OK) {
  241. ESP_LOGE(TAG, "Camera probe failed with error 0x%x(%s)", err, esp_err_to_name(err));
  242. goto fail;
  243. }
  244. framesize_t frame_size = (framesize_t) config->frame_size;
  245. pixformat_t pix_format = (pixformat_t) config->pixel_format;
  246. if (PIXFORMAT_JPEG == pix_format && (!camera_sensor[camera_model].support_jpeg)) {
  247. ESP_LOGE(TAG, "JPEG format is not supported on this sensor");
  248. err = ESP_ERR_NOT_SUPPORTED;
  249. goto fail;
  250. }
  251. if (frame_size > camera_sensor[camera_model].max_size) {
  252. ESP_LOGW(TAG, "The frame size exceeds the maximum for this sensor, it will be forced to the maximum possible value");
  253. frame_size = camera_sensor[camera_model].max_size;
  254. }
  255. err = cam_config(config, frame_size, s_state->sensor.id.PID);
  256. if (err != ESP_OK) {
  257. ESP_LOGE(TAG, "Camera config failed with error 0x%x", err);
  258. goto fail;
  259. }
  260. s_state->sensor.status.framesize = frame_size;
  261. s_state->sensor.pixformat = pix_format;
  262. ESP_LOGD(TAG, "Setting frame size to %dx%d", resolution[frame_size].width, resolution[frame_size].height);
  263. if (s_state->sensor.set_framesize(&s_state->sensor, frame_size) != 0) {
  264. ESP_LOGE(TAG, "Failed to set frame size");
  265. err = ESP_ERR_CAMERA_FAILED_TO_SET_FRAME_SIZE;
  266. goto fail;
  267. }
  268. s_state->sensor.set_pixformat(&s_state->sensor, pix_format);
  269. #if CONFIG_CAMERA_CONVERTER_ENABLED
  270. if(config->conv_mode) {
  271. s_state->sensor.pixformat = get_output_data_format(config->conv_mode); // If conversion enabled, change the out data format by conversion mode
  272. }
  273. #endif
  274. if (s_state->sensor.id.PID == OV2640_PID) {
  275. s_state->sensor.set_gainceiling(&s_state->sensor, GAINCEILING_2X);
  276. s_state->sensor.set_bpc(&s_state->sensor, false);
  277. s_state->sensor.set_wpc(&s_state->sensor, true);
  278. s_state->sensor.set_lenc(&s_state->sensor, true);
  279. }
  280. if (pix_format == PIXFORMAT_JPEG) {
  281. s_state->sensor.set_quality(&s_state->sensor, config->jpeg_quality);
  282. }
  283. s_state->sensor.init_status(&s_state->sensor);
  284. cam_start();
  285. return ESP_OK;
  286. fail:
  287. esp_camera_deinit();
  288. return err;
  289. }
  290. esp_err_t esp_camera_deinit()
  291. {
  292. esp_err_t ret = cam_deinit();
  293. CAMERA_DISABLE_OUT_CLOCK();
  294. if (s_state) {
  295. SCCB_Deinit();
  296. free(s_state);
  297. s_state = NULL;
  298. }
  299. return ret;
  300. }
  301. #define FB_GET_TIMEOUT (4000 / portTICK_PERIOD_MS)
  302. camera_fb_t *esp_camera_fb_get()
  303. {
  304. if (s_state == NULL) {
  305. return NULL;
  306. }
  307. camera_fb_t *fb = cam_take(FB_GET_TIMEOUT);
  308. //set the frame properties
  309. if (fb) {
  310. fb->width = resolution[s_state->sensor.status.framesize].width;
  311. fb->height = resolution[s_state->sensor.status.framesize].height;
  312. fb->format = s_state->sensor.pixformat;
  313. }
  314. return fb;
  315. }
  316. void esp_camera_fb_return(camera_fb_t *fb)
  317. {
  318. if (s_state == NULL) {
  319. return;
  320. }
  321. cam_give(fb);
  322. }
  323. sensor_t *esp_camera_sensor_get()
  324. {
  325. if (s_state == NULL) {
  326. return NULL;
  327. }
  328. return &s_state->sensor;
  329. }
  330. esp_err_t esp_camera_save_to_nvs(const char *key)
  331. {
  332. #if ESP_IDF_VERSION_MAJOR > 3
  333. nvs_handle_t handle;
  334. #else
  335. nvs_handle handle;
  336. #endif
  337. esp_err_t ret = nvs_open(key, NVS_READWRITE, &handle);
  338. if (ret == ESP_OK) {
  339. sensor_t *s = esp_camera_sensor_get();
  340. if (s != NULL) {
  341. ret = nvs_set_blob(handle, CAMERA_SENSOR_NVS_KEY, &s->status, sizeof(camera_status_t));
  342. if (ret == ESP_OK) {
  343. uint8_t pf = s->pixformat;
  344. ret = nvs_set_u8(handle, CAMERA_PIXFORMAT_NVS_KEY, pf);
  345. }
  346. return ret;
  347. } else {
  348. return ESP_ERR_CAMERA_NOT_DETECTED;
  349. }
  350. nvs_close(handle);
  351. return ret;
  352. } else {
  353. return ret;
  354. }
  355. }
  356. esp_err_t esp_camera_load_from_nvs(const char *key)
  357. {
  358. #if ESP_IDF_VERSION_MAJOR > 3
  359. nvs_handle_t handle;
  360. #else
  361. nvs_handle handle;
  362. #endif
  363. uint8_t pf;
  364. esp_err_t ret = nvs_open(key, NVS_READWRITE, &handle);
  365. if (ret == ESP_OK) {
  366. sensor_t *s = esp_camera_sensor_get();
  367. camera_status_t st;
  368. if (s != NULL) {
  369. size_t size = sizeof(camera_status_t);
  370. ret = nvs_get_blob(handle, CAMERA_SENSOR_NVS_KEY, &st, &size);
  371. if (ret == ESP_OK) {
  372. s->set_ae_level(s, st.ae_level);
  373. s->set_aec2(s, st.aec2);
  374. s->set_aec_value(s, st.aec_value);
  375. s->set_agc_gain(s, st.agc_gain);
  376. s->set_awb_gain(s, st.awb_gain);
  377. s->set_bpc(s, st.bpc);
  378. s->set_brightness(s, st.brightness);
  379. s->set_colorbar(s, st.colorbar);
  380. s->set_contrast(s, st.contrast);
  381. s->set_dcw(s, st.dcw);
  382. s->set_denoise(s, st.denoise);
  383. s->set_exposure_ctrl(s, st.aec);
  384. s->set_framesize(s, st.framesize);
  385. s->set_gain_ctrl(s, st.agc);
  386. s->set_gainceiling(s, st.gainceiling);
  387. s->set_hmirror(s, st.hmirror);
  388. s->set_lenc(s, st.lenc);
  389. s->set_quality(s, st.quality);
  390. s->set_raw_gma(s, st.raw_gma);
  391. s->set_saturation(s, st.saturation);
  392. s->set_sharpness(s, st.sharpness);
  393. s->set_special_effect(s, st.special_effect);
  394. s->set_vflip(s, st.vflip);
  395. s->set_wb_mode(s, st.wb_mode);
  396. s->set_whitebal(s, st.awb);
  397. s->set_wpc(s, st.wpc);
  398. }
  399. ret = nvs_get_u8(handle, CAMERA_PIXFORMAT_NVS_KEY, &pf);
  400. if (ret == ESP_OK) {
  401. s->set_pixformat(s, pf);
  402. }
  403. } else {
  404. return ESP_ERR_CAMERA_NOT_DETECTED;
  405. }
  406. nvs_close(handle);
  407. return ret;
  408. } else {
  409. ESP_LOGW(TAG, "Error (%d) opening nvs key \"%s\"", ret, key);
  410. return ret;
  411. }
  412. }