esp_camera.c 12 KB

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