Updated and Validated
[betaflight.git] / lib / main / STM32F7 / Drivers / STM32F7xx_HAL_Driver / Src / stm32f7xx_hal_jpeg.c
blob282d8f70dc4c5d20472caa5d1ae66afe667370fa
1 /**
2 ******************************************************************************
3 * @file stm32f7xx_hal_jpeg.c
4 * @author MCD Application Team
5 * @brief JPEG HAL module driver.
6 * This file provides firmware functions to manage the following
7 * functionalities of the JPEG encoder/decoder peripheral:
8 * + Initialization and de-initialization functions
9 * + JPEG processing functions encoding and decoding
10 * + JPEG decoding Getting Info and encoding configuration setting
11 * + JPEG enable/disable header parsing functions (for decoding)
12 * + JPEG Input/Output Buffer configuration.
13 * + JPEG callback functions
14 * + JPEG Abort/Pause/Resume functions
15 * + JPEG custom quantization tables setting functions
16 * + IRQ handler management
17 * + Peripheral State and Error functions
19 @verbatim
20 ==============================================================================
21 ##### How to use this driver #####
22 ==============================================================================
23 [..]
24 (#) Initialize the JPEG peripheral using HAL_JPEG_Init : No initialization parameters are required.
25 Only the call to HAL_JPEG_Init is necessary to initialize the JPEG peripheral.
27 (#) If operation is JPEG encoding use function HAL_JPEG_ConfigEncoding to set
28 the encoding parameters (mandatory before calling the encoding function).
29 the application can change the encoding parameter ImageQuality from
30 1 to 100 to obtain a more or less quality (visual quality vs the original row image),
31 and inversely more or less jpg file size.
33 (#) Note that for decoding operation the JPEG peripheral output data are organized in
34 YCbCr blocks called MCU (Minimum Coded Unit) as defioned in the JPEG specification
35 ISO/IEC 10918-1 standard.
36 It is up to the application to transform these YCbCr blocks to RGB data that can be display.
38 Respectively, for Encoding operation the JPEG peripheral input should be organized
39 in YCbCr MCU blocks. It is up to the application to perform the necessary RGB to YCbCr
40 MCU blocks transformation before feeding the JPEG peripheral with data.
42 (#) Use functions HAL_JPEG_Encode and HAL_JPEG_Decode to start respectively
43 a JPEG encoding/decoding operation in polling method (blocking).
45 (#) Use functions HAL_JPEG_Encode_IT and HAL_JPEG_Decode_IT to start respectively
46 a JPEG encoding/decoding operation with Interrupt method (not blocking).
48 (#) Use functions HAL_JPEG_Encode_DMA and HAL_JPEG_Decode_DMA to start respectively
49 a JPEG encoding/decoding operation with DMA method (not blocking).
51 (#) Callback HAL_JPEG_InfoReadyCallback is asserted if the current operation
52 is a JPEG decoding to provide the application with JPEG image parameters.
53 This callback is asserted when the JPEG peripheral successfully parse the
54 JPEG header.
56 (#) Callback HAL_JPEG_GetDataCallback is asserted for both encoding and decoding
57 operations to inform the application that the input buffer has been
58 consumed by the peripheral and to ask for a new data chunk if the operation
59 (encoding/decoding) has not been complete yet.
61 (++) This CallBack should be implemented in the application side. It should
62 call the function HAL_JPEG_ConfigInputBuffer if new input data are available,
63 or call HAL_JPEG_Pause with parameter XferSelection set to JPEG_PAUSE_RESUME_INPUT
64 to inform the JPEG HAL driver that the ongoing operation shall pause waiting for the
65 application to provide a new input data chunk.
66 Once the application succeed getting new data and if the input has been paused,
67 the application can call the function HAL_JPEG_ConfigInputBuffer to set the new
68 input buffer and size, then resume the JPEG HAL input by calling new function HAL_JPEG_Resume.
69 If the application has ended feeding the HAL JPEG with input data (no more input data), the application
70 Should call the function HAL_JPEG_ConfigInputBuffer (within the callback HAL_JPEG_GetDataCallback)
71 with the parameter InDataLength set to zero.
73 (++) The mechanism of HAL_JPEG_ConfigInputBuffer/HAL_JPEG_Pause/HAL_JPEG_Resume allows
74 to the application to provide the input data (for encoding or decoding) by chunks.
75 If the new input data chunk is not available (because data should be read from an input file
76 for example) the application can pause the JPEG input (using function HAL_JPEG_Pause)
77 Once the new input data chunk is available ( read from a file for example), the application
78 can call the function HAL_JPEG_ConfigInputBuffer to provide the HAL with the new chunk
79 then resume the JPEG HAL input by calling function HAL_JPEG_Resume.
81 (++) The application can call functions HAL_JPEG_ConfigInputBuffer then HAL_JPEG_Resume.
82 any time (outside the HAL_JPEG_GetDataCallback) Once the new input chunk data available.
83 However, to keep data coherency, the function HAL_JPEG_Pause must be imperatively called
84 (if necessary) within the callback HAL_JPEG_GetDataCallback, i.e when the HAL JPEG has ended
85 Transferring the previous chunk buffer to the JPEG peripheral.
87 (#) Callback HAL_JPEG_DataReadyCallback is asserted when the HAL JPEG driver
88 has filled the given output buffer with the given size.
90 (++) This CallBack should be implemented in the application side. It should
91 call the function HAL_JPEG_ConfigOutputBuffer to provide the HAL JPEG driver
92 with the new output buffer location and size to be used to store next data chunk.
93 if the application is not ready to provide the output chunk location then it can
94 call the function HAL_JPEG_Pause with parameter XferSelection set to JPEG_PAUSE_RESUME_OUTPUT
95 to inform the JPEG HAL driver that it shall pause output data. Once the application
96 is ready to receive the new data chunk (output buffer location free or available) it should call
97 the function HAL_JPEG_ConfigOutputBuffer to provide the HAL JPEG driver
98 with the new output chunk buffer location and size, then call HAL_JPEG_Resume
99 to inform the HAL that it shall resume outputting data in the given output buffer.
101 (++) The mechanism of HAL_JPEG_ConfigOutputBuffer/HAL_JPEG_Pause/HAL_JPEG_Resume allows
102 the application to receive data from the JPEG peripheral by chunks. when a chunk
103 is received, the application can pause the HAL JPEG output data to be able to process
104 these received data (YCbCr to RGB conversion in case of decoding or data storage in case
105 of encoding).
107 (++) The application can call functions HAL_JPEG_ ConfigOutputBuffer then HAL_JPEG_Resume.
108 any time (outside the HAL_JPEG_DataReadyCallback) Once the output data buffer is free to use.
109 However, to keep data coherency, the function HAL_JPEG_Pause must be imperatively called
110 (if necessary) within the callback HAL_JPEG_ DataReadyCallback, i.e when the HAL JPEG has ended
111 Transferring the previous chunk buffer from the JPEG peripheral to the application.
113 (#) Callback HAL_JPEG_EncodeCpltCallback is asserted when the HAL JPEG driver has
114 ended the current JPEG encoding operation, and all output data has been transmitted
115 to the application.
117 (#) Callback HAL_JPEG_DecodeCpltCallback is asserted when the HAL JPEG driver has
118 ended the current JPEG decoding operation. and all output data has been transmitted
119 to the application.
121 (#) Callback HAL_JPEG_ErrorCallback is asserted when an error occurred during
122 the current operation. the application can call the function HAL_JPEG_GetError()
123 to retrieve the error codes.
125 (#) By default the HAL JPEG driver uses the default quantization tables
126 as provide in the JPEG specification (ISO/IEC 10918-1 standard) for encoding.
127 User can change these default tables if necessary using the function HAL_JPEG_SetUserQuantTables
128 Note that for decoding the quantization tables are automatically extracted from
129 the JPEG header.
131 (#) To control JPEG state you can use the following function: HAL_JPEG_GetState()
133 *** JPEG HAL driver macros list ***
134 =============================================
135 [..]
136 Below the list of most used macros in JPEG HAL driver.
138 (+) __HAL_JPEG_RESET_HANDLE_STATE : Reset JPEG handle state.
139 (+) __HAL_JPEG_ENABLE : Enable the JPEG peripheral.
140 (+) __HAL_JPEG_DISABLE : Disable the JPEG peripheral.
141 (+) __HAL_JPEG_GET_FLAG : Check the specified JPEG status flag.
142 (+) __HAL_JPEG_CLEAR_FLAG : Clear the specified JPEG status flag.
143 (+) __HAL_JPEG_ENABLE_IT : Enable the specified JPEG Interrupt.
144 (+) __HAL_JPEG_DISABLE_IT : Disable the specified JPEG Interrupt.
145 (+) __HAL_JPEG_GET_IT_SOURCE : returns the state of the specified JPEG Interrupt (Enabled or disabled).
147 *** Callback registration ***
148 =============================================
150 The compilation define USE_HAL_JPEG_REGISTER_CALLBACKS when set to 1
151 allows the user to configure dynamically the driver callbacks.
152 Use Functions HAL_JPEG_RegisterCallback() or HAL_JPEG_RegisterXXXCallback()
153 to register an interrupt callback.
155 Function HAL_JPEG_RegisterCallback() allows to register following callbacks:
156 (+) EncodeCpltCallback : callback for end of encoding operation.
157 (+) DecodeCpltCallback : callback for end of decoding operation.
158 (+) ErrorCallback : callback for error detection.
159 (+) MspInitCallback : JPEG MspInit.
160 (+) MspDeInitCallback : JPEG MspDeInit.
161 This function takes as parameters the HAL peripheral handle, the Callback ID
162 and a pointer to the user callback function.
164 For specific callbacks InfoReadyCallback, GetDataCallback and DataReadyCallback use dedicated
165 register callbacks : respectively HAL_JPEG_RegisterInfoReadyCallback(),
166 HAL_JPEG_RegisterGetDataCallback() and HAL_JPEG_RegisterDataReadyCallback().
168 Use function HAL_JPEG_UnRegisterCallback() to reset a callback to the default
169 weak function.
170 HAL_JPEG_UnRegisterCallback() takes as parameters the HAL peripheral handle,
171 and the Callback ID.
172 This function allows to reset following callbacks:
173 (+) EncodeCpltCallback : callback for end of encoding operation.
174 (+) DecodeCpltCallback : callback for end of decoding operation.
175 (+) ErrorCallback : callback for error detection.
176 (+) MspInitCallback : JPEG MspInit.
177 (+) MspDeInitCallback : JPEG MspDeInit.
179 For callbacks InfoReadyCallback, GetDataCallback and DataReadyCallback use dedicated
180 unregister callbacks : respectively HAL_JPEG_UnRegisterInfoReadyCallback(),
181 HAL_JPEG_UnRegisterGetDataCallback() and HAL_JPEG_UnRegisterDataReadyCallback().
183 By default, after the HAL_JPEG_Init() and when the state is HAL_JPEG_STATE_RESET
184 all callbacks are set to the corresponding weak functions :
185 examples HAL_JPEG_DecodeCpltCallback() , HAL_JPEG_GetDataCallback().
186 Exception done for MspInit and MspDeInit functions that are
187 reset to the legacy weak function in the HAL_JPEG_Init()/ HAL_JPEG_DeInit() only when
188 these callbacks are null (not registered beforehand).
189 if not, MspInit or MspDeInit are not null, the HAL_JPEG_Init() / HAL_JPEG_DeInit()
190 keep and use the user MspInit/MspDeInit functions (registered beforehand)
192 Callbacks can be registered/unregistered in HAL_JPEG_STATE_READY state only.
193 Exception done MspInit/MspDeInit callbacks that can be registered/unregistered
194 in HAL_JPEG_STATE_READY or HAL_JPEG_STATE_RESET state,
195 thus registered (user) MspInit/DeInit callbacks can be used during the Init/DeInit.
196 In that case first register the MspInit/MspDeInit user callbacks
197 using HAL_JPEG_RegisterCallback() before calling HAL_JPEG_DeInit()
198 or HAL_JPEG_Init() function.
200 When The compilation define USE_HAL_JPEG_REGISTER_CALLBACKS is set to 0 or
201 not defined, the callback registration feature is not available and all callbacks
202 are set to the corresponding weak functions.
204 @endverbatim
205 ******************************************************************************
206 * @attention
208 * <h2><center>&copy; Copyright (c) 2017 STMicroelectronics.
209 * All rights reserved.</center></h2>
211 * This software component is licensed by ST under BSD 3-Clause license,
212 * the "License"; You may not use this file except in compliance with the
213 * License. You may obtain a copy of the License at:
214 * opensource.org/licenses/BSD-3-Clause
216 ******************************************************************************
219 /* Includes ------------------------------------------------------------------*/
220 #include "stm32f7xx_hal.h"
222 /** @addtogroup STM32F7xx_HAL_Driver
223 * @{
226 /** @defgroup JPEG JPEG
227 * @brief JPEG HAL module driver.
228 * @{
231 #ifdef HAL_JPEG_MODULE_ENABLED
233 #if defined (JPEG)
235 /* Private define ------------------------------------------------------------*/
236 /** @addtogroup JPEG_Private_Constants
237 * @{
239 #define JPEG_TIMEOUT_VALUE ((uint32_t)1000) /* 1s */
240 #define JPEG_AC_HUFF_TABLE_SIZE ((uint32_t)162) /* Huffman AC table size : 162 codes*/
241 #define JPEG_DC_HUFF_TABLE_SIZE ((uint32_t)12) /* Huffman AC table size : 12 codes*/
243 #define JPEG_FIFO_SIZE ((uint32_t)16U) /* JPEG Input/Output HW FIFO size in words*/
245 #define JPEG_FIFO_TH_SIZE ((uint32_t)4U) /* JPEG Input/Output HW FIFO Threshold in words*/
247 #define JPEG_DMA_MASK ((uint32_t)0x00001800) /* JPEG DMA request Mask*/
248 #define JPEG_DMA_IDMA ((uint32_t)JPEG_CR_IDMAEN) /* DMA request for the input FIFO */
249 #define JPEG_DMA_ODMA ((uint32_t)JPEG_CR_ODMAEN) /* DMA request for the output FIFO */
251 #define JPEG_INTERRUPT_MASK ((uint32_t)0x0000007EU) /* JPEG Interrupt Mask*/
253 #define JPEG_CONTEXT_ENCODE ((uint32_t)0x00000001) /* JPEG context : operation is encoding*/
254 #define JPEG_CONTEXT_DECODE ((uint32_t)0x00000002) /* JPEG context : operation is decoding*/
255 #define JPEG_CONTEXT_OPERATION_MASK ((uint32_t)0x00000003) /* JPEG context : operation Mask */
257 #define JPEG_CONTEXT_POLLING ((uint32_t)0x00000004) /* JPEG context : Transfer use Polling */
258 #define JPEG_CONTEXT_IT ((uint32_t)0x00000008) /* JPEG context : Transfer use Interrupt */
259 #define JPEG_CONTEXT_DMA ((uint32_t)0x0000000C) /* JPEG context : Transfer use DMA */
260 #define JPEG_CONTEXT_METHOD_MASK ((uint32_t)0x0000000C) /* JPEG context : Transfer Mask */
263 #define JPEG_CONTEXT_CONF_ENCODING ((uint32_t)0x00000100) /* JPEG context : encoding config done */
265 #define JPEG_CONTEXT_PAUSE_INPUT ((uint32_t)0x00001000) /* JPEG context : Pause Input */
266 #define JPEG_CONTEXT_PAUSE_OUTPUT ((uint32_t)0x00002000) /* JPEG context : Pause Output */
268 #define JPEG_CONTEXT_CUSTOM_TABLES ((uint32_t)0x00004000) /* JPEG context : Use custom quantization tables */
270 #define JPEG_CONTEXT_ENDING_DMA ((uint32_t)0x00008000) /* JPEG context : ending with DMA in progress */
272 #define JPEG_PROCESS_ONGOING ((uint32_t)0x00000000) /* Process is on going */
273 #define JPEG_PROCESS_DONE ((uint32_t)0x00000001) /* Process is done (ends) */
275 * @}
278 /* Private typedef -----------------------------------------------------------*/
279 /** @addtogroup JPEG_Private_Types
280 * @{
284 JPEG Huffman Table Structure definition :
285 This implementation of Huffman table structure is compliant with ISO/IEC 10918-1 standard , Annex C Huffman Table specification
287 typedef struct
289 /* These two fields directly represent the contents of a JPEG DHT marker */
290 uint8_t Bits[16]; /*!< bits[k] = # of symbols with codes of length k bits, this parameter corresponds to BITS list in the Annex C */
292 uint8_t HuffVal[162]; /*!< The symbols, in order of incremented code length, this parameter corresponds to HUFFVAL list in the Annex C */
295 } JPEG_ACHuffTableTypeDef;
297 typedef struct
299 /* These two fields directly represent the contents of a JPEG DHT marker */
300 uint8_t Bits[16]; /*!< bits[k] = # of symbols with codes of length k bits, this parameter corresponds to BITS list in the Annex C */
302 uint8_t HuffVal[12]; /*!< The symbols, in order of incremented code length, this parameter corresponds to HUFFVAL list in the Annex C */
305 } JPEG_DCHuffTableTypeDef;
307 typedef struct
309 uint8_t CodeLength[JPEG_AC_HUFF_TABLE_SIZE]; /*!< Code length */
311 uint32_t HuffmanCode[JPEG_AC_HUFF_TABLE_SIZE]; /*!< HuffmanCode */
313 } JPEG_AC_HuffCodeTableTypeDef;
315 typedef struct
317 uint8_t CodeLength[JPEG_DC_HUFF_TABLE_SIZE]; /*!< Code length */
319 uint32_t HuffmanCode[JPEG_DC_HUFF_TABLE_SIZE]; /*!< HuffmanCode */
321 } JPEG_DC_HuffCodeTableTypeDef;
323 * @}
326 /* Private macro -------------------------------------------------------------*/
327 /** @addtogroup JPEG_Private_Macros
328 * @{
330 #define JPEG_ENABLE_DMA(__HANDLE__,__DMA__) ((__HANDLE__)->Instance->CR |= ((__DMA__) & JPEG_DMA_MASK))
331 /*note : To disable a DMA request we must use MODIFY_REG macro to avoid writing "1" to the FIFO flush bits
332 located in the same DMA request enable register (CR register). */
333 #define JPEG_DISABLE_DMA(__HANDLE__,__DMA__) MODIFY_REG((__HANDLE__)->Instance->CR, ((__DMA__) & JPEG_DMA_MASK), 0UL)
335 * @}
338 /* Private variables ---------------------------------------------------------*/
339 /** @addtogroup JPEG_Private_Variables
340 * @{
343 static const JPEG_DCHuffTableTypeDef JPEG_DCLUM_HuffTable =
345 { 0, 1, 5, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0 }, /*Bits*/
347 { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0xa, 0xb } /*HUFFVAL */
351 static const JPEG_DCHuffTableTypeDef JPEG_DCCHROM_HuffTable =
353 { 0, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0 }, /*Bits*/
355 { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0xa, 0xb } /*HUFFVAL */
358 static const JPEG_ACHuffTableTypeDef JPEG_ACLUM_HuffTable =
360 { 0, 2, 1, 3, 3, 2, 4, 3, 5, 5, 4, 4, 0, 0, 1, 0x7d }, /*Bits*/
363 0x01, 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12, /*HUFFVAL */
364 0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61, 0x07,
365 0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xa1, 0x08,
366 0x23, 0x42, 0xb1, 0xc1, 0x15, 0x52, 0xd1, 0xf0,
367 0x24, 0x33, 0x62, 0x72, 0x82, 0x09, 0x0a, 0x16,
368 0x17, 0x18, 0x19, 0x1a, 0x25, 0x26, 0x27, 0x28,
369 0x29, 0x2a, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
370 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49,
371 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59,
372 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69,
373 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79,
374 0x7a, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89,
375 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
376 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
377 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6,
378 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3, 0xc4, 0xc5,
379 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 0xd3, 0xd4,
380 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe1, 0xe2,
381 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea,
382 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
383 0xf9, 0xfa
387 static const JPEG_ACHuffTableTypeDef JPEG_ACCHROM_HuffTable =
389 { 0, 2, 1, 2, 4, 4, 3, 4, 7, 5, 4, 4, 0, 1, 2, 0x77 }, /*Bits*/
392 0x00, 0x01, 0x02, 0x03, 0x11, 0x04, 0x05, 0x21, /*HUFFVAL */
393 0x31, 0x06, 0x12, 0x41, 0x51, 0x07, 0x61, 0x71,
394 0x13, 0x22, 0x32, 0x81, 0x08, 0x14, 0x42, 0x91,
395 0xa1, 0xb1, 0xc1, 0x09, 0x23, 0x33, 0x52, 0xf0,
396 0x15, 0x62, 0x72, 0xd1, 0x0a, 0x16, 0x24, 0x34,
397 0xe1, 0x25, 0xf1, 0x17, 0x18, 0x19, 0x1a, 0x26,
398 0x27, 0x28, 0x29, 0x2a, 0x35, 0x36, 0x37, 0x38,
399 0x39, 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48,
400 0x49, 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58,
401 0x59, 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68,
402 0x69, 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78,
403 0x79, 0x7a, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
404 0x88, 0x89, 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96,
405 0x97, 0x98, 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5,
406 0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4,
407 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3,
408 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2,
409 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda,
410 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9,
411 0xea, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
412 0xf9, 0xfa
416 static const uint8_t JPEG_ZIGZAG_ORDER[JPEG_QUANT_TABLE_SIZE] =
418 0, 1, 8, 16, 9, 2, 3, 10,
419 17, 24, 32, 25, 18, 11, 4, 5,
420 12, 19, 26, 33, 40, 48, 41, 34,
421 27, 20, 13, 6, 7, 14, 21, 28,
422 35, 42, 49, 56, 57, 50, 43, 36,
423 29, 22, 15, 23, 30, 37, 44, 51,
424 58, 59, 52, 45, 38, 31, 39, 46,
425 53, 60, 61, 54, 47, 55, 62, 63
428 * @}
431 /* Private function prototypes -----------------------------------------------*/
432 /** @addtogroup JPEG_Private_Functions_Prototypes
433 * @{
436 static HAL_StatusTypeDef JPEG_Bits_To_SizeCodes(uint8_t *Bits, uint8_t *Huffsize, uint32_t *Huffcode, uint32_t *LastK);
437 static HAL_StatusTypeDef JPEG_DCHuff_BitsVals_To_SizeCodes(JPEG_DCHuffTableTypeDef *DC_BitsValsTable,
438 JPEG_DC_HuffCodeTableTypeDef *DC_SizeCodesTable);
439 static HAL_StatusTypeDef JPEG_ACHuff_BitsVals_To_SizeCodes(JPEG_ACHuffTableTypeDef *AC_BitsValsTable,
440 JPEG_AC_HuffCodeTableTypeDef *AC_SizeCodesTable);
441 static HAL_StatusTypeDef JPEG_Set_HuffDC_Mem(JPEG_HandleTypeDef *hjpeg, JPEG_DCHuffTableTypeDef *HuffTableDC,
442 const __IO uint32_t *DCTableAddress);
443 static HAL_StatusTypeDef JPEG_Set_HuffAC_Mem(JPEG_HandleTypeDef *hjpeg, JPEG_ACHuffTableTypeDef *HuffTableAC,
444 const __IO uint32_t *ACTableAddress);
445 static HAL_StatusTypeDef JPEG_Set_HuffEnc_Mem(JPEG_HandleTypeDef *hjpeg);
446 static void JPEG_Set_Huff_DHTMem(JPEG_HandleTypeDef *hjpeg);
447 static uint32_t JPEG_Set_Quantization_Mem(JPEG_HandleTypeDef *hjpeg, uint8_t *QTable,
448 __IO uint32_t *QTableAddress);
449 static void JPEG_SetColorYCBCR(JPEG_HandleTypeDef *hjpeg);
450 static void JPEG_SetColorGrayScale(JPEG_HandleTypeDef *hjpeg);
451 static void JPEG_SetColorCMYK(JPEG_HandleTypeDef *hjpeg);
453 static void JPEG_Init_Process(JPEG_HandleTypeDef *hjpeg);
454 static uint32_t JPEG_Process(JPEG_HandleTypeDef *hjpeg);
455 static void JPEG_ReadInputData(JPEG_HandleTypeDef *hjpeg, uint32_t nbRequestWords);
456 static void JPEG_StoreOutputData(JPEG_HandleTypeDef *hjpeg, uint32_t nbOutputWords);
457 static uint32_t JPEG_GetQuality(JPEG_HandleTypeDef *hjpeg);
459 static HAL_StatusTypeDef JPEG_DMA_StartProcess(JPEG_HandleTypeDef *hjpeg);
460 static void JPEG_DMA_ContinueProcess(JPEG_HandleTypeDef *hjpeg);
461 static void JPEG_DMA_EndProcess(JPEG_HandleTypeDef *hjpeg);
462 static void JPEG_DMA_PollResidualData(JPEG_HandleTypeDef *hjpeg);
463 static void JPEG_DMAOutCpltCallback(DMA_HandleTypeDef *hdma);
464 static void JPEG_DMAInCpltCallback(DMA_HandleTypeDef *hdma);
465 static void JPEG_DMAErrorCallback(DMA_HandleTypeDef *hdma);
466 static void JPEG_DMAOutAbortCallback(DMA_HandleTypeDef *hdma) ;
469 * @}
472 /** @defgroup JPEG_Exported_Functions JPEG Exported Functions
473 * @{
476 /** @defgroup JPEG_Exported_Functions_Group1 Initialization and de-initialization functions
477 * @brief Initialization and de-initialization functions.
479 @verbatim
480 ==============================================================================
481 ##### Initialization and de-initialization functions #####
482 ==============================================================================
483 [..] This section provides functions allowing to:
484 (+) Initialize the JPEG peripheral and creates the associated handle
485 (+) DeInitialize the JPEG peripheral
487 @endverbatim
488 * @{
492 * @brief Initializes the JPEG according to the specified
493 * parameters in the JPEG_InitTypeDef and creates the associated handle.
494 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
495 * the configuration information for JPEG module
496 * @retval HAL status
498 HAL_StatusTypeDef HAL_JPEG_Init(JPEG_HandleTypeDef *hjpeg)
500 /* These are the sample quantization tables given in JPEG spec ISO/IEC 10918-1 standard , section K.1. */
501 static const uint8_t JPEG_LUM_QuantTable[JPEG_QUANT_TABLE_SIZE] =
503 16, 11, 10, 16, 24, 40, 51, 61,
504 12, 12, 14, 19, 26, 58, 60, 55,
505 14, 13, 16, 24, 40, 57, 69, 56,
506 14, 17, 22, 29, 51, 87, 80, 62,
507 18, 22, 37, 56, 68, 109, 103, 77,
508 24, 35, 55, 64, 81, 104, 113, 92,
509 49, 64, 78, 87, 103, 121, 120, 101,
510 72, 92, 95, 98, 112, 100, 103, 99
512 static const uint8_t JPEG_CHROM_QuantTable[JPEG_QUANT_TABLE_SIZE] =
514 17, 18, 24, 47, 99, 99, 99, 99,
515 18, 21, 26, 66, 99, 99, 99, 99,
516 24, 26, 56, 99, 99, 99, 99, 99,
517 47, 66, 99, 99, 99, 99, 99, 99,
518 99, 99, 99, 99, 99, 99, 99, 99,
519 99, 99, 99, 99, 99, 99, 99, 99,
520 99, 99, 99, 99, 99, 99, 99, 99,
521 99, 99, 99, 99, 99, 99, 99, 99
524 /* Check the JPEG handle allocation */
525 if (hjpeg == NULL)
527 return HAL_ERROR;
530 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
531 if (hjpeg->State == HAL_JPEG_STATE_RESET)
533 /* Allocate lock resource and initialize it */
534 hjpeg->Lock = HAL_UNLOCKED;
536 hjpeg->InfoReadyCallback = HAL_JPEG_InfoReadyCallback; /* Legacy weak InfoReadyCallback */
537 hjpeg->EncodeCpltCallback = HAL_JPEG_EncodeCpltCallback; /* Legacy weak EncodeCpltCallback */
538 hjpeg->DecodeCpltCallback = HAL_JPEG_DecodeCpltCallback; /* Legacy weak DecodeCpltCallback */
539 hjpeg->ErrorCallback = HAL_JPEG_ErrorCallback; /* Legacy weak ErrorCallback */
540 hjpeg->GetDataCallback = HAL_JPEG_GetDataCallback; /* Legacy weak GetDataCallback */
541 hjpeg->DataReadyCallback = HAL_JPEG_DataReadyCallback; /* Legacy weak DataReadyCallback */
543 if (hjpeg->MspInitCallback == NULL)
545 hjpeg->MspInitCallback = HAL_JPEG_MspInit; /* Legacy weak MspInit */
548 /* Init the low level hardware */
549 hjpeg->MspInitCallback(hjpeg);
551 #else
552 if (hjpeg->State == HAL_JPEG_STATE_RESET)
554 /* Allocate lock resource and initialize it */
555 hjpeg->Lock = HAL_UNLOCKED;
557 /* Init the low level hardware : GPIO, CLOCK */
558 HAL_JPEG_MspInit(hjpeg);
560 #endif /* USE_HAL_JPEG_REGISTER_CALLBACKS */
562 /* Change the JPEG state */
563 hjpeg->State = HAL_JPEG_STATE_BUSY;
565 /* Start the JPEG Core*/
566 __HAL_JPEG_ENABLE(hjpeg);
568 /* Stop the JPEG encoding/decoding process*/
569 hjpeg->Instance->CONFR0 &= ~JPEG_CONFR0_START;
571 /* Disable All Interrupts */
572 __HAL_JPEG_DISABLE_IT(hjpeg, JPEG_INTERRUPT_MASK);
574 /* Disable All DMA requests */
575 JPEG_DISABLE_DMA(hjpeg, JPEG_DMA_MASK);
577 /* Flush input and output FIFOs*/
578 hjpeg->Instance->CR |= JPEG_CR_IFF;
579 hjpeg->Instance->CR |= JPEG_CR_OFF;
581 /* Clear all flags */
582 __HAL_JPEG_CLEAR_FLAG(hjpeg, JPEG_FLAG_ALL);
584 /* init default quantization tables*/
585 hjpeg->QuantTable0 = (uint8_t *)((uint32_t)JPEG_LUM_QuantTable);
586 hjpeg->QuantTable1 = (uint8_t *)((uint32_t)JPEG_CHROM_QuantTable);
587 hjpeg->QuantTable2 = NULL;
588 hjpeg->QuantTable3 = NULL;
590 /* init the default Huffman tables*/
591 if (JPEG_Set_HuffEnc_Mem(hjpeg) != HAL_OK)
593 hjpeg->ErrorCode = HAL_JPEG_ERROR_HUFF_TABLE;
595 return HAL_ERROR;
598 /* Enable header processing*/
599 hjpeg->Instance->CONFR1 |= JPEG_CONFR1_HDR;
601 /* Reset JpegInCount and JpegOutCount */
602 hjpeg->JpegInCount = 0;
603 hjpeg->JpegOutCount = 0;
605 /* Change the JPEG state */
606 hjpeg->State = HAL_JPEG_STATE_READY;
608 /* Reset the JPEG ErrorCode */
609 hjpeg->ErrorCode = HAL_JPEG_ERROR_NONE;
611 /*Clear the context filelds*/
612 hjpeg->Context = 0;
614 /* Return function status */
615 return HAL_OK;
619 * @brief DeInitializes the JPEG peripheral.
620 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
621 * the configuration information for JPEG module
622 * @retval HAL status
624 HAL_StatusTypeDef HAL_JPEG_DeInit(JPEG_HandleTypeDef *hjpeg)
626 /* Check the JPEG handle allocation */
627 if (hjpeg == NULL)
629 return HAL_ERROR;
632 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
633 if (hjpeg->MspDeInitCallback == NULL)
635 hjpeg->MspDeInitCallback = HAL_JPEG_MspDeInit; /* Legacy weak MspDeInit */
638 /* DeInit the low level hardware */
639 hjpeg->MspDeInitCallback(hjpeg);
641 #else
642 /* DeInit the low level hardware: CLOCK, NVIC.*/
643 HAL_JPEG_MspDeInit(hjpeg);
644 #endif /* USE_HAL_JPEG_REGISTER_CALLBACKS */
646 /* Change the JPEG state */
647 hjpeg->State = HAL_JPEG_STATE_BUSY;
649 /* Reset the JPEG ErrorCode */
650 hjpeg->ErrorCode = HAL_JPEG_ERROR_NONE;
652 /* Reset JpegInCount and JpegOutCount */
653 hjpeg->JpegInCount = 0;
654 hjpeg->JpegOutCount = 0;
656 /* Change the JPEG state */
657 hjpeg->State = HAL_JPEG_STATE_RESET;
659 /*Clear the context fields*/
660 hjpeg->Context = 0;
662 /* Release Lock */
663 __HAL_UNLOCK(hjpeg);
665 /* Return function status */
666 return HAL_OK;
670 * @brief Initializes the JPEG MSP.
671 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
672 * the configuration information for JPEG module
673 * @retval None
675 __weak void HAL_JPEG_MspInit(JPEG_HandleTypeDef *hjpeg)
677 /* Prevent unused argument(s) compilation warning */
678 UNUSED(hjpeg);
680 /* NOTE : This function Should not be modified, when the callback is needed,
681 the HAL_JPEG_MspInit could be implemented in the user file
686 * @brief DeInitializes JPEG MSP.
687 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
688 * the configuration information for JPEG module
689 * @retval None
691 __weak void HAL_JPEG_MspDeInit(JPEG_HandleTypeDef *hjpeg)
693 /* Prevent unused argument(s) compilation warning */
694 UNUSED(hjpeg);
696 /* NOTE : This function Should not be modified, when the callback is needed,
697 the HAL_JPEG_MspDeInit could be implemented in the user file
701 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
703 * @brief Register a User JPEG Callback
704 * To be used instead of the weak predefined callback
705 * @param hjpeg JPEG handle
706 * @param CallbackID ID of the callback to be registered
707 * This parameter can be one of the following values:
708 * @arg @ref HAL_JPEG_ENCODE_CPLT_CB_ID Encode Complete callback ID
709 * @arg @ref HAL_JPEG_DECODE_CPLT_CB_ID Decode Complete callback ID
710 * @arg @ref HAL_JPEG_ERROR_CB_ID Error callback ID
711 * @arg @ref HAL_JPEG_MSPINIT_CB_ID MspInit callback ID
712 * @arg @ref HAL_JPEG_MSPDEINIT_CB_ID MspDeInit callback ID
713 * @param pCallback pointer to the Callback function
714 * @retval HAL status
716 HAL_StatusTypeDef HAL_JPEG_RegisterCallback(JPEG_HandleTypeDef *hjpeg, HAL_JPEG_CallbackIDTypeDef CallbackID,
717 pJPEG_CallbackTypeDef pCallback)
719 HAL_StatusTypeDef status = HAL_OK;
721 if (pCallback == NULL)
723 /* Update the error code */
724 hjpeg->ErrorCode |= HAL_JPEG_ERROR_INVALID_CALLBACK;
725 return HAL_ERROR;
727 /* Process locked */
728 __HAL_LOCK(hjpeg);
730 if (HAL_JPEG_STATE_READY == hjpeg->State)
732 switch (CallbackID)
734 case HAL_JPEG_ENCODE_CPLT_CB_ID :
735 hjpeg->EncodeCpltCallback = pCallback;
736 break;
738 case HAL_JPEG_DECODE_CPLT_CB_ID :
739 hjpeg->DecodeCpltCallback = pCallback;
740 break;
742 case HAL_JPEG_ERROR_CB_ID :
743 hjpeg->ErrorCallback = pCallback;
744 break;
746 case HAL_JPEG_MSPINIT_CB_ID :
747 hjpeg->MspInitCallback = pCallback;
748 break;
750 case HAL_JPEG_MSPDEINIT_CB_ID :
751 hjpeg->MspDeInitCallback = pCallback;
752 break;
754 default :
755 /* Update the error code */
756 hjpeg->ErrorCode |= HAL_JPEG_ERROR_INVALID_CALLBACK;
757 /* Return error status */
758 status = HAL_ERROR;
759 break;
762 else if (HAL_JPEG_STATE_RESET == hjpeg->State)
764 switch (CallbackID)
766 case HAL_JPEG_MSPINIT_CB_ID :
767 hjpeg->MspInitCallback = pCallback;
768 break;
770 case HAL_JPEG_MSPDEINIT_CB_ID :
771 hjpeg->MspDeInitCallback = pCallback;
772 break;
774 default :
775 /* Update the error code */
776 hjpeg->ErrorCode |= HAL_JPEG_ERROR_INVALID_CALLBACK;
777 /* Return error status */
778 status = HAL_ERROR;
779 break;
782 else
784 /* Update the error code */
785 hjpeg->ErrorCode |= HAL_JPEG_ERROR_INVALID_CALLBACK;
786 /* Return error status */
787 status = HAL_ERROR;
790 /* Release Lock */
791 __HAL_UNLOCK(hjpeg);
792 return status;
796 * @brief Unregister a JPEG Callback
797 * JPEG callabck is redirected to the weak predefined callback
798 * @param hjpeg JPEG handle
799 * @param CallbackID ID of the callback to be unregistered
800 * This parameter can be one of the following values:
801 * This parameter can be one of the following values:
802 * @arg @ref HAL_JPEG_ENCODE_CPLT_CB_ID Encode Complete callback ID
803 * @arg @ref HAL_JPEG_DECODE_CPLT_CB_ID Decode Complete callback ID
804 * @arg @ref HAL_JPEG_ERROR_CB_ID Error callback ID
805 * @arg @ref HAL_JPEG_MSPINIT_CB_ID MspInit callback ID
806 * @arg @ref HAL_JPEG_MSPDEINIT_CB_ID MspDeInit callback ID
807 * @retval HAL status
809 HAL_StatusTypeDef HAL_JPEG_UnRegisterCallback(JPEG_HandleTypeDef *hjpeg, HAL_JPEG_CallbackIDTypeDef CallbackID)
811 HAL_StatusTypeDef status = HAL_OK;
813 /* Process locked */
814 __HAL_LOCK(hjpeg);
816 if (HAL_JPEG_STATE_READY == hjpeg->State)
818 switch (CallbackID)
820 case HAL_JPEG_ENCODE_CPLT_CB_ID :
821 hjpeg->EncodeCpltCallback = HAL_JPEG_EncodeCpltCallback; /* Legacy weak EncodeCpltCallback */
822 break;
824 case HAL_JPEG_DECODE_CPLT_CB_ID :
825 hjpeg->DecodeCpltCallback = HAL_JPEG_DecodeCpltCallback; /* Legacy weak DecodeCpltCallback */
826 break;
828 case HAL_JPEG_ERROR_CB_ID :
829 hjpeg->ErrorCallback = HAL_JPEG_ErrorCallback; /* Legacy weak ErrorCallback */
830 break;
832 case HAL_JPEG_MSPINIT_CB_ID :
833 hjpeg->MspInitCallback = HAL_JPEG_MspInit; /* Legacy weak MspInit */
834 break;
836 case HAL_JPEG_MSPDEINIT_CB_ID :
837 hjpeg->MspDeInitCallback = HAL_JPEG_MspDeInit; /* Legacy weak MspDeInit */
838 break;
840 default :
841 /* Update the error code */
842 hjpeg->ErrorCode |= HAL_JPEG_ERROR_INVALID_CALLBACK;
843 /* Return error status */
844 status = HAL_ERROR;
845 break;
848 else if (HAL_JPEG_STATE_RESET == hjpeg->State)
850 switch (CallbackID)
852 case HAL_JPEG_MSPINIT_CB_ID :
853 hjpeg->MspInitCallback = HAL_JPEG_MspInit; /* Legacy weak MspInit */
854 break;
856 case HAL_JPEG_MSPDEINIT_CB_ID :
857 hjpeg->MspDeInitCallback = HAL_JPEG_MspDeInit; /* Legacy weak MspInit */
858 break;
860 default :
861 /* Update the error code */
862 hjpeg->ErrorCode |= HAL_JPEG_ERROR_INVALID_CALLBACK;
863 /* Return error status */
864 status = HAL_ERROR;
865 break;
868 else
870 /* Update the error code */
871 hjpeg->ErrorCode |= HAL_JPEG_ERROR_INVALID_CALLBACK;
872 /* Return error status */
873 status = HAL_ERROR;
876 /* Release Lock */
877 __HAL_UNLOCK(hjpeg);
878 return status;
882 * @brief Register Info Ready JPEG Callback
883 * To be used instead of the weak HAL_JPEG_InfoReadyCallback() predefined callback
884 * @param hjpeg JPEG handle
885 * @param pCallback pointer to the Info Ready Callback function
886 * @retval HAL status
888 HAL_StatusTypeDef HAL_JPEG_RegisterInfoReadyCallback(JPEG_HandleTypeDef *hjpeg,
889 pJPEG_InfoReadyCallbackTypeDef pCallback)
891 HAL_StatusTypeDef status = HAL_OK;
893 if (pCallback == NULL)
895 /* Update the error code */
896 hjpeg->ErrorCode |= HAL_JPEG_ERROR_INVALID_CALLBACK;
897 return HAL_ERROR;
899 /* Process locked */
900 __HAL_LOCK(hjpeg);
902 if (HAL_JPEG_STATE_READY == hjpeg->State)
904 hjpeg->InfoReadyCallback = pCallback;
906 else
908 /* Update the error code */
909 hjpeg->ErrorCode |= HAL_JPEG_ERROR_INVALID_CALLBACK;
910 /* Return error status */
911 status = HAL_ERROR;
914 /* Release Lock */
915 __HAL_UNLOCK(hjpeg);
916 return status;
920 * @brief UnRegister the Info Ready JPEG Callback
921 * Info Ready JPEG Callback is redirected to the weak HAL_JPEG_InfoReadyCallback() predefined callback
922 * @param hjpeg JPEG handle
923 * @retval HAL status
925 HAL_StatusTypeDef HAL_JPEG_UnRegisterInfoReadyCallback(JPEG_HandleTypeDef *hjpeg)
927 HAL_StatusTypeDef status = HAL_OK;
929 /* Process locked */
930 __HAL_LOCK(hjpeg);
932 if (HAL_JPEG_STATE_READY == hjpeg->State)
934 hjpeg->InfoReadyCallback = HAL_JPEG_InfoReadyCallback; /* Legacy weak InfoReadyCallback */
936 else
938 /* Update the error code */
939 hjpeg->ErrorCode |= HAL_JPEG_ERROR_INVALID_CALLBACK;
940 /* Return error status */
941 status = HAL_ERROR;
944 /* Release Lock */
945 __HAL_UNLOCK(hjpeg);
946 return status;
950 * @brief Register Get Data JPEG Callback
951 * To be used instead of the weak HAL_JPEG_GetDataCallback() predefined callback
952 * @param hjpeg JPEG handle
953 * @param pCallback pointer to the Get Data Callback function
954 * @retval HAL status
956 HAL_StatusTypeDef HAL_JPEG_RegisterGetDataCallback(JPEG_HandleTypeDef *hjpeg, pJPEG_GetDataCallbackTypeDef pCallback)
958 HAL_StatusTypeDef status = HAL_OK;
960 if (pCallback == NULL)
962 /* Update the error code */
963 hjpeg->ErrorCode |= HAL_JPEG_ERROR_INVALID_CALLBACK;
964 return HAL_ERROR;
966 /* Process locked */
967 __HAL_LOCK(hjpeg);
969 if (HAL_JPEG_STATE_READY == hjpeg->State)
971 hjpeg->GetDataCallback = pCallback;
973 else
975 /* Update the error code */
976 hjpeg->ErrorCode |= HAL_JPEG_ERROR_INVALID_CALLBACK;
977 /* Return error status */
978 status = HAL_ERROR;
981 /* Release Lock */
982 __HAL_UNLOCK(hjpeg);
983 return status;
987 * @brief UnRegister the Get Data JPEG Callback
988 * Get Data JPEG Callback is redirected to the weak HAL_JPEG_GetDataCallback() predefined callback
989 * @param hjpeg JPEG handle
990 * @retval HAL status
992 HAL_StatusTypeDef HAL_JPEG_UnRegisterGetDataCallback(JPEG_HandleTypeDef *hjpeg)
994 HAL_StatusTypeDef status = HAL_OK;
996 /* Process locked */
997 __HAL_LOCK(hjpeg);
999 if (HAL_JPEG_STATE_READY == hjpeg->State)
1001 hjpeg->GetDataCallback = HAL_JPEG_GetDataCallback; /* Legacy weak GetDataCallback */
1003 else
1005 /* Update the error code */
1006 hjpeg->ErrorCode |= HAL_JPEG_ERROR_INVALID_CALLBACK;
1007 /* Return error status */
1008 status = HAL_ERROR;
1011 /* Release Lock */
1012 __HAL_UNLOCK(hjpeg);
1013 return status;
1017 * @brief Register Data Ready JPEG Callback
1018 * To be used instead of the weak HAL_JPEG_DataReadyCallback() predefined callback
1019 * @param hjpeg JPEG handle
1020 * @param pCallback pointer to the Get Data Callback function
1021 * @retval HAL status
1023 HAL_StatusTypeDef HAL_JPEG_RegisterDataReadyCallback(JPEG_HandleTypeDef *hjpeg,
1024 pJPEG_DataReadyCallbackTypeDef pCallback)
1026 HAL_StatusTypeDef status = HAL_OK;
1028 if (pCallback == NULL)
1030 /* Update the error code */
1031 hjpeg->ErrorCode |= HAL_JPEG_ERROR_INVALID_CALLBACK;
1032 return HAL_ERROR;
1034 /* Process locked */
1035 __HAL_LOCK(hjpeg);
1037 if (HAL_JPEG_STATE_READY == hjpeg->State)
1039 hjpeg->DataReadyCallback = pCallback;
1041 else
1043 /* Update the error code */
1044 hjpeg->ErrorCode |= HAL_JPEG_ERROR_INVALID_CALLBACK;
1045 /* Return error status */
1046 status = HAL_ERROR;
1049 /* Release Lock */
1050 __HAL_UNLOCK(hjpeg);
1051 return status;
1055 * @brief UnRegister the Data Ready JPEG Callback
1056 * Get Data Ready Callback is redirected to the weak HAL_JPEG_DataReadyCallback() predefined callback
1057 * @param hjpeg JPEG handle
1058 * @retval HAL status
1060 HAL_StatusTypeDef HAL_JPEG_UnRegisterDataReadyCallback(JPEG_HandleTypeDef *hjpeg)
1062 HAL_StatusTypeDef status = HAL_OK;
1064 /* Process locked */
1065 __HAL_LOCK(hjpeg);
1067 if (HAL_JPEG_STATE_READY == hjpeg->State)
1069 hjpeg->DataReadyCallback = HAL_JPEG_DataReadyCallback; /* Legacy weak DataReadyCallback */
1071 else
1073 /* Update the error code */
1074 hjpeg->ErrorCode |= HAL_JPEG_ERROR_INVALID_CALLBACK;
1075 /* Return error status */
1076 status = HAL_ERROR;
1079 /* Release Lock */
1080 __HAL_UNLOCK(hjpeg);
1081 return status;
1084 #endif /* USE_HAL_JPEG_REGISTER_CALLBACKS */
1087 * @}
1090 /** @defgroup JPEG_Exported_Functions_Group2 Configuration functions
1091 * @brief JPEG Configuration functions.
1093 @verbatim
1094 ==============================================================================
1095 ##### Configuration functions #####
1096 ==============================================================================
1097 [..] This section provides functions allowing to:
1098 (+) HAL_JPEG_ConfigEncoding() : JPEG encoding configuration
1099 (+) HAL_JPEG_GetInfo() : Extract the image configuration from the JPEG header during the decoding
1100 (+) HAL_JPEG_EnableHeaderParsing() : Enable JPEG Header parsing for decoding
1101 (+) HAL_JPEG_DisableHeaderParsing() : Disable JPEG Header parsing for decoding
1102 (+) HAL_JPEG_SetUserQuantTables : Modify the default Quantization tables used for JPEG encoding.
1104 @endverbatim
1105 * @{
1109 * @brief Set the JPEG encoding configuration.
1110 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
1111 * the configuration information for JPEG module
1112 * @param pConf pointer to a JPEG_ConfTypeDef structure that contains
1113 * the encoding configuration
1114 * @retval HAL status
1116 HAL_StatusTypeDef HAL_JPEG_ConfigEncoding(JPEG_HandleTypeDef *hjpeg, JPEG_ConfTypeDef *pConf)
1118 uint32_t error;
1119 uint32_t numberMCU, hfactor, vfactor, hMCU, vMCU;
1121 /* Check the JPEG handle allocation */
1122 if ((hjpeg == NULL) || (pConf == NULL))
1124 return HAL_ERROR;
1126 else
1128 /* Check the parameters */
1129 assert_param(IS_JPEG_COLORSPACE(pConf->ColorSpace));
1130 assert_param(IS_JPEG_CHROMASUBSAMPLING(pConf->ChromaSubsampling));
1131 assert_param(IS_JPEG_IMAGE_QUALITY(pConf->ImageQuality));
1133 /* Process Locked */
1134 __HAL_LOCK(hjpeg);
1136 if (hjpeg->State == HAL_JPEG_STATE_READY)
1138 hjpeg->State = HAL_JPEG_STATE_BUSY;
1140 hjpeg->Conf.ColorSpace = pConf->ColorSpace;
1141 hjpeg->Conf.ChromaSubsampling = pConf->ChromaSubsampling;
1142 hjpeg->Conf.ImageHeight = pConf->ImageHeight;
1143 hjpeg->Conf.ImageWidth = pConf->ImageWidth;
1144 hjpeg->Conf.ImageQuality = pConf->ImageQuality;
1146 /* Reset the Color Space : by default only one quantization table is used*/
1147 hjpeg->Instance->CONFR1 &= ~JPEG_CONFR1_COLORSPACE;
1149 /* Set Number of color components*/
1150 if (hjpeg->Conf.ColorSpace == JPEG_GRAYSCALE_COLORSPACE)
1152 /*Gray Scale is only one component 8x8 blocks i.e 4:4:4*/
1153 hjpeg->Conf.ChromaSubsampling = JPEG_444_SUBSAMPLING;
1155 JPEG_SetColorGrayScale(hjpeg);
1156 /* Set quantization table 0*/
1157 error = JPEG_Set_Quantization_Mem(hjpeg, hjpeg->QuantTable0, (hjpeg->Instance->QMEM0));
1159 else if (hjpeg->Conf.ColorSpace == JPEG_YCBCR_COLORSPACE)
1162 Set the Color Space for YCbCr : 2 quantization tables are used
1163 one for Luminance(Y) and one for both Chrominances (Cb & Cr)
1165 hjpeg->Instance->CONFR1 |= JPEG_CONFR1_COLORSPACE_0;
1167 JPEG_SetColorYCBCR(hjpeg);
1169 /* Set quantization table 0*/
1170 error = JPEG_Set_Quantization_Mem(hjpeg, hjpeg->QuantTable0, (hjpeg->Instance->QMEM0));
1171 /*By default quantization table 0 for component 0 and quantization table 1 for both components 1 and 2*/
1172 error |= JPEG_Set_Quantization_Mem(hjpeg, hjpeg->QuantTable1, (hjpeg->Instance->QMEM1));
1174 if ((hjpeg->Context & JPEG_CONTEXT_CUSTOM_TABLES) != 0UL)
1176 /*Use user customized quantization tables , 1 table per component*/
1177 /* use 3 quantization tables , one for each component*/
1178 hjpeg->Instance->CONFR1 &= (~JPEG_CONFR1_COLORSPACE);
1179 hjpeg->Instance->CONFR1 |= JPEG_CONFR1_COLORSPACE_1;
1181 error |= JPEG_Set_Quantization_Mem(hjpeg, hjpeg->QuantTable2, (hjpeg->Instance->QMEM2));
1183 /*Use Quantization 1 table for component 1*/
1184 hjpeg->Instance->CONFR5 &= (~JPEG_CONFR5_QT);
1185 hjpeg->Instance->CONFR5 |= JPEG_CONFR5_QT_0;
1187 /*Use Quantization 2 table for component 2*/
1188 hjpeg->Instance->CONFR6 &= (~JPEG_CONFR6_QT);
1189 hjpeg->Instance->CONFR6 |= JPEG_CONFR6_QT_1;
1192 else /* ColorSpace == JPEG_CMYK_COLORSPACE */
1194 JPEG_SetColorCMYK(hjpeg);
1196 /* Set quantization table 0*/
1197 error = JPEG_Set_Quantization_Mem(hjpeg, hjpeg->QuantTable0, (hjpeg->Instance->QMEM0));
1198 /*By default quantization table 0 for All components*/
1200 if ((hjpeg->Context & JPEG_CONTEXT_CUSTOM_TABLES) != 0UL)
1202 /*Use user customized quantization tables , 1 table per component*/
1203 /* use 4 quantization tables , one for each component*/
1204 hjpeg->Instance->CONFR1 |= JPEG_CONFR1_COLORSPACE;
1206 error |= JPEG_Set_Quantization_Mem(hjpeg, hjpeg->QuantTable1, (hjpeg->Instance->QMEM1));
1207 error |= JPEG_Set_Quantization_Mem(hjpeg, hjpeg->QuantTable2, (hjpeg->Instance->QMEM2));
1208 error |= JPEG_Set_Quantization_Mem(hjpeg, hjpeg->QuantTable3, (hjpeg->Instance->QMEM3));
1210 /*Use Quantization 1 table for component 1*/
1211 hjpeg->Instance->CONFR5 |= JPEG_CONFR5_QT_0;
1213 /*Use Quantization 2 table for component 2*/
1214 hjpeg->Instance->CONFR6 |= JPEG_CONFR6_QT_1;
1216 /*Use Quantization 3 table for component 3*/
1217 hjpeg->Instance->CONFR7 |= JPEG_CONFR7_QT;
1221 if (error != 0UL)
1223 hjpeg->ErrorCode = HAL_JPEG_ERROR_QUANT_TABLE;
1225 /* Process Unlocked */
1226 __HAL_UNLOCK(hjpeg);
1228 /* Set the JPEG State to ready */
1229 hjpeg->State = HAL_JPEG_STATE_READY;
1231 return HAL_ERROR;
1233 /* Set the image size*/
1234 /* set the number of lines*/
1235 MODIFY_REG(hjpeg->Instance->CONFR1, JPEG_CONFR1_YSIZE, ((hjpeg->Conf.ImageHeight & 0x0000FFFFUL) << 16));
1236 /* set the number of pixels per line*/
1237 MODIFY_REG(hjpeg->Instance->CONFR3, JPEG_CONFR3_XSIZE, ((hjpeg->Conf.ImageWidth & 0x0000FFFFUL) << 16));
1240 if (hjpeg->Conf.ChromaSubsampling == JPEG_420_SUBSAMPLING) /* 4:2:0*/
1242 hfactor = 16;
1243 vfactor = 16;
1245 else if (hjpeg->Conf.ChromaSubsampling == JPEG_422_SUBSAMPLING) /* 4:2:2*/
1247 hfactor = 16;
1248 vfactor = 8;
1250 else /* Default is 8x8 MCU, 4:4:4*/
1252 hfactor = 8;
1253 vfactor = 8;
1256 hMCU = (hjpeg->Conf.ImageWidth / hfactor);
1257 if ((hjpeg->Conf.ImageWidth % hfactor) != 0UL)
1259 hMCU++; /*+1 for horizontal incomplete MCU */
1262 vMCU = (hjpeg->Conf.ImageHeight / vfactor);
1263 if ((hjpeg->Conf.ImageHeight % vfactor) != 0UL)
1265 vMCU++; /*+1 for vertical incomplete MCU */
1268 numberMCU = (hMCU * vMCU) - 1UL; /* Bit Field JPEG_CONFR2_NMCU shall be set to NB_MCU - 1*/
1269 /* Set the number of MCU*/
1270 hjpeg->Instance->CONFR2 = (numberMCU & JPEG_CONFR2_NMCU);
1272 hjpeg->Context |= JPEG_CONTEXT_CONF_ENCODING;
1274 /* Process Unlocked */
1275 __HAL_UNLOCK(hjpeg);
1277 /* Set the JPEG State to ready */
1278 hjpeg->State = HAL_JPEG_STATE_READY;
1280 /* Return function status */
1281 return HAL_OK;
1283 else
1285 /* Process Unlocked */
1286 __HAL_UNLOCK(hjpeg);
1288 /* Return function status */
1289 return HAL_BUSY;
1295 * @brief Extract the image configuration from the JPEG header during the decoding
1296 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
1297 * the configuration information for JPEG module
1298 * @param pInfo pointer to a JPEG_ConfTypeDef structure that contains
1299 * The JPEG decoded header informations
1300 * @retval HAL status
1302 HAL_StatusTypeDef HAL_JPEG_GetInfo(JPEG_HandleTypeDef *hjpeg, JPEG_ConfTypeDef *pInfo)
1304 uint32_t yblockNb, cBblockNb, cRblockNb;
1306 /* Check the JPEG handle allocation */
1307 if ((hjpeg == NULL) || (pInfo == NULL))
1309 return HAL_ERROR;
1312 /*Read the conf parameters */
1313 if ((hjpeg->Instance->CONFR1 & JPEG_CONFR1_NF) == JPEG_CONFR1_NF_1)
1315 pInfo->ColorSpace = JPEG_YCBCR_COLORSPACE;
1317 else if ((hjpeg->Instance->CONFR1 & JPEG_CONFR1_NF) == 0UL)
1319 pInfo->ColorSpace = JPEG_GRAYSCALE_COLORSPACE;
1321 else if ((hjpeg->Instance->CONFR1 & JPEG_CONFR1_NF) == JPEG_CONFR1_NF)
1323 pInfo->ColorSpace = JPEG_CMYK_COLORSPACE;
1325 else
1327 return HAL_ERROR;
1330 pInfo->ImageHeight = (hjpeg->Instance->CONFR1 & 0xFFFF0000UL) >> 16;
1331 pInfo->ImageWidth = (hjpeg->Instance->CONFR3 & 0xFFFF0000UL) >> 16;
1333 if ((pInfo->ColorSpace == JPEG_YCBCR_COLORSPACE) || (pInfo->ColorSpace == JPEG_CMYK_COLORSPACE))
1335 yblockNb = (hjpeg->Instance->CONFR4 & JPEG_CONFR4_NB) >> 4;
1336 cBblockNb = (hjpeg->Instance->CONFR5 & JPEG_CONFR5_NB) >> 4;
1337 cRblockNb = (hjpeg->Instance->CONFR6 & JPEG_CONFR6_NB) >> 4;
1339 if ((yblockNb == 1UL) && (cBblockNb == 0UL) && (cRblockNb == 0UL))
1341 pInfo->ChromaSubsampling = JPEG_422_SUBSAMPLING; /*16x8 block*/
1343 else if ((yblockNb == 0UL) && (cBblockNb == 0UL) && (cRblockNb == 0UL))
1345 pInfo->ChromaSubsampling = JPEG_444_SUBSAMPLING;
1347 else if ((yblockNb == 3UL) && (cBblockNb == 0UL) && (cRblockNb == 0UL))
1349 pInfo->ChromaSubsampling = JPEG_420_SUBSAMPLING;
1351 else /*Default is 4:4:4*/
1353 pInfo->ChromaSubsampling = JPEG_444_SUBSAMPLING;
1356 else
1358 pInfo->ChromaSubsampling = JPEG_444_SUBSAMPLING;
1361 pInfo->ImageQuality = JPEG_GetQuality(hjpeg);
1363 /* Return function status */
1364 return HAL_OK;
1368 * @brief Enable JPEG Header parsing for decoding
1369 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
1370 * the configuration information for the JPEG.
1371 * @retval HAL status
1373 HAL_StatusTypeDef HAL_JPEG_EnableHeaderParsing(JPEG_HandleTypeDef *hjpeg)
1375 /* Process locked */
1376 __HAL_LOCK(hjpeg);
1378 if (hjpeg->State == HAL_JPEG_STATE_READY)
1380 /* Change the JPEG state */
1381 hjpeg->State = HAL_JPEG_STATE_BUSY;
1383 /* Enable header processing*/
1384 hjpeg->Instance->CONFR1 |= JPEG_CONFR1_HDR;
1386 /* Process unlocked */
1387 __HAL_UNLOCK(hjpeg);
1389 /* Change the JPEG state */
1390 hjpeg->State = HAL_JPEG_STATE_READY;
1392 return HAL_OK;
1394 else
1396 /* Process unlocked */
1397 __HAL_UNLOCK(hjpeg);
1399 return HAL_BUSY;
1404 * @brief Disable JPEG Header parsing for decoding
1405 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
1406 * the configuration information for the JPEG.
1407 * @retval HAL status
1409 HAL_StatusTypeDef HAL_JPEG_DisableHeaderParsing(JPEG_HandleTypeDef *hjpeg)
1411 /* Process locked */
1412 __HAL_LOCK(hjpeg);
1414 if (hjpeg->State == HAL_JPEG_STATE_READY)
1416 /* Change the JPEG state */
1417 hjpeg->State = HAL_JPEG_STATE_BUSY;
1419 /* Disable header processing*/
1420 hjpeg->Instance->CONFR1 &= ~JPEG_CONFR1_HDR;
1422 /* Process unlocked */
1423 __HAL_UNLOCK(hjpeg);
1425 /* Change the JPEG state */
1426 hjpeg->State = HAL_JPEG_STATE_READY;
1428 return HAL_OK;
1430 else
1432 /* Process unlocked */
1433 __HAL_UNLOCK(hjpeg);
1435 return HAL_BUSY;
1440 * @brief Modify the default Quantization tables used for JPEG encoding.
1441 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
1442 * the configuration information for JPEG module
1443 * @param QTable0 pointer to uint8_t , define the user quantification table for color component 1.
1444 * If NULL assume no need to update the table and no error return
1445 * @param QTable1 pointer to uint8_t , define the user quantification table for color component 2.
1446 * If NULL assume no need to update the table and no error return.
1447 * @param QTable2 pointer to uint8_t , define the user quantification table for color component 3,
1448 * If NULL assume no need to update the table and no error return.
1449 * @param QTable3 pointer to uint8_t , define the user quantification table for color component 4.
1450 * If NULL assume no need to update the table and no error return.
1452 * @retval HAL status
1456 HAL_StatusTypeDef HAL_JPEG_SetUserQuantTables(JPEG_HandleTypeDef *hjpeg, uint8_t *QTable0, uint8_t *QTable1,
1457 uint8_t *QTable2, uint8_t *QTable3)
1459 /* Process Locked */
1460 __HAL_LOCK(hjpeg);
1462 if (hjpeg->State == HAL_JPEG_STATE_READY)
1464 /* Change the DMA state */
1465 hjpeg->State = HAL_JPEG_STATE_BUSY;
1467 hjpeg->Context |= JPEG_CONTEXT_CUSTOM_TABLES;
1469 hjpeg->QuantTable0 = QTable0;
1470 hjpeg->QuantTable1 = QTable1;
1471 hjpeg->QuantTable2 = QTable2;
1472 hjpeg->QuantTable3 = QTable3;
1474 /* Process Unlocked */
1475 __HAL_UNLOCK(hjpeg);
1477 /* Change the DMA state */
1478 hjpeg->State = HAL_JPEG_STATE_READY;
1480 /* Return function status */
1481 return HAL_OK;
1483 else
1485 /* Process Unlocked */
1486 __HAL_UNLOCK(hjpeg);
1488 return HAL_BUSY;
1493 * @}
1496 /** @defgroup JPEG_Exported_Functions_Group3 encoding/decoding processing functions
1497 * @brief processing functions.
1499 @verbatim
1500 ==============================================================================
1501 ##### JPEG processing functions #####
1502 ==============================================================================
1503 [..] This section provides functions allowing to:
1504 (+) HAL_JPEG_Encode() : JPEG encoding with polling process
1505 (+) HAL_JPEG_Decode() : JPEG decoding with polling process
1506 (+) HAL_JPEG_Encode_IT() : JPEG encoding with interrupt process
1507 (+) HAL_JPEG_Decode_IT() : JPEG decoding with interrupt process
1508 (+) HAL_JPEG_Encode_DMA() : JPEG encoding with DMA process
1509 (+) HAL_JPEG_Decode_DMA() : JPEG decoding with DMA process
1510 (+) HAL_JPEG_Pause() : Pause the Input/Output processing
1511 (+) HAL_JPEG_Resume() : Resume the JPEG Input/Output processing
1512 (+) HAL_JPEG_ConfigInputBuffer() : Config Encoding/Decoding Input Buffer
1513 (+) HAL_JPEG_ConfigOutputBuffer() : Config Encoding/Decoding Output Buffer
1514 (+) HAL_JPEG_Abort() : Aborts the JPEG Encoding/Decoding
1516 @endverbatim
1517 * @{
1521 * @brief Starts JPEG encoding with polling processing
1522 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
1523 * the configuration information for JPEG module
1524 * @param pDataInMCU Pointer to the Input buffer
1525 * @param InDataLength size in bytes Input buffer
1526 * @param pDataOut Pointer to the jpeg output data buffer
1527 * @param OutDataLength size in bytes of the Output buffer
1528 * @param Timeout Specify Timeout value
1529 * @retval HAL status
1531 HAL_StatusTypeDef HAL_JPEG_Encode(JPEG_HandleTypeDef *hjpeg, uint8_t *pDataInMCU, uint32_t InDataLength,
1532 uint8_t *pDataOut, uint32_t OutDataLength, uint32_t Timeout)
1534 uint32_t tickstart;
1536 /* Check the parameters */
1537 assert_param((InDataLength >= 4UL));
1538 assert_param((OutDataLength >= 4UL));
1540 /* Check In/out buffer allocation and size */
1541 if ((hjpeg == NULL) || (pDataInMCU == NULL) || (pDataOut == NULL))
1543 return HAL_ERROR;
1545 /* Process locked */
1546 __HAL_LOCK(hjpeg);
1548 if (hjpeg->State != HAL_JPEG_STATE_READY)
1550 /* Process Unlocked */
1551 __HAL_UNLOCK(hjpeg);
1553 return HAL_BUSY;
1556 if (hjpeg->State == HAL_JPEG_STATE_READY)
1558 if ((hjpeg->Context & JPEG_CONTEXT_CONF_ENCODING) == JPEG_CONTEXT_CONF_ENCODING)
1560 /*Change JPEG state*/
1561 hjpeg->State = HAL_JPEG_STATE_BUSY_ENCODING;
1563 /*Set the Context to Encode with Polling*/
1564 hjpeg->Context &= ~(JPEG_CONTEXT_OPERATION_MASK | JPEG_CONTEXT_METHOD_MASK);
1565 hjpeg->Context |= (JPEG_CONTEXT_ENCODE | JPEG_CONTEXT_POLLING);
1567 /* Get tick */
1568 tickstart = HAL_GetTick();
1570 /*Store In/out buffers pointers and size*/
1571 hjpeg->pJpegInBuffPtr = pDataInMCU;
1572 hjpeg->pJpegOutBuffPtr = pDataOut;
1573 hjpeg->InDataLength = InDataLength - (InDataLength % 4UL); /* In Data length must be multiple of 4 Bytes (1 word)*/
1574 hjpeg->OutDataLength = OutDataLength - (OutDataLength % 4UL); /* Out Data length must be multiple of 4 Bytes (1 word)*/
1576 /*Reset In/out data counter */
1577 hjpeg->JpegInCount = 0;
1578 hjpeg->JpegOutCount = 0;
1580 /*Init decoding process*/
1581 JPEG_Init_Process(hjpeg);
1583 /*JPEG data processing : In/Out FIFO transfer*/
1584 while ((JPEG_Process(hjpeg) == JPEG_PROCESS_ONGOING))
1586 if (Timeout != HAL_MAX_DELAY)
1588 if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0UL))
1591 /* Update error code */
1592 hjpeg->ErrorCode |= HAL_JPEG_ERROR_TIMEOUT;
1594 /* Process Unlocked */
1595 __HAL_UNLOCK(hjpeg);
1597 /*Change JPEG state*/
1598 hjpeg->State = HAL_JPEG_STATE_READY;
1600 return HAL_TIMEOUT;
1605 /* Process Unlocked */
1606 __HAL_UNLOCK(hjpeg);
1608 /*Change JPEG state*/
1609 hjpeg->State = HAL_JPEG_STATE_READY;
1612 else
1614 /* Process Unlocked */
1615 __HAL_UNLOCK(hjpeg);
1617 return HAL_ERROR;
1620 /* Return function status */
1621 return HAL_OK;
1625 * @brief Starts JPEG decoding with polling processing
1626 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
1627 * the configuration information for JPEG module
1628 * @param pDataIn Pointer to the input data buffer
1629 * @param InDataLength size in bytes Input buffer
1630 * @param pDataOutMCU Pointer to the Output data buffer
1631 * @param OutDataLength size in bytes of the Output buffer
1632 * @param Timeout Specify Timeout value
1633 * @retval HAL status
1635 HAL_StatusTypeDef HAL_JPEG_Decode(JPEG_HandleTypeDef *hjpeg, uint8_t *pDataIn, uint32_t InDataLength,
1636 uint8_t *pDataOutMCU, uint32_t OutDataLength, uint32_t Timeout)
1638 uint32_t tickstart;
1640 /* Check the parameters */
1641 assert_param((InDataLength >= 4UL));
1642 assert_param((OutDataLength >= 4UL));
1644 /* Check In/out buffer allocation and size */
1645 if ((hjpeg == NULL) || (pDataIn == NULL) || (pDataOutMCU == NULL))
1647 return HAL_ERROR;
1650 /* Process Locked */
1651 __HAL_LOCK(hjpeg);
1653 /* Get tick */
1654 tickstart = HAL_GetTick();
1656 if (hjpeg->State == HAL_JPEG_STATE_READY)
1658 /*Change JPEG state*/
1659 hjpeg->State = HAL_JPEG_STATE_BUSY_DECODING;
1661 /*Set the Context to Decode with Polling*/
1662 /*Set the Context to Encode with Polling*/
1663 hjpeg->Context &= ~(JPEG_CONTEXT_OPERATION_MASK | JPEG_CONTEXT_METHOD_MASK);
1664 hjpeg->Context |= (JPEG_CONTEXT_DECODE | JPEG_CONTEXT_POLLING);
1666 /*Store In/out buffers pointers and size*/
1667 hjpeg->pJpegInBuffPtr = pDataIn;
1668 hjpeg->pJpegOutBuffPtr = pDataOutMCU;
1669 hjpeg->InDataLength = InDataLength - (InDataLength % 4UL); /*In Data length must be multiple of 4 Bytes (1 word)*/
1670 hjpeg->OutDataLength = OutDataLength - (OutDataLength % 4UL); /*Out Data length must be multiple of 4 Bytes (1 word)*/
1672 /*Reset In/out data counter */
1673 hjpeg->JpegInCount = 0;
1674 hjpeg->JpegOutCount = 0;
1676 /*Init decoding process*/
1677 JPEG_Init_Process(hjpeg);
1679 /*JPEG data processing : In/Out FIFO transfer*/
1680 while ((JPEG_Process(hjpeg) == JPEG_PROCESS_ONGOING))
1682 if (Timeout != HAL_MAX_DELAY)
1684 if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0UL))
1687 /* Update error code */
1688 hjpeg->ErrorCode |= HAL_JPEG_ERROR_TIMEOUT;
1690 /* Process Unlocked */
1691 __HAL_UNLOCK(hjpeg);
1693 /*Change JPEG state*/
1694 hjpeg->State = HAL_JPEG_STATE_READY;
1696 return HAL_TIMEOUT;
1701 /* Process Unlocked */
1702 __HAL_UNLOCK(hjpeg);
1704 /*Change JPEG state*/
1705 hjpeg->State = HAL_JPEG_STATE_READY;
1708 else
1710 /* Process Unlocked */
1711 __HAL_UNLOCK(hjpeg);
1713 return HAL_BUSY;
1715 /* Return function status */
1716 return HAL_OK;
1720 * @brief Starts JPEG encoding with interrupt processing
1721 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
1722 * the configuration information for JPEG module
1723 * @param pDataInMCU Pointer to the Input buffer
1724 * @param InDataLength size in bytes Input buffer
1725 * @param pDataOut Pointer to the jpeg output data buffer
1726 * @param OutDataLength size in bytes of the Output buffer
1727 * @retval HAL status
1729 HAL_StatusTypeDef HAL_JPEG_Encode_IT(JPEG_HandleTypeDef *hjpeg, uint8_t *pDataInMCU, uint32_t InDataLength,
1730 uint8_t *pDataOut, uint32_t OutDataLength)
1732 /* Check the parameters */
1733 assert_param((InDataLength >= 4UL));
1734 assert_param((OutDataLength >= 4UL));
1736 /* Check In/out buffer allocation and size */
1737 if ((hjpeg == NULL) || (pDataInMCU == NULL) || (pDataOut == NULL))
1739 return HAL_ERROR;
1742 /* Process Locked */
1743 __HAL_LOCK(hjpeg);
1745 if (hjpeg->State != HAL_JPEG_STATE_READY)
1747 /* Process Unlocked */
1748 __HAL_UNLOCK(hjpeg);
1750 return HAL_BUSY;
1752 else
1754 if ((hjpeg->Context & JPEG_CONTEXT_CONF_ENCODING) == JPEG_CONTEXT_CONF_ENCODING)
1756 /*Change JPEG state*/
1757 hjpeg->State = HAL_JPEG_STATE_BUSY_ENCODING;
1759 /*Set the Context to Encode with IT*/
1760 hjpeg->Context &= ~(JPEG_CONTEXT_OPERATION_MASK | JPEG_CONTEXT_METHOD_MASK);
1761 hjpeg->Context |= (JPEG_CONTEXT_ENCODE | JPEG_CONTEXT_IT);
1763 /*Store In/out buffers pointers and size*/
1764 hjpeg->pJpegInBuffPtr = pDataInMCU;
1765 hjpeg->pJpegOutBuffPtr = pDataOut;
1766 hjpeg->InDataLength = InDataLength - (InDataLength % 4UL); /*In Data length must be multiple of 4 Bytes (1 word)*/
1767 hjpeg->OutDataLength = OutDataLength - (OutDataLength % 4UL); /*Out Data length must be multiple of 4 Bytes (1 word)*/
1769 /*Reset In/out data counter */
1770 hjpeg->JpegInCount = 0;
1771 hjpeg->JpegOutCount = 0;
1773 /*Init decoding process*/
1774 JPEG_Init_Process(hjpeg);
1777 else
1779 /* Process Unlocked */
1780 __HAL_UNLOCK(hjpeg);
1782 return HAL_ERROR;
1785 /* Return function status */
1786 return HAL_OK;
1790 * @brief Starts JPEG decoding with interrupt processing
1791 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
1792 * the configuration information for JPEG module
1793 * @param pDataIn Pointer to the input data buffer
1794 * @param InDataLength size in bytes Input buffer
1795 * @param pDataOutMCU Pointer to the Output data buffer
1796 * @param OutDataLength size in bytes of the Output buffer
1797 * @retval HAL status
1799 HAL_StatusTypeDef HAL_JPEG_Decode_IT(JPEG_HandleTypeDef *hjpeg, uint8_t *pDataIn, uint32_t InDataLength,
1800 uint8_t *pDataOutMCU, uint32_t OutDataLength)
1802 /* Check the parameters */
1803 assert_param((InDataLength >= 4UL));
1804 assert_param((OutDataLength >= 4UL));
1806 /* Check In/out buffer allocation and size */
1807 if ((hjpeg == NULL) || (pDataIn == NULL) || (pDataOutMCU == NULL))
1809 return HAL_ERROR;
1812 /* Process Locked */
1813 __HAL_LOCK(hjpeg);
1815 if (hjpeg->State == HAL_JPEG_STATE_READY)
1817 /*Change JPEG state*/
1818 hjpeg->State = HAL_JPEG_STATE_BUSY_DECODING;
1820 /*Set the Context to Decode with IT*/
1821 hjpeg->Context &= ~(JPEG_CONTEXT_OPERATION_MASK | JPEG_CONTEXT_METHOD_MASK);
1822 hjpeg->Context |= (JPEG_CONTEXT_DECODE | JPEG_CONTEXT_IT);
1824 /*Store In/out buffers pointers and size*/
1825 hjpeg->pJpegInBuffPtr = pDataIn;
1826 hjpeg->pJpegOutBuffPtr = pDataOutMCU;
1827 hjpeg->InDataLength = InDataLength - (InDataLength % 4UL); /*In Data length must be multiple of 4 Bytes (1 word)*/
1828 hjpeg->OutDataLength = OutDataLength - (OutDataLength % 4UL); /*Out Data length must be multiple of 4 Bytes (1 word)*/
1830 /*Reset In/out data counter */
1831 hjpeg->JpegInCount = 0;
1832 hjpeg->JpegOutCount = 0;
1834 /*Init decoding process*/
1835 JPEG_Init_Process(hjpeg);
1838 else
1840 /* Process Unlocked */
1841 __HAL_UNLOCK(hjpeg);
1843 return HAL_BUSY;
1845 /* Return function status */
1846 return HAL_OK;
1850 * @brief Starts JPEG encoding with DMA processing
1851 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
1852 * the configuration information for JPEG module
1853 * @param pDataInMCU Pointer to the Input buffer
1854 * @param InDataLength size in bytes Input buffer
1855 * @param pDataOut Pointer to the jpeg output data buffer
1856 * @param OutDataLength size in bytes of the Output buffer
1857 * @retval HAL status
1859 HAL_StatusTypeDef HAL_JPEG_Encode_DMA(JPEG_HandleTypeDef *hjpeg, uint8_t *pDataInMCU, uint32_t InDataLength,
1860 uint8_t *pDataOut, uint32_t OutDataLength)
1862 /* Check the parameters */
1863 assert_param((InDataLength >= 4UL));
1864 assert_param((OutDataLength >= 4UL));
1866 /* Check In/out buffer allocation and size */
1867 if ((hjpeg == NULL) || (pDataInMCU == NULL) || (pDataOut == NULL))
1869 return HAL_ERROR;
1872 /* Process Locked */
1873 __HAL_LOCK(hjpeg);
1875 if (hjpeg->State != HAL_JPEG_STATE_READY)
1877 /* Process Unlocked */
1878 __HAL_UNLOCK(hjpeg);
1880 return HAL_BUSY;
1882 else
1884 if ((hjpeg->Context & JPEG_CONTEXT_CONF_ENCODING) == JPEG_CONTEXT_CONF_ENCODING)
1886 /*Change JPEG state*/
1887 hjpeg->State = HAL_JPEG_STATE_BUSY_ENCODING;
1889 /*Set the Context to Encode with DMA*/
1890 hjpeg->Context &= ~(JPEG_CONTEXT_OPERATION_MASK | JPEG_CONTEXT_METHOD_MASK);
1891 hjpeg->Context |= (JPEG_CONTEXT_ENCODE | JPEG_CONTEXT_DMA);
1893 /*Store In/out buffers pointers and size*/
1894 hjpeg->pJpegInBuffPtr = pDataInMCU;
1895 hjpeg->pJpegOutBuffPtr = pDataOut;
1896 hjpeg->InDataLength = InDataLength;
1897 hjpeg->OutDataLength = OutDataLength;
1899 /*Reset In/out data counter */
1900 hjpeg->JpegInCount = 0;
1901 hjpeg->JpegOutCount = 0;
1903 /*Init decoding process*/
1904 JPEG_Init_Process(hjpeg);
1906 /* JPEG encoding process using DMA */
1907 if (JPEG_DMA_StartProcess(hjpeg) != HAL_OK)
1909 /* Update State */
1910 hjpeg->State = HAL_JPEG_STATE_ERROR;
1911 /* Process Unlocked */
1912 __HAL_UNLOCK(hjpeg);
1914 return HAL_ERROR;
1918 else
1920 /* Process Unlocked */
1921 __HAL_UNLOCK(hjpeg);
1923 return HAL_ERROR;
1926 /* Return function status */
1927 return HAL_OK;
1931 * @brief Starts JPEG decoding with DMA processing
1932 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
1933 * the configuration information for JPEG module
1934 * @param pDataIn Pointer to the input data buffer
1935 * @param InDataLength size in bytes Input buffer
1936 * @param pDataOutMCU Pointer to the Output data buffer
1937 * @param OutDataLength size in bytes of the Output buffer
1938 * @retval HAL status
1940 HAL_StatusTypeDef HAL_JPEG_Decode_DMA(JPEG_HandleTypeDef *hjpeg, uint8_t *pDataIn, uint32_t InDataLength,
1941 uint8_t *pDataOutMCU, uint32_t OutDataLength)
1943 /* Check the parameters */
1944 assert_param((InDataLength >= 4UL));
1945 assert_param((OutDataLength >= 4UL));
1947 /* Check In/out buffer allocation and size */
1948 if ((hjpeg == NULL) || (pDataIn == NULL) || (pDataOutMCU == NULL))
1950 return HAL_ERROR;
1953 /* Process Locked */
1954 __HAL_LOCK(hjpeg);
1956 if (hjpeg->State == HAL_JPEG_STATE_READY)
1958 /*Change JPEG state*/
1959 hjpeg->State = HAL_JPEG_STATE_BUSY_DECODING;
1961 /*Set the Context to Decode with DMA*/
1962 hjpeg->Context &= ~(JPEG_CONTEXT_OPERATION_MASK | JPEG_CONTEXT_METHOD_MASK);
1963 hjpeg->Context |= (JPEG_CONTEXT_DECODE | JPEG_CONTEXT_DMA);
1965 /*Store In/out buffers pointers and size*/
1966 hjpeg->pJpegInBuffPtr = pDataIn;
1967 hjpeg->pJpegOutBuffPtr = pDataOutMCU;
1968 hjpeg->InDataLength = InDataLength;
1969 hjpeg->OutDataLength = OutDataLength;
1971 /*Reset In/out data counter */
1972 hjpeg->JpegInCount = 0;
1973 hjpeg->JpegOutCount = 0;
1975 /*Init decoding process*/
1976 JPEG_Init_Process(hjpeg);
1978 /* JPEG decoding process using DMA */
1979 if (JPEG_DMA_StartProcess(hjpeg) != HAL_OK)
1981 /* Update State */
1982 hjpeg->State = HAL_JPEG_STATE_ERROR;
1983 /* Process Unlocked */
1984 __HAL_UNLOCK(hjpeg);
1986 return HAL_ERROR;
1989 else
1991 /* Process Unlocked */
1992 __HAL_UNLOCK(hjpeg);
1994 return HAL_BUSY;
1996 /* Return function status */
1997 return HAL_OK;
2001 * @brief Pause the JPEG Input/Output processing
2002 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
2003 * the configuration information for JPEG module
2004 * @param XferSelection This parameter can be one of the following values :
2005 * JPEG_PAUSE_RESUME_INPUT : Pause Input processing
2006 * JPEG_PAUSE_RESUME_OUTPUT: Pause Output processing
2007 * JPEG_PAUSE_RESUME_INPUT_OUTPUT: Pause Input and Output processing
2008 * @retval HAL status
2010 HAL_StatusTypeDef HAL_JPEG_Pause(JPEG_HandleTypeDef *hjpeg, uint32_t XferSelection)
2012 uint32_t mask = 0;
2014 assert_param(IS_JPEG_PAUSE_RESUME_STATE(XferSelection));
2016 if ((hjpeg->Context & JPEG_CONTEXT_METHOD_MASK) == JPEG_CONTEXT_DMA)
2018 if ((XferSelection & JPEG_PAUSE_RESUME_INPUT) == JPEG_PAUSE_RESUME_INPUT)
2020 hjpeg->Context |= JPEG_CONTEXT_PAUSE_INPUT;
2021 mask |= JPEG_DMA_IDMA;
2023 if ((XferSelection & JPEG_PAUSE_RESUME_OUTPUT) == JPEG_PAUSE_RESUME_OUTPUT)
2025 hjpeg->Context |= JPEG_CONTEXT_PAUSE_OUTPUT;
2026 mask |= JPEG_DMA_ODMA;
2028 JPEG_DISABLE_DMA(hjpeg, mask);
2031 else if ((hjpeg->Context & JPEG_CONTEXT_METHOD_MASK) == JPEG_CONTEXT_IT)
2034 if ((XferSelection & JPEG_PAUSE_RESUME_INPUT) == JPEG_PAUSE_RESUME_INPUT)
2036 hjpeg->Context |= JPEG_CONTEXT_PAUSE_INPUT;
2037 mask |= (JPEG_IT_IFT | JPEG_IT_IFNF);
2039 if ((XferSelection & JPEG_PAUSE_RESUME_OUTPUT) == JPEG_PAUSE_RESUME_OUTPUT)
2041 hjpeg->Context |= JPEG_CONTEXT_PAUSE_OUTPUT;
2042 mask |= (JPEG_IT_OFT | JPEG_IT_OFNE | JPEG_IT_EOC);
2044 __HAL_JPEG_DISABLE_IT(hjpeg, mask);
2047 else
2049 /* Nothing to do */
2052 /* Return function status */
2053 return HAL_OK;
2057 * @brief Resume the JPEG Input/Output processing
2058 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
2059 * the configuration information for JPEG module
2060 * @param XferSelection This parameter can be one of the following values :
2061 * JPEG_PAUSE_RESUME_INPUT : Resume Input processing
2062 * JPEG_PAUSE_RESUME_OUTPUT: Resume Output processing
2063 * JPEG_PAUSE_RESUME_INPUT_OUTPUT: Resume Input and Output processing
2064 * @retval HAL status
2066 HAL_StatusTypeDef HAL_JPEG_Resume(JPEG_HandleTypeDef *hjpeg, uint32_t XferSelection)
2068 uint32_t mask = 0;
2070 assert_param(IS_JPEG_PAUSE_RESUME_STATE(XferSelection));
2072 if ((hjpeg->Context & (JPEG_CONTEXT_PAUSE_INPUT | JPEG_CONTEXT_PAUSE_OUTPUT)) == 0UL)
2074 /* if nothing paused to resume return error*/
2075 return HAL_ERROR;
2078 if ((hjpeg->Context & JPEG_CONTEXT_METHOD_MASK) == JPEG_CONTEXT_DMA)
2081 if ((XferSelection & JPEG_PAUSE_RESUME_INPUT) == JPEG_PAUSE_RESUME_INPUT)
2083 hjpeg->Context &= (~JPEG_CONTEXT_PAUSE_INPUT);
2084 mask |= JPEG_DMA_IDMA;
2086 /*JPEG Input DMA transfer data number must be multiple of DMA buffer size
2087 as the destination is a 32 bits register */
2088 hjpeg->InDataLength = hjpeg->InDataLength - (hjpeg->InDataLength % 4UL);
2090 if (hjpeg->InDataLength > 0UL)
2092 /* Start DMA FIFO In transfer */
2093 if (HAL_DMA_Start_IT(hjpeg->hdmain, (uint32_t)hjpeg->pJpegInBuffPtr, (uint32_t)&hjpeg->Instance->DIR,
2094 hjpeg->InDataLength >> 2) != HAL_OK)
2096 hjpeg->ErrorCode |= HAL_JPEG_ERROR_DMA;
2097 hjpeg->State = HAL_JPEG_STATE_ERROR;
2098 return HAL_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);
2110 else
2112 mask |= JPEG_DMA_ODMA;
2114 /* Start DMA FIFO Out transfer */
2115 if (HAL_DMA_Start_IT(hjpeg->hdmaout, (uint32_t)&hjpeg->Instance->DOR, (uint32_t)hjpeg->pJpegOutBuffPtr,
2116 hjpeg->OutDataLength >> 2) != HAL_OK)
2118 hjpeg->ErrorCode |= HAL_JPEG_ERROR_DMA;
2119 hjpeg->State = HAL_JPEG_STATE_ERROR;
2120 return HAL_ERROR;
2125 JPEG_ENABLE_DMA(hjpeg, mask);
2128 else if ((hjpeg->Context & JPEG_CONTEXT_METHOD_MASK) == JPEG_CONTEXT_IT)
2130 if ((XferSelection & JPEG_PAUSE_RESUME_INPUT) == JPEG_PAUSE_RESUME_INPUT)
2132 hjpeg->Context &= (~JPEG_CONTEXT_PAUSE_INPUT);
2133 mask |= (JPEG_IT_IFT | JPEG_IT_IFNF);
2135 if ((XferSelection & JPEG_PAUSE_RESUME_OUTPUT) == JPEG_PAUSE_RESUME_OUTPUT)
2137 hjpeg->Context &= (~JPEG_CONTEXT_PAUSE_OUTPUT);
2138 mask |= (JPEG_IT_OFT | JPEG_IT_OFNE | JPEG_IT_EOC);
2140 __HAL_JPEG_ENABLE_IT(hjpeg, mask);
2143 else
2145 /* Nothing to do */
2148 /* Return function status */
2149 return HAL_OK;
2153 * @brief Config Encoding/Decoding Input Buffer.
2154 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
2155 * the configuration information for JPEG module.
2156 * @param pNewInputBuffer Pointer to the new input data buffer
2157 * @param InDataLength Size in bytes of the new Input data buffer
2158 * @retval HAL status
2160 void HAL_JPEG_ConfigInputBuffer(JPEG_HandleTypeDef *hjpeg, uint8_t *pNewInputBuffer, uint32_t InDataLength)
2162 hjpeg->pJpegInBuffPtr = pNewInputBuffer;
2163 hjpeg->InDataLength = InDataLength;
2167 * @brief Config Encoding/Decoding Output Buffer.
2168 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
2169 * the configuration information for JPEG module.
2170 * @param pNewOutputBuffer Pointer to the new output data buffer
2171 * @param OutDataLength Size in bytes of the new Output data buffer
2172 * @retval HAL status
2174 void HAL_JPEG_ConfigOutputBuffer(JPEG_HandleTypeDef *hjpeg, uint8_t *pNewOutputBuffer, uint32_t OutDataLength)
2176 hjpeg->pJpegOutBuffPtr = pNewOutputBuffer;
2177 hjpeg->OutDataLength = OutDataLength;
2181 * @brief Aborts the JPEG Encoding/Decoding.
2182 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
2183 * the configuration information for JPEG module
2184 * @retval HAL status
2186 HAL_StatusTypeDef HAL_JPEG_Abort(JPEG_HandleTypeDef *hjpeg)
2188 uint32_t tickstart, tmpContext;
2189 tmpContext = hjpeg->Context;
2191 /*Reset the Context operation and method*/
2192 hjpeg->Context &= ~(JPEG_CONTEXT_OPERATION_MASK | JPEG_CONTEXT_METHOD_MASK | JPEG_CONTEXT_ENDING_DMA);
2194 if ((tmpContext & JPEG_CONTEXT_METHOD_MASK) == JPEG_CONTEXT_DMA)
2196 /* Stop the DMA In/out Xfer*/
2197 if (HAL_DMA_Abort(hjpeg->hdmaout) != HAL_OK)
2199 if (hjpeg->hdmaout->ErrorCode == HAL_DMA_ERROR_TIMEOUT)
2201 hjpeg->ErrorCode |= HAL_JPEG_ERROR_DMA;
2204 if (HAL_DMA_Abort(hjpeg->hdmain) != HAL_OK)
2206 if (hjpeg->hdmain->ErrorCode == HAL_DMA_ERROR_TIMEOUT)
2208 hjpeg->ErrorCode |= HAL_JPEG_ERROR_DMA;
2214 /* Stop the JPEG encoding/decoding process*/
2215 hjpeg->Instance->CONFR0 &= ~JPEG_CONFR0_START;
2217 /* Get tick */
2218 tickstart = HAL_GetTick();
2220 /* Check if the JPEG Codec is effectively disabled */
2221 while (__HAL_JPEG_GET_FLAG(hjpeg, JPEG_FLAG_COF) != 0UL)
2223 /* Check for the Timeout */
2224 if ((HAL_GetTick() - tickstart) > JPEG_TIMEOUT_VALUE)
2226 /* Update error code */
2227 hjpeg->ErrorCode |= HAL_JPEG_ERROR_TIMEOUT;
2229 /* Change the DMA state */
2230 hjpeg->State = HAL_JPEG_STATE_ERROR;
2231 break;
2235 /* Disable All Interrupts */
2236 __HAL_JPEG_DISABLE_IT(hjpeg, JPEG_INTERRUPT_MASK);
2238 /* Disable All DMA requests */
2239 JPEG_DISABLE_DMA(hjpeg, JPEG_DMA_MASK);
2241 /* Flush input and output FIFOs*/
2242 hjpeg->Instance->CR |= JPEG_CR_IFF;
2243 hjpeg->Instance->CR |= JPEG_CR_OFF;
2245 /* Clear all flags */
2246 __HAL_JPEG_CLEAR_FLAG(hjpeg, JPEG_FLAG_ALL);
2248 /* Reset JpegInCount and JpegOutCount */
2249 hjpeg->JpegInCount = 0;
2250 hjpeg->JpegOutCount = 0;
2252 /*Reset the Context Pause*/
2253 hjpeg->Context &= ~(JPEG_CONTEXT_PAUSE_INPUT | JPEG_CONTEXT_PAUSE_OUTPUT);
2255 /* Change the DMA state*/
2256 if (hjpeg->ErrorCode != HAL_JPEG_ERROR_NONE)
2258 hjpeg->State = HAL_JPEG_STATE_ERROR;
2259 /* Process Unlocked */
2260 __HAL_UNLOCK(hjpeg);
2261 /* Return function status */
2262 return HAL_ERROR;
2264 else
2266 hjpeg->State = HAL_JPEG_STATE_READY;
2267 /* Process Unlocked */
2268 __HAL_UNLOCK(hjpeg);
2269 /* Return function status */
2270 return HAL_OK;
2277 * @}
2280 /** @defgroup JPEG_Exported_Functions_Group4 JPEG Decode/Encode callback functions
2281 * @brief JPEG process callback functions.
2283 @verbatim
2284 ==============================================================================
2285 ##### JPEG Decode and Encode callback functions #####
2286 ==============================================================================
2287 [..] This section provides callback functions:
2288 (+) HAL_JPEG_InfoReadyCallback() : Decoding JPEG Info ready callback
2289 (+) HAL_JPEG_EncodeCpltCallback() : Encoding complete callback.
2290 (+) HAL_JPEG_DecodeCpltCallback() : Decoding complete callback.
2291 (+) HAL_JPEG_ErrorCallback() : JPEG error callback.
2292 (+) HAL_JPEG_GetDataCallback() : Get New Data chunk callback.
2293 (+) HAL_JPEG_DataReadyCallback() : Decoded/Encoded Data ready callback.
2295 @endverbatim
2296 * @{
2300 * @brief Decoding JPEG Info ready callback.
2301 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
2302 * the configuration information for JPEG module
2303 * @param pInfo pointer to a JPEG_ConfTypeDef structure that contains
2304 * The JPEG decoded header informations
2305 * @retval None
2307 __weak void HAL_JPEG_InfoReadyCallback(JPEG_HandleTypeDef *hjpeg, JPEG_ConfTypeDef *pInfo)
2309 /* Prevent unused argument(s) compilation warning */
2310 UNUSED(hjpeg);
2311 UNUSED(pInfo);
2313 /* NOTE : This function Should not be modified, when the callback is needed,
2314 the HAL_JPEG_HeaderParsingCpltCallback could be implemented in the user file
2319 * @brief Encoding complete callback.
2320 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
2321 * the configuration information for JPEG module
2322 * @retval None
2324 __weak void HAL_JPEG_EncodeCpltCallback(JPEG_HandleTypeDef *hjpeg)
2326 /* Prevent unused argument(s) compilation warning */
2327 UNUSED(hjpeg);
2329 /* NOTE : This function Should not be modified, when the callback is needed,
2330 the HAL_JPEG_EncodeCpltCallback could be implemented in the user file
2335 * @brief Decoding complete callback.
2336 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
2337 * the configuration information for JPEG module
2338 * @retval None
2340 __weak void HAL_JPEG_DecodeCpltCallback(JPEG_HandleTypeDef *hjpeg)
2342 /* Prevent unused argument(s) compilation warning */
2343 UNUSED(hjpeg);
2345 /* NOTE : This function Should not be modified, when the callback is needed,
2346 the HAL_JPEG_EncodeCpltCallback could be implemented in the user file
2351 * @brief JPEG error callback.
2352 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
2353 * the configuration information for JPEG module
2354 * @retval None
2356 __weak void HAL_JPEG_ErrorCallback(JPEG_HandleTypeDef *hjpeg)
2358 /* Prevent unused argument(s) compilation warning */
2359 UNUSED(hjpeg);
2361 /* NOTE : This function Should not be modified, when the callback is needed,
2362 the HAL_JPEG_ErrorCallback could be implemented in the user file
2367 * @brief Get New Data chunk callback.
2368 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
2369 * the configuration information for JPEG module
2370 * @param NbDecodedData Number of consummed data in the previous chunk in bytes
2371 * @retval None
2373 __weak void HAL_JPEG_GetDataCallback(JPEG_HandleTypeDef *hjpeg, uint32_t NbDecodedData)
2375 /* Prevent unused argument(s) compilation warning */
2376 UNUSED(hjpeg);
2377 UNUSED(NbDecodedData);
2379 /* NOTE : This function Should not be modified, when the callback is needed,
2380 the HAL_JPEG_GetDataCallback could be implemented in the user file
2385 * @brief Decoded/Encoded Data ready callback.
2386 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
2387 * the configuration information for JPEG module
2388 * @param pDataOut pointer to the output data buffer
2389 * @param OutDataLength number in bytes of data available in the specified output buffer
2390 * @retval None
2392 __weak void HAL_JPEG_DataReadyCallback(JPEG_HandleTypeDef *hjpeg, uint8_t *pDataOut, uint32_t OutDataLength)
2394 /* Prevent unused argument(s) compilation warning */
2395 UNUSED(hjpeg);
2396 UNUSED(pDataOut);
2397 UNUSED(OutDataLength);
2399 /* NOTE : This function Should not be modified, when the callback is needed,
2400 the HAL_JPEG_DataReadyCallback could be implemented in the user file
2405 * @}
2409 /** @defgroup JPEG_Exported_Functions_Group5 JPEG IRQ handler management
2410 * @brief JPEG IRQ handler.
2412 @verbatim
2413 ==============================================================================
2414 ##### JPEG IRQ handler management #####
2415 ==============================================================================
2416 [..] This section provides JPEG IRQ handler function.
2417 (+) HAL_JPEG_IRQHandler() : handles JPEG interrupt request
2419 @endverbatim
2420 * @{
2424 * @brief This function handles JPEG interrupt request.
2425 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
2426 * the configuration information for JPEG module
2427 * @retval None
2429 void HAL_JPEG_IRQHandler(JPEG_HandleTypeDef *hjpeg)
2431 switch (hjpeg->State)
2433 case HAL_JPEG_STATE_BUSY_ENCODING:
2434 case HAL_JPEG_STATE_BUSY_DECODING:
2435 /* continue JPEG data encoding/Decoding*/
2436 /* JPEG data processing : In/Out FIFO transfer*/
2437 if ((hjpeg->Context & JPEG_CONTEXT_METHOD_MASK) == JPEG_CONTEXT_IT)
2439 (void) JPEG_Process(hjpeg);
2441 else if ((hjpeg->Context & JPEG_CONTEXT_METHOD_MASK) == JPEG_CONTEXT_DMA)
2443 JPEG_DMA_ContinueProcess(hjpeg);
2445 else
2447 /* Nothing to do */
2449 break;
2451 default:
2452 break;
2457 * @}
2460 /** @defgroup JPEG_Exported_Functions_Group6 Peripheral State functions
2461 * @brief Peripheral State functions.
2463 @verbatim
2464 ==============================================================================
2465 ##### Peripheral State and Error functions #####
2466 ==============================================================================
2467 [..] This section provides JPEG State and Errors function.
2468 (+) HAL_JPEG_GetState() : permits to get in run-time the JPEG state.
2469 (+) HAL_JPEG_GetError() : Returns the JPEG error code if any.
2471 @endverbatim
2472 * @{
2476 * @brief Returns the JPEG state.
2477 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
2478 * the configuration information for JPEG module
2479 * @retval JPEG state
2481 HAL_JPEG_STATETypeDef HAL_JPEG_GetState(JPEG_HandleTypeDef *hjpeg)
2483 return hjpeg->State;
2487 * @brief Return the JPEG error code
2488 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
2489 * the configuration information for the specified JPEG.
2490 * @retval JPEG Error Code
2492 uint32_t HAL_JPEG_GetError(JPEG_HandleTypeDef *hjpeg)
2494 return hjpeg->ErrorCode;
2498 * @}
2502 * @}
2506 /** @addtogroup JPEG_Private_Functions
2507 * @{
2511 * @brief Generates Huffman sizes/Codes Table from Bits/vals Table
2512 * @param Bits pointer to bits table
2513 * @param Huffsize pointer to sizes table
2514 * @param Huffcode pointer to codes table
2515 * @param LastK pointer to last Coeff (table dimmension)
2516 * @retval HAL status
2518 static HAL_StatusTypeDef JPEG_Bits_To_SizeCodes(uint8_t *Bits, uint8_t *Huffsize, uint32_t *Huffcode, uint32_t *LastK)
2520 uint32_t i, p, l, code, si;
2522 /* Figure C.1: Generation of table of Huffman code sizes */
2523 p = 0;
2524 for (l = 0; l < 16UL; l++)
2526 i = (uint32_t)Bits[l];
2527 if ((p + i) > 256UL)
2529 /* check for table overflow */
2530 return HAL_ERROR;
2532 while (i != 0UL)
2534 Huffsize[p] = (uint8_t) l + 1U;
2535 p++;
2536 i--;
2539 Huffsize[p] = 0;
2540 *LastK = p;
2542 /* Figure C.2: Generation of table of Huffman codes */
2543 code = 0;
2544 si = Huffsize[0];
2545 p = 0;
2546 while (Huffsize[p] != 0U)
2548 while (((uint32_t) Huffsize[p]) == si)
2550 Huffcode[p] = code;
2551 p++;
2552 code++;
2554 /* code must fit in "size" bits (si), no code is allowed to be all ones*/
2555 if(si > 31UL)
2557 return HAL_ERROR;
2559 if (((uint32_t) code) >= (((uint32_t) 1) << si))
2561 return HAL_ERROR;
2563 code <<= 1;
2564 si++;
2566 /* Return function status */
2567 return HAL_OK;
2571 * @brief Transform a Bits/Vals AC Huffman table to sizes/Codes huffman Table
2572 * that can programmed to the JPEG encoder registers
2573 * @param AC_BitsValsTable pointer to AC huffman bits/vals table
2574 * @param AC_SizeCodesTable pointer to AC huffman Sizes/Codes table
2575 * @retval HAL status
2577 static HAL_StatusTypeDef JPEG_ACHuff_BitsVals_To_SizeCodes(JPEG_ACHuffTableTypeDef *AC_BitsValsTable,
2578 JPEG_AC_HuffCodeTableTypeDef *AC_SizeCodesTable)
2580 HAL_StatusTypeDef error;
2581 uint8_t huffsize[257];
2582 uint32_t huffcode[257];
2583 uint32_t k;
2584 uint32_t l, lsb, msb;
2585 uint32_t lastK;
2587 error = JPEG_Bits_To_SizeCodes(AC_BitsValsTable->Bits, huffsize, huffcode, &lastK);
2588 if (error != HAL_OK)
2590 return error;
2593 /* Figure C.3: Ordering procedure for encoding procedure code tables */
2594 k = 0;
2596 while (k < lastK)
2598 l = AC_BitsValsTable->HuffVal[k];
2599 if (l == 0UL)
2601 l = 160; /*l = 0x00 EOB code*/
2603 else if (l == 0xF0UL) /* l = 0xF0 ZRL code*/
2605 l = 161;
2607 else
2609 msb = (l & 0xF0UL) >> 4;
2610 lsb = (l & 0x0FUL);
2611 l = (msb * 10UL) + lsb - 1UL;
2613 if (l >= JPEG_AC_HUFF_TABLE_SIZE)
2615 return HAL_ERROR; /* Huffman Table overflow error*/
2617 else
2619 AC_SizeCodesTable->HuffmanCode[l] = huffcode[k];
2620 AC_SizeCodesTable->CodeLength[l] = huffsize[k] - 1U;
2621 k++;
2625 /* Return function status */
2626 return HAL_OK;
2630 * @brief Transform a Bits/Vals DC Huffman table to sizes/Codes huffman Table
2631 * that can programmed to the JPEG encoder registers
2632 * @param DC_BitsValsTable pointer to DC huffman bits/vals table
2633 * @param DC_SizeCodesTable pointer to DC huffman Sizes/Codes table
2634 * @retval HAL status
2636 static HAL_StatusTypeDef JPEG_DCHuff_BitsVals_To_SizeCodes(JPEG_DCHuffTableTypeDef *DC_BitsValsTable,
2637 JPEG_DC_HuffCodeTableTypeDef *DC_SizeCodesTable)
2639 HAL_StatusTypeDef error;
2641 uint32_t k;
2642 uint32_t l;
2643 uint32_t lastK;
2644 uint8_t huffsize[257];
2645 uint32_t huffcode[257];
2646 error = JPEG_Bits_To_SizeCodes(DC_BitsValsTable->Bits, huffsize, huffcode, &lastK);
2647 if (error != HAL_OK)
2649 return error;
2651 /* Figure C.3: ordering procedure for encoding procedure code tables */
2652 k = 0;
2654 while (k < lastK)
2656 l = DC_BitsValsTable->HuffVal[k];
2657 if (l >= JPEG_DC_HUFF_TABLE_SIZE)
2659 return HAL_ERROR; /* Huffman Table overflow error*/
2661 else
2663 DC_SizeCodesTable->HuffmanCode[l] = huffcode[k];
2664 DC_SizeCodesTable->CodeLength[l] = huffsize[k] - 1U;
2665 k++;
2669 /* Return function status */
2670 return HAL_OK;
2674 * @brief Set the JPEG register with an DC huffman table at the given DC table address
2675 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
2676 * the configuration information for JPEG module
2677 * @param HuffTableDC pointer to DC huffman table
2678 * @param DCTableAddress Encoder DC huffman table address it could be HUFFENC_DC0 or HUFFENC_DC1.
2679 * @retval HAL status
2681 static HAL_StatusTypeDef JPEG_Set_HuffDC_Mem(JPEG_HandleTypeDef *hjpeg, JPEG_DCHuffTableTypeDef *HuffTableDC,
2682 const __IO uint32_t *DCTableAddress)
2684 HAL_StatusTypeDef error;
2685 JPEG_DC_HuffCodeTableTypeDef dcSizeCodesTable;
2686 uint32_t i, lsb, msb;
2687 __IO uint32_t *address, *addressDef;
2689 if (DCTableAddress == (hjpeg->Instance->HUFFENC_DC0))
2691 address = (hjpeg->Instance->HUFFENC_DC0 + (JPEG_DC_HUFF_TABLE_SIZE / 2UL));
2693 else if (DCTableAddress == (hjpeg->Instance->HUFFENC_DC1))
2695 address = (hjpeg->Instance->HUFFENC_DC1 + (JPEG_DC_HUFF_TABLE_SIZE / 2UL));
2697 else
2699 return HAL_ERROR;
2702 if (HuffTableDC != NULL)
2704 error = JPEG_DCHuff_BitsVals_To_SizeCodes(HuffTableDC, &dcSizeCodesTable);
2705 if (error != HAL_OK)
2707 return error;
2709 addressDef = address;
2710 *addressDef = 0x0FFF0FFF;
2711 addressDef++;
2712 *addressDef = 0x0FFF0FFF;
2714 i = JPEG_DC_HUFF_TABLE_SIZE;
2715 while (i > 1UL)
2717 i--;
2718 address --;
2719 msb = ((uint32_t)(((uint32_t)dcSizeCodesTable.CodeLength[i] & 0xFU) << 8)) | ((uint32_t)dcSizeCodesTable.HuffmanCode[i] &
2720 0xFFUL);
2721 i--;
2722 lsb = ((uint32_t)(((uint32_t)dcSizeCodesTable.CodeLength[i] & 0xFU) << 8)) | ((uint32_t)dcSizeCodesTable.HuffmanCode[i] &
2723 0xFFUL);
2725 *address = lsb | (msb << 16);
2729 /* Return function status */
2730 return HAL_OK;
2734 * @brief Set the JPEG register with an AC huffman table at the given AC table address
2735 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
2736 * the configuration information for JPEG module
2737 * @param HuffTableAC pointer to AC huffman table
2738 * @param ACTableAddress Encoder AC huffman table address it could be HUFFENC_AC0 or HUFFENC_AC1.
2739 * @retval HAL status
2741 static HAL_StatusTypeDef JPEG_Set_HuffAC_Mem(JPEG_HandleTypeDef *hjpeg, JPEG_ACHuffTableTypeDef *HuffTableAC,
2742 const __IO uint32_t *ACTableAddress)
2744 HAL_StatusTypeDef error;
2745 JPEG_AC_HuffCodeTableTypeDef acSizeCodesTable;
2746 uint32_t i, lsb, msb;
2747 __IO uint32_t *address, *addressDef;
2749 if (ACTableAddress == (hjpeg->Instance->HUFFENC_AC0))
2751 address = (hjpeg->Instance->HUFFENC_AC0 + (JPEG_AC_HUFF_TABLE_SIZE / 2UL));
2753 else if (ACTableAddress == (hjpeg->Instance->HUFFENC_AC1))
2755 address = (hjpeg->Instance->HUFFENC_AC1 + (JPEG_AC_HUFF_TABLE_SIZE / 2UL));
2757 else
2759 return HAL_ERROR;
2762 if (HuffTableAC != NULL)
2764 error = JPEG_ACHuff_BitsVals_To_SizeCodes(HuffTableAC, &acSizeCodesTable);
2765 if (error != HAL_OK)
2767 return error;
2769 /* Default values settings: 162:167 FFFh , 168:175 FD0h_FD7h */
2770 /* Locations 162:175 of each AC table contain information used internally by the core */
2772 addressDef = address;
2773 for (i = 0; i < 3UL; i++)
2775 *addressDef = 0x0FFF0FFF;
2776 addressDef++;
2778 *addressDef = 0x0FD10FD0;
2779 addressDef++;
2780 *addressDef = 0x0FD30FD2;
2781 addressDef++;
2782 *addressDef = 0x0FD50FD4;
2783 addressDef++;
2784 *addressDef = 0x0FD70FD6;
2785 /* end of Locations 162:175 */
2788 i = JPEG_AC_HUFF_TABLE_SIZE;
2789 while (i > 1UL)
2791 i--;
2792 address--;
2793 msb = ((uint32_t)(((uint32_t)acSizeCodesTable.CodeLength[i] & 0xFU) << 8)) | ((uint32_t)acSizeCodesTable.HuffmanCode[i] &
2794 0xFFUL);
2795 i--;
2796 lsb = ((uint32_t)(((uint32_t)acSizeCodesTable.CodeLength[i] & 0xFU) << 8)) | ((uint32_t)acSizeCodesTable.HuffmanCode[i] &
2797 0xFFUL);
2799 *address = lsb | (msb << 16);
2803 /* Return function status */
2804 return HAL_OK;
2808 * @brief Configure the JPEG encoder register huffman tables to used during
2809 * the encdoing operation
2810 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
2811 * the configuration information for JPEG module
2812 * @retval None
2814 static HAL_StatusTypeDef JPEG_Set_HuffEnc_Mem(JPEG_HandleTypeDef *hjpeg)
2816 HAL_StatusTypeDef error;
2818 JPEG_Set_Huff_DHTMem(hjpeg);
2819 error = JPEG_Set_HuffAC_Mem(hjpeg, (JPEG_ACHuffTableTypeDef *)(uint32_t)&JPEG_ACLUM_HuffTable,
2820 (hjpeg->Instance->HUFFENC_AC0));
2821 if (error != HAL_OK)
2823 return error;
2826 error = JPEG_Set_HuffAC_Mem(hjpeg, (JPEG_ACHuffTableTypeDef *)(uint32_t)&JPEG_ACCHROM_HuffTable,
2827 (hjpeg->Instance->HUFFENC_AC1));
2828 if (error != HAL_OK)
2830 return error;
2833 error = JPEG_Set_HuffDC_Mem(hjpeg, (JPEG_DCHuffTableTypeDef *)(uint32_t)&JPEG_DCLUM_HuffTable,
2834 hjpeg->Instance->HUFFENC_DC0);
2835 if (error != HAL_OK)
2837 return error;
2840 error = JPEG_Set_HuffDC_Mem(hjpeg, (JPEG_DCHuffTableTypeDef *)(uint32_t)&JPEG_DCCHROM_HuffTable,
2841 hjpeg->Instance->HUFFENC_DC1);
2842 if (error != HAL_OK)
2844 return error;
2846 /* Return function status */
2847 return HAL_OK;
2851 * @brief Configure the JPEG register huffman tables to be included in the JPEG
2852 * file header (used for encoding only)
2853 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
2854 * the configuration information for JPEG module
2855 * @retval None
2857 static void JPEG_Set_Huff_DHTMem(JPEG_HandleTypeDef *hjpeg)
2859 JPEG_ACHuffTableTypeDef *HuffTableAC0 = (JPEG_ACHuffTableTypeDef *)(uint32_t)&JPEG_ACLUM_HuffTable;
2860 JPEG_ACHuffTableTypeDef *HuffTableAC1 = (JPEG_ACHuffTableTypeDef *)(uint32_t)&JPEG_ACCHROM_HuffTable;
2861 JPEG_DCHuffTableTypeDef *HuffTableDC0 = (JPEG_DCHuffTableTypeDef *)(uint32_t)&JPEG_DCLUM_HuffTable;
2862 JPEG_DCHuffTableTypeDef *HuffTableDC1 = (JPEG_DCHuffTableTypeDef *)(uint32_t)&JPEG_DCCHROM_HuffTable;
2863 uint32_t value, index;
2864 __IO uint32_t *address;
2866 /* DC0 Huffman Table : BITS*/
2867 /* DC0 BITS is a 16 Bytes table i.e 4x32bits words from DHTMEM base address to DHTMEM + 3*/
2868 address = (hjpeg->Instance->DHTMEM + 3);
2869 index = 16;
2870 while (index > 3UL)
2873 *address = (((uint32_t)HuffTableDC0->Bits[index - 1UL] & 0xFFUL) << 24) |
2874 (((uint32_t)HuffTableDC0->Bits[index - 2UL] & 0xFFUL) << 16) |
2875 (((uint32_t)HuffTableDC0->Bits[index - 3UL] & 0xFFUL) << 8) |
2876 ((uint32_t)HuffTableDC0->Bits[index - 4UL] & 0xFFUL);
2877 address--;
2878 index -= 4UL;
2881 /* DC0 Huffman Table : Val*/
2882 /* DC0 VALS is a 12 Bytes table i.e 3x32bits words from DHTMEM base address +4 to DHTMEM + 6 */
2883 address = (hjpeg->Instance->DHTMEM + 6);
2884 index = 12;
2885 while (index > 3UL)
2887 *address = (((uint32_t)HuffTableDC0->HuffVal[index - 1UL] & 0xFFUL) << 24) |
2888 (((uint32_t)HuffTableDC0->HuffVal[index - 2UL] & 0xFFUL) << 16) |
2889 (((uint32_t)HuffTableDC0->HuffVal[index - 3UL] & 0xFFUL) << 8) |
2890 ((uint32_t)HuffTableDC0->HuffVal[index - 4UL] & 0xFFUL);
2891 address--;
2892 index -= 4UL;
2895 /* AC0 Huffman Table : BITS*/
2896 /* AC0 BITS is a 16 Bytes table i.e 4x32bits words from DHTMEM base address + 7 to DHTMEM + 10*/
2897 address = (hjpeg->Instance->DHTMEM + 10UL);
2898 index = 16;
2899 while (index > 3UL)
2902 *address = (((uint32_t)HuffTableAC0->Bits[index - 1UL] & 0xFFUL) << 24) |
2903 (((uint32_t)HuffTableAC0->Bits[index - 2UL] & 0xFFUL) << 16) |
2904 (((uint32_t)HuffTableAC0->Bits[index - 3UL] & 0xFFUL) << 8) |
2905 ((uint32_t)HuffTableAC0->Bits[index - 4UL] & 0xFFUL);
2906 address--;
2907 index -= 4UL;
2910 /* AC0 Huffman Table : Val*/
2911 /* AC0 VALS is a 162 Bytes table i.e 41x32bits words from DHTMEM base address + 11 to DHTMEM + 51 */
2912 /* only Byte 0 and Byte 1 of the last word (@ DHTMEM + 51) belong to AC0 VALS table */
2913 address = (hjpeg->Instance->DHTMEM + 51);
2914 value = *address & 0xFFFF0000U;
2915 value = value | (((uint32_t)HuffTableAC0->HuffVal[161] & 0xFFUL) << 8) | ((uint32_t)HuffTableAC0->HuffVal[160] & 0xFFUL);
2916 *address = value;
2918 /*continue setting 160 AC0 huffman values */
2919 address--; /* address = hjpeg->Instance->DHTMEM + 50*/
2920 index = 160;
2921 while (index > 3UL)
2923 *address = (((uint32_t)HuffTableAC0->HuffVal[index - 1UL] & 0xFFUL) << 24) |
2924 (((uint32_t)HuffTableAC0->HuffVal[index - 2UL] & 0xFFUL) << 16) |
2925 (((uint32_t)HuffTableAC0->HuffVal[index - 3UL] & 0xFFUL) << 8) |
2926 ((uint32_t)HuffTableAC0->HuffVal[index - 4UL] & 0xFFUL);
2927 address--;
2928 index -= 4UL;
2931 /* DC1 Huffman Table : BITS*/
2932 /* DC1 BITS is a 16 Bytes table i.e 4x32bits words from DHTMEM + 51 base address to DHTMEM + 55*/
2933 /* only Byte 2 and Byte 3 of the first word (@ DHTMEM + 51) belong to DC1 Bits table */
2934 address = (hjpeg->Instance->DHTMEM + 51);
2935 value = *address & 0x0000FFFFU;
2936 value = value | (((uint32_t)HuffTableDC1->Bits[1] & 0xFFUL) << 24) | (((uint32_t)HuffTableDC1->Bits[0] & 0xFFUL) << 16);
2937 *address = value;
2939 /* only Byte 0 and Byte 1 of the last word (@ DHTMEM + 55) belong to DC1 Bits table */
2940 address = (hjpeg->Instance->DHTMEM + 55);
2941 value = *address & 0xFFFF0000U;
2942 value = value | (((uint32_t)HuffTableDC1->Bits[15] & 0xFFUL) << 8) | ((uint32_t)HuffTableDC1->Bits[14] & 0xFFUL);
2943 *address = value;
2945 /*continue setting 12 DC1 huffman Bits from DHTMEM + 54 down to DHTMEM + 52*/
2946 address--;
2947 index = 12;
2948 while (index > 3UL)
2951 *address = (((uint32_t)HuffTableDC1->Bits[index + 1UL] & 0xFFUL) << 24) |
2952 (((uint32_t)HuffTableDC1->Bits[index] & 0xFFUL) << 16) |
2953 (((uint32_t)HuffTableDC1->Bits[index - 1UL] & 0xFFUL) << 8) |
2954 ((uint32_t)HuffTableDC1->Bits[index - 2UL] & 0xFFUL);
2955 address--;
2956 index -= 4UL;
2959 /* DC1 Huffman Table : Val*/
2960 /* DC1 VALS is a 12 Bytes table i.e 3x32bits words from DHTMEM base address +55 to DHTMEM + 58 */
2961 /* only Byte 2 and Byte 3 of the first word (@ DHTMEM + 55) belong to DC1 Val table */
2962 address = (hjpeg->Instance->DHTMEM + 55);
2963 value = *address & 0x0000FFFFUL;
2964 value = value | (((uint32_t)HuffTableDC1->HuffVal[1] & 0xFFUL) << 24) | (((uint32_t)HuffTableDC1->HuffVal[0] & 0xFFUL) <<
2965 16);
2966 *address = value;
2968 /* only Byte 0 and Byte 1 of the last word (@ DHTMEM + 58) belong to DC1 Val table */
2969 address = (hjpeg->Instance->DHTMEM + 58);
2970 value = *address & 0xFFFF0000UL;
2971 value = value | (((uint32_t)HuffTableDC1->HuffVal[11] & 0xFFUL) << 8) | ((uint32_t)HuffTableDC1->HuffVal[10] & 0xFFUL);
2972 *address = value;
2974 /*continue setting 8 DC1 huffman val from DHTMEM + 57 down to DHTMEM + 56*/
2975 address--;
2976 index = 8;
2977 while (index > 3UL)
2979 *address = (((uint32_t)HuffTableDC1->HuffVal[index + 1UL] & 0xFFUL) << 24) |
2980 (((uint32_t)HuffTableDC1->HuffVal[index] & 0xFFUL) << 16) |
2981 (((uint32_t)HuffTableDC1->HuffVal[index - 1UL] & 0xFFUL) << 8) |
2982 ((uint32_t)HuffTableDC1->HuffVal[index - 2UL] & 0xFFUL);
2983 address--;
2984 index -= 4UL;
2987 /* AC1 Huffman Table : BITS*/
2988 /* AC1 BITS is a 16 Bytes table i.e 4x32bits words from DHTMEM base address + 58 to DHTMEM + 62*/
2989 /* only Byte 2 and Byte 3 of the first word (@ DHTMEM + 58) belong to AC1 Bits table */
2990 address = (hjpeg->Instance->DHTMEM + 58);
2991 value = *address & 0x0000FFFFU;
2992 value = value | (((uint32_t)HuffTableAC1->Bits[1] & 0xFFUL) << 24) | (((uint32_t)HuffTableAC1->Bits[0] & 0xFFUL) << 16);
2993 *address = value;
2995 /* only Byte 0 and Byte 1 of the last word (@ DHTMEM + 62) belong to Bits Val table */
2996 address = (hjpeg->Instance->DHTMEM + 62);
2997 value = *address & 0xFFFF0000U;
2998 value = value | (((uint32_t)HuffTableAC1->Bits[15] & 0xFFUL) << 8) | ((uint32_t)HuffTableAC1->Bits[14] & 0xFFUL);
2999 *address = value;
3001 /*continue setting 12 AC1 huffman Bits from DHTMEM + 61 down to DHTMEM + 59*/
3002 address--;
3003 index = 12;
3004 while (index > 3UL)
3007 *address = (((uint32_t)HuffTableAC1->Bits[index + 1UL] & 0xFFUL) << 24) |
3008 (((uint32_t)HuffTableAC1->Bits[index] & 0xFFUL) << 16) |
3009 (((uint32_t)HuffTableAC1->Bits[index - 1UL] & 0xFFUL) << 8) |
3010 ((uint32_t)HuffTableAC1->Bits[index - 2UL] & 0xFFUL);
3011 address--;
3012 index -= 4UL;
3015 /* AC1 Huffman Table : Val*/
3016 /* AC1 VALS is a 162 Bytes table i.e 41x32bits words from DHTMEM base address + 62 to DHTMEM + 102 */
3017 /* only Byte 2 and Byte 3 of the first word (@ DHTMEM + 62) belong to AC1 VALS table */
3018 address = (hjpeg->Instance->DHTMEM + 62);
3019 value = *address & 0x0000FFFFUL;
3020 value = value | (((uint32_t)HuffTableAC1->HuffVal[1] & 0xFFUL) << 24) | (((uint32_t)HuffTableAC1->HuffVal[0] & 0xFFUL) <<
3021 16);
3022 *address = value;
3024 /*continue setting 160 AC1 huffman values from DHTMEM + 63 to DHTMEM+102 */
3025 address = (hjpeg->Instance->DHTMEM + 102);
3026 index = 160;
3027 while (index > 3UL)
3029 *address = (((uint32_t)HuffTableAC1->HuffVal[index + 1UL] & 0xFFUL) << 24) |
3030 (((uint32_t)HuffTableAC1->HuffVal[index] & 0xFFUL) << 16) |
3031 (((uint32_t)HuffTableAC1->HuffVal[index - 1UL] & 0xFFUL) << 8) |
3032 ((uint32_t)HuffTableAC1->HuffVal[index - 2UL] & 0xFFUL);
3033 address--;
3034 index -= 4UL;
3040 * @brief Configure the JPEG registers with a given quantization table
3041 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
3042 * the configuration information for JPEG module
3043 * @param QTable pointer to an array of 64 bytes giving the quantization table
3044 * @param QTableAddress destination quantization address in the JPEG peripheral
3045 * it could be QMEM0, QMEM1, QMEM2 or QMEM3
3046 * @retval 0 if no error, 1 if error
3048 static uint32_t JPEG_Set_Quantization_Mem(JPEG_HandleTypeDef *hjpeg, uint8_t *QTable,
3049 __IO uint32_t *QTableAddress)
3051 uint32_t i, j, quantRow, quantVal, ScaleFactor;
3052 __IO uint32_t *tableAddress;
3054 tableAddress = QTableAddress;
3056 if ((hjpeg->Conf.ImageQuality >= 50UL) && (hjpeg->Conf.ImageQuality <= 100UL))
3058 ScaleFactor = 200UL - (hjpeg->Conf.ImageQuality * 2UL);
3060 else if (hjpeg->Conf.ImageQuality > 0UL)
3062 ScaleFactor = ((uint32_t) 5000) / ((uint32_t) hjpeg->Conf.ImageQuality);
3064 else
3066 return 1UL;
3069 /*Quantization_table = (Standard_quanization_table * ScaleFactor + 50) / 100*/
3070 i = 0;
3071 while (i < (JPEG_QUANT_TABLE_SIZE - 3UL))
3073 quantRow = 0;
3074 for (j = 0; j < 4UL; j++)
3076 /* Note that the quantization coefficients must be specified in the table in zigzag order */
3077 quantVal = ((((uint32_t) QTable[JPEG_ZIGZAG_ORDER[i + j]]) * ScaleFactor) + 50UL) / 100UL;
3079 if (quantVal == 0UL)
3081 quantVal = 1UL;
3083 else if (quantVal > 255UL)
3085 quantVal = 255UL;
3087 else
3089 /* Nothing to do, keep same value of quantVal */
3092 quantRow |= ((quantVal & 0xFFUL) << (8UL * j));
3095 i += 4UL;
3096 *tableAddress = quantRow;
3097 tableAddress ++;
3100 /* Return function status */
3101 return 0UL;
3105 * @brief Configure the JPEG registers for YCbCr color space
3106 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
3107 * the configuration information for JPEG module
3108 * @retval None
3110 static void JPEG_SetColorYCBCR(JPEG_HandleTypeDef *hjpeg)
3112 uint32_t ySamplingH;
3113 uint32_t ySamplingV;
3114 uint32_t yblockNb;
3116 /*Set Number of color components to 3*/
3117 hjpeg->Instance->CONFR1 &= ~JPEG_CONFR1_NF;
3118 hjpeg->Instance->CONFR1 |= JPEG_CONFR1_NF_1;
3120 /* compute MCU block size and Y, Cb ,Cr sampling factors*/
3121 if (hjpeg->Conf.ChromaSubsampling == JPEG_420_SUBSAMPLING)
3123 ySamplingH = JPEG_CONFR4_HSF_1; /* Hs = 2*/
3124 ySamplingV = JPEG_CONFR4_VSF_1; /* Vs = 2*/
3126 yblockNb = 0x30; /* 4 blocks of 8x8*/
3128 else if (hjpeg->Conf.ChromaSubsampling == JPEG_422_SUBSAMPLING)
3130 ySamplingH = JPEG_CONFR4_HSF_1; /* Hs = 2*/
3131 ySamplingV = JPEG_CONFR4_VSF_0; /* Vs = 1*/
3133 yblockNb = 0x10; /* 2 blocks of 8x8*/
3135 else /*JPEG_444_SUBSAMPLING and default*/
3137 ySamplingH = JPEG_CONFR4_HSF_0; /* Hs = 1*/
3138 ySamplingV = JPEG_CONFR4_VSF_0; /* Vs = 1*/
3140 yblockNb = 0; /* 1 block of 8x8*/
3143 hjpeg->Instance->CONFR1 &= ~(JPEG_CONFR1_NF | JPEG_CONFR1_NS);
3144 hjpeg->Instance->CONFR1 |= (JPEG_CONFR1_NF_1 | JPEG_CONFR1_NS_1);
3146 /*Reset CONFR4 register*/
3147 hjpeg->Instance->CONFR4 = 0;
3148 /*Set Horizental and Vertical sampling factor , number of blocks , Quantization table and Huffman AC/DC tables for component 0*/
3149 hjpeg->Instance->CONFR4 |= (ySamplingH | ySamplingV | (yblockNb & JPEG_CONFR4_NB));
3151 /*Reset CONFR5 register*/
3152 hjpeg->Instance->CONFR5 = 0;
3153 /*Set Horizental and Vertical sampling factor , number of blocks , Quantization table and Huffman AC/DC tables for component 1*/
3154 hjpeg->Instance->CONFR5 |= (JPEG_CONFR5_HSF_0 | JPEG_CONFR5_VSF_0 | JPEG_CONFR5_QT_0 | JPEG_CONFR5_HA | JPEG_CONFR5_HD);
3156 /*Reset CONFR6 register*/
3157 hjpeg->Instance->CONFR6 = 0;
3158 /*Set Horizental and Vertical sampling factor and number of blocks for component 2*/
3159 /* In YCBCR , by default, both chrominance components (component 1 and component 2) use the same Quantization table (table 1) */
3160 /* In YCBCR , both chrominance components (component 1 and component 2) use the same Huffman tables (table 1) */
3161 hjpeg->Instance->CONFR6 |= (JPEG_CONFR6_HSF_0 | JPEG_CONFR6_VSF_0 | JPEG_CONFR6_QT_0 | JPEG_CONFR6_HA | JPEG_CONFR6_HD);
3166 * @brief Configure the JPEG registers for GrayScale color space
3167 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
3168 * the configuration information for JPEG module
3169 * @retval None
3171 static void JPEG_SetColorGrayScale(JPEG_HandleTypeDef *hjpeg)
3173 /*Set Number of color components to 1*/
3174 hjpeg->Instance->CONFR1 &= ~(JPEG_CONFR1_NF | JPEG_CONFR1_NS);
3176 /*in GrayScale use 1 single Quantization table (Table 0)*/
3177 /*in GrayScale use only one couple of AC/DC huffman table (table 0)*/
3179 /*Reset CONFR4 register*/
3180 hjpeg->Instance->CONFR4 = 0;
3181 /*Set Horizental and Vertical sampling factor , number of blocks , Quantization table and Huffman AC/DC tables for component 0*/
3182 hjpeg->Instance->CONFR4 |= JPEG_CONFR4_HSF_0 | JPEG_CONFR4_VSF_0 ;
3186 * @brief Configure the JPEG registers for CMYK color space
3187 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
3188 * the configuration information for JPEG module
3189 * @retval None
3191 static void JPEG_SetColorCMYK(JPEG_HandleTypeDef *hjpeg)
3193 uint32_t ySamplingH;
3194 uint32_t ySamplingV;
3195 uint32_t yblockNb;
3197 /*Set Number of color components to 4*/
3198 hjpeg->Instance->CONFR1 |= (JPEG_CONFR1_NF | JPEG_CONFR1_NS);
3200 /* compute MCU block size and Y, Cb ,Cr sampling factors*/
3201 if (hjpeg->Conf.ChromaSubsampling == JPEG_420_SUBSAMPLING)
3203 ySamplingH = JPEG_CONFR4_HSF_1; /* Hs = 2*/
3204 ySamplingV = JPEG_CONFR4_VSF_1; /* Vs = 2*/
3206 yblockNb = 0x30; /* 4 blocks of 8x8*/
3208 else if (hjpeg->Conf.ChromaSubsampling == JPEG_422_SUBSAMPLING)
3210 ySamplingH = JPEG_CONFR4_HSF_1; /* Hs = 2*/
3211 ySamplingV = JPEG_CONFR4_VSF_0; /* Vs = 1*/
3213 yblockNb = 0x10; /* 2 blocks of 8x8*/
3215 else /*JPEG_444_SUBSAMPLING and default*/
3217 ySamplingH = JPEG_CONFR4_HSF_0; /* Hs = 1*/
3218 ySamplingV = JPEG_CONFR4_VSF_0; /* Vs = 1*/
3220 yblockNb = 0; /* 1 block of 8x8*/
3223 /*Reset CONFR4 register*/
3224 hjpeg->Instance->CONFR4 = 0;
3225 /*Set Horizental and Vertical sampling factor , number of blocks , Quantization table and Huffman AC/DC tables for component 0*/
3226 hjpeg->Instance->CONFR4 |= (ySamplingH | ySamplingV | (yblockNb & JPEG_CONFR4_NB));
3228 /*Reset CONFR5 register*/
3229 hjpeg->Instance->CONFR5 = 0;
3230 /*Set Horizental and Vertical sampling factor , number of blocks , Quantization table and Huffman AC/DC tables for component 1*/
3231 hjpeg->Instance->CONFR5 |= (JPEG_CONFR5_HSF_0 | JPEG_CONFR5_VSF_0);
3233 /*Reset CONFR6 register*/
3234 hjpeg->Instance->CONFR6 = 0;
3235 /*Set Horizental and Vertical sampling factor , number of blocks , Quantization table and Huffman AC/DC tables for component 2*/
3236 hjpeg->Instance->CONFR6 |= (JPEG_CONFR6_HSF_0 | JPEG_CONFR6_VSF_0);
3238 /*Reset CONFR7 register*/
3239 hjpeg->Instance->CONFR7 = 0;
3240 /*Set Horizental and Vertical sampling factor , number of blocks , Quantization table and Huffman AC/DC tables for component 3*/
3241 hjpeg->Instance->CONFR7 |= (JPEG_CONFR7_HSF_0 | JPEG_CONFR7_VSF_0);
3245 * @brief Init the JPEG encoding/decoding process in case of Polling or Interrupt and DMA
3246 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
3247 * the configuration information for JPEG module
3248 * @retval None
3250 static void JPEG_Init_Process(JPEG_HandleTypeDef *hjpeg)
3252 /*Reset pause*/
3253 hjpeg->Context &= (~(JPEG_CONTEXT_PAUSE_INPUT | JPEG_CONTEXT_PAUSE_OUTPUT));
3255 if ((hjpeg->Context & JPEG_CONTEXT_OPERATION_MASK) == JPEG_CONTEXT_DECODE)
3257 /*Set JPEG Codec to Decoding mode */
3258 hjpeg->Instance->CONFR1 |= JPEG_CONFR1_DE;
3260 else /* JPEG_CONTEXT_ENCODE */
3262 /*Set JPEG Codec to Encoding mode */
3263 hjpeg->Instance->CONFR1 &= ~JPEG_CONFR1_DE;
3266 /*Stop JPEG processing */
3267 hjpeg->Instance->CONFR0 &= ~JPEG_CONFR0_START;
3269 /* Disable All Interrupts */
3270 __HAL_JPEG_DISABLE_IT(hjpeg, JPEG_INTERRUPT_MASK);
3272 /* Disable All DMA requests */
3273 JPEG_DISABLE_DMA(hjpeg, JPEG_DMA_MASK);
3274 /* Flush input and output FIFOs*/
3275 hjpeg->Instance->CR |= JPEG_CR_IFF;
3276 hjpeg->Instance->CR |= JPEG_CR_OFF;
3278 /* Clear all flags */
3279 __HAL_JPEG_CLEAR_FLAG(hjpeg, JPEG_FLAG_ALL);
3281 /*Start Encoding/Decoding*/
3282 hjpeg->Instance->CONFR0 |= JPEG_CONFR0_START;
3284 if ((hjpeg->Context & JPEG_CONTEXT_METHOD_MASK) == JPEG_CONTEXT_IT)
3286 /*Enable IN/OUT, end of Conversation, and end of header parsing interruptions*/
3287 __HAL_JPEG_ENABLE_IT(hjpeg, JPEG_IT_IFT | JPEG_IT_IFNF | JPEG_IT_OFT | JPEG_IT_OFNE | JPEG_IT_EOC | JPEG_IT_HPD);
3289 else if ((hjpeg->Context & JPEG_CONTEXT_METHOD_MASK) == JPEG_CONTEXT_DMA)
3291 /*Enable End Of Conversation, and End Of Header parsing interruptions*/
3292 __HAL_JPEG_ENABLE_IT(hjpeg, JPEG_IT_EOC | JPEG_IT_HPD);
3295 else
3297 /* Nothing to do */
3302 * @brief JPEG encoding/decoding process in case of Polling or Interrupt
3303 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
3304 * the configuration information for JPEG module
3305 * @retval JPEG_PROCESS_DONE if the process has ends else JPEG_PROCESS_ONGOING
3307 static uint32_t JPEG_Process(JPEG_HandleTypeDef *hjpeg)
3309 uint32_t tmpContext;
3311 /*End of header processing flag rised*/
3312 if ((hjpeg->Context & JPEG_CONTEXT_OPERATION_MASK) == JPEG_CONTEXT_DECODE)
3314 if (__HAL_JPEG_GET_FLAG(hjpeg, JPEG_FLAG_HPDF) != 0UL)
3316 /*Call Header parsing complet callback */
3317 (void) HAL_JPEG_GetInfo(hjpeg, &hjpeg->Conf);
3318 /* Reset the ImageQuality */
3319 hjpeg->Conf.ImageQuality = 0;
3320 /* Note : the image quality is only available at the end of the decoding operation */
3321 /* at the current stage the calculated image quality is not correct so reset it */
3323 /*Call Info Ready callback */
3324 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
3325 hjpeg->InfoReadyCallback(hjpeg, &hjpeg->Conf);
3326 #else
3327 HAL_JPEG_InfoReadyCallback(hjpeg, &hjpeg->Conf);
3328 #endif /* USE_HAL_JPEG_REGISTER_CALLBACKS */
3330 __HAL_JPEG_DISABLE_IT(hjpeg, JPEG_IT_HPD);
3332 /* Clear header processing done flag */
3333 __HAL_JPEG_CLEAR_FLAG(hjpeg, JPEG_FLAG_HPDF);
3337 /*Input FIFO status handling*/
3338 if ((hjpeg->Context & JPEG_CONTEXT_PAUSE_INPUT) == 0UL)
3340 if (__HAL_JPEG_GET_FLAG(hjpeg, JPEG_FLAG_IFTF) != 0UL)
3342 /*Input FIFO threshold flag rised*/
3343 /*JPEG_FIFO_TH_SIZE words can be written in */
3344 JPEG_ReadInputData(hjpeg, JPEG_FIFO_TH_SIZE);
3346 else if (__HAL_JPEG_GET_FLAG(hjpeg, JPEG_FLAG_IFNFF) != 0UL)
3348 /*Input FIFO Not Full flag rised*/
3349 /*32-bit value can be written in */
3350 JPEG_ReadInputData(hjpeg, 1);
3352 else
3354 /* Nothing to do */
3359 /*Output FIFO flag handling*/
3360 if ((hjpeg->Context & JPEG_CONTEXT_PAUSE_OUTPUT) == 0UL)
3362 if (__HAL_JPEG_GET_FLAG(hjpeg, JPEG_FLAG_OFTF) != 0UL)
3364 /*Output FIFO threshold flag rised*/
3365 /*JPEG_FIFO_TH_SIZE words can be read out */
3366 JPEG_StoreOutputData(hjpeg, JPEG_FIFO_TH_SIZE);
3368 else if (__HAL_JPEG_GET_FLAG(hjpeg, JPEG_FLAG_OFNEF) != 0UL)
3370 /*Output FIFO Not Empty flag rised*/
3371 /*32-bit value can be read out */
3372 JPEG_StoreOutputData(hjpeg, 1);
3374 else
3376 /* Nothing to do */
3380 /*End of Conversion handling :i.e EOC flag is high and OFTF low and OFNEF low*/
3381 if (__HAL_JPEG_GET_FLAG(hjpeg, JPEG_FLAG_EOCF | JPEG_FLAG_OFTF | JPEG_FLAG_OFNEF) == JPEG_FLAG_EOCF)
3383 /*Stop Encoding/Decoding*/
3384 hjpeg->Instance->CONFR0 &= ~JPEG_CONFR0_START;
3386 if ((hjpeg->Context & JPEG_CONTEXT_METHOD_MASK) == JPEG_CONTEXT_IT)
3388 /* Disable All Interrupts */
3389 __HAL_JPEG_DISABLE_IT(hjpeg, JPEG_INTERRUPT_MASK);
3392 /* Clear all flags */
3393 __HAL_JPEG_CLEAR_FLAG(hjpeg, JPEG_FLAG_ALL);
3395 /*Call End of conversion callback */
3396 if (hjpeg->JpegOutCount > 0UL)
3398 /*Output Buffer is not empty, call DecodedDataReadyCallback*/
3399 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
3400 hjpeg->DataReadyCallback(hjpeg, hjpeg->pJpegOutBuffPtr, hjpeg->JpegOutCount);
3401 #else
3402 HAL_JPEG_DataReadyCallback(hjpeg, hjpeg->pJpegOutBuffPtr, hjpeg->JpegOutCount);
3403 #endif /* USE_HAL_JPEG_REGISTER_CALLBACKS */
3405 hjpeg->JpegOutCount = 0;
3408 /*Reset Context Operation*/
3409 tmpContext = hjpeg->Context;
3410 /*Clear all context fields execpt JPEG_CONTEXT_CONF_ENCODING and JPEG_CONTEXT_CUSTOM_TABLES*/
3411 hjpeg->Context &= (JPEG_CONTEXT_CONF_ENCODING | JPEG_CONTEXT_CUSTOM_TABLES);
3413 /* Process Unlocked */
3414 __HAL_UNLOCK(hjpeg);
3416 /* Change the JPEG state */
3417 hjpeg->State = HAL_JPEG_STATE_READY;
3419 /*Call End of Encoding/Decoding callback */
3420 if ((tmpContext & JPEG_CONTEXT_OPERATION_MASK) == JPEG_CONTEXT_DECODE)
3422 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
3423 hjpeg->DecodeCpltCallback(hjpeg);
3424 #else
3425 HAL_JPEG_DecodeCpltCallback(hjpeg);
3426 #endif /*USE_HAL_JPEG_REGISTER_CALLBACKS*/
3428 else /* JPEG_CONTEXT_ENCODE */
3430 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
3431 hjpeg->EncodeCpltCallback(hjpeg);
3432 #else
3433 HAL_JPEG_EncodeCpltCallback(hjpeg);
3434 #endif
3437 return JPEG_PROCESS_DONE;
3441 return JPEG_PROCESS_ONGOING;
3445 * @brief Store some output data from the JPEG peripheral to the output buffer.
3446 * This function is used when the JPEG peripheral has new data to output
3447 * in case of Polling or Interrupt process
3448 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
3449 * the configuration information for JPEG module
3450 * @param nbOutputWords Number of output words (of 32 bits) ready from the JPEG peripheral
3451 * @retval None
3453 static void JPEG_StoreOutputData(JPEG_HandleTypeDef *hjpeg, uint32_t nbOutputWords)
3455 uint32_t index, nBwords, nbBytes, dataword;
3457 if (hjpeg->OutDataLength >= (hjpeg->JpegOutCount + (nbOutputWords * 4UL)))
3459 for (index = 0; index < nbOutputWords; index++)
3461 /*Transfer 32 bits from the JPEG output FIFO*/
3462 dataword = hjpeg->Instance->DOR;
3463 hjpeg->pJpegOutBuffPtr[hjpeg->JpegOutCount] = (uint8_t)(dataword & 0x000000FFUL);
3464 hjpeg->pJpegOutBuffPtr[hjpeg->JpegOutCount + 1UL] = (uint8_t)((dataword & 0x0000FF00UL) >> 8);
3465 hjpeg->pJpegOutBuffPtr[hjpeg->JpegOutCount + 2UL] = (uint8_t)((dataword & 0x00FF0000UL) >> 16);
3466 hjpeg->pJpegOutBuffPtr[hjpeg->JpegOutCount + 3UL] = (uint8_t)((dataword & 0xFF000000UL) >> 24);
3467 hjpeg->JpegOutCount += 4UL;
3469 if (hjpeg->OutDataLength == hjpeg->JpegOutCount)
3471 /*Output Buffer is full, call DecodedDataReadyCallback*/
3472 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
3473 hjpeg->DataReadyCallback(hjpeg, hjpeg->pJpegOutBuffPtr, hjpeg->JpegOutCount);
3474 #else
3475 HAL_JPEG_DataReadyCallback(hjpeg, hjpeg->pJpegOutBuffPtr, hjpeg->JpegOutCount);
3476 #endif /*USE_HAL_JPEG_REGISTER_CALLBACKS*/
3477 hjpeg->JpegOutCount = 0;
3480 else if (hjpeg->OutDataLength > hjpeg->JpegOutCount)
3482 nBwords = (hjpeg->OutDataLength - hjpeg->JpegOutCount) / 4UL;
3483 for (index = 0; index < nBwords; index++)
3485 /*Transfer 32 bits from the JPEG output FIFO*/
3486 dataword = hjpeg->Instance->DOR;
3487 hjpeg->pJpegOutBuffPtr[hjpeg->JpegOutCount] = (uint8_t)(dataword & 0x000000FFUL);
3488 hjpeg->pJpegOutBuffPtr[hjpeg->JpegOutCount + 1UL] = (uint8_t)((dataword & 0x0000FF00UL) >> 8);
3489 hjpeg->pJpegOutBuffPtr[hjpeg->JpegOutCount + 2UL] = (uint8_t)((dataword & 0x00FF0000UL) >> 16);
3490 hjpeg->pJpegOutBuffPtr[hjpeg->JpegOutCount + 3UL] = (uint8_t)((dataword & 0xFF000000UL) >> 24);
3491 hjpeg->JpegOutCount += 4UL;
3493 if (hjpeg->OutDataLength == hjpeg->JpegOutCount)
3495 /*Output Buffer is full, call DecodedDataReadyCallback*/
3496 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
3497 hjpeg->DataReadyCallback(hjpeg, hjpeg->pJpegOutBuffPtr, hjpeg->JpegOutCount);
3498 #else
3499 HAL_JPEG_DataReadyCallback(hjpeg, hjpeg->pJpegOutBuffPtr, hjpeg->JpegOutCount);
3500 #endif /* USE_HAL_JPEG_REGISTER_CALLBACKS */
3501 hjpeg->JpegOutCount = 0;
3503 else
3505 nbBytes = hjpeg->OutDataLength - hjpeg->JpegOutCount;
3506 dataword = hjpeg->Instance->DOR;
3507 for (index = 0; index < nbBytes; index++)
3509 hjpeg->pJpegOutBuffPtr[hjpeg->JpegOutCount] = (uint8_t)((dataword >> (8UL * (index & 0x3UL))) & 0xFFUL);
3510 hjpeg->JpegOutCount++;
3512 /*Output Buffer is full, call DecodedDataReadyCallback*/
3513 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
3514 hjpeg->DataReadyCallback(hjpeg, hjpeg->pJpegOutBuffPtr, hjpeg->JpegOutCount);
3515 #else
3516 HAL_JPEG_DataReadyCallback(hjpeg, hjpeg->pJpegOutBuffPtr, hjpeg->JpegOutCount);
3517 #endif /* USE_HAL_JPEG_REGISTER_CALLBACKS */
3519 hjpeg->JpegOutCount = 0;
3521 nbBytes = 4UL - nbBytes;
3522 for (index = nbBytes; index < 4UL; index++)
3524 hjpeg->pJpegOutBuffPtr[hjpeg->JpegOutCount] = (uint8_t)((dataword >> (8UL * index)) & 0xFFUL);
3525 hjpeg->JpegOutCount++;
3529 else
3531 /* Nothing to do */
3536 * @brief Read some input Data from the input buffer.
3537 * This function is used when the JPEG peripheral needs new data
3538 * in case of Polling or Interrupt process
3539 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
3540 * the configuration information for JPEG module
3541 * @param nbRequestWords Number of input words (of 32 bits) that the JPE peripheral request
3542 * @retval None
3544 static void JPEG_ReadInputData(JPEG_HandleTypeDef *hjpeg, uint32_t nbRequestWords)
3546 uint32_t nbBytes = 0, nBwords, index, Dataword, inputCount;
3548 if ((hjpeg->InDataLength == 0UL) || (nbRequestWords == 0UL))
3550 /* No more Input data : nothing to do*/
3551 (void) HAL_JPEG_Pause(hjpeg, JPEG_PAUSE_RESUME_INPUT);
3553 else if (hjpeg->InDataLength > hjpeg->JpegInCount)
3555 nbBytes = hjpeg->InDataLength - hjpeg->JpegInCount;
3557 else if (hjpeg->InDataLength == hjpeg->JpegInCount)
3559 /*Call HAL_JPEG_GetDataCallback to get new data */
3560 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
3561 hjpeg->GetDataCallback(hjpeg, hjpeg->JpegInCount);
3562 #else
3563 HAL_JPEG_GetDataCallback(hjpeg, hjpeg->JpegInCount);
3564 #endif /*USE_HAL_JPEG_REGISTER_CALLBACKS*/
3566 if (hjpeg->InDataLength > 4UL)
3568 hjpeg->InDataLength = hjpeg->InDataLength - (hjpeg->InDataLength % 4UL);
3570 hjpeg->JpegInCount = 0;
3571 nbBytes = hjpeg->InDataLength;
3573 else
3575 /* Nothing to do */
3577 if (((hjpeg->Context & JPEG_CONTEXT_PAUSE_INPUT) == 0UL) && (nbBytes > 0UL))
3579 nBwords = nbBytes / 4UL;
3580 if (nBwords >= nbRequestWords)
3582 for (index = 0; index < nbRequestWords; index++)
3584 inputCount = hjpeg->JpegInCount;
3585 hjpeg->Instance->DIR = (((uint32_t)(hjpeg->pJpegInBuffPtr[inputCount])) | \
3586 (((uint32_t)(hjpeg->pJpegInBuffPtr[inputCount + 1UL])) << 8) | \
3587 (((uint32_t)(hjpeg->pJpegInBuffPtr[inputCount + 2UL])) << 16) | \
3588 (((uint32_t)(hjpeg->pJpegInBuffPtr[inputCount + 3UL])) << 24));
3590 hjpeg->JpegInCount += 4UL;
3593 else /*nBwords < nbRequestWords*/
3595 if (nBwords > 0UL)
3597 for (index = 0; index < nBwords; index++)
3599 inputCount = hjpeg->JpegInCount;
3600 hjpeg->Instance->DIR = (((uint32_t)(hjpeg->pJpegInBuffPtr[inputCount])) | \
3601 (((uint32_t)(hjpeg->pJpegInBuffPtr[inputCount + 1UL])) << 8) | \
3602 (((uint32_t)(hjpeg->pJpegInBuffPtr[inputCount + 2UL])) << 16) | \
3603 (((uint32_t)(hjpeg->pJpegInBuffPtr[inputCount + 3UL])) << 24));
3605 hjpeg->JpegInCount += 4UL;
3608 else
3610 /* end of file*/
3611 Dataword = 0;
3612 for (index = 0; index < nbBytes; index++)
3614 Dataword |= (uint32_t)hjpeg->pJpegInBuffPtr[hjpeg->JpegInCount] << (8UL * (index & 0x03UL));
3615 hjpeg->JpegInCount++;
3617 hjpeg->Instance->DIR = Dataword;
3624 * @brief Start the JPEG DMA process (encoding/decoding)
3625 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
3626 * the configuration information for JPEG module
3627 * @retval JPEG_PROCESS_DONE if process ends else JPEG_PROCESS_ONGOING
3629 static HAL_StatusTypeDef JPEG_DMA_StartProcess(JPEG_HandleTypeDef *hjpeg)
3631 if ((hjpeg->InDataLength < 4UL) || (hjpeg->OutDataLength < 4UL))
3633 return HAL_ERROR;
3635 /* Reset Ending DMA internal context flag*/
3636 hjpeg->Context &= ~JPEG_CONTEXT_ENDING_DMA;
3638 /* Disable DMA In/Out Request*/
3639 JPEG_DISABLE_DMA(hjpeg, JPEG_DMA_ODMA | JPEG_DMA_IDMA);
3641 /* Set the JPEG DMA In transfer complete callback */
3642 hjpeg->hdmain->XferCpltCallback = JPEG_DMAInCpltCallback;
3643 /* Set the DMA In error callback */
3644 hjpeg->hdmain->XferErrorCallback = JPEG_DMAErrorCallback;
3646 /* Set the JPEG DMA Out transfer complete callback */
3647 hjpeg->hdmaout->XferCpltCallback = JPEG_DMAOutCpltCallback;
3648 /* Set the DMA Out error callback */
3649 hjpeg->hdmaout->XferErrorCallback = JPEG_DMAErrorCallback;
3650 /* Set the DMA Out Abort callback */
3651 hjpeg->hdmaout->XferAbortCallback = JPEG_DMAOutAbortCallback;
3653 /*DMA transfer size must be a multiple of 4 bytes i.e mutliple of 32bits words*/
3654 hjpeg->InDataLength = hjpeg->InDataLength - (hjpeg->InDataLength % 4UL);
3656 /*DMA transfer size must be a multiple of 4 bytes i.e mutliple of 32bits words*/
3657 hjpeg->OutDataLength = hjpeg->OutDataLength - (hjpeg->OutDataLength % 4UL);
3659 /* Start DMA FIFO In transfer */
3660 if (HAL_DMA_Start_IT(hjpeg->hdmain, (uint32_t)hjpeg->pJpegInBuffPtr, (uint32_t)&hjpeg->Instance->DIR,
3661 hjpeg->InDataLength >> 2) != HAL_OK)
3663 hjpeg->ErrorCode |= HAL_JPEG_ERROR_DMA;
3664 return HAL_ERROR;
3667 /* Start DMA FIFO Out transfer */
3668 if (HAL_DMA_Start_IT(hjpeg->hdmaout, (uint32_t)&hjpeg->Instance->DOR, (uint32_t)hjpeg->pJpegOutBuffPtr,
3669 hjpeg->OutDataLength >> 2) != HAL_OK)
3671 hjpeg->ErrorCode |= HAL_JPEG_ERROR_DMA;
3672 return HAL_ERROR;
3675 /* Enable JPEG In/Out DMA requests*/
3676 JPEG_ENABLE_DMA(hjpeg, JPEG_DMA_IDMA | JPEG_DMA_ODMA);
3678 return HAL_OK;
3682 * @brief Continue the current JPEG DMA process (encoding/decoding)
3683 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
3684 * the configuration information for JPEG module
3685 * @retval JPEG_PROCESS_DONE if process ends else JPEG_PROCESS_ONGOING
3687 static void JPEG_DMA_ContinueProcess(JPEG_HandleTypeDef *hjpeg)
3689 /*End of header processing flag rises*/
3690 if ((hjpeg->Context & JPEG_CONTEXT_OPERATION_MASK) == JPEG_CONTEXT_DECODE)
3692 if (__HAL_JPEG_GET_FLAG(hjpeg, JPEG_FLAG_HPDF) != 0UL)
3694 /*Call Header parsing complete callback */
3695 (void) HAL_JPEG_GetInfo(hjpeg, &hjpeg->Conf);
3697 /* Reset the ImageQuality */
3698 hjpeg->Conf.ImageQuality = 0;
3699 /* Note : the image quality is only available at the end of the decoding operation */
3700 /* at the current stage the calculated image quality is not correct so reset it */
3702 /*Call Info Ready callback */
3703 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
3704 hjpeg->InfoReadyCallback(hjpeg, &hjpeg->Conf);
3705 #else
3706 HAL_JPEG_InfoReadyCallback(hjpeg, &hjpeg->Conf);
3707 #endif /* USE_HAL_JPEG_REGISTER_CALLBACKS */
3709 __HAL_JPEG_DISABLE_IT(hjpeg, JPEG_IT_HPD);
3711 /* Clear header processing done flag */
3712 __HAL_JPEG_CLEAR_FLAG(hjpeg, JPEG_FLAG_HPDF);
3716 /*End of Conversion handling*/
3717 if (__HAL_JPEG_GET_FLAG(hjpeg, JPEG_FLAG_EOCF) != 0UL)
3719 /*Disabkle JPEG In/Out DMA Requests*/
3720 JPEG_DISABLE_DMA(hjpeg, JPEG_DMA_ODMA | JPEG_DMA_IDMA);
3722 hjpeg->Context |= JPEG_CONTEXT_ENDING_DMA;
3724 /*Stop Encoding/Decoding*/
3725 hjpeg->Instance->CONFR0 &= ~JPEG_CONFR0_START;
3727 __HAL_JPEG_DISABLE_IT(hjpeg, JPEG_INTERRUPT_MASK);
3729 /* Clear all flags */
3730 __HAL_JPEG_CLEAR_FLAG(hjpeg, JPEG_FLAG_ALL);
3732 if (hjpeg->hdmain->State == HAL_DMA_STATE_BUSY)
3734 /* Stop the DMA In Xfer*/
3735 (void) HAL_DMA_Abort_IT(hjpeg->hdmain);
3738 if (hjpeg->hdmaout->State == HAL_DMA_STATE_BUSY)
3740 /* Stop the DMA out Xfer*/
3741 (void) HAL_DMA_Abort_IT(hjpeg->hdmaout);
3743 else
3745 JPEG_DMA_EndProcess(hjpeg);
3753 * @brief Finalize the current JPEG DMA process (encoding/decoding)
3754 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
3755 * the configuration information for JPEG module
3756 * @retval JPEG_PROCESS_DONE
3758 static void JPEG_DMA_EndProcess(JPEG_HandleTypeDef *hjpeg)
3760 uint32_t tmpContext;
3761 hjpeg->JpegOutCount = hjpeg->OutDataLength - ((hjpeg->hdmaout->Instance->NDTR & DMA_SxNDT) << 2);
3763 /*if Output Buffer is full, call HAL_JPEG_DataReadyCallback*/
3764 if (hjpeg->JpegOutCount == hjpeg->OutDataLength)
3766 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
3767 hjpeg->DataReadyCallback(hjpeg, hjpeg->pJpegOutBuffPtr, hjpeg->JpegOutCount);
3768 #else
3769 HAL_JPEG_DataReadyCallback(hjpeg, hjpeg->pJpegOutBuffPtr, hjpeg->JpegOutCount);
3770 #endif /* USE_HAL_JPEG_REGISTER_CALLBACKS */
3772 hjpeg->JpegOutCount = 0;
3775 /*Check if remaining data in the output FIFO*/
3776 if (__HAL_JPEG_GET_FLAG(hjpeg, JPEG_FLAG_OFNEF) == 0UL)
3778 if (hjpeg->JpegOutCount > 0UL)
3780 /*Output Buffer is not empty, call DecodedDataReadyCallback*/
3781 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
3782 hjpeg->DataReadyCallback(hjpeg, hjpeg->pJpegOutBuffPtr, hjpeg->JpegOutCount);
3783 #else
3784 HAL_JPEG_DataReadyCallback(hjpeg, hjpeg->pJpegOutBuffPtr, hjpeg->JpegOutCount);
3785 #endif /* USE_HAL_JPEG_REGISTER_CALLBACKS */
3787 hjpeg->JpegOutCount = 0;
3790 /*Stop Encoding/Decoding*/
3791 hjpeg->Instance->CONFR0 &= ~JPEG_CONFR0_START;
3793 tmpContext = hjpeg->Context;
3794 /*Clear all context fileds execpt JPEG_CONTEXT_CONF_ENCODING and JPEG_CONTEXT_CUSTOM_TABLES*/
3795 hjpeg->Context &= (JPEG_CONTEXT_CONF_ENCODING | JPEG_CONTEXT_CUSTOM_TABLES);
3797 /* Process Unlocked */
3798 __HAL_UNLOCK(hjpeg);
3800 /* Change the JPEG state */
3801 hjpeg->State = HAL_JPEG_STATE_READY;
3803 /*Call End of Encoding/Decoding callback */
3804 if ((tmpContext & JPEG_CONTEXT_OPERATION_MASK) == JPEG_CONTEXT_DECODE)
3806 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
3807 hjpeg->DecodeCpltCallback(hjpeg);
3808 #else
3809 HAL_JPEG_DecodeCpltCallback(hjpeg);
3810 #endif /* USE_HAL_JPEG_REGISTER_CALLBACKS */
3812 else /* JPEG_CONTEXT_ENCODE */
3814 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
3815 hjpeg->EncodeCpltCallback(hjpeg);
3816 #else
3817 HAL_JPEG_EncodeCpltCallback(hjpeg);
3818 #endif /* USE_HAL_JPEG_REGISTER_CALLBACKS */
3821 else if ((hjpeg->Context & JPEG_CONTEXT_PAUSE_OUTPUT) == 0UL)
3823 JPEG_DMA_PollResidualData(hjpeg);
3825 else
3827 /* Nothing to do */
3833 * @brief Poll residual output data when DMA process (encoding/decoding)
3834 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
3835 * the configuration information for JPEG module
3836 * @retval None.
3838 static void JPEG_DMA_PollResidualData(JPEG_HandleTypeDef *hjpeg)
3840 uint32_t tmpContext, count, dataOut;
3842 for (count = JPEG_FIFO_SIZE; count > 0UL; count--)
3844 if ((hjpeg->Context & JPEG_CONTEXT_PAUSE_OUTPUT) == 0UL)
3846 if (__HAL_JPEG_GET_FLAG(hjpeg, JPEG_FLAG_OFNEF) != 0UL)
3848 dataOut = hjpeg->Instance->DOR;
3849 hjpeg->pJpegOutBuffPtr[hjpeg->JpegOutCount] = (uint8_t)(dataOut & 0x000000FFUL);
3850 hjpeg->pJpegOutBuffPtr[hjpeg->JpegOutCount + 1UL] = (uint8_t)((dataOut & 0x0000FF00UL) >> 8);
3851 hjpeg->pJpegOutBuffPtr[hjpeg->JpegOutCount + 2UL] = (uint8_t)((dataOut & 0x00FF0000UL) >> 16);
3852 hjpeg->pJpegOutBuffPtr[hjpeg->JpegOutCount + 3UL] = (uint8_t)((dataOut & 0xFF000000UL) >> 24);
3853 hjpeg->JpegOutCount += 4UL;
3855 if (hjpeg->JpegOutCount == hjpeg->OutDataLength)
3857 /*Output Buffer is full, call HAL_JPEG_DataReadyCallback*/
3858 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
3859 hjpeg->DataReadyCallback(hjpeg, hjpeg->pJpegOutBuffPtr, hjpeg->JpegOutCount);
3860 #else
3861 HAL_JPEG_DataReadyCallback(hjpeg, hjpeg->pJpegOutBuffPtr, hjpeg->JpegOutCount);
3862 #endif /* USE_HAL_JPEG_REGISTER_CALLBACKS */
3864 hjpeg->JpegOutCount = 0;
3871 tmpContext = hjpeg->Context;
3873 if ((__HAL_JPEG_GET_FLAG(hjpeg, JPEG_FLAG_OFNEF) == 0UL) || ((tmpContext & JPEG_CONTEXT_PAUSE_OUTPUT) == 0UL))
3875 /*Stop Encoding/Decoding*/
3876 hjpeg->Instance->CONFR0 &= ~JPEG_CONFR0_START;
3878 if (hjpeg->JpegOutCount > 0UL)
3880 /*Output Buffer is not empty, call DecodedDataReadyCallback*/
3881 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
3882 hjpeg->DataReadyCallback(hjpeg, hjpeg->pJpegOutBuffPtr, hjpeg->JpegOutCount);
3883 #else
3884 HAL_JPEG_DataReadyCallback(hjpeg, hjpeg->pJpegOutBuffPtr, hjpeg->JpegOutCount);
3885 #endif /* USE_HAL_JPEG_REGISTER_CALLBACKS */
3887 hjpeg->JpegOutCount = 0;
3890 tmpContext = hjpeg->Context;
3891 /*Clear all context fileds execpt JPEG_CONTEXT_CONF_ENCODING and JPEG_CONTEXT_CUSTOM_TABLES*/
3892 hjpeg->Context &= (JPEG_CONTEXT_CONF_ENCODING | JPEG_CONTEXT_CUSTOM_TABLES);
3894 /* Process Unlocked */
3895 __HAL_UNLOCK(hjpeg);
3897 /* Change the JPEG state */
3898 hjpeg->State = HAL_JPEG_STATE_READY;
3900 /*Call End of Encoding/Decoding callback */
3901 if ((tmpContext & JPEG_CONTEXT_OPERATION_MASK) == JPEG_CONTEXT_DECODE)
3903 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
3904 hjpeg->DecodeCpltCallback(hjpeg);
3905 #else
3906 HAL_JPEG_DecodeCpltCallback(hjpeg);
3907 #endif /* USE_HAL_JPEG_REGISTER_CALLBACKS */
3909 else /* JPEG_CONTEXT_ENCODE */
3911 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
3912 hjpeg->EncodeCpltCallback(hjpeg);
3913 #else
3914 HAL_JPEG_EncodeCpltCallback(hjpeg);
3915 #endif /* USE_HAL_JPEG_REGISTER_CALLBACKS */
3921 * @brief DMA input transfer complete callback
3922 * @param hdma pointer to a DMA_HandleTypeDef structure.
3923 * @retval None
3925 static void JPEG_DMAInCpltCallback(DMA_HandleTypeDef *hdma)
3927 JPEG_HandleTypeDef *hjpeg = (JPEG_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
3929 /* Disable The JPEG IT so the DMA Input Callback can not be interrupted by the JPEG EOC IT or JPEG HPD IT */
3930 __HAL_JPEG_DISABLE_IT(hjpeg, JPEG_INTERRUPT_MASK);
3932 if ((hjpeg->Context & (JPEG_CONTEXT_METHOD_MASK | JPEG_CONTEXT_ENDING_DMA)) ==
3933 JPEG_CONTEXT_DMA) /* Check if context method is DMA and we are not in ending DMA stage */
3935 JPEG_DISABLE_DMA(hjpeg, JPEG_DMA_IDMA);
3937 hjpeg->JpegInCount = hjpeg->InDataLength - ((hdma->Instance->NDTR & DMA_SxNDT) << 2);
3939 /*Call HAL_JPEG_GetDataCallback to get new data */
3940 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
3941 hjpeg->GetDataCallback(hjpeg, hjpeg->JpegInCount);
3942 #else
3943 HAL_JPEG_GetDataCallback(hjpeg, hjpeg->JpegInCount);
3944 #endif /* USE_HAL_JPEG_REGISTER_CALLBACKS */
3946 if (hjpeg->InDataLength >= 4UL)
3948 /*JPEG Input DMA transfer data number must be multiple of 32 bits word
3949 as the destination is a 32 bits (4 bytes) register */
3950 hjpeg->InDataLength = hjpeg->InDataLength - (hjpeg->InDataLength % 4UL);
3952 else if (hjpeg->InDataLength > 0UL)
3954 /*Transfer last data word (i.e last 4 bytes)*/
3955 hjpeg->InDataLength = 4;
3958 if (((hjpeg->Context & JPEG_CONTEXT_PAUSE_INPUT) == 0UL) && (hjpeg->InDataLength > 0UL))
3960 /* Start DMA FIFO In transfer */
3961 if (HAL_DMA_Start_IT(hjpeg->hdmain, (uint32_t)hjpeg->pJpegInBuffPtr, (uint32_t)&hjpeg->Instance->DIR,
3962 hjpeg->InDataLength >> 2) != HAL_OK)
3964 hjpeg->ErrorCode |= HAL_JPEG_ERROR_DMA;
3965 hjpeg->State = HAL_JPEG_STATE_ERROR;
3966 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
3967 hjpeg->ErrorCallback(hjpeg);
3968 #else
3969 HAL_JPEG_ErrorCallback(hjpeg);
3970 #endif /* USE_HAL_JPEG_REGISTER_CALLBACKS */
3971 return;
3973 JPEG_ENABLE_DMA(hjpeg, JPEG_DMA_IDMA);
3976 /* JPEG Conversion still on going : Enable the JPEG IT */
3977 __HAL_JPEG_ENABLE_IT(hjpeg, JPEG_IT_EOC | JPEG_IT_HPD);
3982 * @brief DMA output transfer complete callback
3983 * @param hdma pointer to a DMA_HandleTypeDef structure.
3984 * @retval None
3986 static void JPEG_DMAOutCpltCallback(DMA_HandleTypeDef *hdma)
3988 JPEG_HandleTypeDef *hjpeg = (JPEG_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
3990 /* Disable The JPEG IT so the DMA Output Callback can not be interrupted by the JPEG EOC IT or JPEG HPD IT */
3991 __HAL_JPEG_DISABLE_IT(hjpeg, JPEG_INTERRUPT_MASK);
3993 if ((hjpeg->Context & (JPEG_CONTEXT_METHOD_MASK | JPEG_CONTEXT_ENDING_DMA)) ==
3994 JPEG_CONTEXT_DMA) /* Check if context method is DMA and we are not in ending DMA stage */
3996 if (__HAL_JPEG_GET_FLAG(hjpeg, JPEG_FLAG_EOCF) == 0UL)
3998 JPEG_DISABLE_DMA(hjpeg, JPEG_DMA_ODMA);
3999 hjpeg->JpegOutCount = hjpeg->OutDataLength - ((hdma->Instance->NDTR & DMA_SxNDT) << 2);
4001 /*Output Buffer is full, call HAL_JPEG_DataReadyCallback*/
4002 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
4003 hjpeg->DataReadyCallback(hjpeg, hjpeg->pJpegOutBuffPtr, hjpeg->JpegOutCount);
4004 #else
4005 HAL_JPEG_DataReadyCallback(hjpeg, hjpeg->pJpegOutBuffPtr, hjpeg->JpegOutCount);
4006 #endif /* USE_HAL_JPEG_REGISTER_CALLBACKS */
4008 if ((hjpeg->Context & JPEG_CONTEXT_PAUSE_OUTPUT) == 0UL)
4010 /* Start DMA FIFO Out transfer */
4011 if (HAL_DMA_Start_IT(hjpeg->hdmaout, (uint32_t)&hjpeg->Instance->DOR, (uint32_t)hjpeg->pJpegOutBuffPtr,
4012 hjpeg->OutDataLength >> 2) != HAL_OK)
4014 hjpeg->ErrorCode |= HAL_JPEG_ERROR_DMA;
4015 hjpeg->State = HAL_JPEG_STATE_ERROR;
4016 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
4017 hjpeg->ErrorCallback(hjpeg);
4018 #else
4019 HAL_JPEG_ErrorCallback(hjpeg);
4020 #endif /* USE_HAL_JPEG_REGISTER_CALLBACKS */
4021 return;
4023 JPEG_ENABLE_DMA(hjpeg, JPEG_DMA_ODMA);
4027 /* JPEG Conversion still on going : Enable the JPEG IT */
4028 __HAL_JPEG_ENABLE_IT(hjpeg, JPEG_IT_EOC | JPEG_IT_HPD);
4034 * @brief DMA Transfer error callback
4035 * @param hdma pointer to a DMA_HandleTypeDef structure.
4036 * @retval None
4038 static void JPEG_DMAErrorCallback(DMA_HandleTypeDef *hdma)
4040 JPEG_HandleTypeDef *hjpeg = (JPEG_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
4042 /* if DMA error is FIFO error ignore it */
4043 if (HAL_DMA_GetError(hdma) != HAL_DMA_ERROR_FE)
4045 /*Stop Encoding/Decoding*/
4046 hjpeg->Instance->CONFR0 &= ~JPEG_CONFR0_START;
4048 /* Disable All Interrupts */
4049 __HAL_JPEG_DISABLE_IT(hjpeg, JPEG_INTERRUPT_MASK);
4051 /* Disable All DMA requests */
4052 JPEG_DISABLE_DMA(hjpeg, JPEG_DMA_MASK);
4054 hjpeg->State = HAL_JPEG_STATE_READY;
4055 hjpeg->ErrorCode |= HAL_JPEG_ERROR_DMA;
4056 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
4057 hjpeg->ErrorCallback(hjpeg);
4058 #else
4059 HAL_JPEG_ErrorCallback(hjpeg);
4060 #endif /* USE_HAL_JPEG_REGISTER_CALLBACKS */
4065 * @brief DMA output Abort callback
4066 * @param hdma pointer to a DMA_HandleTypeDef structure.
4067 * @retval None
4069 static void JPEG_DMAOutAbortCallback(DMA_HandleTypeDef *hdma)
4071 JPEG_HandleTypeDef *hjpeg = (JPEG_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
4073 if ((hjpeg->Context & JPEG_CONTEXT_ENDING_DMA) != 0UL)
4075 JPEG_DMA_EndProcess(hjpeg);
4081 * @brief Calculate the decoded image quality (from 1 to 100)
4082 * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
4083 * the configuration information for JPEG module
4084 * @retval JPEG image quality from 1 to 100.
4086 static uint32_t JPEG_GetQuality(JPEG_HandleTypeDef *hjpeg)
4088 uint32_t quality = 0;
4089 uint32_t quantRow, quantVal, scale, i, j;
4090 __IO uint32_t *tableAddress = hjpeg->Instance->QMEM0;
4092 i = 0;
4093 while (i < (JPEG_QUANT_TABLE_SIZE - 3UL))
4095 quantRow = *tableAddress;
4096 for (j = 0; j < 4UL; j++)
4098 quantVal = (quantRow >> (8UL * j)) & 0xFFUL;
4099 if (quantVal == 1UL)
4101 /* if Quantization value = 1 then quality is 100%*/
4102 quality += 100UL;
4104 else
4106 /* Note that the quantization coefficients must be specified in the table in zigzag order */
4107 scale = (quantVal * 100UL) / ((uint32_t) hjpeg->QuantTable0[JPEG_ZIGZAG_ORDER[i + j]]);
4109 if (scale <= 100UL)
4111 quality += (200UL - scale) / 2UL;
4113 else
4115 quality += 5000UL / scale;
4120 i += 4UL;
4121 tableAddress ++;
4124 return (quality / 64UL);
4127 * @}
4130 #endif /* JPEG */
4131 #endif /* HAL_JPEG_MODULE_ENABLED */
4133 * @}
4137 * @}
4140 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/