1 /**
2   ******************************************************************************
3   * @file    stm32f7xx_hal_jpeg.c
4   * @author  MCD Application Team
5   * @brief   JPEG HAL module driver.
6   *          This file provides firmware functions to manage the following
7   *          functionalities of the JPEG encoder/decoder peripheral:
8   *           + Initialization and de-initialization functions
9   *           + JPEG processing functions encoding and decoding
10   *           + JPEG decoding Getting Info and encoding configuration setting
11   *           + JPEG enable/disable header parsing functions (for decoding)
12   *           + JPEG Input/Output Buffer configuration.
13   *           + JPEG callback functions
14   *           + JPEG Abort/Pause/Resume functions
15   *           + JPEG custom quantization tables setting functions
16   *           + IRQ handler management
17   *           + Peripheral State and Error functions
18   *
19   ******************************************************************************
20   * @attention
21   *
22   * Copyright (c) 2017 STMicroelectronics.
23   * All rights reserved.
24   *
25   * This software is licensed under terms that can be found in the LICENSE file
26   * in the root directory of this software component.
27   * If no LICENSE file comes with this software, it is provided AS-IS.
28   *
29   ******************************************************************************
30   @verbatim
31   ==============================================================================
32                      ##### How to use this driver #####
33   ==============================================================================
34     [..]
35      (#) Initialize the JPEG peripheral using HAL_JPEG_Init : No initialization parameters are required.
36          Only the call to HAL_JPEG_Init is necessary to initialize the JPEG peripheral.
37 
38      (#) If operation is JPEG encoding use function HAL_JPEG_ConfigEncoding to set
39          the encoding parameters (mandatory before calling the encoding function).
40          the application can change the encoding parameter ImageQuality from
41          1 to 100 to obtain a more or less quality (visual quality vs the original row image),
42          and inversely more or less jpg file size.
43 
44      (#) Note that for decoding operation the JPEG peripheral output data are organized in
45          YCbCr blocks called MCU (Minimum Coded Unit) as defioned in the JPEG specification
46          ISO/IEC 10918-1 standard.
47          It is up to the application to transform these YCbCr blocks to RGB data that can be display.
48 
49          Respectively, for Encoding operation the JPEG peripheral input should be organized
50          in YCbCr MCU blocks. It is up to the application to perform the necessary RGB to YCbCr
51          MCU blocks transformation before feeding the JPEG peripheral with data.
52 
53      (#) Use functions HAL_JPEG_Encode and HAL_JPEG_Decode to start respectively
54          a JPEG encoding/decoding operation in polling method (blocking).
55 
56      (#) Use functions HAL_JPEG_Encode_IT and HAL_JPEG_Decode_IT to start respectively
57          a JPEG encoding/decoding operation with Interrupt method (not blocking).
58 
59      (#) Use functions HAL_JPEG_Encode_DMA and HAL_JPEG_Decode_DMA to start respectively
60          a JPEG encoding/decoding operation with DMA method (not blocking).
61 
62      (#) Callback HAL_JPEG_InfoReadyCallback is asserted if the current operation
63          is a JPEG decoding to provide the application with JPEG image  parameters.
64          This callback is asserted when the JPEG peripheral successfully parse the
65          JPEG header.
66 
67      (#) Callback HAL_JPEG_GetDataCallback is asserted for both encoding and decoding
68          operations to inform the application that the input buffer has been
69          consumed by the peripheral and to ask for a new data chunk if the operation
70          (encoding/decoding) has not been complete yet.
71 
72         (++) This CallBack should be implemented in the application side. It should
73              call the function HAL_JPEG_ConfigInputBuffer if new input data are available,
74              or call HAL_JPEG_Pause with parameter XferSelection set to JPEG_PAUSE_RESUME_INPUT
75              to inform the JPEG HAL driver that the ongoing operation shall pause waiting for the
76              application to provide a new input data chunk.
77              Once the application succeed getting new data and if the input has been paused,
78              the application can call the function HAL_JPEG_ConfigInputBuffer to set the new
79              input buffer and size, then resume the JPEG HAL input by calling new function HAL_JPEG_Resume.
80              If the application has ended feeding the HAL JPEG with input data (no more input data), the application
81              Should call the function HAL_JPEG_ConfigInputBuffer (within the callback HAL_JPEG_GetDataCallback)
82              with the parameter InDataLength set to zero.
83 
84          (++) The mechanism of HAL_JPEG_ConfigInputBuffer/HAL_JPEG_Pause/HAL_JPEG_Resume allows
85               to the application to provide the input data (for encoding or decoding) by chunks.
86               If the new input data chunk is not available (because data should be read from an input file
87               for example) the application can pause the JPEG input (using function HAL_JPEG_Pause)
88               Once the new input data chunk is available ( read from a file for example), the application
89               can call the function HAL_JPEG_ConfigInputBuffer to provide the HAL with the new chunk
90               then resume the JPEG HAL input by calling function HAL_JPEG_Resume.
91 
92          (++) The application can call functions HAL_JPEG_ConfigInputBuffer then HAL_JPEG_Resume.
93               any time (outside the HAL_JPEG_GetDataCallback)  Once the new input chunk data available.
94               However, to keep data coherency, the function HAL_JPEG_Pause must be imperatively called
95               (if necessary) within the callback HAL_JPEG_GetDataCallback, i.e when the HAL JPEG has ended
96               Transferring the previous chunk buffer to the JPEG peripheral.
97 
98      (#) Callback HAL_JPEG_DataReadyCallback is asserted when the HAL JPEG driver
99          has filled the given output buffer with the given size.
100 
101          (++) This CallBack should be implemented in the application side. It should
102               call the function HAL_JPEG_ConfigOutputBuffer to provide the HAL JPEG driver
103               with the new output buffer location and size to be used  to store next data chunk.
104               if the application is not ready to provide the output chunk location then it can
105               call the function HAL_JPEG_Pause with parameter XferSelection set to JPEG_PAUSE_RESUME_OUTPUT
106               to inform the JPEG HAL driver that it shall pause output data. Once the application
107               is ready to receive the new data chunk (output buffer location free or available) it should call
108               the function HAL_JPEG_ConfigOutputBuffer to provide the HAL JPEG driver
109               with the new output chunk buffer location and size, then call HAL_JPEG_Resume
110               to inform the HAL that it shall resume outputting data in the given output buffer.
111 
112          (++) The mechanism of HAL_JPEG_ConfigOutputBuffer/HAL_JPEG_Pause/HAL_JPEG_Resume allows
113               the application to receive data from the JPEG peripheral by chunks. when a chunk
114               is received, the application can pause the HAL JPEG output data to be able to process
115               these received data (YCbCr to RGB conversion in case of decoding or data storage in case
116               of encoding).
117 
118          (++) The application can call  functions HAL_JPEG_ ConfigOutputBuffer then HAL_JPEG_Resume.
119               any time (outside the HAL_JPEG_DataReadyCallback) Once the output data buffer is free to use.
120               However, to keep data coherency, the function HAL_JPEG_Pause must be imperatively called
121               (if necessary) within the callback HAL_JPEG_ DataReadyCallback, i.e when the HAL JPEG has ended
122               Transferring the previous chunk buffer from the JPEG peripheral to the application.
123 
124      (#) Callback HAL_JPEG_EncodeCpltCallback is asserted when the HAL JPEG driver has
125          ended the current JPEG encoding operation, and all output data has been transmitted
126          to the application.
127 
128      (#) Callback HAL_JPEG_DecodeCpltCallback is asserted when the HAL JPEG driver has
129          ended the current JPEG decoding operation. and all output data has been transmitted
130          to the application.
131 
132      (#) Callback HAL_JPEG_ErrorCallback is asserted when an error occurred during
133          the current operation. the application can call the function HAL_JPEG_GetError()
134          to retrieve the error codes.
135 
136      (#) By default the HAL JPEG driver uses the default quantization tables
137          as provide in the JPEG specification (ISO/IEC 10918-1 standard) for encoding.
138          User can change these default tables if necessary using the function HAL_JPEG_SetUserQuantTables
139          Note that for decoding the quantization tables are automatically extracted from
140          the JPEG header.
141 
142       (#) To control JPEG state you can use the following function: HAL_JPEG_GetState()
143 
144      *** JPEG HAL driver macros list ***
145      =============================================
146      [..]
147        Below the list of most used macros in JPEG HAL driver.
148 
149       (+) __HAL_JPEG_RESET_HANDLE_STATE : Reset JPEG handle state.
150       (+) __HAL_JPEG_ENABLE             : Enable the JPEG peripheral.
151       (+) __HAL_JPEG_DISABLE            : Disable the JPEG peripheral.
152       (+) __HAL_JPEG_GET_FLAG           : Check the specified JPEG status flag.
153       (+) __HAL_JPEG_CLEAR_FLAG         : Clear the specified JPEG status flag.
154       (+) __HAL_JPEG_ENABLE_IT          : Enable the specified JPEG Interrupt.
155       (+) __HAL_JPEG_DISABLE_IT         : Disable the specified JPEG Interrupt.
156       (+) __HAL_JPEG_GET_IT_SOURCE      : returns the state of the specified JPEG Interrupt (Enabled or disabled).
157 
158   *** Callback registration ***
159   =============================================
160 
161   The compilation define  USE_HAL_JPEG_REGISTER_CALLBACKS when set to 1
162   allows the user to configure dynamically the driver callbacks.
163   Use Functions HAL_JPEG_RegisterCallback() or HAL_JPEG_RegisterXXXCallback()
164   to register an interrupt callback.
165 
166   Function HAL_JPEG_RegisterCallback() allows to register following callbacks:
167     (+) EncodeCpltCallback : callback for end of encoding operation.
168     (+) DecodeCpltCallback : callback for end of decoding operation.
169     (+) ErrorCallback      : callback for error detection.
170     (+) MspInitCallback    : JPEG MspInit.
171     (+) MspDeInitCallback  : JPEG MspDeInit.
172   This function takes as parameters the HAL peripheral handle, the Callback ID
173   and a pointer to the user callback function.
174 
175   For specific callbacks InfoReadyCallback, GetDataCallback and DataReadyCallback use dedicated
176   register callbacks : respectively HAL_JPEG_RegisterInfoReadyCallback(),
177   HAL_JPEG_RegisterGetDataCallback() and HAL_JPEG_RegisterDataReadyCallback().
178 
179   Use function HAL_JPEG_UnRegisterCallback() to reset a callback to the default
180   weak function.
181   HAL_JPEG_UnRegisterCallback() takes as parameters the HAL peripheral handle,
182   and the Callback ID.
183   This function allows to reset following callbacks:
184     (+) EncodeCpltCallback : callback for end of encoding operation.
185     (+) DecodeCpltCallback : callback for end of decoding operation.
186     (+) ErrorCallback      : callback for error detection.
187     (+) MspInitCallback    : JPEG MspInit.
188     (+) MspDeInitCallback  : JPEG MspDeInit.
189 
190   For callbacks InfoReadyCallback, GetDataCallback and DataReadyCallback use dedicated
191   unregister callbacks : respectively HAL_JPEG_UnRegisterInfoReadyCallback(),
192   HAL_JPEG_UnRegisterGetDataCallback() and HAL_JPEG_UnRegisterDataReadyCallback().
193 
194   By default, after the HAL_JPEG_Init() and when the state is HAL_JPEG_STATE_RESET
195   all callbacks are set to the corresponding weak functions :
196   examples HAL_JPEG_DecodeCpltCallback() , HAL_JPEG_GetDataCallback().
197   Exception done for MspInit and MspDeInit functions that are
198   reset to the legacy weak function in the HAL_JPEG_Init()/ HAL_JPEG_DeInit() only when
199   these callbacks are null (not registered beforehand).
200   if not, MspInit or MspDeInit are not null, the HAL_JPEG_Init() / HAL_JPEG_DeInit()
201   keep and use the user MspInit/MspDeInit functions (registered beforehand)
202 
203   Callbacks can be registered/unregistered in HAL_JPEG_STATE_READY state only.
204   Exception done MspInit/MspDeInit callbacks that can be registered/unregistered
205   in HAL_JPEG_STATE_READY or HAL_JPEG_STATE_RESET state,
206   thus registered (user) MspInit/DeInit callbacks can be used during the Init/DeInit.
207   In that case first register the MspInit/MspDeInit user callbacks
208   using HAL_JPEG_RegisterCallback() before calling HAL_JPEG_DeInit()
209   or HAL_JPEG_Init() function.
210 
211   When The compilation define USE_HAL_JPEG_REGISTER_CALLBACKS is set to 0 or
212   not defined, the callback registration feature is not available and all callbacks
213   are set to the corresponding weak functions.
214 
215   @endverbatim
216   ******************************************************************************
217   */
218 
219 /* Includes ------------------------------------------------------------------*/
220 #include "stm32f7xx_hal.h"
221 
222 /** @addtogroup STM32F7xx_HAL_Driver
223   * @{
224   */
225 
226 #ifdef HAL_JPEG_MODULE_ENABLED
227 
228 #if defined (JPEG)
229 
230 /** @defgroup JPEG JPEG
231   * @brief JPEG HAL module driver.
232   * @{
233   */
234 
235 /* Private define ------------------------------------------------------------*/
236 /** @addtogroup JPEG_Private_Constants
237   * @{
238   */
239 #define JPEG_TIMEOUT_VALUE  ((uint32_t)1000)     /* 1s */
240 #define JPEG_AC_HUFF_TABLE_SIZE  ((uint32_t)162) /* Huffman AC table size : 162 codes*/
241 #define JPEG_DC_HUFF_TABLE_SIZE  ((uint32_t)12)  /* Huffman AC table size : 12 codes*/
242 
243 #define JPEG_FIFO_SIZE ((uint32_t)16U)             /* JPEG Input/Output HW FIFO size in words*/
244 
245 #define JPEG_FIFO_TH_SIZE ((uint32_t)4U)              /* JPEG Input/Output HW FIFO Threshold in words*/
246 
247 #define JPEG_DMA_MASK     ((uint32_t)0x00001800)     /* JPEG DMA request Mask*/
248 #define JPEG_DMA_IDMA     ((uint32_t)JPEG_CR_IDMAEN)  /* DMA request for the input FIFO */
249 #define JPEG_DMA_ODMA     ((uint32_t)JPEG_CR_ODMAEN)  /* DMA request for the output FIFO */
250 
251 #define JPEG_INTERRUPT_MASK  ((uint32_t)0x0000007EU) /* JPEG Interrupt Mask*/
252 
253 #define JPEG_CONTEXT_ENCODE          ((uint32_t)0x00000001) /* JPEG context : operation is encoding*/
254 #define JPEG_CONTEXT_DECODE          ((uint32_t)0x00000002) /* JPEG context : operation is decoding*/
255 #define JPEG_CONTEXT_OPERATION_MASK  ((uint32_t)0x00000003) /* JPEG context : operation Mask */
256 
257 #define JPEG_CONTEXT_POLLING        ((uint32_t)0x00000004)  /* JPEG context : Transfer use Polling */
258 #define JPEG_CONTEXT_IT             ((uint32_t)0x00000008)  /* JPEG context : Transfer use Interrupt */
259 #define JPEG_CONTEXT_DMA            ((uint32_t)0x0000000C)  /* JPEG context : Transfer use DMA */
260 #define JPEG_CONTEXT_METHOD_MASK    ((uint32_t)0x0000000C)  /* JPEG context : Transfer Mask */
261 
262 
263 #define JPEG_CONTEXT_CONF_ENCODING  ((uint32_t)0x00000100)  /* JPEG context : encoding config done */
264 
265 #define JPEG_CONTEXT_PAUSE_INPUT    ((uint32_t)0x00001000)  /* JPEG context : Pause Input */
266 #define JPEG_CONTEXT_PAUSE_OUTPUT   ((uint32_t)0x00002000)  /* JPEG context : Pause Output */
267 
268 #define JPEG_CONTEXT_CUSTOM_TABLES  ((uint32_t)0x00004000)  /* JPEG context : Use custom quantization tables */
269 
270 #define JPEG_CONTEXT_ENDING_DMA     ((uint32_t)0x00008000)  /* JPEG context : ending with DMA in progress */
271 
272 #define JPEG_PROCESS_ONGOING        ((uint32_t)0x00000000)  /* Process is on going */
273 #define JPEG_PROCESS_DONE           ((uint32_t)0x00000001)  /* Process is done (ends) */
274 /**
275   * @}
276   */
277 
278 /* Private typedef -----------------------------------------------------------*/
279 /** @addtogroup JPEG_Private_Types
280   * @{
281   */
282 
283 /*
284  JPEG Huffman Table Structure definition :
285  This implementation of Huffman table structure is compliant with ISO/IEC 10918-1 standard , Annex C Huffman Table specification
286  */
287 typedef struct
288 {
289   /* These two fields directly represent the contents of a JPEG DHT marker */
290   uint8_t Bits[16];        /*!< bits[k] = # of symbols with codes of length k bits, this parameter corresponds to BITS list in the Annex C */
291 
292   uint8_t HuffVal[162];    /*!< The symbols, in order of incremented code length, this parameter corresponds to HUFFVAL list in the Annex C */
293 
294 
295 } JPEG_ACHuffTableTypeDef;
296 
297 typedef struct
298 {
299   /* These two fields directly represent the contents of a JPEG DHT marker */
300   uint8_t Bits[16];        /*!< bits[k] = # of symbols with codes of length k bits, this parameter corresponds to BITS list in the Annex C */
301 
302   uint8_t HuffVal[12];    /*!< The symbols, in order of incremented code length, this parameter corresponds to HUFFVAL list in the Annex C */
303 
304 
305 } JPEG_DCHuffTableTypeDef;
306 
307 typedef struct
308 {
309   uint8_t CodeLength[JPEG_AC_HUFF_TABLE_SIZE];      /*!< Code length  */
310 
311   uint32_t HuffmanCode[JPEG_AC_HUFF_TABLE_SIZE];    /*!< HuffmanCode */
312 
313 } JPEG_AC_HuffCodeTableTypeDef;
314 
315 typedef struct
316 {
317   uint8_t CodeLength[JPEG_DC_HUFF_TABLE_SIZE];        /*!< Code length  */
318 
319   uint32_t HuffmanCode[JPEG_DC_HUFF_TABLE_SIZE];    /*!< HuffmanCode */
320 
321 } JPEG_DC_HuffCodeTableTypeDef;
322 /**
323   * @}
324   */
325 
326 /* Private macro -------------------------------------------------------------*/
327 /** @addtogroup JPEG_Private_Macros
328   * @{
329   */
330 #define JPEG_ENABLE_DMA(__HANDLE__,__DMA__)  ((__HANDLE__)->Instance->CR |= ((__DMA__) & JPEG_DMA_MASK))
331 /*note  : To disable a DMA request we must use MODIFY_REG macro to avoid writing "1" to the FIFO flush bits
332          located in the same DMA request enable register (CR register). */
333 #define JPEG_DISABLE_DMA(__HANDLE__,__DMA__) MODIFY_REG((__HANDLE__)->Instance->CR, ((__DMA__) & JPEG_DMA_MASK), 0UL)
334 /**
335   * @}
336   */
337 
338 /* Private variables ---------------------------------------------------------*/
339 /** @addtogroup JPEG_Private_Variables
340   * @{
341   */
342 
343 static const JPEG_DCHuffTableTypeDef JPEG_DCLUM_HuffTable =
344 {
345   { 0, 1, 5, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0 },   /*Bits*/
346 
347   { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0xa, 0xb }           /*HUFFVAL */
348 
349 };
350 
351 static const JPEG_DCHuffTableTypeDef JPEG_DCCHROM_HuffTable =
352 {
353   { 0, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0 },  /*Bits*/
354 
355   { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0xa, 0xb }          /*HUFFVAL */
356 };
357 
358 static const JPEG_ACHuffTableTypeDef JPEG_ACLUM_HuffTable =
359 {
360   { 0, 2, 1, 3, 3, 2, 4, 3, 5, 5, 4, 4, 0, 0, 1, 0x7d },  /*Bits*/
361 
362   {
363     0x01, 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12,     /*HUFFVAL */
364     0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61, 0x07,
365     0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xa1, 0x08,
366     0x23, 0x42, 0xb1, 0xc1, 0x15, 0x52, 0xd1, 0xf0,
367     0x24, 0x33, 0x62, 0x72, 0x82, 0x09, 0x0a, 0x16,
368     0x17, 0x18, 0x19, 0x1a, 0x25, 0x26, 0x27, 0x28,
369     0x29, 0x2a, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
370     0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49,
371     0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59,
372     0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69,
373     0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79,
374     0x7a, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89,
375     0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
376     0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
377     0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6,
378     0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3, 0xc4, 0xc5,
379     0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 0xd3, 0xd4,
380     0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe1, 0xe2,
381     0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea,
382     0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
383     0xf9, 0xfa
384   }
385 };
386 
387 static const JPEG_ACHuffTableTypeDef JPEG_ACCHROM_HuffTable =
388 {
389   { 0, 2, 1, 2, 4, 4, 3, 4, 7, 5, 4, 4, 0, 1, 2, 0x77 },   /*Bits*/
390 
391   {
392     0x00, 0x01, 0x02, 0x03, 0x11, 0x04, 0x05, 0x21,      /*HUFFVAL */
393     0x31, 0x06, 0x12, 0x41, 0x51, 0x07, 0x61, 0x71,
394     0x13, 0x22, 0x32, 0x81, 0x08, 0x14, 0x42, 0x91,
395     0xa1, 0xb1, 0xc1, 0x09, 0x23, 0x33, 0x52, 0xf0,
396     0x15, 0x62, 0x72, 0xd1, 0x0a, 0x16, 0x24, 0x34,
397     0xe1, 0x25, 0xf1, 0x17, 0x18, 0x19, 0x1a, 0x26,
398     0x27, 0x28, 0x29, 0x2a, 0x35, 0x36, 0x37, 0x38,
399     0x39, 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48,
400     0x49, 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58,
401     0x59, 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68,
402     0x69, 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78,
403     0x79, 0x7a, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
404     0x88, 0x89, 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96,
405     0x97, 0x98, 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5,
406     0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4,
407     0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3,
408     0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2,
409     0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda,
410     0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9,
411     0xea, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
412     0xf9, 0xfa
413   }
414 };
415 
416 static const uint8_t JPEG_ZIGZAG_ORDER[JPEG_QUANT_TABLE_SIZE] =
417 {
418   0,   1,   8,  16,   9,   2,   3,  10,
419   17,  24,  32,  25,  18,  11,   4,   5,
420   12,  19,  26,  33,  40,  48,  41,  34,
421   27,  20,  13,   6,   7,  14,  21,  28,
422   35,  42,  49,  56,  57,  50,  43,  36,
423   29,  22,  15,  23,  30,  37,  44,  51,
424   58,  59,  52,  45,  38,  31,  39,  46,
425   53,  60,  61,  54,  47,  55,  62,  63
426 };
427 /**
428   * @}
429   */
430 
431 /* Private function prototypes -----------------------------------------------*/
432 /** @addtogroup JPEG_Private_Functions_Prototypes
433   * @{
434   */
435 
436 static HAL_StatusTypeDef JPEG_Bits_To_SizeCodes(uint8_t *Bits, uint8_t *Huffsize, uint32_t *Huffcode, uint32_t *LastK);
437 static HAL_StatusTypeDef JPEG_DCHuff_BitsVals_To_SizeCodes(JPEG_DCHuffTableTypeDef *DC_BitsValsTable,
438                                                            JPEG_DC_HuffCodeTableTypeDef *DC_SizeCodesTable);
439 static HAL_StatusTypeDef JPEG_ACHuff_BitsVals_To_SizeCodes(JPEG_ACHuffTableTypeDef *AC_BitsValsTable,
440                                                            JPEG_AC_HuffCodeTableTypeDef *AC_SizeCodesTable);
441 static HAL_StatusTypeDef JPEG_Set_HuffDC_Mem(JPEG_HandleTypeDef *hjpeg, JPEG_DCHuffTableTypeDef *HuffTableDC,
442                                              const __IO uint32_t *DCTableAddress);
443 static HAL_StatusTypeDef JPEG_Set_HuffAC_Mem(JPEG_HandleTypeDef *hjpeg, JPEG_ACHuffTableTypeDef *HuffTableAC,
444                                              const __IO uint32_t *ACTableAddress);
445 static HAL_StatusTypeDef JPEG_Set_HuffEnc_Mem(JPEG_HandleTypeDef *hjpeg);
446 static void JPEG_Set_Huff_DHTMem(JPEG_HandleTypeDef *hjpeg);
447 static uint32_t  JPEG_Set_Quantization_Mem(JPEG_HandleTypeDef *hjpeg, uint8_t *QTable,
448                                                     __IO uint32_t *QTableAddress);
449 static void JPEG_SetColorYCBCR(JPEG_HandleTypeDef *hjpeg);
450 static void JPEG_SetColorGrayScale(JPEG_HandleTypeDef *hjpeg);
451 static void JPEG_SetColorCMYK(JPEG_HandleTypeDef *hjpeg);
452 
453 static void JPEG_Init_Process(JPEG_HandleTypeDef *hjpeg);
454 static uint32_t JPEG_Process(JPEG_HandleTypeDef *hjpeg);
455 static void JPEG_ReadInputData(JPEG_HandleTypeDef *hjpeg, uint32_t nbRequestWords);
456 static void JPEG_StoreOutputData(JPEG_HandleTypeDef *hjpeg, uint32_t nbOutputWords);
457 static uint32_t JPEG_GetQuality(JPEG_HandleTypeDef *hjpeg);
458 
459 static HAL_StatusTypeDef JPEG_DMA_StartProcess(JPEG_HandleTypeDef *hjpeg);
460 static void JPEG_DMA_ContinueProcess(JPEG_HandleTypeDef *hjpeg);
461 static void JPEG_DMA_EndProcess(JPEG_HandleTypeDef *hjpeg);
462 static void JPEG_DMA_PollResidualData(JPEG_HandleTypeDef *hjpeg);
463 static void JPEG_DMAOutCpltCallback(DMA_HandleTypeDef *hdma);
464 static void JPEG_DMAInCpltCallback(DMA_HandleTypeDef *hdma);
465 static void JPEG_DMAErrorCallback(DMA_HandleTypeDef *hdma);
466 static void JPEG_DMAOutAbortCallback(DMA_HandleTypeDef *hdma)  ;
467 
468 /**
469   * @}
470   */
471 
472 /** @defgroup JPEG_Exported_Functions JPEG Exported Functions
473   * @{
474   */
475 
476 /** @defgroup JPEG_Exported_Functions_Group1 Initialization and de-initialization functions
477   *  @brief    Initialization and de-initialization functions.
478   *
479 @verbatim
480   ==============================================================================
481               ##### Initialization and de-initialization functions #####
482   ==============================================================================
483     [..]  This section provides functions allowing to:
484       (+) Initialize the JPEG peripheral and creates the associated handle
485       (+) DeInitialize the JPEG peripheral
486 
487 @endverbatim
488   * @{
489   */
490 
491 /**
492   * @brief  Initializes the JPEG according to the specified
493   *         parameters in the JPEG_InitTypeDef and creates the associated handle.
494   * @param  hjpeg pointer to a JPEG_HandleTypeDef structure that contains
495   *         the configuration information for JPEG module
496   * @retval HAL status
497   */
HAL_JPEG_Init(JPEG_HandleTypeDef * hjpeg)498 HAL_StatusTypeDef HAL_JPEG_Init(JPEG_HandleTypeDef *hjpeg)
499 {
500   /* These are the sample quantization tables given in JPEG spec ISO/IEC 10918-1 standard , section K.1. */
501   static const uint8_t JPEG_LUM_QuantTable[JPEG_QUANT_TABLE_SIZE] =
502   {
503     16,  11,  10,  16,  24,  40,  51,  61,
504     12,  12,  14,  19,  26,  58,  60,  55,
505     14,  13,  16,  24,  40,  57,  69,  56,
506     14,  17,  22,  29,  51,  87,  80,  62,
507     18,  22,  37,  56,  68, 109, 103,  77,
508     24,  35,  55,  64,  81, 104, 113,  92,
509     49,  64,  78,  87, 103, 121, 120, 101,
510     72,  92,  95,  98, 112, 100, 103,  99
511   };
512   static const uint8_t JPEG_CHROM_QuantTable[JPEG_QUANT_TABLE_SIZE] =
513   {
514     17,  18,  24,  47,  99,  99,  99,  99,
515     18,  21,  26,  66,  99,  99,  99,  99,
516     24,  26,  56,  99,  99,  99,  99,  99,
517     47,  66,  99,  99,  99,  99,  99,  99,
518     99,  99,  99,  99,  99,  99,  99,  99,
519     99,  99,  99,  99,  99,  99,  99,  99,
520     99,  99,  99,  99,  99,  99,  99,  99,
521     99,  99,  99,  99,  99,  99,  99,  99
522   };
523 
524   /* Check the JPEG handle allocation */
525   if (hjpeg == NULL)
526   {
527     return HAL_ERROR;
528   }
529 
530 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
531   if (hjpeg->State == HAL_JPEG_STATE_RESET)
532   {
533     /* Allocate lock resource and initialize it */
534     hjpeg->Lock = HAL_UNLOCKED;
535 
536     hjpeg->InfoReadyCallback  = HAL_JPEG_InfoReadyCallback;  /* Legacy weak InfoReadyCallback  */
537     hjpeg->EncodeCpltCallback = HAL_JPEG_EncodeCpltCallback; /* Legacy weak EncodeCpltCallback */
538     hjpeg->DecodeCpltCallback = HAL_JPEG_DecodeCpltCallback; /* Legacy weak DecodeCpltCallback */
539     hjpeg->ErrorCallback      = HAL_JPEG_ErrorCallback;      /* Legacy weak ErrorCallback      */
540     hjpeg->GetDataCallback    = HAL_JPEG_GetDataCallback;    /* Legacy weak GetDataCallback    */
541     hjpeg->DataReadyCallback  = HAL_JPEG_DataReadyCallback;  /* Legacy weak DataReadyCallback  */
542 
543     if (hjpeg->MspInitCallback == NULL)
544     {
545       hjpeg->MspInitCallback = HAL_JPEG_MspInit; /* Legacy weak MspInit  */
546     }
547 
548     /* Init the low level hardware */
549     hjpeg->MspInitCallback(hjpeg);
550   }
551 #else
552   if (hjpeg->State == HAL_JPEG_STATE_RESET)
553   {
554     /* Allocate lock resource and initialize it */
555     hjpeg->Lock = HAL_UNLOCKED;
556 
557     /* Init the low level hardware : GPIO, CLOCK */
558     HAL_JPEG_MspInit(hjpeg);
559   }
560 #endif /* USE_HAL_JPEG_REGISTER_CALLBACKS */
561 
562   /* Change the JPEG state */
563   hjpeg->State = HAL_JPEG_STATE_BUSY;
564 
565   /* Start the JPEG Core*/
566   __HAL_JPEG_ENABLE(hjpeg);
567 
568   /* Stop the JPEG encoding/decoding process*/
569   hjpeg->Instance->CONFR0 &=  ~JPEG_CONFR0_START;
570 
571   /* Disable All Interrupts */
572   __HAL_JPEG_DISABLE_IT(hjpeg, JPEG_INTERRUPT_MASK);
573 
574   /* Disable All DMA requests */
575   JPEG_DISABLE_DMA(hjpeg, JPEG_DMA_MASK);
576 
577   /* Flush input and output FIFOs*/
578   hjpeg->Instance->CR |= JPEG_CR_IFF;
579   hjpeg->Instance->CR |= JPEG_CR_OFF;
580 
581   /* Clear all flags */
582   __HAL_JPEG_CLEAR_FLAG(hjpeg, JPEG_FLAG_ALL);
583 
584   /* init default quantization tables*/
585   hjpeg->QuantTable0 = (uint8_t *)((uint32_t)JPEG_LUM_QuantTable);
586   hjpeg->QuantTable1 = (uint8_t *)((uint32_t)JPEG_CHROM_QuantTable);
587   hjpeg->QuantTable2 = NULL;
588   hjpeg->QuantTable3 = NULL;
589 
590   /* init the default Huffman tables*/
591   if (JPEG_Set_HuffEnc_Mem(hjpeg) != HAL_OK)
592   {
593     hjpeg->ErrorCode = HAL_JPEG_ERROR_HUFF_TABLE;
594 
595     return HAL_ERROR;
596   }
597 
598   /* Enable header processing*/
599   hjpeg->Instance->CONFR1 |= JPEG_CONFR1_HDR;
600 
601   /* Reset JpegInCount and JpegOutCount */
602   hjpeg->JpegInCount = 0;
603   hjpeg->JpegOutCount = 0;
604 
605   /* Change the JPEG state */
606   hjpeg->State = HAL_JPEG_STATE_READY;
607 
608   /* Reset the JPEG ErrorCode */
609   hjpeg->ErrorCode = HAL_JPEG_ERROR_NONE;
610 
611   /*Clear the context filelds*/
612   hjpeg->Context = 0;
613 
614   /* Return function status */
615   return HAL_OK;
616 }
617 
618 /**
619   * @brief  DeInitializes the JPEG peripheral.
620   * @param  hjpeg pointer to a JPEG_HandleTypeDef structure that contains
621   *         the configuration information for JPEG module
622   * @retval HAL status
623   */
HAL_JPEG_DeInit(JPEG_HandleTypeDef * hjpeg)624 HAL_StatusTypeDef HAL_JPEG_DeInit(JPEG_HandleTypeDef *hjpeg)
625 {
626   /* Check the JPEG handle allocation */
627   if (hjpeg == NULL)
628   {
629     return HAL_ERROR;
630   }
631 
632 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
633   if (hjpeg->MspDeInitCallback == NULL)
634   {
635     hjpeg->MspDeInitCallback = HAL_JPEG_MspDeInit; /* Legacy weak MspDeInit  */
636   }
637 
638   /* DeInit the low level hardware */
639   hjpeg->MspDeInitCallback(hjpeg);
640 
641 #else
642   /* DeInit the low level hardware: CLOCK, NVIC.*/
643   HAL_JPEG_MspDeInit(hjpeg);
644 #endif /* USE_HAL_JPEG_REGISTER_CALLBACKS */
645 
646   /* Change the JPEG state */
647   hjpeg->State = HAL_JPEG_STATE_BUSY;
648 
649   /* Reset the JPEG ErrorCode */
650   hjpeg->ErrorCode = HAL_JPEG_ERROR_NONE;
651 
652   /* Reset JpegInCount and JpegOutCount */
653   hjpeg->JpegInCount = 0;
654   hjpeg->JpegOutCount = 0;
655 
656   /* Change the JPEG state */
657   hjpeg->State = HAL_JPEG_STATE_RESET;
658 
659   /*Clear the context fields*/
660   hjpeg->Context = 0;
661 
662   /* Release Lock */
663   __HAL_UNLOCK(hjpeg);
664 
665   /* Return function status */
666   return HAL_OK;
667 }
668 
669 /**
670   * @brief  Initializes the JPEG MSP.
671   * @param  hjpeg pointer to a JPEG_HandleTypeDef structure that contains
672   *         the configuration information for JPEG module
673   * @retval None
674   */
HAL_JPEG_MspInit(JPEG_HandleTypeDef * hjpeg)675 __weak void HAL_JPEG_MspInit(JPEG_HandleTypeDef *hjpeg)
676 {
677   /* Prevent unused argument(s) compilation warning */
678   UNUSED(hjpeg);
679 
680   /* NOTE : This function Should not be modified, when the callback is needed,
681             the HAL_JPEG_MspInit could be implemented in the user file
682    */
683 }
684 
685 /**
686   * @brief  DeInitializes JPEG MSP.
687   * @param  hjpeg pointer to a JPEG_HandleTypeDef structure that contains
688   *         the configuration information for JPEG module
689   * @retval None
690   */
HAL_JPEG_MspDeInit(JPEG_HandleTypeDef * hjpeg)691 __weak void HAL_JPEG_MspDeInit(JPEG_HandleTypeDef *hjpeg)
692 {
693   /* Prevent unused argument(s) compilation warning */
694   UNUSED(hjpeg);
695 
696   /* NOTE : This function Should not be modified, when the callback is needed,
697             the HAL_JPEG_MspDeInit could be implemented in the user file
698    */
699 }
700 
701 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
702 /**
703   * @brief  Register a User JPEG Callback
704   *         To be used instead of the weak predefined callback
705   * @param  hjpeg JPEG handle
706   * @param  CallbackID ID of the callback to be registered
707   *         This parameter can be one of the following values:
708   *          @arg @ref HAL_JPEG_ENCODE_CPLT_CB_ID Encode Complete callback ID
709   *          @arg @ref HAL_JPEG_DECODE_CPLT_CB_ID Decode Complete callback ID
710   *          @arg @ref HAL_JPEG_ERROR_CB_ID Error callback ID
711   *          @arg @ref HAL_JPEG_MSPINIT_CB_ID MspInit callback ID
712   *          @arg @ref HAL_JPEG_MSPDEINIT_CB_ID MspDeInit callback ID
713   * @param  pCallback pointer to the Callback function
714   * @retval HAL status
715   */
HAL_JPEG_RegisterCallback(JPEG_HandleTypeDef * hjpeg,HAL_JPEG_CallbackIDTypeDef CallbackID,pJPEG_CallbackTypeDef pCallback)716 HAL_StatusTypeDef HAL_JPEG_RegisterCallback(JPEG_HandleTypeDef *hjpeg, HAL_JPEG_CallbackIDTypeDef CallbackID,
717                                             pJPEG_CallbackTypeDef pCallback)
718 {
719   HAL_StatusTypeDef status = HAL_OK;
720 
721   if (pCallback == NULL)
722   {
723     /* Update the error code */
724     hjpeg->ErrorCode |= HAL_JPEG_ERROR_INVALID_CALLBACK;
725     return HAL_ERROR;
726   }
727   /* Process locked */
728   __HAL_LOCK(hjpeg);
729 
730   if (HAL_JPEG_STATE_READY == hjpeg->State)
731   {
732     switch (CallbackID)
733     {
734       case HAL_JPEG_ENCODE_CPLT_CB_ID :
735         hjpeg->EncodeCpltCallback = pCallback;
736         break;
737 
738       case HAL_JPEG_DECODE_CPLT_CB_ID :
739         hjpeg->DecodeCpltCallback = pCallback;
740         break;
741 
742       case HAL_JPEG_ERROR_CB_ID :
743         hjpeg->ErrorCallback = pCallback;
744         break;
745 
746       case HAL_JPEG_MSPINIT_CB_ID :
747         hjpeg->MspInitCallback = pCallback;
748         break;
749 
750       case HAL_JPEG_MSPDEINIT_CB_ID :
751         hjpeg->MspDeInitCallback = pCallback;
752         break;
753 
754       default :
755         /* Update the error code */
756         hjpeg->ErrorCode |= HAL_JPEG_ERROR_INVALID_CALLBACK;
757         /* Return error status */
758         status =  HAL_ERROR;
759         break;
760     }
761   }
762   else if (HAL_JPEG_STATE_RESET == hjpeg->State)
763   {
764     switch (CallbackID)
765     {
766       case HAL_JPEG_MSPINIT_CB_ID :
767         hjpeg->MspInitCallback = pCallback;
768         break;
769 
770       case HAL_JPEG_MSPDEINIT_CB_ID :
771         hjpeg->MspDeInitCallback = pCallback;
772         break;
773 
774       default :
775         /* Update the error code */
776         hjpeg->ErrorCode |= HAL_JPEG_ERROR_INVALID_CALLBACK;
777         /* Return error status */
778         status =  HAL_ERROR;
779         break;
780     }
781   }
782   else
783   {
784     /* Update the error code */
785     hjpeg->ErrorCode |= HAL_JPEG_ERROR_INVALID_CALLBACK;
786     /* Return error status */
787     status =  HAL_ERROR;
788   }
789 
790   /* Release Lock */
791   __HAL_UNLOCK(hjpeg);
792   return status;
793 }
794 
795 /**
796   * @brief  Unregister a JPEG Callback
797   *         JPEG callabck is redirected to the weak predefined callback
798   * @param  hjpeg JPEG handle
799   * @param  CallbackID ID of the callback to be unregistered
800   *         This parameter can be one of the following values:
801   *         This parameter can be one of the following values:
802   *          @arg @ref HAL_JPEG_ENCODE_CPLT_CB_ID Encode Complete callback ID
803   *          @arg @ref HAL_JPEG_DECODE_CPLT_CB_ID Decode Complete callback ID
804   *          @arg @ref HAL_JPEG_ERROR_CB_ID Error callback ID
805   *          @arg @ref HAL_JPEG_MSPINIT_CB_ID MspInit callback ID
806   *          @arg @ref HAL_JPEG_MSPDEINIT_CB_ID MspDeInit callback ID
807   * @retval HAL status
808   */
HAL_JPEG_UnRegisterCallback(JPEG_HandleTypeDef * hjpeg,HAL_JPEG_CallbackIDTypeDef CallbackID)809 HAL_StatusTypeDef HAL_JPEG_UnRegisterCallback(JPEG_HandleTypeDef *hjpeg, HAL_JPEG_CallbackIDTypeDef CallbackID)
810 {
811   HAL_StatusTypeDef status = HAL_OK;
812 
813   /* Process locked */
814   __HAL_LOCK(hjpeg);
815 
816   if (HAL_JPEG_STATE_READY == hjpeg->State)
817   {
818     switch (CallbackID)
819     {
820       case HAL_JPEG_ENCODE_CPLT_CB_ID :
821         hjpeg->EncodeCpltCallback = HAL_JPEG_EncodeCpltCallback; /* Legacy weak EncodeCpltCallback  */
822         break;
823 
824       case HAL_JPEG_DECODE_CPLT_CB_ID :
825         hjpeg->DecodeCpltCallback = HAL_JPEG_DecodeCpltCallback; /* Legacy weak DecodeCpltCallback  */
826         break;
827 
828       case HAL_JPEG_ERROR_CB_ID :
829         hjpeg->ErrorCallback = HAL_JPEG_ErrorCallback;           /* Legacy weak ErrorCallback  */
830         break;
831 
832       case HAL_JPEG_MSPINIT_CB_ID :
833         hjpeg->MspInitCallback = HAL_JPEG_MspInit;              /* Legacy weak MspInit  */
834         break;
835 
836       case HAL_JPEG_MSPDEINIT_CB_ID :
837         hjpeg->MspDeInitCallback = HAL_JPEG_MspDeInit;          /* Legacy weak MspDeInit  */
838         break;
839 
840       default :
841         /* Update the error code */
842         hjpeg->ErrorCode |= HAL_JPEG_ERROR_INVALID_CALLBACK;
843         /* Return error status */
844         status =  HAL_ERROR;
845         break;
846     }
847   }
848   else if (HAL_JPEG_STATE_RESET == hjpeg->State)
849   {
850     switch (CallbackID)
851     {
852       case HAL_JPEG_MSPINIT_CB_ID :
853         hjpeg->MspInitCallback = HAL_JPEG_MspInit;           /* Legacy weak MspInit  */
854         break;
855 
856       case HAL_JPEG_MSPDEINIT_CB_ID :
857         hjpeg->MspDeInitCallback = HAL_JPEG_MspDeInit;       /* Legacy weak MspInit  */
858         break;
859 
860       default :
861         /* Update the error code */
862         hjpeg->ErrorCode |= HAL_JPEG_ERROR_INVALID_CALLBACK;
863         /* Return error status */
864         status =  HAL_ERROR;
865         break;
866     }
867   }
868   else
869   {
870     /* Update the error code */
871     hjpeg->ErrorCode |= HAL_JPEG_ERROR_INVALID_CALLBACK;
872     /* Return error status */
873     status =  HAL_ERROR;
874   }
875 
876   /* Release Lock */
877   __HAL_UNLOCK(hjpeg);
878   return status;
879 }
880 
881 /**
882   * @brief  Register Info Ready JPEG Callback
883   *         To be used instead of the weak HAL_JPEG_InfoReadyCallback() predefined callback
884   * @param  hjpeg JPEG handle
885   * @param  pCallback pointer to the Info Ready Callback function
886   * @retval HAL status
887   */
HAL_JPEG_RegisterInfoReadyCallback(JPEG_HandleTypeDef * hjpeg,pJPEG_InfoReadyCallbackTypeDef pCallback)888 HAL_StatusTypeDef HAL_JPEG_RegisterInfoReadyCallback(JPEG_HandleTypeDef *hjpeg,
889                                                      pJPEG_InfoReadyCallbackTypeDef pCallback)
890 {
891   HAL_StatusTypeDef status = HAL_OK;
892 
893   if (pCallback == NULL)
894   {
895     /* Update the error code */
896     hjpeg->ErrorCode |= HAL_JPEG_ERROR_INVALID_CALLBACK;
897     return HAL_ERROR;
898   }
899   /* Process locked */
900   __HAL_LOCK(hjpeg);
901 
902   if (HAL_JPEG_STATE_READY == hjpeg->State)
903   {
904     hjpeg->InfoReadyCallback = pCallback;
905   }
906   else
907   {
908     /* Update the error code */
909     hjpeg->ErrorCode |= HAL_JPEG_ERROR_INVALID_CALLBACK;
910     /* Return error status */
911     status =  HAL_ERROR;
912   }
913 
914   /* Release Lock */
915   __HAL_UNLOCK(hjpeg);
916   return status;
917 }
918 
919 /**
920   * @brief  UnRegister the Info Ready JPEG Callback
921   *         Info Ready JPEG Callback is redirected to the weak HAL_JPEG_InfoReadyCallback() predefined callback
922   * @param  hjpeg JPEG handle
923   * @retval HAL status
924   */
HAL_JPEG_UnRegisterInfoReadyCallback(JPEG_HandleTypeDef * hjpeg)925 HAL_StatusTypeDef HAL_JPEG_UnRegisterInfoReadyCallback(JPEG_HandleTypeDef *hjpeg)
926 {
927   HAL_StatusTypeDef status = HAL_OK;
928 
929   /* Process locked */
930   __HAL_LOCK(hjpeg);
931 
932   if (HAL_JPEG_STATE_READY == hjpeg->State)
933   {
934     hjpeg->InfoReadyCallback = HAL_JPEG_InfoReadyCallback; /* Legacy weak InfoReadyCallback  */
935   }
936   else
937   {
938     /* Update the error code */
939     hjpeg->ErrorCode |= HAL_JPEG_ERROR_INVALID_CALLBACK;
940     /* Return error status */
941     status =  HAL_ERROR;
942   }
943 
944   /* Release Lock */
945   __HAL_UNLOCK(hjpeg);
946   return status;
947 }
948 
949 /**
950   * @brief  Register Get Data JPEG Callback
951   *         To be used instead of the weak HAL_JPEG_GetDataCallback() predefined callback
952   * @param  hjpeg JPEG handle
953   * @param  pCallback pointer to the Get Data Callback function
954   * @retval HAL status
955   */
HAL_JPEG_RegisterGetDataCallback(JPEG_HandleTypeDef * hjpeg,pJPEG_GetDataCallbackTypeDef pCallback)956 HAL_StatusTypeDef HAL_JPEG_RegisterGetDataCallback(JPEG_HandleTypeDef *hjpeg, pJPEG_GetDataCallbackTypeDef pCallback)
957 {
958   HAL_StatusTypeDef status = HAL_OK;
959 
960   if (pCallback == NULL)
961   {
962     /* Update the error code */
963     hjpeg->ErrorCode |= HAL_JPEG_ERROR_INVALID_CALLBACK;
964     return HAL_ERROR;
965   }
966   /* Process locked */
967   __HAL_LOCK(hjpeg);
968 
969   if (HAL_JPEG_STATE_READY == hjpeg->State)
970   {
971     hjpeg->GetDataCallback = pCallback;
972   }
973   else
974   {
975     /* Update the error code */
976     hjpeg->ErrorCode |= HAL_JPEG_ERROR_INVALID_CALLBACK;
977     /* Return error status */
978     status =  HAL_ERROR;
979   }
980 
981   /* Release Lock */
982   __HAL_UNLOCK(hjpeg);
983   return status;
984 }
985 
986 /**
987   * @brief  UnRegister the Get Data JPEG Callback
988   *         Get Data JPEG Callback is redirected to the weak HAL_JPEG_GetDataCallback() predefined callback
989   * @param  hjpeg JPEG handle
990   * @retval HAL status
991   */
HAL_JPEG_UnRegisterGetDataCallback(JPEG_HandleTypeDef * hjpeg)992 HAL_StatusTypeDef HAL_JPEG_UnRegisterGetDataCallback(JPEG_HandleTypeDef *hjpeg)
993 {
994   HAL_StatusTypeDef status = HAL_OK;
995 
996   /* Process locked */
997   __HAL_LOCK(hjpeg);
998 
999   if (HAL_JPEG_STATE_READY == hjpeg->State)
1000   {
1001     hjpeg->GetDataCallback = HAL_JPEG_GetDataCallback;  /* Legacy weak GetDataCallback  */
1002   }
1003   else
1004   {
1005     /* Update the error code */
1006     hjpeg->ErrorCode |= HAL_JPEG_ERROR_INVALID_CALLBACK;
1007     /* Return error status */
1008     status =  HAL_ERROR;
1009   }
1010 
1011   /* Release Lock */
1012   __HAL_UNLOCK(hjpeg);
1013   return status;
1014 }
1015 
1016 /**
1017   * @brief  Register Data Ready JPEG Callback
1018   *         To be used instead of the weak HAL_JPEG_DataReadyCallback() predefined callback
1019   * @param  hjpeg JPEG handle
1020   * @param  pCallback pointer to the Get Data Callback function
1021   * @retval HAL status
1022   */
HAL_JPEG_RegisterDataReadyCallback(JPEG_HandleTypeDef * hjpeg,pJPEG_DataReadyCallbackTypeDef pCallback)1023 HAL_StatusTypeDef HAL_JPEG_RegisterDataReadyCallback(JPEG_HandleTypeDef *hjpeg,
1024                                                      pJPEG_DataReadyCallbackTypeDef pCallback)
1025 {
1026   HAL_StatusTypeDef status = HAL_OK;
1027 
1028   if (pCallback == NULL)
1029   {
1030     /* Update the error code */
1031     hjpeg->ErrorCode |= HAL_JPEG_ERROR_INVALID_CALLBACK;
1032     return HAL_ERROR;
1033   }
1034   /* Process locked */
1035   __HAL_LOCK(hjpeg);
1036 
1037   if (HAL_JPEG_STATE_READY == hjpeg->State)
1038   {
1039     hjpeg->DataReadyCallback = pCallback;
1040   }
1041   else
1042   {
1043     /* Update the error code */
1044     hjpeg->ErrorCode |= HAL_JPEG_ERROR_INVALID_CALLBACK;
1045     /* Return error status */
1046     status =  HAL_ERROR;
1047   }
1048 
1049   /* Release Lock */
1050   __HAL_UNLOCK(hjpeg);
1051   return status;
1052 }
1053 
1054 /**
1055   * @brief  UnRegister the Data Ready JPEG Callback
1056   *         Get Data Ready Callback is redirected to the weak HAL_JPEG_DataReadyCallback() predefined callback
1057   * @param  hjpeg JPEG handle
1058   * @retval HAL status
1059   */
HAL_JPEG_UnRegisterDataReadyCallback(JPEG_HandleTypeDef * hjpeg)1060 HAL_StatusTypeDef HAL_JPEG_UnRegisterDataReadyCallback(JPEG_HandleTypeDef *hjpeg)
1061 {
1062   HAL_StatusTypeDef status = HAL_OK;
1063 
1064   /* Process locked */
1065   __HAL_LOCK(hjpeg);
1066 
1067   if (HAL_JPEG_STATE_READY == hjpeg->State)
1068   {
1069     hjpeg->DataReadyCallback = HAL_JPEG_DataReadyCallback;  /* Legacy weak DataReadyCallback  */
1070   }
1071   else
1072   {
1073     /* Update the error code */
1074     hjpeg->ErrorCode |= HAL_JPEG_ERROR_INVALID_CALLBACK;
1075     /* Return error status */
1076     status =  HAL_ERROR;
1077   }
1078 
1079   /* Release Lock */
1080   __HAL_UNLOCK(hjpeg);
1081   return status;
1082 }
1083 
1084 #endif /* USE_HAL_JPEG_REGISTER_CALLBACKS */
1085 
1086 /**
1087   * @}
1088   */
1089 
1090 /** @defgroup JPEG_Exported_Functions_Group2 Configuration functions
1091   *  @brief    JPEG Configuration functions.
1092   *
1093 @verbatim
1094   ==============================================================================
1095               ##### Configuration functions #####
1096   ==============================================================================
1097     [..]  This section provides functions allowing to:
1098       (+) HAL_JPEG_ConfigEncoding() : JPEG encoding configuration
1099       (+) HAL_JPEG_GetInfo() :  Extract the image configuration from the JPEG header during the decoding
1100       (+) HAL_JPEG_EnableHeaderParsing() :  Enable JPEG Header parsing for decoding
1101       (+) HAL_JPEG_DisableHeaderParsing() : Disable JPEG Header parsing for decoding
1102       (+) HAL_JPEG_SetUserQuantTables : Modify the default Quantization tables used for JPEG encoding.
1103 
1104 @endverbatim
1105   * @{
1106   */
1107 
1108 /**
1109   * @brief  Set the JPEG encoding configuration.
1110   * @param  hjpeg pointer to a JPEG_HandleTypeDef structure that contains
1111   *         the configuration information for JPEG module
1112   * @param  pConf pointer to a JPEG_ConfTypeDef structure that contains
1113   *         the encoding configuration
1114   * @retval HAL status
1115   */
HAL_JPEG_ConfigEncoding(JPEG_HandleTypeDef * hjpeg,JPEG_ConfTypeDef * pConf)1116 HAL_StatusTypeDef HAL_JPEG_ConfigEncoding(JPEG_HandleTypeDef *hjpeg, JPEG_ConfTypeDef *pConf)
1117 {
1118   uint32_t error;
1119   uint32_t numberMCU;
1120   uint32_t hfactor;
1121   uint32_t vfactor;
1122   uint32_t hMCU;
1123   uint32_t vMCU;
1124 
1125   /* Check the JPEG handle allocation */
1126   if ((hjpeg == NULL) || (pConf == NULL))
1127   {
1128     return HAL_ERROR;
1129   }
1130   else
1131   {
1132     /* Check the parameters */
1133     assert_param(IS_JPEG_COLORSPACE(pConf->ColorSpace));
1134     assert_param(IS_JPEG_CHROMASUBSAMPLING(pConf->ChromaSubsampling));
1135     assert_param(IS_JPEG_IMAGE_QUALITY(pConf->ImageQuality));
1136 
1137     /* Process Locked */
1138     __HAL_LOCK(hjpeg);
1139 
1140     if (hjpeg->State == HAL_JPEG_STATE_READY)
1141     {
1142       hjpeg->State = HAL_JPEG_STATE_BUSY;
1143 
1144       hjpeg->Conf.ColorSpace          =  pConf->ColorSpace;
1145       hjpeg->Conf.ChromaSubsampling   =  pConf->ChromaSubsampling;
1146       hjpeg->Conf.ImageHeight         =  pConf->ImageHeight;
1147       hjpeg->Conf.ImageWidth          =  pConf->ImageWidth;
1148       hjpeg->Conf.ImageQuality        =  pConf->ImageQuality;
1149 
1150       /* Reset the Color Space : by default only one quantization table is used*/
1151       hjpeg->Instance->CONFR1 &= ~JPEG_CONFR1_COLORSPACE;
1152 
1153       /* Set Number of color components*/
1154       if (hjpeg->Conf.ColorSpace == JPEG_GRAYSCALE_COLORSPACE)
1155       {
1156         /*Gray Scale is only one component 8x8 blocks i.e 4:4:4*/
1157         hjpeg->Conf.ChromaSubsampling = JPEG_444_SUBSAMPLING;
1158 
1159         JPEG_SetColorGrayScale(hjpeg);
1160         /* Set quantization table 0*/
1161         error = JPEG_Set_Quantization_Mem(hjpeg, hjpeg->QuantTable0, (hjpeg->Instance->QMEM0));
1162       }
1163       else if (hjpeg->Conf.ColorSpace == JPEG_YCBCR_COLORSPACE)
1164       {
1165         /*
1166            Set the Color Space for YCbCr : 2 quantization tables are used
1167            one for Luminance(Y) and one for both Chrominances (Cb & Cr)
1168           */
1169         hjpeg->Instance->CONFR1 |= JPEG_CONFR1_COLORSPACE_0;
1170 
1171         JPEG_SetColorYCBCR(hjpeg);
1172 
1173         /* Set quantization table 0*/
1174         error  = JPEG_Set_Quantization_Mem(hjpeg, hjpeg->QuantTable0, (hjpeg->Instance->QMEM0));
1175         /*By default quantization table 0 for component 0 and quantization table 1 for both components 1 and 2*/
1176         error |= JPEG_Set_Quantization_Mem(hjpeg, hjpeg->QuantTable1, (hjpeg->Instance->QMEM1));
1177 
1178         if ((hjpeg->Context & JPEG_CONTEXT_CUSTOM_TABLES) != 0UL)
1179         {
1180           /*Use user customized quantization tables , 1 table per component*/
1181           /* use 3 quantization tables , one for each component*/
1182           hjpeg->Instance->CONFR1 &= (~JPEG_CONFR1_COLORSPACE);
1183           hjpeg->Instance->CONFR1 |= JPEG_CONFR1_COLORSPACE_1;
1184 
1185           error |= JPEG_Set_Quantization_Mem(hjpeg, hjpeg->QuantTable2, (hjpeg->Instance->QMEM2));
1186 
1187           /*Use Quantization 1 table for component 1*/
1188           hjpeg->Instance->CONFR5 &= (~JPEG_CONFR5_QT);
1189           hjpeg->Instance->CONFR5 |=  JPEG_CONFR5_QT_0;
1190 
1191           /*Use Quantization 2 table for component 2*/
1192           hjpeg->Instance->CONFR6 &= (~JPEG_CONFR6_QT);
1193           hjpeg->Instance->CONFR6 |=  JPEG_CONFR6_QT_1;
1194         }
1195       }
1196       else /* ColorSpace == JPEG_CMYK_COLORSPACE */
1197       {
1198         JPEG_SetColorCMYK(hjpeg);
1199 
1200         /* Set quantization table 0*/
1201         error  = JPEG_Set_Quantization_Mem(hjpeg, hjpeg->QuantTable0, (hjpeg->Instance->QMEM0));
1202         /*By default quantization table 0 for All components*/
1203 
1204         if ((hjpeg->Context & JPEG_CONTEXT_CUSTOM_TABLES) != 0UL)
1205         {
1206           /*Use user customized quantization tables , 1 table per component*/
1207           /* use 4 quantization tables , one for each component*/
1208           hjpeg->Instance->CONFR1 |= JPEG_CONFR1_COLORSPACE;
1209 
1210           error |= JPEG_Set_Quantization_Mem(hjpeg, hjpeg->QuantTable1, (hjpeg->Instance->QMEM1));
1211           error |= JPEG_Set_Quantization_Mem(hjpeg, hjpeg->QuantTable2, (hjpeg->Instance->QMEM2));
1212           error |= JPEG_Set_Quantization_Mem(hjpeg, hjpeg->QuantTable3, (hjpeg->Instance->QMEM3));
1213 
1214           /*Use Quantization 1 table for component 1*/
1215           hjpeg->Instance->CONFR5 |=  JPEG_CONFR5_QT_0;
1216 
1217           /*Use Quantization 2 table for component 2*/
1218           hjpeg->Instance->CONFR6 |=  JPEG_CONFR6_QT_1;
1219 
1220           /*Use Quantization 3 table for component 3*/
1221           hjpeg->Instance->CONFR7 |=  JPEG_CONFR7_QT;
1222         }
1223       }
1224 
1225       if (error != 0UL)
1226       {
1227         hjpeg->ErrorCode = HAL_JPEG_ERROR_QUANT_TABLE;
1228 
1229         /* Process Unlocked */
1230         __HAL_UNLOCK(hjpeg);
1231 
1232         /* Set the JPEG State to ready */
1233         hjpeg->State = HAL_JPEG_STATE_READY;
1234 
1235         return  HAL_ERROR;
1236       }
1237       /* Set the image size*/
1238       /* set the number of lines*/
1239       MODIFY_REG(hjpeg->Instance->CONFR1, JPEG_CONFR1_YSIZE, ((hjpeg->Conf.ImageHeight & 0x0000FFFFUL) << 16));
1240       /* set the number of pixels per line*/
1241       MODIFY_REG(hjpeg->Instance->CONFR3, JPEG_CONFR3_XSIZE, ((hjpeg->Conf.ImageWidth & 0x0000FFFFUL) << 16));
1242 
1243 
1244       if (hjpeg->Conf.ChromaSubsampling == JPEG_420_SUBSAMPLING) /* 4:2:0*/
1245       {
1246         hfactor = 16;
1247         vfactor = 16;
1248       }
1249       else if (hjpeg->Conf.ChromaSubsampling == JPEG_422_SUBSAMPLING) /* 4:2:2*/
1250       {
1251         hfactor = 16;
1252         vfactor = 8;
1253       }
1254       else /* Default is 8x8 MCU,  4:4:4*/
1255       {
1256         hfactor = 8;
1257         vfactor = 8;
1258       }
1259 
1260       hMCU = (hjpeg->Conf.ImageWidth / hfactor);
1261       if ((hjpeg->Conf.ImageWidth % hfactor) != 0UL)
1262       {
1263         hMCU++; /*+1 for horizontal incomplete MCU */
1264       }
1265 
1266       vMCU = (hjpeg->Conf.ImageHeight / vfactor);
1267       if ((hjpeg->Conf.ImageHeight % vfactor) != 0UL)
1268       {
1269         vMCU++; /*+1 for vertical incomplete MCU */
1270       }
1271 
1272       numberMCU = (hMCU * vMCU) - 1UL; /* Bit Field JPEG_CONFR2_NMCU shall be set to NB_MCU - 1*/
1273       /* Set the number of MCU*/
1274       hjpeg->Instance->CONFR2 = (numberMCU & JPEG_CONFR2_NMCU);
1275 
1276       hjpeg->Context |= JPEG_CONTEXT_CONF_ENCODING;
1277 
1278       /* Process Unlocked */
1279       __HAL_UNLOCK(hjpeg);
1280 
1281       /* Set the JPEG State to ready */
1282       hjpeg->State = HAL_JPEG_STATE_READY;
1283 
1284       /* Return function status */
1285       return HAL_OK;
1286     }
1287     else
1288     {
1289       /* Process Unlocked */
1290       __HAL_UNLOCK(hjpeg);
1291 
1292       /* Return function status */
1293       return HAL_BUSY;
1294     }
1295   }
1296 }
1297 
1298 /**
1299   * @brief  Extract the image configuration from the JPEG header during the decoding
1300   * @param  hjpeg pointer to a JPEG_HandleTypeDef structure that contains
1301   *         the configuration information for JPEG module
1302   * @param  pInfo pointer to a JPEG_ConfTypeDef structure that contains
1303   *         The JPEG decoded header information
1304   * @retval HAL status
1305   */
HAL_JPEG_GetInfo(JPEG_HandleTypeDef * hjpeg,JPEG_ConfTypeDef * pInfo)1306 HAL_StatusTypeDef HAL_JPEG_GetInfo(JPEG_HandleTypeDef *hjpeg, JPEG_ConfTypeDef *pInfo)
1307 {
1308   uint32_t yblockNb;
1309   uint32_t cBblockNb;
1310   uint32_t cRblockNb;
1311 
1312   /* Check the JPEG handle allocation */
1313   if ((hjpeg == NULL) || (pInfo == NULL))
1314   {
1315     return HAL_ERROR;
1316   }
1317 
1318   /*Read the conf parameters */
1319   if ((hjpeg->Instance->CONFR1 & JPEG_CONFR1_NF) == JPEG_CONFR1_NF_1)
1320   {
1321     pInfo->ColorSpace = JPEG_YCBCR_COLORSPACE;
1322   }
1323   else if ((hjpeg->Instance->CONFR1 & JPEG_CONFR1_NF) == 0UL)
1324   {
1325     pInfo->ColorSpace = JPEG_GRAYSCALE_COLORSPACE;
1326   }
1327   else if ((hjpeg->Instance->CONFR1 & JPEG_CONFR1_NF) == JPEG_CONFR1_NF)
1328   {
1329     pInfo->ColorSpace = JPEG_CMYK_COLORSPACE;
1330   }
1331   else
1332   {
1333     return HAL_ERROR;
1334   }
1335 
1336   pInfo->ImageHeight = (hjpeg->Instance->CONFR1 & 0xFFFF0000UL) >> 16;
1337   pInfo->ImageWidth  = (hjpeg->Instance->CONFR3 & 0xFFFF0000UL) >> 16;
1338 
1339   if ((pInfo->ColorSpace == JPEG_YCBCR_COLORSPACE) || (pInfo->ColorSpace == JPEG_CMYK_COLORSPACE))
1340   {
1341     yblockNb  = (hjpeg->Instance->CONFR4 & JPEG_CONFR4_NB) >> 4;
1342     cBblockNb = (hjpeg->Instance->CONFR5 & JPEG_CONFR5_NB) >> 4;
1343     cRblockNb = (hjpeg->Instance->CONFR6 & JPEG_CONFR6_NB) >> 4;
1344 
1345     if ((yblockNb == 1UL) && (cBblockNb == 0UL) && (cRblockNb == 0UL))
1346     {
1347       pInfo->ChromaSubsampling = JPEG_422_SUBSAMPLING; /*16x8 block*/
1348     }
1349     else if ((yblockNb == 0UL) && (cBblockNb == 0UL) && (cRblockNb == 0UL))
1350     {
1351       pInfo->ChromaSubsampling = JPEG_444_SUBSAMPLING;
1352     }
1353     else if ((yblockNb == 3UL) && (cBblockNb == 0UL) && (cRblockNb == 0UL))
1354     {
1355       pInfo->ChromaSubsampling = JPEG_420_SUBSAMPLING;
1356     }
1357     else /*Default is 4:4:4*/
1358     {
1359       pInfo->ChromaSubsampling = JPEG_444_SUBSAMPLING;
1360     }
1361   }
1362   else
1363   {
1364     pInfo->ChromaSubsampling = JPEG_444_SUBSAMPLING;
1365   }
1366 
1367   pInfo->ImageQuality = JPEG_GetQuality(hjpeg);
1368 
1369   /* Return function status */
1370   return HAL_OK;
1371 }
1372 
1373 /**
1374   * @brief  Enable JPEG Header parsing for decoding
1375   * @param  hjpeg pointer to a JPEG_HandleTypeDef structure that contains
1376   *               the configuration information for the JPEG.
1377   * @retval HAL status
1378   */
HAL_JPEG_EnableHeaderParsing(JPEG_HandleTypeDef * hjpeg)1379 HAL_StatusTypeDef HAL_JPEG_EnableHeaderParsing(JPEG_HandleTypeDef *hjpeg)
1380 {
1381   /* Process locked */
1382   __HAL_LOCK(hjpeg);
1383 
1384   if (hjpeg->State == HAL_JPEG_STATE_READY)
1385   {
1386     /* Change the JPEG state */
1387     hjpeg->State = HAL_JPEG_STATE_BUSY;
1388 
1389     /* Enable header processing*/
1390     hjpeg->Instance->CONFR1 |= JPEG_CONFR1_HDR;
1391 
1392     /* Process unlocked */
1393     __HAL_UNLOCK(hjpeg);
1394 
1395     /* Change the JPEG state */
1396     hjpeg->State = HAL_JPEG_STATE_READY;
1397 
1398     return HAL_OK;
1399   }
1400   else
1401   {
1402     /* Process unlocked */
1403     __HAL_UNLOCK(hjpeg);
1404 
1405     return HAL_BUSY;
1406   }
1407 }
1408 
1409 /**
1410   * @brief  Disable JPEG Header parsing for decoding
1411   * @param  hjpeg pointer to a JPEG_HandleTypeDef structure that contains
1412   *               the configuration information for the JPEG.
1413   * @retval HAL status
1414   */
HAL_JPEG_DisableHeaderParsing(JPEG_HandleTypeDef * hjpeg)1415 HAL_StatusTypeDef HAL_JPEG_DisableHeaderParsing(JPEG_HandleTypeDef *hjpeg)
1416 {
1417   /* Process locked */
1418   __HAL_LOCK(hjpeg);
1419 
1420   if (hjpeg->State == HAL_JPEG_STATE_READY)
1421   {
1422     /* Change the JPEG state */
1423     hjpeg->State = HAL_JPEG_STATE_BUSY;
1424 
1425     /* Disable header processing*/
1426     hjpeg->Instance->CONFR1 &= ~JPEG_CONFR1_HDR;
1427 
1428     /* Process unlocked */
1429     __HAL_UNLOCK(hjpeg);
1430 
1431     /* Change the JPEG state */
1432     hjpeg->State = HAL_JPEG_STATE_READY;
1433 
1434     return HAL_OK;
1435   }
1436   else
1437   {
1438     /* Process unlocked */
1439     __HAL_UNLOCK(hjpeg);
1440 
1441     return HAL_BUSY;
1442   }
1443 }
1444 
1445 /**
1446   * @brief  Modify the default Quantization tables used for JPEG encoding.
1447   * @param  hjpeg pointer to a JPEG_HandleTypeDef structure that contains
1448   *         the configuration information for JPEG module
1449   * @param  QTable0  pointer to uint8_t , define the user quantification table for color component 1.
1450   *                  If NULL assume no need to update  the table and no error return
1451   * @param  QTable1  pointer to uint8_t , define the user quantification table for color component 2.
1452   *                  If NULL assume no need to update  the table and no error return.
1453   * @param  QTable2  pointer to uint8_t , define the user quantification table for color component 3,
1454   *                  If NULL assume no need to update  the table and no error return.
1455   * @param  QTable3  pointer to uint8_t , define the user quantification table for color component 4.
1456   *                  If NULL assume no need to update  the table and no error return.
1457   *
1458   * @retval HAL status
1459   */
1460 
1461 
HAL_JPEG_SetUserQuantTables(JPEG_HandleTypeDef * hjpeg,uint8_t * QTable0,uint8_t * QTable1,uint8_t * QTable2,uint8_t * QTable3)1462 HAL_StatusTypeDef  HAL_JPEG_SetUserQuantTables(JPEG_HandleTypeDef *hjpeg, uint8_t *QTable0, uint8_t *QTable1,
1463                                                uint8_t *QTable2, uint8_t *QTable3)
1464 {
1465   /* Process Locked */
1466   __HAL_LOCK(hjpeg);
1467 
1468   if (hjpeg->State == HAL_JPEG_STATE_READY)
1469   {
1470     /* Change the DMA state */
1471     hjpeg->State = HAL_JPEG_STATE_BUSY;
1472 
1473     hjpeg->Context |= JPEG_CONTEXT_CUSTOM_TABLES;
1474 
1475     hjpeg->QuantTable0 = QTable0;
1476     hjpeg->QuantTable1 = QTable1;
1477     hjpeg->QuantTable2 = QTable2;
1478     hjpeg->QuantTable3 = QTable3;
1479 
1480     /* Process Unlocked */
1481     __HAL_UNLOCK(hjpeg);
1482 
1483     /* Change the DMA state */
1484     hjpeg->State = HAL_JPEG_STATE_READY;
1485 
1486     /* Return function status */
1487     return HAL_OK;
1488   }
1489   else
1490   {
1491     /* Process Unlocked */
1492     __HAL_UNLOCK(hjpeg);
1493 
1494     return HAL_BUSY;
1495   }
1496 }
1497 
1498 /**
1499   * @}
1500   */
1501 
1502 /** @defgroup JPEG_Exported_Functions_Group3 encoding/decoding processing functions
1503   *  @brief   processing functions.
1504   *
1505 @verbatim
1506   ==============================================================================
1507                       ##### JPEG processing functions #####
1508   ==============================================================================
1509     [..]  This section provides functions allowing to:
1510       (+) HAL_JPEG_Encode()     : JPEG encoding with polling process
1511       (+) HAL_JPEG_Decode()     : JPEG decoding with polling process
1512       (+) HAL_JPEG_Encode_IT()  : JPEG encoding with interrupt process
1513       (+) HAL_JPEG_Decode_IT()  : JPEG decoding with interrupt process
1514       (+) HAL_JPEG_Encode_DMA() : JPEG encoding with DMA process
1515       (+) HAL_JPEG_Decode_DMA() : JPEG decoding with DMA process
1516       (+) HAL_JPEG_Pause()      :   Pause the Input/Output processing
1517       (+) HAL_JPEG_Resume()     :  Resume the JPEG Input/Output processing
1518       (+) HAL_JPEG_ConfigInputBuffer()  : Config Encoding/Decoding Input Buffer
1519       (+) HAL_JPEG_ConfigOutputBuffer() : Config Encoding/Decoding Output Buffer
1520       (+) HAL_JPEG_Abort()              : Aborts the JPEG Encoding/Decoding
1521 
1522 @endverbatim
1523   * @{
1524   */
1525 
1526 /**
1527   * @brief  Starts JPEG encoding with polling processing
1528   * @param  hjpeg pointer to a JPEG_HandleTypeDef structure that contains
1529   *         the configuration information for JPEG module
1530   * @param  pDataInMCU Pointer to the Input buffer
1531   * @param  InDataLength size in bytes Input buffer
1532   * @param  pDataOut Pointer to the jpeg output data buffer
1533   * @param  OutDataLength size in bytes of the Output buffer
1534   * @param  Timeout Specify Timeout value
1535   * @retval HAL status
1536   */
HAL_JPEG_Encode(JPEG_HandleTypeDef * hjpeg,uint8_t * pDataInMCU,uint32_t InDataLength,uint8_t * pDataOut,uint32_t OutDataLength,uint32_t Timeout)1537 HAL_StatusTypeDef  HAL_JPEG_Encode(JPEG_HandleTypeDef *hjpeg, uint8_t *pDataInMCU, uint32_t InDataLength,
1538                                    uint8_t *pDataOut, uint32_t OutDataLength, uint32_t Timeout)
1539 {
1540   uint32_t tickstart;
1541 
1542   /* Check the parameters */
1543   assert_param((InDataLength >= 4UL));
1544   assert_param((OutDataLength >= 4UL));
1545 
1546   /* Check In/out buffer allocation and size */
1547   if ((hjpeg == NULL) || (pDataInMCU == NULL) || (pDataOut == NULL))
1548   {
1549     return HAL_ERROR;
1550   }
1551   /* Process locked */
1552   __HAL_LOCK(hjpeg);
1553 
1554   if (hjpeg->State != HAL_JPEG_STATE_READY)
1555   {
1556     /* Process Unlocked */
1557     __HAL_UNLOCK(hjpeg);
1558 
1559     return HAL_BUSY;
1560   }
1561 
1562   if (hjpeg->State == HAL_JPEG_STATE_READY)
1563   {
1564     if ((hjpeg->Context & JPEG_CONTEXT_CONF_ENCODING) == JPEG_CONTEXT_CONF_ENCODING)
1565     {
1566       /*Change JPEG state*/
1567       hjpeg->State = HAL_JPEG_STATE_BUSY_ENCODING;
1568 
1569       /*Set the Context to Encode with Polling*/
1570       hjpeg->Context &= ~(JPEG_CONTEXT_OPERATION_MASK | JPEG_CONTEXT_METHOD_MASK);
1571       hjpeg->Context |= (JPEG_CONTEXT_ENCODE | JPEG_CONTEXT_POLLING);
1572 
1573       /* Get tick */
1574       tickstart = HAL_GetTick();
1575 
1576       /*Store In/out buffers pointers and size*/
1577       hjpeg->pJpegInBuffPtr = pDataInMCU;
1578       hjpeg->pJpegOutBuffPtr = pDataOut;
1579       hjpeg->InDataLength = InDataLength - (InDataLength % 4UL);    /* In Data length must be multiple of 4 Bytes (1 word)*/
1580       hjpeg->OutDataLength = OutDataLength - (OutDataLength % 4UL); /* Out Data length must be multiple of 4 Bytes (1 word)*/
1581 
1582       /*Reset In/out data counter */
1583       hjpeg->JpegInCount = 0;
1584       hjpeg->JpegOutCount = 0;
1585 
1586       /*Init decoding process*/
1587       JPEG_Init_Process(hjpeg);
1588 
1589       /*JPEG data processing : In/Out FIFO transfer*/
1590       while ((JPEG_Process(hjpeg) == JPEG_PROCESS_ONGOING))
1591       {
1592         if (Timeout != HAL_MAX_DELAY)
1593         {
1594           if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0UL))
1595           {
1596 
1597             /* Update error code */
1598             hjpeg->ErrorCode |= HAL_JPEG_ERROR_TIMEOUT;
1599 
1600             /* Process Unlocked */
1601             __HAL_UNLOCK(hjpeg);
1602 
1603             /*Change JPEG state*/
1604             hjpeg->State = HAL_JPEG_STATE_READY;
1605 
1606             return HAL_TIMEOUT;
1607           }
1608         }
1609       }
1610 
1611       /* Process Unlocked */
1612       __HAL_UNLOCK(hjpeg);
1613 
1614       /*Change JPEG state*/
1615       hjpeg->State = HAL_JPEG_STATE_READY;
1616 
1617     }
1618     else
1619     {
1620       /* Process Unlocked */
1621       __HAL_UNLOCK(hjpeg);
1622 
1623       return HAL_ERROR;
1624     }
1625   }
1626   /* Return function status */
1627   return HAL_OK;
1628 }
1629 
1630 /**
1631   * @brief  Starts JPEG decoding with polling processing
1632   * @param  hjpeg pointer to a JPEG_HandleTypeDef structure that contains
1633   *         the configuration information for JPEG module
1634   * @param  pDataIn Pointer to the input data buffer
1635   * @param  InDataLength size in bytes Input buffer
1636   * @param  pDataOutMCU Pointer to the Output data buffer
1637   * @param  OutDataLength size in bytes of the Output buffer
1638   * @param  Timeout Specify Timeout value
1639   * @retval HAL status
1640   */
HAL_JPEG_Decode(JPEG_HandleTypeDef * hjpeg,uint8_t * pDataIn,uint32_t InDataLength,uint8_t * pDataOutMCU,uint32_t OutDataLength,uint32_t Timeout)1641 HAL_StatusTypeDef  HAL_JPEG_Decode(JPEG_HandleTypeDef *hjpeg, uint8_t *pDataIn, uint32_t InDataLength,
1642                                    uint8_t *pDataOutMCU, uint32_t OutDataLength, uint32_t Timeout)
1643 {
1644   uint32_t tickstart;
1645 
1646   /* Check the parameters */
1647   assert_param((InDataLength >= 4UL));
1648   assert_param((OutDataLength >= 4UL));
1649 
1650   /* Check In/out buffer allocation and size */
1651   if ((hjpeg == NULL) || (pDataIn == NULL) || (pDataOutMCU == NULL))
1652   {
1653     return HAL_ERROR;
1654   }
1655 
1656   /* Process Locked */
1657   __HAL_LOCK(hjpeg);
1658 
1659   /* Get tick */
1660   tickstart = HAL_GetTick();
1661 
1662   if (hjpeg->State == HAL_JPEG_STATE_READY)
1663   {
1664     /*Change JPEG state*/
1665     hjpeg->State = HAL_JPEG_STATE_BUSY_DECODING;
1666 
1667     /*Set the Context to Decode with Polling*/
1668     /*Set the Context to Encode with Polling*/
1669     hjpeg->Context &= ~(JPEG_CONTEXT_OPERATION_MASK | JPEG_CONTEXT_METHOD_MASK);
1670     hjpeg->Context |= (JPEG_CONTEXT_DECODE | JPEG_CONTEXT_POLLING);
1671 
1672     /*Store In/out buffers pointers and size*/
1673     hjpeg->pJpegInBuffPtr = pDataIn;
1674     hjpeg->pJpegOutBuffPtr = pDataOutMCU;
1675     hjpeg->InDataLength = InDataLength - (InDataLength % 4UL);    /*In Data length must be multiple of 4 Bytes (1 word)*/
1676     hjpeg->OutDataLength = OutDataLength - (OutDataLength % 4UL); /*Out Data length must be multiple of 4 Bytes (1 word)*/
1677 
1678     /*Reset In/out data counter */
1679     hjpeg->JpegInCount = 0;
1680     hjpeg->JpegOutCount = 0;
1681 
1682     /*Init decoding process*/
1683     JPEG_Init_Process(hjpeg);
1684 
1685     /*JPEG data processing : In/Out FIFO transfer*/
1686     while ((JPEG_Process(hjpeg) == JPEG_PROCESS_ONGOING))
1687     {
1688       if (Timeout != HAL_MAX_DELAY)
1689       {
1690         if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0UL))
1691         {
1692 
1693           /* Update error code */
1694           hjpeg->ErrorCode |= HAL_JPEG_ERROR_TIMEOUT;
1695 
1696           /* Process Unlocked */
1697           __HAL_UNLOCK(hjpeg);
1698 
1699           /*Change JPEG state*/
1700           hjpeg->State = HAL_JPEG_STATE_READY;
1701 
1702           return HAL_TIMEOUT;
1703         }
1704       }
1705     }
1706 
1707     /* Process Unlocked */
1708     __HAL_UNLOCK(hjpeg);
1709 
1710     /*Change JPEG state*/
1711     hjpeg->State = HAL_JPEG_STATE_READY;
1712 
1713   }
1714   else
1715   {
1716     /* Process Unlocked */
1717     __HAL_UNLOCK(hjpeg);
1718 
1719     return HAL_BUSY;
1720   }
1721   /* Return function status */
1722   return HAL_OK;
1723 }
1724 
1725 /**
1726   * @brief  Starts JPEG encoding with interrupt processing
1727   * @param  hjpeg pointer to a JPEG_HandleTypeDef structure that contains
1728   *         the configuration information for JPEG module
1729   * @param  pDataInMCU Pointer to the Input buffer
1730   * @param  InDataLength size in bytes Input buffer
1731   * @param  pDataOut Pointer to the jpeg output data buffer
1732   * @param  OutDataLength size in bytes of the Output buffer
1733   * @retval HAL status
1734   */
HAL_JPEG_Encode_IT(JPEG_HandleTypeDef * hjpeg,uint8_t * pDataInMCU,uint32_t InDataLength,uint8_t * pDataOut,uint32_t OutDataLength)1735 HAL_StatusTypeDef  HAL_JPEG_Encode_IT(JPEG_HandleTypeDef *hjpeg, uint8_t *pDataInMCU, uint32_t InDataLength,
1736                                       uint8_t *pDataOut, uint32_t OutDataLength)
1737 {
1738   /* Check the parameters */
1739   assert_param((InDataLength >= 4UL));
1740   assert_param((OutDataLength >= 4UL));
1741 
1742   /* Check In/out buffer allocation and size */
1743   if ((hjpeg == NULL) || (pDataInMCU == NULL) || (pDataOut == NULL))
1744   {
1745     return HAL_ERROR;
1746   }
1747 
1748   /* Process Locked */
1749   __HAL_LOCK(hjpeg);
1750 
1751   if (hjpeg->State != HAL_JPEG_STATE_READY)
1752   {
1753     /* Process Unlocked */
1754     __HAL_UNLOCK(hjpeg);
1755 
1756     return HAL_BUSY;
1757   }
1758   else
1759   {
1760     if ((hjpeg->Context & JPEG_CONTEXT_CONF_ENCODING) == JPEG_CONTEXT_CONF_ENCODING)
1761     {
1762       /*Change JPEG state*/
1763       hjpeg->State = HAL_JPEG_STATE_BUSY_ENCODING;
1764 
1765       /*Set the Context to Encode with IT*/
1766       hjpeg->Context &= ~(JPEG_CONTEXT_OPERATION_MASK | JPEG_CONTEXT_METHOD_MASK);
1767       hjpeg->Context |= (JPEG_CONTEXT_ENCODE | JPEG_CONTEXT_IT);
1768 
1769       /*Store In/out buffers pointers and size*/
1770       hjpeg->pJpegInBuffPtr = pDataInMCU;
1771       hjpeg->pJpegOutBuffPtr = pDataOut;
1772       hjpeg->InDataLength = InDataLength - (InDataLength % 4UL);    /*In Data length must be multiple of 4 Bytes (1 word)*/
1773       hjpeg->OutDataLength = OutDataLength - (OutDataLength % 4UL); /*Out Data length must be multiple of 4 Bytes (1 word)*/
1774 
1775       /*Reset In/out data counter */
1776       hjpeg->JpegInCount = 0;
1777       hjpeg->JpegOutCount = 0;
1778 
1779       /*Init decoding process*/
1780       JPEG_Init_Process(hjpeg);
1781 
1782     }
1783     else
1784     {
1785       /* Process Unlocked */
1786       __HAL_UNLOCK(hjpeg);
1787 
1788       return HAL_ERROR;
1789     }
1790   }
1791   /* Return function status */
1792   return HAL_OK;
1793 }
1794 
1795 /**
1796   * @brief  Starts JPEG decoding with interrupt processing
1797   * @param  hjpeg pointer to a JPEG_HandleTypeDef structure that contains
1798   *         the configuration information for JPEG module
1799   * @param  pDataIn Pointer to the input data buffer
1800   * @param  InDataLength size in bytes Input buffer
1801   * @param  pDataOutMCU Pointer to the Output data buffer
1802   * @param  OutDataLength size in bytes of the Output buffer
1803   * @retval HAL status
1804   */
HAL_JPEG_Decode_IT(JPEG_HandleTypeDef * hjpeg,uint8_t * pDataIn,uint32_t InDataLength,uint8_t * pDataOutMCU,uint32_t OutDataLength)1805 HAL_StatusTypeDef  HAL_JPEG_Decode_IT(JPEG_HandleTypeDef *hjpeg, uint8_t *pDataIn, uint32_t InDataLength,
1806                                       uint8_t *pDataOutMCU, uint32_t OutDataLength)
1807 {
1808   /* Check the parameters */
1809   assert_param((InDataLength >= 4UL));
1810   assert_param((OutDataLength >= 4UL));
1811 
1812   /* Check In/out buffer allocation and size */
1813   if ((hjpeg == NULL) || (pDataIn == NULL) || (pDataOutMCU == NULL))
1814   {
1815     return HAL_ERROR;
1816   }
1817 
1818   /* Process Locked */
1819   __HAL_LOCK(hjpeg);
1820 
1821   if (hjpeg->State == HAL_JPEG_STATE_READY)
1822   {
1823     /*Change JPEG state*/
1824     hjpeg->State = HAL_JPEG_STATE_BUSY_DECODING;
1825 
1826     /*Set the Context to Decode with IT*/
1827     hjpeg->Context &= ~(JPEG_CONTEXT_OPERATION_MASK | JPEG_CONTEXT_METHOD_MASK);
1828     hjpeg->Context |= (JPEG_CONTEXT_DECODE | JPEG_CONTEXT_IT);
1829 
1830     /*Store In/out buffers pointers and size*/
1831     hjpeg->pJpegInBuffPtr = pDataIn;
1832     hjpeg->pJpegOutBuffPtr = pDataOutMCU;
1833     hjpeg->InDataLength = InDataLength - (InDataLength % 4UL);    /*In Data length must be multiple of 4 Bytes (1 word)*/
1834     hjpeg->OutDataLength = OutDataLength - (OutDataLength % 4UL); /*Out Data length must be multiple of 4 Bytes (1 word)*/
1835 
1836     /*Reset In/out data counter */
1837     hjpeg->JpegInCount = 0;
1838     hjpeg->JpegOutCount = 0;
1839 
1840     /*Init decoding process*/
1841     JPEG_Init_Process(hjpeg);
1842 
1843   }
1844   else
1845   {
1846     /* Process Unlocked */
1847     __HAL_UNLOCK(hjpeg);
1848 
1849     return HAL_BUSY;
1850   }
1851   /* Return function status */
1852   return HAL_OK;
1853 }
1854 
1855 /**
1856   * @brief  Starts JPEG encoding with DMA processing
1857   * @param  hjpeg pointer to a JPEG_HandleTypeDef structure that contains
1858   *         the configuration information for JPEG module
1859   * @param  pDataInMCU Pointer to the Input buffer
1860   * @param  InDataLength size in bytes Input buffer
1861   * @param  pDataOut Pointer to the jpeg output data buffer
1862   * @param  OutDataLength size in bytes of the Output buffer
1863   * @retval HAL status
1864   */
HAL_JPEG_Encode_DMA(JPEG_HandleTypeDef * hjpeg,uint8_t * pDataInMCU,uint32_t InDataLength,uint8_t * pDataOut,uint32_t OutDataLength)1865 HAL_StatusTypeDef  HAL_JPEG_Encode_DMA(JPEG_HandleTypeDef *hjpeg, uint8_t *pDataInMCU, uint32_t InDataLength,
1866                                        uint8_t *pDataOut, uint32_t OutDataLength)
1867 {
1868   /* Check the parameters */
1869   assert_param((InDataLength >= 4UL));
1870   assert_param((OutDataLength >= 4UL));
1871 
1872   /* Check In/out buffer allocation and size */
1873   if ((hjpeg == NULL) || (pDataInMCU == NULL) || (pDataOut == NULL))
1874   {
1875     return HAL_ERROR;
1876   }
1877 
1878   /* Process Locked */
1879   __HAL_LOCK(hjpeg);
1880 
1881   if (hjpeg->State != HAL_JPEG_STATE_READY)
1882   {
1883     /* Process Unlocked */
1884     __HAL_UNLOCK(hjpeg);
1885 
1886     return HAL_BUSY;
1887   }
1888   else
1889   {
1890     if ((hjpeg->Context & JPEG_CONTEXT_CONF_ENCODING) == JPEG_CONTEXT_CONF_ENCODING)
1891     {
1892       /*Change JPEG state*/
1893       hjpeg->State = HAL_JPEG_STATE_BUSY_ENCODING;
1894 
1895       /*Set the Context to Encode with DMA*/
1896       hjpeg->Context &= ~(JPEG_CONTEXT_OPERATION_MASK | JPEG_CONTEXT_METHOD_MASK);
1897       hjpeg->Context |= (JPEG_CONTEXT_ENCODE | JPEG_CONTEXT_DMA);
1898 
1899       /*Store In/out buffers pointers and size*/
1900       hjpeg->pJpegInBuffPtr = pDataInMCU;
1901       hjpeg->pJpegOutBuffPtr = pDataOut;
1902       hjpeg->InDataLength = InDataLength;
1903       hjpeg->OutDataLength = OutDataLength;
1904 
1905       /*Reset In/out data counter */
1906       hjpeg->JpegInCount = 0;
1907       hjpeg->JpegOutCount = 0;
1908 
1909       /*Init decoding process*/
1910       JPEG_Init_Process(hjpeg);
1911 
1912       /* JPEG encoding process using DMA */
1913       if (JPEG_DMA_StartProcess(hjpeg) != HAL_OK)
1914       {
1915         /* Update State */
1916         hjpeg->State = HAL_JPEG_STATE_ERROR;
1917         /* Process Unlocked */
1918         __HAL_UNLOCK(hjpeg);
1919 
1920         return HAL_ERROR;
1921       }
1922 
1923     }
1924     else
1925     {
1926       /* Process Unlocked */
1927       __HAL_UNLOCK(hjpeg);
1928 
1929       return HAL_ERROR;
1930     }
1931   }
1932   /* Return function status */
1933   return HAL_OK;
1934 }
1935 
1936 /**
1937   * @brief  Starts JPEG decoding with DMA processing
1938   * @param  hjpeg pointer to a JPEG_HandleTypeDef structure that contains
1939   *         the configuration information for JPEG module
1940   * @param  pDataIn Pointer to the input data buffer
1941   * @param  InDataLength size in bytes Input buffer
1942   * @param  pDataOutMCU Pointer to the Output data buffer
1943   * @param  OutDataLength size in bytes of the Output buffer
1944   * @retval HAL status
1945   */
HAL_JPEG_Decode_DMA(JPEG_HandleTypeDef * hjpeg,uint8_t * pDataIn,uint32_t InDataLength,uint8_t * pDataOutMCU,uint32_t OutDataLength)1946 HAL_StatusTypeDef  HAL_JPEG_Decode_DMA(JPEG_HandleTypeDef *hjpeg, uint8_t *pDataIn, uint32_t InDataLength,
1947                                        uint8_t *pDataOutMCU, uint32_t OutDataLength)
1948 {
1949   /* Check the parameters */
1950   assert_param((InDataLength >= 4UL));
1951   assert_param((OutDataLength >= 4UL));
1952 
1953   /* Check In/out buffer allocation and size */
1954   if ((hjpeg == NULL) || (pDataIn == NULL) || (pDataOutMCU == NULL))
1955   {
1956     return HAL_ERROR;
1957   }
1958 
1959   /* Process Locked */
1960   __HAL_LOCK(hjpeg);
1961 
1962   if (hjpeg->State == HAL_JPEG_STATE_READY)
1963   {
1964     /*Change JPEG state*/
1965     hjpeg->State = HAL_JPEG_STATE_BUSY_DECODING;
1966 
1967     /*Set the Context to Decode with DMA*/
1968     hjpeg->Context &= ~(JPEG_CONTEXT_OPERATION_MASK | JPEG_CONTEXT_METHOD_MASK);
1969     hjpeg->Context |= (JPEG_CONTEXT_DECODE | JPEG_CONTEXT_DMA);
1970 
1971     /*Store In/out buffers pointers and size*/
1972     hjpeg->pJpegInBuffPtr = pDataIn;
1973     hjpeg->pJpegOutBuffPtr = pDataOutMCU;
1974     hjpeg->InDataLength = InDataLength;
1975     hjpeg->OutDataLength = OutDataLength;
1976 
1977     /*Reset In/out data counter */
1978     hjpeg->JpegInCount = 0;
1979     hjpeg->JpegOutCount = 0;
1980 
1981     /*Init decoding process*/
1982     JPEG_Init_Process(hjpeg);
1983 
1984     /* JPEG decoding process using DMA */
1985     if (JPEG_DMA_StartProcess(hjpeg) != HAL_OK)
1986     {
1987       /* Update State */
1988       hjpeg->State = HAL_JPEG_STATE_ERROR;
1989       /* Process Unlocked */
1990       __HAL_UNLOCK(hjpeg);
1991 
1992       return HAL_ERROR;
1993     }
1994   }
1995   else
1996   {
1997     /* Process Unlocked */
1998     __HAL_UNLOCK(hjpeg);
1999 
2000     return HAL_BUSY;
2001   }
2002   /* Return function status */
2003   return HAL_OK;
2004 }
2005 
2006 /**
2007   * @brief  Pause the JPEG Input/Output processing
2008   * @param  hjpeg pointer to a JPEG_HandleTypeDef structure that contains
2009   *         the configuration information for JPEG module
2010   * @param  XferSelection This parameter can be one of the following values :
2011   *                           JPEG_PAUSE_RESUME_INPUT : Pause Input processing
2012   *                           JPEG_PAUSE_RESUME_OUTPUT: Pause Output processing
2013   *                           JPEG_PAUSE_RESUME_INPUT_OUTPUT: Pause Input and Output processing
2014   * @retval HAL status
2015   */
HAL_JPEG_Pause(JPEG_HandleTypeDef * hjpeg,uint32_t XferSelection)2016 HAL_StatusTypeDef  HAL_JPEG_Pause(JPEG_HandleTypeDef *hjpeg, uint32_t XferSelection)
2017 {
2018   uint32_t mask = 0;
2019 
2020   assert_param(IS_JPEG_PAUSE_RESUME_STATE(XferSelection));
2021 
2022   if ((hjpeg->Context & JPEG_CONTEXT_METHOD_MASK) == JPEG_CONTEXT_DMA)
2023   {
2024     if ((XferSelection & JPEG_PAUSE_RESUME_INPUT) == JPEG_PAUSE_RESUME_INPUT)
2025     {
2026       hjpeg->Context |= JPEG_CONTEXT_PAUSE_INPUT;
2027       mask |= JPEG_DMA_IDMA;
2028     }
2029     if ((XferSelection & JPEG_PAUSE_RESUME_OUTPUT) == JPEG_PAUSE_RESUME_OUTPUT)
2030     {
2031       hjpeg->Context |= JPEG_CONTEXT_PAUSE_OUTPUT;
2032       mask |= JPEG_DMA_ODMA;
2033     }
2034     JPEG_DISABLE_DMA(hjpeg, mask);
2035 
2036   }
2037   else if ((hjpeg->Context & JPEG_CONTEXT_METHOD_MASK) == JPEG_CONTEXT_IT)
2038   {
2039 
2040     if ((XferSelection & JPEG_PAUSE_RESUME_INPUT) == JPEG_PAUSE_RESUME_INPUT)
2041     {
2042       hjpeg->Context |= JPEG_CONTEXT_PAUSE_INPUT;
2043       mask |= (JPEG_IT_IFT | JPEG_IT_IFNF);
2044     }
2045     if ((XferSelection & JPEG_PAUSE_RESUME_OUTPUT) == JPEG_PAUSE_RESUME_OUTPUT)
2046     {
2047       hjpeg->Context |= JPEG_CONTEXT_PAUSE_OUTPUT;
2048       mask |= (JPEG_IT_OFT | JPEG_IT_OFNE | JPEG_IT_EOC);
2049     }
2050     __HAL_JPEG_DISABLE_IT(hjpeg, mask);
2051 
2052   }
2053   else
2054   {
2055     /* Nothing to do */
2056   }
2057 
2058   /* Return function status */
2059   return HAL_OK;
2060 }
2061 
2062 /**
2063   * @brief  Resume the JPEG Input/Output processing
2064   * @param  hjpeg pointer to a JPEG_HandleTypeDef structure that contains
2065   *         the configuration information for JPEG module
2066   * @param  XferSelection This parameter can be one of the following values :
2067   *                           JPEG_PAUSE_RESUME_INPUT : Resume Input processing
2068   *                           JPEG_PAUSE_RESUME_OUTPUT: Resume Output processing
2069   *                           JPEG_PAUSE_RESUME_INPUT_OUTPUT: Resume Input and Output processing
2070   * @retval HAL status
2071   */
HAL_JPEG_Resume(JPEG_HandleTypeDef * hjpeg,uint32_t XferSelection)2072 HAL_StatusTypeDef  HAL_JPEG_Resume(JPEG_HandleTypeDef *hjpeg, uint32_t XferSelection)
2073 {
2074   uint32_t mask = 0;
2075 
2076   assert_param(IS_JPEG_PAUSE_RESUME_STATE(XferSelection));
2077 
2078   if ((hjpeg->Context & (JPEG_CONTEXT_PAUSE_INPUT | JPEG_CONTEXT_PAUSE_OUTPUT)) == 0UL)
2079   {
2080     /* if nothing paused to resume return error*/
2081     return HAL_ERROR;
2082   }
2083 
2084   if ((hjpeg->Context & JPEG_CONTEXT_METHOD_MASK) == JPEG_CONTEXT_DMA)
2085   {
2086 
2087     if ((XferSelection & JPEG_PAUSE_RESUME_INPUT) == JPEG_PAUSE_RESUME_INPUT)
2088     {
2089       hjpeg->Context &= (~JPEG_CONTEXT_PAUSE_INPUT);
2090       mask |= JPEG_DMA_IDMA;
2091 
2092       /*JPEG Input DMA transfer data number must be multiple of DMA buffer size
2093         as the destination is a 32 bits register */
2094       hjpeg->InDataLength = hjpeg->InDataLength - (hjpeg->InDataLength % 4UL);
2095 
2096       if (hjpeg->InDataLength > 0UL)
2097       {
2098         /* Start DMA FIFO In transfer */
2099         if (HAL_DMA_Start_IT(hjpeg->hdmain, (uint32_t)hjpeg->pJpegInBuffPtr, (uint32_t)&hjpeg->Instance->DIR,
2100                              hjpeg->InDataLength >> 2) != HAL_OK)
2101         {
2102           hjpeg->ErrorCode |= HAL_JPEG_ERROR_DMA;
2103           hjpeg->State = HAL_JPEG_STATE_ERROR;
2104           return HAL_ERROR;
2105         }
2106       }
2107     }
2108     if ((XferSelection & JPEG_PAUSE_RESUME_OUTPUT) == JPEG_PAUSE_RESUME_OUTPUT)
2109     {
2110       hjpeg->Context &= (~JPEG_CONTEXT_PAUSE_OUTPUT);
2111 
2112       if ((hjpeg->Context & JPEG_CONTEXT_ENDING_DMA) != 0UL)
2113       {
2114         JPEG_DMA_PollResidualData(hjpeg);
2115       }
2116       else
2117       {
2118         mask |= JPEG_DMA_ODMA;
2119 
2120         /* Start DMA FIFO Out transfer */
2121         if (HAL_DMA_Start_IT(hjpeg->hdmaout, (uint32_t)&hjpeg->Instance->DOR, (uint32_t)hjpeg->pJpegOutBuffPtr,
2122                              hjpeg->OutDataLength >> 2) != HAL_OK)
2123         {
2124           hjpeg->ErrorCode |= HAL_JPEG_ERROR_DMA;
2125           hjpeg->State = HAL_JPEG_STATE_ERROR;
2126           return HAL_ERROR;
2127         }
2128       }
2129 
2130     }
2131     JPEG_ENABLE_DMA(hjpeg, mask);
2132 
2133   }
2134   else if ((hjpeg->Context & JPEG_CONTEXT_METHOD_MASK) == JPEG_CONTEXT_IT)
2135   {
2136     if ((XferSelection & JPEG_PAUSE_RESUME_INPUT) == JPEG_PAUSE_RESUME_INPUT)
2137     {
2138       hjpeg->Context &= (~JPEG_CONTEXT_PAUSE_INPUT);
2139       mask |= (JPEG_IT_IFT | JPEG_IT_IFNF);
2140     }
2141     if ((XferSelection & JPEG_PAUSE_RESUME_OUTPUT) == JPEG_PAUSE_RESUME_OUTPUT)
2142     {
2143       hjpeg->Context &= (~JPEG_CONTEXT_PAUSE_OUTPUT);
2144       mask |= (JPEG_IT_OFT | JPEG_IT_OFNE | JPEG_IT_EOC);
2145     }
2146     __HAL_JPEG_ENABLE_IT(hjpeg, mask);
2147 
2148   }
2149   else
2150   {
2151     /* Nothing to do */
2152   }
2153 
2154   /* Return function status */
2155   return HAL_OK;
2156 }
2157 
2158 /**
2159   * @brief  Config Encoding/Decoding Input Buffer.
2160   * @param  hjpeg pointer to a JPEG_HandleTypeDef structure that contains
2161   *         the configuration information for JPEG module.
2162   * @param  pNewInputBuffer Pointer to the new input data buffer
2163   * @param  InDataLength Size in bytes of the new Input data buffer
2164   * @retval HAL status
2165   */
HAL_JPEG_ConfigInputBuffer(JPEG_HandleTypeDef * hjpeg,uint8_t * pNewInputBuffer,uint32_t InDataLength)2166 void HAL_JPEG_ConfigInputBuffer(JPEG_HandleTypeDef *hjpeg, uint8_t *pNewInputBuffer, uint32_t InDataLength)
2167 {
2168   hjpeg->pJpegInBuffPtr =  pNewInputBuffer;
2169   hjpeg->InDataLength = InDataLength;
2170 }
2171 
2172 /**
2173   * @brief  Config Encoding/Decoding Output Buffer.
2174   * @param  hjpeg pointer to a JPEG_HandleTypeDef structure that contains
2175   *         the configuration information for JPEG module.
2176   * @param  pNewOutputBuffer Pointer to the new output data buffer
2177   * @param  OutDataLength Size in bytes of the new Output data buffer
2178   * @retval HAL status
2179   */
HAL_JPEG_ConfigOutputBuffer(JPEG_HandleTypeDef * hjpeg,uint8_t * pNewOutputBuffer,uint32_t OutDataLength)2180 void HAL_JPEG_ConfigOutputBuffer(JPEG_HandleTypeDef *hjpeg, uint8_t *pNewOutputBuffer, uint32_t OutDataLength)
2181 {
2182   hjpeg->pJpegOutBuffPtr = pNewOutputBuffer;
2183   hjpeg->OutDataLength = OutDataLength;
2184 }
2185 
2186 /**
2187   * @brief  Aborts the JPEG Encoding/Decoding.
2188   * @param  hjpeg pointer to a JPEG_HandleTypeDef structure that contains
2189   *         the configuration information for JPEG module
2190   * @retval HAL status
2191   */
HAL_JPEG_Abort(JPEG_HandleTypeDef * hjpeg)2192 HAL_StatusTypeDef HAL_JPEG_Abort(JPEG_HandleTypeDef *hjpeg)
2193 {
2194   uint32_t tickstart;
2195   uint32_t tmpContext;
2196   tmpContext = hjpeg->Context;
2197 
2198   /*Reset the Context operation and method*/
2199   hjpeg->Context &= ~(JPEG_CONTEXT_OPERATION_MASK | JPEG_CONTEXT_METHOD_MASK | JPEG_CONTEXT_ENDING_DMA);
2200 
2201   if ((tmpContext & JPEG_CONTEXT_METHOD_MASK) == JPEG_CONTEXT_DMA)
2202   {
2203     /* Stop the DMA In/out Xfer*/
2204     if (HAL_DMA_Abort(hjpeg->hdmaout) != HAL_OK)
2205     {
2206       if (hjpeg->hdmaout->ErrorCode == HAL_DMA_ERROR_TIMEOUT)
2207       {
2208         hjpeg->ErrorCode |= HAL_JPEG_ERROR_DMA;
2209       }
2210     }
2211     if (HAL_DMA_Abort(hjpeg->hdmain) != HAL_OK)
2212     {
2213       if (hjpeg->hdmain->ErrorCode == HAL_DMA_ERROR_TIMEOUT)
2214       {
2215         hjpeg->ErrorCode |= HAL_JPEG_ERROR_DMA;
2216       }
2217     }
2218 
2219   }
2220 
2221   /* Stop the JPEG encoding/decoding process*/
2222   hjpeg->Instance->CONFR0 &=  ~JPEG_CONFR0_START;
2223 
2224   /* Get tick */
2225   tickstart = HAL_GetTick();
2226 
2227   /* Check if the JPEG Codec is effectively disabled */
2228   while (__HAL_JPEG_GET_FLAG(hjpeg, JPEG_FLAG_COF) != 0UL)
2229   {
2230     /* Check for the Timeout */
2231     if ((HAL_GetTick() - tickstart) > JPEG_TIMEOUT_VALUE)
2232     {
2233       /* Update error code */
2234       hjpeg->ErrorCode |= HAL_JPEG_ERROR_TIMEOUT;
2235 
2236       /* Change the DMA state */
2237       hjpeg->State = HAL_JPEG_STATE_ERROR;
2238       break;
2239     }
2240   }
2241 
2242   /* Disable All Interrupts */
2243   __HAL_JPEG_DISABLE_IT(hjpeg, JPEG_INTERRUPT_MASK);
2244 
2245   /* Disable All DMA requests */
2246   JPEG_DISABLE_DMA(hjpeg, JPEG_DMA_MASK);
2247 
2248   /* Flush input and output FIFOs*/
2249   hjpeg->Instance->CR |= JPEG_CR_IFF;
2250   hjpeg->Instance->CR |= JPEG_CR_OFF;
2251 
2252   /* Clear all flags */
2253   __HAL_JPEG_CLEAR_FLAG(hjpeg, JPEG_FLAG_ALL);
2254 
2255   /* Reset JpegInCount and JpegOutCount */
2256   hjpeg->JpegInCount = 0;
2257   hjpeg->JpegOutCount = 0;
2258 
2259   /*Reset the Context Pause*/
2260   hjpeg->Context &= ~(JPEG_CONTEXT_PAUSE_INPUT | JPEG_CONTEXT_PAUSE_OUTPUT);
2261 
2262   /* Change the DMA state*/
2263   if (hjpeg->ErrorCode != HAL_JPEG_ERROR_NONE)
2264   {
2265     hjpeg->State = HAL_JPEG_STATE_ERROR;
2266     /* Process Unlocked */
2267     __HAL_UNLOCK(hjpeg);
2268     /* Return function status */
2269     return HAL_ERROR;
2270   }
2271   else
2272   {
2273     hjpeg->State = HAL_JPEG_STATE_READY;
2274     /* Process Unlocked */
2275     __HAL_UNLOCK(hjpeg);
2276     /* Return function status */
2277     return HAL_OK;
2278   }
2279 
2280 }
2281 
2282 
2283 /**
2284   * @}
2285   */
2286 
2287 /** @defgroup JPEG_Exported_Functions_Group4 JPEG Decode/Encode callback functions
2288   *  @brief   JPEG process callback functions.
2289   *
2290 @verbatim
2291   ==============================================================================
2292               #####  JPEG Decode and Encode callback functions  #####
2293   ==============================================================================
2294     [..]  This section provides callback functions:
2295       (+) HAL_JPEG_InfoReadyCallback()  : Decoding JPEG Info ready callback
2296       (+) HAL_JPEG_EncodeCpltCallback() : Encoding complete callback.
2297       (+) HAL_JPEG_DecodeCpltCallback() : Decoding complete callback.
2298       (+) HAL_JPEG_ErrorCallback()      : JPEG error callback.
2299       (+) HAL_JPEG_GetDataCallback()    : Get New Data chunk callback.
2300       (+) HAL_JPEG_DataReadyCallback()  : Decoded/Encoded Data ready  callback.
2301 
2302 @endverbatim
2303   * @{
2304   */
2305 
2306 /**
2307   * @brief  Decoding JPEG Info ready callback.
2308   * @param  hjpeg pointer to a JPEG_HandleTypeDef structure that contains
2309   *         the configuration information for JPEG module
2310   * @param  pInfo pointer to a JPEG_ConfTypeDef structure that contains
2311   *         The JPEG decoded header information
2312   * @retval None
2313   */
HAL_JPEG_InfoReadyCallback(JPEG_HandleTypeDef * hjpeg,JPEG_ConfTypeDef * pInfo)2314 __weak void HAL_JPEG_InfoReadyCallback(JPEG_HandleTypeDef *hjpeg, JPEG_ConfTypeDef *pInfo)
2315 {
2316   /* Prevent unused argument(s) compilation warning */
2317   UNUSED(hjpeg);
2318   UNUSED(pInfo);
2319 
2320   /* NOTE : This function Should not be modified, when the callback is needed,
2321             the HAL_JPEG_HeaderParsingCpltCallback could be implemented in the user file
2322    */
2323 }
2324 
2325 /**
2326   * @brief  Encoding complete callback.
2327   * @param  hjpeg pointer to a JPEG_HandleTypeDef structure that contains
2328   *         the configuration information for JPEG module
2329   * @retval None
2330   */
HAL_JPEG_EncodeCpltCallback(JPEG_HandleTypeDef * hjpeg)2331 __weak void HAL_JPEG_EncodeCpltCallback(JPEG_HandleTypeDef *hjpeg)
2332 {
2333   /* Prevent unused argument(s) compilation warning */
2334   UNUSED(hjpeg);
2335 
2336   /* NOTE : This function Should not be modified, when the callback is needed,
2337             the HAL_JPEG_EncodeCpltCallback could be implemented in the user file
2338    */
2339 }
2340 
2341 /**
2342   * @brief  Decoding complete callback.
2343   * @param  hjpeg pointer to a JPEG_HandleTypeDef structure that contains
2344   *         the configuration information for JPEG module
2345   * @retval None
2346   */
HAL_JPEG_DecodeCpltCallback(JPEG_HandleTypeDef * hjpeg)2347 __weak void HAL_JPEG_DecodeCpltCallback(JPEG_HandleTypeDef *hjpeg)
2348 {
2349   /* Prevent unused argument(s) compilation warning */
2350   UNUSED(hjpeg);
2351 
2352   /* NOTE : This function Should not be modified, when the callback is needed,
2353             the HAL_JPEG_EncodeCpltCallback could be implemented in the user file
2354    */
2355 }
2356 
2357 /**
2358   * @brief  JPEG error callback.
2359   * @param  hjpeg pointer to a JPEG_HandleTypeDef structure that contains
2360   *         the configuration information for JPEG module
2361   * @retval None
2362   */
HAL_JPEG_ErrorCallback(JPEG_HandleTypeDef * hjpeg)2363 __weak void HAL_JPEG_ErrorCallback(JPEG_HandleTypeDef *hjpeg)
2364 {
2365   /* Prevent unused argument(s) compilation warning */
2366   UNUSED(hjpeg);
2367 
2368   /* NOTE : This function Should not be modified, when the callback is needed,
2369             the HAL_JPEG_ErrorCallback could be implemented in the user file
2370    */
2371 }
2372 
2373 /**
2374   * @brief  Get New Data chunk callback.
2375   * @param  hjpeg pointer to a JPEG_HandleTypeDef structure that contains
2376   *         the configuration information for JPEG module
2377   * @param  NbDecodedData Number of consummed data in the previous chunk in bytes
2378   * @retval None
2379   */
HAL_JPEG_GetDataCallback(JPEG_HandleTypeDef * hjpeg,uint32_t NbDecodedData)2380 __weak void HAL_JPEG_GetDataCallback(JPEG_HandleTypeDef *hjpeg, uint32_t NbDecodedData)
2381 {
2382   /* Prevent unused argument(s) compilation warning */
2383   UNUSED(hjpeg);
2384   UNUSED(NbDecodedData);
2385 
2386   /* NOTE : This function Should not be modified, when the callback is needed,
2387             the HAL_JPEG_GetDataCallback could be implemented in the user file
2388    */
2389 }
2390 
2391 /**
2392   * @brief  Decoded/Encoded Data ready  callback.
2393   * @param  hjpeg pointer to a JPEG_HandleTypeDef structure that contains
2394   *         the configuration information for JPEG module
2395   * @param  pDataOut pointer to the output data buffer
2396   * @param  OutDataLength number in bytes of data available in the specified output buffer
2397   * @retval None
2398   */
HAL_JPEG_DataReadyCallback(JPEG_HandleTypeDef * hjpeg,uint8_t * pDataOut,uint32_t OutDataLength)2399 __weak void HAL_JPEG_DataReadyCallback(JPEG_HandleTypeDef *hjpeg, uint8_t *pDataOut, uint32_t OutDataLength)
2400 {
2401   /* Prevent unused argument(s) compilation warning */
2402   UNUSED(hjpeg);
2403   UNUSED(pDataOut);
2404   UNUSED(OutDataLength);
2405 
2406   /* NOTE : This function Should not be modified, when the callback is needed,
2407             the HAL_JPEG_DataReadyCallback could be implemented in the user file
2408    */
2409 }
2410 
2411 /**
2412   * @}
2413   */
2414 
2415 
2416 /** @defgroup JPEG_Exported_Functions_Group5 JPEG IRQ handler management
2417   *  @brief   JPEG IRQ handler.
2418   *
2419 @verbatim
2420   ==============================================================================
2421                 ##### JPEG IRQ handler management #####
2422   ==============================================================================
2423     [..]  This section provides JPEG IRQ handler function.
2424       (+) HAL_JPEG_IRQHandler()  : handles JPEG interrupt request
2425 
2426 @endverbatim
2427   * @{
2428   */
2429 
2430 /**
2431   * @brief  This function handles JPEG interrupt request.
2432   * @param  hjpeg pointer to a JPEG_HandleTypeDef structure that contains
2433   *         the configuration information for JPEG module
2434   * @retval None
2435   */
HAL_JPEG_IRQHandler(JPEG_HandleTypeDef * hjpeg)2436 void HAL_JPEG_IRQHandler(JPEG_HandleTypeDef *hjpeg)
2437 {
2438   switch (hjpeg->State)
2439   {
2440     case HAL_JPEG_STATE_BUSY_ENCODING:
2441     case HAL_JPEG_STATE_BUSY_DECODING:
2442       /* continue JPEG data encoding/Decoding*/
2443       /* JPEG data processing : In/Out FIFO transfer*/
2444       if ((hjpeg->Context & JPEG_CONTEXT_METHOD_MASK) == JPEG_CONTEXT_IT)
2445       {
2446         (void) JPEG_Process(hjpeg);
2447       }
2448       else if ((hjpeg->Context & JPEG_CONTEXT_METHOD_MASK) == JPEG_CONTEXT_DMA)
2449       {
2450         JPEG_DMA_ContinueProcess(hjpeg);
2451       }
2452       else
2453       {
2454         /* Nothing to do */
2455       }
2456       break;
2457 
2458     default:
2459       break;
2460   }
2461 }
2462 
2463 /**
2464   * @}
2465   */
2466 
2467 /** @defgroup JPEG_Exported_Functions_Group6 Peripheral State functions
2468   *  @brief   Peripheral State functions.
2469   *
2470 @verbatim
2471   ==============================================================================
2472                     ##### Peripheral State and Error functions #####
2473   ==============================================================================
2474     [..]  This section provides JPEG State and Errors function.
2475       (+) HAL_JPEG_GetState()  : permits to get in run-time the JPEG state.
2476       (+) HAL_JPEG_GetError()  : Returns the JPEG error code if any.
2477 
2478 @endverbatim
2479   * @{
2480   */
2481 
2482 /**
2483   * @brief  Returns the JPEG state.
2484   * @param  hjpeg pointer to a JPEG_HandleTypeDef structure that contains
2485   *         the configuration information for JPEG module
2486   * @retval JPEG state
2487   */
HAL_JPEG_GetState(JPEG_HandleTypeDef * hjpeg)2488 HAL_JPEG_STATETypeDef HAL_JPEG_GetState(JPEG_HandleTypeDef *hjpeg)
2489 {
2490   return hjpeg->State;
2491 }
2492 
2493 /**
2494   * @brief  Return the JPEG error code
2495   * @param  hjpeg  pointer to a JPEG_HandleTypeDef structure that contains
2496   *              the configuration information for the specified JPEG.
2497   * @retval JPEG Error Code
2498   */
HAL_JPEG_GetError(JPEG_HandleTypeDef * hjpeg)2499 uint32_t HAL_JPEG_GetError(JPEG_HandleTypeDef *hjpeg)
2500 {
2501   return hjpeg->ErrorCode;
2502 }
2503 
2504 /**
2505   * @}
2506   */
2507 
2508 /**
2509   * @}
2510   */
2511 
2512 
2513 /** @addtogroup JPEG_Private_Functions
2514   * @{
2515   */
2516 
2517 /**
2518   * @brief  Generates Huffman sizes/Codes Table from Bits/vals Table
2519   * @param  Bits pointer to bits table
2520   * @param  Huffsize pointer to sizes table
2521   * @param  Huffcode pointer to codes table
2522   * @param  LastK pointer to last Coeff (table dimension)
2523   * @retval HAL status
2524   */
JPEG_Bits_To_SizeCodes(uint8_t * Bits,uint8_t * Huffsize,uint32_t * Huffcode,uint32_t * LastK)2525 static HAL_StatusTypeDef JPEG_Bits_To_SizeCodes(uint8_t *Bits, uint8_t *Huffsize, uint32_t *Huffcode, uint32_t *LastK)
2526 {
2527   uint32_t i;
2528   uint32_t p;
2529   uint32_t l;
2530   uint32_t code;
2531   uint32_t si;
2532 
2533   /* Figure C.1: Generation of table of Huffman code sizes */
2534   p = 0;
2535   for (l = 0; l < 16UL; l++)
2536   {
2537     i = (uint32_t)Bits[l];
2538     if ((p + i) > 256UL)
2539     {
2540       /* check for table overflow */
2541       return HAL_ERROR;
2542     }
2543     while (i != 0UL)
2544     {
2545       Huffsize[p] = (uint8_t) l + 1U;
2546       p++;
2547       i--;
2548     }
2549   }
2550   Huffsize[p] = 0;
2551   *LastK = p;
2552 
2553   /* Figure C.2: Generation of table of Huffman codes */
2554   code = 0;
2555   si = Huffsize[0];
2556   p = 0;
2557   while (Huffsize[p] != 0U)
2558   {
2559     while (((uint32_t) Huffsize[p]) == si)
2560     {
2561       Huffcode[p] = code;
2562       p++;
2563       code++;
2564     }
2565     /* code must fit in "size" bits (si), no code is allowed to be all ones*/
2566     if(si > 31UL)
2567     {
2568       return HAL_ERROR;
2569     }
2570     if (((uint32_t) code) >= (((uint32_t) 1) << si))
2571     {
2572       return HAL_ERROR;
2573     }
2574     code <<= 1;
2575     si++;
2576   }
2577   /* Return function status */
2578   return HAL_OK;
2579 }
2580 
2581 /**
2582   * @brief  Transform a Bits/Vals AC Huffman table to sizes/Codes huffman Table
2583   *         that can programmed to the JPEG encoder registers
2584   * @param  AC_BitsValsTable pointer to AC huffman bits/vals table
2585   * @param  AC_SizeCodesTable pointer to AC huffman Sizes/Codes table
2586   * @retval HAL status
2587   */
JPEG_ACHuff_BitsVals_To_SizeCodes(JPEG_ACHuffTableTypeDef * AC_BitsValsTable,JPEG_AC_HuffCodeTableTypeDef * AC_SizeCodesTable)2588 static HAL_StatusTypeDef JPEG_ACHuff_BitsVals_To_SizeCodes(JPEG_ACHuffTableTypeDef *AC_BitsValsTable,
2589                                                            JPEG_AC_HuffCodeTableTypeDef *AC_SizeCodesTable)
2590 {
2591   HAL_StatusTypeDef error;
2592   uint8_t huffsize[257];
2593   uint32_t huffcode[257];
2594   uint32_t k;
2595   uint32_t l, lsb, msb;
2596   uint32_t lastK;
2597 
2598   error = JPEG_Bits_To_SizeCodes(AC_BitsValsTable->Bits, huffsize, huffcode, &lastK);
2599   if (error != HAL_OK)
2600   {
2601     return  error;
2602   }
2603 
2604   /* Figure C.3: Ordering procedure for encoding procedure code tables */
2605   k = 0;
2606 
2607   while (k < lastK)
2608   {
2609     l = AC_BitsValsTable->HuffVal[k];
2610     if (l == 0UL)
2611     {
2612       l = 160; /*l = 0x00 EOB code*/
2613     }
2614     else if (l == 0xF0UL) /* l = 0xF0 ZRL code*/
2615     {
2616       l = 161;
2617     }
2618     else
2619     {
2620       msb = (l & 0xF0UL) >> 4;
2621       lsb = (l & 0x0FUL);
2622       l = (msb * 10UL) + lsb - 1UL;
2623     }
2624     if (l >= JPEG_AC_HUFF_TABLE_SIZE)
2625     {
2626       return HAL_ERROR; /* Huffman Table overflow error*/
2627     }
2628     else
2629     {
2630       AC_SizeCodesTable->HuffmanCode[l] = huffcode[k];
2631       AC_SizeCodesTable->CodeLength[l] = huffsize[k] - 1U;
2632       k++;
2633     }
2634   }
2635 
2636   /* Return function status */
2637   return HAL_OK;
2638 }
2639 
2640 /**
2641   * @brief  Transform a Bits/Vals DC Huffman table to sizes/Codes huffman Table
2642   *         that can programmed to the JPEG encoder registers
2643   * @param  DC_BitsValsTable pointer to DC huffman bits/vals table
2644   * @param  DC_SizeCodesTable pointer to DC huffman Sizes/Codes table
2645   * @retval HAL status
2646   */
JPEG_DCHuff_BitsVals_To_SizeCodes(JPEG_DCHuffTableTypeDef * DC_BitsValsTable,JPEG_DC_HuffCodeTableTypeDef * DC_SizeCodesTable)2647 static HAL_StatusTypeDef JPEG_DCHuff_BitsVals_To_SizeCodes(JPEG_DCHuffTableTypeDef *DC_BitsValsTable,
2648                                                            JPEG_DC_HuffCodeTableTypeDef *DC_SizeCodesTable)
2649 {
2650   HAL_StatusTypeDef error;
2651 
2652   uint32_t k;
2653   uint32_t l;
2654   uint32_t lastK;
2655   uint8_t huffsize[257];
2656   uint32_t huffcode[257];
2657   error = JPEG_Bits_To_SizeCodes(DC_BitsValsTable->Bits, huffsize, huffcode, &lastK);
2658   if (error != HAL_OK)
2659   {
2660     return  error;
2661   }
2662   /* Figure C.3: ordering procedure for encoding procedure code tables */
2663   k = 0;
2664 
2665   while (k < lastK)
2666   {
2667     l = DC_BitsValsTable->HuffVal[k];
2668     if (l >= JPEG_DC_HUFF_TABLE_SIZE)
2669     {
2670       return HAL_ERROR; /* Huffman Table overflow error*/
2671     }
2672     else
2673     {
2674       DC_SizeCodesTable->HuffmanCode[l] = huffcode[k];
2675       DC_SizeCodesTable->CodeLength[l] = huffsize[k] - 1U;
2676       k++;
2677     }
2678   }
2679 
2680   /* Return function status */
2681   return HAL_OK;
2682 }
2683 
2684 /**
2685   * @brief  Set the JPEG register with an DC huffman table at the given DC table address
2686   * @param  hjpeg pointer to a JPEG_HandleTypeDef structure that contains
2687   *         the configuration information for JPEG module
2688   * @param  HuffTableDC pointer to DC huffman table
2689   * @param  DCTableAddress Encoder DC huffman table address it could be HUFFENC_DC0 or HUFFENC_DC1.
2690   * @retval HAL status
2691   */
JPEG_Set_HuffDC_Mem(JPEG_HandleTypeDef * hjpeg,JPEG_DCHuffTableTypeDef * HuffTableDC,const __IO uint32_t * DCTableAddress)2692 static HAL_StatusTypeDef JPEG_Set_HuffDC_Mem(JPEG_HandleTypeDef *hjpeg, JPEG_DCHuffTableTypeDef *HuffTableDC,
2693                                              const __IO uint32_t *DCTableAddress)
2694 {
2695   HAL_StatusTypeDef error;
2696   JPEG_DC_HuffCodeTableTypeDef dcSizeCodesTable;
2697   uint32_t i;
2698   uint32_t lsb;
2699   uint32_t msb;
2700   __IO uint32_t *address, *addressDef;
2701 
2702   if (DCTableAddress == (hjpeg->Instance->HUFFENC_DC0))
2703   {
2704     address = (hjpeg->Instance->HUFFENC_DC0 + (JPEG_DC_HUFF_TABLE_SIZE / 2UL));
2705   }
2706   else if (DCTableAddress == (hjpeg->Instance->HUFFENC_DC1))
2707   {
2708     address = (hjpeg->Instance->HUFFENC_DC1 + (JPEG_DC_HUFF_TABLE_SIZE / 2UL));
2709   }
2710   else
2711   {
2712     return HAL_ERROR;
2713   }
2714 
2715   if (HuffTableDC != NULL)
2716   {
2717     error = JPEG_DCHuff_BitsVals_To_SizeCodes(HuffTableDC, &dcSizeCodesTable);
2718     if (error != HAL_OK)
2719     {
2720       return  error;
2721     }
2722     addressDef = address;
2723     *addressDef = 0x0FFF0FFF;
2724     addressDef++;
2725     *addressDef = 0x0FFF0FFF;
2726 
2727     i = JPEG_DC_HUFF_TABLE_SIZE;
2728     while (i > 1UL)
2729     {
2730       i--;
2731       address --;
2732       msb = ((uint32_t)(((uint32_t)dcSizeCodesTable.CodeLength[i] & 0xFU) << 8)) | ((uint32_t)dcSizeCodesTable.HuffmanCode[i] &
2733                                                                                    0xFFUL);
2734       i--;
2735       lsb = ((uint32_t)(((uint32_t)dcSizeCodesTable.CodeLength[i] & 0xFU) << 8)) | ((uint32_t)dcSizeCodesTable.HuffmanCode[i] &
2736                                                                                    0xFFUL);
2737 
2738       *address = lsb | (msb << 16);
2739     }
2740   }
2741 
2742   /* Return function status */
2743   return HAL_OK;
2744 }
2745 
2746 /**
2747   * @brief  Set the JPEG register with an AC huffman table at the given AC table address
2748   * @param  hjpeg pointer to a JPEG_HandleTypeDef structure that contains
2749   *         the configuration information for JPEG module
2750   * @param  HuffTableAC pointer to AC huffman table
2751   * @param  ACTableAddress Encoder AC huffman table address it could be HUFFENC_AC0 or HUFFENC_AC1.
2752   * @retval HAL status
2753   */
JPEG_Set_HuffAC_Mem(JPEG_HandleTypeDef * hjpeg,JPEG_ACHuffTableTypeDef * HuffTableAC,const __IO uint32_t * ACTableAddress)2754 static HAL_StatusTypeDef JPEG_Set_HuffAC_Mem(JPEG_HandleTypeDef *hjpeg, JPEG_ACHuffTableTypeDef *HuffTableAC,
2755                                              const __IO uint32_t *ACTableAddress)
2756 {
2757   HAL_StatusTypeDef error;
2758   JPEG_AC_HuffCodeTableTypeDef acSizeCodesTable;
2759   uint32_t i, lsb, msb;
2760   __IO uint32_t *address, *addressDef;
2761 
2762   if (ACTableAddress == (hjpeg->Instance->HUFFENC_AC0))
2763   {
2764     address = (hjpeg->Instance->HUFFENC_AC0 + (JPEG_AC_HUFF_TABLE_SIZE / 2UL));
2765   }
2766   else if (ACTableAddress == (hjpeg->Instance->HUFFENC_AC1))
2767   {
2768     address = (hjpeg->Instance->HUFFENC_AC1 + (JPEG_AC_HUFF_TABLE_SIZE / 2UL));
2769   }
2770   else
2771   {
2772     return HAL_ERROR;
2773   }
2774 
2775   if (HuffTableAC != NULL)
2776   {
2777     error = JPEG_ACHuff_BitsVals_To_SizeCodes(HuffTableAC, &acSizeCodesTable);
2778     if (error != HAL_OK)
2779     {
2780       return  error;
2781     }
2782     /* Default values settings: 162:167 FFFh , 168:175 FD0h_FD7h */
2783     /* Locations 162:175 of each AC table contain information used internally by the core */
2784 
2785     addressDef = address;
2786     for (i = 0; i < 3UL; i++)
2787     {
2788       *addressDef = 0x0FFF0FFF;
2789       addressDef++;
2790     }
2791     *addressDef = 0x0FD10FD0;
2792     addressDef++;
2793     *addressDef = 0x0FD30FD2;
2794     addressDef++;
2795     *addressDef = 0x0FD50FD4;
2796     addressDef++;
2797     *addressDef = 0x0FD70FD6;
2798     /* end of Locations 162:175  */
2799 
2800 
2801     i = JPEG_AC_HUFF_TABLE_SIZE;
2802     while (i > 1UL)
2803     {
2804       i--;
2805       address--;
2806       msb = ((uint32_t)(((uint32_t)acSizeCodesTable.CodeLength[i] & 0xFU) << 8)) | ((uint32_t)acSizeCodesTable.HuffmanCode[i] &
2807                                                                                    0xFFUL);
2808       i--;
2809       lsb = ((uint32_t)(((uint32_t)acSizeCodesTable.CodeLength[i] & 0xFU) << 8)) | ((uint32_t)acSizeCodesTable.HuffmanCode[i] &
2810                                                                                    0xFFUL);
2811 
2812       *address = lsb | (msb << 16);
2813     }
2814   }
2815 
2816   /* Return function status */
2817   return HAL_OK;
2818 }
2819 
2820 /**
2821   * @brief  Configure the JPEG encoder register huffman tables to used during
2822   *         the encdoing operation
2823   * @param  hjpeg pointer to a JPEG_HandleTypeDef structure that contains
2824   *         the configuration information for JPEG module
2825   * @retval None
2826   */
JPEG_Set_HuffEnc_Mem(JPEG_HandleTypeDef * hjpeg)2827 static HAL_StatusTypeDef JPEG_Set_HuffEnc_Mem(JPEG_HandleTypeDef *hjpeg)
2828 {
2829   HAL_StatusTypeDef error;
2830 
2831   JPEG_Set_Huff_DHTMem(hjpeg);
2832   error = JPEG_Set_HuffAC_Mem(hjpeg, (JPEG_ACHuffTableTypeDef *)(uint32_t)&JPEG_ACLUM_HuffTable,
2833                               (hjpeg->Instance->HUFFENC_AC0));
2834   if (error != HAL_OK)
2835   {
2836     return  error;
2837   }
2838 
2839   error = JPEG_Set_HuffAC_Mem(hjpeg, (JPEG_ACHuffTableTypeDef *)(uint32_t)&JPEG_ACCHROM_HuffTable,
2840                               (hjpeg->Instance->HUFFENC_AC1));
2841   if (error != HAL_OK)
2842   {
2843     return  error;
2844   }
2845 
2846   error = JPEG_Set_HuffDC_Mem(hjpeg, (JPEG_DCHuffTableTypeDef *)(uint32_t)&JPEG_DCLUM_HuffTable,
2847                               hjpeg->Instance->HUFFENC_DC0);
2848   if (error != HAL_OK)
2849   {
2850     return  error;
2851   }
2852 
2853   error = JPEG_Set_HuffDC_Mem(hjpeg, (JPEG_DCHuffTableTypeDef *)(uint32_t)&JPEG_DCCHROM_HuffTable,
2854                               hjpeg->Instance->HUFFENC_DC1);
2855   if (error != HAL_OK)
2856   {
2857     return  error;
2858   }
2859   /* Return function status */
2860   return HAL_OK;
2861 }
2862 
2863 /**
2864   * @brief  Configure the JPEG register huffman tables to be included in the JPEG
2865   *         file header (used for encoding only)
2866   * @param  hjpeg pointer to a JPEG_HandleTypeDef structure that contains
2867   *         the configuration information for JPEG module
2868   * @retval None
2869   */
JPEG_Set_Huff_DHTMem(JPEG_HandleTypeDef * hjpeg)2870 static void JPEG_Set_Huff_DHTMem(JPEG_HandleTypeDef *hjpeg)
2871 {
2872   JPEG_ACHuffTableTypeDef *HuffTableAC0 = (JPEG_ACHuffTableTypeDef *)(uint32_t)&JPEG_ACLUM_HuffTable;
2873   JPEG_ACHuffTableTypeDef *HuffTableAC1 = (JPEG_ACHuffTableTypeDef *)(uint32_t)&JPEG_ACCHROM_HuffTable;
2874   JPEG_DCHuffTableTypeDef *HuffTableDC0 = (JPEG_DCHuffTableTypeDef *)(uint32_t)&JPEG_DCLUM_HuffTable;
2875   JPEG_DCHuffTableTypeDef *HuffTableDC1 = (JPEG_DCHuffTableTypeDef *)(uint32_t)&JPEG_DCCHROM_HuffTable;
2876   uint32_t value, index;
2877   __IO uint32_t *address;
2878 
2879   /* DC0 Huffman Table : BITS*/
2880   /* DC0 BITS is a 16 Bytes table i.e 4x32bits words from DHTMEM base address to DHTMEM + 3*/
2881   address = (hjpeg->Instance->DHTMEM + 3);
2882   index = 16;
2883   while (index > 3UL)
2884   {
2885 
2886     *address = (((uint32_t)HuffTableDC0->Bits[index - 1UL] & 0xFFUL) << 24) |
2887                (((uint32_t)HuffTableDC0->Bits[index - 2UL] & 0xFFUL) << 16) |
2888                (((uint32_t)HuffTableDC0->Bits[index - 3UL] & 0xFFUL) << 8) |
2889                ((uint32_t)HuffTableDC0->Bits[index - 4UL] & 0xFFUL);
2890     address--;
2891     index -= 4UL;
2892 
2893   }
2894   /* DC0 Huffman Table : Val*/
2895   /* DC0 VALS is a 12 Bytes table i.e 3x32bits words from DHTMEM base address +4 to DHTMEM + 6 */
2896   address = (hjpeg->Instance->DHTMEM + 6);
2897   index = 12;
2898   while (index > 3UL)
2899   {
2900     *address = (((uint32_t)HuffTableDC0->HuffVal[index - 1UL] & 0xFFUL) << 24) |
2901                (((uint32_t)HuffTableDC0->HuffVal[index - 2UL] & 0xFFUL) << 16) |
2902                (((uint32_t)HuffTableDC0->HuffVal[index - 3UL] & 0xFFUL) << 8) |
2903                ((uint32_t)HuffTableDC0->HuffVal[index - 4UL] & 0xFFUL);
2904     address--;
2905     index -= 4UL;
2906   }
2907 
2908   /* AC0 Huffman Table : BITS*/
2909   /* AC0 BITS is a 16 Bytes table i.e 4x32bits words from DHTMEM base address + 7 to DHTMEM + 10*/
2910   address = (hjpeg->Instance->DHTMEM + 10UL);
2911   index = 16;
2912   while (index > 3UL)
2913   {
2914 
2915     *address = (((uint32_t)HuffTableAC0->Bits[index - 1UL] & 0xFFUL) << 24) |
2916                (((uint32_t)HuffTableAC0->Bits[index - 2UL] & 0xFFUL) << 16) |
2917                (((uint32_t)HuffTableAC0->Bits[index - 3UL] & 0xFFUL) << 8) |
2918                ((uint32_t)HuffTableAC0->Bits[index - 4UL] & 0xFFUL);
2919     address--;
2920     index -= 4UL;
2921 
2922   }
2923   /* AC0 Huffman Table : Val*/
2924   /* AC0 VALS is a 162 Bytes table i.e 41x32bits words from DHTMEM base address + 11 to DHTMEM + 51 */
2925   /* only Byte 0 and Byte 1 of the last word (@ DHTMEM + 51) belong to AC0 VALS table */
2926   address = (hjpeg->Instance->DHTMEM + 51);
2927   value = *address & 0xFFFF0000U;
2928   value = value | (((uint32_t)HuffTableAC0->HuffVal[161] & 0xFFUL) << 8) | ((uint32_t)HuffTableAC0->HuffVal[160] & 0xFFUL);
2929   *address = value;
2930 
2931   /*continue setting 160 AC0 huffman values */
2932   address--; /* address = hjpeg->Instance->DHTMEM + 50*/
2933   index = 160;
2934   while (index > 3UL)
2935   {
2936     *address = (((uint32_t)HuffTableAC0->HuffVal[index - 1UL] & 0xFFUL) << 24) |
2937                (((uint32_t)HuffTableAC0->HuffVal[index - 2UL] & 0xFFUL) << 16) |
2938                (((uint32_t)HuffTableAC0->HuffVal[index - 3UL] & 0xFFUL) << 8) |
2939                ((uint32_t)HuffTableAC0->HuffVal[index - 4UL] & 0xFFUL);
2940     address--;
2941     index -= 4UL;
2942   }
2943 
2944   /* DC1 Huffman Table : BITS*/
2945   /* DC1 BITS is a 16 Bytes table i.e 4x32bits words from DHTMEM + 51 base address to DHTMEM + 55*/
2946   /* only Byte 2 and Byte 3 of the first word (@ DHTMEM + 51) belong to DC1 Bits table */
2947   address = (hjpeg->Instance->DHTMEM + 51);
2948   value = *address & 0x0000FFFFU;
2949   value = value | (((uint32_t)HuffTableDC1->Bits[1] & 0xFFUL) << 24) | (((uint32_t)HuffTableDC1->Bits[0] & 0xFFUL) << 16);
2950   *address = value;
2951 
2952   /* only Byte 0 and Byte 1 of the last word (@ DHTMEM + 55) belong to DC1 Bits table */
2953   address = (hjpeg->Instance->DHTMEM + 55);
2954   value = *address & 0xFFFF0000U;
2955   value = value | (((uint32_t)HuffTableDC1->Bits[15] & 0xFFUL) << 8) | ((uint32_t)HuffTableDC1->Bits[14] & 0xFFUL);
2956   *address = value;
2957 
2958   /*continue setting 12 DC1 huffman Bits from DHTMEM + 54 down to DHTMEM + 52*/
2959   address--;
2960   index = 12;
2961   while (index > 3UL)
2962   {
2963 
2964     *address = (((uint32_t)HuffTableDC1->Bits[index + 1UL] & 0xFFUL) << 24) |
2965                (((uint32_t)HuffTableDC1->Bits[index] & 0xFFUL) << 16) |
2966                (((uint32_t)HuffTableDC1->Bits[index - 1UL] & 0xFFUL) << 8) |
2967                ((uint32_t)HuffTableDC1->Bits[index - 2UL] & 0xFFUL);
2968     address--;
2969     index -= 4UL;
2970 
2971   }
2972   /* DC1 Huffman Table : Val*/
2973   /* DC1 VALS is a 12 Bytes table i.e 3x32bits words from DHTMEM base address +55 to DHTMEM + 58 */
2974   /* only Byte 2 and Byte 3 of the first word (@ DHTMEM + 55) belong to DC1 Val table */
2975   address = (hjpeg->Instance->DHTMEM + 55);
2976   value = *address & 0x0000FFFFUL;
2977   value = value | (((uint32_t)HuffTableDC1->HuffVal[1] & 0xFFUL) << 24) | (((uint32_t)HuffTableDC1->HuffVal[0] & 0xFFUL) <<
2978                                                                          16);
2979   *address = value;
2980 
2981   /* only Byte 0 and Byte 1 of the last word (@ DHTMEM + 58) belong to DC1 Val table */
2982   address = (hjpeg->Instance->DHTMEM + 58);
2983   value = *address & 0xFFFF0000UL;
2984   value = value | (((uint32_t)HuffTableDC1->HuffVal[11] & 0xFFUL) << 8) | ((uint32_t)HuffTableDC1->HuffVal[10] & 0xFFUL);
2985   *address = value;
2986 
2987   /*continue setting 8 DC1 huffman val from DHTMEM + 57 down to DHTMEM + 56*/
2988   address--;
2989   index = 8;
2990   while (index > 3UL)
2991   {
2992     *address = (((uint32_t)HuffTableDC1->HuffVal[index + 1UL] & 0xFFUL) << 24) |
2993                (((uint32_t)HuffTableDC1->HuffVal[index] & 0xFFUL) << 16) |
2994                (((uint32_t)HuffTableDC1->HuffVal[index - 1UL] & 0xFFUL) << 8) |
2995                ((uint32_t)HuffTableDC1->HuffVal[index - 2UL] & 0xFFUL);
2996     address--;
2997     index -= 4UL;
2998   }
2999 
3000   /* AC1 Huffman Table : BITS*/
3001   /* AC1 BITS is a 16 Bytes table i.e 4x32bits words from DHTMEM base address + 58 to DHTMEM + 62*/
3002   /* only Byte 2 and Byte 3 of the first word (@ DHTMEM + 58) belong to AC1 Bits table */
3003   address = (hjpeg->Instance->DHTMEM + 58);
3004   value = *address & 0x0000FFFFU;
3005   value = value | (((uint32_t)HuffTableAC1->Bits[1] & 0xFFUL) << 24) | (((uint32_t)HuffTableAC1->Bits[0] & 0xFFUL) << 16);
3006   *address = value;
3007 
3008   /* only Byte 0 and Byte 1 of the last word (@ DHTMEM + 62) belong to Bits Val table */
3009   address = (hjpeg->Instance->DHTMEM + 62);
3010   value = *address & 0xFFFF0000U;
3011   value = value | (((uint32_t)HuffTableAC1->Bits[15] & 0xFFUL) << 8) | ((uint32_t)HuffTableAC1->Bits[14] & 0xFFUL);
3012   *address = value;
3013 
3014   /*continue setting 12 AC1 huffman Bits from DHTMEM + 61 down to DHTMEM + 59*/
3015   address--;
3016   index = 12;
3017   while (index > 3UL)
3018   {
3019 
3020     *address = (((uint32_t)HuffTableAC1->Bits[index + 1UL] & 0xFFUL) << 24) |
3021                (((uint32_t)HuffTableAC1->Bits[index] & 0xFFUL) << 16) |
3022                (((uint32_t)HuffTableAC1->Bits[index - 1UL] & 0xFFUL) << 8) |
3023                ((uint32_t)HuffTableAC1->Bits[index - 2UL] & 0xFFUL);
3024     address--;
3025     index -= 4UL;
3026 
3027   }
3028   /* AC1 Huffman Table : Val*/
3029   /* AC1 VALS is a 162 Bytes table i.e 41x32bits words from DHTMEM base address + 62 to DHTMEM + 102 */
3030   /* only Byte 2 and Byte 3 of the first word (@ DHTMEM + 62) belong to AC1 VALS table */
3031   address = (hjpeg->Instance->DHTMEM + 62);
3032   value = *address & 0x0000FFFFUL;
3033   value = value | (((uint32_t)HuffTableAC1->HuffVal[1] & 0xFFUL) << 24) | (((uint32_t)HuffTableAC1->HuffVal[0] & 0xFFUL) <<
3034                                                                          16);
3035   *address = value;
3036 
3037   /*continue setting 160 AC1 huffman values from DHTMEM + 63 to DHTMEM+102 */
3038   address = (hjpeg->Instance->DHTMEM + 102);
3039   index = 160;
3040   while (index > 3UL)
3041   {
3042     *address = (((uint32_t)HuffTableAC1->HuffVal[index + 1UL] & 0xFFUL) << 24) |
3043                (((uint32_t)HuffTableAC1->HuffVal[index] & 0xFFUL) << 16) |
3044                (((uint32_t)HuffTableAC1->HuffVal[index - 1UL] & 0xFFUL) << 8) |
3045                ((uint32_t)HuffTableAC1->HuffVal[index - 2UL] & 0xFFUL);
3046     address--;
3047     index -= 4UL;
3048   }
3049 
3050 }
3051 
3052 /**
3053   * @brief  Configure the JPEG registers with a given quantization table
3054   * @param  hjpeg pointer to a JPEG_HandleTypeDef structure that contains
3055   *         the configuration information for JPEG module
3056   * @param  QTable pointer to an array of 64 bytes giving the quantization table
3057   * @param  QTableAddress destination quantization address in the JPEG peripheral
3058   *         it could be QMEM0, QMEM1, QMEM2 or QMEM3
3059   * @retval 0 if no error, 1 if error
3060   */
JPEG_Set_Quantization_Mem(JPEG_HandleTypeDef * hjpeg,uint8_t * QTable,__IO uint32_t * QTableAddress)3061 static uint32_t JPEG_Set_Quantization_Mem(JPEG_HandleTypeDef *hjpeg, uint8_t *QTable,
3062                                                     __IO uint32_t *QTableAddress)
3063 {
3064   uint32_t i;
3065   uint32_t j;
3066   uint32_t quantRow;
3067   uint32_t quantVal;
3068   uint32_t ScaleFactor;
3069   __IO uint32_t *tableAddress;
3070 
3071   tableAddress = QTableAddress;
3072 
3073   if ((hjpeg->Conf.ImageQuality >= 50UL) && (hjpeg->Conf.ImageQuality <= 100UL))
3074   {
3075     ScaleFactor = 200UL - (hjpeg->Conf.ImageQuality * 2UL);
3076   }
3077   else if (hjpeg->Conf.ImageQuality > 0UL)
3078   {
3079     ScaleFactor = ((uint32_t) 5000) / ((uint32_t) hjpeg->Conf.ImageQuality);
3080   }
3081   else
3082   {
3083     return 1UL;
3084   }
3085 
3086   /*Quantization_table = (Standard_quanization_table * ScaleFactor + 50) / 100*/
3087   i = 0;
3088   while (i < (JPEG_QUANT_TABLE_SIZE - 3UL))
3089   {
3090     quantRow = 0;
3091     for (j = 0; j < 4UL; j++)
3092     {
3093       /* Note that the quantization coefficients must be specified in the table in zigzag order */
3094       quantVal = ((((uint32_t) QTable[JPEG_ZIGZAG_ORDER[i + j]]) * ScaleFactor) + 50UL) / 100UL;
3095 
3096       if (quantVal == 0UL)
3097       {
3098         quantVal = 1UL;
3099       }
3100       else if (quantVal > 255UL)
3101       {
3102         quantVal = 255UL;
3103       }
3104       else
3105       {
3106         /* Nothing to do, keep same value of quantVal */
3107       }
3108 
3109       quantRow |= ((quantVal & 0xFFUL) << (8UL * j));
3110     }
3111 
3112     i += 4UL;
3113     *tableAddress = quantRow;
3114     tableAddress ++;
3115   }
3116 
3117   /* Return function status */
3118   return 0UL;
3119 }
3120 
3121 /**
3122   * @brief  Configure the JPEG registers for YCbCr color space
3123   * @param  hjpeg pointer to a JPEG_HandleTypeDef structure that contains
3124   *         the configuration information for JPEG module
3125   * @retval None
3126   */
JPEG_SetColorYCBCR(JPEG_HandleTypeDef * hjpeg)3127 static void JPEG_SetColorYCBCR(JPEG_HandleTypeDef *hjpeg)
3128 {
3129   uint32_t ySamplingH;
3130   uint32_t ySamplingV;
3131   uint32_t yblockNb;
3132 
3133   /*Set Number of color components to 3*/
3134   hjpeg->Instance->CONFR1 &=  ~JPEG_CONFR1_NF;
3135   hjpeg->Instance->CONFR1 |=  JPEG_CONFR1_NF_1;
3136 
3137   /* compute MCU block size and Y, Cb ,Cr sampling factors*/
3138   if (hjpeg->Conf.ChromaSubsampling == JPEG_420_SUBSAMPLING)
3139   {
3140     ySamplingH  = JPEG_CONFR4_HSF_1;   /* Hs = 2*/
3141     ySamplingV  = JPEG_CONFR4_VSF_1;   /* Vs = 2*/
3142 
3143     yblockNb  = 0x30; /* 4 blocks of 8x8*/
3144   }
3145   else if (hjpeg->Conf.ChromaSubsampling == JPEG_422_SUBSAMPLING)
3146   {
3147     ySamplingH  = JPEG_CONFR4_HSF_1;   /* Hs = 2*/
3148     ySamplingV  = JPEG_CONFR4_VSF_0;   /* Vs = 1*/
3149 
3150     yblockNb  = 0x10; /* 2 blocks of 8x8*/
3151   }
3152   else /*JPEG_444_SUBSAMPLING and default*/
3153   {
3154     ySamplingH  = JPEG_CONFR4_HSF_0;   /* Hs = 1*/
3155     ySamplingV  = JPEG_CONFR4_VSF_0;   /* Vs = 1*/
3156 
3157     yblockNb  = 0; /* 1 block of 8x8*/
3158   }
3159 
3160   hjpeg->Instance->CONFR1 &= ~(JPEG_CONFR1_NF | JPEG_CONFR1_NS);
3161   hjpeg->Instance->CONFR1 |= (JPEG_CONFR1_NF_1 | JPEG_CONFR1_NS_1);
3162 
3163   /*Reset CONFR4 register*/
3164   hjpeg->Instance->CONFR4 =  0;
3165   /*Set Horizental and Vertical  sampling factor , number of blocks , Quantization table and Huffman AC/DC tables for component 0*/
3166   hjpeg->Instance->CONFR4 |= (ySamplingH | ySamplingV | (yblockNb & JPEG_CONFR4_NB));
3167 
3168   /*Reset CONFR5 register*/
3169   hjpeg->Instance->CONFR5 =  0;
3170   /*Set Horizental and Vertical  sampling factor , number of blocks , Quantization table and Huffman AC/DC tables for component 1*/
3171   hjpeg->Instance->CONFR5 |= (JPEG_CONFR5_HSF_0 | JPEG_CONFR5_VSF_0 | JPEG_CONFR5_QT_0 | JPEG_CONFR5_HA | JPEG_CONFR5_HD);
3172 
3173   /*Reset CONFR6 register*/
3174   hjpeg->Instance->CONFR6 =  0;
3175   /*Set Horizental and Vertical  sampling factor and number of blocks for component 2*/
3176   /* In YCBCR , by default, both chrominance components (component 1 and component 2) use the same Quantization table (table 1) */
3177   /* In YCBCR , both chrominance components (component 1 and component 2) use the same Huffman tables (table 1) */
3178   hjpeg->Instance->CONFR6 |= (JPEG_CONFR6_HSF_0 | JPEG_CONFR6_VSF_0 | JPEG_CONFR6_QT_0 | JPEG_CONFR6_HA | JPEG_CONFR6_HD);
3179 
3180 }
3181 
3182 /**
3183   * @brief  Configure the JPEG registers for GrayScale color space
3184   * @param  hjpeg pointer to a JPEG_HandleTypeDef structure that contains
3185   *         the configuration information for JPEG module
3186   * @retval None
3187   */
JPEG_SetColorGrayScale(JPEG_HandleTypeDef * hjpeg)3188 static void JPEG_SetColorGrayScale(JPEG_HandleTypeDef *hjpeg)
3189 {
3190   /*Set Number of color components to 1*/
3191   hjpeg->Instance->CONFR1 &= ~(JPEG_CONFR1_NF | JPEG_CONFR1_NS);
3192 
3193   /*in GrayScale use 1 single Quantization table (Table 0)*/
3194   /*in GrayScale use only one couple of AC/DC huffman table (table 0)*/
3195 
3196   /*Reset CONFR4 register*/
3197   hjpeg->Instance->CONFR4 =  0;
3198   /*Set Horizental and Vertical  sampling factor , number of blocks , Quantization table and Huffman AC/DC tables for component 0*/
3199   hjpeg->Instance->CONFR4 |=  JPEG_CONFR4_HSF_0 | JPEG_CONFR4_VSF_0 ;
3200 }
3201 
3202 /**
3203   * @brief  Configure the JPEG registers for CMYK color space
3204   * @param  hjpeg pointer to a JPEG_HandleTypeDef structure that contains
3205   *         the configuration information for JPEG module
3206   * @retval None
3207   */
JPEG_SetColorCMYK(JPEG_HandleTypeDef * hjpeg)3208 static void JPEG_SetColorCMYK(JPEG_HandleTypeDef *hjpeg)
3209 {
3210   uint32_t ySamplingH;
3211   uint32_t ySamplingV;
3212   uint32_t yblockNb;
3213 
3214   /*Set Number of color components to 4*/
3215   hjpeg->Instance->CONFR1 |= (JPEG_CONFR1_NF | JPEG_CONFR1_NS);
3216 
3217   /* compute MCU block size and Y, Cb ,Cr sampling factors*/
3218   if (hjpeg->Conf.ChromaSubsampling == JPEG_420_SUBSAMPLING)
3219   {
3220     ySamplingH  = JPEG_CONFR4_HSF_1;   /* Hs = 2*/
3221     ySamplingV  = JPEG_CONFR4_VSF_1;   /* Vs = 2*/
3222 
3223     yblockNb  = 0x30; /* 4 blocks of 8x8*/
3224   }
3225   else if (hjpeg->Conf.ChromaSubsampling == JPEG_422_SUBSAMPLING)
3226   {
3227     ySamplingH  = JPEG_CONFR4_HSF_1;   /* Hs = 2*/
3228     ySamplingV  = JPEG_CONFR4_VSF_0;   /* Vs = 1*/
3229 
3230     yblockNb  = 0x10; /* 2 blocks of 8x8*/
3231   }
3232   else /*JPEG_444_SUBSAMPLING and default*/
3233   {
3234     ySamplingH  = JPEG_CONFR4_HSF_0;   /* Hs = 1*/
3235     ySamplingV  = JPEG_CONFR4_VSF_0;   /* Vs = 1*/
3236 
3237     yblockNb  = 0; /* 1 block of 8x8*/
3238   }
3239 
3240   /*Reset CONFR4 register*/
3241   hjpeg->Instance->CONFR4 =  0;
3242   /*Set Horizental and Vertical  sampling factor , number of blocks , Quantization table and Huffman AC/DC tables for component 0*/
3243   hjpeg->Instance->CONFR4 |= (ySamplingH | ySamplingV | (yblockNb & JPEG_CONFR4_NB));
3244 
3245   /*Reset CONFR5 register*/
3246   hjpeg->Instance->CONFR5 =  0;
3247   /*Set Horizental and Vertical  sampling factor , number of blocks , Quantization table and Huffman AC/DC tables for component 1*/
3248   hjpeg->Instance->CONFR5 |= (JPEG_CONFR5_HSF_0 | JPEG_CONFR5_VSF_0);
3249 
3250   /*Reset CONFR6 register*/
3251   hjpeg->Instance->CONFR6 =  0;
3252   /*Set Horizental and Vertical  sampling factor , number of blocks , Quantization table and Huffman AC/DC tables for component 2*/
3253   hjpeg->Instance->CONFR6 |= (JPEG_CONFR6_HSF_0 | JPEG_CONFR6_VSF_0);
3254 
3255   /*Reset CONFR7 register*/
3256   hjpeg->Instance->CONFR7 =  0;
3257   /*Set Horizental and Vertical  sampling factor , number of blocks , Quantization table and Huffman AC/DC tables for component 3*/
3258   hjpeg->Instance->CONFR7 |= (JPEG_CONFR7_HSF_0 | JPEG_CONFR7_VSF_0);
3259 }
3260 
3261 /**
3262   * @brief  Init the JPEG encoding/decoding process in case of Polling or Interrupt and DMA
3263   * @param  hjpeg pointer to a JPEG_HandleTypeDef structure that contains
3264   *         the configuration information for JPEG module
3265   * @retval None
3266   */
JPEG_Init_Process(JPEG_HandleTypeDef * hjpeg)3267 static void JPEG_Init_Process(JPEG_HandleTypeDef *hjpeg)
3268 {
3269   /*Reset pause*/
3270   hjpeg->Context &= (~(JPEG_CONTEXT_PAUSE_INPUT | JPEG_CONTEXT_PAUSE_OUTPUT));
3271 
3272   if ((hjpeg->Context & JPEG_CONTEXT_OPERATION_MASK) == JPEG_CONTEXT_DECODE)
3273   {
3274     /*Set JPEG Codec to Decoding mode */
3275     hjpeg->Instance->CONFR1 |= JPEG_CONFR1_DE;
3276   }
3277   else /* JPEG_CONTEXT_ENCODE */
3278   {
3279     /*Set JPEG Codec to Encoding mode */
3280     hjpeg->Instance->CONFR1 &= ~JPEG_CONFR1_DE;
3281   }
3282 
3283   /*Stop JPEG processing */
3284   hjpeg->Instance->CONFR0 &=  ~JPEG_CONFR0_START;
3285 
3286   /* Disable All Interrupts */
3287   __HAL_JPEG_DISABLE_IT(hjpeg, JPEG_INTERRUPT_MASK);
3288 
3289   /* Disable All DMA requests */
3290   JPEG_DISABLE_DMA(hjpeg, JPEG_DMA_MASK);
3291   /* Flush input and output FIFOs*/
3292   hjpeg->Instance->CR |= JPEG_CR_IFF;
3293   hjpeg->Instance->CR |= JPEG_CR_OFF;
3294 
3295   /* Clear all flags */
3296   __HAL_JPEG_CLEAR_FLAG(hjpeg, JPEG_FLAG_ALL);
3297 
3298   /*Start Encoding/Decoding*/
3299   hjpeg->Instance->CONFR0 |=  JPEG_CONFR0_START;
3300 
3301   if ((hjpeg->Context & JPEG_CONTEXT_METHOD_MASK) == JPEG_CONTEXT_IT)
3302   {
3303     /*Enable IN/OUT, end of Conversation, and end of header parsing interruptions*/
3304     __HAL_JPEG_ENABLE_IT(hjpeg, JPEG_IT_IFT | JPEG_IT_IFNF | JPEG_IT_OFT | JPEG_IT_OFNE | JPEG_IT_EOC | JPEG_IT_HPD);
3305   }
3306   else if ((hjpeg->Context & JPEG_CONTEXT_METHOD_MASK) == JPEG_CONTEXT_DMA)
3307   {
3308     /*Enable End Of Conversation, and End Of Header parsing interruptions*/
3309     __HAL_JPEG_ENABLE_IT(hjpeg, JPEG_IT_EOC | JPEG_IT_HPD);
3310 
3311   }
3312   else
3313   {
3314     /* Nothing to do */
3315   }
3316 }
3317 
3318 /**
3319   * @brief  JPEG encoding/decoding process in case of Polling or Interrupt
3320   * @param  hjpeg pointer to a JPEG_HandleTypeDef structure that contains
3321   *         the configuration information for JPEG module
3322   * @retval JPEG_PROCESS_DONE if the process has ends else JPEG_PROCESS_ONGOING
3323   */
JPEG_Process(JPEG_HandleTypeDef * hjpeg)3324 static uint32_t JPEG_Process(JPEG_HandleTypeDef *hjpeg)
3325 {
3326   uint32_t tmpContext;
3327 
3328   /*End of header processing flag */
3329   if ((hjpeg->Context & JPEG_CONTEXT_OPERATION_MASK) == JPEG_CONTEXT_DECODE)
3330   {
3331     if (__HAL_JPEG_GET_FLAG(hjpeg, JPEG_FLAG_HPDF) != 0UL)
3332     {
3333       /*Call Header parsing complete callback */
3334       (void) HAL_JPEG_GetInfo(hjpeg, &hjpeg->Conf);
3335       /* Reset the ImageQuality */
3336       hjpeg->Conf.ImageQuality = 0;
3337       /* Note : the image quality is only available at the end of the decoding operation */
3338       /* at the current stage the calculated image quality is not correct so reset it */
3339 
3340       /*Call Info Ready callback */
3341 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
3342       hjpeg->InfoReadyCallback(hjpeg, &hjpeg->Conf);
3343 #else
3344       HAL_JPEG_InfoReadyCallback(hjpeg, &hjpeg->Conf);
3345 #endif /* USE_HAL_JPEG_REGISTER_CALLBACKS */
3346 
3347       __HAL_JPEG_DISABLE_IT(hjpeg, JPEG_IT_HPD);
3348 
3349       /* Clear header processing done flag */
3350       __HAL_JPEG_CLEAR_FLAG(hjpeg, JPEG_FLAG_HPDF);
3351     }
3352   }
3353 
3354   /*Input FIFO status handling*/
3355   if ((hjpeg->Context &  JPEG_CONTEXT_PAUSE_INPUT) == 0UL)
3356   {
3357     if (__HAL_JPEG_GET_FLAG(hjpeg, JPEG_FLAG_IFTF) != 0UL)
3358     {
3359       /*Input FIFO threshold flag */
3360       /*JPEG_FIFO_TH_SIZE words can be written in */
3361       JPEG_ReadInputData(hjpeg, JPEG_FIFO_TH_SIZE);
3362     }
3363     else if (__HAL_JPEG_GET_FLAG(hjpeg, JPEG_FLAG_IFNFF) != 0UL)
3364     {
3365       /*Input FIFO Not Full flag */
3366       /*32-bit value can be written in */
3367       JPEG_ReadInputData(hjpeg, 1);
3368     }
3369     else
3370     {
3371       /* Nothing to do */
3372     }
3373   }
3374 
3375 
3376   /*Output FIFO flag handling*/
3377   if ((hjpeg->Context &  JPEG_CONTEXT_PAUSE_OUTPUT) == 0UL)
3378   {
3379     if (__HAL_JPEG_GET_FLAG(hjpeg, JPEG_FLAG_OFTF) != 0UL)
3380     {
3381       /*Output FIFO threshold flag */
3382       /*JPEG_FIFO_TH_SIZE words can be read out */
3383       JPEG_StoreOutputData(hjpeg, JPEG_FIFO_TH_SIZE);
3384     }
3385     else if (__HAL_JPEG_GET_FLAG(hjpeg, JPEG_FLAG_OFNEF) != 0UL)
3386     {
3387       /*Output FIFO Not Empty flag */
3388       /*32-bit value can be read out */
3389       JPEG_StoreOutputData(hjpeg, 1);
3390     }
3391     else
3392     {
3393       /* Nothing to do */
3394     }
3395   }
3396 
3397   /*End of Conversion handling :i.e EOC flag is high and OFTF low and OFNEF low*/
3398   if (__HAL_JPEG_GET_FLAG(hjpeg, JPEG_FLAG_EOCF | JPEG_FLAG_OFTF | JPEG_FLAG_OFNEF) == JPEG_FLAG_EOCF)
3399   {
3400     /*Stop Encoding/Decoding*/
3401     hjpeg->Instance->CONFR0 &=  ~JPEG_CONFR0_START;
3402 
3403     if ((hjpeg->Context & JPEG_CONTEXT_METHOD_MASK) == JPEG_CONTEXT_IT)
3404     {
3405       /* Disable All Interrupts */
3406       __HAL_JPEG_DISABLE_IT(hjpeg, JPEG_INTERRUPT_MASK);
3407     }
3408 
3409     /* Clear all flags */
3410     __HAL_JPEG_CLEAR_FLAG(hjpeg, JPEG_FLAG_ALL);
3411 
3412     /*Call End of conversion callback */
3413     if (hjpeg->JpegOutCount > 0UL)
3414     {
3415       /*Output Buffer is not empty, call DecodedDataReadyCallback*/
3416 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
3417       hjpeg->DataReadyCallback(hjpeg, hjpeg->pJpegOutBuffPtr, hjpeg->JpegOutCount);
3418 #else
3419       HAL_JPEG_DataReadyCallback(hjpeg, hjpeg->pJpegOutBuffPtr, hjpeg->JpegOutCount);
3420 #endif /* USE_HAL_JPEG_REGISTER_CALLBACKS */
3421 
3422       hjpeg->JpegOutCount = 0;
3423     }
3424 
3425     /*Reset Context Operation*/
3426     tmpContext = hjpeg->Context;
3427     /*Clear all context fields execpt JPEG_CONTEXT_CONF_ENCODING and JPEG_CONTEXT_CUSTOM_TABLES*/
3428     hjpeg->Context &= (JPEG_CONTEXT_CONF_ENCODING | JPEG_CONTEXT_CUSTOM_TABLES);
3429 
3430     /* Process Unlocked */
3431     __HAL_UNLOCK(hjpeg);
3432 
3433     /* Change the JPEG state */
3434     hjpeg->State = HAL_JPEG_STATE_READY;
3435 
3436     /*Call End of Encoding/Decoding callback */
3437     if ((tmpContext & JPEG_CONTEXT_OPERATION_MASK) == JPEG_CONTEXT_DECODE)
3438     {
3439 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
3440       hjpeg->DecodeCpltCallback(hjpeg);
3441 #else
3442       HAL_JPEG_DecodeCpltCallback(hjpeg);
3443 #endif /*USE_HAL_JPEG_REGISTER_CALLBACKS*/
3444     }
3445     else /* JPEG_CONTEXT_ENCODE */
3446     {
3447 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
3448       hjpeg->EncodeCpltCallback(hjpeg);
3449 #else
3450       HAL_JPEG_EncodeCpltCallback(hjpeg);
3451 #endif
3452     }
3453 
3454     return JPEG_PROCESS_DONE;
3455   }
3456 
3457 
3458   return JPEG_PROCESS_ONGOING;
3459 }
3460 
3461 /**
3462   * @brief  Store some output data from the JPEG peripheral to the output buffer.
3463   *         This function is used when the JPEG peripheral has new data to output
3464   *         in case of Polling or Interrupt process
3465   * @param  hjpeg pointer to a JPEG_HandleTypeDef structure that contains
3466   *         the configuration information for JPEG module
3467   * @param  nbOutputWords Number of output words (of 32 bits) ready from the JPEG peripheral
3468   * @retval None
3469   */
JPEG_StoreOutputData(JPEG_HandleTypeDef * hjpeg,uint32_t nbOutputWords)3470 static void JPEG_StoreOutputData(JPEG_HandleTypeDef *hjpeg, uint32_t nbOutputWords)
3471 {
3472   uint32_t index;
3473   uint32_t nb_words;
3474   uint32_t nb_bytes;
3475   uint32_t dataword;
3476 
3477   if (hjpeg->OutDataLength >= (hjpeg->JpegOutCount + (nbOutputWords * 4UL)))
3478   {
3479     for (index = 0; index < nbOutputWords; index++)
3480     {
3481       /*Transfer 32 bits from the JPEG output FIFO*/
3482       dataword = hjpeg->Instance->DOR;
3483       hjpeg->pJpegOutBuffPtr[hjpeg->JpegOutCount] = (uint8_t)(dataword & 0x000000FFUL);
3484       hjpeg->pJpegOutBuffPtr[hjpeg->JpegOutCount + 1UL] = (uint8_t)((dataword & 0x0000FF00UL) >> 8);
3485       hjpeg->pJpegOutBuffPtr[hjpeg->JpegOutCount + 2UL] = (uint8_t)((dataword & 0x00FF0000UL) >> 16);
3486       hjpeg->pJpegOutBuffPtr[hjpeg->JpegOutCount + 3UL] = (uint8_t)((dataword & 0xFF000000UL) >> 24);
3487       hjpeg->JpegOutCount += 4UL;
3488     }
3489     if (hjpeg->OutDataLength == hjpeg->JpegOutCount)
3490     {
3491       /*Output Buffer is full, call DecodedDataReadyCallback*/
3492 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
3493       hjpeg->DataReadyCallback(hjpeg, hjpeg->pJpegOutBuffPtr, hjpeg->JpegOutCount);
3494 #else
3495       HAL_JPEG_DataReadyCallback(hjpeg, hjpeg->pJpegOutBuffPtr, hjpeg->JpegOutCount);
3496 #endif /*USE_HAL_JPEG_REGISTER_CALLBACKS*/
3497       hjpeg->JpegOutCount = 0;
3498     }
3499   }
3500   else if (hjpeg->OutDataLength > hjpeg->JpegOutCount)
3501   {
3502     nb_words = (hjpeg->OutDataLength - hjpeg->JpegOutCount) / 4UL;
3503     for (index = 0; index < nb_words; index++)
3504     {
3505       /*Transfer 32 bits from the JPEG output FIFO*/
3506       dataword = hjpeg->Instance->DOR;
3507       hjpeg->pJpegOutBuffPtr[hjpeg->JpegOutCount] = (uint8_t)(dataword & 0x000000FFUL);
3508       hjpeg->pJpegOutBuffPtr[hjpeg->JpegOutCount + 1UL] = (uint8_t)((dataword & 0x0000FF00UL) >> 8);
3509       hjpeg->pJpegOutBuffPtr[hjpeg->JpegOutCount + 2UL] = (uint8_t)((dataword & 0x00FF0000UL) >> 16);
3510       hjpeg->pJpegOutBuffPtr[hjpeg->JpegOutCount + 3UL] = (uint8_t)((dataword & 0xFF000000UL) >> 24);
3511       hjpeg->JpegOutCount += 4UL;
3512     }
3513     if (hjpeg->OutDataLength == hjpeg->JpegOutCount)
3514     {
3515       /*Output Buffer is full, call DecodedDataReadyCallback*/
3516 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
3517       hjpeg->DataReadyCallback(hjpeg, hjpeg->pJpegOutBuffPtr, hjpeg->JpegOutCount);
3518 #else
3519       HAL_JPEG_DataReadyCallback(hjpeg, hjpeg->pJpegOutBuffPtr, hjpeg->JpegOutCount);
3520 #endif /* USE_HAL_JPEG_REGISTER_CALLBACKS */
3521       hjpeg->JpegOutCount = 0;
3522     }
3523     else
3524     {
3525       nb_bytes = hjpeg->OutDataLength - hjpeg->JpegOutCount;
3526       dataword = hjpeg->Instance->DOR;
3527       for (index = 0; index < nb_bytes; index++)
3528       {
3529         hjpeg->pJpegOutBuffPtr[hjpeg->JpegOutCount] = (uint8_t)((dataword >> (8UL * (index & 0x3UL))) & 0xFFUL);
3530         hjpeg->JpegOutCount++;
3531       }
3532       /*Output Buffer is full, call DecodedDataReadyCallback*/
3533 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
3534       hjpeg->DataReadyCallback(hjpeg, hjpeg->pJpegOutBuffPtr, hjpeg->JpegOutCount);
3535 #else
3536       HAL_JPEG_DataReadyCallback(hjpeg, hjpeg->pJpegOutBuffPtr, hjpeg->JpegOutCount);
3537 #endif /* USE_HAL_JPEG_REGISTER_CALLBACKS */
3538 
3539       hjpeg->JpegOutCount = 0;
3540 
3541       nb_bytes = 4UL - nb_bytes;
3542       for (index = nb_bytes; index < 4UL; index++)
3543       {
3544         hjpeg->pJpegOutBuffPtr[hjpeg->JpegOutCount] = (uint8_t)((dataword >> (8UL * index)) & 0xFFUL);
3545         hjpeg->JpegOutCount++;
3546       }
3547     }
3548   }
3549   else
3550   {
3551     /* Nothing to do */
3552   }
3553 }
3554 
3555 /**
3556   * @brief  Read some input Data from the input buffer.
3557   *         This function is used when the JPEG peripheral needs new data
3558   *         in case of Polling or Interrupt process
3559   * @param  hjpeg pointer to a JPEG_HandleTypeDef structure that contains
3560   *         the configuration information for JPEG module
3561   * @param  nbRequestWords Number of input words (of 32 bits) that the JPE peripheral request
3562   * @retval None
3563   */
JPEG_ReadInputData(JPEG_HandleTypeDef * hjpeg,uint32_t nbRequestWords)3564 static void JPEG_ReadInputData(JPEG_HandleTypeDef *hjpeg, uint32_t nbRequestWords)
3565 {
3566   uint32_t nb_bytes = 0;
3567   uint32_t nb_words;
3568   uint32_t index;
3569   uint32_t dataword;
3570   uint32_t input_count;
3571 
3572   if ((hjpeg->InDataLength == 0UL) || (nbRequestWords == 0UL))
3573   {
3574     /* No more Input data : nothing to do*/
3575     (void) HAL_JPEG_Pause(hjpeg, JPEG_PAUSE_RESUME_INPUT);
3576   }
3577   else if (hjpeg->InDataLength > hjpeg->JpegInCount)
3578   {
3579     nb_bytes = hjpeg->InDataLength - hjpeg->JpegInCount;
3580   }
3581   else if (hjpeg->InDataLength == hjpeg->JpegInCount)
3582   {
3583     /*Call HAL_JPEG_GetDataCallback to get new data */
3584 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
3585     hjpeg->GetDataCallback(hjpeg, hjpeg->JpegInCount);
3586 #else
3587     HAL_JPEG_GetDataCallback(hjpeg, hjpeg->JpegInCount);
3588 #endif /*USE_HAL_JPEG_REGISTER_CALLBACKS*/
3589 
3590     if (hjpeg->InDataLength > 4UL)
3591     {
3592       hjpeg->InDataLength = hjpeg->InDataLength - (hjpeg->InDataLength % 4UL);
3593     }
3594     hjpeg->JpegInCount = 0;
3595     nb_bytes = hjpeg->InDataLength;
3596   }
3597   else
3598   {
3599     /* Nothing to do */
3600   }
3601   if (((hjpeg->Context &  JPEG_CONTEXT_PAUSE_INPUT) == 0UL) && (nb_bytes > 0UL))
3602   {
3603     nb_words = nb_bytes / 4UL;
3604     if (nb_words >= nbRequestWords)
3605     {
3606       for (index = 0; index < nbRequestWords; index++)
3607       {
3608         input_count = hjpeg->JpegInCount;
3609         hjpeg->Instance->DIR = (((uint32_t)(hjpeg->pJpegInBuffPtr[input_count])) | \
3610                                 (((uint32_t)(hjpeg->pJpegInBuffPtr[input_count + 1UL])) << 8) | \
3611                                 (((uint32_t)(hjpeg->pJpegInBuffPtr[input_count + 2UL])) << 16) | \
3612                                 (((uint32_t)(hjpeg->pJpegInBuffPtr[input_count + 3UL])) << 24));
3613 
3614         hjpeg->JpegInCount += 4UL;
3615       }
3616     }
3617     else /*nb_words < nbRequestWords*/
3618     {
3619       if (nb_words > 0UL)
3620       {
3621         for (index = 0; index < nb_words; index++)
3622         {
3623           input_count = hjpeg->JpegInCount;
3624           hjpeg->Instance->DIR = (((uint32_t)(hjpeg->pJpegInBuffPtr[input_count])) | \
3625                                   (((uint32_t)(hjpeg->pJpegInBuffPtr[input_count + 1UL])) << 8) | \
3626                                   (((uint32_t)(hjpeg->pJpegInBuffPtr[input_count + 2UL])) << 16) | \
3627                                   (((uint32_t)(hjpeg->pJpegInBuffPtr[input_count + 3UL])) << 24));
3628 
3629           hjpeg->JpegInCount += 4UL;
3630         }
3631       }
3632       else
3633       {
3634         /* end of file*/
3635         dataword = 0;
3636         for (index = 0; index < nb_bytes; index++)
3637         {
3638           dataword |= (uint32_t)hjpeg->pJpegInBuffPtr[hjpeg->JpegInCount] << (8UL * (index & 0x03UL));
3639           hjpeg->JpegInCount++;
3640         }
3641         hjpeg->Instance->DIR = dataword;
3642       }
3643     }
3644   }
3645 }
3646 
3647 /**
3648   * @brief  Start the JPEG DMA process (encoding/decoding)
3649   * @param  hjpeg pointer to a JPEG_HandleTypeDef structure that contains
3650   *         the configuration information for JPEG module
3651   * @retval JPEG_PROCESS_DONE if process ends else JPEG_PROCESS_ONGOING
3652   */
JPEG_DMA_StartProcess(JPEG_HandleTypeDef * hjpeg)3653 static HAL_StatusTypeDef JPEG_DMA_StartProcess(JPEG_HandleTypeDef *hjpeg)
3654 {
3655   if ((hjpeg->InDataLength < 4UL) || (hjpeg->OutDataLength < 4UL))
3656   {
3657     return HAL_ERROR;
3658   }
3659   /* Reset Ending DMA internal context flag*/
3660   hjpeg->Context &= ~JPEG_CONTEXT_ENDING_DMA;
3661 
3662   /* Disable DMA In/Out Request*/
3663   JPEG_DISABLE_DMA(hjpeg, JPEG_DMA_ODMA | JPEG_DMA_IDMA);
3664 
3665   /* Set the JPEG DMA In transfer complete callback */
3666   hjpeg->hdmain->XferCpltCallback = JPEG_DMAInCpltCallback;
3667   /* Set the DMA In error callback */
3668   hjpeg->hdmain->XferErrorCallback = JPEG_DMAErrorCallback;
3669 
3670   /* Set the JPEG DMA Out transfer complete callback */
3671   hjpeg->hdmaout->XferCpltCallback = JPEG_DMAOutCpltCallback;
3672   /* Set the DMA Out error callback */
3673   hjpeg->hdmaout->XferErrorCallback = JPEG_DMAErrorCallback;
3674   /* Set the DMA Out Abort callback */
3675   hjpeg->hdmaout->XferAbortCallback = JPEG_DMAOutAbortCallback;
3676 
3677   /*DMA transfer size must be a multiple of 4 bytes i.e multiple of 32bits words*/
3678   hjpeg->InDataLength = hjpeg->InDataLength - (hjpeg->InDataLength % 4UL);
3679 
3680   /*DMA transfer size must be a multiple of 4 bytes i.e multiple of 32bits words*/
3681   hjpeg->OutDataLength = hjpeg->OutDataLength - (hjpeg->OutDataLength % 4UL);
3682 
3683   /* Start DMA FIFO In transfer */
3684   if (HAL_DMA_Start_IT(hjpeg->hdmain, (uint32_t)hjpeg->pJpegInBuffPtr, (uint32_t)&hjpeg->Instance->DIR,
3685                        hjpeg->InDataLength >> 2)  != HAL_OK)
3686   {
3687     hjpeg->ErrorCode |= HAL_JPEG_ERROR_DMA;
3688     return HAL_ERROR;
3689   }
3690 
3691   /* Start DMA FIFO Out transfer */
3692   if (HAL_DMA_Start_IT(hjpeg->hdmaout, (uint32_t)&hjpeg->Instance->DOR, (uint32_t)hjpeg->pJpegOutBuffPtr,
3693                        hjpeg->OutDataLength >> 2)  != HAL_OK)
3694   {
3695     hjpeg->ErrorCode |= HAL_JPEG_ERROR_DMA;
3696     return HAL_ERROR;
3697   }
3698 
3699   /* Enable JPEG In/Out DMA requests*/
3700   JPEG_ENABLE_DMA(hjpeg, JPEG_DMA_IDMA | JPEG_DMA_ODMA);
3701 
3702   return HAL_OK;
3703 }
3704 
3705 /**
3706   * @brief  Continue the current JPEG DMA process (encoding/decoding)
3707   * @param  hjpeg pointer to a JPEG_HandleTypeDef structure that contains
3708   *         the configuration information for JPEG module
3709   * @retval JPEG_PROCESS_DONE if process ends else JPEG_PROCESS_ONGOING
3710   */
JPEG_DMA_ContinueProcess(JPEG_HandleTypeDef * hjpeg)3711 static void JPEG_DMA_ContinueProcess(JPEG_HandleTypeDef *hjpeg)
3712 {
3713   /*End of header processing flag rises*/
3714   if ((hjpeg->Context & JPEG_CONTEXT_OPERATION_MASK) == JPEG_CONTEXT_DECODE)
3715   {
3716     if (__HAL_JPEG_GET_FLAG(hjpeg, JPEG_FLAG_HPDF) != 0UL)
3717     {
3718       /*Call Header parsing complete callback */
3719       (void) HAL_JPEG_GetInfo(hjpeg, &hjpeg->Conf);
3720 
3721       /* Reset the ImageQuality */
3722       hjpeg->Conf.ImageQuality = 0;
3723       /* Note : the image quality is only available at the end of the decoding operation */
3724       /* at the current stage the calculated image quality is not correct so reset it */
3725 
3726       /*Call Info Ready callback */
3727 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
3728       hjpeg->InfoReadyCallback(hjpeg, &hjpeg->Conf);
3729 #else
3730       HAL_JPEG_InfoReadyCallback(hjpeg, &hjpeg->Conf);
3731 #endif /* USE_HAL_JPEG_REGISTER_CALLBACKS */
3732 
3733       __HAL_JPEG_DISABLE_IT(hjpeg, JPEG_IT_HPD);
3734 
3735       /* Clear header processing done flag */
3736       __HAL_JPEG_CLEAR_FLAG(hjpeg, JPEG_FLAG_HPDF);
3737     }
3738   }
3739 
3740   /*End of Conversion handling*/
3741   if (__HAL_JPEG_GET_FLAG(hjpeg, JPEG_FLAG_EOCF) != 0UL)
3742   {
3743     /*Disabkle JPEG In/Out DMA Requests*/
3744     JPEG_DISABLE_DMA(hjpeg, JPEG_DMA_ODMA | JPEG_DMA_IDMA);
3745 
3746     hjpeg->Context |= JPEG_CONTEXT_ENDING_DMA;
3747 
3748     /*Stop Encoding/Decoding*/
3749     hjpeg->Instance->CONFR0 &=  ~JPEG_CONFR0_START;
3750 
3751     __HAL_JPEG_DISABLE_IT(hjpeg, JPEG_INTERRUPT_MASK);
3752 
3753     /* Clear all flags */
3754     __HAL_JPEG_CLEAR_FLAG(hjpeg, JPEG_FLAG_ALL);
3755 
3756     if (hjpeg->hdmain->State == HAL_DMA_STATE_BUSY)
3757     {
3758       /* Stop the DMA In Xfer*/
3759       (void) HAL_DMA_Abort_IT(hjpeg->hdmain);
3760     }
3761 
3762     if (hjpeg->hdmaout->State == HAL_DMA_STATE_BUSY)
3763     {
3764       /* Stop the DMA out Xfer*/
3765       (void) HAL_DMA_Abort_IT(hjpeg->hdmaout);
3766     }
3767     else
3768     {
3769       JPEG_DMA_EndProcess(hjpeg);
3770     }
3771   }
3772 
3773 
3774 }
3775 
3776 /**
3777   * @brief  Finalize the current JPEG DMA process (encoding/decoding)
3778   * @param  hjpeg pointer to a JPEG_HandleTypeDef structure that contains
3779   *         the configuration information for JPEG module
3780   * @retval JPEG_PROCESS_DONE
3781   */
JPEG_DMA_EndProcess(JPEG_HandleTypeDef * hjpeg)3782 static void JPEG_DMA_EndProcess(JPEG_HandleTypeDef *hjpeg)
3783 {
3784   uint32_t tmpContext;
3785   hjpeg->JpegOutCount = hjpeg->OutDataLength - ((hjpeg->hdmaout->Instance->NDTR & DMA_SxNDT) << 2);
3786 
3787   /*if Output Buffer is full, call HAL_JPEG_DataReadyCallback*/
3788   if (hjpeg->JpegOutCount == hjpeg->OutDataLength)
3789   {
3790 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
3791     hjpeg->DataReadyCallback(hjpeg, hjpeg->pJpegOutBuffPtr, hjpeg->JpegOutCount);
3792 #else
3793     HAL_JPEG_DataReadyCallback(hjpeg, hjpeg->pJpegOutBuffPtr, hjpeg->JpegOutCount);
3794 #endif /* USE_HAL_JPEG_REGISTER_CALLBACKS */
3795 
3796     hjpeg->JpegOutCount = 0;
3797   }
3798 
3799   /*Check if remaining data in the output FIFO*/
3800   if (__HAL_JPEG_GET_FLAG(hjpeg, JPEG_FLAG_OFNEF) == 0UL)
3801   {
3802     if (hjpeg->JpegOutCount > 0UL)
3803     {
3804       /*Output Buffer is not empty, call DecodedDataReadyCallback*/
3805 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
3806       hjpeg->DataReadyCallback(hjpeg, hjpeg->pJpegOutBuffPtr, hjpeg->JpegOutCount);
3807 #else
3808       HAL_JPEG_DataReadyCallback(hjpeg, hjpeg->pJpegOutBuffPtr, hjpeg->JpegOutCount);
3809 #endif /* USE_HAL_JPEG_REGISTER_CALLBACKS */
3810 
3811       hjpeg->JpegOutCount = 0;
3812     }
3813 
3814     /*Stop Encoding/Decoding*/
3815     hjpeg->Instance->CONFR0 &=  ~JPEG_CONFR0_START;
3816 
3817     tmpContext = hjpeg->Context;
3818     /*Clear all context fields execpt JPEG_CONTEXT_CONF_ENCODING and JPEG_CONTEXT_CUSTOM_TABLES*/
3819     hjpeg->Context &= (JPEG_CONTEXT_CONF_ENCODING | JPEG_CONTEXT_CUSTOM_TABLES);
3820 
3821     /* Process Unlocked */
3822     __HAL_UNLOCK(hjpeg);
3823 
3824     /* Change the JPEG state */
3825     hjpeg->State = HAL_JPEG_STATE_READY;
3826 
3827     /*Call End of Encoding/Decoding callback */
3828     if ((tmpContext & JPEG_CONTEXT_OPERATION_MASK) == JPEG_CONTEXT_DECODE)
3829     {
3830 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
3831       hjpeg->DecodeCpltCallback(hjpeg);
3832 #else
3833       HAL_JPEG_DecodeCpltCallback(hjpeg);
3834 #endif /* USE_HAL_JPEG_REGISTER_CALLBACKS */
3835     }
3836     else /* JPEG_CONTEXT_ENCODE */
3837     {
3838 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
3839       hjpeg->EncodeCpltCallback(hjpeg);
3840 #else
3841       HAL_JPEG_EncodeCpltCallback(hjpeg);
3842 #endif /* USE_HAL_JPEG_REGISTER_CALLBACKS */
3843     }
3844   }
3845   else if ((hjpeg->Context &  JPEG_CONTEXT_PAUSE_OUTPUT) == 0UL)
3846   {
3847     JPEG_DMA_PollResidualData(hjpeg);
3848   }
3849   else
3850   {
3851     /* Nothing to do */
3852   }
3853 
3854 }
3855 
3856 /**
3857   * @brief  Poll residual output data when DMA process (encoding/decoding)
3858   * @param  hjpeg pointer to a JPEG_HandleTypeDef structure that contains
3859   *         the configuration information for JPEG module
3860   * @retval None.
3861   */
JPEG_DMA_PollResidualData(JPEG_HandleTypeDef * hjpeg)3862 static void JPEG_DMA_PollResidualData(JPEG_HandleTypeDef *hjpeg)
3863 {
3864   uint32_t tmpContext;
3865   uint32_t count;
3866   uint32_t dataOut;
3867 
3868   for (count = JPEG_FIFO_SIZE; count > 0UL; count--)
3869   {
3870     if ((hjpeg->Context &  JPEG_CONTEXT_PAUSE_OUTPUT) == 0UL)
3871     {
3872       if (__HAL_JPEG_GET_FLAG(hjpeg, JPEG_FLAG_OFNEF) != 0UL)
3873       {
3874         dataOut = hjpeg->Instance->DOR;
3875         hjpeg->pJpegOutBuffPtr[hjpeg->JpegOutCount] = (uint8_t)(dataOut & 0x000000FFUL);
3876         hjpeg->pJpegOutBuffPtr[hjpeg->JpegOutCount + 1UL] = (uint8_t)((dataOut & 0x0000FF00UL) >> 8);
3877         hjpeg->pJpegOutBuffPtr[hjpeg->JpegOutCount + 2UL] = (uint8_t)((dataOut & 0x00FF0000UL) >> 16);
3878         hjpeg->pJpegOutBuffPtr[hjpeg->JpegOutCount + 3UL] = (uint8_t)((dataOut & 0xFF000000UL) >> 24);
3879         hjpeg->JpegOutCount += 4UL;
3880 
3881         if (hjpeg->JpegOutCount == hjpeg->OutDataLength)
3882         {
3883           /*Output Buffer is full, call HAL_JPEG_DataReadyCallback*/
3884 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
3885           hjpeg->DataReadyCallback(hjpeg, hjpeg->pJpegOutBuffPtr, hjpeg->JpegOutCount);
3886 #else
3887           HAL_JPEG_DataReadyCallback(hjpeg, hjpeg->pJpegOutBuffPtr, hjpeg->JpegOutCount);
3888 #endif /* USE_HAL_JPEG_REGISTER_CALLBACKS */
3889 
3890           hjpeg->JpegOutCount = 0;
3891         }
3892 
3893       }
3894     }
3895   }
3896 
3897   tmpContext = hjpeg->Context;
3898 
3899   if ((__HAL_JPEG_GET_FLAG(hjpeg, JPEG_FLAG_OFNEF) == 0UL) || ((tmpContext & JPEG_CONTEXT_PAUSE_OUTPUT) == 0UL))
3900   {
3901     /*Stop Encoding/Decoding*/
3902     hjpeg->Instance->CONFR0 &=  ~JPEG_CONFR0_START;
3903 
3904     if (hjpeg->JpegOutCount > 0UL)
3905     {
3906       /*Output Buffer is not empty, call DecodedDataReadyCallback*/
3907 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
3908       hjpeg->DataReadyCallback(hjpeg, hjpeg->pJpegOutBuffPtr, hjpeg->JpegOutCount);
3909 #else
3910       HAL_JPEG_DataReadyCallback(hjpeg, hjpeg->pJpegOutBuffPtr, hjpeg->JpegOutCount);
3911 #endif /* USE_HAL_JPEG_REGISTER_CALLBACKS */
3912 
3913       hjpeg->JpegOutCount = 0;
3914     }
3915 
3916     tmpContext = hjpeg->Context;
3917     /*Clear all context fields execpt JPEG_CONTEXT_CONF_ENCODING and JPEG_CONTEXT_CUSTOM_TABLES*/
3918     hjpeg->Context &= (JPEG_CONTEXT_CONF_ENCODING | JPEG_CONTEXT_CUSTOM_TABLES);
3919 
3920     /* Process Unlocked */
3921     __HAL_UNLOCK(hjpeg);
3922 
3923     /* Change the JPEG state */
3924     hjpeg->State = HAL_JPEG_STATE_READY;
3925 
3926     /*Call End of Encoding/Decoding callback */
3927     if ((tmpContext & JPEG_CONTEXT_OPERATION_MASK) == JPEG_CONTEXT_DECODE)
3928     {
3929 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
3930       hjpeg->DecodeCpltCallback(hjpeg);
3931 #else
3932       HAL_JPEG_DecodeCpltCallback(hjpeg);
3933 #endif /* USE_HAL_JPEG_REGISTER_CALLBACKS */
3934     }
3935     else /* JPEG_CONTEXT_ENCODE */
3936     {
3937 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
3938       hjpeg->EncodeCpltCallback(hjpeg);
3939 #else
3940       HAL_JPEG_EncodeCpltCallback(hjpeg);
3941 #endif /* USE_HAL_JPEG_REGISTER_CALLBACKS */
3942     }
3943   }
3944 }
3945 
3946 /**
3947   * @brief  DMA input transfer complete callback
3948   * @param  hdma pointer to a DMA_HandleTypeDef structure.
3949   * @retval None
3950   */
JPEG_DMAInCpltCallback(DMA_HandleTypeDef * hdma)3951 static void JPEG_DMAInCpltCallback(DMA_HandleTypeDef *hdma)
3952 {
3953   JPEG_HandleTypeDef *hjpeg = (JPEG_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
3954 
3955   /* Disable The JPEG IT so the DMA Input Callback can not be interrupted by the JPEG EOC IT or JPEG HPD IT */
3956   __HAL_JPEG_DISABLE_IT(hjpeg, JPEG_INTERRUPT_MASK);
3957 
3958   if ((hjpeg->Context & (JPEG_CONTEXT_METHOD_MASK | JPEG_CONTEXT_ENDING_DMA)) ==
3959       JPEG_CONTEXT_DMA) /* Check if context method is DMA and we are not in ending DMA stage */
3960   {
3961     JPEG_DISABLE_DMA(hjpeg, JPEG_DMA_IDMA);
3962 
3963     hjpeg->JpegInCount = hjpeg->InDataLength - ((hdma->Instance->NDTR & DMA_SxNDT) << 2);
3964 
3965     /*Call HAL_JPEG_GetDataCallback to get new data */
3966 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
3967     hjpeg->GetDataCallback(hjpeg, hjpeg->JpegInCount);
3968 #else
3969     HAL_JPEG_GetDataCallback(hjpeg, hjpeg->JpegInCount);
3970 #endif /* USE_HAL_JPEG_REGISTER_CALLBACKS */
3971 
3972     if (hjpeg->InDataLength >= 4UL)
3973     {
3974       /*JPEG Input DMA transfer data number must be multiple of 32 bits word
3975         as the destination is a 32 bits (4 bytes) register */
3976       hjpeg->InDataLength = hjpeg->InDataLength - (hjpeg->InDataLength % 4UL);
3977     }
3978     else if (hjpeg->InDataLength > 0UL)
3979     {
3980       /*Transfer last data word (i.e last 4 bytes)*/
3981       hjpeg->InDataLength = 4;
3982     }
3983 
3984     if (((hjpeg->Context &  JPEG_CONTEXT_PAUSE_INPUT) == 0UL) && (hjpeg->InDataLength > 0UL))
3985     {
3986       /* Start DMA FIFO In transfer */
3987       if (HAL_DMA_Start_IT(hjpeg->hdmain, (uint32_t)hjpeg->pJpegInBuffPtr, (uint32_t)&hjpeg->Instance->DIR,
3988                            hjpeg->InDataLength >> 2) != HAL_OK)
3989       {
3990         hjpeg->ErrorCode |= HAL_JPEG_ERROR_DMA;
3991         hjpeg->State = HAL_JPEG_STATE_ERROR;
3992 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
3993         hjpeg->ErrorCallback(hjpeg);
3994 #else
3995         HAL_JPEG_ErrorCallback(hjpeg);
3996 #endif /* USE_HAL_JPEG_REGISTER_CALLBACKS */
3997         return;
3998       }
3999       JPEG_ENABLE_DMA(hjpeg, JPEG_DMA_IDMA);
4000     }
4001 
4002     /* JPEG Conversion still on going : Enable the JPEG IT */
4003     __HAL_JPEG_ENABLE_IT(hjpeg, JPEG_IT_EOC | JPEG_IT_HPD);
4004   }
4005 }
4006 
4007 /**
4008   * @brief  DMA output transfer complete callback
4009   * @param  hdma pointer to a DMA_HandleTypeDef structure.
4010   * @retval None
4011   */
JPEG_DMAOutCpltCallback(DMA_HandleTypeDef * hdma)4012 static void JPEG_DMAOutCpltCallback(DMA_HandleTypeDef *hdma)
4013 {
4014   JPEG_HandleTypeDef *hjpeg = (JPEG_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
4015 
4016   /* Disable The JPEG IT so the DMA Output Callback can not be interrupted by the JPEG EOC IT or JPEG HPD IT */
4017   __HAL_JPEG_DISABLE_IT(hjpeg, JPEG_INTERRUPT_MASK);
4018 
4019   if ((hjpeg->Context & (JPEG_CONTEXT_METHOD_MASK | JPEG_CONTEXT_ENDING_DMA)) ==
4020       JPEG_CONTEXT_DMA) /* Check if context method is DMA and we are not in ending DMA stage */
4021   {
4022     if (__HAL_JPEG_GET_FLAG(hjpeg, JPEG_FLAG_EOCF) == 0UL)
4023     {
4024       JPEG_DISABLE_DMA(hjpeg, JPEG_DMA_ODMA);
4025       hjpeg->JpegOutCount = hjpeg->OutDataLength - ((hdma->Instance->NDTR & DMA_SxNDT) << 2);
4026 
4027       /*Output Buffer is full, call HAL_JPEG_DataReadyCallback*/
4028 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
4029       hjpeg->DataReadyCallback(hjpeg, hjpeg->pJpegOutBuffPtr, hjpeg->JpegOutCount);
4030 #else
4031       HAL_JPEG_DataReadyCallback(hjpeg, hjpeg->pJpegOutBuffPtr, hjpeg->JpegOutCount);
4032 #endif /* USE_HAL_JPEG_REGISTER_CALLBACKS */
4033 
4034       if ((hjpeg->Context &  JPEG_CONTEXT_PAUSE_OUTPUT) == 0UL)
4035       {
4036         /* Start DMA FIFO Out transfer */
4037         if (HAL_DMA_Start_IT(hjpeg->hdmaout, (uint32_t)&hjpeg->Instance->DOR, (uint32_t)hjpeg->pJpegOutBuffPtr,
4038                              hjpeg->OutDataLength >> 2) != HAL_OK)
4039         {
4040           hjpeg->ErrorCode |= HAL_JPEG_ERROR_DMA;
4041           hjpeg->State = HAL_JPEG_STATE_ERROR;
4042 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
4043           hjpeg->ErrorCallback(hjpeg);
4044 #else
4045           HAL_JPEG_ErrorCallback(hjpeg);
4046 #endif /* USE_HAL_JPEG_REGISTER_CALLBACKS */
4047           return;
4048         }
4049         JPEG_ENABLE_DMA(hjpeg, JPEG_DMA_ODMA);
4050       }
4051     }
4052 
4053     /* JPEG Conversion still on going : Enable the JPEG IT */
4054     __HAL_JPEG_ENABLE_IT(hjpeg, JPEG_IT_EOC | JPEG_IT_HPD);
4055   }
4056 }
4057 
4058 
4059 /**
4060   * @brief  DMA Transfer error callback
4061   * @param  hdma pointer to a DMA_HandleTypeDef structure.
4062   * @retval None
4063   */
JPEG_DMAErrorCallback(DMA_HandleTypeDef * hdma)4064 static void JPEG_DMAErrorCallback(DMA_HandleTypeDef *hdma)
4065 {
4066   JPEG_HandleTypeDef *hjpeg = (JPEG_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
4067 
4068   /* if DMA error is FIFO error ignore it */
4069   if (HAL_DMA_GetError(hdma) != HAL_DMA_ERROR_FE)
4070   {
4071     /*Stop Encoding/Decoding*/
4072     hjpeg->Instance->CONFR0 &=  ~JPEG_CONFR0_START;
4073 
4074     /* Disable All Interrupts */
4075     __HAL_JPEG_DISABLE_IT(hjpeg, JPEG_INTERRUPT_MASK);
4076 
4077     /* Disable All DMA requests */
4078     JPEG_DISABLE_DMA(hjpeg, JPEG_DMA_MASK);
4079 
4080     hjpeg->State = HAL_JPEG_STATE_READY;
4081     hjpeg->ErrorCode |= HAL_JPEG_ERROR_DMA;
4082 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
4083     hjpeg->ErrorCallback(hjpeg);
4084 #else
4085     HAL_JPEG_ErrorCallback(hjpeg);
4086 #endif /* USE_HAL_JPEG_REGISTER_CALLBACKS */
4087   }
4088 }
4089 
4090 /**
4091   * @brief  DMA output Abort callback
4092   * @param  hdma pointer to a DMA_HandleTypeDef structure.
4093   * @retval None
4094   */
JPEG_DMAOutAbortCallback(DMA_HandleTypeDef * hdma)4095 static void JPEG_DMAOutAbortCallback(DMA_HandleTypeDef *hdma)
4096 {
4097   JPEG_HandleTypeDef *hjpeg = (JPEG_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
4098 
4099   if ((hjpeg->Context & JPEG_CONTEXT_ENDING_DMA) != 0UL)
4100   {
4101     JPEG_DMA_EndProcess(hjpeg);
4102   }
4103 }
4104 
4105 
4106 /**
4107   * @brief  Calculate the decoded image quality (from 1 to 100)
4108   * @param  hjpeg pointer to a JPEG_HandleTypeDef structure that contains
4109   *         the configuration information for JPEG module
4110   * @retval JPEG image quality from 1 to 100.
4111   */
JPEG_GetQuality(JPEG_HandleTypeDef * hjpeg)4112 static uint32_t JPEG_GetQuality(JPEG_HandleTypeDef *hjpeg)
4113 {
4114   uint32_t quality = 0;
4115   uint32_t quantRow, quantVal, scale, i, j;
4116   __IO uint32_t *tableAddress = hjpeg->Instance->QMEM0;
4117 
4118   i = 0;
4119   while (i < (JPEG_QUANT_TABLE_SIZE - 3UL))
4120   {
4121     quantRow = *tableAddress;
4122     for (j = 0; j < 4UL; j++)
4123     {
4124       quantVal = (quantRow >> (8UL * j)) & 0xFFUL;
4125       if (quantVal == 1UL)
4126       {
4127         /* if Quantization value = 1 then quality is 100%*/
4128         quality += 100UL;
4129       }
4130       else
4131       {
4132         /* Note that the quantization coefficients must be specified in the table in zigzag order */
4133         scale = (quantVal * 100UL) / ((uint32_t) hjpeg->QuantTable0[JPEG_ZIGZAG_ORDER[i + j]]);
4134 
4135         if (scale <= 100UL)
4136         {
4137           quality += (200UL - scale) / 2UL;
4138         }
4139         else
4140         {
4141           quality += 5000UL / scale;
4142         }
4143       }
4144     }
4145 
4146     i += 4UL;
4147     tableAddress ++;
4148   }
4149 
4150   return (quality / 64UL);
4151 }
4152 /**
4153   * @}
4154   */
4155 
4156 /**
4157   * @}
4158   */
4159 #endif /* JPEG */
4160 #endif /* HAL_JPEG_MODULE_ENABLED */
4161 
4162 
4163 /**
4164   * @}
4165   */
4166 
4167