1 /*
2 * Copyright (c) 2021 HPMicro
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 *
6 */
7
8 #ifndef HPM_JPEG_DRV_H
9 #define HPM_JPEG_DRV_H
10
11 #include "hpm_common.h"
12 #include "hpm_jpeg_regs.h"
13
14 /**
15 * @brief Jpeg driver APIs
16 * @defgroup Jpeg_interface JPEG driver APIs
17 * @ingroup io_interfaces
18 * @{
19 */
20
21 /**
22 * @brief Define events of the jpeg module
23 */
24 #define JPEG_EVENT_BUSY JPEG_STAT_BUSY_MASK
25 #define JPEG_EVENT_OUT_DMA_FINISH JPEG_STAT_OUT_DMA_TRANSFER_DONE_MASK
26 #define JPEG_EVENT_IN_DMA_FINISH JPEG_STAT_IN_DMA_TRANSFER_DONE_MASK
27 #define JPEG_EVENT_ERROR (JPEG_STAT_RESTART_MARKER_ERROR_MASK | (0xF << 7))
28
29 /**
30 * @brief byte order in a word
31 */
32 #define JPEG_BYTE_ORDER_3210 (0U) /**< no order change, {A3, A2, A1, A0} */
33 #define JPEG_BYTE_ORDER_2301 (1U) /**< order change, {A2, A3, A0, A1} */
34 #define JPEG_BYTE_ORDER_1032 (2U) /**< order change, {A1, A0, A2, A3} */
35 #define JPEG_BYTE_ORDER_0123 (3U) /**< order change, {A0, A1, A2, A3} */
36
37 /**
38 * @brief jpeg pixel conversion format
39 */
40
41
42 /**
43 * @brief jpeg data format definition
44 */
45 #define JPEG_SUPPORTED_FORMAT_420 (0U) /**< hy=2, vy=2, hc=1, vc=1 */
46 #define JPEG_SUPPORTED_FORMAT_422H (1U) /**< hy=2, vy=1, hc=1, vc=1 */
47 #define JPEG_SUPPORTED_FORMAT_422V (2U) /**< hy=1, vy=2, hc=1, vc=1 */
48 #define JPEG_SUPPORTED_FORMAT_444 (3U) /**< hy=1, vy=1, hc=1, vc=1 */
49 #define JPEG_SUPPORTED_FORMAT_400 (4U) /**< hy=2, vy=2, hc=0, vc=0 */
50
51 /**
52 * @brief data format definition
53 */
54 typedef struct {
55 uint8_t hy:2; /**< bit: 1-0 --> horizontal y component */
56 uint8_t vy:2; /**< bit: 3-2 --> Vertical y component */
57 uint8_t hc:2; /**< bit: bit: 5-4 --> horizontal c component */
58 uint8_t vc:2; /**< bit: 7-6 --> Vertical c component */
59 } jpeg_sampling_t;
60
61 typedef struct {
62 uint8_t pixel_width;
63 uint8_t ipath;
64 uint8_t opath;
65 bool is_rgb;
66 } jpeg_pixel_t;
67
68 /**
69 * @brief jpeg encoding and decoding configuration parameters
70 * @arg bit: 31-27 --> name
71 * @arg bit: 26-23 --> WIDTH IN BYTE
72 * @arg bit: 22-3 --> ELEMENT COUNT
73 * @arg bit: 2-0 --> TYPE
74 */
75 typedef enum jpeg_table {
76 jpeg_table_qmem = 0x201002, /**< definition Decoder and Encoder Q. values */
77 jpeg_table_huffenc = 0x201803, /**< definition Huffman Encoder table */
78 jpeg_table_huffmin = 0x400104, /**< definition Huffman min values */
79 jpeg_table_huffbase = 0x200405, /**< definition Huffman BASE mem values */
80 jpeg_table_huffsymb = 0x101506, /**< definition Huffman SYMB mem values */
81 } jpeg_table_t;
82
83 typedef enum jpeg_pixel_format {
84 jpeg_pixel_format_argb8888 = 0,
85 jpeg_pixel_format_rgb565,
86 jpeg_pixel_format_yuv422h1p,
87 jpeg_pixel_format_yuv422h2p,
88 jpeg_pixel_format_yuv420,
89 jpeg_pixel_format_y8,
90 } jpeg_pixel_format_t;
91
92 /**
93 * @brief jpeg encoding and decoding configuration parameters
94 */
95 typedef struct {
96 uint8_t jpeg_format; /**< supported jpeg format */
97 jpeg_pixel_format_t in_pixel_format;
98 jpeg_pixel_format_t out_pixel_format;
99 uint8_t in_byte_order; /**< byte order */
100 uint8_t out_byte_order; /**< byte order */
101 bool enable_ycbcr; /**< enable YCbCr or YUV */
102 uint16_t width_in_pixel; /**< Image width register*/
103 uint16_t height_in_pixel; /**< Image height register*/
104 uint32_t in_buffer; /**< input buffer */
105 uint32_t out_buffer; /**< output buffer */
106 } jpeg_job_config_t;
107
108 #ifdef __cplusplus
109 extern "C" {
110 #endif
111
112 /**
113 * @brief clear jpeg cfg Register
114 *
115 * @param [in] ptr JPEG base address, HPM_JPEG
116 */
jpeg_clear_cfg(JPEG_Type * ptr)117 static inline void jpeg_clear_cfg(JPEG_Type *ptr)
118 {
119 ptr->CFG = 0;
120 }
121
122 /**
123 * @brief jpeg function disable
124 *
125 * @param [in] ptr JPEG base address, HPM_JPEG
126 */
jpeg_disable(JPEG_Type * ptr)127 static inline void jpeg_disable(JPEG_Type *ptr)
128 {
129 ptr->CFG &= ~JPEG_CFG_JPEG_EN_MASK;
130 }
131
132 /**
133 * @brief jpeg function enable
134 *
135 * @param [in] ptr JPEG base address, HPM_JPEG
136 */
jpeg_enable(JPEG_Type * ptr)137 static inline void jpeg_enable(JPEG_Type *ptr)
138 {
139 ptr->CFG |= JPEG_CFG_JPEG_EN_MASK;
140 }
141
142 /**
143 * @brief stop a encoder/decoder conversion
144 *
145 * @param [in] ptr JPEG base address, HPM_JPEG
146 */
jpeg_stop(JPEG_Type * ptr)147 static inline void jpeg_stop(JPEG_Type *ptr)
148 {
149 ptr->CFG &= ~JPEG_CFG_START_MASK;
150 }
151
152 /**
153 * @brief start a new encoder/decoder conversion
154 *
155 * @param [in] ptr JPEG base address, HPM_JPEG
156 */
jpeg_start(JPEG_Type * ptr)157 static inline void jpeg_start(JPEG_Type *ptr)
158 {
159 ptr->CFG |= JPEG_CFG_START_MASK;
160 }
161
162 /**
163 * @brief obtain jpeg Status Register
164 *
165 * @param [in] ptr JPEG base address, HPM_JPEG
166 * @retval jpeg register's status
167 */
jpeg_get_status(JPEG_Type * ptr)168 static inline uint32_t jpeg_get_status(JPEG_Type *ptr)
169 {
170 return ptr->STAT;
171 }
172
173 /**
174 * @brief clear jpeg Status Register
175 *
176 * @param [in] ptr JPEG base address, HPM_JPEG
177 * @param [in] mask
178 * @arg JPEG_EVENT_BUSY: the module is busy doing conversion and data transfer
179 * @arg JPEG_EVENT_OUT_DMA_FINISH: OutDMA process done
180 * @arg JPEG_EVENT_IN_DMA_FINISH: InDMA process done
181 * @arg JPEG_EVENT_ERROR: the axi err
182 *
183 */
jpeg_clear_status(JPEG_Type * ptr,uint32_t mask)184 static inline void jpeg_clear_status(JPEG_Type *ptr, uint32_t mask)
185 {
186 ptr->STAT |= mask;
187 }
188
189 /**
190 * @brief Out DMA Bytes Counter
191 *
192 * @param [in] ptr JPEG base address, HPM_JPEG
193 * @retval The out DMA counter
194 */
jpeg_get_encoded_length(JPEG_Type * ptr)195 static inline uint32_t jpeg_get_encoded_length(JPEG_Type *ptr)
196 {
197 return JPEG_OUTDMACNT_VAL_GET(ptr->OUTDMACNT);
198 }
199
200 /**
201 * @brief jpeg Software Reset
202 *
203 * @param [in] ptr JPEG base address, HPM_JPEG
204 */
jpeg_software_reset(JPEG_Type * ptr)205 static inline void jpeg_software_reset(JPEG_Type *ptr)
206 {
207 ptr->CFG |= JPEG_CFG_JPEG_SFTRST_MASK;
208 ptr->CFG &= ~JPEG_CFG_JPEG_SFTRST_MASK;
209 }
210
211 /**
212 * @brief stop a encoder/decoder conversion and Software Reset
213 *
214 * @param [in] ptr JPEG base address, HPM_JPEG
215 */
216 void jpeg_reset(JPEG_Type *ptr);
217
218 /**
219 * @brief jpeg enable interrupt
220 *
221 * @param [in] ptr JPEG base address, HPM_JPEG
222 * @param [in] mask
223 * @arg JPEG_EVENT_IN_DMA_FINISH: In DMA Done enable
224 * @arg JPEG_EVENT_OUT_DMA_FINISH: interrupt enable for all interrupt sources of In DMA module
225 * @arg JPEG_EVENT_ERROR: The jpg endec restart error interrupt enable
226 *
227 */
228 void jpeg_enable_irq(JPEG_Type *ptr, uint32_t mask);
229
230 /**
231 * @brief jpeg disable interrupt
232 *
233 * @param [in] ptr JPEG base address, HPM_JPEG
234 * @param [in] mask
235 * @arg JPEG_EVENT_IN_DMA_FINISH: In DMA Done disable
236 * @arg JPEG_EVENT_OUT_DMA_FINISH: interrupt disable for all interrupt sources of In DMA module
237 * @arg JPEG_EVENT_ERROR: The jpg endec restart error interrupt disable
238 *
239 */
240 void jpeg_disable_irq(JPEG_Type *ptr, uint32_t mask);
241
242 /**
243 * @brief stop a encoder/decoder conversion and Software Reset
244 *
245 * @param [in] ptr JPEG base address, HPM_JPEG
246 */
247 void jpeg_init(JPEG_Type *ptr);
248
249 /**
250 * @brief fill tables for jpeg controller
251 *
252 * @param [in] ptr JPEG base address, HPM_JPEG
253 * @param [in] table
254 * @arg jpeg_table_qmem: file describe for Decoder and Encoder Q. values
255 * @arg jpeg_table_huffenc: file describe for Huffman Encoder table
256 * @arg jpeg_table_huffmin: file describe for Huffman min values
257 * @arg jpeg_table_huffbase: file describe for Huffman BASE mem values
258 * @arg jpeg_table_huffsymb: file describe for Huffman SYMB mem values
259 * @param [in] data
260 * @arg huffenc: data for Huffman Encoder table
261 * @arg huffmin: data for Huffman min values
262 * @arg huffbase: data for Huffman BASE mem values
263 * @arg huffsymb: data for Huffman SYMB mem values
264 * @arg qetable: data for Encoder Q. values
265 * @arg qdtable: data for Decoder Q. values
266 * @param [in] count data length
267 * @retval fill tables's status
268 *
269 */
270 hpm_stat_t jpeg_fill_table(JPEG_Type *ptr, jpeg_table_t table, uint8_t *data, uint32_t count);
271
272 /**
273 * @brief it will start decoding, and the process status needs to be checked by
274 * querying JPEG_EVENT
275 *
276 * @param [in] ptr JPEG base address, HPM_JPEG
277 * @param [in] config config A pointer to the configuration struct of "jpeg_job_config_t"
278 * @param [in] length Decoded data length
279 * @retval jpeg decoding's status
280 *
281 */
282 hpm_stat_t jpeg_start_decode(JPEG_Type *ptr, jpeg_job_config_t *config, uint32_t length);
283
284 /**
285 * @brief * it will start encoding, and the process status needs to be checked by
286 * querying JPEG_EVENT
287 *
288 * @param [in] ptr JPEG base address, HPM_JPEG
289 * @param [in] config config A pointer to the configuration struct of "jpeg_job_config_t"
290 * @retval jpeg encoding's status
291 *
292 */
293 hpm_stat_t jpeg_start_encode(JPEG_Type *ptr, jpeg_job_config_t *config);
294
295 /**
296 * @}
297 *
298 */
299
300 #ifdef __cplusplus
301 }
302 #endif
303 #endif /* HPM_JPEG_DRV_H */
304