sidestep
[mTask.git] / int / com / lib / STM32F7xx_HAL_Driver / Src / stm32f7xx_hal_jpeg.c
1 /**
2 ******************************************************************************
3 * @file stm32f7xx_hal_jpeg.c
4 * @author MCD Application Team
5 * @version V1.1.0
6 * @date 22-April-2016
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
20 *
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.
28
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.
34
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.
39
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.
43
44 (#) Use functions HAL_JPEG_Encode and HAL_JPEG_Decode to start respectively
45 a JPEG encoding/decoding operation in polling method (blocking).
46
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).
49
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).
52
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.
57
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.
62
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 \93HAL_JPEG_ConfigInputBuffer\94 (within the callback HAL_JPEG_GetDataCallback)
73 with the parameter \93InDataLength\94 set to zero.
74
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".
82
83 The application can call functions \93HAL_JPEG_ConfigInputBuffer \93 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 \93HAL_JPEG_Pause\94 must be imperatively called
86 (if necessary) within the callback \93HAL_JPEG_GetDataCallback\94, i.e when the HAL JPEG has ended
87 Transferring the previous chunk buffer to the JPEG peripheral.
88
89 (#) Callback HAL_JPEG_DataReadyCallback is asserted when the HAL JPEG driver
90 has filled the given output buffer with the given size.
91
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.
102
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).
108
109 The application can call functions \93HAL_JPEG_ ConfigOutputBuffer\93 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 \93HAL_JPEG_Pause\94 must be imperatively called
112 (if necessary) within the callback \93HAL_JPEG_ DataReadyCallback\94, i.e when the HAL JPEG has ended
113 Transferring the previous chunk buffer from the JPEG peripheral to the application.
114
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.
118
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.
122
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.
126
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.
132
133 (#) To control JPEG state you can use the following function: HAL_JPEG_GetState()
134
135 *** JPEG HAL driver macros list ***
136 =============================================
137 [..]
138 Below the list of most used macros in JPEG HAL driver.
139
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).
148
149
150 @endverbatim
151 ******************************************************************************
152 * @attention
153 *
154 * <h2><center>&copy; COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
155 *
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.
166 *
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.
177 *
178 ******************************************************************************
179 */
180
181 /* Includes ------------------------------------------------------------------*/
182 #include "stm32f7xx_hal.h"
183
184 /** @addtogroup STM32F7xx_HAL_Driver
185 * @{
186 */
187
188 /** @defgroup JPEG JPEG
189 * @brief JPEG HAL module driver.
190 * @{
191 */
192
193 #ifdef HAL_JPEG_MODULE_ENABLED
194
195 #if defined (STM32F767xx) || defined (STM32F769xx) || defined (STM32F777xx) || defined (STM32F779xx)
196
197 /* Private define ------------------------------------------------------------*/
198 /** @addtogroup JPEG_Private_Constants
199 * @{
200 */
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*/
204
205 #define JPEG_FIFO_SIZE ((uint32_t)8U) /* JPEG Input/Output HW FIFO size in words*/
206
207 #define JPEG_INTERRUPT_MASK ((uint32_t)0x0000007EU) /* JPEG Interrupt Mask*/
208
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 */
212
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 */
216
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 */
221
222
223 #define JPEG_CONTEXT_CONF_ENCODING ((uint32_t)0x00000100U) /* JPEG context : encoding config done */
224
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 */
227
228 #define JPEG_CONTEXT_CUSTOM_TABLES ((uint32_t)0x00004000U) /* JPEG context : Use custom quantization tables */
229
230 #define JPEG_CONTEXT_ENDING_DMA ((uint32_t)0x00008000U) /* JPEG context : ending with DMA in progress */
231
232 #define JPEG_PROCESS_ONGOING ((uint32_t)0x00000000U) /* Process is on going */
233 #define JPEG_PROCESS_DONE ((uint32_t)0x00000001U) /* Process is done (ends) */
234 /**
235 * @}
236 */
237
238 /* Private typedef -----------------------------------------------------------*/
239 /** @addtogroup JPEG_Private_Types
240 * @{
241 */
242
243 /*
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
246 */
247 typedef struct
248 {
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 */
251
252 uint8_t HuffVal[162]; /*!< The symbols, in order of incremented code length, this parameter corresponds to HUFFVAL list in the Annex C */
253
254
255 }JPEG_ACHuffTableTypeDef;
256
257 typedef struct
258 {
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 */
261
262 uint8_t HuffVal[12]; /*!< The symbols, in order of incremented code length, this parameter corresponds to HUFFVAL list in the Annex C */
263
264
265 }JPEG_DCHuffTableTypeDef;
266
267 typedef struct
268 {
269 uint8_t CodeLength[JPEG_AC_HUFF_TABLE_SIZE]; /*!< Code length */
270
271 uint32_t HuffmanCode[JPEG_AC_HUFF_TABLE_SIZE]; /*!< HuffmanCode */
272
273 }JPEG_AC_HuffCodeTableTypeDef;
274
275 typedef struct
276 {
277 uint8_t CodeLength[JPEG_DC_HUFF_TABLE_SIZE]; /*!< Code length */
278
279 uint32_t HuffmanCode[JPEG_DC_HUFF_TABLE_SIZE]; /*!< HuffmanCode */
280
281 }JPEG_DC_HuffCodeTableTypeDef;
282 /**
283 * @}
284 */
285
286 /* Private macro -------------------------------------------------------------*/
287 /** @addtogroup JPEG_Private_Macros
288 * @{
289 */
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)
294 /**
295 * @}
296 */
297
298
299 /* Private variables ---------------------------------------------------------*/
300 /** @addtogroup JPEG_Private_Variables
301 * @{
302 */
303
304 static const JPEG_DCHuffTableTypeDef JPEG_DCLUM_HuffTable =
305 {
306 { 0, 1, 5, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0 }, /*Bits*/
307
308 { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0xa, 0xb } /*HUFFVAL */
309
310 };
311
312 static const JPEG_DCHuffTableTypeDef JPEG_DCCHROM_HuffTable =
313 {
314 { 0, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0 }, /*Bits*/
315
316 { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0xa, 0xb } /*HUFFVAL */
317 };
318
319 static const JPEG_ACHuffTableTypeDef JPEG_ACLUM_HuffTable =
320 {
321 { 0, 2, 1, 3, 3, 2, 4, 3, 5, 5, 4, 4, 0, 0, 1, 0x7d }, /*Bits*/
322
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 }
344 };
345
346 static const JPEG_ACHuffTableTypeDef JPEG_ACCHROM_HuffTable =
347 {
348 { 0, 2, 1, 2, 4, 4, 3, 4, 7, 5, 4, 4, 0, 1, 2, 0x77 }, /*Bits*/
349
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 }
371 };
372
373
374 /*
375 These are the sample quantization tables given in JPEG spec ISO/IEC 10918-1 standard , section K.1.
376 */
377 static const uint8_t JPEG_LUM_QuantTable[JPEG_QUANT_TABLE_SIZE] =
378 {
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
387 };
388 static const uint8_t JPEG_CHROM_QuantTable[JPEG_QUANT_TABLE_SIZE] =
389 {
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
398 };
399
400 static const uint8_t JPEG_ZIGZAG_ORDER[JPEG_QUANT_TABLE_SIZE] =
401 {
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
410 };
411 /**
412 * @}
413 */
414
415 /* Private function prototypes -----------------------------------------------*/
416 /** @addtogroup JPEG_Private_Functions_Prototypes
417 * @{
418 */
419
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);
431
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);
437
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_DMAOutCpltCallback(DMA_HandleTypeDef *hdma);
442 static void JPEG_DMAInCpltCallback(DMA_HandleTypeDef *hdma);
443 static void JPEG_DMAErrorCallback(DMA_HandleTypeDef *hdma);
444 static void JPEG_DMAOutAbortCallback(DMA_HandleTypeDef *hdma) ;
445 /**
446 * @}
447 */
448
449 /** @defgroup JPEG_Exported_Functions JPEG Exported Functions
450 * @{
451 */
452
453 /** @defgroup JPEG_Exported_Functions_Group1 Initialization and de-initialization functions
454 * @brief Initialization and de-initialization functions.
455 *
456 @verbatim
457 ==============================================================================
458 ##### Initialization and de-initialization functions #####
459 ==============================================================================
460 [..] This section provides functions allowing to:
461 (+) Initialize the JPEG peripheral and creates the associated handle
462 (+) DeInitialize the JPEG peripheral
463
464 @endverbatim
465 * @{
466 */
467
468 /**
469 * @brief Initializes the JPEG according to the specified
470 * parameters in the JPEG_InitTypeDef and creates the associated handle.
471 * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
472 * the configuration information for JPEG module
473 * @retval HAL status
474 */
475 HAL_StatusTypeDef HAL_JPEG_Init(JPEG_HandleTypeDef *hjpeg)
476 {
477 /*Note : these intermediate variables are used to avoid MISRA warning
478 regarding rule 11.5 */
479 uint32_t acLum_huffmanTableAddr = (uint32_t)(&JPEG_ACLUM_HuffTable);
480 uint32_t dcLum_huffmanTableAddr = (uint32_t)(&JPEG_DCLUM_HuffTable);
481 uint32_t acChrom_huffmanTableAddr = (uint32_t)(&JPEG_ACCHROM_HuffTable);
482 uint32_t dcChrom_huffmanTableAddr = (uint32_t)(&JPEG_DCCHROM_HuffTable);
483
484 /* Check the JPEG handle allocation */
485 if(hjpeg == NULL)
486 {
487 return HAL_ERROR;
488 }
489
490 if(hjpeg->State == HAL_JPEG_STATE_RESET)
491 {
492 /* Allocate lock resource and initialize it */
493 hjpeg->Lock = HAL_UNLOCKED;
494
495 /* Init the low level hardware : GPIO, CLOCK */
496 HAL_JPEG_MspInit(hjpeg);
497 }
498
499 /* Change the JPEG state */
500 hjpeg->State = HAL_JPEG_STATE_BUSY;
501
502 /* Start the JPEG Core*/
503 __HAL_JPEG_ENABLE(hjpeg);
504
505 /* Stop the JPEG encoding/decoding process*/
506 hjpeg->Instance->CONFR0 &= ~JPEG_CONFR0_START;
507
508 /* Disable All Interrupts */
509 __HAL_JPEG_DISABLE_IT(hjpeg,JPEG_INTERRUPT_MASK);
510
511 /* Disable All DMA requests */
512 JPEG_DISABLE_DMA(hjpeg,JPEG_DMA_MASK);
513
514 /* Flush input and output FIFOs*/
515 hjpeg->Instance->CR |= JPEG_CR_IFF;
516 hjpeg->Instance->CR |= JPEG_CR_OFF;
517
518 /* Clear all flags */
519 __HAL_JPEG_CLEAR_FLAG(hjpeg,JPEG_FLAG_ALL);
520
521 hjpeg->QuantTable0 = (uint8_t *)JPEG_LUM_QuantTable;
522 hjpeg->QuantTable1 = (uint8_t *)JPEG_CHROM_QuantTable;
523 hjpeg->QuantTable2 = NULL;
524 hjpeg->QuantTable3 = NULL;
525
526 /* init the default Huffman tables*/
527 if(JPEG_Set_HuffEnc_Mem(hjpeg, (JPEG_ACHuffTableTypeDef *)acLum_huffmanTableAddr, (JPEG_DCHuffTableTypeDef *)dcLum_huffmanTableAddr, (JPEG_ACHuffTableTypeDef *)acChrom_huffmanTableAddr, (JPEG_DCHuffTableTypeDef *)dcChrom_huffmanTableAddr) != HAL_OK)
528 {
529 hjpeg->ErrorCode = HAL_JPEG_ERROR_HUFF_TABLE;
530
531 return HAL_ERROR;
532 }
533
534 /* Enable header processing*/
535 hjpeg->Instance->CONFR1 |= JPEG_CONFR1_HDR;
536
537 /* Reset JpegInCount and JpegOutCount */
538 hjpeg->JpegInCount = 0;
539 hjpeg->JpegOutCount = 0;
540
541 /* Change the JPEG state */
542 hjpeg->State = HAL_JPEG_STATE_READY;
543
544 /* Reset the JPEG ErrorCode */
545 hjpeg->ErrorCode = HAL_JPEG_ERROR_NONE;
546
547 /*Clear the context filelds*/
548 hjpeg->Context = 0;
549
550 /* Return function status */
551 return HAL_OK;
552 }
553
554 /**
555 * @brief DeInitializes the JPEG peripheral.
556 * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
557 * the configuration information for JPEG module
558 * @retval HAL status
559 */
560 HAL_StatusTypeDef HAL_JPEG_DeInit(JPEG_HandleTypeDef *hjpeg)
561 {
562 /* Check the JPEG handle allocation */
563 if(hjpeg == NULL)
564 {
565 return HAL_ERROR;
566 }
567
568 /* DeInit the low level hardware: CLOCK, NVIC.*/
569 HAL_JPEG_MspDeInit(hjpeg);
570
571 /* Change the JPEG state */
572 hjpeg->State = HAL_JPEG_STATE_BUSY;
573
574 /* Reset the JPEG ErrorCode */
575 hjpeg->ErrorCode = HAL_JPEG_ERROR_NONE;
576
577 /* Reset JpegInCount and JpegOutCount */
578 hjpeg->JpegInCount = 0;
579 hjpeg->JpegOutCount = 0;
580
581 /* Change the JPEG state */
582 hjpeg->State = HAL_JPEG_STATE_RESET;
583
584 /*Clear the context fields*/
585 hjpeg->Context = 0;
586
587 /* Release Lock */
588 __HAL_UNLOCK(hjpeg);
589
590 /* Return function status */
591 return HAL_OK;
592 }
593
594 /**
595 * @brief Initializes the JPEG MSP.
596 * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
597 * the configuration information for JPEG module
598 * @retval None
599 */
600 __weak void HAL_JPEG_MspInit(JPEG_HandleTypeDef *hjpeg)
601 {
602 /* Prevent unused argument(s) compilation warning */
603 UNUSED(hjpeg);
604
605 /* NOTE : This function Should not be modified, when the callback is needed,
606 the HAL_JPEG_MspInit could be implemented in the user file
607 */
608 }
609
610 /**
611 * @brief DeInitializes JPEG MSP.
612 * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
613 * the configuration information for JPEG module
614 * @retval None
615 */
616 __weak void HAL_JPEG_MspDeInit(JPEG_HandleTypeDef *hjpeg)
617 {
618 /* Prevent unused argument(s) compilation warning */
619 UNUSED(hjpeg);
620
621 /* NOTE : This function Should not be modified, when the callback is needed,
622 the HAL_JPEG_MspDeInit could be implemented in the user file
623 */
624 }
625
626 /**
627 * @}
628 */
629
630 /** @defgroup JPEG_Exported_Functions_Group2 Configuration functions
631 * @brief JPEG Configuration functions.
632 *
633 @verbatim
634 ==============================================================================
635 ##### Configuration functions #####
636 ==============================================================================
637 [..] This section provides functions allowing to:
638 (+) HAL_JPEG_ConfigEncoding() : JPEG encoding configuration
639 (+) HAL_JPEG_GetInfo() : Extract the image configuration from the JPEG header during the decoding
640 (+) HAL_JPEG_EnableHeaderParsing() : Enable JPEG Header parsing for decoding
641 (+) HAL_JPEG_DisableHeaderParsing() : Disable JPEG Header parsing for decoding
642 (+) HAL_JPEG_SetUserQuantTables : Modify the default Quantization tables used for JPEG encoding.
643
644 @endverbatim
645 * @{
646 */
647
648 /**
649 * @brief Set the JPEG encoding configuration.
650 * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
651 * the configuration information for JPEG module
652 * @param pConf: pointer to a JPEG_ConfTypeDef structure that contains
653 * the encoding configuration
654 * @retval HAL status
655 */
656 HAL_StatusTypeDef HAL_JPEG_ConfigEncoding(JPEG_HandleTypeDef *hjpeg, JPEG_ConfTypeDef *pConf)
657 {
658 uint32_t error = HAL_OK;
659 uint32_t numberMCU, hfactor, vfactor,hMCU, vMCU;
660
661 /* Check the JPEG handle allocation */
662 if( (hjpeg == NULL) || (pConf == NULL) )
663 {
664 return HAL_ERROR;
665 }
666 else
667 {
668 /* Check the parameters */
669 assert_param(IS_JPEG_COLORSPACE(pConf->ColorSpace));
670 assert_param(IS_JPEG_CHROMASUBSAMPLING(pConf->ChromaSubsampling));
671 assert_param(IS_JPEG_IMAGE_QUALITY(pConf->ImageQuality));
672
673 /* Process Locked */
674 __HAL_LOCK(hjpeg);
675
676 if(hjpeg->State == HAL_JPEG_STATE_READY)
677 {
678 hjpeg->State = HAL_JPEG_STATE_BUSY;
679
680 hjpeg->Conf.ColorSpace = pConf->ColorSpace;
681 hjpeg->Conf.ChromaSubsampling = pConf->ChromaSubsampling;
682 hjpeg->Conf.ImageHeight = pConf->ImageHeight;
683 hjpeg->Conf.ImageWidth = pConf->ImageWidth;
684 hjpeg->Conf.ImageQuality = pConf->ImageQuality;
685
686 /* Reset the Color Space : by default only one quantization table is used*/
687 hjpeg->Instance->CONFR1 &= ~JPEG_CONFR1_COLORSPACE;
688
689 /* Set Number of color components*/
690 if(hjpeg->Conf.ColorSpace == JPEG_GRAYSCALE_COLORSPACE)
691 {
692 /*Gray Scale is only one component 8x8 blocks i.e 4:4:4*/
693 hjpeg->Conf.ChromaSubsampling = JPEG_444_SUBSAMPLING;
694
695 JPEG_SetColorGrayScale(hjpeg);
696 /* Set quantization table 0*/
697 error = JPEG_Set_Quantization_Mem(hjpeg, hjpeg->QuantTable0, (uint32_t *)(hjpeg->Instance->QMEM0));
698 }
699 else if(hjpeg->Conf.ColorSpace == JPEG_YCBCR_COLORSPACE)
700 {
701 /*
702 Set the Color Space for YCbCr : 2 quantization tables are used
703 one for Luminance(Y) and one for both Chrominances (Cb & Cr)
704 */
705 hjpeg->Instance->CONFR1 |= JPEG_CONFR1_COLORSPACE_0;
706
707 JPEG_SetColorYCBCR(hjpeg);
708
709 /* Set quantization table 0*/
710 error = JPEG_Set_Quantization_Mem(hjpeg, hjpeg->QuantTable0, (uint32_t *)(hjpeg->Instance->QMEM0));
711 /*By default quantization table 0 for component 0 and quantization table 1 for both components 1 and 2*/
712 error |= JPEG_Set_Quantization_Mem(hjpeg, hjpeg->QuantTable1, (uint32_t *)(hjpeg->Instance->QMEM1));
713
714 if((hjpeg->Context & JPEG_CONTEXT_CUSTOM_TABLES) != 0) /*Use user customized quantization tables , 1 table per component*/
715 {
716 /* use 3 quantization tables , one for each component*/
717 hjpeg->Instance->CONFR1 &= (~JPEG_CONFR1_COLORSPACE);
718 hjpeg->Instance->CONFR1 |= JPEG_CONFR1_COLORSPACE_1;
719
720 error |= JPEG_Set_Quantization_Mem(hjpeg, hjpeg->QuantTable2, (uint32_t *)(hjpeg->Instance->QMEM2));
721
722 /*Use Quantization 1 table for component 1*/
723 hjpeg->Instance->CONFR5 &= (~JPEG_CONFR6_QT);
724 hjpeg->Instance->CONFR5 |= JPEG_CONFR5_QT_0;
725
726 /*Use Quantization 2 table for component 2*/
727 hjpeg->Instance->CONFR6 &= (~JPEG_CONFR6_QT);
728 hjpeg->Instance->CONFR6 |= JPEG_CONFR6_QT_1;
729 }
730 }
731 else if(hjpeg->Conf.ColorSpace == JPEG_CMYK_COLORSPACE)
732 {
733 JPEG_SetColorCMYK(hjpeg);
734
735 /* Set quantization table 0*/
736 error = JPEG_Set_Quantization_Mem(hjpeg, hjpeg->QuantTable0, (uint32_t *)(hjpeg->Instance->QMEM0));
737 /*By default quantization table 0 for All components*/
738
739 if((hjpeg->Context & JPEG_CONTEXT_CUSTOM_TABLES) != 0) /*Use user customized quantization tables , 1 table per component*/
740 {
741 /* use 4 quantization tables , one for each component*/
742 hjpeg->Instance->CONFR1 |= JPEG_CONFR1_COLORSPACE;
743
744 error |= JPEG_Set_Quantization_Mem(hjpeg, hjpeg->QuantTable1, (uint32_t *)(hjpeg->Instance->QMEM1));
745 error |= JPEG_Set_Quantization_Mem(hjpeg, hjpeg->QuantTable2, (uint32_t *)(hjpeg->Instance->QMEM2));
746 error |= JPEG_Set_Quantization_Mem(hjpeg, hjpeg->QuantTable3, (uint32_t *)(hjpeg->Instance->QMEM3));
747
748 /*Use Quantization 1 table for component 1*/
749 hjpeg->Instance->CONFR5 |= JPEG_CONFR5_QT_0;
750
751 /*Use Quantization 2 table for component 2*/
752 hjpeg->Instance->CONFR6 |= JPEG_CONFR6_QT_1;
753
754 /*Use Quantization 3 table for component 3*/
755 hjpeg->Instance->CONFR7 |= JPEG_CONFR7_QT;
756 }
757 }
758
759 if(error != HAL_OK)
760 {
761 hjpeg->ErrorCode = HAL_JPEG_ERROR_QUANT_TABLE;
762
763 /* Process Unlocked */
764 __HAL_UNLOCK(hjpeg);
765
766 /* Set the JPEG State to ready */
767 hjpeg->State = HAL_JPEG_STATE_READY;
768
769 return HAL_ERROR;
770 }
771 /* Set the image size*/
772 hjpeg->Instance->CONFR1 |= ((hjpeg->Conf.ImageHeight & 0x0000FFFF) << 16); /* set the number of lines*/
773 hjpeg->Instance->CONFR3 |= ((hjpeg->Conf.ImageWidth & 0x0000FFFF) << 16); /* set the number of pixels per line*/
774
775 if(hjpeg->Conf.ChromaSubsampling == JPEG_420_SUBSAMPLING) /* 4:2:0*/
776 {
777 hfactor = 16;
778 vfactor = 16;
779 }
780 else if(hjpeg->Conf.ChromaSubsampling == JPEG_422_SUBSAMPLING) /* 4:2:2*/
781 {
782 hfactor = 16;
783 vfactor = 8;
784 }
785 else /* Default is 8x8 MCU, 4:4:4*/
786 {
787 hfactor = 8;
788 vfactor = 8;
789 }
790
791 hMCU = (hjpeg->Conf.ImageWidth / hfactor);
792 if((hjpeg->Conf.ImageWidth % hfactor) != 0)
793 {
794 hMCU++; /*+1 for horizontal incomplete MCU */
795 }
796
797 vMCU = (hjpeg->Conf.ImageHeight / vfactor);
798 if((hjpeg->Conf.ImageHeight % vfactor) != 0)
799 {
800 vMCU++; /*+1 for vertical incomplete MCU */
801 }
802
803 numberMCU = (hMCU * vMCU) - 1; /* Bit Field JPEG_CONFR2_NMCU shall be set to NB_MCU - 1*/
804 /* Set the number of MCU*/
805 hjpeg->Instance->CONFR2 = (numberMCU & JPEG_CONFR2_NMCU);
806
807 hjpeg->Context |= JPEG_CONTEXT_CONF_ENCODING;
808
809 /* Process Unlocked */
810 __HAL_UNLOCK(hjpeg);
811
812 /* Set the JPEG State to ready */
813 hjpeg->State = HAL_JPEG_STATE_READY;
814
815 /* Return function status */
816 return HAL_OK;
817 }
818 else
819 {
820 /* Process Unlocked */
821 __HAL_UNLOCK(hjpeg);
822
823 /* Return function status */
824 return HAL_BUSY;
825 }
826 }
827 }
828
829 /**
830 * @brief Extract the image configuration from the JPEG header during the decoding
831 * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
832 * the configuration information for JPEG module
833 * @param pInfo: pointer to a JPEG_ConfTypeDef structure that contains
834 * The JPEG decoded header informations
835 * @retval HAL status
836 */
837 HAL_StatusTypeDef HAL_JPEG_GetInfo(JPEG_HandleTypeDef *hjpeg, JPEG_ConfTypeDef *pInfo)
838 {
839 uint32_t yblockNb, cBblockNb, cRblockNb;
840
841 /* Check the JPEG handle allocation */
842 if((hjpeg == NULL) || (pInfo == NULL))
843 {
844 return HAL_ERROR;
845 }
846
847 /*Read the conf parameters */
848 if((hjpeg->Instance->CONFR1 & JPEG_CONFR1_NF) == JPEG_CONFR1_NF_1)
849 {
850 pInfo->ColorSpace = JPEG_YCBCR_COLORSPACE;
851 }
852 else if((hjpeg->Instance->CONFR1 & JPEG_CONFR1_NF) == 0)
853 {
854 pInfo->ColorSpace = JPEG_GRAYSCALE_COLORSPACE;
855 }
856 else if((hjpeg->Instance->CONFR1 & JPEG_CONFR1_NF) == JPEG_CONFR1_NF)
857 {
858 pInfo->ColorSpace = JPEG_CMYK_COLORSPACE;
859 }
860
861 pInfo->ImageHeight = (hjpeg->Instance->CONFR1 & 0xFFFF0000U) >> 16;
862 pInfo->ImageWidth = (hjpeg->Instance->CONFR3 & 0xFFFF0000U) >> 16;
863
864 if((pInfo->ColorSpace == JPEG_YCBCR_COLORSPACE) || (pInfo->ColorSpace == JPEG_CMYK_COLORSPACE))
865 {
866 yblockNb = (hjpeg->Instance->CONFR4 & JPEG_CONFR4_NB) >> 4;
867 cBblockNb = (hjpeg->Instance->CONFR5 & JPEG_CONFR5_NB) >> 4;
868 cRblockNb = (hjpeg->Instance->CONFR6 & JPEG_CONFR6_NB) >> 4;
869
870 if((yblockNb == 1) && (cBblockNb == 0) && (cRblockNb == 0))
871 {
872 pInfo->ChromaSubsampling = JPEG_422_SUBSAMPLING; /*16x8 block*/
873 }
874 else if((yblockNb == 0) && (cBblockNb == 0) && (cRblockNb == 0))
875 {
876 pInfo->ChromaSubsampling = JPEG_444_SUBSAMPLING;
877 }
878 else if((yblockNb == 3) && (cBblockNb == 0) && (cRblockNb == 0))
879 {
880 pInfo->ChromaSubsampling = JPEG_420_SUBSAMPLING;
881 }
882 else /*Default is 4:4:4*/
883 {
884 pInfo->ChromaSubsampling = JPEG_444_SUBSAMPLING;
885 }
886 }
887 else
888 {
889 pInfo->ChromaSubsampling = JPEG_444_SUBSAMPLING;
890 }
891
892 pInfo->ImageQuality = JPEG_GetQuality(hjpeg);
893
894 /* Return function status */
895 return HAL_OK;
896 }
897
898 /**
899 * @brief Enable JPEG Header parsing for decoding
900 * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
901 * the configuration information for the JPEG.
902 * @retval HAL status
903 */
904 HAL_StatusTypeDef HAL_JPEG_EnableHeaderParsing(JPEG_HandleTypeDef *hjpeg)
905 {
906 /* Process locked */
907 __HAL_LOCK(hjpeg);
908
909 if(hjpeg->State == HAL_JPEG_STATE_READY)
910 {
911 /* Change the JPEG state */
912 hjpeg->State = HAL_JPEG_STATE_BUSY;
913
914 /* Enable header processing*/
915 hjpeg->Instance->CONFR1 |= JPEG_CONFR1_HDR;
916
917 /* Process unlocked */
918 __HAL_UNLOCK(hjpeg);
919
920 /* Change the JPEG state */
921 hjpeg->State = HAL_JPEG_STATE_READY;
922
923 return HAL_OK;
924 }
925 else
926 {
927 /* Process unlocked */
928 __HAL_UNLOCK(hjpeg);
929
930 return HAL_BUSY;
931 }
932 }
933
934 /**
935 * @brief Disable JPEG Header parsing for decoding
936 * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
937 * the configuration information for the JPEG.
938 * @retval HAL status
939 */
940 HAL_StatusTypeDef HAL_JPEG_DisableHeaderParsing(JPEG_HandleTypeDef *hjpeg)
941 {
942 /* Process locked */
943 __HAL_LOCK(hjpeg);
944
945 if(hjpeg->State == HAL_JPEG_STATE_READY)
946 {
947 /* Change the JPEG state */
948 hjpeg->State = HAL_JPEG_STATE_BUSY;
949
950 /* Disable header processing*/
951 hjpeg->Instance->CONFR1 &= ~JPEG_CONFR1_HDR;
952
953 /* Process unlocked */
954 __HAL_UNLOCK(hjpeg);
955
956 /* Change the JPEG state */
957 hjpeg->State = HAL_JPEG_STATE_READY;
958
959 return HAL_OK;
960 }
961 else
962 {
963 /* Process unlocked */
964 __HAL_UNLOCK(hjpeg);
965
966 return HAL_BUSY;
967 }
968 }
969
970 /**
971 * @brief Modify the default Quantization tables used for JPEG encoding.
972 * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
973 * the configuration information for JPEG module
974 * @param QTable0 : pointer to uint8_t , define the user quantification table for color component 1.
975 * If NULL assume no need to update the table and no error return
976 * @param QTable1 : pointer to uint8_t , define the user quantification table for color component 2.
977 * If NULL assume no need to update the table and no error return.
978 * @param QTable2 : pointer to uint8_t , define the user quantification table for color component 3,
979 * If NULL assume no need to update the table and no error return.
980 * @param QTable3 : pointer to uint8_t , define the user quantification table for color component 4.
981 * If NULL assume no need to update the table and no error return.
982 *
983 * @retval HAL status
984 */
985
986
987 HAL_StatusTypeDef HAL_JPEG_SetUserQuantTables(JPEG_HandleTypeDef *hjpeg, uint8_t *QTable0, uint8_t *QTable1, uint8_t *QTable2, uint8_t *QTable3)
988 {
989 /* Process Locked */
990 __HAL_LOCK(hjpeg);
991
992 if(hjpeg->State == HAL_JPEG_STATE_READY)
993 {
994 /* Change the DMA state */
995 hjpeg->State = HAL_JPEG_STATE_BUSY;
996
997 hjpeg->Context |= JPEG_CONTEXT_CUSTOM_TABLES;
998
999 hjpeg->QuantTable0 = QTable0;
1000 hjpeg->QuantTable1 = QTable1;
1001 hjpeg->QuantTable2 = QTable2;
1002 hjpeg->QuantTable3 = QTable3;
1003
1004 /* Process Unlocked */
1005 __HAL_UNLOCK(hjpeg);
1006
1007 /* Change the DMA state */
1008 hjpeg->State = HAL_JPEG_STATE_READY;
1009
1010 /* Return function status */
1011 return HAL_OK;
1012 }
1013 else
1014 {
1015 /* Process Unlocked */
1016 __HAL_UNLOCK(hjpeg);
1017
1018 return HAL_BUSY;
1019 }
1020 }
1021
1022 /**
1023 * @}
1024 */
1025
1026 /** @defgroup JPEG_Exported_Functions_Group3 encoding/decoding processing functions
1027 * @brief processing functions.
1028 *
1029 @verbatim
1030 ==============================================================================
1031 ##### JPEG processing functions #####
1032 ==============================================================================
1033 [..] This section provides functions allowing to:
1034 (+) HAL_JPEG_Encode() : JPEG encoding with polling process
1035 (+) HAL_JPEG_Decode() : JPEG decoding with polling process
1036 (+) HAL_JPEG_Encode_IT() : JPEG encoding with interrupt process
1037 (+) HAL_JPEG_Decode_IT() : JPEG decoding with interrupt process
1038 (+) HAL_JPEG_Encode_DMA() : JPEG encoding with DMA process
1039 (+) HAL_JPEG_Decode_DMA() : JPEG decoding with DMA process
1040 (+) HAL_JPEG_Pause() : Pause the Input/Output processing
1041 (+) HAL_JPEG_Resume() : Resume the JPEG Input/Output processing
1042 (+) HAL_JPEG_ConfigInputBuffer() : Config Encoding/Decoding Input Buffer
1043 (+) HAL_JPEG_ConfigOutputBuffer() : Config Encoding/Decoding Output Buffer
1044 (+) HAL_JPEG_Abort() : Aborts the JPEG Encoding/Decoding
1045
1046 @endverbatim
1047 * @{
1048 */
1049
1050 /**
1051 * @brief Starts JPEG encoding with polling processing
1052 * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
1053 * the configuration information for JPEG module
1054 * @param pDataInMCU: Pointer to the Input buffer
1055 * @param InDataLength: size in bytes Input buffer
1056 * @param pDataOut: Pointer to the jpeg output data buffer
1057 * @param OutDataLength: size in bytes of the Output buffer
1058 * @param Timeout: Specify Timeout value
1059 * @retval HAL status
1060 */
1061 HAL_StatusTypeDef HAL_JPEG_Encode(JPEG_HandleTypeDef *hjpeg, uint8_t *pDataInMCU, uint32_t InDataLength, uint8_t *pDataOut, uint32_t OutDataLength, uint32_t Timeout)
1062 {
1063 uint32_t tickstart = 0;
1064
1065 /* Check the parameters */
1066 assert_param((InDataLength >= 4));
1067 assert_param((OutDataLength >= 4));
1068
1069 /* Check In/out buffer allocation and size */
1070 if((hjpeg == NULL) || (pDataInMCU == NULL) || (pDataOut == NULL) || \
1071 (InDataLength == 0) || (OutDataLength == 0))
1072 {
1073 return HAL_ERROR;
1074 }
1075 /* Process locked */
1076 __HAL_LOCK(hjpeg);
1077
1078 if(hjpeg->State != HAL_JPEG_STATE_READY)
1079 {
1080 /* Process Unlocked */
1081 __HAL_UNLOCK(hjpeg);
1082
1083 return HAL_BUSY;
1084 }
1085
1086 if(hjpeg->State == HAL_JPEG_STATE_READY)
1087 {
1088 if((hjpeg->Context & JPEG_CONTEXT_CONF_ENCODING) == JPEG_CONTEXT_CONF_ENCODING )
1089 {
1090 /*Change JPEG state*/
1091 hjpeg->State = HAL_JPEG_STATE_BUSY_ENCODING;
1092
1093 /*Set the Context to Encode with Polling*/
1094 hjpeg->Context &= ~(JPEG_CONTEXT_OPERATION_MASK | JPEG_CONTEXT_METHOD_MASK);
1095 hjpeg->Context |= (JPEG_CONTEXT_ENCODE | JPEG_CONTEXT_POLLING);
1096
1097 /* Get tick */
1098 tickstart = HAL_GetTick();
1099 /*In/Out Data length must be multiple of 4 Bytes (1 word)*/
1100 InDataLength = InDataLength - (InDataLength % 4);
1101 OutDataLength = OutDataLength - (OutDataLength % 4);
1102
1103 /*Store In/out buffers pointers and size*/
1104 hjpeg->pJpegInBuffPtr = pDataInMCU;
1105 hjpeg->pJpegOutBuffPtr = pDataOut;
1106 hjpeg->InDataLength = InDataLength;
1107 hjpeg->OutDataLength = OutDataLength;
1108
1109 /*Reset In/out data counter */
1110 hjpeg->JpegInCount = 0;
1111 hjpeg->JpegOutCount = 0;
1112
1113 /*Init decoding process*/
1114 JPEG_Init_Process(hjpeg);
1115
1116 /*JPEG data processing : In/Out FIFO transfer*/
1117 while((JPEG_Process(hjpeg) == JPEG_PROCESS_ONGOING))
1118 {
1119 if(Timeout != HAL_MAX_DELAY)
1120 {
1121 if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
1122 {
1123
1124 /* Update error code */
1125 hjpeg->ErrorCode |= HAL_JPEG_ERROR_TIMEOUT;
1126
1127 /* Process Unlocked */
1128 __HAL_UNLOCK(hjpeg);
1129
1130 /*Change JPEG state*/
1131 hjpeg->State= HAL_JPEG_STATE_READY;
1132
1133 return HAL_TIMEOUT;
1134 }
1135 }
1136 }
1137
1138 /* Process Unlocked */
1139 __HAL_UNLOCK(hjpeg);
1140
1141 /*Change JPEG state*/
1142 hjpeg->State= HAL_JPEG_STATE_READY;
1143
1144 }else
1145 {
1146 /* Process Unlocked */
1147 __HAL_UNLOCK(hjpeg);
1148
1149 return HAL_ERROR;
1150 }
1151 }
1152 /* Return function status */
1153 return HAL_OK;
1154 }
1155
1156 /**
1157 * @brief Starts JPEG decoding with polling processing
1158 * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
1159 * the configuration information for JPEG module
1160 * @param pDataIn: Pointer to the input data buffer
1161 * @param InDataLength: size in bytes Input buffer
1162 * @param pDataOutMCU: Pointer to the Output data buffer
1163 * @param OutDataLength: size in bytes of the Output buffer
1164 * @param Timeout: Specify Timeout value
1165 * @retval HAL status
1166 */
1167 HAL_StatusTypeDef HAL_JPEG_Decode(JPEG_HandleTypeDef *hjpeg ,uint8_t *pDataIn ,uint32_t InDataLength ,uint8_t *pDataOutMCU ,uint32_t OutDataLength, uint32_t Timeout)
1168 {
1169 uint32_t tickstart = 0;
1170
1171 /* Check the parameters */
1172 assert_param((InDataLength >= 4));
1173 assert_param((OutDataLength >= 4));
1174
1175 /* Check In/out buffer allocation and size */
1176 if((hjpeg == NULL) || (pDataIn == NULL) || (pDataOutMCU == NULL) || \
1177 (InDataLength == 0) || (OutDataLength == 0))
1178 {
1179 return HAL_ERROR;
1180 }
1181
1182 /* Process Locked */
1183 __HAL_LOCK(hjpeg);
1184
1185 /* Get tick */
1186 tickstart = HAL_GetTick();
1187
1188 if(hjpeg->State == HAL_JPEG_STATE_READY)
1189 {
1190 /*Change JPEG state*/
1191 hjpeg->State = HAL_JPEG_STATE_BUSY_DECODING;
1192
1193 /*Set the Context to Decode with Polling*/
1194 /*Set the Context to Encode with Polling*/
1195 hjpeg->Context &= ~(JPEG_CONTEXT_OPERATION_MASK | JPEG_CONTEXT_METHOD_MASK);
1196 hjpeg->Context |= (JPEG_CONTEXT_DECODE | JPEG_CONTEXT_POLLING);
1197
1198 /*In/Out Data length must be multiple of 4 Bytes (1 word)*/
1199 InDataLength = InDataLength - (InDataLength % 4);
1200 OutDataLength = OutDataLength - (OutDataLength % 4);
1201
1202 /*Store In/out buffers pointers and size*/
1203 hjpeg->pJpegInBuffPtr = pDataIn;
1204 hjpeg->pJpegOutBuffPtr = pDataOutMCU;
1205 hjpeg->InDataLength = InDataLength;
1206 hjpeg->OutDataLength = OutDataLength;
1207
1208 /*Reset In/out data counter */
1209 hjpeg->JpegInCount = 0;
1210 hjpeg->JpegOutCount = 0;
1211
1212 /*Init decoding process*/
1213 JPEG_Init_Process(hjpeg);
1214
1215 /*JPEG data processing : In/Out FIFO transfer*/
1216 while((JPEG_Process(hjpeg) == JPEG_PROCESS_ONGOING))
1217 {
1218 if(Timeout != HAL_MAX_DELAY)
1219 {
1220 if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
1221 {
1222
1223 /* Update error code */
1224 hjpeg->ErrorCode |= HAL_JPEG_ERROR_TIMEOUT;
1225
1226 /* Process Unlocked */
1227 __HAL_UNLOCK(hjpeg);
1228
1229 /*Change JPEG state*/
1230 hjpeg->State= HAL_JPEG_STATE_READY;
1231
1232 return HAL_TIMEOUT;
1233 }
1234 }
1235 }
1236
1237 /* Process Unlocked */
1238 __HAL_UNLOCK(hjpeg);
1239
1240 /*Change JPEG state*/
1241 hjpeg->State= HAL_JPEG_STATE_READY;
1242
1243 }else
1244 {
1245 /* Process Unlocked */
1246 __HAL_UNLOCK(hjpeg);
1247
1248 return HAL_BUSY;
1249 }
1250 /* Return function status */
1251 return HAL_OK;
1252 }
1253
1254 /**
1255 * @brief Starts JPEG encoding with interrupt processing
1256 * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
1257 * the configuration information for JPEG module
1258 * @param pDataInMCU: Pointer to the Input buffer
1259 * @param InDataLength: size in bytes Input buffer
1260 * @param pDataOut: Pointer to the jpeg output data buffer
1261 * @param OutDataLength: size in bytes of the Output buffer
1262 * @retval HAL status
1263 */
1264 HAL_StatusTypeDef HAL_JPEG_Encode_IT(JPEG_HandleTypeDef *hjpeg, uint8_t *pDataInMCU, uint32_t InDataLength, uint8_t *pDataOut, uint32_t OutDataLength)
1265 {
1266 /* Check the parameters */
1267 assert_param((InDataLength >= 4));
1268 assert_param((OutDataLength >= 4));
1269
1270 /* Check In/out buffer allocation and size */
1271 if((hjpeg == NULL) || (pDataInMCU == NULL) || (pDataOut == NULL) || \
1272 (InDataLength == 0) || (OutDataLength == 0))
1273 {
1274 return HAL_ERROR;
1275 }
1276
1277 /* Process Locked */
1278 __HAL_LOCK(hjpeg);
1279
1280 if(hjpeg->State != HAL_JPEG_STATE_READY)
1281 {
1282 /* Process Unlocked */
1283 __HAL_UNLOCK(hjpeg);
1284
1285 return HAL_BUSY;
1286 }
1287 else
1288 {
1289 if((hjpeg->Context & JPEG_CONTEXT_CONF_ENCODING) == JPEG_CONTEXT_CONF_ENCODING )
1290 {
1291 /*Change JPEG state*/
1292 hjpeg->State = HAL_JPEG_STATE_BUSY_ENCODING;
1293
1294 /*Set the Context to Encode with IT*/
1295 hjpeg->Context &= ~(JPEG_CONTEXT_OPERATION_MASK | JPEG_CONTEXT_METHOD_MASK);
1296 hjpeg->Context |= (JPEG_CONTEXT_ENCODE | JPEG_CONTEXT_IT);
1297
1298 /*In/Out Data length must be multiple of 4 Bytes (1 word)*/
1299 InDataLength = InDataLength - (InDataLength % 4);
1300 OutDataLength = OutDataLength - (OutDataLength % 4);
1301
1302 /*Store In/out buffers pointers and size*/
1303 hjpeg->pJpegInBuffPtr = pDataInMCU;
1304 hjpeg->pJpegOutBuffPtr = pDataOut;
1305 hjpeg->InDataLength = InDataLength;
1306 hjpeg->OutDataLength = OutDataLength;
1307
1308 /*Reset In/out data counter */
1309 hjpeg->JpegInCount = 0;
1310 hjpeg->JpegOutCount = 0;
1311
1312 /*Init decoding process*/
1313 JPEG_Init_Process(hjpeg);
1314
1315 }
1316 else
1317 {
1318 /* Process Unlocked */
1319 __HAL_UNLOCK(hjpeg);
1320
1321 return HAL_ERROR;
1322 }
1323 }
1324 /* Return function status */
1325 return HAL_OK;
1326 }
1327
1328 /**
1329 * @brief Starts JPEG decoding with interrupt processing
1330 * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
1331 * the configuration information for JPEG module
1332 * @param pDataIn: Pointer to the input data buffer
1333 * @param InDataLength: size in bytes Input buffer
1334 * @param pDataOutMCU: Pointer to the Output data buffer
1335 * @param OutDataLength: size in bytes of the Output buffer
1336 * @retval HAL status
1337 */
1338 HAL_StatusTypeDef HAL_JPEG_Decode_IT(JPEG_HandleTypeDef *hjpeg ,uint8_t *pDataIn ,uint32_t InDataLength ,uint8_t *pDataOutMCU ,uint32_t OutDataLength)
1339 {
1340 /* Check the parameters */
1341 assert_param((InDataLength >= 4));
1342 assert_param((OutDataLength >= 4));
1343
1344 /* Check In/out buffer allocation and size */
1345 if((hjpeg == NULL) || (pDataIn == NULL) || (pDataOutMCU == NULL) || \
1346 (InDataLength == 0) || (OutDataLength == 0))
1347 {
1348 return HAL_ERROR;
1349 }
1350
1351 /* Process Locked */
1352 __HAL_LOCK(hjpeg);
1353
1354 if(hjpeg->State == HAL_JPEG_STATE_READY)
1355 {
1356 /*Change JPEG state*/
1357 hjpeg->State = HAL_JPEG_STATE_BUSY_DECODING;
1358
1359 /*Set the Context to Decode with IT*/
1360 hjpeg->Context &= ~(JPEG_CONTEXT_OPERATION_MASK | JPEG_CONTEXT_METHOD_MASK);
1361 hjpeg->Context |= (JPEG_CONTEXT_DECODE | JPEG_CONTEXT_IT);
1362
1363 /*In/Out Data length must be multiple of 4 Bytes (1 word)*/
1364 InDataLength = InDataLength - (InDataLength % 4);
1365 OutDataLength = OutDataLength - (OutDataLength % 4);
1366
1367 /*Store In/out buffers pointers and size*/
1368 hjpeg->pJpegInBuffPtr = pDataIn;
1369 hjpeg->pJpegOutBuffPtr = pDataOutMCU;
1370 hjpeg->InDataLength = InDataLength;
1371 hjpeg->OutDataLength = OutDataLength;
1372
1373 /*Reset In/out data counter */
1374 hjpeg->JpegInCount = 0;
1375 hjpeg->JpegOutCount = 0;
1376
1377 /*Init decoding process*/
1378 JPEG_Init_Process(hjpeg);
1379
1380 }
1381 else
1382 {
1383 /* Process Unlocked */
1384 __HAL_UNLOCK(hjpeg);
1385
1386 return HAL_BUSY;
1387 }
1388 /* Return function status */
1389 return HAL_OK;
1390 }
1391
1392 /**
1393 * @brief Starts JPEG encoding with DMA processing
1394 * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
1395 * the configuration information for JPEG module
1396 * @param pDataInMCU: Pointer to the Input buffer
1397 * @param InDataLength: size in bytes Input buffer
1398 * @param pDataOut: Pointer to the jpeg output data buffer
1399 * @param OutDataLength: size in bytes of the Output buffer
1400 * @retval HAL status
1401 */
1402 HAL_StatusTypeDef HAL_JPEG_Encode_DMA(JPEG_HandleTypeDef *hjpeg, uint8_t *pDataInMCU, uint32_t InDataLength, uint8_t *pDataOut, uint32_t OutDataLength)
1403 {
1404 /* Check the parameters */
1405 assert_param((InDataLength >= 4));
1406 assert_param((OutDataLength >= 4));
1407
1408 /* Check In/out buffer allocation and size */
1409 if((hjpeg == NULL) || (pDataInMCU == NULL) || (pDataOut == NULL) || \
1410 (InDataLength == 0) || (OutDataLength == 0))
1411 {
1412 return HAL_ERROR;
1413 }
1414
1415 /* Process Locked */
1416 __HAL_LOCK(hjpeg);
1417
1418 if(hjpeg->State != HAL_JPEG_STATE_READY)
1419 {
1420 /* Process Unlocked */
1421 __HAL_UNLOCK(hjpeg);
1422
1423 return HAL_BUSY;
1424 }
1425 else
1426 {
1427 if((hjpeg->Context & JPEG_CONTEXT_CONF_ENCODING) == JPEG_CONTEXT_CONF_ENCODING )
1428 {
1429 /*Change JPEG state*/
1430 hjpeg->State = HAL_JPEG_STATE_BUSY_ENCODING;
1431
1432 /*Set the Context to Encode with DMA*/
1433 hjpeg->Context &= ~(JPEG_CONTEXT_OPERATION_MASK | JPEG_CONTEXT_METHOD_MASK);
1434 hjpeg->Context |= (JPEG_CONTEXT_ENCODE | JPEG_CONTEXT_DMA);
1435
1436 /*Store In/out buffers pointers and size*/
1437 hjpeg->pJpegInBuffPtr = pDataInMCU;
1438 hjpeg->pJpegOutBuffPtr = pDataOut;
1439 hjpeg->InDataLength = InDataLength;
1440 hjpeg->OutDataLength = OutDataLength;
1441
1442 /*Reset In/out data counter */
1443 hjpeg->JpegInCount = 0;
1444 hjpeg->JpegOutCount = 0;
1445
1446 /*Init decoding process*/
1447 JPEG_Init_Process(hjpeg);
1448
1449 /* JPEG encoding process using DMA */
1450 JPEG_DMA_StartProcess(hjpeg);
1451
1452 }
1453 else
1454 {
1455 /* Process Unlocked */
1456 __HAL_UNLOCK(hjpeg);
1457
1458 return HAL_ERROR;
1459 }
1460 }
1461 /* Return function status */
1462 return HAL_OK;
1463 }
1464
1465 /**
1466 * @brief Starts JPEG decoding with DMA processing
1467 * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
1468 * the configuration information for JPEG module
1469 * @param pDataIn: Pointer to the input data buffer
1470 * @param InDataLength: size in bytes Input buffer
1471 * @param pDataOutMCU: Pointer to the Output data buffer
1472 * @param OutDataLength: size in bytes of the Output buffer
1473 * @retval HAL status
1474 */
1475 HAL_StatusTypeDef HAL_JPEG_Decode_DMA(JPEG_HandleTypeDef *hjpeg ,uint8_t *pDataIn ,uint32_t InDataLength ,uint8_t *pDataOutMCU ,uint32_t OutDataLength)
1476 {
1477 /* Check the parameters */
1478 assert_param((InDataLength >= 4));
1479 assert_param((OutDataLength >= 4));
1480
1481 /* Check In/out buffer allocation and size */
1482 if((hjpeg == NULL) || (pDataIn == NULL) || (pDataOutMCU == NULL) || \
1483 (InDataLength == 0) || (OutDataLength == 0))
1484 {
1485 return HAL_ERROR;
1486 }
1487
1488 /* Process Locked */
1489 __HAL_LOCK(hjpeg);
1490
1491 if(hjpeg->State == HAL_JPEG_STATE_READY)
1492 {
1493 /*Change JPEG state*/
1494 hjpeg->State = HAL_JPEG_STATE_BUSY_DECODING;
1495
1496 /*Set the Context to Decode with DMA*/
1497 hjpeg->Context &= ~(JPEG_CONTEXT_OPERATION_MASK | JPEG_CONTEXT_METHOD_MASK);
1498 hjpeg->Context |= (JPEG_CONTEXT_DECODE | JPEG_CONTEXT_DMA);
1499
1500 /*Store In/out buffers pointers and size*/
1501 hjpeg->pJpegInBuffPtr = pDataIn;
1502 hjpeg->pJpegOutBuffPtr = pDataOutMCU;
1503 hjpeg->InDataLength = InDataLength;
1504 hjpeg->OutDataLength = OutDataLength;
1505
1506 /*Reset In/out data counter */
1507 hjpeg->JpegInCount = 0;
1508 hjpeg->JpegOutCount = 0;
1509
1510 /*Init decoding process*/
1511 JPEG_Init_Process(hjpeg);
1512
1513 /* JPEG decoding process using DMA */
1514 JPEG_DMA_StartProcess(hjpeg);
1515
1516 }
1517 else
1518 {
1519 /* Process Unlocked */
1520 __HAL_UNLOCK(hjpeg);
1521
1522 return HAL_BUSY;
1523 }
1524 /* Return function status */
1525 return HAL_OK;
1526 }
1527
1528 /**
1529 * @brief Pause the JPEG Input/Output processing
1530 * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
1531 * the configuration information for JPEG module
1532 * @param XferSelection: This parameter can be one of the following values :
1533 * JPEG_PAUSE_RESUME_INPUT : Pause Input processing
1534 * JPEG_PAUSE_RESUME_OUTPUT: Pause Output processing
1535 * JPEG_PAUSE_RESUME_INPUT_OUTPUT: Pause Input and Output processing
1536 * @retval HAL status
1537 */
1538 HAL_StatusTypeDef HAL_JPEG_Pause(JPEG_HandleTypeDef *hjpeg, uint32_t XferSelection)
1539 {
1540 uint32_t mask = 0;
1541
1542 assert_param(IS_JPEG_PAUSE_RESUME_STATE(XferSelection));
1543
1544 if((hjpeg->Context & JPEG_CONTEXT_METHOD_MASK) == JPEG_CONTEXT_DMA)
1545 {
1546 if((XferSelection & JPEG_PAUSE_RESUME_INPUT) == JPEG_PAUSE_RESUME_INPUT)
1547 {
1548 hjpeg->Context |= JPEG_CONTEXT_PAUSE_INPUT;
1549 mask |= JPEG_DMA_IDMA;
1550 }
1551 if((XferSelection & JPEG_PAUSE_RESUME_OUTPUT) == JPEG_PAUSE_RESUME_OUTPUT)
1552 {
1553 hjpeg->Context |= JPEG_CONTEXT_PAUSE_OUTPUT;
1554 mask |= JPEG_DMA_ODMA;
1555 }
1556 JPEG_DISABLE_DMA(hjpeg,mask);
1557
1558 }
1559 else if((hjpeg->Context & JPEG_CONTEXT_METHOD_MASK) == JPEG_CONTEXT_IT)
1560 {
1561
1562 if((XferSelection & JPEG_PAUSE_RESUME_INPUT) == JPEG_PAUSE_RESUME_INPUT)
1563 {
1564 hjpeg->Context |= JPEG_CONTEXT_PAUSE_INPUT;
1565 mask |= (JPEG_IT_IFT | JPEG_IT_IFNF);
1566 }
1567 if((XferSelection & JPEG_PAUSE_RESUME_OUTPUT) == JPEG_PAUSE_RESUME_OUTPUT)
1568 {
1569 hjpeg->Context |= JPEG_CONTEXT_PAUSE_OUTPUT;
1570 mask |= (JPEG_IT_OFT | JPEG_IT_OFNE | JPEG_IT_EOC);
1571 }
1572 __HAL_JPEG_DISABLE_IT(hjpeg,mask);
1573
1574 }
1575
1576 /* Return function status */
1577 return HAL_OK;
1578 }
1579
1580 /**
1581 * @brief Resume the JPEG Input/Output processing
1582 * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
1583 * the configuration information for JPEG module
1584 * @param XferSelection: This parameter can be one of the following values :
1585 * JPEG_PAUSE_RESUME_INPUT : Resume Input processing
1586 * JPEG_PAUSE_RESUME_OUTPUT: Resume Output processing
1587 * JPEG_PAUSE_RESUME_INPUT_OUTPUT: Resume Input and Output processing
1588 * @retval HAL status
1589 */
1590 HAL_StatusTypeDef HAL_JPEG_Resume(JPEG_HandleTypeDef *hjpeg, uint32_t XferSelection)
1591 {
1592 uint32_t mask = 0;
1593
1594 assert_param(IS_JPEG_PAUSE_RESUME_STATE(XferSelection));
1595
1596 if((hjpeg->Context & JPEG_CONTEXT_METHOD_MASK) == JPEG_CONTEXT_DMA)
1597 {
1598
1599 if((XferSelection & JPEG_PAUSE_RESUME_INPUT) == JPEG_PAUSE_RESUME_INPUT)
1600 {
1601 hjpeg->Context &= (~JPEG_CONTEXT_PAUSE_INPUT);
1602 mask |= JPEG_DMA_IDMA;
1603
1604 /*JPEG Input DMA transfer data number must be multiple of DMA buffer size
1605 as the destination is a 32 bits register */
1606 hjpeg->InDataLength = hjpeg->InDataLength - (hjpeg->InDataLength % 4);
1607
1608 if(hjpeg->InDataLength > 0)
1609 {
1610 /* Start DMA FIFO In transfer */
1611 HAL_DMA_Start_IT(hjpeg->hdmain, (uint32_t)hjpeg->pJpegInBuffPtr, (uint32_t)&hjpeg->Instance->DIR, hjpeg->InDataLength >> 2);
1612 }
1613
1614 }
1615 if((XferSelection & JPEG_PAUSE_RESUME_OUTPUT) == JPEG_PAUSE_RESUME_OUTPUT)
1616 {
1617 hjpeg->Context &= (~JPEG_CONTEXT_PAUSE_OUTPUT);
1618 mask |= JPEG_DMA_ODMA;
1619
1620 /* Start DMA FIFO Out transfer */
1621 HAL_DMA_Start_IT(hjpeg->hdmaout, (uint32_t)&hjpeg->Instance->DOR, (uint32_t)hjpeg->pJpegOutBuffPtr, hjpeg->OutDataLength >> 2);
1622 }
1623 JPEG_ENABLE_DMA(hjpeg,mask);
1624
1625 }
1626 else if((hjpeg->Context & JPEG_CONTEXT_METHOD_MASK) == JPEG_CONTEXT_IT)
1627 {
1628 if((XferSelection & JPEG_PAUSE_RESUME_INPUT) == JPEG_PAUSE_RESUME_INPUT)
1629 {
1630 hjpeg->Context &= (~JPEG_CONTEXT_PAUSE_INPUT);
1631 mask |= (JPEG_IT_IFT | JPEG_IT_IFNF);
1632 }
1633 if((XferSelection & JPEG_PAUSE_RESUME_OUTPUT) == JPEG_PAUSE_RESUME_OUTPUT)
1634 {
1635 hjpeg->Context &= (~JPEG_CONTEXT_PAUSE_OUTPUT);
1636 mask |= (JPEG_IT_OFT | JPEG_IT_OFNE | JPEG_IT_EOC);
1637 }
1638 __HAL_JPEG_ENABLE_IT(hjpeg,mask);
1639
1640 }
1641
1642 /* Return function status */
1643 return HAL_OK;
1644 }
1645
1646 /**
1647 * @brief Config Encoding/Decoding Input Buffer.
1648 * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
1649 * the configuration information for JPEG module.
1650 * @param pNewInputBuffer: Pointer to the new input data buffer
1651 * @param InDataLength: Size in bytes of the new Input data buffer
1652 * @retval HAL status
1653 */
1654 void HAL_JPEG_ConfigInputBuffer(JPEG_HandleTypeDef *hjpeg, uint8_t *pNewInputBuffer, uint32_t InDataLength)
1655 {
1656 hjpeg->pJpegInBuffPtr = pNewInputBuffer;
1657 hjpeg->InDataLength = InDataLength;
1658 }
1659
1660 /**
1661 * @brief Config Encoding/Decoding Output Buffer.
1662 * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
1663 * the configuration information for JPEG module.
1664 * @param pNewOutputBuffer: Pointer to the new output data buffer
1665 * @param OutDataLength: Size in bytes of the new Output data buffer
1666 * @retval HAL status
1667 */
1668 void HAL_JPEG_ConfigOutputBuffer(JPEG_HandleTypeDef *hjpeg, uint8_t *pNewOutputBuffer, uint32_t OutDataLength)
1669 {
1670 hjpeg->pJpegOutBuffPtr = pNewOutputBuffer;
1671 hjpeg->OutDataLength = OutDataLength;
1672 }
1673
1674 /**
1675 * @brief Aborts the JPEG Encoding/Decoding.
1676 * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
1677 * the configuration information for JPEG module
1678 * @retval HAL status
1679 */
1680 HAL_StatusTypeDef HAL_JPEG_Abort(JPEG_HandleTypeDef *hjpeg)
1681 {
1682 uint32_t tickstart, tmpContext;
1683
1684 tmpContext = hjpeg->Context;
1685
1686 /*Reset the Context operation and method*/
1687 hjpeg->Context &= ~(JPEG_CONTEXT_OPERATION_MASK | JPEG_CONTEXT_METHOD_MASK | JPEG_CONTEXT_ENDING_DMA);
1688
1689 if((tmpContext & JPEG_CONTEXT_METHOD_MASK) == JPEG_CONTEXT_DMA)
1690 {
1691 /* Stop the DMA In/out Xfer*/
1692 HAL_DMA_Abort(hjpeg->hdmaout);
1693 HAL_DMA_Abort(hjpeg->hdmain);
1694 }
1695
1696 /* Stop the JPEG encoding/decoding process*/
1697 hjpeg->Instance->CONFR0 &= ~JPEG_CONFR0_START;
1698
1699 /* Get tick */
1700 tickstart = HAL_GetTick();
1701
1702 /* Check if the JPEG Codec is effectively disabled */
1703 while(__HAL_JPEG_GET_FLAG(hjpeg, JPEG_FLAG_COF) != RESET)
1704 {
1705 /* Check for the Timeout */
1706 if((HAL_GetTick() - tickstart ) > JPEG_TIMEOUT_VALUE)
1707 {
1708 /* Update error code */
1709 hjpeg->ErrorCode |= HAL_JPEG_ERROR_TIMEOUT;
1710
1711 /* Change the DMA state */
1712 hjpeg->State = HAL_JPEG_STATE_TIMEOUT;
1713
1714 /* Process Unlocked */
1715 __HAL_UNLOCK(hjpeg);
1716
1717 return HAL_TIMEOUT;
1718 }
1719 }
1720
1721 /* Disable All Interrupts */
1722 __HAL_JPEG_DISABLE_IT(hjpeg,JPEG_INTERRUPT_MASK);
1723
1724 /* Disable All DMA requests */
1725 JPEG_DISABLE_DMA(hjpeg,JPEG_DMA_MASK);
1726
1727 /* Flush input and output FIFOs*/
1728 hjpeg->Instance->CR |= JPEG_CR_IFF;
1729 hjpeg->Instance->CR |= JPEG_CR_OFF;
1730
1731 /* Clear all flags */
1732 __HAL_JPEG_CLEAR_FLAG(hjpeg,JPEG_FLAG_ALL);
1733
1734 /* Reset JpegInCount and JpegOutCount */
1735 hjpeg->JpegInCount = 0;
1736 hjpeg->JpegOutCount = 0;
1737
1738 /*Reset the Context Pause*/
1739 hjpeg->Context &= ~(JPEG_CONTEXT_PAUSE_INPUT | JPEG_CONTEXT_PAUSE_OUTPUT);
1740
1741 /* Change the DMA state*/
1742 hjpeg->State = HAL_JPEG_STATE_READY;
1743
1744 /* Process Unlocked */
1745 __HAL_UNLOCK(hjpeg);
1746
1747 /* Return function status */
1748 return HAL_OK;
1749 }
1750
1751
1752 /**
1753 * @}
1754 */
1755
1756 /** @defgroup JPEG_Exported_Functions_Group4 JPEG Decode/Encode callback functions
1757 * @brief JPEG process callback functions.
1758 *
1759 @verbatim
1760 ==============================================================================
1761 ##### JPEG Decode/Encode callback functions #####
1762 ==============================================================================
1763 [..] This section provides callback functions:
1764 (+) HAL_JPEG_InfoReadyCallback() : Decoding JPEG Info ready callback
1765 (+) HAL_JPEG_EncodeCpltCallback() : Encoding complete callback.
1766 (+) HAL_JPEG_DecodeCpltCallback() : Decoding complete callback.
1767 (+) HAL_JPEG_ErrorCallback() : JPEG error callback.
1768 (+) HAL_JPEG_GetDataCallback() : Get New Data chunk callback.
1769 (+) HAL_JPEG_DataReadyCallback() : Decoded/Encoded Data ready callback.
1770
1771 @endverbatim
1772 * @{
1773 */
1774
1775 /**
1776 * @brief Decoding JPEG Info ready callback.
1777 * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
1778 * the configuration information for JPEG module
1779 * @param pInfo: pointer to a JPEG_ConfTypeDef structure that contains
1780 * The JPEG decoded header informations
1781 * @retval None
1782 */
1783 __weak void HAL_JPEG_InfoReadyCallback(JPEG_HandleTypeDef *hjpeg,JPEG_ConfTypeDef *pInfo)
1784 {
1785 /* Prevent unused argument(s) compilation warning */
1786 UNUSED(hjpeg);
1787 UNUSED(pInfo);
1788
1789 /* NOTE : This function Should not be modified, when the callback is needed,
1790 the HAL_JPEG_HeaderParsingCpltCallback could be implemented in the user file
1791 */
1792 }
1793
1794 /**
1795 * @brief Encoding complete callback.
1796 * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
1797 * the configuration information for JPEG module
1798 * @retval None
1799 */
1800 __weak void HAL_JPEG_EncodeCpltCallback(JPEG_HandleTypeDef *hjpeg)
1801 {
1802 /* Prevent unused argument(s) compilation warning */
1803 UNUSED(hjpeg);
1804
1805 /* NOTE : This function Should not be modified, when the callback is needed,
1806 the HAL_JPEG_EncodeCpltCallback could be implemented in the user file
1807 */
1808 }
1809
1810 /**
1811 * @brief Decoding complete callback.
1812 * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
1813 * the configuration information for JPEG module
1814 * @retval None
1815 */
1816 __weak void HAL_JPEG_DecodeCpltCallback(JPEG_HandleTypeDef *hjpeg)
1817 {
1818 /* Prevent unused argument(s) compilation warning */
1819 UNUSED(hjpeg);
1820
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
1823 */
1824 }
1825
1826 /**
1827 * @brief JPEG error callback.
1828 * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
1829 * the configuration information for JPEG module
1830 * @retval None
1831 */
1832 __weak void HAL_JPEG_ErrorCallback(JPEG_HandleTypeDef *hjpeg)
1833 {
1834 /* Prevent unused argument(s) compilation warning */
1835 UNUSED(hjpeg);
1836
1837 /* NOTE : This function Should not be modified, when the callback is needed,
1838 the HAL_JPEG_ErrorCallback could be implemented in the user file
1839 */
1840 }
1841
1842 /**
1843 * @brief Get New Data chunk callback.
1844 * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
1845 * the configuration information for JPEG module
1846 * @param NbDecodedData: Number of consummed data in the previous chunk in bytes
1847 * @retval None
1848 */
1849 __weak void HAL_JPEG_GetDataCallback(JPEG_HandleTypeDef *hjpeg, uint32_t NbDecodedData)
1850 {
1851 /* Prevent unused argument(s) compilation warning */
1852 UNUSED(hjpeg);
1853 UNUSED(NbDecodedData);
1854
1855 /* NOTE : This function Should not be modified, when the callback is needed,
1856 the HAL_JPEG_GetDataCallback could be implemented in the user file
1857 */
1858 }
1859
1860 /**
1861 * @brief Decoded/Encoded Data ready callback.
1862 * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
1863 * the configuration information for JPEG module
1864 * @param pDataOut: pointer to the output data buffer
1865 * @param OutDataLength: number in bytes of data available in the specified output buffer
1866 * @retval None
1867 */
1868 __weak void HAL_JPEG_DataReadyCallback (JPEG_HandleTypeDef *hjpeg, uint8_t *pDataOut, uint32_t OutDataLength)
1869 {
1870 /* Prevent unused argument(s) compilation warning */
1871 UNUSED(hjpeg);
1872 UNUSED(pDataOut);
1873 UNUSED(OutDataLength);
1874
1875 /* NOTE : This function Should not be modified, when the callback is needed,
1876 the HAL_JPEG_DataReadyCallback could be implemented in the user file
1877 */
1878 }
1879
1880 /**
1881 * @}
1882 */
1883
1884
1885 /** @defgroup JPEG_Exported_Functions_Group5 JPEG IRQ handler management
1886 * @brief JPEG IRQ handler.
1887 *
1888 @verbatim
1889 ==============================================================================
1890 ##### JPEG IRQ handler management #####
1891 ==============================================================================
1892 [..] This section provides JPEG IRQ handler function.
1893 (+) HAL_JPEG_IRQHandler() : handles JPEG interrupt request
1894
1895 @endverbatim
1896 * @{
1897 */
1898
1899 /**
1900 * @brief This function handles JPEG interrupt request.
1901 * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
1902 * the configuration information for JPEG module
1903 * @retval None
1904 */
1905 void HAL_JPEG_IRQHandler(JPEG_HandleTypeDef *hjpeg)
1906 {
1907 switch(hjpeg->State)
1908 {
1909 case HAL_JPEG_STATE_BUSY_ENCODING:
1910 case HAL_JPEG_STATE_BUSY_DECODING:
1911 /* continue JPEG data encoding/Decoding*/
1912 /* JPEG data processing : In/Out FIFO transfer*/
1913 if((hjpeg->Context & JPEG_CONTEXT_METHOD_MASK) == JPEG_CONTEXT_IT)
1914 {
1915 JPEG_Process(hjpeg);
1916 }
1917 else if((hjpeg->Context & JPEG_CONTEXT_METHOD_MASK) == JPEG_CONTEXT_DMA)
1918 {
1919 JPEG_DMA_ContinueProcess(hjpeg);
1920
1921 }
1922
1923 break;
1924
1925 default:
1926 break;
1927 }
1928 }
1929
1930 /**
1931 * @}
1932 */
1933
1934 /** @defgroup JPEG_Exported_Functions_Group6 Peripheral State functions
1935 * @brief Peripheral State functions.
1936 *
1937 @verbatim
1938 ==============================================================================
1939 ##### Peripheral State and Error functions #####
1940 ==============================================================================
1941 [..] This section provides JPEG State and Errors function.
1942 (+) HAL_JPEG_GetState() : permits to get in run-time the JPEG state.
1943 (+) HAL_JPEG_GetError() : Returns the JPEG error code if any.
1944
1945 @endverbatim
1946 * @{
1947 */
1948
1949 /**
1950 * @brief Returns the JPEG state.
1951 * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
1952 * the configuration information for JPEG module
1953 * @retval JPEG state
1954 */
1955 HAL_JPEG_STATETypeDef HAL_JPEG_GetState(JPEG_HandleTypeDef *hjpeg)
1956 {
1957 return hjpeg->State;
1958 }
1959
1960 /**
1961 * @brief Return the JPEG error code
1962 * @param hjpeg : pointer to a JPEG_HandleTypeDef structure that contains
1963 * the configuration information for the specified JPEG.
1964 * @retval JPEG Error Code
1965 */
1966 uint32_t HAL_JPEG_GetError(JPEG_HandleTypeDef *hjpeg)
1967 {
1968 return hjpeg->ErrorCode;
1969 }
1970
1971 /**
1972 * @}
1973 */
1974
1975 /**
1976 * @}
1977 */
1978
1979
1980 /** @addtogroup JPEG_Private_Functions
1981 * @{
1982 */
1983
1984 /**
1985 * @brief Generates Huffman sizes/Codes Table from Bits/vals Table
1986 * @param Bits: pointer to bits table
1987 * @param Huffsize: pointer to sizes table
1988 * @param Huffcode: pointer to codes table
1989 * @param LastK: pointer to last Coeff (table dimmension)
1990 * @retval HAL status
1991 */
1992 static HAL_StatusTypeDef JPEG_Bits_To_SizeCodes(uint8_t *Bits, uint8_t *Huffsize, uint32_t *Huffcode, uint32_t *LastK)
1993 {
1994 uint32_t i, p, l, code, si;
1995
1996 /* Figure C.1 \96 Generation of table of Huffman code sizes */
1997 p = 0;
1998 for (l = 0; l < 16; l++)
1999 {
2000 i = (uint32_t)Bits[l];
2001 if ( (p + i) > 256)
2002 { /* check for table overflow */
2003 return HAL_ERROR;
2004 }
2005 while (i != 0)
2006 {
2007 Huffsize[p++] = (uint8_t) l+1;
2008 i--;
2009 }
2010 }
2011 Huffsize[p] = 0;
2012 *LastK = p;
2013
2014 /* Figure C.2 \96 Generation of table of Huffman codes */
2015 code = 0;
2016 si = Huffsize[0];
2017 p = 0;
2018 while (Huffsize[p] != 0)
2019 {
2020 while (((uint32_t) Huffsize[p]) == si)
2021 {
2022 Huffcode[p++] = code;
2023 code++;
2024 }
2025 /* code must fit in "size" bits (si), no code is allowed to be all ones*/
2026 if (((uint32_t) code) >= (((uint32_t) 1) << si))
2027 {
2028 return HAL_ERROR;
2029 }
2030 code <<= 1;
2031 si++;
2032 }
2033 /* Return function status */
2034 return HAL_OK;
2035 }
2036
2037 /**
2038 * @brief Transform a Bits/Vals AC Huffman table to sizes/Codes huffman Table
2039 * that can programmed to the JPEG encoder registers
2040 * @param AC_BitsValsTable: pointer to AC huffman bits/vals table
2041 * @param AC_SizeCodesTable: pointer to AC huffman Sizes/Codes table
2042 * @retval HAL status
2043 */
2044 static HAL_StatusTypeDef JPEG_ACHuff_BitsVals_To_SizeCodes(JPEG_ACHuffTableTypeDef *AC_BitsValsTable, JPEG_AC_HuffCodeTableTypeDef *AC_SizeCodesTable)
2045 {
2046 HAL_StatusTypeDef error;
2047 uint8_t huffsize[257];
2048 uint32_t huffcode[257];
2049 uint32_t k;
2050 uint32_t l,lsb, msb;
2051 uint32_t lastK;
2052
2053 error = JPEG_Bits_To_SizeCodes(AC_BitsValsTable->Bits, huffsize, huffcode, &lastK);
2054 if(error != HAL_OK)
2055 {
2056 return error;
2057 }
2058
2059 /* Figure C.3 \96 Ordering procedure for encoding procedure code tables */
2060 k=0;
2061
2062 while(k < lastK)
2063 {
2064 l = AC_BitsValsTable->HuffVal[k];
2065 if(l == 0)
2066 {
2067 l = 160; /*l = 0x00 EOB code*/
2068 }
2069 else if(l == 0xF0)/* l = 0xF0 ZRL code*/
2070 {
2071 l = 161;
2072 }
2073 else
2074 {
2075 msb = (l & 0xF0) >> 4;
2076 lsb = (l & 0x0F);
2077 l = (msb * 10) + lsb - 1;
2078 }
2079 if(l >= JPEG_AC_HUFF_TABLE_SIZE)
2080 {
2081 return HAL_ERROR; /* Huffman Table overflow error*/
2082 }
2083 else
2084 {
2085 AC_SizeCodesTable->HuffmanCode[l] = huffcode[k];
2086 AC_SizeCodesTable->CodeLength[l] = huffsize[k] - 1;
2087 k++;
2088 }
2089 }
2090
2091 /* Return function status */
2092 return HAL_OK;
2093 }
2094
2095 /**
2096 * @brief Transform a Bits/Vals DC Huffman table to sizes/Codes huffman Table
2097 * that can programmed to the JPEG encoder registers
2098 * @param DC_BitsValsTable: pointer to DC huffman bits/vals table
2099 * @param DC_SizeCodesTable: pointer to DC huffman Sizes/Codes table
2100 * @retval HAL status
2101 */
2102 static HAL_StatusTypeDef JPEG_DCHuff_BitsVals_To_SizeCodes(JPEG_DCHuffTableTypeDef *DC_BitsValsTable, JPEG_DC_HuffCodeTableTypeDef *DC_SizeCodesTable)
2103 {
2104 HAL_StatusTypeDef error;
2105
2106 uint32_t k;
2107 uint32_t l;
2108 uint32_t lastK;
2109 uint8_t huffsize[257];
2110 uint32_t huffcode[257];
2111 error = JPEG_Bits_To_SizeCodes(DC_BitsValsTable->Bits, huffsize, huffcode, &lastK);
2112 if(error != HAL_OK)
2113 {
2114 return error;
2115 }
2116 /* Figure C.3: ordering procedure for encoding procedure code tables */
2117 k=0;
2118
2119 while(k < lastK)
2120 {
2121 l = DC_BitsValsTable->HuffVal[k];
2122 if(l >= JPEG_DC_HUFF_TABLE_SIZE)
2123 {
2124 return HAL_ERROR; /* Huffman Table overflow error*/
2125 }
2126 else
2127 {
2128 DC_SizeCodesTable->HuffmanCode[l] = huffcode[k];
2129 DC_SizeCodesTable->CodeLength[l] = huffsize[k] - 1;
2130 k++;
2131 }
2132 }
2133
2134 /* Return function status */
2135 return HAL_OK;
2136 }
2137
2138 /**
2139 * @brief Set the JPEG register with an DC huffman table at the given DC table address
2140 * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
2141 * the configuration information for JPEG module
2142 * @param HuffTableDC: pointer to DC huffman table
2143 * @param DCTableAddress: Encoder DC huffman table address it could be HUFFENC_DC0 or HUFFENC_DC1.
2144 * @retval HAL status
2145 */
2146 static HAL_StatusTypeDef JPEG_Set_HuffDC_Mem(JPEG_HandleTypeDef *hjpeg, JPEG_DCHuffTableTypeDef *HuffTableDC, uint32_t *DCTableAddress)
2147 {
2148 HAL_StatusTypeDef error = HAL_OK;
2149 JPEG_DC_HuffCodeTableTypeDef dcSizeCodesTable;
2150 uint32_t i, lsb, msb;
2151 __IO uint32_t *address, *addressDef;
2152
2153 if(DCTableAddress == (uint32_t *)(hjpeg->Instance->HUFFENC_DC0))
2154 {
2155 address = (hjpeg->Instance->HUFFENC_DC0 + (JPEG_DC_HUFF_TABLE_SIZE/2));
2156 }
2157 else if (DCTableAddress == (uint32_t *)(hjpeg->Instance->HUFFENC_DC1))
2158 {
2159 address = (hjpeg->Instance->HUFFENC_DC1 + (JPEG_DC_HUFF_TABLE_SIZE/2));
2160 }
2161 else
2162 {
2163 return HAL_ERROR;
2164 }
2165
2166 if(HuffTableDC != NULL)
2167 {
2168 error = JPEG_DCHuff_BitsVals_To_SizeCodes(HuffTableDC, &dcSizeCodesTable);
2169 if(error != HAL_OK)
2170 {
2171 return error;
2172 }
2173 addressDef = address;
2174 *addressDef = 0x0FFF0FFF;
2175 addressDef++;
2176 *addressDef = 0x0FFF0FFF;
2177
2178 i = JPEG_DC_HUFF_TABLE_SIZE;
2179 while(i>0)
2180 {
2181 i--;
2182 address --;
2183 msb = ((uint32_t)(((uint32_t)dcSizeCodesTable.CodeLength[i] & 0xF) << 8 )) | ((uint32_t)dcSizeCodesTable.HuffmanCode[i] & 0xFF);
2184 i--;
2185 lsb = ((uint32_t)(((uint32_t)dcSizeCodesTable.CodeLength[i] & 0xF) << 8 )) | ((uint32_t)dcSizeCodesTable.HuffmanCode[i] & 0xFF);
2186
2187 *address = lsb | (msb << 16);
2188 }
2189 }
2190
2191 /* Return function status */
2192 return HAL_OK;
2193 }
2194
2195 /**
2196 * @brief Set the JPEG register with an AC huffman table at the given AC table address
2197 * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
2198 * the configuration information for JPEG module
2199 * @param HuffTableAC: pointer to AC huffman table
2200 * @param ACTableAddress: Encoder AC huffman table address it could be HUFFENC_AC0 or HUFFENC_AC1.
2201 * @retval HAL status
2202 */
2203 static HAL_StatusTypeDef JPEG_Set_HuffAC_Mem(JPEG_HandleTypeDef *hjpeg, JPEG_ACHuffTableTypeDef *HuffTableAC, uint32_t *ACTableAddress)
2204 {
2205 HAL_StatusTypeDef error = HAL_OK;
2206 JPEG_AC_HuffCodeTableTypeDef acSizeCodesTable;
2207 uint32_t i, lsb, msb;
2208 __IO uint32_t *address, *addressDef;
2209
2210 if(ACTableAddress == (uint32_t *)(hjpeg->Instance->HUFFENC_AC0))
2211 {
2212 address = (hjpeg->Instance->HUFFENC_AC0 + (JPEG_AC_HUFF_TABLE_SIZE/2));
2213 }
2214 else if (ACTableAddress == (uint32_t *)(hjpeg->Instance->HUFFENC_AC1))
2215 {
2216 address = (hjpeg->Instance->HUFFENC_AC1 + (JPEG_AC_HUFF_TABLE_SIZE/2));
2217 }
2218 else
2219 {
2220 return HAL_ERROR;
2221 }
2222
2223 if(HuffTableAC != NULL)
2224 {
2225 error = JPEG_ACHuff_BitsVals_To_SizeCodes(HuffTableAC, &acSizeCodesTable);
2226 if(error != HAL_OK)
2227 {
2228 return error;
2229 }
2230 /* Default values settings : 162\96167 FFFh , 168\96175 FD0h\96FD7h */
2231 /* Locations 162:175 of each AC table contain information used internally by the core */
2232
2233 addressDef = address;
2234 for(i=0; i<3; i++)
2235 {
2236 *addressDef = 0x0FFF0FFF;
2237 addressDef++;
2238 }
2239 *addressDef = 0x0FD10FD0;
2240 addressDef++;
2241 *addressDef = 0x0FD30FD2;
2242 addressDef++;
2243 *addressDef = 0x0FD50FD4;
2244 addressDef++;
2245 *addressDef = 0x0FD70FD6;
2246 /* end of Locations 162:175 */
2247
2248
2249 i = JPEG_AC_HUFF_TABLE_SIZE;
2250 while (i > 0)
2251 {
2252 i--;
2253 address--;
2254 msb = ((uint32_t)(((uint32_t)acSizeCodesTable.CodeLength[i] & 0xF) << 8 )) | ((uint32_t)acSizeCodesTable.HuffmanCode[i] & 0xFF);
2255 i--;
2256 lsb = ((uint32_t)(((uint32_t)acSizeCodesTable.CodeLength[i] & 0xF) << 8 )) | ((uint32_t)acSizeCodesTable.HuffmanCode[i] & 0xFF);
2257
2258 *address = lsb | (msb << 16);
2259 }
2260 }
2261
2262 /* Return function status */
2263 return HAL_OK;
2264 }
2265
2266 /**
2267 * @brief Configure the JPEG encoder register huffman tables to used during
2268 * the encdoing operation
2269 * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
2270 * the configuration information for JPEG module
2271 * @param HuffTableAC0: AC0 huffman table
2272 * @param HuffTableDC0: DC0 huffman table
2273 * @param HuffTableAC1: AC1 huffman table
2274 * @param HuffTableDC1: DC1 huffman table
2275 * @retval None
2276 */
2277 static HAL_StatusTypeDef JPEG_Set_HuffEnc_Mem(JPEG_HandleTypeDef *hjpeg, JPEG_ACHuffTableTypeDef *HuffTableAC0, JPEG_DCHuffTableTypeDef *HuffTableDC0 , JPEG_ACHuffTableTypeDef *HuffTableAC1, JPEG_DCHuffTableTypeDef *HuffTableDC1)
2278 {
2279 HAL_StatusTypeDef error = HAL_OK;
2280
2281 JPEG_Set_Huff_DHTMem(hjpeg, HuffTableAC0, HuffTableDC0, HuffTableAC1, HuffTableDC1);
2282
2283 if(HuffTableAC0 != NULL)
2284 {
2285 error = JPEG_Set_HuffAC_Mem(hjpeg, HuffTableAC0, (uint32_t *)(hjpeg->Instance->HUFFENC_AC0));
2286 if(error != HAL_OK)
2287 {
2288 return error;
2289 }
2290 }
2291
2292 if(HuffTableAC1 != NULL)
2293 {
2294 error = JPEG_Set_HuffAC_Mem(hjpeg, HuffTableAC1, (uint32_t *)(hjpeg->Instance->HUFFENC_AC1));
2295 if(error != HAL_OK)
2296 {
2297 return error;
2298 }
2299 }
2300
2301 if(HuffTableDC0 != NULL)
2302 {
2303 error = JPEG_Set_HuffDC_Mem(hjpeg, HuffTableDC0, (uint32_t *)hjpeg->Instance->HUFFENC_DC0);
2304 if(error != HAL_OK)
2305 {
2306 return error;
2307 }
2308 }
2309
2310 if(HuffTableDC1 != NULL)
2311 {
2312 error = JPEG_Set_HuffDC_Mem(hjpeg, HuffTableDC1, (uint32_t *)hjpeg->Instance->HUFFENC_DC1);
2313 if(error != HAL_OK)
2314 {
2315 return error;
2316 }
2317 }
2318 /* Return function status */
2319 return HAL_OK;
2320 }
2321
2322 /**
2323 * @brief Configure the JPEG register huffman tables to be included in the JPEG
2324 * file header (used for encoding only)
2325 * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
2326 * the configuration information for JPEG module
2327 * @param HuffTableAC0: AC0 huffman table
2328 * @param HuffTableDC0: DC0 huffman table
2329 * @param HuffTableAC1: AC1 huffman table
2330 * @param HuffTableDC1: DC1 huffman table
2331 * @retval None
2332 */
2333 static void JPEG_Set_Huff_DHTMem(JPEG_HandleTypeDef *hjpeg, JPEG_ACHuffTableTypeDef *HuffTableAC0, JPEG_DCHuffTableTypeDef *HuffTableDC0 , JPEG_ACHuffTableTypeDef *HuffTableAC1, JPEG_DCHuffTableTypeDef *HuffTableDC1)
2334 {
2335 uint32_t value, index;
2336 __IO uint32_t *address;
2337 if(HuffTableDC0 != NULL)
2338 {
2339 /* DC0 Huffman Table : BITS*/
2340 /* DC0 BITS is a 16 Bytes table i.e 4x32bits words from DHTMEM base address to DHTMEM + 3*/
2341 address = (hjpeg->Instance->DHTMEM + 3);
2342 index = 16;
2343 while(index > 0)
2344 {
2345
2346 *address = (((uint32_t)HuffTableDC0->Bits[index-1] & 0xFF) << 24)|
2347 (((uint32_t)HuffTableDC0->Bits[index-2] & 0xFF) << 16)|
2348 (((uint32_t)HuffTableDC0->Bits[index-3] & 0xFF) << 8) |
2349 ((uint32_t)HuffTableDC0->Bits[index-4] & 0xFF);
2350 address--;
2351 index -=4;
2352
2353 }
2354 /* DC0 Huffman Table : Val*/
2355 /* DC0 VALS is a 12 Bytes table i.e 3x32bits words from DHTMEM base address +4 to DHTMEM + 6 */
2356 address = (hjpeg->Instance->DHTMEM + 6);
2357 index = 12;
2358 while(index > 0)
2359 {
2360 *address = (((uint32_t)HuffTableDC0->HuffVal[index-1] & 0xFF) << 24)|
2361 (((uint32_t)HuffTableDC0->HuffVal[index-2] & 0xFF) << 16)|
2362 (((uint32_t)HuffTableDC0->HuffVal[index-3] & 0xFF) << 8) |
2363 ((uint32_t)HuffTableDC0->HuffVal[index-4] & 0xFF);
2364 address--;
2365 index -=4;
2366 }
2367 }
2368
2369 if(HuffTableAC0 != NULL)
2370 {
2371 /* AC0 Huffman Table : BITS*/
2372 /* AC0 BITS is a 16 Bytes table i.e 4x32bits words from DHTMEM base address + 7 to DHTMEM + 10*/
2373 address = (hjpeg->Instance->DHTMEM + 10);
2374 index = 16;
2375 while(index > 0)
2376 {
2377
2378 *address = (((uint32_t)HuffTableAC0->Bits[index-1] & 0xFF) << 24)|
2379 (((uint32_t)HuffTableAC0->Bits[index-2] & 0xFF) << 16)|
2380 (((uint32_t)HuffTableAC0->Bits[index-3] & 0xFF) << 8) |
2381 ((uint32_t)HuffTableAC0->Bits[index-4] & 0xFF);
2382 address--;
2383 index -=4;
2384
2385 }
2386 /* AC0 Huffman Table : Val*/
2387 /* AC0 VALS is a 162 Bytes table i.e 41x32bits words from DHTMEM base address + 11 to DHTMEM + 51 */
2388 /* only Byte 0 and Byte 1 of the last word (@ DHTMEM + 51) belong to AC0 VALS table */
2389 address = (hjpeg->Instance->DHTMEM + 51);
2390 value = *address & 0xFFFF0000U;
2391 value = value | (((uint32_t)HuffTableAC0->HuffVal[161] & 0xFF) << 8) | ((uint32_t)HuffTableAC0->HuffVal[160] & 0xFF);
2392 *address = value;
2393
2394 /*continue setting 160 AC0 huffman values */
2395 address--; /* address = hjpeg->Instance->DHTMEM + 50*/
2396 index = 160;
2397 while(index > 0)
2398 {
2399 *address = (((uint32_t)HuffTableAC0->HuffVal[index-1] & 0xFF) << 24)|
2400 (((uint32_t)HuffTableAC0->HuffVal[index-2] & 0xFF) << 16)|
2401 (((uint32_t)HuffTableAC0->HuffVal[index-3] & 0xFF) << 8) |
2402 ((uint32_t)HuffTableAC0->HuffVal[index-4] & 0xFF);
2403 address--;
2404 index -=4;
2405 }
2406 }
2407
2408 if(HuffTableDC1 != NULL)
2409 {
2410 /* DC1 Huffman Table : BITS*/
2411 /* DC1 BITS is a 16 Bytes table i.e 4x32bits words from DHTMEM + 51 base address to DHTMEM + 55*/
2412 /* only Byte 2 and Byte 3 of the first word (@ DHTMEM + 51) belong to DC1 Bits table */
2413 address = (hjpeg->Instance->DHTMEM + 51);
2414 value = *address & 0x0000FFFFU;
2415 value = value | (((uint32_t)HuffTableDC1->Bits[1] & 0xFF) << 24) | (((uint32_t)HuffTableDC1->Bits[0] & 0xFF) << 16);
2416 *address = value;
2417
2418 /* only Byte 0 and Byte 1 of the last word (@ DHTMEM + 55) belong to DC1 Bits table */
2419 address = (hjpeg->Instance->DHTMEM + 55);
2420 value = *address & 0xFFFF0000U;
2421 value = value | (((uint32_t)HuffTableDC1->Bits[15] & 0xFF) << 8) | ((uint32_t)HuffTableDC1->Bits[14] & 0xFF);
2422 *address = value;
2423
2424 /*continue setting 12 DC1 huffman Bits from DHTMEM + 54 down to DHTMEM + 52*/
2425 address--;
2426 index = 12;
2427 while(index > 0)
2428 {
2429
2430 *address = (((uint32_t)HuffTableDC1->Bits[index+1] & 0xFF) << 24)|
2431 (((uint32_t)HuffTableDC1->Bits[index] & 0xFF) << 16)|
2432 (((uint32_t)HuffTableDC1->Bits[index-1] & 0xFF) << 8) |
2433 ((uint32_t)HuffTableDC1->Bits[index-2] & 0xFF);
2434 address--;
2435 index -=4;
2436
2437 }
2438 /* DC1 Huffman Table : Val*/
2439 /* DC1 VALS is a 12 Bytes table i.e 3x32bits words from DHTMEM base address +55 to DHTMEM + 58 */
2440 /* only Byte 2 and Byte 3 of the first word (@ DHTMEM + 55) belong to DC1 Val table */
2441 address = (hjpeg->Instance->DHTMEM + 55);
2442 value = *address & 0x0000FFFF;
2443 value = value | (((uint32_t)HuffTableDC1->HuffVal[1] & 0xFF) << 24) | (((uint32_t)HuffTableDC1->HuffVal[0] & 0xFF) << 16);
2444 *address = value;
2445
2446 /* only Byte 0 and Byte 1 of the last word (@ DHTMEM + 58) belong to DC1 Val table */
2447 address = (hjpeg->Instance->DHTMEM + 58);
2448 value = *address & 0xFFFF0000U;
2449 value = value | (((uint32_t)HuffTableDC1->HuffVal[11] & 0xFF) << 8) | ((uint32_t)HuffTableDC1->HuffVal[10] & 0xFF);
2450 *address = value;
2451
2452 /*continue setting 8 DC1 huffman val from DHTMEM + 57 down to DHTMEM + 56*/
2453 address--;
2454 index = 8;
2455 while(index > 0)
2456 {
2457 *address = (((uint32_t)HuffTableDC1->HuffVal[index+1] & 0xFF) << 24)|
2458 (((uint32_t)HuffTableDC1->HuffVal[index] & 0xFF) << 16)|
2459 (((uint32_t)HuffTableDC1->HuffVal[index-1] & 0xFF) << 8) |
2460 ((uint32_t)HuffTableDC1->HuffVal[index-2] & 0xFF);
2461 address--;
2462 index -=4;
2463 }
2464 }
2465
2466 if(HuffTableAC1 != NULL)
2467 {
2468 /* AC1 Huffman Table : BITS*/
2469 /* AC1 BITS is a 16 Bytes table i.e 4x32bits words from DHTMEM base address + 58 to DHTMEM + 62*/
2470 /* only Byte 2 and Byte 3 of the first word (@ DHTMEM + 58) belong to AC1 Bits table */
2471 address = (hjpeg->Instance->DHTMEM + 58);
2472 value = *address & 0x0000FFFFU;
2473 value = value | (((uint32_t)HuffTableAC1->Bits[1] & 0xFF) << 24) | (((uint32_t)HuffTableAC1->Bits[0] & 0xFF) << 16);
2474 *address = value;
2475
2476 /* only Byte 0 and Byte 1 of the last word (@ DHTMEM + 62) belong to Bits Val table */
2477 address = (hjpeg->Instance->DHTMEM + 62);
2478 value = *address & 0xFFFF0000U;
2479 value = value | (((uint32_t)HuffTableAC1->Bits[15] & 0xFF) << 8) | ((uint32_t)HuffTableAC1->Bits[14] & 0xFF);
2480 *address = value;
2481
2482 /*continue setting 12 AC1 huffman Bits from DHTMEM + 61 down to DHTMEM + 59*/
2483 address--;
2484 index = 12;
2485 while(index > 0)
2486 {
2487
2488 *address = (((uint32_t)HuffTableAC1->Bits[index+1] & 0xFF) << 24)|
2489 (((uint32_t)HuffTableAC1->Bits[index] & 0xFF) << 16)|
2490 (((uint32_t)HuffTableAC1->Bits[index-1] & 0xFF) << 8) |
2491 ((uint32_t)HuffTableAC1->Bits[index-2] & 0xFF);
2492 address--;
2493 index -=4;
2494
2495 }
2496 /* AC1 Huffman Table : Val*/
2497 /* AC1 VALS is a 162 Bytes table i.e 41x32bits words from DHTMEM base address + 62 to DHTMEM + 102 */
2498 /* only Byte 2 and Byte 3 of the first word (@ DHTMEM + 62) belong to AC1 VALS table */
2499 address = (hjpeg->Instance->DHTMEM + 62);
2500 value = *address & 0x0000FFFF;
2501 value = value | (((uint32_t)HuffTableAC1->HuffVal[1] & 0xFF) << 24) | (((uint32_t)HuffTableAC1->HuffVal[0] & 0xFF) << 16);
2502 *address = value;
2503
2504 /*continue setting 160 AC1 huffman values from DHTMEM + 63 to DHTMEM+102 */
2505 address = (hjpeg->Instance->DHTMEM + 102);
2506 index = 160;
2507 while(index > 0)
2508 {
2509 *address = (((uint32_t)HuffTableAC1->HuffVal[index+1] & 0xFF) << 24)|
2510 (((uint32_t)HuffTableAC1->HuffVal[index] & 0xFF) << 16)|
2511 (((uint32_t)HuffTableAC1->HuffVal[index-1] & 0xFF) << 8) |
2512 ((uint32_t)HuffTableAC1->HuffVal[index-2] & 0xFF);
2513 address--;
2514 index -=4;
2515 }
2516 }
2517 }
2518
2519 /**
2520 * @brief Configure the JPEG registers with a given quantization table
2521 * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
2522 * the configuration information for JPEG module
2523 * @param QTable: pointer to an array of 64 bytes giving the quantization table
2524 * @param QTableAddress: destination quantization address in the JPEG peripheral
2525 * it could be QMEM0, QMEM1, QMEM2 or QMEM3
2526 * @retval None
2527 */
2528 static HAL_StatusTypeDef JPEG_Set_Quantization_Mem(JPEG_HandleTypeDef *hjpeg, uint8_t *QTable, uint32_t *QTableAddress)
2529 {
2530 uint32_t i, j, *tableAddress, quantRow, quantVal, ScaleFactor;
2531
2532 if((QTableAddress == ((uint32_t *)(hjpeg->Instance->QMEM0))) ||
2533 (QTableAddress == ((uint32_t *)(hjpeg->Instance->QMEM1))) ||
2534 (QTableAddress == ((uint32_t *)(hjpeg->Instance->QMEM2))) ||
2535 (QTableAddress == ((uint32_t *)(hjpeg->Instance->QMEM3))))
2536 {
2537 tableAddress = QTableAddress;
2538 }
2539 else
2540 {
2541 return HAL_ERROR;
2542 }
2543
2544 if ((hjpeg->Conf.ImageQuality >= 50) && (hjpeg->Conf.ImageQuality <= 100))
2545 {
2546 ScaleFactor = 200 - (hjpeg->Conf.ImageQuality * 2);
2547 }
2548 else if (hjpeg->Conf.ImageQuality > 0)
2549 {
2550 ScaleFactor = ((uint32_t) 5000) / ((uint32_t) hjpeg->Conf.ImageQuality);
2551 }
2552 else
2553 {
2554 return HAL_ERROR;
2555 }
2556
2557 /*Quantization_table = (Standard_quanization_table * ScaleFactor + 50) / 100*/
2558 i = 0;
2559 while( i < JPEG_QUANT_TABLE_SIZE)
2560 {
2561 quantRow = 0;
2562 for(j=0; j<4; j++)
2563 {
2564 /* Note that the quantization coefficients must be specified in the table in zigzag order */
2565 quantVal = ((((uint32_t) QTable[JPEG_ZIGZAG_ORDER[i+j]]) * ScaleFactor) + 50) / 100;
2566
2567 if(quantVal == 0)
2568 {
2569 quantVal = 1;
2570 }
2571 else if (quantVal > 255)
2572 {
2573 quantVal = 255;
2574 }
2575
2576 quantRow |= ((quantVal & 0xFF) << (8 * j));
2577 }
2578
2579 i += 4;
2580 *tableAddress = quantRow;
2581 tableAddress ++;
2582 }
2583
2584 /* Return function status */
2585 return HAL_OK;
2586 }
2587
2588 /**
2589 * @brief Configure the JPEG registers for YCbCr color space
2590 * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
2591 * the configuration information for JPEG module
2592 * @retval None
2593 */
2594 static void JPEG_SetColorYCBCR(JPEG_HandleTypeDef *hjpeg)
2595 {
2596 uint32_t ySamplingH;
2597 uint32_t ySamplingV;
2598 uint32_t yblockNb;
2599
2600 /*Set Number of color components to 3*/
2601 hjpeg->Instance->CONFR1 &= ~JPEG_CONFR1_NF;
2602 hjpeg->Instance->CONFR1 |= JPEG_CONFR1_NF_1;
2603
2604 /* compute MCU block size and Y, Cb ,Cr sampling factors*/
2605 if(hjpeg->Conf.ChromaSubsampling == JPEG_420_SUBSAMPLING)
2606 {
2607 ySamplingH = JPEG_CONFR4_HSF_1; /* Hs = 2*/
2608 ySamplingV = JPEG_CONFR4_VSF_1; /* Vs = 2*/
2609
2610 yblockNb = 0x30; /* 4 blocks of 8x8*/
2611 }
2612 else if(hjpeg->Conf.ChromaSubsampling == JPEG_422_SUBSAMPLING)
2613 {
2614 ySamplingH = JPEG_CONFR4_HSF_1; /* Hs = 2*/
2615 ySamplingV = JPEG_CONFR4_VSF_0; /* Vs = 1*/
2616
2617 yblockNb = 0x10; /* 2 blocks of 8x8*/
2618 }
2619 else /*JPEG_444_SUBSAMPLING and default*/
2620 {
2621 ySamplingH = JPEG_CONFR4_HSF_0; /* Hs = 1*/
2622 ySamplingV = JPEG_CONFR4_VSF_0; /* Vs = 1*/
2623
2624 yblockNb = 0; /* 1 block of 8x8*/
2625 }
2626
2627 hjpeg->Instance->CONFR1 &= ~(JPEG_CONFR1_NF | JPEG_CONFR1_NS);
2628 hjpeg->Instance->CONFR1 |= (JPEG_CONFR1_NF_1 | JPEG_CONFR1_NS_1);
2629
2630 /*Reset CONFR4 register*/
2631 hjpeg->Instance->CONFR4 = 0;
2632 /*Set Horizental and Vertical sampling factor , number of blocks , Quantization table and Huffman AC/DC tables for component 0*/
2633 hjpeg->Instance->CONFR4 |= (ySamplingH | ySamplingV | (yblockNb & JPEG_CONFR4_NB) );
2634
2635 /*Reset CONFR5 register*/
2636 hjpeg->Instance->CONFR5 = 0;
2637 /*Set Horizental and Vertical sampling factor , number of blocks , Quantization table and Huffman AC/DC tables for component 1*/
2638 hjpeg->Instance->CONFR5 |= (JPEG_CONFR5_HSF_0 | JPEG_CONFR5_VSF_0 | JPEG_CONFR5_QT_0 | JPEG_CONFR5_HA | JPEG_CONFR5_HD);
2639
2640 /*Reset CONFR6 register*/
2641 hjpeg->Instance->CONFR6 = 0;
2642 /*Set Horizental and Vertical sampling factor and number of blocks for component 2*/
2643 /* In YCBCR , by default, both chrominance components (component 1 and component 2) use the same Quantization table (table 1) */
2644 /* In YCBCR , both chrominance components (component 1 and component 2) use the same Huffman tables (table 1) */
2645 hjpeg->Instance->CONFR6 |= (JPEG_CONFR6_HSF_0 | JPEG_CONFR6_VSF_0 | JPEG_CONFR6_QT_0 | JPEG_CONFR6_HA | JPEG_CONFR6_HD);
2646
2647 }
2648
2649 /**
2650 * @brief Configure the JPEG registers for GrayScale color space
2651 * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
2652 * the configuration information for JPEG module
2653 * @retval None
2654 */
2655 static void JPEG_SetColorGrayScale(JPEG_HandleTypeDef *hjpeg)
2656 {
2657 /*Set Number of color components to 1*/
2658 hjpeg->Instance->CONFR1 &= ~(JPEG_CONFR1_NF | JPEG_CONFR1_NS);
2659
2660 /*in GrayScale use 1 single Quantization table (Table 0)*/
2661 /*in GrayScale use only one couple of AC/DC huffman table (table 0)*/
2662
2663 /*Reset CONFR4 register*/
2664 hjpeg->Instance->CONFR4 = 0;
2665 /*Set Horizental and Vertical sampling factor , number of blocks , Quantization table and Huffman AC/DC tables for component 0*/
2666 hjpeg->Instance->CONFR4 |= JPEG_CONFR4_HSF_0 | JPEG_CONFR4_VSF_0 ;
2667 }
2668
2669 /**
2670 * @brief Configure the JPEG registers for CMYK color space
2671 * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
2672 * the configuration information for JPEG module
2673 * @retval None
2674 */
2675 static void JPEG_SetColorCMYK(JPEG_HandleTypeDef *hjpeg)
2676 {
2677 uint32_t ySamplingH;
2678 uint32_t ySamplingV;
2679 uint32_t yblockNb;
2680
2681 /*Set Number of color components to 4*/
2682 hjpeg->Instance->CONFR1 |= (JPEG_CONFR1_NF | JPEG_CONFR1_NS);
2683
2684 /* compute MCU block size and Y, Cb ,Cr sampling factors*/
2685 if(hjpeg->Conf.ChromaSubsampling == JPEG_420_SUBSAMPLING)
2686 {
2687 ySamplingH = JPEG_CONFR4_HSF_1; /* Hs = 2*/
2688 ySamplingV = JPEG_CONFR4_VSF_1; /* Vs = 2*/
2689
2690 yblockNb = 0x30; /* 4 blocks of 8x8*/
2691 }
2692 else if(hjpeg->Conf.ChromaSubsampling == JPEG_422_SUBSAMPLING)
2693 {
2694 ySamplingH = JPEG_CONFR4_HSF_1; /* Hs = 2*/
2695 ySamplingV = JPEG_CONFR4_VSF_0; /* Vs = 1*/
2696
2697 yblockNb = 0x10; /* 2 blocks of 8x8*/
2698 }
2699 else /*JPEG_444_SUBSAMPLING and default*/
2700 {
2701 ySamplingH = JPEG_CONFR4_HSF_0; /* Hs = 1*/
2702 ySamplingV = JPEG_CONFR4_VSF_0; /* Vs = 1*/
2703
2704 yblockNb = 0; /* 1 block of 8x8*/
2705 }
2706
2707 /*Reset CONFR4 register*/
2708 hjpeg->Instance->CONFR4 = 0;
2709 /*Set Horizental and Vertical sampling factor , number of blocks , Quantization table and Huffman AC/DC tables for component 0*/
2710 hjpeg->Instance->CONFR4 |= (ySamplingH | ySamplingV | (yblockNb & JPEG_CONFR4_NB) );
2711
2712 /*Reset CONFR5 register*/
2713 hjpeg->Instance->CONFR5 = 0;
2714 /*Set Horizental and Vertical sampling factor , number of blocks , Quantization table and Huffman AC/DC tables for component 1*/
2715 hjpeg->Instance->CONFR5 |= (JPEG_CONFR5_HSF_0 | JPEG_CONFR5_VSF_0);
2716
2717 /*Reset CONFR6 register*/
2718 hjpeg->Instance->CONFR6 = 0;
2719 /*Set Horizental and Vertical sampling factor , number of blocks , Quantization table and Huffman AC/DC tables for component 2*/
2720 hjpeg->Instance->CONFR6 |= (JPEG_CONFR6_HSF_0 | JPEG_CONFR6_VSF_0);
2721
2722 /*Reset CONFR7 register*/
2723 hjpeg->Instance->CONFR7 = 0;
2724 /*Set Horizental and Vertical sampling factor , number of blocks , Quantization table and Huffman AC/DC tables for component 3*/
2725 hjpeg->Instance->CONFR7 |= (JPEG_CONFR7_HSF_0 | JPEG_CONFR7_VSF_0);
2726 }
2727
2728 /**
2729 * @brief Init the JPEG encoding/decoding process in case of Polling or Interrupt and DMA
2730 * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
2731 * the configuration information for JPEG module
2732 * @retval None
2733 */
2734 static void JPEG_Init_Process(JPEG_HandleTypeDef *hjpeg)
2735 {
2736 /*Reset pause*/
2737 hjpeg->Context &= (~(JPEG_CONTEXT_PAUSE_INPUT | JPEG_CONTEXT_PAUSE_OUTPUT));
2738
2739 if((hjpeg->Context & JPEG_CONTEXT_OPERATION_MASK) == JPEG_CONTEXT_DECODE)
2740 {
2741 /*Set JPEG Codec to Decoding mode */
2742 hjpeg->Instance->CONFR1 |= JPEG_CONFR1_DE;
2743 }
2744 else if((hjpeg->Context & JPEG_CONTEXT_OPERATION_MASK) == JPEG_CONTEXT_ENCODE)
2745 {
2746 /*Set JPEG Codec to Encoding mode */
2747 hjpeg->Instance->CONFR1 &= ~JPEG_CONFR1_DE;
2748 }
2749
2750 /*Stop JPEG processing */
2751 hjpeg->Instance->CONFR0 &= ~JPEG_CONFR0_START;
2752
2753 /* Disable All Interrupts */
2754 __HAL_JPEG_DISABLE_IT(hjpeg,JPEG_INTERRUPT_MASK);
2755
2756 /* Disable All DMA requests */
2757 JPEG_DISABLE_DMA(hjpeg,JPEG_DMA_MASK);
2758
2759 /* Flush input and output FIFOs*/
2760 hjpeg->Instance->CR |= JPEG_CR_IFF;
2761 hjpeg->Instance->CR |= JPEG_CR_OFF;
2762
2763 /* Clear all flags */
2764 __HAL_JPEG_CLEAR_FLAG(hjpeg,JPEG_FLAG_ALL);
2765
2766 /*Start Encoding/Decoding*/
2767 hjpeg->Instance->CONFR0 |= JPEG_CONFR0_START;
2768
2769 if((hjpeg->Context & JPEG_CONTEXT_METHOD_MASK) == JPEG_CONTEXT_IT)
2770 {
2771 /*Enable IN/OUT, end of Conversation, and end of header parsing interruptions*/
2772 __HAL_JPEG_ENABLE_IT(hjpeg, JPEG_IT_IFT | JPEG_IT_IFNF | JPEG_IT_OFT | JPEG_IT_OFNE | JPEG_IT_EOC |JPEG_IT_HPD);
2773 }
2774 else if((hjpeg->Context & JPEG_CONTEXT_METHOD_MASK) == JPEG_CONTEXT_DMA)
2775 {
2776 /*Enable End Of Conversation, and End Of Header parsing interruptions*/
2777 __HAL_JPEG_ENABLE_IT(hjpeg, JPEG_IT_EOC |JPEG_IT_HPD);
2778
2779 }
2780 }
2781
2782 /**
2783 * @brief JPEG encoding/decoding process in case of Polling or Interrupt
2784 * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
2785 * the configuration information for JPEG module
2786 * @retval JPEG_PROCESS_DONE if the process has ends else JPEG_PROCESS_ONGOING
2787 */
2788 static uint32_t JPEG_Process(JPEG_HandleTypeDef *hjpeg)
2789 {
2790 uint32_t tmpContext;
2791
2792 /*End of header processing flag rised*/
2793 if(((hjpeg->Context & JPEG_CONTEXT_OPERATION_MASK) == JPEG_CONTEXT_DECODE) && (__HAL_JPEG_GET_FLAG(hjpeg, JPEG_FLAG_HPDF) != RESET))
2794 {
2795 /*Call Header parsing complet callback */
2796 HAL_JPEG_GetInfo(hjpeg, &hjpeg->Conf);
2797 /* Reset the ImageQuality */
2798 hjpeg->Conf.ImageQuality = 0;
2799 /* Note : the image quality is only available at the end of the decoding operation */
2800 /* at the current stage the calculated image quality is not correct so reset it */
2801
2802 /*Call Info Ready callback */
2803 HAL_JPEG_InfoReadyCallback(hjpeg, &hjpeg->Conf);
2804
2805 __HAL_JPEG_DISABLE_IT(hjpeg,JPEG_IT_HPD);
2806
2807 /* Clear header processing done flag */
2808 __HAL_JPEG_CLEAR_FLAG(hjpeg,JPEG_FLAG_HPDF);
2809 }
2810
2811 /*Input FIFO status handling*/
2812 if((hjpeg->Context & JPEG_CONTEXT_PAUSE_INPUT) == 0)
2813 {
2814 if(__HAL_JPEG_GET_FLAG(hjpeg, JPEG_FLAG_IFTF) != RESET)
2815 {
2816 /*Input FIFO threshold flag rised*/
2817 /*4 words (16 bytes) can be written in */
2818 JPEG_ReadInputData(hjpeg,4);
2819 }
2820 else if(__HAL_JPEG_GET_FLAG(hjpeg, JPEG_FLAG_IFNFF) != RESET)
2821 {
2822 /*Input FIFO Not Full flag rised*/
2823 /*32-bit value can be written in */
2824 JPEG_ReadInputData(hjpeg,1);
2825 }
2826 }
2827
2828
2829 /*Output FIFO flag handling*/
2830 if((hjpeg->Context & JPEG_CONTEXT_PAUSE_OUTPUT) == 0)
2831 {
2832 if(__HAL_JPEG_GET_FLAG(hjpeg, JPEG_FLAG_OFTF) != RESET)
2833 {
2834 /*Output FIFO threshold flag rised*/
2835 /*4 words (16 bytes) can be read out */
2836 JPEG_StoreOutputData(hjpeg, 4);
2837 }
2838 else if(__HAL_JPEG_GET_FLAG(hjpeg, JPEG_FLAG_OFNEF) != RESET)
2839 {
2840 /*Output FIFO Not Empty flag rised*/
2841 /*32-bit value can be read out */
2842 JPEG_StoreOutputData(hjpeg, 1);
2843 }
2844 }
2845
2846 /*End of Conversion handling :i.e EOC flag is high and OFTF low and OFNEF low*/
2847 if(__HAL_JPEG_GET_FLAG(hjpeg, JPEG_FLAG_EOCF | JPEG_FLAG_OFTF | JPEG_FLAG_OFNEF) == JPEG_FLAG_EOCF)
2848 {
2849 /*Stop Encoding/Decoding*/
2850 hjpeg->Instance->CONFR0 &= ~JPEG_CONFR0_START;
2851
2852 if((hjpeg->Context & JPEG_CONTEXT_METHOD_MASK) == JPEG_CONTEXT_IT)
2853 {
2854 /* Disable All Interrupts */
2855 __HAL_JPEG_DISABLE_IT(hjpeg,JPEG_INTERRUPT_MASK);
2856 }
2857
2858 /* Clear all flags */
2859 __HAL_JPEG_CLEAR_FLAG(hjpeg,JPEG_FLAG_ALL);
2860
2861 /*Call End of conversion callback */
2862 if(hjpeg->JpegOutCount > 0)
2863 {
2864 /*Output Buffer is not empty, call DecodedDataReadyCallback*/
2865 HAL_JPEG_DataReadyCallback (hjpeg, hjpeg->pJpegOutBuffPtr, hjpeg->JpegOutCount);
2866 hjpeg->JpegOutCount = 0;
2867 }
2868
2869 /*Reset Context Operation*/
2870 tmpContext = hjpeg->Context;
2871 /*Clear all context fields execpt JPEG_CONTEXT_CONF_ENCODING and JPEG_CONTEXT_CUSTOM_TABLES*/
2872 hjpeg->Context &= (JPEG_CONTEXT_CONF_ENCODING | JPEG_CONTEXT_CUSTOM_TABLES);
2873
2874 /* Process Unlocked */
2875 __HAL_UNLOCK(hjpeg);
2876
2877 /* Change the JPEG state */
2878 hjpeg->State = HAL_JPEG_STATE_READY;
2879
2880 /*Call End of Encoding/Decoding callback */
2881 if((tmpContext & JPEG_CONTEXT_OPERATION_MASK) == JPEG_CONTEXT_DECODE)
2882 {
2883 HAL_JPEG_DecodeCpltCallback(hjpeg);
2884 }
2885 else if((tmpContext & JPEG_CONTEXT_OPERATION_MASK) == JPEG_CONTEXT_ENCODE)
2886 {
2887 HAL_JPEG_EncodeCpltCallback(hjpeg);
2888 }
2889
2890 return JPEG_PROCESS_DONE;
2891 }
2892
2893
2894 return JPEG_PROCESS_ONGOING;
2895 }
2896
2897 /**
2898 * @brief Store some output data from the JPEG peripheral to the output buffer.
2899 * This function is used when the JPEG peripheral has new data to output
2900 * in case of Polling or Interrupt process
2901 * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
2902 * the configuration information for JPEG module
2903 * @param nbOutputWords: Number of output words (of 32 bits) ready from the JPEG peripheral
2904 * @retval None
2905 */
2906 static void JPEG_StoreOutputData(JPEG_HandleTypeDef *hjpeg, uint32_t nbOutputWords)
2907 {
2908 uint32_t index, nBwords, nbBytes , dataword, *pOutData;
2909
2910 pOutData = (uint32_t *)(((uint32_t *)hjpeg->pJpegOutBuffPtr) + (hjpeg->JpegOutCount/4));
2911
2912 if(hjpeg->OutDataLength >= (hjpeg->JpegOutCount + (nbOutputWords*4)))
2913 {
2914 for(index = 0; index < nbOutputWords; index++)
2915 {
2916 /*Transfer 32 bits from the JPEG output FIFO*/
2917 *pOutData = hjpeg->Instance->DOR;
2918 pOutData++;
2919 hjpeg->JpegOutCount += 4;
2920 }
2921 if(hjpeg->OutDataLength == hjpeg->JpegOutCount)
2922 {
2923 /*Output Buffer is full, call DecodedDataReadyCallback*/
2924 HAL_JPEG_DataReadyCallback (hjpeg, hjpeg->pJpegOutBuffPtr, hjpeg->JpegOutCount);
2925 hjpeg->JpegOutCount = 0;
2926 }
2927 }
2928 else if(hjpeg->OutDataLength > hjpeg->JpegOutCount)
2929 {
2930 nBwords = (hjpeg->OutDataLength - hjpeg->JpegOutCount)/4;
2931 for(index = 0; index < nBwords; index++)
2932 {
2933 /*Transfer 32 bits from the JPEG output FIFO*/
2934 *pOutData = hjpeg->Instance->DOR;
2935 pOutData++;
2936 hjpeg->JpegOutCount += 4;
2937 }
2938 if(hjpeg->OutDataLength == hjpeg->JpegOutCount)
2939 {
2940 /*Output Buffer is full, call DecodedDataReadyCallback*/
2941 HAL_JPEG_DataReadyCallback (hjpeg, hjpeg->pJpegOutBuffPtr, hjpeg->JpegOutCount);
2942 hjpeg->JpegOutCount = 0;
2943 }
2944 else
2945 {
2946 nbBytes = hjpeg->OutDataLength - hjpeg->JpegOutCount;
2947 dataword = hjpeg->Instance->DOR;
2948 for(index = 0; index < nbBytes; index++)
2949 {
2950 hjpeg->pJpegOutBuffPtr[hjpeg->JpegOutCount] = (dataword >> (8*index)) & 0xFF;
2951 hjpeg->JpegOutCount++;
2952 }
2953 /*Output Buffer is full, call DecodedDataReadyCallback*/
2954 HAL_JPEG_DataReadyCallback (hjpeg, hjpeg->pJpegOutBuffPtr, hjpeg->JpegOutCount);
2955 hjpeg->JpegOutCount = 0;
2956
2957 nbBytes = 4 - nbBytes;
2958 for(index = nbBytes; index < 4; index++)
2959 {
2960 hjpeg->pJpegOutBuffPtr[hjpeg->JpegOutCount] = (dataword >> (8*index)) & 0xFF;
2961 hjpeg->JpegOutCount++;
2962 }
2963 }
2964 }
2965 }
2966
2967 /**
2968 * @brief Read some input Data from the input buffer.
2969 * This function is used when the JPEG peripheral needs new data
2970 * in case of Polling or Interrupt process
2971 * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
2972 * the configuration information for JPEG module
2973 * @param nbRequestWords: Number of input words (of 32 bits) that the JPE peripheral request
2974 * @retval None
2975 */
2976 static void JPEG_ReadInputData(JPEG_HandleTypeDef *hjpeg, uint32_t nbRequestWords)
2977 {
2978 uint32_t nbBytes = 0, nBwords, index, Dataword;
2979
2980 if((hjpeg->InDataLength == 0) || (nbRequestWords == 0))
2981 {
2982 /* No more Input data : nothing to do*/
2983 HAL_JPEG_Pause(hjpeg, JPEG_PAUSE_RESUME_INPUT);
2984 }
2985 else if(hjpeg->InDataLength > hjpeg->JpegInCount)
2986 {
2987 nbBytes = hjpeg->InDataLength - hjpeg->JpegInCount;
2988 }
2989 else if(hjpeg->InDataLength == hjpeg->JpegInCount)
2990 {
2991 /*Call HAL_JPEG_GetDataCallback to get new data */
2992 HAL_JPEG_GetDataCallback(hjpeg, hjpeg->JpegInCount);
2993 if(hjpeg->InDataLength > 4)
2994 {
2995 hjpeg->InDataLength = hjpeg->InDataLength - (hjpeg->InDataLength % 4);
2996 }
2997 hjpeg->JpegInCount = 0;
2998 nbBytes = hjpeg->InDataLength;
2999 }
3000 if((nbBytes > 0) && ((hjpeg->Context & JPEG_CONTEXT_PAUSE_INPUT) == 0))
3001 {
3002 nBwords = nbBytes / 4;
3003 if(nBwords >= nbRequestWords)
3004 {
3005 for(index = 0; index < nbRequestWords; index++)
3006 {
3007 hjpeg->Instance->DIR = *((uint32_t *)(((uint32_t *)hjpeg->pJpegInBuffPtr) + (hjpeg->JpegInCount/4)));
3008
3009 hjpeg->JpegInCount += 4;
3010 }
3011 }
3012 else /*nBwords < nbRequestWords*/
3013 {
3014 if(nBwords > 0)
3015 {
3016 for(index = 0; index < nBwords; index++)
3017 {
3018 hjpeg->Instance->DIR = *((uint32_t *)(((uint32_t *)hjpeg->pJpegInBuffPtr) + (hjpeg->JpegInCount/4)));
3019
3020 hjpeg->JpegInCount += 4;
3021 }
3022 }
3023 else
3024 {
3025 /* end of file*/
3026 Dataword = 0;
3027 for(index=0; index< nbBytes; index++)
3028 {
3029 Dataword |= (uint32_t)hjpeg->pJpegInBuffPtr[hjpeg->JpegInCount] << (8 * index);
3030 hjpeg->JpegInCount++;
3031 }
3032 hjpeg->Instance->DIR = Dataword;
3033 }
3034 }
3035 }
3036 }
3037
3038 /**
3039 * @brief Start the JPEG DMA process (encoding/decoding)
3040 * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
3041 * the configuration information for JPEG module
3042 * @retval JPEG_PROCESS_DONE if process ends else JPEG_PROCESS_ONGOING
3043 */
3044 static HAL_StatusTypeDef JPEG_DMA_StartProcess(JPEG_HandleTypeDef *hjpeg)
3045 {
3046 if((hjpeg->InDataLength < 4) || (hjpeg->OutDataLength < 4))
3047 {
3048 return HAL_ERROR;
3049 }
3050 /* Reset Ending DMA internal context flag*/
3051 hjpeg->Context &= ~JPEG_CONTEXT_ENDING_DMA;
3052
3053 /* Disable DMA In/Out Request*/
3054 JPEG_DISABLE_DMA(hjpeg,JPEG_DMA_ODMA | JPEG_DMA_IDMA);
3055
3056 /* Set the JPEG DMA In transfer complete callback */
3057 hjpeg->hdmain->XferCpltCallback = JPEG_DMAInCpltCallback;
3058 /* Set the DMA In error callback */
3059 hjpeg->hdmain->XferErrorCallback = JPEG_DMAErrorCallback;
3060
3061 /* Set the JPEG DMA Out transfer complete callback */
3062 hjpeg->hdmaout->XferCpltCallback = JPEG_DMAOutCpltCallback;
3063 /* Set the DMA Out error callback */
3064 hjpeg->hdmaout->XferErrorCallback = JPEG_DMAErrorCallback;
3065 /* Set the DMA Out Abort callback */
3066 hjpeg->hdmaout->XferAbortCallback = JPEG_DMAOutAbortCallback;
3067
3068 /*DMA transfer size must be a multiple of 4 bytes i.e mutliple of 32bits words*/
3069 hjpeg->InDataLength = hjpeg->InDataLength - (hjpeg->InDataLength % 4);
3070
3071 /*DMA transfer size must be a multiple of 4 bytes i.e mutliple of 32bits words*/
3072 hjpeg->OutDataLength = hjpeg->OutDataLength - (hjpeg->OutDataLength % 4);
3073
3074 /* Start DMA FIFO In transfer */
3075 HAL_DMA_Start_IT(hjpeg->hdmain, (uint32_t)hjpeg->pJpegInBuffPtr, (uint32_t)&hjpeg->Instance->DIR, hjpeg->InDataLength >> 2);
3076
3077 /* Start DMA FIFO Out transfer */
3078 HAL_DMA_Start_IT(hjpeg->hdmaout, (uint32_t)&hjpeg->Instance->DOR, (uint32_t)hjpeg->pJpegOutBuffPtr, hjpeg->OutDataLength >> 2);
3079
3080 /* Enable JPEG In/Out DMA requests*/
3081 JPEG_ENABLE_DMA(hjpeg,JPEG_DMA_IDMA | JPEG_DMA_ODMA);
3082
3083 return HAL_OK;
3084 }
3085
3086 /**
3087 * @brief Continue the current JPEG DMA process (encoding/decoding)
3088 * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
3089 * the configuration information for JPEG module
3090 * @retval JPEG_PROCESS_DONE if process ends else JPEG_PROCESS_ONGOING
3091 */
3092 static uint32_t JPEG_DMA_ContinueProcess(JPEG_HandleTypeDef *hjpeg)
3093 {
3094 /*End of header processing flag rises*/
3095 if(((hjpeg->Context & JPEG_CONTEXT_OPERATION_MASK) == JPEG_CONTEXT_DECODE) && (__HAL_JPEG_GET_FLAG(hjpeg, JPEG_FLAG_HPDF) != RESET))
3096 {
3097 /*Call Header parsing complete callback */
3098 HAL_JPEG_GetInfo(hjpeg, &hjpeg->Conf);
3099
3100 /* Reset the ImageQuality */
3101 hjpeg->Conf.ImageQuality = 0;
3102 /* Note : the image quality is only available at the end of the decoding operation */
3103 /* at the current stage the calculated image quality is not correct so reset it */
3104
3105 /*Call Info Ready callback */
3106 HAL_JPEG_InfoReadyCallback(hjpeg, &hjpeg->Conf);
3107
3108 __HAL_JPEG_DISABLE_IT(hjpeg,JPEG_IT_HPD);
3109
3110 /* Clear header processing done flag */
3111 __HAL_JPEG_CLEAR_FLAG(hjpeg,JPEG_FLAG_HPDF);
3112 }
3113
3114 /*End of Conversion handling*/
3115 if(__HAL_JPEG_GET_FLAG(hjpeg, JPEG_FLAG_EOCF) != RESET)
3116 {
3117 /*Disabkle JPEG In/Out DMA Requests*/
3118 JPEG_DISABLE_DMA(hjpeg,JPEG_DMA_ODMA | JPEG_DMA_IDMA);
3119
3120 hjpeg->Context |= JPEG_CONTEXT_ENDING_DMA;
3121
3122 /*Stop Encoding/Decoding*/
3123 hjpeg->Instance->CONFR0 &= ~JPEG_CONFR0_START;
3124
3125 __HAL_JPEG_DISABLE_IT(hjpeg,JPEG_INTERRUPT_MASK);
3126
3127 /* Clear all flags */
3128 __HAL_JPEG_CLEAR_FLAG(hjpeg,JPEG_FLAG_ALL);
3129
3130 if(hjpeg->hdmain->State == HAL_DMA_STATE_BUSY)
3131 {
3132 /* Stop the DMA In Xfer*/
3133 HAL_DMA_Abort_IT(hjpeg->hdmain);
3134 }
3135
3136 if(hjpeg->hdmaout->State == HAL_DMA_STATE_BUSY)
3137 {
3138 /* Stop the DMA out Xfer*/
3139 HAL_DMA_Abort_IT(hjpeg->hdmaout);
3140 }
3141 else
3142 {
3143 return JPEG_DMA_EndProcess(hjpeg);
3144 }
3145 }
3146
3147 return JPEG_PROCESS_ONGOING;
3148 }
3149
3150 /**
3151 * @brief Finalize the current JPEG DMA process (encoding/decoding)
3152 * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
3153 * the configuration information for JPEG module
3154 * @retval JPEG_PROCESS_DONE
3155 */
3156 static uint32_t JPEG_DMA_EndProcess(JPEG_HandleTypeDef *hjpeg)
3157 {
3158 uint32_t tmpContext, count = JPEG_FIFO_SIZE, *pDataOut;
3159
3160 hjpeg->JpegOutCount = hjpeg->OutDataLength - ((hjpeg->hdmaout->Instance->NDTR & DMA_SxNDT) << 2);
3161
3162 /*if Output Buffer is full, call HAL_JPEG_DataReadyCallback*/
3163 if(hjpeg->JpegOutCount == hjpeg->OutDataLength)
3164 {
3165 HAL_JPEG_DataReadyCallback (hjpeg, hjpeg->pJpegOutBuffPtr, hjpeg->JpegOutCount);
3166 hjpeg->JpegOutCount = 0;
3167 }
3168
3169 pDataOut = (uint32_t *)(hjpeg->pJpegOutBuffPtr + hjpeg->JpegOutCount);
3170
3171 while((__HAL_JPEG_GET_FLAG(hjpeg, JPEG_FLAG_OFNEF) != 0) && (count > 0))
3172 {
3173 count--;
3174
3175 *pDataOut = hjpeg->Instance->DOR;
3176 pDataOut++;
3177 hjpeg->JpegOutCount += 4;
3178
3179 if(hjpeg->JpegOutCount == hjpeg->OutDataLength)
3180 {
3181 /*Output Buffer is full, call HAL_JPEG_DataReadyCallback*/
3182 HAL_JPEG_DataReadyCallback (hjpeg, hjpeg->pJpegOutBuffPtr, hjpeg->JpegOutCount);
3183 hjpeg->JpegOutCount = 0;
3184 }
3185 }
3186
3187 /*Stop Encoding/Decoding*/
3188 hjpeg->Instance->CONFR0 &= ~JPEG_CONFR0_START;
3189
3190 if(hjpeg->JpegOutCount > 0)
3191 {
3192 /*Output Buffer is not empty, call DecodedDataReadyCallback*/
3193 HAL_JPEG_DataReadyCallback (hjpeg, hjpeg->pJpegOutBuffPtr, hjpeg->JpegOutCount);
3194 hjpeg->JpegOutCount = 0;
3195 }
3196
3197 tmpContext = hjpeg->Context;
3198 /*Clear all context fileds execpt JPEG_CONTEXT_CONF_ENCODING and JPEG_CONTEXT_CUSTOM_TABLES*/
3199 hjpeg->Context &= (JPEG_CONTEXT_CONF_ENCODING | JPEG_CONTEXT_CUSTOM_TABLES);
3200
3201 /* Process Unlocked */
3202 __HAL_UNLOCK(hjpeg);
3203
3204 /* Change the JPEG state */
3205 hjpeg->State = HAL_JPEG_STATE_READY;
3206
3207 /*Call End of Encoding/Decoding callback */
3208 if((tmpContext & JPEG_CONTEXT_OPERATION_MASK) == JPEG_CONTEXT_DECODE)
3209 {
3210 HAL_JPEG_DecodeCpltCallback(hjpeg);
3211 }
3212 else if((tmpContext & JPEG_CONTEXT_OPERATION_MASK) == JPEG_CONTEXT_ENCODE)
3213 {
3214 HAL_JPEG_EncodeCpltCallback(hjpeg);
3215 }
3216
3217
3218 return JPEG_PROCESS_DONE;
3219 }
3220
3221 /**
3222 * @brief DMA input transfer complete callback
3223 * @param hdma: pointer to a DMA_HandleTypeDef structure.
3224 * @retval None
3225 */
3226 static void JPEG_DMAInCpltCallback(DMA_HandleTypeDef *hdma)
3227 {
3228 JPEG_HandleTypeDef* hjpeg = (JPEG_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
3229
3230 /* Disable The JPEG IT so the DMA Input Callback can not be interrupted by the JPEG EOC IT or JPEG HPD IT */
3231 __HAL_JPEG_DISABLE_IT(hjpeg,JPEG_INTERRUPT_MASK);
3232
3233 if(((hjpeg->Context & JPEG_CONTEXT_METHOD_MASK) == JPEG_CONTEXT_DMA) && ((hjpeg->Context & JPEG_CONTEXT_ENDING_DMA) == 0))
3234 {
3235 JPEG_DISABLE_DMA(hjpeg,JPEG_DMA_IDMA);
3236
3237 hjpeg->JpegInCount = hjpeg->InDataLength - ((hdma->Instance->NDTR & DMA_SxNDT) << 2);
3238
3239 /*Call HAL_JPEG_GetDataCallback to get new data */
3240 HAL_JPEG_GetDataCallback(hjpeg, hjpeg->JpegInCount);
3241
3242 if(hjpeg->InDataLength >= 4)
3243 {
3244 /*JPEG Input DMA transfer data number must be multiple of 32 bits word
3245 as the destination is a 32 bits (4 bytes) register */
3246 hjpeg->InDataLength = hjpeg->InDataLength - (hjpeg->InDataLength % 4);
3247 }
3248 else if(hjpeg->InDataLength > 0)
3249 {
3250 /*Transfer last data word (i.e last 4 bytes)*/
3251 hjpeg->InDataLength = 4;
3252 }
3253
3254 if(((hjpeg->Context & JPEG_CONTEXT_PAUSE_INPUT) == 0) && (hjpeg->InDataLength > 0))
3255 {
3256 /* Start DMA FIFO In transfer */
3257 HAL_DMA_Start_IT(hjpeg->hdmain, (uint32_t)hjpeg->pJpegInBuffPtr, (uint32_t)&hjpeg->Instance->DIR, hjpeg->InDataLength >> 2);
3258 JPEG_ENABLE_DMA(hjpeg,JPEG_DMA_IDMA);
3259 }
3260
3261 /* JPEG Conversion still on going : Enable the JPEG IT */
3262 __HAL_JPEG_ENABLE_IT(hjpeg,JPEG_IT_EOC |JPEG_IT_HPD);
3263 }
3264 }
3265
3266 /**
3267 * @brief DMA output transfer complete callback
3268 * @param hdma: pointer to a DMA_HandleTypeDef structure.
3269 * @retval None
3270 */
3271 static void JPEG_DMAOutCpltCallback(DMA_HandleTypeDef *hdma)
3272 {
3273 JPEG_HandleTypeDef* hjpeg = (JPEG_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
3274
3275 /* Disable The JPEG IT so the DMA Output Callback can not be interrupted by the JPEG EOC IT or JPEG HPD IT */
3276 __HAL_JPEG_DISABLE_IT(hjpeg,JPEG_INTERRUPT_MASK);
3277
3278 if(((hjpeg->Context & JPEG_CONTEXT_METHOD_MASK) == JPEG_CONTEXT_DMA) && ((hjpeg->Context & JPEG_CONTEXT_ENDING_DMA) == 0))
3279 {
3280 if(__HAL_JPEG_GET_FLAG(hjpeg, JPEG_FLAG_EOCF) == 0)
3281 {
3282 JPEG_DISABLE_DMA(hjpeg,JPEG_DMA_ODMA);
3283 hjpeg->JpegOutCount = hjpeg->OutDataLength - ((hdma->Instance->NDTR & DMA_SxNDT) << 2);
3284
3285 /*Output Buffer is full, call HAL_JPEG_DataReadyCallback*/
3286 HAL_JPEG_DataReadyCallback (hjpeg, hjpeg->pJpegOutBuffPtr, hjpeg->JpegOutCount);
3287
3288 if((hjpeg->Context & JPEG_CONTEXT_PAUSE_OUTPUT) == 0)
3289 {
3290 /* Start DMA FIFO Out transfer */
3291 HAL_DMA_Start_IT(hjpeg->hdmaout, (uint32_t)&hjpeg->Instance->DOR, (uint32_t)hjpeg->pJpegOutBuffPtr, hjpeg->OutDataLength >> 2);
3292 JPEG_ENABLE_DMA(hjpeg,JPEG_DMA_ODMA);
3293 }
3294 }
3295
3296 /* JPEG Conversion still on going : Enable the JPEG IT */
3297 __HAL_JPEG_ENABLE_IT(hjpeg,JPEG_IT_EOC |JPEG_IT_HPD);
3298 }
3299 }
3300
3301 /**
3302 * @brief DMA Transfer error callback
3303 * @param hdma: pointer to a DMA_HandleTypeDef structure.
3304 * @retval None
3305 */
3306 static void JPEG_DMAErrorCallback(DMA_HandleTypeDef *hdma)
3307 {
3308 JPEG_HandleTypeDef* hjpeg = (JPEG_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
3309
3310 /* if DMA error is FIFO error ignore it */
3311 if(HAL_DMA_GetError(hdma) != HAL_DMA_ERROR_FE)
3312 {
3313 /*Stop Encoding/Decoding*/
3314 hjpeg->Instance->CONFR0 &= ~JPEG_CONFR0_START;
3315
3316 /* Disable All Interrupts */
3317 __HAL_JPEG_DISABLE_IT(hjpeg,JPEG_INTERRUPT_MASK);
3318
3319 /* Disable All DMA requests */
3320 JPEG_DISABLE_DMA(hjpeg,JPEG_DMA_MASK);
3321
3322 hjpeg->State= HAL_JPEG_STATE_READY;
3323 hjpeg->ErrorCode |= HAL_JPEG_ERROR_DMA;
3324 HAL_JPEG_ErrorCallback(hjpeg);
3325 }
3326 }
3327
3328 /**
3329 * @brief DMA output Abort callback
3330 * @param hdma: pointer to a DMA_HandleTypeDef structure.
3331 * @retval None
3332 */
3333 static void JPEG_DMAOutAbortCallback(DMA_HandleTypeDef *hdma)
3334 {
3335 JPEG_HandleTypeDef* hjpeg = (JPEG_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
3336
3337 if((hjpeg->Context & JPEG_CONTEXT_ENDING_DMA) != 0)
3338 {
3339 JPEG_DMA_EndProcess(hjpeg);
3340 }
3341 }
3342
3343 /**
3344 * @brief Calculate the decoded image quality (from 1 to 100)
3345 * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
3346 * the configuration information for JPEG module
3347 * @retval JPEG image quality from 1 to 100.
3348 */
3349 static uint32_t JPEG_GetQuality(JPEG_HandleTypeDef *hjpeg)
3350 {
3351 uint32_t quality = 0;
3352 uint32_t quantRow, quantVal,scale, i, j;
3353 uint32_t *tableAddress = (uint32_t *)hjpeg->Instance->QMEM0;
3354
3355 i = 0;
3356 while( i < JPEG_QUANT_TABLE_SIZE)
3357 {
3358 quantRow = *tableAddress;
3359 for(j=0; j<4; j++)
3360 {
3361 quantVal = (quantRow >> (8 * j)) & 0xFF;
3362 if(quantVal == 1)
3363 {
3364 /* if Quantization value = 1 then quality is 100%*/
3365 quality += 100;
3366 }
3367 else
3368 {
3369 /* Note that the quantization coefficients must be specified in the table in zigzag order */
3370 scale = (quantVal*100)/((uint32_t) JPEG_LUM_QuantTable[JPEG_ZIGZAG_ORDER[i+j]]);
3371
3372 if(scale <= 100)
3373 {
3374 quality += (200 - scale)/2;
3375 }
3376 else
3377 {
3378 quality += 5000/scale;
3379 }
3380 }
3381 }
3382
3383 i += 4;
3384 tableAddress ++;
3385 }
3386
3387 return (quality/((uint32_t)64));
3388 }
3389 /**
3390 * @}
3391 */
3392
3393 #endif /* STM32F767xx || STM32F769xx || STM32F777xx || STM32F779xx */
3394 #endif /* HAL_JPEG_MODULE_ENABLED */
3395 /**
3396 * @}
3397 */
3398
3399 /**
3400 * @}
3401 */
3402
3403 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/