dma2d_hal.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. /*
  2. * Copyright (c) 2020, Actions Semi Co., Inc.
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. /**
  7. * @file
  8. * @brief DMA2D Public API
  9. */
  10. /* Define to prevent recursive inclusion -------------------------------------*/
  11. #ifndef FRAMEWORK_DMA2D_HAL_H_
  12. #define FRAMEWORK_DMA2D_HAL_H_
  13. #include <zephyr.h>
  14. #include <sys/atomic.h>
  15. #include <drivers/display/display_engine.h>
  16. /**
  17. * @brief Display DMA2D Interface
  18. * @defgroup display_dma2d_interface Display DMA2D Interface
  19. * @ingroup display_libraries
  20. * @{
  21. */
  22. #ifdef __cplusplus
  23. extern "C" {
  24. #endif
  25. #define HAL_DMA2D_MAX_LAYER 2U /*!< DMA2D maximum number of layers */
  26. /**
  27. * @brief DMA2D output structure definition
  28. */
  29. typedef struct {
  30. uint16_t mode; /*!< Configures the DMA2D transfer mode.
  31. This parameter can be one value of @ref DMA2D_MODE. */
  32. uint16_t output_offset; /*!< Specifies the output offset value.
  33. This parameter must be a number between Min_Data = 0x0000 and Max_Data = 0x01FF. */
  34. uint16_t color_mode; /*!< Configures the color format of the output image.
  35. This parameter can be one value of @ref DMA2D_COLOR_MODE. */
  36. uint16_t rb_swap; /*!< Select regular mode (RGB or ARGB) or swap mode (BGR or ABGR)
  37. for the output pixel format converter.
  38. his parameter can be one value of @ref HAL_DMA2D_RB_SWAP. */
  39. } hal_dma2d_output_cfg_t;
  40. /**
  41. * @brief DMA2D Layer structure definition
  42. */
  43. typedef struct {
  44. uint16_t input_offset; /*!< Configures the DMA2D foreground or background offset.
  45. This parameter must be a number between Min_Data = 0x0000 and Max_Data = 0x3FFF. */
  46. uint16_t color_mode; /*!< Configures the DMA2D foreground or background color mode.
  47. This parameter can be one value of @ref DMA2D_COLOR_MODE. */
  48. uint16_t rb_swap; /*!< Select regular mode (RGB or ARGB) or swap mode (BGR or ABGR).
  49. This parameter can be one value of @ref HAL_DMA2D_RB_SWAP. */
  50. uint16_t alpha_mode; /*!< Configures the DMA2D foreground or background alpha mode.
  51. This parameter can be one value of @ref DMA2D_ALPHA_Mode. */
  52. uint32_t input_alpha; /*!< Specifies the DMA2D foreground or background alpha value and color value in case of A8 or A4 color mode.
  53. This parameter must be a number between Min_Data = 0x00 and Max_Data = 0xFF except for the color modes detailed below.
  54. @note In case of A8 or A4 color mode (ARGB), this parameter must be a number between
  55. Min_Data = 0x00000000 and Max_Data = 0xFFFFFFFF where
  56. - input_alpha[24:31] is the alpha value ALPHA[0:7]
  57. - input_alpha[16:23] is the red value RED[0:7]
  58. - input_alpha[8:15] is the green value GREEN[0:7]
  59. - input_alpha[0:7] is the blue value BLUE[0:7]. */
  60. } hal_dma2d_layer_cfg_t;
  61. /**
  62. * @brief DMA2D Rotation structure definition
  63. */
  64. typedef struct {
  65. uint16_t input_offset; /*!< Configures the DMA2D rotation input offset.
  66. This parameter must be a number between Min_Data = 0x0000 and Max_Data = 0x3FFF. */
  67. uint16_t color_mode; /*!< Configures the DMA2D rotation input color mode.
  68. This parameter can be one value of @ref DMA2D_COLOR_MODE. */
  69. uint16_t rb_swap; /*!< Select regular mode (RGB or ARGB) or swap mode (BGR or ABGR).
  70. This parameter can be one value of @ref HAL_DMA2D_RB_SWAP. */
  71. uint16_t outer_diameter; /*!< Outer Diameter in pixels of the ring area of the Source, and must equal to the source image width and height */
  72. uint16_t inner_diameter; /*!< Inner Diameter in pixels of the ring area of the Source */
  73. uint16_t angle; /*!< Rotate angle in 0.1 degree [0, 3600] */
  74. uint16_t fill_enable; /*!< Enable filling color outside the ring area */
  75. uint32_t fill_color; /*!< Fill color (ARGB8888) outside the ring area */
  76. /* Private parameters computed in HAL implementation and passed to device driver */
  77. int32_t src_coord_x0; /* src X coord in .12 fixedpoint mapping to dest coord (0, 0) */
  78. int32_t src_coord_y0; /* src Y coord in .12 fixedpoint mapping to dest coord (0, 0) */
  79. display_engine_rotation_t hw_cfg;
  80. } hal_dma2d_rotation_cfg_t;
  81. /**
  82. * @brief HAL DMA2D State structures definition
  83. */
  84. typedef enum {
  85. HAL_DMA2D_STATE_RESET = 0x00U, /*!< DMA2D not yet initialized or disabled */
  86. HAL_DMA2D_STATE_READY = 0x01U, /*!< Peripheral Initialized and ready for use */
  87. HAL_DMA2D_STATE_BUSY = 0x02U, /*!< An internal process is ongoing */
  88. HAL_DMA2D_STATE_TIMEOUT = 0x03U, /*!< timeout state */
  89. HAL_DMA2D_STATE_ERROR = 0x04U, /*!< DMA2D state error */
  90. } hal_dma2d_state_e;
  91. struct _hal_dma2d_handle;
  92. /**
  93. * @brief HAL DMA2D Callback pointer definition provided the current command sequence
  94. */
  95. typedef void (* hal_dma2d_callback_t)(struct _hal_dma2d_handle * hdma2d, uint16_t cmd_seq); /*!< Pointer to a DMA2D common callback function */
  96. /**
  97. * @brief DMA2D handle Structure definition
  98. */
  99. typedef struct _hal_dma2d_handle {
  100. int instance; /*!< DMA2D instance ID */
  101. atomic_t xfer_count; /*!< DMA2D pending transfer count */
  102. hal_dma2d_callback_t xfer_cplt_callback; /*!< DMA2D transfer complete callback. */
  103. hal_dma2d_callback_t xfer_error_callback; /*!< DMA2D transfer error callback. */
  104. hal_dma2d_output_cfg_t output_cfg; /*!< DMA2D output parameters. */
  105. hal_dma2d_layer_cfg_t layer_cfg[HAL_DMA2D_MAX_LAYER]; /*!< DMA2D Layers parameters */
  106. hal_dma2d_rotation_cfg_t rotation_cfg; /*!< DMA2D Rotation parameters */
  107. uint32_t error_code; /*!< DMA2D error code. */
  108. } hal_dma2d_handle_t;
  109. /**
  110. * @brief HAL DMA2D_Layers DMA2D Layers
  111. */
  112. #define HAL_DMA2D_BACKGROUND_LAYER 0x0000U /*!< DMA2D Background Layer (layer 0) */
  113. #define HAL_DMA2D_FOREGROUND_LAYER 0x0001U /*!< DMA2D Foreground Layer (layer 1) */
  114. /**
  115. * @brief HAL DMA2D_Offset DMA2D Offset
  116. */
  117. #define HAL_DMA2D_OFFSET 511U /*!< maximum Line Offset */
  118. /**
  119. * @brief HAL DMA2D_Size DMA2D Size
  120. */
  121. #define HAL_DMA2D_PIXEL 512U /*!< DMA2D maximum number of pixels per line */
  122. #define HAL_DMA2D_LINE 512U /*!< DMA2D maximum number of lines */
  123. /**
  124. * @brief HAL DMA2D_Error_Code DMA2D Error Code
  125. */
  126. #define HAL_DMA2D_ERROR_NONE 0x0000U /*!< No error */
  127. #define HAL_DMA2D_ERROR_TE 0x0001U /*!< Transfer error */
  128. #define HAL_DMA2D_ERROR_CE 0x0002U /*!< Configuration error */
  129. #define HAL_DMA2D_ERROR_TIMEOUT 0x0020U /*!< Timeout error */
  130. #define HAL_DMA2D_ERROR_INVALID_CALLBACK 0x0040U /*!< Invalid callback error */
  131. /**
  132. * @brief HAL DMA2D_MODE DMA2D Mode
  133. */
  134. #define HAL_DMA2D_R2M 0x0000U /*!< DMA2D register to memory transfer mode */
  135. #define HAL_DMA2D_M2M 0x0001U /*!< DMA2D memory to memory transfer mode, optionally with pixel format conversion */
  136. #define HAL_DMA2D_M2M_BLEND 0x0002U /*!< DMA2D memory to memory with blending transfer mode */
  137. #define HAL_DMA2D_M2M_BLEND_FG 0x0004U /*!< DMA2D memory to memory with blending transfer mode and fixed color FG */
  138. #define HAL_DMA2D_M2M_BLEND_BG 0x0008U /*!< DMA2D memory to memory with blending transfer mode and fixed color BG */
  139. #define HAL_DMA2D_M2M_ROTATE 0x0010U /*!< DMA2D memory to memory with rotation transfer mode */
  140. /**
  141. * @brief HAL DMA2D_COLOR_MODE DMA2D Color Mode
  142. */
  143. #define HAL_DMA2D_ARGB8888 0x0000U /*!< ARGB8888 color mode */
  144. #define HAL_DMA2D_ARGB6666 0x0001U /*!< ARGB6666 color mode */
  145. #define HAL_DMA2D_RGB888 0x0002U /*!< RGB888 color mode */
  146. #define HAL_DMA2D_RGB565 0x0004U /*!< RGB565 color mode */
  147. #define HAL_DMA2D_A8 0x0008U /*!< A8 color mode */
  148. #define HAL_DMA2D_A4 0x0010U /*!< A4 color mode */
  149. #define HAL_DMA2D_A1 0x0020U /*!< A1 color mode */
  150. #define HAL_DMA2D_A4_LE 0x0040U /*!< A4 (little endian) color mode */
  151. #define HAL_DMA2D_A1_LE 0x0080U /*!< A1 (little endian) color mode */
  152. #define HAL_DMA2D_RGB565_LE 0x0100U /*!< RGB565 (little endian) color mode */
  153. /**
  154. * @brief HAL DMA2D_Alpha_Mode DMA2D Alpha Mode
  155. */
  156. #define HAL_DMA2D_NO_MODIF_ALPHA 0x0000U /*!< No modification of the alpha channel value */
  157. #define HAL_DMA2D_REPLACE_ALPHA 0x0001U /*!< Replace original alpha channel value by programmed alpha value */
  158. #define HAL_DMA2D_COMBINE_ALPHA 0x0002U /*!< Replace original alpha channel value by programmed alpha value
  159. with original alpha channel value */
  160. /**
  161. * @brief HAL DMA2D_RB_Swap DMA2D Red and Blue Swap
  162. */
  163. #define HAL_DMA2D_RB_REGULAR 0x0000U /*!< Select regular mode (RGB or ARGB) */
  164. #define HAL_DMA2D_RB_SWAP 0x0001U /*!< Select swap mode (BGR or ABGR) */
  165. /**
  166. * @brief HAL DMA2D common Callback ID enumeration definition
  167. */
  168. typedef enum {
  169. HAL_DMA2D_TRANSFERCOMPLETE_CB_ID = 0x00U, /*!< DMA2D transfer complete callback ID */
  170. HAL_DMA2D_TRANSFERERROR_CB_ID = 0x01U, /*!< DMA2D transfer error callback ID */
  171. } hal_dma2d_callback_e;
  172. /* Exported functions --------------------------------------------------------*/
  173. /* Initialization and de-initialization functions *******************************/
  174. /**
  175. * @brief Initialize the DMA2D peripheral and create the associated handle.
  176. * @param hdma2d pointer to a hal_dma2d_handle_t structure that contains
  177. * the configuration information for the DMA2D.
  178. * @retval 0 on success else negative errno code.
  179. */
  180. int hal_dma2d_init(hal_dma2d_handle_t *hdma2d);
  181. /**
  182. * @brief Deinitializes the DMA2D peripheral registers to their default reset
  183. * values.
  184. * @param hdma2d pointer to a hal_dma2d_handle_t structure that contains
  185. * the configuration information for the DMA2D.
  186. * @retval 0 on success else negative errno code.
  187. */
  188. int hal_dma2d_deinit(hal_dma2d_handle_t *hdma2d);
  189. /* Callbacks Register/UnRegister functions ***********************************/
  190. /**
  191. * @brief Register a User DMA2D Callback
  192. * To be used instead of the weak (surcharged) predefined callback
  193. * @param hdma2d DMA2D handle
  194. * @param callback_id ID of the callback to be registered
  195. * This parameter can be one of the following values:
  196. * @arg @ref HAL_DMA2D_TRANSFERCOMPLETE_CB_ID DMA2D transfer complete Callback ID
  197. * @arg @ref HAL_DMA2D_TRANSFERERROR_CB_ID DMA2D transfer error Callback ID
  198. * @param callback_fn pointer to the callback function
  199. * @retval 0 on success else negative errno code.
  200. */
  201. int hal_dma2d_register_callback(hal_dma2d_handle_t *hdma2d, hal_dma2d_callback_e callback_id, hal_dma2d_callback_t callback_fn);
  202. /**
  203. * @brief Unregister a DMA2D Callback
  204. * DMA2D Callback is redirected to the weak (surcharged) predefined callback
  205. * @param hdma2d DMA2D handle
  206. * @param callback_id ID of the callback to be unregistered
  207. * This parameter can be one of the following values:
  208. * @arg @ref HAL_DMA2D_TRANSFERCOMPLETE_CB_ID DMA2D transfer complete Callback ID
  209. * @arg @ref HAL_DMA2D_TRANSFERERROR_CB_ID DMA2D transfer error Callback ID
  210. * @retval 0 on success else negative errno code.
  211. */
  212. int hal_dma2d_unregister_callback(hal_dma2d_handle_t *hdma2d, hal_dma2d_callback_e callback_id);
  213. /* IO operation functions *******************************************************/
  214. /**
  215. * @brief Start the DMA2D Transfer.
  216. * @param hdma2d Pointer to a hal_dma2d_handle_t structure that contains
  217. * the configuration information for the DMA2D.
  218. * @param pdata Configure the source memory Buffer address if
  219. * Memory-to-Memory or Memory-to-Memory with pixel format
  220. * conversion mode is selected, or configure
  221. * the color value if Register-to-Memory mode is selected.
  222. * @param dst_address The destination memory Buffer address.
  223. * @param width The width of data to be transferred from source to destination (expressed in number of pixels per line).
  224. * @param height The height of data to be transferred from source to destination (expressed in number of lines).
  225. * @retval command sequence (uint16_t) on success else negative errno code.
  226. */
  227. int hal_dma2d_start(hal_dma2d_handle_t *hdma2d, uint32_t pdata, uint32_t dst_address, uint16_t width, uint16_t height);
  228. /**
  229. * @brief Start the multi-source DMA2D Transfer.
  230. * @param hdma2d Pointer to a hal_dma2d_handle_t structure that contains
  231. * the configuration information for the DMA2D.
  232. * @param src_address1 The source memory Buffer address for the foreground layer.
  233. * @param src_address2 The source memory Buffer address for the background layer.
  234. * @param dst_address The destination memory Buffer address.
  235. * @param width The width of data to be transferred from source to destination (expressed in number of pixels per line).
  236. * @param height The height of data to be transferred from source to destination (expressed in number of lines).
  237. * @retval command sequence (uint16_t) on success else negative errno code.
  238. */
  239. int hal_dma2d_blending_start(hal_dma2d_handle_t *hdma2d, uint32_t src_address1, uint32_t src_address2, uint32_t dst_address, uint16_t width, uint16_t height);
  240. /**
  241. * @brief Start the DMA2D Rotation Transfer with interrupt enabled.
  242. *
  243. * The source size must be square, and only the ring area defined in hal_dma2d_rotation_cfg_t
  244. * is rotated, and the pixels inside the inner ring can be filled with constant
  245. * color defined in hal_dma2d_rotation_cfg_t.
  246. *
  247. * @param hdma2d Pointer to a hal_dma2d_handle_t structure that contains
  248. * the configuration information for the DMA2D.
  249. * @param src_address The source memory Buffer start address
  250. * @param dst_address The destination memory Buffer address.
  251. * @param start_line The start line to rotate.
  252. * @param num_lines Number of lines to rotate.
  253. * @retval command sequence (uint16_t) on success else negative errno code.
  254. */
  255. int hal_dma2d_rotation_start(hal_dma2d_handle_t *hdma2d, uint32_t src_address, uint32_t dst_address, uint16_t start_line, uint16_t num_lines);
  256. /**
  257. * @brief Polling for transfer complete.
  258. * @param hdma2d Pointer to a hal_dma2d_handle_t structure that contains
  259. * the configuration information for the DMA2D.
  260. * @param timeout timeout duration in milliseconds, if negative, means wait forever
  261. * @retval 0 on success else negative errno code.
  262. */
  263. int hal_dma2d_poll_transfer(hal_dma2d_handle_t *hdma2d, int32_t timeout);
  264. /* Peripheral Control functions *************************************************/
  265. /**
  266. * @brief Configure the DMA2D transfer mode and output according to the
  267. * specified parameters in the hal_dma2d_handle_t.
  268. * @param hdma2d Pointer to a hal_dma2d_handle_t structure that contains
  269. * the configuration information for the DMA2D.
  270. * @retval 0 on success else negative errno code.
  271. */
  272. int hal_dma2d_config_output(hal_dma2d_handle_t *hdma2d);
  273. /**
  274. * @brief Configure the DMA2D Layer according to the specified
  275. * parameters in the hal_dma2d_handle_t.
  276. * @param hdma2d Pointer to a hal_dma2d_handle_t structure that contains
  277. * the configuration information for the DMA2D.
  278. * @param layer_idx DMA2D Layer index.
  279. * This parameter can be one of the following values:
  280. * HAL_DMA2D_BACKGROUND_LAYER(0) / HAL_DMA2D_FOREGROUND_LAYER(1)
  281. * @retval 0 on success else negative errno code.
  282. */
  283. int hal_dma2d_config_layer(hal_dma2d_handle_t *hdma2d, uint16_t layer_idx);
  284. /**
  285. * @brief Configure the DMA2D Rotation according to the specified
  286. * parameters in the hal_dma2d_handle_t.
  287. * @param hdma2d Pointer to a hal_dma2d_handle_t structure that contains
  288. * the configuration information for the DMA2D.
  289. * @retval 0 on success else negative errno code.
  290. */
  291. int hal_dma2d_config_rotation(hal_dma2d_handle_t *hdma2d);
  292. /* Peripheral State functions ***************************************************/
  293. /**
  294. * @brief Return the DMA2D state
  295. * @param hdma2d pointer to a hal_dma2d_handle_t structure that contains
  296. * the configuration information for the DMA2D.
  297. * @retval HAL state
  298. */
  299. hal_dma2d_state_e hal_dma2d_get_state(hal_dma2d_handle_t *hdma2d);
  300. /**
  301. * @brief Return the DMA2D error code
  302. * @param hdma2d pointer to a hal_dma2d_handle_t structure that contains
  303. * the configuration information for DMA2D.
  304. * @retval DMA2D Error Code
  305. */
  306. uint32_t hal_dma2d_get_error(hal_dma2d_handle_t *hdma2d);
  307. /**
  308. * @cond INTERNAL_HIDDEN
  309. */
  310. /**
  311. * @brief Global enable/disable DMAD functions
  312. * @param enabled enable or not
  313. * @retval N/A
  314. */
  315. void hal_dma2d_set_global_enabled(bool enabled);
  316. /**
  317. * INTERNAL_HIDDEN @endcond
  318. */
  319. #ifdef __cplusplus
  320. }
  321. #endif
  322. /**
  323. * @}
  324. */
  325. #endif /* FRAMEWORK_DMA2D_HAL_H_ */