Merge pull request #10558 from iNavFlight/MrD_Correct-comments-on-OSD-symbols
[inav.git] / lib / main / STM32F7 / Drivers / STM32F7xx_HAL_Driver / Src / stm32f7xx_hal_jpeg.c
blob07c45c8ae1cc227bba4d7ec769c92c173c58d0dd
1 /**
2 ******************************************************************************
3 * @file stm32f7xx_hal_jpeg.c
4 * @author MCD Application Team
5 * @version V1.2.2
6 * @date 14-April-2017
7 * @brief JPEG HAL module driver.
8 * This file provides firmware functions to manage the following
9 * functionalities of the JPEG encoder/decoder peripheral:
10 * + Initialization and de-initialization functions
11 * + JPEG processing functions encoding and decoding
12 * + JPEG decoding Getting Info and encoding configuration setting
13 * + JPEG enable/disable header parsing functions (for decoding)
14 * + JPEG Input/Output Buffer configuration.
15 * + JPEG callback functions
16 * + JPEG Abort/Pause/Resume functions
17 * + JPEG custom quantization tables setting functions
18 * + IRQ handler management
19 * + Peripheral State and Error functions
21 @verbatim
22 ==============================================================================
23 ##### How to use this driver #####
24 ==============================================================================
25 [..]
26 (#) Initialize the JPEG peripheral using HAL_JPEG_Init : No initialization parameters are required.
27 Only the call to HAL_JPEG_Init is necessary to initialize the JPEG peripheral.
29 (#) If operation is JPEG encoding use function HAL_JPEG_ConfigEncoding to set
30 the encoding parameters (mandatory before calling the encoding function).
31 the application can change the encoding parameter "ImageQuality" from
32 1 to 100 to obtain a more or less quality (visual quality vs the original row image),
33 and inversely more or less jpg file size.
35 (#) Note that for decoding operation the JPEG peripheral output data are organized in
36 YCbCr blocks called MCU (Minimum Coded Unit) as defioned in the JPEG specification
37 ISO/IEC 10918-1 standard.
38 It is up to the application to transform these YCbCr blocks to RGB data that can be display.
40 Respectively, for Encoding operation the JPEG peripheral input should be organized
41 in YCbCr MCU blocks. It is up to the application to perform the necessary RGB to YCbCr
42 MCU blocks transformation before feeding the JPEG peripheral with data.
44 (#) Use functions HAL_JPEG_Encode and HAL_JPEG_Decode to start respectively
45 a JPEG encoding/decoding operation in polling method (blocking).
47 (#) Use functions HAL_JPEG_Encode_IT and HAL_JPEG_Decode_IT to start respectively
48 a JPEG encoding/decoding operation with Interrupt method (not blocking).
50 (#) Use functions HAL_JPEG_Encode_DMA and HAL_JPEG_Decode_DMA to start respectively
51 a JPEG encoding/decoding operation with DMA method (not blocking).
53 (#) Callback HAL_JPEG_InfoReadyCallback is asserted if the current operation
54 is a JPEG decoding to provide the application with JPEG image parameters.
55 This callback is asserted when the JPEG peripheral successfully parse the
56 JPEG header.
58 (#) Callback HAL_JPEG_GetDataCallback is asserted for both encoding and decoding
59 operations to inform the application that the input buffer has been
60 consumed by the peripheral and to ask for a new data chunk if the operation
61 (encoding/decoding) has not been complete yet.
63 (++) This CallBack should be implemented in the application side. It should
64 call the function HAL_JPEG_ConfigInputBuffer if new input data are available,
65 or call HAL_JPEG_Pause with parameter XferSelection set to JPEG_PAUSE_RESUME_INPUT
66 to inform the JPEG HAL driver that the ongoing operation shall pause waiting for the
67 application to provide a new input data chunk.
68 Once the application succeed getting new data and if the input has been paused,
69 the application can call the function HAL_JPEG_ConfigInputBuffer to set the new
70 input buffer and size, then resume the JPEG HAL input by calling new function HAL_JPEG_Resume.
71 If the application has ended feeding the HAL JPEG with input data (no more input data), the application
72 Should call the function HAL_JPEG_ConfigInputBuffer (within the callback HAL_JPEG_GetDataCallback)
73 with the parameter InDataLength set to zero.
75 (++) The mechanism of HAL_JPEG_ConfigInputBuffer/HAL_JPEG_Pause/HAL_JPEG_Resume allows
76 to the application to provide the input data (for encoding or decoding) by chunks.
77 If the new input data chunk is not available (because data should be read from an input file
78 for example) the application can pause the JPEG input (using function HAL_JPEG_Pause)
79 Once the new input data chunk is available ( read from a file for example), the application
80 can call the function HAL_JPEG_ConfigInputBuffer to provide the HAL with the new chunk
81 then resume the JPEG HAL input by calling function HAL_JPEG_Resume.
83 (++) The application can call functions HAL_JPEG_ConfigInputBuffer then HAL_JPEG_Resume.
84 any time (outside the HAL_JPEG_GetDataCallback) Once the new input chunk data available.
85 However, to keep data coherency, the function HAL_JPEG_Pause must be imperatively called
86 (if necessary) within the callback HAL_JPEG_GetDataCallback, i.e when the HAL JPEG has ended
87 Transferring the previous chunk buffer to the JPEG peripheral.
89 (#) Callback HAL_JPEG_DataReadyCallback is asserted when the HAL JPEG driver
90 has filled the given output buffer with the given size.
92 (++) This CallBack should be implemented in the application side. It should
93 call the function HAL_JPEG_ConfigOutputBuffer to provide the HAL JPEG driver
94 with the new output buffer location and size to be used to store next data chunk.
95 if the application is not ready to provide the output chunk location then it can
96 call the function HAL_JPEG_Pause with parameter XferSelection set to "JPEG_PAUSE_RESUME_OUTPUT"
97 to inform the JPEG HAL driver that it shall pause output data. Once the application
98 is ready to receive the new data chunk (output buffer location free or available) it should call
99 the function HAL_JPEG_ConfigOutputBuffer to provide the HAL JPEG driver
100 with the new output chunk buffer location and size, then call "HAL_JPEG_Resume"
101 to inform the HAL that it shall resume outputting data in the given output buffer.
103 (++) The mechanism of HAL_JPEG_ConfigOutputBuffer/HAL_JPEG_Pause/HAL_JPEG_Resume allows
104 the application to receive data from the JPEG peripheral by chunks. when a chunk
105 is received, the application can pause the HAL JPEG output data to be able to process
106 these received data (YCbCr to RGB conversion in case of decoding or data storage in case
107 of encoding).
109 (++) The application can call functions HAL_JPEG_ ConfigOutputBuffer then HAL_JPEG_Resume.
110 any time (outside the HAL_JPEG_DataReadyCallback) Once the output data buffer is free to use.
111 However, to keep data coherency, the function HAL_JPEG_Pause must be imperatively called
112 (if necessary) within the callback HAL_JPEG_ DataReadyCallback, i.e when the HAL JPEG has ended
113 Transferring the previous chunk buffer from the JPEG peripheral to the application.
115 (#) Callback HAL_JPEG_EncodeCpltCallback is asserted when the HAL JPEG driver has
116 ended the current JPEG encoding operation, and all output data has been transmitted
117 to the application.
119 (#) Callback HAL_JPEG_DecodeCpltCallback is asserted when the HAL JPEG driver has
120 ended the current JPEG decoding operation. and all output data has been transmitted
121 to the application.
123 (#) Callback HAL_JPEG_ErrorCallback is asserted when an error occurred during
124 the current operation. the application can call the function "HAL_JPEG_GetError"
125 to retrieve the error codes.
127 (#) By default the HAL JPEG driver uses the default quantization tables
128 as provide in the JPEG specification (ISO/IEC 10918-1 standard) for encoding.
129 User can change these default tables if necessary using the function HAL_JPEG_SetUserQuantTables
130 Note that for decoding the quantization tables are automatically extracted from
131 the JPEG header.
133 (#) To control JPEG state you can use the following function: HAL_JPEG_GetState()
135 *** JPEG HAL driver macros list ***
136 =============================================
137 [..]
138 Below the list of most used macros in JPEG HAL driver.
140 (+) __HAL_JPEG_RESET_HANDLE_STATE : Reset JPEG handle state.
141 (+) __HAL_JPEG_ENABLE : Enable the JPEG peripheral.
142 (+) __HAL_JPEG_DISABLE : Disable the JPEG peripheral.
143 (+) __HAL_JPEG_GET_FLAG : Check the specified JPEG status flag.
144 (+) __HAL_JPEG_CLEAR_FLAG : Clear the specified JPEG status flag.
145 (+) __HAL_JPEG_ENABLE_IT : Enable the specified JPEG Interrupt.
146 (+) __HAL_JPEG_DISABLE_IT : Disable the specified JPEG Interrupt.
147 (+) __HAL_JPEG_GET_IT_SOURCE : returns the state of the specified JPEG Interrupt (Enabled or disabled).
150 @endverbatim
151 ******************************************************************************
152 * @attention
154 * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
156 * Redistribution and use in source and binary forms, with or without modification,
157 * are permitted provided that the following conditions are met:
158 * 1. Redistributions of source code must retain the above copyright notice,
159 * this list of conditions and the following disclaimer.
160 * 2. Redistributions in binary form must reproduce the above copyright notice,
161 * this list of conditions and the following disclaimer in the documentation
162 * and/or other materials provided with the distribution.
163 * 3. Neither the name of STMicroelectronics nor the names of its contributors
164 * may be used to endorse or promote products derived from this software
165 * without specific prior written permission.
167 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
168 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
169 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
170 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
171 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
172 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
173 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
174 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
175 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
176 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
178 ******************************************************************************
181 /* Includes ------------------------------------------------------------------*/
182 #include "stm32f7xx_hal.h"
184 /** @addtogroup STM32F7xx_HAL_Driver
185 * @{
188 /** @defgroup JPEG JPEG
189 * @brief JPEG HAL module driver.
190 * @{
193 #ifdef HAL_JPEG_MODULE_ENABLED
195 #if defined (STM32F767xx) || defined (STM32F769xx) || defined (STM32F777xx) || defined (STM32F779xx)
197 /* Private define ------------------------------------------------------------*/
198 /** @addtogroup JPEG_Private_Constants
199 * @{
201 #define JPEG_TIMEOUT_VALUE ((uint32_t)1000U) /* 1s */
202 #define JPEG_AC_HUFF_TABLE_SIZE ((uint32_t)162U) /* Huffman AC table size : 162 codes*/
203 #define JPEG_DC_HUFF_TABLE_SIZE ((uint32_t)12U) /* Huffman AC table size : 12 codes*/
205 #define JPEG_FIFO_SIZE ((uint32_t)16U) /* JPEG Input/Output HW FIFO size in words*/
207 #define JPEG_INTERRUPT_MASK ((uint32_t)0x0000007EU) /* JPEG Interrupt Mask*/
209 #define JPEG_DMA_MASK ((uint32_t)0x00001800U) /* JPEG DMA request Mask*/
210 #define JPEG_DMA_IDMA ((uint32_t)JPEG_CR_IDMAEN) /* DMA request for the input FIFO */
211 #define JPEG_DMA_ODMA ((uint32_t)JPEG_CR_ODMAEN) /* DMA request for the output FIFO */
213 #define JPEG_CONTEXT_ENCODE ((uint32_t)0x00000001U) /* JPEG context : operation is encoding*/
214 #define JPEG_CONTEXT_DECODE ((uint32_t)0x00000002U) /* JPEG context : operation is decoding*/
215 #define JPEG_CONTEXT_OPERATION_MASK ((uint32_t)0x00000003U) /* JPEG context : operation Mask */
217 #define JPEG_CONTEXT_POLLING ((uint32_t)0x00000004U) /* JPEG context : Transfer use Polling */
218 #define JPEG_CONTEXT_IT ((uint32_t)0x00000008U) /* JPEG context : Transfer use Interrupt */
219 #define JPEG_CONTEXT_DMA ((uint32_t)0x0000000CU) /* JPEG context : Transfer use DMA */
220 #define JPEG_CONTEXT_METHOD_MASK ((uint32_t)0x0000000CU) /* JPEG context : Transfer Mask */
223 #define JPEG_CONTEXT_CONF_ENCODING ((uint32_t)0x00000100U) /* JPEG context : encoding config done */
225 #define JPEG_CONTEXT_PAUSE_INPUT ((uint32_t)0x00001000U) /* JPEG context : Pause Input */
226 #define JPEG_CONTEXT_PAUSE_OUTPUT ((uint32_t)0x00002000U) /* JPEG context : Pause Output */
228 #define JPEG_CONTEXT_CUSTOM_TABLES ((uint32_t)0x00004000U) /* JPEG context : Use custom quantization tables */
230 #define JPEG_CONTEXT_ENDING_DMA ((uint32_t)0x00008000U) /* JPEG context : ending with DMA in progress */
232 #define JPEG_PROCESS_ONGOING ((uint32_t)0x00000000U) /* Process is on going */
233 #define JPEG_PROCESS_DONE ((uint32_t)0x00000001U) /* Process is done (ends) */
235 * @}
238 /* Private typedef -----------------------------------------------------------*/
239 /** @addtogroup JPEG_Private_Types
240 * @{
244 JPEG Huffman Table Structure definition :
245 This implementation of Huffman table structure is compliant with ISO/IEC 10918-1 standard , Annex C Huffman Table specification
247 typedef struct
249 /* These two fields directly represent the contents of a JPEG DHT marker */
250 uint8_t Bits[16]; /*!< bits[k] = # of symbols with codes of length k bits, this parameter corresponds to BITS list in the Annex C */
252 uint8_t HuffVal[162]; /*!< The symbols, in order of incremented code length, this parameter corresponds to HUFFVAL list in the Annex C */
255 }JPEG_ACHuffTableTypeDef;
257 typedef struct
259 /* These two fields directly represent the contents of a JPEG DHT marker */
260 uint8_t Bits[16]; /*!< bits[k] = # of symbols with codes of length k bits, this parameter corresponds to BITS list in the Annex C */
262 uint8_t HuffVal[12]; /*!< The symbols, in order of incremented code length, this parameter corresponds to HUFFVAL list in the Annex C */
265 }JPEG_DCHuffTableTypeDef;
267 typedef struct
269 uint8_t CodeLength[JPEG_AC_HUFF_TABLE_SIZE]; /*!< Code length */
271 uint32_t HuffmanCode[JPEG_AC_HUFF_TABLE_SIZE]; /*!< HuffmanCode */
273 }JPEG_AC_HuffCodeTableTypeDef;
275 typedef struct
277 uint8_t CodeLength[JPEG_DC_HUFF_TABLE_SIZE]; /*!< Code length */
279 uint32_t HuffmanCode[JPEG_DC_HUFF_TABLE_SIZE]; /*!< HuffmanCode */
281 }JPEG_DC_HuffCodeTableTypeDef;
283 * @}
286 /* Private macro -------------------------------------------------------------*/
287 /** @addtogroup JPEG_Private_Macros
288 * @{
290 #define JPEG_ENABLE_DMA(__HANDLE__,__DMA__) ((__HANDLE__)->Instance->CR |= ((__DMA__) & JPEG_DMA_MASK))
291 /*note : To disable a DMA request we must use MODIFY_REG macro to avoid writing "1" to the FIFO flush bits
292 located in the same DMA request enable register (CR register). */
293 #define JPEG_DISABLE_DMA(__HANDLE__,__DMA__) MODIFY_REG((__HANDLE__)->Instance->CR, ((__DMA__) & JPEG_DMA_MASK), 0)
295 * @}
299 /* Private variables ---------------------------------------------------------*/
300 /** @addtogroup JPEG_Private_Variables
301 * @{
304 static const JPEG_DCHuffTableTypeDef JPEG_DCLUM_HuffTable =
306 { 0, 1, 5, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0 }, /*Bits*/
308 { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0xa, 0xb } /*HUFFVAL */
312 static const JPEG_DCHuffTableTypeDef JPEG_DCCHROM_HuffTable =
314 { 0, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0 }, /*Bits*/
316 { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0xa, 0xb } /*HUFFVAL */
319 static const JPEG_ACHuffTableTypeDef JPEG_ACLUM_HuffTable =
321 { 0, 2, 1, 3, 3, 2, 4, 3, 5, 5, 4, 4, 0, 0, 1, 0x7d }, /*Bits*/
323 { 0x01, 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12, /*HUFFVAL */
324 0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61, 0x07,
325 0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xa1, 0x08,
326 0x23, 0x42, 0xb1, 0xc1, 0x15, 0x52, 0xd1, 0xf0,
327 0x24, 0x33, 0x62, 0x72, 0x82, 0x09, 0x0a, 0x16,
328 0x17, 0x18, 0x19, 0x1a, 0x25, 0x26, 0x27, 0x28,
329 0x29, 0x2a, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
330 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49,
331 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59,
332 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69,
333 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79,
334 0x7a, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89,
335 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
336 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
337 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6,
338 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3, 0xc4, 0xc5,
339 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 0xd3, 0xd4,
340 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe1, 0xe2,
341 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea,
342 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
343 0xf9, 0xfa }
346 static const JPEG_ACHuffTableTypeDef JPEG_ACCHROM_HuffTable =
348 { 0, 2, 1, 2, 4, 4, 3, 4, 7, 5, 4, 4, 0, 1, 2, 0x77 }, /*Bits*/
350 { 0x00, 0x01, 0x02, 0x03, 0x11, 0x04, 0x05, 0x21, /*HUFFVAL */
351 0x31, 0x06, 0x12, 0x41, 0x51, 0x07, 0x61, 0x71,
352 0x13, 0x22, 0x32, 0x81, 0x08, 0x14, 0x42, 0x91,
353 0xa1, 0xb1, 0xc1, 0x09, 0x23, 0x33, 0x52, 0xf0,
354 0x15, 0x62, 0x72, 0xd1, 0x0a, 0x16, 0x24, 0x34,
355 0xe1, 0x25, 0xf1, 0x17, 0x18, 0x19, 0x1a, 0x26,
356 0x27, 0x28, 0x29, 0x2a, 0x35, 0x36, 0x37, 0x38,
357 0x39, 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48,
358 0x49, 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58,
359 0x59, 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68,
360 0x69, 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78,
361 0x79, 0x7a, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
362 0x88, 0x89, 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96,
363 0x97, 0x98, 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5,
364 0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4,
365 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3,
366 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2,
367 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda,
368 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9,
369 0xea, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
370 0xf9, 0xfa }
375 These are the sample quantization tables given in JPEG spec ISO/IEC 10918-1 standard , section K.1.
377 static const uint8_t JPEG_LUM_QuantTable[JPEG_QUANT_TABLE_SIZE] =
379 16, 11, 10, 16, 24, 40, 51, 61,
380 12, 12, 14, 19, 26, 58, 60, 55,
381 14, 13, 16, 24, 40, 57, 69, 56,
382 14, 17, 22, 29, 51, 87, 80, 62,
383 18, 22, 37, 56, 68, 109, 103, 77,
384 24, 35, 55, 64, 81, 104, 113, 92,
385 49, 64, 78, 87, 103, 121, 120, 101,
386 72, 92, 95, 98, 112, 100, 103, 99
388 static const uint8_t JPEG_CHROM_QuantTable[JPEG_QUANT_TABLE_SIZE] =
390 17, 18, 24, 47, 99, 99, 99, 99,
391 18, 21, 26, 66, 99, 99, 99, 99,
392 24, 26, 56, 99, 99, 99, 99, 99,
393 47, 66, 99, 99, 99, 99, 99, 99,
394 99, 99, 99, 99, 99, 99, 99, 99,
395 99, 99, 99, 99, 99, 99, 99, 99,
396 99, 99, 99, 99, 99, 99, 99, 99,
397 99, 99, 99, 99, 99, 99, 99, 99
400 static const uint8_t JPEG_ZIGZAG_ORDER[JPEG_QUANT_TABLE_SIZE] =
402 0, 1, 8, 16, 9, 2, 3, 10,
403 17, 24, 32, 25, 18, 11, 4, 5,
404 12, 19, 26, 33, 40, 48, 41, 34,
405 27, 20, 13, 6, 7, 14, 21, 28,
406 35, 42, 49, 56, 57, 50, 43, 36,
407 29, 22, 15, 23, 30, 37, 44, 51,
408 58, 59, 52, 45, 38, 31, 39, 46,
409 53, 60, 61, 54, 47, 55, 62, 63
412 * @}
415 /* Private function prototypes -----------------------------------------------*/
416 /** @addtogroup JPEG_Private_Functions_Prototypes
417 * @{
420 static HAL_StatusTypeDef JPEG_Bits_To_SizeCodes(uint8_t *Bits, uint8_t *Huffsize, uint32_t *Huffcode, uint32_t *LastK);
421 static HAL_StatusTypeDef JPEG_DCHuff_BitsVals_To_SizeCodes(JPEG_DCHuffTableTypeDef *DC_BitsValsTable, JPEG_DC_HuffCodeTableTypeDef *DC_SizeCodesTable);
422 static HAL_StatusTypeDef JPEG_ACHuff_BitsVals_To_SizeCodes(JPEG_ACHuffTableTypeDef *AC_BitsValsTable, JPEG_AC_HuffCodeTableTypeDef *AC_SizeCodesTable);
423 static HAL_StatusTypeDef JPEG_Set_HuffDC_Mem(JPEG_HandleTypeDef *hjpeg, JPEG_DCHuffTableTypeDef *HuffTableDC, uint32_t *DCTableAddress);
424 static HAL_StatusTypeDef JPEG_Set_HuffAC_Mem(JPEG_HandleTypeDef *hjpeg, JPEG_ACHuffTableTypeDef *HuffTableAC, uint32_t *ACTableAddress);
425 static HAL_StatusTypeDef JPEG_Set_HuffEnc_Mem(JPEG_HandleTypeDef *hjpeg, JPEG_ACHuffTableTypeDef *HuffTableAC0, JPEG_DCHuffTableTypeDef *HuffTableDC0 , JPEG_ACHuffTableTypeDef *HuffTableAC1, JPEG_DCHuffTableTypeDef *HuffTableDC1);
426 static void JPEG_Set_Huff_DHTMem(JPEG_HandleTypeDef *hjpeg, JPEG_ACHuffTableTypeDef *HuffTableAC0, JPEG_DCHuffTableTypeDef *HuffTableDC0 , JPEG_ACHuffTableTypeDef *HuffTableAC1, JPEG_DCHuffTableTypeDef *HuffTableDC1);
427 static HAL_StatusTypeDef JPEG_Set_Quantization_Mem(JPEG_HandleTypeDef *hjpeg, uint8_t *QTable, uint32_t *QTableAddress);
428 static void JPEG_SetColorYCBCR(JPEG_HandleTypeDef *hjpeg);
429 static void JPEG_SetColorGrayScale(JPEG_HandleTypeDef *hjpeg);
430 static void JPEG_SetColorCMYK(JPEG_HandleTypeDef *hjpeg);
432 static void JPEG_Init_Process(JPEG_HandleTypeDef *hjpeg);
433 static uint32_t JPEG_Process(JPEG_HandleTypeDef *hjpeg);
434 static void JPEG_ReadInputData(JPEG_HandleTypeDef *hjpeg, uint32_t nbRequestWords);
435 static void JPEG_StoreOutputData(JPEG_HandleTypeDef *hjpeg, uint32_t nbOutputWords);
436 static uint32_t JPEG_GetQuality(JPEG_HandleTypeDef *hjpeg);
438 static HAL_StatusTypeDef JPEG_DMA_StartProcess(JPEG_HandleTypeDef *hjpeg);
439 static uint32_t JPEG_DMA_ContinueProcess(JPEG_HandleTypeDef *hjpeg);
440 static uint32_t JPEG_DMA_EndProcess(JPEG_HandleTypeDef *hjpeg);
441 static void JPEG_DMA_PollResidualData(JPEG_HandleTypeDef *hjpeg);
442 static void JPEG_DMAOutCpltCallback(DMA_HandleTypeDef *hdma);
443 static void JPEG_DMAInCpltCallback(DMA_HandleTypeDef *hdma);
444 static void JPEG_DMAErrorCallback(DMA_HandleTypeDef *hdma);
445 static void JPEG_DMAOutAbortCallback(DMA_HandleTypeDef *hdma) ;
447 * @}
450 /** @defgroup JPEG_Exported_Functions JPEG Exported Functions
451 * @{
454 /** @defgroup JPEG_Exported_Functions_Group1 Initialization and de-initialization functions
455 * @brief Initialization and de-initialization functions.
457 @verbatim
458 ==============================================================================
459 ##### Initialization and de-initialization functions #####
460 ==============================================================================
461 [..] This section provides functions allowing to:
462 (+) Initialize the JPEG peripheral and creates the associated handle
463 (+) DeInitialize the JPEG peripheral
465 @endverbatim
466 * @{
470 * @brief Initializes the JPEG according to the specified
471 * parameters in the JPEG_InitTypeDef and creates the associated handle.
472 * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
473 * the configuration information for JPEG module
474 * @retval HAL status
476 HAL_StatusTypeDef HAL_JPEG_Init(JPEG_HandleTypeDef *hjpeg)
478 /*Note : these intermediate variables are used to avoid MISRA warning
479 regarding rule 11.5 */
480 uint32_t acLum_huffmanTableAddr = (uint32_t)(&JPEG_ACLUM_HuffTable);
481 uint32_t dcLum_huffmanTableAddr = (uint32_t)(&JPEG_DCLUM_HuffTable);
482 uint32_t acChrom_huffmanTableAddr = (uint32_t)(&JPEG_ACCHROM_HuffTable);
483 uint32_t dcChrom_huffmanTableAddr = (uint32_t)(&JPEG_DCCHROM_HuffTable);
485 /* Check the JPEG handle allocation */
486 if(hjpeg == NULL)
488 return HAL_ERROR;
491 if(hjpeg->State == HAL_JPEG_STATE_RESET)
493 /* Allocate lock resource and initialize it */
494 hjpeg->Lock = HAL_UNLOCKED;
496 /* Init the low level hardware : GPIO, CLOCK */
497 HAL_JPEG_MspInit(hjpeg);
500 /* Change the JPEG state */
501 hjpeg->State = HAL_JPEG_STATE_BUSY;
503 /* Start the JPEG Core*/
504 __HAL_JPEG_ENABLE(hjpeg);
506 /* Stop the JPEG encoding/decoding process*/
507 hjpeg->Instance->CONFR0 &= ~JPEG_CONFR0_START;
509 /* Disable All Interrupts */
510 __HAL_JPEG_DISABLE_IT(hjpeg,JPEG_INTERRUPT_MASK);
512 /* Disable All DMA requests */
513 JPEG_DISABLE_DMA(hjpeg,JPEG_DMA_MASK);
515 /* Flush input and output FIFOs*/
516 hjpeg->Instance->CR |= JPEG_CR_IFF;
517 hjpeg->Instance->CR |= JPEG_CR_OFF;
519 /* Clear all flags */
520 __HAL_JPEG_CLEAR_FLAG(hjpeg,JPEG_FLAG_ALL);
522 hjpeg->QuantTable0 = (uint8_t *)JPEG_LUM_QuantTable;
523 hjpeg->QuantTable1 = (uint8_t *)JPEG_CHROM_QuantTable;
524 hjpeg->QuantTable2 = NULL;
525 hjpeg->QuantTable3 = NULL;
527 /* init the default Huffman tables*/
528 if(JPEG_Set_HuffEnc_Mem(hjpeg, (JPEG_ACHuffTableTypeDef *)acLum_huffmanTableAddr, (JPEG_DCHuffTableTypeDef *)dcLum_huffmanTableAddr, (JPEG_ACHuffTableTypeDef *)acChrom_huffmanTableAddr, (JPEG_DCHuffTableTypeDef *)dcChrom_huffmanTableAddr) != HAL_OK)
530 hjpeg->ErrorCode = HAL_JPEG_ERROR_HUFF_TABLE;
532 return HAL_ERROR;
535 /* Enable header processing*/
536 hjpeg->Instance->CONFR1 |= JPEG_CONFR1_HDR;
538 /* Reset JpegInCount and JpegOutCount */
539 hjpeg->JpegInCount = 0;
540 hjpeg->JpegOutCount = 0;
542 /* Change the JPEG state */
543 hjpeg->State = HAL_JPEG_STATE_READY;
545 /* Reset the JPEG ErrorCode */
546 hjpeg->ErrorCode = HAL_JPEG_ERROR_NONE;
548 /*Clear the context filelds*/
549 hjpeg->Context = 0;
551 /* Return function status */
552 return HAL_OK;
556 * @brief DeInitializes the JPEG peripheral.
557 * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
558 * the configuration information for JPEG module
559 * @retval HAL status
561 HAL_StatusTypeDef HAL_JPEG_DeInit(JPEG_HandleTypeDef *hjpeg)
563 /* Check the JPEG handle allocation */
564 if(hjpeg == NULL)
566 return HAL_ERROR;
569 /* DeInit the low level hardware: CLOCK, NVIC.*/
570 HAL_JPEG_MspDeInit(hjpeg);
572 /* Change the JPEG state */
573 hjpeg->State = HAL_JPEG_STATE_BUSY;
575 /* Reset the JPEG ErrorCode */
576 hjpeg->ErrorCode = HAL_JPEG_ERROR_NONE;
578 /* Reset JpegInCount and JpegOutCount */
579 hjpeg->JpegInCount = 0;
580 hjpeg->JpegOutCount = 0;
582 /* Change the JPEG state */
583 hjpeg->State = HAL_JPEG_STATE_RESET;
585 /*Clear the context fields*/
586 hjpeg->Context = 0;
588 /* Release Lock */
589 __HAL_UNLOCK(hjpeg);
591 /* Return function status */
592 return HAL_OK;
596 * @brief Initializes the JPEG MSP.
597 * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
598 * the configuration information for JPEG module
599 * @retval None
601 __weak void HAL_JPEG_MspInit(JPEG_HandleTypeDef *hjpeg)
603 /* Prevent unused argument(s) compilation warning */
604 UNUSED(hjpeg);
606 /* NOTE : This function Should not be modified, when the callback is needed,
607 the HAL_JPEG_MspInit could be implemented in the user file
612 * @brief DeInitializes JPEG MSP.
613 * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
614 * the configuration information for JPEG module
615 * @retval None
617 __weak void HAL_JPEG_MspDeInit(JPEG_HandleTypeDef *hjpeg)
619 /* Prevent unused argument(s) compilation warning */
620 UNUSED(hjpeg);
622 /* NOTE : This function Should not be modified, when the callback is needed,
623 the HAL_JPEG_MspDeInit could be implemented in the user file
628 * @}
631 /** @defgroup JPEG_Exported_Functions_Group2 Configuration functions
632 * @brief JPEG Configuration functions.
634 @verbatim
635 ==============================================================================
636 ##### Configuration functions #####
637 ==============================================================================
638 [..] This section provides functions allowing to:
639 (+) HAL_JPEG_ConfigEncoding() : JPEG encoding configuration
640 (+) HAL_JPEG_GetInfo() : Extract the image configuration from the JPEG header during the decoding
641 (+) HAL_JPEG_EnableHeaderParsing() : Enable JPEG Header parsing for decoding
642 (+) HAL_JPEG_DisableHeaderParsing() : Disable JPEG Header parsing for decoding
643 (+) HAL_JPEG_SetUserQuantTables : Modify the default Quantization tables used for JPEG encoding.
645 @endverbatim
646 * @{
650 * @brief Set the JPEG encoding configuration.
651 * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
652 * the configuration information for JPEG module
653 * @param pConf: pointer to a JPEG_ConfTypeDef structure that contains
654 * the encoding configuration
655 * @retval HAL status
657 HAL_StatusTypeDef HAL_JPEG_ConfigEncoding(JPEG_HandleTypeDef *hjpeg, JPEG_ConfTypeDef *pConf)
659 uint32_t error = HAL_OK;
660 uint32_t numberMCU, hfactor, vfactor,hMCU, vMCU;
662 /* Check the JPEG handle allocation */
663 if( (hjpeg == NULL) || (pConf == NULL) )
665 return HAL_ERROR;
667 else
669 /* Check the parameters */
670 assert_param(IS_JPEG_COLORSPACE(pConf->ColorSpace));
671 assert_param(IS_JPEG_CHROMASUBSAMPLING(pConf->ChromaSubsampling));
672 assert_param(IS_JPEG_IMAGE_QUALITY(pConf->ImageQuality));
674 /* Process Locked */
675 __HAL_LOCK(hjpeg);
677 if(hjpeg->State == HAL_JPEG_STATE_READY)
679 hjpeg->State = HAL_JPEG_STATE_BUSY;
681 hjpeg->Conf.ColorSpace = pConf->ColorSpace;
682 hjpeg->Conf.ChromaSubsampling = pConf->ChromaSubsampling;
683 hjpeg->Conf.ImageHeight = pConf->ImageHeight;
684 hjpeg->Conf.ImageWidth = pConf->ImageWidth;
685 hjpeg->Conf.ImageQuality = pConf->ImageQuality;
687 /* Reset the Color Space : by default only one quantization table is used*/
688 hjpeg->Instance->CONFR1 &= ~JPEG_CONFR1_COLORSPACE;
690 /* Set Number of color components*/
691 if(hjpeg->Conf.ColorSpace == JPEG_GRAYSCALE_COLORSPACE)
693 /*Gray Scale is only one component 8x8 blocks i.e 4:4:4*/
694 hjpeg->Conf.ChromaSubsampling = JPEG_444_SUBSAMPLING;
696 JPEG_SetColorGrayScale(hjpeg);
697 /* Set quantization table 0*/
698 error = JPEG_Set_Quantization_Mem(hjpeg, hjpeg->QuantTable0, (uint32_t *)(hjpeg->Instance->QMEM0));
700 else if(hjpeg->Conf.ColorSpace == JPEG_YCBCR_COLORSPACE)
703 Set the Color Space for YCbCr : 2 quantization tables are used
704 one for Luminance(Y) and one for both Chrominances (Cb & Cr)
706 hjpeg->Instance->CONFR1 |= JPEG_CONFR1_COLORSPACE_0;
708 JPEG_SetColorYCBCR(hjpeg);
710 /* Set quantization table 0*/
711 error = JPEG_Set_Quantization_Mem(hjpeg, hjpeg->QuantTable0, (uint32_t *)(hjpeg->Instance->QMEM0));
712 /*By default quantization table 0 for component 0 and quantization table 1 for both components 1 and 2*/
713 error |= JPEG_Set_Quantization_Mem(hjpeg, hjpeg->QuantTable1, (uint32_t *)(hjpeg->Instance->QMEM1));
715 if((hjpeg->Context & JPEG_CONTEXT_CUSTOM_TABLES) != 0) /*Use user customized quantization tables , 1 table per component*/
717 /* use 3 quantization tables , one for each component*/
718 hjpeg->Instance->CONFR1 &= (~JPEG_CONFR1_COLORSPACE);
719 hjpeg->Instance->CONFR1 |= JPEG_CONFR1_COLORSPACE_1;
721 error |= JPEG_Set_Quantization_Mem(hjpeg, hjpeg->QuantTable2, (uint32_t *)(hjpeg->Instance->QMEM2));
723 /*Use Quantization 1 table for component 1*/
724 hjpeg->Instance->CONFR5 &= (~JPEG_CONFR5_QT);
725 hjpeg->Instance->CONFR5 |= JPEG_CONFR5_QT_0;
727 /*Use Quantization 2 table for component 2*/
728 hjpeg->Instance->CONFR6 &= (~JPEG_CONFR6_QT);
729 hjpeg->Instance->CONFR6 |= JPEG_CONFR6_QT_1;
732 else if(hjpeg->Conf.ColorSpace == JPEG_CMYK_COLORSPACE)
734 JPEG_SetColorCMYK(hjpeg);
736 /* Set quantization table 0*/
737 error = JPEG_Set_Quantization_Mem(hjpeg, hjpeg->QuantTable0, (uint32_t *)(hjpeg->Instance->QMEM0));
738 /*By default quantization table 0 for All components*/
740 if((hjpeg->Context & JPEG_CONTEXT_CUSTOM_TABLES) != 0) /*Use user customized quantization tables , 1 table per component*/
742 /* use 4 quantization tables , one for each component*/
743 hjpeg->Instance->CONFR1 |= JPEG_CONFR1_COLORSPACE;
745 error |= JPEG_Set_Quantization_Mem(hjpeg, hjpeg->QuantTable1, (uint32_t *)(hjpeg->Instance->QMEM1));
746 error |= JPEG_Set_Quantization_Mem(hjpeg, hjpeg->QuantTable2, (uint32_t *)(hjpeg->Instance->QMEM2));
747 error |= JPEG_Set_Quantization_Mem(hjpeg, hjpeg->QuantTable3, (uint32_t *)(hjpeg->Instance->QMEM3));
749 /*Use Quantization 1 table for component 1*/
750 hjpeg->Instance->CONFR5 |= JPEG_CONFR5_QT_0;
752 /*Use Quantization 2 table for component 2*/
753 hjpeg->Instance->CONFR6 |= JPEG_CONFR6_QT_1;
755 /*Use Quantization 3 table for component 3*/
756 hjpeg->Instance->CONFR7 |= JPEG_CONFR7_QT;
760 if(error != HAL_OK)
762 hjpeg->ErrorCode = HAL_JPEG_ERROR_QUANT_TABLE;
764 /* Process Unlocked */
765 __HAL_UNLOCK(hjpeg);
767 /* Set the JPEG State to ready */
768 hjpeg->State = HAL_JPEG_STATE_READY;
770 return HAL_ERROR;
772 /* Set the image size*/
773 MODIFY_REG(hjpeg->Instance->CONFR1, JPEG_CONFR1_YSIZE, ((hjpeg->Conf.ImageHeight & 0x0000FFFF) << 16)); /* set the number of lines*/
774 MODIFY_REG(hjpeg->Instance->CONFR3, JPEG_CONFR3_XSIZE, ((hjpeg->Conf.ImageWidth & 0x0000FFFF) << 16)); /* set the number of pixels per line*/
776 if(hjpeg->Conf.ChromaSubsampling == JPEG_420_SUBSAMPLING) /* 4:2:0*/
778 hfactor = 16;
779 vfactor = 16;
781 else if(hjpeg->Conf.ChromaSubsampling == JPEG_422_SUBSAMPLING) /* 4:2:2*/
783 hfactor = 16;
784 vfactor = 8;
786 else /* Default is 8x8 MCU, 4:4:4*/
788 hfactor = 8;
789 vfactor = 8;
792 hMCU = (hjpeg->Conf.ImageWidth / hfactor);
793 if((hjpeg->Conf.ImageWidth % hfactor) != 0)
795 hMCU++; /*+1 for horizontal incomplete MCU */
798 vMCU = (hjpeg->Conf.ImageHeight / vfactor);
799 if((hjpeg->Conf.ImageHeight % vfactor) != 0)
801 vMCU++; /*+1 for vertical incomplete MCU */
804 numberMCU = (hMCU * vMCU) - 1; /* Bit Field JPEG_CONFR2_NMCU shall be set to NB_MCU - 1*/
805 /* Set the number of MCU*/
806 hjpeg->Instance->CONFR2 = (numberMCU & JPEG_CONFR2_NMCU);
808 hjpeg->Context |= JPEG_CONTEXT_CONF_ENCODING;
810 /* Process Unlocked */
811 __HAL_UNLOCK(hjpeg);
813 /* Set the JPEG State to ready */
814 hjpeg->State = HAL_JPEG_STATE_READY;
816 /* Return function status */
817 return HAL_OK;
819 else
821 /* Process Unlocked */
822 __HAL_UNLOCK(hjpeg);
824 /* Return function status */
825 return HAL_BUSY;
831 * @brief Extract the image configuration from the JPEG header during the decoding
832 * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
833 * the configuration information for JPEG module
834 * @param pInfo: pointer to a JPEG_ConfTypeDef structure that contains
835 * The JPEG decoded header informations
836 * @retval HAL status
838 HAL_StatusTypeDef HAL_JPEG_GetInfo(JPEG_HandleTypeDef *hjpeg, JPEG_ConfTypeDef *pInfo)
840 uint32_t yblockNb, cBblockNb, cRblockNb;
842 /* Check the JPEG handle allocation */
843 if((hjpeg == NULL) || (pInfo == NULL))
845 return HAL_ERROR;
848 /*Read the conf parameters */
849 if((hjpeg->Instance->CONFR1 & JPEG_CONFR1_NF) == JPEG_CONFR1_NF_1)
851 pInfo->ColorSpace = JPEG_YCBCR_COLORSPACE;
853 else if((hjpeg->Instance->CONFR1 & JPEG_CONFR1_NF) == 0)
855 pInfo->ColorSpace = JPEG_GRAYSCALE_COLORSPACE;
857 else if((hjpeg->Instance->CONFR1 & JPEG_CONFR1_NF) == JPEG_CONFR1_NF)
859 pInfo->ColorSpace = JPEG_CMYK_COLORSPACE;
862 pInfo->ImageHeight = (hjpeg->Instance->CONFR1 & 0xFFFF0000U) >> 16;
863 pInfo->ImageWidth = (hjpeg->Instance->CONFR3 & 0xFFFF0000U) >> 16;
865 if((pInfo->ColorSpace == JPEG_YCBCR_COLORSPACE) || (pInfo->ColorSpace == JPEG_CMYK_COLORSPACE))
867 yblockNb = (hjpeg->Instance->CONFR4 & JPEG_CONFR4_NB) >> 4;
868 cBblockNb = (hjpeg->Instance->CONFR5 & JPEG_CONFR5_NB) >> 4;
869 cRblockNb = (hjpeg->Instance->CONFR6 & JPEG_CONFR6_NB) >> 4;
871 if((yblockNb == 1) && (cBblockNb == 0) && (cRblockNb == 0))
873 pInfo->ChromaSubsampling = JPEG_422_SUBSAMPLING; /*16x8 block*/
875 else if((yblockNb == 0) && (cBblockNb == 0) && (cRblockNb == 0))
877 pInfo->ChromaSubsampling = JPEG_444_SUBSAMPLING;
879 else if((yblockNb == 3) && (cBblockNb == 0) && (cRblockNb == 0))
881 pInfo->ChromaSubsampling = JPEG_420_SUBSAMPLING;
883 else /*Default is 4:4:4*/
885 pInfo->ChromaSubsampling = JPEG_444_SUBSAMPLING;
888 else
890 pInfo->ChromaSubsampling = JPEG_444_SUBSAMPLING;
893 pInfo->ImageQuality = JPEG_GetQuality(hjpeg);
895 /* Return function status */
896 return HAL_OK;
900 * @brief Enable JPEG Header parsing for decoding
901 * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
902 * the configuration information for the JPEG.
903 * @retval HAL status
905 HAL_StatusTypeDef HAL_JPEG_EnableHeaderParsing(JPEG_HandleTypeDef *hjpeg)
907 /* Process locked */
908 __HAL_LOCK(hjpeg);
910 if(hjpeg->State == HAL_JPEG_STATE_READY)
912 /* Change the JPEG state */
913 hjpeg->State = HAL_JPEG_STATE_BUSY;
915 /* Enable header processing*/
916 hjpeg->Instance->CONFR1 |= JPEG_CONFR1_HDR;
918 /* Process unlocked */
919 __HAL_UNLOCK(hjpeg);
921 /* Change the JPEG state */
922 hjpeg->State = HAL_JPEG_STATE_READY;
924 return HAL_OK;
926 else
928 /* Process unlocked */
929 __HAL_UNLOCK(hjpeg);
931 return HAL_BUSY;
936 * @brief Disable JPEG Header parsing for decoding
937 * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
938 * the configuration information for the JPEG.
939 * @retval HAL status
941 HAL_StatusTypeDef HAL_JPEG_DisableHeaderParsing(JPEG_HandleTypeDef *hjpeg)
943 /* Process locked */
944 __HAL_LOCK(hjpeg);
946 if(hjpeg->State == HAL_JPEG_STATE_READY)
948 /* Change the JPEG state */
949 hjpeg->State = HAL_JPEG_STATE_BUSY;
951 /* Disable header processing*/
952 hjpeg->Instance->CONFR1 &= ~JPEG_CONFR1_HDR;
954 /* Process unlocked */
955 __HAL_UNLOCK(hjpeg);
957 /* Change the JPEG state */
958 hjpeg->State = HAL_JPEG_STATE_READY;
960 return HAL_OK;
962 else
964 /* Process unlocked */
965 __HAL_UNLOCK(hjpeg);
967 return HAL_BUSY;
972 * @brief Modify the default Quantization tables used for JPEG encoding.
973 * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
974 * the configuration information for JPEG module
975 * @param QTable0 : pointer to uint8_t , define the user quantification table for color component 1.
976 * If NULL assume no need to update the table and no error return
977 * @param QTable1 : pointer to uint8_t , define the user quantification table for color component 2.
978 * If NULL assume no need to update the table and no error return.
979 * @param QTable2 : pointer to uint8_t , define the user quantification table for color component 3,
980 * If NULL assume no need to update the table and no error return.
981 * @param QTable3 : pointer to uint8_t , define the user quantification table for color component 4.
982 * If NULL assume no need to update the table and no error return.
984 * @retval HAL status
988 HAL_StatusTypeDef HAL_JPEG_SetUserQuantTables(JPEG_HandleTypeDef *hjpeg, uint8_t *QTable0, uint8_t *QTable1, uint8_t *QTable2, uint8_t *QTable3)
990 /* Process Locked */
991 __HAL_LOCK(hjpeg);
993 if(hjpeg->State == HAL_JPEG_STATE_READY)
995 /* Change the DMA state */
996 hjpeg->State = HAL_JPEG_STATE_BUSY;
998 hjpeg->Context |= JPEG_CONTEXT_CUSTOM_TABLES;
1000 hjpeg->QuantTable0 = QTable0;
1001 hjpeg->QuantTable1 = QTable1;
1002 hjpeg->QuantTable2 = QTable2;
1003 hjpeg->QuantTable3 = QTable3;
1005 /* Process Unlocked */
1006 __HAL_UNLOCK(hjpeg);
1008 /* Change the DMA state */
1009 hjpeg->State = HAL_JPEG_STATE_READY;
1011 /* Return function status */
1012 return HAL_OK;
1014 else
1016 /* Process Unlocked */
1017 __HAL_UNLOCK(hjpeg);
1019 return HAL_BUSY;
1024 * @}
1027 /** @defgroup JPEG_Exported_Functions_Group3 encoding/decoding processing functions
1028 * @brief processing functions.
1030 @verbatim
1031 ==============================================================================
1032 ##### JPEG processing functions #####
1033 ==============================================================================
1034 [..] This section provides functions allowing to:
1035 (+) HAL_JPEG_Encode() : JPEG encoding with polling process
1036 (+) HAL_JPEG_Decode() : JPEG decoding with polling process
1037 (+) HAL_JPEG_Encode_IT() : JPEG encoding with interrupt process
1038 (+) HAL_JPEG_Decode_IT() : JPEG decoding with interrupt process
1039 (+) HAL_JPEG_Encode_DMA() : JPEG encoding with DMA process
1040 (+) HAL_JPEG_Decode_DMA() : JPEG decoding with DMA process
1041 (+) HAL_JPEG_Pause() : Pause the Input/Output processing
1042 (+) HAL_JPEG_Resume() : Resume the JPEG Input/Output processing
1043 (+) HAL_JPEG_ConfigInputBuffer() : Config Encoding/Decoding Input Buffer
1044 (+) HAL_JPEG_ConfigOutputBuffer() : Config Encoding/Decoding Output Buffer
1045 (+) HAL_JPEG_Abort() : Aborts the JPEG Encoding/Decoding
1047 @endverbatim
1048 * @{
1052 * @brief Starts JPEG encoding with polling processing
1053 * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
1054 * the configuration information for JPEG module
1055 * @param pDataInMCU: Pointer to the Input buffer
1056 * @param InDataLength: size in bytes Input buffer
1057 * @param pDataOut: Pointer to the jpeg output data buffer
1058 * @param OutDataLength: size in bytes of the Output buffer
1059 * @param Timeout: Specify Timeout value
1060 * @retval HAL status
1062 HAL_StatusTypeDef HAL_JPEG_Encode(JPEG_HandleTypeDef *hjpeg, uint8_t *pDataInMCU, uint32_t InDataLength, uint8_t *pDataOut, uint32_t OutDataLength, uint32_t Timeout)
1064 uint32_t tickstart = 0;
1066 /* Check the parameters */
1067 assert_param((InDataLength >= 4));
1068 assert_param((OutDataLength >= 4));
1070 /* Check In/out buffer allocation and size */
1071 if((hjpeg == NULL) || (pDataInMCU == NULL) || (pDataOut == NULL) || \
1072 (InDataLength == 0) || (OutDataLength == 0))
1074 return HAL_ERROR;
1076 /* Process locked */
1077 __HAL_LOCK(hjpeg);
1079 if(hjpeg->State != HAL_JPEG_STATE_READY)
1081 /* Process Unlocked */
1082 __HAL_UNLOCK(hjpeg);
1084 return HAL_BUSY;
1087 if(hjpeg->State == HAL_JPEG_STATE_READY)
1089 if((hjpeg->Context & JPEG_CONTEXT_CONF_ENCODING) == JPEG_CONTEXT_CONF_ENCODING )
1091 /*Change JPEG state*/
1092 hjpeg->State = HAL_JPEG_STATE_BUSY_ENCODING;
1094 /*Set the Context to Encode with Polling*/
1095 hjpeg->Context &= ~(JPEG_CONTEXT_OPERATION_MASK | JPEG_CONTEXT_METHOD_MASK);
1096 hjpeg->Context |= (JPEG_CONTEXT_ENCODE | JPEG_CONTEXT_POLLING);
1098 /* Get tick */
1099 tickstart = HAL_GetTick();
1100 /*In/Out Data length must be multiple of 4 Bytes (1 word)*/
1101 InDataLength = InDataLength - (InDataLength % 4);
1102 OutDataLength = OutDataLength - (OutDataLength % 4);
1104 /*Store In/out buffers pointers and size*/
1105 hjpeg->pJpegInBuffPtr = pDataInMCU;
1106 hjpeg->pJpegOutBuffPtr = pDataOut;
1107 hjpeg->InDataLength = InDataLength;
1108 hjpeg->OutDataLength = OutDataLength;
1110 /*Reset In/out data counter */
1111 hjpeg->JpegInCount = 0;
1112 hjpeg->JpegOutCount = 0;
1114 /*Init decoding process*/
1115 JPEG_Init_Process(hjpeg);
1117 /*JPEG data processing : In/Out FIFO transfer*/
1118 while((JPEG_Process(hjpeg) == JPEG_PROCESS_ONGOING))
1120 if(Timeout != HAL_MAX_DELAY)
1122 if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
1125 /* Update error code */
1126 hjpeg->ErrorCode |= HAL_JPEG_ERROR_TIMEOUT;
1128 /* Process Unlocked */
1129 __HAL_UNLOCK(hjpeg);
1131 /*Change JPEG state*/
1132 hjpeg->State= HAL_JPEG_STATE_READY;
1134 return HAL_TIMEOUT;
1139 /* Process Unlocked */
1140 __HAL_UNLOCK(hjpeg);
1142 /*Change JPEG state*/
1143 hjpeg->State= HAL_JPEG_STATE_READY;
1145 }else
1147 /* Process Unlocked */
1148 __HAL_UNLOCK(hjpeg);
1150 return HAL_ERROR;
1153 /* Return function status */
1154 return HAL_OK;
1158 * @brief Starts JPEG decoding with polling processing
1159 * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
1160 * the configuration information for JPEG module
1161 * @param pDataIn: Pointer to the input data buffer
1162 * @param InDataLength: size in bytes Input buffer
1163 * @param pDataOutMCU: Pointer to the Output data buffer
1164 * @param OutDataLength: size in bytes of the Output buffer
1165 * @param Timeout: Specify Timeout value
1166 * @retval HAL status
1168 HAL_StatusTypeDef HAL_JPEG_Decode(JPEG_HandleTypeDef *hjpeg ,uint8_t *pDataIn ,uint32_t InDataLength ,uint8_t *pDataOutMCU ,uint32_t OutDataLength, uint32_t Timeout)
1170 uint32_t tickstart = 0;
1172 /* Check the parameters */
1173 assert_param((InDataLength >= 4));
1174 assert_param((OutDataLength >= 4));
1176 /* Check In/out buffer allocation and size */
1177 if((hjpeg == NULL) || (pDataIn == NULL) || (pDataOutMCU == NULL) || \
1178 (InDataLength == 0) || (OutDataLength == 0))
1180 return HAL_ERROR;
1183 /* Process Locked */
1184 __HAL_LOCK(hjpeg);
1186 /* Get tick */
1187 tickstart = HAL_GetTick();
1189 if(hjpeg->State == HAL_JPEG_STATE_READY)
1191 /*Change JPEG state*/
1192 hjpeg->State = HAL_JPEG_STATE_BUSY_DECODING;
1194 /*Set the Context to Decode with Polling*/
1195 /*Set the Context to Encode with Polling*/
1196 hjpeg->Context &= ~(JPEG_CONTEXT_OPERATION_MASK | JPEG_CONTEXT_METHOD_MASK);
1197 hjpeg->Context |= (JPEG_CONTEXT_DECODE | JPEG_CONTEXT_POLLING);
1199 /*In/Out Data length must be multiple of 4 Bytes (1 word)*/
1200 InDataLength = InDataLength - (InDataLength % 4);
1201 OutDataLength = OutDataLength - (OutDataLength % 4);
1203 /*Store In/out buffers pointers and size*/
1204 hjpeg->pJpegInBuffPtr = pDataIn;
1205 hjpeg->pJpegOutBuffPtr = pDataOutMCU;
1206 hjpeg->InDataLength = InDataLength;
1207 hjpeg->OutDataLength = OutDataLength;
1209 /*Reset In/out data counter */
1210 hjpeg->JpegInCount = 0;
1211 hjpeg->JpegOutCount = 0;
1213 /*Init decoding process*/
1214 JPEG_Init_Process(hjpeg);
1216 /*JPEG data processing : In/Out FIFO transfer*/
1217 while((JPEG_Process(hjpeg) == JPEG_PROCESS_ONGOING))
1219 if(Timeout != HAL_MAX_DELAY)
1221 if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
1224 /* Update error code */
1225 hjpeg->ErrorCode |= HAL_JPEG_ERROR_TIMEOUT;
1227 /* Process Unlocked */
1228 __HAL_UNLOCK(hjpeg);
1230 /*Change JPEG state*/
1231 hjpeg->State= HAL_JPEG_STATE_READY;
1233 return HAL_TIMEOUT;
1238 /* Process Unlocked */
1239 __HAL_UNLOCK(hjpeg);
1241 /*Change JPEG state*/
1242 hjpeg->State= HAL_JPEG_STATE_READY;
1244 }else
1246 /* Process Unlocked */
1247 __HAL_UNLOCK(hjpeg);
1249 return HAL_BUSY;
1251 /* Return function status */
1252 return HAL_OK;
1256 * @brief Starts JPEG encoding with interrupt processing
1257 * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
1258 * the configuration information for JPEG module
1259 * @param pDataInMCU: Pointer to the Input buffer
1260 * @param InDataLength: size in bytes Input buffer
1261 * @param pDataOut: Pointer to the jpeg output data buffer
1262 * @param OutDataLength: size in bytes of the Output buffer
1263 * @retval HAL status
1265 HAL_StatusTypeDef HAL_JPEG_Encode_IT(JPEG_HandleTypeDef *hjpeg, uint8_t *pDataInMCU, uint32_t InDataLength, uint8_t *pDataOut, uint32_t OutDataLength)
1267 /* Check the parameters */
1268 assert_param((InDataLength >= 4));
1269 assert_param((OutDataLength >= 4));
1271 /* Check In/out buffer allocation and size */
1272 if((hjpeg == NULL) || (pDataInMCU == NULL) || (pDataOut == NULL) || \
1273 (InDataLength == 0) || (OutDataLength == 0))
1275 return HAL_ERROR;
1278 /* Process Locked */
1279 __HAL_LOCK(hjpeg);
1281 if(hjpeg->State != HAL_JPEG_STATE_READY)
1283 /* Process Unlocked */
1284 __HAL_UNLOCK(hjpeg);
1286 return HAL_BUSY;
1288 else
1290 if((hjpeg->Context & JPEG_CONTEXT_CONF_ENCODING) == JPEG_CONTEXT_CONF_ENCODING )
1292 /*Change JPEG state*/
1293 hjpeg->State = HAL_JPEG_STATE_BUSY_ENCODING;
1295 /*Set the Context to Encode with IT*/
1296 hjpeg->Context &= ~(JPEG_CONTEXT_OPERATION_MASK | JPEG_CONTEXT_METHOD_MASK);
1297 hjpeg->Context |= (JPEG_CONTEXT_ENCODE | JPEG_CONTEXT_IT);
1299 /*In/Out Data length must be multiple of 4 Bytes (1 word)*/
1300 InDataLength = InDataLength - (InDataLength % 4);
1301 OutDataLength = OutDataLength - (OutDataLength % 4);
1303 /*Store In/out buffers pointers and size*/
1304 hjpeg->pJpegInBuffPtr = pDataInMCU;
1305 hjpeg->pJpegOutBuffPtr = pDataOut;
1306 hjpeg->InDataLength = InDataLength;
1307 hjpeg->OutDataLength = OutDataLength;
1309 /*Reset In/out data counter */
1310 hjpeg->JpegInCount = 0;
1311 hjpeg->JpegOutCount = 0;
1313 /*Init decoding process*/
1314 JPEG_Init_Process(hjpeg);
1317 else
1319 /* Process Unlocked */
1320 __HAL_UNLOCK(hjpeg);
1322 return HAL_ERROR;
1325 /* Return function status */
1326 return HAL_OK;
1330 * @brief Starts JPEG decoding with interrupt processing
1331 * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
1332 * the configuration information for JPEG module
1333 * @param pDataIn: Pointer to the input data buffer
1334 * @param InDataLength: size in bytes Input buffer
1335 * @param pDataOutMCU: Pointer to the Output data buffer
1336 * @param OutDataLength: size in bytes of the Output buffer
1337 * @retval HAL status
1339 HAL_StatusTypeDef HAL_JPEG_Decode_IT(JPEG_HandleTypeDef *hjpeg ,uint8_t *pDataIn ,uint32_t InDataLength ,uint8_t *pDataOutMCU ,uint32_t OutDataLength)
1341 /* Check the parameters */
1342 assert_param((InDataLength >= 4));
1343 assert_param((OutDataLength >= 4));
1345 /* Check In/out buffer allocation and size */
1346 if((hjpeg == NULL) || (pDataIn == NULL) || (pDataOutMCU == NULL) || \
1347 (InDataLength == 0) || (OutDataLength == 0))
1349 return HAL_ERROR;
1352 /* Process Locked */
1353 __HAL_LOCK(hjpeg);
1355 if(hjpeg->State == HAL_JPEG_STATE_READY)
1357 /*Change JPEG state*/
1358 hjpeg->State = HAL_JPEG_STATE_BUSY_DECODING;
1360 /*Set the Context to Decode with IT*/
1361 hjpeg->Context &= ~(JPEG_CONTEXT_OPERATION_MASK | JPEG_CONTEXT_METHOD_MASK);
1362 hjpeg->Context |= (JPEG_CONTEXT_DECODE | JPEG_CONTEXT_IT);
1364 /*In/Out Data length must be multiple of 4 Bytes (1 word)*/
1365 InDataLength = InDataLength - (InDataLength % 4);
1366 OutDataLength = OutDataLength - (OutDataLength % 4);
1368 /*Store In/out buffers pointers and size*/
1369 hjpeg->pJpegInBuffPtr = pDataIn;
1370 hjpeg->pJpegOutBuffPtr = pDataOutMCU;
1371 hjpeg->InDataLength = InDataLength;
1372 hjpeg->OutDataLength = OutDataLength;
1374 /*Reset In/out data counter */
1375 hjpeg->JpegInCount = 0;
1376 hjpeg->JpegOutCount = 0;
1378 /*Init decoding process*/
1379 JPEG_Init_Process(hjpeg);
1382 else
1384 /* Process Unlocked */
1385 __HAL_UNLOCK(hjpeg);
1387 return HAL_BUSY;
1389 /* Return function status */
1390 return HAL_OK;
1394 * @brief Starts JPEG encoding with DMA processing
1395 * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
1396 * the configuration information for JPEG module
1397 * @param pDataInMCU: Pointer to the Input buffer
1398 * @param InDataLength: size in bytes Input buffer
1399 * @param pDataOut: Pointer to the jpeg output data buffer
1400 * @param OutDataLength: size in bytes of the Output buffer
1401 * @retval HAL status
1403 HAL_StatusTypeDef HAL_JPEG_Encode_DMA(JPEG_HandleTypeDef *hjpeg, uint8_t *pDataInMCU, uint32_t InDataLength, uint8_t *pDataOut, uint32_t OutDataLength)
1405 /* Check the parameters */
1406 assert_param((InDataLength >= 4));
1407 assert_param((OutDataLength >= 4));
1409 /* Check In/out buffer allocation and size */
1410 if((hjpeg == NULL) || (pDataInMCU == NULL) || (pDataOut == NULL) || \
1411 (InDataLength == 0) || (OutDataLength == 0))
1413 return HAL_ERROR;
1416 /* Process Locked */
1417 __HAL_LOCK(hjpeg);
1419 if(hjpeg->State != HAL_JPEG_STATE_READY)
1421 /* Process Unlocked */
1422 __HAL_UNLOCK(hjpeg);
1424 return HAL_BUSY;
1426 else
1428 if((hjpeg->Context & JPEG_CONTEXT_CONF_ENCODING) == JPEG_CONTEXT_CONF_ENCODING )
1430 /*Change JPEG state*/
1431 hjpeg->State = HAL_JPEG_STATE_BUSY_ENCODING;
1433 /*Set the Context to Encode with DMA*/
1434 hjpeg->Context &= ~(JPEG_CONTEXT_OPERATION_MASK | JPEG_CONTEXT_METHOD_MASK);
1435 hjpeg->Context |= (JPEG_CONTEXT_ENCODE | JPEG_CONTEXT_DMA);
1437 /*Store In/out buffers pointers and size*/
1438 hjpeg->pJpegInBuffPtr = pDataInMCU;
1439 hjpeg->pJpegOutBuffPtr = pDataOut;
1440 hjpeg->InDataLength = InDataLength;
1441 hjpeg->OutDataLength = OutDataLength;
1443 /*Reset In/out data counter */
1444 hjpeg->JpegInCount = 0;
1445 hjpeg->JpegOutCount = 0;
1447 /*Init decoding process*/
1448 JPEG_Init_Process(hjpeg);
1450 /* JPEG encoding process using DMA */
1451 JPEG_DMA_StartProcess(hjpeg);
1454 else
1456 /* Process Unlocked */
1457 __HAL_UNLOCK(hjpeg);
1459 return HAL_ERROR;
1462 /* Return function status */
1463 return HAL_OK;
1467 * @brief Starts JPEG decoding with DMA processing
1468 * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
1469 * the configuration information for JPEG module
1470 * @param pDataIn: Pointer to the input data buffer
1471 * @param InDataLength: size in bytes Input buffer
1472 * @param pDataOutMCU: Pointer to the Output data buffer
1473 * @param OutDataLength: size in bytes of the Output buffer
1474 * @retval HAL status
1476 HAL_StatusTypeDef HAL_JPEG_Decode_DMA(JPEG_HandleTypeDef *hjpeg ,uint8_t *pDataIn ,uint32_t InDataLength ,uint8_t *pDataOutMCU ,uint32_t OutDataLength)
1478 /* Check the parameters */
1479 assert_param((InDataLength >= 4));
1480 assert_param((OutDataLength >= 4));
1482 /* Check In/out buffer allocation and size */
1483 if((hjpeg == NULL) || (pDataIn == NULL) || (pDataOutMCU == NULL) || \
1484 (InDataLength == 0) || (OutDataLength == 0))
1486 return HAL_ERROR;
1489 /* Process Locked */
1490 __HAL_LOCK(hjpeg);
1492 if(hjpeg->State == HAL_JPEG_STATE_READY)
1494 /*Change JPEG state*/
1495 hjpeg->State = HAL_JPEG_STATE_BUSY_DECODING;
1497 /*Set the Context to Decode with DMA*/
1498 hjpeg->Context &= ~(JPEG_CONTEXT_OPERATION_MASK | JPEG_CONTEXT_METHOD_MASK);
1499 hjpeg->Context |= (JPEG_CONTEXT_DECODE | JPEG_CONTEXT_DMA);
1501 /*Store In/out buffers pointers and size*/
1502 hjpeg->pJpegInBuffPtr = pDataIn;
1503 hjpeg->pJpegOutBuffPtr = pDataOutMCU;
1504 hjpeg->InDataLength = InDataLength;
1505 hjpeg->OutDataLength = OutDataLength;
1507 /*Reset In/out data counter */
1508 hjpeg->JpegInCount = 0;
1509 hjpeg->JpegOutCount = 0;
1511 /*Init decoding process*/
1512 JPEG_Init_Process(hjpeg);
1514 /* JPEG decoding process using DMA */
1515 JPEG_DMA_StartProcess(hjpeg);
1518 else
1520 /* Process Unlocked */
1521 __HAL_UNLOCK(hjpeg);
1523 return HAL_BUSY;
1525 /* Return function status */
1526 return HAL_OK;
1530 * @brief Pause the JPEG Input/Output processing
1531 * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
1532 * the configuration information for JPEG module
1533 * @param XferSelection: This parameter can be one of the following values :
1534 * JPEG_PAUSE_RESUME_INPUT : Pause Input processing
1535 * JPEG_PAUSE_RESUME_OUTPUT: Pause Output processing
1536 * JPEG_PAUSE_RESUME_INPUT_OUTPUT: Pause Input and Output processing
1537 * @retval HAL status
1539 HAL_StatusTypeDef HAL_JPEG_Pause(JPEG_HandleTypeDef *hjpeg, uint32_t XferSelection)
1541 uint32_t mask = 0;
1543 assert_param(IS_JPEG_PAUSE_RESUME_STATE(XferSelection));
1545 if((hjpeg->Context & JPEG_CONTEXT_METHOD_MASK) == JPEG_CONTEXT_DMA)
1547 if((XferSelection & JPEG_PAUSE_RESUME_INPUT) == JPEG_PAUSE_RESUME_INPUT)
1549 hjpeg->Context |= JPEG_CONTEXT_PAUSE_INPUT;
1550 mask |= JPEG_DMA_IDMA;
1552 if((XferSelection & JPEG_PAUSE_RESUME_OUTPUT) == JPEG_PAUSE_RESUME_OUTPUT)
1554 hjpeg->Context |= JPEG_CONTEXT_PAUSE_OUTPUT;
1555 mask |= JPEG_DMA_ODMA;
1557 JPEG_DISABLE_DMA(hjpeg,mask);
1560 else if((hjpeg->Context & JPEG_CONTEXT_METHOD_MASK) == JPEG_CONTEXT_IT)
1563 if((XferSelection & JPEG_PAUSE_RESUME_INPUT) == JPEG_PAUSE_RESUME_INPUT)
1565 hjpeg->Context |= JPEG_CONTEXT_PAUSE_INPUT;
1566 mask |= (JPEG_IT_IFT | JPEG_IT_IFNF);
1568 if((XferSelection & JPEG_PAUSE_RESUME_OUTPUT) == JPEG_PAUSE_RESUME_OUTPUT)
1570 hjpeg->Context |= JPEG_CONTEXT_PAUSE_OUTPUT;
1571 mask |= (JPEG_IT_OFT | JPEG_IT_OFNE | JPEG_IT_EOC);
1573 __HAL_JPEG_DISABLE_IT(hjpeg,mask);
1577 /* Return function status */
1578 return HAL_OK;
1582 * @brief Resume the JPEG Input/Output processing
1583 * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
1584 * the configuration information for JPEG module
1585 * @param XferSelection: This parameter can be one of the following values :
1586 * JPEG_PAUSE_RESUME_INPUT : Resume Input processing
1587 * JPEG_PAUSE_RESUME_OUTPUT: Resume Output processing
1588 * JPEG_PAUSE_RESUME_INPUT_OUTPUT: Resume Input and Output processing
1589 * @retval HAL status
1591 HAL_StatusTypeDef HAL_JPEG_Resume(JPEG_HandleTypeDef *hjpeg, uint32_t XferSelection)
1593 uint32_t mask = 0;
1595 assert_param(IS_JPEG_PAUSE_RESUME_STATE(XferSelection));
1597 if(((hjpeg->Context & JPEG_CONTEXT_PAUSE_INPUT) == 0) && ((hjpeg->Context & JPEG_CONTEXT_PAUSE_OUTPUT) == 0))
1599 /* if nothing paused to resume return error*/
1600 return HAL_ERROR;
1603 if((hjpeg->Context & JPEG_CONTEXT_METHOD_MASK) == JPEG_CONTEXT_DMA)
1606 if((XferSelection & JPEG_PAUSE_RESUME_INPUT) == JPEG_PAUSE_RESUME_INPUT)
1608 hjpeg->Context &= (~JPEG_CONTEXT_PAUSE_INPUT);
1609 mask |= JPEG_DMA_IDMA;
1611 /*JPEG Input DMA transfer data number must be multiple of DMA buffer size
1612 as the destination is a 32 bits register */
1613 hjpeg->InDataLength = hjpeg->InDataLength - (hjpeg->InDataLength % 4);
1615 if(hjpeg->InDataLength > 0)
1617 /* Start DMA FIFO In transfer */
1618 HAL_DMA_Start_IT(hjpeg->hdmain, (uint32_t)hjpeg->pJpegInBuffPtr, (uint32_t)&hjpeg->Instance->DIR, hjpeg->InDataLength >> 2);
1622 if((XferSelection & JPEG_PAUSE_RESUME_OUTPUT) == JPEG_PAUSE_RESUME_OUTPUT)
1625 if((hjpeg->Context & JPEG_CONTEXT_ENDING_DMA) != 0)
1627 JPEG_DMA_PollResidualData(hjpeg);
1629 else
1631 hjpeg->Context &= (~JPEG_CONTEXT_PAUSE_OUTPUT);
1632 mask |= JPEG_DMA_ODMA;
1634 /* Start DMA FIFO Out transfer */
1635 HAL_DMA_Start_IT(hjpeg->hdmaout, (uint32_t)&hjpeg->Instance->DOR, (uint32_t)hjpeg->pJpegOutBuffPtr, hjpeg->OutDataLength >> 2);
1639 JPEG_ENABLE_DMA(hjpeg,mask);
1642 else if((hjpeg->Context & JPEG_CONTEXT_METHOD_MASK) == JPEG_CONTEXT_IT)
1644 if((XferSelection & JPEG_PAUSE_RESUME_INPUT) == JPEG_PAUSE_RESUME_INPUT)
1646 hjpeg->Context &= (~JPEG_CONTEXT_PAUSE_INPUT);
1647 mask |= (JPEG_IT_IFT | JPEG_IT_IFNF);
1649 if((XferSelection & JPEG_PAUSE_RESUME_OUTPUT) == JPEG_PAUSE_RESUME_OUTPUT)
1651 hjpeg->Context &= (~JPEG_CONTEXT_PAUSE_OUTPUT);
1652 mask |= (JPEG_IT_OFT | JPEG_IT_OFNE | JPEG_IT_EOC);
1654 __HAL_JPEG_ENABLE_IT(hjpeg,mask);
1658 /* Return function status */
1659 return HAL_OK;
1663 * @brief Config Encoding/Decoding Input Buffer.
1664 * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
1665 * the configuration information for JPEG module.
1666 * @param pNewInputBuffer: Pointer to the new input data buffer
1667 * @param InDataLength: Size in bytes of the new Input data buffer
1668 * @retval HAL status
1670 void HAL_JPEG_ConfigInputBuffer(JPEG_HandleTypeDef *hjpeg, uint8_t *pNewInputBuffer, uint32_t InDataLength)
1672 hjpeg->pJpegInBuffPtr = pNewInputBuffer;
1673 hjpeg->InDataLength = InDataLength;
1677 * @brief Config Encoding/Decoding Output Buffer.
1678 * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
1679 * the configuration information for JPEG module.
1680 * @param pNewOutputBuffer: Pointer to the new output data buffer
1681 * @param OutDataLength: Size in bytes of the new Output data buffer
1682 * @retval HAL status
1684 void HAL_JPEG_ConfigOutputBuffer(JPEG_HandleTypeDef *hjpeg, uint8_t *pNewOutputBuffer, uint32_t OutDataLength)
1686 hjpeg->pJpegOutBuffPtr = pNewOutputBuffer;
1687 hjpeg->OutDataLength = OutDataLength;
1691 * @brief Aborts the JPEG Encoding/Decoding.
1692 * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
1693 * the configuration information for JPEG module
1694 * @retval HAL status
1696 HAL_StatusTypeDef HAL_JPEG_Abort(JPEG_HandleTypeDef *hjpeg)
1698 uint32_t tickstart, tmpContext;
1700 tmpContext = hjpeg->Context;
1702 /*Reset the Context operation and method*/
1703 hjpeg->Context &= ~(JPEG_CONTEXT_OPERATION_MASK | JPEG_CONTEXT_METHOD_MASK | JPEG_CONTEXT_ENDING_DMA);
1705 if((tmpContext & JPEG_CONTEXT_METHOD_MASK) == JPEG_CONTEXT_DMA)
1707 /* Stop the DMA In/out Xfer*/
1708 HAL_DMA_Abort(hjpeg->hdmaout);
1709 HAL_DMA_Abort(hjpeg->hdmain);
1712 /* Stop the JPEG encoding/decoding process*/
1713 hjpeg->Instance->CONFR0 &= ~JPEG_CONFR0_START;
1715 /* Get tick */
1716 tickstart = HAL_GetTick();
1718 /* Check if the JPEG Codec is effectively disabled */
1719 while(__HAL_JPEG_GET_FLAG(hjpeg, JPEG_FLAG_COF) != RESET)
1721 /* Check for the Timeout */
1722 if((HAL_GetTick() - tickstart ) > JPEG_TIMEOUT_VALUE)
1724 /* Update error code */
1725 hjpeg->ErrorCode |= HAL_JPEG_ERROR_TIMEOUT;
1727 /* Change the DMA state */
1728 hjpeg->State = HAL_JPEG_STATE_TIMEOUT;
1730 /* Process Unlocked */
1731 __HAL_UNLOCK(hjpeg);
1733 return HAL_TIMEOUT;
1737 /* Disable All Interrupts */
1738 __HAL_JPEG_DISABLE_IT(hjpeg,JPEG_INTERRUPT_MASK);
1740 /* Disable All DMA requests */
1741 JPEG_DISABLE_DMA(hjpeg,JPEG_DMA_MASK);
1743 /* Flush input and output FIFOs*/
1744 hjpeg->Instance->CR |= JPEG_CR_IFF;
1745 hjpeg->Instance->CR |= JPEG_CR_OFF;
1747 /* Clear all flags */
1748 __HAL_JPEG_CLEAR_FLAG(hjpeg,JPEG_FLAG_ALL);
1750 /* Reset JpegInCount and JpegOutCount */
1751 hjpeg->JpegInCount = 0;
1752 hjpeg->JpegOutCount = 0;
1754 /*Reset the Context Pause*/
1755 hjpeg->Context &= ~(JPEG_CONTEXT_PAUSE_INPUT | JPEG_CONTEXT_PAUSE_OUTPUT);
1757 /* Change the DMA state*/
1758 hjpeg->State = HAL_JPEG_STATE_READY;
1760 /* Process Unlocked */
1761 __HAL_UNLOCK(hjpeg);
1763 /* Return function status */
1764 return HAL_OK;
1769 * @}
1772 /** @defgroup JPEG_Exported_Functions_Group4 JPEG Decode/Encode callback functions
1773 * @brief JPEG process callback functions.
1775 @verbatim
1776 ==============================================================================
1777 ##### JPEG Decode and Encode callback functions #####
1778 ==============================================================================
1779 [..] This section provides callback functions:
1780 (+) HAL_JPEG_InfoReadyCallback() : Decoding JPEG Info ready callback
1781 (+) HAL_JPEG_EncodeCpltCallback() : Encoding complete callback.
1782 (+) HAL_JPEG_DecodeCpltCallback() : Decoding complete callback.
1783 (+) HAL_JPEG_ErrorCallback() : JPEG error callback.
1784 (+) HAL_JPEG_GetDataCallback() : Get New Data chunk callback.
1785 (+) HAL_JPEG_DataReadyCallback() : Decoded/Encoded Data ready callback.
1787 @endverbatim
1788 * @{
1792 * @brief Decoding JPEG Info ready callback.
1793 * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
1794 * the configuration information for JPEG module
1795 * @param pInfo: pointer to a JPEG_ConfTypeDef structure that contains
1796 * The JPEG decoded header informations
1797 * @retval None
1799 __weak void HAL_JPEG_InfoReadyCallback(JPEG_HandleTypeDef *hjpeg,JPEG_ConfTypeDef *pInfo)
1801 /* Prevent unused argument(s) compilation warning */
1802 UNUSED(hjpeg);
1803 UNUSED(pInfo);
1805 /* NOTE : This function Should not be modified, when the callback is needed,
1806 the HAL_JPEG_HeaderParsingCpltCallback could be implemented in the user file
1811 * @brief Encoding complete callback.
1812 * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
1813 * the configuration information for JPEG module
1814 * @retval None
1816 __weak void HAL_JPEG_EncodeCpltCallback(JPEG_HandleTypeDef *hjpeg)
1818 /* Prevent unused argument(s) compilation warning */
1819 UNUSED(hjpeg);
1821 /* NOTE : This function Should not be modified, when the callback is needed,
1822 the HAL_JPEG_EncodeCpltCallback could be implemented in the user file
1827 * @brief Decoding complete callback.
1828 * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
1829 * the configuration information for JPEG module
1830 * @retval None
1832 __weak void HAL_JPEG_DecodeCpltCallback(JPEG_HandleTypeDef *hjpeg)
1834 /* Prevent unused argument(s) compilation warning */
1835 UNUSED(hjpeg);
1837 /* NOTE : This function Should not be modified, when the callback is needed,
1838 the HAL_JPEG_EncodeCpltCallback could be implemented in the user file
1843 * @brief JPEG error callback.
1844 * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
1845 * the configuration information for JPEG module
1846 * @retval None
1848 __weak void HAL_JPEG_ErrorCallback(JPEG_HandleTypeDef *hjpeg)
1850 /* Prevent unused argument(s) compilation warning */
1851 UNUSED(hjpeg);
1853 /* NOTE : This function Should not be modified, when the callback is needed,
1854 the HAL_JPEG_ErrorCallback could be implemented in the user file
1859 * @brief Get New Data chunk callback.
1860 * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
1861 * the configuration information for JPEG module
1862 * @param NbDecodedData: Number of consummed data in the previous chunk in bytes
1863 * @retval None
1865 __weak void HAL_JPEG_GetDataCallback(JPEG_HandleTypeDef *hjpeg, uint32_t NbDecodedData)
1867 /* Prevent unused argument(s) compilation warning */
1868 UNUSED(hjpeg);
1869 UNUSED(NbDecodedData);
1871 /* NOTE : This function Should not be modified, when the callback is needed,
1872 the HAL_JPEG_GetDataCallback could be implemented in the user file
1877 * @brief Decoded/Encoded Data ready callback.
1878 * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
1879 * the configuration information for JPEG module
1880 * @param pDataOut: pointer to the output data buffer
1881 * @param OutDataLength: number in bytes of data available in the specified output buffer
1882 * @retval None
1884 __weak void HAL_JPEG_DataReadyCallback (JPEG_HandleTypeDef *hjpeg, uint8_t *pDataOut, uint32_t OutDataLength)
1886 /* Prevent unused argument(s) compilation warning */
1887 UNUSED(hjpeg);
1888 UNUSED(pDataOut);
1889 UNUSED(OutDataLength);
1891 /* NOTE : This function Should not be modified, when the callback is needed,
1892 the HAL_JPEG_DataReadyCallback could be implemented in the user file
1897 * @}
1901 /** @defgroup JPEG_Exported_Functions_Group5 JPEG IRQ handler management
1902 * @brief JPEG IRQ handler.
1904 @verbatim
1905 ==============================================================================
1906 ##### JPEG IRQ handler management #####
1907 ==============================================================================
1908 [..] This section provides JPEG IRQ handler function.
1909 (+) HAL_JPEG_IRQHandler() : handles JPEG interrupt request
1911 @endverbatim
1912 * @{
1916 * @brief This function handles JPEG interrupt request.
1917 * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
1918 * the configuration information for JPEG module
1919 * @retval None
1921 void HAL_JPEG_IRQHandler(JPEG_HandleTypeDef *hjpeg)
1923 switch(hjpeg->State)
1925 case HAL_JPEG_STATE_BUSY_ENCODING:
1926 case HAL_JPEG_STATE_BUSY_DECODING:
1927 /* continue JPEG data encoding/Decoding*/
1928 /* JPEG data processing : In/Out FIFO transfer*/
1929 if((hjpeg->Context & JPEG_CONTEXT_METHOD_MASK) == JPEG_CONTEXT_IT)
1931 JPEG_Process(hjpeg);
1933 else if((hjpeg->Context & JPEG_CONTEXT_METHOD_MASK) == JPEG_CONTEXT_DMA)
1935 JPEG_DMA_ContinueProcess(hjpeg);
1939 break;
1941 default:
1942 break;
1947 * @}
1950 /** @defgroup JPEG_Exported_Functions_Group6 Peripheral State functions
1951 * @brief Peripheral State functions.
1953 @verbatim
1954 ==============================================================================
1955 ##### Peripheral State and Error functions #####
1956 ==============================================================================
1957 [..] This section provides JPEG State and Errors function.
1958 (+) HAL_JPEG_GetState() : permits to get in run-time the JPEG state.
1959 (+) HAL_JPEG_GetError() : Returns the JPEG error code if any.
1961 @endverbatim
1962 * @{
1966 * @brief Returns the JPEG state.
1967 * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
1968 * the configuration information for JPEG module
1969 * @retval JPEG state
1971 HAL_JPEG_STATETypeDef HAL_JPEG_GetState(JPEG_HandleTypeDef *hjpeg)
1973 return hjpeg->State;
1977 * @brief Return the JPEG error code
1978 * @param hjpeg : pointer to a JPEG_HandleTypeDef structure that contains
1979 * the configuration information for the specified JPEG.
1980 * @retval JPEG Error Code
1982 uint32_t HAL_JPEG_GetError(JPEG_HandleTypeDef *hjpeg)
1984 return hjpeg->ErrorCode;
1988 * @}
1992 * @}
1996 /** @addtogroup JPEG_Private_Functions
1997 * @{
2001 * @brief Generates Huffman sizes/Codes Table from Bits/vals Table
2002 * @param Bits: pointer to bits table
2003 * @param Huffsize: pointer to sizes table
2004 * @param Huffcode: pointer to codes table
2005 * @param LastK: pointer to last Coeff (table dimmension)
2006 * @retval HAL status
2008 static HAL_StatusTypeDef JPEG_Bits_To_SizeCodes(uint8_t *Bits, uint8_t *Huffsize, uint32_t *Huffcode, uint32_t *LastK)
2010 uint32_t i, p, l, code, si;
2012 /* Figure C.1: Generation of table of Huffman code sizes */
2013 p = 0;
2014 for (l = 0; l < 16; l++)
2016 i = (uint32_t)Bits[l];
2017 if ( (p + i) > 256)
2018 { /* check for table overflow */
2019 return HAL_ERROR;
2021 while (i != 0)
2023 Huffsize[p++] = (uint8_t) l+1;
2024 i--;
2027 Huffsize[p] = 0;
2028 *LastK = p;
2030 /* Figure C.2: Generation of table of Huffman codes */
2031 code = 0;
2032 si = Huffsize[0];
2033 p = 0;
2034 while (Huffsize[p] != 0)
2036 while (((uint32_t) Huffsize[p]) == si)
2038 Huffcode[p++] = code;
2039 code++;
2041 /* code must fit in "size" bits (si), no code is allowed to be all ones*/
2042 if (((uint32_t) code) >= (((uint32_t) 1) << si))
2044 return HAL_ERROR;
2046 code <<= 1;
2047 si++;
2049 /* Return function status */
2050 return HAL_OK;
2054 * @brief Transform a Bits/Vals AC Huffman table to sizes/Codes huffman Table
2055 * that can programmed to the JPEG encoder registers
2056 * @param AC_BitsValsTable: pointer to AC huffman bits/vals table
2057 * @param AC_SizeCodesTable: pointer to AC huffman Sizes/Codes table
2058 * @retval HAL status
2060 static HAL_StatusTypeDef JPEG_ACHuff_BitsVals_To_SizeCodes(JPEG_ACHuffTableTypeDef *AC_BitsValsTable, JPEG_AC_HuffCodeTableTypeDef *AC_SizeCodesTable)
2062 HAL_StatusTypeDef error;
2063 uint8_t huffsize[257];
2064 uint32_t huffcode[257];
2065 uint32_t k;
2066 uint32_t l,lsb, msb;
2067 uint32_t lastK;
2069 error = JPEG_Bits_To_SizeCodes(AC_BitsValsTable->Bits, huffsize, huffcode, &lastK);
2070 if(error != HAL_OK)
2072 return error;
2075 /* Figure C.3: Ordering procedure for encoding procedure code tables */
2076 k=0;
2078 while(k < lastK)
2080 l = AC_BitsValsTable->HuffVal[k];
2081 if(l == 0)
2083 l = 160; /*l = 0x00 EOB code*/
2085 else if(l == 0xF0)/* l = 0xF0 ZRL code*/
2087 l = 161;
2089 else
2091 msb = (l & 0xF0) >> 4;
2092 lsb = (l & 0x0F);
2093 l = (msb * 10) + lsb - 1;
2095 if(l >= JPEG_AC_HUFF_TABLE_SIZE)
2097 return HAL_ERROR; /* Huffman Table overflow error*/
2099 else
2101 AC_SizeCodesTable->HuffmanCode[l] = huffcode[k];
2102 AC_SizeCodesTable->CodeLength[l] = huffsize[k] - 1;
2103 k++;
2107 /* Return function status */
2108 return HAL_OK;
2112 * @brief Transform a Bits/Vals DC Huffman table to sizes/Codes huffman Table
2113 * that can programmed to the JPEG encoder registers
2114 * @param DC_BitsValsTable: pointer to DC huffman bits/vals table
2115 * @param DC_SizeCodesTable: pointer to DC huffman Sizes/Codes table
2116 * @retval HAL status
2118 static HAL_StatusTypeDef JPEG_DCHuff_BitsVals_To_SizeCodes(JPEG_DCHuffTableTypeDef *DC_BitsValsTable, JPEG_DC_HuffCodeTableTypeDef *DC_SizeCodesTable)
2120 HAL_StatusTypeDef error;
2122 uint32_t k;
2123 uint32_t l;
2124 uint32_t lastK;
2125 uint8_t huffsize[257];
2126 uint32_t huffcode[257];
2127 error = JPEG_Bits_To_SizeCodes(DC_BitsValsTable->Bits, huffsize, huffcode, &lastK);
2128 if(error != HAL_OK)
2130 return error;
2132 /* Figure C.3: ordering procedure for encoding procedure code tables */
2133 k=0;
2135 while(k < lastK)
2137 l = DC_BitsValsTable->HuffVal[k];
2138 if(l >= JPEG_DC_HUFF_TABLE_SIZE)
2140 return HAL_ERROR; /* Huffman Table overflow error*/
2142 else
2144 DC_SizeCodesTable->HuffmanCode[l] = huffcode[k];
2145 DC_SizeCodesTable->CodeLength[l] = huffsize[k] - 1;
2146 k++;
2150 /* Return function status */
2151 return HAL_OK;
2155 * @brief Set the JPEG register with an DC huffman table at the given DC table address
2156 * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
2157 * the configuration information for JPEG module
2158 * @param HuffTableDC: pointer to DC huffman table
2159 * @param DCTableAddress: Encoder DC huffman table address it could be HUFFENC_DC0 or HUFFENC_DC1.
2160 * @retval HAL status
2162 static HAL_StatusTypeDef JPEG_Set_HuffDC_Mem(JPEG_HandleTypeDef *hjpeg, JPEG_DCHuffTableTypeDef *HuffTableDC, uint32_t *DCTableAddress)
2164 HAL_StatusTypeDef error = HAL_OK;
2165 JPEG_DC_HuffCodeTableTypeDef dcSizeCodesTable;
2166 uint32_t i, lsb, msb;
2167 __IO uint32_t *address, *addressDef;
2169 if(DCTableAddress == (uint32_t *)(hjpeg->Instance->HUFFENC_DC0))
2171 address = (hjpeg->Instance->HUFFENC_DC0 + (JPEG_DC_HUFF_TABLE_SIZE/2));
2173 else if (DCTableAddress == (uint32_t *)(hjpeg->Instance->HUFFENC_DC1))
2175 address = (hjpeg->Instance->HUFFENC_DC1 + (JPEG_DC_HUFF_TABLE_SIZE/2));
2177 else
2179 return HAL_ERROR;
2182 if(HuffTableDC != NULL)
2184 error = JPEG_DCHuff_BitsVals_To_SizeCodes(HuffTableDC, &dcSizeCodesTable);
2185 if(error != HAL_OK)
2187 return error;
2189 addressDef = address;
2190 *addressDef = 0x0FFF0FFF;
2191 addressDef++;
2192 *addressDef = 0x0FFF0FFF;
2194 i = JPEG_DC_HUFF_TABLE_SIZE;
2195 while(i>0)
2197 i--;
2198 address --;
2199 msb = ((uint32_t)(((uint32_t)dcSizeCodesTable.CodeLength[i] & 0xF) << 8 )) | ((uint32_t)dcSizeCodesTable.HuffmanCode[i] & 0xFF);
2200 i--;
2201 lsb = ((uint32_t)(((uint32_t)dcSizeCodesTable.CodeLength[i] & 0xF) << 8 )) | ((uint32_t)dcSizeCodesTable.HuffmanCode[i] & 0xFF);
2203 *address = lsb | (msb << 16);
2207 /* Return function status */
2208 return HAL_OK;
2212 * @brief Set the JPEG register with an AC huffman table at the given AC table address
2213 * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
2214 * the configuration information for JPEG module
2215 * @param HuffTableAC: pointer to AC huffman table
2216 * @param ACTableAddress: Encoder AC huffman table address it could be HUFFENC_AC0 or HUFFENC_AC1.
2217 * @retval HAL status
2219 static HAL_StatusTypeDef JPEG_Set_HuffAC_Mem(JPEG_HandleTypeDef *hjpeg, JPEG_ACHuffTableTypeDef *HuffTableAC, uint32_t *ACTableAddress)
2221 HAL_StatusTypeDef error = HAL_OK;
2222 JPEG_AC_HuffCodeTableTypeDef acSizeCodesTable;
2223 uint32_t i, lsb, msb;
2224 __IO uint32_t *address, *addressDef;
2226 if(ACTableAddress == (uint32_t *)(hjpeg->Instance->HUFFENC_AC0))
2228 address = (hjpeg->Instance->HUFFENC_AC0 + (JPEG_AC_HUFF_TABLE_SIZE/2));
2230 else if (ACTableAddress == (uint32_t *)(hjpeg->Instance->HUFFENC_AC1))
2232 address = (hjpeg->Instance->HUFFENC_AC1 + (JPEG_AC_HUFF_TABLE_SIZE/2));
2234 else
2236 return HAL_ERROR;
2239 if(HuffTableAC != NULL)
2241 error = JPEG_ACHuff_BitsVals_To_SizeCodes(HuffTableAC, &acSizeCodesTable);
2242 if(error != HAL_OK)
2244 return error;
2246 /* Default values settings: 162:167 FFFh , 168:175 FD0h_FD7h */
2247 /* Locations 162:175 of each AC table contain information used internally by the core */
2249 addressDef = address;
2250 for(i=0; i<3; i++)
2252 *addressDef = 0x0FFF0FFF;
2253 addressDef++;
2255 *addressDef = 0x0FD10FD0;
2256 addressDef++;
2257 *addressDef = 0x0FD30FD2;
2258 addressDef++;
2259 *addressDef = 0x0FD50FD4;
2260 addressDef++;
2261 *addressDef = 0x0FD70FD6;
2262 /* end of Locations 162:175 */
2265 i = JPEG_AC_HUFF_TABLE_SIZE;
2266 while (i > 0)
2268 i--;
2269 address--;
2270 msb = ((uint32_t)(((uint32_t)acSizeCodesTable.CodeLength[i] & 0xF) << 8 )) | ((uint32_t)acSizeCodesTable.HuffmanCode[i] & 0xFF);
2271 i--;
2272 lsb = ((uint32_t)(((uint32_t)acSizeCodesTable.CodeLength[i] & 0xF) << 8 )) | ((uint32_t)acSizeCodesTable.HuffmanCode[i] & 0xFF);
2274 *address = lsb | (msb << 16);
2278 /* Return function status */
2279 return HAL_OK;
2283 * @brief Configure the JPEG encoder register huffman tables to used during
2284 * the encdoing operation
2285 * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
2286 * the configuration information for JPEG module
2287 * @param HuffTableAC0: AC0 huffman table
2288 * @param HuffTableDC0: DC0 huffman table
2289 * @param HuffTableAC1: AC1 huffman table
2290 * @param HuffTableDC1: DC1 huffman table
2291 * @retval None
2293 static HAL_StatusTypeDef JPEG_Set_HuffEnc_Mem(JPEG_HandleTypeDef *hjpeg, JPEG_ACHuffTableTypeDef *HuffTableAC0, JPEG_DCHuffTableTypeDef *HuffTableDC0 , JPEG_ACHuffTableTypeDef *HuffTableAC1, JPEG_DCHuffTableTypeDef *HuffTableDC1)
2295 HAL_StatusTypeDef error = HAL_OK;
2297 JPEG_Set_Huff_DHTMem(hjpeg, HuffTableAC0, HuffTableDC0, HuffTableAC1, HuffTableDC1);
2299 if(HuffTableAC0 != NULL)
2301 error = JPEG_Set_HuffAC_Mem(hjpeg, HuffTableAC0, (uint32_t *)(hjpeg->Instance->HUFFENC_AC0));
2302 if(error != HAL_OK)
2304 return error;
2308 if(HuffTableAC1 != NULL)
2310 error = JPEG_Set_HuffAC_Mem(hjpeg, HuffTableAC1, (uint32_t *)(hjpeg->Instance->HUFFENC_AC1));
2311 if(error != HAL_OK)
2313 return error;
2317 if(HuffTableDC0 != NULL)
2319 error = JPEG_Set_HuffDC_Mem(hjpeg, HuffTableDC0, (uint32_t *)hjpeg->Instance->HUFFENC_DC0);
2320 if(error != HAL_OK)
2322 return error;
2326 if(HuffTableDC1 != NULL)
2328 error = JPEG_Set_HuffDC_Mem(hjpeg, HuffTableDC1, (uint32_t *)hjpeg->Instance->HUFFENC_DC1);
2329 if(error != HAL_OK)
2331 return error;
2334 /* Return function status */
2335 return HAL_OK;
2339 * @brief Configure the JPEG register huffman tables to be included in the JPEG
2340 * file header (used for encoding only)
2341 * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
2342 * the configuration information for JPEG module
2343 * @param HuffTableAC0: AC0 huffman table
2344 * @param HuffTableDC0: DC0 huffman table
2345 * @param HuffTableAC1: AC1 huffman table
2346 * @param HuffTableDC1: DC1 huffman table
2347 * @retval None
2349 static void JPEG_Set_Huff_DHTMem(JPEG_HandleTypeDef *hjpeg, JPEG_ACHuffTableTypeDef *HuffTableAC0, JPEG_DCHuffTableTypeDef *HuffTableDC0 , JPEG_ACHuffTableTypeDef *HuffTableAC1, JPEG_DCHuffTableTypeDef *HuffTableDC1)
2351 uint32_t value, index;
2352 __IO uint32_t *address;
2353 if(HuffTableDC0 != NULL)
2355 /* DC0 Huffman Table : BITS*/
2356 /* DC0 BITS is a 16 Bytes table i.e 4x32bits words from DHTMEM base address to DHTMEM + 3*/
2357 address = (hjpeg->Instance->DHTMEM + 3);
2358 index = 16;
2359 while(index > 0)
2362 *address = (((uint32_t)HuffTableDC0->Bits[index-1] & 0xFF) << 24)|
2363 (((uint32_t)HuffTableDC0->Bits[index-2] & 0xFF) << 16)|
2364 (((uint32_t)HuffTableDC0->Bits[index-3] & 0xFF) << 8) |
2365 ((uint32_t)HuffTableDC0->Bits[index-4] & 0xFF);
2366 address--;
2367 index -=4;
2370 /* DC0 Huffman Table : Val*/
2371 /* DC0 VALS is a 12 Bytes table i.e 3x32bits words from DHTMEM base address +4 to DHTMEM + 6 */
2372 address = (hjpeg->Instance->DHTMEM + 6);
2373 index = 12;
2374 while(index > 0)
2376 *address = (((uint32_t)HuffTableDC0->HuffVal[index-1] & 0xFF) << 24)|
2377 (((uint32_t)HuffTableDC0->HuffVal[index-2] & 0xFF) << 16)|
2378 (((uint32_t)HuffTableDC0->HuffVal[index-3] & 0xFF) << 8) |
2379 ((uint32_t)HuffTableDC0->HuffVal[index-4] & 0xFF);
2380 address--;
2381 index -=4;
2385 if(HuffTableAC0 != NULL)
2387 /* AC0 Huffman Table : BITS*/
2388 /* AC0 BITS is a 16 Bytes table i.e 4x32bits words from DHTMEM base address + 7 to DHTMEM + 10*/
2389 address = (hjpeg->Instance->DHTMEM + 10);
2390 index = 16;
2391 while(index > 0)
2394 *address = (((uint32_t)HuffTableAC0->Bits[index-1] & 0xFF) << 24)|
2395 (((uint32_t)HuffTableAC0->Bits[index-2] & 0xFF) << 16)|
2396 (((uint32_t)HuffTableAC0->Bits[index-3] & 0xFF) << 8) |
2397 ((uint32_t)HuffTableAC0->Bits[index-4] & 0xFF);
2398 address--;
2399 index -=4;
2402 /* AC0 Huffman Table : Val*/
2403 /* AC0 VALS is a 162 Bytes table i.e 41x32bits words from DHTMEM base address + 11 to DHTMEM + 51 */
2404 /* only Byte 0 and Byte 1 of the last word (@ DHTMEM + 51) belong to AC0 VALS table */
2405 address = (hjpeg->Instance->DHTMEM + 51);
2406 value = *address & 0xFFFF0000U;
2407 value = value | (((uint32_t)HuffTableAC0->HuffVal[161] & 0xFF) << 8) | ((uint32_t)HuffTableAC0->HuffVal[160] & 0xFF);
2408 *address = value;
2410 /*continue setting 160 AC0 huffman values */
2411 address--; /* address = hjpeg->Instance->DHTMEM + 50*/
2412 index = 160;
2413 while(index > 0)
2415 *address = (((uint32_t)HuffTableAC0->HuffVal[index-1] & 0xFF) << 24)|
2416 (((uint32_t)HuffTableAC0->HuffVal[index-2] & 0xFF) << 16)|
2417 (((uint32_t)HuffTableAC0->HuffVal[index-3] & 0xFF) << 8) |
2418 ((uint32_t)HuffTableAC0->HuffVal[index-4] & 0xFF);
2419 address--;
2420 index -=4;
2424 if(HuffTableDC1 != NULL)
2426 /* DC1 Huffman Table : BITS*/
2427 /* DC1 BITS is a 16 Bytes table i.e 4x32bits words from DHTMEM + 51 base address to DHTMEM + 55*/
2428 /* only Byte 2 and Byte 3 of the first word (@ DHTMEM + 51) belong to DC1 Bits table */
2429 address = (hjpeg->Instance->DHTMEM + 51);
2430 value = *address & 0x0000FFFFU;
2431 value = value | (((uint32_t)HuffTableDC1->Bits[1] & 0xFF) << 24) | (((uint32_t)HuffTableDC1->Bits[0] & 0xFF) << 16);
2432 *address = value;
2434 /* only Byte 0 and Byte 1 of the last word (@ DHTMEM + 55) belong to DC1 Bits table */
2435 address = (hjpeg->Instance->DHTMEM + 55);
2436 value = *address & 0xFFFF0000U;
2437 value = value | (((uint32_t)HuffTableDC1->Bits[15] & 0xFF) << 8) | ((uint32_t)HuffTableDC1->Bits[14] & 0xFF);
2438 *address = value;
2440 /*continue setting 12 DC1 huffman Bits from DHTMEM + 54 down to DHTMEM + 52*/
2441 address--;
2442 index = 12;
2443 while(index > 0)
2446 *address = (((uint32_t)HuffTableDC1->Bits[index+1] & 0xFF) << 24)|
2447 (((uint32_t)HuffTableDC1->Bits[index] & 0xFF) << 16)|
2448 (((uint32_t)HuffTableDC1->Bits[index-1] & 0xFF) << 8) |
2449 ((uint32_t)HuffTableDC1->Bits[index-2] & 0xFF);
2450 address--;
2451 index -=4;
2454 /* DC1 Huffman Table : Val*/
2455 /* DC1 VALS is a 12 Bytes table i.e 3x32bits words from DHTMEM base address +55 to DHTMEM + 58 */
2456 /* only Byte 2 and Byte 3 of the first word (@ DHTMEM + 55) belong to DC1 Val table */
2457 address = (hjpeg->Instance->DHTMEM + 55);
2458 value = *address & 0x0000FFFF;
2459 value = value | (((uint32_t)HuffTableDC1->HuffVal[1] & 0xFF) << 24) | (((uint32_t)HuffTableDC1->HuffVal[0] & 0xFF) << 16);
2460 *address = value;
2462 /* only Byte 0 and Byte 1 of the last word (@ DHTMEM + 58) belong to DC1 Val table */
2463 address = (hjpeg->Instance->DHTMEM + 58);
2464 value = *address & 0xFFFF0000U;
2465 value = value | (((uint32_t)HuffTableDC1->HuffVal[11] & 0xFF) << 8) | ((uint32_t)HuffTableDC1->HuffVal[10] & 0xFF);
2466 *address = value;
2468 /*continue setting 8 DC1 huffman val from DHTMEM + 57 down to DHTMEM + 56*/
2469 address--;
2470 index = 8;
2471 while(index > 0)
2473 *address = (((uint32_t)HuffTableDC1->HuffVal[index+1] & 0xFF) << 24)|
2474 (((uint32_t)HuffTableDC1->HuffVal[index] & 0xFF) << 16)|
2475 (((uint32_t)HuffTableDC1->HuffVal[index-1] & 0xFF) << 8) |
2476 ((uint32_t)HuffTableDC1->HuffVal[index-2] & 0xFF);
2477 address--;
2478 index -=4;
2482 if(HuffTableAC1 != NULL)
2484 /* AC1 Huffman Table : BITS*/
2485 /* AC1 BITS is a 16 Bytes table i.e 4x32bits words from DHTMEM base address + 58 to DHTMEM + 62*/
2486 /* only Byte 2 and Byte 3 of the first word (@ DHTMEM + 58) belong to AC1 Bits table */
2487 address = (hjpeg->Instance->DHTMEM + 58);
2488 value = *address & 0x0000FFFFU;
2489 value = value | (((uint32_t)HuffTableAC1->Bits[1] & 0xFF) << 24) | (((uint32_t)HuffTableAC1->Bits[0] & 0xFF) << 16);
2490 *address = value;
2492 /* only Byte 0 and Byte 1 of the last word (@ DHTMEM + 62) belong to Bits Val table */
2493 address = (hjpeg->Instance->DHTMEM + 62);
2494 value = *address & 0xFFFF0000U;
2495 value = value | (((uint32_t)HuffTableAC1->Bits[15] & 0xFF) << 8) | ((uint32_t)HuffTableAC1->Bits[14] & 0xFF);
2496 *address = value;
2498 /*continue setting 12 AC1 huffman Bits from DHTMEM + 61 down to DHTMEM + 59*/
2499 address--;
2500 index = 12;
2501 while(index > 0)
2504 *address = (((uint32_t)HuffTableAC1->Bits[index+1] & 0xFF) << 24)|
2505 (((uint32_t)HuffTableAC1->Bits[index] & 0xFF) << 16)|
2506 (((uint32_t)HuffTableAC1->Bits[index-1] & 0xFF) << 8) |
2507 ((uint32_t)HuffTableAC1->Bits[index-2] & 0xFF);
2508 address--;
2509 index -=4;
2512 /* AC1 Huffman Table : Val*/
2513 /* AC1 VALS is a 162 Bytes table i.e 41x32bits words from DHTMEM base address + 62 to DHTMEM + 102 */
2514 /* only Byte 2 and Byte 3 of the first word (@ DHTMEM + 62) belong to AC1 VALS table */
2515 address = (hjpeg->Instance->DHTMEM + 62);
2516 value = *address & 0x0000FFFF;
2517 value = value | (((uint32_t)HuffTableAC1->HuffVal[1] & 0xFF) << 24) | (((uint32_t)HuffTableAC1->HuffVal[0] & 0xFF) << 16);
2518 *address = value;
2520 /*continue setting 160 AC1 huffman values from DHTMEM + 63 to DHTMEM+102 */
2521 address = (hjpeg->Instance->DHTMEM + 102);
2522 index = 160;
2523 while(index > 0)
2525 *address = (((uint32_t)HuffTableAC1->HuffVal[index+1] & 0xFF) << 24)|
2526 (((uint32_t)HuffTableAC1->HuffVal[index] & 0xFF) << 16)|
2527 (((uint32_t)HuffTableAC1->HuffVal[index-1] & 0xFF) << 8) |
2528 ((uint32_t)HuffTableAC1->HuffVal[index-2] & 0xFF);
2529 address--;
2530 index -=4;
2536 * @brief Configure the JPEG registers with a given quantization table
2537 * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
2538 * the configuration information for JPEG module
2539 * @param QTable: pointer to an array of 64 bytes giving the quantization table
2540 * @param QTableAddress: destination quantization address in the JPEG peripheral
2541 * it could be QMEM0, QMEM1, QMEM2 or QMEM3
2542 * @retval None
2544 static HAL_StatusTypeDef JPEG_Set_Quantization_Mem(JPEG_HandleTypeDef *hjpeg, uint8_t *QTable, uint32_t *QTableAddress)
2546 uint32_t i, j, *tableAddress, quantRow, quantVal, ScaleFactor;
2548 if((QTableAddress == ((uint32_t *)(hjpeg->Instance->QMEM0))) ||
2549 (QTableAddress == ((uint32_t *)(hjpeg->Instance->QMEM1))) ||
2550 (QTableAddress == ((uint32_t *)(hjpeg->Instance->QMEM2))) ||
2551 (QTableAddress == ((uint32_t *)(hjpeg->Instance->QMEM3))))
2553 tableAddress = QTableAddress;
2555 else
2557 return HAL_ERROR;
2560 if ((hjpeg->Conf.ImageQuality >= 50) && (hjpeg->Conf.ImageQuality <= 100))
2562 ScaleFactor = 200 - (hjpeg->Conf.ImageQuality * 2);
2564 else if (hjpeg->Conf.ImageQuality > 0)
2566 ScaleFactor = ((uint32_t) 5000) / ((uint32_t) hjpeg->Conf.ImageQuality);
2568 else
2570 return HAL_ERROR;
2573 /*Quantization_table = (Standard_quanization_table * ScaleFactor + 50) / 100*/
2574 i = 0;
2575 while( i < JPEG_QUANT_TABLE_SIZE)
2577 quantRow = 0;
2578 for(j=0; j<4; j++)
2580 /* Note that the quantization coefficients must be specified in the table in zigzag order */
2581 quantVal = ((((uint32_t) QTable[JPEG_ZIGZAG_ORDER[i+j]]) * ScaleFactor) + 50) / 100;
2583 if(quantVal == 0)
2585 quantVal = 1;
2587 else if (quantVal > 255)
2589 quantVal = 255;
2592 quantRow |= ((quantVal & 0xFF) << (8 * j));
2595 i += 4;
2596 *tableAddress = quantRow;
2597 tableAddress ++;
2600 /* Return function status */
2601 return HAL_OK;
2605 * @brief Configure the JPEG registers for YCbCr color space
2606 * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
2607 * the configuration information for JPEG module
2608 * @retval None
2610 static void JPEG_SetColorYCBCR(JPEG_HandleTypeDef *hjpeg)
2612 uint32_t ySamplingH;
2613 uint32_t ySamplingV;
2614 uint32_t yblockNb;
2616 /*Set Number of color components to 3*/
2617 hjpeg->Instance->CONFR1 &= ~JPEG_CONFR1_NF;
2618 hjpeg->Instance->CONFR1 |= JPEG_CONFR1_NF_1;
2620 /* compute MCU block size and Y, Cb ,Cr sampling factors*/
2621 if(hjpeg->Conf.ChromaSubsampling == JPEG_420_SUBSAMPLING)
2623 ySamplingH = JPEG_CONFR4_HSF_1; /* Hs = 2*/
2624 ySamplingV = JPEG_CONFR4_VSF_1; /* Vs = 2*/
2626 yblockNb = 0x30; /* 4 blocks of 8x8*/
2628 else if(hjpeg->Conf.ChromaSubsampling == JPEG_422_SUBSAMPLING)
2630 ySamplingH = JPEG_CONFR4_HSF_1; /* Hs = 2*/
2631 ySamplingV = JPEG_CONFR4_VSF_0; /* Vs = 1*/
2633 yblockNb = 0x10; /* 2 blocks of 8x8*/
2635 else /*JPEG_444_SUBSAMPLING and default*/
2637 ySamplingH = JPEG_CONFR4_HSF_0; /* Hs = 1*/
2638 ySamplingV = JPEG_CONFR4_VSF_0; /* Vs = 1*/
2640 yblockNb = 0; /* 1 block of 8x8*/
2643 hjpeg->Instance->CONFR1 &= ~(JPEG_CONFR1_NF | JPEG_CONFR1_NS);
2644 hjpeg->Instance->CONFR1 |= (JPEG_CONFR1_NF_1 | JPEG_CONFR1_NS_1);
2646 /*Reset CONFR4 register*/
2647 hjpeg->Instance->CONFR4 = 0;
2648 /*Set Horizental and Vertical sampling factor , number of blocks , Quantization table and Huffman AC/DC tables for component 0*/
2649 hjpeg->Instance->CONFR4 |= (ySamplingH | ySamplingV | (yblockNb & JPEG_CONFR4_NB) );
2651 /*Reset CONFR5 register*/
2652 hjpeg->Instance->CONFR5 = 0;
2653 /*Set Horizental and Vertical sampling factor , number of blocks , Quantization table and Huffman AC/DC tables for component 1*/
2654 hjpeg->Instance->CONFR5 |= (JPEG_CONFR5_HSF_0 | JPEG_CONFR5_VSF_0 | JPEG_CONFR5_QT_0 | JPEG_CONFR5_HA | JPEG_CONFR5_HD);
2656 /*Reset CONFR6 register*/
2657 hjpeg->Instance->CONFR6 = 0;
2658 /*Set Horizental and Vertical sampling factor and number of blocks for component 2*/
2659 /* In YCBCR , by default, both chrominance components (component 1 and component 2) use the same Quantization table (table 1) */
2660 /* In YCBCR , both chrominance components (component 1 and component 2) use the same Huffman tables (table 1) */
2661 hjpeg->Instance->CONFR6 |= (JPEG_CONFR6_HSF_0 | JPEG_CONFR6_VSF_0 | JPEG_CONFR6_QT_0 | JPEG_CONFR6_HA | JPEG_CONFR6_HD);
2666 * @brief Configure the JPEG registers for GrayScale color space
2667 * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
2668 * the configuration information for JPEG module
2669 * @retval None
2671 static void JPEG_SetColorGrayScale(JPEG_HandleTypeDef *hjpeg)
2673 /*Set Number of color components to 1*/
2674 hjpeg->Instance->CONFR1 &= ~(JPEG_CONFR1_NF | JPEG_CONFR1_NS);
2676 /*in GrayScale use 1 single Quantization table (Table 0)*/
2677 /*in GrayScale use only one couple of AC/DC huffman table (table 0)*/
2679 /*Reset CONFR4 register*/
2680 hjpeg->Instance->CONFR4 = 0;
2681 /*Set Horizental and Vertical sampling factor , number of blocks , Quantization table and Huffman AC/DC tables for component 0*/
2682 hjpeg->Instance->CONFR4 |= JPEG_CONFR4_HSF_0 | JPEG_CONFR4_VSF_0 ;
2686 * @brief Configure the JPEG registers for CMYK color space
2687 * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
2688 * the configuration information for JPEG module
2689 * @retval None
2691 static void JPEG_SetColorCMYK(JPEG_HandleTypeDef *hjpeg)
2693 uint32_t ySamplingH;
2694 uint32_t ySamplingV;
2695 uint32_t yblockNb;
2697 /*Set Number of color components to 4*/
2698 hjpeg->Instance->CONFR1 |= (JPEG_CONFR1_NF | JPEG_CONFR1_NS);
2700 /* compute MCU block size and Y, Cb ,Cr sampling factors*/
2701 if(hjpeg->Conf.ChromaSubsampling == JPEG_420_SUBSAMPLING)
2703 ySamplingH = JPEG_CONFR4_HSF_1; /* Hs = 2*/
2704 ySamplingV = JPEG_CONFR4_VSF_1; /* Vs = 2*/
2706 yblockNb = 0x30; /* 4 blocks of 8x8*/
2708 else if(hjpeg->Conf.ChromaSubsampling == JPEG_422_SUBSAMPLING)
2710 ySamplingH = JPEG_CONFR4_HSF_1; /* Hs = 2*/
2711 ySamplingV = JPEG_CONFR4_VSF_0; /* Vs = 1*/
2713 yblockNb = 0x10; /* 2 blocks of 8x8*/
2715 else /*JPEG_444_SUBSAMPLING and default*/
2717 ySamplingH = JPEG_CONFR4_HSF_0; /* Hs = 1*/
2718 ySamplingV = JPEG_CONFR4_VSF_0; /* Vs = 1*/
2720 yblockNb = 0; /* 1 block of 8x8*/
2723 /*Reset CONFR4 register*/
2724 hjpeg->Instance->CONFR4 = 0;
2725 /*Set Horizental and Vertical sampling factor , number of blocks , Quantization table and Huffman AC/DC tables for component 0*/
2726 hjpeg->Instance->CONFR4 |= (ySamplingH | ySamplingV | (yblockNb & JPEG_CONFR4_NB) );
2728 /*Reset CONFR5 register*/
2729 hjpeg->Instance->CONFR5 = 0;
2730 /*Set Horizental and Vertical sampling factor , number of blocks , Quantization table and Huffman AC/DC tables for component 1*/
2731 hjpeg->Instance->CONFR5 |= (JPEG_CONFR5_HSF_0 | JPEG_CONFR5_VSF_0);
2733 /*Reset CONFR6 register*/
2734 hjpeg->Instance->CONFR6 = 0;
2735 /*Set Horizental and Vertical sampling factor , number of blocks , Quantization table and Huffman AC/DC tables for component 2*/
2736 hjpeg->Instance->CONFR6 |= (JPEG_CONFR6_HSF_0 | JPEG_CONFR6_VSF_0);
2738 /*Reset CONFR7 register*/
2739 hjpeg->Instance->CONFR7 = 0;
2740 /*Set Horizental and Vertical sampling factor , number of blocks , Quantization table and Huffman AC/DC tables for component 3*/
2741 hjpeg->Instance->CONFR7 |= (JPEG_CONFR7_HSF_0 | JPEG_CONFR7_VSF_0);
2745 * @brief Init the JPEG encoding/decoding process in case of Polling or Interrupt and DMA
2746 * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
2747 * the configuration information for JPEG module
2748 * @retval None
2750 static void JPEG_Init_Process(JPEG_HandleTypeDef *hjpeg)
2752 /*Reset pause*/
2753 hjpeg->Context &= (~(JPEG_CONTEXT_PAUSE_INPUT | JPEG_CONTEXT_PAUSE_OUTPUT));
2755 if((hjpeg->Context & JPEG_CONTEXT_OPERATION_MASK) == JPEG_CONTEXT_DECODE)
2757 /*Set JPEG Codec to Decoding mode */
2758 hjpeg->Instance->CONFR1 |= JPEG_CONFR1_DE;
2760 else if((hjpeg->Context & JPEG_CONTEXT_OPERATION_MASK) == JPEG_CONTEXT_ENCODE)
2762 /*Set JPEG Codec to Encoding mode */
2763 hjpeg->Instance->CONFR1 &= ~JPEG_CONFR1_DE;
2766 /*Stop JPEG processing */
2767 hjpeg->Instance->CONFR0 &= ~JPEG_CONFR0_START;
2769 /* Disable All Interrupts */
2770 __HAL_JPEG_DISABLE_IT(hjpeg,JPEG_INTERRUPT_MASK);
2772 /* Disable All DMA requests */
2773 JPEG_DISABLE_DMA(hjpeg,JPEG_DMA_MASK);
2775 /* Flush input and output FIFOs*/
2776 hjpeg->Instance->CR |= JPEG_CR_IFF;
2777 hjpeg->Instance->CR |= JPEG_CR_OFF;
2779 /* Clear all flags */
2780 __HAL_JPEG_CLEAR_FLAG(hjpeg,JPEG_FLAG_ALL);
2782 /*Start Encoding/Decoding*/
2783 hjpeg->Instance->CONFR0 |= JPEG_CONFR0_START;
2785 if((hjpeg->Context & JPEG_CONTEXT_METHOD_MASK) == JPEG_CONTEXT_IT)
2787 /*Enable IN/OUT, end of Conversation, and end of header parsing interruptions*/
2788 __HAL_JPEG_ENABLE_IT(hjpeg, JPEG_IT_IFT | JPEG_IT_IFNF | JPEG_IT_OFT | JPEG_IT_OFNE | JPEG_IT_EOC |JPEG_IT_HPD);
2790 else if((hjpeg->Context & JPEG_CONTEXT_METHOD_MASK) == JPEG_CONTEXT_DMA)
2792 /*Enable End Of Conversation, and End Of Header parsing interruptions*/
2793 __HAL_JPEG_ENABLE_IT(hjpeg, JPEG_IT_EOC |JPEG_IT_HPD);
2799 * @brief JPEG encoding/decoding process in case of Polling or Interrupt
2800 * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
2801 * the configuration information for JPEG module
2802 * @retval JPEG_PROCESS_DONE if the process has ends else JPEG_PROCESS_ONGOING
2804 static uint32_t JPEG_Process(JPEG_HandleTypeDef *hjpeg)
2806 uint32_t tmpContext;
2808 /*End of header processing flag rised*/
2809 if(((hjpeg->Context & JPEG_CONTEXT_OPERATION_MASK) == JPEG_CONTEXT_DECODE) && (__HAL_JPEG_GET_FLAG(hjpeg, JPEG_FLAG_HPDF) != RESET))
2811 /*Call Header parsing complet callback */
2812 HAL_JPEG_GetInfo(hjpeg, &hjpeg->Conf);
2813 /* Reset the ImageQuality */
2814 hjpeg->Conf.ImageQuality = 0;
2815 /* Note : the image quality is only available at the end of the decoding operation */
2816 /* at the current stage the calculated image quality is not correct so reset it */
2818 /*Call Info Ready callback */
2819 HAL_JPEG_InfoReadyCallback(hjpeg, &hjpeg->Conf);
2821 __HAL_JPEG_DISABLE_IT(hjpeg,JPEG_IT_HPD);
2823 /* Clear header processing done flag */
2824 __HAL_JPEG_CLEAR_FLAG(hjpeg,JPEG_FLAG_HPDF);
2827 /*Input FIFO status handling*/
2828 if((hjpeg->Context & JPEG_CONTEXT_PAUSE_INPUT) == 0)
2830 if(__HAL_JPEG_GET_FLAG(hjpeg, JPEG_FLAG_IFTF) != RESET)
2832 /*Input FIFO threshold flag rised*/
2833 /*4 words (16 bytes) can be written in */
2834 JPEG_ReadInputData(hjpeg,4);
2836 else if(__HAL_JPEG_GET_FLAG(hjpeg, JPEG_FLAG_IFNFF) != RESET)
2838 /*Input FIFO Not Full flag rised*/
2839 /*32-bit value can be written in */
2840 JPEG_ReadInputData(hjpeg,1);
2845 /*Output FIFO flag handling*/
2846 if((hjpeg->Context & JPEG_CONTEXT_PAUSE_OUTPUT) == 0)
2848 if(__HAL_JPEG_GET_FLAG(hjpeg, JPEG_FLAG_OFTF) != RESET)
2850 /*Output FIFO threshold flag rised*/
2851 /*4 words (16 bytes) can be read out */
2852 JPEG_StoreOutputData(hjpeg, 4);
2854 else if(__HAL_JPEG_GET_FLAG(hjpeg, JPEG_FLAG_OFNEF) != RESET)
2856 /*Output FIFO Not Empty flag rised*/
2857 /*32-bit value can be read out */
2858 JPEG_StoreOutputData(hjpeg, 1);
2862 /*End of Conversion handling :i.e EOC flag is high and OFTF low and OFNEF low*/
2863 if(__HAL_JPEG_GET_FLAG(hjpeg, JPEG_FLAG_EOCF | JPEG_FLAG_OFTF | JPEG_FLAG_OFNEF) == JPEG_FLAG_EOCF)
2865 /*Stop Encoding/Decoding*/
2866 hjpeg->Instance->CONFR0 &= ~JPEG_CONFR0_START;
2868 if((hjpeg->Context & JPEG_CONTEXT_METHOD_MASK) == JPEG_CONTEXT_IT)
2870 /* Disable All Interrupts */
2871 __HAL_JPEG_DISABLE_IT(hjpeg,JPEG_INTERRUPT_MASK);
2874 /* Clear all flags */
2875 __HAL_JPEG_CLEAR_FLAG(hjpeg,JPEG_FLAG_ALL);
2877 /*Call End of conversion callback */
2878 if(hjpeg->JpegOutCount > 0)
2880 /*Output Buffer is not empty, call DecodedDataReadyCallback*/
2881 HAL_JPEG_DataReadyCallback (hjpeg, hjpeg->pJpegOutBuffPtr, hjpeg->JpegOutCount);
2882 hjpeg->JpegOutCount = 0;
2885 /*Reset Context Operation*/
2886 tmpContext = hjpeg->Context;
2887 /*Clear all context fields execpt JPEG_CONTEXT_CONF_ENCODING and JPEG_CONTEXT_CUSTOM_TABLES*/
2888 hjpeg->Context &= (JPEG_CONTEXT_CONF_ENCODING | JPEG_CONTEXT_CUSTOM_TABLES);
2890 /* Process Unlocked */
2891 __HAL_UNLOCK(hjpeg);
2893 /* Change the JPEG state */
2894 hjpeg->State = HAL_JPEG_STATE_READY;
2896 /*Call End of Encoding/Decoding callback */
2897 if((tmpContext & JPEG_CONTEXT_OPERATION_MASK) == JPEG_CONTEXT_DECODE)
2899 HAL_JPEG_DecodeCpltCallback(hjpeg);
2901 else if((tmpContext & JPEG_CONTEXT_OPERATION_MASK) == JPEG_CONTEXT_ENCODE)
2903 HAL_JPEG_EncodeCpltCallback(hjpeg);
2906 return JPEG_PROCESS_DONE;
2910 return JPEG_PROCESS_ONGOING;
2914 * @brief Store some output data from the JPEG peripheral to the output buffer.
2915 * This function is used when the JPEG peripheral has new data to output
2916 * in case of Polling or Interrupt process
2917 * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
2918 * the configuration information for JPEG module
2919 * @param nbOutputWords: Number of output words (of 32 bits) ready from the JPEG peripheral
2920 * @retval None
2922 static void JPEG_StoreOutputData(JPEG_HandleTypeDef *hjpeg, uint32_t nbOutputWords)
2924 uint32_t index, nBwords, nbBytes , dataword, *pOutData;
2926 pOutData = (uint32_t *)(((uint32_t *)hjpeg->pJpegOutBuffPtr) + (hjpeg->JpegOutCount/4));
2928 if(hjpeg->OutDataLength >= (hjpeg->JpegOutCount + (nbOutputWords*4)))
2930 for(index = 0; index < nbOutputWords; index++)
2932 /*Transfer 32 bits from the JPEG output FIFO*/
2933 *pOutData = hjpeg->Instance->DOR;
2934 pOutData++;
2935 hjpeg->JpegOutCount += 4;
2937 if(hjpeg->OutDataLength == hjpeg->JpegOutCount)
2939 /*Output Buffer is full, call DecodedDataReadyCallback*/
2940 HAL_JPEG_DataReadyCallback (hjpeg, hjpeg->pJpegOutBuffPtr, hjpeg->JpegOutCount);
2941 hjpeg->JpegOutCount = 0;
2944 else if(hjpeg->OutDataLength > hjpeg->JpegOutCount)
2946 nBwords = (hjpeg->OutDataLength - hjpeg->JpegOutCount)/4;
2947 for(index = 0; index < nBwords; index++)
2949 /*Transfer 32 bits from the JPEG output FIFO*/
2950 *pOutData = hjpeg->Instance->DOR;
2951 pOutData++;
2952 hjpeg->JpegOutCount += 4;
2954 if(hjpeg->OutDataLength == hjpeg->JpegOutCount)
2956 /*Output Buffer is full, call DecodedDataReadyCallback*/
2957 HAL_JPEG_DataReadyCallback (hjpeg, hjpeg->pJpegOutBuffPtr, hjpeg->JpegOutCount);
2958 hjpeg->JpegOutCount = 0;
2960 else
2962 nbBytes = hjpeg->OutDataLength - hjpeg->JpegOutCount;
2963 dataword = hjpeg->Instance->DOR;
2964 for(index = 0; index < nbBytes; index++)
2966 hjpeg->pJpegOutBuffPtr[hjpeg->JpegOutCount] = (dataword >> (8*index)) & 0xFF;
2967 hjpeg->JpegOutCount++;
2969 /*Output Buffer is full, call DecodedDataReadyCallback*/
2970 HAL_JPEG_DataReadyCallback (hjpeg, hjpeg->pJpegOutBuffPtr, hjpeg->JpegOutCount);
2971 hjpeg->JpegOutCount = 0;
2973 nbBytes = 4 - nbBytes;
2974 for(index = nbBytes; index < 4; index++)
2976 hjpeg->pJpegOutBuffPtr[hjpeg->JpegOutCount] = (dataword >> (8*index)) & 0xFF;
2977 hjpeg->JpegOutCount++;
2984 * @brief Read some input Data from the input buffer.
2985 * This function is used when the JPEG peripheral needs new data
2986 * in case of Polling or Interrupt process
2987 * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
2988 * the configuration information for JPEG module
2989 * @param nbRequestWords: Number of input words (of 32 bits) that the JPE peripheral request
2990 * @retval None
2992 static void JPEG_ReadInputData(JPEG_HandleTypeDef *hjpeg, uint32_t nbRequestWords)
2994 uint32_t nbBytes = 0, nBwords, index, Dataword;
2996 if((hjpeg->InDataLength == 0) || (nbRequestWords == 0))
2998 /* No more Input data : nothing to do*/
2999 HAL_JPEG_Pause(hjpeg, JPEG_PAUSE_RESUME_INPUT);
3001 else if(hjpeg->InDataLength > hjpeg->JpegInCount)
3003 nbBytes = hjpeg->InDataLength - hjpeg->JpegInCount;
3005 else if(hjpeg->InDataLength == hjpeg->JpegInCount)
3007 /*Call HAL_JPEG_GetDataCallback to get new data */
3008 HAL_JPEG_GetDataCallback(hjpeg, hjpeg->JpegInCount);
3009 if(hjpeg->InDataLength > 4)
3011 hjpeg->InDataLength = hjpeg->InDataLength - (hjpeg->InDataLength % 4);
3013 hjpeg->JpegInCount = 0;
3014 nbBytes = hjpeg->InDataLength;
3016 if((nbBytes > 0) && ((hjpeg->Context & JPEG_CONTEXT_PAUSE_INPUT) == 0))
3018 nBwords = nbBytes / 4;
3019 if(nBwords >= nbRequestWords)
3021 for(index = 0; index < nbRequestWords; index++)
3023 hjpeg->Instance->DIR = *((uint32_t *)(((uint32_t *)hjpeg->pJpegInBuffPtr) + (hjpeg->JpegInCount/4)));
3025 hjpeg->JpegInCount += 4;
3028 else /*nBwords < nbRequestWords*/
3030 if(nBwords > 0)
3032 for(index = 0; index < nBwords; index++)
3034 hjpeg->Instance->DIR = *((uint32_t *)(((uint32_t *)hjpeg->pJpegInBuffPtr) + (hjpeg->JpegInCount/4)));
3036 hjpeg->JpegInCount += 4;
3039 else
3041 /* end of file*/
3042 Dataword = 0;
3043 for(index=0; index< nbBytes; index++)
3045 Dataword |= (uint32_t)hjpeg->pJpegInBuffPtr[hjpeg->JpegInCount] << (8 * index);
3046 hjpeg->JpegInCount++;
3048 hjpeg->Instance->DIR = Dataword;
3055 * @brief Start the JPEG DMA process (encoding/decoding)
3056 * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
3057 * the configuration information for JPEG module
3058 * @retval JPEG_PROCESS_DONE if process ends else JPEG_PROCESS_ONGOING
3060 static HAL_StatusTypeDef JPEG_DMA_StartProcess(JPEG_HandleTypeDef *hjpeg)
3062 if((hjpeg->InDataLength < 4) || (hjpeg->OutDataLength < 4))
3064 return HAL_ERROR;
3066 /* Reset Ending DMA internal context flag*/
3067 hjpeg->Context &= ~JPEG_CONTEXT_ENDING_DMA;
3069 /* Disable DMA In/Out Request*/
3070 JPEG_DISABLE_DMA(hjpeg,JPEG_DMA_ODMA | JPEG_DMA_IDMA);
3072 /* Set the JPEG DMA In transfer complete callback */
3073 hjpeg->hdmain->XferCpltCallback = JPEG_DMAInCpltCallback;
3074 /* Set the DMA In error callback */
3075 hjpeg->hdmain->XferErrorCallback = JPEG_DMAErrorCallback;
3077 /* Set the JPEG DMA Out transfer complete callback */
3078 hjpeg->hdmaout->XferCpltCallback = JPEG_DMAOutCpltCallback;
3079 /* Set the DMA Out error callback */
3080 hjpeg->hdmaout->XferErrorCallback = JPEG_DMAErrorCallback;
3081 /* Set the DMA Out Abort callback */
3082 hjpeg->hdmaout->XferAbortCallback = JPEG_DMAOutAbortCallback;
3084 /*DMA transfer size must be a multiple of 4 bytes i.e mutliple of 32bits words*/
3085 hjpeg->InDataLength = hjpeg->InDataLength - (hjpeg->InDataLength % 4);
3087 /*DMA transfer size must be a multiple of 4 bytes i.e mutliple of 32bits words*/
3088 hjpeg->OutDataLength = hjpeg->OutDataLength - (hjpeg->OutDataLength % 4);
3090 /* Start DMA FIFO In transfer */
3091 HAL_DMA_Start_IT(hjpeg->hdmain, (uint32_t)hjpeg->pJpegInBuffPtr, (uint32_t)&hjpeg->Instance->DIR, hjpeg->InDataLength >> 2);
3093 /* Start DMA FIFO Out transfer */
3094 HAL_DMA_Start_IT(hjpeg->hdmaout, (uint32_t)&hjpeg->Instance->DOR, (uint32_t)hjpeg->pJpegOutBuffPtr, hjpeg->OutDataLength >> 2);
3096 /* Enable JPEG In/Out DMA requests*/
3097 JPEG_ENABLE_DMA(hjpeg,JPEG_DMA_IDMA | JPEG_DMA_ODMA);
3099 return HAL_OK;
3103 * @brief Continue the current JPEG DMA process (encoding/decoding)
3104 * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
3105 * the configuration information for JPEG module
3106 * @retval JPEG_PROCESS_DONE if process ends else JPEG_PROCESS_ONGOING
3108 static uint32_t JPEG_DMA_ContinueProcess(JPEG_HandleTypeDef *hjpeg)
3110 /*End of header processing flag rises*/
3111 if(((hjpeg->Context & JPEG_CONTEXT_OPERATION_MASK) == JPEG_CONTEXT_DECODE) && (__HAL_JPEG_GET_FLAG(hjpeg, JPEG_FLAG_HPDF) != RESET))
3113 /*Call Header parsing complete callback */
3114 HAL_JPEG_GetInfo(hjpeg, &hjpeg->Conf);
3116 /* Reset the ImageQuality */
3117 hjpeg->Conf.ImageQuality = 0;
3118 /* Note : the image quality is only available at the end of the decoding operation */
3119 /* at the current stage the calculated image quality is not correct so reset it */
3121 /*Call Info Ready callback */
3122 HAL_JPEG_InfoReadyCallback(hjpeg, &hjpeg->Conf);
3124 __HAL_JPEG_DISABLE_IT(hjpeg,JPEG_IT_HPD);
3126 /* Clear header processing done flag */
3127 __HAL_JPEG_CLEAR_FLAG(hjpeg,JPEG_FLAG_HPDF);
3130 /*End of Conversion handling*/
3131 if(__HAL_JPEG_GET_FLAG(hjpeg, JPEG_FLAG_EOCF) != RESET)
3133 /*Disabkle JPEG In/Out DMA Requests*/
3134 JPEG_DISABLE_DMA(hjpeg,JPEG_DMA_ODMA | JPEG_DMA_IDMA);
3136 hjpeg->Context |= JPEG_CONTEXT_ENDING_DMA;
3138 /*Stop Encoding/Decoding*/
3139 hjpeg->Instance->CONFR0 &= ~JPEG_CONFR0_START;
3141 __HAL_JPEG_DISABLE_IT(hjpeg,JPEG_INTERRUPT_MASK);
3143 /* Clear all flags */
3144 __HAL_JPEG_CLEAR_FLAG(hjpeg,JPEG_FLAG_ALL);
3146 if(hjpeg->hdmain->State == HAL_DMA_STATE_BUSY)
3148 /* Stop the DMA In Xfer*/
3149 HAL_DMA_Abort_IT(hjpeg->hdmain);
3152 if(hjpeg->hdmaout->State == HAL_DMA_STATE_BUSY)
3154 /* Stop the DMA out Xfer*/
3155 HAL_DMA_Abort_IT(hjpeg->hdmaout);
3157 else
3159 return JPEG_DMA_EndProcess(hjpeg);
3163 return JPEG_PROCESS_ONGOING;
3167 * @brief Finalize the current JPEG DMA process (encoding/decoding)
3168 * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
3169 * the configuration information for JPEG module
3170 * @retval JPEG_PROCESS_DONE
3172 static uint32_t JPEG_DMA_EndProcess(JPEG_HandleTypeDef *hjpeg)
3174 uint32_t tmpContext;
3175 hjpeg->JpegOutCount = hjpeg->OutDataLength - ((hjpeg->hdmaout->Instance->NDTR & DMA_SxNDT) << 2);
3177 /*if Output Buffer is full, call HAL_JPEG_DataReadyCallback*/
3178 if(hjpeg->JpegOutCount == hjpeg->OutDataLength)
3180 HAL_JPEG_DataReadyCallback (hjpeg, hjpeg->pJpegOutBuffPtr, hjpeg->JpegOutCount);
3181 hjpeg->JpegOutCount = 0;
3184 /*Check if remaining data in the output FIFO*/
3185 if(__HAL_JPEG_GET_FLAG(hjpeg, JPEG_FLAG_OFNEF) == 0)
3187 /*Stop Encoding/Decoding*/
3188 hjpeg->Instance->CONFR0 &= ~JPEG_CONFR0_START;
3190 tmpContext = hjpeg->Context;
3191 /*Clear all context fileds execpt JPEG_CONTEXT_CONF_ENCODING and JPEG_CONTEXT_CUSTOM_TABLES*/
3192 hjpeg->Context &= (JPEG_CONTEXT_CONF_ENCODING | JPEG_CONTEXT_CUSTOM_TABLES);
3194 /* Process Unlocked */
3195 __HAL_UNLOCK(hjpeg);
3197 /* Change the JPEG state */
3198 hjpeg->State = HAL_JPEG_STATE_READY;
3200 /*Call End of Encoding/Decoding callback */
3201 if((tmpContext & JPEG_CONTEXT_OPERATION_MASK) == JPEG_CONTEXT_DECODE)
3203 HAL_JPEG_DecodeCpltCallback(hjpeg);
3205 else if((tmpContext & JPEG_CONTEXT_OPERATION_MASK) == JPEG_CONTEXT_ENCODE)
3207 HAL_JPEG_EncodeCpltCallback(hjpeg);
3210 else if((hjpeg->Context & JPEG_CONTEXT_PAUSE_OUTPUT) == 0)
3212 JPEG_DMA_PollResidualData(hjpeg);
3214 return JPEG_PROCESS_DONE;
3217 return JPEG_PROCESS_ONGOING;
3221 * @brief Poll residual output data when DMA process (encoding/decoding)
3222 * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
3223 * the configuration information for JPEG module
3224 * @retval None.
3226 static void JPEG_DMA_PollResidualData(JPEG_HandleTypeDef *hjpeg)
3228 uint32_t tmpContext, count = JPEG_FIFO_SIZE, *pDataOut;
3229 pDataOut = (uint32_t *)(hjpeg->pJpegOutBuffPtr + hjpeg->JpegOutCount);
3231 while((__HAL_JPEG_GET_FLAG(hjpeg, JPEG_FLAG_OFNEF) != 0) && (count > 0))
3233 count--;
3235 *pDataOut = hjpeg->Instance->DOR;
3236 pDataOut++;
3237 hjpeg->JpegOutCount += 4;
3239 if(hjpeg->JpegOutCount == hjpeg->OutDataLength)
3241 /*Output Buffer is full, call HAL_JPEG_DataReadyCallback*/
3242 HAL_JPEG_DataReadyCallback (hjpeg, hjpeg->pJpegOutBuffPtr, hjpeg->JpegOutCount);
3243 hjpeg->JpegOutCount = 0;
3247 /*Stop Encoding/Decoding*/
3248 hjpeg->Instance->CONFR0 &= ~JPEG_CONFR0_START;
3250 if(hjpeg->JpegOutCount > 0)
3252 /*Output Buffer is not empty, call DecodedDataReadyCallback*/
3253 HAL_JPEG_DataReadyCallback (hjpeg, hjpeg->pJpegOutBuffPtr, hjpeg->JpegOutCount);
3254 hjpeg->JpegOutCount = 0;
3257 tmpContext = hjpeg->Context;
3258 /*Clear all context fileds execpt JPEG_CONTEXT_CONF_ENCODING and JPEG_CONTEXT_CUSTOM_TABLES*/
3259 hjpeg->Context &= (JPEG_CONTEXT_CONF_ENCODING | JPEG_CONTEXT_CUSTOM_TABLES);
3261 /* Process Unlocked */
3262 __HAL_UNLOCK(hjpeg);
3264 /* Change the JPEG state */
3265 hjpeg->State = HAL_JPEG_STATE_READY;
3267 /*Call End of Encoding/Decoding callback */
3268 if((tmpContext & JPEG_CONTEXT_OPERATION_MASK) == JPEG_CONTEXT_DECODE)
3270 HAL_JPEG_DecodeCpltCallback(hjpeg);
3272 else if((tmpContext & JPEG_CONTEXT_OPERATION_MASK) == JPEG_CONTEXT_ENCODE)
3274 HAL_JPEG_EncodeCpltCallback(hjpeg);
3279 * @brief DMA input transfer complete callback
3280 * @param hdma: pointer to a DMA_HandleTypeDef structure.
3281 * @retval None
3283 static void JPEG_DMAInCpltCallback(DMA_HandleTypeDef *hdma)
3285 JPEG_HandleTypeDef* hjpeg = (JPEG_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
3287 /* Disable The JPEG IT so the DMA Input Callback can not be interrupted by the JPEG EOC IT or JPEG HPD IT */
3288 __HAL_JPEG_DISABLE_IT(hjpeg,JPEG_INTERRUPT_MASK);
3290 if(((hjpeg->Context & JPEG_CONTEXT_METHOD_MASK) == JPEG_CONTEXT_DMA) && ((hjpeg->Context & JPEG_CONTEXT_ENDING_DMA) == 0))
3292 JPEG_DISABLE_DMA(hjpeg,JPEG_DMA_IDMA);
3294 hjpeg->JpegInCount = hjpeg->InDataLength - ((hdma->Instance->NDTR & DMA_SxNDT) << 2);
3296 /*Call HAL_JPEG_GetDataCallback to get new data */
3297 HAL_JPEG_GetDataCallback(hjpeg, hjpeg->JpegInCount);
3299 if(hjpeg->InDataLength >= 4)
3301 /*JPEG Input DMA transfer data number must be multiple of 32 bits word
3302 as the destination is a 32 bits (4 bytes) register */
3303 hjpeg->InDataLength = hjpeg->InDataLength - (hjpeg->InDataLength % 4);
3305 else if(hjpeg->InDataLength > 0)
3307 /*Transfer last data word (i.e last 4 bytes)*/
3308 hjpeg->InDataLength = 4;
3311 if(((hjpeg->Context & JPEG_CONTEXT_PAUSE_INPUT) == 0) && (hjpeg->InDataLength > 0))
3313 /* Start DMA FIFO In transfer */
3314 HAL_DMA_Start_IT(hjpeg->hdmain, (uint32_t)hjpeg->pJpegInBuffPtr, (uint32_t)&hjpeg->Instance->DIR, hjpeg->InDataLength >> 2);
3315 JPEG_ENABLE_DMA(hjpeg,JPEG_DMA_IDMA);
3318 /* JPEG Conversion still on going : Enable the JPEG IT */
3319 __HAL_JPEG_ENABLE_IT(hjpeg,JPEG_IT_EOC |JPEG_IT_HPD);
3324 * @brief DMA output transfer complete callback
3325 * @param hdma: pointer to a DMA_HandleTypeDef structure.
3326 * @retval None
3328 static void JPEG_DMAOutCpltCallback(DMA_HandleTypeDef *hdma)
3330 JPEG_HandleTypeDef* hjpeg = (JPEG_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
3332 /* Disable The JPEG IT so the DMA Output Callback can not be interrupted by the JPEG EOC IT or JPEG HPD IT */
3333 __HAL_JPEG_DISABLE_IT(hjpeg,JPEG_INTERRUPT_MASK);
3335 if(((hjpeg->Context & JPEG_CONTEXT_METHOD_MASK) == JPEG_CONTEXT_DMA) && ((hjpeg->Context & JPEG_CONTEXT_ENDING_DMA) == 0))
3337 if(__HAL_JPEG_GET_FLAG(hjpeg, JPEG_FLAG_EOCF) == 0)
3339 JPEG_DISABLE_DMA(hjpeg,JPEG_DMA_ODMA);
3340 hjpeg->JpegOutCount = hjpeg->OutDataLength - ((hdma->Instance->NDTR & DMA_SxNDT) << 2);
3342 /*Output Buffer is full, call HAL_JPEG_DataReadyCallback*/
3343 HAL_JPEG_DataReadyCallback (hjpeg, hjpeg->pJpegOutBuffPtr, hjpeg->JpegOutCount);
3345 if((hjpeg->Context & JPEG_CONTEXT_PAUSE_OUTPUT) == 0)
3347 /* Start DMA FIFO Out transfer */
3348 HAL_DMA_Start_IT(hjpeg->hdmaout, (uint32_t)&hjpeg->Instance->DOR, (uint32_t)hjpeg->pJpegOutBuffPtr, hjpeg->OutDataLength >> 2);
3349 JPEG_ENABLE_DMA(hjpeg,JPEG_DMA_ODMA);
3353 /* JPEG Conversion still on going : Enable the JPEG IT */
3354 __HAL_JPEG_ENABLE_IT(hjpeg,JPEG_IT_EOC |JPEG_IT_HPD);
3359 * @brief DMA Transfer error callback
3360 * @param hdma: pointer to a DMA_HandleTypeDef structure.
3361 * @retval None
3363 static void JPEG_DMAErrorCallback(DMA_HandleTypeDef *hdma)
3365 JPEG_HandleTypeDef* hjpeg = (JPEG_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
3367 /* if DMA error is FIFO error ignore it */
3368 if(HAL_DMA_GetError(hdma) != HAL_DMA_ERROR_FE)
3370 /*Stop Encoding/Decoding*/
3371 hjpeg->Instance->CONFR0 &= ~JPEG_CONFR0_START;
3373 /* Disable All Interrupts */
3374 __HAL_JPEG_DISABLE_IT(hjpeg,JPEG_INTERRUPT_MASK);
3376 /* Disable All DMA requests */
3377 JPEG_DISABLE_DMA(hjpeg,JPEG_DMA_MASK);
3379 hjpeg->State= HAL_JPEG_STATE_READY;
3380 hjpeg->ErrorCode |= HAL_JPEG_ERROR_DMA;
3381 HAL_JPEG_ErrorCallback(hjpeg);
3386 * @brief DMA output Abort callback
3387 * @param hdma: pointer to a DMA_HandleTypeDef structure.
3388 * @retval None
3390 static void JPEG_DMAOutAbortCallback(DMA_HandleTypeDef *hdma)
3392 JPEG_HandleTypeDef* hjpeg = (JPEG_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
3394 if((hjpeg->Context & JPEG_CONTEXT_ENDING_DMA) != 0)
3396 JPEG_DMA_EndProcess(hjpeg);
3401 * @brief Calculate the decoded image quality (from 1 to 100)
3402 * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
3403 * the configuration information for JPEG module
3404 * @retval JPEG image quality from 1 to 100.
3406 static uint32_t JPEG_GetQuality(JPEG_HandleTypeDef *hjpeg)
3408 uint32_t quality = 0;
3409 uint32_t quantRow, quantVal,scale, i, j;
3410 uint32_t *tableAddress = (uint32_t *)hjpeg->Instance->QMEM0;
3412 i = 0;
3413 while( i < JPEG_QUANT_TABLE_SIZE)
3415 quantRow = *tableAddress;
3416 for(j=0; j<4; j++)
3418 quantVal = (quantRow >> (8 * j)) & 0xFF;
3419 if(quantVal == 1)
3421 /* if Quantization value = 1 then quality is 100%*/
3422 quality += 100;
3424 else
3426 /* Note that the quantization coefficients must be specified in the table in zigzag order */
3427 scale = (quantVal*100)/((uint32_t) JPEG_LUM_QuantTable[JPEG_ZIGZAG_ORDER[i+j]]);
3429 if(scale <= 100)
3431 quality += (200 - scale)/2;
3433 else
3435 quality += 5000/scale;
3440 i += 4;
3441 tableAddress ++;
3444 return (quality/((uint32_t)64));
3447 * @}
3450 #endif /* STM32F767xx || STM32F769xx || STM32F777xx || STM32F779xx */
3451 #endif /* HAL_JPEG_MODULE_ENABLED */
3453 * @}
3457 * @}
3460 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/