1 /**
2 ******************************************************************************
3 * @file stm32n6xx_hal_jpeg.c
4 * @author MCD Application Team
5 * @brief JPEG HAL module driver.
6 * This file provides firmware functions to manage the following
7 * functionalities of the JPEG encoder/decoder peripheral:
8 * + Initialization and de-initialization functions
9 * + JPEG processing functions encoding and decoding
10 * + JPEG decoding Getting Info and encoding configuration setting
11 * + JPEG enable/disable header parsing functions (for decoding)
12 * + JPEG Input/Output Buffer configuration.
13 * + JPEG callback functions
14 * + JPEG Abort/Pause/Resume functions
15 * + JPEG custom quantization tables setting functions
16 * + IRQ handler management
17 * + Peripheral State and Error functions
18 *
19 ******************************************************************************
20 * @attention
21 *
22 * Copyright (c) 2023 STMicroelectronics.
23 * All rights reserved.
24 *
25 * This software is licensed under terms that can be found in the LICENSE file
26 * in the root directory of this software component.
27 * If no LICENSE file comes with this software, it is provided AS-IS.
28 *
29 ******************************************************************************
30 @verbatim
31 ==============================================================================
32 ##### How to use this driver #####
33 ==============================================================================
34 [..]
35 (#) Initialize the JPEG peripheral using HAL_JPEG_Init : No initialization parameters are required.
36 Only the call to HAL_JPEG_Init is necessary to initialize the JPEG peripheral.
37
38 (#) If operation is JPEG encoding use function HAL_JPEG_ConfigEncoding to set
39 the encoding parameters (mandatory before calling the encoding function).
40 the application can change the encoding parameter ImageQuality from
41 1 to 100 to obtain a more or less quality (visual quality vs the original row image),
42 and inversely more or less jpg file size.
43
44 (#) Note that for decoding operation the JPEG peripheral output data are organized in
45 YCbCr blocks called MCU (Minimum Coded Unit) as defioned in the JPEG specification
46 ISO/IEC 10918-1 standard.
47 It is up to the application to transform these YCbCr blocks to RGB data that can be display.
48
49 Respectively, for Encoding operation the JPEG peripheral input should be organized
50 in YCbCr MCU blocks. It is up to the application to perform the necessary RGB to YCbCr
51 MCU blocks transformation before feeding the JPEG peripheral with data.
52
53 (#) Use functions HAL_JPEG_Encode and HAL_JPEG_Decode to start respectively
54 a JPEG encoding/decoding operation in polling method (blocking).
55
56 (#) Use functions HAL_JPEG_Encode_IT and HAL_JPEG_Decode_IT to start respectively
57 a JPEG encoding/decoding operation with Interrupt method (not blocking).
58
59 (#) Use functions HAL_JPEG_Encode_DMA and HAL_JPEG_Decode_DMA to start respectively
60 a JPEG encoding/decoding operation with DMA method (not blocking).
61
62 (#) Callback HAL_JPEG_InfoReadyCallback is asserted if the current operation
63 is a JPEG decoding to provide the application with JPEG image parameters.
64 This callback is asserted when the JPEG peripheral successfully parse the
65 JPEG header.
66
67 (#) Callback HAL_JPEG_GetDataCallback is asserted for both encoding and decoding
68 operations to inform the application that the input buffer has been
69 consumed by the peripheral and to ask for a new data chunk if the operation
70 (encoding/decoding) has not been complete yet.
71
72 (++) This CallBack should be implemented in the application side. It should
73 call the function HAL_JPEG_ConfigInputBuffer if new input data are available,
74 or call HAL_JPEG_Pause with parameter XferSelection set to JPEG_PAUSE_RESUME_INPUT
75 to inform the JPEG HAL driver that the ongoing operation shall pause waiting for the
76 application to provide a new input data chunk.
77 Once the application succeed getting new data and if the input has been paused,
78 the application can call the function HAL_JPEG_ConfigInputBuffer to set the new
79 input buffer and size, then resume the JPEG HAL input by calling new function HAL_JPEG_Resume.
80 If the application has ended feeding the HAL JPEG with input data (no more input data), the application
81 Should call the function HAL_JPEG_ConfigInputBuffer (within the callback HAL_JPEG_GetDataCallback)
82 with the parameter InDataLength set to zero.
83
84 (++) The mechanism of HAL_JPEG_ConfigInputBuffer/HAL_JPEG_Pause/HAL_JPEG_Resume allows
85 to the application to provide the input data (for encoding or decoding) by chunks.
86 If the new input data chunk is not available (because data should be read from an input file
87 for example) the application can pause the JPEG input (using function HAL_JPEG_Pause)
88 Once the new input data chunk is available ( read from a file for example), the application
89 can call the function HAL_JPEG_ConfigInputBuffer to provide the HAL with the new chunk
90 then resume the JPEG HAL input by calling function HAL_JPEG_Resume.
91
92 (++) The application can call functions HAL_JPEG_ConfigInputBuffer then HAL_JPEG_Resume.
93 any time (outside the HAL_JPEG_GetDataCallback) Once the new input chunk data available.
94 However, to keep data coherency, the function HAL_JPEG_Pause must be imperatively called
95 (if necessary) within the callback HAL_JPEG_GetDataCallback, i.e when the HAL JPEG has ended
96 Transferring the previous chunk buffer to the JPEG peripheral.
97
98 (#) Callback HAL_JPEG_DataReadyCallback is asserted when the HAL JPEG driver
99 has filled the given output buffer with the given size.
100
101 (++) This CallBack should be implemented in the application side. It should
102 call the function HAL_JPEG_ConfigOutputBuffer to provide the HAL JPEG driver
103 with the new output buffer location and size to be used to store next data chunk.
104 if the application is not ready to provide the output chunk location then it can
105 call the function HAL_JPEG_Pause with parameter XferSelection set to JPEG_PAUSE_RESUME_OUTPUT
106 to inform the JPEG HAL driver that it shall pause output data. Once the application
107 is ready to receive the new data chunk (output buffer location free or available) it should call
108 the function HAL_JPEG_ConfigOutputBuffer to provide the HAL JPEG driver
109 with the new output chunk buffer location and size, then call HAL_JPEG_Resume
110 to inform the HAL that it shall resume outputting data in the given output buffer.
111
112 (++) The mechanism of HAL_JPEG_ConfigOutputBuffer/HAL_JPEG_Pause/HAL_JPEG_Resume allows
113 the application to receive data from the JPEG peripheral by chunks. when a chunk
114 is received, the application can pause the HAL JPEG output data to be able to process
115 these received data (YCbCr to RGB conversion in case of decoding or data storage in case
116 of encoding).
117
118 (++) The application can call functions HAL_JPEG_ ConfigOutputBuffer then HAL_JPEG_Resume.
119 any time (outside the HAL_JPEG_DataReadyCallback) Once the output data buffer is free to use.
120 However, to keep data coherency, the function HAL_JPEG_Pause must be imperatively called
121 (if necessary) within the callback HAL_JPEG_ DataReadyCallback, i.e when the HAL JPEG has ended
122 Transferring the previous chunk buffer from the JPEG peripheral to the application.
123
124 (#) Callback HAL_JPEG_EncodeCpltCallback is asserted when the HAL JPEG driver has
125 ended the current JPEG encoding operation, and all output data has been transmitted
126 to the application.
127
128 (#) Callback HAL_JPEG_DecodeCpltCallback is asserted when the HAL JPEG driver has
129 ended the current JPEG decoding operation. and all output data has been transmitted
130 to the application.
131
132 (#) Callback HAL_JPEG_ErrorCallback is asserted when an error occurred during
133 the current operation. the application can call the function HAL_JPEG_GetError()
134 to retrieve the error codes.
135
136 (#) By default the HAL JPEG driver uses the default quantization tables
137 as provide in the JPEG specification (ISO/IEC 10918-1 standard) for encoding.
138 User can change these default tables if necessary using the function HAL_JPEG_SetUserQuantTables
139 Note that for decoding the quantization tables are automatically extracted from
140 the JPEG header.
141
142 (#) To control JPEG state you can use the following function: HAL_JPEG_GetState()
143
144 *** JPEG HAL driver macros list ***
145 =============================================
146 [..]
147 Below the list of most used macros in JPEG HAL driver.
148
149 (+) __HAL_JPEG_RESET_HANDLE_STATE : Reset JPEG handle state.
150 (+) __HAL_JPEG_ENABLE : Enable the JPEG peripheral.
151 (+) __HAL_JPEG_DISABLE : Disable the JPEG peripheral.
152 (+) __HAL_JPEG_GET_FLAG : Check the specified JPEG status flag.
153 (+) __HAL_JPEG_CLEAR_FLAG : Clear the specified JPEG status flag.
154 (+) __HAL_JPEG_ENABLE_IT : Enable the specified JPEG Interrupt.
155 (+) __HAL_JPEG_DISABLE_IT : Disable the specified JPEG Interrupt.
156 (+) __HAL_JPEG_GET_IT_SOURCE : returns the state of the specified JPEG Interrupt (Enabled or disabled).
157
158 *** Callback registration ***
159 =============================================
160
161 The compilation define USE_HAL_JPEG_REGISTER_CALLBACKS when set to 1
162 allows the user to configure dynamically the driver callbacks.
163 Use Functions HAL_JPEG_RegisterCallback() or HAL_JPEG_RegisterXXXCallback()
164 to register an interrupt callback.
165
166 Function HAL_JPEG_RegisterCallback() allows to register following callbacks:
167 (+) EncodeCpltCallback : callback for end of encoding operation.
168 (+) DecodeCpltCallback : callback for end of decoding operation.
169 (+) ErrorCallback : callback for error detection.
170 (+) MspInitCallback : JPEG MspInit.
171 (+) MspDeInitCallback : JPEG MspDeInit.
172 This function takes as parameters the HAL peripheral handle, the Callback ID
173 and a pointer to the user callback function.
174
175 For specific callbacks InfoReadyCallback, GetDataCallback and DataReadyCallback use dedicated
176 register callbacks : respectively HAL_JPEG_RegisterInfoReadyCallback(),
177 HAL_JPEG_RegisterGetDataCallback() and HAL_JPEG_RegisterDataReadyCallback().
178
179 Use function HAL_JPEG_UnRegisterCallback() to reset a callback to the default
180 weak function.
181 HAL_JPEG_UnRegisterCallback() takes as parameters the HAL peripheral handle,
182 and the Callback ID.
183 This function allows to reset following callbacks:
184 (+) EncodeCpltCallback : callback for end of encoding operation.
185 (+) DecodeCpltCallback : callback for end of decoding operation.
186 (+) ErrorCallback : callback for error detection.
187 (+) MspInitCallback : JPEG MspInit.
188 (+) MspDeInitCallback : JPEG MspDeInit.
189
190 For callbacks InfoReadyCallback, GetDataCallback and DataReadyCallback use dedicated
191 unregister callbacks : respectively HAL_JPEG_UnRegisterInfoReadyCallback(),
192 HAL_JPEG_UnRegisterGetDataCallback() and HAL_JPEG_UnRegisterDataReadyCallback().
193
194 By default, after the HAL_JPEG_Init() and when the state is HAL_JPEG_STATE_RESET
195 all callbacks are set to the corresponding weak functions :
196 examples HAL_JPEG_DecodeCpltCallback() , HAL_JPEG_GetDataCallback().
197 Exception done for MspInit and MspDeInit functions that are
198 reset to the legacy weak function in the HAL_JPEG_Init()/ HAL_JPEG_DeInit() only when
199 these callbacks are null (not registered beforehand).
200 if not, MspInit or MspDeInit are not null, the HAL_JPEG_Init() / HAL_JPEG_DeInit()
201 keep and use the user MspInit/MspDeInit functions (registered beforehand)
202
203 Callbacks can be registered/unregistered in HAL_JPEG_STATE_READY state only.
204 Exception done MspInit/MspDeInit callbacks that can be registered/unregistered
205 in HAL_JPEG_STATE_READY or HAL_JPEG_STATE_RESET state,
206 thus registered (user) MspInit/DeInit callbacks can be used during the Init/DeInit.
207 In that case first register the MspInit/MspDeInit user callbacks
208 using HAL_JPEG_RegisterCallback() before calling HAL_JPEG_DeInit()
209 or HAL_JPEG_Init() function.
210
211 When The compilation define USE_HAL_JPEG_REGISTER_CALLBACKS is set to 0 or
212 not defined, the callback registration feature is not available and all callbacks
213 are set to the corresponding weak functions.
214
215 @endverbatim
216 ******************************************************************************
217 */
218
219 /* Includes ------------------------------------------------------------------*/
220 #include "stm32n6xx_hal.h"
221
222 /** @addtogroup STM32N6xx_HAL_Driver
223 * @{
224 */
225
226 #ifdef HAL_JPEG_MODULE_ENABLED
227
228 #if defined (JPEG)
229
230 /** @defgroup JPEG JPEG
231 * @brief JPEG HAL module driver.
232 * @{
233 */
234
235 /* Private define ------------------------------------------------------------*/
236 /** @addtogroup JPEG_Private_Constants
237 * @{
238 */
239 #define JPEG_TIMEOUT_VALUE ((uint32_t)1000) /* 1s */
240 #define JPEG_AC_HUFF_TABLE_SIZE ((uint32_t)162) /* Huffman AC table size : 162 codes*/
241 #define JPEG_DC_HUFF_TABLE_SIZE ((uint32_t)12) /* Huffman DC table size : 12 codes*/
242
243 #define JPEG_QUANTVAL_MAX ((uint32_t)255) /* Quantization values are 8-bit numbers*/
244 #define JPEG_LOW_QUALITY_REFERENCE ((uint32_t)5000) /*Reference value to generate scaling factor
245 for low quality factors (<50) */
246 #define JPEG_HIGH_QUALITY_REFERENCE ((uint32_t)200) /*Reference value to generate scaling factor
247 for high quality factors (>=50)*/
248
249 #define JPEG_FIFO_SIZE ((uint32_t)16U) /* JPEG Input/Output HW FIFO size in words*/
250
251 #define JPEG_FIFO_TH_SIZE ((uint32_t)4U) /* JPEG Input/Output HW FIFO Threshold in words*/
252
253 #define JPEG_DMA_MASK ((uint32_t)0x00001800) /* JPEG DMA request Mask*/
254 #define JPEG_DMA_IDMA ((uint32_t)JPEG_CR_IDMAEN) /* DMA request for the input FIFO */
255 #define JPEG_DMA_ODMA ((uint32_t)JPEG_CR_ODMAEN) /* DMA request for the output FIFO */
256
257 #define JPEG_INTERRUPT_MASK ((uint32_t)0x0000007EU) /* JPEG Interrupt Mask*/
258
259 #define JPEG_CONTEXT_ENCODE ((uint32_t)0x00000001) /* JPEG context : operation is encoding*/
260 #define JPEG_CONTEXT_DECODE ((uint32_t)0x00000002) /* JPEG context : operation is decoding*/
261 #define JPEG_CONTEXT_OPERATION_MASK ((uint32_t)0x00000003) /* JPEG context : operation Mask */
262
263 #define JPEG_CONTEXT_POLLING ((uint32_t)0x00000004) /* JPEG context : Transfer use Polling */
264 #define JPEG_CONTEXT_IT ((uint32_t)0x00000008) /* JPEG context : Transfer use Interrupt */
265 #define JPEG_CONTEXT_DMA ((uint32_t)0x0000000C) /* JPEG context : Transfer use DMA */
266 #define JPEG_CONTEXT_METHOD_MASK ((uint32_t)0x0000000C) /* JPEG context : Transfer Mask */
267
268
269 #define JPEG_CONTEXT_CONF_ENCODING ((uint32_t)0x00000100) /* JPEG context : encoding config done */
270
271 #define JPEG_CONTEXT_PAUSE_INPUT ((uint32_t)0x00001000) /* JPEG context : Pause Input */
272 #define JPEG_CONTEXT_PAUSE_OUTPUT ((uint32_t)0x00002000) /* JPEG context : Pause Output */
273
274 #define JPEG_CONTEXT_CUSTOM_TABLES ((uint32_t)0x00004000) /* JPEG context : Use custom quantization tables */
275
276 #define JPEG_CONTEXT_ENDING_DMA ((uint32_t)0x00008000) /* JPEG context : ending with DMA in progress */
277
278 #define JPEG_PROCESS_ONGOING ((uint32_t)0x00000000) /* Process is on going */
279 #define JPEG_PROCESS_DONE ((uint32_t)0x00000001) /* Process is done (ends) */
280 #define JPEG_GET_DMA_REMAIN_DATA(__HANDLE__) (__HAL_DMA_GET_COUNTER(__HANDLE__)\
281 + HAL_DMAEx_GetFifoLevel(__HANDLE__))
282 /**
283 * @}
284 */
285
286 /* Private typedef -----------------------------------------------------------*/
287 /** @addtogroup JPEG_Private_Types
288 * @{
289 */
290
291 /*
292 JPEG Huffman Table Structure definition :
293 This implementation of Huffman table structure is compliant with ISO/IEC 10918-1 standard,
294 Annex C Huffman Table specification
295 */
296 typedef struct
297 {
298 /* These two fields directly represent the contents of a JPEG DHT marker */
299 uint8_t Bits[16]; /*!< bits[k] = # of symbols with codes of length k bits,
300 this parameter corresponds to BITS list in the Annex C */
301
302 uint8_t HuffVal[162]; /*!< The symbols, in order of incremented code length,
303 this parameter corresponds to HUFFVAL list in the Annex C */
304
305
306 } JPEG_ACHuffTableTypeDef;
307
308 typedef struct
309 {
310 /* These two fields directly represent the contents of a JPEG DHT marker */
311 uint8_t Bits[16]; /*!< bits[k] = # of symbols with codes of length k bits,
312 this parameter corresponds to BITS list in the Annex C */
313
314 uint8_t HuffVal[12]; /*!< The symbols, in order of incremented code length,
315 this parameter corresponds to HUFFVAL list in the Annex C */
316
317
318 } JPEG_DCHuffTableTypeDef;
319
320 typedef struct
321 {
322 uint8_t CodeLength[JPEG_AC_HUFF_TABLE_SIZE]; /*!< Code length */
323
324 uint32_t HuffmanCode[JPEG_AC_HUFF_TABLE_SIZE]; /*!< HuffmanCode */
325
326 } JPEG_AC_HuffCodeTableTypeDef;
327
328 typedef struct
329 {
330 uint8_t CodeLength[JPEG_DC_HUFF_TABLE_SIZE]; /*!< Code length */
331
332 uint32_t HuffmanCode[JPEG_DC_HUFF_TABLE_SIZE]; /*!< HuffmanCode */
333
334 } JPEG_DC_HuffCodeTableTypeDef;
335 /**
336 * @}
337 */
338
339 /* Private macro -------------------------------------------------------------*/
340 /** @addtogroup JPEG_Private_Macros
341 * @{
342 */
343 #define JPEG_ENABLE_DMA(__HANDLE__,__DMA__) ((__HANDLE__)->Instance->CR |= ((__DMA__) & JPEG_DMA_MASK))
344 /*note : To disable a DMA request we must use MODIFY_REG macro to avoid writing "1" to the FIFO flush bits
345 located in the same DMA request enable register (CR register). */
346 #define JPEG_DISABLE_DMA(__HANDLE__,__DMA__) MODIFY_REG((__HANDLE__)->Instance->CR, ((__DMA__) & JPEG_DMA_MASK), 0UL)
347 /**
348 * @}
349 */
350
351 /* Private variables ---------------------------------------------------------*/
352 /** @addtogroup JPEG_Private_Variables
353 * @{
354 */
355
356 /* The following Huffman tables, are based on the JPEG standard as defined by the ITU-T Recommendation T.81. */
357 static const JPEG_DCHuffTableTypeDef JPEG_DCLUM_HuffTable =
358 {
359 { 0, 1, 5, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0 }, /*Bits*/
360
361 { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0xa, 0xb } /*HUFFVAL */
362
363 };
364
365 static const JPEG_DCHuffTableTypeDef JPEG_DCCHROM_HuffTable =
366 {
367 { 0, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0 }, /*Bits*/
368
369 { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0xa, 0xb } /*HUFFVAL */
370 };
371
372 static const JPEG_ACHuffTableTypeDef JPEG_ACLUM_HuffTable =
373 {
374 { 0, 2, 1, 3, 3, 2, 4, 3, 5, 5, 4, 4, 0, 0, 1, 0x7d }, /*Bits*/
375
376 {
377 0x01, 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12, /*HUFFVAL */
378 0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61, 0x07,
379 0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xa1, 0x08,
380 0x23, 0x42, 0xb1, 0xc1, 0x15, 0x52, 0xd1, 0xf0,
381 0x24, 0x33, 0x62, 0x72, 0x82, 0x09, 0x0a, 0x16,
382 0x17, 0x18, 0x19, 0x1a, 0x25, 0x26, 0x27, 0x28,
383 0x29, 0x2a, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
384 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49,
385 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59,
386 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69,
387 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79,
388 0x7a, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89,
389 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
390 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
391 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6,
392 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3, 0xc4, 0xc5,
393 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 0xd3, 0xd4,
394 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe1, 0xe2,
395 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea,
396 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
397 0xf9, 0xfa
398 }
399 };
400
401 static const JPEG_ACHuffTableTypeDef JPEG_ACCHROM_HuffTable =
402 {
403 { 0, 2, 1, 2, 4, 4, 3, 4, 7, 5, 4, 4, 0, 1, 2, 0x77 }, /*Bits*/
404
405 {
406 0x00, 0x01, 0x02, 0x03, 0x11, 0x04, 0x05, 0x21, /*HUFFVAL */
407 0x31, 0x06, 0x12, 0x41, 0x51, 0x07, 0x61, 0x71,
408 0x13, 0x22, 0x32, 0x81, 0x08, 0x14, 0x42, 0x91,
409 0xa1, 0xb1, 0xc1, 0x09, 0x23, 0x33, 0x52, 0xf0,
410 0x15, 0x62, 0x72, 0xd1, 0x0a, 0x16, 0x24, 0x34,
411 0xe1, 0x25, 0xf1, 0x17, 0x18, 0x19, 0x1a, 0x26,
412 0x27, 0x28, 0x29, 0x2a, 0x35, 0x36, 0x37, 0x38,
413 0x39, 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48,
414 0x49, 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58,
415 0x59, 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68,
416 0x69, 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78,
417 0x79, 0x7a, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
418 0x88, 0x89, 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96,
419 0x97, 0x98, 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5,
420 0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4,
421 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3,
422 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2,
423 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda,
424 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9,
425 0xea, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
426 0xf9, 0xfa
427 }
428 };
429
430 static const uint8_t JPEG_ZIGZAG_ORDER[JPEG_QUANT_TABLE_SIZE] =
431 {
432 0, 1, 8, 16, 9, 2, 3, 10,
433 17, 24, 32, 25, 18, 11, 4, 5,
434 12, 19, 26, 33, 40, 48, 41, 34,
435 27, 20, 13, 6, 7, 14, 21, 28,
436 35, 42, 49, 56, 57, 50, 43, 36,
437 29, 22, 15, 23, 30, 37, 44, 51,
438 58, 59, 52, 45, 38, 31, 39, 46,
439 53, 60, 61, 54, 47, 55, 62, 63
440 };
441 /**
442 * @}
443 */
444
445 /* Private function prototypes -----------------------------------------------*/
446 /** @addtogroup JPEG_Private_Functions_Prototypes
447 * @{
448 */
449
450 static HAL_StatusTypeDef JPEG_Bits_To_SizeCodes(const uint8_t *Bits, uint8_t *Huffsize, uint32_t *Huffcode,
451 uint32_t *LastK);
452 static HAL_StatusTypeDef JPEG_DCHuff_BitsVals_To_SizeCodes(JPEG_DCHuffTableTypeDef *DC_BitsValsTable,
453 JPEG_DC_HuffCodeTableTypeDef *DC_SizeCodesTable);
454 static HAL_StatusTypeDef JPEG_ACHuff_BitsVals_To_SizeCodes(JPEG_ACHuffTableTypeDef *AC_BitsValsTable,
455 JPEG_AC_HuffCodeTableTypeDef *AC_SizeCodesTable);
456 static HAL_StatusTypeDef JPEG_Set_HuffDC_Mem(const JPEG_HandleTypeDef *hjpeg, JPEG_DCHuffTableTypeDef *HuffTableDC,
457 const __IO uint32_t *DCTableAddress);
458 static HAL_StatusTypeDef JPEG_Set_HuffAC_Mem(const JPEG_HandleTypeDef *hjpeg, JPEG_ACHuffTableTypeDef *HuffTableAC,
459 const __IO uint32_t *ACTableAddress);
460 static HAL_StatusTypeDef JPEG_Set_HuffEnc_Mem(JPEG_HandleTypeDef *hjpeg);
461 static void JPEG_Set_Huff_DHTMem(const JPEG_HandleTypeDef *hjpeg);
462 static uint32_t JPEG_Set_Quantization_Mem(const JPEG_HandleTypeDef *hjpeg, const uint8_t *QTable,
463 __IO uint32_t *QTableAddress);
464 static void JPEG_SetColorYCBCR(JPEG_HandleTypeDef *hjpeg);
465 static void JPEG_SetColorGrayScale(JPEG_HandleTypeDef *hjpeg);
466 static void JPEG_SetColorCMYK(JPEG_HandleTypeDef *hjpeg);
467
468 static void JPEG_Init_Process(JPEG_HandleTypeDef *hjpeg);
469 static uint32_t JPEG_Process(JPEG_HandleTypeDef *hjpeg);
470 static void JPEG_ReadInputData(JPEG_HandleTypeDef *hjpeg, uint32_t nbRequestWords);
471 static void JPEG_StoreOutputData(JPEG_HandleTypeDef *hjpeg, uint32_t nbOutputWords);
472 static uint32_t JPEG_GetQuality(const JPEG_HandleTypeDef *hjpeg);
473
474 static HAL_StatusTypeDef JPEG_DMA_StartProcess(JPEG_HandleTypeDef *hjpeg);
475 static void JPEG_DMA_ContinueProcess(JPEG_HandleTypeDef *hjpeg);
476 static void JPEG_DMA_EndProcess(JPEG_HandleTypeDef *hjpeg);
477 static void JPEG_DMA_PollResidualData(JPEG_HandleTypeDef *hjpeg);
478 static void JPEG_DMAOutCpltCallback(DMA_HandleTypeDef *hdma);
479 static void JPEG_DMAInCpltCallback(DMA_HandleTypeDef *hdma);
480 static void JPEG_DMAErrorCallback(DMA_HandleTypeDef *hdma);
481 static void JPEG_DMAOutAbortCallback(DMA_HandleTypeDef *hdma) ;
482
483 /**
484 * @}
485 */
486
487 /** @defgroup JPEG_Exported_Functions JPEG Exported Functions
488 * @{
489 */
490
491 /** @defgroup JPEG_Exported_Functions_Group1 Initialization and de-initialization functions
492 * @brief Initialization and de-initialization functions.
493 *
494 @verbatim
495 ==============================================================================
496 ##### Initialization and de-initialization functions #####
497 ==============================================================================
498 [..] This section provides functions allowing to:
499 (+) Initialize the JPEG peripheral and creates the associated handle
500 (+) DeInitialize the JPEG peripheral
501
502 @endverbatim
503 * @{
504 */
505
506 /**
507 * @brief Initializes the JPEG according to the specified
508 * parameters in the JPEG_InitTypeDef and creates the associated handle.
509 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
510 * the configuration information for JPEG module
511 * @retval HAL status
512 */
HAL_JPEG_Init(JPEG_HandleTypeDef * hjpeg)513 HAL_StatusTypeDef HAL_JPEG_Init(JPEG_HandleTypeDef *hjpeg)
514 {
515 /* These are the sample quantization tables given in JPEG spec ISO/IEC 10918-1 standard , section K.1. */
516 static const uint8_t JPEG_LUM_QuantTable[JPEG_QUANT_TABLE_SIZE] =
517 {
518 16, 11, 10, 16, 24, 40, 51, 61,
519 12, 12, 14, 19, 26, 58, 60, 55,
520 14, 13, 16, 24, 40, 57, 69, 56,
521 14, 17, 22, 29, 51, 87, 80, 62,
522 18, 22, 37, 56, 68, 109, 103, 77,
523 24, 35, 55, 64, 81, 104, 113, 92,
524 49, 64, 78, 87, 103, 121, 120, 101,
525 72, 92, 95, 98, 112, 100, 103, 99
526 };
527 static const uint8_t JPEG_CHROM_QuantTable[JPEG_QUANT_TABLE_SIZE] =
528 {
529 17, 18, 24, 47, 99, 99, 99, 99,
530 18, 21, 26, 66, 99, 99, 99, 99,
531 24, 26, 56, 99, 99, 99, 99, 99,
532 47, 66, 99, 99, 99, 99, 99, 99,
533 99, 99, 99, 99, 99, 99, 99, 99,
534 99, 99, 99, 99, 99, 99, 99, 99,
535 99, 99, 99, 99, 99, 99, 99, 99,
536 99, 99, 99, 99, 99, 99, 99, 99
537 };
538
539 /* Check the JPEG handle allocation */
540 if (hjpeg == NULL)
541 {
542 return HAL_ERROR;
543 }
544
545 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
546 if (hjpeg->State == HAL_JPEG_STATE_RESET)
547 {
548 /* Allocate lock resource and initialize it */
549 hjpeg->Lock = HAL_UNLOCKED;
550
551 hjpeg->InfoReadyCallback = HAL_JPEG_InfoReadyCallback; /* Legacy weak InfoReadyCallback */
552 hjpeg->EncodeCpltCallback = HAL_JPEG_EncodeCpltCallback; /* Legacy weak EncodeCpltCallback */
553 hjpeg->DecodeCpltCallback = HAL_JPEG_DecodeCpltCallback; /* Legacy weak DecodeCpltCallback */
554 hjpeg->ErrorCallback = HAL_JPEG_ErrorCallback; /* Legacy weak ErrorCallback */
555 hjpeg->GetDataCallback = HAL_JPEG_GetDataCallback; /* Legacy weak GetDataCallback */
556 hjpeg->DataReadyCallback = HAL_JPEG_DataReadyCallback; /* Legacy weak DataReadyCallback */
557
558 if (hjpeg->MspInitCallback == NULL)
559 {
560 hjpeg->MspInitCallback = HAL_JPEG_MspInit; /* Legacy weak MspInit */
561 }
562
563 /* Init the low level hardware */
564 hjpeg->MspInitCallback(hjpeg);
565 }
566 #else
567 if (hjpeg->State == HAL_JPEG_STATE_RESET)
568 {
569 /* Allocate lock resource and initialize it */
570 hjpeg->Lock = HAL_UNLOCKED;
571
572 /* Init the low level hardware : GPIO, CLOCK */
573 HAL_JPEG_MspInit(hjpeg);
574 }
575 #endif /* USE_HAL_JPEG_REGISTER_CALLBACKS */
576
577 /* Change the JPEG state */
578 hjpeg->State = HAL_JPEG_STATE_BUSY;
579
580 /* Start the JPEG Core*/
581 __HAL_JPEG_ENABLE(hjpeg);
582
583 /* Stop the JPEG encoding/decoding process*/
584 hjpeg->Instance->CONFR0 &= ~JPEG_CONFR0_START;
585
586 /* Disable All Interrupts */
587 __HAL_JPEG_DISABLE_IT(hjpeg, JPEG_INTERRUPT_MASK);
588
589 /* Disable All DMA requests */
590 JPEG_DISABLE_DMA(hjpeg, JPEG_DMA_MASK);
591
592 /* Flush input and output FIFOs*/
593 hjpeg->Instance->CR |= JPEG_CR_IFF;
594 hjpeg->Instance->CR |= JPEG_CR_OFF;
595
596 /* Clear all flags */
597 __HAL_JPEG_CLEAR_FLAG(hjpeg, JPEG_FLAG_ALL);
598
599 /* init default quantization tables*/
600 hjpeg->QuantTable0 = (uint8_t *)((uint32_t)JPEG_LUM_QuantTable);
601 hjpeg->QuantTable1 = (uint8_t *)((uint32_t)JPEG_CHROM_QuantTable);
602 hjpeg->QuantTable2 = NULL;
603 hjpeg->QuantTable3 = NULL;
604
605 /* init the default Huffman tables*/
606 if (JPEG_Set_HuffEnc_Mem(hjpeg) != HAL_OK)
607 {
608 hjpeg->ErrorCode = HAL_JPEG_ERROR_HUFF_TABLE;
609
610 return HAL_ERROR;
611 }
612
613 /* Enable header processing*/
614 hjpeg->Instance->CONFR1 |= JPEG_CONFR1_HDR;
615
616 /* Reset JpegInCount and JpegOutCount */
617 hjpeg->JpegInCount = 0;
618 hjpeg->JpegOutCount = 0;
619
620 /* Change the JPEG state */
621 hjpeg->State = HAL_JPEG_STATE_READY;
622
623 /* Reset the JPEG ErrorCode */
624 hjpeg->ErrorCode = HAL_JPEG_ERROR_NONE;
625
626 /* Clear the context fields */
627 hjpeg->Context = 0;
628
629 /* Return function status */
630 return HAL_OK;
631 }
632
633 /**
634 * @brief DeInitializes the JPEG peripheral.
635 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
636 * the configuration information for JPEG module
637 * @retval HAL status
638 */
HAL_JPEG_DeInit(JPEG_HandleTypeDef * hjpeg)639 HAL_StatusTypeDef HAL_JPEG_DeInit(JPEG_HandleTypeDef *hjpeg)
640 {
641 /* Check the JPEG handle allocation */
642 if (hjpeg == NULL)
643 {
644 return HAL_ERROR;
645 }
646
647 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
648 if (hjpeg->MspDeInitCallback == NULL)
649 {
650 hjpeg->MspDeInitCallback = HAL_JPEG_MspDeInit; /* Legacy weak MspDeInit */
651 }
652
653 /* DeInit the low level hardware */
654 hjpeg->MspDeInitCallback(hjpeg);
655
656 #else
657 /* DeInit the low level hardware: CLOCK, NVIC.*/
658 HAL_JPEG_MspDeInit(hjpeg);
659 #endif /* USE_HAL_JPEG_REGISTER_CALLBACKS */
660
661 /* Change the JPEG state */
662 hjpeg->State = HAL_JPEG_STATE_BUSY;
663
664 /* Reset the JPEG ErrorCode */
665 hjpeg->ErrorCode = HAL_JPEG_ERROR_NONE;
666
667 /* Reset JpegInCount and JpegOutCount */
668 hjpeg->JpegInCount = 0;
669 hjpeg->JpegOutCount = 0;
670
671 /* Change the JPEG state */
672 hjpeg->State = HAL_JPEG_STATE_RESET;
673
674 /*Clear the context fields*/
675 hjpeg->Context = 0;
676
677 /* Release Lock */
678 __HAL_UNLOCK(hjpeg);
679
680 /* Return function status */
681 return HAL_OK;
682 }
683
684 /**
685 * @brief Initializes the JPEG MSP.
686 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
687 * the configuration information for JPEG module
688 * @retval None
689 */
HAL_JPEG_MspInit(JPEG_HandleTypeDef * hjpeg)690 __weak void HAL_JPEG_MspInit(JPEG_HandleTypeDef *hjpeg)
691 {
692 /* Prevent unused argument(s) compilation warning */
693 UNUSED(hjpeg);
694
695 /* NOTE : This function Should not be modified, when the callback is needed,
696 the HAL_JPEG_MspInit could be implemented in the user file
697 */
698 }
699
700 /**
701 * @brief DeInitializes JPEG MSP.
702 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
703 * the configuration information for JPEG module
704 * @retval None
705 */
HAL_JPEG_MspDeInit(JPEG_HandleTypeDef * hjpeg)706 __weak void HAL_JPEG_MspDeInit(JPEG_HandleTypeDef *hjpeg)
707 {
708 /* Prevent unused argument(s) compilation warning */
709 UNUSED(hjpeg);
710
711 /* NOTE : This function Should not be modified, when the callback is needed,
712 the HAL_JPEG_MspDeInit could be implemented in the user file
713 */
714 }
715
716 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
717 /**
718 * @brief Register a User JPEG Callback
719 * To be used to override the weak predefined callback
720 * @param hjpeg JPEG handle
721 * @param CallbackID ID of the callback to be registered
722 * This parameter can be one of the following values:
723 * @arg @ref HAL_JPEG_ENCODE_CPLT_CB_ID Encode Complete callback ID
724 * @arg @ref HAL_JPEG_DECODE_CPLT_CB_ID Decode Complete callback ID
725 * @arg @ref HAL_JPEG_ERROR_CB_ID Error callback ID
726 * @arg @ref HAL_JPEG_MSPINIT_CB_ID MspInit callback ID
727 * @arg @ref HAL_JPEG_MSPDEINIT_CB_ID MspDeInit callback ID
728 * @param pCallback pointer to the Callback function
729 * @retval HAL status
730 */
HAL_JPEG_RegisterCallback(JPEG_HandleTypeDef * hjpeg,HAL_JPEG_CallbackIDTypeDef CallbackID,pJPEG_CallbackTypeDef pCallback)731 HAL_StatusTypeDef HAL_JPEG_RegisterCallback(JPEG_HandleTypeDef *hjpeg, HAL_JPEG_CallbackIDTypeDef CallbackID,
732 pJPEG_CallbackTypeDef pCallback)
733 {
734 HAL_StatusTypeDef status = HAL_OK;
735
736 if (pCallback == NULL)
737 {
738 /* Update the error code */
739 hjpeg->ErrorCode |= HAL_JPEG_ERROR_INVALID_CALLBACK;
740 return HAL_ERROR;
741 }
742 /* Process locked */
743 __HAL_LOCK(hjpeg);
744
745 if (HAL_JPEG_STATE_READY == hjpeg->State)
746 {
747 switch (CallbackID)
748 {
749 case HAL_JPEG_ENCODE_CPLT_CB_ID :
750 hjpeg->EncodeCpltCallback = pCallback;
751 break;
752
753 case HAL_JPEG_DECODE_CPLT_CB_ID :
754 hjpeg->DecodeCpltCallback = pCallback;
755 break;
756
757 case HAL_JPEG_ERROR_CB_ID :
758 hjpeg->ErrorCallback = pCallback;
759 break;
760
761 case HAL_JPEG_MSPINIT_CB_ID :
762 hjpeg->MspInitCallback = pCallback;
763 break;
764
765 case HAL_JPEG_MSPDEINIT_CB_ID :
766 hjpeg->MspDeInitCallback = pCallback;
767 break;
768
769 default :
770 /* Update the error code */
771 hjpeg->ErrorCode |= HAL_JPEG_ERROR_INVALID_CALLBACK;
772 /* Return error status */
773 status = HAL_ERROR;
774 break;
775 }
776 }
777 else if (HAL_JPEG_STATE_RESET == hjpeg->State)
778 {
779 switch (CallbackID)
780 {
781 case HAL_JPEG_MSPINIT_CB_ID :
782 hjpeg->MspInitCallback = pCallback;
783 break;
784
785 case HAL_JPEG_MSPDEINIT_CB_ID :
786 hjpeg->MspDeInitCallback = pCallback;
787 break;
788
789 default :
790 /* Update the error code */
791 hjpeg->ErrorCode |= HAL_JPEG_ERROR_INVALID_CALLBACK;
792 /* Return error status */
793 status = HAL_ERROR;
794 break;
795 }
796 }
797 else
798 {
799 /* Update the error code */
800 hjpeg->ErrorCode |= HAL_JPEG_ERROR_INVALID_CALLBACK;
801 /* Return error status */
802 status = HAL_ERROR;
803 }
804
805 /* Release Lock */
806 __HAL_UNLOCK(hjpeg);
807 return status;
808 }
809
810 /**
811 * @brief Unregister a JPEG Callback
812 * JPEG callback is redirected to the weak predefined callback
813 * @param hjpeg JPEG handle
814 * @param CallbackID ID of the callback to be unregistered
815 * This parameter can be one of the following values:
816 * @arg @ref HAL_JPEG_ENCODE_CPLT_CB_ID Encode Complete callback ID
817 * @arg @ref HAL_JPEG_DECODE_CPLT_CB_ID Decode Complete callback ID
818 * @arg @ref HAL_JPEG_ERROR_CB_ID Error callback ID
819 * @arg @ref HAL_JPEG_MSPINIT_CB_ID MspInit callback ID
820 * @arg @ref HAL_JPEG_MSPDEINIT_CB_ID MspDeInit callback ID
821 * @retval HAL status
822 */
HAL_JPEG_UnRegisterCallback(JPEG_HandleTypeDef * hjpeg,HAL_JPEG_CallbackIDTypeDef CallbackID)823 HAL_StatusTypeDef HAL_JPEG_UnRegisterCallback(JPEG_HandleTypeDef *hjpeg, HAL_JPEG_CallbackIDTypeDef CallbackID)
824 {
825 HAL_StatusTypeDef status = HAL_OK;
826
827 /* Process locked */
828 __HAL_LOCK(hjpeg);
829
830 if (HAL_JPEG_STATE_READY == hjpeg->State)
831 {
832 switch (CallbackID)
833 {
834 case HAL_JPEG_ENCODE_CPLT_CB_ID :
835 hjpeg->EncodeCpltCallback = HAL_JPEG_EncodeCpltCallback; /* Legacy weak EncodeCpltCallback */
836 break;
837
838 case HAL_JPEG_DECODE_CPLT_CB_ID :
839 hjpeg->DecodeCpltCallback = HAL_JPEG_DecodeCpltCallback; /* Legacy weak DecodeCpltCallback */
840 break;
841
842 case HAL_JPEG_ERROR_CB_ID :
843 hjpeg->ErrorCallback = HAL_JPEG_ErrorCallback; /* Legacy weak ErrorCallback */
844 break;
845
846 case HAL_JPEG_MSPINIT_CB_ID :
847 hjpeg->MspInitCallback = HAL_JPEG_MspInit; /* Legacy weak MspInit */
848 break;
849
850 case HAL_JPEG_MSPDEINIT_CB_ID :
851 hjpeg->MspDeInitCallback = HAL_JPEG_MspDeInit; /* Legacy weak MspDeInit */
852 break;
853
854 default :
855 /* Update the error code */
856 hjpeg->ErrorCode |= HAL_JPEG_ERROR_INVALID_CALLBACK;
857 /* Return error status */
858 status = HAL_ERROR;
859 break;
860 }
861 }
862 else if (HAL_JPEG_STATE_RESET == hjpeg->State)
863 {
864 switch (CallbackID)
865 {
866 case HAL_JPEG_MSPINIT_CB_ID :
867 hjpeg->MspInitCallback = HAL_JPEG_MspInit; /* Legacy weak MspInit */
868 break;
869
870 case HAL_JPEG_MSPDEINIT_CB_ID :
871 hjpeg->MspDeInitCallback = HAL_JPEG_MspDeInit; /* Legacy weak MspInit */
872 break;
873
874 default :
875 /* Update the error code */
876 hjpeg->ErrorCode |= HAL_JPEG_ERROR_INVALID_CALLBACK;
877 /* Return error status */
878 status = HAL_ERROR;
879 break;
880 }
881 }
882 else
883 {
884 /* Update the error code */
885 hjpeg->ErrorCode |= HAL_JPEG_ERROR_INVALID_CALLBACK;
886 /* Return error status */
887 status = HAL_ERROR;
888 }
889
890 /* Release Lock */
891 __HAL_UNLOCK(hjpeg);
892 return status;
893 }
894
895 /**
896 * @brief Register Info Ready JPEG Callback
897 * To be used instead of the weak HAL_JPEG_InfoReadyCallback() predefined callback
898 * @param hjpeg JPEG handle
899 * @param pCallback pointer to the Info Ready Callback function
900 * @retval HAL status
901 */
HAL_JPEG_RegisterInfoReadyCallback(JPEG_HandleTypeDef * hjpeg,pJPEG_InfoReadyCallbackTypeDef pCallback)902 HAL_StatusTypeDef HAL_JPEG_RegisterInfoReadyCallback(JPEG_HandleTypeDef *hjpeg,
903 pJPEG_InfoReadyCallbackTypeDef pCallback)
904 {
905 HAL_StatusTypeDef status = HAL_OK;
906
907 if (pCallback == NULL)
908 {
909 /* Update the error code */
910 hjpeg->ErrorCode |= HAL_JPEG_ERROR_INVALID_CALLBACK;
911 return HAL_ERROR;
912 }
913 /* Process locked */
914 __HAL_LOCK(hjpeg);
915
916 if (HAL_JPEG_STATE_READY == hjpeg->State)
917 {
918 hjpeg->InfoReadyCallback = pCallback;
919 }
920 else
921 {
922 /* Update the error code */
923 hjpeg->ErrorCode |= HAL_JPEG_ERROR_INVALID_CALLBACK;
924 /* Return error status */
925 status = HAL_ERROR;
926 }
927
928 /* Release Lock */
929 __HAL_UNLOCK(hjpeg);
930 return status;
931 }
932
933 /**
934 * @brief UnRegister the Info Ready JPEG Callback
935 * Info Ready JPEG Callback is redirected to the weak HAL_JPEG_InfoReadyCallback() predefined callback
936 * @param hjpeg JPEG handle
937 * @retval HAL status
938 */
HAL_JPEG_UnRegisterInfoReadyCallback(JPEG_HandleTypeDef * hjpeg)939 HAL_StatusTypeDef HAL_JPEG_UnRegisterInfoReadyCallback(JPEG_HandleTypeDef *hjpeg)
940 {
941 HAL_StatusTypeDef status = HAL_OK;
942
943 /* Process locked */
944 __HAL_LOCK(hjpeg);
945
946 if (HAL_JPEG_STATE_READY == hjpeg->State)
947 {
948 hjpeg->InfoReadyCallback = HAL_JPEG_InfoReadyCallback; /* Legacy weak InfoReadyCallback */
949 }
950 else
951 {
952 /* Update the error code */
953 hjpeg->ErrorCode |= HAL_JPEG_ERROR_INVALID_CALLBACK;
954 /* Return error status */
955 status = HAL_ERROR;
956 }
957
958 /* Release Lock */
959 __HAL_UNLOCK(hjpeg);
960 return status;
961 }
962
963 /**
964 * @brief Register Get Data JPEG Callback
965 * To be used instead of the weak HAL_JPEG_GetDataCallback() predefined callback
966 * @param hjpeg JPEG handle
967 * @param pCallback pointer to the Get Data Callback function
968 * @retval HAL status
969 */
HAL_JPEG_RegisterGetDataCallback(JPEG_HandleTypeDef * hjpeg,pJPEG_GetDataCallbackTypeDef pCallback)970 HAL_StatusTypeDef HAL_JPEG_RegisterGetDataCallback(JPEG_HandleTypeDef *hjpeg, pJPEG_GetDataCallbackTypeDef pCallback)
971 {
972 HAL_StatusTypeDef status = HAL_OK;
973
974 if (pCallback == NULL)
975 {
976 /* Update the error code */
977 hjpeg->ErrorCode |= HAL_JPEG_ERROR_INVALID_CALLBACK;
978 return HAL_ERROR;
979 }
980 /* Process locked */
981 __HAL_LOCK(hjpeg);
982
983 if (HAL_JPEG_STATE_READY == hjpeg->State)
984 {
985 hjpeg->GetDataCallback = pCallback;
986 }
987 else
988 {
989 /* Update the error code */
990 hjpeg->ErrorCode |= HAL_JPEG_ERROR_INVALID_CALLBACK;
991 /* Return error status */
992 status = HAL_ERROR;
993 }
994
995 /* Release Lock */
996 __HAL_UNLOCK(hjpeg);
997 return status;
998 }
999
1000 /**
1001 * @brief UnRegister the Get Data JPEG Callback
1002 * Get Data JPEG Callback is redirected to the weak HAL_JPEG_GetDataCallback() predefined callback
1003 * @param hjpeg JPEG handle
1004 * @retval HAL status
1005 */
HAL_JPEG_UnRegisterGetDataCallback(JPEG_HandleTypeDef * hjpeg)1006 HAL_StatusTypeDef HAL_JPEG_UnRegisterGetDataCallback(JPEG_HandleTypeDef *hjpeg)
1007 {
1008 HAL_StatusTypeDef status = HAL_OK;
1009
1010 /* Process locked */
1011 __HAL_LOCK(hjpeg);
1012
1013 if (HAL_JPEG_STATE_READY == hjpeg->State)
1014 {
1015 hjpeg->GetDataCallback = HAL_JPEG_GetDataCallback; /* Legacy weak GetDataCallback */
1016 }
1017 else
1018 {
1019 /* Update the error code */
1020 hjpeg->ErrorCode |= HAL_JPEG_ERROR_INVALID_CALLBACK;
1021 /* Return error status */
1022 status = HAL_ERROR;
1023 }
1024
1025 /* Release Lock */
1026 __HAL_UNLOCK(hjpeg);
1027 return status;
1028 }
1029
1030 /**
1031 * @brief Register Data Ready JPEG Callback
1032 * To be used instead of the weak HAL_JPEG_DataReadyCallback() predefined callback
1033 * @param hjpeg JPEG handle
1034 * @param pCallback pointer to the Get Data Callback function
1035 * @retval HAL status
1036 */
HAL_JPEG_RegisterDataReadyCallback(JPEG_HandleTypeDef * hjpeg,pJPEG_DataReadyCallbackTypeDef pCallback)1037 HAL_StatusTypeDef HAL_JPEG_RegisterDataReadyCallback(JPEG_HandleTypeDef *hjpeg,
1038 pJPEG_DataReadyCallbackTypeDef pCallback)
1039 {
1040 HAL_StatusTypeDef status = HAL_OK;
1041
1042 if (pCallback == NULL)
1043 {
1044 /* Update the error code */
1045 hjpeg->ErrorCode |= HAL_JPEG_ERROR_INVALID_CALLBACK;
1046 return HAL_ERROR;
1047 }
1048 /* Process locked */
1049 __HAL_LOCK(hjpeg);
1050
1051 if (HAL_JPEG_STATE_READY == hjpeg->State)
1052 {
1053 hjpeg->DataReadyCallback = pCallback;
1054 }
1055 else
1056 {
1057 /* Update the error code */
1058 hjpeg->ErrorCode |= HAL_JPEG_ERROR_INVALID_CALLBACK;
1059 /* Return error status */
1060 status = HAL_ERROR;
1061 }
1062
1063 /* Release Lock */
1064 __HAL_UNLOCK(hjpeg);
1065 return status;
1066 }
1067
1068 /**
1069 * @brief UnRegister the Data Ready JPEG Callback
1070 * Get Data Ready Callback is redirected to the weak HAL_JPEG_DataReadyCallback() predefined callback
1071 * @param hjpeg JPEG handle
1072 * @retval HAL status
1073 */
HAL_JPEG_UnRegisterDataReadyCallback(JPEG_HandleTypeDef * hjpeg)1074 HAL_StatusTypeDef HAL_JPEG_UnRegisterDataReadyCallback(JPEG_HandleTypeDef *hjpeg)
1075 {
1076 HAL_StatusTypeDef status = HAL_OK;
1077
1078 /* Process locked */
1079 __HAL_LOCK(hjpeg);
1080
1081 if (HAL_JPEG_STATE_READY == hjpeg->State)
1082 {
1083 hjpeg->DataReadyCallback = HAL_JPEG_DataReadyCallback; /* Legacy weak DataReadyCallback */
1084 }
1085 else
1086 {
1087 /* Update the error code */
1088 hjpeg->ErrorCode |= HAL_JPEG_ERROR_INVALID_CALLBACK;
1089 /* Return error status */
1090 status = HAL_ERROR;
1091 }
1092
1093 /* Release Lock */
1094 __HAL_UNLOCK(hjpeg);
1095 return status;
1096 }
1097
1098 #endif /* USE_HAL_JPEG_REGISTER_CALLBACKS */
1099
1100 /**
1101 * @}
1102 */
1103
1104 /** @defgroup JPEG_Exported_Functions_Group2 Configuration functions
1105 * @brief JPEG Configuration functions.
1106 *
1107 @verbatim
1108 ==============================================================================
1109 ##### Configuration functions #####
1110 ==============================================================================
1111 [..] This section provides functions allowing to:
1112 (+) HAL_JPEG_ConfigEncoding() : JPEG encoding configuration
1113 (+) HAL_JPEG_GetInfo() : Extract the image configuration from the JPEG header during the decoding
1114 (+) HAL_JPEG_EnableHeaderParsing() : Enable JPEG Header parsing for decoding
1115 (+) HAL_JPEG_DisableHeaderParsing() : Disable JPEG Header parsing for decoding
1116 (+) HAL_JPEG_SetUserQuantTables : Modify the default Quantization tables used for JPEG encoding.
1117
1118 @endverbatim
1119 * @{
1120 */
1121
1122 /**
1123 * @brief Set the JPEG encoding configuration.
1124 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
1125 * the configuration information for JPEG module
1126 * @param pConf pointer to a JPEG_ConfTypeDef structure that contains
1127 * the encoding configuration
1128 * @retval HAL status
1129 */
HAL_JPEG_ConfigEncoding(JPEG_HandleTypeDef * hjpeg,const JPEG_ConfTypeDef * pConf)1130 HAL_StatusTypeDef HAL_JPEG_ConfigEncoding(JPEG_HandleTypeDef *hjpeg, const JPEG_ConfTypeDef *pConf)
1131 {
1132 uint32_t error;
1133 uint32_t numberMCU;
1134 uint32_t hfactor;
1135 uint32_t vfactor;
1136 uint32_t hMCU;
1137 uint32_t vMCU;
1138
1139 /* Check the JPEG handle allocation */
1140 if ((hjpeg == NULL) || (pConf == NULL))
1141 {
1142 return HAL_ERROR;
1143 }
1144 else
1145 {
1146 /* Check the parameters */
1147 assert_param(IS_JPEG_COLORSPACE(pConf->ColorSpace));
1148 assert_param(IS_JPEG_CHROMASUBSAMPLING(pConf->ChromaSubsampling));
1149 assert_param(IS_JPEG_IMAGE_QUALITY(pConf->ImageQuality));
1150
1151 /* Process Locked */
1152 __HAL_LOCK(hjpeg);
1153
1154 if (hjpeg->State == HAL_JPEG_STATE_READY)
1155 {
1156 hjpeg->State = HAL_JPEG_STATE_BUSY;
1157
1158 hjpeg->Conf.ColorSpace = pConf->ColorSpace;
1159 hjpeg->Conf.ChromaSubsampling = pConf->ChromaSubsampling;
1160 hjpeg->Conf.ImageHeight = pConf->ImageHeight;
1161 hjpeg->Conf.ImageWidth = pConf->ImageWidth;
1162 hjpeg->Conf.ImageQuality = pConf->ImageQuality;
1163
1164 /* Reset the Color Space : by default only one quantization table is used*/
1165 hjpeg->Instance->CONFR1 &= ~JPEG_CONFR1_COLORSPACE;
1166
1167 /* Set Number of color components*/
1168 if (hjpeg->Conf.ColorSpace == JPEG_GRAYSCALE_COLORSPACE)
1169 {
1170 /*Gray Scale is only one component 8x8 blocks i.e 4:4:4*/
1171 hjpeg->Conf.ChromaSubsampling = JPEG_444_SUBSAMPLING;
1172
1173 JPEG_SetColorGrayScale(hjpeg);
1174 /* Set quantization table 0*/
1175 error = JPEG_Set_Quantization_Mem(hjpeg, hjpeg->QuantTable0, (hjpeg->Instance->QMEM0));
1176 }
1177 else if (hjpeg->Conf.ColorSpace == JPEG_YCBCR_COLORSPACE)
1178 {
1179 /*
1180 Set the Color Space for YCbCr : 2 quantization tables are used
1181 one for Luminance(Y) and one for both Chrominances (Cb & Cr)
1182 */
1183 hjpeg->Instance->CONFR1 |= JPEG_CONFR1_COLORSPACE_0;
1184
1185 JPEG_SetColorYCBCR(hjpeg);
1186
1187 /* Set quantization table 0*/
1188 error = JPEG_Set_Quantization_Mem(hjpeg, hjpeg->QuantTable0, (hjpeg->Instance->QMEM0));
1189 /*By default quantization table 0 for component 0 and quantization table 1 for both components 1 and 2*/
1190 (void) JPEG_Set_Quantization_Mem(hjpeg, hjpeg->QuantTable1, (hjpeg->Instance->QMEM1));
1191
1192 if ((hjpeg->Context & JPEG_CONTEXT_CUSTOM_TABLES) != 0UL)
1193 {
1194 /*Use user customized quantization tables , 1 table per component*/
1195 /* use 3 quantization tables , one for each component*/
1196 hjpeg->Instance->CONFR1 &= (~JPEG_CONFR1_COLORSPACE);
1197 hjpeg->Instance->CONFR1 |= JPEG_CONFR1_COLORSPACE_1;
1198
1199 (void) JPEG_Set_Quantization_Mem(hjpeg, hjpeg->QuantTable2, (hjpeg->Instance->QMEM2));
1200
1201 /*Use Quantization 1 table for component 1*/
1202 hjpeg->Instance->CONFR5 &= (~JPEG_CONFR5_QT);
1203 hjpeg->Instance->CONFR5 |= JPEG_CONFR5_QT_0;
1204
1205 /*Use Quantization 2 table for component 2*/
1206 hjpeg->Instance->CONFR6 &= (~JPEG_CONFR6_QT);
1207 hjpeg->Instance->CONFR6 |= JPEG_CONFR6_QT_1;
1208 }
1209 }
1210 else /* ColorSpace == JPEG_CMYK_COLORSPACE */
1211 {
1212 JPEG_SetColorCMYK(hjpeg);
1213
1214 /* Set quantization table 0*/
1215 error = JPEG_Set_Quantization_Mem(hjpeg, hjpeg->QuantTable0, (hjpeg->Instance->QMEM0));
1216 /*By default quantization table 0 for All components*/
1217
1218 if ((hjpeg->Context & JPEG_CONTEXT_CUSTOM_TABLES) != 0UL)
1219 {
1220 /*Use user customized quantization tables , 1 table per component*/
1221 /* use 4 quantization tables , one for each component*/
1222 hjpeg->Instance->CONFR1 |= JPEG_CONFR1_COLORSPACE;
1223
1224 (void) JPEG_Set_Quantization_Mem(hjpeg, hjpeg->QuantTable1, (hjpeg->Instance->QMEM1));
1225 (void) JPEG_Set_Quantization_Mem(hjpeg, hjpeg->QuantTable2, (hjpeg->Instance->QMEM2));
1226 (void) JPEG_Set_Quantization_Mem(hjpeg, hjpeg->QuantTable3, (hjpeg->Instance->QMEM3));
1227
1228 /*Use Quantization 1 table for component 1*/
1229 hjpeg->Instance->CONFR5 |= JPEG_CONFR5_QT_0;
1230
1231 /*Use Quantization 2 table for component 2*/
1232 hjpeg->Instance->CONFR6 |= JPEG_CONFR6_QT_1;
1233
1234 /*Use Quantization 3 table for component 3*/
1235 hjpeg->Instance->CONFR7 |= JPEG_CONFR7_QT;
1236 }
1237 }
1238
1239 if (error != 0UL)
1240 {
1241 hjpeg->ErrorCode = HAL_JPEG_ERROR_QUANT_TABLE;
1242
1243 /* Process Unlocked */
1244 __HAL_UNLOCK(hjpeg);
1245
1246 /* Set the JPEG State to ready */
1247 hjpeg->State = HAL_JPEG_STATE_READY;
1248
1249 return HAL_ERROR;
1250 }
1251 /* Set the image size*/
1252 /* set the number of lines*/
1253 MODIFY_REG(hjpeg->Instance->CONFR1, JPEG_CONFR1_YSIZE, ((hjpeg->Conf.ImageHeight & 0x0000FFFFUL) << 16));
1254 /* set the number of pixels per line*/
1255 MODIFY_REG(hjpeg->Instance->CONFR3, JPEG_CONFR3_XSIZE, ((hjpeg->Conf.ImageWidth & 0x0000FFFFUL) << 16));
1256
1257
1258 if (hjpeg->Conf.ChromaSubsampling == JPEG_420_SUBSAMPLING) /* 4:2:0*/
1259 {
1260 hfactor = 16;
1261 vfactor = 16;
1262 }
1263 else if (hjpeg->Conf.ChromaSubsampling == JPEG_422_SUBSAMPLING) /* 4:2:2*/
1264 {
1265 hfactor = 16;
1266 vfactor = 8;
1267 }
1268 else /* Default is 8x8 MCU, 4:4:4*/
1269 {
1270 hfactor = 8;
1271 vfactor = 8;
1272 }
1273
1274 hMCU = (hjpeg->Conf.ImageWidth / hfactor);
1275 if ((hjpeg->Conf.ImageWidth % hfactor) != 0UL)
1276 {
1277 hMCU++; /*+1 for horizontal incomplete MCU */
1278 }
1279
1280 vMCU = (hjpeg->Conf.ImageHeight / vfactor);
1281 if ((hjpeg->Conf.ImageHeight % vfactor) != 0UL)
1282 {
1283 vMCU++; /*+1 for vertical incomplete MCU */
1284 }
1285
1286 numberMCU = (hMCU * vMCU) - 1UL; /* Bit Field JPEG_CONFR2_NMCU shall be set to NB_MCU - 1*/
1287 /* Set the number of MCU*/
1288 hjpeg->Instance->CONFR2 = (numberMCU & JPEG_CONFR2_NMCU);
1289
1290 hjpeg->Context |= JPEG_CONTEXT_CONF_ENCODING;
1291
1292 /* Process Unlocked */
1293 __HAL_UNLOCK(hjpeg);
1294
1295 /* Set the JPEG State to ready */
1296 hjpeg->State = HAL_JPEG_STATE_READY;
1297
1298 /* Return function status */
1299 return HAL_OK;
1300 }
1301 else
1302 {
1303 /* Process Unlocked */
1304 __HAL_UNLOCK(hjpeg);
1305
1306 /* Return function status */
1307 return HAL_BUSY;
1308 }
1309 }
1310 }
1311
1312 /**
1313 * @brief Extract the image configuration from the JPEG header during the decoding
1314 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
1315 * the configuration information for JPEG module
1316 * @param pInfo pointer to a JPEG_ConfTypeDef structure that contains
1317 * The JPEG decoded header information
1318 * @retval HAL status
1319 */
HAL_JPEG_GetInfo(JPEG_HandleTypeDef * hjpeg,JPEG_ConfTypeDef * pInfo)1320 HAL_StatusTypeDef HAL_JPEG_GetInfo(JPEG_HandleTypeDef *hjpeg, JPEG_ConfTypeDef *pInfo)
1321 {
1322 uint32_t yblockNb;
1323 uint32_t cBblockNb;
1324 uint32_t cRblockNb;
1325
1326 /* Check the JPEG handle allocation */
1327 if ((hjpeg == NULL) || (pInfo == NULL))
1328 {
1329 return HAL_ERROR;
1330 }
1331
1332 /*Read the conf parameters */
1333 if ((hjpeg->Instance->CONFR1 & JPEG_CONFR1_NF) == JPEG_CONFR1_NF_1)
1334 {
1335 pInfo->ColorSpace = JPEG_YCBCR_COLORSPACE;
1336 }
1337 else if ((hjpeg->Instance->CONFR1 & JPEG_CONFR1_NF) == 0UL)
1338 {
1339 pInfo->ColorSpace = JPEG_GRAYSCALE_COLORSPACE;
1340 }
1341 else if ((hjpeg->Instance->CONFR1 & JPEG_CONFR1_NF) == JPEG_CONFR1_NF)
1342 {
1343 pInfo->ColorSpace = JPEG_CMYK_COLORSPACE;
1344 }
1345 else
1346 {
1347 return HAL_ERROR;
1348 }
1349
1350 pInfo->ImageHeight = (hjpeg->Instance->CONFR1 & 0xFFFF0000UL) >> 16;
1351 pInfo->ImageWidth = (hjpeg->Instance->CONFR3 & 0xFFFF0000UL) >> 16;
1352
1353 if ((pInfo->ColorSpace == JPEG_YCBCR_COLORSPACE) || (pInfo->ColorSpace == JPEG_CMYK_COLORSPACE))
1354 {
1355 yblockNb = (hjpeg->Instance->CONFR4 & JPEG_CONFR4_NB) >> 4;
1356 cBblockNb = (hjpeg->Instance->CONFR5 & JPEG_CONFR5_NB) >> 4;
1357 cRblockNb = (hjpeg->Instance->CONFR6 & JPEG_CONFR6_NB) >> 4;
1358
1359 if ((yblockNb == 1UL) && (cBblockNb == 0UL) && (cRblockNb == 0UL))
1360 {
1361 pInfo->ChromaSubsampling = JPEG_422_SUBSAMPLING; /*16x8 block*/
1362 }
1363 else if ((yblockNb == 0UL) && (cBblockNb == 0UL) && (cRblockNb == 0UL))
1364 {
1365 pInfo->ChromaSubsampling = JPEG_444_SUBSAMPLING;
1366 }
1367 else if ((yblockNb == 3UL) && (cBblockNb == 0UL) && (cRblockNb == 0UL))
1368 {
1369 pInfo->ChromaSubsampling = JPEG_420_SUBSAMPLING;
1370 }
1371 else /*Default is 4:4:4*/
1372 {
1373 pInfo->ChromaSubsampling = JPEG_444_SUBSAMPLING;
1374 }
1375 }
1376 else
1377 {
1378 pInfo->ChromaSubsampling = JPEG_444_SUBSAMPLING;
1379 }
1380
1381 pInfo->ImageQuality = JPEG_GetQuality(hjpeg);
1382
1383 /* Return function status */
1384 return HAL_OK;
1385 }
1386
1387 /**
1388 * @brief Enable JPEG Header parsing for decoding
1389 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
1390 * the configuration information for the JPEG.
1391 * @retval HAL status
1392 */
HAL_JPEG_EnableHeaderParsing(JPEG_HandleTypeDef * hjpeg)1393 HAL_StatusTypeDef HAL_JPEG_EnableHeaderParsing(JPEG_HandleTypeDef *hjpeg)
1394 {
1395 /* Process locked */
1396 __HAL_LOCK(hjpeg);
1397
1398 if (hjpeg->State == HAL_JPEG_STATE_READY)
1399 {
1400 /* Change the JPEG state */
1401 hjpeg->State = HAL_JPEG_STATE_BUSY;
1402
1403 /* Enable header processing*/
1404 hjpeg->Instance->CONFR1 |= JPEG_CONFR1_HDR;
1405
1406 /* Process unlocked */
1407 __HAL_UNLOCK(hjpeg);
1408
1409 /* Change the JPEG state */
1410 hjpeg->State = HAL_JPEG_STATE_READY;
1411
1412 return HAL_OK;
1413 }
1414 else
1415 {
1416 /* Process unlocked */
1417 __HAL_UNLOCK(hjpeg);
1418
1419 return HAL_BUSY;
1420 }
1421 }
1422
1423 /**
1424 * @brief Disable JPEG Header parsing for decoding
1425 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
1426 * the configuration information for the JPEG.
1427 * @retval HAL status
1428 */
HAL_JPEG_DisableHeaderParsing(JPEG_HandleTypeDef * hjpeg)1429 HAL_StatusTypeDef HAL_JPEG_DisableHeaderParsing(JPEG_HandleTypeDef *hjpeg)
1430 {
1431 /* Process locked */
1432 __HAL_LOCK(hjpeg);
1433
1434 if (hjpeg->State == HAL_JPEG_STATE_READY)
1435 {
1436 /* Change the JPEG state */
1437 hjpeg->State = HAL_JPEG_STATE_BUSY;
1438
1439 /* Disable header processing*/
1440 hjpeg->Instance->CONFR1 &= ~JPEG_CONFR1_HDR;
1441
1442 /* Process unlocked */
1443 __HAL_UNLOCK(hjpeg);
1444
1445 /* Change the JPEG state */
1446 hjpeg->State = HAL_JPEG_STATE_READY;
1447
1448 return HAL_OK;
1449 }
1450 else
1451 {
1452 /* Process unlocked */
1453 __HAL_UNLOCK(hjpeg);
1454
1455 return HAL_BUSY;
1456 }
1457 }
1458
1459 /**
1460 * @brief Modify the default Quantization tables used for JPEG encoding.
1461 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
1462 * the configuration information for JPEG module
1463 * @param QTable0 pointer to uint8_t , define the user quantification table for color component 1.
1464 * If NULL assume no need to update the table and no error return
1465 * @param QTable1 pointer to uint8_t , define the user quantification table for color component 2.
1466 * If NULL assume no need to update the table and no error return.
1467 * @param QTable2 pointer to uint8_t , define the user quantification table for color component 3,
1468 * If NULL assume no need to update the table and no error return.
1469 * @param QTable3 pointer to uint8_t , define the user quantification table for color component 4.
1470 * If NULL assume no need to update the table and no error return.
1471 *
1472 * @retval HAL status
1473 */
1474
1475
HAL_JPEG_SetUserQuantTables(JPEG_HandleTypeDef * hjpeg,uint8_t * QTable0,uint8_t * QTable1,uint8_t * QTable2,uint8_t * QTable3)1476 HAL_StatusTypeDef HAL_JPEG_SetUserQuantTables(JPEG_HandleTypeDef *hjpeg, uint8_t *QTable0, uint8_t *QTable1,
1477 uint8_t *QTable2, uint8_t *QTable3)
1478 {
1479 /* Process Locked */
1480 __HAL_LOCK(hjpeg);
1481
1482 if (hjpeg->State == HAL_JPEG_STATE_READY)
1483 {
1484 /* Change the DMA state */
1485 hjpeg->State = HAL_JPEG_STATE_BUSY;
1486
1487 hjpeg->Context |= JPEG_CONTEXT_CUSTOM_TABLES;
1488
1489 hjpeg->QuantTable0 = QTable0;
1490 hjpeg->QuantTable1 = QTable1;
1491 hjpeg->QuantTable2 = QTable2;
1492 hjpeg->QuantTable3 = QTable3;
1493
1494 /* Process Unlocked */
1495 __HAL_UNLOCK(hjpeg);
1496
1497 /* Change the DMA state */
1498 hjpeg->State = HAL_JPEG_STATE_READY;
1499
1500 /* Return function status */
1501 return HAL_OK;
1502 }
1503 else
1504 {
1505 /* Process Unlocked */
1506 __HAL_UNLOCK(hjpeg);
1507
1508 return HAL_BUSY;
1509 }
1510 }
1511
1512 /**
1513 * @}
1514 */
1515
1516 /** @defgroup JPEG_Exported_Functions_Group3 encoding/decoding processing functions
1517 * @brief processing functions.
1518 *
1519 @verbatim
1520 ==============================================================================
1521 ##### JPEG processing functions #####
1522 ==============================================================================
1523 [..] This section provides functions allowing to:
1524 (+) HAL_JPEG_Encode() : JPEG encoding with polling process
1525 (+) HAL_JPEG_Decode() : JPEG decoding with polling process
1526 (+) HAL_JPEG_Encode_IT() : JPEG encoding with interrupt process
1527 (+) HAL_JPEG_Decode_IT() : JPEG decoding with interrupt process
1528 (+) HAL_JPEG_Encode_DMA() : JPEG encoding with DMA process
1529 (+) HAL_JPEG_Decode_DMA() : JPEG decoding with DMA process
1530 (+) HAL_JPEG_Pause() : Pause the Input/Output processing
1531 (+) HAL_JPEG_Resume() : Resume the JPEG Input/Output processing
1532 (+) HAL_JPEG_ConfigInputBuffer() : Config Encoding/Decoding Input Buffer
1533 (+) HAL_JPEG_ConfigOutputBuffer() : Config Encoding/Decoding Output Buffer
1534 (+) HAL_JPEG_Abort() : Aborts the JPEG Encoding/Decoding
1535
1536 @endverbatim
1537 * @{
1538 */
1539
1540 /**
1541 * @brief Starts JPEG encoding with polling processing
1542 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
1543 * the configuration information for JPEG module
1544 * @param pDataInMCU Pointer to the Input buffer
1545 * @param InDataLength size in bytes Input buffer
1546 * @param pDataOut Pointer to the jpeg output data buffer
1547 * @param OutDataLength size in bytes of the Output buffer
1548 * @param Timeout Specify Timeout value
1549 * @retval HAL status
1550 */
HAL_JPEG_Encode(JPEG_HandleTypeDef * hjpeg,uint8_t * pDataInMCU,uint32_t InDataLength,uint8_t * pDataOut,uint32_t OutDataLength,uint32_t Timeout)1551 HAL_StatusTypeDef HAL_JPEG_Encode(JPEG_HandleTypeDef *hjpeg, uint8_t *pDataInMCU, uint32_t InDataLength,
1552 uint8_t *pDataOut, uint32_t OutDataLength, uint32_t Timeout)
1553 {
1554 uint32_t tickstart;
1555
1556 /* Check the parameters */
1557 assert_param((InDataLength >= 4UL));
1558 assert_param((OutDataLength >= 4UL));
1559
1560 /* Check In/out buffer allocation and size */
1561 if ((hjpeg == NULL) || (pDataInMCU == NULL) || (pDataOut == NULL))
1562 {
1563 return HAL_ERROR;
1564 }
1565 /* Process locked */
1566 __HAL_LOCK(hjpeg);
1567
1568 if (hjpeg->State != HAL_JPEG_STATE_READY)
1569 {
1570 /* Process Unlocked */
1571 __HAL_UNLOCK(hjpeg);
1572
1573 return HAL_BUSY;
1574 }
1575
1576 if (hjpeg->State == HAL_JPEG_STATE_READY)
1577 {
1578 if ((hjpeg->Context & JPEG_CONTEXT_CONF_ENCODING) == JPEG_CONTEXT_CONF_ENCODING)
1579 {
1580 /*Change JPEG state*/
1581 hjpeg->State = HAL_JPEG_STATE_BUSY_ENCODING;
1582
1583 /*Set the Context to Encode with Polling*/
1584 hjpeg->Context &= ~(JPEG_CONTEXT_OPERATION_MASK | JPEG_CONTEXT_METHOD_MASK);
1585 hjpeg->Context |= (JPEG_CONTEXT_ENCODE | JPEG_CONTEXT_POLLING);
1586
1587 /* Get tick */
1588 tickstart = HAL_GetTick();
1589
1590 /*Store In/out buffers pointers and size*/
1591 hjpeg->pJpegInBuffPtr = pDataInMCU;
1592 hjpeg->pJpegOutBuffPtr = pDataOut;
1593 hjpeg->InDataLength = InDataLength - (InDataLength % 4UL); /* In Data length must be multiple
1594 of 4 Bytes (1 word)*/
1595 hjpeg->OutDataLength = OutDataLength - (OutDataLength % 4UL); /* Out Data length must be multiple
1596 of 4 Bytes (1 word)*/
1597
1598 /*Reset In/out data counter */
1599 hjpeg->JpegInCount = 0;
1600 hjpeg->JpegOutCount = 0;
1601
1602 /*Init decoding process*/
1603 JPEG_Init_Process(hjpeg);
1604
1605 /*JPEG data processing : In/Out FIFO transfer*/
1606 while ((JPEG_Process(hjpeg) == JPEG_PROCESS_ONGOING))
1607 {
1608 if (Timeout != HAL_MAX_DELAY)
1609 {
1610 if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0UL))
1611 {
1612
1613 /* Update error code */
1614 hjpeg->ErrorCode |= HAL_JPEG_ERROR_TIMEOUT;
1615
1616 /* Process Unlocked */
1617 __HAL_UNLOCK(hjpeg);
1618
1619 /*Change JPEG state*/
1620 hjpeg->State = HAL_JPEG_STATE_READY;
1621
1622 return HAL_TIMEOUT;
1623 }
1624 }
1625 }
1626
1627 /* Process Unlocked */
1628 __HAL_UNLOCK(hjpeg);
1629
1630 /*Change JPEG state*/
1631 hjpeg->State = HAL_JPEG_STATE_READY;
1632
1633 }
1634 else
1635 {
1636 /* Process Unlocked */
1637 __HAL_UNLOCK(hjpeg);
1638
1639 return HAL_ERROR;
1640 }
1641 }
1642 /* Return function status */
1643 return HAL_OK;
1644 }
1645
1646 /**
1647 * @brief Starts JPEG decoding with polling processing
1648 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
1649 * the configuration information for JPEG module
1650 * @param pDataIn Pointer to the input data buffer
1651 * @param InDataLength size in bytes Input buffer
1652 * @param pDataOutMCU Pointer to the Output data buffer
1653 * @param OutDataLength size in bytes of the Output buffer
1654 * @param Timeout Specify Timeout value
1655 * @retval HAL status
1656 */
HAL_JPEG_Decode(JPEG_HandleTypeDef * hjpeg,uint8_t * pDataIn,uint32_t InDataLength,uint8_t * pDataOutMCU,uint32_t OutDataLength,uint32_t Timeout)1657 HAL_StatusTypeDef HAL_JPEG_Decode(JPEG_HandleTypeDef *hjpeg, uint8_t *pDataIn, uint32_t InDataLength,
1658 uint8_t *pDataOutMCU, uint32_t OutDataLength, uint32_t Timeout)
1659 {
1660 uint32_t tickstart;
1661
1662 /* Check the parameters */
1663 assert_param((InDataLength >= 4UL));
1664 assert_param((OutDataLength >= 4UL));
1665
1666 /* Check In/out buffer allocation and size */
1667 if ((hjpeg == NULL) || (pDataIn == NULL) || (pDataOutMCU == NULL))
1668 {
1669 return HAL_ERROR;
1670 }
1671
1672 /* Process Locked */
1673 __HAL_LOCK(hjpeg);
1674
1675 /* Get tick */
1676 tickstart = HAL_GetTick();
1677
1678 if (hjpeg->State == HAL_JPEG_STATE_READY)
1679 {
1680 /*Change JPEG state*/
1681 hjpeg->State = HAL_JPEG_STATE_BUSY_DECODING;
1682
1683 /*Set the Context to Decode with Polling*/
1684 /*Set the Context to Encode with Polling*/
1685 hjpeg->Context &= ~(JPEG_CONTEXT_OPERATION_MASK | JPEG_CONTEXT_METHOD_MASK);
1686 hjpeg->Context |= (JPEG_CONTEXT_DECODE | JPEG_CONTEXT_POLLING);
1687
1688 /*Store In/out buffers pointers and size*/
1689 hjpeg->pJpegInBuffPtr = pDataIn;
1690 hjpeg->pJpegOutBuffPtr = pDataOutMCU;
1691 hjpeg->InDataLength = InDataLength - (InDataLength % 4UL); /*In Data length must be multiple
1692 of 4 Bytes (1 word)*/
1693 hjpeg->OutDataLength = OutDataLength - (OutDataLength % 4UL); /*Out Data length must be multiple
1694 of 4 Bytes (1 word)*/
1695
1696 /*Reset In/out data counter */
1697 hjpeg->JpegInCount = 0;
1698 hjpeg->JpegOutCount = 0;
1699
1700 /*Init decoding process*/
1701 JPEG_Init_Process(hjpeg);
1702
1703 /*JPEG data processing : In/Out FIFO transfer*/
1704 while ((JPEG_Process(hjpeg) == JPEG_PROCESS_ONGOING))
1705 {
1706 if (Timeout != HAL_MAX_DELAY)
1707 {
1708 if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0UL))
1709 {
1710
1711 /* Update error code */
1712 hjpeg->ErrorCode |= HAL_JPEG_ERROR_TIMEOUT;
1713
1714 /* Process Unlocked */
1715 __HAL_UNLOCK(hjpeg);
1716
1717 /*Change JPEG state*/
1718 hjpeg->State = HAL_JPEG_STATE_READY;
1719
1720 return HAL_TIMEOUT;
1721 }
1722 }
1723 }
1724
1725 /* Process Unlocked */
1726 __HAL_UNLOCK(hjpeg);
1727
1728 /*Change JPEG state*/
1729 hjpeg->State = HAL_JPEG_STATE_READY;
1730
1731 }
1732 else
1733 {
1734 /* Process Unlocked */
1735 __HAL_UNLOCK(hjpeg);
1736
1737 return HAL_BUSY;
1738 }
1739 /* Return function status */
1740 return HAL_OK;
1741 }
1742
1743 /**
1744 * @brief Starts JPEG encoding with interrupt processing
1745 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
1746 * the configuration information for JPEG module
1747 * @param pDataInMCU Pointer to the Input buffer
1748 * @param InDataLength size in bytes Input buffer
1749 * @param pDataOut Pointer to the jpeg output data buffer
1750 * @param OutDataLength size in bytes of the Output buffer
1751 * @retval HAL status
1752 */
HAL_JPEG_Encode_IT(JPEG_HandleTypeDef * hjpeg,uint8_t * pDataInMCU,uint32_t InDataLength,uint8_t * pDataOut,uint32_t OutDataLength)1753 HAL_StatusTypeDef HAL_JPEG_Encode_IT(JPEG_HandleTypeDef *hjpeg, uint8_t *pDataInMCU, uint32_t InDataLength,
1754 uint8_t *pDataOut, uint32_t OutDataLength)
1755 {
1756 /* Check the parameters */
1757 assert_param((InDataLength >= 4UL));
1758 assert_param((OutDataLength >= 4UL));
1759
1760 /* Check In/out buffer allocation and size */
1761 if ((hjpeg == NULL) || (pDataInMCU == NULL) || (pDataOut == NULL))
1762 {
1763 return HAL_ERROR;
1764 }
1765
1766 /* Process Locked */
1767 __HAL_LOCK(hjpeg);
1768
1769 if (hjpeg->State != HAL_JPEG_STATE_READY)
1770 {
1771 /* Process Unlocked */
1772 __HAL_UNLOCK(hjpeg);
1773
1774 return HAL_BUSY;
1775 }
1776 else
1777 {
1778 if ((hjpeg->Context & JPEG_CONTEXT_CONF_ENCODING) == JPEG_CONTEXT_CONF_ENCODING)
1779 {
1780 /*Change JPEG state*/
1781 hjpeg->State = HAL_JPEG_STATE_BUSY_ENCODING;
1782
1783 /*Set the Context to Encode with IT*/
1784 hjpeg->Context &= ~(JPEG_CONTEXT_OPERATION_MASK | JPEG_CONTEXT_METHOD_MASK);
1785 hjpeg->Context |= (JPEG_CONTEXT_ENCODE | JPEG_CONTEXT_IT);
1786
1787 /*Store In/out buffers pointers and size*/
1788 hjpeg->pJpegInBuffPtr = pDataInMCU;
1789 hjpeg->pJpegOutBuffPtr = pDataOut;
1790 hjpeg->InDataLength = InDataLength - (InDataLength % 4UL); /*In Data length must be multiple
1791 of 4 Bytes (1 word)*/
1792 hjpeg->OutDataLength = OutDataLength - (OutDataLength % 4UL); /*Out Data length must be multiple
1793 of 4 Bytes (1 word)*/
1794
1795 /*Reset In/out data counter */
1796 hjpeg->JpegInCount = 0;
1797 hjpeg->JpegOutCount = 0;
1798
1799 /*Init decoding process*/
1800 JPEG_Init_Process(hjpeg);
1801
1802 }
1803 else
1804 {
1805 /* Process Unlocked */
1806 __HAL_UNLOCK(hjpeg);
1807
1808 return HAL_ERROR;
1809 }
1810 }
1811 /* Return function status */
1812 return HAL_OK;
1813 }
1814
1815 /**
1816 * @brief Starts JPEG decoding with interrupt processing
1817 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
1818 * the configuration information for JPEG module
1819 * @param pDataIn Pointer to the input data buffer
1820 * @param InDataLength size in bytes Input buffer
1821 * @param pDataOutMCU Pointer to the Output data buffer
1822 * @param OutDataLength size in bytes of the Output buffer
1823 * @retval HAL status
1824 */
HAL_JPEG_Decode_IT(JPEG_HandleTypeDef * hjpeg,uint8_t * pDataIn,uint32_t InDataLength,uint8_t * pDataOutMCU,uint32_t OutDataLength)1825 HAL_StatusTypeDef HAL_JPEG_Decode_IT(JPEG_HandleTypeDef *hjpeg, uint8_t *pDataIn, uint32_t InDataLength,
1826 uint8_t *pDataOutMCU, uint32_t OutDataLength)
1827 {
1828 /* Check the parameters */
1829 assert_param((InDataLength >= 4UL));
1830 assert_param((OutDataLength >= 4UL));
1831
1832 /* Check In/out buffer allocation and size */
1833 if ((hjpeg == NULL) || (pDataIn == NULL) || (pDataOutMCU == NULL))
1834 {
1835 return HAL_ERROR;
1836 }
1837
1838 /* Process Locked */
1839 __HAL_LOCK(hjpeg);
1840
1841 if (hjpeg->State == HAL_JPEG_STATE_READY)
1842 {
1843 /*Change JPEG state*/
1844 hjpeg->State = HAL_JPEG_STATE_BUSY_DECODING;
1845
1846 /*Set the Context to Decode with IT*/
1847 hjpeg->Context &= ~(JPEG_CONTEXT_OPERATION_MASK | JPEG_CONTEXT_METHOD_MASK);
1848 hjpeg->Context |= (JPEG_CONTEXT_DECODE | JPEG_CONTEXT_IT);
1849
1850 /*Store In/out buffers pointers and size*/
1851 hjpeg->pJpegInBuffPtr = pDataIn;
1852 hjpeg->pJpegOutBuffPtr = pDataOutMCU;
1853 hjpeg->InDataLength = InDataLength - (InDataLength % 4UL); /*In Data length must be multiple
1854 of 4 Bytes (1 word)*/
1855 hjpeg->OutDataLength = OutDataLength - (OutDataLength % 4UL); /*Out Data length must be multiple
1856 of 4 Bytes (1 word)*/
1857
1858 /*Reset In/out data counter */
1859 hjpeg->JpegInCount = 0;
1860 hjpeg->JpegOutCount = 0;
1861
1862 /*Init decoding process*/
1863 JPEG_Init_Process(hjpeg);
1864
1865 }
1866 else
1867 {
1868 /* Process Unlocked */
1869 __HAL_UNLOCK(hjpeg);
1870
1871 return HAL_BUSY;
1872 }
1873 /* Return function status */
1874 return HAL_OK;
1875 }
1876
1877 /**
1878 * @brief Starts JPEG encoding with DMA processing
1879 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
1880 * the configuration information for JPEG module
1881 * @param pDataInMCU Pointer to the Input buffer
1882 * @param InDataLength size in bytes Input buffer
1883 * @param pDataOut Pointer to the jpeg output data buffer
1884 * @param OutDataLength size in bytes of the Output buffer
1885 * @retval HAL status
1886 */
HAL_JPEG_Encode_DMA(JPEG_HandleTypeDef * hjpeg,uint8_t * pDataInMCU,uint32_t InDataLength,uint8_t * pDataOut,uint32_t OutDataLength)1887 HAL_StatusTypeDef HAL_JPEG_Encode_DMA(JPEG_HandleTypeDef *hjpeg, uint8_t *pDataInMCU, uint32_t InDataLength,
1888 uint8_t *pDataOut, uint32_t OutDataLength)
1889 {
1890 /* Check the parameters */
1891 assert_param((InDataLength >= 4UL));
1892 assert_param((OutDataLength >= 4UL));
1893
1894 /* Check In/out buffer allocation and size */
1895 if ((hjpeg == NULL) || (pDataInMCU == NULL) || (pDataOut == NULL))
1896 {
1897 return HAL_ERROR;
1898 }
1899
1900 /* Process Locked */
1901 __HAL_LOCK(hjpeg);
1902
1903 if (hjpeg->State != HAL_JPEG_STATE_READY)
1904 {
1905 /* Process Unlocked */
1906 __HAL_UNLOCK(hjpeg);
1907
1908 return HAL_BUSY;
1909 }
1910 else
1911 {
1912 if ((hjpeg->Context & JPEG_CONTEXT_CONF_ENCODING) == JPEG_CONTEXT_CONF_ENCODING)
1913 {
1914 /*Change JPEG state*/
1915 hjpeg->State = HAL_JPEG_STATE_BUSY_ENCODING;
1916
1917 /*Set the Context to Encode with DMA*/
1918 hjpeg->Context &= ~(JPEG_CONTEXT_OPERATION_MASK | JPEG_CONTEXT_METHOD_MASK);
1919 hjpeg->Context |= (JPEG_CONTEXT_ENCODE | JPEG_CONTEXT_DMA);
1920
1921 /*Store In/out buffers pointers and size*/
1922 hjpeg->pJpegInBuffPtr = pDataInMCU;
1923 hjpeg->pJpegOutBuffPtr = pDataOut;
1924 hjpeg->InDataLength = InDataLength;
1925 hjpeg->OutDataLength = OutDataLength;
1926
1927 /*Reset In/out data counter */
1928 hjpeg->JpegInCount = 0;
1929 hjpeg->JpegOutCount = 0;
1930
1931 /*Init decoding process*/
1932 JPEG_Init_Process(hjpeg);
1933
1934 /* JPEG encoding process using DMA */
1935 if (JPEG_DMA_StartProcess(hjpeg) != HAL_OK)
1936 {
1937 /* Update State */
1938 hjpeg->State = HAL_JPEG_STATE_ERROR;
1939 /* Process Unlocked */
1940 __HAL_UNLOCK(hjpeg);
1941
1942 return HAL_ERROR;
1943 }
1944
1945 }
1946 else
1947 {
1948 /* Process Unlocked */
1949 __HAL_UNLOCK(hjpeg);
1950
1951 return HAL_ERROR;
1952 }
1953 }
1954 /* Return function status */
1955 return HAL_OK;
1956 }
1957
1958 /**
1959 * @brief Starts JPEG decoding with DMA processing
1960 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
1961 * the configuration information for JPEG module
1962 * @param pDataIn Pointer to the input data buffer
1963 * @param InDataLength size in bytes Input buffer
1964 * @param pDataOutMCU Pointer to the Output data buffer
1965 * @param OutDataLength size in bytes of the Output buffer
1966 * @retval HAL status
1967 */
HAL_JPEG_Decode_DMA(JPEG_HandleTypeDef * hjpeg,uint8_t * pDataIn,uint32_t InDataLength,uint8_t * pDataOutMCU,uint32_t OutDataLength)1968 HAL_StatusTypeDef HAL_JPEG_Decode_DMA(JPEG_HandleTypeDef *hjpeg, uint8_t *pDataIn, uint32_t InDataLength,
1969 uint8_t *pDataOutMCU, uint32_t OutDataLength)
1970 {
1971 /* Check the parameters */
1972 assert_param((InDataLength >= 4UL));
1973 assert_param((OutDataLength >= 4UL));
1974
1975 /* Check In/out buffer allocation and size */
1976 if ((hjpeg == NULL) || (pDataIn == NULL) || (pDataOutMCU == NULL))
1977 {
1978 return HAL_ERROR;
1979 }
1980
1981 /* Process Locked */
1982 __HAL_LOCK(hjpeg);
1983
1984 if (hjpeg->State == HAL_JPEG_STATE_READY)
1985 {
1986 /*Change JPEG state*/
1987 hjpeg->State = HAL_JPEG_STATE_BUSY_DECODING;
1988
1989 /*Set the Context to Decode with DMA*/
1990 hjpeg->Context &= ~(JPEG_CONTEXT_OPERATION_MASK | JPEG_CONTEXT_METHOD_MASK);
1991 hjpeg->Context |= (JPEG_CONTEXT_DECODE | JPEG_CONTEXT_DMA);
1992
1993 /*Store In/out buffers pointers and size*/
1994 hjpeg->pJpegInBuffPtr = pDataIn;
1995 hjpeg->pJpegOutBuffPtr = pDataOutMCU;
1996 hjpeg->InDataLength = InDataLength;
1997 hjpeg->OutDataLength = OutDataLength;
1998
1999 /*Reset In/out data counter */
2000 hjpeg->JpegInCount = 0;
2001 hjpeg->JpegOutCount = 0;
2002
2003 /*Init decoding process*/
2004 JPEG_Init_Process(hjpeg);
2005
2006 /* JPEG decoding process using DMA */
2007 if (JPEG_DMA_StartProcess(hjpeg) != HAL_OK)
2008 {
2009 /* Update State */
2010 hjpeg->State = HAL_JPEG_STATE_ERROR;
2011 /* Process Unlocked */
2012 __HAL_UNLOCK(hjpeg);
2013
2014 return HAL_ERROR;
2015 }
2016 }
2017 else
2018 {
2019 /* Process Unlocked */
2020 __HAL_UNLOCK(hjpeg);
2021
2022 return HAL_BUSY;
2023 }
2024 /* Return function status */
2025 return HAL_OK;
2026 }
2027
2028 /**
2029 * @brief Pause the JPEG Input/Output processing
2030 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
2031 * the configuration information for JPEG module
2032 * @param XferSelection This parameter can be one of the following values :
2033 * JPEG_PAUSE_RESUME_INPUT : Pause Input processing
2034 * JPEG_PAUSE_RESUME_OUTPUT: Pause Output processing
2035 * JPEG_PAUSE_RESUME_INPUT_OUTPUT: Pause Input and Output processing
2036 * @retval HAL status
2037 */
HAL_JPEG_Pause(JPEG_HandleTypeDef * hjpeg,uint32_t XferSelection)2038 HAL_StatusTypeDef HAL_JPEG_Pause(JPEG_HandleTypeDef *hjpeg, uint32_t XferSelection)
2039 {
2040 uint32_t mask = 0;
2041
2042 assert_param(IS_JPEG_PAUSE_RESUME_STATE(XferSelection));
2043
2044 if ((hjpeg->Context & JPEG_CONTEXT_METHOD_MASK) == JPEG_CONTEXT_DMA)
2045 {
2046 if ((XferSelection & JPEG_PAUSE_RESUME_INPUT) == JPEG_PAUSE_RESUME_INPUT)
2047 {
2048 hjpeg->Context |= JPEG_CONTEXT_PAUSE_INPUT;
2049 mask |= JPEG_DMA_IDMA;
2050 }
2051 if ((XferSelection & JPEG_PAUSE_RESUME_OUTPUT) == JPEG_PAUSE_RESUME_OUTPUT)
2052 {
2053 hjpeg->Context |= JPEG_CONTEXT_PAUSE_OUTPUT;
2054 mask |= JPEG_DMA_ODMA;
2055 }
2056 JPEG_DISABLE_DMA(hjpeg, mask);
2057
2058 }
2059 else if ((hjpeg->Context & JPEG_CONTEXT_METHOD_MASK) == JPEG_CONTEXT_IT)
2060 {
2061
2062 if ((XferSelection & JPEG_PAUSE_RESUME_INPUT) == JPEG_PAUSE_RESUME_INPUT)
2063 {
2064 hjpeg->Context |= JPEG_CONTEXT_PAUSE_INPUT;
2065 mask |= (JPEG_IT_IFT | JPEG_IT_IFNF);
2066 }
2067 if ((XferSelection & JPEG_PAUSE_RESUME_OUTPUT) == JPEG_PAUSE_RESUME_OUTPUT)
2068 {
2069 hjpeg->Context |= JPEG_CONTEXT_PAUSE_OUTPUT;
2070 mask |= (JPEG_IT_OFT | JPEG_IT_OFNE | JPEG_IT_EOC);
2071 }
2072 __HAL_JPEG_DISABLE_IT(hjpeg, mask);
2073
2074 }
2075 else
2076 {
2077 /* Nothing to do */
2078 }
2079
2080 /* Return function status */
2081 return HAL_OK;
2082 }
2083
2084 /**
2085 * @brief Resume the JPEG Input/Output processing
2086 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
2087 * the configuration information for JPEG module
2088 * @param XferSelection This parameter can be one of the following values :
2089 * JPEG_PAUSE_RESUME_INPUT : Resume Input processing
2090 * JPEG_PAUSE_RESUME_OUTPUT: Resume Output processing
2091 * JPEG_PAUSE_RESUME_INPUT_OUTPUT: Resume Input and Output processing
2092 * @retval HAL status
2093 */
HAL_JPEG_Resume(JPEG_HandleTypeDef * hjpeg,uint32_t XferSelection)2094 HAL_StatusTypeDef HAL_JPEG_Resume(JPEG_HandleTypeDef *hjpeg, uint32_t XferSelection)
2095 {
2096 uint32_t mask = 0;
2097
2098 assert_param(IS_JPEG_PAUSE_RESUME_STATE(XferSelection));
2099
2100 if ((hjpeg->Context & (JPEG_CONTEXT_PAUSE_INPUT | JPEG_CONTEXT_PAUSE_OUTPUT)) == 0UL)
2101 {
2102 /* if nothing paused to resume return error*/
2103 return HAL_ERROR;
2104 }
2105
2106 if ((hjpeg->Context & JPEG_CONTEXT_METHOD_MASK) == JPEG_CONTEXT_DMA)
2107 {
2108
2109 if ((XferSelection & JPEG_PAUSE_RESUME_INPUT) == JPEG_PAUSE_RESUME_INPUT)
2110 {
2111 hjpeg->Context &= (~JPEG_CONTEXT_PAUSE_INPUT);
2112 mask |= JPEG_DMA_IDMA;
2113
2114 /*JPEG Input DMA transfer data number must be multiple of DMA buffer size
2115 as the destination is a 32 bits register */
2116 hjpeg->InDataLength = hjpeg->InDataLength - (hjpeg->InDataLength % 4UL);
2117
2118 if (hjpeg->InDataLength > 0UL)
2119 {
2120 /* Start DMA FIFO In transfer */
2121 if (HAL_DMA_Start_IT(hjpeg->hdmain, (uint32_t)hjpeg->pJpegInBuffPtr, (uint32_t)&hjpeg->Instance->DIR,
2122 hjpeg->InDataLength) != HAL_OK)
2123 {
2124 hjpeg->ErrorCode |= HAL_JPEG_ERROR_DMA;
2125 hjpeg->State = HAL_JPEG_STATE_ERROR;
2126 return HAL_ERROR;
2127 }
2128 }
2129 }
2130 if ((XferSelection & JPEG_PAUSE_RESUME_OUTPUT) == JPEG_PAUSE_RESUME_OUTPUT)
2131 {
2132 hjpeg->Context &= (~JPEG_CONTEXT_PAUSE_OUTPUT);
2133
2134 if ((hjpeg->Context & JPEG_CONTEXT_ENDING_DMA) != 0UL)
2135 {
2136 JPEG_DMA_PollResidualData(hjpeg);
2137 }
2138 else
2139 {
2140 mask |= JPEG_DMA_ODMA;
2141
2142 /* Start DMA FIFO Out transfer */
2143 if (HAL_DMA_Start_IT(hjpeg->hdmaout, (uint32_t)&hjpeg->Instance->DOR, (uint32_t)hjpeg->pJpegOutBuffPtr,
2144 hjpeg->OutDataLength) != HAL_OK)
2145 {
2146 hjpeg->ErrorCode |= HAL_JPEG_ERROR_DMA;
2147 hjpeg->State = HAL_JPEG_STATE_ERROR;
2148 return HAL_ERROR;
2149 }
2150 }
2151
2152 }
2153 JPEG_ENABLE_DMA(hjpeg, mask);
2154
2155 }
2156 else if ((hjpeg->Context & JPEG_CONTEXT_METHOD_MASK) == JPEG_CONTEXT_IT)
2157 {
2158 if ((XferSelection & JPEG_PAUSE_RESUME_INPUT) == JPEG_PAUSE_RESUME_INPUT)
2159 {
2160 hjpeg->Context &= (~JPEG_CONTEXT_PAUSE_INPUT);
2161 mask |= (JPEG_IT_IFT | JPEG_IT_IFNF);
2162 }
2163 if ((XferSelection & JPEG_PAUSE_RESUME_OUTPUT) == JPEG_PAUSE_RESUME_OUTPUT)
2164 {
2165 hjpeg->Context &= (~JPEG_CONTEXT_PAUSE_OUTPUT);
2166 mask |= (JPEG_IT_OFT | JPEG_IT_OFNE | JPEG_IT_EOC);
2167 }
2168 __HAL_JPEG_ENABLE_IT(hjpeg, mask);
2169
2170 }
2171 else
2172 {
2173 /* Nothing to do */
2174 }
2175
2176 /* Return function status */
2177 return HAL_OK;
2178 }
2179
2180 /**
2181 * @brief Config Encoding/Decoding Input Buffer.
2182 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
2183 * the configuration information for JPEG module.
2184 * @param pNewInputBuffer Pointer to the new input data buffer
2185 * @param InDataLength Size in bytes of the new Input data buffer
2186 * @retval HAL status
2187 */
HAL_JPEG_ConfigInputBuffer(JPEG_HandleTypeDef * hjpeg,uint8_t * pNewInputBuffer,uint32_t InDataLength)2188 void HAL_JPEG_ConfigInputBuffer(JPEG_HandleTypeDef *hjpeg, uint8_t *pNewInputBuffer, uint32_t InDataLength)
2189 {
2190 hjpeg->pJpegInBuffPtr = pNewInputBuffer;
2191 hjpeg->InDataLength = InDataLength;
2192 }
2193
2194 /**
2195 * @brief Config Encoding/Decoding Output Buffer.
2196 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
2197 * the configuration information for JPEG module.
2198 * @param pNewOutputBuffer Pointer to the new output data buffer
2199 * @param OutDataLength Size in bytes of the new Output data buffer
2200 * @retval HAL status
2201 */
HAL_JPEG_ConfigOutputBuffer(JPEG_HandleTypeDef * hjpeg,uint8_t * pNewOutputBuffer,uint32_t OutDataLength)2202 void HAL_JPEG_ConfigOutputBuffer(JPEG_HandleTypeDef *hjpeg, uint8_t *pNewOutputBuffer, uint32_t OutDataLength)
2203 {
2204 hjpeg->pJpegOutBuffPtr = pNewOutputBuffer;
2205 hjpeg->OutDataLength = OutDataLength;
2206 }
2207
2208 /**
2209 * @brief Aborts the JPEG Encoding/Decoding.
2210 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
2211 * the configuration information for JPEG module
2212 * @retval HAL status
2213 */
HAL_JPEG_Abort(JPEG_HandleTypeDef * hjpeg)2214 HAL_StatusTypeDef HAL_JPEG_Abort(JPEG_HandleTypeDef *hjpeg)
2215 {
2216 uint32_t tickstart;
2217 uint32_t tmpContext;
2218 tmpContext = hjpeg->Context;
2219
2220 /*Reset the Context operation and method*/
2221 hjpeg->Context &= ~(JPEG_CONTEXT_OPERATION_MASK | JPEG_CONTEXT_METHOD_MASK | JPEG_CONTEXT_ENDING_DMA);
2222
2223 if ((tmpContext & JPEG_CONTEXT_METHOD_MASK) == JPEG_CONTEXT_DMA)
2224 {
2225 /* Stop the DMA In/out Xfer*/
2226 if (HAL_DMA_Abort(hjpeg->hdmaout) != HAL_OK)
2227 {
2228 if (hjpeg->hdmaout->ErrorCode == HAL_DMA_ERROR_TIMEOUT)
2229 {
2230 hjpeg->ErrorCode |= HAL_JPEG_ERROR_DMA;
2231 }
2232 }
2233 if (HAL_DMA_Abort(hjpeg->hdmain) != HAL_OK)
2234 {
2235 if (hjpeg->hdmain->ErrorCode == HAL_DMA_ERROR_TIMEOUT)
2236 {
2237 hjpeg->ErrorCode |= HAL_JPEG_ERROR_DMA;
2238 }
2239 }
2240
2241 }
2242
2243 /* Stop the JPEG encoding/decoding process*/
2244 hjpeg->Instance->CONFR0 &= ~JPEG_CONFR0_START;
2245
2246 /* Get tick */
2247 tickstart = HAL_GetTick();
2248
2249 /* Check if the JPEG Codec is effectively disabled */
2250 while (__HAL_JPEG_GET_FLAG(hjpeg, JPEG_FLAG_COF) != 0UL)
2251 {
2252 /* Check for the Timeout */
2253 if ((HAL_GetTick() - tickstart) > JPEG_TIMEOUT_VALUE)
2254 {
2255 /* Update error code */
2256 hjpeg->ErrorCode |= HAL_JPEG_ERROR_TIMEOUT;
2257
2258 /* Change the DMA state */
2259 hjpeg->State = HAL_JPEG_STATE_ERROR;
2260 break;
2261 }
2262 }
2263
2264 /* Disable All Interrupts */
2265 __HAL_JPEG_DISABLE_IT(hjpeg, JPEG_INTERRUPT_MASK);
2266
2267 /* Disable All DMA requests */
2268 JPEG_DISABLE_DMA(hjpeg, JPEG_DMA_MASK);
2269
2270 /* Flush input and output FIFOs*/
2271 hjpeg->Instance->CR |= JPEG_CR_IFF;
2272 hjpeg->Instance->CR |= JPEG_CR_OFF;
2273
2274 /* Clear all flags */
2275 __HAL_JPEG_CLEAR_FLAG(hjpeg, JPEG_FLAG_ALL);
2276
2277 /* Reset JpegInCount and JpegOutCount */
2278 hjpeg->JpegInCount = 0;
2279 hjpeg->JpegOutCount = 0;
2280
2281 /*Reset the Context Pause*/
2282 hjpeg->Context &= ~(JPEG_CONTEXT_PAUSE_INPUT | JPEG_CONTEXT_PAUSE_OUTPUT);
2283
2284 /* Change the DMA state*/
2285 if (hjpeg->ErrorCode != HAL_JPEG_ERROR_NONE)
2286 {
2287 hjpeg->State = HAL_JPEG_STATE_ERROR;
2288 /* Process Unlocked */
2289 __HAL_UNLOCK(hjpeg);
2290 /* Return function status */
2291 return HAL_ERROR;
2292 }
2293 else
2294 {
2295 hjpeg->State = HAL_JPEG_STATE_READY;
2296 /* Process Unlocked */
2297 __HAL_UNLOCK(hjpeg);
2298 /* Return function status */
2299 return HAL_OK;
2300 }
2301
2302 }
2303
2304
2305 /**
2306 * @}
2307 */
2308
2309 /** @defgroup JPEG_Exported_Functions_Group4 JPEG Decode/Encode callback functions
2310 * @brief JPEG process callback functions.
2311 *
2312 @verbatim
2313 ==============================================================================
2314 ##### JPEG Decode and Encode callback functions #####
2315 ==============================================================================
2316 [..] This section provides callback functions:
2317 (+) HAL_JPEG_InfoReadyCallback() : Decoding JPEG Info ready callback
2318 (+) HAL_JPEG_EncodeCpltCallback() : Encoding complete callback.
2319 (+) HAL_JPEG_DecodeCpltCallback() : Decoding complete callback.
2320 (+) HAL_JPEG_ErrorCallback() : JPEG error callback.
2321 (+) HAL_JPEG_GetDataCallback() : Get New Data chunk callback.
2322 (+) HAL_JPEG_DataReadyCallback() : Decoded/Encoded Data ready callback.
2323
2324 @endverbatim
2325 * @{
2326 */
2327
2328 /**
2329 * @brief Decoding JPEG Info ready callback.
2330 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
2331 * the configuration information for JPEG module
2332 * @param pInfo pointer to a JPEG_ConfTypeDef structure that contains
2333 * The JPEG decoded header information
2334 * @retval None
2335 */
HAL_JPEG_InfoReadyCallback(JPEG_HandleTypeDef * hjpeg,JPEG_ConfTypeDef * pInfo)2336 __weak void HAL_JPEG_InfoReadyCallback(JPEG_HandleTypeDef *hjpeg, JPEG_ConfTypeDef *pInfo)
2337 {
2338 /* Prevent unused argument(s) compilation warning */
2339 UNUSED(hjpeg);
2340 UNUSED(pInfo);
2341
2342 /* NOTE : This function Should not be modified, when the callback is needed,
2343 the HAL_JPEG_HeaderParsingCpltCallback could be implemented in the user file
2344 */
2345 }
2346
2347 /**
2348 * @brief Encoding complete callback.
2349 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
2350 * the configuration information for JPEG module
2351 * @retval None
2352 */
HAL_JPEG_EncodeCpltCallback(JPEG_HandleTypeDef * hjpeg)2353 __weak void HAL_JPEG_EncodeCpltCallback(JPEG_HandleTypeDef *hjpeg)
2354 {
2355 /* Prevent unused argument(s) compilation warning */
2356 UNUSED(hjpeg);
2357
2358 /* NOTE : This function Should not be modified, when the callback is needed,
2359 the HAL_JPEG_EncodeCpltCallback could be implemented in the user file
2360 */
2361 }
2362
2363 /**
2364 * @brief Decoding complete callback.
2365 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
2366 * the configuration information for JPEG module
2367 * @retval None
2368 */
HAL_JPEG_DecodeCpltCallback(JPEG_HandleTypeDef * hjpeg)2369 __weak void HAL_JPEG_DecodeCpltCallback(JPEG_HandleTypeDef *hjpeg)
2370 {
2371 /* Prevent unused argument(s) compilation warning */
2372 UNUSED(hjpeg);
2373
2374 /* NOTE : This function Should not be modified, when the callback is needed,
2375 the HAL_JPEG_EncodeCpltCallback could be implemented in the user file
2376 */
2377 }
2378
2379 /**
2380 * @brief JPEG error callback.
2381 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
2382 * the configuration information for JPEG module
2383 * @retval None
2384 */
HAL_JPEG_ErrorCallback(JPEG_HandleTypeDef * hjpeg)2385 __weak void HAL_JPEG_ErrorCallback(JPEG_HandleTypeDef *hjpeg)
2386 {
2387 /* Prevent unused argument(s) compilation warning */
2388 UNUSED(hjpeg);
2389
2390 /* NOTE : This function Should not be modified, when the callback is needed,
2391 the HAL_JPEG_ErrorCallback could be implemented in the user file
2392 */
2393 }
2394
2395 /**
2396 * @brief Get New Data chunk callback.
2397 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
2398 * the configuration information for JPEG module
2399 * @param NbDecodedData Number of consumed data in the previous chunk in bytes
2400 * @retval None
2401 */
HAL_JPEG_GetDataCallback(JPEG_HandleTypeDef * hjpeg,uint32_t NbDecodedData)2402 __weak void HAL_JPEG_GetDataCallback(JPEG_HandleTypeDef *hjpeg, uint32_t NbDecodedData)
2403 {
2404 /* Prevent unused argument(s) compilation warning */
2405 UNUSED(hjpeg);
2406 UNUSED(NbDecodedData);
2407
2408 /* NOTE : This function Should not be modified, when the callback is needed,
2409 the HAL_JPEG_GetDataCallback could be implemented in the user file
2410 */
2411 }
2412
2413 /**
2414 * @brief Decoded/Encoded Data ready callback.
2415 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
2416 * the configuration information for JPEG module
2417 * @param pDataOut pointer to the output data buffer
2418 * @param OutDataLength number in bytes of data available in the specified output buffer
2419 * @retval None
2420 */
HAL_JPEG_DataReadyCallback(JPEG_HandleTypeDef * hjpeg,uint8_t * pDataOut,uint32_t OutDataLength)2421 __weak void HAL_JPEG_DataReadyCallback(JPEG_HandleTypeDef *hjpeg, uint8_t *pDataOut, uint32_t OutDataLength)
2422 {
2423 /* Prevent unused argument(s) compilation warning */
2424 UNUSED(hjpeg);
2425 UNUSED(pDataOut);
2426 UNUSED(OutDataLength);
2427
2428 /* NOTE : This function Should not be modified, when the callback is needed,
2429 the HAL_JPEG_DataReadyCallback could be implemented in the user file
2430 */
2431 }
2432
2433 /**
2434 * @}
2435 */
2436
2437
2438 /** @defgroup JPEG_Exported_Functions_Group5 JPEG IRQ handler management
2439 * @brief JPEG IRQ handler.
2440 *
2441 @verbatim
2442 ==============================================================================
2443 ##### JPEG IRQ handler management #####
2444 ==============================================================================
2445 [..] This section provides JPEG IRQ handler function.
2446 (+) HAL_JPEG_IRQHandler() : handles JPEG interrupt request
2447
2448 @endverbatim
2449 * @{
2450 */
2451
2452 /**
2453 * @brief This function handles JPEG interrupt request.
2454 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
2455 * the configuration information for JPEG module
2456 * @retval None
2457 */
HAL_JPEG_IRQHandler(JPEG_HandleTypeDef * hjpeg)2458 void HAL_JPEG_IRQHandler(JPEG_HandleTypeDef *hjpeg)
2459 {
2460 switch (hjpeg->State)
2461 {
2462 case HAL_JPEG_STATE_BUSY_ENCODING:
2463 case HAL_JPEG_STATE_BUSY_DECODING:
2464 /* continue JPEG data encoding/Decoding*/
2465 /* JPEG data processing : In/Out FIFO transfer*/
2466 if ((hjpeg->Context & JPEG_CONTEXT_METHOD_MASK) == JPEG_CONTEXT_IT)
2467 {
2468 (void) JPEG_Process(hjpeg);
2469 }
2470 else if ((hjpeg->Context & JPEG_CONTEXT_METHOD_MASK) == JPEG_CONTEXT_DMA)
2471 {
2472 JPEG_DMA_ContinueProcess(hjpeg);
2473 }
2474 else
2475 {
2476 /* Nothing to do */
2477 }
2478 break;
2479
2480 default:
2481 break;
2482 }
2483 }
2484
2485 /**
2486 * @}
2487 */
2488
2489 /** @defgroup JPEG_Exported_Functions_Group6 Peripheral State functions
2490 * @brief Peripheral State functions.
2491 *
2492 @verbatim
2493 ==============================================================================
2494 ##### Peripheral State and Error functions #####
2495 ==============================================================================
2496 [..] This section provides JPEG State and Errors function.
2497 (+) HAL_JPEG_GetState() : permits to get in run-time the JPEG state.
2498 (+) HAL_JPEG_GetError() : Returns the JPEG error code if any.
2499
2500 @endverbatim
2501 * @{
2502 */
2503
2504 /**
2505 * @brief Returns the JPEG state.
2506 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
2507 * the configuration information for JPEG module
2508 * @retval JPEG state
2509 */
HAL_JPEG_GetState(const JPEG_HandleTypeDef * hjpeg)2510 HAL_JPEG_STATETypeDef HAL_JPEG_GetState(const JPEG_HandleTypeDef *hjpeg)
2511 {
2512 return hjpeg->State;
2513 }
2514
2515 /**
2516 * @brief Return the JPEG error code
2517 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
2518 * the configuration information for the specified JPEG.
2519 * @retval JPEG Error Code
2520 */
HAL_JPEG_GetError(const JPEG_HandleTypeDef * hjpeg)2521 uint32_t HAL_JPEG_GetError(const JPEG_HandleTypeDef *hjpeg)
2522 {
2523 return hjpeg->ErrorCode;
2524 }
2525
2526 /**
2527 * @}
2528 */
2529
2530 /**
2531 * @}
2532 */
2533
2534
2535 /** @addtogroup JPEG_Private_Functions
2536 * @{
2537 */
2538
2539 /**
2540 * @brief Generates Huffman sizes/Codes Table from Bits/vals Table
2541 * @param Bits pointer to bits table
2542 * @param Huffsize pointer to sizes table
2543 * @param Huffcode pointer to codes table
2544 * @param LastK pointer to last Coeff (table dimension)
2545 * @retval HAL status
2546 */
JPEG_Bits_To_SizeCodes(const uint8_t * Bits,uint8_t * Huffsize,uint32_t * Huffcode,uint32_t * LastK)2547 static HAL_StatusTypeDef JPEG_Bits_To_SizeCodes(const uint8_t *Bits, uint8_t *Huffsize, uint32_t *Huffcode,
2548 uint32_t *LastK)
2549 {
2550 uint32_t i;
2551 uint32_t j;
2552 uint32_t k;
2553 uint32_t code;
2554 uint32_t si;
2555
2556 /* Figure C.1: Generation of table of Huffman code sizes */
2557 j = 0;
2558 for (k = 0; k < 16UL; k++)
2559 {
2560 i = (uint32_t)Bits[k];
2561 if ((j + i) > 256UL)
2562 {
2563 /* check for table overflow */
2564 return HAL_ERROR;
2565 }
2566 while (i != 0UL)
2567 {
2568 Huffsize[j] = (uint8_t) k + 1U;
2569 j++;
2570 i--;
2571 }
2572 }
2573 Huffsize[j] = 0;
2574 *LastK = j;
2575
2576 /* Figure C.2: Generation of table of Huffman codes */
2577 code = 0;
2578 si = Huffsize[0];
2579 j = 0;
2580 while (Huffsize[j] != 0U)
2581 {
2582 while (((uint32_t) Huffsize[j]) == si)
2583 {
2584 Huffcode[j] = code;
2585 j++;
2586 code++;
2587 }
2588 /* code must fit in "size" bits (si), no code is allowed to be all ones*/
2589 if (si > 31UL)
2590 {
2591 return HAL_ERROR;
2592 }
2593 if (((uint32_t) code) >= (((uint32_t) 1) << si))
2594 {
2595 return HAL_ERROR;
2596 }
2597 code <<= 1;
2598 si++;
2599 }
2600 /* Return function status */
2601 return HAL_OK;
2602 }
2603
2604 /**
2605 * @brief Transform a Bits/Vals AC Huffman table to sizes/Codes huffman Table
2606 * that can programmed to the JPEG encoder registers
2607 * @param AC_BitsValsTable pointer to AC huffman bits/vals table
2608 * @param AC_SizeCodesTable pointer to AC huffman Sizes/Codes table
2609 * @retval HAL status
2610 */
JPEG_ACHuff_BitsVals_To_SizeCodes(JPEG_ACHuffTableTypeDef * AC_BitsValsTable,JPEG_AC_HuffCodeTableTypeDef * AC_SizeCodesTable)2611 static HAL_StatusTypeDef JPEG_ACHuff_BitsVals_To_SizeCodes(JPEG_ACHuffTableTypeDef *AC_BitsValsTable,
2612 JPEG_AC_HuffCodeTableTypeDef *AC_SizeCodesTable)
2613 {
2614 HAL_StatusTypeDef error;
2615 uint8_t huffsize[257];
2616 uint32_t huffcode[257];
2617 uint32_t k;
2618 uint32_t i;
2619 uint32_t lsb;
2620 uint32_t msb;
2621 uint32_t lastK;
2622
2623 error = JPEG_Bits_To_SizeCodes(AC_BitsValsTable->Bits, huffsize, huffcode, &lastK);
2624 if (error != HAL_OK)
2625 {
2626 return error;
2627 }
2628
2629 /* Figure C.3: Ordering procedure for encoding procedure code tables */
2630 k = 0;
2631
2632 while (k < lastK)
2633 {
2634 i = AC_BitsValsTable->HuffVal[k];
2635 if (i == 0UL)
2636 {
2637 i = JPEG_AC_HUFF_TABLE_SIZE - 2UL; /*i = 0x00 EOB code*/
2638 }
2639 else if (i == 0xF0UL) /* i = 0xF0 ZRL code*/
2640 {
2641 i = JPEG_AC_HUFF_TABLE_SIZE - 1UL;
2642 }
2643 else
2644 {
2645 msb = (i & 0xF0UL) >> 4;
2646 lsb = (i & 0x0FUL);
2647 i = (msb * 10UL) + lsb - 1UL;
2648 }
2649 if (i >= JPEG_AC_HUFF_TABLE_SIZE)
2650 {
2651 return HAL_ERROR; /* Huffman Table overflow error*/
2652 }
2653 else
2654 {
2655 AC_SizeCodesTable->HuffmanCode[i] = huffcode[k];
2656 AC_SizeCodesTable->CodeLength[i] = huffsize[k] - 1U;
2657 k++;
2658 }
2659 }
2660
2661 /* Return function status */
2662 return HAL_OK;
2663 }
2664
2665 /**
2666 * @brief Transform a Bits/Vals DC Huffman table to sizes/Codes huffman Table
2667 * that can programmed to the JPEG encoder registers
2668 * @param DC_BitsValsTable pointer to DC huffman bits/vals table
2669 * @param DC_SizeCodesTable pointer to DC huffman Sizes/Codes table
2670 * @retval HAL status
2671 */
JPEG_DCHuff_BitsVals_To_SizeCodes(JPEG_DCHuffTableTypeDef * DC_BitsValsTable,JPEG_DC_HuffCodeTableTypeDef * DC_SizeCodesTable)2672 static HAL_StatusTypeDef JPEG_DCHuff_BitsVals_To_SizeCodes(JPEG_DCHuffTableTypeDef *DC_BitsValsTable,
2673 JPEG_DC_HuffCodeTableTypeDef *DC_SizeCodesTable)
2674 {
2675 HAL_StatusTypeDef error;
2676
2677 uint32_t k;
2678 uint32_t i;
2679 uint32_t lastK;
2680 uint8_t huffsize[257];
2681 uint32_t huffcode[257];
2682 error = JPEG_Bits_To_SizeCodes(DC_BitsValsTable->Bits, huffsize, huffcode, &lastK);
2683 if (error != HAL_OK)
2684 {
2685 return error;
2686 }
2687 /* Figure C.3: ordering procedure for encoding procedure code tables */
2688 k = 0;
2689
2690 while (k < lastK)
2691 {
2692 i = DC_BitsValsTable->HuffVal[k];
2693 if (i >= JPEG_DC_HUFF_TABLE_SIZE)
2694 {
2695 return HAL_ERROR; /* Huffman Table overflow error*/
2696 }
2697 else
2698 {
2699 DC_SizeCodesTable->HuffmanCode[i] = huffcode[k];
2700 DC_SizeCodesTable->CodeLength[i] = huffsize[k] - 1U;
2701 k++;
2702 }
2703 }
2704
2705 /* Return function status */
2706 return HAL_OK;
2707 }
2708
2709 /**
2710 * @brief Set the JPEG register with an DC huffman table at the given DC table address
2711 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
2712 * the configuration information for JPEG module
2713 * @param HuffTableDC pointer to DC huffman table
2714 * @param DCTableAddress Encoder DC huffman table address it could be HUFFENC_DC0 or HUFFENC_DC1.
2715 * @retval HAL status
2716 */
JPEG_Set_HuffDC_Mem(const JPEG_HandleTypeDef * hjpeg,JPEG_DCHuffTableTypeDef * HuffTableDC,const __IO uint32_t * DCTableAddress)2717 static HAL_StatusTypeDef JPEG_Set_HuffDC_Mem(const JPEG_HandleTypeDef *hjpeg, JPEG_DCHuffTableTypeDef *HuffTableDC,
2718 const __IO uint32_t *DCTableAddress)
2719 {
2720 HAL_StatusTypeDef error;
2721 JPEG_DC_HuffCodeTableTypeDef dcSizeCodesTable;
2722 uint32_t i;
2723 uint32_t lsb;
2724 uint32_t msb;
2725 __IO uint32_t *address;
2726 __IO uint32_t *addressDef;
2727
2728 if (DCTableAddress == (hjpeg->Instance->HUFFENC_DC0))
2729 {
2730 address = (hjpeg->Instance->HUFFENC_DC0 + (JPEG_DC_HUFF_TABLE_SIZE / 2UL));
2731 }
2732 else if (DCTableAddress == (hjpeg->Instance->HUFFENC_DC1))
2733 {
2734 address = (hjpeg->Instance->HUFFENC_DC1 + (JPEG_DC_HUFF_TABLE_SIZE / 2UL));
2735 }
2736 else
2737 {
2738 return HAL_ERROR;
2739 }
2740
2741 if (HuffTableDC != NULL)
2742 {
2743 error = JPEG_DCHuff_BitsVals_To_SizeCodes(HuffTableDC, &dcSizeCodesTable);
2744 if (error != HAL_OK)
2745 {
2746 return error;
2747 }
2748 addressDef = address;
2749 *addressDef = 0x0FFF0FFF;
2750 addressDef++;
2751 *addressDef = 0x0FFF0FFF;
2752
2753 i = JPEG_DC_HUFF_TABLE_SIZE;
2754 while (i > 1UL)
2755 {
2756 i--;
2757 address --;
2758 msb = ((uint32_t)(((uint32_t)dcSizeCodesTable.CodeLength[i] & 0xFU) << 8)) |
2759 ((uint32_t)dcSizeCodesTable.HuffmanCode[i] & 0xFFUL);
2760 i--;
2761 lsb = ((uint32_t)(((uint32_t)dcSizeCodesTable.CodeLength[i] & 0xFU) << 8)) |
2762 ((uint32_t)dcSizeCodesTable.HuffmanCode[i] & 0xFFUL);
2763
2764 *address = lsb | (msb << 16);
2765 }
2766 }
2767
2768 /* Return function status */
2769 return HAL_OK;
2770 }
2771
2772 /**
2773 * @brief Set the JPEG register with an AC huffman table at the given AC table address
2774 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
2775 * the configuration information for JPEG module
2776 * @param HuffTableAC pointer to AC huffman table
2777 * @param ACTableAddress Encoder AC huffman table address it could be HUFFENC_AC0 or HUFFENC_AC1.
2778 * @retval HAL status
2779 */
JPEG_Set_HuffAC_Mem(const JPEG_HandleTypeDef * hjpeg,JPEG_ACHuffTableTypeDef * HuffTableAC,const __IO uint32_t * ACTableAddress)2780 static HAL_StatusTypeDef JPEG_Set_HuffAC_Mem(const JPEG_HandleTypeDef *hjpeg, JPEG_ACHuffTableTypeDef *HuffTableAC,
2781 const __IO uint32_t *ACTableAddress)
2782 {
2783 HAL_StatusTypeDef error;
2784 JPEG_AC_HuffCodeTableTypeDef acSizeCodesTable;
2785 uint32_t i;
2786 uint32_t lsb;
2787 uint32_t msb;
2788 __IO uint32_t *address;
2789 __IO uint32_t *addressDef;
2790
2791 if (ACTableAddress == (hjpeg->Instance->HUFFENC_AC0))
2792 {
2793 address = (hjpeg->Instance->HUFFENC_AC0 + (JPEG_AC_HUFF_TABLE_SIZE / 2UL));
2794 }
2795 else if (ACTableAddress == (hjpeg->Instance->HUFFENC_AC1))
2796 {
2797 address = (hjpeg->Instance->HUFFENC_AC1 + (JPEG_AC_HUFF_TABLE_SIZE / 2UL));
2798 }
2799 else
2800 {
2801 return HAL_ERROR;
2802 }
2803
2804 if (HuffTableAC != NULL)
2805 {
2806 error = JPEG_ACHuff_BitsVals_To_SizeCodes(HuffTableAC, &acSizeCodesTable);
2807 if (error != HAL_OK)
2808 {
2809 return error;
2810 }
2811 /* Default values settings: 162:167 FFFh , 168:175 FD0h_FD7h */
2812 /* Locations 162:175 of each AC table contain information used internally by the core */
2813
2814 addressDef = address;
2815 for (i = 0; i < 3UL; i++)
2816 {
2817 *addressDef = 0x0FFF0FFF;
2818 addressDef++;
2819 }
2820 *addressDef = 0x0FD10FD0;
2821 addressDef++;
2822 *addressDef = 0x0FD30FD2;
2823 addressDef++;
2824 *addressDef = 0x0FD50FD4;
2825 addressDef++;
2826 *addressDef = 0x0FD70FD6;
2827 /* end of Locations 162:175 */
2828
2829
2830 i = JPEG_AC_HUFF_TABLE_SIZE;
2831 while (i > 1UL)
2832 {
2833 i--;
2834 address--;
2835 msb = ((uint32_t)(((uint32_t)acSizeCodesTable.CodeLength[i] & 0xFU) << 8)) |
2836 ((uint32_t)acSizeCodesTable.HuffmanCode[i] & 0xFFUL);
2837 i--;
2838 lsb = ((uint32_t)(((uint32_t)acSizeCodesTable.CodeLength[i] & 0xFU) << 8)) |
2839 ((uint32_t)acSizeCodesTable.HuffmanCode[i] & 0xFFUL);
2840
2841 *address = lsb | (msb << 16);
2842 }
2843 }
2844
2845 /* Return function status */
2846 return HAL_OK;
2847 }
2848
2849 /**
2850 * @brief Configure the JPEG encoder register huffman tables to used during
2851 * the encdoing operation
2852 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
2853 * the configuration information for JPEG module
2854 * @retval None
2855 */
JPEG_Set_HuffEnc_Mem(JPEG_HandleTypeDef * hjpeg)2856 static HAL_StatusTypeDef JPEG_Set_HuffEnc_Mem(JPEG_HandleTypeDef *hjpeg)
2857 {
2858 HAL_StatusTypeDef error;
2859
2860 JPEG_Set_Huff_DHTMem(hjpeg);
2861 error = JPEG_Set_HuffAC_Mem(hjpeg, (JPEG_ACHuffTableTypeDef *)(uint32_t)&JPEG_ACLUM_HuffTable,
2862 (hjpeg->Instance->HUFFENC_AC0));
2863 if (error != HAL_OK)
2864 {
2865 return error;
2866 }
2867
2868 error = JPEG_Set_HuffAC_Mem(hjpeg, (JPEG_ACHuffTableTypeDef *)(uint32_t)&JPEG_ACCHROM_HuffTable,
2869 (hjpeg->Instance->HUFFENC_AC1));
2870 if (error != HAL_OK)
2871 {
2872 return error;
2873 }
2874
2875 error = JPEG_Set_HuffDC_Mem(hjpeg, (JPEG_DCHuffTableTypeDef *)(uint32_t)&JPEG_DCLUM_HuffTable,
2876 hjpeg->Instance->HUFFENC_DC0);
2877 if (error != HAL_OK)
2878 {
2879 return error;
2880 }
2881
2882 error = JPEG_Set_HuffDC_Mem(hjpeg, (JPEG_DCHuffTableTypeDef *)(uint32_t)&JPEG_DCCHROM_HuffTable,
2883 hjpeg->Instance->HUFFENC_DC1);
2884 if (error != HAL_OK)
2885 {
2886 return error;
2887 }
2888 /* Return function status */
2889 return HAL_OK;
2890 }
2891
2892 /**
2893 * @brief Configure the JPEG register huffman tables to be included in the JPEG
2894 * file header (used for encoding only)
2895 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
2896 * the configuration information for JPEG module
2897 * @retval None
2898 */
JPEG_Set_Huff_DHTMem(const JPEG_HandleTypeDef * hjpeg)2899 static void JPEG_Set_Huff_DHTMem(const JPEG_HandleTypeDef *hjpeg)
2900 {
2901 const JPEG_ACHuffTableTypeDef *HuffTableAC0 = (JPEG_ACHuffTableTypeDef *)(uint32_t)&JPEG_ACLUM_HuffTable;
2902 const JPEG_ACHuffTableTypeDef *HuffTableAC1 = (JPEG_ACHuffTableTypeDef *)(uint32_t)&JPEG_ACCHROM_HuffTable;
2903 const JPEG_DCHuffTableTypeDef *HuffTableDC0 = (JPEG_DCHuffTableTypeDef *)(uint32_t)&JPEG_DCLUM_HuffTable;
2904 const JPEG_DCHuffTableTypeDef *HuffTableDC1 = (JPEG_DCHuffTableTypeDef *)(uint32_t)&JPEG_DCCHROM_HuffTable;
2905 uint32_t value;
2906 uint32_t index;
2907 __IO uint32_t *address;
2908
2909 /* DC0 Huffman Table : BITS*/
2910 /* DC0 BITS is a 16 Bytes table i.e 4x32bits words from DHTMEM base address to DHTMEM + 3*/
2911 address = (hjpeg->Instance->DHTMEM + 3);
2912 index = 16;
2913 while (index > 3UL)
2914 {
2915
2916 *address = (((uint32_t)HuffTableDC0->Bits[index - 1UL] & 0xFFUL) << 24) |
2917 (((uint32_t)HuffTableDC0->Bits[index - 2UL] & 0xFFUL) << 16) |
2918 (((uint32_t)HuffTableDC0->Bits[index - 3UL] & 0xFFUL) << 8) |
2919 ((uint32_t)HuffTableDC0->Bits[index - 4UL] & 0xFFUL);
2920 address--;
2921 index -= 4UL;
2922
2923 }
2924 /* DC0 Huffman Table : Val*/
2925 /* DC0 VALS is a 12 Bytes table i.e 3x32bits words from DHTMEM base address +4 to DHTMEM + 6 */
2926 address = (hjpeg->Instance->DHTMEM + 6);
2927 index = 12;
2928 while (index > 3UL)
2929 {
2930 *address = (((uint32_t)HuffTableDC0->HuffVal[index - 1UL] & 0xFFUL) << 24) |
2931 (((uint32_t)HuffTableDC0->HuffVal[index - 2UL] & 0xFFUL) << 16) |
2932 (((uint32_t)HuffTableDC0->HuffVal[index - 3UL] & 0xFFUL) << 8) |
2933 ((uint32_t)HuffTableDC0->HuffVal[index - 4UL] & 0xFFUL);
2934 address--;
2935 index -= 4UL;
2936 }
2937
2938 /* AC0 Huffman Table : BITS*/
2939 /* AC0 BITS is a 16 Bytes table i.e 4x32bits words from DHTMEM base address + 7 to DHTMEM + 10*/
2940 address = (hjpeg->Instance->DHTMEM + 10UL);
2941 index = 16;
2942 while (index > 3UL)
2943 {
2944
2945 *address = (((uint32_t)HuffTableAC0->Bits[index - 1UL] & 0xFFUL) << 24) |
2946 (((uint32_t)HuffTableAC0->Bits[index - 2UL] & 0xFFUL) << 16) |
2947 (((uint32_t)HuffTableAC0->Bits[index - 3UL] & 0xFFUL) << 8) |
2948 ((uint32_t)HuffTableAC0->Bits[index - 4UL] & 0xFFUL);
2949 address--;
2950 index -= 4UL;
2951
2952 }
2953 /* AC0 Huffman Table : Val*/
2954 /* AC0 VALS is a 162 Bytes table i.e 41x32bits words from DHTMEM base address + 11 to DHTMEM + 51 */
2955 /* only Byte 0 and Byte 1 of the last word (@ DHTMEM + 51) belong to AC0 VALS table */
2956 address = (hjpeg->Instance->DHTMEM + 51);
2957 value = *address & 0xFFFF0000U;
2958 value = value | (((uint32_t)HuffTableAC0->HuffVal[161] & 0xFFUL) << 8) |
2959 ((uint32_t)HuffTableAC0->HuffVal[160] & 0xFFUL);
2960 *address = value;
2961
2962 /*continue setting 160 AC0 huffman values */
2963 address--; /* address = hjpeg->Instance->DHTMEM + 50*/
2964 index = JPEG_AC_HUFF_TABLE_SIZE - 2UL;
2965 while (index > 3UL)
2966 {
2967 *address = (((uint32_t)HuffTableAC0->HuffVal[index - 1UL] & 0xFFUL) << 24) |
2968 (((uint32_t)HuffTableAC0->HuffVal[index - 2UL] & 0xFFUL) << 16) |
2969 (((uint32_t)HuffTableAC0->HuffVal[index - 3UL] & 0xFFUL) << 8) |
2970 ((uint32_t)HuffTableAC0->HuffVal[index - 4UL] & 0xFFUL);
2971 address--;
2972 index -= 4UL;
2973 }
2974
2975 /* DC1 Huffman Table : BITS*/
2976 /* DC1 BITS is a 16 Bytes table i.e 4x32bits words from DHTMEM + 51 base address to DHTMEM + 55*/
2977 /* only Byte 2 and Byte 3 of the first word (@ DHTMEM + 51) belong to DC1 Bits table */
2978 address = (hjpeg->Instance->DHTMEM + 51);
2979 value = *address & 0x0000FFFFU;
2980 value = value | (((uint32_t)HuffTableDC1->Bits[1] & 0xFFUL) << 24) |
2981 (((uint32_t)HuffTableDC1->Bits[0] & 0xFFUL) << 16);
2982 *address = value;
2983
2984 /* only Byte 0 and Byte 1 of the last word (@ DHTMEM + 55) belong to DC1 Bits table */
2985 address = (hjpeg->Instance->DHTMEM + 55);
2986 value = *address & 0xFFFF0000U;
2987 value = value | (((uint32_t)HuffTableDC1->Bits[15] & 0xFFUL) << 8) |
2988 ((uint32_t)HuffTableDC1->Bits[14] & 0xFFUL);
2989 *address = value;
2990
2991 /*continue setting 12 DC1 huffman Bits from DHTMEM + 54 down to DHTMEM + 52*/
2992 address--;
2993 index = 12;
2994 while (index > 3UL)
2995 {
2996
2997 *address = (((uint32_t)HuffTableDC1->Bits[index + 1UL] & 0xFFUL) << 24) |
2998 (((uint32_t)HuffTableDC1->Bits[index] & 0xFFUL) << 16) |
2999 (((uint32_t)HuffTableDC1->Bits[index - 1UL] & 0xFFUL) << 8) |
3000 ((uint32_t)HuffTableDC1->Bits[index - 2UL] & 0xFFUL);
3001 address--;
3002 index -= 4UL;
3003
3004 }
3005 /* DC1 Huffman Table : Val*/
3006 /* DC1 VALS is a 12 Bytes table i.e 3x32bits words from DHTMEM base address +55 to DHTMEM + 58 */
3007 /* only Byte 2 and Byte 3 of the first word (@ DHTMEM + 55) belong to DC1 Val table */
3008 address = (hjpeg->Instance->DHTMEM + 55);
3009 value = *address & 0x0000FFFFUL;
3010 value = value | (((uint32_t)HuffTableDC1->HuffVal[1] & 0xFFUL) << 24) |
3011 (((uint32_t)HuffTableDC1->HuffVal[0] & 0xFFUL) << 16);
3012 *address = value;
3013
3014 /* only Byte 0 and Byte 1 of the last word (@ DHTMEM + 58) belong to DC1 Val table */
3015 address = (hjpeg->Instance->DHTMEM + 58);
3016 value = *address & 0xFFFF0000UL;
3017 value = value | (((uint32_t)HuffTableDC1->HuffVal[11] & 0xFFUL) << 8) |
3018 ((uint32_t)HuffTableDC1->HuffVal[10] & 0xFFUL);
3019 *address = value;
3020
3021 /*continue setting 8 DC1 huffman val from DHTMEM + 57 down to DHTMEM + 56*/
3022 address--;
3023 index = 8;
3024 while (index > 3UL)
3025 {
3026 *address = (((uint32_t)HuffTableDC1->HuffVal[index + 1UL] & 0xFFUL) << 24) |
3027 (((uint32_t)HuffTableDC1->HuffVal[index] & 0xFFUL) << 16) |
3028 (((uint32_t)HuffTableDC1->HuffVal[index - 1UL] & 0xFFUL) << 8) |
3029 ((uint32_t)HuffTableDC1->HuffVal[index - 2UL] & 0xFFUL);
3030 address--;
3031 index -= 4UL;
3032 }
3033
3034 /* AC1 Huffman Table : BITS*/
3035 /* AC1 BITS is a 16 Bytes table i.e 4x32bits words from DHTMEM base address + 58 to DHTMEM + 62*/
3036 /* only Byte 2 and Byte 3 of the first word (@ DHTMEM + 58) belong to AC1 Bits table */
3037 address = (hjpeg->Instance->DHTMEM + 58);
3038 value = *address & 0x0000FFFFU;
3039 value = value | (((uint32_t)HuffTableAC1->Bits[1] & 0xFFUL) << 24) |
3040 (((uint32_t)HuffTableAC1->Bits[0] & 0xFFUL) << 16);
3041 *address = value;
3042
3043 /* only Byte 0 and Byte 1 of the last word (@ DHTMEM + 62) belong to Bits Val table */
3044 address = (hjpeg->Instance->DHTMEM + 62);
3045 value = *address & 0xFFFF0000U;
3046 value = value | (((uint32_t)HuffTableAC1->Bits[15] & 0xFFUL) << 8) | ((uint32_t)HuffTableAC1->Bits[14] & 0xFFUL);
3047 *address = value;
3048
3049 /*continue setting 12 AC1 huffman Bits from DHTMEM + 61 down to DHTMEM + 59*/
3050 address--;
3051 index = 12;
3052 while (index > 3UL)
3053 {
3054
3055 *address = (((uint32_t)HuffTableAC1->Bits[index + 1UL] & 0xFFUL) << 24) |
3056 (((uint32_t)HuffTableAC1->Bits[index] & 0xFFUL) << 16) |
3057 (((uint32_t)HuffTableAC1->Bits[index - 1UL] & 0xFFUL) << 8) |
3058 ((uint32_t)HuffTableAC1->Bits[index - 2UL] & 0xFFUL);
3059 address--;
3060 index -= 4UL;
3061
3062 }
3063 /* AC1 Huffman Table : Val*/
3064 /* AC1 VALS is a 162 Bytes table i.e 41x32bits words from DHTMEM base address + 62 to DHTMEM + 102 */
3065 /* only Byte 2 and Byte 3 of the first word (@ DHTMEM + 62) belong to AC1 VALS table */
3066 address = (hjpeg->Instance->DHTMEM + 62);
3067 value = *address & 0x0000FFFFUL;
3068 value = value | (((uint32_t)HuffTableAC1->HuffVal[1] & 0xFFUL) << 24) |
3069 (((uint32_t)HuffTableAC1->HuffVal[0] & 0xFFUL) << 16);
3070 *address = value;
3071
3072 /*continue setting 160 AC1 huffman values from DHTMEM + 63 to DHTMEM+102 */
3073 address = (hjpeg->Instance->DHTMEM + 102);
3074 index = JPEG_AC_HUFF_TABLE_SIZE - 2UL;
3075 while (index > 3UL)
3076 {
3077 *address = (((uint32_t)HuffTableAC1->HuffVal[index + 1UL] & 0xFFUL) << 24) |
3078 (((uint32_t)HuffTableAC1->HuffVal[index] & 0xFFUL) << 16) |
3079 (((uint32_t)HuffTableAC1->HuffVal[index - 1UL] & 0xFFUL) << 8) |
3080 ((uint32_t)HuffTableAC1->HuffVal[index - 2UL] & 0xFFUL);
3081 address--;
3082 index -= 4UL;
3083 }
3084
3085 }
3086
3087 /**
3088 * @brief Configure the JPEG registers with a given quantization table
3089 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
3090 * the configuration information for JPEG module
3091 * @param QTable pointer to an array of 64 bytes giving the quantization table
3092 * @param QTableAddress destination quantization address in the JPEG peripheral
3093 * it could be QMEM0, QMEM1, QMEM2 or QMEM3
3094 * @retval 0 if no error, 1 if error
3095 */
JPEG_Set_Quantization_Mem(const JPEG_HandleTypeDef * hjpeg,const uint8_t * QTable,__IO uint32_t * QTableAddress)3096 static uint32_t JPEG_Set_Quantization_Mem(const JPEG_HandleTypeDef *hjpeg, const uint8_t *QTable,
3097 __IO uint32_t *QTableAddress)
3098 {
3099 uint32_t i;
3100 uint32_t j;
3101 uint32_t quantRow;
3102 uint32_t quantVal;
3103 uint32_t ScaleFactor;
3104 __IO uint32_t *tableAddress;
3105
3106 tableAddress = QTableAddress;
3107
3108 if ((hjpeg->Conf.ImageQuality >= 50UL) && (hjpeg->Conf.ImageQuality <= 100UL))
3109 {
3110 ScaleFactor = JPEG_HIGH_QUALITY_REFERENCE - (hjpeg->Conf.ImageQuality * 2UL);
3111 }
3112 else if (hjpeg->Conf.ImageQuality > 0UL)
3113 {
3114 ScaleFactor = JPEG_LOW_QUALITY_REFERENCE / ((uint32_t) hjpeg->Conf.ImageQuality);
3115 }
3116 else
3117 {
3118 return 1UL;
3119 }
3120
3121 /*Quantization_table = (Standard_quanization_table * ScaleFactor + 50) / 100*/
3122 i = 0;
3123 while (i < (JPEG_QUANT_TABLE_SIZE - 3UL))
3124 {
3125 quantRow = 0;
3126 for (j = 0; j < 4UL; j++)
3127 {
3128 /* Note that the quantization coefficients must be specified in the table in zigzag order */
3129 quantVal = ((((uint32_t) QTable[JPEG_ZIGZAG_ORDER[i + j]]) * ScaleFactor) + 50UL) / 100UL;
3130
3131 if (quantVal == 0UL)
3132 {
3133 quantVal = 1UL;
3134 }
3135 else if (quantVal > 255UL)
3136 {
3137 quantVal = JPEG_QUANTVAL_MAX;
3138 }
3139 else
3140 {
3141 /* Nothing to do, keep same value of quantVal */
3142 }
3143
3144 quantRow |= ((quantVal & 0xFFUL) << (8UL * j));
3145 }
3146
3147 i += 4UL;
3148 *tableAddress = quantRow;
3149 tableAddress ++;
3150 }
3151
3152 /* Return function status */
3153 return 0UL;
3154 }
3155
3156 /**
3157 * @brief Configure the JPEG registers for YCbCr color space
3158 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
3159 * the configuration information for JPEG module
3160 * @retval None
3161 */
JPEG_SetColorYCBCR(JPEG_HandleTypeDef * hjpeg)3162 static void JPEG_SetColorYCBCR(JPEG_HandleTypeDef *hjpeg)
3163 {
3164 uint32_t ySamplingH;
3165 uint32_t ySamplingV;
3166 uint32_t yblockNb;
3167
3168 /*Set Number of color components to 3*/
3169 hjpeg->Instance->CONFR1 &= ~JPEG_CONFR1_NF;
3170 hjpeg->Instance->CONFR1 |= JPEG_CONFR1_NF_1;
3171
3172 /* compute MCU block size and Y, Cb ,Cr sampling factors*/
3173 if (hjpeg->Conf.ChromaSubsampling == JPEG_420_SUBSAMPLING)
3174 {
3175 ySamplingH = JPEG_CONFR4_HSF_1; /* Hs = 2*/
3176 ySamplingV = JPEG_CONFR4_VSF_1; /* Vs = 2*/
3177
3178 yblockNb = 0x30; /* 4 blocks of 8x8*/
3179 }
3180 else if (hjpeg->Conf.ChromaSubsampling == JPEG_422_SUBSAMPLING)
3181 {
3182 ySamplingH = JPEG_CONFR4_HSF_1; /* Hs = 2*/
3183 ySamplingV = JPEG_CONFR4_VSF_0; /* Vs = 1*/
3184
3185 yblockNb = 0x10; /* 2 blocks of 8x8*/
3186 }
3187 else /*JPEG_444_SUBSAMPLING and default*/
3188 {
3189 ySamplingH = JPEG_CONFR4_HSF_0; /* Hs = 1*/
3190 ySamplingV = JPEG_CONFR4_VSF_0; /* Vs = 1*/
3191
3192 yblockNb = 0; /* 1 block of 8x8*/
3193 }
3194
3195 hjpeg->Instance->CONFR1 &= ~(JPEG_CONFR1_NF | JPEG_CONFR1_NS);
3196 hjpeg->Instance->CONFR1 |= (JPEG_CONFR1_NF_1 | JPEG_CONFR1_NS_1);
3197
3198 /*Reset CONFR4 register*/
3199 hjpeg->Instance->CONFR4 = 0;
3200 /*Set Horizental and Vertical sampling factor , number of blocks,
3201 Quantization table and Huffman AC/DC tables for component 0*/
3202 hjpeg->Instance->CONFR4 |= (ySamplingH | ySamplingV | (yblockNb & JPEG_CONFR4_NB));
3203
3204 /*Reset CONFR5 register*/
3205 hjpeg->Instance->CONFR5 = 0;
3206 /*Set Horizental and Vertical sampling factor , number of blocks,
3207 Quantization table and Huffman AC/DC tables for component 1*/
3208 hjpeg->Instance->CONFR5 |= (JPEG_CONFR5_HSF_0 | JPEG_CONFR5_VSF_0 | JPEG_CONFR5_QT_0 | JPEG_CONFR5_HA |
3209 JPEG_CONFR5_HD);
3210
3211 /*Reset CONFR6 register*/
3212 hjpeg->Instance->CONFR6 = 0;
3213 /*Set Horizental and Vertical sampling factor and number of blocks for component 2*/
3214 /* In YCBCR , by default, both chrominance components (component 1 and component 2)
3215 use the same Quantization table (table 1) */
3216 /* In YCBCR , both chrominance components (component 1 and component 2) use the same Huffman tables (table 1) */
3217 hjpeg->Instance->CONFR6 |= (JPEG_CONFR6_HSF_0 | JPEG_CONFR6_VSF_0 | JPEG_CONFR6_QT_0 | JPEG_CONFR6_HA |
3218 JPEG_CONFR6_HD);
3219
3220 }
3221
3222 /**
3223 * @brief Configure the JPEG registers for GrayScale color space
3224 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
3225 * the configuration information for JPEG module
3226 * @retval None
3227 */
JPEG_SetColorGrayScale(JPEG_HandleTypeDef * hjpeg)3228 static void JPEG_SetColorGrayScale(JPEG_HandleTypeDef *hjpeg)
3229 {
3230 /*Set Number of color components to 1*/
3231 hjpeg->Instance->CONFR1 &= ~(JPEG_CONFR1_NF | JPEG_CONFR1_NS);
3232
3233 /*in GrayScale use 1 single Quantization table (Table 0)*/
3234 /*in GrayScale use only one couple of AC/DC huffman table (table 0)*/
3235
3236 /*Reset CONFR4 register*/
3237 hjpeg->Instance->CONFR4 = 0;
3238 /*Set Horizental and Vertical sampling factor , number of blocks,
3239 Quantization table and Huffman AC/DC tables for component 0*/
3240 hjpeg->Instance->CONFR4 |= JPEG_CONFR4_HSF_0 | JPEG_CONFR4_VSF_0 ;
3241 }
3242
3243 /**
3244 * @brief Configure the JPEG registers for CMYK color space
3245 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
3246 * the configuration information for JPEG module
3247 * @retval None
3248 */
JPEG_SetColorCMYK(JPEG_HandleTypeDef * hjpeg)3249 static void JPEG_SetColorCMYK(JPEG_HandleTypeDef *hjpeg)
3250 {
3251 uint32_t ySamplingH;
3252 uint32_t ySamplingV;
3253 uint32_t yblockNb;
3254
3255 /*Set Number of color components to 4*/
3256 hjpeg->Instance->CONFR1 |= (JPEG_CONFR1_NF | JPEG_CONFR1_NS);
3257
3258 /* compute MCU block size and Y, Cb ,Cr sampling factors*/
3259 if (hjpeg->Conf.ChromaSubsampling == JPEG_420_SUBSAMPLING)
3260 {
3261 ySamplingH = JPEG_CONFR4_HSF_1; /* Hs = 2*/
3262 ySamplingV = JPEG_CONFR4_VSF_1; /* Vs = 2*/
3263
3264 yblockNb = 0x30; /* 4 blocks of 8x8*/
3265 }
3266 else if (hjpeg->Conf.ChromaSubsampling == JPEG_422_SUBSAMPLING)
3267 {
3268 ySamplingH = JPEG_CONFR4_HSF_1; /* Hs = 2*/
3269 ySamplingV = JPEG_CONFR4_VSF_0; /* Vs = 1*/
3270
3271 yblockNb = 0x10; /* 2 blocks of 8x8*/
3272 }
3273 else /*JPEG_444_SUBSAMPLING and default*/
3274 {
3275 ySamplingH = JPEG_CONFR4_HSF_0; /* Hs = 1*/
3276 ySamplingV = JPEG_CONFR4_VSF_0; /* Vs = 1*/
3277
3278 yblockNb = 0; /* 1 block of 8x8*/
3279 }
3280
3281 /*Reset CONFR4 register*/
3282 hjpeg->Instance->CONFR4 = 0;
3283 /*Set Horizental and Vertical sampling factor , number of blocks,
3284 Quantization table and Huffman AC/DC tables for component 0*/
3285 hjpeg->Instance->CONFR4 |= (ySamplingH | ySamplingV | (yblockNb & JPEG_CONFR4_NB));
3286
3287 /*Reset CONFR5 register*/
3288 hjpeg->Instance->CONFR5 = 0;
3289 /*Set Horizental and Vertical sampling factor , number of blocks,
3290 Quantization table and Huffman AC/DC tables for component 1*/
3291 hjpeg->Instance->CONFR5 |= (JPEG_CONFR5_HSF_0 | JPEG_CONFR5_VSF_0);
3292
3293 /*Reset CONFR6 register*/
3294 hjpeg->Instance->CONFR6 = 0;
3295 /*Set Horizental and Vertical sampling factor , number of blocks,
3296 Quantization table and Huffman AC/DC tables for component 2*/
3297 hjpeg->Instance->CONFR6 |= (JPEG_CONFR6_HSF_0 | JPEG_CONFR6_VSF_0);
3298
3299 /*Reset CONFR7 register*/
3300 hjpeg->Instance->CONFR7 = 0;
3301 /*Set Horizental and Vertical sampling factor , number of blocks,
3302 Quantization table and Huffman AC/DC tables for component 3*/
3303 hjpeg->Instance->CONFR7 |= (JPEG_CONFR7_HSF_0 | JPEG_CONFR7_VSF_0);
3304 }
3305
3306 /**
3307 * @brief Init the JPEG encoding/decoding process in case of Polling or Interrupt and DMA
3308 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
3309 * the configuration information for JPEG module
3310 * @retval None
3311 */
JPEG_Init_Process(JPEG_HandleTypeDef * hjpeg)3312 static void JPEG_Init_Process(JPEG_HandleTypeDef *hjpeg)
3313 {
3314 /*Reset pause*/
3315 hjpeg->Context &= (~(JPEG_CONTEXT_PAUSE_INPUT | JPEG_CONTEXT_PAUSE_OUTPUT));
3316
3317 if ((hjpeg->Context & JPEG_CONTEXT_OPERATION_MASK) == JPEG_CONTEXT_DECODE)
3318 {
3319 /*Set JPEG Codec to Decoding mode */
3320 hjpeg->Instance->CONFR1 |= JPEG_CONFR1_DE;
3321 }
3322 else /* JPEG_CONTEXT_ENCODE */
3323 {
3324 /*Set JPEG Codec to Encoding mode */
3325 hjpeg->Instance->CONFR1 &= ~JPEG_CONFR1_DE;
3326 }
3327
3328 /*Stop JPEG processing */
3329 hjpeg->Instance->CONFR0 &= ~JPEG_CONFR0_START;
3330
3331 /* Disable All Interrupts */
3332 __HAL_JPEG_DISABLE_IT(hjpeg, JPEG_INTERRUPT_MASK);
3333
3334 /* Disable All DMA requests */
3335 JPEG_DISABLE_DMA(hjpeg, JPEG_DMA_MASK);
3336 /* Flush input and output FIFOs*/
3337 hjpeg->Instance->CR |= JPEG_CR_IFF;
3338 hjpeg->Instance->CR |= JPEG_CR_OFF;
3339
3340 /* Clear all flags */
3341 __HAL_JPEG_CLEAR_FLAG(hjpeg, JPEG_FLAG_ALL);
3342
3343 /*Start Encoding/Decoding*/
3344 hjpeg->Instance->CONFR0 |= JPEG_CONFR0_START;
3345
3346 if ((hjpeg->Context & JPEG_CONTEXT_METHOD_MASK) == JPEG_CONTEXT_IT)
3347 {
3348 /*Enable IN/OUT, end of Conversation, and end of header parsing interruptions*/
3349 __HAL_JPEG_ENABLE_IT(hjpeg, JPEG_IT_IFT | JPEG_IT_IFNF | JPEG_IT_OFT | JPEG_IT_OFNE | JPEG_IT_EOC | JPEG_IT_HPD);
3350 }
3351 else if ((hjpeg->Context & JPEG_CONTEXT_METHOD_MASK) == JPEG_CONTEXT_DMA)
3352 {
3353 /*Enable End Of Conversation, and End Of Header parsing interruptions*/
3354 __HAL_JPEG_ENABLE_IT(hjpeg, JPEG_IT_EOC | JPEG_IT_HPD);
3355
3356 }
3357 else
3358 {
3359 /* Nothing to do */
3360 }
3361 }
3362
3363 /**
3364 * @brief JPEG encoding/decoding process in case of Polling or Interrupt
3365 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
3366 * the configuration information for JPEG module
3367 * @retval JPEG_PROCESS_DONE if the process has ends else JPEG_PROCESS_ONGOING
3368 */
JPEG_Process(JPEG_HandleTypeDef * hjpeg)3369 static uint32_t JPEG_Process(JPEG_HandleTypeDef *hjpeg)
3370 {
3371 uint32_t tmpContext;
3372 uint32_t itflag = hjpeg->Instance->SR;
3373
3374 /*End of header processing flag */
3375 if ((hjpeg->Context & JPEG_CONTEXT_OPERATION_MASK) == JPEG_CONTEXT_DECODE)
3376 {
3377 if ((itflag & JPEG_FLAG_HPDF) != 0UL)
3378 {
3379 /*Call Header parsing complete callback */
3380 (void) HAL_JPEG_GetInfo(hjpeg, &hjpeg->Conf);
3381 /* Reset the ImageQuality */
3382 hjpeg->Conf.ImageQuality = 0;
3383 /* Note : the image quality is only available at the end of the decoding operation */
3384 /* at the current stage the calculated image quality is not correct so reset it */
3385
3386 /*Call Info Ready callback */
3387 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
3388 hjpeg->InfoReadyCallback(hjpeg, &hjpeg->Conf);
3389 #else
3390 HAL_JPEG_InfoReadyCallback(hjpeg, &hjpeg->Conf);
3391 #endif /* USE_HAL_JPEG_REGISTER_CALLBACKS */
3392
3393 __HAL_JPEG_DISABLE_IT(hjpeg, JPEG_IT_HPD);
3394
3395 /* Clear header processing done flag */
3396 __HAL_JPEG_CLEAR_FLAG(hjpeg, JPEG_FLAG_HPDF);
3397 }
3398 }
3399
3400 /*Input FIFO status handling*/
3401 if ((hjpeg->Context & JPEG_CONTEXT_PAUSE_INPUT) == 0UL)
3402 {
3403 if ((itflag & JPEG_FLAG_IFTF) != 0UL)
3404 {
3405 /*Input FIFO threshold flag */
3406 /*JPEG_FIFO_TH_SIZE words can be written in */
3407 JPEG_ReadInputData(hjpeg, JPEG_FIFO_TH_SIZE);
3408 }
3409 else if ((itflag & JPEG_FLAG_IFNFF) != 0UL)
3410 {
3411 /*Input FIFO Not Full flag */
3412 /*32-bit value can be written in */
3413 JPEG_ReadInputData(hjpeg, 1);
3414 }
3415 else
3416 {
3417 /* Nothing to do */
3418 }
3419 }
3420
3421 /*Output FIFO flag handling*/
3422 if ((hjpeg->Context & JPEG_CONTEXT_PAUSE_OUTPUT) == 0UL)
3423 {
3424 if ((itflag & JPEG_FLAG_OFTF) != 0UL)
3425 {
3426 /*Output FIFO threshold flag */
3427 /*JPEG_FIFO_TH_SIZE words can be read out */
3428 JPEG_StoreOutputData(hjpeg, JPEG_FIFO_TH_SIZE);
3429 }
3430 else if ((itflag & JPEG_FLAG_OFNEF) != 0UL)
3431 {
3432 /*Output FIFO Not Empty flag */
3433 /*32-bit value can be read out */
3434 JPEG_StoreOutputData(hjpeg, 1);
3435 }
3436 else
3437 {
3438 /* Nothing to do */
3439 }
3440 }
3441
3442 /*End of Conversion handling :i.e EOC flag is high and OFTF low and OFNEF low*/
3443 if ((itflag & (JPEG_FLAG_EOCF | JPEG_FLAG_OFTF | JPEG_FLAG_OFNEF)) == JPEG_FLAG_EOCF)
3444 {
3445 /*Stop Encoding/Decoding*/
3446 hjpeg->Instance->CONFR0 &= ~JPEG_CONFR0_START;
3447
3448 if ((hjpeg->Context & JPEG_CONTEXT_METHOD_MASK) == JPEG_CONTEXT_IT)
3449 {
3450 /* Disable All Interrupts */
3451 __HAL_JPEG_DISABLE_IT(hjpeg, JPEG_INTERRUPT_MASK);
3452 }
3453
3454 /* Clear all flags */
3455 __HAL_JPEG_CLEAR_FLAG(hjpeg, JPEG_FLAG_ALL);
3456
3457 /*Call End of conversion callback */
3458 if (hjpeg->JpegOutCount > 0UL)
3459 {
3460 /*Output Buffer is not empty, call DecodedDataReadyCallback*/
3461 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
3462 hjpeg->DataReadyCallback(hjpeg, hjpeg->pJpegOutBuffPtr, hjpeg->JpegOutCount);
3463 #else
3464 HAL_JPEG_DataReadyCallback(hjpeg, hjpeg->pJpegOutBuffPtr, hjpeg->JpegOutCount);
3465 #endif /* USE_HAL_JPEG_REGISTER_CALLBACKS */
3466
3467 hjpeg->JpegOutCount = 0;
3468 }
3469
3470 /*Reset Context Operation*/
3471 tmpContext = hjpeg->Context;
3472 /*Clear all context fields execpt JPEG_CONTEXT_CONF_ENCODING and JPEG_CONTEXT_CUSTOM_TABLES*/
3473 hjpeg->Context &= (JPEG_CONTEXT_CONF_ENCODING | JPEG_CONTEXT_CUSTOM_TABLES);
3474
3475 /* Process Unlocked */
3476 __HAL_UNLOCK(hjpeg);
3477
3478 /* Change the JPEG state */
3479 hjpeg->State = HAL_JPEG_STATE_READY;
3480
3481 /*Call End of Encoding/Decoding callback */
3482 if ((tmpContext & JPEG_CONTEXT_OPERATION_MASK) == JPEG_CONTEXT_DECODE)
3483 {
3484 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
3485 hjpeg->DecodeCpltCallback(hjpeg);
3486 #else
3487 HAL_JPEG_DecodeCpltCallback(hjpeg);
3488 #endif /*USE_HAL_JPEG_REGISTER_CALLBACKS*/
3489 }
3490 else /* JPEG_CONTEXT_ENCODE */
3491 {
3492 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
3493 hjpeg->EncodeCpltCallback(hjpeg);
3494 #else
3495 HAL_JPEG_EncodeCpltCallback(hjpeg);
3496 #endif /*USE_HAL_JPEG_REGISTER_CALLBACKS*/
3497 }
3498
3499 return JPEG_PROCESS_DONE;
3500 }
3501
3502 return JPEG_PROCESS_ONGOING;
3503 }
3504
3505 /**
3506 * @brief Store some output data from the JPEG peripheral to the output buffer.
3507 * This function is used when the JPEG peripheral has new data to output
3508 * in case of Polling or Interrupt process
3509 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
3510 * the configuration information for JPEG module
3511 * @param nbOutputWords Number of output words (of 32 bits) ready from the JPEG peripheral
3512 * @retval None
3513 */
JPEG_StoreOutputData(JPEG_HandleTypeDef * hjpeg,uint32_t nbOutputWords)3514 static void JPEG_StoreOutputData(JPEG_HandleTypeDef *hjpeg, uint32_t nbOutputWords)
3515 {
3516 uint32_t index;
3517 uint32_t nb_words;
3518 uint32_t nb_bytes;
3519 uint32_t dataword;
3520
3521 if (hjpeg->OutDataLength >= (hjpeg->JpegOutCount + (nbOutputWords * 4UL)))
3522 {
3523 for (index = 0; index < nbOutputWords; index++)
3524 {
3525 /*Transfer 32 bits from the JPEG output FIFO*/
3526 dataword = hjpeg->Instance->DOR;
3527 hjpeg->pJpegOutBuffPtr[hjpeg->JpegOutCount] = (uint8_t)(dataword & 0x000000FFUL);
3528 hjpeg->pJpegOutBuffPtr[hjpeg->JpegOutCount + 1UL] = (uint8_t)((dataword & 0x0000FF00UL) >> 8);
3529 hjpeg->pJpegOutBuffPtr[hjpeg->JpegOutCount + 2UL] = (uint8_t)((dataword & 0x00FF0000UL) >> 16);
3530 hjpeg->pJpegOutBuffPtr[hjpeg->JpegOutCount + 3UL] = (uint8_t)((dataword & 0xFF000000UL) >> 24);
3531 hjpeg->JpegOutCount += 4UL;
3532 }
3533 if (hjpeg->OutDataLength == hjpeg->JpegOutCount)
3534 {
3535 /*Output Buffer is full, call DecodedDataReadyCallback*/
3536 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
3537 hjpeg->DataReadyCallback(hjpeg, hjpeg->pJpegOutBuffPtr, hjpeg->JpegOutCount);
3538 #else
3539 HAL_JPEG_DataReadyCallback(hjpeg, hjpeg->pJpegOutBuffPtr, hjpeg->JpegOutCount);
3540 #endif /*USE_HAL_JPEG_REGISTER_CALLBACKS*/
3541 hjpeg->JpegOutCount = 0;
3542 }
3543 }
3544 else if (hjpeg->OutDataLength > hjpeg->JpegOutCount)
3545 {
3546 nb_words = (hjpeg->OutDataLength - hjpeg->JpegOutCount) / 4UL;
3547 for (index = 0; index < nb_words; index++)
3548 {
3549 /*Transfer 32 bits from the JPEG output FIFO*/
3550 dataword = hjpeg->Instance->DOR;
3551 hjpeg->pJpegOutBuffPtr[hjpeg->JpegOutCount] = (uint8_t)(dataword & 0x000000FFUL);
3552 hjpeg->pJpegOutBuffPtr[hjpeg->JpegOutCount + 1UL] = (uint8_t)((dataword & 0x0000FF00UL) >> 8);
3553 hjpeg->pJpegOutBuffPtr[hjpeg->JpegOutCount + 2UL] = (uint8_t)((dataword & 0x00FF0000UL) >> 16);
3554 hjpeg->pJpegOutBuffPtr[hjpeg->JpegOutCount + 3UL] = (uint8_t)((dataword & 0xFF000000UL) >> 24);
3555 hjpeg->JpegOutCount += 4UL;
3556 }
3557 if (hjpeg->OutDataLength == hjpeg->JpegOutCount)
3558 {
3559 /*Output Buffer is full, call DecodedDataReadyCallback*/
3560 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
3561 hjpeg->DataReadyCallback(hjpeg, hjpeg->pJpegOutBuffPtr, hjpeg->JpegOutCount);
3562 #else
3563 HAL_JPEG_DataReadyCallback(hjpeg, hjpeg->pJpegOutBuffPtr, hjpeg->JpegOutCount);
3564 #endif /* USE_HAL_JPEG_REGISTER_CALLBACKS */
3565 hjpeg->JpegOutCount = 0;
3566 }
3567 else
3568 {
3569 nb_bytes = hjpeg->OutDataLength - hjpeg->JpegOutCount;
3570 dataword = hjpeg->Instance->DOR;
3571 for (index = 0; index < nb_bytes; index++)
3572 {
3573 hjpeg->pJpegOutBuffPtr[hjpeg->JpegOutCount] = (uint8_t)((dataword >> (8UL * (index & 0x3UL))) & 0xFFUL);
3574 hjpeg->JpegOutCount++;
3575 }
3576 /*Output Buffer is full, call DecodedDataReadyCallback*/
3577 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
3578 hjpeg->DataReadyCallback(hjpeg, hjpeg->pJpegOutBuffPtr, hjpeg->JpegOutCount);
3579 #else
3580 HAL_JPEG_DataReadyCallback(hjpeg, hjpeg->pJpegOutBuffPtr, hjpeg->JpegOutCount);
3581 #endif /* USE_HAL_JPEG_REGISTER_CALLBACKS */
3582
3583 hjpeg->JpegOutCount = 0;
3584
3585 nb_bytes = 4UL - nb_bytes;
3586 for (index = nb_bytes; index < 4UL; index++)
3587 {
3588 hjpeg->pJpegOutBuffPtr[hjpeg->JpegOutCount] = (uint8_t)((dataword >> (8UL * index)) & 0xFFUL);
3589 hjpeg->JpegOutCount++;
3590 }
3591 }
3592 }
3593 else
3594 {
3595 /* Nothing to do */
3596 }
3597 }
3598
3599 /**
3600 * @brief Read some input Data from the input buffer.
3601 * This function is used when the JPEG peripheral needs new data
3602 * in case of Polling or Interrupt process
3603 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
3604 * the configuration information for JPEG module
3605 * @param nbRequestWords Number of input words (of 32 bits) that the JPE peripheral request
3606 * @retval None
3607 */
JPEG_ReadInputData(JPEG_HandleTypeDef * hjpeg,uint32_t nbRequestWords)3608 static void JPEG_ReadInputData(JPEG_HandleTypeDef *hjpeg, uint32_t nbRequestWords)
3609 {
3610 uint32_t nb_bytes = 0;
3611 uint32_t nb_words;
3612 uint32_t index;
3613 uint32_t dataword;
3614 uint32_t input_count;
3615
3616 if ((hjpeg->InDataLength == 0UL) || (nbRequestWords == 0UL))
3617 {
3618 /* No more Input data : nothing to do*/
3619 (void) HAL_JPEG_Pause(hjpeg, JPEG_PAUSE_RESUME_INPUT);
3620 }
3621 else if (hjpeg->InDataLength > hjpeg->JpegInCount)
3622 {
3623 nb_bytes = hjpeg->InDataLength - hjpeg->JpegInCount;
3624 }
3625 else if (hjpeg->InDataLength == hjpeg->JpegInCount)
3626 {
3627 /*Call HAL_JPEG_GetDataCallback to get new data */
3628 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
3629 hjpeg->GetDataCallback(hjpeg, hjpeg->JpegInCount);
3630 #else
3631 HAL_JPEG_GetDataCallback(hjpeg, hjpeg->JpegInCount);
3632 #endif /*USE_HAL_JPEG_REGISTER_CALLBACKS*/
3633
3634 if (hjpeg->InDataLength > 4UL)
3635 {
3636 hjpeg->InDataLength = ((hjpeg->InDataLength + 3UL) / 4UL) * 4UL;
3637 }
3638 hjpeg->JpegInCount = 0;
3639 nb_bytes = hjpeg->InDataLength;
3640 }
3641 else
3642 {
3643 /* Nothing to do */
3644 }
3645 if (((hjpeg->Context & JPEG_CONTEXT_PAUSE_INPUT) == 0UL) && (nb_bytes > 0UL))
3646 {
3647 nb_words = nb_bytes / 4UL;
3648 if (nb_words >= nbRequestWords)
3649 {
3650 for (index = 0; index < nbRequestWords; index++)
3651 {
3652 input_count = hjpeg->JpegInCount;
3653 hjpeg->Instance->DIR = (((uint32_t)(hjpeg->pJpegInBuffPtr[input_count])) | \
3654 (((uint32_t)(hjpeg->pJpegInBuffPtr[input_count + 1UL])) << 8) | \
3655 (((uint32_t)(hjpeg->pJpegInBuffPtr[input_count + 2UL])) << 16) | \
3656 (((uint32_t)(hjpeg->pJpegInBuffPtr[input_count + 3UL])) << 24));
3657
3658 hjpeg->JpegInCount += 4UL;
3659 }
3660 }
3661 else /*nb_words < nbRequestWords*/
3662 {
3663 if (nb_words > 0UL)
3664 {
3665 for (index = 0; index < nb_words; index++)
3666 {
3667 input_count = hjpeg->JpegInCount;
3668 hjpeg->Instance->DIR = (((uint32_t)(hjpeg->pJpegInBuffPtr[input_count])) | \
3669 (((uint32_t)(hjpeg->pJpegInBuffPtr[input_count + 1UL])) << 8) | \
3670 (((uint32_t)(hjpeg->pJpegInBuffPtr[input_count + 2UL])) << 16) | \
3671 (((uint32_t)(hjpeg->pJpegInBuffPtr[input_count + 3UL])) << 24));
3672
3673 hjpeg->JpegInCount += 4UL;
3674 }
3675 }
3676 else
3677 {
3678 /* end of file*/
3679 dataword = 0;
3680 for (index = 0; index < nb_bytes; index++)
3681 {
3682 dataword |= (uint32_t)hjpeg->pJpegInBuffPtr[hjpeg->JpegInCount] << (8UL * (index & 0x03UL));
3683 hjpeg->JpegInCount++;
3684 }
3685 hjpeg->Instance->DIR = dataword;
3686 }
3687 }
3688 }
3689 }
3690
3691 /**
3692 * @brief Start the JPEG DMA process (encoding/decoding)
3693 * @note The DMA interrupt must have a higher priority than the JPEG
3694 * interrupt to prevent the JPEG interrupt from preempting the DMA interrupt
3695 * before the DMA state is updated to ready.
3696 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
3697 * the configuration information for JPEG module
3698 * @retval JPEG_PROCESS_DONE if process ends else JPEG_PROCESS_ONGOING
3699 */
JPEG_DMA_StartProcess(JPEG_HandleTypeDef * hjpeg)3700 static HAL_StatusTypeDef JPEG_DMA_StartProcess(JPEG_HandleTypeDef *hjpeg)
3701 {
3702 if ((hjpeg->InDataLength < 4UL) || (hjpeg->OutDataLength < 4UL))
3703 {
3704 return HAL_ERROR;
3705 }
3706 /* Reset Ending DMA internal context flag*/
3707 hjpeg->Context &= ~JPEG_CONTEXT_ENDING_DMA;
3708
3709 /* Disable DMA In/Out Request*/
3710 JPEG_DISABLE_DMA(hjpeg, JPEG_DMA_ODMA | JPEG_DMA_IDMA);
3711
3712 /* Set the JPEG DMA In transfer complete callback */
3713 hjpeg->hdmain->XferCpltCallback = JPEG_DMAInCpltCallback;
3714 /* Set the DMA In error callback */
3715 hjpeg->hdmain->XferErrorCallback = JPEG_DMAErrorCallback;
3716
3717 /* Set the JPEG DMA Out transfer complete callback */
3718 hjpeg->hdmaout->XferCpltCallback = JPEG_DMAOutCpltCallback;
3719 /* Set the DMA Out error callback */
3720 hjpeg->hdmaout->XferErrorCallback = JPEG_DMAErrorCallback;
3721 /* Set the DMA Out Abort callback */
3722 hjpeg->hdmaout->XferAbortCallback = JPEG_DMAOutAbortCallback;
3723
3724 /*DMA transfer size must be a multiple of 4 bytes i.e multiple of 32bits words*/
3725 hjpeg->InDataLength = hjpeg->InDataLength - (hjpeg->InDataLength % 4UL);
3726
3727 /*DMA transfer size must be a multiple of 4 bytes i.e multiple of 32bits words*/
3728 hjpeg->OutDataLength = hjpeg->OutDataLength - (hjpeg->OutDataLength % 4UL);
3729
3730 if ((hjpeg->hdmain->Mode & DMA_LINKEDLIST) == DMA_LINKEDLIST)
3731 {
3732 if (hjpeg->hdmain->LinkedListQueue != NULL)
3733 {
3734 /* Set DMA data size */
3735 hjpeg->hdmain->LinkedListQueue->Head->LinkRegisters[NODE_CBR1_DEFAULT_OFFSET] = hjpeg->InDataLength;
3736
3737 /* Set DMA source address */
3738 hjpeg->hdmain->LinkedListQueue->Head->LinkRegisters[NODE_CSAR_DEFAULT_OFFSET] = (uint32_t)hjpeg->pJpegInBuffPtr;
3739
3740 /* Set DMA destination address */
3741 hjpeg->hdmain->LinkedListQueue->Head->LinkRegisters[NODE_CDAR_DEFAULT_OFFSET] = (uint32_t)&hjpeg->Instance->DIR;
3742
3743 if (HAL_DMAEx_List_Start_IT(hjpeg->hdmain) != HAL_OK)
3744 {
3745 return HAL_ERROR;
3746 }
3747 }
3748 else
3749 {
3750 /* Set the JPEG State to ready */
3751 hjpeg->State = HAL_JPEG_STATE_READY;
3752
3753 /* Update JPEG error code */
3754 hjpeg->ErrorCode |= HAL_JPEG_ERROR_DMA;
3755
3756 /* Process Unlocked */
3757 __HAL_UNLOCK(hjpeg);
3758
3759 return HAL_ERROR;
3760 }
3761 }
3762 else
3763 {
3764 /* Start DMA FIFO In transfer */
3765 if (HAL_DMA_Start_IT(hjpeg->hdmain, (uint32_t)hjpeg->pJpegInBuffPtr, (uint32_t)&hjpeg->Instance->DIR,
3766 hjpeg->InDataLength) != HAL_OK)
3767 {
3768 hjpeg->ErrorCode |= HAL_JPEG_ERROR_DMA;
3769 return HAL_ERROR;
3770 }
3771 }
3772
3773 if ((hjpeg->hdmaout->Mode & DMA_LINKEDLIST) == DMA_LINKEDLIST)
3774 {
3775 if (hjpeg->hdmaout->LinkedListQueue != NULL)
3776 {
3777 /* Set DMA data size */
3778 hjpeg->hdmaout->LinkedListQueue->Head->LinkRegisters[NODE_CBR1_DEFAULT_OFFSET] = hjpeg->OutDataLength;
3779
3780 /* Set DMA source address */
3781 hjpeg->hdmaout->LinkedListQueue->Head->LinkRegisters[NODE_CSAR_DEFAULT_OFFSET] = (uint32_t)&hjpeg->Instance->DOR;
3782
3783 /* Set DMA destination address */
3784 hjpeg->hdmaout->LinkedListQueue->Head->LinkRegisters[NODE_CDAR_DEFAULT_OFFSET] = (uint32_t)hjpeg->pJpegOutBuffPtr;
3785
3786 if (HAL_DMAEx_List_Start_IT(hjpeg->hdmaout) != HAL_OK)
3787 {
3788 return HAL_ERROR;
3789 }
3790 }
3791 else
3792 {
3793 /* Set the JPEG State to ready */
3794 hjpeg->State = HAL_JPEG_STATE_READY;
3795
3796 /* Update JPEG error code */
3797 hjpeg->ErrorCode |= HAL_JPEG_ERROR_DMA;
3798
3799 /* Process Unlocked */
3800 __HAL_UNLOCK(hjpeg);
3801
3802 return HAL_ERROR;
3803 }
3804 }
3805 else
3806 {
3807 /* Start DMA FIFO Out transfer */
3808 if (HAL_DMA_Start_IT(hjpeg->hdmaout, (uint32_t)&hjpeg->Instance->DOR, (uint32_t)hjpeg->pJpegOutBuffPtr,
3809 hjpeg->OutDataLength) != HAL_OK)
3810 {
3811 hjpeg->ErrorCode |= HAL_JPEG_ERROR_DMA;
3812 return HAL_ERROR;
3813 }
3814 }
3815
3816 /* Enable JPEG In/Out DMA requests*/
3817 JPEG_ENABLE_DMA(hjpeg, JPEG_DMA_IDMA | JPEG_DMA_ODMA);
3818
3819 return HAL_OK;
3820 }
3821
3822 /**
3823 * @brief Continue the current JPEG DMA process (encoding/decoding)
3824 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
3825 * the configuration information for JPEG module
3826 * @retval JPEG_PROCESS_DONE if process ends else JPEG_PROCESS_ONGOING
3827 */
JPEG_DMA_ContinueProcess(JPEG_HandleTypeDef * hjpeg)3828 static void JPEG_DMA_ContinueProcess(JPEG_HandleTypeDef *hjpeg)
3829 {
3830 uint32_t itflag = hjpeg->Instance->SR;
3831
3832 /*End of header processing flag rises*/
3833 if ((hjpeg->Context & JPEG_CONTEXT_OPERATION_MASK) == JPEG_CONTEXT_DECODE)
3834 {
3835 if ((itflag & JPEG_FLAG_HPDF) != 0UL)
3836 {
3837 /*Call Header parsing complete callback */
3838 (void) HAL_JPEG_GetInfo(hjpeg, &hjpeg->Conf);
3839
3840 /* Reset the ImageQuality */
3841 hjpeg->Conf.ImageQuality = 0;
3842 /* Note : the image quality is only available at the end of the decoding operation */
3843 /* at the current stage the calculated image quality is not correct so reset it */
3844
3845 /*Call Info Ready callback */
3846 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
3847 hjpeg->InfoReadyCallback(hjpeg, &hjpeg->Conf);
3848 #else
3849 HAL_JPEG_InfoReadyCallback(hjpeg, &hjpeg->Conf);
3850 #endif /* USE_HAL_JPEG_REGISTER_CALLBACKS */
3851
3852 __HAL_JPEG_DISABLE_IT(hjpeg, JPEG_IT_HPD);
3853
3854 /* Clear header processing done flag */
3855 __HAL_JPEG_CLEAR_FLAG(hjpeg, JPEG_FLAG_HPDF);
3856 }
3857 }
3858
3859 /*End of Conversion handling*/
3860 if ((itflag & JPEG_FLAG_EOCF) != 0UL)
3861 {
3862 /*Disable JPEG In/Out DMA Requests*/
3863 JPEG_DISABLE_DMA(hjpeg, JPEG_DMA_ODMA | JPEG_DMA_IDMA);
3864
3865 hjpeg->Context |= JPEG_CONTEXT_ENDING_DMA;
3866
3867 /*Stop Encoding/Decoding*/
3868 hjpeg->Instance->CONFR0 &= ~JPEG_CONFR0_START;
3869
3870 __HAL_JPEG_DISABLE_IT(hjpeg, JPEG_INTERRUPT_MASK);
3871
3872 /* Clear all flags */
3873 __HAL_JPEG_CLEAR_FLAG(hjpeg, JPEG_FLAG_ALL);
3874
3875 if (hjpeg->hdmain->State == HAL_DMA_STATE_BUSY)
3876 {
3877 /* Stop the DMA In Xfer*/
3878 (void) HAL_DMA_Abort_IT(hjpeg->hdmain);
3879 }
3880
3881 if (hjpeg->hdmaout->State == HAL_DMA_STATE_BUSY)
3882 {
3883 /* Stop the DMA out Xfer*/
3884 (void) HAL_DMA_Abort_IT(hjpeg->hdmaout);
3885 }
3886 else
3887 {
3888 JPEG_DMA_EndProcess(hjpeg);
3889 }
3890 }
3891 }
3892
3893 /**
3894 * @brief Finalize the current JPEG DMA process (encoding/decoding)
3895 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
3896 * the configuration information for JPEG module
3897 * @retval JPEG_PROCESS_DONE
3898 */
JPEG_DMA_EndProcess(JPEG_HandleTypeDef * hjpeg)3899 static void JPEG_DMA_EndProcess(JPEG_HandleTypeDef *hjpeg)
3900 {
3901 uint32_t tmpContext;
3902 hjpeg->JpegOutCount = hjpeg->OutDataLength - JPEG_GET_DMA_REMAIN_DATA(hjpeg->hdmaout);
3903
3904 /*if Output Buffer is full, call HAL_JPEG_DataReadyCallback*/
3905 if (hjpeg->JpegOutCount == hjpeg->OutDataLength)
3906 {
3907 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
3908 hjpeg->DataReadyCallback(hjpeg, hjpeg->pJpegOutBuffPtr, hjpeg->JpegOutCount);
3909 #else
3910 HAL_JPEG_DataReadyCallback(hjpeg, hjpeg->pJpegOutBuffPtr, hjpeg->JpegOutCount);
3911 #endif /* USE_HAL_JPEG_REGISTER_CALLBACKS */
3912
3913 hjpeg->JpegOutCount = 0;
3914 }
3915
3916 /*Check if remaining data in the output FIFO*/
3917 if (__HAL_JPEG_GET_FLAG(hjpeg, JPEG_FLAG_OFNEF) == 0UL)
3918 {
3919 if (hjpeg->JpegOutCount > 0UL)
3920 {
3921 /*Output Buffer is not empty, call DecodedDataReadyCallback*/
3922 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
3923 hjpeg->DataReadyCallback(hjpeg, hjpeg->pJpegOutBuffPtr, hjpeg->JpegOutCount);
3924 #else
3925 HAL_JPEG_DataReadyCallback(hjpeg, hjpeg->pJpegOutBuffPtr, hjpeg->JpegOutCount);
3926 #endif /* USE_HAL_JPEG_REGISTER_CALLBACKS */
3927
3928 hjpeg->JpegOutCount = 0;
3929 }
3930
3931 /*Stop Encoding/Decoding*/
3932 hjpeg->Instance->CONFR0 &= ~JPEG_CONFR0_START;
3933
3934 tmpContext = hjpeg->Context;
3935 /*Clear all context fields execpt JPEG_CONTEXT_CONF_ENCODING and JPEG_CONTEXT_CUSTOM_TABLES*/
3936 hjpeg->Context &= (JPEG_CONTEXT_CONF_ENCODING | JPEG_CONTEXT_CUSTOM_TABLES);
3937
3938 /* Process Unlocked */
3939 __HAL_UNLOCK(hjpeg);
3940
3941 /* Change the JPEG state */
3942 hjpeg->State = HAL_JPEG_STATE_READY;
3943
3944 /*Call End of Encoding/Decoding callback */
3945 if ((tmpContext & JPEG_CONTEXT_OPERATION_MASK) == JPEG_CONTEXT_DECODE)
3946 {
3947 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
3948 hjpeg->DecodeCpltCallback(hjpeg);
3949 #else
3950 HAL_JPEG_DecodeCpltCallback(hjpeg);
3951 #endif /* USE_HAL_JPEG_REGISTER_CALLBACKS */
3952 }
3953 else /* JPEG_CONTEXT_ENCODE */
3954 {
3955 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
3956 hjpeg->EncodeCpltCallback(hjpeg);
3957 #else
3958 HAL_JPEG_EncodeCpltCallback(hjpeg);
3959 #endif /* USE_HAL_JPEG_REGISTER_CALLBACKS */
3960 }
3961 }
3962 else if ((hjpeg->Context & JPEG_CONTEXT_PAUSE_OUTPUT) == 0UL)
3963 {
3964 JPEG_DMA_PollResidualData(hjpeg);
3965 }
3966 else
3967 {
3968 /* Nothing to do */
3969 }
3970
3971 }
3972
3973 /**
3974 * @brief Poll residual output data when DMA process (encoding/decoding)
3975 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
3976 * the configuration information for JPEG module
3977 * @retval None.
3978 */
JPEG_DMA_PollResidualData(JPEG_HandleTypeDef * hjpeg)3979 static void JPEG_DMA_PollResidualData(JPEG_HandleTypeDef *hjpeg)
3980 {
3981 uint32_t tmpContext;
3982 uint32_t count;
3983 uint32_t dataOut;
3984
3985 for (count = JPEG_FIFO_SIZE; count > 0UL; count--)
3986 {
3987 if ((hjpeg->Context & JPEG_CONTEXT_PAUSE_OUTPUT) == 0UL)
3988 {
3989 if (__HAL_JPEG_GET_FLAG(hjpeg, JPEG_FLAG_OFNEF) != 0UL)
3990 {
3991 dataOut = hjpeg->Instance->DOR;
3992 hjpeg->pJpegOutBuffPtr[hjpeg->JpegOutCount] = (uint8_t)(dataOut & 0x000000FFUL);
3993 hjpeg->pJpegOutBuffPtr[hjpeg->JpegOutCount + 1UL] = (uint8_t)((dataOut & 0x0000FF00UL) >> 8);
3994 hjpeg->pJpegOutBuffPtr[hjpeg->JpegOutCount + 2UL] = (uint8_t)((dataOut & 0x00FF0000UL) >> 16);
3995 hjpeg->pJpegOutBuffPtr[hjpeg->JpegOutCount + 3UL] = (uint8_t)((dataOut & 0xFF000000UL) >> 24);
3996 hjpeg->JpegOutCount += 4UL;
3997
3998 if (hjpeg->JpegOutCount == hjpeg->OutDataLength)
3999 {
4000 /*Output Buffer is full, call HAL_JPEG_DataReadyCallback*/
4001 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
4002 hjpeg->DataReadyCallback(hjpeg, hjpeg->pJpegOutBuffPtr, hjpeg->JpegOutCount);
4003 #else
4004 HAL_JPEG_DataReadyCallback(hjpeg, hjpeg->pJpegOutBuffPtr, hjpeg->JpegOutCount);
4005 #endif /* USE_HAL_JPEG_REGISTER_CALLBACKS */
4006
4007 hjpeg->JpegOutCount = 0;
4008 }
4009
4010 }
4011 }
4012 }
4013
4014 tmpContext = hjpeg->Context;
4015
4016 if ((__HAL_JPEG_GET_FLAG(hjpeg, JPEG_FLAG_OFNEF) == 0UL) || ((tmpContext & JPEG_CONTEXT_PAUSE_OUTPUT) == 0UL))
4017 {
4018 /*Stop Encoding/Decoding*/
4019 hjpeg->Instance->CONFR0 &= ~JPEG_CONFR0_START;
4020
4021 if (hjpeg->JpegOutCount > 0UL)
4022 {
4023 /*Output Buffer is not empty, call DecodedDataReadyCallback*/
4024 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
4025 hjpeg->DataReadyCallback(hjpeg, hjpeg->pJpegOutBuffPtr, hjpeg->JpegOutCount);
4026 #else
4027 HAL_JPEG_DataReadyCallback(hjpeg, hjpeg->pJpegOutBuffPtr, hjpeg->JpegOutCount);
4028 #endif /* USE_HAL_JPEG_REGISTER_CALLBACKS */
4029
4030 hjpeg->JpegOutCount = 0;
4031 }
4032
4033 tmpContext = hjpeg->Context;
4034 /*Clear all context fields execpt JPEG_CONTEXT_CONF_ENCODING and JPEG_CONTEXT_CUSTOM_TABLES*/
4035 hjpeg->Context &= (JPEG_CONTEXT_CONF_ENCODING | JPEG_CONTEXT_CUSTOM_TABLES);
4036
4037 /* Process Unlocked */
4038 __HAL_UNLOCK(hjpeg);
4039
4040 /* Change the JPEG state */
4041 hjpeg->State = HAL_JPEG_STATE_READY;
4042
4043 /*Call End of Encoding/Decoding callback */
4044 if ((tmpContext & JPEG_CONTEXT_OPERATION_MASK) == JPEG_CONTEXT_DECODE)
4045 {
4046 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
4047 hjpeg->DecodeCpltCallback(hjpeg);
4048 #else
4049 HAL_JPEG_DecodeCpltCallback(hjpeg);
4050 #endif /* USE_HAL_JPEG_REGISTER_CALLBACKS */
4051 }
4052 else /* JPEG_CONTEXT_ENCODE */
4053 {
4054 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
4055 hjpeg->EncodeCpltCallback(hjpeg);
4056 #else
4057 HAL_JPEG_EncodeCpltCallback(hjpeg);
4058 #endif /* USE_HAL_JPEG_REGISTER_CALLBACKS */
4059 }
4060 }
4061 }
4062
4063 /**
4064 * @brief DMA input transfer complete callback
4065 * @param hdma pointer to a DMA_HandleTypeDef structure.
4066 * @retval None
4067 */
JPEG_DMAInCpltCallback(DMA_HandleTypeDef * hdma)4068 static void JPEG_DMAInCpltCallback(DMA_HandleTypeDef *hdma)
4069 {
4070 JPEG_HandleTypeDef *hjpeg = (JPEG_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
4071
4072 /* Disable The JPEG IT so the DMA Input Callback can not be interrupted by the JPEG EOC IT or JPEG HPD IT */
4073 __HAL_JPEG_DISABLE_IT(hjpeg, JPEG_INTERRUPT_MASK);
4074
4075 if ((hjpeg->Context & (JPEG_CONTEXT_METHOD_MASK | JPEG_CONTEXT_ENDING_DMA)) ==
4076 JPEG_CONTEXT_DMA) /* Check if context method is DMA and we are not in ending DMA stage */
4077 {
4078 JPEG_DISABLE_DMA(hjpeg, JPEG_DMA_IDMA);
4079
4080 hjpeg->JpegInCount = hjpeg->InDataLength - JPEG_GET_DMA_REMAIN_DATA(hdma);
4081
4082 /*Call HAL_JPEG_GetDataCallback to get new data */
4083 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
4084 hjpeg->GetDataCallback(hjpeg, hjpeg->JpegInCount);
4085 #else
4086 HAL_JPEG_GetDataCallback(hjpeg, hjpeg->JpegInCount);
4087 #endif /* USE_HAL_JPEG_REGISTER_CALLBACKS */
4088
4089 if (hjpeg->InDataLength >= 4UL)
4090 {
4091 /*JPEG Input DMA transfer data number must be multiple of 32 bits word
4092 as the destination is a 32 bits (4 bytes) register */
4093 hjpeg->InDataLength = ((hjpeg->InDataLength + 3UL) / 4UL) * 4UL;
4094 }
4095 else
4096 {
4097 /* Nothing to do */
4098 }
4099
4100 if (((hjpeg->Context & JPEG_CONTEXT_PAUSE_INPUT) == 0UL) && (hjpeg->InDataLength > 0UL))
4101 {
4102 /* Start DMA FIFO In transfer */
4103 if (HAL_DMA_Start_IT(hjpeg->hdmain, (uint32_t)hjpeg->pJpegInBuffPtr, (uint32_t)&hjpeg->Instance->DIR,
4104 hjpeg->InDataLength) != HAL_OK)
4105 {
4106 hjpeg->ErrorCode |= HAL_JPEG_ERROR_DMA;
4107 hjpeg->State = HAL_JPEG_STATE_ERROR;
4108 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
4109 hjpeg->ErrorCallback(hjpeg);
4110 #else
4111 HAL_JPEG_ErrorCallback(hjpeg);
4112 #endif /* USE_HAL_JPEG_REGISTER_CALLBACKS */
4113 return;
4114 }
4115 JPEG_ENABLE_DMA(hjpeg, JPEG_DMA_IDMA);
4116 }
4117
4118 /* JPEG Conversion still on going : Enable the JPEG IT */
4119 __HAL_JPEG_ENABLE_IT(hjpeg, JPEG_IT_EOC | JPEG_IT_HPD);
4120 }
4121 }
4122
4123 /**
4124 * @brief DMA output transfer complete callback
4125 * @param hdma pointer to a DMA_HandleTypeDef structure.
4126 * @retval None
4127 */
JPEG_DMAOutCpltCallback(DMA_HandleTypeDef * hdma)4128 static void JPEG_DMAOutCpltCallback(DMA_HandleTypeDef *hdma)
4129 {
4130 JPEG_HandleTypeDef *hjpeg = (JPEG_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
4131
4132 /* Disable The JPEG IT so the DMA Output Callback can not be interrupted by the JPEG EOC IT or JPEG HPD IT */
4133 __HAL_JPEG_DISABLE_IT(hjpeg, JPEG_INTERRUPT_MASK);
4134
4135 if ((hjpeg->Context & (JPEG_CONTEXT_METHOD_MASK | JPEG_CONTEXT_ENDING_DMA)) ==
4136 JPEG_CONTEXT_DMA) /* Check if context method is DMA and we are not in ending DMA stage */
4137 {
4138 if (__HAL_JPEG_GET_FLAG(hjpeg, JPEG_FLAG_EOCF) == 0UL)
4139 {
4140 JPEG_DISABLE_DMA(hjpeg, JPEG_DMA_ODMA);
4141 hjpeg->JpegOutCount = hjpeg->OutDataLength - JPEG_GET_DMA_REMAIN_DATA(hdma);
4142
4143 /*Output Buffer is full, call HAL_JPEG_DataReadyCallback*/
4144 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
4145 hjpeg->DataReadyCallback(hjpeg, hjpeg->pJpegOutBuffPtr, hjpeg->JpegOutCount);
4146 #else
4147 HAL_JPEG_DataReadyCallback(hjpeg, hjpeg->pJpegOutBuffPtr, hjpeg->JpegOutCount);
4148 #endif /* USE_HAL_JPEG_REGISTER_CALLBACKS */
4149
4150 if ((hjpeg->Context & JPEG_CONTEXT_PAUSE_OUTPUT) == 0UL)
4151 {
4152 /* Start DMA FIFO Out transfer */
4153 if (HAL_DMA_Start_IT(hjpeg->hdmaout, (uint32_t)&hjpeg->Instance->DOR, (uint32_t)hjpeg->pJpegOutBuffPtr,
4154 hjpeg->OutDataLength) != HAL_OK)
4155 {
4156 hjpeg->ErrorCode |= HAL_JPEG_ERROR_DMA;
4157 hjpeg->State = HAL_JPEG_STATE_ERROR;
4158 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
4159 hjpeg->ErrorCallback(hjpeg);
4160 #else
4161 HAL_JPEG_ErrorCallback(hjpeg);
4162 #endif /* USE_HAL_JPEG_REGISTER_CALLBACKS */
4163 return;
4164 }
4165 JPEG_ENABLE_DMA(hjpeg, JPEG_DMA_ODMA);
4166 }
4167 }
4168
4169 /* JPEG Conversion still on going : Enable the JPEG IT */
4170 __HAL_JPEG_ENABLE_IT(hjpeg, JPEG_IT_EOC | JPEG_IT_HPD);
4171 }
4172 }
4173
4174
4175 /**
4176 * @brief DMA Transfer error callback
4177 * @param hdma pointer to a DMA_HandleTypeDef structure.
4178 * @retval None
4179 */
JPEG_DMAErrorCallback(DMA_HandleTypeDef * hdma)4180 static void JPEG_DMAErrorCallback(DMA_HandleTypeDef *hdma)
4181 {
4182 JPEG_HandleTypeDef *hjpeg = (JPEG_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
4183
4184 if (HAL_DMA_GetError(hdma) != HAL_DMA_ERROR_NONE)
4185 {
4186 /*Stop Encoding/Decoding*/
4187 hjpeg->Instance->CONFR0 &= ~JPEG_CONFR0_START;
4188
4189 /* Disable All Interrupts */
4190 __HAL_JPEG_DISABLE_IT(hjpeg, JPEG_INTERRUPT_MASK);
4191
4192 /* Disable All DMA requests */
4193 JPEG_DISABLE_DMA(hjpeg, JPEG_DMA_MASK);
4194
4195 hjpeg->State = HAL_JPEG_STATE_READY;
4196 hjpeg->ErrorCode |= HAL_JPEG_ERROR_DMA;
4197 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
4198 hjpeg->ErrorCallback(hjpeg);
4199 #else
4200 HAL_JPEG_ErrorCallback(hjpeg);
4201 #endif /* USE_HAL_JPEG_REGISTER_CALLBACKS */
4202 }
4203 }
4204
4205 /**
4206 * @brief DMA output Abort callback
4207 * @param hdma pointer to a DMA_HandleTypeDef structure.
4208 * @retval None
4209 */
JPEG_DMAOutAbortCallback(DMA_HandleTypeDef * hdma)4210 static void JPEG_DMAOutAbortCallback(DMA_HandleTypeDef *hdma)
4211 {
4212 JPEG_HandleTypeDef *hjpeg = (JPEG_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
4213
4214 if ((hjpeg->Context & JPEG_CONTEXT_ENDING_DMA) != 0UL)
4215 {
4216 JPEG_DMA_EndProcess(hjpeg);
4217 }
4218 }
4219
4220
4221 /**
4222 * @brief Calculate the decoded image quality (from 1 to 100)
4223 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
4224 * the configuration information for JPEG module
4225 * @retval JPEG image quality from 1 to 100.
4226 */
JPEG_GetQuality(const JPEG_HandleTypeDef * hjpeg)4227 static uint32_t JPEG_GetQuality(const JPEG_HandleTypeDef *hjpeg)
4228 {
4229 uint32_t quality = 0;
4230 uint32_t quantRow;
4231 uint32_t quantVal;
4232 uint32_t scale;
4233 uint32_t i;
4234 uint32_t j;
4235 const __IO uint32_t *tableAddress = hjpeg->Instance->QMEM0;
4236
4237 i = 0;
4238 while (i < (JPEG_QUANT_TABLE_SIZE - 3UL))
4239 {
4240 quantRow = *tableAddress;
4241 for (j = 0; j < 4UL; j++)
4242 {
4243 quantVal = (quantRow >> (8UL * j)) & 0xFFUL;
4244 if (quantVal == 1UL)
4245 {
4246 /* if Quantization value = 1 then quality is 100%*/
4247 quality += 100UL;
4248 }
4249 else
4250 {
4251 /* Note that the quantization coefficients must be specified in the table in zigzag order */
4252 scale = (quantVal * 100UL) / ((uint32_t) hjpeg->QuantTable0[JPEG_ZIGZAG_ORDER[i + j]]);
4253
4254 if (scale <= 100UL)
4255 {
4256 quality += (200UL - scale) / 2UL;
4257 }
4258 else
4259 {
4260 quality += 5000UL / scale;
4261 }
4262 }
4263 }
4264
4265 i += 4UL;
4266 tableAddress ++;
4267 }
4268
4269 return (quality / 64UL);
4270 }
4271 /**
4272 * @}
4273 */
4274
4275 /**
4276 * @}
4277 */
4278 #endif /* JPEG */
4279 #endif /* HAL_JPEG_MODULE_ENABLED */
4280
4281
4282 /**
4283 * @}
4284 */
4285
4286