esp_lcd_panel_io.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /*
  2. * SPDX-FileCopyrightText: 2021-2023 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #pragma once
  7. #include <stdbool.h>
  8. #include "esp_err.h"
  9. #include "esp_lcd_types.h"
  10. #include "soc/soc_caps.h"
  11. #include "hal/lcd_types.h"
  12. #include "hal/i2c_types.h"
  13. #include "driver/i2c_types.h"
  14. #ifdef __cplusplus
  15. extern "C" {
  16. #endif
  17. typedef void *esp_lcd_spi_bus_handle_t; /*!< Type of LCD SPI bus handle */
  18. typedef uint32_t esp_lcd_i2c_bus_handle_t; /*!< Type of LCD I2C bus handle */
  19. typedef struct esp_lcd_i80_bus_t *esp_lcd_i80_bus_handle_t; /*!< Type of LCD intel 8080 bus handle */
  20. /**
  21. * @brief Type of LCD panel IO event data
  22. */
  23. typedef struct {
  24. } esp_lcd_panel_io_event_data_t;
  25. /**
  26. * @brief Declare the prototype of the function that will be invoked when panel IO finishes transferring color data
  27. *
  28. * @param[in] panel_io LCD panel IO handle, which is created by factory API like `esp_lcd_new_panel_io_spi()`
  29. * @param[in] edata Panel IO event data, fed by driver
  30. * @param[in] user_ctx User data, passed from `esp_lcd_panel_io_xxx_config_t`
  31. * @return Whether a high priority task has been waken up by this function
  32. */
  33. typedef bool (*esp_lcd_panel_io_color_trans_done_cb_t)(esp_lcd_panel_io_handle_t panel_io, esp_lcd_panel_io_event_data_t *edata, void *user_ctx);
  34. /**
  35. * @brief Type of LCD panel IO callbacks
  36. */
  37. typedef struct {
  38. esp_lcd_panel_io_color_trans_done_cb_t on_color_trans_done; /*!< Callback invoked when color data transfer has finished */
  39. } esp_lcd_panel_io_callbacks_t;
  40. /**
  41. * @brief Transmit LCD command and receive corresponding parameters
  42. *
  43. * @note Commands sent by this function are short, so they are sent using polling transactions.
  44. * The function does not return before the command transfer is completed.
  45. * If any queued transactions sent by `esp_lcd_panel_io_tx_color()` are still pending when this function is called,
  46. * this function will wait until they are finished and the queue is empty before sending the command(s).
  47. *
  48. * @param[in] io LCD panel IO handle, which is created by other factory API like `esp_lcd_new_panel_io_spi()`
  49. * @param[in] lcd_cmd The specific LCD command, set to -1 if no command needed
  50. * @param[out] param Buffer for the command data
  51. * @param[in] param_size Size of `param` buffer
  52. * @return
  53. * - ESP_ERR_INVALID_ARG if parameter is invalid
  54. * - ESP_ERR_NOT_SUPPORTED if read is not supported by transport
  55. * - ESP_OK on success
  56. */
  57. esp_err_t esp_lcd_panel_io_rx_param(esp_lcd_panel_io_handle_t io, int lcd_cmd, void *param, size_t param_size);
  58. /**
  59. * @brief Transmit LCD command and corresponding parameters
  60. *
  61. * @note Commands sent by this function are short, so they are sent using polling transactions.
  62. * The function does not return before the command transfer is completed.
  63. * If any queued transactions sent by `esp_lcd_panel_io_tx_color()` are still pending when this function is called,
  64. * this function will wait until they are finished and the queue is empty before sending the command(s).
  65. *
  66. * @param[in] io LCD panel IO handle, which is created by other factory API like `esp_lcd_new_panel_io_spi()`
  67. * @param[in] lcd_cmd The specific LCD command, set to -1 if no command needed
  68. * @param[in] param Buffer that holds the command specific parameters, set to NULL if no parameter is needed for the command
  69. * @param[in] param_size Size of `param` in memory, in bytes, set to zero if no parameter is needed for the command
  70. * @return
  71. * - ESP_ERR_INVALID_ARG if parameter is invalid
  72. * - ESP_OK on success
  73. */
  74. esp_err_t esp_lcd_panel_io_tx_param(esp_lcd_panel_io_handle_t io, int lcd_cmd, const void *param, size_t param_size);
  75. /**
  76. * @brief Transmit LCD RGB data
  77. *
  78. * @note This function will package the command and RGB data into a transaction, and push into a queue.
  79. * The real transmission is performed in the background (DMA+interrupt).
  80. * The caller should take care of the lifecycle of the `color` buffer.
  81. * Recycling of color buffer should be done in the callback `on_color_trans_done()`.
  82. *
  83. * @param[in] io LCD panel IO handle, which is created by factory API like `esp_lcd_new_panel_io_spi()`
  84. * @param[in] lcd_cmd The specific LCD command, set to -1 if no command needed
  85. * @param[in] color Buffer that holds the RGB color data
  86. * @param[in] color_size Size of `color` in memory, in bytes
  87. * @return
  88. * - ESP_ERR_INVALID_ARG if parameter is invalid
  89. * - ESP_OK on success
  90. */
  91. esp_err_t esp_lcd_panel_io_tx_color(esp_lcd_panel_io_handle_t io, int lcd_cmd, const void *color, size_t color_size);
  92. /**
  93. * @brief Destroy LCD panel IO handle (deinitialize panel and free all corresponding resource)
  94. *
  95. * @param[in] io LCD panel IO handle, which is created by factory API like `esp_lcd_new_panel_io_spi()`
  96. * @return
  97. * - ESP_ERR_INVALID_ARG if parameter is invalid
  98. * - ESP_OK on success
  99. */
  100. esp_err_t esp_lcd_panel_io_del(esp_lcd_panel_io_handle_t io);
  101. /**
  102. * @brief Register LCD panel IO callbacks
  103. *
  104. * @param[in] io LCD panel IO handle, which is created by factory API like `esp_lcd_new_panel_io_spi()`
  105. * @param[in] cbs structure with all LCD panel IO callbacks
  106. * @param[in] user_ctx User private data, passed directly to callback's user_ctx
  107. * @return
  108. * - ESP_ERR_INVALID_ARG if parameter is invalid
  109. * - ESP_OK on success
  110. */
  111. esp_err_t esp_lcd_panel_io_register_event_callbacks(esp_lcd_panel_io_handle_t io, const esp_lcd_panel_io_callbacks_t *cbs, void *user_ctx);
  112. /**
  113. * @brief Panel IO configuration structure, for SPI interface
  114. */
  115. typedef struct {
  116. int cs_gpio_num; /*!< GPIO used for CS line */
  117. int dc_gpio_num; /*!< GPIO used to select the D/C line, set this to -1 if the D/C line is not used */
  118. int spi_mode; /*!< Traditional SPI mode (0~3) */
  119. unsigned int pclk_hz; /*!< Frequency of pixel clock */
  120. size_t trans_queue_depth; /*!< Size of internal transaction queue */
  121. esp_lcd_panel_io_color_trans_done_cb_t on_color_trans_done; /*!< Callback invoked when color data transfer has finished */
  122. void *user_ctx; /*!< User private data, passed directly to on_color_trans_done's user_ctx */
  123. int lcd_cmd_bits; /*!< Bit-width of LCD command */
  124. int lcd_param_bits; /*!< Bit-width of LCD parameter */
  125. struct {
  126. unsigned int dc_low_on_data: 1; /*!< If this flag is enabled, DC line = 0 means transfer data, DC line = 1 means transfer command; vice versa */
  127. unsigned int octal_mode: 1; /*!< transmit with octal mode (8 data lines), this mode is used to simulate Intel 8080 timing */
  128. unsigned int sio_mode: 1; /*!< Read and write through a single data line (MOSI) */
  129. unsigned int lsb_first: 1; /*!< transmit LSB bit first */
  130. unsigned int cs_high_active: 1; /*!< CS line is high active */
  131. } flags; /*!< Extra flags to fine-tune the SPI device */
  132. } esp_lcd_panel_io_spi_config_t;
  133. /**
  134. * @brief Create LCD panel IO handle, for SPI interface
  135. *
  136. * @param[in] bus SPI bus handle
  137. * @param[in] io_config IO configuration, for SPI interface
  138. * @param[out] ret_io Returned IO handle
  139. * @return
  140. * - ESP_ERR_INVALID_ARG if parameter is invalid
  141. * - ESP_ERR_NO_MEM if out of memory
  142. * - ESP_OK on success
  143. */
  144. esp_err_t esp_lcd_new_panel_io_spi(esp_lcd_spi_bus_handle_t bus, const esp_lcd_panel_io_spi_config_t *io_config, esp_lcd_panel_io_handle_t *ret_io);
  145. /**
  146. * @brief Panel IO configuration structure, for I2C interface
  147. *
  148. */
  149. typedef struct {
  150. uint32_t dev_addr; /*!< I2C device address */
  151. esp_lcd_panel_io_color_trans_done_cb_t on_color_trans_done; /*!< Callback invoked when color data transfer has finished */
  152. void *user_ctx; /*!< User private data, passed directly to on_color_trans_done's user_ctx */
  153. size_t control_phase_bytes; /*!< I2C LCD panel will encode control information (e.g. D/C selection) into control phase, in several bytes */
  154. unsigned int dc_bit_offset; /*!< Offset of the D/C selection bit in control phase */
  155. int lcd_cmd_bits; /*!< Bit-width of LCD command */
  156. int lcd_param_bits; /*!< Bit-width of LCD parameter */
  157. struct {
  158. unsigned int dc_low_on_data: 1; /*!< If this flag is enabled, DC line = 0 means transfer data, DC line = 1 means transfer command; vice versa */
  159. unsigned int disable_control_phase: 1; /*!< If this flag is enabled, the control phase isn't used */
  160. } flags; /*!< Extra flags to fine-tune the I2C device */
  161. uint32_t scl_speed_hz; /*!< I2C LCD SCL frequency (hz) */
  162. } esp_lcd_panel_io_i2c_config_t;
  163. /**
  164. * @brief Create LCD panel IO handle, for I2C interface in legacy implementation
  165. *
  166. * @param[in] bus I2C bus handle, (in uint32_t)
  167. * @param[in] io_config IO configuration, for I2C interface
  168. * @param[out] ret_io Returned IO handle
  169. *
  170. * @note Please don't call this function in your project directly. Please call `esp_lcd_new_panel_to_i2c` instead.
  171. *
  172. * @return
  173. * - ESP_ERR_INVALID_ARG if parameter is invalid
  174. * - ESP_ERR_NO_MEM if out of memory
  175. * - ESP_OK on success
  176. */
  177. esp_err_t esp_lcd_new_panel_io_i2c_v1(uint32_t bus, const esp_lcd_panel_io_i2c_config_t *io_config, esp_lcd_panel_io_handle_t *ret_io);
  178. /**
  179. * @brief Create LCD panel IO handle, for I2C interface in new implementation
  180. *
  181. * @param[in] bus I2C bus handle, (in i2c_master_dev_handle_t)
  182. * @param[in] io_config IO configuration, for I2C interface
  183. * @param[out] ret_io Returned IO handle
  184. *
  185. * @note Please don't call this function in your project directly. Please call `esp_lcd_new_panel_to_i2c` instead.
  186. *
  187. * @return
  188. * - ESP_ERR_INVALID_ARG if parameter is invalid
  189. * - ESP_ERR_NO_MEM if out of memory
  190. * - ESP_OK on success
  191. */
  192. esp_err_t esp_lcd_new_panel_io_i2c_v2(i2c_master_bus_handle_t bus, const esp_lcd_panel_io_i2c_config_t *io_config, esp_lcd_panel_io_handle_t *ret_io);
  193. /**
  194. * @brief Create LCD panel IO handle
  195. *
  196. * @param[in] bus I2C bus handle
  197. * @param[in] io_config IO configuration, for I2C interface
  198. * @param[out] ret_io Returned IO handle
  199. * @return
  200. * - ESP_ERR_INVALID_ARG if parameter is invalid
  201. * - ESP_ERR_NO_MEM if out of memory
  202. * - ESP_OK on success
  203. */
  204. #define esp_lcd_new_panel_io_i2c(bus, io_config, ret_io) _Generic((bus), \
  205. i2c_master_bus_handle_t : esp_lcd_new_panel_io_i2c_v2, \
  206. default : esp_lcd_new_panel_io_i2c_v1) (bus, io_config, ret_io) \
  207. #if SOC_LCD_I80_SUPPORTED
  208. /**
  209. * @brief LCD Intel 8080 bus configuration structure
  210. */
  211. typedef struct {
  212. int dc_gpio_num; /*!< GPIO used for D/C line */
  213. int wr_gpio_num; /*!< GPIO used for WR line */
  214. lcd_clock_source_t clk_src; /*!< Clock source for the I80 LCD peripheral */
  215. int data_gpio_nums[SOC_LCD_I80_BUS_WIDTH]; /*!< GPIOs used for data lines */
  216. size_t bus_width; /*!< Number of data lines, 8 or 16 */
  217. size_t max_transfer_bytes; /*!< Maximum transfer size, this determines the length of internal DMA link */
  218. size_t psram_trans_align; /*!< DMA transfer alignment for data allocated from PSRAM */
  219. size_t sram_trans_align; /*!< DMA transfer alignment for data allocated from SRAM */
  220. } esp_lcd_i80_bus_config_t;
  221. /**
  222. * @brief Create Intel 8080 bus handle
  223. *
  224. * @param[in] bus_config Bus configuration
  225. * @param[out] ret_bus Returned bus handle
  226. * @return
  227. * - ESP_ERR_INVALID_ARG if parameter is invalid
  228. * - ESP_ERR_NO_MEM if out of memory
  229. * - ESP_ERR_NOT_FOUND if no free bus is available
  230. * - ESP_OK on success
  231. */
  232. esp_err_t esp_lcd_new_i80_bus(const esp_lcd_i80_bus_config_t *bus_config, esp_lcd_i80_bus_handle_t *ret_bus);
  233. /**
  234. * @brief Destroy Intel 8080 bus handle
  235. *
  236. * @param[in] bus Intel 8080 bus handle, created by `esp_lcd_new_i80_bus()`
  237. * @return
  238. * - ESP_ERR_INVALID_ARG if parameter is invalid
  239. * - ESP_ERR_INVALID_STATE if there still be some device attached to the bus
  240. * - ESP_OK on success
  241. */
  242. esp_err_t esp_lcd_del_i80_bus(esp_lcd_i80_bus_handle_t bus);
  243. /**
  244. * @brief Panel IO configuration structure, for intel 8080 interface
  245. */
  246. typedef struct {
  247. int cs_gpio_num; /*!< GPIO used for CS line, set to -1 will declaim exclusively use of I80 bus */
  248. uint32_t pclk_hz; /*!< Frequency of pixel clock */
  249. size_t trans_queue_depth; /*!< Transaction queue size, larger queue, higher throughput */
  250. esp_lcd_panel_io_color_trans_done_cb_t on_color_trans_done; /*!< Callback invoked when color data was transferred done */
  251. void *user_ctx; /*!< User private data, passed directly to on_color_trans_done's user_ctx */
  252. int lcd_cmd_bits; /*!< Bit-width of LCD command */
  253. int lcd_param_bits; /*!< Bit-width of LCD parameter */
  254. struct {
  255. unsigned int dc_idle_level: 1; /*!< Level of DC line in IDLE phase */
  256. unsigned int dc_cmd_level: 1; /*!< Level of DC line in CMD phase */
  257. unsigned int dc_dummy_level: 1; /*!< Level of DC line in DUMMY phase */
  258. unsigned int dc_data_level: 1; /*!< Level of DC line in DATA phase */
  259. } dc_levels; /*!< Each i80 device might have its own D/C control logic */
  260. struct {
  261. unsigned int cs_active_high: 1; /*!< If set, a high level of CS line will select the device, otherwise, CS line is low level active */
  262. unsigned int reverse_color_bits: 1; /*!< Reverse the data bits, D[N:0] -> D[0:N] */
  263. unsigned int swap_color_bytes: 1; /*!< Swap adjacent two color bytes */
  264. unsigned int pclk_active_neg: 1; /*!< The display will write data lines when there's a falling edge on WR signal (a.k.a the PCLK) */
  265. unsigned int pclk_idle_low: 1; /*!< The WR signal (a.k.a the PCLK) stays at low level in IDLE phase */
  266. } flags; /*!< Panel IO config flags */
  267. } esp_lcd_panel_io_i80_config_t;
  268. /**
  269. * @brief Create LCD panel IO, for Intel 8080 interface
  270. *
  271. * @param[in] bus Intel 8080 bus handle, created by `esp_lcd_new_i80_bus()`
  272. * @param[in] io_config IO configuration, for i80 interface
  273. * @param[out] ret_io Returned panel IO handle
  274. * @return
  275. * - ESP_ERR_INVALID_ARG if parameter is invalid
  276. * - ESP_ERR_NOT_SUPPORTED if some configuration can't be satisfied, e.g. pixel clock out of the range
  277. * - ESP_ERR_NO_MEM if out of memory
  278. * - ESP_OK on success
  279. */
  280. esp_err_t esp_lcd_new_panel_io_i80(esp_lcd_i80_bus_handle_t bus, const esp_lcd_panel_io_i80_config_t *io_config, esp_lcd_panel_io_handle_t *ret_io);
  281. #endif // SOC_LCD_I80_SUPPORTED
  282. #ifdef __cplusplus
  283. }
  284. #endif