ll_cam.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  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. #include <stdio.h>
  15. #include <string.h>
  16. #include "soc/i2s_struct.h"
  17. #include "esp_idf_version.h"
  18. #if (ESP_IDF_VERSION_MAJOR >= 4) && (ESP_IDF_VERSION_MINOR > 1)
  19. #include "hal/gpio_ll.h"
  20. #else
  21. #include "soc/gpio_periph.h"
  22. #define esp_rom_delay_us ets_delay_us
  23. static inline int gpio_ll_get_level(gpio_dev_t *hw, int gpio_num)
  24. {
  25. if (gpio_num < 32) {
  26. return (hw->in >> gpio_num) & 0x1;
  27. } else {
  28. return (hw->in1.data >> (gpio_num - 32)) & 0x1;
  29. }
  30. }
  31. #endif
  32. #include "ll_cam.h"
  33. #include "xclk.h"
  34. #include "cam_hal.h"
  35. #if (ESP_IDF_VERSION_MAJOR >= 5)
  36. #define GPIO_PIN_INTR_POSEDGE GPIO_INTR_POSEDGE
  37. #define GPIO_PIN_INTR_NEGEDGE GPIO_INTR_NEGEDGE
  38. #define gpio_matrix_in(a,b,c) gpio_iomux_in(a,b)
  39. #endif
  40. static const char *TAG = "esp32 ll_cam";
  41. #define I2S_ISR_ENABLE(i) {I2S0.int_clr.i = 1;I2S0.int_ena.i = 1;}
  42. #define I2S_ISR_DISABLE(i) {I2S0.int_ena.i = 0;I2S0.int_clr.i = 1;}
  43. typedef union {
  44. struct {
  45. uint32_t sample2:8;
  46. uint32_t unused2:8;
  47. uint32_t sample1:8;
  48. uint32_t unused1:8;
  49. };
  50. uint32_t val;
  51. } dma_elem_t;
  52. typedef enum {
  53. /* camera sends byte sequence: s1, s2, s3, s4, ...
  54. * fifo receives: 00 s1 00 s2, 00 s2 00 s3, 00 s3 00 s4, ...
  55. */
  56. SM_0A0B_0B0C = 0,
  57. /* camera sends byte sequence: s1, s2, s3, s4, ...
  58. * fifo receives: 00 s1 00 s2, 00 s3 00 s4, ...
  59. */
  60. SM_0A0B_0C0D = 1,
  61. /* camera sends byte sequence: s1, s2, s3, s4, ...
  62. * fifo receives: 00 s1 00 00, 00 s2 00 00, 00 s3 00 00, ...
  63. */
  64. SM_0A00_0B00 = 3,
  65. } i2s_sampling_mode_t;
  66. typedef size_t (*dma_filter_t)(uint8_t* dst, const uint8_t* src, size_t len);
  67. static i2s_sampling_mode_t sampling_mode = SM_0A00_0B00;
  68. static size_t ll_cam_bytes_per_sample(i2s_sampling_mode_t mode)
  69. {
  70. switch(mode) {
  71. case SM_0A00_0B00:
  72. return 4;
  73. case SM_0A0B_0B0C:
  74. return 4;
  75. case SM_0A0B_0C0D:
  76. return 2;
  77. default:
  78. assert(0 && "invalid sampling mode");
  79. return 0;
  80. }
  81. }
  82. static size_t IRAM_ATTR ll_cam_dma_filter_jpeg(uint8_t* dst, const uint8_t* src, size_t len)
  83. {
  84. const dma_elem_t* dma_el = (const dma_elem_t*)src;
  85. size_t elements = len / sizeof(dma_elem_t);
  86. size_t end = elements / 4;
  87. // manually unrolling 4 iterations of the loop here
  88. for (size_t i = 0; i < end; ++i) {
  89. dst[0] = dma_el[0].sample1;
  90. dst[1] = dma_el[1].sample1;
  91. dst[2] = dma_el[2].sample1;
  92. dst[3] = dma_el[3].sample1;
  93. dma_el += 4;
  94. dst += 4;
  95. }
  96. return elements;
  97. }
  98. static size_t IRAM_ATTR ll_cam_dma_filter_grayscale(uint8_t* dst, const uint8_t* src, size_t len)
  99. {
  100. const dma_elem_t* dma_el = (const dma_elem_t*)src;
  101. size_t elements = len / sizeof(dma_elem_t);
  102. size_t end = elements / 4;
  103. for (size_t i = 0; i < end; ++i) {
  104. // manually unrolling 4 iterations of the loop here
  105. dst[0] = dma_el[0].sample1;
  106. dst[1] = dma_el[1].sample1;
  107. dst[2] = dma_el[2].sample1;
  108. dst[3] = dma_el[3].sample1;
  109. dma_el += 4;
  110. dst += 4;
  111. }
  112. return elements;
  113. }
  114. static size_t IRAM_ATTR ll_cam_dma_filter_grayscale_highspeed(uint8_t* dst, const uint8_t* src, size_t len)
  115. {
  116. const dma_elem_t* dma_el = (const dma_elem_t*)src;
  117. size_t elements = len / sizeof(dma_elem_t);
  118. size_t end = elements / 8;
  119. for (size_t i = 0; i < end; ++i) {
  120. // manually unrolling 4 iterations of the loop here
  121. dst[0] = dma_el[0].sample1;
  122. dst[1] = dma_el[2].sample1;
  123. dst[2] = dma_el[4].sample1;
  124. dst[3] = dma_el[6].sample1;
  125. dma_el += 8;
  126. dst += 4;
  127. }
  128. // the final sample of a line in SM_0A0B_0B0C sampling mode needs special handling
  129. if ((elements & 0x7) != 0) {
  130. dst[0] = dma_el[0].sample1;
  131. dst[1] = dma_el[2].sample1;
  132. elements += 1;
  133. }
  134. return elements / 2;
  135. }
  136. static size_t IRAM_ATTR ll_cam_dma_filter_yuyv(uint8_t* dst, const uint8_t* src, size_t len)
  137. {
  138. const dma_elem_t* dma_el = (const dma_elem_t*)src;
  139. size_t elements = len / sizeof(dma_elem_t);
  140. size_t end = elements / 4;
  141. for (size_t i = 0; i < end; ++i) {
  142. dst[0] = dma_el[0].sample1;//y0
  143. dst[1] = dma_el[0].sample2;//u
  144. dst[2] = dma_el[1].sample1;//y1
  145. dst[3] = dma_el[1].sample2;//v
  146. dst[4] = dma_el[2].sample1;//y0
  147. dst[5] = dma_el[2].sample2;//u
  148. dst[6] = dma_el[3].sample1;//y1
  149. dst[7] = dma_el[3].sample2;//v
  150. dma_el += 4;
  151. dst += 8;
  152. }
  153. return elements * 2;
  154. }
  155. static size_t IRAM_ATTR ll_cam_dma_filter_yuyv_highspeed(uint8_t* dst, const uint8_t* src, size_t len)
  156. {
  157. const dma_elem_t* dma_el = (const dma_elem_t*)src;
  158. size_t elements = len / sizeof(dma_elem_t);
  159. size_t end = elements / 8;
  160. for (size_t i = 0; i < end; ++i) {
  161. dst[0] = dma_el[0].sample1;//y0
  162. dst[1] = dma_el[1].sample1;//u
  163. dst[2] = dma_el[2].sample1;//y1
  164. dst[3] = dma_el[3].sample1;//v
  165. dst[4] = dma_el[4].sample1;//y0
  166. dst[5] = dma_el[5].sample1;//u
  167. dst[6] = dma_el[6].sample1;//y1
  168. dst[7] = dma_el[7].sample1;//v
  169. dma_el += 8;
  170. dst += 8;
  171. }
  172. if ((elements & 0x7) != 0) {
  173. dst[0] = dma_el[0].sample1;//y0
  174. dst[1] = dma_el[1].sample1;//u
  175. dst[2] = dma_el[2].sample1;//y1
  176. dst[3] = dma_el[2].sample2;//v
  177. elements += 4;
  178. }
  179. return elements;
  180. }
  181. static void IRAM_ATTR ll_cam_vsync_isr(void *arg)
  182. {
  183. //DBG_PIN_SET(1);
  184. cam_obj_t *cam = (cam_obj_t *)arg;
  185. BaseType_t HPTaskAwoken = pdFALSE;
  186. // filter
  187. ets_delay_us(1);
  188. if (gpio_ll_get_level(&GPIO, cam->vsync_pin) == !cam->vsync_invert) {
  189. ll_cam_send_event(cam, CAM_VSYNC_EVENT, &HPTaskAwoken);
  190. if (HPTaskAwoken == pdTRUE) {
  191. portYIELD_FROM_ISR();
  192. }
  193. }
  194. //DBG_PIN_SET(0);
  195. }
  196. static void IRAM_ATTR ll_cam_dma_isr(void *arg)
  197. {
  198. //DBG_PIN_SET(1);
  199. cam_obj_t *cam = (cam_obj_t *)arg;
  200. BaseType_t HPTaskAwoken = pdFALSE;
  201. typeof(I2S0.int_st) status = I2S0.int_st;
  202. if (status.val == 0) {
  203. return;
  204. }
  205. I2S0.int_clr.val = status.val;
  206. if (status.in_suc_eof) {
  207. ll_cam_send_event(cam, CAM_IN_SUC_EOF_EVENT, &HPTaskAwoken);
  208. }
  209. if (HPTaskAwoken == pdTRUE) {
  210. portYIELD_FROM_ISR();
  211. }
  212. //DBG_PIN_SET(0);
  213. }
  214. bool IRAM_ATTR ll_cam_stop(cam_obj_t *cam)
  215. {
  216. I2S0.conf.rx_start = 0;
  217. I2S_ISR_DISABLE(in_suc_eof);
  218. I2S0.in_link.stop = 1;
  219. return true;
  220. }
  221. esp_err_t ll_cam_deinit(cam_obj_t *cam)
  222. {
  223. gpio_isr_handler_remove(cam->vsync_pin);
  224. if (cam->cam_intr_handle) {
  225. esp_intr_free(cam->cam_intr_handle);
  226. cam->cam_intr_handle = NULL;
  227. }
  228. return ESP_OK;
  229. }
  230. bool ll_cam_start(cam_obj_t *cam, int frame_pos)
  231. {
  232. I2S0.conf.rx_start = 0;
  233. I2S_ISR_ENABLE(in_suc_eof);
  234. I2S0.conf.rx_reset = 1;
  235. I2S0.conf.rx_reset = 0;
  236. I2S0.conf.rx_fifo_reset = 1;
  237. I2S0.conf.rx_fifo_reset = 0;
  238. I2S0.lc_conf.in_rst = 1;
  239. I2S0.lc_conf.in_rst = 0;
  240. I2S0.lc_conf.ahbm_fifo_rst = 1;
  241. I2S0.lc_conf.ahbm_fifo_rst = 0;
  242. I2S0.lc_conf.ahbm_rst = 1;
  243. I2S0.lc_conf.ahbm_rst = 0;
  244. I2S0.rx_eof_num = cam->dma_half_buffer_size / sizeof(dma_elem_t);
  245. I2S0.in_link.addr = ((uint32_t)&cam->dma[0]) & 0xfffff;
  246. I2S0.in_link.start = 1;
  247. I2S0.conf.rx_start = 1;
  248. return true;
  249. }
  250. esp_err_t ll_cam_config(cam_obj_t *cam, const camera_config_t *config)
  251. {
  252. // Enable and configure I2S peripheral
  253. periph_module_enable(PERIPH_I2S0_MODULE);
  254. I2S0.conf.rx_reset = 1;
  255. I2S0.conf.rx_reset = 0;
  256. I2S0.conf.rx_fifo_reset = 1;
  257. I2S0.conf.rx_fifo_reset = 0;
  258. I2S0.lc_conf.in_rst = 1;
  259. I2S0.lc_conf.in_rst = 0;
  260. I2S0.lc_conf.ahbm_fifo_rst = 1;
  261. I2S0.lc_conf.ahbm_fifo_rst = 0;
  262. I2S0.lc_conf.ahbm_rst = 1;
  263. I2S0.lc_conf.ahbm_rst = 0;
  264. I2S0.conf.rx_slave_mod = 1;
  265. I2S0.conf.rx_right_first = 0;
  266. I2S0.conf.rx_msb_right = 0;
  267. I2S0.conf.rx_msb_shift = 0;
  268. I2S0.conf.rx_mono = 0;
  269. I2S0.conf.rx_short_sync = 0;
  270. I2S0.conf2.lcd_en = 1;
  271. I2S0.conf2.camera_en = 1;
  272. // Configure clock divider
  273. I2S0.clkm_conf.clkm_div_a = 0;
  274. I2S0.clkm_conf.clkm_div_b = 0;
  275. I2S0.clkm_conf.clkm_div_num = 2;
  276. I2S0.fifo_conf.dscr_en = 1;
  277. I2S0.fifo_conf.rx_fifo_mod = sampling_mode;
  278. I2S0.fifo_conf.rx_fifo_mod_force_en = 1;
  279. I2S0.conf_chan.rx_chan_mod = 1;
  280. I2S0.sample_rate_conf.rx_bits_mod = 0;
  281. I2S0.timing.val = 0;
  282. I2S0.timing.rx_dsync_sw = 1;
  283. return ESP_OK;
  284. }
  285. void ll_cam_vsync_intr_enable(cam_obj_t *cam, bool en)
  286. {
  287. if (en) {
  288. gpio_intr_enable(cam->vsync_pin);
  289. } else {
  290. gpio_intr_disable(cam->vsync_pin);
  291. }
  292. }
  293. esp_err_t ll_cam_set_pin(cam_obj_t *cam, const camera_config_t *config)
  294. {
  295. gpio_config_t io_conf = {0};
  296. io_conf.intr_type = cam->vsync_invert ? GPIO_PIN_INTR_NEGEDGE : GPIO_PIN_INTR_POSEDGE;
  297. io_conf.pin_bit_mask = 1ULL << config->pin_vsync;
  298. io_conf.mode = GPIO_MODE_INPUT;
  299. io_conf.pull_up_en = 1;
  300. io_conf.pull_down_en = 0;
  301. gpio_config(&io_conf);
  302. gpio_install_isr_service(ESP_INTR_FLAG_LOWMED | ESP_INTR_FLAG_IRAM);
  303. gpio_isr_handler_add(config->pin_vsync, ll_cam_vsync_isr, cam);
  304. gpio_intr_disable(config->pin_vsync);
  305. PIN_FUNC_SELECT(GPIO_PIN_MUX_REG[config->pin_pclk], PIN_FUNC_GPIO);
  306. gpio_set_direction(config->pin_pclk, GPIO_MODE_INPUT);
  307. gpio_set_pull_mode(config->pin_pclk, GPIO_FLOATING);
  308. gpio_matrix_in(config->pin_pclk, I2S0I_WS_IN_IDX, false);
  309. PIN_FUNC_SELECT(GPIO_PIN_MUX_REG[config->pin_vsync], PIN_FUNC_GPIO);
  310. gpio_set_direction(config->pin_vsync, GPIO_MODE_INPUT);
  311. gpio_set_pull_mode(config->pin_vsync, GPIO_FLOATING);
  312. gpio_matrix_in(config->pin_vsync, I2S0I_V_SYNC_IDX, false);
  313. PIN_FUNC_SELECT(GPIO_PIN_MUX_REG[config->pin_href], PIN_FUNC_GPIO);
  314. gpio_set_direction(config->pin_href, GPIO_MODE_INPUT);
  315. gpio_set_pull_mode(config->pin_href, GPIO_FLOATING);
  316. gpio_matrix_in(config->pin_href, I2S0I_H_SYNC_IDX, false);
  317. int data_pins[8] = {
  318. config->pin_d0, config->pin_d1, config->pin_d2, config->pin_d3, config->pin_d4, config->pin_d5, config->pin_d6, config->pin_d7,
  319. };
  320. for (int i = 0; i < 8; i++) {
  321. PIN_FUNC_SELECT(GPIO_PIN_MUX_REG[data_pins[i]], PIN_FUNC_GPIO);
  322. gpio_set_direction(data_pins[i], GPIO_MODE_INPUT);
  323. gpio_set_pull_mode(data_pins[i], GPIO_FLOATING);
  324. gpio_matrix_in(data_pins[i], I2S0I_DATA_IN0_IDX + i, false);
  325. }
  326. gpio_matrix_in(0x38, I2S0I_H_ENABLE_IDX, false);
  327. return ESP_OK;
  328. }
  329. esp_err_t ll_cam_init_isr(cam_obj_t *cam)
  330. {
  331. return esp_intr_alloc(ETS_I2S0_INTR_SOURCE, ESP_INTR_FLAG_LOWMED | ESP_INTR_FLAG_IRAM, ll_cam_dma_isr, cam, &cam->cam_intr_handle);
  332. }
  333. void ll_cam_do_vsync(cam_obj_t *cam)
  334. {
  335. }
  336. uint8_t ll_cam_get_dma_align(cam_obj_t *cam)
  337. {
  338. return 0;
  339. }
  340. static bool ll_cam_calc_rgb_dma(cam_obj_t *cam){
  341. size_t dma_half_buffer_max = CONFIG_CAMERA_DMA_BUFFER_SIZE_MAX / 2 / cam->dma_bytes_per_item;
  342. size_t dma_buffer_max = 2 * dma_half_buffer_max;
  343. size_t node_max = LCD_CAM_DMA_NODE_BUFFER_MAX_SIZE / cam->dma_bytes_per_item;
  344. size_t line_width = cam->width * cam->in_bytes_per_pixel;
  345. size_t image_size = cam->height * line_width;
  346. if (image_size > (4 * 1024 * 1024) || (line_width > dma_half_buffer_max)) {
  347. ESP_LOGE(TAG, "Resolution too high");
  348. return 0;
  349. }
  350. size_t node_size = node_max;
  351. size_t nodes_per_line = 1;
  352. size_t lines_per_node = 1;
  353. size_t lines_per_half_buffer = 1;
  354. size_t dma_half_buffer_min = node_max;
  355. size_t dma_half_buffer = dma_half_buffer_max;
  356. size_t dma_buffer_size = dma_buffer_max;
  357. // Calculate DMA Node Size so that it's divisable by or divisor of the line width
  358. if(line_width >= node_max){
  359. // One or more nodes will be requied for one line
  360. for(size_t i = node_max; i > 0; i=i-1){
  361. if ((line_width % i) == 0) {
  362. node_size = i;
  363. nodes_per_line = line_width / node_size;
  364. break;
  365. }
  366. }
  367. } else {
  368. // One or more lines can fit into one node
  369. for(size_t i = node_max; i > 0; i=i-1){
  370. if ((i % line_width) == 0) {
  371. node_size = i;
  372. lines_per_node = node_size / line_width;
  373. while((cam->height % lines_per_node) != 0){
  374. lines_per_node = lines_per_node - 1;
  375. node_size = lines_per_node * line_width;
  376. }
  377. break;
  378. }
  379. }
  380. }
  381. // Calculate minimum EOF size = max(mode_size, line_size)
  382. dma_half_buffer_min = node_size * nodes_per_line;
  383. // Calculate max EOF size divisable by node size
  384. dma_half_buffer = (dma_half_buffer_max / dma_half_buffer_min) * dma_half_buffer_min;
  385. // Adjust EOF size so that height will be divisable by the number of lines in each EOF
  386. lines_per_half_buffer = dma_half_buffer / line_width;
  387. while((cam->height % lines_per_half_buffer) != 0){
  388. dma_half_buffer = dma_half_buffer - dma_half_buffer_min;
  389. lines_per_half_buffer = dma_half_buffer / line_width;
  390. }
  391. // Calculate DMA size
  392. dma_buffer_size =(dma_buffer_max / dma_half_buffer) * dma_half_buffer;
  393. ESP_LOGI(TAG, "node_size: %4u, nodes_per_line: %u, lines_per_node: %u, dma_half_buffer_min: %5u, dma_half_buffer: %5u, lines_per_half_buffer: %2u, dma_buffer_size: %5u, image_size: %u",
  394. node_size * cam->dma_bytes_per_item, nodes_per_line, lines_per_node, dma_half_buffer_min * cam->dma_bytes_per_item, dma_half_buffer * cam->dma_bytes_per_item, lines_per_half_buffer, dma_buffer_size * cam->dma_bytes_per_item, image_size);
  395. cam->dma_buffer_size = dma_buffer_size * cam->dma_bytes_per_item;
  396. cam->dma_half_buffer_size = dma_half_buffer * cam->dma_bytes_per_item;
  397. cam->dma_node_buffer_size = node_size * cam->dma_bytes_per_item;
  398. cam->dma_half_buffer_cnt = cam->dma_buffer_size / cam->dma_half_buffer_size;
  399. return 1;
  400. }
  401. bool ll_cam_dma_sizes(cam_obj_t *cam)
  402. {
  403. cam->dma_bytes_per_item = ll_cam_bytes_per_sample(sampling_mode);
  404. if (cam->jpeg_mode) {
  405. cam->dma_half_buffer_cnt = 8;
  406. cam->dma_node_buffer_size = 2048;
  407. cam->dma_half_buffer_size = cam->dma_node_buffer_size * 2;
  408. cam->dma_buffer_size = cam->dma_half_buffer_cnt * cam->dma_half_buffer_size;
  409. } else {
  410. return ll_cam_calc_rgb_dma(cam);
  411. }
  412. return 1;
  413. }
  414. static dma_filter_t dma_filter = ll_cam_dma_filter_jpeg;
  415. size_t IRAM_ATTR ll_cam_memcpy(cam_obj_t *cam, uint8_t *out, const uint8_t *in, size_t len)
  416. {
  417. //DBG_PIN_SET(1);
  418. size_t r = dma_filter(out, in, len);
  419. //DBG_PIN_SET(0);
  420. return r;
  421. }
  422. 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)
  423. {
  424. if (pix_format == PIXFORMAT_GRAYSCALE) {
  425. if (sensor_pid == OV3660_PID || sensor_pid == OV5640_PID || sensor_pid == NT99141_PID) {
  426. if (xclk_freq_hz > 10000000) {
  427. sampling_mode = SM_0A00_0B00;
  428. dma_filter = ll_cam_dma_filter_yuyv_highspeed;
  429. } else {
  430. sampling_mode = SM_0A0B_0C0D;
  431. dma_filter = ll_cam_dma_filter_yuyv;
  432. }
  433. cam->in_bytes_per_pixel = 1; // camera sends Y8
  434. } else {
  435. if (xclk_freq_hz > 10000000 && sensor_pid != OV7725_PID) {
  436. sampling_mode = SM_0A00_0B00;
  437. dma_filter = ll_cam_dma_filter_grayscale_highspeed;
  438. } else {
  439. sampling_mode = SM_0A0B_0C0D;
  440. dma_filter = ll_cam_dma_filter_grayscale;
  441. }
  442. cam->in_bytes_per_pixel = 2; // camera sends YU/YV
  443. }
  444. cam->fb_bytes_per_pixel = 1; // frame buffer stores Y8
  445. } else if (pix_format == PIXFORMAT_YUV422 || pix_format == PIXFORMAT_RGB565) {
  446. if (xclk_freq_hz > 10000000 && sensor_pid != OV7725_PID) {
  447. if (sensor_pid == OV7670_PID) {
  448. sampling_mode = SM_0A0B_0B0C;
  449. } else {
  450. sampling_mode = SM_0A00_0B00;
  451. }
  452. dma_filter = ll_cam_dma_filter_yuyv_highspeed;
  453. } else {
  454. sampling_mode = SM_0A0B_0C0D;
  455. dma_filter = ll_cam_dma_filter_yuyv;
  456. }
  457. cam->in_bytes_per_pixel = 2; // camera sends YU/YV
  458. cam->fb_bytes_per_pixel = 2; // frame buffer stores YU/YV/RGB565
  459. } else if (pix_format == PIXFORMAT_JPEG) {
  460. cam->in_bytes_per_pixel = 1;
  461. cam->fb_bytes_per_pixel = 1;
  462. dma_filter = ll_cam_dma_filter_jpeg;
  463. sampling_mode = SM_0A00_0B00;
  464. } else {
  465. ESP_LOGE(TAG, "Requested format is not supported");
  466. return ESP_ERR_NOT_SUPPORTED;
  467. }
  468. I2S0.fifo_conf.rx_fifo_mod = sampling_mode;
  469. return ESP_OK;
  470. }