hpm_cam_drv.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. /*
  2. * Copyright (c) 2021 HPMicro
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. *
  6. */
  7. #ifndef HPM_CAM_DRV_H
  8. #define HPM_CAM_DRV_H
  9. #include "hpm_common.h"
  10. #include "hpm_display_common.h"
  11. #include "hpm_cam_regs.h"
  12. #include "hpm_soc_feature.h"
  13. /**
  14. * @brief CAM driver APIs
  15. * @defgroup cam_interface CAM driver APIs
  16. * @ingroup io_interfaces
  17. * @{
  18. */
  19. /**
  20. * @brief CAM data store mode
  21. */
  22. #define CAM_DATA_STORE_MODE_NORMAL (0U)
  23. #define CAM_DATA_STORE_MODE_Y_UV_PLANES (CAM_CR1_STORAGE_MODE_SET(1))
  24. #define CAM_DATA_STORE_MODE_Y_ONLY (CAM_CR1_STORAGE_MODE_SET(2))
  25. #define CAM_DATA_STORE_MODE_BINARY (CAM_CR1_STORAGE_MODE_SET(3))
  26. /**
  27. * @brief CAM sensor bitwidth
  28. */
  29. #define CAM_SENSOR_BITWIDTH_8BITS (CAM_CR1_SENSOR_BIT_WIDTH_SET(0))
  30. #define CAM_SENSOR_BITWIDTH_10BITS (CAM_CR1_SENSOR_BIT_WIDTH_SET(1))
  31. #define CAM_SENSOR_BITWIDTH_24BITS (CAM_CR1_SENSOR_BIT_WIDTH_SET(3))
  32. /**
  33. * @brief CAM IRQ mask
  34. */
  35. typedef enum {
  36. cam_irq_unsupported_configuration = CAM_INT_EN_ERR_CL_BWID_CFG_INT_EN_MASK,
  37. cam_irq_hist_calculation_done = CAM_INT_EN_HIST_DONE_INT_EN_MASK,
  38. cam_irq_hresponse_error = CAM_INT_EN_HRESP_ERR_EN_MASK,
  39. cam_irq_end_of_frame = CAM_INT_EN_EOF_INT_EN_MASK,
  40. cam_irq_rx_fifo_overrun = CAM_INT_EN_RF_OR_INTEN_MASK,
  41. cam_irq_fb2_dma_transfer_done = CAM_INT_EN_FB2_DMA_DONE_INTEN_MASK,
  42. cam_irq_fb1_dma_transfer_done = CAM_INT_EN_FB1_DMA_DONE_INTEN_MASK,
  43. cam_irq_start_of_frame = CAM_INT_EN_SOF_INT_EN_MASK
  44. } cam_irq_mask_t;
  45. /**
  46. * @brief CAM status mask
  47. */
  48. typedef enum {
  49. cam_status_unsupported_configuration = CAM_STA_ERR_CL_BWID_CFG_MASK,
  50. cam_status_hist_calculation_done = CAM_STA_HIST_DONE_MASK,
  51. cam_status_rx_fifo_overrun = CAM_STA_RF_OR_INT_MASK,
  52. cam_status_fb2_dma_transfer_done = CAM_STA_DMA_TSF_DONE_FB2_MASK,
  53. cam_status_fb1_dma_transfer_done = CAM_STA_DMA_TSF_DONE_FB1_MASK,
  54. cam_status_end_of_frame = CAM_STA_EOF_INT_MASK,
  55. cam_status_start_of_frame = CAM_STA_SOF_INT_MASK,
  56. cam_status_hresponse_error = CAM_STA_HRESP_ERR_INT_MASK
  57. } cam_status_mask_t;
  58. /**
  59. * @brief CAM input color format
  60. */
  61. #define CAM_COLOR_FORMAT_RGB888 (CAM_CR1_COLOR_FORMATS_SET(2))
  62. #define CAM_COLOR_FORMAT_RGB565 (CAM_CR1_COLOR_FORMATS_SET(4))
  63. #define CAM_COLOR_FORMAT_RGB555 (CAM_CR1_COLOR_FORMATS_SET(6))
  64. #define CAM_COLOR_FORMAT_YCBCR422 (CAM_CR1_COLOR_FORMATS_SET(7))
  65. #define CAM_COLOR_FORMAT_YUV444 (CAM_CR1_COLOR_FORMATS_SET(8))
  66. #define CAM_COLOR_FORMAT_RAW8 (CAM_CR1_COLOR_FORMATS_SET(0xf))
  67. #define CAM_COLOR_FORMAT_UNSUPPORTED (1)
  68. /**
  69. * @brief CAM config
  70. */
  71. typedef struct {
  72. uint32_t width;
  73. uint32_t height;
  74. bool pixclk_sampling_falling;
  75. #if defined(HPM_IP_FEATURE_CAM_INV_DEN) && (HPM_IP_FEATURE_CAM_INV_DEN == 1)
  76. bool de_active_low; /* de_active_low must is same with hsync_active_low when dvp be used */
  77. #endif
  78. bool hsync_active_low;
  79. bool vsync_active_low;
  80. bool color_ext;
  81. bool data_pack_msb;
  82. uint16_t data_store_mode;
  83. uint8_t color_format;
  84. uint8_t sensor_bitwidth;
  85. uint32_t buffer1;
  86. uint32_t buffer2;
  87. display_yuv2rgb_config_t csc_config;
  88. } cam_config_t;
  89. /**
  90. * @brief cam input pixel byte order
  91. */
  92. typedef enum {
  93. cam_input_pixel_yuv444 = 0, /* Y[23:16] U[15:8] V[7:0] */
  94. cam_input_pixel_yvu444 = 1, /* Y[23:16] V[15:8] U[7:0] */
  95. cam_input_pixel_uyv444 = 2, /* U[23:16] Y[15:8] V[7:0] */
  96. cam_input_pixel_vyu444 = 3, /* V[23:16] Y[15:8] U[7:0] */
  97. cam_input_pixel_uvy444 = 4, /* U[23:16] V[15:8] Y[7:0] */
  98. cam_input_pixel_vuy444 = 5, /* V[23:16] U[15:8] Y[7:0] */
  99. cam_input_pixel_yuyv422 = 0, /* Y0[31:24] U0[23:16] Y1[15:8] V0[7:0] */
  100. cam_input_pixel_yvyu422 = 1, /* Y0[31:24] V0[23:16] Y1[15:8] U0[7:0] */
  101. cam_input_pixel_uyvy422 = 2, /* U0[31:24] Y0[23:16] V0[15:8] Y1[7:0] */
  102. cam_input_pixel_vyuy422 = 3, /* V0[31:24] Y0[23:16] U0[15:8] Y1[7:0] */
  103. cam_input_pixel_rgb565 = 0, /* R[15:11] G[10:8] G[7:5] B[4:0] */
  104. cam_input_pixel_bgr565 = 1, /* B[15:11] G[10:8] G[7:5] R[4:0] */
  105. cam_input_pixel_gbr888 = 0, /* G[23:16] B[15:8] R[7:0] */
  106. cam_input_pixel_grb888 = 1, /* G[23:16] R[15:8] B[7:0] */
  107. cam_input_pixel_bgr888 = 2, /* B[23:16] G[15:8] R[7:0] */
  108. cam_input_pixel_rgb888 = 3, /* R[23:16] G[15:8] B[7:0] */
  109. cam_input_pixel_brg888 = 4, /* B[23:16] R[15:8] G[7:0] */
  110. cam_input_pixel_rbg888 = 5, /* R[23:16] B[15:8] G[7:0] */
  111. } cam_input_pixel_byte_order_t;
  112. #ifdef __cplusplus
  113. extern "C" {
  114. #endif
  115. /**
  116. * @brief cam get pixel format value
  117. *
  118. * @param format display_pixel_format_t
  119. * @return uint32_t cam color format, like CAM_COLOR_FORMAT_RGB565
  120. */
  121. static inline uint32_t cam_get_pixel_format(display_pixel_format_t format)
  122. {
  123. switch (format) {
  124. case display_pixel_format_rgb565:
  125. return CAM_COLOR_FORMAT_RGB565;
  126. case display_pixel_format_ycbcr422:
  127. return CAM_COLOR_FORMAT_YCBCR422;
  128. case display_pixel_format_raw8:
  129. return CAM_COLOR_FORMAT_RAW8;
  130. default:
  131. return CAM_COLOR_FORMAT_UNSUPPORTED;
  132. }
  133. }
  134. /**
  135. * @brief CAM set high and low limits of color key
  136. *
  137. * @param [in] ptr CAM base address
  138. * @param [in] high color key high limits
  139. * @param [in] low color key low limits
  140. */
  141. static inline void cam_set_color_key(CAM_Type *ptr, uint32_t high, uint32_t low)
  142. {
  143. ptr->CLRKEY_LOW = CAM_CLRKEY_LOW_LIMIT_SET(low);
  144. ptr->CLRKEY_HIGH = CAM_CLRKEY_HIGH_LIMIT_SET(high);
  145. }
  146. /**
  147. * @brief CAM get default config
  148. *
  149. * @param [in] ptr CAM base address
  150. * @param [out] config cam_config_t
  151. * @param [in] pixel_format display_pixel_format_t
  152. */
  153. void cam_get_default_config(CAM_Type *ptr, cam_config_t *config, display_pixel_format_t pixel_format);
  154. /**
  155. * @brief CAM init
  156. *
  157. * @param [in] ptr CAM base address
  158. * @param [in] config cam_config_t
  159. *
  160. * @retval hpm_stat_t status_invalid_argument or status_success
  161. */
  162. hpm_stat_t cam_init(CAM_Type *ptr, cam_config_t *config);
  163. /**
  164. * @brief CAM start
  165. *
  166. * @param [in] ptr CAM base address
  167. */
  168. void cam_start(CAM_Type *ptr);
  169. /**
  170. * @brief CAM stop
  171. *
  172. * @note this API will stop CAM immediately no matter there's any frame is being processed or not
  173. *
  174. * @param [in] ptr CAM base address
  175. */
  176. void cam_stop(CAM_Type *ptr);
  177. /**
  178. * @brief CAM update DMASA_FB1 buffer
  179. *
  180. * @param [in] ptr CAM base address
  181. * @param [in] buffer buffer point address
  182. */
  183. static inline void cam_update_buffer(CAM_Type *ptr, uint32_t buffer)
  184. {
  185. ptr->DMASA_FB1 = buffer;
  186. }
  187. /**
  188. * @brief CAM update DMASA_FB2 buffer
  189. *
  190. * @param [in] ptr CAM base address
  191. * @param [in] buffer buffer point address
  192. */
  193. static inline void cam_update_buffer2(CAM_Type *ptr, uint32_t buffer)
  194. {
  195. ptr->DMASA_FB2 = buffer;
  196. }
  197. /**
  198. * @brief CAM enable binary output
  199. *
  200. * This function is used to enable CAM binary output after
  201. * the CAM is initialized by the cam_init.
  202. *
  203. * @param [in] ptr CAM base address
  204. */
  205. static inline void cam_enable_binary_output(CAM_Type *ptr)
  206. {
  207. ptr->CR20 |= CAM_CR20_BINARY_EN_MASK;
  208. }
  209. /**
  210. * @brief CAM disable binary output
  211. *
  212. * @param [in] ptr CAM base address
  213. */
  214. static inline void cam_disable_binary_output(CAM_Type *ptr)
  215. {
  216. ptr->CR20 &= ~CAM_CR20_BINARY_EN_MASK;
  217. }
  218. /**
  219. * @brief CAM set binary threshold
  220. *
  221. * @param [in] ptr CAM base address
  222. * @param [in] threshold threshold value of binary output
  223. */
  224. static inline void cam_set_binary_threshold(CAM_Type *ptr, uint8_t threshold)
  225. {
  226. ptr->CR20 = (ptr->CR20 & (~CAM_CR20_THRESHOLD_MASK)) | CAM_CR20_THRESHOLD_SET(threshold);
  227. }
  228. /**
  229. * @brief CAM enable argb8888 output
  230. *
  231. * This function is used to enable CAM argb8888 pixel output after the CAM is initialized by
  232. * the cam_init and input pixel byte order is configured by the cam_set_input_pixel_byte_order.
  233. *
  234. * @param [in] ptr CAM base address
  235. */
  236. static inline void cam_enable_argb8888_output(CAM_Type *ptr)
  237. {
  238. ptr->CR1 |= CAM_CR1_COLOR_EXT_MASK;
  239. }
  240. /**
  241. * @brief CAM disable argb8888 output
  242. *
  243. * @param [in] ptr CAM base address
  244. */
  245. static inline void cam_disable_argb8888_output(CAM_Type *ptr)
  246. {
  247. ptr->CR1 &= ~CAM_CR1_COLOR_EXT_MASK;
  248. }
  249. /**
  250. * @brief CAM set input pixel byte order
  251. *
  252. * @param [in] ptr CAM base address
  253. * @param [in] order cam_input_pixel_byte_order_t
  254. */
  255. static inline void cam_set_input_pixel_byte_order(CAM_Type *ptr, cam_input_pixel_byte_order_t order)
  256. {
  257. ptr->CR2 = (ptr->CR2 & (~CAM_CR2_CLRBITFORMAT_MASK)) | CAM_CR2_CLRBITFORMAT_SET(order);
  258. }
  259. /**
  260. * @brief CAM enable irq
  261. *
  262. * @param [in] ptr CAM base address
  263. * @param [in] irq_mask irq mask value
  264. */
  265. static inline void cam_enable_irq(CAM_Type *ptr, cam_irq_mask_t irq_mask)
  266. {
  267. ptr->INT_EN |= irq_mask;
  268. }
  269. /**
  270. * @brief CAM disable irq
  271. *
  272. * @param [in] ptr CAM base address
  273. * @param [in] irq_mask irq mask value
  274. */
  275. static inline void cam_disable_irq(CAM_Type *ptr, cam_irq_mask_t irq_mask)
  276. {
  277. ptr->INT_EN &= ~irq_mask;
  278. }
  279. /**
  280. * @brief Check CAM status according to the given status mask
  281. *
  282. * @param [in] ptr CAM base address
  283. * @param sta_mask sta_mask refer to cam_status_mask_t
  284. * @retval true if any bit in given mask is set
  285. * @retval false if none of any bit in given mask is set
  286. */
  287. static inline bool cam_check_status(CAM_Type *ptr, cam_status_mask_t sta_mask)
  288. {
  289. return ((ptr->STA & sta_mask) != 0U) ? true : false;
  290. }
  291. /**
  292. * @brief Clear CAM status according to the given status mask
  293. *
  294. * @param [in] ptr CAM base address
  295. * @param sta_mask sta_mask refer to cam_status_mask_t
  296. */
  297. static inline void cam_clear_status(CAM_Type *ptr, cam_status_mask_t sta_mask)
  298. {
  299. ptr->STA = sta_mask;
  300. }
  301. /**
  302. * @brief CAM safety stop
  303. *
  304. * @note this API will wait for end-of-frame event before stopping CAM
  305. *
  306. * @param [in] ptr CAM base address
  307. */
  308. void cam_stop_safely(CAM_Type *ptr);
  309. /**
  310. * @}
  311. *
  312. */
  313. #ifdef __cplusplus
  314. }
  315. #endif
  316. #endif /* HPM_CAM_DRV_H */