ll_cam.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. #if __has_include("esp_private/gdma.h")
  39. # include "esp_private/gdma.h"
  40. #endif
  41. #if CONFIG_LCD_CAM_ISR_IRAM_SAFE
  42. #define CAMERA_ISR_IRAM_FLAG ESP_INTR_FLAG_IRAM
  43. #define CAMERA_ISR_IRAM_ATTR IRAM_ATTR
  44. #else
  45. #define CAMERA_ISR_IRAM_FLAG 0
  46. #define CAMERA_ISR_IRAM_ATTR
  47. #endif
  48. #define CAMERA_DBG_PIN_ENABLE 0
  49. #if CAMERA_DBG_PIN_ENABLE
  50. #if CONFIG_IDF_TARGET_ESP32
  51. #define DBG_PIN_NUM 26
  52. #else
  53. #define DBG_PIN_NUM 7
  54. #endif
  55. #include "hal/gpio_ll.h"
  56. #define DBG_PIN_SET(v) gpio_ll_set_level(&GPIO, DBG_PIN_NUM, v)
  57. #else
  58. #define DBG_PIN_SET(v)
  59. #endif
  60. #define CAM_CHECK(a, str, ret) if (!(a)) { \
  61. ESP_LOGE(TAG,"%s(%d): %s", __FUNCTION__, __LINE__, str); \
  62. return (ret); \
  63. }
  64. #define CAM_CHECK_GOTO(a, str, lab) if (!(a)) { \
  65. ESP_LOGE(TAG,"%s(%d): %s", __FUNCTION__, __LINE__, str); \
  66. goto lab; \
  67. }
  68. #define LCD_CAM_DMA_NODE_BUFFER_MAX_SIZE (4092)
  69. typedef enum {
  70. CAM_IN_SUC_EOF_EVENT = 0,
  71. CAM_VSYNC_EVENT
  72. } cam_event_t;
  73. typedef enum {
  74. CAM_STATE_IDLE = 0,
  75. CAM_STATE_READ_BUF = 1,
  76. } cam_state_t;
  77. typedef struct {
  78. camera_fb_t fb;
  79. uint8_t en;
  80. //for RGB/YUV modes
  81. lldesc_t *dma;
  82. size_t fb_offset;
  83. } cam_frame_t;
  84. typedef struct {
  85. uint32_t dma_bytes_per_item;
  86. uint32_t dma_buffer_size;
  87. uint32_t dma_half_buffer_size;
  88. uint32_t dma_half_buffer_cnt;
  89. uint32_t dma_node_buffer_size;
  90. uint32_t dma_node_cnt;
  91. uint32_t frame_copy_cnt;
  92. //for JPEG mode
  93. lldesc_t *dma;
  94. uint8_t *dma_buffer;
  95. cam_frame_t *frames;
  96. QueueHandle_t event_queue;
  97. QueueHandle_t frame_buffer_queue;
  98. TaskHandle_t task_handle;
  99. intr_handle_t cam_intr_handle;
  100. uint8_t dma_num;//ESP32-S3
  101. intr_handle_t dma_intr_handle;//ESP32-S3
  102. #if SOC_GDMA_SUPPORTED
  103. gdma_channel_handle_t dma_channel_handle;//ESP32-S3
  104. #endif
  105. uint8_t jpeg_mode;
  106. uint8_t vsync_pin;
  107. uint8_t vsync_invert;
  108. uint32_t frame_cnt;
  109. uint32_t recv_size;
  110. bool swap_data;
  111. bool psram_mode;
  112. //for RGB/YUV modes
  113. uint16_t width;
  114. uint16_t height;
  115. #if CONFIG_CAMERA_CONVERTER_ENABLED
  116. float in_bytes_per_pixel;
  117. float fb_bytes_per_pixel;
  118. camera_conv_mode_t conv_mode;
  119. #else
  120. uint8_t in_bytes_per_pixel;
  121. uint8_t fb_bytes_per_pixel;
  122. #endif
  123. uint32_t fb_size;
  124. cam_state_t state;
  125. } cam_obj_t;
  126. bool ll_cam_stop(cam_obj_t *cam);
  127. bool ll_cam_start(cam_obj_t *cam, int frame_pos);
  128. esp_err_t ll_cam_config(cam_obj_t *cam, const camera_config_t *config);
  129. esp_err_t ll_cam_deinit(cam_obj_t *cam);
  130. void ll_cam_vsync_intr_enable(cam_obj_t *cam, bool en);
  131. esp_err_t ll_cam_set_pin(cam_obj_t *cam, const camera_config_t *config);
  132. esp_err_t ll_cam_init_isr(cam_obj_t *cam);
  133. void ll_cam_do_vsync(cam_obj_t *cam);
  134. uint8_t ll_cam_get_dma_align(cam_obj_t *cam);
  135. bool ll_cam_dma_sizes(cam_obj_t *cam);
  136. size_t ll_cam_memcpy(cam_obj_t *cam, uint8_t *out, const uint8_t *in, size_t len);
  137. 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);
  138. #if CONFIG_IDF_TARGET_ESP32S3
  139. void ll_cam_dma_print_state(cam_obj_t *cam);
  140. void ll_cam_dma_reset(cam_obj_t *cam);
  141. #endif
  142. // implemented in cam_hal
  143. void ll_cam_send_event(cam_obj_t *cam, cam_event_t cam_event, BaseType_t * HPTaskAwoken);