esp_lcd_panel_rgb.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. /*
  2. * SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #pragma once
  7. #include <stdint.h>
  8. #include <stdbool.h>
  9. #include "esp_err.h"
  10. #include "esp_lcd_types.h"
  11. #include "soc/soc_caps.h"
  12. #include "hal/lcd_types.h"
  13. #ifdef __cplusplus
  14. extern "C" {
  15. #endif
  16. #if SOC_LCD_RGB_SUPPORTED
  17. /**
  18. * @brief LCD RGB timing structure
  19. * @verbatim
  20. * Total Width
  21. * <--------------------------------------------------->
  22. * HSYNC width HBP Active Width HFP
  23. * <---><--><--------------------------------------><--->
  24. * ____ ____|_______________________________________|____|
  25. * |___| | | |
  26. * | | |
  27. * __| | | |
  28. * /|\ /|\ | | | |
  29. * | VSYNC| | | | |
  30. * |Width\|/ |__ | | |
  31. * | /|\ | | | |
  32. * | VBP | | | | |
  33. * | \|/_____|_________|_______________________________________| |
  34. * | /|\ | | / / / / / / / / / / / / / / / / / / / | |
  35. * | | | |/ / / / / / / / / / / / / / / / / / / /| |
  36. * Total | | | |/ / / / / / / / / / / / / / / / / / / /| |
  37. * Height | | | |/ / / / / / / / / / / / / / / / / / / /| |
  38. * |Active| | |/ / / / / / / / / / / / / / / / / / / /| |
  39. * |Heigh | | |/ / / / / / Active Display Area / / / /| |
  40. * | | | |/ / / / / / / / / / / / / / / / / / / /| |
  41. * | | | |/ / / / / / / / / / / / / / / / / / / /| |
  42. * | | | |/ / / / / / / / / / / / / / / / / / / /| |
  43. * | | | |/ / / / / / / / / / / / / / / / / / / /| |
  44. * | | | |/ / / / / / / / / / / / / / / / / / / /| |
  45. * | \|/_____|_________|_______________________________________| |
  46. * | /|\ | |
  47. * | VFP | | |
  48. * \|/ \|/_____|______________________________________________________|
  49. * @endverbatim
  50. */
  51. typedef struct {
  52. uint32_t pclk_hz; /*!< Frequency of pixel clock */
  53. uint32_t h_res; /*!< Horizontal resolution, i.e. the number of pixels in a line */
  54. uint32_t v_res; /*!< Vertical resolution, i.e. the number of lines in the frame */
  55. uint32_t hsync_pulse_width; /*!< Horizontal sync width, unit: PCLK period */
  56. uint32_t hsync_back_porch; /*!< Horizontal back porch, number of PCLK between hsync and start of line active data */
  57. uint32_t hsync_front_porch; /*!< Horizontal front porch, number of PCLK between the end of active data and the next hsync */
  58. uint32_t vsync_pulse_width; /*!< Vertical sync width, unit: number of lines */
  59. uint32_t vsync_back_porch; /*!< Vertical back porch, number of invalid lines between vsync and start of frame */
  60. uint32_t vsync_front_porch; /*!< Vertical front porch, number of invalid lines between the end of frame and the next vsync */
  61. struct {
  62. uint32_t hsync_idle_low: 1; /*!< The hsync signal is low in IDLE state */
  63. uint32_t vsync_idle_low: 1; /*!< The vsync signal is low in IDLE state */
  64. uint32_t de_idle_high: 1; /*!< The de signal is high in IDLE state */
  65. uint32_t pclk_active_neg: 1; /*!< Whether the display data is clocked out on the falling edge of PCLK */
  66. uint32_t pclk_idle_high: 1; /*!< The PCLK stays at high level in IDLE phase */
  67. } flags; /*!< LCD RGB timing flags */
  68. } esp_lcd_rgb_timing_t;
  69. /**
  70. * @brief Type of RGB LCD panel event data
  71. */
  72. typedef struct {
  73. } esp_lcd_rgb_panel_event_data_t;
  74. /**
  75. * @brief RGB LCD VSYNC event callback prototype
  76. *
  77. * @param[in] panel LCD panel handle, returned from `esp_lcd_new_rgb_panel`
  78. * @param[in] edata Panel event data, fed by driver
  79. * @param[in] user_ctx User data, passed from `esp_lcd_rgb_panel_register_event_callbacks()`
  80. * @return Whether a high priority task has been waken up by this function
  81. */
  82. typedef bool (*esp_lcd_rgb_panel_vsync_cb_t)(esp_lcd_panel_handle_t panel, const esp_lcd_rgb_panel_event_data_t *edata, void *user_ctx);
  83. /**
  84. * @brief Prototype for function to re-fill a bounce buffer, rather than copying from the frame buffer
  85. *
  86. * @param[in] panel LCD panel handle, returned from `esp_lcd_new_rgb_panel`
  87. * @param[in] bounce_buf Bounce buffer to write data into
  88. * @param[in] pos_px How many pixels already were sent to the display in this frame, in other words,
  89. * at what pixel the routine should start putting data into bounce_buf
  90. * @param[in] len_bytes Length, in bytes, of the bounce buffer. Routine should fill this length fully.
  91. * @param[in] user_ctx Opaque pointer that was passed from `esp_lcd_rgb_panel_register_event_callbacks()`
  92. * @return Whether a high priority task has been waken up by this function
  93. */
  94. typedef bool (*esp_lcd_rgb_panel_bounce_buf_fill_cb_t)(esp_lcd_panel_handle_t panel, void *bounce_buf, int pos_px, int len_bytes, void *user_ctx);
  95. /**
  96. * @brief Prototype for the function to be called when the bounce buffer finish copying the entire frame.
  97. *
  98. * @param[in] panel LCD panel handle, returned from `esp_lcd_new_rgb_panel`
  99. * @param[in] edata Panel event data, fed by driver
  100. * @param[in] user_ctx User data, passed from `esp_lcd_rgb_panel_register_event_callbacks()`
  101. * @return Whether a high priority task has been waken up by this function
  102. */
  103. typedef bool (*esp_lcd_rgb_panel_bounce_buf_finish_cb_t)(esp_lcd_panel_handle_t panel, const esp_lcd_rgb_panel_event_data_t *edata, void *user_ctx);
  104. /**
  105. * @brief Group of supported RGB LCD panel callbacks
  106. * @note The callbacks are all running under ISR environment
  107. * @note When CONFIG_LCD_RGB_ISR_IRAM_SAFE is enabled, the callback itself and functions called by it should be placed in IRAM.
  108. */
  109. typedef struct {
  110. esp_lcd_rgb_panel_vsync_cb_t on_vsync; /*!< VSYNC event callback */
  111. esp_lcd_rgb_panel_bounce_buf_fill_cb_t on_bounce_empty; /*!< Bounce buffer empty callback. */
  112. esp_lcd_rgb_panel_bounce_buf_finish_cb_t on_bounce_frame_finish; /*!< Bounce buffer finish callback. */
  113. } esp_lcd_rgb_panel_event_callbacks_t;
  114. /**
  115. * @brief LCD RGB panel configuration structure
  116. */
  117. typedef struct {
  118. lcd_clock_source_t clk_src; /*!< Clock source for the RGB LCD peripheral */
  119. esp_lcd_rgb_timing_t timings; /*!< RGB timing parameters, including the screen resolution */
  120. size_t data_width; /*!< Number of data lines */
  121. size_t bits_per_pixel; /*!< Frame buffer color depth, in bpp, specially, if set to zero, it will default to `data_width`.
  122. When using a Serial RGB interface, this value could be different from `data_width` */
  123. size_t num_fbs; /*!< Number of screen-sized frame buffers that allocated by the driver. By default (set to either 0 or 1) only one frame buffer will be used. Maximum number of buffers are 3 */
  124. size_t bounce_buffer_size_px; /*!< If it's non-zero, the driver allocates two DRAM bounce buffers for DMA use.
  125. DMA fetching from DRAM bounce buffer is much faster than PSRAM frame buffer. */
  126. size_t sram_trans_align; /*!< Alignment of buffers (frame buffer or bounce buffer) that allocated in SRAM */
  127. size_t psram_trans_align; /*!< Alignment of buffers (frame buffer) that allocated in PSRAM */
  128. int hsync_gpio_num; /*!< GPIO used for HSYNC signal */
  129. int vsync_gpio_num; /*!< GPIO used for VSYNC signal */
  130. int de_gpio_num; /*!< GPIO used for DE signal, set to -1 if it's not used */
  131. int pclk_gpio_num; /*!< GPIO used for PCLK signal, set to -1 if it's not used */
  132. int disp_gpio_num; /*!< GPIO used for display control signal, set to -1 if it's not used */
  133. int data_gpio_nums[SOC_LCD_RGB_DATA_WIDTH]; /*!< GPIOs used for data lines */
  134. struct {
  135. uint32_t disp_active_low: 1; /*!< If this flag is enabled, a low level of display control signal can turn the screen on; vice versa */
  136. uint32_t refresh_on_demand: 1; /*!< If this flag is enabled, the host only refresh the frame buffer when `esp_lcd_panel_draw_bitmap` is called.
  137. This is useful when the LCD screen has a GRAM and can refresh the LCD by itself. */
  138. uint32_t fb_in_psram: 1; /*!< If this flag is enabled, the frame buffer will be allocated from PSRAM, preferentially */
  139. uint32_t double_fb: 1; /*!< If this flag is enabled, the driver will allocate two screen sized frame buffer, same as num_fbs=2 */
  140. uint32_t no_fb: 1; /*!< If this flag is enabled, the driver won't allocate frame buffer.
  141. Instead, user should fill in the bounce buffer manually in the `on_bounce_empty` callback */
  142. uint32_t bb_invalidate_cache: 1; /*!< If this flag is enabled, in bounce back mode we'll do a cache invalidate on the read data, freeing the cache.
  143. Can be dangerous if data is written from other core(s). */
  144. } flags; /*!< LCD RGB panel configuration flags */
  145. } esp_lcd_rgb_panel_config_t;
  146. /**
  147. * @brief Create RGB LCD panel
  148. *
  149. * @param[in] rgb_panel_config RGB panel configuration
  150. * @param[out] ret_panel Returned LCD panel handle
  151. * @return
  152. * - ESP_ERR_INVALID_ARG: Create RGB LCD panel failed because of invalid argument
  153. * - ESP_ERR_NO_MEM: Create RGB LCD panel failed because of out of memory
  154. * - ESP_ERR_NOT_FOUND: Create RGB LCD panel failed because some mandatory hardware resources are not found
  155. * - ESP_OK: Create RGB LCD panel successfully
  156. */
  157. esp_err_t esp_lcd_new_rgb_panel(const esp_lcd_rgb_panel_config_t *rgb_panel_config, esp_lcd_panel_handle_t *ret_panel);
  158. /**
  159. * @brief Register LCD RGB panel event callbacks
  160. *
  161. * @param[in] panel LCD panel handle, returned from `esp_lcd_new_rgb_panel`
  162. * @param[in] callbacks Group of callback functions
  163. * @param[in] user_ctx User data, which will be passed to the callback functions directly
  164. * @return
  165. * - ESP_OK: Set event callbacks successfully
  166. * - ESP_ERR_INVALID_ARG: Set event callbacks failed because of invalid argument
  167. * - ESP_FAIL: Set event callbacks failed because of other error
  168. */
  169. esp_err_t esp_lcd_rgb_panel_register_event_callbacks(esp_lcd_panel_handle_t panel, const esp_lcd_rgb_panel_event_callbacks_t *callbacks, void *user_ctx);
  170. /**
  171. * @brief Set frequency of PCLK for RGB LCD panel
  172. *
  173. * @note The PCLK frequency is set in the `esp_lcd_rgb_timing_t` and gets configured during LCD panel initialization.
  174. * Usually you don't need to call this function to set the PCLK again, but in some cases, you might want to change the PCLK frequency.
  175. * e.g. slow down the PCLK frequency to reduce power consumption or to reduce the memory throughput during OTA.
  176. * @note This function doesn't cause the hardware to update the PCLK immediately but to record the new frequency and set a flag internally.
  177. * Only in the next VSYNC event handler, will the driver attempt to update the PCLK frequency.
  178. *
  179. * @param[in] panel LCD panel handle, returned from `esp_lcd_new_rgb_panel`
  180. * @param[in] freq_hz Frequency of pixel clock, in Hz
  181. * @return
  182. * - ESP_ERR_INVALID_ARG: Set PCLK frequency failed because of invalid argument
  183. * - ESP_OK: Set PCLK frequency successfully
  184. */
  185. esp_err_t esp_lcd_rgb_panel_set_pclk(esp_lcd_panel_handle_t panel, uint32_t freq_hz);
  186. /**
  187. * @brief Restart the LCD transmission
  188. *
  189. * @note This function can be useful when the LCD controller is out of sync with the DMA because of insufficient bandwidth.
  190. * To save the screen from a permanent shift, you can call this function to restart the LCD DMA.
  191. * @note This function doesn't restart the DMA immediately but to set a flag internally.
  192. * Only in the next VSYNC event handler, will the driver attempt to do the restart job.
  193. * @note If CONFIG_LCD_RGB_RESTART_IN_VSYNC is enabled, you don't need to call this function manually,
  194. * because the restart job will be done automatically in the VSYNC event handler.
  195. *
  196. * @param[in] panel panel LCD panel handle, returned from `esp_lcd_new_rgb_panel`
  197. * @return
  198. * - ESP_ERR_INVALID_ARG: Restart the LCD failed because of invalid argument
  199. * - ESP_ERR_INVALID_STATE: Restart the LCD failed because the LCD diver is working in refresh-on-demand mode
  200. * - ESP_OK: Restart the LCD successfully
  201. */
  202. esp_err_t esp_lcd_rgb_panel_restart(esp_lcd_panel_handle_t panel);
  203. /**
  204. * @brief Get the address of the frame buffer(s) that allocated by the driver
  205. *
  206. * @param[in] panel LCD panel handle, returned from `esp_lcd_new_rgb_panel`
  207. * @param[in] fb_num Number of frame buffer(s) to get. This value must be the same as the number of the following parameters.
  208. * @param[out] fb0 Returned address of the frame buffer 0
  209. * @param[out] ... List of other frame buffer addresses
  210. * @return
  211. * - ESP_ERR_INVALID_ARG: Get frame buffer address failed because of invalid argument
  212. * - ESP_OK: Get frame buffer address successfully
  213. */
  214. esp_err_t esp_lcd_rgb_panel_get_frame_buffer(esp_lcd_panel_handle_t panel, uint32_t fb_num, void **fb0, ...);
  215. /**
  216. * @brief Manually trigger once transmission of the frame buffer to the LCD panel
  217. *
  218. * @note This function should only be called when the RGB panel is working under the `refresh_on_demand` mode.
  219. *
  220. * @param[in] panel LCD panel handle, returned from `esp_lcd_new_rgb_panel`
  221. * @return
  222. * - ESP_ERR_INVALID_ARG: Start a refresh failed because of invalid argument
  223. * - ESP_ERR_INVALID_STATE: Start a refresh failed because the LCD panel is not created with the `refresh_on_demand` flag enabled.
  224. * - ESP_OK: Start a refresh successfully
  225. */
  226. esp_err_t esp_lcd_rgb_panel_refresh(esp_lcd_panel_handle_t panel);
  227. /**
  228. * @brief LCD color conversion profile
  229. */
  230. typedef struct {
  231. lcd_color_space_t color_space; /*!< Color space of the image */
  232. lcd_color_range_t color_range; /*!< Color range of the image */
  233. lcd_yuv_sample_t yuv_sample; /*!< YUV sample format of the image */
  234. } esp_lcd_color_conv_profile_t;
  235. /**
  236. * @brief Configuration of YUG-RGB conversion
  237. */
  238. typedef struct {
  239. lcd_yuv_conv_std_t std; /*!< YUV conversion standard: BT601, BT709 */
  240. esp_lcd_color_conv_profile_t src; /*!< Color conversion profile of the input image */
  241. esp_lcd_color_conv_profile_t dst; /*!< Color conversion profile of the output image */
  242. } esp_lcd_yuv_conv_config_t;
  243. /**
  244. * @brief Configure how to convert the color format between RGB and YUV
  245. *
  246. * @note Pass in `config` as NULL will disable the RGB-YUV converter.
  247. * @note The hardware converter can only parse a "packed" storage format, while "planar" and "semi-planar" format is not supported.
  248. *
  249. * @param[in] panel LCD panel handle, returned from `esp_lcd_new_rgb_panel`
  250. * @param[in] config Configuration of RGB-YUV conversion
  251. * @return
  252. * - ESP_ERR_INVALID_ARG: Configure RGB-YUV conversion failed because of invalid argument
  253. * - ESP_ERR_NOT_SUPPORTED: Configure RGB-YUV conversion failed because the conversion mode is not supported by the hardware
  254. * - ESP_OK: Configure RGB-YUV conversion successfully
  255. */
  256. esp_err_t esp_lcd_rgb_panel_set_yuv_conversion(esp_lcd_panel_handle_t panel, const esp_lcd_yuv_conv_config_t *config);
  257. #endif // SOC_LCD_RGB_SUPPORTED
  258. #ifdef __cplusplus
  259. }
  260. #endif