hpm_lcdc_drv.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. /*
  2. * Copyright (c) 2021 HPMicro
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. *
  6. */
  7. #ifndef HPM_LCDC_DRV_H
  8. #define HPM_LCDC_DRV_H
  9. #include "hpm_display_common.h"
  10. #include "hpm_soc_feature.h"
  11. #include "hpm_lcdc_regs.h"
  12. /**
  13. *
  14. * @brief LCD driver APIs
  15. * @defgroup lcd_interface LCD driver APIs
  16. * @ingroup io_interfaces
  17. * @{
  18. */
  19. #define LCDC_TEST_MODE_DISABLE (0U)
  20. #define LCDC_TEST_MODE_BACKGROUND (1U)
  21. #define LCDC_TEST_MODE_COLOR_BAR_COL (2U)
  22. #define LCDC_TEST_MODE_COLOR_BAR_ROW (3U)
  23. /* @brief LCD driver specific status */
  24. enum {
  25. status_lcdc_no_active_layer_yet = MAKE_STATUS(status_group_lcdc, 1),
  26. status_lcdc_layer_not_supported = MAKE_STATUS(status_group_lcdc, 2),
  27. };
  28. /* @brief LCD line pattern */
  29. typedef enum lcdc_line_pattern {
  30. lcdc_line_pattern_rgb = 0,
  31. lcdc_line_pattern_rbg,
  32. lcdc_line_pattern_gbr,
  33. lcdc_line_pattern_grb,
  34. lcdc_line_pattern_brg,
  35. lcdc_line_pattern_bgr,
  36. } lcdc_line_pattern_t;
  37. /* @brief LCD display mode */
  38. typedef enum lcdc_display_mode {
  39. lcdc_display_mode_normal = 0,
  40. lcdc_display_mode_test_mode_1,
  41. lcdc_display_mode_test_mode_2,
  42. lcdc_display_mode_test_mode_3,
  43. } lcdc_display_mode_t;
  44. /* @brief LCD layer transfer max bytes */
  45. typedef enum lcdc_layer_max_bytes_per_transfer {
  46. lcdc_layer_max_bytes_64 = 0,
  47. lcdc_layer_max_bytes_128,
  48. lcdc_layer_max_bytes_256,
  49. lcdc_layer_max_bytes_512,
  50. lcdc_layer_max_bytes_1024,
  51. } lcdc_layer_max_bytes_per_transfer_t;
  52. /* @brief LCD control */
  53. typedef struct lcdc_control {
  54. lcdc_line_pattern_t line_pattern; /**< Line pattern setting */
  55. lcdc_display_mode_t display_mode; /**< Display mode setting */
  56. bool invert_pixel_data; /**< Invert pixel data level */
  57. bool invert_pixel_clock; /**< Invert pixel clock level */
  58. bool invert_href; /**< Invert href level */
  59. bool invert_vsync; /**< Invert vsync level */
  60. bool invert_hsync; /**< Invert hsync level */
  61. } lcdc_control_t;
  62. /* @brief LCD hsync/vsync config */
  63. typedef struct lcdc_xsync_config {
  64. uint16_t front_porch_pulse; /**< Front porch pulse */
  65. uint16_t back_porch_pulse; /**< Back porch pulse */
  66. uint16_t pulse_width; /**< Pulse width */
  67. } lcdc_xsync_config_t;
  68. /* @brief LCD config */
  69. typedef struct lcdc_config {
  70. uint16_t resolution_x; /**< Horizontal resolution in pixel */
  71. uint16_t resolution_y; /**< Vertial resolution in pixel */
  72. lcdc_xsync_config_t hsync; /**< Hsync config */
  73. lcdc_xsync_config_t vsync; /**< Vsync config */
  74. display_color_32b_t background; /**< Background color */
  75. lcdc_control_t control; /**< LCD control */
  76. } lcdc_config_t;
  77. /* @brief LCD layer config */
  78. typedef struct lcdc_layer_config {
  79. uint8_t max_ot; /**< Maximum outstanding transfer */
  80. display_byteorder_t byteorder; /**< Byte order */
  81. display_yuv_format_t yuv; /**< YUV format */
  82. display_pixel_format_t pixel_format; /**< Pixel format */
  83. display_alphablend_option_t alphablend; /**< Alphablending option */
  84. display_yuv2rgb_config_t csc_config; /**< Color space conversion config */
  85. lcdc_layer_max_bytes_per_transfer_t max_bytes; /**< Layer max transfer bytes */
  86. uint16_t height; /**< Layer height in pixel */
  87. uint16_t width; /**< Layer width in pixel */
  88. uint16_t position_x; /**< Layer output position X coord */
  89. uint16_t position_y; /**< Layer output position Y coord */
  90. display_color_32b_t background; /**< Background color */
  91. uint32_t buffer; /**< Pointer of layer display buffer */
  92. } lcdc_layer_config_t;
  93. #ifdef __cplusplus
  94. extern "C" {
  95. #endif
  96. /**
  97. *
  98. * @brief Layer config
  99. *
  100. * @param[in] ptr LCD base address
  101. */
  102. static inline void lcdc_software_reset(LCDC_Type *ptr)
  103. {
  104. ptr->CTRL |= LCDC_CTRL_SW_RST_MASK;
  105. ptr->CTRL &= ~LCDC_CTRL_SW_RST_MASK;
  106. }
  107. /**
  108. *
  109. * @brief Enable interrupt according to the given mask
  110. *
  111. * @param[in] ptr LCD base address
  112. * @param[in] interrupt_mask Mask of interrupts to be enabled
  113. */
  114. static inline void lcdc_enable_interrupt(LCDC_Type *ptr, uint32_t interrupt_mask)
  115. {
  116. ptr->INT_EN |= interrupt_mask;
  117. }
  118. /**
  119. *
  120. * @brief Disable interrupt according to the given mask
  121. *
  122. * @param[in] ptr LCD base address
  123. * @param[in] interrupt_mask Mask of interrupts to be disabled
  124. */
  125. static inline void lcdc_disable_interrupt(LCDC_Type *ptr, uint32_t interrupt_mask)
  126. {
  127. ptr->INT_EN &= ~interrupt_mask;
  128. }
  129. /**
  130. *
  131. * @brief Clear specific status according to the given mask
  132. *
  133. * @param[in] ptr LCD base address
  134. * @param[in] mask Status mask of status to be cleared
  135. */
  136. static inline void lcdc_clear_status(LCDC_Type *ptr, uint32_t mask)
  137. {
  138. ptr->ST |= mask;
  139. }
  140. /**
  141. *
  142. * @brief Make layer control shadow registers take effect
  143. *
  144. * @param[in] ptr LCD base address
  145. * @param[in] layer_index Index of layer to be controlled
  146. */
  147. static inline bool lcdc_layer_control_shadow_loaded(LCDC_Type *ptr, uint8_t layer_index)
  148. {
  149. return !(ptr->LAYER[layer_index].LAYCTRL & LCDC_LAYER_LAYCTRL_SHADOW_LOAD_EN_MASK);
  150. }
  151. /**
  152. *
  153. * @brief Get DMA status
  154. *
  155. * @param[in] ptr LCD base address
  156. * @retval DMA status
  157. */
  158. static inline uint32_t lcdc_get_dma_status(LCDC_Type *ptr)
  159. {
  160. return ptr->DMA_ST;
  161. }
  162. /**
  163. *
  164. * @brief Check DMA status against the given mask
  165. *
  166. * @param[in] ptr LCD base address
  167. * @param[in] mask Mask of expected DMA status
  168. * @retval true if all bits set to 1 in mask are set
  169. * @retval false if any bit set to 1 in mask is not set
  170. */
  171. static inline bool lcdc_check_dma_status(LCDC_Type *ptr, uint32_t mask)
  172. {
  173. return ((ptr->DMA_ST & mask) == mask);
  174. }
  175. /**
  176. *
  177. * @brief Clear DMA status according to the given mask
  178. *
  179. * @param[in] ptr LCD base address
  180. * @param[in] mask Mask of expected DMA status
  181. */
  182. static inline void lcdc_clear_dma_status(LCDC_Type *ptr, uint32_t mask)
  183. {
  184. ptr->DMA_ST |= mask;
  185. }
  186. /**
  187. *
  188. * @brief Get status
  189. *
  190. * @param[in] ptr LCD base address
  191. * @retval current status
  192. */
  193. static inline uint32_t lcdc_get_status(LCDC_Type *ptr)
  194. {
  195. return ptr->ST;
  196. }
  197. /**
  198. *
  199. * @brief Check status against the given mask
  200. *
  201. * @param[in] ptr LCD base address
  202. * @param[in] mask Mask of expected status
  203. * @retval true if all bits set to 1 in mask are set
  204. * @retval false if any bit set to 1 in mask is not set
  205. */
  206. static inline bool lcdc_check_status(LCDC_Type *ptr, uint32_t mask)
  207. {
  208. return (ptr->ST & mask) == mask;
  209. }
  210. /**
  211. *
  212. * @brief Set next buffer for certain layer
  213. *
  214. * @param[in] ptr LCD base address
  215. * @param[in] layer_index target layer to be configured
  216. * @param[in] buffer display buffer to be set
  217. */
  218. static inline void lcdc_layer_set_next_buffer(LCDC_Type *ptr, uint32_t layer_index, uint32_t buffer)
  219. {
  220. ptr->LAYER[layer_index].START0 = LCDC_LAYER_START0_ADDR0_SET(buffer);
  221. ptr->LAYER[layer_index].LAYCTRL |= LCDC_LAYER_LAYCTRL_SHADOW_LOAD_EN_MASK;
  222. }
  223. /**
  224. *
  225. * @brief Update specific layer background
  226. *
  227. * @param[in] ptr LCD base address
  228. * @param[in] layer_index target layer to be configured
  229. * @param[in] background color to be set as background
  230. */
  231. static inline void lcdc_layer_update_background(LCDC_Type *ptr,
  232. uint8_t layer_index, display_color_32b_t background)
  233. {
  234. ptr->LAYER[layer_index].BG_CL = LCDC_LAYER_BG_CL_ARGB_SET(background.u);
  235. ptr->LAYER[layer_index].LAYCTRL |= LCDC_LAYER_LAYCTRL_SHADOW_LOAD_EN_MASK;
  236. }
  237. /**
  238. *
  239. * @brief Update specific layer position
  240. *
  241. * @param[in] ptr LCD base address
  242. * @param[in] layer_index target layer to be configured
  243. * @param[in] x Position X coord
  244. * @param[in] y Position Y coord
  245. */
  246. static inline void lcdc_layer_update_position(LCDC_Type *ptr,
  247. uint8_t layer_index, uint16_t x, uint32_t y)
  248. {
  249. ptr->LAYER[layer_index].LAYPOS = LCDC_LAYER_LAYPOS_X_SET(x)
  250. | LCDC_LAYER_LAYPOS_Y_SET(y);
  251. ptr->LAYER[layer_index].LAYCTRL |= LCDC_LAYER_LAYCTRL_SHADOW_LOAD_EN_MASK;
  252. }
  253. /**
  254. *
  255. * @brief Update specific layer dimension
  256. *
  257. * @param[in] ptr LCD base address
  258. * @param[in] layer_index target layer to be configured
  259. * @param[in] width Width in pixel
  260. * @param[in] height Height in pixel
  261. */
  262. static inline void lcdc_layer_update_dimension(LCDC_Type *ptr,
  263. uint8_t layer_index, uint8_t width, uint8_t height)
  264. {
  265. ptr->LAYER[layer_index].LAYSIZE = LCDC_LAYER_LAYSIZE_WIDTH_SET(width)
  266. | LCDC_LAYER_LAYSIZE_HEIGHT_SET(height);
  267. ptr->LAYER[layer_index].LAYCTRL |= LCDC_LAYER_LAYCTRL_SHADOW_LOAD_EN_MASK;
  268. }
  269. /**
  270. *
  271. * @brief Update specific layer region
  272. *
  273. * @param[in] ptr LCD base address
  274. * @param[in] layer_index target layer to be configured
  275. * @param[in] x1 X coord of the top left pixel
  276. * @param[in] y1 Y coord of the top left pixel
  277. * @param[in] x2 X coord of the bottom right pixel
  278. * @param[in] y2 Y coord of the bottom right pixel
  279. */
  280. static inline void lcdc_layer_set_region(LCDC_Type *ptr, uint8_t layer_index,
  281. uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2)
  282. {
  283. ptr->LAYER[layer_index].LAYPOS = LCDC_LAYER_LAYPOS_X_SET(x1)
  284. | LCDC_LAYER_LAYPOS_Y_SET(y1);
  285. ptr->LAYER[layer_index].LAYSIZE = LCDC_LAYER_LAYSIZE_WIDTH_SET(x2 - x1 + 1)
  286. | LCDC_LAYER_LAYSIZE_HEIGHT_SET(y2 - y1 + 1);
  287. ptr->LAYER[layer_index].LAYCTRL |= LCDC_LAYER_LAYCTRL_SHADOW_LOAD_EN_MASK;
  288. }
  289. /**
  290. *
  291. * @brief Update specific layer configuration
  292. *
  293. * @param[in] ptr LCD base address
  294. * @param[in] layer_index target layer to be configured
  295. */
  296. static inline void lcdc_layer_update(LCDC_Type *ptr, uint8_t layer_index)
  297. {
  298. ptr->LAYER[layer_index].LAYCTRL |= LCDC_LAYER_LAYCTRL_SHADOW_LOAD_EN_MASK;
  299. }
  300. /**
  301. *
  302. * @brief Enable specific layer
  303. *
  304. * @param[in] ptr LCD base address
  305. * @param[in] layer_index target layer to be configured
  306. */
  307. static inline void lcdc_layer_enable(LCDC_Type *ptr, uint32_t layer_index)
  308. {
  309. ptr->LAYER[layer_index].LAYCTRL |=
  310. (LCDC_LAYER_LAYCTRL_EN_MASK | LCDC_LAYER_LAYCTRL_SHADOW_LOAD_EN_MASK);
  311. }
  312. /**
  313. *
  314. * @brief Disable specific layer
  315. *
  316. * @param[in] ptr LCD base address
  317. * @param[in] layer_index target layer to be configured
  318. */
  319. static inline void lcdc_layer_disable(LCDC_Type *ptr, uint32_t layer_index)
  320. {
  321. ptr->LAYER[layer_index].LAYCTRL =
  322. (ptr->LAYER[layer_index].LAYCTRL & (~LCDC_LAYER_LAYCTRL_EN_MASK))
  323. | LCDC_LAYER_LAYCTRL_SHADOW_LOAD_EN_MASK;
  324. }
  325. /**
  326. *
  327. * @brief Set test mode
  328. *
  329. * @param[in] ptr LCD base address
  330. * @param[in] test_mode target test mode to be enabled
  331. */
  332. static inline void lcdc_set_testmode(LCDC_Type *ptr, uint8_t test_mode)
  333. {
  334. ptr->CTRL = ((ptr->CTRL & ~LCDC_CTRL_DISP_MODE_MASK))
  335. | LCDC_CTRL_DISP_MODE_SET(test_mode)
  336. | LCDC_CTRL_DISP_ON_MASK;
  337. }
  338. /**
  339. *
  340. * @brief Set background
  341. *
  342. * @param[in] ptr LCD base address
  343. * @param[in] color background color
  344. */
  345. static inline void lcdc_set_background(LCDC_Type *ptr,
  346. display_color_32b_t color)
  347. {
  348. ptr->BGND_CL = LCDC_BGND_CL_R_SET(color.r)
  349. | LCDC_BGND_CL_G_SET(color.g)
  350. | LCDC_BGND_CL_B_SET(color.b);
  351. }
  352. /**
  353. *
  354. * @brief Get default layer configuration value
  355. *
  356. * @param[in] ptr LCD base address
  357. * @param[out] layer Pointer of layer configuration struct buffer
  358. * @param[in] pixel_format Pixel format to be used for this layer
  359. * @param[in] layer_index target layer to be configured
  360. */
  361. void lcdc_get_default_layer_config(LCDC_Type *ptr,
  362. lcdc_layer_config_t *layer, display_pixel_format_t pixel_format, uint8_t layer_index);
  363. /**
  364. *
  365. * @brief Get default configuration value
  366. *
  367. * @param[in] ptr LCD base address
  368. * @param[out] config Pointer of configuration struct buffer
  369. */
  370. void lcdc_get_default_config(LCDC_Type *ptr, lcdc_config_t *config);
  371. /**
  372. *
  373. * @brief Initialize LCD controller
  374. *
  375. * @param[in] ptr LCD base address
  376. * @param[in] config Pointer of configuration struct buffer
  377. */
  378. void lcdc_init(LCDC_Type *ptr, lcdc_config_t *config);
  379. /**
  380. *
  381. * @brief Configure specific layer
  382. *
  383. * @param[in] ptr LCD base address
  384. * @param[in] layer_index target layer to be configured
  385. * @param[in] layer_config Pointer of layer configuration struct buffer
  386. * @param[in] enable_layer Set true if the layer needs to be enabled right after being configured
  387. */
  388. hpm_stat_t lcdc_config_layer(LCDC_Type *ptr, uint8_t layer_index,
  389. lcdc_layer_config_t *layer_config, bool enable_layer);
  390. /**
  391. *
  392. * @brief Turn on display
  393. *
  394. * @param[in] ptr LCD base address
  395. */
  396. void lcdc_turn_on_display(LCDC_Type *ptr);
  397. /**
  398. *
  399. * @brief Turn off display
  400. *
  401. * @param[in] ptr LCD base address
  402. */
  403. void lcdc_turn_off_display(LCDC_Type *ptr);
  404. #ifdef __cplusplus
  405. }
  406. #endif
  407. /**
  408. * @}
  409. */
  410. #endif /* HPM_LCDC_DRV_H */