esp_camera.c 12 KB

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