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