ll_cam.c 19 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/system_reg.h"
  17. #include "soc/lcd_cam_struct.h"
  18. #include "soc/lcd_cam_reg.h"
  19. #include "soc/gdma_struct.h"
  20. #include "soc/gdma_periph.h"
  21. #include "soc/gdma_reg.h"
  22. #include "ll_cam.h"
  23. #include "cam_hal.h"
  24. #include "esp_rom_gpio.h"
  25. #if (ESP_IDF_VERSION_MAJOR >= 5)
  26. #include "soc/gpio_sig_map.h"
  27. #include "soc/gpio_periph.h"
  28. #include "soc/io_mux_reg.h"
  29. #define gpio_matrix_in(a,b,c) esp_rom_gpio_connect_in_signal(a,b,c)
  30. #define gpio_matrix_out(a,b,c,d) esp_rom_gpio_connect_out_signal(a,b,c,d)
  31. #define ets_delay_us(a) esp_rom_delay_us(a)
  32. #endif
  33. static const char *TAG = "s3 ll_cam";
  34. static void IRAM_ATTR ll_cam_vsync_isr(void *arg)
  35. {
  36. //DBG_PIN_SET(1);
  37. cam_obj_t *cam = (cam_obj_t *)arg;
  38. BaseType_t HPTaskAwoken = pdFALSE;
  39. typeof(LCD_CAM.lc_dma_int_st) status = LCD_CAM.lc_dma_int_st;
  40. if (status.val == 0) {
  41. return;
  42. }
  43. LCD_CAM.lc_dma_int_clr.val = status.val;
  44. if (status.cam_vsync_int_st) {
  45. ll_cam_send_event(cam, CAM_VSYNC_EVENT, &HPTaskAwoken);
  46. }
  47. if (HPTaskAwoken == pdTRUE) {
  48. portYIELD_FROM_ISR();
  49. }
  50. //DBG_PIN_SET(0);
  51. }
  52. static void IRAM_ATTR ll_cam_dma_isr(void *arg)
  53. {
  54. cam_obj_t *cam = (cam_obj_t *)arg;
  55. BaseType_t HPTaskAwoken = pdFALSE;
  56. typeof(GDMA.channel[cam->dma_num].in.int_st) status = GDMA.channel[cam->dma_num].in.int_st;
  57. if (status.val == 0) {
  58. return;
  59. }
  60. GDMA.channel[cam->dma_num].in.int_clr.val = status.val;
  61. if (status.in_suc_eof) {
  62. ll_cam_send_event(cam, CAM_IN_SUC_EOF_EVENT, &HPTaskAwoken);
  63. }
  64. if (HPTaskAwoken == pdTRUE) {
  65. portYIELD_FROM_ISR();
  66. }
  67. }
  68. bool IRAM_ATTR ll_cam_stop(cam_obj_t *cam)
  69. {
  70. if (cam->jpeg_mode || !cam->psram_mode) {
  71. GDMA.channel[cam->dma_num].in.int_ena.in_suc_eof = 0;
  72. GDMA.channel[cam->dma_num].in.int_clr.in_suc_eof = 1;
  73. }
  74. GDMA.channel[cam->dma_num].in.link.stop = 1;
  75. return true;
  76. }
  77. esp_err_t ll_cam_deinit(cam_obj_t *cam)
  78. {
  79. if (cam->cam_intr_handle) {
  80. esp_intr_free(cam->cam_intr_handle);
  81. cam->cam_intr_handle = NULL;
  82. }
  83. if (cam->dma_intr_handle) {
  84. esp_intr_free(cam->dma_intr_handle);
  85. cam->dma_intr_handle = NULL;
  86. }
  87. GDMA.channel[cam->dma_num].in.link.addr = 0x0;
  88. LCD_CAM.cam_ctrl1.cam_start = 0;
  89. LCD_CAM.cam_ctrl1.cam_reset = 1;
  90. LCD_CAM.cam_ctrl1.cam_reset = 0;
  91. return ESP_OK;
  92. }
  93. bool ll_cam_start(cam_obj_t *cam, int frame_pos)
  94. {
  95. LCD_CAM.cam_ctrl1.cam_start = 0;
  96. if (cam->jpeg_mode || !cam->psram_mode) {
  97. GDMA.channel[cam->dma_num].in.int_clr.in_suc_eof = 1;
  98. GDMA.channel[cam->dma_num].in.int_ena.in_suc_eof = 1;
  99. }
  100. LCD_CAM.cam_ctrl1.cam_reset = 1;
  101. LCD_CAM.cam_ctrl1.cam_reset = 0;
  102. LCD_CAM.cam_ctrl1.cam_afifo_reset = 1;
  103. LCD_CAM.cam_ctrl1.cam_afifo_reset = 0;
  104. GDMA.channel[cam->dma_num].in.conf0.in_rst = 1;
  105. GDMA.channel[cam->dma_num].in.conf0.in_rst = 0;
  106. LCD_CAM.cam_ctrl1.cam_rec_data_bytelen = cam->dma_half_buffer_size - 1; // Ping pong operation
  107. if (!cam->psram_mode) {
  108. GDMA.channel[cam->dma_num].in.link.addr = ((uint32_t)&cam->dma[0]) & 0xfffff;
  109. } else {
  110. GDMA.channel[cam->dma_num].in.link.addr = ((uint32_t)&cam->frames[frame_pos].dma[0]) & 0xfffff;
  111. }
  112. GDMA.channel[cam->dma_num].in.link.start = 1;
  113. LCD_CAM.cam_ctrl.cam_update = 1;
  114. LCD_CAM.cam_ctrl1.cam_start = 1;
  115. return true;
  116. }
  117. static esp_err_t ll_cam_dma_init(cam_obj_t *cam)
  118. {
  119. for (int x = (SOC_GDMA_PAIRS_PER_GROUP - 1); x >= 0; x--) {
  120. if (GDMA.channel[x].in.link.addr == 0x0) {
  121. cam->dma_num = x;
  122. ESP_LOGI(TAG, "DMA Channel=%d", cam->dma_num);
  123. break;
  124. }
  125. if (x == 0) {
  126. cam_deinit();
  127. ESP_LOGE(TAG, "Can't found available GDMA channel");
  128. return ESP_FAIL;
  129. }
  130. }
  131. if (REG_GET_BIT(SYSTEM_PERIP_CLK_EN1_REG, SYSTEM_DMA_CLK_EN) == 0) {
  132. REG_CLR_BIT(SYSTEM_PERIP_CLK_EN1_REG, SYSTEM_DMA_CLK_EN);
  133. REG_SET_BIT(SYSTEM_PERIP_CLK_EN1_REG, SYSTEM_DMA_CLK_EN);
  134. REG_SET_BIT(SYSTEM_PERIP_RST_EN1_REG, SYSTEM_DMA_RST);
  135. REG_CLR_BIT(SYSTEM_PERIP_RST_EN1_REG, SYSTEM_DMA_RST);
  136. }
  137. GDMA.channel[cam->dma_num].in.int_clr.val = ~0;
  138. GDMA.channel[cam->dma_num].in.int_ena.val = 0;
  139. GDMA.channel[cam->dma_num].in.conf0.val = 0;
  140. GDMA.channel[cam->dma_num].in.conf0.in_rst = 1;
  141. GDMA.channel[cam->dma_num].in.conf0.in_rst = 0;
  142. //internal SRAM only
  143. if (!cam->psram_mode) {
  144. GDMA.channel[cam->dma_num].in.conf0.indscr_burst_en = 1;
  145. GDMA.channel[cam->dma_num].in.conf0.in_data_burst_en = 1;
  146. }
  147. GDMA.channel[cam->dma_num].in.conf1.in_check_owner = 0;
  148. // GDMA.channel[cam->dma_num].in.conf1.in_ext_mem_bk_size = 2;
  149. GDMA.channel[cam->dma_num].in.peri_sel.sel = 5;
  150. //GDMA.channel[cam->dma_num].in.pri.rx_pri = 1;//rx prio 0-15
  151. //GDMA.channel[cam->dma_num].in.sram_size.in_size = 6;//This register is used to configure the size of L2 Tx FIFO for Rx channel. 0:16 bytes, 1:24 bytes, 2:32 bytes, 3: 40 bytes, 4: 48 bytes, 5:56 bytes, 6: 64 bytes, 7: 72 bytes, 8: 80 bytes.
  152. //GDMA.channel[cam->dma_num].in.wight.rx_weight = 7;//The weight of Rx channel 0-15
  153. return ESP_OK;
  154. }
  155. #if CONFIG_CAMERA_CONVERTER_ENABLED
  156. static esp_err_t ll_cam_converter_config(cam_obj_t *cam, const camera_config_t *config)
  157. {
  158. esp_err_t ret = ESP_OK;
  159. switch (config->conv_mode) {
  160. case YUV422_TO_YUV420:
  161. if (config->pixel_format != PIXFORMAT_YUV422) {
  162. ret = ESP_FAIL;
  163. } else {
  164. ESP_LOGI(TAG, "YUV422 to YUV420 mode");
  165. LCD_CAM.cam_rgb_yuv.cam_conv_yuv2yuv_mode = 1;
  166. LCD_CAM.cam_rgb_yuv.cam_conv_yuv_mode = 0;
  167. LCD_CAM.cam_rgb_yuv.cam_conv_trans_mode = 1;
  168. }
  169. break;
  170. case YUV422_TO_RGB565:
  171. if (config->pixel_format != PIXFORMAT_YUV422) {
  172. ret = ESP_FAIL;
  173. } else {
  174. ESP_LOGI(TAG, "YUV422 to RGB565 mode");
  175. LCD_CAM.cam_rgb_yuv.cam_conv_yuv2yuv_mode = 3;
  176. LCD_CAM.cam_rgb_yuv.cam_conv_yuv_mode = 0;
  177. LCD_CAM.cam_rgb_yuv.cam_conv_trans_mode = 0;
  178. }
  179. break;
  180. default:
  181. break;
  182. }
  183. #if CONFIG_LCD_CAM_CONV_BT709_ENABLED
  184. LCD_CAM.cam_rgb_yuv.cam_conv_protocol_mode = 1;
  185. #else
  186. LCD_CAM.cam_rgb_yuv.cam_conv_protocol_mode = 0;
  187. #endif
  188. LCD_CAM.cam_rgb_yuv.cam_conv_data_out_mode = 0;
  189. LCD_CAM.cam_rgb_yuv.cam_conv_data_in_mode = 0;
  190. LCD_CAM.cam_rgb_yuv.cam_conv_mode_8bits_on = 1;
  191. LCD_CAM.cam_rgb_yuv.cam_conv_bypass = 1;
  192. cam->conv_mode = config->conv_mode;
  193. return ret;
  194. }
  195. #endif
  196. esp_err_t ll_cam_config(cam_obj_t *cam, const camera_config_t *config)
  197. {
  198. esp_err_t ret = ESP_OK;
  199. if (REG_GET_BIT(SYSTEM_PERIP_CLK_EN1_REG, SYSTEM_LCD_CAM_CLK_EN) == 0) {
  200. REG_CLR_BIT(SYSTEM_PERIP_CLK_EN1_REG, SYSTEM_LCD_CAM_CLK_EN);
  201. REG_SET_BIT(SYSTEM_PERIP_CLK_EN1_REG, SYSTEM_LCD_CAM_CLK_EN);
  202. REG_SET_BIT(SYSTEM_PERIP_RST_EN1_REG, SYSTEM_LCD_CAM_RST);
  203. REG_CLR_BIT(SYSTEM_PERIP_RST_EN1_REG, SYSTEM_LCD_CAM_RST);
  204. }
  205. LCD_CAM.cam_ctrl.val = 0;
  206. LCD_CAM.cam_ctrl.cam_clkm_div_b = 0;
  207. LCD_CAM.cam_ctrl.cam_clkm_div_a = 0;
  208. LCD_CAM.cam_ctrl.cam_clkm_div_num = 160000000 / config->xclk_freq_hz;
  209. LCD_CAM.cam_ctrl.cam_clk_sel = 3;//Select Camera module source clock. 0: no clock. 1: APLL. 2: CLK160. 3: no clock.
  210. LCD_CAM.cam_ctrl.cam_stop_en = 0;
  211. LCD_CAM.cam_ctrl.cam_vsync_filter_thres = 4; // Filter by LCD_CAM clock
  212. LCD_CAM.cam_ctrl.cam_update = 0;
  213. LCD_CAM.cam_ctrl.cam_byte_order = cam->swap_data;
  214. LCD_CAM.cam_ctrl.cam_bit_order = 0;
  215. LCD_CAM.cam_ctrl.cam_line_int_en = 0;
  216. LCD_CAM.cam_ctrl.cam_vs_eof_en = 0; //1: CAM_VSYNC to generate in_suc_eof. 0: in_suc_eof is controlled by reg_cam_rec_data_cyclelen
  217. LCD_CAM.cam_ctrl1.val = 0;
  218. LCD_CAM.cam_ctrl1.cam_rec_data_bytelen = LCD_CAM_DMA_NODE_BUFFER_MAX_SIZE - 1; // Cannot be assigned to 0, and it is easy to overflow
  219. LCD_CAM.cam_ctrl1.cam_line_int_num = 0; // The number of hsyncs that generate hs interrupts
  220. LCD_CAM.cam_ctrl1.cam_clk_inv = 0;
  221. LCD_CAM.cam_ctrl1.cam_vsync_filter_en = 1;
  222. LCD_CAM.cam_ctrl1.cam_2byte_en = 0;
  223. LCD_CAM.cam_ctrl1.cam_de_inv = 0;
  224. LCD_CAM.cam_ctrl1.cam_hsync_inv = 0;
  225. LCD_CAM.cam_ctrl1.cam_vsync_inv = 0;
  226. LCD_CAM.cam_ctrl1.cam_vh_de_mode_en = 0;
  227. LCD_CAM.cam_rgb_yuv.val = 0;
  228. #if CONFIG_CAMERA_CONVERTER_ENABLED
  229. if (config->conv_mode) {
  230. ret = ll_cam_converter_config(cam, config);
  231. if(ret != ESP_OK) {
  232. return ret;
  233. }
  234. }
  235. #endif
  236. LCD_CAM.cam_ctrl.cam_update = 1;
  237. LCD_CAM.cam_ctrl1.cam_start = 1;
  238. ret = ll_cam_dma_init(cam);
  239. return ret;
  240. }
  241. void ll_cam_vsync_intr_enable(cam_obj_t *cam, bool en)
  242. {
  243. LCD_CAM.lc_dma_int_clr.cam_vsync_int_clr = 1;
  244. if (en) {
  245. LCD_CAM.lc_dma_int_ena.cam_vsync_int_ena = 1;
  246. } else {
  247. LCD_CAM.lc_dma_int_ena.cam_vsync_int_ena = 0;
  248. }
  249. }
  250. esp_err_t ll_cam_set_pin(cam_obj_t *cam, const camera_config_t *config)
  251. {
  252. PIN_FUNC_SELECT(GPIO_PIN_MUX_REG[config->pin_pclk], PIN_FUNC_GPIO);
  253. gpio_set_direction(config->pin_pclk, GPIO_MODE_INPUT);
  254. gpio_set_pull_mode(config->pin_pclk, GPIO_FLOATING);
  255. gpio_matrix_in(config->pin_pclk, CAM_PCLK_IDX, false);
  256. PIN_FUNC_SELECT(GPIO_PIN_MUX_REG[config->pin_vsync], PIN_FUNC_GPIO);
  257. gpio_set_direction(config->pin_vsync, GPIO_MODE_INPUT);
  258. gpio_set_pull_mode(config->pin_vsync, GPIO_FLOATING);
  259. gpio_matrix_in(config->pin_vsync, CAM_V_SYNC_IDX, cam->vsync_invert);
  260. PIN_FUNC_SELECT(GPIO_PIN_MUX_REG[config->pin_href], PIN_FUNC_GPIO);
  261. gpio_set_direction(config->pin_href, GPIO_MODE_INPUT);
  262. gpio_set_pull_mode(config->pin_href, GPIO_FLOATING);
  263. gpio_matrix_in(config->pin_href, CAM_H_ENABLE_IDX, false);
  264. int data_pins[8] = {
  265. config->pin_d0, config->pin_d1, config->pin_d2, config->pin_d3, config->pin_d4, config->pin_d5, config->pin_d6, config->pin_d7,
  266. };
  267. for (int i = 0; i < 8; i++) {
  268. PIN_FUNC_SELECT(GPIO_PIN_MUX_REG[data_pins[i]], PIN_FUNC_GPIO);
  269. gpio_set_direction(data_pins[i], GPIO_MODE_INPUT);
  270. gpio_set_pull_mode(data_pins[i], GPIO_FLOATING);
  271. gpio_matrix_in(data_pins[i], CAM_DATA_IN0_IDX + i, false);
  272. }
  273. PIN_FUNC_SELECT(GPIO_PIN_MUX_REG[config->pin_xclk], PIN_FUNC_GPIO);
  274. gpio_set_direction(config->pin_xclk, GPIO_MODE_OUTPUT);
  275. gpio_set_pull_mode(config->pin_xclk, GPIO_FLOATING);
  276. gpio_matrix_out(config->pin_xclk, CAM_CLK_IDX, false, false);
  277. return ESP_OK;
  278. }
  279. esp_err_t ll_cam_init_isr(cam_obj_t *cam)
  280. {
  281. esp_err_t ret = ESP_OK;
  282. ret = esp_intr_alloc_intrstatus(gdma_periph_signals.groups[0].pairs[cam->dma_num].rx_irq_id,
  283. ESP_INTR_FLAG_LOWMED | ESP_INTR_FLAG_SHARED | ESP_INTR_FLAG_IRAM,
  284. (uint32_t)&GDMA.channel[cam->dma_num].in.int_st, GDMA_IN_SUC_EOF_CH0_INT_ST_M,
  285. ll_cam_dma_isr, cam, &cam->dma_intr_handle);
  286. if (ret != ESP_OK) {
  287. ESP_LOGE(TAG, "DMA interrupt allocation of camera failed");
  288. return ret;
  289. }
  290. ret = esp_intr_alloc_intrstatus(ETS_LCD_CAM_INTR_SOURCE,
  291. ESP_INTR_FLAG_LOWMED | ESP_INTR_FLAG_SHARED | ESP_INTR_FLAG_IRAM,
  292. (uint32_t)&LCD_CAM.lc_dma_int_st.val, LCD_CAM_CAM_VSYNC_INT_ST_M,
  293. ll_cam_vsync_isr, cam, &cam->cam_intr_handle);
  294. if (ret != ESP_OK) {
  295. ESP_LOGE(TAG, "LCD_CAM interrupt allocation of camera failed");
  296. return ret;
  297. }
  298. return ESP_OK;
  299. }
  300. void ll_cam_do_vsync(cam_obj_t *cam)
  301. {
  302. gpio_matrix_in(cam->vsync_pin, CAM_V_SYNC_IDX, !cam->vsync_invert);
  303. ets_delay_us(10);
  304. gpio_matrix_in(cam->vsync_pin, CAM_V_SYNC_IDX, cam->vsync_invert);
  305. }
  306. uint8_t ll_cam_get_dma_align(cam_obj_t *cam)
  307. {
  308. return 16 << GDMA.channel[cam->dma_num].in.conf1.in_ext_mem_bk_size;
  309. }
  310. static bool ll_cam_calc_rgb_dma(cam_obj_t *cam){
  311. size_t node_max = LCD_CAM_DMA_NODE_BUFFER_MAX_SIZE / cam->dma_bytes_per_item;
  312. size_t line_width = cam->width * cam->in_bytes_per_pixel;
  313. size_t node_size = node_max;
  314. size_t nodes_per_line = 1;
  315. size_t lines_per_node = 1;
  316. // Calculate DMA Node Size so that it's divisable by or divisor of the line width
  317. if(line_width >= node_max){
  318. // One or more nodes will be requied for one line
  319. for(size_t i = node_max; i > 0; i=i-1){
  320. if ((line_width % i) == 0) {
  321. node_size = i;
  322. nodes_per_line = line_width / node_size;
  323. break;
  324. }
  325. }
  326. } else {
  327. // One or more lines can fit into one node
  328. for(size_t i = node_max; i > 0; i=i-1){
  329. if ((i % line_width) == 0) {
  330. node_size = i;
  331. lines_per_node = node_size / line_width;
  332. while((cam->height % lines_per_node) != 0){
  333. lines_per_node = lines_per_node - 1;
  334. node_size = lines_per_node * line_width;
  335. }
  336. break;
  337. }
  338. }
  339. }
  340. ESP_LOGI(TAG, "node_size: %4u, nodes_per_line: %u, lines_per_node: %u",
  341. node_size * cam->dma_bytes_per_item, nodes_per_line, lines_per_node);
  342. cam->dma_node_buffer_size = node_size * cam->dma_bytes_per_item;
  343. size_t dma_half_buffer_max = CONFIG_CAMERA_DMA_BUFFER_SIZE_MAX / 2 / cam->dma_bytes_per_item;
  344. if (line_width > dma_half_buffer_max) {
  345. ESP_LOGE(TAG, "Resolution too high");
  346. return 0;
  347. }
  348. // Calculate minimum EOF size = max(mode_size, line_size)
  349. size_t dma_half_buffer_min = node_size * nodes_per_line;
  350. // Calculate max EOF size divisable by node size
  351. size_t dma_half_buffer = (dma_half_buffer_max / dma_half_buffer_min) * dma_half_buffer_min;
  352. // Adjust EOF size so that height will be divisable by the number of lines in each EOF
  353. size_t lines_per_half_buffer = dma_half_buffer / line_width;
  354. while((cam->height % lines_per_half_buffer) != 0){
  355. dma_half_buffer = dma_half_buffer - dma_half_buffer_min;
  356. lines_per_half_buffer = dma_half_buffer / line_width;
  357. }
  358. // Calculate DMA size
  359. size_t dma_buffer_max = 2 * dma_half_buffer_max;
  360. if (cam->psram_mode) {
  361. dma_buffer_max = cam->recv_size / cam->dma_bytes_per_item;
  362. }
  363. size_t dma_buffer_size = dma_buffer_max;
  364. if (!cam->psram_mode) {
  365. dma_buffer_size =(dma_buffer_max / dma_half_buffer) * dma_half_buffer;
  366. }
  367. ESP_LOGI(TAG, "dma_half_buffer_min: %5u, dma_half_buffer: %5u, lines_per_half_buffer: %2u, dma_buffer_size: %5u",
  368. 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);
  369. cam->dma_buffer_size = dma_buffer_size * cam->dma_bytes_per_item;
  370. cam->dma_half_buffer_size = dma_half_buffer * cam->dma_bytes_per_item;
  371. cam->dma_half_buffer_cnt = cam->dma_buffer_size / cam->dma_half_buffer_size;
  372. return 1;
  373. }
  374. bool ll_cam_dma_sizes(cam_obj_t *cam)
  375. {
  376. cam->dma_bytes_per_item = 1;
  377. if (cam->jpeg_mode) {
  378. if (cam->psram_mode) {
  379. cam->dma_buffer_size = cam->recv_size;
  380. cam->dma_half_buffer_size = 1024;
  381. cam->dma_half_buffer_cnt = cam->dma_buffer_size / cam->dma_half_buffer_size;
  382. cam->dma_node_buffer_size = cam->dma_half_buffer_size;
  383. } else {
  384. cam->dma_half_buffer_cnt = 16;
  385. cam->dma_buffer_size = cam->dma_half_buffer_cnt * 1024;
  386. cam->dma_half_buffer_size = cam->dma_buffer_size / cam->dma_half_buffer_cnt;
  387. cam->dma_node_buffer_size = cam->dma_half_buffer_size;
  388. }
  389. } else {
  390. return ll_cam_calc_rgb_dma(cam);
  391. }
  392. return 1;
  393. }
  394. size_t IRAM_ATTR ll_cam_memcpy(cam_obj_t *cam, uint8_t *out, const uint8_t *in, size_t len)
  395. {
  396. // YUV to Grayscale
  397. if (cam->in_bytes_per_pixel == 2 && cam->fb_bytes_per_pixel == 1) {
  398. size_t end = len / 8;
  399. for (size_t i = 0; i < end; ++i) {
  400. out[0] = in[0];
  401. out[1] = in[2];
  402. out[2] = in[4];
  403. out[3] = in[6];
  404. out += 4;
  405. in += 8;
  406. }
  407. return len / 2;
  408. }
  409. // just memcpy
  410. memcpy(out, in, len);
  411. return len;
  412. }
  413. 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)
  414. {
  415. if (pix_format == PIXFORMAT_GRAYSCALE) {
  416. if (sensor_pid == OV3660_PID || sensor_pid == OV5640_PID || sensor_pid == NT99141_PID) {
  417. cam->in_bytes_per_pixel = 1; // camera sends Y8
  418. } else {
  419. cam->in_bytes_per_pixel = 2; // camera sends YU/YV
  420. }
  421. cam->fb_bytes_per_pixel = 1; // frame buffer stores Y8
  422. } else if (pix_format == PIXFORMAT_YUV422 || pix_format == PIXFORMAT_RGB565) {
  423. #if CONFIG_CAMERA_CONVERTER_ENABLED
  424. switch (cam->conv_mode) {
  425. case YUV422_TO_YUV420:
  426. cam->in_bytes_per_pixel = 1.5; // for DMA receive
  427. cam->fb_bytes_per_pixel = 1.5; // frame buffer stores YUV420
  428. break;
  429. case YUV422_TO_RGB565:
  430. default:
  431. cam->in_bytes_per_pixel = 2; // for DMA receive
  432. cam->fb_bytes_per_pixel = 2; // frame buffer stores YU/YV/RGB565
  433. break;
  434. }
  435. #else
  436. cam->in_bytes_per_pixel = 2; // for DMA receive
  437. cam->fb_bytes_per_pixel = 2; // frame buffer stores YU/YV/RGB565
  438. #endif
  439. } else if (pix_format == PIXFORMAT_JPEG) {
  440. cam->in_bytes_per_pixel = 1;
  441. cam->fb_bytes_per_pixel = 1;
  442. } else {
  443. ESP_LOGE(TAG, "Requested format is not supported");
  444. return ESP_ERR_NOT_SUPPORTED;
  445. }
  446. return ESP_OK;
  447. }
  448. // implements function from xclk.c to allow dynamic XCLK change
  449. esp_err_t xclk_timer_conf(int ledc_timer, int xclk_freq_hz)
  450. {
  451. LCD_CAM.cam_ctrl.cam_clkm_div_b = 0;
  452. LCD_CAM.cam_ctrl.cam_clkm_div_a = 0;
  453. LCD_CAM.cam_ctrl.cam_clkm_div_num = 160000000 / xclk_freq_hz;
  454. LCD_CAM.cam_ctrl.cam_clk_sel = 3;//Select Camera module source clock. 0: no clock. 1: APLL. 2: CLK160. 3: no clock.
  455. LCD_CAM.cam_ctrl.cam_update = 1;
  456. return ESP_OK;
  457. }