ll_cam.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. // Copyright 2010-2020 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. #pragma once
  15. #include <stdint.h>
  16. #include "sdkconfig.h"
  17. #include "esp_idf_version.h"
  18. #if CONFIG_IDF_TARGET_ESP32
  19. #if ESP_IDF_VERSION_MAJOR >= 4
  20. #include "esp32/rom/lldesc.h"
  21. #else
  22. #include "rom/lldesc.h"
  23. #endif
  24. #elif CONFIG_IDF_TARGET_ESP32S2
  25. #include "esp32s2/rom/lldesc.h"
  26. #elif CONFIG_IDF_TARGET_ESP32S3
  27. #include "esp32s3/rom/lldesc.h"
  28. #endif
  29. #include "esp_log.h"
  30. #include "esp_camera.h"
  31. #include "freertos/FreeRTOS.h"
  32. #include "freertos/queue.h"
  33. #include "freertos/task.h"
  34. #include "freertos/semphr.h"
  35. #if __has_include("esp_private/periph_ctrl.h")
  36. # include "esp_private/periph_ctrl.h"
  37. #endif
  38. #define CAMERA_DBG_PIN_ENABLE 0
  39. #if CAMERA_DBG_PIN_ENABLE
  40. #if CONFIG_IDF_TARGET_ESP32
  41. #define DBG_PIN_NUM 26
  42. #else
  43. #define DBG_PIN_NUM 7
  44. #endif
  45. #include "hal/gpio_ll.h"
  46. #define DBG_PIN_SET(v) gpio_ll_set_level(&GPIO, DBG_PIN_NUM, v)
  47. #else
  48. #define DBG_PIN_SET(v)
  49. #endif
  50. #define CAM_CHECK(a, str, ret) if (!(a)) { \
  51. ESP_LOGE(TAG,"%s(%d): %s", __FUNCTION__, __LINE__, str); \
  52. return (ret); \
  53. }
  54. #define CAM_CHECK_GOTO(a, str, lab) if (!(a)) { \
  55. ESP_LOGE(TAG,"%s(%d): %s", __FUNCTION__, __LINE__, str); \
  56. goto lab; \
  57. }
  58. #define LCD_CAM_DMA_NODE_BUFFER_MAX_SIZE (4092)
  59. typedef enum {
  60. CAM_IN_SUC_EOF_EVENT = 0,
  61. CAM_VSYNC_EVENT
  62. } cam_event_t;
  63. typedef enum {
  64. CAM_STATE_IDLE = 0,
  65. CAM_STATE_READ_BUF = 1,
  66. } cam_state_t;
  67. typedef struct {
  68. camera_fb_t fb;
  69. uint8_t en;
  70. //for RGB/YUV modes
  71. lldesc_t *dma;
  72. size_t fb_offset;
  73. } cam_frame_t;
  74. typedef struct {
  75. uint32_t dma_bytes_per_item;
  76. uint32_t dma_buffer_size;
  77. uint32_t dma_half_buffer_size;
  78. uint32_t dma_half_buffer_cnt;
  79. uint32_t dma_node_buffer_size;
  80. uint32_t dma_node_cnt;
  81. uint32_t frame_copy_cnt;
  82. //for JPEG mode
  83. lldesc_t *dma;
  84. uint8_t *dma_buffer;
  85. cam_frame_t *frames;
  86. QueueHandle_t event_queue;
  87. QueueHandle_t frame_buffer_queue;
  88. TaskHandle_t task_handle;
  89. intr_handle_t cam_intr_handle;
  90. uint8_t dma_num;//ESP32-S3
  91. intr_handle_t dma_intr_handle;//ESP32-S3
  92. uint8_t jpeg_mode;
  93. uint8_t vsync_pin;
  94. uint8_t vsync_invert;
  95. uint32_t frame_cnt;
  96. uint32_t recv_size;
  97. bool swap_data;
  98. bool psram_mode;
  99. //for RGB/YUV modes
  100. uint16_t width;
  101. uint16_t height;
  102. uint8_t in_bytes_per_pixel;
  103. uint8_t fb_bytes_per_pixel;
  104. uint32_t fb_size;
  105. cam_state_t state;
  106. } cam_obj_t;
  107. bool ll_cam_stop(cam_obj_t *cam);
  108. bool ll_cam_start(cam_obj_t *cam, int frame_pos);
  109. esp_err_t ll_cam_config(cam_obj_t *cam, const camera_config_t *config);
  110. esp_err_t ll_cam_deinit(cam_obj_t *cam);
  111. void ll_cam_vsync_intr_enable(cam_obj_t *cam, bool en);
  112. esp_err_t ll_cam_set_pin(cam_obj_t *cam, const camera_config_t *config);
  113. esp_err_t ll_cam_init_isr(cam_obj_t *cam);
  114. void ll_cam_do_vsync(cam_obj_t *cam);
  115. uint8_t ll_cam_get_dma_align(cam_obj_t *cam);
  116. bool ll_cam_dma_sizes(cam_obj_t *cam);
  117. size_t IRAM_ATTR ll_cam_memcpy(cam_obj_t *cam, uint8_t *out, const uint8_t *in, size_t len);
  118. esp_err_t ll_cam_set_sample_mode(cam_obj_t *cam, pixformat_t pix_format, uint32_t xclk_freq_hz, uint16_t sensor_pid);
  119. // implemented in cam_hal
  120. void ll_cam_send_event(cam_obj_t *cam, cam_event_t cam_event, BaseType_t * HPTaskAwoken);