STM32H735xx HAL User Manual
stm32h7xx_hal_jpeg.c
Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    stm32h7xx_hal_jpeg.c
00004   * @author  MCD Application Team
00005   * @brief   JPEG HAL module driver.
00006   *          This file provides firmware functions to manage the following
00007   *          functionalities of the JPEG encoder/decoder peripheral:
00008   *           + Initialization and de-initialization functions
00009   *           + JPEG processing functions encoding and decoding
00010   *           + JPEG decoding Getting Info and encoding configuration setting
00011   *           + JPEG enable/disable header parsing functions (for decoding)
00012   *           + JPEG Input/Output Buffer configuration.
00013   *           + JPEG callback functions
00014   *           + JPEG Abort/Pause/Resume functions
00015   *           + JPEG custom quantization tables setting functions
00016   *           + IRQ handler management
00017   *           + Peripheral State and Error functions
00018   *
00019   ******************************************************************************
00020   * @attention
00021   *
00022   * Copyright (c) 2017 STMicroelectronics.
00023   * All rights reserved.
00024   *
00025   * This software is licensed under terms that can be found in the LICENSE file
00026   * in the root directory of this software component.
00027   * If no LICENSE file comes with this software, it is provided AS-IS.
00028   *
00029   ******************************************************************************
00030   @verbatim
00031   ==============================================================================
00032                      ##### How to use this driver #####
00033   ==============================================================================
00034     [..]
00035      (#) Initialize the JPEG peripheral using HAL_JPEG_Init : No initialization parameters are required.
00036          Only the call to HAL_JPEG_Init is necessary to initialize the JPEG peripheral.
00037 
00038      (#) If operation is JPEG encoding use function HAL_JPEG_ConfigEncoding to set
00039          the encoding parameters (mandatory before calling the encoding function).
00040          the application can change the encoding parameter ImageQuality from
00041          1 to 100 to obtain a more or less quality (visual quality vs the original row image),
00042          and inversely more or less jpg file size.
00043 
00044      (#) Note that for decoding operation the JPEG peripheral output data are organized in
00045          YCbCr blocks called MCU (Minimum Coded Unit) as defioned in the JPEG specification
00046          ISO/IEC 10918-1 standard.
00047          It is up to the application to transform these YCbCr blocks to RGB data that can be display.
00048 
00049          Respectively, for Encoding operation the JPEG peripheral input should be organized
00050          in YCbCr MCU blocks. It is up to the application to perform the necessary RGB to YCbCr
00051          MCU blocks transformation before feeding the JPEG peripheral with data.
00052 
00053      (#) Use functions HAL_JPEG_Encode and HAL_JPEG_Decode to start respectively
00054          a JPEG encoding/decoding operation in polling method (blocking).
00055 
00056      (#) Use functions HAL_JPEG_Encode_IT and HAL_JPEG_Decode_IT to start respectively
00057          a JPEG encoding/decoding operation with Interrupt method (not blocking).
00058 
00059      (#) Use functions HAL_JPEG_Encode_DMA and HAL_JPEG_Decode_DMA to start respectively
00060          a JPEG encoding/decoding operation with DMA method (not blocking).
00061 
00062      (#) Callback HAL_JPEG_InfoReadyCallback is asserted if the current operation
00063          is a JPEG decoding to provide the application with JPEG image  parameters.
00064          This callback is asserted when the JPEG peripheral successfully parse the
00065          JPEG header.
00066 
00067      (#) Callback HAL_JPEG_GetDataCallback is asserted for both encoding and decoding
00068          operations to inform the application that the input buffer has been
00069          consumed by the peripheral and to ask for a new data chunk if the operation
00070          (encoding/decoding) has not been complete yet.
00071 
00072         (++) This CallBack should be implemented in the application side. It should
00073              call the function HAL_JPEG_ConfigInputBuffer if new input data are available,
00074              or call HAL_JPEG_Pause with parameter XferSelection set to JPEG_PAUSE_RESUME_INPUT
00075              to inform the JPEG HAL driver that the ongoing operation shall pause waiting for the
00076              application to provide a new input data chunk.
00077              Once the application succeed getting new data and if the input has been paused,
00078              the application can call the function HAL_JPEG_ConfigInputBuffer to set the new
00079              input buffer and size, then resume the JPEG HAL input by calling new function HAL_JPEG_Resume.
00080              If the application has ended feeding the HAL JPEG with input data (no more input data), the application
00081              Should call the function HAL_JPEG_ConfigInputBuffer (within the callback HAL_JPEG_GetDataCallback)
00082              with the parameter InDataLength set to zero.
00083 
00084          (++) The mechanism of HAL_JPEG_ConfigInputBuffer/HAL_JPEG_Pause/HAL_JPEG_Resume allows
00085               to the application to provide the input data (for encoding or decoding) by chunks.
00086               If the new input data chunk is not available (because data should be read from an input file
00087               for example) the application can pause the JPEG input (using function HAL_JPEG_Pause)
00088               Once the new input data chunk is available ( read from a file for example), the application
00089               can call the function HAL_JPEG_ConfigInputBuffer to provide the HAL with the new chunk
00090               then resume the JPEG HAL input by calling function HAL_JPEG_Resume.
00091 
00092          (++) The application can call functions HAL_JPEG_ConfigInputBuffer then HAL_JPEG_Resume.
00093               any time (outside the HAL_JPEG_GetDataCallback)  Once the new input chunk data available.
00094               However, to keep data coherency, the function HAL_JPEG_Pause must be imperatively called
00095               (if necessary) within the callback HAL_JPEG_GetDataCallback, i.e when the HAL JPEG has ended
00096               Transferring the previous chunk buffer to the JPEG peripheral.
00097 
00098      (#) Callback HAL_JPEG_DataReadyCallback is asserted when the HAL JPEG driver
00099          has filled the given output buffer with the given size.
00100 
00101          (++) This CallBack should be implemented in the application side. It should
00102               call the function HAL_JPEG_ConfigOutputBuffer to provide the HAL JPEG driver
00103               with the new output buffer location and size to be used  to store next data chunk.
00104               if the application is not ready to provide the output chunk location then it can
00105               call the function HAL_JPEG_Pause with parameter XferSelection set to JPEG_PAUSE_RESUME_OUTPUT
00106               to inform the JPEG HAL driver that it shall pause output data. Once the application
00107               is ready to receive the new data chunk (output buffer location free or available) it should call
00108               the function HAL_JPEG_ConfigOutputBuffer to provide the HAL JPEG driver
00109               with the new output chunk buffer location and size, then call HAL_JPEG_Resume
00110               to inform the HAL that it shall resume outputting data in the given output buffer.
00111 
00112          (++) The mechanism of HAL_JPEG_ConfigOutputBuffer/HAL_JPEG_Pause/HAL_JPEG_Resume allows
00113               the application to receive data from the JPEG peripheral by chunks. when a chunk
00114               is received, the application can pause the HAL JPEG output data to be able to process
00115               these received data (YCbCr to RGB conversion in case of decoding or data storage in case
00116               of encoding).
00117 
00118          (++) The application can call  functions HAL_JPEG_ ConfigOutputBuffer then HAL_JPEG_Resume.
00119               any time (outside the HAL_JPEG_DataReadyCallback) Once the output data buffer is free to use.
00120               However, to keep data coherency, the function HAL_JPEG_Pause must be imperatively called
00121               (if necessary) within the callback HAL_JPEG_ DataReadyCallback, i.e when the HAL JPEG has ended
00122               Transferring the previous chunk buffer from the JPEG peripheral to the application.
00123 
00124      (#) Callback HAL_JPEG_EncodeCpltCallback is asserted when the HAL JPEG driver has
00125          ended the current JPEG encoding operation, and all output data has been transmitted
00126          to the application.
00127 
00128      (#) Callback HAL_JPEG_DecodeCpltCallback is asserted when the HAL JPEG driver has
00129          ended the current JPEG decoding operation. and all output data has been transmitted
00130          to the application.
00131 
00132      (#) Callback HAL_JPEG_ErrorCallback is asserted when an error occurred during
00133          the current operation. the application can call the function HAL_JPEG_GetError()
00134          to retrieve the error codes.
00135 
00136      (#) By default the HAL JPEG driver uses the default quantization tables
00137          as provide in the JPEG specification (ISO/IEC 10918-1 standard) for encoding.
00138          User can change these default tables if necessary using the function HAL_JPEG_SetUserQuantTables
00139          Note that for decoding the quantization tables are automatically extracted from
00140          the JPEG header.
00141 
00142       (#) To control JPEG state you can use the following function: HAL_JPEG_GetState()
00143 
00144      *** JPEG HAL driver macros list ***
00145      =============================================
00146      [..]
00147        Below the list of most used macros in JPEG HAL driver.
00148 
00149       (+) __HAL_JPEG_RESET_HANDLE_STATE : Reset JPEG handle state.
00150       (+) __HAL_JPEG_ENABLE             : Enable the JPEG peripheral.
00151       (+) __HAL_JPEG_DISABLE            : Disable the JPEG peripheral.
00152       (+) __HAL_JPEG_GET_FLAG           : Check the specified JPEG status flag.
00153       (+) __HAL_JPEG_CLEAR_FLAG         : Clear the specified JPEG status flag.
00154       (+) __HAL_JPEG_ENABLE_IT          : Enable the specified JPEG Interrupt.
00155       (+) __HAL_JPEG_DISABLE_IT         : Disable the specified JPEG Interrupt.
00156       (+) __HAL_JPEG_GET_IT_SOURCE      : returns the state of the specified JPEG Interrupt (Enabled or disabled).
00157 
00158   *** Callback registration ***
00159   =============================================
00160 
00161   The compilation define  USE_HAL_JPEG_REGISTER_CALLBACKS when set to 1
00162   allows the user to configure dynamically the driver callbacks.
00163   Use Functions HAL_JPEG_RegisterCallback() or HAL_JPEG_RegisterXXXCallback()
00164   to register an interrupt callback.
00165 
00166   Function HAL_JPEG_RegisterCallback() allows to register following callbacks:
00167     (+) EncodeCpltCallback : callback for end of encoding operation.
00168     (+) DecodeCpltCallback : callback for end of decoding operation.
00169     (+) ErrorCallback      : callback for error detection.
00170     (+) MspInitCallback    : JPEG MspInit.
00171     (+) MspDeInitCallback  : JPEG MspDeInit.
00172   This function takes as parameters the HAL peripheral handle, the Callback ID
00173   and a pointer to the user callback function.
00174 
00175   For specific callbacks InfoReadyCallback, GetDataCallback and DataReadyCallback use dedicated
00176   register callbacks : respectively HAL_JPEG_RegisterInfoReadyCallback(),
00177   HAL_JPEG_RegisterGetDataCallback() and HAL_JPEG_RegisterDataReadyCallback().
00178 
00179   Use function HAL_JPEG_UnRegisterCallback() to reset a callback to the default
00180   weak function.
00181   HAL_JPEG_UnRegisterCallback() takes as parameters the HAL peripheral handle,
00182   and the Callback ID.
00183   This function allows to reset following callbacks:
00184     (+) EncodeCpltCallback : callback for end of encoding operation.
00185     (+) DecodeCpltCallback : callback for end of decoding operation.
00186     (+) ErrorCallback      : callback for error detection.
00187     (+) MspInitCallback    : JPEG MspInit.
00188     (+) MspDeInitCallback  : JPEG MspDeInit.
00189 
00190   For callbacks InfoReadyCallback, GetDataCallback and DataReadyCallback use dedicated
00191   unregister callbacks : respectively HAL_JPEG_UnRegisterInfoReadyCallback(),
00192   HAL_JPEG_UnRegisterGetDataCallback() and HAL_JPEG_UnRegisterDataReadyCallback().
00193 
00194   By default, after the HAL_JPEG_Init() and when the state is HAL_JPEG_STATE_RESET
00195   all callbacks are set to the corresponding weak functions :
00196   examples HAL_JPEG_DecodeCpltCallback() , HAL_JPEG_GetDataCallback().
00197   Exception done for MspInit and MspDeInit functions that are
00198   reset to the legacy weak function in the HAL_JPEG_Init()/ HAL_JPEG_DeInit() only when
00199   these callbacks are null (not registered beforehand).
00200   if not, MspInit or MspDeInit are not null, the HAL_JPEG_Init() / HAL_JPEG_DeInit()
00201   keep and use the user MspInit/MspDeInit functions (registered beforehand)
00202 
00203   Callbacks can be registered/unregistered in HAL_JPEG_STATE_READY state only.
00204   Exception done MspInit/MspDeInit callbacks that can be registered/unregistered
00205   in HAL_JPEG_STATE_READY or HAL_JPEG_STATE_RESET state,
00206   thus registered (user) MspInit/DeInit callbacks can be used during the Init/DeInit.
00207   In that case first register the MspInit/MspDeInit user callbacks
00208   using HAL_JPEG_RegisterCallback() before calling HAL_JPEG_DeInit()
00209   or HAL_JPEG_Init() function.
00210 
00211   When The compilation define USE_HAL_JPEG_REGISTER_CALLBACKS is set to 0 or
00212   not defined, the callback registration feature is not available and all callbacks
00213   are set to the corresponding weak functions.
00214 
00215   @endverbatim
00216   ******************************************************************************
00217   */
00218 
00219 /* Includes ------------------------------------------------------------------*/
00220 #include "stm32h7xx_hal.h"
00221 
00222 /** @addtogroup STM32H7xx_HAL_Driver
00223   * @{
00224   */
00225 
00226 #ifdef HAL_JPEG_MODULE_ENABLED
00227 
00228 #if defined (JPEG)
00229 
00230 /** @defgroup JPEG JPEG
00231   * @brief JPEG HAL module driver.
00232   * @{
00233   */
00234 
00235 /* Private define ------------------------------------------------------------*/
00236 /** @addtogroup JPEG_Private_Constants
00237   * @{
00238   */
00239 #define JPEG_TIMEOUT_VALUE  ((uint32_t)1000)     /* 1s */
00240 #define JPEG_AC_HUFF_TABLE_SIZE  ((uint32_t)162) /* Huffman AC table size : 162 codes*/
00241 #define JPEG_DC_HUFF_TABLE_SIZE  ((uint32_t)12)  /* Huffman AC table size : 12 codes*/
00242 
00243 #define JPEG_FIFO_SIZE ((uint32_t)16U)             /* JPEG Input/Output HW FIFO size in words*/
00244 
00245 #define JPEG_FIFO_TH_SIZE ((uint32_t)8U)              /* JPEG Input/Output HW FIFO Threshold in words*/
00246 
00247 #define JPEG_INTERRUPT_MASK  ((uint32_t)0x0000007EU) /* JPEG Interrupt Mask*/
00248 
00249 #define JPEG_CONTEXT_ENCODE          ((uint32_t)0x00000001) /* JPEG context : operation is encoding*/
00250 #define JPEG_CONTEXT_DECODE          ((uint32_t)0x00000002) /* JPEG context : operation is decoding*/
00251 #define JPEG_CONTEXT_OPERATION_MASK  ((uint32_t)0x00000003) /* JPEG context : operation Mask */
00252 
00253 #define JPEG_CONTEXT_POLLING        ((uint32_t)0x00000004)  /* JPEG context : Transfer use Polling */
00254 #define JPEG_CONTEXT_IT             ((uint32_t)0x00000008)  /* JPEG context : Transfer use Interrupt */
00255 #define JPEG_CONTEXT_DMA            ((uint32_t)0x0000000C)  /* JPEG context : Transfer use DMA */
00256 #define JPEG_CONTEXT_METHOD_MASK    ((uint32_t)0x0000000C)  /* JPEG context : Transfer Mask */
00257 
00258 
00259 #define JPEG_CONTEXT_CONF_ENCODING  ((uint32_t)0x00000100)  /* JPEG context : encoding config done */
00260 
00261 #define JPEG_CONTEXT_PAUSE_INPUT    ((uint32_t)0x00001000)  /* JPEG context : Pause Input */
00262 #define JPEG_CONTEXT_PAUSE_OUTPUT   ((uint32_t)0x00002000)  /* JPEG context : Pause Output */
00263 
00264 #define JPEG_CONTEXT_CUSTOM_TABLES  ((uint32_t)0x00004000)  /* JPEG context : Use custom quantization tables */
00265 
00266 #define JPEG_CONTEXT_ENDING_DMA     ((uint32_t)0x00008000)  /* JPEG context : ending with DMA in progress */
00267 
00268 #define JPEG_PROCESS_ONGOING        ((uint32_t)0x00000000)  /* Process is on going */
00269 #define JPEG_PROCESS_DONE           ((uint32_t)0x00000001)  /* Process is done (ends) */
00270 /**
00271   * @}
00272   */
00273 
00274 /* Private typedef -----------------------------------------------------------*/
00275 /** @addtogroup JPEG_Private_Types
00276   * @{
00277   */
00278 
00279 /*
00280  JPEG Huffman Table Structure definition :
00281  This implementation of Huffman table structure is compliant with ISO/IEC 10918-1 standard , Annex C Huffman Table specification
00282  */
00283 typedef struct
00284 {
00285   /* These two fields directly represent the contents of a JPEG DHT marker */
00286   uint8_t Bits[16];        /*!< bits[k] = # of symbols with codes of length k bits, this parameter corresponds to BITS list in the Annex C */
00287 
00288   uint8_t HuffVal[162];    /*!< The symbols, in order of incremented code length, this parameter corresponds to HUFFVAL list in the Annex C */
00289 
00290 
00291 } JPEG_ACHuffTableTypeDef;
00292 
00293 typedef struct
00294 {
00295   /* These two fields directly represent the contents of a JPEG DHT marker */
00296   uint8_t Bits[16];        /*!< bits[k] = # of symbols with codes of length k bits, this parameter corresponds to BITS list in the Annex C */
00297 
00298   uint8_t HuffVal[12];    /*!< The symbols, in order of incremented code length, this parameter corresponds to HUFFVAL list in the Annex C */
00299 
00300 
00301 } JPEG_DCHuffTableTypeDef;
00302 
00303 typedef struct
00304 {
00305   uint8_t CodeLength[JPEG_AC_HUFF_TABLE_SIZE];      /*!< Code length  */
00306 
00307   uint32_t HuffmanCode[JPEG_AC_HUFF_TABLE_SIZE];    /*!< HuffmanCode */
00308 
00309 } JPEG_AC_HuffCodeTableTypeDef;
00310 
00311 typedef struct
00312 {
00313   uint8_t CodeLength[JPEG_DC_HUFF_TABLE_SIZE];        /*!< Code length  */
00314 
00315   uint32_t HuffmanCode[JPEG_DC_HUFF_TABLE_SIZE];    /*!< HuffmanCode */
00316 
00317 } JPEG_DC_HuffCodeTableTypeDef;
00318 /**
00319   * @}
00320   */
00321 
00322 /* Private macro -------------------------------------------------------------*/
00323 
00324 /* Private variables ---------------------------------------------------------*/
00325 /** @addtogroup JPEG_Private_Variables
00326   * @{
00327   */
00328 
00329 static const JPEG_DCHuffTableTypeDef JPEG_DCLUM_HuffTable =
00330 {
00331   { 0, 1, 5, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0 },   /*Bits*/
00332 
00333   { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0xa, 0xb }           /*HUFFVAL */
00334 
00335 };
00336 
00337 static const JPEG_DCHuffTableTypeDef JPEG_DCCHROM_HuffTable =
00338 {
00339   { 0, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0 },  /*Bits*/
00340 
00341   { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0xa, 0xb }          /*HUFFVAL */
00342 };
00343 
00344 static const JPEG_ACHuffTableTypeDef JPEG_ACLUM_HuffTable =
00345 {
00346   { 0, 2, 1, 3, 3, 2, 4, 3, 5, 5, 4, 4, 0, 0, 1, 0x7d },  /*Bits*/
00347 
00348   {
00349     0x01, 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12,     /*HUFFVAL */
00350     0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61, 0x07,
00351     0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xa1, 0x08,
00352     0x23, 0x42, 0xb1, 0xc1, 0x15, 0x52, 0xd1, 0xf0,
00353     0x24, 0x33, 0x62, 0x72, 0x82, 0x09, 0x0a, 0x16,
00354     0x17, 0x18, 0x19, 0x1a, 0x25, 0x26, 0x27, 0x28,
00355     0x29, 0x2a, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
00356     0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49,
00357     0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59,
00358     0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69,
00359     0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79,
00360     0x7a, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89,
00361     0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
00362     0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
00363     0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6,
00364     0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3, 0xc4, 0xc5,
00365     0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 0xd3, 0xd4,
00366     0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe1, 0xe2,
00367     0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea,
00368     0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
00369     0xf9, 0xfa
00370   }
00371 };
00372 
00373 static const JPEG_ACHuffTableTypeDef JPEG_ACCHROM_HuffTable =
00374 {
00375   { 0, 2, 1, 2, 4, 4, 3, 4, 7, 5, 4, 4, 0, 1, 2, 0x77 },   /*Bits*/
00376 
00377   {
00378     0x00, 0x01, 0x02, 0x03, 0x11, 0x04, 0x05, 0x21,      /*HUFFVAL */
00379     0x31, 0x06, 0x12, 0x41, 0x51, 0x07, 0x61, 0x71,
00380     0x13, 0x22, 0x32, 0x81, 0x08, 0x14, 0x42, 0x91,
00381     0xa1, 0xb1, 0xc1, 0x09, 0x23, 0x33, 0x52, 0xf0,
00382     0x15, 0x62, 0x72, 0xd1, 0x0a, 0x16, 0x24, 0x34,
00383     0xe1, 0x25, 0xf1, 0x17, 0x18, 0x19, 0x1a, 0x26,
00384     0x27, 0x28, 0x29, 0x2a, 0x35, 0x36, 0x37, 0x38,
00385     0x39, 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48,
00386     0x49, 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58,
00387     0x59, 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68,
00388     0x69, 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78,
00389     0x79, 0x7a, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
00390     0x88, 0x89, 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96,
00391     0x97, 0x98, 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5,
00392     0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4,
00393     0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3,
00394     0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2,
00395     0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda,
00396     0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9,
00397     0xea, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
00398     0xf9, 0xfa
00399   }
00400 };
00401 
00402 static const uint8_t JPEG_ZIGZAG_ORDER[JPEG_QUANT_TABLE_SIZE] =
00403 {
00404   0,   1,   8,  16,   9,   2,   3,  10,
00405   17,  24,  32,  25,  18,  11,   4,   5,
00406   12,  19,  26,  33,  40,  48,  41,  34,
00407   27,  20,  13,   6,   7,  14,  21,  28,
00408   35,  42,  49,  56,  57,  50,  43,  36,
00409   29,  22,  15,  23,  30,  37,  44,  51,
00410   58,  59,  52,  45,  38,  31,  39,  46,
00411   53,  60,  61,  54,  47,  55,  62,  63
00412 };
00413 /**
00414   * @}
00415   */
00416 
00417 /* Private function prototypes -----------------------------------------------*/
00418 /** @addtogroup JPEG_Private_Functions_Prototypes
00419   * @{
00420   */
00421 
00422 static HAL_StatusTypeDef JPEG_Bits_To_SizeCodes(uint8_t *Bits, uint8_t *Huffsize, uint32_t *Huffcode, uint32_t *LastK);
00423 static HAL_StatusTypeDef JPEG_DCHuff_BitsVals_To_SizeCodes(JPEG_DCHuffTableTypeDef *DC_BitsValsTable,
00424                                                            JPEG_DC_HuffCodeTableTypeDef *DC_SizeCodesTable);
00425 static HAL_StatusTypeDef JPEG_ACHuff_BitsVals_To_SizeCodes(JPEG_ACHuffTableTypeDef *AC_BitsValsTable,
00426                                                            JPEG_AC_HuffCodeTableTypeDef *AC_SizeCodesTable);
00427 static HAL_StatusTypeDef JPEG_Set_HuffDC_Mem(JPEG_HandleTypeDef *hjpeg, JPEG_DCHuffTableTypeDef *HuffTableDC,
00428                                              const __IO uint32_t *DCTableAddress);
00429 static HAL_StatusTypeDef JPEG_Set_HuffAC_Mem(JPEG_HandleTypeDef *hjpeg, JPEG_ACHuffTableTypeDef *HuffTableAC,
00430                                              const __IO uint32_t *ACTableAddress);
00431 static HAL_StatusTypeDef JPEG_Set_HuffEnc_Mem(JPEG_HandleTypeDef *hjpeg);
00432 static void JPEG_Set_Huff_DHTMem(JPEG_HandleTypeDef *hjpeg);
00433 static uint32_t  JPEG_Set_Quantization_Mem(JPEG_HandleTypeDef *hjpeg, uint8_t *QTable,
00434                                                     __IO uint32_t *QTableAddress);
00435 static void JPEG_SetColorYCBCR(JPEG_HandleTypeDef *hjpeg);
00436 static void JPEG_SetColorGrayScale(JPEG_HandleTypeDef *hjpeg);
00437 static void JPEG_SetColorCMYK(JPEG_HandleTypeDef *hjpeg);
00438 
00439 static void JPEG_Init_Process(JPEG_HandleTypeDef *hjpeg);
00440 static uint32_t JPEG_Process(JPEG_HandleTypeDef *hjpeg);
00441 static void JPEG_ReadInputData(JPEG_HandleTypeDef *hjpeg, uint32_t nbRequestWords);
00442 static void JPEG_StoreOutputData(JPEG_HandleTypeDef *hjpeg, uint32_t nbOutputWords);
00443 static uint32_t JPEG_GetQuality(JPEG_HandleTypeDef *hjpeg);
00444 
00445 static HAL_StatusTypeDef JPEG_DMA_StartProcess(JPEG_HandleTypeDef *hjpeg);
00446 static void JPEG_DMA_ContinueProcess(JPEG_HandleTypeDef *hjpeg);
00447 static void JPEG_DMA_EndProcess(JPEG_HandleTypeDef *hjpeg);
00448 static void JPEG_DMA_PollResidualData(JPEG_HandleTypeDef *hjpeg);
00449 static void JPEG_MDMAOutCpltCallback(MDMA_HandleTypeDef *hmdma);
00450 static void JPEG_MDMAInCpltCallback(MDMA_HandleTypeDef *hmdma);
00451 static void JPEG_MDMAErrorCallback(MDMA_HandleTypeDef *hmdma);
00452 static void JPEG_MDMAOutAbortCallback(MDMA_HandleTypeDef *hmdma);
00453 
00454 /**
00455   * @}
00456   */
00457 
00458 /** @defgroup JPEG_Exported_Functions JPEG Exported Functions
00459   * @{
00460   */
00461 
00462 /** @defgroup JPEG_Exported_Functions_Group1 Initialization and de-initialization functions
00463   *  @brief    Initialization and de-initialization functions.
00464   *
00465 @verbatim
00466   ==============================================================================
00467               ##### Initialization and de-initialization functions #####
00468   ==============================================================================
00469     [..]  This section provides functions allowing to:
00470       (+) Initialize the JPEG peripheral and creates the associated handle
00471       (+) DeInitialize the JPEG peripheral
00472 
00473 @endverbatim
00474   * @{
00475   */
00476 
00477 /**
00478   * @brief  Initializes the JPEG according to the specified
00479   *         parameters in the JPEG_InitTypeDef and creates the associated handle.
00480   * @param  hjpeg pointer to a JPEG_HandleTypeDef structure that contains
00481   *         the configuration information for JPEG module
00482   * @retval HAL status
00483   */
00484 HAL_StatusTypeDef HAL_JPEG_Init(JPEG_HandleTypeDef *hjpeg)
00485 {
00486   /* These are the sample quantization tables given in JPEG spec ISO/IEC 10918-1 standard , section K.1. */
00487   static const uint8_t JPEG_LUM_QuantTable[JPEG_QUANT_TABLE_SIZE] =
00488   {
00489     16,  11,  10,  16,  24,  40,  51,  61,
00490     12,  12,  14,  19,  26,  58,  60,  55,
00491     14,  13,  16,  24,  40,  57,  69,  56,
00492     14,  17,  22,  29,  51,  87,  80,  62,
00493     18,  22,  37,  56,  68, 109, 103,  77,
00494     24,  35,  55,  64,  81, 104, 113,  92,
00495     49,  64,  78,  87, 103, 121, 120, 101,
00496     72,  92,  95,  98, 112, 100, 103,  99
00497   };
00498   static const uint8_t JPEG_CHROM_QuantTable[JPEG_QUANT_TABLE_SIZE] =
00499   {
00500     17,  18,  24,  47,  99,  99,  99,  99,
00501     18,  21,  26,  66,  99,  99,  99,  99,
00502     24,  26,  56,  99,  99,  99,  99,  99,
00503     47,  66,  99,  99,  99,  99,  99,  99,
00504     99,  99,  99,  99,  99,  99,  99,  99,
00505     99,  99,  99,  99,  99,  99,  99,  99,
00506     99,  99,  99,  99,  99,  99,  99,  99,
00507     99,  99,  99,  99,  99,  99,  99,  99
00508   };
00509 
00510   /* Check the JPEG handle allocation */
00511   if (hjpeg == NULL)
00512   {
00513     return HAL_ERROR;
00514   }
00515 
00516 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
00517   if (hjpeg->State == HAL_JPEG_STATE_RESET)
00518   {
00519     /* Allocate lock resource and initialize it */
00520     hjpeg->Lock = HAL_UNLOCKED;
00521 
00522     hjpeg->InfoReadyCallback  = HAL_JPEG_InfoReadyCallback;  /* Legacy weak InfoReadyCallback  */
00523     hjpeg->EncodeCpltCallback = HAL_JPEG_EncodeCpltCallback; /* Legacy weak EncodeCpltCallback */
00524     hjpeg->DecodeCpltCallback = HAL_JPEG_DecodeCpltCallback; /* Legacy weak DecodeCpltCallback */
00525     hjpeg->ErrorCallback      = HAL_JPEG_ErrorCallback;      /* Legacy weak ErrorCallback      */
00526     hjpeg->GetDataCallback    = HAL_JPEG_GetDataCallback;    /* Legacy weak GetDataCallback    */
00527     hjpeg->DataReadyCallback  = HAL_JPEG_DataReadyCallback;  /* Legacy weak DataReadyCallback  */
00528 
00529     if (hjpeg->MspInitCallback == NULL)
00530     {
00531       hjpeg->MspInitCallback = HAL_JPEG_MspInit; /* Legacy weak MspInit  */
00532     }
00533 
00534     /* Init the low level hardware */
00535     hjpeg->MspInitCallback(hjpeg);
00536   }
00537 #else
00538   if (hjpeg->State == HAL_JPEG_STATE_RESET)
00539   {
00540     /* Allocate lock resource and initialize it */
00541     hjpeg->Lock = HAL_UNLOCKED;
00542 
00543     /* Init the low level hardware : GPIO, CLOCK */
00544     HAL_JPEG_MspInit(hjpeg);
00545   }
00546 #endif /* USE_HAL_JPEG_REGISTER_CALLBACKS */
00547 
00548   /* Change the JPEG state */
00549   hjpeg->State = HAL_JPEG_STATE_BUSY;
00550 
00551   /* Start the JPEG Core*/
00552   __HAL_JPEG_ENABLE(hjpeg);
00553 
00554   /* Stop the JPEG encoding/decoding process*/
00555   hjpeg->Instance->CONFR0 &=  ~JPEG_CONFR0_START;
00556 
00557   /* Disable All Interrupts */
00558   __HAL_JPEG_DISABLE_IT(hjpeg, JPEG_INTERRUPT_MASK);
00559 
00560 
00561   /* Flush input and output FIFOs*/
00562   hjpeg->Instance->CR |= JPEG_CR_IFF;
00563   hjpeg->Instance->CR |= JPEG_CR_OFF;
00564 
00565   /* Clear all flags */
00566   __HAL_JPEG_CLEAR_FLAG(hjpeg, JPEG_FLAG_ALL);
00567 
00568   /* init default quantization tables*/
00569   hjpeg->QuantTable0 = (uint8_t *)((uint32_t)JPEG_LUM_QuantTable);
00570   hjpeg->QuantTable1 = (uint8_t *)((uint32_t)JPEG_CHROM_QuantTable);
00571   hjpeg->QuantTable2 = NULL;
00572   hjpeg->QuantTable3 = NULL;
00573 
00574   /* init the default Huffman tables*/
00575   if (JPEG_Set_HuffEnc_Mem(hjpeg) != HAL_OK)
00576   {
00577     hjpeg->ErrorCode = HAL_JPEG_ERROR_HUFF_TABLE;
00578 
00579     return HAL_ERROR;
00580   }
00581 
00582   /* Enable header processing*/
00583   hjpeg->Instance->CONFR1 |= JPEG_CONFR1_HDR;
00584 
00585   /* Reset JpegInCount and JpegOutCount */
00586   hjpeg->JpegInCount = 0;
00587   hjpeg->JpegOutCount = 0;
00588 
00589   /* Change the JPEG state */
00590   hjpeg->State = HAL_JPEG_STATE_READY;
00591 
00592   /* Reset the JPEG ErrorCode */
00593   hjpeg->ErrorCode = HAL_JPEG_ERROR_NONE;
00594 
00595   /*Clear the context filelds*/
00596   hjpeg->Context = 0;
00597 
00598   /* Return function status */
00599   return HAL_OK;
00600 }
00601 
00602 /**
00603   * @brief  DeInitializes the JPEG peripheral.
00604   * @param  hjpeg pointer to a JPEG_HandleTypeDef structure that contains
00605   *         the configuration information for JPEG module
00606   * @retval HAL status
00607   */
00608 HAL_StatusTypeDef HAL_JPEG_DeInit(JPEG_HandleTypeDef *hjpeg)
00609 {
00610   /* Check the JPEG handle allocation */
00611   if (hjpeg == NULL)
00612   {
00613     return HAL_ERROR;
00614   }
00615 
00616 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
00617   if (hjpeg->MspDeInitCallback == NULL)
00618   {
00619     hjpeg->MspDeInitCallback = HAL_JPEG_MspDeInit; /* Legacy weak MspDeInit  */
00620   }
00621 
00622   /* DeInit the low level hardware */
00623   hjpeg->MspDeInitCallback(hjpeg);
00624 
00625 #else
00626   /* DeInit the low level hardware: CLOCK, NVIC.*/
00627   HAL_JPEG_MspDeInit(hjpeg);
00628 #endif /* USE_HAL_JPEG_REGISTER_CALLBACKS */
00629 
00630   /* Change the JPEG state */
00631   hjpeg->State = HAL_JPEG_STATE_BUSY;
00632 
00633   /* Reset the JPEG ErrorCode */
00634   hjpeg->ErrorCode = HAL_JPEG_ERROR_NONE;
00635 
00636   /* Reset JpegInCount and JpegOutCount */
00637   hjpeg->JpegInCount = 0;
00638   hjpeg->JpegOutCount = 0;
00639 
00640   /* Change the JPEG state */
00641   hjpeg->State = HAL_JPEG_STATE_RESET;
00642 
00643   /*Clear the context fields*/
00644   hjpeg->Context = 0;
00645 
00646   /* Release Lock */
00647   __HAL_UNLOCK(hjpeg);
00648 
00649   /* Return function status */
00650   return HAL_OK;
00651 }
00652 
00653 /**
00654   * @brief  Initializes the JPEG MSP.
00655   * @param  hjpeg pointer to a JPEG_HandleTypeDef structure that contains
00656   *         the configuration information for JPEG module
00657   * @retval None
00658   */
00659 __weak void HAL_JPEG_MspInit(JPEG_HandleTypeDef *hjpeg)
00660 {
00661   /* Prevent unused argument(s) compilation warning */
00662   UNUSED(hjpeg);
00663 
00664   /* NOTE : This function Should not be modified, when the callback is needed,
00665             the HAL_JPEG_MspInit could be implemented in the user file
00666    */
00667 }
00668 
00669 /**
00670   * @brief  DeInitializes JPEG MSP.
00671   * @param  hjpeg pointer to a JPEG_HandleTypeDef structure that contains
00672   *         the configuration information for JPEG module
00673   * @retval None
00674   */
00675 __weak void HAL_JPEG_MspDeInit(JPEG_HandleTypeDef *hjpeg)
00676 {
00677   /* Prevent unused argument(s) compilation warning */
00678   UNUSED(hjpeg);
00679 
00680   /* NOTE : This function Should not be modified, when the callback is needed,
00681             the HAL_JPEG_MspDeInit could be implemented in the user file
00682    */
00683 }
00684 
00685 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
00686 /**
00687   * @brief  Register a User JPEG Callback
00688   *         To be used instead of the weak predefined callback
00689   * @param  hjpeg JPEG handle
00690   * @param  CallbackID ID of the callback to be registered
00691   *         This parameter can be one of the following values:
00692   *          @arg @ref HAL_JPEG_ENCODE_CPLT_CB_ID Encode Complete callback ID
00693   *          @arg @ref HAL_JPEG_DECODE_CPLT_CB_ID Decode Complete callback ID
00694   *          @arg @ref HAL_JPEG_ERROR_CB_ID Error callback ID
00695   *          @arg @ref HAL_JPEG_MSPINIT_CB_ID MspInit callback ID
00696   *          @arg @ref HAL_JPEG_MSPDEINIT_CB_ID MspDeInit callback ID
00697   * @param  pCallback pointer to the Callback function
00698   * @retval HAL status
00699   */
00700 HAL_StatusTypeDef HAL_JPEG_RegisterCallback(JPEG_HandleTypeDef *hjpeg, HAL_JPEG_CallbackIDTypeDef CallbackID,
00701                                             pJPEG_CallbackTypeDef pCallback)
00702 {
00703   HAL_StatusTypeDef status = HAL_OK;
00704 
00705   if (pCallback == NULL)
00706   {
00707     /* Update the error code */
00708     hjpeg->ErrorCode |= HAL_JPEG_ERROR_INVALID_CALLBACK;
00709     return HAL_ERROR;
00710   }
00711   /* Process locked */
00712   __HAL_LOCK(hjpeg);
00713 
00714   if (HAL_JPEG_STATE_READY == hjpeg->State)
00715   {
00716     switch (CallbackID)
00717     {
00718       case HAL_JPEG_ENCODE_CPLT_CB_ID :
00719         hjpeg->EncodeCpltCallback = pCallback;
00720         break;
00721 
00722       case HAL_JPEG_DECODE_CPLT_CB_ID :
00723         hjpeg->DecodeCpltCallback = pCallback;
00724         break;
00725 
00726       case HAL_JPEG_ERROR_CB_ID :
00727         hjpeg->ErrorCallback = pCallback;
00728         break;
00729 
00730       case HAL_JPEG_MSPINIT_CB_ID :
00731         hjpeg->MspInitCallback = pCallback;
00732         break;
00733 
00734       case HAL_JPEG_MSPDEINIT_CB_ID :
00735         hjpeg->MspDeInitCallback = pCallback;
00736         break;
00737 
00738       default :
00739         /* Update the error code */
00740         hjpeg->ErrorCode |= HAL_JPEG_ERROR_INVALID_CALLBACK;
00741         /* Return error status */
00742         status =  HAL_ERROR;
00743         break;
00744     }
00745   }
00746   else if (HAL_JPEG_STATE_RESET == hjpeg->State)
00747   {
00748     switch (CallbackID)
00749     {
00750       case HAL_JPEG_MSPINIT_CB_ID :
00751         hjpeg->MspInitCallback = pCallback;
00752         break;
00753 
00754       case HAL_JPEG_MSPDEINIT_CB_ID :
00755         hjpeg->MspDeInitCallback = pCallback;
00756         break;
00757 
00758       default :
00759         /* Update the error code */
00760         hjpeg->ErrorCode |= HAL_JPEG_ERROR_INVALID_CALLBACK;
00761         /* Return error status */
00762         status =  HAL_ERROR;
00763         break;
00764     }
00765   }
00766   else
00767   {
00768     /* Update the error code */
00769     hjpeg->ErrorCode |= HAL_JPEG_ERROR_INVALID_CALLBACK;
00770     /* Return error status */
00771     status =  HAL_ERROR;
00772   }
00773 
00774   /* Release Lock */
00775   __HAL_UNLOCK(hjpeg);
00776   return status;
00777 }
00778 
00779 /**
00780   * @brief  Unregister a JPEG Callback
00781   *         JPEG callabck is redirected to the weak predefined callback
00782   * @param  hjpeg JPEG handle
00783   * @param  CallbackID ID of the callback to be unregistered
00784   *         This parameter can be one of the following values:
00785   *         This parameter can be one of the following values:
00786   *          @arg @ref HAL_JPEG_ENCODE_CPLT_CB_ID Encode Complete callback ID
00787   *          @arg @ref HAL_JPEG_DECODE_CPLT_CB_ID Decode Complete callback ID
00788   *          @arg @ref HAL_JPEG_ERROR_CB_ID Error callback ID
00789   *          @arg @ref HAL_JPEG_MSPINIT_CB_ID MspInit callback ID
00790   *          @arg @ref HAL_JPEG_MSPDEINIT_CB_ID MspDeInit callback ID
00791   * @retval HAL status
00792   */
00793 HAL_StatusTypeDef HAL_JPEG_UnRegisterCallback(JPEG_HandleTypeDef *hjpeg, HAL_JPEG_CallbackIDTypeDef CallbackID)
00794 {
00795   HAL_StatusTypeDef status = HAL_OK;
00796 
00797   /* Process locked */
00798   __HAL_LOCK(hjpeg);
00799 
00800   if (HAL_JPEG_STATE_READY == hjpeg->State)
00801   {
00802     switch (CallbackID)
00803     {
00804       case HAL_JPEG_ENCODE_CPLT_CB_ID :
00805         hjpeg->EncodeCpltCallback = HAL_JPEG_EncodeCpltCallback; /* Legacy weak EncodeCpltCallback  */
00806         break;
00807 
00808       case HAL_JPEG_DECODE_CPLT_CB_ID :
00809         hjpeg->DecodeCpltCallback = HAL_JPEG_DecodeCpltCallback; /* Legacy weak DecodeCpltCallback  */
00810         break;
00811 
00812       case HAL_JPEG_ERROR_CB_ID :
00813         hjpeg->ErrorCallback = HAL_JPEG_ErrorCallback;           /* Legacy weak ErrorCallback  */
00814         break;
00815 
00816       case HAL_JPEG_MSPINIT_CB_ID :
00817         hjpeg->MspInitCallback = HAL_JPEG_MspInit;              /* Legacy weak MspInit  */
00818         break;
00819 
00820       case HAL_JPEG_MSPDEINIT_CB_ID :
00821         hjpeg->MspDeInitCallback = HAL_JPEG_MspDeInit;          /* Legacy weak MspDeInit  */
00822         break;
00823 
00824       default :
00825         /* Update the error code */
00826         hjpeg->ErrorCode |= HAL_JPEG_ERROR_INVALID_CALLBACK;
00827         /* Return error status */
00828         status =  HAL_ERROR;
00829         break;
00830     }
00831   }
00832   else if (HAL_JPEG_STATE_RESET == hjpeg->State)
00833   {
00834     switch (CallbackID)
00835     {
00836       case HAL_JPEG_MSPINIT_CB_ID :
00837         hjpeg->MspInitCallback = HAL_JPEG_MspInit;           /* Legacy weak MspInit  */
00838         break;
00839 
00840       case HAL_JPEG_MSPDEINIT_CB_ID :
00841         hjpeg->MspDeInitCallback = HAL_JPEG_MspDeInit;       /* Legacy weak MspInit  */
00842         break;
00843 
00844       default :
00845         /* Update the error code */
00846         hjpeg->ErrorCode |= HAL_JPEG_ERROR_INVALID_CALLBACK;
00847         /* Return error status */
00848         status =  HAL_ERROR;
00849         break;
00850     }
00851   }
00852   else
00853   {
00854     /* Update the error code */
00855     hjpeg->ErrorCode |= HAL_JPEG_ERROR_INVALID_CALLBACK;
00856     /* Return error status */
00857     status =  HAL_ERROR;
00858   }
00859 
00860   /* Release Lock */
00861   __HAL_UNLOCK(hjpeg);
00862   return status;
00863 }
00864 
00865 /**
00866   * @brief  Register Info Ready JPEG Callback
00867   *         To be used instead of the weak HAL_JPEG_InfoReadyCallback() predefined callback
00868   * @param  hjpeg JPEG handle
00869   * @param  pCallback pointer to the Info Ready Callback function
00870   * @retval HAL status
00871   */
00872 HAL_StatusTypeDef HAL_JPEG_RegisterInfoReadyCallback(JPEG_HandleTypeDef *hjpeg,
00873                                                      pJPEG_InfoReadyCallbackTypeDef pCallback)
00874 {
00875   HAL_StatusTypeDef status = HAL_OK;
00876 
00877   if (pCallback == NULL)
00878   {
00879     /* Update the error code */
00880     hjpeg->ErrorCode |= HAL_JPEG_ERROR_INVALID_CALLBACK;
00881     return HAL_ERROR;
00882   }
00883   /* Process locked */
00884   __HAL_LOCK(hjpeg);
00885 
00886   if (HAL_JPEG_STATE_READY == hjpeg->State)
00887   {
00888     hjpeg->InfoReadyCallback = pCallback;
00889   }
00890   else
00891   {
00892     /* Update the error code */
00893     hjpeg->ErrorCode |= HAL_JPEG_ERROR_INVALID_CALLBACK;
00894     /* Return error status */
00895     status =  HAL_ERROR;
00896   }
00897 
00898   /* Release Lock */
00899   __HAL_UNLOCK(hjpeg);
00900   return status;
00901 }
00902 
00903 /**
00904   * @brief  UnRegister the Info Ready JPEG Callback
00905   *         Info Ready JPEG Callback is redirected to the weak HAL_JPEG_InfoReadyCallback() predefined callback
00906   * @param  hjpeg JPEG handle
00907   * @retval HAL status
00908   */
00909 HAL_StatusTypeDef HAL_JPEG_UnRegisterInfoReadyCallback(JPEG_HandleTypeDef *hjpeg)
00910 {
00911   HAL_StatusTypeDef status = HAL_OK;
00912 
00913   /* Process locked */
00914   __HAL_LOCK(hjpeg);
00915 
00916   if (HAL_JPEG_STATE_READY == hjpeg->State)
00917   {
00918     hjpeg->InfoReadyCallback = HAL_JPEG_InfoReadyCallback; /* Legacy weak InfoReadyCallback  */
00919   }
00920   else
00921   {
00922     /* Update the error code */
00923     hjpeg->ErrorCode |= HAL_JPEG_ERROR_INVALID_CALLBACK;
00924     /* Return error status */
00925     status =  HAL_ERROR;
00926   }
00927 
00928   /* Release Lock */
00929   __HAL_UNLOCK(hjpeg);
00930   return status;
00931 }
00932 
00933 /**
00934   * @brief  Register Get Data JPEG Callback
00935   *         To be used instead of the weak HAL_JPEG_GetDataCallback() predefined callback
00936   * @param  hjpeg JPEG handle
00937   * @param  pCallback pointer to the Get Data Callback function
00938   * @retval HAL status
00939   */
00940 HAL_StatusTypeDef HAL_JPEG_RegisterGetDataCallback(JPEG_HandleTypeDef *hjpeg, pJPEG_GetDataCallbackTypeDef pCallback)
00941 {
00942   HAL_StatusTypeDef status = HAL_OK;
00943 
00944   if (pCallback == NULL)
00945   {
00946     /* Update the error code */
00947     hjpeg->ErrorCode |= HAL_JPEG_ERROR_INVALID_CALLBACK;
00948     return HAL_ERROR;
00949   }
00950   /* Process locked */
00951   __HAL_LOCK(hjpeg);
00952 
00953   if (HAL_JPEG_STATE_READY == hjpeg->State)
00954   {
00955     hjpeg->GetDataCallback = pCallback;
00956   }
00957   else
00958   {
00959     /* Update the error code */
00960     hjpeg->ErrorCode |= HAL_JPEG_ERROR_INVALID_CALLBACK;
00961     /* Return error status */
00962     status =  HAL_ERROR;
00963   }
00964 
00965   /* Release Lock */
00966   __HAL_UNLOCK(hjpeg);
00967   return status;
00968 }
00969 
00970 /**
00971   * @brief  UnRegister the Get Data JPEG Callback
00972   *         Get Data JPEG Callback is redirected to the weak HAL_JPEG_GetDataCallback() predefined callback
00973   * @param  hjpeg JPEG handle
00974   * @retval HAL status
00975   */
00976 HAL_StatusTypeDef HAL_JPEG_UnRegisterGetDataCallback(JPEG_HandleTypeDef *hjpeg)
00977 {
00978   HAL_StatusTypeDef status = HAL_OK;
00979 
00980   /* Process locked */
00981   __HAL_LOCK(hjpeg);
00982 
00983   if (HAL_JPEG_STATE_READY == hjpeg->State)
00984   {
00985     hjpeg->GetDataCallback = HAL_JPEG_GetDataCallback;  /* Legacy weak GetDataCallback  */
00986   }
00987   else
00988   {
00989     /* Update the error code */
00990     hjpeg->ErrorCode |= HAL_JPEG_ERROR_INVALID_CALLBACK;
00991     /* Return error status */
00992     status =  HAL_ERROR;
00993   }
00994 
00995   /* Release Lock */
00996   __HAL_UNLOCK(hjpeg);
00997   return status;
00998 }
00999 
01000 /**
01001   * @brief  Register Data Ready JPEG Callback
01002   *         To be used instead of the weak HAL_JPEG_DataReadyCallback() predefined callback
01003   * @param  hjpeg JPEG handle
01004   * @param  pCallback pointer to the Get Data Callback function
01005   * @retval HAL status
01006   */
01007 HAL_StatusTypeDef HAL_JPEG_RegisterDataReadyCallback(JPEG_HandleTypeDef *hjpeg,
01008                                                      pJPEG_DataReadyCallbackTypeDef pCallback)
01009 {
01010   HAL_StatusTypeDef status = HAL_OK;
01011 
01012   if (pCallback == NULL)
01013   {
01014     /* Update the error code */
01015     hjpeg->ErrorCode |= HAL_JPEG_ERROR_INVALID_CALLBACK;
01016     return HAL_ERROR;
01017   }
01018   /* Process locked */
01019   __HAL_LOCK(hjpeg);
01020 
01021   if (HAL_JPEG_STATE_READY == hjpeg->State)
01022   {
01023     hjpeg->DataReadyCallback = pCallback;
01024   }
01025   else
01026   {
01027     /* Update the error code */
01028     hjpeg->ErrorCode |= HAL_JPEG_ERROR_INVALID_CALLBACK;
01029     /* Return error status */
01030     status =  HAL_ERROR;
01031   }
01032 
01033   /* Release Lock */
01034   __HAL_UNLOCK(hjpeg);
01035   return status;
01036 }
01037 
01038 /**
01039   * @brief  UnRegister the Data Ready JPEG Callback
01040   *         Get Data Ready Callback is redirected to the weak HAL_JPEG_DataReadyCallback() predefined callback
01041   * @param  hjpeg JPEG handle
01042   * @retval HAL status
01043   */
01044 HAL_StatusTypeDef HAL_JPEG_UnRegisterDataReadyCallback(JPEG_HandleTypeDef *hjpeg)
01045 {
01046   HAL_StatusTypeDef status = HAL_OK;
01047 
01048   /* Process locked */
01049   __HAL_LOCK(hjpeg);
01050 
01051   if (HAL_JPEG_STATE_READY == hjpeg->State)
01052   {
01053     hjpeg->DataReadyCallback = HAL_JPEG_DataReadyCallback;  /* Legacy weak DataReadyCallback  */
01054   }
01055   else
01056   {
01057     /* Update the error code */
01058     hjpeg->ErrorCode |= HAL_JPEG_ERROR_INVALID_CALLBACK;
01059     /* Return error status */
01060     status =  HAL_ERROR;
01061   }
01062 
01063   /* Release Lock */
01064   __HAL_UNLOCK(hjpeg);
01065   return status;
01066 }
01067 
01068 #endif /* USE_HAL_JPEG_REGISTER_CALLBACKS */
01069 
01070 /**
01071   * @}
01072   */
01073 
01074 /** @defgroup JPEG_Exported_Functions_Group2 Configuration functions
01075   *  @brief    JPEG Configuration functions.
01076   *
01077 @verbatim
01078   ==============================================================================
01079               ##### Configuration functions #####
01080   ==============================================================================
01081     [..]  This section provides functions allowing to:
01082       (+) HAL_JPEG_ConfigEncoding() : JPEG encoding configuration
01083       (+) HAL_JPEG_GetInfo() :  Extract the image configuration from the JPEG header during the decoding
01084       (+) HAL_JPEG_EnableHeaderParsing() :  Enable JPEG Header parsing for decoding
01085       (+) HAL_JPEG_DisableHeaderParsing() : Disable JPEG Header parsing for decoding
01086       (+) HAL_JPEG_SetUserQuantTables : Modify the default Quantization tables used for JPEG encoding.
01087 
01088 @endverbatim
01089   * @{
01090   */
01091 
01092 /**
01093   * @brief  Set the JPEG encoding configuration.
01094   * @param  hjpeg pointer to a JPEG_HandleTypeDef structure that contains
01095   *         the configuration information for JPEG module
01096   * @param  pConf pointer to a JPEG_ConfTypeDef structure that contains
01097   *         the encoding configuration
01098   * @retval HAL status
01099   */
01100 HAL_StatusTypeDef HAL_JPEG_ConfigEncoding(JPEG_HandleTypeDef *hjpeg, JPEG_ConfTypeDef *pConf)
01101 {
01102   uint32_t error;
01103   uint32_t numberMCU;
01104   uint32_t hfactor;
01105   uint32_t vfactor;
01106   uint32_t hMCU;
01107   uint32_t vMCU;
01108 
01109   /* Check the JPEG handle allocation */
01110   if ((hjpeg == NULL) || (pConf == NULL))
01111   {
01112     return HAL_ERROR;
01113   }
01114   else
01115   {
01116     /* Check the parameters */
01117     assert_param(IS_JPEG_COLORSPACE(pConf->ColorSpace));
01118     assert_param(IS_JPEG_CHROMASUBSAMPLING(pConf->ChromaSubsampling));
01119     assert_param(IS_JPEG_IMAGE_QUALITY(pConf->ImageQuality));
01120 
01121     /* Process Locked */
01122     __HAL_LOCK(hjpeg);
01123 
01124     if (hjpeg->State == HAL_JPEG_STATE_READY)
01125     {
01126       hjpeg->State = HAL_JPEG_STATE_BUSY;
01127 
01128       hjpeg->Conf.ColorSpace          =  pConf->ColorSpace;
01129       hjpeg->Conf.ChromaSubsampling   =  pConf->ChromaSubsampling;
01130       hjpeg->Conf.ImageHeight         =  pConf->ImageHeight;
01131       hjpeg->Conf.ImageWidth          =  pConf->ImageWidth;
01132       hjpeg->Conf.ImageQuality        =  pConf->ImageQuality;
01133 
01134       /* Reset the Color Space : by default only one quantization table is used*/
01135       hjpeg->Instance->CONFR1 &= ~JPEG_CONFR1_COLORSPACE;
01136 
01137       /* Set Number of color components*/
01138       if (hjpeg->Conf.ColorSpace == JPEG_GRAYSCALE_COLORSPACE)
01139       {
01140         /*Gray Scale is only one component 8x8 blocks i.e 4:4:4*/
01141         hjpeg->Conf.ChromaSubsampling = JPEG_444_SUBSAMPLING;
01142 
01143         JPEG_SetColorGrayScale(hjpeg);
01144         /* Set quantization table 0*/
01145         error = JPEG_Set_Quantization_Mem(hjpeg, hjpeg->QuantTable0, (hjpeg->Instance->QMEM0));
01146       }
01147       else if (hjpeg->Conf.ColorSpace == JPEG_YCBCR_COLORSPACE)
01148       {
01149         /*
01150            Set the Color Space for YCbCr : 2 quantization tables are used
01151            one for Luminance(Y) and one for both Chrominances (Cb & Cr)
01152           */
01153         hjpeg->Instance->CONFR1 |= JPEG_CONFR1_COLORSPACE_0;
01154 
01155         JPEG_SetColorYCBCR(hjpeg);
01156 
01157         /* Set quantization table 0*/
01158         error  = JPEG_Set_Quantization_Mem(hjpeg, hjpeg->QuantTable0, (hjpeg->Instance->QMEM0));
01159         /*By default quantization table 0 for component 0 and quantization table 1 for both components 1 and 2*/
01160         error |= JPEG_Set_Quantization_Mem(hjpeg, hjpeg->QuantTable1, (hjpeg->Instance->QMEM1));
01161 
01162         if ((hjpeg->Context & JPEG_CONTEXT_CUSTOM_TABLES) != 0UL)
01163         {
01164           /*Use user customized quantization tables , 1 table per component*/
01165           /* use 3 quantization tables , one for each component*/
01166           hjpeg->Instance->CONFR1 &= (~JPEG_CONFR1_COLORSPACE);
01167           hjpeg->Instance->CONFR1 |= JPEG_CONFR1_COLORSPACE_1;
01168 
01169           error |= JPEG_Set_Quantization_Mem(hjpeg, hjpeg->QuantTable2, (hjpeg->Instance->QMEM2));
01170 
01171           /*Use Quantization 1 table for component 1*/
01172           hjpeg->Instance->CONFR5 &= (~JPEG_CONFR5_QT);
01173           hjpeg->Instance->CONFR5 |=  JPEG_CONFR5_QT_0;
01174 
01175           /*Use Quantization 2 table for component 2*/
01176           hjpeg->Instance->CONFR6 &= (~JPEG_CONFR6_QT);
01177           hjpeg->Instance->CONFR6 |=  JPEG_CONFR6_QT_1;
01178         }
01179       }
01180       else /* ColorSpace == JPEG_CMYK_COLORSPACE */
01181       {
01182         JPEG_SetColorCMYK(hjpeg);
01183 
01184         /* Set quantization table 0*/
01185         error  = JPEG_Set_Quantization_Mem(hjpeg, hjpeg->QuantTable0, (hjpeg->Instance->QMEM0));
01186         /*By default quantization table 0 for All components*/
01187 
01188         if ((hjpeg->Context & JPEG_CONTEXT_CUSTOM_TABLES) != 0UL)
01189         {
01190           /*Use user customized quantization tables , 1 table per component*/
01191           /* use 4 quantization tables , one for each component*/
01192           hjpeg->Instance->CONFR1 |= JPEG_CONFR1_COLORSPACE;
01193 
01194           error |= JPEG_Set_Quantization_Mem(hjpeg, hjpeg->QuantTable1, (hjpeg->Instance->QMEM1));
01195           error |= JPEG_Set_Quantization_Mem(hjpeg, hjpeg->QuantTable2, (hjpeg->Instance->QMEM2));
01196           error |= JPEG_Set_Quantization_Mem(hjpeg, hjpeg->QuantTable3, (hjpeg->Instance->QMEM3));
01197 
01198           /*Use Quantization 1 table for component 1*/
01199           hjpeg->Instance->CONFR5 |=  JPEG_CONFR5_QT_0;
01200 
01201           /*Use Quantization 2 table for component 2*/
01202           hjpeg->Instance->CONFR6 |=  JPEG_CONFR6_QT_1;
01203 
01204           /*Use Quantization 3 table for component 3*/
01205           hjpeg->Instance->CONFR7 |=  JPEG_CONFR7_QT;
01206         }
01207       }
01208 
01209       if (error != 0UL)
01210       {
01211         hjpeg->ErrorCode = HAL_JPEG_ERROR_QUANT_TABLE;
01212 
01213         /* Process Unlocked */
01214         __HAL_UNLOCK(hjpeg);
01215 
01216         /* Set the JPEG State to ready */
01217         hjpeg->State = HAL_JPEG_STATE_READY;
01218 
01219         return  HAL_ERROR;
01220       }
01221       /* Set the image size*/
01222       /* set the number of lines*/
01223       MODIFY_REG(hjpeg->Instance->CONFR1, JPEG_CONFR1_YSIZE, ((hjpeg->Conf.ImageHeight & 0x0000FFFFUL) << 16));
01224       /* set the number of pixels per line*/
01225       MODIFY_REG(hjpeg->Instance->CONFR3, JPEG_CONFR3_XSIZE, ((hjpeg->Conf.ImageWidth & 0x0000FFFFUL) << 16));
01226 
01227 
01228       if (hjpeg->Conf.ChromaSubsampling == JPEG_420_SUBSAMPLING) /* 4:2:0*/
01229       {
01230         hfactor = 16;
01231         vfactor = 16;
01232       }
01233       else if (hjpeg->Conf.ChromaSubsampling == JPEG_422_SUBSAMPLING) /* 4:2:2*/
01234       {
01235         hfactor = 16;
01236         vfactor = 8;
01237       }
01238       else /* Default is 8x8 MCU,  4:4:4*/
01239       {
01240         hfactor = 8;
01241         vfactor = 8;
01242       }
01243 
01244       hMCU = (hjpeg->Conf.ImageWidth / hfactor);
01245       if ((hjpeg->Conf.ImageWidth % hfactor) != 0UL)
01246       {
01247         hMCU++; /*+1 for horizontal incomplete MCU */
01248       }
01249 
01250       vMCU = (hjpeg->Conf.ImageHeight / vfactor);
01251       if ((hjpeg->Conf.ImageHeight % vfactor) != 0UL)
01252       {
01253         vMCU++; /*+1 for vertical incomplete MCU */
01254       }
01255 
01256       numberMCU = (hMCU * vMCU) - 1UL; /* Bit Field JPEG_CONFR2_NMCU shall be set to NB_MCU - 1*/
01257       /* Set the number of MCU*/
01258       hjpeg->Instance->CONFR2 = (numberMCU & JPEG_CONFR2_NMCU);
01259 
01260       hjpeg->Context |= JPEG_CONTEXT_CONF_ENCODING;
01261 
01262       /* Process Unlocked */
01263       __HAL_UNLOCK(hjpeg);
01264 
01265       /* Set the JPEG State to ready */
01266       hjpeg->State = HAL_JPEG_STATE_READY;
01267 
01268       /* Return function status */
01269       return HAL_OK;
01270     }
01271     else
01272     {
01273       /* Process Unlocked */
01274       __HAL_UNLOCK(hjpeg);
01275 
01276       /* Return function status */
01277       return HAL_BUSY;
01278     }
01279   }
01280 }
01281 
01282 /**
01283   * @brief  Extract the image configuration from the JPEG header during the decoding
01284   * @param  hjpeg pointer to a JPEG_HandleTypeDef structure that contains
01285   *         the configuration information for JPEG module
01286   * @param  pInfo pointer to a JPEG_ConfTypeDef structure that contains
01287   *         The JPEG decoded header information
01288   * @retval HAL status
01289   */
01290 HAL_StatusTypeDef HAL_JPEG_GetInfo(JPEG_HandleTypeDef *hjpeg, JPEG_ConfTypeDef *pInfo)
01291 {
01292   uint32_t yblockNb;
01293   uint32_t cBblockNb;
01294   uint32_t cRblockNb;
01295 
01296   /* Check the JPEG handle allocation */
01297   if ((hjpeg == NULL) || (pInfo == NULL))
01298   {
01299     return HAL_ERROR;
01300   }
01301 
01302   /*Read the conf parameters */
01303   if ((hjpeg->Instance->CONFR1 & JPEG_CONFR1_NF) == JPEG_CONFR1_NF_1)
01304   {
01305     pInfo->ColorSpace = JPEG_YCBCR_COLORSPACE;
01306   }
01307   else if ((hjpeg->Instance->CONFR1 & JPEG_CONFR1_NF) == 0UL)
01308   {
01309     pInfo->ColorSpace = JPEG_GRAYSCALE_COLORSPACE;
01310   }
01311   else if ((hjpeg->Instance->CONFR1 & JPEG_CONFR1_NF) == JPEG_CONFR1_NF)
01312   {
01313     pInfo->ColorSpace = JPEG_CMYK_COLORSPACE;
01314   }
01315   else
01316   {
01317     return HAL_ERROR;
01318   }
01319 
01320   pInfo->ImageHeight = (hjpeg->Instance->CONFR1 & 0xFFFF0000UL) >> 16;
01321   pInfo->ImageWidth  = (hjpeg->Instance->CONFR3 & 0xFFFF0000UL) >> 16;
01322 
01323   if ((pInfo->ColorSpace == JPEG_YCBCR_COLORSPACE) || (pInfo->ColorSpace == JPEG_CMYK_COLORSPACE))
01324   {
01325     yblockNb  = (hjpeg->Instance->CONFR4 & JPEG_CONFR4_NB) >> 4;
01326     cBblockNb = (hjpeg->Instance->CONFR5 & JPEG_CONFR5_NB) >> 4;
01327     cRblockNb = (hjpeg->Instance->CONFR6 & JPEG_CONFR6_NB) >> 4;
01328 
01329     if ((yblockNb == 1UL) && (cBblockNb == 0UL) && (cRblockNb == 0UL))
01330     {
01331       pInfo->ChromaSubsampling = JPEG_422_SUBSAMPLING; /*16x8 block*/
01332     }
01333     else if ((yblockNb == 0UL) && (cBblockNb == 0UL) && (cRblockNb == 0UL))
01334     {
01335       pInfo->ChromaSubsampling = JPEG_444_SUBSAMPLING;
01336     }
01337     else if ((yblockNb == 3UL) && (cBblockNb == 0UL) && (cRblockNb == 0UL))
01338     {
01339       pInfo->ChromaSubsampling = JPEG_420_SUBSAMPLING;
01340     }
01341     else /*Default is 4:4:4*/
01342     {
01343       pInfo->ChromaSubsampling = JPEG_444_SUBSAMPLING;
01344     }
01345   }
01346   else
01347   {
01348     pInfo->ChromaSubsampling = JPEG_444_SUBSAMPLING;
01349   }
01350 
01351   pInfo->ImageQuality = JPEG_GetQuality(hjpeg);
01352 
01353   /* Return function status */
01354   return HAL_OK;
01355 }
01356 
01357 /**
01358   * @brief  Enable JPEG Header parsing for decoding
01359   * @param  hjpeg pointer to a JPEG_HandleTypeDef structure that contains
01360   *               the configuration information for the JPEG.
01361   * @retval HAL status
01362   */
01363 HAL_StatusTypeDef HAL_JPEG_EnableHeaderParsing(JPEG_HandleTypeDef *hjpeg)
01364 {
01365   /* Process locked */
01366   __HAL_LOCK(hjpeg);
01367 
01368   if (hjpeg->State == HAL_JPEG_STATE_READY)
01369   {
01370     /* Change the JPEG state */
01371     hjpeg->State = HAL_JPEG_STATE_BUSY;
01372 
01373     /* Enable header processing*/
01374     hjpeg->Instance->CONFR1 |= JPEG_CONFR1_HDR;
01375 
01376     /* Process unlocked */
01377     __HAL_UNLOCK(hjpeg);
01378 
01379     /* Change the JPEG state */
01380     hjpeg->State = HAL_JPEG_STATE_READY;
01381 
01382     return HAL_OK;
01383   }
01384   else
01385   {
01386     /* Process unlocked */
01387     __HAL_UNLOCK(hjpeg);
01388 
01389     return HAL_BUSY;
01390   }
01391 }
01392 
01393 /**
01394   * @brief  Disable JPEG Header parsing for decoding
01395   * @param  hjpeg pointer to a JPEG_HandleTypeDef structure that contains
01396   *               the configuration information for the JPEG.
01397   * @retval HAL status
01398   */
01399 HAL_StatusTypeDef HAL_JPEG_DisableHeaderParsing(JPEG_HandleTypeDef *hjpeg)
01400 {
01401   /* Process locked */
01402   __HAL_LOCK(hjpeg);
01403 
01404   if (hjpeg->State == HAL_JPEG_STATE_READY)
01405   {
01406     /* Change the JPEG state */
01407     hjpeg->State = HAL_JPEG_STATE_BUSY;
01408 
01409     /* Disable header processing*/
01410     hjpeg->Instance->CONFR1 &= ~JPEG_CONFR1_HDR;
01411 
01412     /* Process unlocked */
01413     __HAL_UNLOCK(hjpeg);
01414 
01415     /* Change the JPEG state */
01416     hjpeg->State = HAL_JPEG_STATE_READY;
01417 
01418     return HAL_OK;
01419   }
01420   else
01421   {
01422     /* Process unlocked */
01423     __HAL_UNLOCK(hjpeg);
01424 
01425     return HAL_BUSY;
01426   }
01427 }
01428 
01429 /**
01430   * @brief  Modify the default Quantization tables used for JPEG encoding.
01431   * @param  hjpeg pointer to a JPEG_HandleTypeDef structure that contains
01432   *         the configuration information for JPEG module
01433   * @param  QTable0  pointer to uint8_t , define the user quantification table for color component 1.
01434   *                  If NULL assume no need to update  the table and no error return
01435   * @param  QTable1  pointer to uint8_t , define the user quantification table for color component 2.
01436   *                  If NULL assume no need to update  the table and no error return.
01437   * @param  QTable2  pointer to uint8_t , define the user quantification table for color component 3,
01438   *                  If NULL assume no need to update  the table and no error return.
01439   * @param  QTable3  pointer to uint8_t , define the user quantification table for color component 4.
01440   *                  If NULL assume no need to update  the table and no error return.
01441   *
01442   * @retval HAL status
01443   */
01444 
01445 
01446 HAL_StatusTypeDef  HAL_JPEG_SetUserQuantTables(JPEG_HandleTypeDef *hjpeg, uint8_t *QTable0, uint8_t *QTable1,
01447                                                uint8_t *QTable2, uint8_t *QTable3)
01448 {
01449   /* Process Locked */
01450   __HAL_LOCK(hjpeg);
01451 
01452   if (hjpeg->State == HAL_JPEG_STATE_READY)
01453   {
01454     /* Change the DMA state */
01455     hjpeg->State = HAL_JPEG_STATE_BUSY;
01456 
01457     hjpeg->Context |= JPEG_CONTEXT_CUSTOM_TABLES;
01458 
01459     hjpeg->QuantTable0 = QTable0;
01460     hjpeg->QuantTable1 = QTable1;
01461     hjpeg->QuantTable2 = QTable2;
01462     hjpeg->QuantTable3 = QTable3;
01463 
01464     /* Process Unlocked */
01465     __HAL_UNLOCK(hjpeg);
01466 
01467     /* Change the DMA state */
01468     hjpeg->State = HAL_JPEG_STATE_READY;
01469 
01470     /* Return function status */
01471     return HAL_OK;
01472   }
01473   else
01474   {
01475     /* Process Unlocked */
01476     __HAL_UNLOCK(hjpeg);
01477 
01478     return HAL_BUSY;
01479   }
01480 }
01481 
01482 /**
01483   * @}
01484   */
01485 
01486 /** @defgroup JPEG_Exported_Functions_Group3 encoding/decoding processing functions
01487   *  @brief   processing functions.
01488   *
01489 @verbatim
01490   ==============================================================================
01491                       ##### JPEG processing functions #####
01492   ==============================================================================
01493     [..]  This section provides functions allowing to:
01494       (+) HAL_JPEG_Encode()     : JPEG encoding with polling process
01495       (+) HAL_JPEG_Decode()     : JPEG decoding with polling process
01496       (+) HAL_JPEG_Encode_IT()  : JPEG encoding with interrupt process
01497       (+) HAL_JPEG_Decode_IT()  : JPEG decoding with interrupt process
01498       (+) HAL_JPEG_Encode_DMA() : JPEG encoding with DMA process
01499       (+) HAL_JPEG_Decode_DMA() : JPEG decoding with DMA process
01500       (+) HAL_JPEG_Pause()      :   Pause the Input/Output processing
01501       (+) HAL_JPEG_Resume()     :  Resume the JPEG Input/Output processing
01502       (+) HAL_JPEG_ConfigInputBuffer()  : Config Encoding/Decoding Input Buffer
01503       (+) HAL_JPEG_ConfigOutputBuffer() : Config Encoding/Decoding Output Buffer
01504       (+) HAL_JPEG_Abort()              : Aborts the JPEG Encoding/Decoding
01505 
01506 @endverbatim
01507   * @{
01508   */
01509 
01510 /**
01511   * @brief  Starts JPEG encoding with polling processing
01512   * @param  hjpeg pointer to a JPEG_HandleTypeDef structure that contains
01513   *         the configuration information for JPEG module
01514   * @param  pDataInMCU Pointer to the Input buffer
01515   * @param  InDataLength size in bytes Input buffer
01516   * @param  pDataOut Pointer to the jpeg output data buffer
01517   * @param  OutDataLength size in bytes of the Output buffer
01518   * @param  Timeout Specify Timeout value
01519   * @retval HAL status
01520   */
01521 HAL_StatusTypeDef  HAL_JPEG_Encode(JPEG_HandleTypeDef *hjpeg, uint8_t *pDataInMCU, uint32_t InDataLength,
01522                                    uint8_t *pDataOut, uint32_t OutDataLength, uint32_t Timeout)
01523 {
01524   uint32_t tickstart;
01525 
01526   /* Check the parameters */
01527   assert_param((InDataLength >= 4UL));
01528   assert_param((OutDataLength >= 4UL));
01529 
01530   /* Check In/out buffer allocation and size */
01531   if ((hjpeg == NULL) || (pDataInMCU == NULL) || (pDataOut == NULL))
01532   {
01533     return HAL_ERROR;
01534   }
01535   /* Process locked */
01536   __HAL_LOCK(hjpeg);
01537 
01538   if (hjpeg->State != HAL_JPEG_STATE_READY)
01539   {
01540     /* Process Unlocked */
01541     __HAL_UNLOCK(hjpeg);
01542 
01543     return HAL_BUSY;
01544   }
01545 
01546   if (hjpeg->State == HAL_JPEG_STATE_READY)
01547   {
01548     if ((hjpeg->Context & JPEG_CONTEXT_CONF_ENCODING) == JPEG_CONTEXT_CONF_ENCODING)
01549     {
01550       /*Change JPEG state*/
01551       hjpeg->State = HAL_JPEG_STATE_BUSY_ENCODING;
01552 
01553       /*Set the Context to Encode with Polling*/
01554       hjpeg->Context &= ~(JPEG_CONTEXT_OPERATION_MASK | JPEG_CONTEXT_METHOD_MASK);
01555       hjpeg->Context |= (JPEG_CONTEXT_ENCODE | JPEG_CONTEXT_POLLING);
01556 
01557       /* Get tick */
01558       tickstart = HAL_GetTick();
01559 
01560       /*Store In/out buffers pointers and size*/
01561       hjpeg->pJpegInBuffPtr = pDataInMCU;
01562       hjpeg->pJpegOutBuffPtr = pDataOut;
01563       hjpeg->InDataLength = InDataLength - (InDataLength % 4UL);    /* In Data length must be multiple of 4 Bytes (1 word)*/
01564       hjpeg->OutDataLength = OutDataLength - (OutDataLength % 4UL); /* Out Data length must be multiple of 4 Bytes (1 word)*/
01565 
01566       /*Reset In/out data counter */
01567       hjpeg->JpegInCount = 0;
01568       hjpeg->JpegOutCount = 0;
01569 
01570       /*Init decoding process*/
01571       JPEG_Init_Process(hjpeg);
01572 
01573       /*JPEG data processing : In/Out FIFO transfer*/
01574       while ((JPEG_Process(hjpeg) == JPEG_PROCESS_ONGOING))
01575       {
01576         if (Timeout != HAL_MAX_DELAY)
01577         {
01578           if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0UL))
01579           {
01580 
01581             /* Update error code */
01582             hjpeg->ErrorCode |= HAL_JPEG_ERROR_TIMEOUT;
01583 
01584             /* Process Unlocked */
01585             __HAL_UNLOCK(hjpeg);
01586 
01587             /*Change JPEG state*/
01588             hjpeg->State = HAL_JPEG_STATE_READY;
01589 
01590             return HAL_TIMEOUT;
01591           }
01592         }
01593       }
01594 
01595       /* Process Unlocked */
01596       __HAL_UNLOCK(hjpeg);
01597 
01598       /*Change JPEG state*/
01599       hjpeg->State = HAL_JPEG_STATE_READY;
01600 
01601     }
01602     else
01603     {
01604       /* Process Unlocked */
01605       __HAL_UNLOCK(hjpeg);
01606 
01607       return HAL_ERROR;
01608     }
01609   }
01610   /* Return function status */
01611   return HAL_OK;
01612 }
01613 
01614 /**
01615   * @brief  Starts JPEG decoding with polling processing
01616   * @param  hjpeg pointer to a JPEG_HandleTypeDef structure that contains
01617   *         the configuration information for JPEG module
01618   * @param  pDataIn Pointer to the input data buffer
01619   * @param  InDataLength size in bytes Input buffer
01620   * @param  pDataOutMCU Pointer to the Output data buffer
01621   * @param  OutDataLength size in bytes of the Output buffer
01622   * @param  Timeout Specify Timeout value
01623   * @retval HAL status
01624   */
01625 HAL_StatusTypeDef  HAL_JPEG_Decode(JPEG_HandleTypeDef *hjpeg, uint8_t *pDataIn, uint32_t InDataLength,
01626                                    uint8_t *pDataOutMCU, uint32_t OutDataLength, uint32_t Timeout)
01627 {
01628   uint32_t tickstart;
01629 
01630   /* Check the parameters */
01631   assert_param((InDataLength >= 4UL));
01632   assert_param((OutDataLength >= 4UL));
01633 
01634   /* Check In/out buffer allocation and size */
01635   if ((hjpeg == NULL) || (pDataIn == NULL) || (pDataOutMCU == NULL))
01636   {
01637     return HAL_ERROR;
01638   }
01639 
01640   /* Process Locked */
01641   __HAL_LOCK(hjpeg);
01642 
01643   /* Get tick */
01644   tickstart = HAL_GetTick();
01645 
01646   if (hjpeg->State == HAL_JPEG_STATE_READY)
01647   {
01648     /*Change JPEG state*/
01649     hjpeg->State = HAL_JPEG_STATE_BUSY_DECODING;
01650 
01651     /*Set the Context to Decode with Polling*/
01652     /*Set the Context to Encode with Polling*/
01653     hjpeg->Context &= ~(JPEG_CONTEXT_OPERATION_MASK | JPEG_CONTEXT_METHOD_MASK);
01654     hjpeg->Context |= (JPEG_CONTEXT_DECODE | JPEG_CONTEXT_POLLING);
01655 
01656     /*Store In/out buffers pointers and size*/
01657     hjpeg->pJpegInBuffPtr = pDataIn;
01658     hjpeg->pJpegOutBuffPtr = pDataOutMCU;
01659     hjpeg->InDataLength = InDataLength - (InDataLength % 4UL);    /*In Data length must be multiple of 4 Bytes (1 word)*/
01660     hjpeg->OutDataLength = OutDataLength - (OutDataLength % 4UL); /*Out Data length must be multiple of 4 Bytes (1 word)*/
01661 
01662     /*Reset In/out data counter */
01663     hjpeg->JpegInCount = 0;
01664     hjpeg->JpegOutCount = 0;
01665 
01666     /*Init decoding process*/
01667     JPEG_Init_Process(hjpeg);
01668 
01669     /*JPEG data processing : In/Out FIFO transfer*/
01670     while ((JPEG_Process(hjpeg) == JPEG_PROCESS_ONGOING))
01671     {
01672       if (Timeout != HAL_MAX_DELAY)
01673       {
01674         if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0UL))
01675         {
01676 
01677           /* Update error code */
01678           hjpeg->ErrorCode |= HAL_JPEG_ERROR_TIMEOUT;
01679 
01680           /* Process Unlocked */
01681           __HAL_UNLOCK(hjpeg);
01682 
01683           /*Change JPEG state*/
01684           hjpeg->State = HAL_JPEG_STATE_READY;
01685 
01686           return HAL_TIMEOUT;
01687         }
01688       }
01689     }
01690 
01691     /* Process Unlocked */
01692     __HAL_UNLOCK(hjpeg);
01693 
01694     /*Change JPEG state*/
01695     hjpeg->State = HAL_JPEG_STATE_READY;
01696 
01697   }
01698   else
01699   {
01700     /* Process Unlocked */
01701     __HAL_UNLOCK(hjpeg);
01702 
01703     return HAL_BUSY;
01704   }
01705   /* Return function status */
01706   return HAL_OK;
01707 }
01708 
01709 /**
01710   * @brief  Starts JPEG encoding with interrupt processing
01711   * @param  hjpeg pointer to a JPEG_HandleTypeDef structure that contains
01712   *         the configuration information for JPEG module
01713   * @param  pDataInMCU Pointer to the Input buffer
01714   * @param  InDataLength size in bytes Input buffer
01715   * @param  pDataOut Pointer to the jpeg output data buffer
01716   * @param  OutDataLength size in bytes of the Output buffer
01717   * @retval HAL status
01718   */
01719 HAL_StatusTypeDef  HAL_JPEG_Encode_IT(JPEG_HandleTypeDef *hjpeg, uint8_t *pDataInMCU, uint32_t InDataLength,
01720                                       uint8_t *pDataOut, uint32_t OutDataLength)
01721 {
01722   /* Check the parameters */
01723   assert_param((InDataLength >= 4UL));
01724   assert_param((OutDataLength >= 4UL));
01725 
01726   /* Check In/out buffer allocation and size */
01727   if ((hjpeg == NULL) || (pDataInMCU == NULL) || (pDataOut == NULL))
01728   {
01729     return HAL_ERROR;
01730   }
01731 
01732   /* Process Locked */
01733   __HAL_LOCK(hjpeg);
01734 
01735   if (hjpeg->State != HAL_JPEG_STATE_READY)
01736   {
01737     /* Process Unlocked */
01738     __HAL_UNLOCK(hjpeg);
01739 
01740     return HAL_BUSY;
01741   }
01742   else
01743   {
01744     if ((hjpeg->Context & JPEG_CONTEXT_CONF_ENCODING) == JPEG_CONTEXT_CONF_ENCODING)
01745     {
01746       /*Change JPEG state*/
01747       hjpeg->State = HAL_JPEG_STATE_BUSY_ENCODING;
01748 
01749       /*Set the Context to Encode with IT*/
01750       hjpeg->Context &= ~(JPEG_CONTEXT_OPERATION_MASK | JPEG_CONTEXT_METHOD_MASK);
01751       hjpeg->Context |= (JPEG_CONTEXT_ENCODE | JPEG_CONTEXT_IT);
01752 
01753       /*Store In/out buffers pointers and size*/
01754       hjpeg->pJpegInBuffPtr = pDataInMCU;
01755       hjpeg->pJpegOutBuffPtr = pDataOut;
01756       hjpeg->InDataLength = InDataLength - (InDataLength % 4UL);    /*In Data length must be multiple of 4 Bytes (1 word)*/
01757       hjpeg->OutDataLength = OutDataLength - (OutDataLength % 4UL); /*Out Data length must be multiple of 4 Bytes (1 word)*/
01758 
01759       /*Reset In/out data counter */
01760       hjpeg->JpegInCount = 0;
01761       hjpeg->JpegOutCount = 0;
01762 
01763       /*Init decoding process*/
01764       JPEG_Init_Process(hjpeg);
01765 
01766     }
01767     else
01768     {
01769       /* Process Unlocked */
01770       __HAL_UNLOCK(hjpeg);
01771 
01772       return HAL_ERROR;
01773     }
01774   }
01775   /* Return function status */
01776   return HAL_OK;
01777 }
01778 
01779 /**
01780   * @brief  Starts JPEG decoding with interrupt processing
01781   * @param  hjpeg pointer to a JPEG_HandleTypeDef structure that contains
01782   *         the configuration information for JPEG module
01783   * @param  pDataIn Pointer to the input data buffer
01784   * @param  InDataLength size in bytes Input buffer
01785   * @param  pDataOutMCU Pointer to the Output data buffer
01786   * @param  OutDataLength size in bytes of the Output buffer
01787   * @retval HAL status
01788   */
01789 HAL_StatusTypeDef  HAL_JPEG_Decode_IT(JPEG_HandleTypeDef *hjpeg, uint8_t *pDataIn, uint32_t InDataLength,
01790                                       uint8_t *pDataOutMCU, uint32_t OutDataLength)
01791 {
01792   /* Check the parameters */
01793   assert_param((InDataLength >= 4UL));
01794   assert_param((OutDataLength >= 4UL));
01795 
01796   /* Check In/out buffer allocation and size */
01797   if ((hjpeg == NULL) || (pDataIn == NULL) || (pDataOutMCU == NULL))
01798   {
01799     return HAL_ERROR;
01800   }
01801 
01802   /* Process Locked */
01803   __HAL_LOCK(hjpeg);
01804 
01805   if (hjpeg->State == HAL_JPEG_STATE_READY)
01806   {
01807     /*Change JPEG state*/
01808     hjpeg->State = HAL_JPEG_STATE_BUSY_DECODING;
01809 
01810     /*Set the Context to Decode with IT*/
01811     hjpeg->Context &= ~(JPEG_CONTEXT_OPERATION_MASK | JPEG_CONTEXT_METHOD_MASK);
01812     hjpeg->Context |= (JPEG_CONTEXT_DECODE | JPEG_CONTEXT_IT);
01813 
01814     /*Store In/out buffers pointers and size*/
01815     hjpeg->pJpegInBuffPtr = pDataIn;
01816     hjpeg->pJpegOutBuffPtr = pDataOutMCU;
01817     hjpeg->InDataLength = InDataLength - (InDataLength % 4UL);    /*In Data length must be multiple of 4 Bytes (1 word)*/
01818     hjpeg->OutDataLength = OutDataLength - (OutDataLength % 4UL); /*Out Data length must be multiple of 4 Bytes (1 word)*/
01819 
01820     /*Reset In/out data counter */
01821     hjpeg->JpegInCount = 0;
01822     hjpeg->JpegOutCount = 0;
01823 
01824     /*Init decoding process*/
01825     JPEG_Init_Process(hjpeg);
01826 
01827   }
01828   else
01829   {
01830     /* Process Unlocked */
01831     __HAL_UNLOCK(hjpeg);
01832 
01833     return HAL_BUSY;
01834   }
01835   /* Return function status */
01836   return HAL_OK;
01837 }
01838 
01839 /**
01840   * @brief  Starts JPEG encoding with DMA processing
01841   * @param  hjpeg pointer to a JPEG_HandleTypeDef structure that contains
01842   *         the configuration information for JPEG module
01843   * @param  pDataInMCU Pointer to the Input buffer
01844   * @param  InDataLength size in bytes Input buffer
01845   * @param  pDataOut Pointer to the jpeg output data buffer
01846   * @param  OutDataLength size in bytes of the Output buffer
01847   * @retval HAL status
01848   */
01849 HAL_StatusTypeDef  HAL_JPEG_Encode_DMA(JPEG_HandleTypeDef *hjpeg, uint8_t *pDataInMCU, uint32_t InDataLength,
01850                                        uint8_t *pDataOut, uint32_t OutDataLength)
01851 {
01852   /* Check the parameters */
01853   assert_param((InDataLength >= 4UL));
01854   assert_param((OutDataLength >= 4UL));
01855 
01856   /* Check In/out buffer allocation and size */
01857   if ((hjpeg == NULL) || (pDataInMCU == NULL) || (pDataOut == NULL))
01858   {
01859     return HAL_ERROR;
01860   }
01861 
01862   /* Process Locked */
01863   __HAL_LOCK(hjpeg);
01864 
01865   if (hjpeg->State != HAL_JPEG_STATE_READY)
01866   {
01867     /* Process Unlocked */
01868     __HAL_UNLOCK(hjpeg);
01869 
01870     return HAL_BUSY;
01871   }
01872   else
01873   {
01874     if ((hjpeg->Context & JPEG_CONTEXT_CONF_ENCODING) == JPEG_CONTEXT_CONF_ENCODING)
01875     {
01876       /*Change JPEG state*/
01877       hjpeg->State = HAL_JPEG_STATE_BUSY_ENCODING;
01878 
01879       /*Set the Context to Encode with DMA*/
01880       hjpeg->Context &= ~(JPEG_CONTEXT_OPERATION_MASK | JPEG_CONTEXT_METHOD_MASK);
01881       hjpeg->Context |= (JPEG_CONTEXT_ENCODE | JPEG_CONTEXT_DMA);
01882 
01883       /*Store In/out buffers pointers and size*/
01884       hjpeg->pJpegInBuffPtr = pDataInMCU;
01885       hjpeg->pJpegOutBuffPtr = pDataOut;
01886       hjpeg->InDataLength = InDataLength;
01887       hjpeg->OutDataLength = OutDataLength;
01888 
01889       /*Reset In/out data counter */
01890       hjpeg->JpegInCount = 0;
01891       hjpeg->JpegOutCount = 0;
01892 
01893       /*Init decoding process*/
01894       JPEG_Init_Process(hjpeg);
01895 
01896       /* JPEG encoding process using DMA */
01897       if (JPEG_DMA_StartProcess(hjpeg) != HAL_OK)
01898       {
01899         /* Update State */
01900         hjpeg->State = HAL_JPEG_STATE_ERROR;
01901         /* Process Unlocked */
01902         __HAL_UNLOCK(hjpeg);
01903 
01904         return HAL_ERROR;
01905       }
01906 
01907     }
01908     else
01909     {
01910       /* Process Unlocked */
01911       __HAL_UNLOCK(hjpeg);
01912 
01913       return HAL_ERROR;
01914     }
01915   }
01916   /* Return function status */
01917   return HAL_OK;
01918 }
01919 
01920 /**
01921   * @brief  Starts JPEG decoding with DMA processing
01922   * @param  hjpeg pointer to a JPEG_HandleTypeDef structure that contains
01923   *         the configuration information for JPEG module
01924   * @param  pDataIn Pointer to the input data buffer
01925   * @param  InDataLength size in bytes Input buffer
01926   * @param  pDataOutMCU Pointer to the Output data buffer
01927   * @param  OutDataLength size in bytes of the Output buffer
01928   * @retval HAL status
01929   */
01930 HAL_StatusTypeDef  HAL_JPEG_Decode_DMA(JPEG_HandleTypeDef *hjpeg, uint8_t *pDataIn, uint32_t InDataLength,
01931                                        uint8_t *pDataOutMCU, uint32_t OutDataLength)
01932 {
01933   /* Check the parameters */
01934   assert_param((InDataLength >= 4UL));
01935   assert_param((OutDataLength >= 4UL));
01936 
01937   /* Check In/out buffer allocation and size */
01938   if ((hjpeg == NULL) || (pDataIn == NULL) || (pDataOutMCU == NULL))
01939   {
01940     return HAL_ERROR;
01941   }
01942 
01943   /* Process Locked */
01944   __HAL_LOCK(hjpeg);
01945 
01946   if (hjpeg->State == HAL_JPEG_STATE_READY)
01947   {
01948     /*Change JPEG state*/
01949     hjpeg->State = HAL_JPEG_STATE_BUSY_DECODING;
01950 
01951     /*Set the Context to Decode with DMA*/
01952     hjpeg->Context &= ~(JPEG_CONTEXT_OPERATION_MASK | JPEG_CONTEXT_METHOD_MASK);
01953     hjpeg->Context |= (JPEG_CONTEXT_DECODE | JPEG_CONTEXT_DMA);
01954 
01955     /*Store In/out buffers pointers and size*/
01956     hjpeg->pJpegInBuffPtr = pDataIn;
01957     hjpeg->pJpegOutBuffPtr = pDataOutMCU;
01958     hjpeg->InDataLength = InDataLength;
01959     hjpeg->OutDataLength = OutDataLength;
01960 
01961     /*Reset In/out data counter */
01962     hjpeg->JpegInCount = 0;
01963     hjpeg->JpegOutCount = 0;
01964 
01965     /*Init decoding process*/
01966     JPEG_Init_Process(hjpeg);
01967 
01968     /* JPEG decoding process using DMA */
01969     if (JPEG_DMA_StartProcess(hjpeg) != HAL_OK)
01970     {
01971       /* Update State */
01972       hjpeg->State = HAL_JPEG_STATE_ERROR;
01973       /* Process Unlocked */
01974       __HAL_UNLOCK(hjpeg);
01975 
01976       return HAL_ERROR;
01977     }
01978   }
01979   else
01980   {
01981     /* Process Unlocked */
01982     __HAL_UNLOCK(hjpeg);
01983 
01984     return HAL_BUSY;
01985   }
01986   /* Return function status */
01987   return HAL_OK;
01988 }
01989 
01990 /**
01991   * @brief  Pause the JPEG Input/Output processing
01992   * @param  hjpeg pointer to a JPEG_HandleTypeDef structure that contains
01993   *         the configuration information for JPEG module
01994   * @param  XferSelection This parameter can be one of the following values :
01995   *                           JPEG_PAUSE_RESUME_INPUT : Pause Input processing
01996   *                           JPEG_PAUSE_RESUME_OUTPUT: Pause Output processing
01997   *                           JPEG_PAUSE_RESUME_INPUT_OUTPUT: Pause Input and Output processing
01998   * @retval HAL status
01999   */
02000 HAL_StatusTypeDef  HAL_JPEG_Pause(JPEG_HandleTypeDef *hjpeg, uint32_t XferSelection)
02001 {
02002   uint32_t mask = 0;
02003 
02004   assert_param(IS_JPEG_PAUSE_RESUME_STATE(XferSelection));
02005 
02006   if ((hjpeg->Context & JPEG_CONTEXT_METHOD_MASK) == JPEG_CONTEXT_DMA)
02007   {
02008     if ((XferSelection & JPEG_PAUSE_RESUME_INPUT) == JPEG_PAUSE_RESUME_INPUT)
02009     {
02010       hjpeg->Context |= JPEG_CONTEXT_PAUSE_INPUT;
02011     }
02012     if ((XferSelection & JPEG_PAUSE_RESUME_OUTPUT) == JPEG_PAUSE_RESUME_OUTPUT)
02013     {
02014       hjpeg->Context |= JPEG_CONTEXT_PAUSE_OUTPUT;
02015     }
02016 
02017   }
02018   else if ((hjpeg->Context & JPEG_CONTEXT_METHOD_MASK) == JPEG_CONTEXT_IT)
02019   {
02020 
02021     if ((XferSelection & JPEG_PAUSE_RESUME_INPUT) == JPEG_PAUSE_RESUME_INPUT)
02022     {
02023       hjpeg->Context |= JPEG_CONTEXT_PAUSE_INPUT;
02024       mask |= (JPEG_IT_IFT | JPEG_IT_IFNF);
02025     }
02026     if ((XferSelection & JPEG_PAUSE_RESUME_OUTPUT) == JPEG_PAUSE_RESUME_OUTPUT)
02027     {
02028       hjpeg->Context |= JPEG_CONTEXT_PAUSE_OUTPUT;
02029       mask |= (JPEG_IT_OFT | JPEG_IT_OFNE | JPEG_IT_EOC);
02030     }
02031     __HAL_JPEG_DISABLE_IT(hjpeg, mask);
02032 
02033   }
02034   else
02035   {
02036     /* Nothing to do */
02037   }
02038 
02039   /* Return function status */
02040   return HAL_OK;
02041 }
02042 
02043 /**
02044   * @brief  Resume the JPEG Input/Output processing
02045   * @param  hjpeg pointer to a JPEG_HandleTypeDef structure that contains
02046   *         the configuration information for JPEG module
02047   * @param  XferSelection This parameter can be one of the following values :
02048   *                           JPEG_PAUSE_RESUME_INPUT : Resume Input processing
02049   *                           JPEG_PAUSE_RESUME_OUTPUT: Resume Output processing
02050   *                           JPEG_PAUSE_RESUME_INPUT_OUTPUT: Resume Input and Output processing
02051   * @retval HAL status
02052   */
02053 HAL_StatusTypeDef  HAL_JPEG_Resume(JPEG_HandleTypeDef *hjpeg, uint32_t XferSelection)
02054 {
02055   uint32_t mask = 0;
02056   uint32_t xfrSize;
02057 
02058   assert_param(IS_JPEG_PAUSE_RESUME_STATE(XferSelection));
02059 
02060   if ((hjpeg->Context & (JPEG_CONTEXT_PAUSE_INPUT | JPEG_CONTEXT_PAUSE_OUTPUT)) == 0UL)
02061   {
02062     /* if nothing paused to resume return error*/
02063     return HAL_ERROR;
02064   }
02065 
02066   if ((hjpeg->Context & JPEG_CONTEXT_METHOD_MASK) == JPEG_CONTEXT_DMA)
02067   {
02068 
02069     if ((XferSelection & JPEG_PAUSE_RESUME_INPUT) == JPEG_PAUSE_RESUME_INPUT)
02070     {
02071       hjpeg->Context &= (~JPEG_CONTEXT_PAUSE_INPUT);
02072       /*if the MDMA In is triggred with JPEG In FIFO Threshold flag
02073         then MDMA In buffer size is 32 bytes
02074 
02075         else (MDMA In is triggred with JPEG In FIFO not full flag)
02076         then MDMA In buffer size is 4 bytes
02077         */
02078       xfrSize = hjpeg->hdmain->Init.BufferTransferLength;
02079 
02080       if (xfrSize == 0UL)
02081       {
02082         hjpeg->ErrorCode |= HAL_JPEG_ERROR_DMA;
02083         hjpeg->State = HAL_JPEG_STATE_ERROR;
02084         return HAL_ERROR;
02085       }
02086       /*MDMA transfer size (BNDTR) must be a multiple of MDMA buffer size (TLEN)*/
02087       hjpeg->InDataLength = hjpeg->InDataLength - (hjpeg->InDataLength % xfrSize);
02088 
02089 
02090       if (hjpeg->InDataLength > 0UL)
02091       {
02092         /* Start DMA FIFO In transfer */
02093         if (HAL_MDMA_Start_IT(hjpeg->hdmain, (uint32_t)hjpeg->pJpegInBuffPtr, (uint32_t)&hjpeg->Instance->DIR,
02094                               hjpeg->InDataLength, 1) != HAL_OK)
02095         {
02096           hjpeg->ErrorCode |= HAL_JPEG_ERROR_DMA;
02097           hjpeg->State = HAL_JPEG_STATE_ERROR;
02098           return HAL_ERROR;
02099         }
02100       }
02101     }
02102     if ((XferSelection & JPEG_PAUSE_RESUME_OUTPUT) == JPEG_PAUSE_RESUME_OUTPUT)
02103     {
02104       hjpeg->Context &= (~JPEG_CONTEXT_PAUSE_OUTPUT);
02105 
02106       if ((hjpeg->Context & JPEG_CONTEXT_ENDING_DMA) != 0UL)
02107       {
02108         JPEG_DMA_PollResidualData(hjpeg);
02109       }
02110       else
02111       {
02112         /*if the MDMA Out is triggred with JPEG Out FIFO Threshold flag
02113           then MDMA out buffer size is 32 bytes
02114           else (MDMA Out is triggred with JPEG Out FIFO not empty flag)
02115           then MDMA buffer size is 4 bytes
02116           */
02117         xfrSize = hjpeg->hdmaout->Init.BufferTransferLength;
02118 
02119         if (xfrSize == 0UL)
02120         {
02121           hjpeg->ErrorCode |= HAL_JPEG_ERROR_DMA;
02122           hjpeg->State = HAL_JPEG_STATE_ERROR;
02123           return HAL_ERROR;
02124         }
02125         /*MDMA transfer size (BNDTR) must be a multiple of MDMA buffer size (TLEN)*/
02126         hjpeg->OutDataLength = hjpeg->OutDataLength - (hjpeg->OutDataLength % xfrSize);
02127 
02128         /* Start DMA FIFO Out transfer */
02129         if (HAL_MDMA_Start_IT(hjpeg->hdmaout, (uint32_t)&hjpeg->Instance->DOR, (uint32_t)hjpeg->pJpegOutBuffPtr,
02130                               hjpeg->OutDataLength, 1) != HAL_OK)
02131         {
02132           hjpeg->ErrorCode |= HAL_JPEG_ERROR_DMA;
02133           hjpeg->State = HAL_JPEG_STATE_ERROR;
02134           return HAL_ERROR;
02135         }
02136       }
02137 
02138     }
02139 
02140   }
02141   else if ((hjpeg->Context & JPEG_CONTEXT_METHOD_MASK) == JPEG_CONTEXT_IT)
02142   {
02143     if ((XferSelection & JPEG_PAUSE_RESUME_INPUT) == JPEG_PAUSE_RESUME_INPUT)
02144     {
02145       hjpeg->Context &= (~JPEG_CONTEXT_PAUSE_INPUT);
02146       mask |= (JPEG_IT_IFT | JPEG_IT_IFNF);
02147     }
02148     if ((XferSelection & JPEG_PAUSE_RESUME_OUTPUT) == JPEG_PAUSE_RESUME_OUTPUT)
02149     {
02150       hjpeg->Context &= (~JPEG_CONTEXT_PAUSE_OUTPUT);
02151       mask |= (JPEG_IT_OFT | JPEG_IT_OFNE | JPEG_IT_EOC);
02152     }
02153     __HAL_JPEG_ENABLE_IT(hjpeg, mask);
02154 
02155   }
02156   else
02157   {
02158     /* Nothing to do */
02159   }
02160 
02161   /* Return function status */
02162   return HAL_OK;
02163 }
02164 
02165 /**
02166   * @brief  Config Encoding/Decoding Input Buffer.
02167   * @param  hjpeg pointer to a JPEG_HandleTypeDef structure that contains
02168   *         the configuration information for JPEG module.
02169   * @param  pNewInputBuffer Pointer to the new input data buffer
02170   * @param  InDataLength Size in bytes of the new Input data buffer
02171   * @retval HAL status
02172   */
02173 void HAL_JPEG_ConfigInputBuffer(JPEG_HandleTypeDef *hjpeg, uint8_t *pNewInputBuffer, uint32_t InDataLength)
02174 {
02175   hjpeg->pJpegInBuffPtr =  pNewInputBuffer;
02176   hjpeg->InDataLength = InDataLength;
02177 }
02178 
02179 /**
02180   * @brief  Config Encoding/Decoding Output Buffer.
02181   * @param  hjpeg pointer to a JPEG_HandleTypeDef structure that contains
02182   *         the configuration information for JPEG module.
02183   * @param  pNewOutputBuffer Pointer to the new output data buffer
02184   * @param  OutDataLength Size in bytes of the new Output data buffer
02185   * @retval HAL status
02186   */
02187 void HAL_JPEG_ConfigOutputBuffer(JPEG_HandleTypeDef *hjpeg, uint8_t *pNewOutputBuffer, uint32_t OutDataLength)
02188 {
02189   hjpeg->pJpegOutBuffPtr = pNewOutputBuffer;
02190   hjpeg->OutDataLength = OutDataLength;
02191 }
02192 
02193 /**
02194   * @brief  Aborts the JPEG Encoding/Decoding.
02195   * @param  hjpeg pointer to a JPEG_HandleTypeDef structure that contains
02196   *         the configuration information for JPEG module
02197   * @retval HAL status
02198   */
02199 HAL_StatusTypeDef HAL_JPEG_Abort(JPEG_HandleTypeDef *hjpeg)
02200 {
02201   uint32_t tickstart;
02202   uint32_t tmpContext;
02203   tmpContext = hjpeg->Context;
02204 
02205   /*Reset the Context operation and method*/
02206   hjpeg->Context &= ~(JPEG_CONTEXT_OPERATION_MASK | JPEG_CONTEXT_METHOD_MASK | JPEG_CONTEXT_ENDING_DMA);
02207 
02208   if ((tmpContext & JPEG_CONTEXT_METHOD_MASK) == JPEG_CONTEXT_DMA)
02209   {
02210     /* Stop the DMA In/out Xfer*/
02211     if (HAL_MDMA_Abort(hjpeg->hdmaout) != HAL_OK)
02212     {
02213       if (hjpeg->hdmaout->ErrorCode == HAL_MDMA_ERROR_TIMEOUT)
02214       {
02215         hjpeg->ErrorCode |= HAL_JPEG_ERROR_DMA;
02216       }
02217     }
02218     if (HAL_MDMA_Abort(hjpeg->hdmain) != HAL_OK)
02219     {
02220       if (hjpeg->hdmain->ErrorCode == HAL_MDMA_ERROR_TIMEOUT)
02221       {
02222         hjpeg->ErrorCode |= HAL_JPEG_ERROR_DMA;
02223       }
02224     }
02225 
02226   }
02227 
02228   /* Stop the JPEG encoding/decoding process*/
02229   hjpeg->Instance->CONFR0 &=  ~JPEG_CONFR0_START;
02230 
02231   /* Get tick */
02232   tickstart = HAL_GetTick();
02233 
02234   /* Check if the JPEG Codec is effectively disabled */
02235   while (__HAL_JPEG_GET_FLAG(hjpeg, JPEG_FLAG_COF) != 0UL)
02236   {
02237     /* Check for the Timeout */
02238     if ((HAL_GetTick() - tickstart) > JPEG_TIMEOUT_VALUE)
02239     {
02240       /* Update error code */
02241       hjpeg->ErrorCode |= HAL_JPEG_ERROR_TIMEOUT;
02242 
02243       /* Change the DMA state */
02244       hjpeg->State = HAL_JPEG_STATE_ERROR;
02245       break;
02246     }
02247   }
02248 
02249   /* Disable All Interrupts */
02250   __HAL_JPEG_DISABLE_IT(hjpeg, JPEG_INTERRUPT_MASK);
02251 
02252 
02253   /* Flush input and output FIFOs*/
02254   hjpeg->Instance->CR |= JPEG_CR_IFF;
02255   hjpeg->Instance->CR |= JPEG_CR_OFF;
02256 
02257   /* Clear all flags */
02258   __HAL_JPEG_CLEAR_FLAG(hjpeg, JPEG_FLAG_ALL);
02259 
02260   /* Reset JpegInCount and JpegOutCount */
02261   hjpeg->JpegInCount = 0;
02262   hjpeg->JpegOutCount = 0;
02263 
02264   /*Reset the Context Pause*/
02265   hjpeg->Context &= ~(JPEG_CONTEXT_PAUSE_INPUT | JPEG_CONTEXT_PAUSE_OUTPUT);
02266 
02267   /* Change the DMA state*/
02268   if (hjpeg->ErrorCode != HAL_JPEG_ERROR_NONE)
02269   {
02270     hjpeg->State = HAL_JPEG_STATE_ERROR;
02271     /* Process Unlocked */
02272     __HAL_UNLOCK(hjpeg);
02273     /* Return function status */
02274     return HAL_ERROR;
02275   }
02276   else
02277   {
02278     hjpeg->State = HAL_JPEG_STATE_READY;
02279     /* Process Unlocked */
02280     __HAL_UNLOCK(hjpeg);
02281     /* Return function status */
02282     return HAL_OK;
02283   }
02284 
02285 }
02286 
02287 
02288 /**
02289   * @}
02290   */
02291 
02292 /** @defgroup JPEG_Exported_Functions_Group4 JPEG Decode/Encode callback functions
02293   *  @brief   JPEG process callback functions.
02294   *
02295 @verbatim
02296   ==============================================================================
02297               #####  JPEG Decode and Encode callback functions  #####
02298   ==============================================================================
02299     [..]  This section provides callback functions:
02300       (+) HAL_JPEG_InfoReadyCallback()  : Decoding JPEG Info ready callback
02301       (+) HAL_JPEG_EncodeCpltCallback() : Encoding complete callback.
02302       (+) HAL_JPEG_DecodeCpltCallback() : Decoding complete callback.
02303       (+) HAL_JPEG_ErrorCallback()      : JPEG error callback.
02304       (+) HAL_JPEG_GetDataCallback()    : Get New Data chunk callback.
02305       (+) HAL_JPEG_DataReadyCallback()  : Decoded/Encoded Data ready  callback.
02306 
02307 @endverbatim
02308   * @{
02309   */
02310 
02311 /**
02312   * @brief  Decoding JPEG Info ready callback.
02313   * @param  hjpeg pointer to a JPEG_HandleTypeDef structure that contains
02314   *         the configuration information for JPEG module
02315   * @param  pInfo pointer to a JPEG_ConfTypeDef structure that contains
02316   *         The JPEG decoded header information
02317   * @retval None
02318   */
02319 __weak void HAL_JPEG_InfoReadyCallback(JPEG_HandleTypeDef *hjpeg, JPEG_ConfTypeDef *pInfo)
02320 {
02321   /* Prevent unused argument(s) compilation warning */
02322   UNUSED(hjpeg);
02323   UNUSED(pInfo);
02324 
02325   /* NOTE : This function Should not be modified, when the callback is needed,
02326             the HAL_JPEG_HeaderParsingCpltCallback could be implemented in the user file
02327    */
02328 }
02329 
02330 /**
02331   * @brief  Encoding complete callback.
02332   * @param  hjpeg pointer to a JPEG_HandleTypeDef structure that contains
02333   *         the configuration information for JPEG module
02334   * @retval None
02335   */
02336 __weak void HAL_JPEG_EncodeCpltCallback(JPEG_HandleTypeDef *hjpeg)
02337 {
02338   /* Prevent unused argument(s) compilation warning */
02339   UNUSED(hjpeg);
02340 
02341   /* NOTE : This function Should not be modified, when the callback is needed,
02342             the HAL_JPEG_EncodeCpltCallback could be implemented in the user file
02343    */
02344 }
02345 
02346 /**
02347   * @brief  Decoding complete callback.
02348   * @param  hjpeg pointer to a JPEG_HandleTypeDef structure that contains
02349   *         the configuration information for JPEG module
02350   * @retval None
02351   */
02352 __weak void HAL_JPEG_DecodeCpltCallback(JPEG_HandleTypeDef *hjpeg)
02353 {
02354   /* Prevent unused argument(s) compilation warning */
02355   UNUSED(hjpeg);
02356 
02357   /* NOTE : This function Should not be modified, when the callback is needed,
02358             the HAL_JPEG_EncodeCpltCallback could be implemented in the user file
02359    */
02360 }
02361 
02362 /**
02363   * @brief  JPEG error callback.
02364   * @param  hjpeg pointer to a JPEG_HandleTypeDef structure that contains
02365   *         the configuration information for JPEG module
02366   * @retval None
02367   */
02368 __weak void HAL_JPEG_ErrorCallback(JPEG_HandleTypeDef *hjpeg)
02369 {
02370   /* Prevent unused argument(s) compilation warning */
02371   UNUSED(hjpeg);
02372 
02373   /* NOTE : This function Should not be modified, when the callback is needed,
02374             the HAL_JPEG_ErrorCallback could be implemented in the user file
02375    */
02376 }
02377 
02378 /**
02379   * @brief  Get New Data chunk callback.
02380   * @param  hjpeg pointer to a JPEG_HandleTypeDef structure that contains
02381   *         the configuration information for JPEG module
02382   * @param  NbDecodedData Number of consummed data in the previous chunk in bytes
02383   * @retval None
02384   */
02385 __weak void HAL_JPEG_GetDataCallback(JPEG_HandleTypeDef *hjpeg, uint32_t NbDecodedData)
02386 {
02387   /* Prevent unused argument(s) compilation warning */
02388   UNUSED(hjpeg);
02389   UNUSED(NbDecodedData);
02390 
02391   /* NOTE : This function Should not be modified, when the callback is needed,
02392             the HAL_JPEG_GetDataCallback could be implemented in the user file
02393    */
02394 }
02395 
02396 /**
02397   * @brief  Decoded/Encoded Data ready  callback.
02398   * @param  hjpeg pointer to a JPEG_HandleTypeDef structure that contains
02399   *         the configuration information for JPEG module
02400   * @param  pDataOut pointer to the output data buffer
02401   * @param  OutDataLength number in bytes of data available in the specified output buffer
02402   * @retval None
02403   */
02404 __weak void HAL_JPEG_DataReadyCallback(JPEG_HandleTypeDef *hjpeg, uint8_t *pDataOut, uint32_t OutDataLength)
02405 {
02406   /* Prevent unused argument(s) compilation warning */
02407   UNUSED(hjpeg);
02408   UNUSED(pDataOut);
02409   UNUSED(OutDataLength);
02410 
02411   /* NOTE : This function Should not be modified, when the callback is needed,
02412             the HAL_JPEG_DataReadyCallback could be implemented in the user file
02413    */
02414 }
02415 
02416 /**
02417   * @}
02418   */
02419 
02420 
02421 /** @defgroup JPEG_Exported_Functions_Group5 JPEG IRQ handler management
02422   *  @brief   JPEG IRQ handler.
02423   *
02424 @verbatim
02425   ==============================================================================
02426                 ##### JPEG IRQ handler management #####
02427   ==============================================================================
02428     [..]  This section provides JPEG IRQ handler function.
02429       (+) HAL_JPEG_IRQHandler()  : handles JPEG interrupt request
02430 
02431 @endverbatim
02432   * @{
02433   */
02434 
02435 /**
02436   * @brief  This function handles JPEG interrupt request.
02437   * @param  hjpeg pointer to a JPEG_HandleTypeDef structure that contains
02438   *         the configuration information for JPEG module
02439   * @retval None
02440   */
02441 void HAL_JPEG_IRQHandler(JPEG_HandleTypeDef *hjpeg)
02442 {
02443   switch (hjpeg->State)
02444   {
02445     case HAL_JPEG_STATE_BUSY_ENCODING:
02446     case HAL_JPEG_STATE_BUSY_DECODING:
02447       /* continue JPEG data encoding/Decoding*/
02448       /* JPEG data processing : In/Out FIFO transfer*/
02449       if ((hjpeg->Context & JPEG_CONTEXT_METHOD_MASK) == JPEG_CONTEXT_IT)
02450       {
02451         (void) JPEG_Process(hjpeg);
02452       }
02453       else if ((hjpeg->Context & JPEG_CONTEXT_METHOD_MASK) == JPEG_CONTEXT_DMA)
02454       {
02455         JPEG_DMA_ContinueProcess(hjpeg);
02456       }
02457       else
02458       {
02459         /* Nothing to do */
02460       }
02461       break;
02462 
02463     default:
02464       break;
02465   }
02466 }
02467 
02468 /**
02469   * @}
02470   */
02471 
02472 /** @defgroup JPEG_Exported_Functions_Group6 Peripheral State functions
02473   *  @brief   Peripheral State functions.
02474   *
02475 @verbatim
02476   ==============================================================================
02477                     ##### Peripheral State and Error functions #####
02478   ==============================================================================
02479     [..]  This section provides JPEG State and Errors function.
02480       (+) HAL_JPEG_GetState()  : permits to get in run-time the JPEG state.
02481       (+) HAL_JPEG_GetError()  : Returns the JPEG error code if any.
02482 
02483 @endverbatim
02484   * @{
02485   */
02486 
02487 /**
02488   * @brief  Returns the JPEG state.
02489   * @param  hjpeg pointer to a JPEG_HandleTypeDef structure that contains
02490   *         the configuration information for JPEG module
02491   * @retval JPEG state
02492   */
02493 HAL_JPEG_STATETypeDef HAL_JPEG_GetState(JPEG_HandleTypeDef *hjpeg)
02494 {
02495   return hjpeg->State;
02496 }
02497 
02498 /**
02499   * @brief  Return the JPEG error code
02500   * @param  hjpeg  pointer to a JPEG_HandleTypeDef structure that contains
02501   *              the configuration information for the specified JPEG.
02502   * @retval JPEG Error Code
02503   */
02504 uint32_t HAL_JPEG_GetError(JPEG_HandleTypeDef *hjpeg)
02505 {
02506   return hjpeg->ErrorCode;
02507 }
02508 
02509 /**
02510   * @}
02511   */
02512 
02513 /**
02514   * @}
02515   */
02516 
02517 
02518 /** @addtogroup JPEG_Private_Functions
02519   * @{
02520   */
02521 
02522 /**
02523   * @brief  Generates Huffman sizes/Codes Table from Bits/vals Table
02524   * @param  Bits pointer to bits table
02525   * @param  Huffsize pointer to sizes table
02526   * @param  Huffcode pointer to codes table
02527   * @param  LastK pointer to last Coeff (table dimension)
02528   * @retval HAL status
02529   */
02530 static HAL_StatusTypeDef JPEG_Bits_To_SizeCodes(uint8_t *Bits, uint8_t *Huffsize, uint32_t *Huffcode, uint32_t *LastK)
02531 {
02532   uint32_t i;
02533   uint32_t p;
02534   uint32_t l;
02535   uint32_t code;
02536   uint32_t si;
02537 
02538   /* Figure C.1: Generation of table of Huffman code sizes */
02539   p = 0;
02540   for (l = 0; l < 16UL; l++)
02541   {
02542     i = (uint32_t)Bits[l];
02543     if ((p + i) > 256UL)
02544     {
02545       /* check for table overflow */
02546       return HAL_ERROR;
02547     }
02548     while (i != 0UL)
02549     {
02550       Huffsize[p] = (uint8_t) l + 1U;
02551       p++;
02552       i--;
02553     }
02554   }
02555   Huffsize[p] = 0;
02556   *LastK = p;
02557 
02558   /* Figure C.2: Generation of table of Huffman codes */
02559   code = 0;
02560   si = Huffsize[0];
02561   p = 0;
02562   while (Huffsize[p] != 0U)
02563   {
02564     while (((uint32_t) Huffsize[p]) == si)
02565     {
02566       Huffcode[p] = code;
02567       p++;
02568       code++;
02569     }
02570     /* code must fit in "size" bits (si), no code is allowed to be all ones*/
02571     if(si > 31UL)
02572     {
02573       return HAL_ERROR;
02574     }
02575     if (((uint32_t) code) >= (((uint32_t) 1) << si))
02576     {
02577       return HAL_ERROR;
02578     }
02579     code <<= 1;
02580     si++;
02581   }
02582   /* Return function status */
02583   return HAL_OK;
02584 }
02585 
02586 /**
02587   * @brief  Transform a Bits/Vals AC Huffman table to sizes/Codes huffman Table
02588   *         that can programmed to the JPEG encoder registers
02589   * @param  AC_BitsValsTable pointer to AC huffman bits/vals table
02590   * @param  AC_SizeCodesTable pointer to AC huffman Sizes/Codes table
02591   * @retval HAL status
02592   */
02593 static HAL_StatusTypeDef JPEG_ACHuff_BitsVals_To_SizeCodes(JPEG_ACHuffTableTypeDef *AC_BitsValsTable,
02594                                                            JPEG_AC_HuffCodeTableTypeDef *AC_SizeCodesTable)
02595 {
02596   HAL_StatusTypeDef error;
02597   uint8_t huffsize[257];
02598   uint32_t huffcode[257];
02599   uint32_t k;
02600   uint32_t l, lsb, msb;
02601   uint32_t lastK;
02602 
02603   error = JPEG_Bits_To_SizeCodes(AC_BitsValsTable->Bits, huffsize, huffcode, &lastK);
02604   if (error != HAL_OK)
02605   {
02606     return  error;
02607   }
02608 
02609   /* Figure C.3: Ordering procedure for encoding procedure code tables */
02610   k = 0;
02611 
02612   while (k < lastK)
02613   {
02614     l = AC_BitsValsTable->HuffVal[k];
02615     if (l == 0UL)
02616     {
02617       l = 160; /*l = 0x00 EOB code*/
02618     }
02619     else if (l == 0xF0UL) /* l = 0xF0 ZRL code*/
02620     {
02621       l = 161;
02622     }
02623     else
02624     {
02625       msb = (l & 0xF0UL) >> 4;
02626       lsb = (l & 0x0FUL);
02627       l = (msb * 10UL) + lsb - 1UL;
02628     }
02629     if (l >= JPEG_AC_HUFF_TABLE_SIZE)
02630     {
02631       return HAL_ERROR; /* Huffman Table overflow error*/
02632     }
02633     else
02634     {
02635       AC_SizeCodesTable->HuffmanCode[l] = huffcode[k];
02636       AC_SizeCodesTable->CodeLength[l] = huffsize[k] - 1U;
02637       k++;
02638     }
02639   }
02640 
02641   /* Return function status */
02642   return HAL_OK;
02643 }
02644 
02645 /**
02646   * @brief  Transform a Bits/Vals DC Huffman table to sizes/Codes huffman Table
02647   *         that can programmed to the JPEG encoder registers
02648   * @param  DC_BitsValsTable pointer to DC huffman bits/vals table
02649   * @param  DC_SizeCodesTable pointer to DC huffman Sizes/Codes table
02650   * @retval HAL status
02651   */
02652 static HAL_StatusTypeDef JPEG_DCHuff_BitsVals_To_SizeCodes(JPEG_DCHuffTableTypeDef *DC_BitsValsTable,
02653                                                            JPEG_DC_HuffCodeTableTypeDef *DC_SizeCodesTable)
02654 {
02655   HAL_StatusTypeDef error;
02656 
02657   uint32_t k;
02658   uint32_t l;
02659   uint32_t lastK;
02660   uint8_t huffsize[257];
02661   uint32_t huffcode[257];
02662   error = JPEG_Bits_To_SizeCodes(DC_BitsValsTable->Bits, huffsize, huffcode, &lastK);
02663   if (error != HAL_OK)
02664   {
02665     return  error;
02666   }
02667   /* Figure C.3: ordering procedure for encoding procedure code tables */
02668   k = 0;
02669 
02670   while (k < lastK)
02671   {
02672     l = DC_BitsValsTable->HuffVal[k];
02673     if (l >= JPEG_DC_HUFF_TABLE_SIZE)
02674     {
02675       return HAL_ERROR; /* Huffman Table overflow error*/
02676     }
02677     else
02678     {
02679       DC_SizeCodesTable->HuffmanCode[l] = huffcode[k];
02680       DC_SizeCodesTable->CodeLength[l] = huffsize[k] - 1U;
02681       k++;
02682     }
02683   }
02684 
02685   /* Return function status */
02686   return HAL_OK;
02687 }
02688 
02689 /**
02690   * @brief  Set the JPEG register with an DC huffman table at the given DC table address
02691   * @param  hjpeg pointer to a JPEG_HandleTypeDef structure that contains
02692   *         the configuration information for JPEG module
02693   * @param  HuffTableDC pointer to DC huffman table
02694   * @param  DCTableAddress Encoder DC huffman table address it could be HUFFENC_DC0 or HUFFENC_DC1.
02695   * @retval HAL status
02696   */
02697 static HAL_StatusTypeDef JPEG_Set_HuffDC_Mem(JPEG_HandleTypeDef *hjpeg, JPEG_DCHuffTableTypeDef *HuffTableDC,
02698                                              const __IO uint32_t *DCTableAddress)
02699 {
02700   HAL_StatusTypeDef error;
02701   JPEG_DC_HuffCodeTableTypeDef dcSizeCodesTable;
02702   uint32_t i;
02703   uint32_t lsb;
02704   uint32_t msb;
02705   __IO uint32_t *address, *addressDef;
02706 
02707   if (DCTableAddress == (hjpeg->Instance->HUFFENC_DC0))
02708   {
02709     address = (hjpeg->Instance->HUFFENC_DC0 + (JPEG_DC_HUFF_TABLE_SIZE / 2UL));
02710   }
02711   else if (DCTableAddress == (hjpeg->Instance->HUFFENC_DC1))
02712   {
02713     address = (hjpeg->Instance->HUFFENC_DC1 + (JPEG_DC_HUFF_TABLE_SIZE / 2UL));
02714   }
02715   else
02716   {
02717     return HAL_ERROR;
02718   }
02719 
02720   if (HuffTableDC != NULL)
02721   {
02722     error = JPEG_DCHuff_BitsVals_To_SizeCodes(HuffTableDC, &dcSizeCodesTable);
02723     if (error != HAL_OK)
02724     {
02725       return  error;
02726     }
02727     addressDef = address;
02728     *addressDef = 0x0FFF0FFF;
02729     addressDef++;
02730     *addressDef = 0x0FFF0FFF;
02731 
02732     i = JPEG_DC_HUFF_TABLE_SIZE;
02733     while (i > 1UL)
02734     {
02735       i--;
02736       address --;
02737       msb = ((uint32_t)(((uint32_t)dcSizeCodesTable.CodeLength[i] & 0xFU) << 8)) | ((uint32_t)dcSizeCodesTable.HuffmanCode[i] &
02738                                                                                    0xFFUL);
02739       i--;
02740       lsb = ((uint32_t)(((uint32_t)dcSizeCodesTable.CodeLength[i] & 0xFU) << 8)) | ((uint32_t)dcSizeCodesTable.HuffmanCode[i] &
02741                                                                                    0xFFUL);
02742 
02743       *address = lsb | (msb << 16);
02744     }
02745   }
02746 
02747   /* Return function status */
02748   return HAL_OK;
02749 }
02750 
02751 /**
02752   * @brief  Set the JPEG register with an AC huffman table at the given AC table address
02753   * @param  hjpeg pointer to a JPEG_HandleTypeDef structure that contains
02754   *         the configuration information for JPEG module
02755   * @param  HuffTableAC pointer to AC huffman table
02756   * @param  ACTableAddress Encoder AC huffman table address it could be HUFFENC_AC0 or HUFFENC_AC1.
02757   * @retval HAL status
02758   */
02759 static HAL_StatusTypeDef JPEG_Set_HuffAC_Mem(JPEG_HandleTypeDef *hjpeg, JPEG_ACHuffTableTypeDef *HuffTableAC,
02760                                              const __IO uint32_t *ACTableAddress)
02761 {
02762   HAL_StatusTypeDef error;
02763   JPEG_AC_HuffCodeTableTypeDef acSizeCodesTable;
02764   uint32_t i, lsb, msb;
02765   __IO uint32_t *address, *addressDef;
02766 
02767   if (ACTableAddress == (hjpeg->Instance->HUFFENC_AC0))
02768   {
02769     address = (hjpeg->Instance->HUFFENC_AC0 + (JPEG_AC_HUFF_TABLE_SIZE / 2UL));
02770   }
02771   else if (ACTableAddress == (hjpeg->Instance->HUFFENC_AC1))
02772   {
02773     address = (hjpeg->Instance->HUFFENC_AC1 + (JPEG_AC_HUFF_TABLE_SIZE / 2UL));
02774   }
02775   else
02776   {
02777     return HAL_ERROR;
02778   }
02779 
02780   if (HuffTableAC != NULL)
02781   {
02782     error = JPEG_ACHuff_BitsVals_To_SizeCodes(HuffTableAC, &acSizeCodesTable);
02783     if (error != HAL_OK)
02784     {
02785       return  error;
02786     }
02787     /* Default values settings: 162:167 FFFh , 168:175 FD0h_FD7h */
02788     /* Locations 162:175 of each AC table contain information used internally by the core */
02789 
02790     addressDef = address;
02791     for (i = 0; i < 3UL; i++)
02792     {
02793       *addressDef = 0x0FFF0FFF;
02794       addressDef++;
02795     }
02796     *addressDef = 0x0FD10FD0;
02797     addressDef++;
02798     *addressDef = 0x0FD30FD2;
02799     addressDef++;
02800     *addressDef = 0x0FD50FD4;
02801     addressDef++;
02802     *addressDef = 0x0FD70FD6;
02803     /* end of Locations 162:175  */
02804 
02805 
02806     i = JPEG_AC_HUFF_TABLE_SIZE;
02807     while (i > 1UL)
02808     {
02809       i--;
02810       address--;
02811       msb = ((uint32_t)(((uint32_t)acSizeCodesTable.CodeLength[i] & 0xFU) << 8)) | ((uint32_t)acSizeCodesTable.HuffmanCode[i] &
02812                                                                                    0xFFUL);
02813       i--;
02814       lsb = ((uint32_t)(((uint32_t)acSizeCodesTable.CodeLength[i] & 0xFU) << 8)) | ((uint32_t)acSizeCodesTable.HuffmanCode[i] &
02815                                                                                    0xFFUL);
02816 
02817       *address = lsb | (msb << 16);
02818     }
02819   }
02820 
02821   /* Return function status */
02822   return HAL_OK;
02823 }
02824 
02825 /**
02826   * @brief  Configure the JPEG encoder register huffman tables to used during
02827   *         the encdoing operation
02828   * @param  hjpeg pointer to a JPEG_HandleTypeDef structure that contains
02829   *         the configuration information for JPEG module
02830   * @retval None
02831   */
02832 static HAL_StatusTypeDef JPEG_Set_HuffEnc_Mem(JPEG_HandleTypeDef *hjpeg)
02833 {
02834   HAL_StatusTypeDef error;
02835 
02836   JPEG_Set_Huff_DHTMem(hjpeg);
02837   error = JPEG_Set_HuffAC_Mem(hjpeg, (JPEG_ACHuffTableTypeDef *)(uint32_t)&JPEG_ACLUM_HuffTable,
02838                               (hjpeg->Instance->HUFFENC_AC0));
02839   if (error != HAL_OK)
02840   {
02841     return  error;
02842   }
02843 
02844   error = JPEG_Set_HuffAC_Mem(hjpeg, (JPEG_ACHuffTableTypeDef *)(uint32_t)&JPEG_ACCHROM_HuffTable,
02845                               (hjpeg->Instance->HUFFENC_AC1));
02846   if (error != HAL_OK)
02847   {
02848     return  error;
02849   }
02850 
02851   error = JPEG_Set_HuffDC_Mem(hjpeg, (JPEG_DCHuffTableTypeDef *)(uint32_t)&JPEG_DCLUM_HuffTable,
02852                               hjpeg->Instance->HUFFENC_DC0);
02853   if (error != HAL_OK)
02854   {
02855     return  error;
02856   }
02857 
02858   error = JPEG_Set_HuffDC_Mem(hjpeg, (JPEG_DCHuffTableTypeDef *)(uint32_t)&JPEG_DCCHROM_HuffTable,
02859                               hjpeg->Instance->HUFFENC_DC1);
02860   if (error != HAL_OK)
02861   {
02862     return  error;
02863   }
02864   /* Return function status */
02865   return HAL_OK;
02866 }
02867 
02868 /**
02869   * @brief  Configure the JPEG register huffman tables to be included in the JPEG
02870   *         file header (used for encoding only)
02871   * @param  hjpeg pointer to a JPEG_HandleTypeDef structure that contains
02872   *         the configuration information for JPEG module
02873   * @retval None
02874   */
02875 static void JPEG_Set_Huff_DHTMem(JPEG_HandleTypeDef *hjpeg)
02876 {
02877   JPEG_ACHuffTableTypeDef *HuffTableAC0 = (JPEG_ACHuffTableTypeDef *)(uint32_t)&JPEG_ACLUM_HuffTable;
02878   JPEG_ACHuffTableTypeDef *HuffTableAC1 = (JPEG_ACHuffTableTypeDef *)(uint32_t)&JPEG_ACCHROM_HuffTable;
02879   JPEG_DCHuffTableTypeDef *HuffTableDC0 = (JPEG_DCHuffTableTypeDef *)(uint32_t)&JPEG_DCLUM_HuffTable;
02880   JPEG_DCHuffTableTypeDef *HuffTableDC1 = (JPEG_DCHuffTableTypeDef *)(uint32_t)&JPEG_DCCHROM_HuffTable;
02881   uint32_t value, index;
02882   __IO uint32_t *address;
02883 
02884   /* DC0 Huffman Table : BITS*/
02885   /* DC0 BITS is a 16 Bytes table i.e 4x32bits words from DHTMEM base address to DHTMEM + 3*/
02886   address = (hjpeg->Instance->DHTMEM + 3);
02887   index = 16;
02888   while (index > 3UL)
02889   {
02890 
02891     *address = (((uint32_t)HuffTableDC0->Bits[index - 1UL] & 0xFFUL) << 24) |
02892                (((uint32_t)HuffTableDC0->Bits[index - 2UL] & 0xFFUL) << 16) |
02893                (((uint32_t)HuffTableDC0->Bits[index - 3UL] & 0xFFUL) << 8) |
02894                ((uint32_t)HuffTableDC0->Bits[index - 4UL] & 0xFFUL);
02895     address--;
02896     index -= 4UL;
02897 
02898   }
02899   /* DC0 Huffman Table : Val*/
02900   /* DC0 VALS is a 12 Bytes table i.e 3x32bits words from DHTMEM base address +4 to DHTMEM + 6 */
02901   address = (hjpeg->Instance->DHTMEM + 6);
02902   index = 12;
02903   while (index > 3UL)
02904   {
02905     *address = (((uint32_t)HuffTableDC0->HuffVal[index - 1UL] & 0xFFUL) << 24) |
02906                (((uint32_t)HuffTableDC0->HuffVal[index - 2UL] & 0xFFUL) << 16) |
02907                (((uint32_t)HuffTableDC0->HuffVal[index - 3UL] & 0xFFUL) << 8) |
02908                ((uint32_t)HuffTableDC0->HuffVal[index - 4UL] & 0xFFUL);
02909     address--;
02910     index -= 4UL;
02911   }
02912 
02913   /* AC0 Huffman Table : BITS*/
02914   /* AC0 BITS is a 16 Bytes table i.e 4x32bits words from DHTMEM base address + 7 to DHTMEM + 10*/
02915   address = (hjpeg->Instance->DHTMEM + 10UL);
02916   index = 16;
02917   while (index > 3UL)
02918   {
02919 
02920     *address = (((uint32_t)HuffTableAC0->Bits[index - 1UL] & 0xFFUL) << 24) |
02921                (((uint32_t)HuffTableAC0->Bits[index - 2UL] & 0xFFUL) << 16) |
02922                (((uint32_t)HuffTableAC0->Bits[index - 3UL] & 0xFFUL) << 8) |
02923                ((uint32_t)HuffTableAC0->Bits[index - 4UL] & 0xFFUL);
02924     address--;
02925     index -= 4UL;
02926 
02927   }
02928   /* AC0 Huffman Table : Val*/
02929   /* AC0 VALS is a 162 Bytes table i.e 41x32bits words from DHTMEM base address + 11 to DHTMEM + 51 */
02930   /* only Byte 0 and Byte 1 of the last word (@ DHTMEM + 51) belong to AC0 VALS table */
02931   address = (hjpeg->Instance->DHTMEM + 51);
02932   value = *address & 0xFFFF0000U;
02933   value = value | (((uint32_t)HuffTableAC0->HuffVal[161] & 0xFFUL) << 8) | ((uint32_t)HuffTableAC0->HuffVal[160] & 0xFFUL);
02934   *address = value;
02935 
02936   /*continue setting 160 AC0 huffman values */
02937   address--; /* address = hjpeg->Instance->DHTMEM + 50*/
02938   index = 160;
02939   while (index > 3UL)
02940   {
02941     *address = (((uint32_t)HuffTableAC0->HuffVal[index - 1UL] & 0xFFUL) << 24) |
02942                (((uint32_t)HuffTableAC0->HuffVal[index - 2UL] & 0xFFUL) << 16) |
02943                (((uint32_t)HuffTableAC0->HuffVal[index - 3UL] & 0xFFUL) << 8) |
02944                ((uint32_t)HuffTableAC0->HuffVal[index - 4UL] & 0xFFUL);
02945     address--;
02946     index -= 4UL;
02947   }
02948 
02949   /* DC1 Huffman Table : BITS*/
02950   /* DC1 BITS is a 16 Bytes table i.e 4x32bits words from DHTMEM + 51 base address to DHTMEM + 55*/
02951   /* only Byte 2 and Byte 3 of the first word (@ DHTMEM + 51) belong to DC1 Bits table */
02952   address = (hjpeg->Instance->DHTMEM + 51);
02953   value = *address & 0x0000FFFFU;
02954   value = value | (((uint32_t)HuffTableDC1->Bits[1] & 0xFFUL) << 24) | (((uint32_t)HuffTableDC1->Bits[0] & 0xFFUL) << 16);
02955   *address = value;
02956 
02957   /* only Byte 0 and Byte 1 of the last word (@ DHTMEM + 55) belong to DC1 Bits table */
02958   address = (hjpeg->Instance->DHTMEM + 55);
02959   value = *address & 0xFFFF0000U;
02960   value = value | (((uint32_t)HuffTableDC1->Bits[15] & 0xFFUL) << 8) | ((uint32_t)HuffTableDC1->Bits[14] & 0xFFUL);
02961   *address = value;
02962 
02963   /*continue setting 12 DC1 huffman Bits from DHTMEM + 54 down to DHTMEM + 52*/
02964   address--;
02965   index = 12;
02966   while (index > 3UL)
02967   {
02968 
02969     *address = (((uint32_t)HuffTableDC1->Bits[index + 1UL] & 0xFFUL) << 24) |
02970                (((uint32_t)HuffTableDC1->Bits[index] & 0xFFUL) << 16) |
02971                (((uint32_t)HuffTableDC1->Bits[index - 1UL] & 0xFFUL) << 8) |
02972                ((uint32_t)HuffTableDC1->Bits[index - 2UL] & 0xFFUL);
02973     address--;
02974     index -= 4UL;
02975 
02976   }
02977   /* DC1 Huffman Table : Val*/
02978   /* DC1 VALS is a 12 Bytes table i.e 3x32bits words from DHTMEM base address +55 to DHTMEM + 58 */
02979   /* only Byte 2 and Byte 3 of the first word (@ DHTMEM + 55) belong to DC1 Val table */
02980   address = (hjpeg->Instance->DHTMEM + 55);
02981   value = *address & 0x0000FFFFUL;
02982   value = value | (((uint32_t)HuffTableDC1->HuffVal[1] & 0xFFUL) << 24) | (((uint32_t)HuffTableDC1->HuffVal[0] & 0xFFUL) <<
02983                                                                          16);
02984   *address = value;
02985 
02986   /* only Byte 0 and Byte 1 of the last word (@ DHTMEM + 58) belong to DC1 Val table */
02987   address = (hjpeg->Instance->DHTMEM + 58);
02988   value = *address & 0xFFFF0000UL;
02989   value = value | (((uint32_t)HuffTableDC1->HuffVal[11] & 0xFFUL) << 8) | ((uint32_t)HuffTableDC1->HuffVal[10] & 0xFFUL);
02990   *address = value;
02991 
02992   /*continue setting 8 DC1 huffman val from DHTMEM + 57 down to DHTMEM + 56*/
02993   address--;
02994   index = 8;
02995   while (index > 3UL)
02996   {
02997     *address = (((uint32_t)HuffTableDC1->HuffVal[index + 1UL] & 0xFFUL) << 24) |
02998                (((uint32_t)HuffTableDC1->HuffVal[index] & 0xFFUL) << 16) |
02999                (((uint32_t)HuffTableDC1->HuffVal[index - 1UL] & 0xFFUL) << 8) |
03000                ((uint32_t)HuffTableDC1->HuffVal[index - 2UL] & 0xFFUL);
03001     address--;
03002     index -= 4UL;
03003   }
03004 
03005   /* AC1 Huffman Table : BITS*/
03006   /* AC1 BITS is a 16 Bytes table i.e 4x32bits words from DHTMEM base address + 58 to DHTMEM + 62*/
03007   /* only Byte 2 and Byte 3 of the first word (@ DHTMEM + 58) belong to AC1 Bits table */
03008   address = (hjpeg->Instance->DHTMEM + 58);
03009   value = *address & 0x0000FFFFU;
03010   value = value | (((uint32_t)HuffTableAC1->Bits[1] & 0xFFUL) << 24) | (((uint32_t)HuffTableAC1->Bits[0] & 0xFFUL) << 16);
03011   *address = value;
03012 
03013   /* only Byte 0 and Byte 1 of the last word (@ DHTMEM + 62) belong to Bits Val table */
03014   address = (hjpeg->Instance->DHTMEM + 62);
03015   value = *address & 0xFFFF0000U;
03016   value = value | (((uint32_t)HuffTableAC1->Bits[15] & 0xFFUL) << 8) | ((uint32_t)HuffTableAC1->Bits[14] & 0xFFUL);
03017   *address = value;
03018 
03019   /*continue setting 12 AC1 huffman Bits from DHTMEM + 61 down to DHTMEM + 59*/
03020   address--;
03021   index = 12;
03022   while (index > 3UL)
03023   {
03024 
03025     *address = (((uint32_t)HuffTableAC1->Bits[index + 1UL] & 0xFFUL) << 24) |
03026                (((uint32_t)HuffTableAC1->Bits[index] & 0xFFUL) << 16) |
03027                (((uint32_t)HuffTableAC1->Bits[index - 1UL] & 0xFFUL) << 8) |
03028                ((uint32_t)HuffTableAC1->Bits[index - 2UL] & 0xFFUL);
03029     address--;
03030     index -= 4UL;
03031 
03032   }
03033   /* AC1 Huffman Table : Val*/
03034   /* AC1 VALS is a 162 Bytes table i.e 41x32bits words from DHTMEM base address + 62 to DHTMEM + 102 */
03035   /* only Byte 2 and Byte 3 of the first word (@ DHTMEM + 62) belong to AC1 VALS table */
03036   address = (hjpeg->Instance->DHTMEM + 62);
03037   value = *address & 0x0000FFFFUL;
03038   value = value | (((uint32_t)HuffTableAC1->HuffVal[1] & 0xFFUL) << 24) | (((uint32_t)HuffTableAC1->HuffVal[0] & 0xFFUL) <<
03039                                                                          16);
03040   *address = value;
03041 
03042   /*continue setting 160 AC1 huffman values from DHTMEM + 63 to DHTMEM+102 */
03043   address = (hjpeg->Instance->DHTMEM + 102);
03044   index = 160;
03045   while (index > 3UL)
03046   {
03047     *address = (((uint32_t)HuffTableAC1->HuffVal[index + 1UL] & 0xFFUL) << 24) |
03048                (((uint32_t)HuffTableAC1->HuffVal[index] & 0xFFUL) << 16) |
03049                (((uint32_t)HuffTableAC1->HuffVal[index - 1UL] & 0xFFUL) << 8) |
03050                ((uint32_t)HuffTableAC1->HuffVal[index - 2UL] & 0xFFUL);
03051     address--;
03052     index -= 4UL;
03053   }
03054 
03055 }
03056 
03057 /**
03058   * @brief  Configure the JPEG registers with a given quantization table
03059   * @param  hjpeg pointer to a JPEG_HandleTypeDef structure that contains
03060   *         the configuration information for JPEG module
03061   * @param  QTable pointer to an array of 64 bytes giving the quantization table
03062   * @param  QTableAddress destination quantization address in the JPEG peripheral
03063   *         it could be QMEM0, QMEM1, QMEM2 or QMEM3
03064   * @retval 0 if no error, 1 if error
03065   */
03066 static uint32_t JPEG_Set_Quantization_Mem(JPEG_HandleTypeDef *hjpeg, uint8_t *QTable,
03067                                                     __IO uint32_t *QTableAddress)
03068 {
03069   uint32_t i;
03070   uint32_t j;
03071   uint32_t quantRow;
03072   uint32_t quantVal;
03073   uint32_t ScaleFactor;
03074   __IO uint32_t *tableAddress;
03075 
03076   tableAddress = QTableAddress;
03077 
03078   if ((hjpeg->Conf.ImageQuality >= 50UL) && (hjpeg->Conf.ImageQuality <= 100UL))
03079   {
03080     ScaleFactor = 200UL - (hjpeg->Conf.ImageQuality * 2UL);
03081   }
03082   else if (hjpeg->Conf.ImageQuality > 0UL)
03083   {
03084     ScaleFactor = ((uint32_t) 5000) / ((uint32_t) hjpeg->Conf.ImageQuality);
03085   }
03086   else
03087   {
03088     return 1UL;
03089   }
03090 
03091   /*Quantization_table = (Standard_quanization_table * ScaleFactor + 50) / 100*/
03092   i = 0;
03093   while (i < (JPEG_QUANT_TABLE_SIZE - 3UL))
03094   {
03095     quantRow = 0;
03096     for (j = 0; j < 4UL; j++)
03097     {
03098       /* Note that the quantization coefficients must be specified in the table in zigzag order */
03099       quantVal = ((((uint32_t) QTable[JPEG_ZIGZAG_ORDER[i + j]]) * ScaleFactor) + 50UL) / 100UL;
03100 
03101       if (quantVal == 0UL)
03102       {
03103         quantVal = 1UL;
03104       }
03105       else if (quantVal > 255UL)
03106       {
03107         quantVal = 255UL;
03108       }
03109       else
03110       {
03111         /* Nothing to do, keep same value of quantVal */
03112       }
03113 
03114       quantRow |= ((quantVal & 0xFFUL) << (8UL * j));
03115     }
03116 
03117     i += 4UL;
03118     *tableAddress = quantRow;
03119     tableAddress ++;
03120   }
03121 
03122   /* Return function status */
03123   return 0UL;
03124 }
03125 
03126 /**
03127   * @brief  Configure the JPEG registers for YCbCr color space
03128   * @param  hjpeg pointer to a JPEG_HandleTypeDef structure that contains
03129   *         the configuration information for JPEG module
03130   * @retval None
03131   */
03132 static void JPEG_SetColorYCBCR(JPEG_HandleTypeDef *hjpeg)
03133 {
03134   uint32_t ySamplingH;
03135   uint32_t ySamplingV;
03136   uint32_t yblockNb;
03137 
03138   /*Set Number of color components to 3*/
03139   hjpeg->Instance->CONFR1 &=  ~JPEG_CONFR1_NF;
03140   hjpeg->Instance->CONFR1 |=  JPEG_CONFR1_NF_1;
03141 
03142   /* compute MCU block size and Y, Cb ,Cr sampling factors*/
03143   if (hjpeg->Conf.ChromaSubsampling == JPEG_420_SUBSAMPLING)
03144   {
03145     ySamplingH  = JPEG_CONFR4_HSF_1;   /* Hs = 2*/
03146     ySamplingV  = JPEG_CONFR4_VSF_1;   /* Vs = 2*/
03147 
03148     yblockNb  = 0x30; /* 4 blocks of 8x8*/
03149   }
03150   else if (hjpeg->Conf.ChromaSubsampling == JPEG_422_SUBSAMPLING)
03151   {
03152     ySamplingH  = JPEG_CONFR4_HSF_1;   /* Hs = 2*/
03153     ySamplingV  = JPEG_CONFR4_VSF_0;   /* Vs = 1*/
03154 
03155     yblockNb  = 0x10; /* 2 blocks of 8x8*/
03156   }
03157   else /*JPEG_444_SUBSAMPLING and default*/
03158   {
03159     ySamplingH  = JPEG_CONFR4_HSF_0;   /* Hs = 1*/
03160     ySamplingV  = JPEG_CONFR4_VSF_0;   /* Vs = 1*/
03161 
03162     yblockNb  = 0; /* 1 block of 8x8*/
03163   }
03164 
03165   hjpeg->Instance->CONFR1 &= ~(JPEG_CONFR1_NF | JPEG_CONFR1_NS);
03166   hjpeg->Instance->CONFR1 |= (JPEG_CONFR1_NF_1 | JPEG_CONFR1_NS_1);
03167 
03168   /*Reset CONFR4 register*/
03169   hjpeg->Instance->CONFR4 =  0;
03170   /*Set Horizental and Vertical  sampling factor , number of blocks , Quantization table and Huffman AC/DC tables for component 0*/
03171   hjpeg->Instance->CONFR4 |= (ySamplingH | ySamplingV | (yblockNb & JPEG_CONFR4_NB));
03172 
03173   /*Reset CONFR5 register*/
03174   hjpeg->Instance->CONFR5 =  0;
03175   /*Set Horizental and Vertical  sampling factor , number of blocks , Quantization table and Huffman AC/DC tables for component 1*/
03176   hjpeg->Instance->CONFR5 |= (JPEG_CONFR5_HSF_0 | JPEG_CONFR5_VSF_0 | JPEG_CONFR5_QT_0 | JPEG_CONFR5_HA | JPEG_CONFR5_HD);
03177 
03178   /*Reset CONFR6 register*/
03179   hjpeg->Instance->CONFR6 =  0;
03180   /*Set Horizental and Vertical  sampling factor and number of blocks for component 2*/
03181   /* In YCBCR , by default, both chrominance components (component 1 and component 2) use the same Quantization table (table 1) */
03182   /* In YCBCR , both chrominance components (component 1 and component 2) use the same Huffman tables (table 1) */
03183   hjpeg->Instance->CONFR6 |= (JPEG_CONFR6_HSF_0 | JPEG_CONFR6_VSF_0 | JPEG_CONFR6_QT_0 | JPEG_CONFR6_HA | JPEG_CONFR6_HD);
03184 
03185 }
03186 
03187 /**
03188   * @brief  Configure the JPEG registers for GrayScale color space
03189   * @param  hjpeg pointer to a JPEG_HandleTypeDef structure that contains
03190   *         the configuration information for JPEG module
03191   * @retval None
03192   */
03193 static void JPEG_SetColorGrayScale(JPEG_HandleTypeDef *hjpeg)
03194 {
03195   /*Set Number of color components to 1*/
03196   hjpeg->Instance->CONFR1 &= ~(JPEG_CONFR1_NF | JPEG_CONFR1_NS);
03197 
03198   /*in GrayScale use 1 single Quantization table (Table 0)*/
03199   /*in GrayScale use only one couple of AC/DC huffman table (table 0)*/
03200 
03201   /*Reset CONFR4 register*/
03202   hjpeg->Instance->CONFR4 =  0;
03203   /*Set Horizental and Vertical  sampling factor , number of blocks , Quantization table and Huffman AC/DC tables for component 0*/
03204   hjpeg->Instance->CONFR4 |=  JPEG_CONFR4_HSF_0 | JPEG_CONFR4_VSF_0 ;
03205 }
03206 
03207 /**
03208   * @brief  Configure the JPEG registers for CMYK color space
03209   * @param  hjpeg pointer to a JPEG_HandleTypeDef structure that contains
03210   *         the configuration information for JPEG module
03211   * @retval None
03212   */
03213 static void JPEG_SetColorCMYK(JPEG_HandleTypeDef *hjpeg)
03214 {
03215   uint32_t ySamplingH;
03216   uint32_t ySamplingV;
03217   uint32_t yblockNb;
03218 
03219   /*Set Number of color components to 4*/
03220   hjpeg->Instance->CONFR1 |= (JPEG_CONFR1_NF | JPEG_CONFR1_NS);
03221 
03222   /* compute MCU block size and Y, Cb ,Cr sampling factors*/
03223   if (hjpeg->Conf.ChromaSubsampling == JPEG_420_SUBSAMPLING)
03224   {
03225     ySamplingH  = JPEG_CONFR4_HSF_1;   /* Hs = 2*/
03226     ySamplingV  = JPEG_CONFR4_VSF_1;   /* Vs = 2*/
03227 
03228     yblockNb  = 0x30; /* 4 blocks of 8x8*/
03229   }
03230   else if (hjpeg->Conf.ChromaSubsampling == JPEG_422_SUBSAMPLING)
03231   {
03232     ySamplingH  = JPEG_CONFR4_HSF_1;   /* Hs = 2*/
03233     ySamplingV  = JPEG_CONFR4_VSF_0;   /* Vs = 1*/
03234 
03235     yblockNb  = 0x10; /* 2 blocks of 8x8*/
03236   }
03237   else /*JPEG_444_SUBSAMPLING and default*/
03238   {
03239     ySamplingH  = JPEG_CONFR4_HSF_0;   /* Hs = 1*/
03240     ySamplingV  = JPEG_CONFR4_VSF_0;   /* Vs = 1*/
03241 
03242     yblockNb  = 0; /* 1 block of 8x8*/
03243   }
03244 
03245   /*Reset CONFR4 register*/
03246   hjpeg->Instance->CONFR4 =  0;
03247   /*Set Horizental and Vertical  sampling factor , number of blocks , Quantization table and Huffman AC/DC tables for component 0*/
03248   hjpeg->Instance->CONFR4 |= (ySamplingH | ySamplingV | (yblockNb & JPEG_CONFR4_NB));
03249 
03250   /*Reset CONFR5 register*/
03251   hjpeg->Instance->CONFR5 =  0;
03252   /*Set Horizental and Vertical  sampling factor , number of blocks , Quantization table and Huffman AC/DC tables for component 1*/
03253   hjpeg->Instance->CONFR5 |= (JPEG_CONFR5_HSF_0 | JPEG_CONFR5_VSF_0);
03254 
03255   /*Reset CONFR6 register*/
03256   hjpeg->Instance->CONFR6 =  0;
03257   /*Set Horizental and Vertical  sampling factor , number of blocks , Quantization table and Huffman AC/DC tables for component 2*/
03258   hjpeg->Instance->CONFR6 |= (JPEG_CONFR6_HSF_0 | JPEG_CONFR6_VSF_0);
03259 
03260   /*Reset CONFR7 register*/
03261   hjpeg->Instance->CONFR7 =  0;
03262   /*Set Horizental and Vertical  sampling factor , number of blocks , Quantization table and Huffman AC/DC tables for component 3*/
03263   hjpeg->Instance->CONFR7 |= (JPEG_CONFR7_HSF_0 | JPEG_CONFR7_VSF_0);
03264 }
03265 
03266 /**
03267   * @brief  Init the JPEG encoding/decoding process in case of Polling or Interrupt and DMA
03268   * @param  hjpeg pointer to a JPEG_HandleTypeDef structure that contains
03269   *         the configuration information for JPEG module
03270   * @retval None
03271   */
03272 static void JPEG_Init_Process(JPEG_HandleTypeDef *hjpeg)
03273 {
03274   /*Reset pause*/
03275   hjpeg->Context &= (~(JPEG_CONTEXT_PAUSE_INPUT | JPEG_CONTEXT_PAUSE_OUTPUT));
03276 
03277   if ((hjpeg->Context & JPEG_CONTEXT_OPERATION_MASK) == JPEG_CONTEXT_DECODE)
03278   {
03279     /*Set JPEG Codec to Decoding mode */
03280     hjpeg->Instance->CONFR1 |= JPEG_CONFR1_DE;
03281   }
03282   else /* JPEG_CONTEXT_ENCODE */
03283   {
03284     /*Set JPEG Codec to Encoding mode */
03285     hjpeg->Instance->CONFR1 &= ~JPEG_CONFR1_DE;
03286   }
03287 
03288   /*Stop JPEG processing */
03289   hjpeg->Instance->CONFR0 &=  ~JPEG_CONFR0_START;
03290 
03291   /* Disable All Interrupts */
03292   __HAL_JPEG_DISABLE_IT(hjpeg, JPEG_INTERRUPT_MASK);
03293 
03294   /* Flush input and output FIFOs*/
03295   hjpeg->Instance->CR |= JPEG_CR_IFF;
03296   hjpeg->Instance->CR |= JPEG_CR_OFF;
03297 
03298   /* Clear all flags */
03299   __HAL_JPEG_CLEAR_FLAG(hjpeg, JPEG_FLAG_ALL);
03300 
03301   /*Start Encoding/Decoding*/
03302   hjpeg->Instance->CONFR0 |=  JPEG_CONFR0_START;
03303 
03304   if ((hjpeg->Context & JPEG_CONTEXT_METHOD_MASK) == JPEG_CONTEXT_IT)
03305   {
03306     /*Enable IN/OUT, end of Conversation, and end of header parsing interruptions*/
03307     __HAL_JPEG_ENABLE_IT(hjpeg, JPEG_IT_IFT | JPEG_IT_IFNF | JPEG_IT_OFT | JPEG_IT_OFNE | JPEG_IT_EOC | JPEG_IT_HPD);
03308   }
03309   else if ((hjpeg->Context & JPEG_CONTEXT_METHOD_MASK) == JPEG_CONTEXT_DMA)
03310   {
03311     /*Enable End Of Conversation, and End Of Header parsing interruptions*/
03312     __HAL_JPEG_ENABLE_IT(hjpeg, JPEG_IT_EOC | JPEG_IT_HPD);
03313 
03314   }
03315   else
03316   {
03317     /* Nothing to do */
03318   }
03319 }
03320 
03321 /**
03322   * @brief  JPEG encoding/decoding process in case of Polling or Interrupt
03323   * @param  hjpeg pointer to a JPEG_HandleTypeDef structure that contains
03324   *         the configuration information for JPEG module
03325   * @retval JPEG_PROCESS_DONE if the process has ends else JPEG_PROCESS_ONGOING
03326   */
03327 static uint32_t JPEG_Process(JPEG_HandleTypeDef *hjpeg)
03328 {
03329   uint32_t tmpContext;
03330 
03331   /*End of header processing flag */
03332   if ((hjpeg->Context & JPEG_CONTEXT_OPERATION_MASK) == JPEG_CONTEXT_DECODE)
03333   {
03334     if (__HAL_JPEG_GET_FLAG(hjpeg, JPEG_FLAG_HPDF) != 0UL)
03335     {
03336       /*Call Header parsing complete callback */
03337       (void) HAL_JPEG_GetInfo(hjpeg, &hjpeg->Conf);
03338       /* Reset the ImageQuality */
03339       hjpeg->Conf.ImageQuality = 0;
03340       /* Note : the image quality is only available at the end of the decoding operation */
03341       /* at the current stage the calculated image quality is not correct so reset it */
03342 
03343       /*Call Info Ready callback */
03344 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
03345       hjpeg->InfoReadyCallback(hjpeg, &hjpeg->Conf);
03346 #else
03347       HAL_JPEG_InfoReadyCallback(hjpeg, &hjpeg->Conf);
03348 #endif /* USE_HAL_JPEG_REGISTER_CALLBACKS */
03349 
03350       __HAL_JPEG_DISABLE_IT(hjpeg, JPEG_IT_HPD);
03351 
03352       /* Clear header processing done flag */
03353       __HAL_JPEG_CLEAR_FLAG(hjpeg, JPEG_FLAG_HPDF);
03354     }
03355   }
03356 
03357   /*Input FIFO status handling*/
03358   if ((hjpeg->Context &  JPEG_CONTEXT_PAUSE_INPUT) == 0UL)
03359   {
03360     if (__HAL_JPEG_GET_FLAG(hjpeg, JPEG_FLAG_IFTF) != 0UL)
03361     {
03362       /*Input FIFO threshold flag */
03363       /*JPEG_FIFO_TH_SIZE words can be written in */
03364       JPEG_ReadInputData(hjpeg, JPEG_FIFO_TH_SIZE);
03365     }
03366     else if (__HAL_JPEG_GET_FLAG(hjpeg, JPEG_FLAG_IFNFF) != 0UL)
03367     {
03368       /*Input FIFO Not Full flag */
03369       /*32-bit value can be written in */
03370       JPEG_ReadInputData(hjpeg, 1);
03371     }
03372     else
03373     {
03374       /* Nothing to do */
03375     }
03376   }
03377 
03378 
03379   /*Output FIFO flag handling*/
03380   if ((hjpeg->Context &  JPEG_CONTEXT_PAUSE_OUTPUT) == 0UL)
03381   {
03382     if (__HAL_JPEG_GET_FLAG(hjpeg, JPEG_FLAG_OFTF) != 0UL)
03383     {
03384       /*Output FIFO threshold flag */
03385       /*JPEG_FIFO_TH_SIZE words can be read out */
03386       JPEG_StoreOutputData(hjpeg, JPEG_FIFO_TH_SIZE);
03387     }
03388     else if (__HAL_JPEG_GET_FLAG(hjpeg, JPEG_FLAG_OFNEF) != 0UL)
03389     {
03390       /*Output FIFO Not Empty flag */
03391       /*32-bit value can be read out */
03392       JPEG_StoreOutputData(hjpeg, 1);
03393     }
03394     else
03395     {
03396       /* Nothing to do */
03397     }
03398   }
03399 
03400   /*End of Conversion handling :i.e EOC flag is high and OFTF low and OFNEF low*/
03401   if (__HAL_JPEG_GET_FLAG(hjpeg, JPEG_FLAG_EOCF | JPEG_FLAG_OFTF | JPEG_FLAG_OFNEF) == JPEG_FLAG_EOCF)
03402   {
03403     /*Stop Encoding/Decoding*/
03404     hjpeg->Instance->CONFR0 &=  ~JPEG_CONFR0_START;
03405 
03406     if ((hjpeg->Context & JPEG_CONTEXT_METHOD_MASK) == JPEG_CONTEXT_IT)
03407     {
03408       /* Disable All Interrupts */
03409       __HAL_JPEG_DISABLE_IT(hjpeg, JPEG_INTERRUPT_MASK);
03410     }
03411 
03412     /* Clear all flags */
03413     __HAL_JPEG_CLEAR_FLAG(hjpeg, JPEG_FLAG_ALL);
03414 
03415     /*Call End of conversion callback */
03416     if (hjpeg->JpegOutCount > 0UL)
03417     {
03418       /*Output Buffer is not empty, call DecodedDataReadyCallback*/
03419 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
03420       hjpeg->DataReadyCallback(hjpeg, hjpeg->pJpegOutBuffPtr, hjpeg->JpegOutCount);
03421 #else
03422       HAL_JPEG_DataReadyCallback(hjpeg, hjpeg->pJpegOutBuffPtr, hjpeg->JpegOutCount);
03423 #endif /* USE_HAL_JPEG_REGISTER_CALLBACKS */
03424 
03425       hjpeg->JpegOutCount = 0;
03426     }
03427 
03428     /*Reset Context Operation*/
03429     tmpContext = hjpeg->Context;
03430     /*Clear all context fields execpt JPEG_CONTEXT_CONF_ENCODING and JPEG_CONTEXT_CUSTOM_TABLES*/
03431     hjpeg->Context &= (JPEG_CONTEXT_CONF_ENCODING | JPEG_CONTEXT_CUSTOM_TABLES);
03432 
03433     /* Process Unlocked */
03434     __HAL_UNLOCK(hjpeg);
03435 
03436     /* Change the JPEG state */
03437     hjpeg->State = HAL_JPEG_STATE_READY;
03438 
03439     /*Call End of Encoding/Decoding callback */
03440     if ((tmpContext & JPEG_CONTEXT_OPERATION_MASK) == JPEG_CONTEXT_DECODE)
03441     {
03442 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
03443       hjpeg->DecodeCpltCallback(hjpeg);
03444 #else
03445       HAL_JPEG_DecodeCpltCallback(hjpeg);
03446 #endif /*USE_HAL_JPEG_REGISTER_CALLBACKS*/
03447     }
03448     else /* JPEG_CONTEXT_ENCODE */
03449     {
03450 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
03451       hjpeg->EncodeCpltCallback(hjpeg);
03452 #else
03453       HAL_JPEG_EncodeCpltCallback(hjpeg);
03454 #endif
03455     }
03456 
03457     return JPEG_PROCESS_DONE;
03458   }
03459 
03460 
03461   return JPEG_PROCESS_ONGOING;
03462 }
03463 
03464 /**
03465   * @brief  Store some output data from the JPEG peripheral to the output buffer.
03466   *         This function is used when the JPEG peripheral has new data to output
03467   *         in case of Polling or Interrupt process
03468   * @param  hjpeg pointer to a JPEG_HandleTypeDef structure that contains
03469   *         the configuration information for JPEG module
03470   * @param  nbOutputWords Number of output words (of 32 bits) ready from the JPEG peripheral
03471   * @retval None
03472   */
03473 static void JPEG_StoreOutputData(JPEG_HandleTypeDef *hjpeg, uint32_t nbOutputWords)
03474 {
03475   uint32_t index;
03476   uint32_t nb_words;
03477   uint32_t nb_bytes;
03478   uint32_t dataword;
03479 
03480   if (hjpeg->OutDataLength >= (hjpeg->JpegOutCount + (nbOutputWords * 4UL)))
03481   {
03482     for (index = 0; index < nbOutputWords; index++)
03483     {
03484       /*Transfer 32 bits from the JPEG output FIFO*/
03485       dataword = hjpeg->Instance->DOR;
03486       hjpeg->pJpegOutBuffPtr[hjpeg->JpegOutCount] = (uint8_t)(dataword & 0x000000FFUL);
03487       hjpeg->pJpegOutBuffPtr[hjpeg->JpegOutCount + 1UL] = (uint8_t)((dataword & 0x0000FF00UL) >> 8);
03488       hjpeg->pJpegOutBuffPtr[hjpeg->JpegOutCount + 2UL] = (uint8_t)((dataword & 0x00FF0000UL) >> 16);
03489       hjpeg->pJpegOutBuffPtr[hjpeg->JpegOutCount + 3UL] = (uint8_t)((dataword & 0xFF000000UL) >> 24);
03490       hjpeg->JpegOutCount += 4UL;
03491     }
03492     if (hjpeg->OutDataLength == hjpeg->JpegOutCount)
03493     {
03494       /*Output Buffer is full, call DecodedDataReadyCallback*/
03495 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
03496       hjpeg->DataReadyCallback(hjpeg, hjpeg->pJpegOutBuffPtr, hjpeg->JpegOutCount);
03497 #else
03498       HAL_JPEG_DataReadyCallback(hjpeg, hjpeg->pJpegOutBuffPtr, hjpeg->JpegOutCount);
03499 #endif /*USE_HAL_JPEG_REGISTER_CALLBACKS*/
03500       hjpeg->JpegOutCount = 0;
03501     }
03502   }
03503   else if (hjpeg->OutDataLength > hjpeg->JpegOutCount)
03504   {
03505     nb_words = (hjpeg->OutDataLength - hjpeg->JpegOutCount) / 4UL;
03506     for (index = 0; index < nb_words; index++)
03507     {
03508       /*Transfer 32 bits from the JPEG output FIFO*/
03509       dataword = hjpeg->Instance->DOR;
03510       hjpeg->pJpegOutBuffPtr[hjpeg->JpegOutCount] = (uint8_t)(dataword & 0x000000FFUL);
03511       hjpeg->pJpegOutBuffPtr[hjpeg->JpegOutCount + 1UL] = (uint8_t)((dataword & 0x0000FF00UL) >> 8);
03512       hjpeg->pJpegOutBuffPtr[hjpeg->JpegOutCount + 2UL] = (uint8_t)((dataword & 0x00FF0000UL) >> 16);
03513       hjpeg->pJpegOutBuffPtr[hjpeg->JpegOutCount + 3UL] = (uint8_t)((dataword & 0xFF000000UL) >> 24);
03514       hjpeg->JpegOutCount += 4UL;
03515     }
03516     if (hjpeg->OutDataLength == hjpeg->JpegOutCount)
03517     {
03518       /*Output Buffer is full, call DecodedDataReadyCallback*/
03519 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
03520       hjpeg->DataReadyCallback(hjpeg, hjpeg->pJpegOutBuffPtr, hjpeg->JpegOutCount);
03521 #else
03522       HAL_JPEG_DataReadyCallback(hjpeg, hjpeg->pJpegOutBuffPtr, hjpeg->JpegOutCount);
03523 #endif /* USE_HAL_JPEG_REGISTER_CALLBACKS */
03524       hjpeg->JpegOutCount = 0;
03525     }
03526     else
03527     {
03528       nb_bytes = hjpeg->OutDataLength - hjpeg->JpegOutCount;
03529       dataword = hjpeg->Instance->DOR;
03530       for (index = 0; index < nb_bytes; index++)
03531       {
03532         hjpeg->pJpegOutBuffPtr[hjpeg->JpegOutCount] = (uint8_t)((dataword >> (8UL * (index & 0x3UL))) & 0xFFUL);
03533         hjpeg->JpegOutCount++;
03534       }
03535       /*Output Buffer is full, call DecodedDataReadyCallback*/
03536 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
03537       hjpeg->DataReadyCallback(hjpeg, hjpeg->pJpegOutBuffPtr, hjpeg->JpegOutCount);
03538 #else
03539       HAL_JPEG_DataReadyCallback(hjpeg, hjpeg->pJpegOutBuffPtr, hjpeg->JpegOutCount);
03540 #endif /* USE_HAL_JPEG_REGISTER_CALLBACKS */
03541 
03542       hjpeg->JpegOutCount = 0;
03543 
03544       nb_bytes = 4UL - nb_bytes;
03545       for (index = nb_bytes; index < 4UL; index++)
03546       {
03547         hjpeg->pJpegOutBuffPtr[hjpeg->JpegOutCount] = (uint8_t)((dataword >> (8UL * index)) & 0xFFUL);
03548         hjpeg->JpegOutCount++;
03549       }
03550     }
03551   }
03552   else
03553   {
03554     /* Nothing to do */
03555   }
03556 }
03557 
03558 /**
03559   * @brief  Read some input Data from the input buffer.
03560   *         This function is used when the JPEG peripheral needs new data
03561   *         in case of Polling or Interrupt process
03562   * @param  hjpeg pointer to a JPEG_HandleTypeDef structure that contains
03563   *         the configuration information for JPEG module
03564   * @param  nbRequestWords Number of input words (of 32 bits) that the JPE peripheral request
03565   * @retval None
03566   */
03567 static void JPEG_ReadInputData(JPEG_HandleTypeDef *hjpeg, uint32_t nbRequestWords)
03568 {
03569   uint32_t nb_bytes = 0;
03570   uint32_t nb_words;
03571   uint32_t index;
03572   uint32_t dataword;
03573   uint32_t input_count;
03574 
03575   if ((hjpeg->InDataLength == 0UL) || (nbRequestWords == 0UL))
03576   {
03577     /* No more Input data : nothing to do*/
03578     (void) HAL_JPEG_Pause(hjpeg, JPEG_PAUSE_RESUME_INPUT);
03579   }
03580   else if (hjpeg->InDataLength > hjpeg->JpegInCount)
03581   {
03582     nb_bytes = hjpeg->InDataLength - hjpeg->JpegInCount;
03583   }
03584   else if (hjpeg->InDataLength == hjpeg->JpegInCount)
03585   {
03586     /*Call HAL_JPEG_GetDataCallback to get new data */
03587 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
03588     hjpeg->GetDataCallback(hjpeg, hjpeg->JpegInCount);
03589 #else
03590     HAL_JPEG_GetDataCallback(hjpeg, hjpeg->JpegInCount);
03591 #endif /*USE_HAL_JPEG_REGISTER_CALLBACKS*/
03592 
03593     if (hjpeg->InDataLength > 4UL)
03594     {
03595       hjpeg->InDataLength = hjpeg->InDataLength - (hjpeg->InDataLength % 4UL);
03596     }
03597     hjpeg->JpegInCount = 0;
03598     nb_bytes = hjpeg->InDataLength;
03599   }
03600   else
03601   {
03602     /* Nothing to do */
03603   }
03604   if (((hjpeg->Context &  JPEG_CONTEXT_PAUSE_INPUT) == 0UL) && (nb_bytes > 0UL))
03605   {
03606     nb_words = nb_bytes / 4UL;
03607     if (nb_words >= nbRequestWords)
03608     {
03609       for (index = 0; index < nbRequestWords; index++)
03610       {
03611         input_count = hjpeg->JpegInCount;
03612         hjpeg->Instance->DIR = (((uint32_t)(hjpeg->pJpegInBuffPtr[input_count])) | \
03613                                 (((uint32_t)(hjpeg->pJpegInBuffPtr[input_count + 1UL])) << 8) | \
03614                                 (((uint32_t)(hjpeg->pJpegInBuffPtr[input_count + 2UL])) << 16) | \
03615                                 (((uint32_t)(hjpeg->pJpegInBuffPtr[input_count + 3UL])) << 24));
03616 
03617         hjpeg->JpegInCount += 4UL;
03618       }
03619     }
03620     else /*nb_words < nbRequestWords*/
03621     {
03622       if (nb_words > 0UL)
03623       {
03624         for (index = 0; index < nb_words; index++)
03625         {
03626           input_count = hjpeg->JpegInCount;
03627           hjpeg->Instance->DIR = (((uint32_t)(hjpeg->pJpegInBuffPtr[input_count])) | \
03628                                   (((uint32_t)(hjpeg->pJpegInBuffPtr[input_count + 1UL])) << 8) | \
03629                                   (((uint32_t)(hjpeg->pJpegInBuffPtr[input_count + 2UL])) << 16) | \
03630                                   (((uint32_t)(hjpeg->pJpegInBuffPtr[input_count + 3UL])) << 24));
03631 
03632           hjpeg->JpegInCount += 4UL;
03633         }
03634       }
03635       else
03636       {
03637         /* end of file*/
03638         dataword = 0;
03639         for (index = 0; index < nb_bytes; index++)
03640         {
03641           dataword |= (uint32_t)hjpeg->pJpegInBuffPtr[hjpeg->JpegInCount] << (8UL * (index & 0x03UL));
03642           hjpeg->JpegInCount++;
03643         }
03644         hjpeg->Instance->DIR = dataword;
03645       }
03646     }
03647   }
03648 }
03649 
03650 /**
03651   * @brief  Start the JPEG DMA process (encoding/decoding)
03652   * @param  hjpeg pointer to a JPEG_HandleTypeDef structure that contains
03653   *         the configuration information for JPEG module
03654   * @retval JPEG_PROCESS_DONE if process ends else JPEG_PROCESS_ONGOING
03655   */
03656 static HAL_StatusTypeDef JPEG_DMA_StartProcess(JPEG_HandleTypeDef *hjpeg)
03657 {
03658   uint32_t inXfrSize, outXfrSize;
03659 
03660   /*if the MDMA In is triggred with JPEG In FIFO Threshold flag
03661       then MDMA In buffer size is 32 bytes
03662     else (MDMA In is triggred with JPEG In FIFO not full flag)
03663       then MDMA In buffer size is 4 bytes
03664     */
03665   inXfrSize = hjpeg->hdmain->Init.BufferTransferLength;
03666 
03667   /*if the MDMA Out is triggred with JPEG Out FIFO Threshold flag
03668       then MDMA out buffer size is 32 bytes
03669     else (MDMA Out is triggred with JPEG Out FIFO not empty flag)
03670       then MDMA buffer size is 4 bytes
03671     */
03672   outXfrSize = hjpeg->hdmaout->Init.BufferTransferLength;
03673 
03674   if ((hjpeg->InDataLength < inXfrSize) || (hjpeg->OutDataLength < outXfrSize))
03675   {
03676     return HAL_ERROR;
03677   }
03678   /* Set the JPEG MDMA In transfer complete callback */
03679   hjpeg->hdmain->XferCpltCallback = JPEG_MDMAInCpltCallback;
03680   /* Set the MDMA In error callback */
03681   hjpeg->hdmain->XferErrorCallback = JPEG_MDMAErrorCallback;
03682 
03683   /* Set the JPEG MDMA Out transfer complete callback */
03684   hjpeg->hdmaout->XferCpltCallback = JPEG_MDMAOutCpltCallback;
03685   /* Set the MDMA In error callback */
03686   hjpeg->hdmaout->XferErrorCallback = JPEG_MDMAErrorCallback;
03687   /* Set the MDMA Out Abort callback */
03688   hjpeg->hdmaout->XferAbortCallback = JPEG_MDMAOutAbortCallback;
03689 
03690   if ((inXfrSize == 0UL) || (outXfrSize == 0UL))
03691   {
03692     hjpeg->ErrorCode |= HAL_JPEG_ERROR_DMA;
03693     return HAL_ERROR;
03694   }
03695   /*MDMA transfer size (BNDTR) must be a multiple of MDMA buffer size (TLEN)*/
03696   hjpeg->InDataLength = hjpeg->InDataLength - (hjpeg->InDataLength % inXfrSize);
03697 
03698   /*MDMA transfer size (BNDTR) must be a multiple of MDMA buffer size (TLEN)*/
03699   hjpeg->OutDataLength = hjpeg->OutDataLength - (hjpeg->OutDataLength % outXfrSize);
03700 
03701 
03702   /* Start MDMA FIFO Out transfer */
03703   if (HAL_MDMA_Start_IT(hjpeg->hdmaout, (uint32_t)&hjpeg->Instance->DOR, (uint32_t)hjpeg->pJpegOutBuffPtr,
03704                         hjpeg->OutDataLength, 1) != HAL_OK)
03705   {
03706     hjpeg->ErrorCode |= HAL_JPEG_ERROR_DMA;
03707     return HAL_ERROR;
03708   }
03709   /* Start DMA FIFO In transfer */
03710   if (HAL_MDMA_Start_IT(hjpeg->hdmain, (uint32_t)hjpeg->pJpegInBuffPtr, (uint32_t)&hjpeg->Instance->DIR,
03711                         hjpeg->InDataLength, 1) != HAL_OK)
03712   {
03713     hjpeg->ErrorCode |= HAL_JPEG_ERROR_DMA;
03714     return HAL_ERROR;
03715   }
03716 
03717   return HAL_OK;
03718 }
03719 
03720 /**
03721   * @brief  Continue the current JPEG DMA process (encoding/decoding)
03722   * @param  hjpeg pointer to a JPEG_HandleTypeDef structure that contains
03723   *         the configuration information for JPEG module
03724   * @retval JPEG_PROCESS_DONE if process ends else JPEG_PROCESS_ONGOING
03725   */
03726 static void JPEG_DMA_ContinueProcess(JPEG_HandleTypeDef *hjpeg)
03727 {
03728   /*End of header processing flag rises*/
03729   if ((hjpeg->Context & JPEG_CONTEXT_OPERATION_MASK) == JPEG_CONTEXT_DECODE)
03730   {
03731     if (__HAL_JPEG_GET_FLAG(hjpeg, JPEG_FLAG_HPDF) != 0UL)
03732     {
03733       /*Call Header parsing complete callback */
03734       (void) HAL_JPEG_GetInfo(hjpeg, &hjpeg->Conf);
03735 
03736       /* Reset the ImageQuality */
03737       hjpeg->Conf.ImageQuality = 0;
03738       /* Note : the image quality is only available at the end of the decoding operation */
03739       /* at the current stage the calculated image quality is not correct so reset it */
03740 
03741       /*Call Info Ready callback */
03742 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
03743       hjpeg->InfoReadyCallback(hjpeg, &hjpeg->Conf);
03744 #else
03745       HAL_JPEG_InfoReadyCallback(hjpeg, &hjpeg->Conf);
03746 #endif /* USE_HAL_JPEG_REGISTER_CALLBACKS */
03747 
03748       __HAL_JPEG_DISABLE_IT(hjpeg, JPEG_IT_HPD);
03749 
03750       /* Clear header processing done flag */
03751       __HAL_JPEG_CLEAR_FLAG(hjpeg, JPEG_FLAG_HPDF);
03752     }
03753   }
03754 
03755   /*End of Conversion handling*/
03756   if (__HAL_JPEG_GET_FLAG(hjpeg, JPEG_FLAG_EOCF) != 0UL)
03757   {
03758 
03759     hjpeg->Context |= JPEG_CONTEXT_ENDING_DMA;
03760 
03761     /*Stop Encoding/Decoding*/
03762     hjpeg->Instance->CONFR0 &=  ~JPEG_CONFR0_START;
03763 
03764     __HAL_JPEG_DISABLE_IT(hjpeg, JPEG_INTERRUPT_MASK);
03765 
03766     /* Clear all flags */
03767     __HAL_JPEG_CLEAR_FLAG(hjpeg, JPEG_FLAG_ALL);
03768 
03769     if (hjpeg->hdmain->State == HAL_MDMA_STATE_BUSY)
03770     {
03771       /* Stop the MDMA In Xfer*/
03772       (void) HAL_MDMA_Abort_IT(hjpeg->hdmain);
03773     }
03774 
03775     if (hjpeg->hdmaout->State == HAL_MDMA_STATE_BUSY)
03776     {
03777       /* Stop the MDMA out Xfer*/
03778       (void) HAL_MDMA_Abort_IT(hjpeg->hdmaout);
03779     }
03780     else
03781     {
03782       JPEG_DMA_EndProcess(hjpeg);
03783     }
03784   }
03785 
03786 
03787 }
03788 
03789 /**
03790   * @brief  Finalize the current JPEG DMA process (encoding/decoding)
03791   * @param  hjpeg pointer to a JPEG_HandleTypeDef structure that contains
03792   *         the configuration information for JPEG module
03793   * @retval JPEG_PROCESS_DONE
03794   */
03795 static void JPEG_DMA_EndProcess(JPEG_HandleTypeDef *hjpeg)
03796 {
03797   uint32_t tmpContext;
03798   hjpeg->JpegOutCount = hjpeg->OutDataLength - (hjpeg->hdmaout->Instance->CBNDTR & MDMA_CBNDTR_BNDT);
03799 
03800   /*if Output Buffer is full, call HAL_JPEG_DataReadyCallback*/
03801   if (hjpeg->JpegOutCount == hjpeg->OutDataLength)
03802   {
03803 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
03804     hjpeg->DataReadyCallback(hjpeg, hjpeg->pJpegOutBuffPtr, hjpeg->JpegOutCount);
03805 #else
03806     HAL_JPEG_DataReadyCallback(hjpeg, hjpeg->pJpegOutBuffPtr, hjpeg->JpegOutCount);
03807 #endif /* USE_HAL_JPEG_REGISTER_CALLBACKS */
03808 
03809     hjpeg->JpegOutCount = 0;
03810   }
03811 
03812   /*Check if remaining data in the output FIFO*/
03813   if (__HAL_JPEG_GET_FLAG(hjpeg, JPEG_FLAG_OFNEF) == 0UL)
03814   {
03815     if (hjpeg->JpegOutCount > 0UL)
03816     {
03817       /*Output Buffer is not empty, call DecodedDataReadyCallback*/
03818 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
03819       hjpeg->DataReadyCallback(hjpeg, hjpeg->pJpegOutBuffPtr, hjpeg->JpegOutCount);
03820 #else
03821       HAL_JPEG_DataReadyCallback(hjpeg, hjpeg->pJpegOutBuffPtr, hjpeg->JpegOutCount);
03822 #endif /* USE_HAL_JPEG_REGISTER_CALLBACKS */
03823 
03824       hjpeg->JpegOutCount = 0;
03825     }
03826 
03827     /*Stop Encoding/Decoding*/
03828     hjpeg->Instance->CONFR0 &=  ~JPEG_CONFR0_START;
03829 
03830     tmpContext = hjpeg->Context;
03831     /*Clear all context fields execpt JPEG_CONTEXT_CONF_ENCODING and JPEG_CONTEXT_CUSTOM_TABLES*/
03832     hjpeg->Context &= (JPEG_CONTEXT_CONF_ENCODING | JPEG_CONTEXT_CUSTOM_TABLES);
03833 
03834     /* Process Unlocked */
03835     __HAL_UNLOCK(hjpeg);
03836 
03837     /* Change the JPEG state */
03838     hjpeg->State = HAL_JPEG_STATE_READY;
03839 
03840     /*Call End of Encoding/Decoding callback */
03841     if ((tmpContext & JPEG_CONTEXT_OPERATION_MASK) == JPEG_CONTEXT_DECODE)
03842     {
03843 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
03844       hjpeg->DecodeCpltCallback(hjpeg);
03845 #else
03846       HAL_JPEG_DecodeCpltCallback(hjpeg);
03847 #endif /* USE_HAL_JPEG_REGISTER_CALLBACKS */
03848     }
03849     else /* JPEG_CONTEXT_ENCODE */
03850     {
03851 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
03852       hjpeg->EncodeCpltCallback(hjpeg);
03853 #else
03854       HAL_JPEG_EncodeCpltCallback(hjpeg);
03855 #endif /* USE_HAL_JPEG_REGISTER_CALLBACKS */
03856     }
03857   }
03858   else if ((hjpeg->Context &  JPEG_CONTEXT_PAUSE_OUTPUT) == 0UL)
03859   {
03860     JPEG_DMA_PollResidualData(hjpeg);
03861   }
03862   else
03863   {
03864     /* Nothing to do */
03865   }
03866 
03867 }
03868 
03869 /**
03870   * @brief  Poll residual output data when DMA process (encoding/decoding)
03871   * @param  hjpeg pointer to a JPEG_HandleTypeDef structure that contains
03872   *         the configuration information for JPEG module
03873   * @retval None.
03874   */
03875 static void JPEG_DMA_PollResidualData(JPEG_HandleTypeDef *hjpeg)
03876 {
03877   uint32_t tmpContext;
03878   uint32_t count;
03879   uint32_t dataOut;
03880 
03881   for (count = JPEG_FIFO_SIZE; count > 0UL; count--)
03882   {
03883     if ((hjpeg->Context &  JPEG_CONTEXT_PAUSE_OUTPUT) == 0UL)
03884     {
03885       if (__HAL_JPEG_GET_FLAG(hjpeg, JPEG_FLAG_OFNEF) != 0UL)
03886       {
03887         dataOut = hjpeg->Instance->DOR;
03888         hjpeg->pJpegOutBuffPtr[hjpeg->JpegOutCount] = (uint8_t)(dataOut & 0x000000FFUL);
03889         hjpeg->pJpegOutBuffPtr[hjpeg->JpegOutCount + 1UL] = (uint8_t)((dataOut & 0x0000FF00UL) >> 8);
03890         hjpeg->pJpegOutBuffPtr[hjpeg->JpegOutCount + 2UL] = (uint8_t)((dataOut & 0x00FF0000UL) >> 16);
03891         hjpeg->pJpegOutBuffPtr[hjpeg->JpegOutCount + 3UL] = (uint8_t)((dataOut & 0xFF000000UL) >> 24);
03892         hjpeg->JpegOutCount += 4UL;
03893 
03894         if (hjpeg->JpegOutCount == hjpeg->OutDataLength)
03895         {
03896           /*Output Buffer is full, call HAL_JPEG_DataReadyCallback*/
03897 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
03898           hjpeg->DataReadyCallback(hjpeg, hjpeg->pJpegOutBuffPtr, hjpeg->JpegOutCount);
03899 #else
03900           HAL_JPEG_DataReadyCallback(hjpeg, hjpeg->pJpegOutBuffPtr, hjpeg->JpegOutCount);
03901 #endif /* USE_HAL_JPEG_REGISTER_CALLBACKS */
03902 
03903           hjpeg->JpegOutCount = 0;
03904         }
03905 
03906       }
03907     }
03908   }
03909 
03910   tmpContext = hjpeg->Context;
03911 
03912   if ((__HAL_JPEG_GET_FLAG(hjpeg, JPEG_FLAG_OFNEF) == 0UL) || ((tmpContext & JPEG_CONTEXT_PAUSE_OUTPUT) == 0UL))
03913   {
03914     /*Stop Encoding/Decoding*/
03915     hjpeg->Instance->CONFR0 &=  ~JPEG_CONFR0_START;
03916 
03917     if (hjpeg->JpegOutCount > 0UL)
03918     {
03919       /*Output Buffer is not empty, call DecodedDataReadyCallback*/
03920 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
03921       hjpeg->DataReadyCallback(hjpeg, hjpeg->pJpegOutBuffPtr, hjpeg->JpegOutCount);
03922 #else
03923       HAL_JPEG_DataReadyCallback(hjpeg, hjpeg->pJpegOutBuffPtr, hjpeg->JpegOutCount);
03924 #endif /* USE_HAL_JPEG_REGISTER_CALLBACKS */
03925 
03926       hjpeg->JpegOutCount = 0;
03927     }
03928 
03929     tmpContext = hjpeg->Context;
03930     /*Clear all context fields execpt JPEG_CONTEXT_CONF_ENCODING and JPEG_CONTEXT_CUSTOM_TABLES*/
03931     hjpeg->Context &= (JPEG_CONTEXT_CONF_ENCODING | JPEG_CONTEXT_CUSTOM_TABLES);
03932 
03933     /* Process Unlocked */
03934     __HAL_UNLOCK(hjpeg);
03935 
03936     /* Change the JPEG state */
03937     hjpeg->State = HAL_JPEG_STATE_READY;
03938 
03939     /*Call End of Encoding/Decoding callback */
03940     if ((tmpContext & JPEG_CONTEXT_OPERATION_MASK) == JPEG_CONTEXT_DECODE)
03941     {
03942 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
03943       hjpeg->DecodeCpltCallback(hjpeg);
03944 #else
03945       HAL_JPEG_DecodeCpltCallback(hjpeg);
03946 #endif /* USE_HAL_JPEG_REGISTER_CALLBACKS */
03947     }
03948     else /* JPEG_CONTEXT_ENCODE */
03949     {
03950 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
03951       hjpeg->EncodeCpltCallback(hjpeg);
03952 #else
03953       HAL_JPEG_EncodeCpltCallback(hjpeg);
03954 #endif /* USE_HAL_JPEG_REGISTER_CALLBACKS */
03955     }
03956   }
03957 }
03958 
03959 /**
03960   * @brief  MDMA input transfer complete callback
03961   * @param  hmdma pointer to a MDMA_HandleTypeDef structure.
03962   * @retval None
03963   */
03964 static void JPEG_MDMAInCpltCallback(MDMA_HandleTypeDef *hmdma)
03965 {
03966   uint32_t inXfrSize;
03967 
03968   JPEG_HandleTypeDef *hjpeg = (JPEG_HandleTypeDef *)((MDMA_HandleTypeDef *)hmdma)->Parent;
03969 
03970   /* Disable The JPEG IT so the MDMA Input Callback can not be interrupted by the JPEG EOC IT or JPEG HPD IT */
03971   __HAL_JPEG_DISABLE_IT(hjpeg, JPEG_INTERRUPT_MASK);
03972 
03973   /* Check if context method is DMA and we are not in ending DMA stage */
03974   if ((hjpeg->Context & (JPEG_CONTEXT_METHOD_MASK | JPEG_CONTEXT_ENDING_DMA)) == JPEG_CONTEXT_DMA)
03975   {
03976 
03977     /*if the MDMA In is triggred with JPEG In FIFO Threshold flag
03978       then MDMA In buffer size is 32 bytes
03979       else (MDMA In is triggred with JPEG In FIFO not full flag)
03980       then MDMA In buffer size is 4 bytes
03981       */
03982     inXfrSize = hjpeg->hdmain->Init.BufferTransferLength;
03983 
03984     hjpeg->JpegInCount = hjpeg->InDataLength - (hmdma->Instance->CBNDTR & MDMA_CBNDTR_BNDT);
03985 
03986     /*Call HAL_JPEG_GetDataCallback to get new data */
03987 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
03988     hjpeg->GetDataCallback(hjpeg, hjpeg->JpegInCount);
03989 #else
03990     HAL_JPEG_GetDataCallback(hjpeg, hjpeg->JpegInCount);
03991 #endif /* USE_HAL_JPEG_REGISTER_CALLBACKS */
03992 
03993 
03994     if (hjpeg->InDataLength >= inXfrSize)
03995     {
03996       if (inXfrSize == 0UL)
03997       {
03998         hjpeg->ErrorCode |= HAL_JPEG_ERROR_DMA;
03999         hjpeg->State = HAL_JPEG_STATE_ERROR;
04000 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
04001         hjpeg->ErrorCallback(hjpeg);
04002 #else
04003         HAL_JPEG_ErrorCallback(hjpeg);
04004 #endif /* USE_HAL_JPEG_REGISTER_CALLBACKS */
04005         return;
04006       }
04007       /*JPEG Input MDMA transfer data number must be multiple of MDMA buffer size
04008         as the destination is a 32 bits register */
04009       hjpeg->InDataLength = hjpeg->InDataLength - (hjpeg->InDataLength % inXfrSize);
04010 
04011     }
04012     else if (hjpeg->InDataLength > 0UL)
04013     {
04014       /* Transfer the remaining Data, must be multiple of source data size (byte) and destination data size (word) */
04015       if ((hjpeg->InDataLength % 4UL) != 0UL)
04016       {
04017         hjpeg->InDataLength = ((hjpeg->InDataLength / 4UL) + 1UL) * 4UL;
04018       }
04019     }
04020     else
04021     {
04022       /* Nothing to do */
04023     }
04024 
04025     if (((hjpeg->Context &  JPEG_CONTEXT_PAUSE_INPUT) == 0UL) && (hjpeg->InDataLength > 0UL))
04026     {
04027       /* Start MDMA FIFO In transfer */
04028       if (HAL_MDMA_Start_IT(hjpeg->hdmain, (uint32_t)hjpeg->pJpegInBuffPtr, (uint32_t)&hjpeg->Instance->DIR,
04029                             hjpeg->InDataLength, 1) != HAL_OK)
04030       {
04031         hjpeg->ErrorCode |= HAL_JPEG_ERROR_DMA;
04032         hjpeg->State = HAL_JPEG_STATE_ERROR;
04033 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
04034         hjpeg->ErrorCallback(hjpeg);
04035 #else
04036         HAL_JPEG_ErrorCallback(hjpeg);
04037 #endif /* USE_HAL_JPEG_REGISTER_CALLBACKS */
04038         return;
04039       }
04040     }
04041 
04042     /* JPEG Conversion still on going : Enable the JPEG IT */
04043     __HAL_JPEG_ENABLE_IT(hjpeg, JPEG_IT_EOC | JPEG_IT_HPD);
04044   }
04045 }
04046 
04047 /**
04048   * @brief  MDMA output transfer complete callback
04049   * @param  hmdma pointer to a MDMA_HandleTypeDef structure.
04050   * @retval None
04051   */
04052 static void JPEG_MDMAOutCpltCallback(MDMA_HandleTypeDef *hmdma)
04053 {
04054   JPEG_HandleTypeDef *hjpeg = (JPEG_HandleTypeDef *)((MDMA_HandleTypeDef *)hmdma)->Parent;
04055 
04056 
04057   /* Disable The JPEG IT so the MDMA Output Callback can not be interrupted by the JPEG EOC IT or JPEG HPD IT */
04058   __HAL_JPEG_DISABLE_IT(hjpeg, JPEG_INTERRUPT_MASK);
04059 
04060   if ((hjpeg->Context & (JPEG_CONTEXT_METHOD_MASK | JPEG_CONTEXT_ENDING_DMA)) ==
04061       JPEG_CONTEXT_DMA) /* Check if context method is DMA and we are not in ending DMA stage */
04062   {
04063     if (__HAL_JPEG_GET_FLAG(hjpeg, JPEG_FLAG_EOCF) == 0UL)
04064     {
04065       hjpeg->JpegOutCount = hjpeg->OutDataLength - (hmdma->Instance->CBNDTR & MDMA_CBNDTR_BNDT);
04066 
04067       /*Output Buffer is full, call HAL_JPEG_DataReadyCallback*/
04068 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
04069       hjpeg->DataReadyCallback(hjpeg, hjpeg->pJpegOutBuffPtr, hjpeg->JpegOutCount);
04070 #else
04071       HAL_JPEG_DataReadyCallback(hjpeg, hjpeg->pJpegOutBuffPtr, hjpeg->JpegOutCount);
04072 #endif /* USE_HAL_JPEG_REGISTER_CALLBACKS */
04073 
04074       if ((hjpeg->Context &  JPEG_CONTEXT_PAUSE_OUTPUT) == 0UL)
04075       {
04076         /* Start MDMA FIFO Out transfer */
04077         if (HAL_MDMA_Start_IT(hjpeg->hdmaout, (uint32_t)&hjpeg->Instance->DOR, (uint32_t)hjpeg->pJpegOutBuffPtr,
04078                               hjpeg->OutDataLength, 1) != HAL_OK)
04079         {
04080           hjpeg->ErrorCode |= HAL_JPEG_ERROR_DMA;
04081           hjpeg->State = HAL_JPEG_STATE_ERROR;
04082 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
04083           hjpeg->ErrorCallback(hjpeg);
04084 #else
04085           HAL_JPEG_ErrorCallback(hjpeg);
04086 #endif /* USE_HAL_JPEG_REGISTER_CALLBACKS */
04087           return;
04088         }
04089       }
04090     }
04091 
04092     /* JPEG Conversion still on going : Enable the JPEG IT */
04093     __HAL_JPEG_ENABLE_IT(hjpeg, JPEG_IT_EOC | JPEG_IT_HPD);
04094   }
04095 
04096 }
04097 
04098 /**
04099   * @brief  MDMA Transfer error callback
04100   * @param  hmdma pointer to a MDMA_HandleTypeDef structure.
04101   * @retval None
04102   */
04103 static void JPEG_MDMAErrorCallback(MDMA_HandleTypeDef *hmdma)
04104 {
04105   JPEG_HandleTypeDef *hjpeg = (JPEG_HandleTypeDef *)((MDMA_HandleTypeDef *)hmdma)->Parent;
04106 
04107   /*Stop Encoding/Decoding*/
04108   hjpeg->Instance->CONFR0 &=  ~JPEG_CONFR0_START;
04109 
04110   /* Disable All Interrupts */
04111   __HAL_JPEG_DISABLE_IT(hjpeg, JPEG_INTERRUPT_MASK);
04112 
04113   hjpeg->State = HAL_JPEG_STATE_READY;
04114   hjpeg->ErrorCode |= HAL_JPEG_ERROR_DMA;
04115 
04116 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
04117   hjpeg->ErrorCallback(hjpeg);
04118 #else
04119   HAL_JPEG_ErrorCallback(hjpeg);
04120 #endif /* USE_HAL_JPEG_REGISTER_CALLBACKS */
04121 }
04122 
04123 /**
04124   * @brief  MDMA output Abort callback
04125   * @param  hmdma pointer to a MDMA_HandleTypeDef structure.
04126   * @retval None
04127   */
04128 static void JPEG_MDMAOutAbortCallback(MDMA_HandleTypeDef *hmdma)
04129 {
04130   JPEG_HandleTypeDef *hjpeg = (JPEG_HandleTypeDef *)((MDMA_HandleTypeDef *)hmdma)->Parent;
04131 
04132   if ((hjpeg->Context & JPEG_CONTEXT_ENDING_DMA) != 0UL)
04133   {
04134     JPEG_DMA_EndProcess(hjpeg);
04135   }
04136 }
04137 
04138 
04139 /**
04140   * @brief  Calculate the decoded image quality (from 1 to 100)
04141   * @param  hjpeg pointer to a JPEG_HandleTypeDef structure that contains
04142   *         the configuration information for JPEG module
04143   * @retval JPEG image quality from 1 to 100.
04144   */
04145 static uint32_t JPEG_GetQuality(JPEG_HandleTypeDef *hjpeg)
04146 {
04147   uint32_t quality = 0;
04148   uint32_t quantRow, quantVal, scale, i, j;
04149   __IO uint32_t *tableAddress = hjpeg->Instance->QMEM0;
04150 
04151   i = 0;
04152   while (i < (JPEG_QUANT_TABLE_SIZE - 3UL))
04153   {
04154     quantRow = *tableAddress;
04155     for (j = 0; j < 4UL; j++)
04156     {
04157       quantVal = (quantRow >> (8UL * j)) & 0xFFUL;
04158       if (quantVal == 1UL)
04159       {
04160         /* if Quantization value = 1 then quality is 100%*/
04161         quality += 100UL;
04162       }
04163       else
04164       {
04165         /* Note that the quantization coefficients must be specified in the table in zigzag order */
04166         scale = (quantVal * 100UL) / ((uint32_t) hjpeg->QuantTable0[JPEG_ZIGZAG_ORDER[i + j]]);
04167 
04168         if (scale <= 100UL)
04169         {
04170           quality += (200UL - scale) / 2UL;
04171         }
04172         else
04173         {
04174           quality += 5000UL / scale;
04175         }
04176       }
04177     }
04178 
04179     i += 4UL;
04180     tableAddress ++;
04181   }
04182 
04183   return (quality / 64UL);
04184 }
04185 /**
04186   * @}
04187   */
04188 
04189 /**
04190   * @}
04191   */
04192 #endif /* JPEG */
04193 #endif /* HAL_JPEG_MODULE_ENABLED */
04194 
04195 
04196 /**
04197   * @}
04198   */
04199