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
20 ==============================================================================
21 ##### How to use this driver #####
22 ==============================================================================
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
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
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
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
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
131 (#) To control JPEG state you can use the following function: HAL_JPEG_GetState()
133 *** JPEG HAL driver macros list ***
134 =============================================
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
170 HAL_JPEG_UnRegisterCallback() takes as parameters the HAL peripheral handle,
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.
205 ******************************************************************************
208 * <h2><center>© 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
226 #ifdef HAL_JPEG_MODULE_ENABLED
230 /** @defgroup JPEG JPEG
231 * @brief JPEG HAL module driver.
235 /* Private define ------------------------------------------------------------*/
236 /** @addtogroup JPEG_Private_Constants
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) */
274 /* Private typedef -----------------------------------------------------------*/
275 /** @addtogroup JPEG_Private_Types
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
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
;
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
;
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
;
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
;
322 /* Private macro -------------------------------------------------------------*/
324 /* Private variables ---------------------------------------------------------*/
325 /** @addtogroup JPEG_Private_Variables
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,
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,
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
417 /* Private function prototypes -----------------------------------------------*/
418 /** @addtogroup JPEG_Private_Functions_Prototypes
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
);
458 /** @defgroup JPEG_Exported_Functions JPEG Exported Functions
462 /** @defgroup JPEG_Exported_Functions_Group1 Initialization and de-initialization functions
463 * @brief Initialization and de-initialization functions.
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
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
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 */
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
);
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
;
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*/
598 /* Return function status */
603 * @brief DeInitializes the JPEG peripheral.
604 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
605 * the configuration information for JPEG module
608 HAL_StatusTypeDef
HAL_JPEG_DeInit(JPEG_HandleTypeDef
*hjpeg
)
610 /* Check the JPEG handle allocation */
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
);
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*/
649 /* Return function status */
654 * @brief Initializes the JPEG MSP.
655 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
656 * the configuration information for JPEG module
659 __weak
void HAL_JPEG_MspInit(JPEG_HandleTypeDef
*hjpeg
)
661 /* Prevent unused argument(s) compilation warning */
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
675 __weak
void HAL_JPEG_MspDeInit(JPEG_HandleTypeDef
*hjpeg
)
677 /* Prevent unused argument(s) compilation warning */
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
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
;
714 if (HAL_JPEG_STATE_READY
== hjpeg
->State
)
718 case HAL_JPEG_ENCODE_CPLT_CB_ID
:
719 hjpeg
->EncodeCpltCallback
= pCallback
;
722 case HAL_JPEG_DECODE_CPLT_CB_ID
:
723 hjpeg
->DecodeCpltCallback
= pCallback
;
726 case HAL_JPEG_ERROR_CB_ID
:
727 hjpeg
->ErrorCallback
= pCallback
;
730 case HAL_JPEG_MSPINIT_CB_ID
:
731 hjpeg
->MspInitCallback
= pCallback
;
734 case HAL_JPEG_MSPDEINIT_CB_ID
:
735 hjpeg
->MspDeInitCallback
= pCallback
;
739 /* Update the error code */
740 hjpeg
->ErrorCode
|= HAL_JPEG_ERROR_INVALID_CALLBACK
;
741 /* Return error status */
746 else if (HAL_JPEG_STATE_RESET
== hjpeg
->State
)
750 case HAL_JPEG_MSPINIT_CB_ID
:
751 hjpeg
->MspInitCallback
= pCallback
;
754 case HAL_JPEG_MSPDEINIT_CB_ID
:
755 hjpeg
->MspDeInitCallback
= pCallback
;
759 /* Update the error code */
760 hjpeg
->ErrorCode
|= HAL_JPEG_ERROR_INVALID_CALLBACK
;
761 /* Return error status */
768 /* Update the error code */
769 hjpeg
->ErrorCode
|= HAL_JPEG_ERROR_INVALID_CALLBACK
;
770 /* Return error 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
793 HAL_StatusTypeDef
HAL_JPEG_UnRegisterCallback(JPEG_HandleTypeDef
*hjpeg
, HAL_JPEG_CallbackIDTypeDef CallbackID
)
795 HAL_StatusTypeDef status
= HAL_OK
;
800 if (HAL_JPEG_STATE_READY
== hjpeg
->State
)
804 case HAL_JPEG_ENCODE_CPLT_CB_ID
:
805 hjpeg
->EncodeCpltCallback
= HAL_JPEG_EncodeCpltCallback
; /* Legacy weak EncodeCpltCallback */
808 case HAL_JPEG_DECODE_CPLT_CB_ID
:
809 hjpeg
->DecodeCpltCallback
= HAL_JPEG_DecodeCpltCallback
; /* Legacy weak DecodeCpltCallback */
812 case HAL_JPEG_ERROR_CB_ID
:
813 hjpeg
->ErrorCallback
= HAL_JPEG_ErrorCallback
; /* Legacy weak ErrorCallback */
816 case HAL_JPEG_MSPINIT_CB_ID
:
817 hjpeg
->MspInitCallback
= HAL_JPEG_MspInit
; /* Legacy weak MspInit */
820 case HAL_JPEG_MSPDEINIT_CB_ID
:
821 hjpeg
->MspDeInitCallback
= HAL_JPEG_MspDeInit
; /* Legacy weak MspDeInit */
825 /* Update the error code */
826 hjpeg
->ErrorCode
|= HAL_JPEG_ERROR_INVALID_CALLBACK
;
827 /* Return error status */
832 else if (HAL_JPEG_STATE_RESET
== hjpeg
->State
)
836 case HAL_JPEG_MSPINIT_CB_ID
:
837 hjpeg
->MspInitCallback
= HAL_JPEG_MspInit
; /* Legacy weak MspInit */
840 case HAL_JPEG_MSPDEINIT_CB_ID
:
841 hjpeg
->MspDeInitCallback
= HAL_JPEG_MspDeInit
; /* Legacy weak MspInit */
845 /* Update the error code */
846 hjpeg
->ErrorCode
|= HAL_JPEG_ERROR_INVALID_CALLBACK
;
847 /* Return error status */
854 /* Update the error code */
855 hjpeg
->ErrorCode
|= HAL_JPEG_ERROR_INVALID_CALLBACK
;
856 /* Return error 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
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
;
886 if (HAL_JPEG_STATE_READY
== hjpeg
->State
)
888 hjpeg
->InfoReadyCallback
= pCallback
;
892 /* Update the error code */
893 hjpeg
->ErrorCode
|= HAL_JPEG_ERROR_INVALID_CALLBACK
;
894 /* Return error 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
909 HAL_StatusTypeDef
HAL_JPEG_UnRegisterInfoReadyCallback(JPEG_HandleTypeDef
*hjpeg
)
911 HAL_StatusTypeDef status
= HAL_OK
;
916 if (HAL_JPEG_STATE_READY
== hjpeg
->State
)
918 hjpeg
->InfoReadyCallback
= HAL_JPEG_InfoReadyCallback
; /* Legacy weak InfoReadyCallback */
922 /* Update the error code */
923 hjpeg
->ErrorCode
|= HAL_JPEG_ERROR_INVALID_CALLBACK
;
924 /* Return error 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
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
;
953 if (HAL_JPEG_STATE_READY
== hjpeg
->State
)
955 hjpeg
->GetDataCallback
= pCallback
;
959 /* Update the error code */
960 hjpeg
->ErrorCode
|= HAL_JPEG_ERROR_INVALID_CALLBACK
;
961 /* Return error 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
976 HAL_StatusTypeDef
HAL_JPEG_UnRegisterGetDataCallback(JPEG_HandleTypeDef
*hjpeg
)
978 HAL_StatusTypeDef status
= HAL_OK
;
983 if (HAL_JPEG_STATE_READY
== hjpeg
->State
)
985 hjpeg
->GetDataCallback
= HAL_JPEG_GetDataCallback
; /* Legacy weak GetDataCallback */
989 /* Update the error code */
990 hjpeg
->ErrorCode
|= HAL_JPEG_ERROR_INVALID_CALLBACK
;
991 /* Return error 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
;
1018 /* Process locked */
1021 if (HAL_JPEG_STATE_READY
== hjpeg
->State
)
1023 hjpeg
->DataReadyCallback
= pCallback
;
1027 /* Update the error code */
1028 hjpeg
->ErrorCode
|= HAL_JPEG_ERROR_INVALID_CALLBACK
;
1029 /* Return error status */
1034 __HAL_UNLOCK(hjpeg
);
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 */
1051 if (HAL_JPEG_STATE_READY
== hjpeg
->State
)
1053 hjpeg
->DataReadyCallback
= HAL_JPEG_DataReadyCallback
; /* Legacy weak DataReadyCallback */
1057 /* Update the error code */
1058 hjpeg
->ErrorCode
|= HAL_JPEG_ERROR_INVALID_CALLBACK
;
1059 /* Return error status */
1064 __HAL_UNLOCK(hjpeg
);
1068 #endif /* USE_HAL_JPEG_REGISTER_CALLBACKS */
1074 /** @defgroup JPEG_Exported_Functions_Group2 Configuration functions
1075 * @brief JPEG Configuration functions.
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.
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
)
1109 /* Check the JPEG handle allocation */
1110 if ((hjpeg
== NULL
) || (pConf
== NULL
))
1116 /* Check the parameters */
1117 assert_param(IS_JPEG_COLORSPACE(pConf
->ColorSpace
));
1118 assert_param(IS_JPEG_CHROMASUBSAMPLING(pConf
->ChromaSubsampling
));
1119 assert_param(IS_JPEG_IMAGE_QUALITY(pConf
->ImageQuality
));
1121 /* Process Locked */
1124 if (hjpeg
->State
== HAL_JPEG_STATE_READY
)
1126 hjpeg
->State
= HAL_JPEG_STATE_BUSY
;
1128 hjpeg
->Conf
.ColorSpace
= pConf
->ColorSpace
;
1129 hjpeg
->Conf
.ChromaSubsampling
= pConf
->ChromaSubsampling
;
1130 hjpeg
->Conf
.ImageHeight
= pConf
->ImageHeight
;
1131 hjpeg
->Conf
.ImageWidth
= pConf
->ImageWidth
;
1132 hjpeg
->Conf
.ImageQuality
= pConf
->ImageQuality
;
1134 /* Reset the Color Space : by default only one quantization table is used*/
1135 hjpeg
->Instance
->CONFR1
&= ~JPEG_CONFR1_COLORSPACE
;
1137 /* Set Number of color components*/
1138 if (hjpeg
->Conf
.ColorSpace
== JPEG_GRAYSCALE_COLORSPACE
)
1140 /*Gray Scale is only one component 8x8 blocks i.e 4:4:4*/
1141 hjpeg
->Conf
.ChromaSubsampling
= JPEG_444_SUBSAMPLING
;
1143 JPEG_SetColorGrayScale(hjpeg
);
1144 /* Set quantization table 0*/
1145 error
= JPEG_Set_Quantization_Mem(hjpeg
, hjpeg
->QuantTable0
, (hjpeg
->Instance
->QMEM0
));
1147 else if (hjpeg
->Conf
.ColorSpace
== JPEG_YCBCR_COLORSPACE
)
1150 Set the Color Space for YCbCr : 2 quantization tables are used
1151 one for Luminance(Y) and one for both Chrominances (Cb & Cr)
1153 hjpeg
->Instance
->CONFR1
|= JPEG_CONFR1_COLORSPACE_0
;
1155 JPEG_SetColorYCBCR(hjpeg
);
1157 /* Set quantization table 0*/
1158 error
= JPEG_Set_Quantization_Mem(hjpeg
, hjpeg
->QuantTable0
, (hjpeg
->Instance
->QMEM0
));
1159 /*By default quantization table 0 for component 0 and quantization table 1 for both components 1 and 2*/
1160 error
|= JPEG_Set_Quantization_Mem(hjpeg
, hjpeg
->QuantTable1
, (hjpeg
->Instance
->QMEM1
));
1162 if ((hjpeg
->Context
& JPEG_CONTEXT_CUSTOM_TABLES
) != 0UL)
1164 /*Use user customized quantization tables , 1 table per component*/
1165 /* use 3 quantization tables , one for each component*/
1166 hjpeg
->Instance
->CONFR1
&= (~JPEG_CONFR1_COLORSPACE
);
1167 hjpeg
->Instance
->CONFR1
|= JPEG_CONFR1_COLORSPACE_1
;
1169 error
|= JPEG_Set_Quantization_Mem(hjpeg
, hjpeg
->QuantTable2
, (hjpeg
->Instance
->QMEM2
));
1171 /*Use Quantization 1 table for component 1*/
1172 hjpeg
->Instance
->CONFR5
&= (~JPEG_CONFR5_QT
);
1173 hjpeg
->Instance
->CONFR5
|= JPEG_CONFR5_QT_0
;
1175 /*Use Quantization 2 table for component 2*/
1176 hjpeg
->Instance
->CONFR6
&= (~JPEG_CONFR6_QT
);
1177 hjpeg
->Instance
->CONFR6
|= JPEG_CONFR6_QT_1
;
1180 else /* ColorSpace == JPEG_CMYK_COLORSPACE */
1182 JPEG_SetColorCMYK(hjpeg
);
1184 /* Set quantization table 0*/
1185 error
= JPEG_Set_Quantization_Mem(hjpeg
, hjpeg
->QuantTable0
, (hjpeg
->Instance
->QMEM0
));
1186 /*By default quantization table 0 for All components*/
1188 if ((hjpeg
->Context
& JPEG_CONTEXT_CUSTOM_TABLES
) != 0UL)
1190 /*Use user customized quantization tables , 1 table per component*/
1191 /* use 4 quantization tables , one for each component*/
1192 hjpeg
->Instance
->CONFR1
|= JPEG_CONFR1_COLORSPACE
;
1194 error
|= JPEG_Set_Quantization_Mem(hjpeg
, hjpeg
->QuantTable1
, (hjpeg
->Instance
->QMEM1
));
1195 error
|= JPEG_Set_Quantization_Mem(hjpeg
, hjpeg
->QuantTable2
, (hjpeg
->Instance
->QMEM2
));
1196 error
|= JPEG_Set_Quantization_Mem(hjpeg
, hjpeg
->QuantTable3
, (hjpeg
->Instance
->QMEM3
));
1198 /*Use Quantization 1 table for component 1*/
1199 hjpeg
->Instance
->CONFR5
|= JPEG_CONFR5_QT_0
;
1201 /*Use Quantization 2 table for component 2*/
1202 hjpeg
->Instance
->CONFR6
|= JPEG_CONFR6_QT_1
;
1204 /*Use Quantization 3 table for component 3*/
1205 hjpeg
->Instance
->CONFR7
|= JPEG_CONFR7_QT
;
1211 hjpeg
->ErrorCode
= HAL_JPEG_ERROR_QUANT_TABLE
;
1213 /* Process Unlocked */
1214 __HAL_UNLOCK(hjpeg
);
1216 /* Set the JPEG State to ready */
1217 hjpeg
->State
= HAL_JPEG_STATE_READY
;
1221 /* Set the image size*/
1222 /* set the number of lines*/
1223 MODIFY_REG(hjpeg
->Instance
->CONFR1
, JPEG_CONFR1_YSIZE
, ((hjpeg
->Conf
.ImageHeight
& 0x0000FFFFUL
) << 16));
1224 /* set the number of pixels per line*/
1225 MODIFY_REG(hjpeg
->Instance
->CONFR3
, JPEG_CONFR3_XSIZE
, ((hjpeg
->Conf
.ImageWidth
& 0x0000FFFFUL
) << 16));
1228 if (hjpeg
->Conf
.ChromaSubsampling
== JPEG_420_SUBSAMPLING
) /* 4:2:0*/
1233 else if (hjpeg
->Conf
.ChromaSubsampling
== JPEG_422_SUBSAMPLING
) /* 4:2:2*/
1238 else /* Default is 8x8 MCU, 4:4:4*/
1244 hMCU
= (hjpeg
->Conf
.ImageWidth
/ hfactor
);
1245 if ((hjpeg
->Conf
.ImageWidth
% hfactor
) != 0UL)
1247 hMCU
++; /*+1 for horizontal incomplete MCU */
1250 vMCU
= (hjpeg
->Conf
.ImageHeight
/ vfactor
);
1251 if ((hjpeg
->Conf
.ImageHeight
% vfactor
) != 0UL)
1253 vMCU
++; /*+1 for vertical incomplete MCU */
1256 numberMCU
= (hMCU
* vMCU
) - 1UL; /* Bit Field JPEG_CONFR2_NMCU shall be set to NB_MCU - 1*/
1257 /* Set the number of MCU*/
1258 hjpeg
->Instance
->CONFR2
= (numberMCU
& JPEG_CONFR2_NMCU
);
1260 hjpeg
->Context
|= JPEG_CONTEXT_CONF_ENCODING
;
1262 /* Process Unlocked */
1263 __HAL_UNLOCK(hjpeg
);
1265 /* Set the JPEG State to ready */
1266 hjpeg
->State
= HAL_JPEG_STATE_READY
;
1268 /* Return function status */
1273 /* Process Unlocked */
1274 __HAL_UNLOCK(hjpeg
);
1276 /* Return function status */
1283 * @brief Extract the image configuration from the JPEG header during the decoding
1284 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
1285 * the configuration information for JPEG module
1286 * @param pInfo pointer to a JPEG_ConfTypeDef structure that contains
1287 * The JPEG decoded header informations
1288 * @retval HAL status
1290 HAL_StatusTypeDef
HAL_JPEG_GetInfo(JPEG_HandleTypeDef
*hjpeg
, JPEG_ConfTypeDef
*pInfo
)
1296 /* Check the JPEG handle allocation */
1297 if ((hjpeg
== NULL
) || (pInfo
== NULL
))
1302 /*Read the conf parameters */
1303 if ((hjpeg
->Instance
->CONFR1
& JPEG_CONFR1_NF
) == JPEG_CONFR1_NF_1
)
1305 pInfo
->ColorSpace
= JPEG_YCBCR_COLORSPACE
;
1307 else if ((hjpeg
->Instance
->CONFR1
& JPEG_CONFR1_NF
) == 0UL)
1309 pInfo
->ColorSpace
= JPEG_GRAYSCALE_COLORSPACE
;
1311 else if ((hjpeg
->Instance
->CONFR1
& JPEG_CONFR1_NF
) == JPEG_CONFR1_NF
)
1313 pInfo
->ColorSpace
= JPEG_CMYK_COLORSPACE
;
1320 pInfo
->ImageHeight
= (hjpeg
->Instance
->CONFR1
& 0xFFFF0000UL
) >> 16;
1321 pInfo
->ImageWidth
= (hjpeg
->Instance
->CONFR3
& 0xFFFF0000UL
) >> 16;
1323 if ((pInfo
->ColorSpace
== JPEG_YCBCR_COLORSPACE
) || (pInfo
->ColorSpace
== JPEG_CMYK_COLORSPACE
))
1325 yblockNb
= (hjpeg
->Instance
->CONFR4
& JPEG_CONFR4_NB
) >> 4;
1326 cBblockNb
= (hjpeg
->Instance
->CONFR5
& JPEG_CONFR5_NB
) >> 4;
1327 cRblockNb
= (hjpeg
->Instance
->CONFR6
& JPEG_CONFR6_NB
) >> 4;
1329 if ((yblockNb
== 1UL) && (cBblockNb
== 0UL) && (cRblockNb
== 0UL))
1331 pInfo
->ChromaSubsampling
= JPEG_422_SUBSAMPLING
; /*16x8 block*/
1333 else if ((yblockNb
== 0UL) && (cBblockNb
== 0UL) && (cRblockNb
== 0UL))
1335 pInfo
->ChromaSubsampling
= JPEG_444_SUBSAMPLING
;
1337 else if ((yblockNb
== 3UL) && (cBblockNb
== 0UL) && (cRblockNb
== 0UL))
1339 pInfo
->ChromaSubsampling
= JPEG_420_SUBSAMPLING
;
1341 else /*Default is 4:4:4*/
1343 pInfo
->ChromaSubsampling
= JPEG_444_SUBSAMPLING
;
1348 pInfo
->ChromaSubsampling
= JPEG_444_SUBSAMPLING
;
1351 pInfo
->ImageQuality
= JPEG_GetQuality(hjpeg
);
1353 /* Return function status */
1358 * @brief Enable JPEG Header parsing for decoding
1359 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
1360 * the configuration information for the JPEG.
1361 * @retval HAL status
1363 HAL_StatusTypeDef
HAL_JPEG_EnableHeaderParsing(JPEG_HandleTypeDef
*hjpeg
)
1365 /* Process locked */
1368 if (hjpeg
->State
== HAL_JPEG_STATE_READY
)
1370 /* Change the JPEG state */
1371 hjpeg
->State
= HAL_JPEG_STATE_BUSY
;
1373 /* Enable header processing*/
1374 hjpeg
->Instance
->CONFR1
|= JPEG_CONFR1_HDR
;
1376 /* Process unlocked */
1377 __HAL_UNLOCK(hjpeg
);
1379 /* Change the JPEG state */
1380 hjpeg
->State
= HAL_JPEG_STATE_READY
;
1386 /* Process unlocked */
1387 __HAL_UNLOCK(hjpeg
);
1394 * @brief Disable JPEG Header parsing for decoding
1395 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
1396 * the configuration information for the JPEG.
1397 * @retval HAL status
1399 HAL_StatusTypeDef
HAL_JPEG_DisableHeaderParsing(JPEG_HandleTypeDef
*hjpeg
)
1401 /* Process locked */
1404 if (hjpeg
->State
== HAL_JPEG_STATE_READY
)
1406 /* Change the JPEG state */
1407 hjpeg
->State
= HAL_JPEG_STATE_BUSY
;
1409 /* Disable header processing*/
1410 hjpeg
->Instance
->CONFR1
&= ~JPEG_CONFR1_HDR
;
1412 /* Process unlocked */
1413 __HAL_UNLOCK(hjpeg
);
1415 /* Change the JPEG state */
1416 hjpeg
->State
= HAL_JPEG_STATE_READY
;
1422 /* Process unlocked */
1423 __HAL_UNLOCK(hjpeg
);
1430 * @brief Modify the default Quantization tables used for JPEG encoding.
1431 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
1432 * the configuration information for JPEG module
1433 * @param QTable0 pointer to uint8_t , define the user quantification table for color component 1.
1434 * If NULL assume no need to update the table and no error return
1435 * @param QTable1 pointer to uint8_t , define the user quantification table for color component 2.
1436 * If NULL assume no need to update the table and no error return.
1437 * @param QTable2 pointer to uint8_t , define the user quantification table for color component 3,
1438 * If NULL assume no need to update the table and no error return.
1439 * @param QTable3 pointer to uint8_t , define the user quantification table for color component 4.
1440 * If NULL assume no need to update the table and no error return.
1442 * @retval HAL status
1446 HAL_StatusTypeDef
HAL_JPEG_SetUserQuantTables(JPEG_HandleTypeDef
*hjpeg
, uint8_t *QTable0
, uint8_t *QTable1
,
1447 uint8_t *QTable2
, uint8_t *QTable3
)
1449 /* Process Locked */
1452 if (hjpeg
->State
== HAL_JPEG_STATE_READY
)
1454 /* Change the DMA state */
1455 hjpeg
->State
= HAL_JPEG_STATE_BUSY
;
1457 hjpeg
->Context
|= JPEG_CONTEXT_CUSTOM_TABLES
;
1459 hjpeg
->QuantTable0
= QTable0
;
1460 hjpeg
->QuantTable1
= QTable1
;
1461 hjpeg
->QuantTable2
= QTable2
;
1462 hjpeg
->QuantTable3
= QTable3
;
1464 /* Process Unlocked */
1465 __HAL_UNLOCK(hjpeg
);
1467 /* Change the DMA state */
1468 hjpeg
->State
= HAL_JPEG_STATE_READY
;
1470 /* Return function status */
1475 /* Process Unlocked */
1476 __HAL_UNLOCK(hjpeg
);
1486 /** @defgroup JPEG_Exported_Functions_Group3 encoding/decoding processing functions
1487 * @brief processing functions.
1490 ==============================================================================
1491 ##### JPEG processing functions #####
1492 ==============================================================================
1493 [..] This section provides functions allowing to:
1494 (+) HAL_JPEG_Encode() : JPEG encoding with polling process
1495 (+) HAL_JPEG_Decode() : JPEG decoding with polling process
1496 (+) HAL_JPEG_Encode_IT() : JPEG encoding with interrupt process
1497 (+) HAL_JPEG_Decode_IT() : JPEG decoding with interrupt process
1498 (+) HAL_JPEG_Encode_DMA() : JPEG encoding with DMA process
1499 (+) HAL_JPEG_Decode_DMA() : JPEG decoding with DMA process
1500 (+) HAL_JPEG_Pause() : Pause the Input/Output processing
1501 (+) HAL_JPEG_Resume() : Resume the JPEG Input/Output processing
1502 (+) HAL_JPEG_ConfigInputBuffer() : Config Encoding/Decoding Input Buffer
1503 (+) HAL_JPEG_ConfigOutputBuffer() : Config Encoding/Decoding Output Buffer
1504 (+) HAL_JPEG_Abort() : Aborts the JPEG Encoding/Decoding
1511 * @brief Starts JPEG encoding with polling processing
1512 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
1513 * the configuration information for JPEG module
1514 * @param pDataInMCU Pointer to the Input buffer
1515 * @param InDataLength size in bytes Input buffer
1516 * @param pDataOut Pointer to the jpeg output data buffer
1517 * @param OutDataLength size in bytes of the Output buffer
1518 * @param Timeout Specify Timeout value
1519 * @retval HAL status
1521 HAL_StatusTypeDef
HAL_JPEG_Encode(JPEG_HandleTypeDef
*hjpeg
, uint8_t *pDataInMCU
, uint32_t InDataLength
,
1522 uint8_t *pDataOut
, uint32_t OutDataLength
, uint32_t Timeout
)
1526 /* Check the parameters */
1527 assert_param((InDataLength
>= 4UL));
1528 assert_param((OutDataLength
>= 4UL));
1530 /* Check In/out buffer allocation and size */
1531 if ((hjpeg
== NULL
) || (pDataInMCU
== NULL
) || (pDataOut
== NULL
))
1535 /* Process locked */
1538 if (hjpeg
->State
!= HAL_JPEG_STATE_READY
)
1540 /* Process Unlocked */
1541 __HAL_UNLOCK(hjpeg
);
1546 if (hjpeg
->State
== HAL_JPEG_STATE_READY
)
1548 if ((hjpeg
->Context
& JPEG_CONTEXT_CONF_ENCODING
) == JPEG_CONTEXT_CONF_ENCODING
)
1550 /*Change JPEG state*/
1551 hjpeg
->State
= HAL_JPEG_STATE_BUSY_ENCODING
;
1553 /*Set the Context to Encode with Polling*/
1554 hjpeg
->Context
&= ~(JPEG_CONTEXT_OPERATION_MASK
| JPEG_CONTEXT_METHOD_MASK
);
1555 hjpeg
->Context
|= (JPEG_CONTEXT_ENCODE
| JPEG_CONTEXT_POLLING
);
1558 tickstart
= HAL_GetTick();
1560 /*Store In/out buffers pointers and size*/
1561 hjpeg
->pJpegInBuffPtr
= pDataInMCU
;
1562 hjpeg
->pJpegOutBuffPtr
= pDataOut
;
1563 hjpeg
->InDataLength
= InDataLength
- (InDataLength
% 4UL); /* In Data length must be multiple of 4 Bytes (1 word)*/
1564 hjpeg
->OutDataLength
= OutDataLength
- (OutDataLength
% 4UL); /* Out Data length must be multiple of 4 Bytes (1 word)*/
1566 /*Reset In/out data counter */
1567 hjpeg
->JpegInCount
= 0;
1568 hjpeg
->JpegOutCount
= 0;
1570 /*Init decoding process*/
1571 JPEG_Init_Process(hjpeg
);
1573 /*JPEG data processing : In/Out FIFO transfer*/
1574 while ((JPEG_Process(hjpeg
) == JPEG_PROCESS_ONGOING
))
1576 if (Timeout
!= HAL_MAX_DELAY
)
1578 if (((HAL_GetTick() - tickstart
) > Timeout
) || (Timeout
== 0UL))
1581 /* Update error code */
1582 hjpeg
->ErrorCode
|= HAL_JPEG_ERROR_TIMEOUT
;
1584 /* Process Unlocked */
1585 __HAL_UNLOCK(hjpeg
);
1587 /*Change JPEG state*/
1588 hjpeg
->State
= HAL_JPEG_STATE_READY
;
1595 /* Process Unlocked */
1596 __HAL_UNLOCK(hjpeg
);
1598 /*Change JPEG state*/
1599 hjpeg
->State
= HAL_JPEG_STATE_READY
;
1604 /* Process Unlocked */
1605 __HAL_UNLOCK(hjpeg
);
1610 /* Return function status */
1615 * @brief Starts JPEG decoding with polling processing
1616 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
1617 * the configuration information for JPEG module
1618 * @param pDataIn Pointer to the input data buffer
1619 * @param InDataLength size in bytes Input buffer
1620 * @param pDataOutMCU Pointer to the Output data buffer
1621 * @param OutDataLength size in bytes of the Output buffer
1622 * @param Timeout Specify Timeout value
1623 * @retval HAL status
1625 HAL_StatusTypeDef
HAL_JPEG_Decode(JPEG_HandleTypeDef
*hjpeg
, uint8_t *pDataIn
, uint32_t InDataLength
,
1626 uint8_t *pDataOutMCU
, uint32_t OutDataLength
, uint32_t Timeout
)
1630 /* Check the parameters */
1631 assert_param((InDataLength
>= 4UL));
1632 assert_param((OutDataLength
>= 4UL));
1634 /* Check In/out buffer allocation and size */
1635 if ((hjpeg
== NULL
) || (pDataIn
== NULL
) || (pDataOutMCU
== NULL
))
1640 /* Process Locked */
1644 tickstart
= HAL_GetTick();
1646 if (hjpeg
->State
== HAL_JPEG_STATE_READY
)
1648 /*Change JPEG state*/
1649 hjpeg
->State
= HAL_JPEG_STATE_BUSY_DECODING
;
1651 /*Set the Context to Decode with Polling*/
1652 /*Set the Context to Encode with Polling*/
1653 hjpeg
->Context
&= ~(JPEG_CONTEXT_OPERATION_MASK
| JPEG_CONTEXT_METHOD_MASK
);
1654 hjpeg
->Context
|= (JPEG_CONTEXT_DECODE
| JPEG_CONTEXT_POLLING
);
1656 /*Store In/out buffers pointers and size*/
1657 hjpeg
->pJpegInBuffPtr
= pDataIn
;
1658 hjpeg
->pJpegOutBuffPtr
= pDataOutMCU
;
1659 hjpeg
->InDataLength
= InDataLength
- (InDataLength
% 4UL); /*In Data length must be multiple of 4 Bytes (1 word)*/
1660 hjpeg
->OutDataLength
= OutDataLength
- (OutDataLength
% 4UL); /*Out Data length must be multiple of 4 Bytes (1 word)*/
1662 /*Reset In/out data counter */
1663 hjpeg
->JpegInCount
= 0;
1664 hjpeg
->JpegOutCount
= 0;
1666 /*Init decoding process*/
1667 JPEG_Init_Process(hjpeg
);
1669 /*JPEG data processing : In/Out FIFO transfer*/
1670 while ((JPEG_Process(hjpeg
) == JPEG_PROCESS_ONGOING
))
1672 if (Timeout
!= HAL_MAX_DELAY
)
1674 if (((HAL_GetTick() - tickstart
) > Timeout
) || (Timeout
== 0UL))
1677 /* Update error code */
1678 hjpeg
->ErrorCode
|= HAL_JPEG_ERROR_TIMEOUT
;
1680 /* Process Unlocked */
1681 __HAL_UNLOCK(hjpeg
);
1683 /*Change JPEG state*/
1684 hjpeg
->State
= HAL_JPEG_STATE_READY
;
1691 /* Process Unlocked */
1692 __HAL_UNLOCK(hjpeg
);
1694 /*Change JPEG state*/
1695 hjpeg
->State
= HAL_JPEG_STATE_READY
;
1700 /* Process Unlocked */
1701 __HAL_UNLOCK(hjpeg
);
1705 /* Return function status */
1710 * @brief Starts JPEG encoding with interrupt processing
1711 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
1712 * the configuration information for JPEG module
1713 * @param pDataInMCU Pointer to the Input buffer
1714 * @param InDataLength size in bytes Input buffer
1715 * @param pDataOut Pointer to the jpeg output data buffer
1716 * @param OutDataLength size in bytes of the Output buffer
1717 * @retval HAL status
1719 HAL_StatusTypeDef
HAL_JPEG_Encode_IT(JPEG_HandleTypeDef
*hjpeg
, uint8_t *pDataInMCU
, uint32_t InDataLength
,
1720 uint8_t *pDataOut
, uint32_t OutDataLength
)
1722 /* Check the parameters */
1723 assert_param((InDataLength
>= 4UL));
1724 assert_param((OutDataLength
>= 4UL));
1726 /* Check In/out buffer allocation and size */
1727 if ((hjpeg
== NULL
) || (pDataInMCU
== NULL
) || (pDataOut
== NULL
))
1732 /* Process Locked */
1735 if (hjpeg
->State
!= HAL_JPEG_STATE_READY
)
1737 /* Process Unlocked */
1738 __HAL_UNLOCK(hjpeg
);
1744 if ((hjpeg
->Context
& JPEG_CONTEXT_CONF_ENCODING
) == JPEG_CONTEXT_CONF_ENCODING
)
1746 /*Change JPEG state*/
1747 hjpeg
->State
= HAL_JPEG_STATE_BUSY_ENCODING
;
1749 /*Set the Context to Encode with IT*/
1750 hjpeg
->Context
&= ~(JPEG_CONTEXT_OPERATION_MASK
| JPEG_CONTEXT_METHOD_MASK
);
1751 hjpeg
->Context
|= (JPEG_CONTEXT_ENCODE
| JPEG_CONTEXT_IT
);
1753 /*Store In/out buffers pointers and size*/
1754 hjpeg
->pJpegInBuffPtr
= pDataInMCU
;
1755 hjpeg
->pJpegOutBuffPtr
= pDataOut
;
1756 hjpeg
->InDataLength
= InDataLength
- (InDataLength
% 4UL); /*In Data length must be multiple of 4 Bytes (1 word)*/
1757 hjpeg
->OutDataLength
= OutDataLength
- (OutDataLength
% 4UL); /*Out Data length must be multiple of 4 Bytes (1 word)*/
1759 /*Reset In/out data counter */
1760 hjpeg
->JpegInCount
= 0;
1761 hjpeg
->JpegOutCount
= 0;
1763 /*Init decoding process*/
1764 JPEG_Init_Process(hjpeg
);
1769 /* Process Unlocked */
1770 __HAL_UNLOCK(hjpeg
);
1775 /* Return function status */
1780 * @brief Starts JPEG decoding with interrupt processing
1781 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
1782 * the configuration information for JPEG module
1783 * @param pDataIn Pointer to the input data buffer
1784 * @param InDataLength size in bytes Input buffer
1785 * @param pDataOutMCU Pointer to the Output data buffer
1786 * @param OutDataLength size in bytes of the Output buffer
1787 * @retval HAL status
1789 HAL_StatusTypeDef
HAL_JPEG_Decode_IT(JPEG_HandleTypeDef
*hjpeg
, uint8_t *pDataIn
, uint32_t InDataLength
,
1790 uint8_t *pDataOutMCU
, uint32_t OutDataLength
)
1792 /* Check the parameters */
1793 assert_param((InDataLength
>= 4UL));
1794 assert_param((OutDataLength
>= 4UL));
1796 /* Check In/out buffer allocation and size */
1797 if ((hjpeg
== NULL
) || (pDataIn
== NULL
) || (pDataOutMCU
== NULL
))
1802 /* Process Locked */
1805 if (hjpeg
->State
== HAL_JPEG_STATE_READY
)
1807 /*Change JPEG state*/
1808 hjpeg
->State
= HAL_JPEG_STATE_BUSY_DECODING
;
1810 /*Set the Context to Decode with IT*/
1811 hjpeg
->Context
&= ~(JPEG_CONTEXT_OPERATION_MASK
| JPEG_CONTEXT_METHOD_MASK
);
1812 hjpeg
->Context
|= (JPEG_CONTEXT_DECODE
| JPEG_CONTEXT_IT
);
1814 /*Store In/out buffers pointers and size*/
1815 hjpeg
->pJpegInBuffPtr
= pDataIn
;
1816 hjpeg
->pJpegOutBuffPtr
= pDataOutMCU
;
1817 hjpeg
->InDataLength
= InDataLength
- (InDataLength
% 4UL); /*In Data length must be multiple of 4 Bytes (1 word)*/
1818 hjpeg
->OutDataLength
= OutDataLength
- (OutDataLength
% 4UL); /*Out Data length must be multiple of 4 Bytes (1 word)*/
1820 /*Reset In/out data counter */
1821 hjpeg
->JpegInCount
= 0;
1822 hjpeg
->JpegOutCount
= 0;
1824 /*Init decoding process*/
1825 JPEG_Init_Process(hjpeg
);
1830 /* Process Unlocked */
1831 __HAL_UNLOCK(hjpeg
);
1835 /* Return function status */
1840 * @brief Starts JPEG encoding with DMA processing
1841 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
1842 * the configuration information for JPEG module
1843 * @param pDataInMCU Pointer to the Input buffer
1844 * @param InDataLength size in bytes Input buffer
1845 * @param pDataOut Pointer to the jpeg output data buffer
1846 * @param OutDataLength size in bytes of the Output buffer
1847 * @retval HAL status
1849 HAL_StatusTypeDef
HAL_JPEG_Encode_DMA(JPEG_HandleTypeDef
*hjpeg
, uint8_t *pDataInMCU
, uint32_t InDataLength
,
1850 uint8_t *pDataOut
, uint32_t OutDataLength
)
1852 /* Check the parameters */
1853 assert_param((InDataLength
>= 4UL));
1854 assert_param((OutDataLength
>= 4UL));
1856 /* Check In/out buffer allocation and size */
1857 if ((hjpeg
== NULL
) || (pDataInMCU
== NULL
) || (pDataOut
== NULL
))
1862 /* Process Locked */
1865 if (hjpeg
->State
!= HAL_JPEG_STATE_READY
)
1867 /* Process Unlocked */
1868 __HAL_UNLOCK(hjpeg
);
1874 if ((hjpeg
->Context
& JPEG_CONTEXT_CONF_ENCODING
) == JPEG_CONTEXT_CONF_ENCODING
)
1876 /*Change JPEG state*/
1877 hjpeg
->State
= HAL_JPEG_STATE_BUSY_ENCODING
;
1879 /*Set the Context to Encode with DMA*/
1880 hjpeg
->Context
&= ~(JPEG_CONTEXT_OPERATION_MASK
| JPEG_CONTEXT_METHOD_MASK
);
1881 hjpeg
->Context
|= (JPEG_CONTEXT_ENCODE
| JPEG_CONTEXT_DMA
);
1883 /*Store In/out buffers pointers and size*/
1884 hjpeg
->pJpegInBuffPtr
= pDataInMCU
;
1885 hjpeg
->pJpegOutBuffPtr
= pDataOut
;
1886 hjpeg
->InDataLength
= InDataLength
;
1887 hjpeg
->OutDataLength
= OutDataLength
;
1889 /*Reset In/out data counter */
1890 hjpeg
->JpegInCount
= 0;
1891 hjpeg
->JpegOutCount
= 0;
1893 /*Init decoding process*/
1894 JPEG_Init_Process(hjpeg
);
1896 /* JPEG encoding process using DMA */
1897 if (JPEG_DMA_StartProcess(hjpeg
) != HAL_OK
)
1900 hjpeg
->State
= HAL_JPEG_STATE_ERROR
;
1901 /* Process Unlocked */
1902 __HAL_UNLOCK(hjpeg
);
1910 /* Process Unlocked */
1911 __HAL_UNLOCK(hjpeg
);
1916 /* Return function status */
1921 * @brief Starts JPEG decoding with DMA processing
1922 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
1923 * the configuration information for JPEG module
1924 * @param pDataIn Pointer to the input data buffer
1925 * @param InDataLength size in bytes Input buffer
1926 * @param pDataOutMCU Pointer to the Output data buffer
1927 * @param OutDataLength size in bytes of the Output buffer
1928 * @retval HAL status
1930 HAL_StatusTypeDef
HAL_JPEG_Decode_DMA(JPEG_HandleTypeDef
*hjpeg
, uint8_t *pDataIn
, uint32_t InDataLength
,
1931 uint8_t *pDataOutMCU
, uint32_t OutDataLength
)
1933 /* Check the parameters */
1934 assert_param((InDataLength
>= 4UL));
1935 assert_param((OutDataLength
>= 4UL));
1937 /* Check In/out buffer allocation and size */
1938 if ((hjpeg
== NULL
) || (pDataIn
== NULL
) || (pDataOutMCU
== NULL
))
1943 /* Process Locked */
1946 if (hjpeg
->State
== HAL_JPEG_STATE_READY
)
1948 /*Change JPEG state*/
1949 hjpeg
->State
= HAL_JPEG_STATE_BUSY_DECODING
;
1951 /*Set the Context to Decode with DMA*/
1952 hjpeg
->Context
&= ~(JPEG_CONTEXT_OPERATION_MASK
| JPEG_CONTEXT_METHOD_MASK
);
1953 hjpeg
->Context
|= (JPEG_CONTEXT_DECODE
| JPEG_CONTEXT_DMA
);
1955 /*Store In/out buffers pointers and size*/
1956 hjpeg
->pJpegInBuffPtr
= pDataIn
;
1957 hjpeg
->pJpegOutBuffPtr
= pDataOutMCU
;
1958 hjpeg
->InDataLength
= InDataLength
;
1959 hjpeg
->OutDataLength
= OutDataLength
;
1961 /*Reset In/out data counter */
1962 hjpeg
->JpegInCount
= 0;
1963 hjpeg
->JpegOutCount
= 0;
1965 /*Init decoding process*/
1966 JPEG_Init_Process(hjpeg
);
1968 /* JPEG decoding process using DMA */
1969 if (JPEG_DMA_StartProcess(hjpeg
) != HAL_OK
)
1972 hjpeg
->State
= HAL_JPEG_STATE_ERROR
;
1973 /* Process Unlocked */
1974 __HAL_UNLOCK(hjpeg
);
1981 /* Process Unlocked */
1982 __HAL_UNLOCK(hjpeg
);
1986 /* Return function status */
1991 * @brief Pause the JPEG Input/Output processing
1992 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
1993 * the configuration information for JPEG module
1994 * @param XferSelection This parameter can be one of the following values :
1995 * JPEG_PAUSE_RESUME_INPUT : Pause Input processing
1996 * JPEG_PAUSE_RESUME_OUTPUT: Pause Output processing
1997 * JPEG_PAUSE_RESUME_INPUT_OUTPUT: Pause Input and Output processing
1998 * @retval HAL status
2000 HAL_StatusTypeDef
HAL_JPEG_Pause(JPEG_HandleTypeDef
*hjpeg
, uint32_t XferSelection
)
2004 assert_param(IS_JPEG_PAUSE_RESUME_STATE(XferSelection
));
2006 if ((hjpeg
->Context
& JPEG_CONTEXT_METHOD_MASK
) == JPEG_CONTEXT_DMA
)
2008 if ((XferSelection
& JPEG_PAUSE_RESUME_INPUT
) == JPEG_PAUSE_RESUME_INPUT
)
2010 hjpeg
->Context
|= JPEG_CONTEXT_PAUSE_INPUT
;
2012 if ((XferSelection
& JPEG_PAUSE_RESUME_OUTPUT
) == JPEG_PAUSE_RESUME_OUTPUT
)
2014 hjpeg
->Context
|= JPEG_CONTEXT_PAUSE_OUTPUT
;
2018 else if ((hjpeg
->Context
& JPEG_CONTEXT_METHOD_MASK
) == JPEG_CONTEXT_IT
)
2021 if ((XferSelection
& JPEG_PAUSE_RESUME_INPUT
) == JPEG_PAUSE_RESUME_INPUT
)
2023 hjpeg
->Context
|= JPEG_CONTEXT_PAUSE_INPUT
;
2024 mask
|= (JPEG_IT_IFT
| JPEG_IT_IFNF
);
2026 if ((XferSelection
& JPEG_PAUSE_RESUME_OUTPUT
) == JPEG_PAUSE_RESUME_OUTPUT
)
2028 hjpeg
->Context
|= JPEG_CONTEXT_PAUSE_OUTPUT
;
2029 mask
|= (JPEG_IT_OFT
| JPEG_IT_OFNE
| JPEG_IT_EOC
);
2031 __HAL_JPEG_DISABLE_IT(hjpeg
, mask
);
2039 /* Return function status */
2044 * @brief Resume the JPEG Input/Output processing
2045 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
2046 * the configuration information for JPEG module
2047 * @param XferSelection This parameter can be one of the following values :
2048 * JPEG_PAUSE_RESUME_INPUT : Resume Input processing
2049 * JPEG_PAUSE_RESUME_OUTPUT: Resume Output processing
2050 * JPEG_PAUSE_RESUME_INPUT_OUTPUT: Resume Input and Output processing
2051 * @retval HAL status
2053 HAL_StatusTypeDef
HAL_JPEG_Resume(JPEG_HandleTypeDef
*hjpeg
, uint32_t XferSelection
)
2058 assert_param(IS_JPEG_PAUSE_RESUME_STATE(XferSelection
));
2060 if ((hjpeg
->Context
& (JPEG_CONTEXT_PAUSE_INPUT
| JPEG_CONTEXT_PAUSE_OUTPUT
)) == 0UL)
2062 /* if nothing paused to resume return error*/
2066 if ((hjpeg
->Context
& JPEG_CONTEXT_METHOD_MASK
) == JPEG_CONTEXT_DMA
)
2069 if ((XferSelection
& JPEG_PAUSE_RESUME_INPUT
) == JPEG_PAUSE_RESUME_INPUT
)
2071 hjpeg
->Context
&= (~JPEG_CONTEXT_PAUSE_INPUT
);
2072 /*if the MDMA In is triggred with JPEG In FIFO Threshold flag
2073 then MDMA In buffer size is 32 bytes
2075 else (MDMA In is triggred with JPEG In FIFO not full flag)
2076 then MDMA In buffer size is 4 bytes
2078 xfrSize
= hjpeg
->hdmain
->Init
.BufferTransferLength
;
2082 hjpeg
->ErrorCode
|= HAL_JPEG_ERROR_DMA
;
2083 hjpeg
->State
= HAL_JPEG_STATE_ERROR
;
2086 /*MDMA transfer size (BNDTR) must be a multiple of MDMA buffer size (TLEN)*/
2087 hjpeg
->InDataLength
= hjpeg
->InDataLength
- (hjpeg
->InDataLength
% xfrSize
);
2090 if (hjpeg
->InDataLength
> 0UL)
2092 /* Start DMA FIFO In transfer */
2093 if (HAL_MDMA_Start_IT(hjpeg
->hdmain
, (uint32_t)hjpeg
->pJpegInBuffPtr
, (uint32_t)&hjpeg
->Instance
->DIR,
2094 hjpeg
->InDataLength
, 1) != HAL_OK
)
2096 hjpeg
->ErrorCode
|= HAL_JPEG_ERROR_DMA
;
2097 hjpeg
->State
= HAL_JPEG_STATE_ERROR
;
2102 if ((XferSelection
& JPEG_PAUSE_RESUME_OUTPUT
) == JPEG_PAUSE_RESUME_OUTPUT
)
2104 hjpeg
->Context
&= (~JPEG_CONTEXT_PAUSE_OUTPUT
);
2106 if ((hjpeg
->Context
& JPEG_CONTEXT_ENDING_DMA
) != 0UL)
2108 JPEG_DMA_PollResidualData(hjpeg
);
2112 /*if the MDMA Out is triggred with JPEG Out FIFO Threshold flag
2113 then MDMA out buffer size is 32 bytes
2114 else (MDMA Out is triggred with JPEG Out FIFO not empty flag)
2115 then MDMA buffer size is 4 bytes
2117 xfrSize
= hjpeg
->hdmaout
->Init
.BufferTransferLength
;
2121 hjpeg
->ErrorCode
|= HAL_JPEG_ERROR_DMA
;
2122 hjpeg
->State
= HAL_JPEG_STATE_ERROR
;
2125 /*MDMA transfer size (BNDTR) must be a multiple of MDMA buffer size (TLEN)*/
2126 hjpeg
->OutDataLength
= hjpeg
->OutDataLength
- (hjpeg
->OutDataLength
% xfrSize
);
2128 /* Start DMA FIFO Out transfer */
2129 if (HAL_MDMA_Start_IT(hjpeg
->hdmaout
, (uint32_t)&hjpeg
->Instance
->DOR
, (uint32_t)hjpeg
->pJpegOutBuffPtr
,
2130 hjpeg
->OutDataLength
, 1) != HAL_OK
)
2132 hjpeg
->ErrorCode
|= HAL_JPEG_ERROR_DMA
;
2133 hjpeg
->State
= HAL_JPEG_STATE_ERROR
;
2141 else if ((hjpeg
->Context
& JPEG_CONTEXT_METHOD_MASK
) == JPEG_CONTEXT_IT
)
2143 if ((XferSelection
& JPEG_PAUSE_RESUME_INPUT
) == JPEG_PAUSE_RESUME_INPUT
)
2145 hjpeg
->Context
&= (~JPEG_CONTEXT_PAUSE_INPUT
);
2146 mask
|= (JPEG_IT_IFT
| JPEG_IT_IFNF
);
2148 if ((XferSelection
& JPEG_PAUSE_RESUME_OUTPUT
) == JPEG_PAUSE_RESUME_OUTPUT
)
2150 hjpeg
->Context
&= (~JPEG_CONTEXT_PAUSE_OUTPUT
);
2151 mask
|= (JPEG_IT_OFT
| JPEG_IT_OFNE
| JPEG_IT_EOC
);
2153 __HAL_JPEG_ENABLE_IT(hjpeg
, mask
);
2161 /* Return function status */
2166 * @brief Config Encoding/Decoding Input Buffer.
2167 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
2168 * the configuration information for JPEG module.
2169 * @param pNewInputBuffer Pointer to the new input data buffer
2170 * @param InDataLength Size in bytes of the new Input data buffer
2171 * @retval HAL status
2173 void HAL_JPEG_ConfigInputBuffer(JPEG_HandleTypeDef
*hjpeg
, uint8_t *pNewInputBuffer
, uint32_t InDataLength
)
2175 hjpeg
->pJpegInBuffPtr
= pNewInputBuffer
;
2176 hjpeg
->InDataLength
= InDataLength
;
2180 * @brief Config Encoding/Decoding Output Buffer.
2181 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
2182 * the configuration information for JPEG module.
2183 * @param pNewOutputBuffer Pointer to the new output data buffer
2184 * @param OutDataLength Size in bytes of the new Output data buffer
2185 * @retval HAL status
2187 void HAL_JPEG_ConfigOutputBuffer(JPEG_HandleTypeDef
*hjpeg
, uint8_t *pNewOutputBuffer
, uint32_t OutDataLength
)
2189 hjpeg
->pJpegOutBuffPtr
= pNewOutputBuffer
;
2190 hjpeg
->OutDataLength
= OutDataLength
;
2194 * @brief Aborts the JPEG Encoding/Decoding.
2195 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
2196 * the configuration information for JPEG module
2197 * @retval HAL status
2199 HAL_StatusTypeDef
HAL_JPEG_Abort(JPEG_HandleTypeDef
*hjpeg
)
2202 uint32_t tmpContext
;
2203 tmpContext
= hjpeg
->Context
;
2205 /*Reset the Context operation and method*/
2206 hjpeg
->Context
&= ~(JPEG_CONTEXT_OPERATION_MASK
| JPEG_CONTEXT_METHOD_MASK
| JPEG_CONTEXT_ENDING_DMA
);
2208 if ((tmpContext
& JPEG_CONTEXT_METHOD_MASK
) == JPEG_CONTEXT_DMA
)
2210 /* Stop the DMA In/out Xfer*/
2211 if (HAL_MDMA_Abort(hjpeg
->hdmaout
) != HAL_OK
)
2213 if (hjpeg
->hdmaout
->ErrorCode
== HAL_MDMA_ERROR_TIMEOUT
)
2215 hjpeg
->ErrorCode
|= HAL_JPEG_ERROR_DMA
;
2218 if (HAL_MDMA_Abort(hjpeg
->hdmain
) != HAL_OK
)
2220 if (hjpeg
->hdmain
->ErrorCode
== HAL_MDMA_ERROR_TIMEOUT
)
2222 hjpeg
->ErrorCode
|= HAL_JPEG_ERROR_DMA
;
2228 /* Stop the JPEG encoding/decoding process*/
2229 hjpeg
->Instance
->CONFR0
&= ~JPEG_CONFR0_START
;
2232 tickstart
= HAL_GetTick();
2234 /* Check if the JPEG Codec is effectively disabled */
2235 while (__HAL_JPEG_GET_FLAG(hjpeg
, JPEG_FLAG_COF
) != 0UL)
2237 /* Check for the Timeout */
2238 if ((HAL_GetTick() - tickstart
) > JPEG_TIMEOUT_VALUE
)
2240 /* Update error code */
2241 hjpeg
->ErrorCode
|= HAL_JPEG_ERROR_TIMEOUT
;
2243 /* Change the DMA state */
2244 hjpeg
->State
= HAL_JPEG_STATE_ERROR
;
2249 /* Disable All Interrupts */
2250 __HAL_JPEG_DISABLE_IT(hjpeg
, JPEG_INTERRUPT_MASK
);
2253 /* Flush input and output FIFOs*/
2254 hjpeg
->Instance
->CR
|= JPEG_CR_IFF
;
2255 hjpeg
->Instance
->CR
|= JPEG_CR_OFF
;
2257 /* Clear all flags */
2258 __HAL_JPEG_CLEAR_FLAG(hjpeg
, JPEG_FLAG_ALL
);
2260 /* Reset JpegInCount and JpegOutCount */
2261 hjpeg
->JpegInCount
= 0;
2262 hjpeg
->JpegOutCount
= 0;
2264 /*Reset the Context Pause*/
2265 hjpeg
->Context
&= ~(JPEG_CONTEXT_PAUSE_INPUT
| JPEG_CONTEXT_PAUSE_OUTPUT
);
2267 /* Change the DMA state*/
2268 if (hjpeg
->ErrorCode
!= HAL_JPEG_ERROR_NONE
)
2270 hjpeg
->State
= HAL_JPEG_STATE_ERROR
;
2271 /* Process Unlocked */
2272 __HAL_UNLOCK(hjpeg
);
2273 /* Return function status */
2278 hjpeg
->State
= HAL_JPEG_STATE_READY
;
2279 /* Process Unlocked */
2280 __HAL_UNLOCK(hjpeg
);
2281 /* Return function status */
2292 /** @defgroup JPEG_Exported_Functions_Group4 JPEG Decode/Encode callback functions
2293 * @brief JPEG process callback functions.
2296 ==============================================================================
2297 ##### JPEG Decode and Encode callback functions #####
2298 ==============================================================================
2299 [..] This section provides callback functions:
2300 (+) HAL_JPEG_InfoReadyCallback() : Decoding JPEG Info ready callback
2301 (+) HAL_JPEG_EncodeCpltCallback() : Encoding complete callback.
2302 (+) HAL_JPEG_DecodeCpltCallback() : Decoding complete callback.
2303 (+) HAL_JPEG_ErrorCallback() : JPEG error callback.
2304 (+) HAL_JPEG_GetDataCallback() : Get New Data chunk callback.
2305 (+) HAL_JPEG_DataReadyCallback() : Decoded/Encoded Data ready callback.
2312 * @brief Decoding JPEG Info ready callback.
2313 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
2314 * the configuration information for JPEG module
2315 * @param pInfo pointer to a JPEG_ConfTypeDef structure that contains
2316 * The JPEG decoded header informations
2319 __weak
void HAL_JPEG_InfoReadyCallback(JPEG_HandleTypeDef
*hjpeg
, JPEG_ConfTypeDef
*pInfo
)
2321 /* Prevent unused argument(s) compilation warning */
2325 /* NOTE : This function Should not be modified, when the callback is needed,
2326 the HAL_JPEG_HeaderParsingCpltCallback could be implemented in the user file
2331 * @brief Encoding complete callback.
2332 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
2333 * the configuration information for JPEG module
2336 __weak
void HAL_JPEG_EncodeCpltCallback(JPEG_HandleTypeDef
*hjpeg
)
2338 /* Prevent unused argument(s) compilation warning */
2341 /* NOTE : This function Should not be modified, when the callback is needed,
2342 the HAL_JPEG_EncodeCpltCallback could be implemented in the user file
2347 * @brief Decoding complete callback.
2348 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
2349 * the configuration information for JPEG module
2352 __weak
void HAL_JPEG_DecodeCpltCallback(JPEG_HandleTypeDef
*hjpeg
)
2354 /* Prevent unused argument(s) compilation warning */
2357 /* NOTE : This function Should not be modified, when the callback is needed,
2358 the HAL_JPEG_EncodeCpltCallback could be implemented in the user file
2363 * @brief JPEG error callback.
2364 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
2365 * the configuration information for JPEG module
2368 __weak
void HAL_JPEG_ErrorCallback(JPEG_HandleTypeDef
*hjpeg
)
2370 /* Prevent unused argument(s) compilation warning */
2373 /* NOTE : This function Should not be modified, when the callback is needed,
2374 the HAL_JPEG_ErrorCallback could be implemented in the user file
2379 * @brief Get New Data chunk callback.
2380 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
2381 * the configuration information for JPEG module
2382 * @param NbDecodedData Number of consummed data in the previous chunk in bytes
2385 __weak
void HAL_JPEG_GetDataCallback(JPEG_HandleTypeDef
*hjpeg
, uint32_t NbDecodedData
)
2387 /* Prevent unused argument(s) compilation warning */
2389 UNUSED(NbDecodedData
);
2391 /* NOTE : This function Should not be modified, when the callback is needed,
2392 the HAL_JPEG_GetDataCallback could be implemented in the user file
2397 * @brief Decoded/Encoded Data ready callback.
2398 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
2399 * the configuration information for JPEG module
2400 * @param pDataOut pointer to the output data buffer
2401 * @param OutDataLength number in bytes of data available in the specified output buffer
2404 __weak
void HAL_JPEG_DataReadyCallback(JPEG_HandleTypeDef
*hjpeg
, uint8_t *pDataOut
, uint32_t OutDataLength
)
2406 /* Prevent unused argument(s) compilation warning */
2409 UNUSED(OutDataLength
);
2411 /* NOTE : This function Should not be modified, when the callback is needed,
2412 the HAL_JPEG_DataReadyCallback could be implemented in the user file
2421 /** @defgroup JPEG_Exported_Functions_Group5 JPEG IRQ handler management
2422 * @brief JPEG IRQ handler.
2425 ==============================================================================
2426 ##### JPEG IRQ handler management #####
2427 ==============================================================================
2428 [..] This section provides JPEG IRQ handler function.
2429 (+) HAL_JPEG_IRQHandler() : handles JPEG interrupt request
2436 * @brief This function handles JPEG interrupt request.
2437 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
2438 * the configuration information for JPEG module
2441 void HAL_JPEG_IRQHandler(JPEG_HandleTypeDef
*hjpeg
)
2443 switch (hjpeg
->State
)
2445 case HAL_JPEG_STATE_BUSY_ENCODING
:
2446 case HAL_JPEG_STATE_BUSY_DECODING
:
2447 /* continue JPEG data encoding/Decoding*/
2448 /* JPEG data processing : In/Out FIFO transfer*/
2449 if ((hjpeg
->Context
& JPEG_CONTEXT_METHOD_MASK
) == JPEG_CONTEXT_IT
)
2451 (void) JPEG_Process(hjpeg
);
2453 else if ((hjpeg
->Context
& JPEG_CONTEXT_METHOD_MASK
) == JPEG_CONTEXT_DMA
)
2455 JPEG_DMA_ContinueProcess(hjpeg
);
2472 /** @defgroup JPEG_Exported_Functions_Group6 Peripheral State functions
2473 * @brief Peripheral State functions.
2476 ==============================================================================
2477 ##### Peripheral State and Error functions #####
2478 ==============================================================================
2479 [..] This section provides JPEG State and Errors function.
2480 (+) HAL_JPEG_GetState() : permits to get in run-time the JPEG state.
2481 (+) HAL_JPEG_GetError() : Returns the JPEG error code if any.
2488 * @brief Returns the JPEG state.
2489 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
2490 * the configuration information for JPEG module
2491 * @retval JPEG state
2493 HAL_JPEG_STATETypeDef
HAL_JPEG_GetState(JPEG_HandleTypeDef
*hjpeg
)
2495 return hjpeg
->State
;
2499 * @brief Return the JPEG error code
2500 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
2501 * the configuration information for the specified JPEG.
2502 * @retval JPEG Error Code
2504 uint32_t HAL_JPEG_GetError(JPEG_HandleTypeDef
*hjpeg
)
2506 return hjpeg
->ErrorCode
;
2518 /** @addtogroup JPEG_Private_Functions
2523 * @brief Generates Huffman sizes/Codes Table from Bits/vals Table
2524 * @param Bits pointer to bits table
2525 * @param Huffsize pointer to sizes table
2526 * @param Huffcode pointer to codes table
2527 * @param LastK pointer to last Coeff (table dimmension)
2528 * @retval HAL status
2530 static HAL_StatusTypeDef
JPEG_Bits_To_SizeCodes(uint8_t *Bits
, uint8_t *Huffsize
, uint32_t *Huffcode
, uint32_t *LastK
)
2538 /* Figure C.1: Generation of table of Huffman code sizes */
2540 for (l
= 0; l
< 16UL; l
++)
2542 i
= (uint32_t)Bits
[l
];
2543 if ((p
+ i
) > 256UL)
2545 /* check for table overflow */
2550 Huffsize
[p
] = (uint8_t) l
+ 1U;
2558 /* Figure C.2: Generation of table of Huffman codes */
2562 while (Huffsize
[p
] != 0U)
2564 while (((uint32_t) Huffsize
[p
]) == si
)
2570 /* code must fit in "size" bits (si), no code is allowed to be all ones*/
2575 if (((uint32_t) code
) >= (((uint32_t) 1) << si
))
2582 /* Return function status */
2587 * @brief Transform a Bits/Vals AC Huffman table to sizes/Codes huffman Table
2588 * that can programmed to the JPEG encoder registers
2589 * @param AC_BitsValsTable pointer to AC huffman bits/vals table
2590 * @param AC_SizeCodesTable pointer to AC huffman Sizes/Codes table
2591 * @retval HAL status
2593 static HAL_StatusTypeDef
JPEG_ACHuff_BitsVals_To_SizeCodes(JPEG_ACHuffTableTypeDef
*AC_BitsValsTable
,
2594 JPEG_AC_HuffCodeTableTypeDef
*AC_SizeCodesTable
)
2596 HAL_StatusTypeDef error
;
2597 uint8_t huffsize
[257];
2598 uint32_t huffcode
[257];
2600 uint32_t l
, lsb
, msb
;
2603 error
= JPEG_Bits_To_SizeCodes(AC_BitsValsTable
->Bits
, huffsize
, huffcode
, &lastK
);
2604 if (error
!= HAL_OK
)
2609 /* Figure C.3: Ordering procedure for encoding procedure code tables */
2614 l
= AC_BitsValsTable
->HuffVal
[k
];
2617 l
= 160; /*l = 0x00 EOB code*/
2619 else if (l
== 0xF0UL
) /* l = 0xF0 ZRL code*/
2625 msb
= (l
& 0xF0UL
) >> 4;
2627 l
= (msb
* 10UL) + lsb
- 1UL;
2629 if (l
>= JPEG_AC_HUFF_TABLE_SIZE
)
2631 return HAL_ERROR
; /* Huffman Table overflow error*/
2635 AC_SizeCodesTable
->HuffmanCode
[l
] = huffcode
[k
];
2636 AC_SizeCodesTable
->CodeLength
[l
] = huffsize
[k
] - 1U;
2641 /* Return function status */
2646 * @brief Transform a Bits/Vals DC Huffman table to sizes/Codes huffman Table
2647 * that can programmed to the JPEG encoder registers
2648 * @param DC_BitsValsTable pointer to DC huffman bits/vals table
2649 * @param DC_SizeCodesTable pointer to DC huffman Sizes/Codes table
2650 * @retval HAL status
2652 static HAL_StatusTypeDef
JPEG_DCHuff_BitsVals_To_SizeCodes(JPEG_DCHuffTableTypeDef
*DC_BitsValsTable
,
2653 JPEG_DC_HuffCodeTableTypeDef
*DC_SizeCodesTable
)
2655 HAL_StatusTypeDef error
;
2660 uint8_t huffsize
[257];
2661 uint32_t huffcode
[257];
2662 error
= JPEG_Bits_To_SizeCodes(DC_BitsValsTable
->Bits
, huffsize
, huffcode
, &lastK
);
2663 if (error
!= HAL_OK
)
2667 /* Figure C.3: ordering procedure for encoding procedure code tables */
2672 l
= DC_BitsValsTable
->HuffVal
[k
];
2673 if (l
>= JPEG_DC_HUFF_TABLE_SIZE
)
2675 return HAL_ERROR
; /* Huffman Table overflow error*/
2679 DC_SizeCodesTable
->HuffmanCode
[l
] = huffcode
[k
];
2680 DC_SizeCodesTable
->CodeLength
[l
] = huffsize
[k
] - 1U;
2685 /* Return function status */
2690 * @brief Set the JPEG register with an DC huffman table at the given DC table address
2691 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
2692 * the configuration information for JPEG module
2693 * @param HuffTableDC pointer to DC huffman table
2694 * @param DCTableAddress Encoder DC huffman table address it could be HUFFENC_DC0 or HUFFENC_DC1.
2695 * @retval HAL status
2697 static HAL_StatusTypeDef
JPEG_Set_HuffDC_Mem(JPEG_HandleTypeDef
*hjpeg
, JPEG_DCHuffTableTypeDef
*HuffTableDC
,
2698 const __IO
uint32_t *DCTableAddress
)
2700 HAL_StatusTypeDef error
;
2701 JPEG_DC_HuffCodeTableTypeDef dcSizeCodesTable
;
2705 __IO
uint32_t *address
, *addressDef
;
2707 if (DCTableAddress
== (hjpeg
->Instance
->HUFFENC_DC0
))
2709 address
= (hjpeg
->Instance
->HUFFENC_DC0
+ (JPEG_DC_HUFF_TABLE_SIZE
/ 2UL));
2711 else if (DCTableAddress
== (hjpeg
->Instance
->HUFFENC_DC1
))
2713 address
= (hjpeg
->Instance
->HUFFENC_DC1
+ (JPEG_DC_HUFF_TABLE_SIZE
/ 2UL));
2720 if (HuffTableDC
!= NULL
)
2722 error
= JPEG_DCHuff_BitsVals_To_SizeCodes(HuffTableDC
, &dcSizeCodesTable
);
2723 if (error
!= HAL_OK
)
2727 addressDef
= address
;
2728 *addressDef
= 0x0FFF0FFF;
2730 *addressDef
= 0x0FFF0FFF;
2732 i
= JPEG_DC_HUFF_TABLE_SIZE
;
2737 msb
= ((uint32_t)(((uint32_t)dcSizeCodesTable
.CodeLength
[i
] & 0xFU
) << 8)) | ((uint32_t)dcSizeCodesTable
.HuffmanCode
[i
] &
2740 lsb
= ((uint32_t)(((uint32_t)dcSizeCodesTable
.CodeLength
[i
] & 0xFU
) << 8)) | ((uint32_t)dcSizeCodesTable
.HuffmanCode
[i
] &
2743 *address
= lsb
| (msb
<< 16);
2747 /* Return function status */
2752 * @brief Set the JPEG register with an AC huffman table at the given AC table address
2753 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
2754 * the configuration information for JPEG module
2755 * @param HuffTableAC pointer to AC huffman table
2756 * @param ACTableAddress Encoder AC huffman table address it could be HUFFENC_AC0 or HUFFENC_AC1.
2757 * @retval HAL status
2759 static HAL_StatusTypeDef
JPEG_Set_HuffAC_Mem(JPEG_HandleTypeDef
*hjpeg
, JPEG_ACHuffTableTypeDef
*HuffTableAC
,
2760 const __IO
uint32_t *ACTableAddress
)
2762 HAL_StatusTypeDef error
;
2763 JPEG_AC_HuffCodeTableTypeDef acSizeCodesTable
;
2764 uint32_t i
, lsb
, msb
;
2765 __IO
uint32_t *address
, *addressDef
;
2767 if (ACTableAddress
== (hjpeg
->Instance
->HUFFENC_AC0
))
2769 address
= (hjpeg
->Instance
->HUFFENC_AC0
+ (JPEG_AC_HUFF_TABLE_SIZE
/ 2UL));
2771 else if (ACTableAddress
== (hjpeg
->Instance
->HUFFENC_AC1
))
2773 address
= (hjpeg
->Instance
->HUFFENC_AC1
+ (JPEG_AC_HUFF_TABLE_SIZE
/ 2UL));
2780 if (HuffTableAC
!= NULL
)
2782 error
= JPEG_ACHuff_BitsVals_To_SizeCodes(HuffTableAC
, &acSizeCodesTable
);
2783 if (error
!= HAL_OK
)
2787 /* Default values settings: 162:167 FFFh , 168:175 FD0h_FD7h */
2788 /* Locations 162:175 of each AC table contain information used internally by the core */
2790 addressDef
= address
;
2791 for (i
= 0; i
< 3UL; i
++)
2793 *addressDef
= 0x0FFF0FFF;
2796 *addressDef
= 0x0FD10FD0;
2798 *addressDef
= 0x0FD30FD2;
2800 *addressDef
= 0x0FD50FD4;
2802 *addressDef
= 0x0FD70FD6;
2803 /* end of Locations 162:175 */
2806 i
= JPEG_AC_HUFF_TABLE_SIZE
;
2811 msb
= ((uint32_t)(((uint32_t)acSizeCodesTable
.CodeLength
[i
] & 0xFU
) << 8)) | ((uint32_t)acSizeCodesTable
.HuffmanCode
[i
] &
2814 lsb
= ((uint32_t)(((uint32_t)acSizeCodesTable
.CodeLength
[i
] & 0xFU
) << 8)) | ((uint32_t)acSizeCodesTable
.HuffmanCode
[i
] &
2817 *address
= lsb
| (msb
<< 16);
2821 /* Return function status */
2826 * @brief Configure the JPEG encoder register huffman tables to used during
2827 * the encdoing operation
2828 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
2829 * the configuration information for JPEG module
2832 static HAL_StatusTypeDef
JPEG_Set_HuffEnc_Mem(JPEG_HandleTypeDef
*hjpeg
)
2834 HAL_StatusTypeDef error
;
2836 JPEG_Set_Huff_DHTMem(hjpeg
);
2837 error
= JPEG_Set_HuffAC_Mem(hjpeg
, (JPEG_ACHuffTableTypeDef
*)(uint32_t)&JPEG_ACLUM_HuffTable
,
2838 (hjpeg
->Instance
->HUFFENC_AC0
));
2839 if (error
!= HAL_OK
)
2844 error
= JPEG_Set_HuffAC_Mem(hjpeg
, (JPEG_ACHuffTableTypeDef
*)(uint32_t)&JPEG_ACCHROM_HuffTable
,
2845 (hjpeg
->Instance
->HUFFENC_AC1
));
2846 if (error
!= HAL_OK
)
2851 error
= JPEG_Set_HuffDC_Mem(hjpeg
, (JPEG_DCHuffTableTypeDef
*)(uint32_t)&JPEG_DCLUM_HuffTable
,
2852 hjpeg
->Instance
->HUFFENC_DC0
);
2853 if (error
!= HAL_OK
)
2858 error
= JPEG_Set_HuffDC_Mem(hjpeg
, (JPEG_DCHuffTableTypeDef
*)(uint32_t)&JPEG_DCCHROM_HuffTable
,
2859 hjpeg
->Instance
->HUFFENC_DC1
);
2860 if (error
!= HAL_OK
)
2864 /* Return function status */
2869 * @brief Configure the JPEG register huffman tables to be included in the JPEG
2870 * file header (used for encoding only)
2871 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
2872 * the configuration information for JPEG module
2875 static void JPEG_Set_Huff_DHTMem(JPEG_HandleTypeDef
*hjpeg
)
2877 JPEG_ACHuffTableTypeDef
*HuffTableAC0
= (JPEG_ACHuffTableTypeDef
*)(uint32_t)&JPEG_ACLUM_HuffTable
;
2878 JPEG_ACHuffTableTypeDef
*HuffTableAC1
= (JPEG_ACHuffTableTypeDef
*)(uint32_t)&JPEG_ACCHROM_HuffTable
;
2879 JPEG_DCHuffTableTypeDef
*HuffTableDC0
= (JPEG_DCHuffTableTypeDef
*)(uint32_t)&JPEG_DCLUM_HuffTable
;
2880 JPEG_DCHuffTableTypeDef
*HuffTableDC1
= (JPEG_DCHuffTableTypeDef
*)(uint32_t)&JPEG_DCCHROM_HuffTable
;
2881 uint32_t value
, index
;
2882 __IO
uint32_t *address
;
2884 /* DC0 Huffman Table : BITS*/
2885 /* DC0 BITS is a 16 Bytes table i.e 4x32bits words from DHTMEM base address to DHTMEM + 3*/
2886 address
= (hjpeg
->Instance
->DHTMEM
+ 3);
2891 *address
= (((uint32_t)HuffTableDC0
->Bits
[index
- 1UL] & 0xFFUL
) << 24) |
2892 (((uint32_t)HuffTableDC0
->Bits
[index
- 2UL] & 0xFFUL
) << 16) |
2893 (((uint32_t)HuffTableDC0
->Bits
[index
- 3UL] & 0xFFUL
) << 8) |
2894 ((uint32_t)HuffTableDC0
->Bits
[index
- 4UL] & 0xFFUL
);
2899 /* DC0 Huffman Table : Val*/
2900 /* DC0 VALS is a 12 Bytes table i.e 3x32bits words from DHTMEM base address +4 to DHTMEM + 6 */
2901 address
= (hjpeg
->Instance
->DHTMEM
+ 6);
2905 *address
= (((uint32_t)HuffTableDC0
->HuffVal
[index
- 1UL] & 0xFFUL
) << 24) |
2906 (((uint32_t)HuffTableDC0
->HuffVal
[index
- 2UL] & 0xFFUL
) << 16) |
2907 (((uint32_t)HuffTableDC0
->HuffVal
[index
- 3UL] & 0xFFUL
) << 8) |
2908 ((uint32_t)HuffTableDC0
->HuffVal
[index
- 4UL] & 0xFFUL
);
2913 /* AC0 Huffman Table : BITS*/
2914 /* AC0 BITS is a 16 Bytes table i.e 4x32bits words from DHTMEM base address + 7 to DHTMEM + 10*/
2915 address
= (hjpeg
->Instance
->DHTMEM
+ 10UL);
2920 *address
= (((uint32_t)HuffTableAC0
->Bits
[index
- 1UL] & 0xFFUL
) << 24) |
2921 (((uint32_t)HuffTableAC0
->Bits
[index
- 2UL] & 0xFFUL
) << 16) |
2922 (((uint32_t)HuffTableAC0
->Bits
[index
- 3UL] & 0xFFUL
) << 8) |
2923 ((uint32_t)HuffTableAC0
->Bits
[index
- 4UL] & 0xFFUL
);
2928 /* AC0 Huffman Table : Val*/
2929 /* AC0 VALS is a 162 Bytes table i.e 41x32bits words from DHTMEM base address + 11 to DHTMEM + 51 */
2930 /* only Byte 0 and Byte 1 of the last word (@ DHTMEM + 51) belong to AC0 VALS table */
2931 address
= (hjpeg
->Instance
->DHTMEM
+ 51);
2932 value
= *address
& 0xFFFF0000U
;
2933 value
= value
| (((uint32_t)HuffTableAC0
->HuffVal
[161] & 0xFFUL
) << 8) | ((uint32_t)HuffTableAC0
->HuffVal
[160] & 0xFFUL
);
2936 /*continue setting 160 AC0 huffman values */
2937 address
--; /* address = hjpeg->Instance->DHTMEM + 50*/
2941 *address
= (((uint32_t)HuffTableAC0
->HuffVal
[index
- 1UL] & 0xFFUL
) << 24) |
2942 (((uint32_t)HuffTableAC0
->HuffVal
[index
- 2UL] & 0xFFUL
) << 16) |
2943 (((uint32_t)HuffTableAC0
->HuffVal
[index
- 3UL] & 0xFFUL
) << 8) |
2944 ((uint32_t)HuffTableAC0
->HuffVal
[index
- 4UL] & 0xFFUL
);
2949 /* DC1 Huffman Table : BITS*/
2950 /* DC1 BITS is a 16 Bytes table i.e 4x32bits words from DHTMEM + 51 base address to DHTMEM + 55*/
2951 /* only Byte 2 and Byte 3 of the first word (@ DHTMEM + 51) belong to DC1 Bits table */
2952 address
= (hjpeg
->Instance
->DHTMEM
+ 51);
2953 value
= *address
& 0x0000FFFFU
;
2954 value
= value
| (((uint32_t)HuffTableDC1
->Bits
[1] & 0xFFUL
) << 24) | (((uint32_t)HuffTableDC1
->Bits
[0] & 0xFFUL
) << 16);
2957 /* only Byte 0 and Byte 1 of the last word (@ DHTMEM + 55) belong to DC1 Bits table */
2958 address
= (hjpeg
->Instance
->DHTMEM
+ 55);
2959 value
= *address
& 0xFFFF0000U
;
2960 value
= value
| (((uint32_t)HuffTableDC1
->Bits
[15] & 0xFFUL
) << 8) | ((uint32_t)HuffTableDC1
->Bits
[14] & 0xFFUL
);
2963 /*continue setting 12 DC1 huffman Bits from DHTMEM + 54 down to DHTMEM + 52*/
2969 *address
= (((uint32_t)HuffTableDC1
->Bits
[index
+ 1UL] & 0xFFUL
) << 24) |
2970 (((uint32_t)HuffTableDC1
->Bits
[index
] & 0xFFUL
) << 16) |
2971 (((uint32_t)HuffTableDC1
->Bits
[index
- 1UL] & 0xFFUL
) << 8) |
2972 ((uint32_t)HuffTableDC1
->Bits
[index
- 2UL] & 0xFFUL
);
2977 /* DC1 Huffman Table : Val*/
2978 /* DC1 VALS is a 12 Bytes table i.e 3x32bits words from DHTMEM base address +55 to DHTMEM + 58 */
2979 /* only Byte 2 and Byte 3 of the first word (@ DHTMEM + 55) belong to DC1 Val table */
2980 address
= (hjpeg
->Instance
->DHTMEM
+ 55);
2981 value
= *address
& 0x0000FFFFUL
;
2982 value
= value
| (((uint32_t)HuffTableDC1
->HuffVal
[1] & 0xFFUL
) << 24) | (((uint32_t)HuffTableDC1
->HuffVal
[0] & 0xFFUL
) <<
2986 /* only Byte 0 and Byte 1 of the last word (@ DHTMEM + 58) belong to DC1 Val table */
2987 address
= (hjpeg
->Instance
->DHTMEM
+ 58);
2988 value
= *address
& 0xFFFF0000UL
;
2989 value
= value
| (((uint32_t)HuffTableDC1
->HuffVal
[11] & 0xFFUL
) << 8) | ((uint32_t)HuffTableDC1
->HuffVal
[10] & 0xFFUL
);
2992 /*continue setting 8 DC1 huffman val from DHTMEM + 57 down to DHTMEM + 56*/
2997 *address
= (((uint32_t)HuffTableDC1
->HuffVal
[index
+ 1UL] & 0xFFUL
) << 24) |
2998 (((uint32_t)HuffTableDC1
->HuffVal
[index
] & 0xFFUL
) << 16) |
2999 (((uint32_t)HuffTableDC1
->HuffVal
[index
- 1UL] & 0xFFUL
) << 8) |
3000 ((uint32_t)HuffTableDC1
->HuffVal
[index
- 2UL] & 0xFFUL
);
3005 /* AC1 Huffman Table : BITS*/
3006 /* AC1 BITS is a 16 Bytes table i.e 4x32bits words from DHTMEM base address + 58 to DHTMEM + 62*/
3007 /* only Byte 2 and Byte 3 of the first word (@ DHTMEM + 58) belong to AC1 Bits table */
3008 address
= (hjpeg
->Instance
->DHTMEM
+ 58);
3009 value
= *address
& 0x0000FFFFU
;
3010 value
= value
| (((uint32_t)HuffTableAC1
->Bits
[1] & 0xFFUL
) << 24) | (((uint32_t)HuffTableAC1
->Bits
[0] & 0xFFUL
) << 16);
3013 /* only Byte 0 and Byte 1 of the last word (@ DHTMEM + 62) belong to Bits Val table */
3014 address
= (hjpeg
->Instance
->DHTMEM
+ 62);
3015 value
= *address
& 0xFFFF0000U
;
3016 value
= value
| (((uint32_t)HuffTableAC1
->Bits
[15] & 0xFFUL
) << 8) | ((uint32_t)HuffTableAC1
->Bits
[14] & 0xFFUL
);
3019 /*continue setting 12 AC1 huffman Bits from DHTMEM + 61 down to DHTMEM + 59*/
3025 *address
= (((uint32_t)HuffTableAC1
->Bits
[index
+ 1UL] & 0xFFUL
) << 24) |
3026 (((uint32_t)HuffTableAC1
->Bits
[index
] & 0xFFUL
) << 16) |
3027 (((uint32_t)HuffTableAC1
->Bits
[index
- 1UL] & 0xFFUL
) << 8) |
3028 ((uint32_t)HuffTableAC1
->Bits
[index
- 2UL] & 0xFFUL
);
3033 /* AC1 Huffman Table : Val*/
3034 /* AC1 VALS is a 162 Bytes table i.e 41x32bits words from DHTMEM base address + 62 to DHTMEM + 102 */
3035 /* only Byte 2 and Byte 3 of the first word (@ DHTMEM + 62) belong to AC1 VALS table */
3036 address
= (hjpeg
->Instance
->DHTMEM
+ 62);
3037 value
= *address
& 0x0000FFFFUL
;
3038 value
= value
| (((uint32_t)HuffTableAC1
->HuffVal
[1] & 0xFFUL
) << 24) | (((uint32_t)HuffTableAC1
->HuffVal
[0] & 0xFFUL
) <<
3042 /*continue setting 160 AC1 huffman values from DHTMEM + 63 to DHTMEM+102 */
3043 address
= (hjpeg
->Instance
->DHTMEM
+ 102);
3047 *address
= (((uint32_t)HuffTableAC1
->HuffVal
[index
+ 1UL] & 0xFFUL
) << 24) |
3048 (((uint32_t)HuffTableAC1
->HuffVal
[index
] & 0xFFUL
) << 16) |
3049 (((uint32_t)HuffTableAC1
->HuffVal
[index
- 1UL] & 0xFFUL
) << 8) |
3050 ((uint32_t)HuffTableAC1
->HuffVal
[index
- 2UL] & 0xFFUL
);
3058 * @brief Configure the JPEG registers with a given quantization table
3059 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
3060 * the configuration information for JPEG module
3061 * @param QTable pointer to an array of 64 bytes giving the quantization table
3062 * @param QTableAddress destination quantization address in the JPEG peripheral
3063 * it could be QMEM0, QMEM1, QMEM2 or QMEM3
3064 * @retval 0 if no error, 1 if error
3066 static uint32_t JPEG_Set_Quantization_Mem(JPEG_HandleTypeDef
*hjpeg
, uint8_t *QTable
,
3067 __IO
uint32_t *QTableAddress
)
3073 uint32_t ScaleFactor
;
3074 __IO
uint32_t *tableAddress
;
3076 tableAddress
= QTableAddress
;
3078 if ((hjpeg
->Conf
.ImageQuality
>= 50UL) && (hjpeg
->Conf
.ImageQuality
<= 100UL))
3080 ScaleFactor
= 200UL - (hjpeg
->Conf
.ImageQuality
* 2UL);
3082 else if (hjpeg
->Conf
.ImageQuality
> 0UL)
3084 ScaleFactor
= ((uint32_t) 5000) / ((uint32_t) hjpeg
->Conf
.ImageQuality
);
3091 /*Quantization_table = (Standard_quanization_table * ScaleFactor + 50) / 100*/
3093 while (i
< (JPEG_QUANT_TABLE_SIZE
- 3UL))
3096 for (j
= 0; j
< 4UL; j
++)
3098 /* Note that the quantization coefficients must be specified in the table in zigzag order */
3099 quantVal
= ((((uint32_t) QTable
[JPEG_ZIGZAG_ORDER
[i
+ j
]]) * ScaleFactor
) + 50UL) / 100UL;
3101 if (quantVal
== 0UL)
3105 else if (quantVal
> 255UL)
3111 /* Nothing to do, keep same value of quantVal */
3114 quantRow
|= ((quantVal
& 0xFFUL
) << (8UL * j
));
3118 *tableAddress
= quantRow
;
3122 /* Return function status */
3127 * @brief Configure the JPEG registers for YCbCr color space
3128 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
3129 * the configuration information for JPEG module
3132 static void JPEG_SetColorYCBCR(JPEG_HandleTypeDef
*hjpeg
)
3134 uint32_t ySamplingH
;
3135 uint32_t ySamplingV
;
3138 /*Set Number of color components to 3*/
3139 hjpeg
->Instance
->CONFR1
&= ~JPEG_CONFR1_NF
;
3140 hjpeg
->Instance
->CONFR1
|= JPEG_CONFR1_NF_1
;
3142 /* compute MCU block size and Y, Cb ,Cr sampling factors*/
3143 if (hjpeg
->Conf
.ChromaSubsampling
== JPEG_420_SUBSAMPLING
)
3145 ySamplingH
= JPEG_CONFR4_HSF_1
; /* Hs = 2*/
3146 ySamplingV
= JPEG_CONFR4_VSF_1
; /* Vs = 2*/
3148 yblockNb
= 0x30; /* 4 blocks of 8x8*/
3150 else if (hjpeg
->Conf
.ChromaSubsampling
== JPEG_422_SUBSAMPLING
)
3152 ySamplingH
= JPEG_CONFR4_HSF_1
; /* Hs = 2*/
3153 ySamplingV
= JPEG_CONFR4_VSF_0
; /* Vs = 1*/
3155 yblockNb
= 0x10; /* 2 blocks of 8x8*/
3157 else /*JPEG_444_SUBSAMPLING and default*/
3159 ySamplingH
= JPEG_CONFR4_HSF_0
; /* Hs = 1*/
3160 ySamplingV
= JPEG_CONFR4_VSF_0
; /* Vs = 1*/
3162 yblockNb
= 0; /* 1 block of 8x8*/
3165 hjpeg
->Instance
->CONFR1
&= ~(JPEG_CONFR1_NF
| JPEG_CONFR1_NS
);
3166 hjpeg
->Instance
->CONFR1
|= (JPEG_CONFR1_NF_1
| JPEG_CONFR1_NS_1
);
3168 /*Reset CONFR4 register*/
3169 hjpeg
->Instance
->CONFR4
= 0;
3170 /*Set Horizental and Vertical sampling factor , number of blocks , Quantization table and Huffman AC/DC tables for component 0*/
3171 hjpeg
->Instance
->CONFR4
|= (ySamplingH
| ySamplingV
| (yblockNb
& JPEG_CONFR4_NB
));
3173 /*Reset CONFR5 register*/
3174 hjpeg
->Instance
->CONFR5
= 0;
3175 /*Set Horizental and Vertical sampling factor , number of blocks , Quantization table and Huffman AC/DC tables for component 1*/
3176 hjpeg
->Instance
->CONFR5
|= (JPEG_CONFR5_HSF_0
| JPEG_CONFR5_VSF_0
| JPEG_CONFR5_QT_0
| JPEG_CONFR5_HA
| JPEG_CONFR5_HD
);
3178 /*Reset CONFR6 register*/
3179 hjpeg
->Instance
->CONFR6
= 0;
3180 /*Set Horizental and Vertical sampling factor and number of blocks for component 2*/
3181 /* In YCBCR , by default, both chrominance components (component 1 and component 2) use the same Quantization table (table 1) */
3182 /* In YCBCR , both chrominance components (component 1 and component 2) use the same Huffman tables (table 1) */
3183 hjpeg
->Instance
->CONFR6
|= (JPEG_CONFR6_HSF_0
| JPEG_CONFR6_VSF_0
| JPEG_CONFR6_QT_0
| JPEG_CONFR6_HA
| JPEG_CONFR6_HD
);
3188 * @brief Configure the JPEG registers for GrayScale color space
3189 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
3190 * the configuration information for JPEG module
3193 static void JPEG_SetColorGrayScale(JPEG_HandleTypeDef
*hjpeg
)
3195 /*Set Number of color components to 1*/
3196 hjpeg
->Instance
->CONFR1
&= ~(JPEG_CONFR1_NF
| JPEG_CONFR1_NS
);
3198 /*in GrayScale use 1 single Quantization table (Table 0)*/
3199 /*in GrayScale use only one couple of AC/DC huffman table (table 0)*/
3201 /*Reset CONFR4 register*/
3202 hjpeg
->Instance
->CONFR4
= 0;
3203 /*Set Horizental and Vertical sampling factor , number of blocks , Quantization table and Huffman AC/DC tables for component 0*/
3204 hjpeg
->Instance
->CONFR4
|= JPEG_CONFR4_HSF_0
| JPEG_CONFR4_VSF_0
;
3208 * @brief Configure the JPEG registers for CMYK color space
3209 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
3210 * the configuration information for JPEG module
3213 static void JPEG_SetColorCMYK(JPEG_HandleTypeDef
*hjpeg
)
3215 uint32_t ySamplingH
;
3216 uint32_t ySamplingV
;
3219 /*Set Number of color components to 4*/
3220 hjpeg
->Instance
->CONFR1
|= (JPEG_CONFR1_NF
| JPEG_CONFR1_NS
);
3222 /* compute MCU block size and Y, Cb ,Cr sampling factors*/
3223 if (hjpeg
->Conf
.ChromaSubsampling
== JPEG_420_SUBSAMPLING
)
3225 ySamplingH
= JPEG_CONFR4_HSF_1
; /* Hs = 2*/
3226 ySamplingV
= JPEG_CONFR4_VSF_1
; /* Vs = 2*/
3228 yblockNb
= 0x30; /* 4 blocks of 8x8*/
3230 else if (hjpeg
->Conf
.ChromaSubsampling
== JPEG_422_SUBSAMPLING
)
3232 ySamplingH
= JPEG_CONFR4_HSF_1
; /* Hs = 2*/
3233 ySamplingV
= JPEG_CONFR4_VSF_0
; /* Vs = 1*/
3235 yblockNb
= 0x10; /* 2 blocks of 8x8*/
3237 else /*JPEG_444_SUBSAMPLING and default*/
3239 ySamplingH
= JPEG_CONFR4_HSF_0
; /* Hs = 1*/
3240 ySamplingV
= JPEG_CONFR4_VSF_0
; /* Vs = 1*/
3242 yblockNb
= 0; /* 1 block of 8x8*/
3245 /*Reset CONFR4 register*/
3246 hjpeg
->Instance
->CONFR4
= 0;
3247 /*Set Horizental and Vertical sampling factor , number of blocks , Quantization table and Huffman AC/DC tables for component 0*/
3248 hjpeg
->Instance
->CONFR4
|= (ySamplingH
| ySamplingV
| (yblockNb
& JPEG_CONFR4_NB
));
3250 /*Reset CONFR5 register*/
3251 hjpeg
->Instance
->CONFR5
= 0;
3252 /*Set Horizental and Vertical sampling factor , number of blocks , Quantization table and Huffman AC/DC tables for component 1*/
3253 hjpeg
->Instance
->CONFR5
|= (JPEG_CONFR5_HSF_0
| JPEG_CONFR5_VSF_0
);
3255 /*Reset CONFR6 register*/
3256 hjpeg
->Instance
->CONFR6
= 0;
3257 /*Set Horizental and Vertical sampling factor , number of blocks , Quantization table and Huffman AC/DC tables for component 2*/
3258 hjpeg
->Instance
->CONFR6
|= (JPEG_CONFR6_HSF_0
| JPEG_CONFR6_VSF_0
);
3260 /*Reset CONFR7 register*/
3261 hjpeg
->Instance
->CONFR7
= 0;
3262 /*Set Horizental and Vertical sampling factor , number of blocks , Quantization table and Huffman AC/DC tables for component 3*/
3263 hjpeg
->Instance
->CONFR7
|= (JPEG_CONFR7_HSF_0
| JPEG_CONFR7_VSF_0
);
3267 * @brief Init the JPEG encoding/decoding process in case of Polling or Interrupt and DMA
3268 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
3269 * the configuration information for JPEG module
3272 static void JPEG_Init_Process(JPEG_HandleTypeDef
*hjpeg
)
3275 hjpeg
->Context
&= (~(JPEG_CONTEXT_PAUSE_INPUT
| JPEG_CONTEXT_PAUSE_OUTPUT
));
3277 if ((hjpeg
->Context
& JPEG_CONTEXT_OPERATION_MASK
) == JPEG_CONTEXT_DECODE
)
3279 /*Set JPEG Codec to Decoding mode */
3280 hjpeg
->Instance
->CONFR1
|= JPEG_CONFR1_DE
;
3282 else /* JPEG_CONTEXT_ENCODE */
3284 /*Set JPEG Codec to Encoding mode */
3285 hjpeg
->Instance
->CONFR1
&= ~JPEG_CONFR1_DE
;
3288 /*Stop JPEG processing */
3289 hjpeg
->Instance
->CONFR0
&= ~JPEG_CONFR0_START
;
3291 /* Disable All Interrupts */
3292 __HAL_JPEG_DISABLE_IT(hjpeg
, JPEG_INTERRUPT_MASK
);
3294 /* Flush input and output FIFOs*/
3295 hjpeg
->Instance
->CR
|= JPEG_CR_IFF
;
3296 hjpeg
->Instance
->CR
|= JPEG_CR_OFF
;
3298 /* Clear all flags */
3299 __HAL_JPEG_CLEAR_FLAG(hjpeg
, JPEG_FLAG_ALL
);
3301 /*Start Encoding/Decoding*/
3302 hjpeg
->Instance
->CONFR0
|= JPEG_CONFR0_START
;
3304 if ((hjpeg
->Context
& JPEG_CONTEXT_METHOD_MASK
) == JPEG_CONTEXT_IT
)
3306 /*Enable IN/OUT, end of Conversation, and end of header parsing interruptions*/
3307 __HAL_JPEG_ENABLE_IT(hjpeg
, JPEG_IT_IFT
| JPEG_IT_IFNF
| JPEG_IT_OFT
| JPEG_IT_OFNE
| JPEG_IT_EOC
| JPEG_IT_HPD
);
3309 else if ((hjpeg
->Context
& JPEG_CONTEXT_METHOD_MASK
) == JPEG_CONTEXT_DMA
)
3311 /*Enable End Of Conversation, and End Of Header parsing interruptions*/
3312 __HAL_JPEG_ENABLE_IT(hjpeg
, JPEG_IT_EOC
| JPEG_IT_HPD
);
3322 * @brief JPEG encoding/decoding process in case of Polling or Interrupt
3323 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
3324 * the configuration information for JPEG module
3325 * @retval JPEG_PROCESS_DONE if the process has ends else JPEG_PROCESS_ONGOING
3327 static uint32_t JPEG_Process(JPEG_HandleTypeDef
*hjpeg
)
3329 uint32_t tmpContext
;
3331 /*End of header processing flag rised*/
3332 if ((hjpeg
->Context
& JPEG_CONTEXT_OPERATION_MASK
) == JPEG_CONTEXT_DECODE
)
3334 if (__HAL_JPEG_GET_FLAG(hjpeg
, JPEG_FLAG_HPDF
) != 0UL)
3336 /*Call Header parsing complet callback */
3337 (void) HAL_JPEG_GetInfo(hjpeg
, &hjpeg
->Conf
);
3338 /* Reset the ImageQuality */
3339 hjpeg
->Conf
.ImageQuality
= 0;
3340 /* Note : the image quality is only available at the end of the decoding operation */
3341 /* at the current stage the calculated image quality is not correct so reset it */
3343 /*Call Info Ready callback */
3344 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
3345 hjpeg
->InfoReadyCallback(hjpeg
, &hjpeg
->Conf
);
3347 HAL_JPEG_InfoReadyCallback(hjpeg
, &hjpeg
->Conf
);
3348 #endif /* USE_HAL_JPEG_REGISTER_CALLBACKS */
3350 __HAL_JPEG_DISABLE_IT(hjpeg
, JPEG_IT_HPD
);
3352 /* Clear header processing done flag */
3353 __HAL_JPEG_CLEAR_FLAG(hjpeg
, JPEG_FLAG_HPDF
);
3357 /*Input FIFO status handling*/
3358 if ((hjpeg
->Context
& JPEG_CONTEXT_PAUSE_INPUT
) == 0UL)
3360 if (__HAL_JPEG_GET_FLAG(hjpeg
, JPEG_FLAG_IFTF
) != 0UL)
3362 /*Input FIFO threshold flag rised*/
3363 /*JPEG_FIFO_TH_SIZE words can be written in */
3364 JPEG_ReadInputData(hjpeg
, JPEG_FIFO_TH_SIZE
);
3366 else if (__HAL_JPEG_GET_FLAG(hjpeg
, JPEG_FLAG_IFNFF
) != 0UL)
3368 /*Input FIFO Not Full flag rised*/
3369 /*32-bit value can be written in */
3370 JPEG_ReadInputData(hjpeg
, 1);
3379 /*Output FIFO flag handling*/
3380 if ((hjpeg
->Context
& JPEG_CONTEXT_PAUSE_OUTPUT
) == 0UL)
3382 if (__HAL_JPEG_GET_FLAG(hjpeg
, JPEG_FLAG_OFTF
) != 0UL)
3384 /*Output FIFO threshold flag rised*/
3385 /*JPEG_FIFO_TH_SIZE words can be read out */
3386 JPEG_StoreOutputData(hjpeg
, JPEG_FIFO_TH_SIZE
);
3388 else if (__HAL_JPEG_GET_FLAG(hjpeg
, JPEG_FLAG_OFNEF
) != 0UL)
3390 /*Output FIFO Not Empty flag rised*/
3391 /*32-bit value can be read out */
3392 JPEG_StoreOutputData(hjpeg
, 1);
3400 /*End of Conversion handling :i.e EOC flag is high and OFTF low and OFNEF low*/
3401 if (__HAL_JPEG_GET_FLAG(hjpeg
, JPEG_FLAG_EOCF
| JPEG_FLAG_OFTF
| JPEG_FLAG_OFNEF
) == JPEG_FLAG_EOCF
)
3403 /*Stop Encoding/Decoding*/
3404 hjpeg
->Instance
->CONFR0
&= ~JPEG_CONFR0_START
;
3406 if ((hjpeg
->Context
& JPEG_CONTEXT_METHOD_MASK
) == JPEG_CONTEXT_IT
)
3408 /* Disable All Interrupts */
3409 __HAL_JPEG_DISABLE_IT(hjpeg
, JPEG_INTERRUPT_MASK
);
3412 /* Clear all flags */
3413 __HAL_JPEG_CLEAR_FLAG(hjpeg
, JPEG_FLAG_ALL
);
3415 /*Call End of conversion callback */
3416 if (hjpeg
->JpegOutCount
> 0UL)
3418 /*Output Buffer is not empty, call DecodedDataReadyCallback*/
3419 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
3420 hjpeg
->DataReadyCallback(hjpeg
, hjpeg
->pJpegOutBuffPtr
, hjpeg
->JpegOutCount
);
3422 HAL_JPEG_DataReadyCallback(hjpeg
, hjpeg
->pJpegOutBuffPtr
, hjpeg
->JpegOutCount
);
3423 #endif /* USE_HAL_JPEG_REGISTER_CALLBACKS */
3425 hjpeg
->JpegOutCount
= 0;
3428 /*Reset Context Operation*/
3429 tmpContext
= hjpeg
->Context
;
3430 /*Clear all context fields execpt JPEG_CONTEXT_CONF_ENCODING and JPEG_CONTEXT_CUSTOM_TABLES*/
3431 hjpeg
->Context
&= (JPEG_CONTEXT_CONF_ENCODING
| JPEG_CONTEXT_CUSTOM_TABLES
);
3433 /* Process Unlocked */
3434 __HAL_UNLOCK(hjpeg
);
3436 /* Change the JPEG state */
3437 hjpeg
->State
= HAL_JPEG_STATE_READY
;
3439 /*Call End of Encoding/Decoding callback */
3440 if ((tmpContext
& JPEG_CONTEXT_OPERATION_MASK
) == JPEG_CONTEXT_DECODE
)
3442 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
3443 hjpeg
->DecodeCpltCallback(hjpeg
);
3445 HAL_JPEG_DecodeCpltCallback(hjpeg
);
3446 #endif /*USE_HAL_JPEG_REGISTER_CALLBACKS*/
3448 else /* JPEG_CONTEXT_ENCODE */
3450 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
3451 hjpeg
->EncodeCpltCallback(hjpeg
);
3453 HAL_JPEG_EncodeCpltCallback(hjpeg
);
3457 return JPEG_PROCESS_DONE
;
3461 return JPEG_PROCESS_ONGOING
;
3465 * @brief Store some output data from the JPEG peripheral to the output buffer.
3466 * This function is used when the JPEG peripheral has new data to output
3467 * in case of Polling or Interrupt process
3468 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
3469 * the configuration information for JPEG module
3470 * @param nbOutputWords Number of output words (of 32 bits) ready from the JPEG peripheral
3473 static void JPEG_StoreOutputData(JPEG_HandleTypeDef
*hjpeg
, uint32_t nbOutputWords
)
3480 if (hjpeg
->OutDataLength
>= (hjpeg
->JpegOutCount
+ (nbOutputWords
* 4UL)))
3482 for (index
= 0; index
< nbOutputWords
; index
++)
3484 /*Transfer 32 bits from the JPEG output FIFO*/
3485 dataword
= hjpeg
->Instance
->DOR
;
3486 hjpeg
->pJpegOutBuffPtr
[hjpeg
->JpegOutCount
] = (uint8_t)(dataword
& 0x000000FFUL
);
3487 hjpeg
->pJpegOutBuffPtr
[hjpeg
->JpegOutCount
+ 1UL] = (uint8_t)((dataword
& 0x0000FF00UL
) >> 8);
3488 hjpeg
->pJpegOutBuffPtr
[hjpeg
->JpegOutCount
+ 2UL] = (uint8_t)((dataword
& 0x00FF0000UL
) >> 16);
3489 hjpeg
->pJpegOutBuffPtr
[hjpeg
->JpegOutCount
+ 3UL] = (uint8_t)((dataword
& 0xFF000000UL
) >> 24);
3490 hjpeg
->JpegOutCount
+= 4UL;
3492 if (hjpeg
->OutDataLength
== hjpeg
->JpegOutCount
)
3494 /*Output Buffer is full, call DecodedDataReadyCallback*/
3495 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
3496 hjpeg
->DataReadyCallback(hjpeg
, hjpeg
->pJpegOutBuffPtr
, hjpeg
->JpegOutCount
);
3498 HAL_JPEG_DataReadyCallback(hjpeg
, hjpeg
->pJpegOutBuffPtr
, hjpeg
->JpegOutCount
);
3499 #endif /*USE_HAL_JPEG_REGISTER_CALLBACKS*/
3500 hjpeg
->JpegOutCount
= 0;
3503 else if (hjpeg
->OutDataLength
> hjpeg
->JpegOutCount
)
3505 nb_words
= (hjpeg
->OutDataLength
- hjpeg
->JpegOutCount
) / 4UL;
3506 for (index
= 0; index
< nb_words
; index
++)
3508 /*Transfer 32 bits from the JPEG output FIFO*/
3509 dataword
= hjpeg
->Instance
->DOR
;
3510 hjpeg
->pJpegOutBuffPtr
[hjpeg
->JpegOutCount
] = (uint8_t)(dataword
& 0x000000FFUL
);
3511 hjpeg
->pJpegOutBuffPtr
[hjpeg
->JpegOutCount
+ 1UL] = (uint8_t)((dataword
& 0x0000FF00UL
) >> 8);
3512 hjpeg
->pJpegOutBuffPtr
[hjpeg
->JpegOutCount
+ 2UL] = (uint8_t)((dataword
& 0x00FF0000UL
) >> 16);
3513 hjpeg
->pJpegOutBuffPtr
[hjpeg
->JpegOutCount
+ 3UL] = (uint8_t)((dataword
& 0xFF000000UL
) >> 24);
3514 hjpeg
->JpegOutCount
+= 4UL;
3516 if (hjpeg
->OutDataLength
== hjpeg
->JpegOutCount
)
3518 /*Output Buffer is full, call DecodedDataReadyCallback*/
3519 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
3520 hjpeg
->DataReadyCallback(hjpeg
, hjpeg
->pJpegOutBuffPtr
, hjpeg
->JpegOutCount
);
3522 HAL_JPEG_DataReadyCallback(hjpeg
, hjpeg
->pJpegOutBuffPtr
, hjpeg
->JpegOutCount
);
3523 #endif /* USE_HAL_JPEG_REGISTER_CALLBACKS */
3524 hjpeg
->JpegOutCount
= 0;
3528 nb_bytes
= hjpeg
->OutDataLength
- hjpeg
->JpegOutCount
;
3529 dataword
= hjpeg
->Instance
->DOR
;
3530 for (index
= 0; index
< nb_bytes
; index
++)
3532 hjpeg
->pJpegOutBuffPtr
[hjpeg
->JpegOutCount
] = (uint8_t)((dataword
>> (8UL * (index
& 0x3UL
))) & 0xFFUL
);
3533 hjpeg
->JpegOutCount
++;
3535 /*Output Buffer is full, call DecodedDataReadyCallback*/
3536 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
3537 hjpeg
->DataReadyCallback(hjpeg
, hjpeg
->pJpegOutBuffPtr
, hjpeg
->JpegOutCount
);
3539 HAL_JPEG_DataReadyCallback(hjpeg
, hjpeg
->pJpegOutBuffPtr
, hjpeg
->JpegOutCount
);
3540 #endif /* USE_HAL_JPEG_REGISTER_CALLBACKS */
3542 hjpeg
->JpegOutCount
= 0;
3544 nb_bytes
= 4UL - nb_bytes
;
3545 for (index
= nb_bytes
; index
< 4UL; index
++)
3547 hjpeg
->pJpegOutBuffPtr
[hjpeg
->JpegOutCount
] = (uint8_t)((dataword
>> (8UL * index
)) & 0xFFUL
);
3548 hjpeg
->JpegOutCount
++;
3559 * @brief Read some input Data from the input buffer.
3560 * This function is used when the JPEG peripheral needs new data
3561 * in case of Polling or Interrupt process
3562 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
3563 * the configuration information for JPEG module
3564 * @param nbRequestWords Number of input words (of 32 bits) that the JPE peripheral request
3567 static void JPEG_ReadInputData(JPEG_HandleTypeDef
*hjpeg
, uint32_t nbRequestWords
)
3569 uint32_t nb_bytes
= 0;
3573 uint32_t input_count
;
3575 if ((hjpeg
->InDataLength
== 0UL) || (nbRequestWords
== 0UL))
3577 /* No more Input data : nothing to do*/
3578 (void) HAL_JPEG_Pause(hjpeg
, JPEG_PAUSE_RESUME_INPUT
);
3580 else if (hjpeg
->InDataLength
> hjpeg
->JpegInCount
)
3582 nb_bytes
= hjpeg
->InDataLength
- hjpeg
->JpegInCount
;
3584 else if (hjpeg
->InDataLength
== hjpeg
->JpegInCount
)
3586 /*Call HAL_JPEG_GetDataCallback to get new data */
3587 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
3588 hjpeg
->GetDataCallback(hjpeg
, hjpeg
->JpegInCount
);
3590 HAL_JPEG_GetDataCallback(hjpeg
, hjpeg
->JpegInCount
);
3591 #endif /*USE_HAL_JPEG_REGISTER_CALLBACKS*/
3593 if (hjpeg
->InDataLength
> 4UL)
3595 hjpeg
->InDataLength
= hjpeg
->InDataLength
- (hjpeg
->InDataLength
% 4UL);
3597 hjpeg
->JpegInCount
= 0;
3598 nb_bytes
= hjpeg
->InDataLength
;
3604 if (((hjpeg
->Context
& JPEG_CONTEXT_PAUSE_INPUT
) == 0UL) && (nb_bytes
> 0UL))
3606 nb_words
= nb_bytes
/ 4UL;
3607 if (nb_words
>= nbRequestWords
)
3609 for (index
= 0; index
< nbRequestWords
; index
++)
3611 input_count
= hjpeg
->JpegInCount
;
3612 hjpeg
->Instance
->DIR = (((uint32_t)(hjpeg
->pJpegInBuffPtr
[input_count
])) | \
3613 (((uint32_t)(hjpeg
->pJpegInBuffPtr
[input_count
+ 1UL])) << 8) | \
3614 (((uint32_t)(hjpeg
->pJpegInBuffPtr
[input_count
+ 2UL])) << 16) | \
3615 (((uint32_t)(hjpeg
->pJpegInBuffPtr
[input_count
+ 3UL])) << 24));
3617 hjpeg
->JpegInCount
+= 4UL;
3620 else /*nb_words < nbRequestWords*/
3624 for (index
= 0; index
< nb_words
; index
++)
3626 input_count
= hjpeg
->JpegInCount
;
3627 hjpeg
->Instance
->DIR = (((uint32_t)(hjpeg
->pJpegInBuffPtr
[input_count
])) | \
3628 (((uint32_t)(hjpeg
->pJpegInBuffPtr
[input_count
+ 1UL])) << 8) | \
3629 (((uint32_t)(hjpeg
->pJpegInBuffPtr
[input_count
+ 2UL])) << 16) | \
3630 (((uint32_t)(hjpeg
->pJpegInBuffPtr
[input_count
+ 3UL])) << 24));
3632 hjpeg
->JpegInCount
+= 4UL;
3639 for (index
= 0; index
< nb_bytes
; index
++)
3641 dataword
|= (uint32_t)hjpeg
->pJpegInBuffPtr
[hjpeg
->JpegInCount
] << (8UL * (index
& 0x03UL
));
3642 hjpeg
->JpegInCount
++;
3644 hjpeg
->Instance
->DIR = dataword
;
3651 * @brief Start the JPEG DMA process (encoding/decoding)
3652 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
3653 * the configuration information for JPEG module
3654 * @retval JPEG_PROCESS_DONE if process ends else JPEG_PROCESS_ONGOING
3656 static HAL_StatusTypeDef
JPEG_DMA_StartProcess(JPEG_HandleTypeDef
*hjpeg
)
3658 uint32_t inXfrSize
, outXfrSize
;
3660 /*if the MDMA In is triggred with JPEG In FIFO Threshold flag
3661 then MDMA In buffer size is 32 bytes
3662 else (MDMA In is triggred with JPEG In FIFO not full flag)
3663 then MDMA In buffer size is 4 bytes
3665 inXfrSize
= hjpeg
->hdmain
->Init
.BufferTransferLength
;
3667 /*if the MDMA Out is triggred with JPEG Out FIFO Threshold flag
3668 then MDMA out buffer size is 32 bytes
3669 else (MDMA Out is triggred with JPEG Out FIFO not empty flag)
3670 then MDMA buffer size is 4 bytes
3672 outXfrSize
= hjpeg
->hdmaout
->Init
.BufferTransferLength
;
3674 if ((hjpeg
->InDataLength
< inXfrSize
) || (hjpeg
->OutDataLength
< outXfrSize
))
3678 /* Set the JPEG MDMA In transfer complete callback */
3679 hjpeg
->hdmain
->XferCpltCallback
= JPEG_MDMAInCpltCallback
;
3680 /* Set the MDMA In error callback */
3681 hjpeg
->hdmain
->XferErrorCallback
= JPEG_MDMAErrorCallback
;
3683 /* Set the JPEG MDMA Out transfer complete callback */
3684 hjpeg
->hdmaout
->XferCpltCallback
= JPEG_MDMAOutCpltCallback
;
3685 /* Set the MDMA In error callback */
3686 hjpeg
->hdmaout
->XferErrorCallback
= JPEG_MDMAErrorCallback
;
3687 /* Set the MDMA Out Abort callback */
3688 hjpeg
->hdmaout
->XferAbortCallback
= JPEG_MDMAOutAbortCallback
;
3690 if ((inXfrSize
== 0UL) || (outXfrSize
== 0UL))
3692 hjpeg
->ErrorCode
|= HAL_JPEG_ERROR_DMA
;
3695 /*MDMA transfer size (BNDTR) must be a multiple of MDMA buffer size (TLEN)*/
3696 hjpeg
->InDataLength
= hjpeg
->InDataLength
- (hjpeg
->InDataLength
% inXfrSize
);
3698 /*MDMA transfer size (BNDTR) must be a multiple of MDMA buffer size (TLEN)*/
3699 hjpeg
->OutDataLength
= hjpeg
->OutDataLength
- (hjpeg
->OutDataLength
% outXfrSize
);
3702 /* Start MDMA FIFO Out transfer */
3703 if (HAL_MDMA_Start_IT(hjpeg
->hdmaout
, (uint32_t)&hjpeg
->Instance
->DOR
, (uint32_t)hjpeg
->pJpegOutBuffPtr
,
3704 hjpeg
->OutDataLength
, 1) != HAL_OK
)
3706 hjpeg
->ErrorCode
|= HAL_JPEG_ERROR_DMA
;
3709 /* Start DMA FIFO In transfer */
3710 if (HAL_MDMA_Start_IT(hjpeg
->hdmain
, (uint32_t)hjpeg
->pJpegInBuffPtr
, (uint32_t)&hjpeg
->Instance
->DIR,
3711 hjpeg
->InDataLength
, 1) != HAL_OK
)
3713 hjpeg
->ErrorCode
|= HAL_JPEG_ERROR_DMA
;
3721 * @brief Continue the current JPEG DMA process (encoding/decoding)
3722 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
3723 * the configuration information for JPEG module
3724 * @retval JPEG_PROCESS_DONE if process ends else JPEG_PROCESS_ONGOING
3726 static void JPEG_DMA_ContinueProcess(JPEG_HandleTypeDef
*hjpeg
)
3728 /*End of header processing flag rises*/
3729 if ((hjpeg
->Context
& JPEG_CONTEXT_OPERATION_MASK
) == JPEG_CONTEXT_DECODE
)
3731 if (__HAL_JPEG_GET_FLAG(hjpeg
, JPEG_FLAG_HPDF
) != 0UL)
3733 /*Call Header parsing complete callback */
3734 (void) HAL_JPEG_GetInfo(hjpeg
, &hjpeg
->Conf
);
3736 /* Reset the ImageQuality */
3737 hjpeg
->Conf
.ImageQuality
= 0;
3738 /* Note : the image quality is only available at the end of the decoding operation */
3739 /* at the current stage the calculated image quality is not correct so reset it */
3741 /*Call Info Ready callback */
3742 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
3743 hjpeg
->InfoReadyCallback(hjpeg
, &hjpeg
->Conf
);
3745 HAL_JPEG_InfoReadyCallback(hjpeg
, &hjpeg
->Conf
);
3746 #endif /* USE_HAL_JPEG_REGISTER_CALLBACKS */
3748 __HAL_JPEG_DISABLE_IT(hjpeg
, JPEG_IT_HPD
);
3750 /* Clear header processing done flag */
3751 __HAL_JPEG_CLEAR_FLAG(hjpeg
, JPEG_FLAG_HPDF
);
3755 /*End of Conversion handling*/
3756 if (__HAL_JPEG_GET_FLAG(hjpeg
, JPEG_FLAG_EOCF
) != 0UL)
3759 hjpeg
->Context
|= JPEG_CONTEXT_ENDING_DMA
;
3761 /*Stop Encoding/Decoding*/
3762 hjpeg
->Instance
->CONFR0
&= ~JPEG_CONFR0_START
;
3764 __HAL_JPEG_DISABLE_IT(hjpeg
, JPEG_INTERRUPT_MASK
);
3766 /* Clear all flags */
3767 __HAL_JPEG_CLEAR_FLAG(hjpeg
, JPEG_FLAG_ALL
);
3769 if (hjpeg
->hdmain
->State
== HAL_MDMA_STATE_BUSY
)
3771 /* Stop the MDMA In Xfer*/
3772 (void) HAL_MDMA_Abort_IT(hjpeg
->hdmain
);
3775 if (hjpeg
->hdmaout
->State
== HAL_MDMA_STATE_BUSY
)
3777 /* Stop the MDMA out Xfer*/
3778 (void) HAL_MDMA_Abort_IT(hjpeg
->hdmaout
);
3782 JPEG_DMA_EndProcess(hjpeg
);
3790 * @brief Finalize the current JPEG DMA process (encoding/decoding)
3791 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
3792 * the configuration information for JPEG module
3793 * @retval JPEG_PROCESS_DONE
3795 static void JPEG_DMA_EndProcess(JPEG_HandleTypeDef
*hjpeg
)
3797 uint32_t tmpContext
;
3798 hjpeg
->JpegOutCount
= hjpeg
->OutDataLength
- (hjpeg
->hdmaout
->Instance
->CBNDTR
& MDMA_CBNDTR_BNDT
);
3800 /*if Output Buffer is full, call HAL_JPEG_DataReadyCallback*/
3801 if (hjpeg
->JpegOutCount
== hjpeg
->OutDataLength
)
3803 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
3804 hjpeg
->DataReadyCallback(hjpeg
, hjpeg
->pJpegOutBuffPtr
, hjpeg
->JpegOutCount
);
3806 HAL_JPEG_DataReadyCallback(hjpeg
, hjpeg
->pJpegOutBuffPtr
, hjpeg
->JpegOutCount
);
3807 #endif /* USE_HAL_JPEG_REGISTER_CALLBACKS */
3809 hjpeg
->JpegOutCount
= 0;
3812 /*Check if remaining data in the output FIFO*/
3813 if (__HAL_JPEG_GET_FLAG(hjpeg
, JPEG_FLAG_OFNEF
) == 0UL)
3815 if (hjpeg
->JpegOutCount
> 0UL)
3817 /*Output Buffer is not empty, call DecodedDataReadyCallback*/
3818 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
3819 hjpeg
->DataReadyCallback(hjpeg
, hjpeg
->pJpegOutBuffPtr
, hjpeg
->JpegOutCount
);
3821 HAL_JPEG_DataReadyCallback(hjpeg
, hjpeg
->pJpegOutBuffPtr
, hjpeg
->JpegOutCount
);
3822 #endif /* USE_HAL_JPEG_REGISTER_CALLBACKS */
3824 hjpeg
->JpegOutCount
= 0;
3827 /*Stop Encoding/Decoding*/
3828 hjpeg
->Instance
->CONFR0
&= ~JPEG_CONFR0_START
;
3830 tmpContext
= hjpeg
->Context
;
3831 /*Clear all context fileds execpt JPEG_CONTEXT_CONF_ENCODING and JPEG_CONTEXT_CUSTOM_TABLES*/
3832 hjpeg
->Context
&= (JPEG_CONTEXT_CONF_ENCODING
| JPEG_CONTEXT_CUSTOM_TABLES
);
3834 /* Process Unlocked */
3835 __HAL_UNLOCK(hjpeg
);
3837 /* Change the JPEG state */
3838 hjpeg
->State
= HAL_JPEG_STATE_READY
;
3840 /*Call End of Encoding/Decoding callback */
3841 if ((tmpContext
& JPEG_CONTEXT_OPERATION_MASK
) == JPEG_CONTEXT_DECODE
)
3843 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
3844 hjpeg
->DecodeCpltCallback(hjpeg
);
3846 HAL_JPEG_DecodeCpltCallback(hjpeg
);
3847 #endif /* USE_HAL_JPEG_REGISTER_CALLBACKS */
3849 else /* JPEG_CONTEXT_ENCODE */
3851 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
3852 hjpeg
->EncodeCpltCallback(hjpeg
);
3854 HAL_JPEG_EncodeCpltCallback(hjpeg
);
3855 #endif /* USE_HAL_JPEG_REGISTER_CALLBACKS */
3858 else if ((hjpeg
->Context
& JPEG_CONTEXT_PAUSE_OUTPUT
) == 0UL)
3860 JPEG_DMA_PollResidualData(hjpeg
);
3870 * @brief Poll residual output data when DMA process (encoding/decoding)
3871 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
3872 * the configuration information for JPEG module
3875 static void JPEG_DMA_PollResidualData(JPEG_HandleTypeDef
*hjpeg
)
3877 uint32_t tmpContext
;
3881 for (count
= JPEG_FIFO_SIZE
; count
> 0UL; count
--)
3883 if ((hjpeg
->Context
& JPEG_CONTEXT_PAUSE_OUTPUT
) == 0UL)
3885 if (__HAL_JPEG_GET_FLAG(hjpeg
, JPEG_FLAG_OFNEF
) != 0UL)
3887 dataOut
= hjpeg
->Instance
->DOR
;
3888 hjpeg
->pJpegOutBuffPtr
[hjpeg
->JpegOutCount
] = (uint8_t)(dataOut
& 0x000000FFUL
);
3889 hjpeg
->pJpegOutBuffPtr
[hjpeg
->JpegOutCount
+ 1UL] = (uint8_t)((dataOut
& 0x0000FF00UL
) >> 8);
3890 hjpeg
->pJpegOutBuffPtr
[hjpeg
->JpegOutCount
+ 2UL] = (uint8_t)((dataOut
& 0x00FF0000UL
) >> 16);
3891 hjpeg
->pJpegOutBuffPtr
[hjpeg
->JpegOutCount
+ 3UL] = (uint8_t)((dataOut
& 0xFF000000UL
) >> 24);
3892 hjpeg
->JpegOutCount
+= 4UL;
3894 if (hjpeg
->JpegOutCount
== hjpeg
->OutDataLength
)
3896 /*Output Buffer is full, call HAL_JPEG_DataReadyCallback*/
3897 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
3898 hjpeg
->DataReadyCallback(hjpeg
, hjpeg
->pJpegOutBuffPtr
, hjpeg
->JpegOutCount
);
3900 HAL_JPEG_DataReadyCallback(hjpeg
, hjpeg
->pJpegOutBuffPtr
, hjpeg
->JpegOutCount
);
3901 #endif /* USE_HAL_JPEG_REGISTER_CALLBACKS */
3903 hjpeg
->JpegOutCount
= 0;
3910 tmpContext
= hjpeg
->Context
;
3912 if ((__HAL_JPEG_GET_FLAG(hjpeg
, JPEG_FLAG_OFNEF
) == 0UL) || ((tmpContext
& JPEG_CONTEXT_PAUSE_OUTPUT
) == 0UL))
3914 /*Stop Encoding/Decoding*/
3915 hjpeg
->Instance
->CONFR0
&= ~JPEG_CONFR0_START
;
3917 if (hjpeg
->JpegOutCount
> 0UL)
3919 /*Output Buffer is not empty, call DecodedDataReadyCallback*/
3920 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
3921 hjpeg
->DataReadyCallback(hjpeg
, hjpeg
->pJpegOutBuffPtr
, hjpeg
->JpegOutCount
);
3923 HAL_JPEG_DataReadyCallback(hjpeg
, hjpeg
->pJpegOutBuffPtr
, hjpeg
->JpegOutCount
);
3924 #endif /* USE_HAL_JPEG_REGISTER_CALLBACKS */
3926 hjpeg
->JpegOutCount
= 0;
3929 tmpContext
= hjpeg
->Context
;
3930 /*Clear all context fileds execpt JPEG_CONTEXT_CONF_ENCODING and JPEG_CONTEXT_CUSTOM_TABLES*/
3931 hjpeg
->Context
&= (JPEG_CONTEXT_CONF_ENCODING
| JPEG_CONTEXT_CUSTOM_TABLES
);
3933 /* Process Unlocked */
3934 __HAL_UNLOCK(hjpeg
);
3936 /* Change the JPEG state */
3937 hjpeg
->State
= HAL_JPEG_STATE_READY
;
3939 /*Call End of Encoding/Decoding callback */
3940 if ((tmpContext
& JPEG_CONTEXT_OPERATION_MASK
) == JPEG_CONTEXT_DECODE
)
3942 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
3943 hjpeg
->DecodeCpltCallback(hjpeg
);
3945 HAL_JPEG_DecodeCpltCallback(hjpeg
);
3946 #endif /* USE_HAL_JPEG_REGISTER_CALLBACKS */
3948 else /* JPEG_CONTEXT_ENCODE */
3950 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
3951 hjpeg
->EncodeCpltCallback(hjpeg
);
3953 HAL_JPEG_EncodeCpltCallback(hjpeg
);
3954 #endif /* USE_HAL_JPEG_REGISTER_CALLBACKS */
3960 * @brief MDMA input transfer complete callback
3961 * @param hmdma pointer to a MDMA_HandleTypeDef structure.
3964 static void JPEG_MDMAInCpltCallback(MDMA_HandleTypeDef
*hmdma
)
3968 JPEG_HandleTypeDef
*hjpeg
= (JPEG_HandleTypeDef
*)((MDMA_HandleTypeDef
*)hmdma
)->Parent
;
3970 /* Disable The JPEG IT so the MDMA Input Callback can not be interrupted by the JPEG EOC IT or JPEG HPD IT */
3971 __HAL_JPEG_DISABLE_IT(hjpeg
, JPEG_INTERRUPT_MASK
);
3973 /* Check if context method is DMA and we are not in ending DMA stage */
3974 if ((hjpeg
->Context
& (JPEG_CONTEXT_METHOD_MASK
| JPEG_CONTEXT_ENDING_DMA
)) == JPEG_CONTEXT_DMA
)
3977 /*if the MDMA In is triggred with JPEG In FIFO Threshold flag
3978 then MDMA In buffer size is 32 bytes
3979 else (MDMA In is triggred with JPEG In FIFO not full flag)
3980 then MDMA In buffer size is 4 bytes
3982 inXfrSize
= hjpeg
->hdmain
->Init
.BufferTransferLength
;
3984 hjpeg
->JpegInCount
= hjpeg
->InDataLength
- (hmdma
->Instance
->CBNDTR
& MDMA_CBNDTR_BNDT
);
3986 /*Call HAL_JPEG_GetDataCallback to get new data */
3987 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
3988 hjpeg
->GetDataCallback(hjpeg
, hjpeg
->JpegInCount
);
3990 HAL_JPEG_GetDataCallback(hjpeg
, hjpeg
->JpegInCount
);
3991 #endif /* USE_HAL_JPEG_REGISTER_CALLBACKS */
3994 if (hjpeg
->InDataLength
>= inXfrSize
)
3996 if (inXfrSize
== 0UL)
3998 hjpeg
->ErrorCode
|= HAL_JPEG_ERROR_DMA
;
3999 hjpeg
->State
= HAL_JPEG_STATE_ERROR
;
4000 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
4001 hjpeg
->ErrorCallback(hjpeg
);
4003 HAL_JPEG_ErrorCallback(hjpeg
);
4004 #endif /* USE_HAL_JPEG_REGISTER_CALLBACKS */
4007 /*JPEG Input MDMA transfer data number must be multiple of MDMA buffer size
4008 as the destination is a 32 bits register */
4009 hjpeg
->InDataLength
= hjpeg
->InDataLength
- (hjpeg
->InDataLength
% inXfrSize
);
4012 else if (hjpeg
->InDataLength
> 0UL)
4014 /* Transfer the remaining Data, must be multiple of source data size (byte) and destination data size (word) */
4015 if ((hjpeg
->InDataLength
% 4UL) != 0UL)
4017 hjpeg
->InDataLength
= ((hjpeg
->InDataLength
/ 4UL) + 1UL) * 4UL;
4025 if (((hjpeg
->Context
& JPEG_CONTEXT_PAUSE_INPUT
) == 0UL) && (hjpeg
->InDataLength
> 0UL))
4027 /* Start MDMA FIFO In transfer */
4028 if (HAL_MDMA_Start_IT(hjpeg
->hdmain
, (uint32_t)hjpeg
->pJpegInBuffPtr
, (uint32_t)&hjpeg
->Instance
->DIR,
4029 hjpeg
->InDataLength
, 1) != HAL_OK
)
4031 hjpeg
->ErrorCode
|= HAL_JPEG_ERROR_DMA
;
4032 hjpeg
->State
= HAL_JPEG_STATE_ERROR
;
4033 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
4034 hjpeg
->ErrorCallback(hjpeg
);
4036 HAL_JPEG_ErrorCallback(hjpeg
);
4037 #endif /* USE_HAL_JPEG_REGISTER_CALLBACKS */
4042 /* JPEG Conversion still on going : Enable the JPEG IT */
4043 __HAL_JPEG_ENABLE_IT(hjpeg
, JPEG_IT_EOC
| JPEG_IT_HPD
);
4048 * @brief MDMA output transfer complete callback
4049 * @param hmdma pointer to a MDMA_HandleTypeDef structure.
4052 static void JPEG_MDMAOutCpltCallback(MDMA_HandleTypeDef
*hmdma
)
4054 JPEG_HandleTypeDef
*hjpeg
= (JPEG_HandleTypeDef
*)((MDMA_HandleTypeDef
*)hmdma
)->Parent
;
4057 /* Disable The JPEG IT so the MDMA Output Callback can not be interrupted by the JPEG EOC IT or JPEG HPD IT */
4058 __HAL_JPEG_DISABLE_IT(hjpeg
, JPEG_INTERRUPT_MASK
);
4060 if ((hjpeg
->Context
& (JPEG_CONTEXT_METHOD_MASK
| JPEG_CONTEXT_ENDING_DMA
)) ==
4061 JPEG_CONTEXT_DMA
) /* Check if context method is DMA and we are not in ending DMA stage */
4063 if (__HAL_JPEG_GET_FLAG(hjpeg
, JPEG_FLAG_EOCF
) == 0UL)
4065 hjpeg
->JpegOutCount
= hjpeg
->OutDataLength
- (hmdma
->Instance
->CBNDTR
& MDMA_CBNDTR_BNDT
);
4067 /*Output Buffer is full, call HAL_JPEG_DataReadyCallback*/
4068 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
4069 hjpeg
->DataReadyCallback(hjpeg
, hjpeg
->pJpegOutBuffPtr
, hjpeg
->JpegOutCount
);
4071 HAL_JPEG_DataReadyCallback(hjpeg
, hjpeg
->pJpegOutBuffPtr
, hjpeg
->JpegOutCount
);
4072 #endif /* USE_HAL_JPEG_REGISTER_CALLBACKS */
4074 if ((hjpeg
->Context
& JPEG_CONTEXT_PAUSE_OUTPUT
) == 0UL)
4076 /* Start MDMA FIFO Out transfer */
4077 if (HAL_MDMA_Start_IT(hjpeg
->hdmaout
, (uint32_t)&hjpeg
->Instance
->DOR
, (uint32_t)hjpeg
->pJpegOutBuffPtr
,
4078 hjpeg
->OutDataLength
, 1) != HAL_OK
)
4080 hjpeg
->ErrorCode
|= HAL_JPEG_ERROR_DMA
;
4081 hjpeg
->State
= HAL_JPEG_STATE_ERROR
;
4082 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
4083 hjpeg
->ErrorCallback(hjpeg
);
4085 HAL_JPEG_ErrorCallback(hjpeg
);
4086 #endif /* USE_HAL_JPEG_REGISTER_CALLBACKS */
4092 /* JPEG Conversion still on going : Enable the JPEG IT */
4093 __HAL_JPEG_ENABLE_IT(hjpeg
, JPEG_IT_EOC
| JPEG_IT_HPD
);
4099 * @brief MDMA Transfer error callback
4100 * @param hmdma pointer to a MDMA_HandleTypeDef structure.
4103 static void JPEG_MDMAErrorCallback(MDMA_HandleTypeDef
*hmdma
)
4105 JPEG_HandleTypeDef
*hjpeg
= (JPEG_HandleTypeDef
*)((MDMA_HandleTypeDef
*)hmdma
)->Parent
;
4107 /*Stop Encoding/Decoding*/
4108 hjpeg
->Instance
->CONFR0
&= ~JPEG_CONFR0_START
;
4110 /* Disable All Interrupts */
4111 __HAL_JPEG_DISABLE_IT(hjpeg
, JPEG_INTERRUPT_MASK
);
4113 hjpeg
->State
= HAL_JPEG_STATE_READY
;
4114 hjpeg
->ErrorCode
|= HAL_JPEG_ERROR_DMA
;
4116 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
4117 hjpeg
->ErrorCallback(hjpeg
);
4119 HAL_JPEG_ErrorCallback(hjpeg
);
4120 #endif /* USE_HAL_JPEG_REGISTER_CALLBACKS */
4124 * @brief MDMA output Abort callback
4125 * @param hmdma pointer to a MDMA_HandleTypeDef structure.
4128 static void JPEG_MDMAOutAbortCallback(MDMA_HandleTypeDef
*hmdma
)
4130 JPEG_HandleTypeDef
*hjpeg
= (JPEG_HandleTypeDef
*)((MDMA_HandleTypeDef
*)hmdma
)->Parent
;
4132 if ((hjpeg
->Context
& JPEG_CONTEXT_ENDING_DMA
) != 0UL)
4134 JPEG_DMA_EndProcess(hjpeg
);
4140 * @brief Calculate the decoded image quality (from 1 to 100)
4141 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
4142 * the configuration information for JPEG module
4143 * @retval JPEG image quality from 1 to 100.
4145 static uint32_t JPEG_GetQuality(JPEG_HandleTypeDef
*hjpeg
)
4147 uint32_t quality
= 0;
4148 uint32_t quantRow
, quantVal
, scale
, i
, j
;
4149 __IO
uint32_t *tableAddress
= hjpeg
->Instance
->QMEM0
;
4152 while (i
< (JPEG_QUANT_TABLE_SIZE
- 3UL))
4154 quantRow
= *tableAddress
;
4155 for (j
= 0; j
< 4UL; j
++)
4157 quantVal
= (quantRow
>> (8UL * j
)) & 0xFFUL
;
4158 if (quantVal
== 1UL)
4160 /* if Quantization value = 1 then quality is 100%*/
4165 /* Note that the quantization coefficients must be specified in the table in zigzag order */
4166 scale
= (quantVal
* 100UL) / ((uint32_t) hjpeg
->QuantTable0
[JPEG_ZIGZAG_ORDER
[i
+ j
]]);
4170 quality
+= (200UL - scale
) / 2UL;
4174 quality
+= 5000UL / scale
;
4183 return (quality
/ 64UL);
4193 #endif /* HAL_JPEG_MODULE_ENABLED */
4200 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/