Set blackbox file handler to NULL after closing file
[inav.git] / lib / main / STM32H7 / Drivers / STM32H7xx_HAL_Driver / Src / stm32h7xx_hal_jpeg.c
blobd60bdd05eebb38b4e2d018bc6bb91ed09d3bd217
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
19 @verbatim
20 ==============================================================================
21 ##### How to use this driver #####
22 ==============================================================================
23 [..]
24 (#) Initialize the JPEG peripheral using HAL_JPEG_Init : No initialization parameters are required.
25 Only the call to HAL_JPEG_Init is necessary to initialize the JPEG peripheral.
27 (#) If operation is JPEG encoding use function HAL_JPEG_ConfigEncoding to set
28 the encoding parameters (mandatory before calling the encoding function).
29 the application can change the encoding parameter ImageQuality from
30 1 to 100 to obtain a more or less quality (visual quality vs the original row image),
31 and inversely more or less jpg file size.
33 (#) Note that for decoding operation the JPEG peripheral output data are organized in
34 YCbCr blocks called MCU (Minimum Coded Unit) as defioned in the JPEG specification
35 ISO/IEC 10918-1 standard.
36 It is up to the application to transform these YCbCr blocks to RGB data that can be display.
38 Respectively, for Encoding operation the JPEG peripheral input should be organized
39 in YCbCr MCU blocks. It is up to the application to perform the necessary RGB to YCbCr
40 MCU blocks transformation before feeding the JPEG peripheral with data.
42 (#) Use functions HAL_JPEG_Encode and HAL_JPEG_Decode to start respectively
43 a JPEG encoding/decoding operation in polling method (blocking).
45 (#) Use functions HAL_JPEG_Encode_IT and HAL_JPEG_Decode_IT to start respectively
46 a JPEG encoding/decoding operation with Interrupt method (not blocking).
48 (#) Use functions HAL_JPEG_Encode_DMA and HAL_JPEG_Decode_DMA to start respectively
49 a JPEG encoding/decoding operation with DMA method (not blocking).
51 (#) Callback HAL_JPEG_InfoReadyCallback is asserted if the current operation
52 is a JPEG decoding to provide the application with JPEG image parameters.
53 This callback is asserted when the JPEG peripheral successfully parse the
54 JPEG header.
56 (#) Callback HAL_JPEG_GetDataCallback is asserted for both encoding and decoding
57 operations to inform the application that the input buffer has been
58 consumed by the peripheral and to ask for a new data chunk if the operation
59 (encoding/decoding) has not been complete yet.
61 (++) This CallBack should be implemented in the application side. It should
62 call the function HAL_JPEG_ConfigInputBuffer if new input data are available,
63 or call HAL_JPEG_Pause with parameter XferSelection set to JPEG_PAUSE_RESUME_INPUT
64 to inform the JPEG HAL driver that the ongoing operation shall pause waiting for the
65 application to provide a new input data chunk.
66 Once the application succeed getting new data and if the input has been paused,
67 the application can call the function HAL_JPEG_ConfigInputBuffer to set the new
68 input buffer and size, then resume the JPEG HAL input by calling new function HAL_JPEG_Resume.
69 If the application has ended feeding the HAL JPEG with input data (no more input data), the application
70 Should call the function HAL_JPEG_ConfigInputBuffer (within the callback HAL_JPEG_GetDataCallback)
71 with the parameter InDataLength set to zero.
73 (++) The mechanism of HAL_JPEG_ConfigInputBuffer/HAL_JPEG_Pause/HAL_JPEG_Resume allows
74 to the application to provide the input data (for encoding or decoding) by chunks.
75 If the new input data chunk is not available (because data should be read from an input file
76 for example) the application can pause the JPEG input (using function HAL_JPEG_Pause)
77 Once the new input data chunk is available ( read from a file for example), the application
78 can call the function HAL_JPEG_ConfigInputBuffer to provide the HAL with the new chunk
79 then resume the JPEG HAL input by calling function HAL_JPEG_Resume.
81 (++) The application can call functions HAL_JPEG_ConfigInputBuffer then HAL_JPEG_Resume.
82 any time (outside the HAL_JPEG_GetDataCallback) Once the new input chunk data available.
83 However, to keep data coherency, the function HAL_JPEG_Pause must be imperatively called
84 (if necessary) within the callback HAL_JPEG_GetDataCallback, i.e when the HAL JPEG has ended
85 Transferring the previous chunk buffer to the JPEG peripheral.
87 (#) Callback HAL_JPEG_DataReadyCallback is asserted when the HAL JPEG driver
88 has filled the given output buffer with the given size.
90 (++) This CallBack should be implemented in the application side. It should
91 call the function HAL_JPEG_ConfigOutputBuffer to provide the HAL JPEG driver
92 with the new output buffer location and size to be used to store next data chunk.
93 if the application is not ready to provide the output chunk location then it can
94 call the function HAL_JPEG_Pause with parameter XferSelection set to JPEG_PAUSE_RESUME_OUTPUT
95 to inform the JPEG HAL driver that it shall pause output data. Once the application
96 is ready to receive the new data chunk (output buffer location free or available) it should call
97 the function HAL_JPEG_ConfigOutputBuffer to provide the HAL JPEG driver
98 with the new output chunk buffer location and size, then call HAL_JPEG_Resume
99 to inform the HAL that it shall resume outputting data in the given output buffer.
101 (++) The mechanism of HAL_JPEG_ConfigOutputBuffer/HAL_JPEG_Pause/HAL_JPEG_Resume allows
102 the application to receive data from the JPEG peripheral by chunks. when a chunk
103 is received, the application can pause the HAL JPEG output data to be able to process
104 these received data (YCbCr to RGB conversion in case of decoding or data storage in case
105 of encoding).
107 (++) The application can call functions HAL_JPEG_ ConfigOutputBuffer then HAL_JPEG_Resume.
108 any time (outside the HAL_JPEG_DataReadyCallback) Once the output data buffer is free to use.
109 However, to keep data coherency, the function HAL_JPEG_Pause must be imperatively called
110 (if necessary) within the callback HAL_JPEG_ DataReadyCallback, i.e when the HAL JPEG has ended
111 Transferring the previous chunk buffer from the JPEG peripheral to the application.
113 (#) Callback HAL_JPEG_EncodeCpltCallback is asserted when the HAL JPEG driver has
114 ended the current JPEG encoding operation, and all output data has been transmitted
115 to the application.
117 (#) Callback HAL_JPEG_DecodeCpltCallback is asserted when the HAL JPEG driver has
118 ended the current JPEG decoding operation. and all output data has been transmitted
119 to the application.
121 (#) Callback HAL_JPEG_ErrorCallback is asserted when an error occurred during
122 the current operation. the application can call the function HAL_JPEG_GetError()
123 to retrieve the error codes.
125 (#) By default the HAL JPEG driver uses the default quantization tables
126 as provide in the JPEG specification (ISO/IEC 10918-1 standard) for encoding.
127 User can change these default tables if necessary using the function HAL_JPEG_SetUserQuantTables
128 Note that for decoding the quantization tables are automatically extracted from
129 the JPEG header.
131 (#) To control JPEG state you can use the following function: HAL_JPEG_GetState()
133 *** JPEG HAL driver macros list ***
134 =============================================
135 [..]
136 Below the list of most used macros in JPEG HAL driver.
138 (+) __HAL_JPEG_RESET_HANDLE_STATE : Reset JPEG handle state.
139 (+) __HAL_JPEG_ENABLE : Enable the JPEG peripheral.
140 (+) __HAL_JPEG_DISABLE : Disable the JPEG peripheral.
141 (+) __HAL_JPEG_GET_FLAG : Check the specified JPEG status flag.
142 (+) __HAL_JPEG_CLEAR_FLAG : Clear the specified JPEG status flag.
143 (+) __HAL_JPEG_ENABLE_IT : Enable the specified JPEG Interrupt.
144 (+) __HAL_JPEG_DISABLE_IT : Disable the specified JPEG Interrupt.
145 (+) __HAL_JPEG_GET_IT_SOURCE : returns the state of the specified JPEG Interrupt (Enabled or disabled).
147 *** Callback registration ***
148 =============================================
150 The compilation define USE_HAL_JPEG_REGISTER_CALLBACKS when set to 1
151 allows the user to configure dynamically the driver callbacks.
152 Use Functions HAL_JPEG_RegisterCallback() or HAL_JPEG_RegisterXXXCallback()
153 to register an interrupt callback.
155 Function HAL_JPEG_RegisterCallback() allows to register following callbacks:
156 (+) EncodeCpltCallback : callback for end of encoding operation.
157 (+) DecodeCpltCallback : callback for end of decoding operation.
158 (+) ErrorCallback : callback for error detection.
159 (+) MspInitCallback : JPEG MspInit.
160 (+) MspDeInitCallback : JPEG MspDeInit.
161 This function takes as parameters the HAL peripheral handle, the Callback ID
162 and a pointer to the user callback function.
164 For specific callbacks InfoReadyCallback, GetDataCallback and DataReadyCallback use dedicated
165 register callbacks : respectively HAL_JPEG_RegisterInfoReadyCallback(),
166 HAL_JPEG_RegisterGetDataCallback() and HAL_JPEG_RegisterDataReadyCallback().
168 Use function HAL_JPEG_UnRegisterCallback() to reset a callback to the default
169 weak function.
170 HAL_JPEG_UnRegisterCallback() takes as parameters the HAL peripheral handle,
171 and the Callback ID.
172 This function allows to reset following callbacks:
173 (+) EncodeCpltCallback : callback for end of encoding operation.
174 (+) DecodeCpltCallback : callback for end of decoding operation.
175 (+) ErrorCallback : callback for error detection.
176 (+) MspInitCallback : JPEG MspInit.
177 (+) MspDeInitCallback : JPEG MspDeInit.
179 For callbacks InfoReadyCallback, GetDataCallback and DataReadyCallback use dedicated
180 unregister callbacks : respectively HAL_JPEG_UnRegisterInfoReadyCallback(),
181 HAL_JPEG_UnRegisterGetDataCallback() and HAL_JPEG_UnRegisterDataReadyCallback().
183 By default, after the HAL_JPEG_Init() and when the state is HAL_JPEG_STATE_RESET
184 all callbacks are set to the corresponding weak functions :
185 examples HAL_JPEG_DecodeCpltCallback() , HAL_JPEG_GetDataCallback().
186 Exception done for MspInit and MspDeInit functions that are
187 reset to the legacy weak function in the HAL_JPEG_Init()/ HAL_JPEG_DeInit() only when
188 these callbacks are null (not registered beforehand).
189 if not, MspInit or MspDeInit are not null, the HAL_JPEG_Init() / HAL_JPEG_DeInit()
190 keep and use the user MspInit/MspDeInit functions (registered beforehand)
192 Callbacks can be registered/unregistered in HAL_JPEG_STATE_READY state only.
193 Exception done MspInit/MspDeInit callbacks that can be registered/unregistered
194 in HAL_JPEG_STATE_READY or HAL_JPEG_STATE_RESET state,
195 thus registered (user) MspInit/DeInit callbacks can be used during the Init/DeInit.
196 In that case first register the MspInit/MspDeInit user callbacks
197 using HAL_JPEG_RegisterCallback() before calling HAL_JPEG_DeInit()
198 or HAL_JPEG_Init() function.
200 When The compilation define USE_HAL_JPEG_REGISTER_CALLBACKS is set to 0 or
201 not defined, the callback registration feature is not available and all callbacks
202 are set to the corresponding weak functions.
204 @endverbatim
205 ******************************************************************************
206 * @attention
208 * <h2><center>&copy; Copyright (c) 2017 STMicroelectronics.
209 * All rights reserved.</center></h2>
211 * This software component is licensed by ST under BSD 3-Clause license,
212 * the "License"; You may not use this file except in compliance with the
213 * License. You may obtain a copy of the License at:
214 * opensource.org/licenses/BSD-3-Clause
216 ******************************************************************************
219 /* Includes ------------------------------------------------------------------*/
220 #include "stm32h7xx_hal.h"
222 /** @addtogroup STM32H7xx_HAL_Driver
223 * @{
226 /** @defgroup JPEG JPEG
227 * @brief JPEG HAL module driver.
228 * @{
231 #ifdef HAL_JPEG_MODULE_ENABLED
233 #if defined (JPEG)
235 /* Private define ------------------------------------------------------------*/
236 /** @addtogroup JPEG_Private_Constants
237 * @{
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*/
243 #define JPEG_FIFO_SIZE ((uint32_t)16U) /* JPEG Input/Output HW FIFO size in words*/
245 #define JPEG_FIFO_TH_SIZE ((uint32_t)8U) /* JPEG Input/Output HW FIFO Threshold in words*/
247 #define JPEG_INTERRUPT_MASK ((uint32_t)0x0000007EU) /* JPEG Interrupt Mask*/
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 */
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 */
259 #define JPEG_CONTEXT_CONF_ENCODING ((uint32_t)0x00000100) /* JPEG context : encoding config done */
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 */
264 #define JPEG_CONTEXT_CUSTOM_TABLES ((uint32_t)0x00004000) /* JPEG context : Use custom quantization tables */
266 #define JPEG_CONTEXT_ENDING_DMA ((uint32_t)0x00008000) /* JPEG context : ending with DMA in progress */
268 #define JPEG_PROCESS_ONGOING ((uint32_t)0x00000000) /* Process is on going */
269 #define JPEG_PROCESS_DONE ((uint32_t)0x00000001) /* Process is done (ends) */
271 * @}
274 /* Private typedef -----------------------------------------------------------*/
275 /** @addtogroup JPEG_Private_Types
276 * @{
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
283 typedef struct
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 */
288 uint8_t HuffVal[162]; /*!< The symbols, in order of incremented code length, this parameter corresponds to HUFFVAL list in the Annex C */
291 } JPEG_ACHuffTableTypeDef;
293 typedef struct
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 */
298 uint8_t HuffVal[12]; /*!< The symbols, in order of incremented code length, this parameter corresponds to HUFFVAL list in the Annex C */
301 } JPEG_DCHuffTableTypeDef;
303 typedef struct
305 uint8_t CodeLength[JPEG_AC_HUFF_TABLE_SIZE]; /*!< Code length */
307 uint32_t HuffmanCode[JPEG_AC_HUFF_TABLE_SIZE]; /*!< HuffmanCode */
309 } JPEG_AC_HuffCodeTableTypeDef;
311 typedef struct
313 uint8_t CodeLength[JPEG_DC_HUFF_TABLE_SIZE]; /*!< Code length */
315 uint32_t HuffmanCode[JPEG_DC_HUFF_TABLE_SIZE]; /*!< HuffmanCode */
317 } JPEG_DC_HuffCodeTableTypeDef;
319 * @}
322 /* Private macro -------------------------------------------------------------*/
324 /* Private variables ---------------------------------------------------------*/
325 /** @addtogroup JPEG_Private_Variables
326 * @{
329 static const JPEG_DCHuffTableTypeDef JPEG_DCLUM_HuffTable =
331 { 0, 1, 5, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0 }, /*Bits*/
333 { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0xa, 0xb } /*HUFFVAL */
337 static const JPEG_DCHuffTableTypeDef JPEG_DCCHROM_HuffTable =
339 { 0, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0 }, /*Bits*/
341 { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0xa, 0xb } /*HUFFVAL */
344 static const JPEG_ACHuffTableTypeDef JPEG_ACLUM_HuffTable =
346 { 0, 2, 1, 3, 3, 2, 4, 3, 5, 5, 4, 4, 0, 0, 1, 0x7d }, /*Bits*/
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
373 static const JPEG_ACHuffTableTypeDef JPEG_ACCHROM_HuffTable =
375 { 0, 2, 1, 2, 4, 4, 3, 4, 7, 5, 4, 4, 0, 1, 2, 0x77 }, /*Bits*/
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
402 static const uint8_t JPEG_ZIGZAG_ORDER[JPEG_QUANT_TABLE_SIZE] =
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
414 * @}
417 /* Private function prototypes -----------------------------------------------*/
418 /** @addtogroup JPEG_Private_Functions_Prototypes
419 * @{
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);
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);
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);
455 * @}
458 /** @defgroup JPEG_Exported_Functions JPEG Exported Functions
459 * @{
462 /** @defgroup JPEG_Exported_Functions_Group1 Initialization and de-initialization functions
463 * @brief Initialization and de-initialization functions.
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
473 @endverbatim
474 * @{
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
484 HAL_StatusTypeDef HAL_JPEG_Init(JPEG_HandleTypeDef *hjpeg)
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] =
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
498 static const uint8_t JPEG_CHROM_QuantTable[JPEG_QUANT_TABLE_SIZE] =
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
510 /* Check the JPEG handle allocation */
511 if (hjpeg == NULL)
513 return HAL_ERROR;
516 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
517 if (hjpeg->State == HAL_JPEG_STATE_RESET)
519 /* Allocate lock resource and initialize it */
520 hjpeg->Lock = HAL_UNLOCKED;
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 */
529 if (hjpeg->MspInitCallback == NULL)
531 hjpeg->MspInitCallback = HAL_JPEG_MspInit; /* Legacy weak MspInit */
534 /* Init the low level hardware */
535 hjpeg->MspInitCallback(hjpeg);
537 #else
538 if (hjpeg->State == HAL_JPEG_STATE_RESET)
540 /* Allocate lock resource and initialize it */
541 hjpeg->Lock = HAL_UNLOCKED;
543 /* Init the low level hardware : GPIO, CLOCK */
544 HAL_JPEG_MspInit(hjpeg);
546 #endif /* USE_HAL_JPEG_REGISTER_CALLBACKS */
548 /* Change the JPEG state */
549 hjpeg->State = HAL_JPEG_STATE_BUSY;
551 /* Start the JPEG Core*/
552 __HAL_JPEG_ENABLE(hjpeg);
554 /* Stop the JPEG encoding/decoding process*/
555 hjpeg->Instance->CONFR0 &= ~JPEG_CONFR0_START;
557 /* Disable All Interrupts */
558 __HAL_JPEG_DISABLE_IT(hjpeg, JPEG_INTERRUPT_MASK);
561 /* Flush input and output FIFOs*/
562 hjpeg->Instance->CR |= JPEG_CR_IFF;
563 hjpeg->Instance->CR |= JPEG_CR_OFF;
565 /* Clear all flags */
566 __HAL_JPEG_CLEAR_FLAG(hjpeg, JPEG_FLAG_ALL);
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;
574 /* init the default Huffman tables*/
575 if (JPEG_Set_HuffEnc_Mem(hjpeg) != HAL_OK)
577 hjpeg->ErrorCode = HAL_JPEG_ERROR_HUFF_TABLE;
579 return HAL_ERROR;
582 /* Enable header processing*/
583 hjpeg->Instance->CONFR1 |= JPEG_CONFR1_HDR;
585 /* Reset JpegInCount and JpegOutCount */
586 hjpeg->JpegInCount = 0;
587 hjpeg->JpegOutCount = 0;
589 /* Change the JPEG state */
590 hjpeg->State = HAL_JPEG_STATE_READY;
592 /* Reset the JPEG ErrorCode */
593 hjpeg->ErrorCode = HAL_JPEG_ERROR_NONE;
595 /*Clear the context filelds*/
596 hjpeg->Context = 0;
598 /* Return function status */
599 return HAL_OK;
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
608 HAL_StatusTypeDef HAL_JPEG_DeInit(JPEG_HandleTypeDef *hjpeg)
610 /* Check the JPEG handle allocation */
611 if (hjpeg == NULL)
613 return HAL_ERROR;
616 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
617 if (hjpeg->MspDeInitCallback == NULL)
619 hjpeg->MspDeInitCallback = HAL_JPEG_MspDeInit; /* Legacy weak MspDeInit */
622 /* DeInit the low level hardware */
623 hjpeg->MspDeInitCallback(hjpeg);
625 #else
626 /* DeInit the low level hardware: CLOCK, NVIC.*/
627 HAL_JPEG_MspDeInit(hjpeg);
628 #endif /* USE_HAL_JPEG_REGISTER_CALLBACKS */
630 /* Change the JPEG state */
631 hjpeg->State = HAL_JPEG_STATE_BUSY;
633 /* Reset the JPEG ErrorCode */
634 hjpeg->ErrorCode = HAL_JPEG_ERROR_NONE;
636 /* Reset JpegInCount and JpegOutCount */
637 hjpeg->JpegInCount = 0;
638 hjpeg->JpegOutCount = 0;
640 /* Change the JPEG state */
641 hjpeg->State = HAL_JPEG_STATE_RESET;
643 /*Clear the context fields*/
644 hjpeg->Context = 0;
646 /* Release Lock */
647 __HAL_UNLOCK(hjpeg);
649 /* Return function status */
650 return HAL_OK;
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
659 __weak void HAL_JPEG_MspInit(JPEG_HandleTypeDef *hjpeg)
661 /* Prevent unused argument(s) compilation warning */
662 UNUSED(hjpeg);
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
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
675 __weak void HAL_JPEG_MspDeInit(JPEG_HandleTypeDef *hjpeg)
677 /* Prevent unused argument(s) compilation warning */
678 UNUSED(hjpeg);
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
685 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
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
700 HAL_StatusTypeDef HAL_JPEG_RegisterCallback(JPEG_HandleTypeDef *hjpeg, HAL_JPEG_CallbackIDTypeDef CallbackID,
701 pJPEG_CallbackTypeDef pCallback)
703 HAL_StatusTypeDef status = HAL_OK;
705 if (pCallback == NULL)
707 /* Update the error code */
708 hjpeg->ErrorCode |= HAL_JPEG_ERROR_INVALID_CALLBACK;
709 return HAL_ERROR;
711 /* Process locked */
712 __HAL_LOCK(hjpeg);
714 if (HAL_JPEG_STATE_READY == hjpeg->State)
716 switch (CallbackID)
718 case HAL_JPEG_ENCODE_CPLT_CB_ID :
719 hjpeg->EncodeCpltCallback = pCallback;
720 break;
722 case HAL_JPEG_DECODE_CPLT_CB_ID :
723 hjpeg->DecodeCpltCallback = pCallback;
724 break;
726 case HAL_JPEG_ERROR_CB_ID :
727 hjpeg->ErrorCallback = pCallback;
728 break;
730 case HAL_JPEG_MSPINIT_CB_ID :
731 hjpeg->MspInitCallback = pCallback;
732 break;
734 case HAL_JPEG_MSPDEINIT_CB_ID :
735 hjpeg->MspDeInitCallback = pCallback;
736 break;
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;
746 else if (HAL_JPEG_STATE_RESET == hjpeg->State)
748 switch (CallbackID)
750 case HAL_JPEG_MSPINIT_CB_ID :
751 hjpeg->MspInitCallback = pCallback;
752 break;
754 case HAL_JPEG_MSPDEINIT_CB_ID :
755 hjpeg->MspDeInitCallback = pCallback;
756 break;
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;
766 else
768 /* Update the error code */
769 hjpeg->ErrorCode |= HAL_JPEG_ERROR_INVALID_CALLBACK;
770 /* Return error status */
771 status = HAL_ERROR;
774 /* Release Lock */
775 __HAL_UNLOCK(hjpeg);
776 return status;
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
793 HAL_StatusTypeDef HAL_JPEG_UnRegisterCallback(JPEG_HandleTypeDef *hjpeg, HAL_JPEG_CallbackIDTypeDef CallbackID)
795 HAL_StatusTypeDef status = HAL_OK;
797 /* Process locked */
798 __HAL_LOCK(hjpeg);
800 if (HAL_JPEG_STATE_READY == hjpeg->State)
802 switch (CallbackID)
804 case HAL_JPEG_ENCODE_CPLT_CB_ID :
805 hjpeg->EncodeCpltCallback = HAL_JPEG_EncodeCpltCallback; /* Legacy weak EncodeCpltCallback */
806 break;
808 case HAL_JPEG_DECODE_CPLT_CB_ID :
809 hjpeg->DecodeCpltCallback = HAL_JPEG_DecodeCpltCallback; /* Legacy weak DecodeCpltCallback */
810 break;
812 case HAL_JPEG_ERROR_CB_ID :
813 hjpeg->ErrorCallback = HAL_JPEG_ErrorCallback; /* Legacy weak ErrorCallback */
814 break;
816 case HAL_JPEG_MSPINIT_CB_ID :
817 hjpeg->MspInitCallback = HAL_JPEG_MspInit; /* Legacy weak MspInit */
818 break;
820 case HAL_JPEG_MSPDEINIT_CB_ID :
821 hjpeg->MspDeInitCallback = HAL_JPEG_MspDeInit; /* Legacy weak MspDeInit */
822 break;
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;
832 else if (HAL_JPEG_STATE_RESET == hjpeg->State)
834 switch (CallbackID)
836 case HAL_JPEG_MSPINIT_CB_ID :
837 hjpeg->MspInitCallback = HAL_JPEG_MspInit; /* Legacy weak MspInit */
838 break;
840 case HAL_JPEG_MSPDEINIT_CB_ID :
841 hjpeg->MspDeInitCallback = HAL_JPEG_MspDeInit; /* Legacy weak MspInit */
842 break;
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;
852 else
854 /* Update the error code */
855 hjpeg->ErrorCode |= HAL_JPEG_ERROR_INVALID_CALLBACK;
856 /* Return error status */
857 status = HAL_ERROR;
860 /* Release Lock */
861 __HAL_UNLOCK(hjpeg);
862 return status;
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
872 HAL_StatusTypeDef HAL_JPEG_RegisterInfoReadyCallback(JPEG_HandleTypeDef *hjpeg,
873 pJPEG_InfoReadyCallbackTypeDef pCallback)
875 HAL_StatusTypeDef status = HAL_OK;
877 if (pCallback == NULL)
879 /* Update the error code */
880 hjpeg->ErrorCode |= HAL_JPEG_ERROR_INVALID_CALLBACK;
881 return HAL_ERROR;
883 /* Process locked */
884 __HAL_LOCK(hjpeg);
886 if (HAL_JPEG_STATE_READY == hjpeg->State)
888 hjpeg->InfoReadyCallback = pCallback;
890 else
892 /* Update the error code */
893 hjpeg->ErrorCode |= HAL_JPEG_ERROR_INVALID_CALLBACK;
894 /* Return error status */
895 status = HAL_ERROR;
898 /* Release Lock */
899 __HAL_UNLOCK(hjpeg);
900 return status;
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
909 HAL_StatusTypeDef HAL_JPEG_UnRegisterInfoReadyCallback(JPEG_HandleTypeDef *hjpeg)
911 HAL_StatusTypeDef status = HAL_OK;
913 /* Process locked */
914 __HAL_LOCK(hjpeg);
916 if (HAL_JPEG_STATE_READY == hjpeg->State)
918 hjpeg->InfoReadyCallback = HAL_JPEG_InfoReadyCallback; /* Legacy weak InfoReadyCallback */
920 else
922 /* Update the error code */
923 hjpeg->ErrorCode |= HAL_JPEG_ERROR_INVALID_CALLBACK;
924 /* Return error status */
925 status = HAL_ERROR;
928 /* Release Lock */
929 __HAL_UNLOCK(hjpeg);
930 return status;
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
940 HAL_StatusTypeDef HAL_JPEG_RegisterGetDataCallback(JPEG_HandleTypeDef *hjpeg, pJPEG_GetDataCallbackTypeDef pCallback)
942 HAL_StatusTypeDef status = HAL_OK;
944 if (pCallback == NULL)
946 /* Update the error code */
947 hjpeg->ErrorCode |= HAL_JPEG_ERROR_INVALID_CALLBACK;
948 return HAL_ERROR;
950 /* Process locked */
951 __HAL_LOCK(hjpeg);
953 if (HAL_JPEG_STATE_READY == hjpeg->State)
955 hjpeg->GetDataCallback = pCallback;
957 else
959 /* Update the error code */
960 hjpeg->ErrorCode |= HAL_JPEG_ERROR_INVALID_CALLBACK;
961 /* Return error status */
962 status = HAL_ERROR;
965 /* Release Lock */
966 __HAL_UNLOCK(hjpeg);
967 return status;
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
976 HAL_StatusTypeDef HAL_JPEG_UnRegisterGetDataCallback(JPEG_HandleTypeDef *hjpeg)
978 HAL_StatusTypeDef status = HAL_OK;
980 /* Process locked */
981 __HAL_LOCK(hjpeg);
983 if (HAL_JPEG_STATE_READY == hjpeg->State)
985 hjpeg->GetDataCallback = HAL_JPEG_GetDataCallback; /* Legacy weak GetDataCallback */
987 else
989 /* Update the error code */
990 hjpeg->ErrorCode |= HAL_JPEG_ERROR_INVALID_CALLBACK;
991 /* Return error status */
992 status = HAL_ERROR;
995 /* Release Lock */
996 __HAL_UNLOCK(hjpeg);
997 return status;
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
1007 HAL_StatusTypeDef HAL_JPEG_RegisterDataReadyCallback(JPEG_HandleTypeDef *hjpeg,
1008 pJPEG_DataReadyCallbackTypeDef pCallback)
1010 HAL_StatusTypeDef status = HAL_OK;
1012 if (pCallback == NULL)
1014 /* Update the error code */
1015 hjpeg->ErrorCode |= HAL_JPEG_ERROR_INVALID_CALLBACK;
1016 return HAL_ERROR;
1018 /* Process locked */
1019 __HAL_LOCK(hjpeg);
1021 if (HAL_JPEG_STATE_READY == hjpeg->State)
1023 hjpeg->DataReadyCallback = pCallback;
1025 else
1027 /* Update the error code */
1028 hjpeg->ErrorCode |= HAL_JPEG_ERROR_INVALID_CALLBACK;
1029 /* Return error status */
1030 status = HAL_ERROR;
1033 /* Release Lock */
1034 __HAL_UNLOCK(hjpeg);
1035 return status;
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
1044 HAL_StatusTypeDef HAL_JPEG_UnRegisterDataReadyCallback(JPEG_HandleTypeDef *hjpeg)
1046 HAL_StatusTypeDef status = HAL_OK;
1048 /* Process locked */
1049 __HAL_LOCK(hjpeg);
1051 if (HAL_JPEG_STATE_READY == hjpeg->State)
1053 hjpeg->DataReadyCallback = HAL_JPEG_DataReadyCallback; /* Legacy weak DataReadyCallback */
1055 else
1057 /* Update the error code */
1058 hjpeg->ErrorCode |= HAL_JPEG_ERROR_INVALID_CALLBACK;
1059 /* Return error status */
1060 status = HAL_ERROR;
1063 /* Release Lock */
1064 __HAL_UNLOCK(hjpeg);
1065 return status;
1068 #endif /* USE_HAL_JPEG_REGISTER_CALLBACKS */
1071 * @}
1074 /** @defgroup JPEG_Exported_Functions_Group2 Configuration functions
1075 * @brief JPEG Configuration functions.
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.
1088 @endverbatim
1089 * @{
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
1100 HAL_StatusTypeDef HAL_JPEG_ConfigEncoding(JPEG_HandleTypeDef *hjpeg, JPEG_ConfTypeDef *pConf)
1102 uint32_t error;
1103 uint32_t numberMCU, hfactor, vfactor, hMCU, vMCU;
1105 /* Check the JPEG handle allocation */
1106 if ((hjpeg == NULL) || (pConf == NULL))
1108 return HAL_ERROR;
1110 else
1112 /* Check the parameters */
1113 assert_param(IS_JPEG_COLORSPACE(pConf->ColorSpace));
1114 assert_param(IS_JPEG_CHROMASUBSAMPLING(pConf->ChromaSubsampling));
1115 assert_param(IS_JPEG_IMAGE_QUALITY(pConf->ImageQuality));
1117 /* Process Locked */
1118 __HAL_LOCK(hjpeg);
1120 if (hjpeg->State == HAL_JPEG_STATE_READY)
1122 hjpeg->State = HAL_JPEG_STATE_BUSY;
1124 hjpeg->Conf.ColorSpace = pConf->ColorSpace;
1125 hjpeg->Conf.ChromaSubsampling = pConf->ChromaSubsampling;
1126 hjpeg->Conf.ImageHeight = pConf->ImageHeight;
1127 hjpeg->Conf.ImageWidth = pConf->ImageWidth;
1128 hjpeg->Conf.ImageQuality = pConf->ImageQuality;
1130 /* Reset the Color Space : by default only one quantization table is used*/
1131 hjpeg->Instance->CONFR1 &= ~JPEG_CONFR1_COLORSPACE;
1133 /* Set Number of color components*/
1134 if (hjpeg->Conf.ColorSpace == JPEG_GRAYSCALE_COLORSPACE)
1136 /*Gray Scale is only one component 8x8 blocks i.e 4:4:4*/
1137 hjpeg->Conf.ChromaSubsampling = JPEG_444_SUBSAMPLING;
1139 JPEG_SetColorGrayScale(hjpeg);
1140 /* Set quantization table 0*/
1141 error = JPEG_Set_Quantization_Mem(hjpeg, hjpeg->QuantTable0, (hjpeg->Instance->QMEM0));
1143 else if (hjpeg->Conf.ColorSpace == JPEG_YCBCR_COLORSPACE)
1146 Set the Color Space for YCbCr : 2 quantization tables are used
1147 one for Luminance(Y) and one for both Chrominances (Cb & Cr)
1149 hjpeg->Instance->CONFR1 |= JPEG_CONFR1_COLORSPACE_0;
1151 JPEG_SetColorYCBCR(hjpeg);
1153 /* Set quantization table 0*/
1154 error = JPEG_Set_Quantization_Mem(hjpeg, hjpeg->QuantTable0, (hjpeg->Instance->QMEM0));
1155 /*By default quantization table 0 for component 0 and quantization table 1 for both components 1 and 2*/
1156 error |= JPEG_Set_Quantization_Mem(hjpeg, hjpeg->QuantTable1, (hjpeg->Instance->QMEM1));
1158 if ((hjpeg->Context & JPEG_CONTEXT_CUSTOM_TABLES) != 0UL)
1160 /*Use user customized quantization tables , 1 table per component*/
1161 /* use 3 quantization tables , one for each component*/
1162 hjpeg->Instance->CONFR1 &= (~JPEG_CONFR1_COLORSPACE);
1163 hjpeg->Instance->CONFR1 |= JPEG_CONFR1_COLORSPACE_1;
1165 error |= JPEG_Set_Quantization_Mem(hjpeg, hjpeg->QuantTable2, (hjpeg->Instance->QMEM2));
1167 /*Use Quantization 1 table for component 1*/
1168 hjpeg->Instance->CONFR5 &= (~JPEG_CONFR5_QT);
1169 hjpeg->Instance->CONFR5 |= JPEG_CONFR5_QT_0;
1171 /*Use Quantization 2 table for component 2*/
1172 hjpeg->Instance->CONFR6 &= (~JPEG_CONFR6_QT);
1173 hjpeg->Instance->CONFR6 |= JPEG_CONFR6_QT_1;
1176 else /* ColorSpace == JPEG_CMYK_COLORSPACE */
1178 JPEG_SetColorCMYK(hjpeg);
1180 /* Set quantization table 0*/
1181 error = JPEG_Set_Quantization_Mem(hjpeg, hjpeg->QuantTable0, (hjpeg->Instance->QMEM0));
1182 /*By default quantization table 0 for All components*/
1184 if ((hjpeg->Context & JPEG_CONTEXT_CUSTOM_TABLES) != 0UL)
1186 /*Use user customized quantization tables , 1 table per component*/
1187 /* use 4 quantization tables , one for each component*/
1188 hjpeg->Instance->CONFR1 |= JPEG_CONFR1_COLORSPACE;
1190 error |= JPEG_Set_Quantization_Mem(hjpeg, hjpeg->QuantTable1, (hjpeg->Instance->QMEM1));
1191 error |= JPEG_Set_Quantization_Mem(hjpeg, hjpeg->QuantTable2, (hjpeg->Instance->QMEM2));
1192 error |= JPEG_Set_Quantization_Mem(hjpeg, hjpeg->QuantTable3, (hjpeg->Instance->QMEM3));
1194 /*Use Quantization 1 table for component 1*/
1195 hjpeg->Instance->CONFR5 |= JPEG_CONFR5_QT_0;
1197 /*Use Quantization 2 table for component 2*/
1198 hjpeg->Instance->CONFR6 |= JPEG_CONFR6_QT_1;
1200 /*Use Quantization 3 table for component 3*/
1201 hjpeg->Instance->CONFR7 |= JPEG_CONFR7_QT;
1205 if (error != 0UL)
1207 hjpeg->ErrorCode = HAL_JPEG_ERROR_QUANT_TABLE;
1209 /* Process Unlocked */
1210 __HAL_UNLOCK(hjpeg);
1212 /* Set the JPEG State to ready */
1213 hjpeg->State = HAL_JPEG_STATE_READY;
1215 return HAL_ERROR;
1217 /* Set the image size*/
1218 /* set the number of lines*/
1219 MODIFY_REG(hjpeg->Instance->CONFR1, JPEG_CONFR1_YSIZE, ((hjpeg->Conf.ImageHeight & 0x0000FFFFUL) << 16));
1220 /* set the number of pixels per line*/
1221 MODIFY_REG(hjpeg->Instance->CONFR3, JPEG_CONFR3_XSIZE, ((hjpeg->Conf.ImageWidth & 0x0000FFFFUL) << 16));
1224 if (hjpeg->Conf.ChromaSubsampling == JPEG_420_SUBSAMPLING) /* 4:2:0*/
1226 hfactor = 16;
1227 vfactor = 16;
1229 else if (hjpeg->Conf.ChromaSubsampling == JPEG_422_SUBSAMPLING) /* 4:2:2*/
1231 hfactor = 16;
1232 vfactor = 8;
1234 else /* Default is 8x8 MCU, 4:4:4*/
1236 hfactor = 8;
1237 vfactor = 8;
1240 hMCU = (hjpeg->Conf.ImageWidth / hfactor);
1241 if ((hjpeg->Conf.ImageWidth % hfactor) != 0UL)
1243 hMCU++; /*+1 for horizontal incomplete MCU */
1246 vMCU = (hjpeg->Conf.ImageHeight / vfactor);
1247 if ((hjpeg->Conf.ImageHeight % vfactor) != 0UL)
1249 vMCU++; /*+1 for vertical incomplete MCU */
1252 numberMCU = (hMCU * vMCU) - 1UL; /* Bit Field JPEG_CONFR2_NMCU shall be set to NB_MCU - 1*/
1253 /* Set the number of MCU*/
1254 hjpeg->Instance->CONFR2 = (numberMCU & JPEG_CONFR2_NMCU);
1256 hjpeg->Context |= JPEG_CONTEXT_CONF_ENCODING;
1258 /* Process Unlocked */
1259 __HAL_UNLOCK(hjpeg);
1261 /* Set the JPEG State to ready */
1262 hjpeg->State = HAL_JPEG_STATE_READY;
1264 /* Return function status */
1265 return HAL_OK;
1267 else
1269 /* Process Unlocked */
1270 __HAL_UNLOCK(hjpeg);
1272 /* Return function status */
1273 return HAL_BUSY;
1279 * @brief Extract the image configuration from the JPEG header during the decoding
1280 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
1281 * the configuration information for JPEG module
1282 * @param pInfo pointer to a JPEG_ConfTypeDef structure that contains
1283 * The JPEG decoded header informations
1284 * @retval HAL status
1286 HAL_StatusTypeDef HAL_JPEG_GetInfo(JPEG_HandleTypeDef *hjpeg, JPEG_ConfTypeDef *pInfo)
1288 uint32_t yblockNb, cBblockNb, cRblockNb;
1290 /* Check the JPEG handle allocation */
1291 if ((hjpeg == NULL) || (pInfo == NULL))
1293 return HAL_ERROR;
1296 /*Read the conf parameters */
1297 if ((hjpeg->Instance->CONFR1 & JPEG_CONFR1_NF) == JPEG_CONFR1_NF_1)
1299 pInfo->ColorSpace = JPEG_YCBCR_COLORSPACE;
1301 else if ((hjpeg->Instance->CONFR1 & JPEG_CONFR1_NF) == 0UL)
1303 pInfo->ColorSpace = JPEG_GRAYSCALE_COLORSPACE;
1305 else if ((hjpeg->Instance->CONFR1 & JPEG_CONFR1_NF) == JPEG_CONFR1_NF)
1307 pInfo->ColorSpace = JPEG_CMYK_COLORSPACE;
1309 else
1311 return HAL_ERROR;
1314 pInfo->ImageHeight = (hjpeg->Instance->CONFR1 & 0xFFFF0000UL) >> 16;
1315 pInfo->ImageWidth = (hjpeg->Instance->CONFR3 & 0xFFFF0000UL) >> 16;
1317 if ((pInfo->ColorSpace == JPEG_YCBCR_COLORSPACE) || (pInfo->ColorSpace == JPEG_CMYK_COLORSPACE))
1319 yblockNb = (hjpeg->Instance->CONFR4 & JPEG_CONFR4_NB) >> 4;
1320 cBblockNb = (hjpeg->Instance->CONFR5 & JPEG_CONFR5_NB) >> 4;
1321 cRblockNb = (hjpeg->Instance->CONFR6 & JPEG_CONFR6_NB) >> 4;
1323 if ((yblockNb == 1UL) && (cBblockNb == 0UL) && (cRblockNb == 0UL))
1325 pInfo->ChromaSubsampling = JPEG_422_SUBSAMPLING; /*16x8 block*/
1327 else if ((yblockNb == 0UL) && (cBblockNb == 0UL) && (cRblockNb == 0UL))
1329 pInfo->ChromaSubsampling = JPEG_444_SUBSAMPLING;
1331 else if ((yblockNb == 3UL) && (cBblockNb == 0UL) && (cRblockNb == 0UL))
1333 pInfo->ChromaSubsampling = JPEG_420_SUBSAMPLING;
1335 else /*Default is 4:4:4*/
1337 pInfo->ChromaSubsampling = JPEG_444_SUBSAMPLING;
1340 else
1342 pInfo->ChromaSubsampling = JPEG_444_SUBSAMPLING;
1345 pInfo->ImageQuality = JPEG_GetQuality(hjpeg);
1347 /* Return function status */
1348 return HAL_OK;
1352 * @brief Enable JPEG Header parsing for decoding
1353 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
1354 * the configuration information for the JPEG.
1355 * @retval HAL status
1357 HAL_StatusTypeDef HAL_JPEG_EnableHeaderParsing(JPEG_HandleTypeDef *hjpeg)
1359 /* Process locked */
1360 __HAL_LOCK(hjpeg);
1362 if (hjpeg->State == HAL_JPEG_STATE_READY)
1364 /* Change the JPEG state */
1365 hjpeg->State = HAL_JPEG_STATE_BUSY;
1367 /* Enable header processing*/
1368 hjpeg->Instance->CONFR1 |= JPEG_CONFR1_HDR;
1370 /* Process unlocked */
1371 __HAL_UNLOCK(hjpeg);
1373 /* Change the JPEG state */
1374 hjpeg->State = HAL_JPEG_STATE_READY;
1376 return HAL_OK;
1378 else
1380 /* Process unlocked */
1381 __HAL_UNLOCK(hjpeg);
1383 return HAL_BUSY;
1388 * @brief Disable JPEG Header parsing for decoding
1389 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
1390 * the configuration information for the JPEG.
1391 * @retval HAL status
1393 HAL_StatusTypeDef HAL_JPEG_DisableHeaderParsing(JPEG_HandleTypeDef *hjpeg)
1395 /* Process locked */
1396 __HAL_LOCK(hjpeg);
1398 if (hjpeg->State == HAL_JPEG_STATE_READY)
1400 /* Change the JPEG state */
1401 hjpeg->State = HAL_JPEG_STATE_BUSY;
1403 /* Disable header processing*/
1404 hjpeg->Instance->CONFR1 &= ~JPEG_CONFR1_HDR;
1406 /* Process unlocked */
1407 __HAL_UNLOCK(hjpeg);
1409 /* Change the JPEG state */
1410 hjpeg->State = HAL_JPEG_STATE_READY;
1412 return HAL_OK;
1414 else
1416 /* Process unlocked */
1417 __HAL_UNLOCK(hjpeg);
1419 return HAL_BUSY;
1424 * @brief Modify the default Quantization tables used for JPEG encoding.
1425 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
1426 * the configuration information for JPEG module
1427 * @param QTable0 pointer to uint8_t , define the user quantification table for color component 1.
1428 * If NULL assume no need to update the table and no error return
1429 * @param QTable1 pointer to uint8_t , define the user quantification table for color component 2.
1430 * If NULL assume no need to update the table and no error return.
1431 * @param QTable2 pointer to uint8_t , define the user quantification table for color component 3,
1432 * If NULL assume no need to update the table and no error return.
1433 * @param QTable3 pointer to uint8_t , define the user quantification table for color component 4.
1434 * If NULL assume no need to update the table and no error return.
1436 * @retval HAL status
1440 HAL_StatusTypeDef HAL_JPEG_SetUserQuantTables(JPEG_HandleTypeDef *hjpeg, uint8_t *QTable0, uint8_t *QTable1,
1441 uint8_t *QTable2, uint8_t *QTable3)
1443 /* Process Locked */
1444 __HAL_LOCK(hjpeg);
1446 if (hjpeg->State == HAL_JPEG_STATE_READY)
1448 /* Change the DMA state */
1449 hjpeg->State = HAL_JPEG_STATE_BUSY;
1451 hjpeg->Context |= JPEG_CONTEXT_CUSTOM_TABLES;
1453 hjpeg->QuantTable0 = QTable0;
1454 hjpeg->QuantTable1 = QTable1;
1455 hjpeg->QuantTable2 = QTable2;
1456 hjpeg->QuantTable3 = QTable3;
1458 /* Process Unlocked */
1459 __HAL_UNLOCK(hjpeg);
1461 /* Change the DMA state */
1462 hjpeg->State = HAL_JPEG_STATE_READY;
1464 /* Return function status */
1465 return HAL_OK;
1467 else
1469 /* Process Unlocked */
1470 __HAL_UNLOCK(hjpeg);
1472 return HAL_BUSY;
1477 * @}
1480 /** @defgroup JPEG_Exported_Functions_Group3 encoding/decoding processing functions
1481 * @brief processing functions.
1483 @verbatim
1484 ==============================================================================
1485 ##### JPEG processing functions #####
1486 ==============================================================================
1487 [..] This section provides functions allowing to:
1488 (+) HAL_JPEG_Encode() : JPEG encoding with polling process
1489 (+) HAL_JPEG_Decode() : JPEG decoding with polling process
1490 (+) HAL_JPEG_Encode_IT() : JPEG encoding with interrupt process
1491 (+) HAL_JPEG_Decode_IT() : JPEG decoding with interrupt process
1492 (+) HAL_JPEG_Encode_DMA() : JPEG encoding with DMA process
1493 (+) HAL_JPEG_Decode_DMA() : JPEG decoding with DMA process
1494 (+) HAL_JPEG_Pause() : Pause the Input/Output processing
1495 (+) HAL_JPEG_Resume() : Resume the JPEG Input/Output processing
1496 (+) HAL_JPEG_ConfigInputBuffer() : Config Encoding/Decoding Input Buffer
1497 (+) HAL_JPEG_ConfigOutputBuffer() : Config Encoding/Decoding Output Buffer
1498 (+) HAL_JPEG_Abort() : Aborts the JPEG Encoding/Decoding
1500 @endverbatim
1501 * @{
1505 * @brief Starts JPEG encoding with polling processing
1506 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
1507 * the configuration information for JPEG module
1508 * @param pDataInMCU Pointer to the Input buffer
1509 * @param InDataLength size in bytes Input buffer
1510 * @param pDataOut Pointer to the jpeg output data buffer
1511 * @param OutDataLength size in bytes of the Output buffer
1512 * @param Timeout Specify Timeout value
1513 * @retval HAL status
1515 HAL_StatusTypeDef HAL_JPEG_Encode(JPEG_HandleTypeDef *hjpeg, uint8_t *pDataInMCU, uint32_t InDataLength,
1516 uint8_t *pDataOut, uint32_t OutDataLength, uint32_t Timeout)
1518 uint32_t tickstart;
1520 /* Check the parameters */
1521 assert_param((InDataLength >= 4UL));
1522 assert_param((OutDataLength >= 4UL));
1524 /* Check In/out buffer allocation and size */
1525 if ((hjpeg == NULL) || (pDataInMCU == NULL) || (pDataOut == NULL))
1527 return HAL_ERROR;
1529 /* Process locked */
1530 __HAL_LOCK(hjpeg);
1532 if (hjpeg->State != HAL_JPEG_STATE_READY)
1534 /* Process Unlocked */
1535 __HAL_UNLOCK(hjpeg);
1537 return HAL_BUSY;
1540 if (hjpeg->State == HAL_JPEG_STATE_READY)
1542 if ((hjpeg->Context & JPEG_CONTEXT_CONF_ENCODING) == JPEG_CONTEXT_CONF_ENCODING)
1544 /*Change JPEG state*/
1545 hjpeg->State = HAL_JPEG_STATE_BUSY_ENCODING;
1547 /*Set the Context to Encode with Polling*/
1548 hjpeg->Context &= ~(JPEG_CONTEXT_OPERATION_MASK | JPEG_CONTEXT_METHOD_MASK);
1549 hjpeg->Context |= (JPEG_CONTEXT_ENCODE | JPEG_CONTEXT_POLLING);
1551 /* Get tick */
1552 tickstart = HAL_GetTick();
1554 /*Store In/out buffers pointers and size*/
1555 hjpeg->pJpegInBuffPtr = pDataInMCU;
1556 hjpeg->pJpegOutBuffPtr = pDataOut;
1557 hjpeg->InDataLength = InDataLength - (InDataLength % 4UL); /* In Data length must be multiple of 4 Bytes (1 word)*/
1558 hjpeg->OutDataLength = OutDataLength - (OutDataLength % 4UL); /* Out Data length must be multiple of 4 Bytes (1 word)*/
1560 /*Reset In/out data counter */
1561 hjpeg->JpegInCount = 0;
1562 hjpeg->JpegOutCount = 0;
1564 /*Init decoding process*/
1565 JPEG_Init_Process(hjpeg);
1567 /*JPEG data processing : In/Out FIFO transfer*/
1568 while ((JPEG_Process(hjpeg) == JPEG_PROCESS_ONGOING))
1570 if (Timeout != HAL_MAX_DELAY)
1572 if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0UL))
1575 /* Update error code */
1576 hjpeg->ErrorCode |= HAL_JPEG_ERROR_TIMEOUT;
1578 /* Process Unlocked */
1579 __HAL_UNLOCK(hjpeg);
1581 /*Change JPEG state*/
1582 hjpeg->State = HAL_JPEG_STATE_READY;
1584 return HAL_TIMEOUT;
1589 /* Process Unlocked */
1590 __HAL_UNLOCK(hjpeg);
1592 /*Change JPEG state*/
1593 hjpeg->State = HAL_JPEG_STATE_READY;
1596 else
1598 /* Process Unlocked */
1599 __HAL_UNLOCK(hjpeg);
1601 return HAL_ERROR;
1604 /* Return function status */
1605 return HAL_OK;
1609 * @brief Starts JPEG decoding with polling processing
1610 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
1611 * the configuration information for JPEG module
1612 * @param pDataIn Pointer to the input data buffer
1613 * @param InDataLength size in bytes Input buffer
1614 * @param pDataOutMCU Pointer to the Output data buffer
1615 * @param OutDataLength size in bytes of the Output buffer
1616 * @param Timeout Specify Timeout value
1617 * @retval HAL status
1619 HAL_StatusTypeDef HAL_JPEG_Decode(JPEG_HandleTypeDef *hjpeg, uint8_t *pDataIn, uint32_t InDataLength,
1620 uint8_t *pDataOutMCU, uint32_t OutDataLength, uint32_t Timeout)
1622 uint32_t tickstart;
1624 /* Check the parameters */
1625 assert_param((InDataLength >= 4UL));
1626 assert_param((OutDataLength >= 4UL));
1628 /* Check In/out buffer allocation and size */
1629 if ((hjpeg == NULL) || (pDataIn == NULL) || (pDataOutMCU == NULL))
1631 return HAL_ERROR;
1634 /* Process Locked */
1635 __HAL_LOCK(hjpeg);
1637 /* Get tick */
1638 tickstart = HAL_GetTick();
1640 if (hjpeg->State == HAL_JPEG_STATE_READY)
1642 /*Change JPEG state*/
1643 hjpeg->State = HAL_JPEG_STATE_BUSY_DECODING;
1645 /*Set the Context to Decode with Polling*/
1646 /*Set the Context to Encode with Polling*/
1647 hjpeg->Context &= ~(JPEG_CONTEXT_OPERATION_MASK | JPEG_CONTEXT_METHOD_MASK);
1648 hjpeg->Context |= (JPEG_CONTEXT_DECODE | JPEG_CONTEXT_POLLING);
1650 /*Store In/out buffers pointers and size*/
1651 hjpeg->pJpegInBuffPtr = pDataIn;
1652 hjpeg->pJpegOutBuffPtr = pDataOutMCU;
1653 hjpeg->InDataLength = InDataLength - (InDataLength % 4UL); /*In Data length must be multiple of 4 Bytes (1 word)*/
1654 hjpeg->OutDataLength = OutDataLength - (OutDataLength % 4UL); /*Out Data length must be multiple of 4 Bytes (1 word)*/
1656 /*Reset In/out data counter */
1657 hjpeg->JpegInCount = 0;
1658 hjpeg->JpegOutCount = 0;
1660 /*Init decoding process*/
1661 JPEG_Init_Process(hjpeg);
1663 /*JPEG data processing : In/Out FIFO transfer*/
1664 while ((JPEG_Process(hjpeg) == JPEG_PROCESS_ONGOING))
1666 if (Timeout != HAL_MAX_DELAY)
1668 if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0UL))
1671 /* Update error code */
1672 hjpeg->ErrorCode |= HAL_JPEG_ERROR_TIMEOUT;
1674 /* Process Unlocked */
1675 __HAL_UNLOCK(hjpeg);
1677 /*Change JPEG state*/
1678 hjpeg->State = HAL_JPEG_STATE_READY;
1680 return HAL_TIMEOUT;
1685 /* Process Unlocked */
1686 __HAL_UNLOCK(hjpeg);
1688 /*Change JPEG state*/
1689 hjpeg->State = HAL_JPEG_STATE_READY;
1692 else
1694 /* Process Unlocked */
1695 __HAL_UNLOCK(hjpeg);
1697 return HAL_BUSY;
1699 /* Return function status */
1700 return HAL_OK;
1704 * @brief Starts JPEG encoding with interrupt processing
1705 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
1706 * the configuration information for JPEG module
1707 * @param pDataInMCU Pointer to the Input buffer
1708 * @param InDataLength size in bytes Input buffer
1709 * @param pDataOut Pointer to the jpeg output data buffer
1710 * @param OutDataLength size in bytes of the Output buffer
1711 * @retval HAL status
1713 HAL_StatusTypeDef HAL_JPEG_Encode_IT(JPEG_HandleTypeDef *hjpeg, uint8_t *pDataInMCU, uint32_t InDataLength,
1714 uint8_t *pDataOut, uint32_t OutDataLength)
1716 /* Check the parameters */
1717 assert_param((InDataLength >= 4UL));
1718 assert_param((OutDataLength >= 4UL));
1720 /* Check In/out buffer allocation and size */
1721 if ((hjpeg == NULL) || (pDataInMCU == NULL) || (pDataOut == NULL))
1723 return HAL_ERROR;
1726 /* Process Locked */
1727 __HAL_LOCK(hjpeg);
1729 if (hjpeg->State != HAL_JPEG_STATE_READY)
1731 /* Process Unlocked */
1732 __HAL_UNLOCK(hjpeg);
1734 return HAL_BUSY;
1736 else
1738 if ((hjpeg->Context & JPEG_CONTEXT_CONF_ENCODING) == JPEG_CONTEXT_CONF_ENCODING)
1740 /*Change JPEG state*/
1741 hjpeg->State = HAL_JPEG_STATE_BUSY_ENCODING;
1743 /*Set the Context to Encode with IT*/
1744 hjpeg->Context &= ~(JPEG_CONTEXT_OPERATION_MASK | JPEG_CONTEXT_METHOD_MASK);
1745 hjpeg->Context |= (JPEG_CONTEXT_ENCODE | JPEG_CONTEXT_IT);
1747 /*Store In/out buffers pointers and size*/
1748 hjpeg->pJpegInBuffPtr = pDataInMCU;
1749 hjpeg->pJpegOutBuffPtr = pDataOut;
1750 hjpeg->InDataLength = InDataLength - (InDataLength % 4UL); /*In Data length must be multiple of 4 Bytes (1 word)*/
1751 hjpeg->OutDataLength = OutDataLength - (OutDataLength % 4UL); /*Out Data length must be multiple of 4 Bytes (1 word)*/
1753 /*Reset In/out data counter */
1754 hjpeg->JpegInCount = 0;
1755 hjpeg->JpegOutCount = 0;
1757 /*Init decoding process*/
1758 JPEG_Init_Process(hjpeg);
1761 else
1763 /* Process Unlocked */
1764 __HAL_UNLOCK(hjpeg);
1766 return HAL_ERROR;
1769 /* Return function status */
1770 return HAL_OK;
1774 * @brief Starts JPEG decoding with interrupt processing
1775 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
1776 * the configuration information for JPEG module
1777 * @param pDataIn Pointer to the input data buffer
1778 * @param InDataLength size in bytes Input buffer
1779 * @param pDataOutMCU Pointer to the Output data buffer
1780 * @param OutDataLength size in bytes of the Output buffer
1781 * @retval HAL status
1783 HAL_StatusTypeDef HAL_JPEG_Decode_IT(JPEG_HandleTypeDef *hjpeg, uint8_t *pDataIn, uint32_t InDataLength,
1784 uint8_t *pDataOutMCU, uint32_t OutDataLength)
1786 /* Check the parameters */
1787 assert_param((InDataLength >= 4UL));
1788 assert_param((OutDataLength >= 4UL));
1790 /* Check In/out buffer allocation and size */
1791 if ((hjpeg == NULL) || (pDataIn == NULL) || (pDataOutMCU == NULL))
1793 return HAL_ERROR;
1796 /* Process Locked */
1797 __HAL_LOCK(hjpeg);
1799 if (hjpeg->State == HAL_JPEG_STATE_READY)
1801 /*Change JPEG state*/
1802 hjpeg->State = HAL_JPEG_STATE_BUSY_DECODING;
1804 /*Set the Context to Decode with IT*/
1805 hjpeg->Context &= ~(JPEG_CONTEXT_OPERATION_MASK | JPEG_CONTEXT_METHOD_MASK);
1806 hjpeg->Context |= (JPEG_CONTEXT_DECODE | JPEG_CONTEXT_IT);
1808 /*Store In/out buffers pointers and size*/
1809 hjpeg->pJpegInBuffPtr = pDataIn;
1810 hjpeg->pJpegOutBuffPtr = pDataOutMCU;
1811 hjpeg->InDataLength = InDataLength - (InDataLength % 4UL); /*In Data length must be multiple of 4 Bytes (1 word)*/
1812 hjpeg->OutDataLength = OutDataLength - (OutDataLength % 4UL); /*Out Data length must be multiple of 4 Bytes (1 word)*/
1814 /*Reset In/out data counter */
1815 hjpeg->JpegInCount = 0;
1816 hjpeg->JpegOutCount = 0;
1818 /*Init decoding process*/
1819 JPEG_Init_Process(hjpeg);
1822 else
1824 /* Process Unlocked */
1825 __HAL_UNLOCK(hjpeg);
1827 return HAL_BUSY;
1829 /* Return function status */
1830 return HAL_OK;
1834 * @brief Starts JPEG encoding with DMA processing
1835 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
1836 * the configuration information for JPEG module
1837 * @param pDataInMCU Pointer to the Input buffer
1838 * @param InDataLength size in bytes Input buffer
1839 * @param pDataOut Pointer to the jpeg output data buffer
1840 * @param OutDataLength size in bytes of the Output buffer
1841 * @retval HAL status
1843 HAL_StatusTypeDef HAL_JPEG_Encode_DMA(JPEG_HandleTypeDef *hjpeg, uint8_t *pDataInMCU, uint32_t InDataLength,
1844 uint8_t *pDataOut, uint32_t OutDataLength)
1846 /* Check the parameters */
1847 assert_param((InDataLength >= 4UL));
1848 assert_param((OutDataLength >= 4UL));
1850 /* Check In/out buffer allocation and size */
1851 if ((hjpeg == NULL) || (pDataInMCU == NULL) || (pDataOut == NULL))
1853 return HAL_ERROR;
1856 /* Process Locked */
1857 __HAL_LOCK(hjpeg);
1859 if (hjpeg->State != HAL_JPEG_STATE_READY)
1861 /* Process Unlocked */
1862 __HAL_UNLOCK(hjpeg);
1864 return HAL_BUSY;
1866 else
1868 if ((hjpeg->Context & JPEG_CONTEXT_CONF_ENCODING) == JPEG_CONTEXT_CONF_ENCODING)
1870 /*Change JPEG state*/
1871 hjpeg->State = HAL_JPEG_STATE_BUSY_ENCODING;
1873 /*Set the Context to Encode with DMA*/
1874 hjpeg->Context &= ~(JPEG_CONTEXT_OPERATION_MASK | JPEG_CONTEXT_METHOD_MASK);
1875 hjpeg->Context |= (JPEG_CONTEXT_ENCODE | JPEG_CONTEXT_DMA);
1877 /*Store In/out buffers pointers and size*/
1878 hjpeg->pJpegInBuffPtr = pDataInMCU;
1879 hjpeg->pJpegOutBuffPtr = pDataOut;
1880 hjpeg->InDataLength = InDataLength;
1881 hjpeg->OutDataLength = OutDataLength;
1883 /*Reset In/out data counter */
1884 hjpeg->JpegInCount = 0;
1885 hjpeg->JpegOutCount = 0;
1887 /*Init decoding process*/
1888 JPEG_Init_Process(hjpeg);
1890 /* JPEG encoding process using DMA */
1891 if (JPEG_DMA_StartProcess(hjpeg) != HAL_OK)
1893 /* Update State */
1894 hjpeg->State = HAL_JPEG_STATE_ERROR;
1895 /* Process Unlocked */
1896 __HAL_UNLOCK(hjpeg);
1898 return HAL_ERROR;
1902 else
1904 /* Process Unlocked */
1905 __HAL_UNLOCK(hjpeg);
1907 return HAL_ERROR;
1910 /* Return function status */
1911 return HAL_OK;
1915 * @brief Starts JPEG decoding with DMA processing
1916 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
1917 * the configuration information for JPEG module
1918 * @param pDataIn Pointer to the input data buffer
1919 * @param InDataLength size in bytes Input buffer
1920 * @param pDataOutMCU Pointer to the Output data buffer
1921 * @param OutDataLength size in bytes of the Output buffer
1922 * @retval HAL status
1924 HAL_StatusTypeDef HAL_JPEG_Decode_DMA(JPEG_HandleTypeDef *hjpeg, uint8_t *pDataIn, uint32_t InDataLength,
1925 uint8_t *pDataOutMCU, uint32_t OutDataLength)
1927 /* Check the parameters */
1928 assert_param((InDataLength >= 4UL));
1929 assert_param((OutDataLength >= 4UL));
1931 /* Check In/out buffer allocation and size */
1932 if ((hjpeg == NULL) || (pDataIn == NULL) || (pDataOutMCU == NULL))
1934 return HAL_ERROR;
1937 /* Process Locked */
1938 __HAL_LOCK(hjpeg);
1940 if (hjpeg->State == HAL_JPEG_STATE_READY)
1942 /*Change JPEG state*/
1943 hjpeg->State = HAL_JPEG_STATE_BUSY_DECODING;
1945 /*Set the Context to Decode with DMA*/
1946 hjpeg->Context &= ~(JPEG_CONTEXT_OPERATION_MASK | JPEG_CONTEXT_METHOD_MASK);
1947 hjpeg->Context |= (JPEG_CONTEXT_DECODE | JPEG_CONTEXT_DMA);
1949 /*Store In/out buffers pointers and size*/
1950 hjpeg->pJpegInBuffPtr = pDataIn;
1951 hjpeg->pJpegOutBuffPtr = pDataOutMCU;
1952 hjpeg->InDataLength = InDataLength;
1953 hjpeg->OutDataLength = OutDataLength;
1955 /*Reset In/out data counter */
1956 hjpeg->JpegInCount = 0;
1957 hjpeg->JpegOutCount = 0;
1959 /*Init decoding process*/
1960 JPEG_Init_Process(hjpeg);
1962 /* JPEG decoding process using DMA */
1963 if (JPEG_DMA_StartProcess(hjpeg) != HAL_OK)
1965 /* Update State */
1966 hjpeg->State = HAL_JPEG_STATE_ERROR;
1967 /* Process Unlocked */
1968 __HAL_UNLOCK(hjpeg);
1970 return HAL_ERROR;
1973 else
1975 /* Process Unlocked */
1976 __HAL_UNLOCK(hjpeg);
1978 return HAL_BUSY;
1980 /* Return function status */
1981 return HAL_OK;
1985 * @brief Pause the JPEG Input/Output processing
1986 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
1987 * the configuration information for JPEG module
1988 * @param XferSelection This parameter can be one of the following values :
1989 * JPEG_PAUSE_RESUME_INPUT : Pause Input processing
1990 * JPEG_PAUSE_RESUME_OUTPUT: Pause Output processing
1991 * JPEG_PAUSE_RESUME_INPUT_OUTPUT: Pause Input and Output processing
1992 * @retval HAL status
1994 HAL_StatusTypeDef HAL_JPEG_Pause(JPEG_HandleTypeDef *hjpeg, uint32_t XferSelection)
1996 uint32_t mask = 0;
1998 assert_param(IS_JPEG_PAUSE_RESUME_STATE(XferSelection));
2000 if ((hjpeg->Context & JPEG_CONTEXT_METHOD_MASK) == JPEG_CONTEXT_DMA)
2002 if ((XferSelection & JPEG_PAUSE_RESUME_INPUT) == JPEG_PAUSE_RESUME_INPUT)
2004 hjpeg->Context |= JPEG_CONTEXT_PAUSE_INPUT;
2006 if ((XferSelection & JPEG_PAUSE_RESUME_OUTPUT) == JPEG_PAUSE_RESUME_OUTPUT)
2008 hjpeg->Context |= JPEG_CONTEXT_PAUSE_OUTPUT;
2012 else if ((hjpeg->Context & JPEG_CONTEXT_METHOD_MASK) == JPEG_CONTEXT_IT)
2015 if ((XferSelection & JPEG_PAUSE_RESUME_INPUT) == JPEG_PAUSE_RESUME_INPUT)
2017 hjpeg->Context |= JPEG_CONTEXT_PAUSE_INPUT;
2018 mask |= (JPEG_IT_IFT | JPEG_IT_IFNF);
2020 if ((XferSelection & JPEG_PAUSE_RESUME_OUTPUT) == JPEG_PAUSE_RESUME_OUTPUT)
2022 hjpeg->Context |= JPEG_CONTEXT_PAUSE_OUTPUT;
2023 mask |= (JPEG_IT_OFT | JPEG_IT_OFNE | JPEG_IT_EOC);
2025 __HAL_JPEG_DISABLE_IT(hjpeg, mask);
2028 else
2030 /* Nothing to do */
2033 /* Return function status */
2034 return HAL_OK;
2038 * @brief Resume the JPEG Input/Output processing
2039 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
2040 * the configuration information for JPEG module
2041 * @param XferSelection This parameter can be one of the following values :
2042 * JPEG_PAUSE_RESUME_INPUT : Resume Input processing
2043 * JPEG_PAUSE_RESUME_OUTPUT: Resume Output processing
2044 * JPEG_PAUSE_RESUME_INPUT_OUTPUT: Resume Input and Output processing
2045 * @retval HAL status
2047 HAL_StatusTypeDef HAL_JPEG_Resume(JPEG_HandleTypeDef *hjpeg, uint32_t XferSelection)
2049 uint32_t mask = 0;
2050 uint32_t xfrSize;
2052 assert_param(IS_JPEG_PAUSE_RESUME_STATE(XferSelection));
2054 if ((hjpeg->Context & (JPEG_CONTEXT_PAUSE_INPUT | JPEG_CONTEXT_PAUSE_OUTPUT)) == 0UL)
2056 /* if nothing paused to resume return error*/
2057 return HAL_ERROR;
2060 if ((hjpeg->Context & JPEG_CONTEXT_METHOD_MASK) == JPEG_CONTEXT_DMA)
2063 if ((XferSelection & JPEG_PAUSE_RESUME_INPUT) == JPEG_PAUSE_RESUME_INPUT)
2065 hjpeg->Context &= (~JPEG_CONTEXT_PAUSE_INPUT);
2066 /*if the MDMA In is triggred with JPEG In FIFO Threshold flag
2067 then MDMA In buffer size is 32 bytes
2069 else (MDMA In is triggred with JPEG In FIFO not full flag)
2070 then MDMA In buffer size is 4 bytes
2072 xfrSize = hjpeg->hdmain->Init.BufferTransferLength;
2074 if (xfrSize == 0UL)
2076 hjpeg->ErrorCode |= HAL_JPEG_ERROR_DMA;
2077 hjpeg->State = HAL_JPEG_STATE_ERROR;
2078 return HAL_ERROR;
2080 /*MDMA transfer size (BNDTR) must be a multiple of MDMA buffer size (TLEN)*/
2081 hjpeg->InDataLength = hjpeg->InDataLength - (hjpeg->InDataLength % xfrSize);
2084 if (hjpeg->InDataLength > 0UL)
2086 /* Start DMA FIFO In transfer */
2087 if (HAL_MDMA_Start_IT(hjpeg->hdmain, (uint32_t)hjpeg->pJpegInBuffPtr, (uint32_t)&hjpeg->Instance->DIR,
2088 hjpeg->InDataLength, 1) != HAL_OK)
2090 hjpeg->ErrorCode |= HAL_JPEG_ERROR_DMA;
2091 hjpeg->State = HAL_JPEG_STATE_ERROR;
2092 return HAL_ERROR;
2096 if ((XferSelection & JPEG_PAUSE_RESUME_OUTPUT) == JPEG_PAUSE_RESUME_OUTPUT)
2098 hjpeg->Context &= (~JPEG_CONTEXT_PAUSE_OUTPUT);
2100 if ((hjpeg->Context & JPEG_CONTEXT_ENDING_DMA) != 0UL)
2102 JPEG_DMA_PollResidualData(hjpeg);
2104 else
2106 /*if the MDMA Out is triggred with JPEG Out FIFO Threshold flag
2107 then MDMA out buffer size is 32 bytes
2108 else (MDMA Out is triggred with JPEG Out FIFO not empty flag)
2109 then MDMA buffer size is 4 bytes
2111 xfrSize = hjpeg->hdmaout->Init.BufferTransferLength;
2113 if (xfrSize == 0UL)
2115 hjpeg->ErrorCode |= HAL_JPEG_ERROR_DMA;
2116 hjpeg->State = HAL_JPEG_STATE_ERROR;
2117 return HAL_ERROR;
2119 /*MDMA transfer size (BNDTR) must be a multiple of MDMA buffer size (TLEN)*/
2120 hjpeg->OutDataLength = hjpeg->OutDataLength - (hjpeg->OutDataLength % xfrSize);
2122 /* Start DMA FIFO Out transfer */
2123 if (HAL_MDMA_Start_IT(hjpeg->hdmaout, (uint32_t)&hjpeg->Instance->DOR, (uint32_t)hjpeg->pJpegOutBuffPtr,
2124 hjpeg->OutDataLength, 1) != HAL_OK)
2126 hjpeg->ErrorCode |= HAL_JPEG_ERROR_DMA;
2127 hjpeg->State = HAL_JPEG_STATE_ERROR;
2128 return HAL_ERROR;
2135 else if ((hjpeg->Context & JPEG_CONTEXT_METHOD_MASK) == JPEG_CONTEXT_IT)
2137 if ((XferSelection & JPEG_PAUSE_RESUME_INPUT) == JPEG_PAUSE_RESUME_INPUT)
2139 hjpeg->Context &= (~JPEG_CONTEXT_PAUSE_INPUT);
2140 mask |= (JPEG_IT_IFT | JPEG_IT_IFNF);
2142 if ((XferSelection & JPEG_PAUSE_RESUME_OUTPUT) == JPEG_PAUSE_RESUME_OUTPUT)
2144 hjpeg->Context &= (~JPEG_CONTEXT_PAUSE_OUTPUT);
2145 mask |= (JPEG_IT_OFT | JPEG_IT_OFNE | JPEG_IT_EOC);
2147 __HAL_JPEG_ENABLE_IT(hjpeg, mask);
2150 else
2152 /* Nothing to do */
2155 /* Return function status */
2156 return HAL_OK;
2160 * @brief Config Encoding/Decoding Input Buffer.
2161 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
2162 * the configuration information for JPEG module.
2163 * @param pNewInputBuffer Pointer to the new input data buffer
2164 * @param InDataLength Size in bytes of the new Input data buffer
2165 * @retval HAL status
2167 void HAL_JPEG_ConfigInputBuffer(JPEG_HandleTypeDef *hjpeg, uint8_t *pNewInputBuffer, uint32_t InDataLength)
2169 hjpeg->pJpegInBuffPtr = pNewInputBuffer;
2170 hjpeg->InDataLength = InDataLength;
2174 * @brief Config Encoding/Decoding Output Buffer.
2175 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
2176 * the configuration information for JPEG module.
2177 * @param pNewOutputBuffer Pointer to the new output data buffer
2178 * @param OutDataLength Size in bytes of the new Output data buffer
2179 * @retval HAL status
2181 void HAL_JPEG_ConfigOutputBuffer(JPEG_HandleTypeDef *hjpeg, uint8_t *pNewOutputBuffer, uint32_t OutDataLength)
2183 hjpeg->pJpegOutBuffPtr = pNewOutputBuffer;
2184 hjpeg->OutDataLength = OutDataLength;
2188 * @brief Aborts the JPEG Encoding/Decoding.
2189 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
2190 * the configuration information for JPEG module
2191 * @retval HAL status
2193 HAL_StatusTypeDef HAL_JPEG_Abort(JPEG_HandleTypeDef *hjpeg)
2195 uint32_t tickstart, tmpContext;
2196 tmpContext = hjpeg->Context;
2198 /*Reset the Context operation and method*/
2199 hjpeg->Context &= ~(JPEG_CONTEXT_OPERATION_MASK | JPEG_CONTEXT_METHOD_MASK | JPEG_CONTEXT_ENDING_DMA);
2201 if ((tmpContext & JPEG_CONTEXT_METHOD_MASK) == JPEG_CONTEXT_DMA)
2203 /* Stop the DMA In/out Xfer*/
2204 if (HAL_MDMA_Abort(hjpeg->hdmaout) != HAL_OK)
2206 if (hjpeg->hdmaout->ErrorCode == HAL_MDMA_ERROR_TIMEOUT)
2208 hjpeg->ErrorCode |= HAL_JPEG_ERROR_DMA;
2211 if (HAL_MDMA_Abort(hjpeg->hdmain) != HAL_OK)
2213 if (hjpeg->hdmain->ErrorCode == HAL_MDMA_ERROR_TIMEOUT)
2215 hjpeg->ErrorCode |= HAL_JPEG_ERROR_DMA;
2221 /* Stop the JPEG encoding/decoding process*/
2222 hjpeg->Instance->CONFR0 &= ~JPEG_CONFR0_START;
2224 /* Get tick */
2225 tickstart = HAL_GetTick();
2227 /* Check if the JPEG Codec is effectively disabled */
2228 while (__HAL_JPEG_GET_FLAG(hjpeg, JPEG_FLAG_COF) != 0UL)
2230 /* Check for the Timeout */
2231 if ((HAL_GetTick() - tickstart) > JPEG_TIMEOUT_VALUE)
2233 /* Update error code */
2234 hjpeg->ErrorCode |= HAL_JPEG_ERROR_TIMEOUT;
2236 /* Change the DMA state */
2237 hjpeg->State = HAL_JPEG_STATE_ERROR;
2238 break;
2242 /* Disable All Interrupts */
2243 __HAL_JPEG_DISABLE_IT(hjpeg, JPEG_INTERRUPT_MASK);
2246 /* Flush input and output FIFOs*/
2247 hjpeg->Instance->CR |= JPEG_CR_IFF;
2248 hjpeg->Instance->CR |= JPEG_CR_OFF;
2250 /* Clear all flags */
2251 __HAL_JPEG_CLEAR_FLAG(hjpeg, JPEG_FLAG_ALL);
2253 /* Reset JpegInCount and JpegOutCount */
2254 hjpeg->JpegInCount = 0;
2255 hjpeg->JpegOutCount = 0;
2257 /*Reset the Context Pause*/
2258 hjpeg->Context &= ~(JPEG_CONTEXT_PAUSE_INPUT | JPEG_CONTEXT_PAUSE_OUTPUT);
2260 /* Change the DMA state*/
2261 if (hjpeg->ErrorCode != HAL_JPEG_ERROR_NONE)
2263 hjpeg->State = HAL_JPEG_STATE_ERROR;
2264 /* Process Unlocked */
2265 __HAL_UNLOCK(hjpeg);
2266 /* Return function status */
2267 return HAL_ERROR;
2269 else
2271 hjpeg->State = HAL_JPEG_STATE_READY;
2272 /* Process Unlocked */
2273 __HAL_UNLOCK(hjpeg);
2274 /* Return function status */
2275 return HAL_OK;
2282 * @}
2285 /** @defgroup JPEG_Exported_Functions_Group4 JPEG Decode/Encode callback functions
2286 * @brief JPEG process callback functions.
2288 @verbatim
2289 ==============================================================================
2290 ##### JPEG Decode and Encode callback functions #####
2291 ==============================================================================
2292 [..] This section provides callback functions:
2293 (+) HAL_JPEG_InfoReadyCallback() : Decoding JPEG Info ready callback
2294 (+) HAL_JPEG_EncodeCpltCallback() : Encoding complete callback.
2295 (+) HAL_JPEG_DecodeCpltCallback() : Decoding complete callback.
2296 (+) HAL_JPEG_ErrorCallback() : JPEG error callback.
2297 (+) HAL_JPEG_GetDataCallback() : Get New Data chunk callback.
2298 (+) HAL_JPEG_DataReadyCallback() : Decoded/Encoded Data ready callback.
2300 @endverbatim
2301 * @{
2305 * @brief Decoding JPEG Info ready callback.
2306 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
2307 * the configuration information for JPEG module
2308 * @param pInfo pointer to a JPEG_ConfTypeDef structure that contains
2309 * The JPEG decoded header informations
2310 * @retval None
2312 __weak void HAL_JPEG_InfoReadyCallback(JPEG_HandleTypeDef *hjpeg, JPEG_ConfTypeDef *pInfo)
2314 /* Prevent unused argument(s) compilation warning */
2315 UNUSED(hjpeg);
2316 UNUSED(pInfo);
2318 /* NOTE : This function Should not be modified, when the callback is needed,
2319 the HAL_JPEG_HeaderParsingCpltCallback could be implemented in the user file
2324 * @brief Encoding complete callback.
2325 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
2326 * the configuration information for JPEG module
2327 * @retval None
2329 __weak void HAL_JPEG_EncodeCpltCallback(JPEG_HandleTypeDef *hjpeg)
2331 /* Prevent unused argument(s) compilation warning */
2332 UNUSED(hjpeg);
2334 /* NOTE : This function Should not be modified, when the callback is needed,
2335 the HAL_JPEG_EncodeCpltCallback could be implemented in the user file
2340 * @brief Decoding complete callback.
2341 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
2342 * the configuration information for JPEG module
2343 * @retval None
2345 __weak void HAL_JPEG_DecodeCpltCallback(JPEG_HandleTypeDef *hjpeg)
2347 /* Prevent unused argument(s) compilation warning */
2348 UNUSED(hjpeg);
2350 /* NOTE : This function Should not be modified, when the callback is needed,
2351 the HAL_JPEG_EncodeCpltCallback could be implemented in the user file
2356 * @brief JPEG error callback.
2357 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
2358 * the configuration information for JPEG module
2359 * @retval None
2361 __weak void HAL_JPEG_ErrorCallback(JPEG_HandleTypeDef *hjpeg)
2363 /* Prevent unused argument(s) compilation warning */
2364 UNUSED(hjpeg);
2366 /* NOTE : This function Should not be modified, when the callback is needed,
2367 the HAL_JPEG_ErrorCallback could be implemented in the user file
2372 * @brief Get New Data chunk callback.
2373 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
2374 * the configuration information for JPEG module
2375 * @param NbDecodedData Number of consummed data in the previous chunk in bytes
2376 * @retval None
2378 __weak void HAL_JPEG_GetDataCallback(JPEG_HandleTypeDef *hjpeg, uint32_t NbDecodedData)
2380 /* Prevent unused argument(s) compilation warning */
2381 UNUSED(hjpeg);
2382 UNUSED(NbDecodedData);
2384 /* NOTE : This function Should not be modified, when the callback is needed,
2385 the HAL_JPEG_GetDataCallback could be implemented in the user file
2390 * @brief Decoded/Encoded Data ready callback.
2391 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
2392 * the configuration information for JPEG module
2393 * @param pDataOut pointer to the output data buffer
2394 * @param OutDataLength number in bytes of data available in the specified output buffer
2395 * @retval None
2397 __weak void HAL_JPEG_DataReadyCallback(JPEG_HandleTypeDef *hjpeg, uint8_t *pDataOut, uint32_t OutDataLength)
2399 /* Prevent unused argument(s) compilation warning */
2400 UNUSED(hjpeg);
2401 UNUSED(pDataOut);
2402 UNUSED(OutDataLength);
2404 /* NOTE : This function Should not be modified, when the callback is needed,
2405 the HAL_JPEG_DataReadyCallback could be implemented in the user file
2410 * @}
2414 /** @defgroup JPEG_Exported_Functions_Group5 JPEG IRQ handler management
2415 * @brief JPEG IRQ handler.
2417 @verbatim
2418 ==============================================================================
2419 ##### JPEG IRQ handler management #####
2420 ==============================================================================
2421 [..] This section provides JPEG IRQ handler function.
2422 (+) HAL_JPEG_IRQHandler() : handles JPEG interrupt request
2424 @endverbatim
2425 * @{
2429 * @brief This function handles JPEG interrupt request.
2430 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
2431 * the configuration information for JPEG module
2432 * @retval None
2434 void HAL_JPEG_IRQHandler(JPEG_HandleTypeDef *hjpeg)
2436 switch (hjpeg->State)
2438 case HAL_JPEG_STATE_BUSY_ENCODING:
2439 case HAL_JPEG_STATE_BUSY_DECODING:
2440 /* continue JPEG data encoding/Decoding*/
2441 /* JPEG data processing : In/Out FIFO transfer*/
2442 if ((hjpeg->Context & JPEG_CONTEXT_METHOD_MASK) == JPEG_CONTEXT_IT)
2444 (void) JPEG_Process(hjpeg);
2446 else if ((hjpeg->Context & JPEG_CONTEXT_METHOD_MASK) == JPEG_CONTEXT_DMA)
2448 JPEG_DMA_ContinueProcess(hjpeg);
2450 else
2452 /* Nothing to do */
2454 break;
2456 default:
2457 break;
2462 * @}
2465 /** @defgroup JPEG_Exported_Functions_Group6 Peripheral State functions
2466 * @brief Peripheral State functions.
2468 @verbatim
2469 ==============================================================================
2470 ##### Peripheral State and Error functions #####
2471 ==============================================================================
2472 [..] This section provides JPEG State and Errors function.
2473 (+) HAL_JPEG_GetState() : permits to get in run-time the JPEG state.
2474 (+) HAL_JPEG_GetError() : Returns the JPEG error code if any.
2476 @endverbatim
2477 * @{
2481 * @brief Returns the JPEG state.
2482 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
2483 * the configuration information for JPEG module
2484 * @retval JPEG state
2486 HAL_JPEG_STATETypeDef HAL_JPEG_GetState(JPEG_HandleTypeDef *hjpeg)
2488 return hjpeg->State;
2492 * @brief Return the JPEG error code
2493 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
2494 * the configuration information for the specified JPEG.
2495 * @retval JPEG Error Code
2497 uint32_t HAL_JPEG_GetError(JPEG_HandleTypeDef *hjpeg)
2499 return hjpeg->ErrorCode;
2503 * @}
2507 * @}
2511 /** @addtogroup JPEG_Private_Functions
2512 * @{
2516 * @brief Generates Huffman sizes/Codes Table from Bits/vals Table
2517 * @param Bits pointer to bits table
2518 * @param Huffsize pointer to sizes table
2519 * @param Huffcode pointer to codes table
2520 * @param LastK pointer to last Coeff (table dimmension)
2521 * @retval HAL status
2523 static HAL_StatusTypeDef JPEG_Bits_To_SizeCodes(uint8_t *Bits, uint8_t *Huffsize, uint32_t *Huffcode, uint32_t *LastK)
2525 uint32_t i, p, l, code, si;
2527 /* Figure C.1: Generation of table of Huffman code sizes */
2528 p = 0;
2529 for (l = 0; l < 16UL; l++)
2531 i = (uint32_t)Bits[l];
2532 if ((p + i) > 256UL)
2534 /* check for table overflow */
2535 return HAL_ERROR;
2537 while (i != 0UL)
2539 Huffsize[p] = (uint8_t) l + 1U;
2540 p++;
2541 i--;
2544 Huffsize[p] = 0;
2545 *LastK = p;
2547 /* Figure C.2: Generation of table of Huffman codes */
2548 code = 0;
2549 si = Huffsize[0];
2550 p = 0;
2551 while (Huffsize[p] != 0U)
2553 while (((uint32_t) Huffsize[p]) == si)
2555 Huffcode[p] = code;
2556 p++;
2557 code++;
2559 /* code must fit in "size" bits (si), no code is allowed to be all ones*/
2560 if(si > 31UL)
2562 return HAL_ERROR;
2564 if (((uint32_t) code) >= (((uint32_t) 1) << si))
2566 return HAL_ERROR;
2568 code <<= 1;
2569 si++;
2571 /* Return function status */
2572 return HAL_OK;
2576 * @brief Transform a Bits/Vals AC Huffman table to sizes/Codes huffman Table
2577 * that can programmed to the JPEG encoder registers
2578 * @param AC_BitsValsTable pointer to AC huffman bits/vals table
2579 * @param AC_SizeCodesTable pointer to AC huffman Sizes/Codes table
2580 * @retval HAL status
2582 static HAL_StatusTypeDef JPEG_ACHuff_BitsVals_To_SizeCodes(JPEG_ACHuffTableTypeDef *AC_BitsValsTable,
2583 JPEG_AC_HuffCodeTableTypeDef *AC_SizeCodesTable)
2585 HAL_StatusTypeDef error;
2586 uint8_t huffsize[257];
2587 uint32_t huffcode[257];
2588 uint32_t k;
2589 uint32_t l, lsb, msb;
2590 uint32_t lastK;
2592 error = JPEG_Bits_To_SizeCodes(AC_BitsValsTable->Bits, huffsize, huffcode, &lastK);
2593 if (error != HAL_OK)
2595 return error;
2598 /* Figure C.3: Ordering procedure for encoding procedure code tables */
2599 k = 0;
2601 while (k < lastK)
2603 l = AC_BitsValsTable->HuffVal[k];
2604 if (l == 0UL)
2606 l = 160; /*l = 0x00 EOB code*/
2608 else if (l == 0xF0UL) /* l = 0xF0 ZRL code*/
2610 l = 161;
2612 else
2614 msb = (l & 0xF0UL) >> 4;
2615 lsb = (l & 0x0FUL);
2616 l = (msb * 10UL) + lsb - 1UL;
2618 if (l >= JPEG_AC_HUFF_TABLE_SIZE)
2620 return HAL_ERROR; /* Huffman Table overflow error*/
2622 else
2624 AC_SizeCodesTable->HuffmanCode[l] = huffcode[k];
2625 AC_SizeCodesTable->CodeLength[l] = huffsize[k] - 1U;
2626 k++;
2630 /* Return function status */
2631 return HAL_OK;
2635 * @brief Transform a Bits/Vals DC Huffman table to sizes/Codes huffman Table
2636 * that can programmed to the JPEG encoder registers
2637 * @param DC_BitsValsTable pointer to DC huffman bits/vals table
2638 * @param DC_SizeCodesTable pointer to DC huffman Sizes/Codes table
2639 * @retval HAL status
2641 static HAL_StatusTypeDef JPEG_DCHuff_BitsVals_To_SizeCodes(JPEG_DCHuffTableTypeDef *DC_BitsValsTable,
2642 JPEG_DC_HuffCodeTableTypeDef *DC_SizeCodesTable)
2644 HAL_StatusTypeDef error;
2646 uint32_t k;
2647 uint32_t l;
2648 uint32_t lastK;
2649 uint8_t huffsize[257];
2650 uint32_t huffcode[257];
2651 error = JPEG_Bits_To_SizeCodes(DC_BitsValsTable->Bits, huffsize, huffcode, &lastK);
2652 if (error != HAL_OK)
2654 return error;
2656 /* Figure C.3: ordering procedure for encoding procedure code tables */
2657 k = 0;
2659 while (k < lastK)
2661 l = DC_BitsValsTable->HuffVal[k];
2662 if (l >= JPEG_DC_HUFF_TABLE_SIZE)
2664 return HAL_ERROR; /* Huffman Table overflow error*/
2666 else
2668 DC_SizeCodesTable->HuffmanCode[l] = huffcode[k];
2669 DC_SizeCodesTable->CodeLength[l] = huffsize[k] - 1U;
2670 k++;
2674 /* Return function status */
2675 return HAL_OK;
2679 * @brief Set the JPEG register with an DC huffman table at the given DC table address
2680 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
2681 * the configuration information for JPEG module
2682 * @param HuffTableDC pointer to DC huffman table
2683 * @param DCTableAddress Encoder DC huffman table address it could be HUFFENC_DC0 or HUFFENC_DC1.
2684 * @retval HAL status
2686 static HAL_StatusTypeDef JPEG_Set_HuffDC_Mem(JPEG_HandleTypeDef *hjpeg, JPEG_DCHuffTableTypeDef *HuffTableDC,
2687 const __IO uint32_t *DCTableAddress)
2689 HAL_StatusTypeDef error;
2690 JPEG_DC_HuffCodeTableTypeDef dcSizeCodesTable;
2691 uint32_t i, lsb, msb;
2692 __IO uint32_t *address, *addressDef;
2694 if (DCTableAddress == (hjpeg->Instance->HUFFENC_DC0))
2696 address = (hjpeg->Instance->HUFFENC_DC0 + (JPEG_DC_HUFF_TABLE_SIZE / 2UL));
2698 else if (DCTableAddress == (hjpeg->Instance->HUFFENC_DC1))
2700 address = (hjpeg->Instance->HUFFENC_DC1 + (JPEG_DC_HUFF_TABLE_SIZE / 2UL));
2702 else
2704 return HAL_ERROR;
2707 if (HuffTableDC != NULL)
2709 error = JPEG_DCHuff_BitsVals_To_SizeCodes(HuffTableDC, &dcSizeCodesTable);
2710 if (error != HAL_OK)
2712 return error;
2714 addressDef = address;
2715 *addressDef = 0x0FFF0FFF;
2716 addressDef++;
2717 *addressDef = 0x0FFF0FFF;
2719 i = JPEG_DC_HUFF_TABLE_SIZE;
2720 while (i > 1UL)
2722 i--;
2723 address --;
2724 msb = ((uint32_t)(((uint32_t)dcSizeCodesTable.CodeLength[i] & 0xFU) << 8)) | ((uint32_t)dcSizeCodesTable.HuffmanCode[i] &
2725 0xFFUL);
2726 i--;
2727 lsb = ((uint32_t)(((uint32_t)dcSizeCodesTable.CodeLength[i] & 0xFU) << 8)) | ((uint32_t)dcSizeCodesTable.HuffmanCode[i] &
2728 0xFFUL);
2730 *address = lsb | (msb << 16);
2734 /* Return function status */
2735 return HAL_OK;
2739 * @brief Set the JPEG register with an AC huffman table at the given AC table address
2740 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
2741 * the configuration information for JPEG module
2742 * @param HuffTableAC pointer to AC huffman table
2743 * @param ACTableAddress Encoder AC huffman table address it could be HUFFENC_AC0 or HUFFENC_AC1.
2744 * @retval HAL status
2746 static HAL_StatusTypeDef JPEG_Set_HuffAC_Mem(JPEG_HandleTypeDef *hjpeg, JPEG_ACHuffTableTypeDef *HuffTableAC,
2747 const __IO uint32_t *ACTableAddress)
2749 HAL_StatusTypeDef error;
2750 JPEG_AC_HuffCodeTableTypeDef acSizeCodesTable;
2751 uint32_t i, lsb, msb;
2752 __IO uint32_t *address, *addressDef;
2754 if (ACTableAddress == (hjpeg->Instance->HUFFENC_AC0))
2756 address = (hjpeg->Instance->HUFFENC_AC0 + (JPEG_AC_HUFF_TABLE_SIZE / 2UL));
2758 else if (ACTableAddress == (hjpeg->Instance->HUFFENC_AC1))
2760 address = (hjpeg->Instance->HUFFENC_AC1 + (JPEG_AC_HUFF_TABLE_SIZE / 2UL));
2762 else
2764 return HAL_ERROR;
2767 if (HuffTableAC != NULL)
2769 error = JPEG_ACHuff_BitsVals_To_SizeCodes(HuffTableAC, &acSizeCodesTable);
2770 if (error != HAL_OK)
2772 return error;
2774 /* Default values settings: 162:167 FFFh , 168:175 FD0h_FD7h */
2775 /* Locations 162:175 of each AC table contain information used internally by the core */
2777 addressDef = address;
2778 for (i = 0; i < 3UL; i++)
2780 *addressDef = 0x0FFF0FFF;
2781 addressDef++;
2783 *addressDef = 0x0FD10FD0;
2784 addressDef++;
2785 *addressDef = 0x0FD30FD2;
2786 addressDef++;
2787 *addressDef = 0x0FD50FD4;
2788 addressDef++;
2789 *addressDef = 0x0FD70FD6;
2790 /* end of Locations 162:175 */
2793 i = JPEG_AC_HUFF_TABLE_SIZE;
2794 while (i > 1UL)
2796 i--;
2797 address--;
2798 msb = ((uint32_t)(((uint32_t)acSizeCodesTable.CodeLength[i] & 0xFU) << 8)) | ((uint32_t)acSizeCodesTable.HuffmanCode[i] &
2799 0xFFUL);
2800 i--;
2801 lsb = ((uint32_t)(((uint32_t)acSizeCodesTable.CodeLength[i] & 0xFU) << 8)) | ((uint32_t)acSizeCodesTable.HuffmanCode[i] &
2802 0xFFUL);
2804 *address = lsb | (msb << 16);
2808 /* Return function status */
2809 return HAL_OK;
2813 * @brief Configure the JPEG encoder register huffman tables to used during
2814 * the encdoing operation
2815 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
2816 * the configuration information for JPEG module
2817 * @retval None
2819 static HAL_StatusTypeDef JPEG_Set_HuffEnc_Mem(JPEG_HandleTypeDef *hjpeg)
2821 HAL_StatusTypeDef error;
2823 JPEG_Set_Huff_DHTMem(hjpeg);
2824 error = JPEG_Set_HuffAC_Mem(hjpeg, (JPEG_ACHuffTableTypeDef *)(uint32_t)&JPEG_ACLUM_HuffTable,
2825 (hjpeg->Instance->HUFFENC_AC0));
2826 if (error != HAL_OK)
2828 return error;
2831 error = JPEG_Set_HuffAC_Mem(hjpeg, (JPEG_ACHuffTableTypeDef *)(uint32_t)&JPEG_ACCHROM_HuffTable,
2832 (hjpeg->Instance->HUFFENC_AC1));
2833 if (error != HAL_OK)
2835 return error;
2838 error = JPEG_Set_HuffDC_Mem(hjpeg, (JPEG_DCHuffTableTypeDef *)(uint32_t)&JPEG_DCLUM_HuffTable,
2839 hjpeg->Instance->HUFFENC_DC0);
2840 if (error != HAL_OK)
2842 return error;
2845 error = JPEG_Set_HuffDC_Mem(hjpeg, (JPEG_DCHuffTableTypeDef *)(uint32_t)&JPEG_DCCHROM_HuffTable,
2846 hjpeg->Instance->HUFFENC_DC1);
2847 if (error != HAL_OK)
2849 return error;
2851 /* Return function status */
2852 return HAL_OK;
2856 * @brief Configure the JPEG register huffman tables to be included in the JPEG
2857 * file header (used for encoding only)
2858 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
2859 * the configuration information for JPEG module
2860 * @retval None
2862 static void JPEG_Set_Huff_DHTMem(JPEG_HandleTypeDef *hjpeg)
2864 JPEG_ACHuffTableTypeDef *HuffTableAC0 = (JPEG_ACHuffTableTypeDef *)(uint32_t)&JPEG_ACLUM_HuffTable;
2865 JPEG_ACHuffTableTypeDef *HuffTableAC1 = (JPEG_ACHuffTableTypeDef *)(uint32_t)&JPEG_ACCHROM_HuffTable;
2866 JPEG_DCHuffTableTypeDef *HuffTableDC0 = (JPEG_DCHuffTableTypeDef *)(uint32_t)&JPEG_DCLUM_HuffTable;
2867 JPEG_DCHuffTableTypeDef *HuffTableDC1 = (JPEG_DCHuffTableTypeDef *)(uint32_t)&JPEG_DCCHROM_HuffTable;
2868 uint32_t value, index;
2869 __IO uint32_t *address;
2871 /* DC0 Huffman Table : BITS*/
2872 /* DC0 BITS is a 16 Bytes table i.e 4x32bits words from DHTMEM base address to DHTMEM + 3*/
2873 address = (hjpeg->Instance->DHTMEM + 3);
2874 index = 16;
2875 while (index > 3UL)
2878 *address = (((uint32_t)HuffTableDC0->Bits[index - 1UL] & 0xFFUL) << 24) |
2879 (((uint32_t)HuffTableDC0->Bits[index - 2UL] & 0xFFUL) << 16) |
2880 (((uint32_t)HuffTableDC0->Bits[index - 3UL] & 0xFFUL) << 8) |
2881 ((uint32_t)HuffTableDC0->Bits[index - 4UL] & 0xFFUL);
2882 address--;
2883 index -= 4UL;
2886 /* DC0 Huffman Table : Val*/
2887 /* DC0 VALS is a 12 Bytes table i.e 3x32bits words from DHTMEM base address +4 to DHTMEM + 6 */
2888 address = (hjpeg->Instance->DHTMEM + 6);
2889 index = 12;
2890 while (index > 3UL)
2892 *address = (((uint32_t)HuffTableDC0->HuffVal[index - 1UL] & 0xFFUL) << 24) |
2893 (((uint32_t)HuffTableDC0->HuffVal[index - 2UL] & 0xFFUL) << 16) |
2894 (((uint32_t)HuffTableDC0->HuffVal[index - 3UL] & 0xFFUL) << 8) |
2895 ((uint32_t)HuffTableDC0->HuffVal[index - 4UL] & 0xFFUL);
2896 address--;
2897 index -= 4UL;
2900 /* AC0 Huffman Table : BITS*/
2901 /* AC0 BITS is a 16 Bytes table i.e 4x32bits words from DHTMEM base address + 7 to DHTMEM + 10*/
2902 address = (hjpeg->Instance->DHTMEM + 10UL);
2903 index = 16;
2904 while (index > 3UL)
2907 *address = (((uint32_t)HuffTableAC0->Bits[index - 1UL] & 0xFFUL) << 24) |
2908 (((uint32_t)HuffTableAC0->Bits[index - 2UL] & 0xFFUL) << 16) |
2909 (((uint32_t)HuffTableAC0->Bits[index - 3UL] & 0xFFUL) << 8) |
2910 ((uint32_t)HuffTableAC0->Bits[index - 4UL] & 0xFFUL);
2911 address--;
2912 index -= 4UL;
2915 /* AC0 Huffman Table : Val*/
2916 /* AC0 VALS is a 162 Bytes table i.e 41x32bits words from DHTMEM base address + 11 to DHTMEM + 51 */
2917 /* only Byte 0 and Byte 1 of the last word (@ DHTMEM + 51) belong to AC0 VALS table */
2918 address = (hjpeg->Instance->DHTMEM + 51);
2919 value = *address & 0xFFFF0000U;
2920 value = value | (((uint32_t)HuffTableAC0->HuffVal[161] & 0xFFUL) << 8) | ((uint32_t)HuffTableAC0->HuffVal[160] & 0xFFUL);
2921 *address = value;
2923 /*continue setting 160 AC0 huffman values */
2924 address--; /* address = hjpeg->Instance->DHTMEM + 50*/
2925 index = 160;
2926 while (index > 3UL)
2928 *address = (((uint32_t)HuffTableAC0->HuffVal[index - 1UL] & 0xFFUL) << 24) |
2929 (((uint32_t)HuffTableAC0->HuffVal[index - 2UL] & 0xFFUL) << 16) |
2930 (((uint32_t)HuffTableAC0->HuffVal[index - 3UL] & 0xFFUL) << 8) |
2931 ((uint32_t)HuffTableAC0->HuffVal[index - 4UL] & 0xFFUL);
2932 address--;
2933 index -= 4UL;
2936 /* DC1 Huffman Table : BITS*/
2937 /* DC1 BITS is a 16 Bytes table i.e 4x32bits words from DHTMEM + 51 base address to DHTMEM + 55*/
2938 /* only Byte 2 and Byte 3 of the first word (@ DHTMEM + 51) belong to DC1 Bits table */
2939 address = (hjpeg->Instance->DHTMEM + 51);
2940 value = *address & 0x0000FFFFU;
2941 value = value | (((uint32_t)HuffTableDC1->Bits[1] & 0xFFUL) << 24) | (((uint32_t)HuffTableDC1->Bits[0] & 0xFFUL) << 16);
2942 *address = value;
2944 /* only Byte 0 and Byte 1 of the last word (@ DHTMEM + 55) belong to DC1 Bits table */
2945 address = (hjpeg->Instance->DHTMEM + 55);
2946 value = *address & 0xFFFF0000U;
2947 value = value | (((uint32_t)HuffTableDC1->Bits[15] & 0xFFUL) << 8) | ((uint32_t)HuffTableDC1->Bits[14] & 0xFFUL);
2948 *address = value;
2950 /*continue setting 12 DC1 huffman Bits from DHTMEM + 54 down to DHTMEM + 52*/
2951 address--;
2952 index = 12;
2953 while (index > 3UL)
2956 *address = (((uint32_t)HuffTableDC1->Bits[index + 1UL] & 0xFFUL) << 24) |
2957 (((uint32_t)HuffTableDC1->Bits[index] & 0xFFUL) << 16) |
2958 (((uint32_t)HuffTableDC1->Bits[index - 1UL] & 0xFFUL) << 8) |
2959 ((uint32_t)HuffTableDC1->Bits[index - 2UL] & 0xFFUL);
2960 address--;
2961 index -= 4UL;
2964 /* DC1 Huffman Table : Val*/
2965 /* DC1 VALS is a 12 Bytes table i.e 3x32bits words from DHTMEM base address +55 to DHTMEM + 58 */
2966 /* only Byte 2 and Byte 3 of the first word (@ DHTMEM + 55) belong to DC1 Val table */
2967 address = (hjpeg->Instance->DHTMEM + 55);
2968 value = *address & 0x0000FFFFUL;
2969 value = value | (((uint32_t)HuffTableDC1->HuffVal[1] & 0xFFUL) << 24) | (((uint32_t)HuffTableDC1->HuffVal[0] & 0xFFUL) <<
2970 16);
2971 *address = value;
2973 /* only Byte 0 and Byte 1 of the last word (@ DHTMEM + 58) belong to DC1 Val table */
2974 address = (hjpeg->Instance->DHTMEM + 58);
2975 value = *address & 0xFFFF0000UL;
2976 value = value | (((uint32_t)HuffTableDC1->HuffVal[11] & 0xFFUL) << 8) | ((uint32_t)HuffTableDC1->HuffVal[10] & 0xFFUL);
2977 *address = value;
2979 /*continue setting 8 DC1 huffman val from DHTMEM + 57 down to DHTMEM + 56*/
2980 address--;
2981 index = 8;
2982 while (index > 3UL)
2984 *address = (((uint32_t)HuffTableDC1->HuffVal[index + 1UL] & 0xFFUL) << 24) |
2985 (((uint32_t)HuffTableDC1->HuffVal[index] & 0xFFUL) << 16) |
2986 (((uint32_t)HuffTableDC1->HuffVal[index - 1UL] & 0xFFUL) << 8) |
2987 ((uint32_t)HuffTableDC1->HuffVal[index - 2UL] & 0xFFUL);
2988 address--;
2989 index -= 4UL;
2992 /* AC1 Huffman Table : BITS*/
2993 /* AC1 BITS is a 16 Bytes table i.e 4x32bits words from DHTMEM base address + 58 to DHTMEM + 62*/
2994 /* only Byte 2 and Byte 3 of the first word (@ DHTMEM + 58) belong to AC1 Bits table */
2995 address = (hjpeg->Instance->DHTMEM + 58);
2996 value = *address & 0x0000FFFFU;
2997 value = value | (((uint32_t)HuffTableAC1->Bits[1] & 0xFFUL) << 24) | (((uint32_t)HuffTableAC1->Bits[0] & 0xFFUL) << 16);
2998 *address = value;
3000 /* only Byte 0 and Byte 1 of the last word (@ DHTMEM + 62) belong to Bits Val table */
3001 address = (hjpeg->Instance->DHTMEM + 62);
3002 value = *address & 0xFFFF0000U;
3003 value = value | (((uint32_t)HuffTableAC1->Bits[15] & 0xFFUL) << 8) | ((uint32_t)HuffTableAC1->Bits[14] & 0xFFUL);
3004 *address = value;
3006 /*continue setting 12 AC1 huffman Bits from DHTMEM + 61 down to DHTMEM + 59*/
3007 address--;
3008 index = 12;
3009 while (index > 3UL)
3012 *address = (((uint32_t)HuffTableAC1->Bits[index + 1UL] & 0xFFUL) << 24) |
3013 (((uint32_t)HuffTableAC1->Bits[index] & 0xFFUL) << 16) |
3014 (((uint32_t)HuffTableAC1->Bits[index - 1UL] & 0xFFUL) << 8) |
3015 ((uint32_t)HuffTableAC1->Bits[index - 2UL] & 0xFFUL);
3016 address--;
3017 index -= 4UL;
3020 /* AC1 Huffman Table : Val*/
3021 /* AC1 VALS is a 162 Bytes table i.e 41x32bits words from DHTMEM base address + 62 to DHTMEM + 102 */
3022 /* only Byte 2 and Byte 3 of the first word (@ DHTMEM + 62) belong to AC1 VALS table */
3023 address = (hjpeg->Instance->DHTMEM + 62);
3024 value = *address & 0x0000FFFFUL;
3025 value = value | (((uint32_t)HuffTableAC1->HuffVal[1] & 0xFFUL) << 24) | (((uint32_t)HuffTableAC1->HuffVal[0] & 0xFFUL) <<
3026 16);
3027 *address = value;
3029 /*continue setting 160 AC1 huffman values from DHTMEM + 63 to DHTMEM+102 */
3030 address = (hjpeg->Instance->DHTMEM + 102);
3031 index = 160;
3032 while (index > 3UL)
3034 *address = (((uint32_t)HuffTableAC1->HuffVal[index + 1UL] & 0xFFUL) << 24) |
3035 (((uint32_t)HuffTableAC1->HuffVal[index] & 0xFFUL) << 16) |
3036 (((uint32_t)HuffTableAC1->HuffVal[index - 1UL] & 0xFFUL) << 8) |
3037 ((uint32_t)HuffTableAC1->HuffVal[index - 2UL] & 0xFFUL);
3038 address--;
3039 index -= 4UL;
3045 * @brief Configure the JPEG registers with a given quantization table
3046 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
3047 * the configuration information for JPEG module
3048 * @param QTable pointer to an array of 64 bytes giving the quantization table
3049 * @param QTableAddress destination quantization address in the JPEG peripheral
3050 * it could be QMEM0, QMEM1, QMEM2 or QMEM3
3051 * @retval 0 if no error, 1 if error
3053 static uint32_t JPEG_Set_Quantization_Mem(JPEG_HandleTypeDef *hjpeg, uint8_t *QTable,
3054 __IO uint32_t *QTableAddress)
3056 uint32_t i, j, quantRow, quantVal, ScaleFactor;
3057 __IO uint32_t *tableAddress;
3059 tableAddress = QTableAddress;
3061 if ((hjpeg->Conf.ImageQuality >= 50UL) && (hjpeg->Conf.ImageQuality <= 100UL))
3063 ScaleFactor = 200UL - (hjpeg->Conf.ImageQuality * 2UL);
3065 else if (hjpeg->Conf.ImageQuality > 0UL)
3067 ScaleFactor = ((uint32_t) 5000) / ((uint32_t) hjpeg->Conf.ImageQuality);
3069 else
3071 return 1UL;
3074 /*Quantization_table = (Standard_quanization_table * ScaleFactor + 50) / 100*/
3075 i = 0;
3076 while (i < (JPEG_QUANT_TABLE_SIZE - 3UL))
3078 quantRow = 0;
3079 for (j = 0; j < 4UL; j++)
3081 /* Note that the quantization coefficients must be specified in the table in zigzag order */
3082 quantVal = ((((uint32_t) QTable[JPEG_ZIGZAG_ORDER[i + j]]) * ScaleFactor) + 50UL) / 100UL;
3084 if (quantVal == 0UL)
3086 quantVal = 1UL;
3088 else if (quantVal > 255UL)
3090 quantVal = 255UL;
3092 else
3094 /* Nothing to do, keep same value of quantVal */
3097 quantRow |= ((quantVal & 0xFFUL) << (8UL * j));
3100 i += 4UL;
3101 *tableAddress = quantRow;
3102 tableAddress ++;
3105 /* Return function status */
3106 return 0UL;
3110 * @brief Configure the JPEG registers for YCbCr color space
3111 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
3112 * the configuration information for JPEG module
3113 * @retval None
3115 static void JPEG_SetColorYCBCR(JPEG_HandleTypeDef *hjpeg)
3117 uint32_t ySamplingH;
3118 uint32_t ySamplingV;
3119 uint32_t yblockNb;
3121 /*Set Number of color components to 3*/
3122 hjpeg->Instance->CONFR1 &= ~JPEG_CONFR1_NF;
3123 hjpeg->Instance->CONFR1 |= JPEG_CONFR1_NF_1;
3125 /* compute MCU block size and Y, Cb ,Cr sampling factors*/
3126 if (hjpeg->Conf.ChromaSubsampling == JPEG_420_SUBSAMPLING)
3128 ySamplingH = JPEG_CONFR4_HSF_1; /* Hs = 2*/
3129 ySamplingV = JPEG_CONFR4_VSF_1; /* Vs = 2*/
3131 yblockNb = 0x30; /* 4 blocks of 8x8*/
3133 else if (hjpeg->Conf.ChromaSubsampling == JPEG_422_SUBSAMPLING)
3135 ySamplingH = JPEG_CONFR4_HSF_1; /* Hs = 2*/
3136 ySamplingV = JPEG_CONFR4_VSF_0; /* Vs = 1*/
3138 yblockNb = 0x10; /* 2 blocks of 8x8*/
3140 else /*JPEG_444_SUBSAMPLING and default*/
3142 ySamplingH = JPEG_CONFR4_HSF_0; /* Hs = 1*/
3143 ySamplingV = JPEG_CONFR4_VSF_0; /* Vs = 1*/
3145 yblockNb = 0; /* 1 block of 8x8*/
3148 hjpeg->Instance->CONFR1 &= ~(JPEG_CONFR1_NF | JPEG_CONFR1_NS);
3149 hjpeg->Instance->CONFR1 |= (JPEG_CONFR1_NF_1 | JPEG_CONFR1_NS_1);
3151 /*Reset CONFR4 register*/
3152 hjpeg->Instance->CONFR4 = 0;
3153 /*Set Horizental and Vertical sampling factor , number of blocks , Quantization table and Huffman AC/DC tables for component 0*/
3154 hjpeg->Instance->CONFR4 |= (ySamplingH | ySamplingV | (yblockNb & JPEG_CONFR4_NB));
3156 /*Reset CONFR5 register*/
3157 hjpeg->Instance->CONFR5 = 0;
3158 /*Set Horizental and Vertical sampling factor , number of blocks , Quantization table and Huffman AC/DC tables for component 1*/
3159 hjpeg->Instance->CONFR5 |= (JPEG_CONFR5_HSF_0 | JPEG_CONFR5_VSF_0 | JPEG_CONFR5_QT_0 | JPEG_CONFR5_HA | JPEG_CONFR5_HD);
3161 /*Reset CONFR6 register*/
3162 hjpeg->Instance->CONFR6 = 0;
3163 /*Set Horizental and Vertical sampling factor and number of blocks for component 2*/
3164 /* In YCBCR , by default, both chrominance components (component 1 and component 2) use the same Quantization table (table 1) */
3165 /* In YCBCR , both chrominance components (component 1 and component 2) use the same Huffman tables (table 1) */
3166 hjpeg->Instance->CONFR6 |= (JPEG_CONFR6_HSF_0 | JPEG_CONFR6_VSF_0 | JPEG_CONFR6_QT_0 | JPEG_CONFR6_HA | JPEG_CONFR6_HD);
3171 * @brief Configure the JPEG registers for GrayScale color space
3172 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
3173 * the configuration information for JPEG module
3174 * @retval None
3176 static void JPEG_SetColorGrayScale(JPEG_HandleTypeDef *hjpeg)
3178 /*Set Number of color components to 1*/
3179 hjpeg->Instance->CONFR1 &= ~(JPEG_CONFR1_NF | JPEG_CONFR1_NS);
3181 /*in GrayScale use 1 single Quantization table (Table 0)*/
3182 /*in GrayScale use only one couple of AC/DC huffman table (table 0)*/
3184 /*Reset CONFR4 register*/
3185 hjpeg->Instance->CONFR4 = 0;
3186 /*Set Horizental and Vertical sampling factor , number of blocks , Quantization table and Huffman AC/DC tables for component 0*/
3187 hjpeg->Instance->CONFR4 |= JPEG_CONFR4_HSF_0 | JPEG_CONFR4_VSF_0 ;
3191 * @brief Configure the JPEG registers for CMYK color space
3192 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
3193 * the configuration information for JPEG module
3194 * @retval None
3196 static void JPEG_SetColorCMYK(JPEG_HandleTypeDef *hjpeg)
3198 uint32_t ySamplingH;
3199 uint32_t ySamplingV;
3200 uint32_t yblockNb;
3202 /*Set Number of color components to 4*/
3203 hjpeg->Instance->CONFR1 |= (JPEG_CONFR1_NF | JPEG_CONFR1_NS);
3205 /* compute MCU block size and Y, Cb ,Cr sampling factors*/
3206 if (hjpeg->Conf.ChromaSubsampling == JPEG_420_SUBSAMPLING)
3208 ySamplingH = JPEG_CONFR4_HSF_1; /* Hs = 2*/
3209 ySamplingV = JPEG_CONFR4_VSF_1; /* Vs = 2*/
3211 yblockNb = 0x30; /* 4 blocks of 8x8*/
3213 else if (hjpeg->Conf.ChromaSubsampling == JPEG_422_SUBSAMPLING)
3215 ySamplingH = JPEG_CONFR4_HSF_1; /* Hs = 2*/
3216 ySamplingV = JPEG_CONFR4_VSF_0; /* Vs = 1*/
3218 yblockNb = 0x10; /* 2 blocks of 8x8*/
3220 else /*JPEG_444_SUBSAMPLING and default*/
3222 ySamplingH = JPEG_CONFR4_HSF_0; /* Hs = 1*/
3223 ySamplingV = JPEG_CONFR4_VSF_0; /* Vs = 1*/
3225 yblockNb = 0; /* 1 block of 8x8*/
3228 /*Reset CONFR4 register*/
3229 hjpeg->Instance->CONFR4 = 0;
3230 /*Set Horizental and Vertical sampling factor , number of blocks , Quantization table and Huffman AC/DC tables for component 0*/
3231 hjpeg->Instance->CONFR4 |= (ySamplingH | ySamplingV | (yblockNb & JPEG_CONFR4_NB));
3233 /*Reset CONFR5 register*/
3234 hjpeg->Instance->CONFR5 = 0;
3235 /*Set Horizental and Vertical sampling factor , number of blocks , Quantization table and Huffman AC/DC tables for component 1*/
3236 hjpeg->Instance->CONFR5 |= (JPEG_CONFR5_HSF_0 | JPEG_CONFR5_VSF_0);
3238 /*Reset CONFR6 register*/
3239 hjpeg->Instance->CONFR6 = 0;
3240 /*Set Horizental and Vertical sampling factor , number of blocks , Quantization table and Huffman AC/DC tables for component 2*/
3241 hjpeg->Instance->CONFR6 |= (JPEG_CONFR6_HSF_0 | JPEG_CONFR6_VSF_0);
3243 /*Reset CONFR7 register*/
3244 hjpeg->Instance->CONFR7 = 0;
3245 /*Set Horizental and Vertical sampling factor , number of blocks , Quantization table and Huffman AC/DC tables for component 3*/
3246 hjpeg->Instance->CONFR7 |= (JPEG_CONFR7_HSF_0 | JPEG_CONFR7_VSF_0);
3250 * @brief Init the JPEG encoding/decoding process in case of Polling or Interrupt and DMA
3251 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
3252 * the configuration information for JPEG module
3253 * @retval None
3255 static void JPEG_Init_Process(JPEG_HandleTypeDef *hjpeg)
3257 /*Reset pause*/
3258 hjpeg->Context &= (~(JPEG_CONTEXT_PAUSE_INPUT | JPEG_CONTEXT_PAUSE_OUTPUT));
3260 if ((hjpeg->Context & JPEG_CONTEXT_OPERATION_MASK) == JPEG_CONTEXT_DECODE)
3262 /*Set JPEG Codec to Decoding mode */
3263 hjpeg->Instance->CONFR1 |= JPEG_CONFR1_DE;
3265 else /* JPEG_CONTEXT_ENCODE */
3267 /*Set JPEG Codec to Encoding mode */
3268 hjpeg->Instance->CONFR1 &= ~JPEG_CONFR1_DE;
3271 /*Stop JPEG processing */
3272 hjpeg->Instance->CONFR0 &= ~JPEG_CONFR0_START;
3274 /* Disable All Interrupts */
3275 __HAL_JPEG_DISABLE_IT(hjpeg, JPEG_INTERRUPT_MASK);
3277 /* Flush input and output FIFOs*/
3278 hjpeg->Instance->CR |= JPEG_CR_IFF;
3279 hjpeg->Instance->CR |= JPEG_CR_OFF;
3281 /* Clear all flags */
3282 __HAL_JPEG_CLEAR_FLAG(hjpeg, JPEG_FLAG_ALL);
3284 /*Start Encoding/Decoding*/
3285 hjpeg->Instance->CONFR0 |= JPEG_CONFR0_START;
3287 if ((hjpeg->Context & JPEG_CONTEXT_METHOD_MASK) == JPEG_CONTEXT_IT)
3289 /*Enable IN/OUT, end of Conversation, and end of header parsing interruptions*/
3290 __HAL_JPEG_ENABLE_IT(hjpeg, JPEG_IT_IFT | JPEG_IT_IFNF | JPEG_IT_OFT | JPEG_IT_OFNE | JPEG_IT_EOC | JPEG_IT_HPD);
3292 else if ((hjpeg->Context & JPEG_CONTEXT_METHOD_MASK) == JPEG_CONTEXT_DMA)
3294 /*Enable End Of Conversation, and End Of Header parsing interruptions*/
3295 __HAL_JPEG_ENABLE_IT(hjpeg, JPEG_IT_EOC | JPEG_IT_HPD);
3298 else
3300 /* Nothing to do */
3305 * @brief JPEG encoding/decoding process in case of Polling or Interrupt
3306 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
3307 * the configuration information for JPEG module
3308 * @retval JPEG_PROCESS_DONE if the process has ends else JPEG_PROCESS_ONGOING
3310 static uint32_t JPEG_Process(JPEG_HandleTypeDef *hjpeg)
3312 uint32_t tmpContext;
3314 /*End of header processing flag rised*/
3315 if ((hjpeg->Context & JPEG_CONTEXT_OPERATION_MASK) == JPEG_CONTEXT_DECODE)
3317 if (__HAL_JPEG_GET_FLAG(hjpeg, JPEG_FLAG_HPDF) != 0UL)
3319 /*Call Header parsing complet callback */
3320 (void) HAL_JPEG_GetInfo(hjpeg, &hjpeg->Conf);
3321 /* Reset the ImageQuality */
3322 hjpeg->Conf.ImageQuality = 0;
3323 /* Note : the image quality is only available at the end of the decoding operation */
3324 /* at the current stage the calculated image quality is not correct so reset it */
3326 /*Call Info Ready callback */
3327 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
3328 hjpeg->InfoReadyCallback(hjpeg, &hjpeg->Conf);
3329 #else
3330 HAL_JPEG_InfoReadyCallback(hjpeg, &hjpeg->Conf);
3331 #endif /* USE_HAL_JPEG_REGISTER_CALLBACKS */
3333 __HAL_JPEG_DISABLE_IT(hjpeg, JPEG_IT_HPD);
3335 /* Clear header processing done flag */
3336 __HAL_JPEG_CLEAR_FLAG(hjpeg, JPEG_FLAG_HPDF);
3340 /*Input FIFO status handling*/
3341 if ((hjpeg->Context & JPEG_CONTEXT_PAUSE_INPUT) == 0UL)
3343 if (__HAL_JPEG_GET_FLAG(hjpeg, JPEG_FLAG_IFTF) != 0UL)
3345 /*Input FIFO threshold flag rised*/
3346 /*JPEG_FIFO_TH_SIZE words can be written in */
3347 JPEG_ReadInputData(hjpeg, JPEG_FIFO_TH_SIZE);
3349 else if (__HAL_JPEG_GET_FLAG(hjpeg, JPEG_FLAG_IFNFF) != 0UL)
3351 /*Input FIFO Not Full flag rised*/
3352 /*32-bit value can be written in */
3353 JPEG_ReadInputData(hjpeg, 1);
3355 else
3357 /* Nothing to do */
3362 /*Output FIFO flag handling*/
3363 if ((hjpeg->Context & JPEG_CONTEXT_PAUSE_OUTPUT) == 0UL)
3365 if (__HAL_JPEG_GET_FLAG(hjpeg, JPEG_FLAG_OFTF) != 0UL)
3367 /*Output FIFO threshold flag rised*/
3368 /*JPEG_FIFO_TH_SIZE words can be read out */
3369 JPEG_StoreOutputData(hjpeg, JPEG_FIFO_TH_SIZE);
3371 else if (__HAL_JPEG_GET_FLAG(hjpeg, JPEG_FLAG_OFNEF) != 0UL)
3373 /*Output FIFO Not Empty flag rised*/
3374 /*32-bit value can be read out */
3375 JPEG_StoreOutputData(hjpeg, 1);
3377 else
3379 /* Nothing to do */
3383 /*End of Conversion handling :i.e EOC flag is high and OFTF low and OFNEF low*/
3384 if (__HAL_JPEG_GET_FLAG(hjpeg, JPEG_FLAG_EOCF | JPEG_FLAG_OFTF | JPEG_FLAG_OFNEF) == JPEG_FLAG_EOCF)
3386 /*Stop Encoding/Decoding*/
3387 hjpeg->Instance->CONFR0 &= ~JPEG_CONFR0_START;
3389 if ((hjpeg->Context & JPEG_CONTEXT_METHOD_MASK) == JPEG_CONTEXT_IT)
3391 /* Disable All Interrupts */
3392 __HAL_JPEG_DISABLE_IT(hjpeg, JPEG_INTERRUPT_MASK);
3395 /* Clear all flags */
3396 __HAL_JPEG_CLEAR_FLAG(hjpeg, JPEG_FLAG_ALL);
3398 /*Call End of conversion callback */
3399 if (hjpeg->JpegOutCount > 0UL)
3401 /*Output Buffer is not empty, call DecodedDataReadyCallback*/
3402 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
3403 hjpeg->DataReadyCallback(hjpeg, hjpeg->pJpegOutBuffPtr, hjpeg->JpegOutCount);
3404 #else
3405 HAL_JPEG_DataReadyCallback(hjpeg, hjpeg->pJpegOutBuffPtr, hjpeg->JpegOutCount);
3406 #endif /* USE_HAL_JPEG_REGISTER_CALLBACKS */
3408 hjpeg->JpegOutCount = 0;
3411 /*Reset Context Operation*/
3412 tmpContext = hjpeg->Context;
3413 /*Clear all context fields execpt JPEG_CONTEXT_CONF_ENCODING and JPEG_CONTEXT_CUSTOM_TABLES*/
3414 hjpeg->Context &= (JPEG_CONTEXT_CONF_ENCODING | JPEG_CONTEXT_CUSTOM_TABLES);
3416 /* Process Unlocked */
3417 __HAL_UNLOCK(hjpeg);
3419 /* Change the JPEG state */
3420 hjpeg->State = HAL_JPEG_STATE_READY;
3422 /*Call End of Encoding/Decoding callback */
3423 if ((tmpContext & JPEG_CONTEXT_OPERATION_MASK) == JPEG_CONTEXT_DECODE)
3425 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
3426 hjpeg->DecodeCpltCallback(hjpeg);
3427 #else
3428 HAL_JPEG_DecodeCpltCallback(hjpeg);
3429 #endif /*USE_HAL_JPEG_REGISTER_CALLBACKS*/
3431 else /* JPEG_CONTEXT_ENCODE */
3433 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
3434 hjpeg->EncodeCpltCallback(hjpeg);
3435 #else
3436 HAL_JPEG_EncodeCpltCallback(hjpeg);
3437 #endif
3440 return JPEG_PROCESS_DONE;
3444 return JPEG_PROCESS_ONGOING;
3448 * @brief Store some output data from the JPEG peripheral to the output buffer.
3449 * This function is used when the JPEG peripheral has new data to output
3450 * in case of Polling or Interrupt process
3451 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
3452 * the configuration information for JPEG module
3453 * @param nbOutputWords Number of output words (of 32 bits) ready from the JPEG peripheral
3454 * @retval None
3456 static void JPEG_StoreOutputData(JPEG_HandleTypeDef *hjpeg, uint32_t nbOutputWords)
3458 uint32_t index, nBwords, nbBytes, dataword;
3460 if (hjpeg->OutDataLength >= (hjpeg->JpegOutCount + (nbOutputWords * 4UL)))
3462 for (index = 0; index < nbOutputWords; index++)
3464 /*Transfer 32 bits from the JPEG output FIFO*/
3465 dataword = hjpeg->Instance->DOR;
3466 hjpeg->pJpegOutBuffPtr[hjpeg->JpegOutCount] = (uint8_t)(dataword & 0x000000FFUL);
3467 hjpeg->pJpegOutBuffPtr[hjpeg->JpegOutCount + 1UL] = (uint8_t)((dataword & 0x0000FF00UL) >> 8);
3468 hjpeg->pJpegOutBuffPtr[hjpeg->JpegOutCount + 2UL] = (uint8_t)((dataword & 0x00FF0000UL) >> 16);
3469 hjpeg->pJpegOutBuffPtr[hjpeg->JpegOutCount + 3UL] = (uint8_t)((dataword & 0xFF000000UL) >> 24);
3470 hjpeg->JpegOutCount += 4UL;
3472 if (hjpeg->OutDataLength == hjpeg->JpegOutCount)
3474 /*Output Buffer is full, call DecodedDataReadyCallback*/
3475 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
3476 hjpeg->DataReadyCallback(hjpeg, hjpeg->pJpegOutBuffPtr, hjpeg->JpegOutCount);
3477 #else
3478 HAL_JPEG_DataReadyCallback(hjpeg, hjpeg->pJpegOutBuffPtr, hjpeg->JpegOutCount);
3479 #endif /*USE_HAL_JPEG_REGISTER_CALLBACKS*/
3480 hjpeg->JpegOutCount = 0;
3483 else if (hjpeg->OutDataLength > hjpeg->JpegOutCount)
3485 nBwords = (hjpeg->OutDataLength - hjpeg->JpegOutCount) / 4UL;
3486 for (index = 0; index < nBwords; index++)
3488 /*Transfer 32 bits from the JPEG output FIFO*/
3489 dataword = hjpeg->Instance->DOR;
3490 hjpeg->pJpegOutBuffPtr[hjpeg->JpegOutCount] = (uint8_t)(dataword & 0x000000FFUL);
3491 hjpeg->pJpegOutBuffPtr[hjpeg->JpegOutCount + 1UL] = (uint8_t)((dataword & 0x0000FF00UL) >> 8);
3492 hjpeg->pJpegOutBuffPtr[hjpeg->JpegOutCount + 2UL] = (uint8_t)((dataword & 0x00FF0000UL) >> 16);
3493 hjpeg->pJpegOutBuffPtr[hjpeg->JpegOutCount + 3UL] = (uint8_t)((dataword & 0xFF000000UL) >> 24);
3494 hjpeg->JpegOutCount += 4UL;
3496 if (hjpeg->OutDataLength == hjpeg->JpegOutCount)
3498 /*Output Buffer is full, call DecodedDataReadyCallback*/
3499 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
3500 hjpeg->DataReadyCallback(hjpeg, hjpeg->pJpegOutBuffPtr, hjpeg->JpegOutCount);
3501 #else
3502 HAL_JPEG_DataReadyCallback(hjpeg, hjpeg->pJpegOutBuffPtr, hjpeg->JpegOutCount);
3503 #endif /* USE_HAL_JPEG_REGISTER_CALLBACKS */
3504 hjpeg->JpegOutCount = 0;
3506 else
3508 nbBytes = hjpeg->OutDataLength - hjpeg->JpegOutCount;
3509 dataword = hjpeg->Instance->DOR;
3510 for (index = 0; index < nbBytes; index++)
3512 hjpeg->pJpegOutBuffPtr[hjpeg->JpegOutCount] = (uint8_t)((dataword >> (8UL * (index & 0x3UL))) & 0xFFUL);
3513 hjpeg->JpegOutCount++;
3515 /*Output Buffer is full, call DecodedDataReadyCallback*/
3516 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
3517 hjpeg->DataReadyCallback(hjpeg, hjpeg->pJpegOutBuffPtr, hjpeg->JpegOutCount);
3518 #else
3519 HAL_JPEG_DataReadyCallback(hjpeg, hjpeg->pJpegOutBuffPtr, hjpeg->JpegOutCount);
3520 #endif /* USE_HAL_JPEG_REGISTER_CALLBACKS */
3522 hjpeg->JpegOutCount = 0;
3524 nbBytes = 4UL - nbBytes;
3525 for (index = nbBytes; index < 4UL; index++)
3527 hjpeg->pJpegOutBuffPtr[hjpeg->JpegOutCount] = (uint8_t)((dataword >> (8UL * index)) & 0xFFUL);
3528 hjpeg->JpegOutCount++;
3532 else
3534 /* Nothing to do */
3539 * @brief Read some input Data from the input buffer.
3540 * This function is used when the JPEG peripheral needs new data
3541 * in case of Polling or Interrupt process
3542 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
3543 * the configuration information for JPEG module
3544 * @param nbRequestWords Number of input words (of 32 bits) that the JPE peripheral request
3545 * @retval None
3547 static void JPEG_ReadInputData(JPEG_HandleTypeDef *hjpeg, uint32_t nbRequestWords)
3549 uint32_t nbBytes = 0, nBwords, index, Dataword, inputCount;
3551 if ((hjpeg->InDataLength == 0UL) || (nbRequestWords == 0UL))
3553 /* No more Input data : nothing to do*/
3554 (void) HAL_JPEG_Pause(hjpeg, JPEG_PAUSE_RESUME_INPUT);
3556 else if (hjpeg->InDataLength > hjpeg->JpegInCount)
3558 nbBytes = hjpeg->InDataLength - hjpeg->JpegInCount;
3560 else if (hjpeg->InDataLength == hjpeg->JpegInCount)
3562 /*Call HAL_JPEG_GetDataCallback to get new data */
3563 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
3564 hjpeg->GetDataCallback(hjpeg, hjpeg->JpegInCount);
3565 #else
3566 HAL_JPEG_GetDataCallback(hjpeg, hjpeg->JpegInCount);
3567 #endif /*USE_HAL_JPEG_REGISTER_CALLBACKS*/
3569 if (hjpeg->InDataLength > 4UL)
3571 hjpeg->InDataLength = hjpeg->InDataLength - (hjpeg->InDataLength % 4UL);
3573 hjpeg->JpegInCount = 0;
3574 nbBytes = hjpeg->InDataLength;
3576 else
3578 /* Nothing to do */
3580 if (((hjpeg->Context & JPEG_CONTEXT_PAUSE_INPUT) == 0UL) && (nbBytes > 0UL))
3582 nBwords = nbBytes / 4UL;
3583 if (nBwords >= nbRequestWords)
3585 for (index = 0; index < nbRequestWords; index++)
3587 inputCount = hjpeg->JpegInCount;
3588 hjpeg->Instance->DIR = (((uint32_t)(hjpeg->pJpegInBuffPtr[inputCount])) | \
3589 (((uint32_t)(hjpeg->pJpegInBuffPtr[inputCount + 1UL])) << 8) | \
3590 (((uint32_t)(hjpeg->pJpegInBuffPtr[inputCount + 2UL])) << 16) | \
3591 (((uint32_t)(hjpeg->pJpegInBuffPtr[inputCount + 3UL])) << 24));
3593 hjpeg->JpegInCount += 4UL;
3596 else /*nBwords < nbRequestWords*/
3598 if (nBwords > 0UL)
3600 for (index = 0; index < nBwords; index++)
3602 inputCount = hjpeg->JpegInCount;
3603 hjpeg->Instance->DIR = (((uint32_t)(hjpeg->pJpegInBuffPtr[inputCount])) | \
3604 (((uint32_t)(hjpeg->pJpegInBuffPtr[inputCount + 1UL])) << 8) | \
3605 (((uint32_t)(hjpeg->pJpegInBuffPtr[inputCount + 2UL])) << 16) | \
3606 (((uint32_t)(hjpeg->pJpegInBuffPtr[inputCount + 3UL])) << 24));
3608 hjpeg->JpegInCount += 4UL;
3611 else
3613 /* end of file*/
3614 Dataword = 0;
3615 for (index = 0; index < nbBytes; index++)
3617 Dataword |= (uint32_t)hjpeg->pJpegInBuffPtr[hjpeg->JpegInCount] << (8UL * (index & 0x03UL));
3618 hjpeg->JpegInCount++;
3620 hjpeg->Instance->DIR = Dataword;
3627 * @brief Start the JPEG DMA process (encoding/decoding)
3628 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
3629 * the configuration information for JPEG module
3630 * @retval JPEG_PROCESS_DONE if process ends else JPEG_PROCESS_ONGOING
3632 static HAL_StatusTypeDef JPEG_DMA_StartProcess(JPEG_HandleTypeDef *hjpeg)
3634 uint32_t inXfrSize, outXfrSize;
3636 /*if the MDMA In is triggred with JPEG In FIFO Threshold flag
3637 then MDMA In buffer size is 32 bytes
3638 else (MDMA In is triggred with JPEG In FIFO not full flag)
3639 then MDMA In buffer size is 4 bytes
3641 inXfrSize = hjpeg->hdmain->Init.BufferTransferLength;
3643 /*if the MDMA Out is triggred with JPEG Out FIFO Threshold flag
3644 then MDMA out buffer size is 32 bytes
3645 else (MDMA Out is triggred with JPEG Out FIFO not empty flag)
3646 then MDMA buffer size is 4 bytes
3648 outXfrSize = hjpeg->hdmaout->Init.BufferTransferLength;
3650 if ((hjpeg->InDataLength < inXfrSize) || (hjpeg->OutDataLength < outXfrSize))
3652 return HAL_ERROR;
3654 /* Set the JPEG MDMA In transfer complete callback */
3655 hjpeg->hdmain->XferCpltCallback = JPEG_MDMAInCpltCallback;
3656 /* Set the MDMA In error callback */
3657 hjpeg->hdmain->XferErrorCallback = JPEG_MDMAErrorCallback;
3659 /* Set the JPEG MDMA Out transfer complete callback */
3660 hjpeg->hdmaout->XferCpltCallback = JPEG_MDMAOutCpltCallback;
3661 /* Set the MDMA In error callback */
3662 hjpeg->hdmaout->XferErrorCallback = JPEG_MDMAErrorCallback;
3663 /* Set the MDMA Out Abort callback */
3664 hjpeg->hdmaout->XferAbortCallback = JPEG_MDMAOutAbortCallback;
3666 if ((inXfrSize == 0UL) || (outXfrSize == 0UL))
3668 hjpeg->ErrorCode |= HAL_JPEG_ERROR_DMA;
3669 return HAL_ERROR;
3671 /*MDMA transfer size (BNDTR) must be a multiple of MDMA buffer size (TLEN)*/
3672 hjpeg->InDataLength = hjpeg->InDataLength - (hjpeg->InDataLength % inXfrSize);
3674 /*MDMA transfer size (BNDTR) must be a multiple of MDMA buffer size (TLEN)*/
3675 hjpeg->OutDataLength = hjpeg->OutDataLength - (hjpeg->OutDataLength % outXfrSize);
3678 /* Start MDMA FIFO Out transfer */
3679 if (HAL_MDMA_Start_IT(hjpeg->hdmaout, (uint32_t)&hjpeg->Instance->DOR, (uint32_t)hjpeg->pJpegOutBuffPtr,
3680 hjpeg->OutDataLength, 1) != HAL_OK)
3682 hjpeg->ErrorCode |= HAL_JPEG_ERROR_DMA;
3683 return HAL_ERROR;
3685 /* Start DMA FIFO In transfer */
3686 if (HAL_MDMA_Start_IT(hjpeg->hdmain, (uint32_t)hjpeg->pJpegInBuffPtr, (uint32_t)&hjpeg->Instance->DIR,
3687 hjpeg->InDataLength, 1) != HAL_OK)
3689 hjpeg->ErrorCode |= HAL_JPEG_ERROR_DMA;
3690 return HAL_ERROR;
3693 return HAL_OK;
3697 * @brief Continue the current JPEG DMA process (encoding/decoding)
3698 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
3699 * the configuration information for JPEG module
3700 * @retval JPEG_PROCESS_DONE if process ends else JPEG_PROCESS_ONGOING
3702 static void JPEG_DMA_ContinueProcess(JPEG_HandleTypeDef *hjpeg)
3704 /*End of header processing flag rises*/
3705 if ((hjpeg->Context & JPEG_CONTEXT_OPERATION_MASK) == JPEG_CONTEXT_DECODE)
3707 if (__HAL_JPEG_GET_FLAG(hjpeg, JPEG_FLAG_HPDF) != 0UL)
3709 /*Call Header parsing complete callback */
3710 (void) HAL_JPEG_GetInfo(hjpeg, &hjpeg->Conf);
3712 /* Reset the ImageQuality */
3713 hjpeg->Conf.ImageQuality = 0;
3714 /* Note : the image quality is only available at the end of the decoding operation */
3715 /* at the current stage the calculated image quality is not correct so reset it */
3717 /*Call Info Ready callback */
3718 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
3719 hjpeg->InfoReadyCallback(hjpeg, &hjpeg->Conf);
3720 #else
3721 HAL_JPEG_InfoReadyCallback(hjpeg, &hjpeg->Conf);
3722 #endif /* USE_HAL_JPEG_REGISTER_CALLBACKS */
3724 __HAL_JPEG_DISABLE_IT(hjpeg, JPEG_IT_HPD);
3726 /* Clear header processing done flag */
3727 __HAL_JPEG_CLEAR_FLAG(hjpeg, JPEG_FLAG_HPDF);
3731 /*End of Conversion handling*/
3732 if (__HAL_JPEG_GET_FLAG(hjpeg, JPEG_FLAG_EOCF) != 0UL)
3735 hjpeg->Context |= JPEG_CONTEXT_ENDING_DMA;
3737 /*Stop Encoding/Decoding*/
3738 hjpeg->Instance->CONFR0 &= ~JPEG_CONFR0_START;
3740 __HAL_JPEG_DISABLE_IT(hjpeg, JPEG_INTERRUPT_MASK);
3742 /* Clear all flags */
3743 __HAL_JPEG_CLEAR_FLAG(hjpeg, JPEG_FLAG_ALL);
3745 if (hjpeg->hdmain->State == HAL_MDMA_STATE_BUSY)
3747 /* Stop the MDMA In Xfer*/
3748 (void) HAL_MDMA_Abort_IT(hjpeg->hdmain);
3751 if (hjpeg->hdmaout->State == HAL_MDMA_STATE_BUSY)
3753 /* Stop the MDMA out Xfer*/
3754 (void) HAL_MDMA_Abort_IT(hjpeg->hdmaout);
3756 else
3758 JPEG_DMA_EndProcess(hjpeg);
3766 * @brief Finalize the current JPEG DMA process (encoding/decoding)
3767 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
3768 * the configuration information for JPEG module
3769 * @retval JPEG_PROCESS_DONE
3771 static void JPEG_DMA_EndProcess(JPEG_HandleTypeDef *hjpeg)
3773 uint32_t tmpContext;
3774 hjpeg->JpegOutCount = hjpeg->OutDataLength - (hjpeg->hdmaout->Instance->CBNDTR & MDMA_CBNDTR_BNDT);
3776 /*if Output Buffer is full, call HAL_JPEG_DataReadyCallback*/
3777 if (hjpeg->JpegOutCount == hjpeg->OutDataLength)
3779 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
3780 hjpeg->DataReadyCallback(hjpeg, hjpeg->pJpegOutBuffPtr, hjpeg->JpegOutCount);
3781 #else
3782 HAL_JPEG_DataReadyCallback(hjpeg, hjpeg->pJpegOutBuffPtr, hjpeg->JpegOutCount);
3783 #endif /* USE_HAL_JPEG_REGISTER_CALLBACKS */
3785 hjpeg->JpegOutCount = 0;
3788 /*Check if remaining data in the output FIFO*/
3789 if (__HAL_JPEG_GET_FLAG(hjpeg, JPEG_FLAG_OFNEF) == 0UL)
3791 if (hjpeg->JpegOutCount > 0UL)
3793 /*Output Buffer is not empty, call DecodedDataReadyCallback*/
3794 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
3795 hjpeg->DataReadyCallback(hjpeg, hjpeg->pJpegOutBuffPtr, hjpeg->JpegOutCount);
3796 #else
3797 HAL_JPEG_DataReadyCallback(hjpeg, hjpeg->pJpegOutBuffPtr, hjpeg->JpegOutCount);
3798 #endif /* USE_HAL_JPEG_REGISTER_CALLBACKS */
3800 hjpeg->JpegOutCount = 0;
3803 /*Stop Encoding/Decoding*/
3804 hjpeg->Instance->CONFR0 &= ~JPEG_CONFR0_START;
3806 tmpContext = hjpeg->Context;
3807 /*Clear all context fileds execpt JPEG_CONTEXT_CONF_ENCODING and JPEG_CONTEXT_CUSTOM_TABLES*/
3808 hjpeg->Context &= (JPEG_CONTEXT_CONF_ENCODING | JPEG_CONTEXT_CUSTOM_TABLES);
3810 /* Process Unlocked */
3811 __HAL_UNLOCK(hjpeg);
3813 /* Change the JPEG state */
3814 hjpeg->State = HAL_JPEG_STATE_READY;
3816 /*Call End of Encoding/Decoding callback */
3817 if ((tmpContext & JPEG_CONTEXT_OPERATION_MASK) == JPEG_CONTEXT_DECODE)
3819 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
3820 hjpeg->DecodeCpltCallback(hjpeg);
3821 #else
3822 HAL_JPEG_DecodeCpltCallback(hjpeg);
3823 #endif /* USE_HAL_JPEG_REGISTER_CALLBACKS */
3825 else /* JPEG_CONTEXT_ENCODE */
3827 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
3828 hjpeg->EncodeCpltCallback(hjpeg);
3829 #else
3830 HAL_JPEG_EncodeCpltCallback(hjpeg);
3831 #endif /* USE_HAL_JPEG_REGISTER_CALLBACKS */
3834 else if ((hjpeg->Context & JPEG_CONTEXT_PAUSE_OUTPUT) == 0UL)
3836 JPEG_DMA_PollResidualData(hjpeg);
3838 else
3840 /* Nothing to do */
3846 * @brief Poll residual output data when DMA process (encoding/decoding)
3847 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
3848 * the configuration information for JPEG module
3849 * @retval None.
3851 static void JPEG_DMA_PollResidualData(JPEG_HandleTypeDef *hjpeg)
3853 uint32_t tmpContext, count, dataOut;
3855 for (count = JPEG_FIFO_SIZE; count > 0UL; count--)
3857 if ((hjpeg->Context & JPEG_CONTEXT_PAUSE_OUTPUT) == 0UL)
3859 if (__HAL_JPEG_GET_FLAG(hjpeg, JPEG_FLAG_OFNEF) != 0UL)
3861 dataOut = hjpeg->Instance->DOR;
3862 hjpeg->pJpegOutBuffPtr[hjpeg->JpegOutCount] = (uint8_t)(dataOut & 0x000000FFUL);
3863 hjpeg->pJpegOutBuffPtr[hjpeg->JpegOutCount + 1UL] = (uint8_t)((dataOut & 0x0000FF00UL) >> 8);
3864 hjpeg->pJpegOutBuffPtr[hjpeg->JpegOutCount + 2UL] = (uint8_t)((dataOut & 0x00FF0000UL) >> 16);
3865 hjpeg->pJpegOutBuffPtr[hjpeg->JpegOutCount + 3UL] = (uint8_t)((dataOut & 0xFF000000UL) >> 24);
3866 hjpeg->JpegOutCount += 4UL;
3868 if (hjpeg->JpegOutCount == hjpeg->OutDataLength)
3870 /*Output Buffer is full, call HAL_JPEG_DataReadyCallback*/
3871 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
3872 hjpeg->DataReadyCallback(hjpeg, hjpeg->pJpegOutBuffPtr, hjpeg->JpegOutCount);
3873 #else
3874 HAL_JPEG_DataReadyCallback(hjpeg, hjpeg->pJpegOutBuffPtr, hjpeg->JpegOutCount);
3875 #endif /* USE_HAL_JPEG_REGISTER_CALLBACKS */
3877 hjpeg->JpegOutCount = 0;
3884 tmpContext = hjpeg->Context;
3886 if ((__HAL_JPEG_GET_FLAG(hjpeg, JPEG_FLAG_OFNEF) == 0UL) || ((tmpContext & JPEG_CONTEXT_PAUSE_OUTPUT) == 0UL))
3888 /*Stop Encoding/Decoding*/
3889 hjpeg->Instance->CONFR0 &= ~JPEG_CONFR0_START;
3891 if (hjpeg->JpegOutCount > 0UL)
3893 /*Output Buffer is not empty, call DecodedDataReadyCallback*/
3894 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
3895 hjpeg->DataReadyCallback(hjpeg, hjpeg->pJpegOutBuffPtr, hjpeg->JpegOutCount);
3896 #else
3897 HAL_JPEG_DataReadyCallback(hjpeg, hjpeg->pJpegOutBuffPtr, hjpeg->JpegOutCount);
3898 #endif /* USE_HAL_JPEG_REGISTER_CALLBACKS */
3900 hjpeg->JpegOutCount = 0;
3903 tmpContext = hjpeg->Context;
3904 /*Clear all context fileds execpt JPEG_CONTEXT_CONF_ENCODING and JPEG_CONTEXT_CUSTOM_TABLES*/
3905 hjpeg->Context &= (JPEG_CONTEXT_CONF_ENCODING | JPEG_CONTEXT_CUSTOM_TABLES);
3907 /* Process Unlocked */
3908 __HAL_UNLOCK(hjpeg);
3910 /* Change the JPEG state */
3911 hjpeg->State = HAL_JPEG_STATE_READY;
3913 /*Call End of Encoding/Decoding callback */
3914 if ((tmpContext & JPEG_CONTEXT_OPERATION_MASK) == JPEG_CONTEXT_DECODE)
3916 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
3917 hjpeg->DecodeCpltCallback(hjpeg);
3918 #else
3919 HAL_JPEG_DecodeCpltCallback(hjpeg);
3920 #endif /* USE_HAL_JPEG_REGISTER_CALLBACKS */
3922 else /* JPEG_CONTEXT_ENCODE */
3924 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
3925 hjpeg->EncodeCpltCallback(hjpeg);
3926 #else
3927 HAL_JPEG_EncodeCpltCallback(hjpeg);
3928 #endif /* USE_HAL_JPEG_REGISTER_CALLBACKS */
3934 * @brief MDMA input transfer complete callback
3935 * @param hmdma pointer to a MDMA_HandleTypeDef structure.
3936 * @retval None
3938 static void JPEG_MDMAInCpltCallback(MDMA_HandleTypeDef *hmdma)
3940 uint32_t inXfrSize;
3942 JPEG_HandleTypeDef *hjpeg = (JPEG_HandleTypeDef *)((MDMA_HandleTypeDef *)hmdma)->Parent;
3944 /* Disable The JPEG IT so the MDMA Input Callback can not be interrupted by the JPEG EOC IT or JPEG HPD IT */
3945 __HAL_JPEG_DISABLE_IT(hjpeg, JPEG_INTERRUPT_MASK);
3947 /* Check if context method is DMA and we are not in ending DMA stage */
3948 if ((hjpeg->Context & (JPEG_CONTEXT_METHOD_MASK | JPEG_CONTEXT_ENDING_DMA)) == JPEG_CONTEXT_DMA)
3951 /*if the MDMA In is triggred with JPEG In FIFO Threshold flag
3952 then MDMA In buffer size is 32 bytes
3953 else (MDMA In is triggred with JPEG In FIFO not full flag)
3954 then MDMA In buffer size is 4 bytes
3956 inXfrSize = hjpeg->hdmain->Init.BufferTransferLength;
3958 hjpeg->JpegInCount = hjpeg->InDataLength - (hmdma->Instance->CBNDTR & MDMA_CBNDTR_BNDT);
3960 /*Call HAL_JPEG_GetDataCallback to get new data */
3961 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
3962 hjpeg->GetDataCallback(hjpeg, hjpeg->JpegInCount);
3963 #else
3964 HAL_JPEG_GetDataCallback(hjpeg, hjpeg->JpegInCount);
3965 #endif /* USE_HAL_JPEG_REGISTER_CALLBACKS */
3968 if (hjpeg->InDataLength >= inXfrSize)
3970 if (inXfrSize == 0UL)
3972 hjpeg->ErrorCode |= HAL_JPEG_ERROR_DMA;
3973 hjpeg->State = HAL_JPEG_STATE_ERROR;
3974 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
3975 hjpeg->ErrorCallback(hjpeg);
3976 #else
3977 HAL_JPEG_ErrorCallback(hjpeg);
3978 #endif /* USE_HAL_JPEG_REGISTER_CALLBACKS */
3979 return;
3981 /*JPEG Input MDMA transfer data number must be multiple of MDMA buffer size
3982 as the destination is a 32 bits register */
3983 hjpeg->InDataLength = hjpeg->InDataLength - (hjpeg->InDataLength % inXfrSize);
3986 else if (hjpeg->InDataLength > 0UL)
3988 /* Transfer the remaining Data, must be multiple of source data size (byte) and destination data size (word) */
3989 if ((hjpeg->InDataLength % 4UL) != 0UL)
3991 hjpeg->InDataLength = ((hjpeg->InDataLength / 4UL) + 1UL) * 4UL;
3994 else
3996 /* Nothing to do */
3999 if (((hjpeg->Context & JPEG_CONTEXT_PAUSE_INPUT) == 0UL) && (hjpeg->InDataLength > 0UL))
4001 /* Start MDMA FIFO In transfer */
4002 if (HAL_MDMA_Start_IT(hjpeg->hdmain, (uint32_t)hjpeg->pJpegInBuffPtr, (uint32_t)&hjpeg->Instance->DIR,
4003 hjpeg->InDataLength, 1) != HAL_OK)
4005 hjpeg->ErrorCode |= HAL_JPEG_ERROR_DMA;
4006 hjpeg->State = HAL_JPEG_STATE_ERROR;
4007 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
4008 hjpeg->ErrorCallback(hjpeg);
4009 #else
4010 HAL_JPEG_ErrorCallback(hjpeg);
4011 #endif /* USE_HAL_JPEG_REGISTER_CALLBACKS */
4012 return;
4016 /* JPEG Conversion still on going : Enable the JPEG IT */
4017 __HAL_JPEG_ENABLE_IT(hjpeg, JPEG_IT_EOC | JPEG_IT_HPD);
4022 * @brief MDMA output transfer complete callback
4023 * @param hmdma pointer to a MDMA_HandleTypeDef structure.
4024 * @retval None
4026 static void JPEG_MDMAOutCpltCallback(MDMA_HandleTypeDef *hmdma)
4028 JPEG_HandleTypeDef *hjpeg = (JPEG_HandleTypeDef *)((MDMA_HandleTypeDef *)hmdma)->Parent;
4031 /* Disable The JPEG IT so the MDMA Output Callback can not be interrupted by the JPEG EOC IT or JPEG HPD IT */
4032 __HAL_JPEG_DISABLE_IT(hjpeg, JPEG_INTERRUPT_MASK);
4034 if ((hjpeg->Context & (JPEG_CONTEXT_METHOD_MASK | JPEG_CONTEXT_ENDING_DMA)) ==
4035 JPEG_CONTEXT_DMA) /* Check if context method is DMA and we are not in ending DMA stage */
4037 if (__HAL_JPEG_GET_FLAG(hjpeg, JPEG_FLAG_EOCF) == 0UL)
4039 hjpeg->JpegOutCount = hjpeg->OutDataLength - (hmdma->Instance->CBNDTR & MDMA_CBNDTR_BNDT);
4041 /*Output Buffer is full, call HAL_JPEG_DataReadyCallback*/
4042 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
4043 hjpeg->DataReadyCallback(hjpeg, hjpeg->pJpegOutBuffPtr, hjpeg->JpegOutCount);
4044 #else
4045 HAL_JPEG_DataReadyCallback(hjpeg, hjpeg->pJpegOutBuffPtr, hjpeg->JpegOutCount);
4046 #endif /* USE_HAL_JPEG_REGISTER_CALLBACKS */
4048 if ((hjpeg->Context & JPEG_CONTEXT_PAUSE_OUTPUT) == 0UL)
4050 /* Start MDMA FIFO Out transfer */
4051 if (HAL_MDMA_Start_IT(hjpeg->hdmaout, (uint32_t)&hjpeg->Instance->DOR, (uint32_t)hjpeg->pJpegOutBuffPtr,
4052 hjpeg->OutDataLength, 1) != HAL_OK)
4054 hjpeg->ErrorCode |= HAL_JPEG_ERROR_DMA;
4055 hjpeg->State = HAL_JPEG_STATE_ERROR;
4056 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
4057 hjpeg->ErrorCallback(hjpeg);
4058 #else
4059 HAL_JPEG_ErrorCallback(hjpeg);
4060 #endif /* USE_HAL_JPEG_REGISTER_CALLBACKS */
4061 return;
4066 /* JPEG Conversion still on going : Enable the JPEG IT */
4067 __HAL_JPEG_ENABLE_IT(hjpeg, JPEG_IT_EOC | JPEG_IT_HPD);
4073 * @brief MDMA Transfer error callback
4074 * @param hmdma pointer to a MDMA_HandleTypeDef structure.
4075 * @retval None
4077 static void JPEG_MDMAErrorCallback(MDMA_HandleTypeDef *hmdma)
4079 JPEG_HandleTypeDef *hjpeg = (JPEG_HandleTypeDef *)((MDMA_HandleTypeDef *)hmdma)->Parent;
4081 /*Stop Encoding/Decoding*/
4082 hjpeg->Instance->CONFR0 &= ~JPEG_CONFR0_START;
4084 /* Disable All Interrupts */
4085 __HAL_JPEG_DISABLE_IT(hjpeg, JPEG_INTERRUPT_MASK);
4087 hjpeg->State = HAL_JPEG_STATE_READY;
4088 hjpeg->ErrorCode |= HAL_JPEG_ERROR_DMA;
4090 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
4091 hjpeg->ErrorCallback(hjpeg);
4092 #else
4093 HAL_JPEG_ErrorCallback(hjpeg);
4094 #endif /* USE_HAL_JPEG_REGISTER_CALLBACKS */
4098 * @brief MDMA output Abort callback
4099 * @param hmdma pointer to a MDMA_HandleTypeDef structure.
4100 * @retval None
4102 static void JPEG_MDMAOutAbortCallback(MDMA_HandleTypeDef *hmdma)
4104 JPEG_HandleTypeDef *hjpeg = (JPEG_HandleTypeDef *)((MDMA_HandleTypeDef *)hmdma)->Parent;
4106 if ((hjpeg->Context & JPEG_CONTEXT_ENDING_DMA) != 0UL)
4108 JPEG_DMA_EndProcess(hjpeg);
4114 * @brief Calculate the decoded image quality (from 1 to 100)
4115 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
4116 * the configuration information for JPEG module
4117 * @retval JPEG image quality from 1 to 100.
4119 static uint32_t JPEG_GetQuality(JPEG_HandleTypeDef *hjpeg)
4121 uint32_t quality = 0;
4122 uint32_t quantRow, quantVal, scale, i, j;
4123 __IO uint32_t *tableAddress = hjpeg->Instance->QMEM0;
4125 i = 0;
4126 while (i < (JPEG_QUANT_TABLE_SIZE - 3UL))
4128 quantRow = *tableAddress;
4129 for (j = 0; j < 4UL; j++)
4131 quantVal = (quantRow >> (8UL * j)) & 0xFFUL;
4132 if (quantVal == 1UL)
4134 /* if Quantization value = 1 then quality is 100%*/
4135 quality += 100UL;
4137 else
4139 /* Note that the quantization coefficients must be specified in the table in zigzag order */
4140 scale = (quantVal * 100UL) / ((uint32_t) hjpeg->QuantTable0[JPEG_ZIGZAG_ORDER[i + j]]);
4142 if (scale <= 100UL)
4144 quality += (200UL - scale) / 2UL;
4146 else
4148 quality += 5000UL / scale;
4153 i += 4UL;
4154 tableAddress ++;
4157 return (quality / 64UL);
4160 * @}
4163 #endif /* JPEG */
4164 #endif /* HAL_JPEG_MODULE_ENABLED */
4166 * @}
4170 * @}
4173 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/