Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:23:08

0001 /**
0002   ******************************************************************************
0003   * @file    stm32h7xx_hal_jpeg.c
0004   * @author  MCD Application Team
0005   * @brief   JPEG HAL module driver.
0006   *          This file provides firmware functions to manage the following
0007   *          functionalities of the JPEG encoder/decoder peripheral:
0008   *           + Initialization and de-initialization functions
0009   *           + JPEG processing functions encoding and decoding
0010   *           + JPEG decoding Getting Info and encoding configuration setting
0011   *           + JPEG enable/disable header parsing functions (for decoding)
0012   *           + JPEG Input/Output Buffer configuration.
0013   *           + JPEG callback functions
0014   *           + JPEG Abort/Pause/Resume functions
0015   *           + JPEG custom quantization tables setting functions
0016   *           + IRQ handler management
0017   *           + Peripheral State and Error functions
0018   *
0019   ******************************************************************************
0020   * @attention
0021   *
0022   * Copyright (c) 2017 STMicroelectronics.
0023   * All rights reserved.
0024   *
0025   * This software is licensed under terms that can be found in the LICENSE file
0026   * in the root directory of this software component.
0027   * If no LICENSE file comes with this software, it is provided AS-IS.
0028   *
0029   ******************************************************************************
0030   @verbatim
0031   ==============================================================================
0032                      ##### How to use this driver #####
0033   ==============================================================================
0034     [..]
0035      (#) Initialize the JPEG peripheral using HAL_JPEG_Init : No initialization parameters are required.
0036          Only the call to HAL_JPEG_Init is necessary to initialize the JPEG peripheral.
0037 
0038      (#) If operation is JPEG encoding use function HAL_JPEG_ConfigEncoding to set
0039          the encoding parameters (mandatory before calling the encoding function).
0040          the application can change the encoding parameter ImageQuality from
0041          1 to 100 to obtain a more or less quality (visual quality vs the original row image),
0042          and inversely more or less jpg file size.
0043 
0044      (#) Note that for decoding operation the JPEG peripheral output data are organized in
0045          YCbCr blocks called MCU (Minimum Coded Unit) as defioned in the JPEG specification
0046          ISO/IEC 10918-1 standard.
0047          It is up to the application to transform these YCbCr blocks to RGB data that can be display.
0048 
0049          Respectively, for Encoding operation the JPEG peripheral input should be organized
0050          in YCbCr MCU blocks. It is up to the application to perform the necessary RGB to YCbCr
0051          MCU blocks transformation before feeding the JPEG peripheral with data.
0052 
0053      (#) Use functions HAL_JPEG_Encode and HAL_JPEG_Decode to start respectively
0054          a JPEG encoding/decoding operation in polling method (blocking).
0055 
0056      (#) Use functions HAL_JPEG_Encode_IT and HAL_JPEG_Decode_IT to start respectively
0057          a JPEG encoding/decoding operation with Interrupt method (not blocking).
0058 
0059      (#) Use functions HAL_JPEG_Encode_DMA and HAL_JPEG_Decode_DMA to start respectively
0060          a JPEG encoding/decoding operation with DMA method (not blocking).
0061 
0062      (#) Callback HAL_JPEG_InfoReadyCallback is asserted if the current operation
0063          is a JPEG decoding to provide the application with JPEG image  parameters.
0064          This callback is asserted when the JPEG peripheral successfully parse the
0065          JPEG header.
0066 
0067      (#) Callback HAL_JPEG_GetDataCallback is asserted for both encoding and decoding
0068          operations to inform the application that the input buffer has been
0069          consumed by the peripheral and to ask for a new data chunk if the operation
0070          (encoding/decoding) has not been complete yet.
0071 
0072         (++) This CallBack should be implemented in the application side. It should
0073              call the function HAL_JPEG_ConfigInputBuffer if new input data are available,
0074              or call HAL_JPEG_Pause with parameter XferSelection set to JPEG_PAUSE_RESUME_INPUT
0075              to inform the JPEG HAL driver that the ongoing operation shall pause waiting for the
0076              application to provide a new input data chunk.
0077              Once the application succeed getting new data and if the input has been paused,
0078              the application can call the function HAL_JPEG_ConfigInputBuffer to set the new
0079              input buffer and size, then resume the JPEG HAL input by calling new function HAL_JPEG_Resume.
0080              If the application has ended feeding the HAL JPEG with input data (no more input data), the application
0081              Should call the function HAL_JPEG_ConfigInputBuffer (within the callback HAL_JPEG_GetDataCallback)
0082              with the parameter InDataLength set to zero.
0083 
0084          (++) The mechanism of HAL_JPEG_ConfigInputBuffer/HAL_JPEG_Pause/HAL_JPEG_Resume allows
0085               to the application to provide the input data (for encoding or decoding) by chunks.
0086               If the new input data chunk is not available (because data should be read from an input file
0087               for example) the application can pause the JPEG input (using function HAL_JPEG_Pause)
0088               Once the new input data chunk is available ( read from a file for example), the application
0089               can call the function HAL_JPEG_ConfigInputBuffer to provide the HAL with the new chunk
0090               then resume the JPEG HAL input by calling function HAL_JPEG_Resume.
0091 
0092          (++) The application can call functions HAL_JPEG_ConfigInputBuffer then HAL_JPEG_Resume.
0093               any time (outside the HAL_JPEG_GetDataCallback)  Once the new input chunk data available.
0094               However, to keep data coherency, the function HAL_JPEG_Pause must be imperatively called
0095               (if necessary) within the callback HAL_JPEG_GetDataCallback, i.e when the HAL JPEG has ended
0096               Transferring the previous chunk buffer to the JPEG peripheral.
0097 
0098      (#) Callback HAL_JPEG_DataReadyCallback is asserted when the HAL JPEG driver
0099          has filled the given output buffer with the given size.
0100 
0101          (++) This CallBack should be implemented in the application side. It should
0102               call the function HAL_JPEG_ConfigOutputBuffer to provide the HAL JPEG driver
0103               with the new output buffer location and size to be used  to store next data chunk.
0104               if the application is not ready to provide the output chunk location then it can
0105               call the function HAL_JPEG_Pause with parameter XferSelection set to JPEG_PAUSE_RESUME_OUTPUT
0106               to inform the JPEG HAL driver that it shall pause output data. Once the application
0107               is ready to receive the new data chunk (output buffer location free or available) it should call
0108               the function HAL_JPEG_ConfigOutputBuffer to provide the HAL JPEG driver
0109               with the new output chunk buffer location and size, then call HAL_JPEG_Resume
0110               to inform the HAL that it shall resume outputting data in the given output buffer.
0111 
0112          (++) The mechanism of HAL_JPEG_ConfigOutputBuffer/HAL_JPEG_Pause/HAL_JPEG_Resume allows
0113               the application to receive data from the JPEG peripheral by chunks. when a chunk
0114               is received, the application can pause the HAL JPEG output data to be able to process
0115               these received data (YCbCr to RGB conversion in case of decoding or data storage in case
0116               of encoding).
0117 
0118          (++) The application can call  functions HAL_JPEG_ ConfigOutputBuffer then HAL_JPEG_Resume.
0119               any time (outside the HAL_JPEG_DataReadyCallback) Once the output data buffer is free to use.
0120               However, to keep data coherency, the function HAL_JPEG_Pause must be imperatively called
0121               (if necessary) within the callback HAL_JPEG_ DataReadyCallback, i.e when the HAL JPEG has ended
0122               Transferring the previous chunk buffer from the JPEG peripheral to the application.
0123 
0124      (#) Callback HAL_JPEG_EncodeCpltCallback is asserted when the HAL JPEG driver has
0125          ended the current JPEG encoding operation, and all output data has been transmitted
0126          to the application.
0127 
0128      (#) Callback HAL_JPEG_DecodeCpltCallback is asserted when the HAL JPEG driver has
0129          ended the current JPEG decoding operation. and all output data has been transmitted
0130          to the application.
0131 
0132      (#) Callback HAL_JPEG_ErrorCallback is asserted when an error occurred during
0133          the current operation. the application can call the function HAL_JPEG_GetError()
0134          to retrieve the error codes.
0135 
0136      (#) By default the HAL JPEG driver uses the default quantization tables
0137          as provide in the JPEG specification (ISO/IEC 10918-1 standard) for encoding.
0138          User can change these default tables if necessary using the function HAL_JPEG_SetUserQuantTables
0139          Note that for decoding the quantization tables are automatically extracted from
0140          the JPEG header.
0141 
0142       (#) To control JPEG state you can use the following function: HAL_JPEG_GetState()
0143 
0144      *** JPEG HAL driver macros list ***
0145      =============================================
0146      [..]
0147        Below the list of most used macros in JPEG HAL driver.
0148 
0149       (+) __HAL_JPEG_RESET_HANDLE_STATE : Reset JPEG handle state.
0150       (+) __HAL_JPEG_ENABLE             : Enable the JPEG peripheral.
0151       (+) __HAL_JPEG_DISABLE            : Disable the JPEG peripheral.
0152       (+) __HAL_JPEG_GET_FLAG           : Check the specified JPEG status flag.
0153       (+) __HAL_JPEG_CLEAR_FLAG         : Clear the specified JPEG status flag.
0154       (+) __HAL_JPEG_ENABLE_IT          : Enable the specified JPEG Interrupt.
0155       (+) __HAL_JPEG_DISABLE_IT         : Disable the specified JPEG Interrupt.
0156       (+) __HAL_JPEG_GET_IT_SOURCE      : returns the state of the specified JPEG Interrupt (Enabled or disabled).
0157 
0158   *** Callback registration ***
0159   =============================================
0160 
0161   The compilation define  USE_HAL_JPEG_REGISTER_CALLBACKS when set to 1
0162   allows the user to configure dynamically the driver callbacks.
0163   Use Functions HAL_JPEG_RegisterCallback() or HAL_JPEG_RegisterXXXCallback()
0164   to register an interrupt callback.
0165 
0166   Function HAL_JPEG_RegisterCallback() allows to register following callbacks:
0167     (+) EncodeCpltCallback : callback for end of encoding operation.
0168     (+) DecodeCpltCallback : callback for end of decoding operation.
0169     (+) ErrorCallback      : callback for error detection.
0170     (+) MspInitCallback    : JPEG MspInit.
0171     (+) MspDeInitCallback  : JPEG MspDeInit.
0172   This function takes as parameters the HAL peripheral handle, the Callback ID
0173   and a pointer to the user callback function.
0174 
0175   For specific callbacks InfoReadyCallback, GetDataCallback and DataReadyCallback use dedicated
0176   register callbacks : respectively HAL_JPEG_RegisterInfoReadyCallback(),
0177   HAL_JPEG_RegisterGetDataCallback() and HAL_JPEG_RegisterDataReadyCallback().
0178 
0179   Use function HAL_JPEG_UnRegisterCallback() to reset a callback to the default
0180   weak function.
0181   HAL_JPEG_UnRegisterCallback() takes as parameters the HAL peripheral handle,
0182   and the Callback ID.
0183   This function allows to reset following callbacks:
0184     (+) EncodeCpltCallback : callback for end of encoding operation.
0185     (+) DecodeCpltCallback : callback for end of decoding operation.
0186     (+) ErrorCallback      : callback for error detection.
0187     (+) MspInitCallback    : JPEG MspInit.
0188     (+) MspDeInitCallback  : JPEG MspDeInit.
0189 
0190   For callbacks InfoReadyCallback, GetDataCallback and DataReadyCallback use dedicated
0191   unregister callbacks : respectively HAL_JPEG_UnRegisterInfoReadyCallback(),
0192   HAL_JPEG_UnRegisterGetDataCallback() and HAL_JPEG_UnRegisterDataReadyCallback().
0193 
0194   By default, after the HAL_JPEG_Init() and when the state is HAL_JPEG_STATE_RESET
0195   all callbacks are set to the corresponding weak functions :
0196   examples HAL_JPEG_DecodeCpltCallback() , HAL_JPEG_GetDataCallback().
0197   Exception done for MspInit and MspDeInit functions that are
0198   reset to the legacy weak function in the HAL_JPEG_Init()/ HAL_JPEG_DeInit() only when
0199   these callbacks are null (not registered beforehand).
0200   if not, MspInit or MspDeInit are not null, the HAL_JPEG_Init() / HAL_JPEG_DeInit()
0201   keep and use the user MspInit/MspDeInit functions (registered beforehand)
0202 
0203   Callbacks can be registered/unregistered in HAL_JPEG_STATE_READY state only.
0204   Exception done MspInit/MspDeInit callbacks that can be registered/unregistered
0205   in HAL_JPEG_STATE_READY or HAL_JPEG_STATE_RESET state,
0206   thus registered (user) MspInit/DeInit callbacks can be used during the Init/DeInit.
0207   In that case first register the MspInit/MspDeInit user callbacks
0208   using HAL_JPEG_RegisterCallback() before calling HAL_JPEG_DeInit()
0209   or HAL_JPEG_Init() function.
0210 
0211   When The compilation define USE_HAL_JPEG_REGISTER_CALLBACKS is set to 0 or
0212   not defined, the callback registration feature is not available and all callbacks
0213   are set to the corresponding weak functions.
0214 
0215   @endverbatim
0216   ******************************************************************************
0217   */
0218 
0219 /* Includes ------------------------------------------------------------------*/
0220 #include "stm32h7xx_hal.h"
0221 
0222 /** @addtogroup STM32H7xx_HAL_Driver
0223   * @{
0224   */
0225 
0226 #ifdef HAL_JPEG_MODULE_ENABLED
0227 
0228 #if defined (JPEG)
0229 
0230 /** @defgroup JPEG JPEG
0231   * @ingroup RTEMSBSPsARMSTM32H7
0232   * @brief JPEG HAL module driver.
0233   * @{
0234   */
0235 
0236 /* Private define ------------------------------------------------------------*/
0237 /** @addtogroup JPEG_Private_Constants
0238   * @{
0239   */
0240 #define JPEG_TIMEOUT_VALUE  ((uint32_t)1000)     /* 1s */
0241 #define JPEG_AC_HUFF_TABLE_SIZE  ((uint32_t)162) /* Huffman AC table size : 162 codes*/
0242 #define JPEG_DC_HUFF_TABLE_SIZE  ((uint32_t)12)  /* Huffman AC table size : 12 codes*/
0243 
0244 #define JPEG_FIFO_SIZE ((uint32_t)16U)             /* JPEG Input/Output HW FIFO size in words*/
0245 
0246 #define JPEG_FIFO_TH_SIZE ((uint32_t)8U)              /* JPEG Input/Output HW FIFO Threshold in words*/
0247 
0248 #define JPEG_INTERRUPT_MASK  ((uint32_t)0x0000007EU) /* JPEG Interrupt Mask*/
0249 
0250 #define JPEG_CONTEXT_ENCODE          ((uint32_t)0x00000001) /* JPEG context : operation is encoding*/
0251 #define JPEG_CONTEXT_DECODE          ((uint32_t)0x00000002) /* JPEG context : operation is decoding*/
0252 #define JPEG_CONTEXT_OPERATION_MASK  ((uint32_t)0x00000003) /* JPEG context : operation Mask */
0253 
0254 #define JPEG_CONTEXT_POLLING        ((uint32_t)0x00000004)  /* JPEG context : Transfer use Polling */
0255 #define JPEG_CONTEXT_IT             ((uint32_t)0x00000008)  /* JPEG context : Transfer use Interrupt */
0256 #define JPEG_CONTEXT_DMA            ((uint32_t)0x0000000C)  /* JPEG context : Transfer use DMA */
0257 #define JPEG_CONTEXT_METHOD_MASK    ((uint32_t)0x0000000C)  /* JPEG context : Transfer Mask */
0258 
0259 
0260 #define JPEG_CONTEXT_CONF_ENCODING  ((uint32_t)0x00000100)  /* JPEG context : encoding config done */
0261 
0262 #define JPEG_CONTEXT_PAUSE_INPUT    ((uint32_t)0x00001000)  /* JPEG context : Pause Input */
0263 #define JPEG_CONTEXT_PAUSE_OUTPUT   ((uint32_t)0x00002000)  /* JPEG context : Pause Output */
0264 
0265 #define JPEG_CONTEXT_CUSTOM_TABLES  ((uint32_t)0x00004000)  /* JPEG context : Use custom quantization tables */
0266 
0267 #define JPEG_CONTEXT_ENDING_DMA     ((uint32_t)0x00008000)  /* JPEG context : ending with DMA in progress */
0268 
0269 #define JPEG_PROCESS_ONGOING        ((uint32_t)0x00000000)  /* Process is on going */
0270 #define JPEG_PROCESS_DONE           ((uint32_t)0x00000001)  /* Process is done (ends) */
0271 /**
0272   * @}
0273   */
0274 
0275 /* Private typedef -----------------------------------------------------------*/
0276 /** @addtogroup JPEG_Private_Types
0277   * @{
0278   */
0279 
0280 /*
0281  JPEG Huffman Table Structure definition :
0282  This implementation of Huffman table structure is compliant with ISO/IEC 10918-1 standard , Annex C Huffman Table specification
0283  */
0284 typedef struct
0285 {
0286   /* These two fields directly represent the contents of a JPEG DHT marker */
0287   uint8_t Bits[16];        /*!< bits[k] = # of symbols with codes of length k bits, this parameter corresponds to BITS list in the Annex C */
0288 
0289   uint8_t HuffVal[162];    /*!< The symbols, in order of incremented code length, this parameter corresponds to HUFFVAL list in the Annex C */
0290 
0291 
0292 } JPEG_ACHuffTableTypeDef;
0293 
0294 typedef struct
0295 {
0296   /* These two fields directly represent the contents of a JPEG DHT marker */
0297   uint8_t Bits[16];        /*!< bits[k] = # of symbols with codes of length k bits, this parameter corresponds to BITS list in the Annex C */
0298 
0299   uint8_t HuffVal[12];    /*!< The symbols, in order of incremented code length, this parameter corresponds to HUFFVAL list in the Annex C */
0300 
0301 
0302 } JPEG_DCHuffTableTypeDef;
0303 
0304 typedef struct
0305 {
0306   uint8_t CodeLength[JPEG_AC_HUFF_TABLE_SIZE];      /*!< Code length  */
0307 
0308   uint32_t HuffmanCode[JPEG_AC_HUFF_TABLE_SIZE];    /*!< HuffmanCode */
0309 
0310 } JPEG_AC_HuffCodeTableTypeDef;
0311 
0312 typedef struct
0313 {
0314   uint8_t CodeLength[JPEG_DC_HUFF_TABLE_SIZE];        /*!< Code length  */
0315 
0316   uint32_t HuffmanCode[JPEG_DC_HUFF_TABLE_SIZE];    /*!< HuffmanCode */
0317 
0318 } JPEG_DC_HuffCodeTableTypeDef;
0319 /**
0320   * @}
0321   */
0322 
0323 /* Private macro -------------------------------------------------------------*/
0324 
0325 /* Private variables ---------------------------------------------------------*/
0326 /** @addtogroup JPEG_Private_Variables
0327   * @{
0328   */
0329 
0330 static const JPEG_DCHuffTableTypeDef JPEG_DCLUM_HuffTable =
0331 {
0332   { 0, 1, 5, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0 },   /*Bits*/
0333 
0334   { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0xa, 0xb }           /*HUFFVAL */
0335 
0336 };
0337 
0338 static const JPEG_DCHuffTableTypeDef JPEG_DCCHROM_HuffTable =
0339 {
0340   { 0, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0 },  /*Bits*/
0341 
0342   { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0xa, 0xb }          /*HUFFVAL */
0343 };
0344 
0345 static const JPEG_ACHuffTableTypeDef JPEG_ACLUM_HuffTable =
0346 {
0347   { 0, 2, 1, 3, 3, 2, 4, 3, 5, 5, 4, 4, 0, 0, 1, 0x7d },  /*Bits*/
0348 
0349   {
0350     0x01, 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12,     /*HUFFVAL */
0351     0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61, 0x07,
0352     0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xa1, 0x08,
0353     0x23, 0x42, 0xb1, 0xc1, 0x15, 0x52, 0xd1, 0xf0,
0354     0x24, 0x33, 0x62, 0x72, 0x82, 0x09, 0x0a, 0x16,
0355     0x17, 0x18, 0x19, 0x1a, 0x25, 0x26, 0x27, 0x28,
0356     0x29, 0x2a, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
0357     0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49,
0358     0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59,
0359     0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69,
0360     0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79,
0361     0x7a, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89,
0362     0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
0363     0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
0364     0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6,
0365     0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3, 0xc4, 0xc5,
0366     0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 0xd3, 0xd4,
0367     0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe1, 0xe2,
0368     0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea,
0369     0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
0370     0xf9, 0xfa
0371   }
0372 };
0373 
0374 static const JPEG_ACHuffTableTypeDef JPEG_ACCHROM_HuffTable =
0375 {
0376   { 0, 2, 1, 2, 4, 4, 3, 4, 7, 5, 4, 4, 0, 1, 2, 0x77 },   /*Bits*/
0377 
0378   {
0379     0x00, 0x01, 0x02, 0x03, 0x11, 0x04, 0x05, 0x21,      /*HUFFVAL */
0380     0x31, 0x06, 0x12, 0x41, 0x51, 0x07, 0x61, 0x71,
0381     0x13, 0x22, 0x32, 0x81, 0x08, 0x14, 0x42, 0x91,
0382     0xa1, 0xb1, 0xc1, 0x09, 0x23, 0x33, 0x52, 0xf0,
0383     0x15, 0x62, 0x72, 0xd1, 0x0a, 0x16, 0x24, 0x34,
0384     0xe1, 0x25, 0xf1, 0x17, 0x18, 0x19, 0x1a, 0x26,
0385     0x27, 0x28, 0x29, 0x2a, 0x35, 0x36, 0x37, 0x38,
0386     0x39, 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48,
0387     0x49, 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58,
0388     0x59, 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68,
0389     0x69, 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78,
0390     0x79, 0x7a, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
0391     0x88, 0x89, 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96,
0392     0x97, 0x98, 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5,
0393     0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4,
0394     0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3,
0395     0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2,
0396     0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda,
0397     0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9,
0398     0xea, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
0399     0xf9, 0xfa
0400   }
0401 };
0402 
0403 static const uint8_t JPEG_ZIGZAG_ORDER[JPEG_QUANT_TABLE_SIZE] =
0404 {
0405   0,   1,   8,  16,   9,   2,   3,  10,
0406   17,  24,  32,  25,  18,  11,   4,   5,
0407   12,  19,  26,  33,  40,  48,  41,  34,
0408   27,  20,  13,   6,   7,  14,  21,  28,
0409   35,  42,  49,  56,  57,  50,  43,  36,
0410   29,  22,  15,  23,  30,  37,  44,  51,
0411   58,  59,  52,  45,  38,  31,  39,  46,
0412   53,  60,  61,  54,  47,  55,  62,  63
0413 };
0414 /**
0415   * @}
0416   */
0417 
0418 /* Private function prototypes -----------------------------------------------*/
0419 /** @addtogroup JPEG_Private_Functions_Prototypes
0420   * @{
0421   */
0422 
0423 static HAL_StatusTypeDef JPEG_Bits_To_SizeCodes(uint8_t *Bits, uint8_t *Huffsize, uint32_t *Huffcode, uint32_t *LastK);
0424 static HAL_StatusTypeDef JPEG_DCHuff_BitsVals_To_SizeCodes(JPEG_DCHuffTableTypeDef *DC_BitsValsTable,
0425                                                            JPEG_DC_HuffCodeTableTypeDef *DC_SizeCodesTable);
0426 static HAL_StatusTypeDef JPEG_ACHuff_BitsVals_To_SizeCodes(JPEG_ACHuffTableTypeDef *AC_BitsValsTable,
0427                                                            JPEG_AC_HuffCodeTableTypeDef *AC_SizeCodesTable);
0428 static HAL_StatusTypeDef JPEG_Set_HuffDC_Mem(JPEG_HandleTypeDef *hjpeg, JPEG_DCHuffTableTypeDef *HuffTableDC,
0429                                              const __IO uint32_t *DCTableAddress);
0430 static HAL_StatusTypeDef JPEG_Set_HuffAC_Mem(JPEG_HandleTypeDef *hjpeg, JPEG_ACHuffTableTypeDef *HuffTableAC,
0431                                              const __IO uint32_t *ACTableAddress);
0432 static HAL_StatusTypeDef JPEG_Set_HuffEnc_Mem(JPEG_HandleTypeDef *hjpeg);
0433 static void JPEG_Set_Huff_DHTMem(JPEG_HandleTypeDef *hjpeg);
0434 static uint32_t  JPEG_Set_Quantization_Mem(JPEG_HandleTypeDef *hjpeg, uint8_t *QTable,
0435                                                     __IO uint32_t *QTableAddress);
0436 static void JPEG_SetColorYCBCR(JPEG_HandleTypeDef *hjpeg);
0437 static void JPEG_SetColorGrayScale(JPEG_HandleTypeDef *hjpeg);
0438 static void JPEG_SetColorCMYK(JPEG_HandleTypeDef *hjpeg);
0439 
0440 static void JPEG_Init_Process(JPEG_HandleTypeDef *hjpeg);
0441 static uint32_t JPEG_Process(JPEG_HandleTypeDef *hjpeg);
0442 static void JPEG_ReadInputData(JPEG_HandleTypeDef *hjpeg, uint32_t nbRequestWords);
0443 static void JPEG_StoreOutputData(JPEG_HandleTypeDef *hjpeg, uint32_t nbOutputWords);
0444 static uint32_t JPEG_GetQuality(JPEG_HandleTypeDef *hjpeg);
0445 
0446 static HAL_StatusTypeDef JPEG_DMA_StartProcess(JPEG_HandleTypeDef *hjpeg);
0447 static void JPEG_DMA_ContinueProcess(JPEG_HandleTypeDef *hjpeg);
0448 static void JPEG_DMA_EndProcess(JPEG_HandleTypeDef *hjpeg);
0449 static void JPEG_DMA_PollResidualData(JPEG_HandleTypeDef *hjpeg);
0450 static void JPEG_MDMAOutCpltCallback(MDMA_HandleTypeDef *hmdma);
0451 static void JPEG_MDMAInCpltCallback(MDMA_HandleTypeDef *hmdma);
0452 static void JPEG_MDMAErrorCallback(MDMA_HandleTypeDef *hmdma);
0453 static void JPEG_MDMAOutAbortCallback(MDMA_HandleTypeDef *hmdma);
0454 
0455 /**
0456   * @}
0457   */
0458 
0459 /** @defgroup JPEG_Exported_Functions JPEG Exported Functions
0460   * @ingroup RTEMSBSPsARMSTM32H7
0461   * @{
0462   */
0463 
0464 /** @defgroup JPEG_Exported_Functions_Group1 Initialization and de-initialization functions
0465   * @ingroup RTEMSBSPsARMSTM32H7
0466   *  @brief    Initialization and de-initialization functions.
0467   *
0468 @verbatim
0469   ==============================================================================
0470               ##### Initialization and de-initialization functions #####
0471   ==============================================================================
0472     [..]  This section provides functions allowing to:
0473       (+) Initialize the JPEG peripheral and creates the associated handle
0474       (+) DeInitialize the JPEG peripheral
0475 
0476 @endverbatim
0477   * @{
0478   */
0479 
0480 /**
0481   * @brief  Initializes the JPEG according to the specified
0482   *         parameters in the JPEG_InitTypeDef and creates the associated handle.
0483   * @param  hjpeg pointer to a JPEG_HandleTypeDef structure that contains
0484   *         the configuration information for JPEG module
0485   * @retval HAL status
0486   */
0487 HAL_StatusTypeDef HAL_JPEG_Init(JPEG_HandleTypeDef *hjpeg)
0488 {
0489   /* These are the sample quantization tables given in JPEG spec ISO/IEC 10918-1 standard , section K.1. */
0490   static const uint8_t JPEG_LUM_QuantTable[JPEG_QUANT_TABLE_SIZE] =
0491   {
0492     16,  11,  10,  16,  24,  40,  51,  61,
0493     12,  12,  14,  19,  26,  58,  60,  55,
0494     14,  13,  16,  24,  40,  57,  69,  56,
0495     14,  17,  22,  29,  51,  87,  80,  62,
0496     18,  22,  37,  56,  68, 109, 103,  77,
0497     24,  35,  55,  64,  81, 104, 113,  92,
0498     49,  64,  78,  87, 103, 121, 120, 101,
0499     72,  92,  95,  98, 112, 100, 103,  99
0500   };
0501   static const uint8_t JPEG_CHROM_QuantTable[JPEG_QUANT_TABLE_SIZE] =
0502   {
0503     17,  18,  24,  47,  99,  99,  99,  99,
0504     18,  21,  26,  66,  99,  99,  99,  99,
0505     24,  26,  56,  99,  99,  99,  99,  99,
0506     47,  66,  99,  99,  99,  99,  99,  99,
0507     99,  99,  99,  99,  99,  99,  99,  99,
0508     99,  99,  99,  99,  99,  99,  99,  99,
0509     99,  99,  99,  99,  99,  99,  99,  99,
0510     99,  99,  99,  99,  99,  99,  99,  99
0511   };
0512 
0513   /* Check the JPEG handle allocation */
0514   if (hjpeg == NULL)
0515   {
0516     return HAL_ERROR;
0517   }
0518 
0519 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
0520   if (hjpeg->State == HAL_JPEG_STATE_RESET)
0521   {
0522     /* Allocate lock resource and initialize it */
0523     hjpeg->Lock = HAL_UNLOCKED;
0524 
0525     hjpeg->InfoReadyCallback  = HAL_JPEG_InfoReadyCallback;  /* Legacy weak InfoReadyCallback  */
0526     hjpeg->EncodeCpltCallback = HAL_JPEG_EncodeCpltCallback; /* Legacy weak EncodeCpltCallback */
0527     hjpeg->DecodeCpltCallback = HAL_JPEG_DecodeCpltCallback; /* Legacy weak DecodeCpltCallback */
0528     hjpeg->ErrorCallback      = HAL_JPEG_ErrorCallback;      /* Legacy weak ErrorCallback      */
0529     hjpeg->GetDataCallback    = HAL_JPEG_GetDataCallback;    /* Legacy weak GetDataCallback    */
0530     hjpeg->DataReadyCallback  = HAL_JPEG_DataReadyCallback;  /* Legacy weak DataReadyCallback  */
0531 
0532     if (hjpeg->MspInitCallback == NULL)
0533     {
0534       hjpeg->MspInitCallback = HAL_JPEG_MspInit; /* Legacy weak MspInit  */
0535     }
0536 
0537     /* Init the low level hardware */
0538     hjpeg->MspInitCallback(hjpeg);
0539   }
0540 #else
0541   if (hjpeg->State == HAL_JPEG_STATE_RESET)
0542   {
0543     /* Allocate lock resource and initialize it */
0544     hjpeg->Lock = HAL_UNLOCKED;
0545 
0546     /* Init the low level hardware : GPIO, CLOCK */
0547     HAL_JPEG_MspInit(hjpeg);
0548   }
0549 #endif /* USE_HAL_JPEG_REGISTER_CALLBACKS */
0550 
0551   /* Change the JPEG state */
0552   hjpeg->State = HAL_JPEG_STATE_BUSY;
0553 
0554   /* Start the JPEG Core*/
0555   __HAL_JPEG_ENABLE(hjpeg);
0556 
0557   /* Stop the JPEG encoding/decoding process*/
0558   hjpeg->Instance->CONFR0 &=  ~JPEG_CONFR0_START;
0559 
0560   /* Disable All Interrupts */
0561   __HAL_JPEG_DISABLE_IT(hjpeg, JPEG_INTERRUPT_MASK);
0562 
0563 
0564   /* Flush input and output FIFOs*/
0565   hjpeg->Instance->CR |= JPEG_CR_IFF;
0566   hjpeg->Instance->CR |= JPEG_CR_OFF;
0567 
0568   /* Clear all flags */
0569   __HAL_JPEG_CLEAR_FLAG(hjpeg, JPEG_FLAG_ALL);
0570 
0571   /* init default quantization tables*/
0572   hjpeg->QuantTable0 = (uint8_t *)((uint32_t)JPEG_LUM_QuantTable);
0573   hjpeg->QuantTable1 = (uint8_t *)((uint32_t)JPEG_CHROM_QuantTable);
0574   hjpeg->QuantTable2 = NULL;
0575   hjpeg->QuantTable3 = NULL;
0576 
0577   /* init the default Huffman tables*/
0578   if (JPEG_Set_HuffEnc_Mem(hjpeg) != HAL_OK)
0579   {
0580     hjpeg->ErrorCode = HAL_JPEG_ERROR_HUFF_TABLE;
0581 
0582     return HAL_ERROR;
0583   }
0584 
0585   /* Enable header processing*/
0586   hjpeg->Instance->CONFR1 |= JPEG_CONFR1_HDR;
0587 
0588   /* Reset JpegInCount and JpegOutCount */
0589   hjpeg->JpegInCount = 0;
0590   hjpeg->JpegOutCount = 0;
0591 
0592   /* Change the JPEG state */
0593   hjpeg->State = HAL_JPEG_STATE_READY;
0594 
0595   /* Reset the JPEG ErrorCode */
0596   hjpeg->ErrorCode = HAL_JPEG_ERROR_NONE;
0597 
0598   /*Clear the context filelds*/
0599   hjpeg->Context = 0;
0600 
0601   /* Return function status */
0602   return HAL_OK;
0603 }
0604 
0605 /**
0606   * @brief  DeInitializes the JPEG peripheral.
0607   * @param  hjpeg pointer to a JPEG_HandleTypeDef structure that contains
0608   *         the configuration information for JPEG module
0609   * @retval HAL status
0610   */
0611 HAL_StatusTypeDef HAL_JPEG_DeInit(JPEG_HandleTypeDef *hjpeg)
0612 {
0613   /* Check the JPEG handle allocation */
0614   if (hjpeg == NULL)
0615   {
0616     return HAL_ERROR;
0617   }
0618 
0619 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
0620   if (hjpeg->MspDeInitCallback == NULL)
0621   {
0622     hjpeg->MspDeInitCallback = HAL_JPEG_MspDeInit; /* Legacy weak MspDeInit  */
0623   }
0624 
0625   /* DeInit the low level hardware */
0626   hjpeg->MspDeInitCallback(hjpeg);
0627 
0628 #else
0629   /* DeInit the low level hardware: CLOCK, NVIC.*/
0630   HAL_JPEG_MspDeInit(hjpeg);
0631 #endif /* USE_HAL_JPEG_REGISTER_CALLBACKS */
0632 
0633   /* Change the JPEG state */
0634   hjpeg->State = HAL_JPEG_STATE_BUSY;
0635 
0636   /* Reset the JPEG ErrorCode */
0637   hjpeg->ErrorCode = HAL_JPEG_ERROR_NONE;
0638 
0639   /* Reset JpegInCount and JpegOutCount */
0640   hjpeg->JpegInCount = 0;
0641   hjpeg->JpegOutCount = 0;
0642 
0643   /* Change the JPEG state */
0644   hjpeg->State = HAL_JPEG_STATE_RESET;
0645 
0646   /*Clear the context fields*/
0647   hjpeg->Context = 0;
0648 
0649   /* Release Lock */
0650   __HAL_UNLOCK(hjpeg);
0651 
0652   /* Return function status */
0653   return HAL_OK;
0654 }
0655 
0656 /**
0657   * @brief  Initializes the JPEG MSP.
0658   * @param  hjpeg pointer to a JPEG_HandleTypeDef structure that contains
0659   *         the configuration information for JPEG module
0660   * @retval None
0661   */
0662 __weak void HAL_JPEG_MspInit(JPEG_HandleTypeDef *hjpeg)
0663 {
0664   /* Prevent unused argument(s) compilation warning */
0665   UNUSED(hjpeg);
0666 
0667   /* NOTE : This function Should not be modified, when the callback is needed,
0668             the HAL_JPEG_MspInit could be implemented in the user file
0669    */
0670 }
0671 
0672 /**
0673   * @brief  DeInitializes JPEG MSP.
0674   * @param  hjpeg pointer to a JPEG_HandleTypeDef structure that contains
0675   *         the configuration information for JPEG module
0676   * @retval None
0677   */
0678 __weak void HAL_JPEG_MspDeInit(JPEG_HandleTypeDef *hjpeg)
0679 {
0680   /* Prevent unused argument(s) compilation warning */
0681   UNUSED(hjpeg);
0682 
0683   /* NOTE : This function Should not be modified, when the callback is needed,
0684             the HAL_JPEG_MspDeInit could be implemented in the user file
0685    */
0686 }
0687 
0688 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
0689 /**
0690   * @brief  Register a User JPEG Callback
0691   *         To be used instead of the weak predefined callback
0692   * @param  hjpeg JPEG handle
0693   * @param  CallbackID ID of the callback to be registered
0694   *         This parameter can be one of the following values:
0695   *          @arg @ref HAL_JPEG_ENCODE_CPLT_CB_ID Encode Complete callback ID
0696   *          @arg @ref HAL_JPEG_DECODE_CPLT_CB_ID Decode Complete callback ID
0697   *          @arg @ref HAL_JPEG_ERROR_CB_ID Error callback ID
0698   *          @arg @ref HAL_JPEG_MSPINIT_CB_ID MspInit callback ID
0699   *          @arg @ref HAL_JPEG_MSPDEINIT_CB_ID MspDeInit callback ID
0700   * @param  pCallback pointer to the Callback function
0701   * @retval HAL status
0702   */
0703 HAL_StatusTypeDef HAL_JPEG_RegisterCallback(JPEG_HandleTypeDef *hjpeg, HAL_JPEG_CallbackIDTypeDef CallbackID,
0704                                             pJPEG_CallbackTypeDef pCallback)
0705 {
0706   HAL_StatusTypeDef status = HAL_OK;
0707 
0708   if (pCallback == NULL)
0709   {
0710     /* Update the error code */
0711     hjpeg->ErrorCode |= HAL_JPEG_ERROR_INVALID_CALLBACK;
0712     return HAL_ERROR;
0713   }
0714   /* Process locked */
0715   __HAL_LOCK(hjpeg);
0716 
0717   if (HAL_JPEG_STATE_READY == hjpeg->State)
0718   {
0719     switch (CallbackID)
0720     {
0721       case HAL_JPEG_ENCODE_CPLT_CB_ID :
0722         hjpeg->EncodeCpltCallback = pCallback;
0723         break;
0724 
0725       case HAL_JPEG_DECODE_CPLT_CB_ID :
0726         hjpeg->DecodeCpltCallback = pCallback;
0727         break;
0728 
0729       case HAL_JPEG_ERROR_CB_ID :
0730         hjpeg->ErrorCallback = pCallback;
0731         break;
0732 
0733       case HAL_JPEG_MSPINIT_CB_ID :
0734         hjpeg->MspInitCallback = pCallback;
0735         break;
0736 
0737       case HAL_JPEG_MSPDEINIT_CB_ID :
0738         hjpeg->MspDeInitCallback = pCallback;
0739         break;
0740 
0741       default :
0742         /* Update the error code */
0743         hjpeg->ErrorCode |= HAL_JPEG_ERROR_INVALID_CALLBACK;
0744         /* Return error status */
0745         status =  HAL_ERROR;
0746         break;
0747     }
0748   }
0749   else if (HAL_JPEG_STATE_RESET == hjpeg->State)
0750   {
0751     switch (CallbackID)
0752     {
0753       case HAL_JPEG_MSPINIT_CB_ID :
0754         hjpeg->MspInitCallback = pCallback;
0755         break;
0756 
0757       case HAL_JPEG_MSPDEINIT_CB_ID :
0758         hjpeg->MspDeInitCallback = pCallback;
0759         break;
0760 
0761       default :
0762         /* Update the error code */
0763         hjpeg->ErrorCode |= HAL_JPEG_ERROR_INVALID_CALLBACK;
0764         /* Return error status */
0765         status =  HAL_ERROR;
0766         break;
0767     }
0768   }
0769   else
0770   {
0771     /* Update the error code */
0772     hjpeg->ErrorCode |= HAL_JPEG_ERROR_INVALID_CALLBACK;
0773     /* Return error status */
0774     status =  HAL_ERROR;
0775   }
0776 
0777   /* Release Lock */
0778   __HAL_UNLOCK(hjpeg);
0779   return status;
0780 }
0781 
0782 /**
0783   * @brief  Unregister a JPEG Callback
0784   *         JPEG callabck is redirected to the weak predefined callback
0785   * @param  hjpeg JPEG handle
0786   * @param  CallbackID ID of the callback to be unregistered
0787   *         This parameter can be one of the following values:
0788   *         This parameter can be one of the following values:
0789   *          @arg @ref HAL_JPEG_ENCODE_CPLT_CB_ID Encode Complete callback ID
0790   *          @arg @ref HAL_JPEG_DECODE_CPLT_CB_ID Decode Complete callback ID
0791   *          @arg @ref HAL_JPEG_ERROR_CB_ID Error callback ID
0792   *          @arg @ref HAL_JPEG_MSPINIT_CB_ID MspInit callback ID
0793   *          @arg @ref HAL_JPEG_MSPDEINIT_CB_ID MspDeInit callback ID
0794   * @retval HAL status
0795   */
0796 HAL_StatusTypeDef HAL_JPEG_UnRegisterCallback(JPEG_HandleTypeDef *hjpeg, HAL_JPEG_CallbackIDTypeDef CallbackID)
0797 {
0798   HAL_StatusTypeDef status = HAL_OK;
0799 
0800   /* Process locked */
0801   __HAL_LOCK(hjpeg);
0802 
0803   if (HAL_JPEG_STATE_READY == hjpeg->State)
0804   {
0805     switch (CallbackID)
0806     {
0807       case HAL_JPEG_ENCODE_CPLT_CB_ID :
0808         hjpeg->EncodeCpltCallback = HAL_JPEG_EncodeCpltCallback; /* Legacy weak EncodeCpltCallback  */
0809         break;
0810 
0811       case HAL_JPEG_DECODE_CPLT_CB_ID :
0812         hjpeg->DecodeCpltCallback = HAL_JPEG_DecodeCpltCallback; /* Legacy weak DecodeCpltCallback  */
0813         break;
0814 
0815       case HAL_JPEG_ERROR_CB_ID :
0816         hjpeg->ErrorCallback = HAL_JPEG_ErrorCallback;           /* Legacy weak ErrorCallback  */
0817         break;
0818 
0819       case HAL_JPEG_MSPINIT_CB_ID :
0820         hjpeg->MspInitCallback = HAL_JPEG_MspInit;              /* Legacy weak MspInit  */
0821         break;
0822 
0823       case HAL_JPEG_MSPDEINIT_CB_ID :
0824         hjpeg->MspDeInitCallback = HAL_JPEG_MspDeInit;          /* Legacy weak MspDeInit  */
0825         break;
0826 
0827       default :
0828         /* Update the error code */
0829         hjpeg->ErrorCode |= HAL_JPEG_ERROR_INVALID_CALLBACK;
0830         /* Return error status */
0831         status =  HAL_ERROR;
0832         break;
0833     }
0834   }
0835   else if (HAL_JPEG_STATE_RESET == hjpeg->State)
0836   {
0837     switch (CallbackID)
0838     {
0839       case HAL_JPEG_MSPINIT_CB_ID :
0840         hjpeg->MspInitCallback = HAL_JPEG_MspInit;           /* Legacy weak MspInit  */
0841         break;
0842 
0843       case HAL_JPEG_MSPDEINIT_CB_ID :
0844         hjpeg->MspDeInitCallback = HAL_JPEG_MspDeInit;       /* Legacy weak MspInit  */
0845         break;
0846 
0847       default :
0848         /* Update the error code */
0849         hjpeg->ErrorCode |= HAL_JPEG_ERROR_INVALID_CALLBACK;
0850         /* Return error status */
0851         status =  HAL_ERROR;
0852         break;
0853     }
0854   }
0855   else
0856   {
0857     /* Update the error code */
0858     hjpeg->ErrorCode |= HAL_JPEG_ERROR_INVALID_CALLBACK;
0859     /* Return error status */
0860     status =  HAL_ERROR;
0861   }
0862 
0863   /* Release Lock */
0864   __HAL_UNLOCK(hjpeg);
0865   return status;
0866 }
0867 
0868 /**
0869   * @brief  Register Info Ready JPEG Callback
0870   *         To be used instead of the weak HAL_JPEG_InfoReadyCallback() predefined callback
0871   * @param  hjpeg JPEG handle
0872   * @param  pCallback pointer to the Info Ready Callback function
0873   * @retval HAL status
0874   */
0875 HAL_StatusTypeDef HAL_JPEG_RegisterInfoReadyCallback(JPEG_HandleTypeDef *hjpeg,
0876                                                      pJPEG_InfoReadyCallbackTypeDef pCallback)
0877 {
0878   HAL_StatusTypeDef status = HAL_OK;
0879 
0880   if (pCallback == NULL)
0881   {
0882     /* Update the error code */
0883     hjpeg->ErrorCode |= HAL_JPEG_ERROR_INVALID_CALLBACK;
0884     return HAL_ERROR;
0885   }
0886   /* Process locked */
0887   __HAL_LOCK(hjpeg);
0888 
0889   if (HAL_JPEG_STATE_READY == hjpeg->State)
0890   {
0891     hjpeg->InfoReadyCallback = pCallback;
0892   }
0893   else
0894   {
0895     /* Update the error code */
0896     hjpeg->ErrorCode |= HAL_JPEG_ERROR_INVALID_CALLBACK;
0897     /* Return error status */
0898     status =  HAL_ERROR;
0899   }
0900 
0901   /* Release Lock */
0902   __HAL_UNLOCK(hjpeg);
0903   return status;
0904 }
0905 
0906 /**
0907   * @brief  UnRegister the Info Ready JPEG Callback
0908   *         Info Ready JPEG Callback is redirected to the weak HAL_JPEG_InfoReadyCallback() predefined callback
0909   * @param  hjpeg JPEG handle
0910   * @retval HAL status
0911   */
0912 HAL_StatusTypeDef HAL_JPEG_UnRegisterInfoReadyCallback(JPEG_HandleTypeDef *hjpeg)
0913 {
0914   HAL_StatusTypeDef status = HAL_OK;
0915 
0916   /* Process locked */
0917   __HAL_LOCK(hjpeg);
0918 
0919   if (HAL_JPEG_STATE_READY == hjpeg->State)
0920   {
0921     hjpeg->InfoReadyCallback = HAL_JPEG_InfoReadyCallback; /* Legacy weak InfoReadyCallback  */
0922   }
0923   else
0924   {
0925     /* Update the error code */
0926     hjpeg->ErrorCode |= HAL_JPEG_ERROR_INVALID_CALLBACK;
0927     /* Return error status */
0928     status =  HAL_ERROR;
0929   }
0930 
0931   /* Release Lock */
0932   __HAL_UNLOCK(hjpeg);
0933   return status;
0934 }
0935 
0936 /**
0937   * @brief  Register Get Data JPEG Callback
0938   *         To be used instead of the weak HAL_JPEG_GetDataCallback() predefined callback
0939   * @param  hjpeg JPEG handle
0940   * @param  pCallback pointer to the Get Data Callback function
0941   * @retval HAL status
0942   */
0943 HAL_StatusTypeDef HAL_JPEG_RegisterGetDataCallback(JPEG_HandleTypeDef *hjpeg, pJPEG_GetDataCallbackTypeDef pCallback)
0944 {
0945   HAL_StatusTypeDef status = HAL_OK;
0946 
0947   if (pCallback == NULL)
0948   {
0949     /* Update the error code */
0950     hjpeg->ErrorCode |= HAL_JPEG_ERROR_INVALID_CALLBACK;
0951     return HAL_ERROR;
0952   }
0953   /* Process locked */
0954   __HAL_LOCK(hjpeg);
0955 
0956   if (HAL_JPEG_STATE_READY == hjpeg->State)
0957   {
0958     hjpeg->GetDataCallback = pCallback;
0959   }
0960   else
0961   {
0962     /* Update the error code */
0963     hjpeg->ErrorCode |= HAL_JPEG_ERROR_INVALID_CALLBACK;
0964     /* Return error status */
0965     status =  HAL_ERROR;
0966   }
0967 
0968   /* Release Lock */
0969   __HAL_UNLOCK(hjpeg);
0970   return status;
0971 }
0972 
0973 /**
0974   * @brief  UnRegister the Get Data JPEG Callback
0975   *         Get Data JPEG Callback is redirected to the weak HAL_JPEG_GetDataCallback() predefined callback
0976   * @param  hjpeg JPEG handle
0977   * @retval HAL status
0978   */
0979 HAL_StatusTypeDef HAL_JPEG_UnRegisterGetDataCallback(JPEG_HandleTypeDef *hjpeg)
0980 {
0981   HAL_StatusTypeDef status = HAL_OK;
0982 
0983   /* Process locked */
0984   __HAL_LOCK(hjpeg);
0985 
0986   if (HAL_JPEG_STATE_READY == hjpeg->State)
0987   {
0988     hjpeg->GetDataCallback = HAL_JPEG_GetDataCallback;  /* Legacy weak GetDataCallback  */
0989   }
0990   else
0991   {
0992     /* Update the error code */
0993     hjpeg->ErrorCode |= HAL_JPEG_ERROR_INVALID_CALLBACK;
0994     /* Return error status */
0995     status =  HAL_ERROR;
0996   }
0997 
0998   /* Release Lock */
0999   __HAL_UNLOCK(hjpeg);
1000   return status;
1001 }
1002 
1003 /**
1004   * @brief  Register Data Ready JPEG Callback
1005   *         To be used instead of the weak HAL_JPEG_DataReadyCallback() predefined callback
1006   * @param  hjpeg JPEG handle
1007   * @param  pCallback pointer to the Get Data Callback function
1008   * @retval HAL status
1009   */
1010 HAL_StatusTypeDef HAL_JPEG_RegisterDataReadyCallback(JPEG_HandleTypeDef *hjpeg,
1011                                                      pJPEG_DataReadyCallbackTypeDef pCallback)
1012 {
1013   HAL_StatusTypeDef status = HAL_OK;
1014 
1015   if (pCallback == NULL)
1016   {
1017     /* Update the error code */
1018     hjpeg->ErrorCode |= HAL_JPEG_ERROR_INVALID_CALLBACK;
1019     return HAL_ERROR;
1020   }
1021   /* Process locked */
1022   __HAL_LOCK(hjpeg);
1023 
1024   if (HAL_JPEG_STATE_READY == hjpeg->State)
1025   {
1026     hjpeg->DataReadyCallback = pCallback;
1027   }
1028   else
1029   {
1030     /* Update the error code */
1031     hjpeg->ErrorCode |= HAL_JPEG_ERROR_INVALID_CALLBACK;
1032     /* Return error status */
1033     status =  HAL_ERROR;
1034   }
1035 
1036   /* Release Lock */
1037   __HAL_UNLOCK(hjpeg);
1038   return status;
1039 }
1040 
1041 /**
1042   * @brief  UnRegister the Data Ready JPEG Callback
1043   *         Get Data Ready Callback is redirected to the weak HAL_JPEG_DataReadyCallback() predefined callback
1044   * @param  hjpeg JPEG handle
1045   * @retval HAL status
1046   */
1047 HAL_StatusTypeDef HAL_JPEG_UnRegisterDataReadyCallback(JPEG_HandleTypeDef *hjpeg)
1048 {
1049   HAL_StatusTypeDef status = HAL_OK;
1050 
1051   /* Process locked */
1052   __HAL_LOCK(hjpeg);
1053 
1054   if (HAL_JPEG_STATE_READY == hjpeg->State)
1055   {
1056     hjpeg->DataReadyCallback = HAL_JPEG_DataReadyCallback;  /* Legacy weak DataReadyCallback  */
1057   }
1058   else
1059   {
1060     /* Update the error code */
1061     hjpeg->ErrorCode |= HAL_JPEG_ERROR_INVALID_CALLBACK;
1062     /* Return error status */
1063     status =  HAL_ERROR;
1064   }
1065 
1066   /* Release Lock */
1067   __HAL_UNLOCK(hjpeg);
1068   return status;
1069 }
1070 
1071 #endif /* USE_HAL_JPEG_REGISTER_CALLBACKS */
1072 
1073 /**
1074   * @}
1075   */
1076 
1077 /** @defgroup JPEG_Exported_Functions_Group2 Configuration functions
1078   * @ingroup RTEMSBSPsARMSTM32H7
1079   *  @brief    JPEG Configuration functions.
1080   *
1081 @verbatim
1082   ==============================================================================
1083               ##### Configuration functions #####
1084   ==============================================================================
1085     [..]  This section provides functions allowing to:
1086       (+) HAL_JPEG_ConfigEncoding() : JPEG encoding configuration
1087       (+) HAL_JPEG_GetInfo() :  Extract the image configuration from the JPEG header during the decoding
1088       (+) HAL_JPEG_EnableHeaderParsing() :  Enable JPEG Header parsing for decoding
1089       (+) HAL_JPEG_DisableHeaderParsing() : Disable JPEG Header parsing for decoding
1090       (+) HAL_JPEG_SetUserQuantTables : Modify the default Quantization tables used for JPEG encoding.
1091 
1092 @endverbatim
1093   * @{
1094   */
1095 
1096 /**
1097   * @brief  Set the JPEG encoding configuration.
1098   * @param  hjpeg pointer to a JPEG_HandleTypeDef structure that contains
1099   *         the configuration information for JPEG module
1100   * @param  pConf pointer to a JPEG_ConfTypeDef structure that contains
1101   *         the encoding configuration
1102   * @retval HAL status
1103   */
1104 HAL_StatusTypeDef HAL_JPEG_ConfigEncoding(JPEG_HandleTypeDef *hjpeg, JPEG_ConfTypeDef *pConf)
1105 {
1106   uint32_t error;
1107   uint32_t numberMCU;
1108   uint32_t hfactor;
1109   uint32_t vfactor;
1110   uint32_t hMCU;
1111   uint32_t vMCU;
1112 
1113   /* Check the JPEG handle allocation */
1114   if ((hjpeg == NULL) || (pConf == NULL))
1115   {
1116     return HAL_ERROR;
1117   }
1118   else
1119   {
1120     /* Check the parameters */
1121     assert_param(IS_JPEG_COLORSPACE(pConf->ColorSpace));
1122     assert_param(IS_JPEG_CHROMASUBSAMPLING(pConf->ChromaSubsampling));
1123     assert_param(IS_JPEG_IMAGE_QUALITY(pConf->ImageQuality));
1124 
1125     /* Process Locked */
1126     __HAL_LOCK(hjpeg);
1127 
1128     if (hjpeg->State == HAL_JPEG_STATE_READY)
1129     {
1130       hjpeg->State = HAL_JPEG_STATE_BUSY;
1131 
1132       hjpeg->Conf.ColorSpace          =  pConf->ColorSpace;
1133       hjpeg->Conf.ChromaSubsampling   =  pConf->ChromaSubsampling;
1134       hjpeg->Conf.ImageHeight         =  pConf->ImageHeight;
1135       hjpeg->Conf.ImageWidth          =  pConf->ImageWidth;
1136       hjpeg->Conf.ImageQuality        =  pConf->ImageQuality;
1137 
1138       /* Reset the Color Space : by default only one quantization table is used*/
1139       hjpeg->Instance->CONFR1 &= ~JPEG_CONFR1_COLORSPACE;
1140 
1141       /* Set Number of color components*/
1142       if (hjpeg->Conf.ColorSpace == JPEG_GRAYSCALE_COLORSPACE)
1143       {
1144         /*Gray Scale is only one component 8x8 blocks i.e 4:4:4*/
1145         hjpeg->Conf.ChromaSubsampling = JPEG_444_SUBSAMPLING;
1146 
1147         JPEG_SetColorGrayScale(hjpeg);
1148         /* Set quantization table 0*/
1149         error = JPEG_Set_Quantization_Mem(hjpeg, hjpeg->QuantTable0, (hjpeg->Instance->QMEM0));
1150       }
1151       else if (hjpeg->Conf.ColorSpace == JPEG_YCBCR_COLORSPACE)
1152       {
1153         /*
1154            Set the Color Space for YCbCr : 2 quantization tables are used
1155            one for Luminance(Y) and one for both Chrominances (Cb & Cr)
1156           */
1157         hjpeg->Instance->CONFR1 |= JPEG_CONFR1_COLORSPACE_0;
1158 
1159         JPEG_SetColorYCBCR(hjpeg);
1160 
1161         /* Set quantization table 0*/
1162         error  = JPEG_Set_Quantization_Mem(hjpeg, hjpeg->QuantTable0, (hjpeg->Instance->QMEM0));
1163         /*By default quantization table 0 for component 0 and quantization table 1 for both components 1 and 2*/
1164         error |= JPEG_Set_Quantization_Mem(hjpeg, hjpeg->QuantTable1, (hjpeg->Instance->QMEM1));
1165 
1166         if ((hjpeg->Context & JPEG_CONTEXT_CUSTOM_TABLES) != 0UL)
1167         {
1168           /*Use user customized quantization tables , 1 table per component*/
1169           /* use 3 quantization tables , one for each component*/
1170           hjpeg->Instance->CONFR1 &= (~JPEG_CONFR1_COLORSPACE);
1171           hjpeg->Instance->CONFR1 |= JPEG_CONFR1_COLORSPACE_1;
1172 
1173           error |= JPEG_Set_Quantization_Mem(hjpeg, hjpeg->QuantTable2, (hjpeg->Instance->QMEM2));
1174 
1175           /*Use Quantization 1 table for component 1*/
1176           hjpeg->Instance->CONFR5 &= (~JPEG_CONFR5_QT);
1177           hjpeg->Instance->CONFR5 |=  JPEG_CONFR5_QT_0;
1178 
1179           /*Use Quantization 2 table for component 2*/
1180           hjpeg->Instance->CONFR6 &= (~JPEG_CONFR6_QT);
1181           hjpeg->Instance->CONFR6 |=  JPEG_CONFR6_QT_1;
1182         }
1183       }
1184       else /* ColorSpace == JPEG_CMYK_COLORSPACE */
1185       {
1186         JPEG_SetColorCMYK(hjpeg);
1187 
1188         /* Set quantization table 0*/
1189         error  = JPEG_Set_Quantization_Mem(hjpeg, hjpeg->QuantTable0, (hjpeg->Instance->QMEM0));
1190         /*By default quantization table 0 for All components*/
1191 
1192         if ((hjpeg->Context & JPEG_CONTEXT_CUSTOM_TABLES) != 0UL)
1193         {
1194           /*Use user customized quantization tables , 1 table per component*/
1195           /* use 4 quantization tables , one for each component*/
1196           hjpeg->Instance->CONFR1 |= JPEG_CONFR1_COLORSPACE;
1197 
1198           error |= JPEG_Set_Quantization_Mem(hjpeg, hjpeg->QuantTable1, (hjpeg->Instance->QMEM1));
1199           error |= JPEG_Set_Quantization_Mem(hjpeg, hjpeg->QuantTable2, (hjpeg->Instance->QMEM2));
1200           error |= JPEG_Set_Quantization_Mem(hjpeg, hjpeg->QuantTable3, (hjpeg->Instance->QMEM3));
1201 
1202           /*Use Quantization 1 table for component 1*/
1203           hjpeg->Instance->CONFR5 |=  JPEG_CONFR5_QT_0;
1204 
1205           /*Use Quantization 2 table for component 2*/
1206           hjpeg->Instance->CONFR6 |=  JPEG_CONFR6_QT_1;
1207 
1208           /*Use Quantization 3 table for component 3*/
1209           hjpeg->Instance->CONFR7 |=  JPEG_CONFR7_QT;
1210         }
1211       }
1212 
1213       if (error != 0UL)
1214       {
1215         hjpeg->ErrorCode = HAL_JPEG_ERROR_QUANT_TABLE;
1216 
1217         /* Process Unlocked */
1218         __HAL_UNLOCK(hjpeg);
1219 
1220         /* Set the JPEG State to ready */
1221         hjpeg->State = HAL_JPEG_STATE_READY;
1222 
1223         return  HAL_ERROR;
1224       }
1225       /* Set the image size*/
1226       /* set the number of lines*/
1227       MODIFY_REG(hjpeg->Instance->CONFR1, JPEG_CONFR1_YSIZE, ((hjpeg->Conf.ImageHeight & 0x0000FFFFUL) << 16));
1228       /* set the number of pixels per line*/
1229       MODIFY_REG(hjpeg->Instance->CONFR3, JPEG_CONFR3_XSIZE, ((hjpeg->Conf.ImageWidth & 0x0000FFFFUL) << 16));
1230 
1231 
1232       if (hjpeg->Conf.ChromaSubsampling == JPEG_420_SUBSAMPLING) /* 4:2:0*/
1233       {
1234         hfactor = 16;
1235         vfactor = 16;
1236       }
1237       else if (hjpeg->Conf.ChromaSubsampling == JPEG_422_SUBSAMPLING) /* 4:2:2*/
1238       {
1239         hfactor = 16;
1240         vfactor = 8;
1241       }
1242       else /* Default is 8x8 MCU,  4:4:4*/
1243       {
1244         hfactor = 8;
1245         vfactor = 8;
1246       }
1247 
1248       hMCU = (hjpeg->Conf.ImageWidth / hfactor);
1249       if ((hjpeg->Conf.ImageWidth % hfactor) != 0UL)
1250       {
1251         hMCU++; /*+1 for horizontal incomplete MCU */
1252       }
1253 
1254       vMCU = (hjpeg->Conf.ImageHeight / vfactor);
1255       if ((hjpeg->Conf.ImageHeight % vfactor) != 0UL)
1256       {
1257         vMCU++; /*+1 for vertical incomplete MCU */
1258       }
1259 
1260       numberMCU = (hMCU * vMCU) - 1UL; /* Bit Field JPEG_CONFR2_NMCU shall be set to NB_MCU - 1*/
1261       /* Set the number of MCU*/
1262       hjpeg->Instance->CONFR2 = (numberMCU & JPEG_CONFR2_NMCU);
1263 
1264       hjpeg->Context |= JPEG_CONTEXT_CONF_ENCODING;
1265 
1266       /* Process Unlocked */
1267       __HAL_UNLOCK(hjpeg);
1268 
1269       /* Set the JPEG State to ready */
1270       hjpeg->State = HAL_JPEG_STATE_READY;
1271 
1272       /* Return function status */
1273       return HAL_OK;
1274     }
1275     else
1276     {
1277       /* Process Unlocked */
1278       __HAL_UNLOCK(hjpeg);
1279 
1280       /* Return function status */
1281       return HAL_BUSY;
1282     }
1283   }
1284 }
1285 
1286 /**
1287   * @brief  Extract the image configuration from the JPEG header during the decoding
1288   * @param  hjpeg pointer to a JPEG_HandleTypeDef structure that contains
1289   *         the configuration information for JPEG module
1290   * @param  pInfo pointer to a JPEG_ConfTypeDef structure that contains
1291   *         The JPEG decoded header information
1292   * @retval HAL status
1293   */
1294 HAL_StatusTypeDef HAL_JPEG_GetInfo(JPEG_HandleTypeDef *hjpeg, JPEG_ConfTypeDef *pInfo)
1295 {
1296   uint32_t yblockNb;
1297   uint32_t cBblockNb;
1298   uint32_t cRblockNb;
1299 
1300   /* Check the JPEG handle allocation */
1301   if ((hjpeg == NULL) || (pInfo == NULL))
1302   {
1303     return HAL_ERROR;
1304   }
1305 
1306   /*Read the conf parameters */
1307   if ((hjpeg->Instance->CONFR1 & JPEG_CONFR1_NF) == JPEG_CONFR1_NF_1)
1308   {
1309     pInfo->ColorSpace = JPEG_YCBCR_COLORSPACE;
1310   }
1311   else if ((hjpeg->Instance->CONFR1 & JPEG_CONFR1_NF) == 0UL)
1312   {
1313     pInfo->ColorSpace = JPEG_GRAYSCALE_COLORSPACE;
1314   }
1315   else if ((hjpeg->Instance->CONFR1 & JPEG_CONFR1_NF) == JPEG_CONFR1_NF)
1316   {
1317     pInfo->ColorSpace = JPEG_CMYK_COLORSPACE;
1318   }
1319   else
1320   {
1321     return HAL_ERROR;
1322   }
1323 
1324   pInfo->ImageHeight = (hjpeg->Instance->CONFR1 & 0xFFFF0000UL) >> 16;
1325   pInfo->ImageWidth  = (hjpeg->Instance->CONFR3 & 0xFFFF0000UL) >> 16;
1326 
1327   if ((pInfo->ColorSpace == JPEG_YCBCR_COLORSPACE) || (pInfo->ColorSpace == JPEG_CMYK_COLORSPACE))
1328   {
1329     yblockNb  = (hjpeg->Instance->CONFR4 & JPEG_CONFR4_NB) >> 4;
1330     cBblockNb = (hjpeg->Instance->CONFR5 & JPEG_CONFR5_NB) >> 4;
1331     cRblockNb = (hjpeg->Instance->CONFR6 & JPEG_CONFR6_NB) >> 4;
1332 
1333     if ((yblockNb == 1UL) && (cBblockNb == 0UL) && (cRblockNb == 0UL))
1334     {
1335       pInfo->ChromaSubsampling = JPEG_422_SUBSAMPLING; /*16x8 block*/
1336     }
1337     else if ((yblockNb == 0UL) && (cBblockNb == 0UL) && (cRblockNb == 0UL))
1338     {
1339       pInfo->ChromaSubsampling = JPEG_444_SUBSAMPLING;
1340     }
1341     else if ((yblockNb == 3UL) && (cBblockNb == 0UL) && (cRblockNb == 0UL))
1342     {
1343       pInfo->ChromaSubsampling = JPEG_420_SUBSAMPLING;
1344     }
1345     else /*Default is 4:4:4*/
1346     {
1347       pInfo->ChromaSubsampling = JPEG_444_SUBSAMPLING;
1348     }
1349   }
1350   else
1351   {
1352     pInfo->ChromaSubsampling = JPEG_444_SUBSAMPLING;
1353   }
1354 
1355   pInfo->ImageQuality = JPEG_GetQuality(hjpeg);
1356 
1357   /* Return function status */
1358   return HAL_OK;
1359 }
1360 
1361 /**
1362   * @brief  Enable JPEG Header parsing for decoding
1363   * @param  hjpeg pointer to a JPEG_HandleTypeDef structure that contains
1364   *               the configuration information for the JPEG.
1365   * @retval HAL status
1366   */
1367 HAL_StatusTypeDef HAL_JPEG_EnableHeaderParsing(JPEG_HandleTypeDef *hjpeg)
1368 {
1369   /* Process locked */
1370   __HAL_LOCK(hjpeg);
1371 
1372   if (hjpeg->State == HAL_JPEG_STATE_READY)
1373   {
1374     /* Change the JPEG state */
1375     hjpeg->State = HAL_JPEG_STATE_BUSY;
1376 
1377     /* Enable header processing*/
1378     hjpeg->Instance->CONFR1 |= JPEG_CONFR1_HDR;
1379 
1380     /* Process unlocked */
1381     __HAL_UNLOCK(hjpeg);
1382 
1383     /* Change the JPEG state */
1384     hjpeg->State = HAL_JPEG_STATE_READY;
1385 
1386     return HAL_OK;
1387   }
1388   else
1389   {
1390     /* Process unlocked */
1391     __HAL_UNLOCK(hjpeg);
1392 
1393     return HAL_BUSY;
1394   }
1395 }
1396 
1397 /**
1398   * @brief  Disable JPEG Header parsing for decoding
1399   * @param  hjpeg pointer to a JPEG_HandleTypeDef structure that contains
1400   *               the configuration information for the JPEG.
1401   * @retval HAL status
1402   */
1403 HAL_StatusTypeDef HAL_JPEG_DisableHeaderParsing(JPEG_HandleTypeDef *hjpeg)
1404 {
1405   /* Process locked */
1406   __HAL_LOCK(hjpeg);
1407 
1408   if (hjpeg->State == HAL_JPEG_STATE_READY)
1409   {
1410     /* Change the JPEG state */
1411     hjpeg->State = HAL_JPEG_STATE_BUSY;
1412 
1413     /* Disable header processing*/
1414     hjpeg->Instance->CONFR1 &= ~JPEG_CONFR1_HDR;
1415 
1416     /* Process unlocked */
1417     __HAL_UNLOCK(hjpeg);
1418 
1419     /* Change the JPEG state */
1420     hjpeg->State = HAL_JPEG_STATE_READY;
1421 
1422     return HAL_OK;
1423   }
1424   else
1425   {
1426     /* Process unlocked */
1427     __HAL_UNLOCK(hjpeg);
1428 
1429     return HAL_BUSY;
1430   }
1431 }
1432 
1433 /**
1434   * @brief  Modify the default Quantization tables used for JPEG encoding.
1435   * @param  hjpeg pointer to a JPEG_HandleTypeDef structure that contains
1436   *         the configuration information for JPEG module
1437   * @param  QTable0  pointer to uint8_t , define the user quantification table for color component 1.
1438   *                  If NULL assume no need to update  the table and no error return
1439   * @param  QTable1  pointer to uint8_t , define the user quantification table for color component 2.
1440   *                  If NULL assume no need to update  the table and no error return.
1441   * @param  QTable2  pointer to uint8_t , define the user quantification table for color component 3,
1442   *                  If NULL assume no need to update  the table and no error return.
1443   * @param  QTable3  pointer to uint8_t , define the user quantification table for color component 4.
1444   *                  If NULL assume no need to update  the table and no error return.
1445   *
1446   * @retval HAL status
1447   */
1448 
1449 
1450 HAL_StatusTypeDef  HAL_JPEG_SetUserQuantTables(JPEG_HandleTypeDef *hjpeg, uint8_t *QTable0, uint8_t *QTable1,
1451                                                uint8_t *QTable2, uint8_t *QTable3)
1452 {
1453   /* Process Locked */
1454   __HAL_LOCK(hjpeg);
1455 
1456   if (hjpeg->State == HAL_JPEG_STATE_READY)
1457   {
1458     /* Change the DMA state */
1459     hjpeg->State = HAL_JPEG_STATE_BUSY;
1460 
1461     hjpeg->Context |= JPEG_CONTEXT_CUSTOM_TABLES;
1462 
1463     hjpeg->QuantTable0 = QTable0;
1464     hjpeg->QuantTable1 = QTable1;
1465     hjpeg->QuantTable2 = QTable2;
1466     hjpeg->QuantTable3 = QTable3;
1467 
1468     /* Process Unlocked */
1469     __HAL_UNLOCK(hjpeg);
1470 
1471     /* Change the DMA state */
1472     hjpeg->State = HAL_JPEG_STATE_READY;
1473 
1474     /* Return function status */
1475     return HAL_OK;
1476   }
1477   else
1478   {
1479     /* Process Unlocked */
1480     __HAL_UNLOCK(hjpeg);
1481 
1482     return HAL_BUSY;
1483   }
1484 }
1485 
1486 /**
1487   * @}
1488   */
1489 
1490 /** @defgroup JPEG_Exported_Functions_Group3 encoding/decoding processing functions
1491   * @ingroup RTEMSBSPsARMSTM32H7
1492   *  @brief   processing functions.
1493   *
1494 @verbatim
1495   ==============================================================================
1496                       ##### JPEG processing functions #####
1497   ==============================================================================
1498     [..]  This section provides functions allowing to:
1499       (+) HAL_JPEG_Encode()     : JPEG encoding with polling process
1500       (+) HAL_JPEG_Decode()     : JPEG decoding with polling process
1501       (+) HAL_JPEG_Encode_IT()  : JPEG encoding with interrupt process
1502       (+) HAL_JPEG_Decode_IT()  : JPEG decoding with interrupt process
1503       (+) HAL_JPEG_Encode_DMA() : JPEG encoding with DMA process
1504       (+) HAL_JPEG_Decode_DMA() : JPEG decoding with DMA process
1505       (+) HAL_JPEG_Pause()      :   Pause the Input/Output processing
1506       (+) HAL_JPEG_Resume()     :  Resume the JPEG Input/Output processing
1507       (+) HAL_JPEG_ConfigInputBuffer()  : Config Encoding/Decoding Input Buffer
1508       (+) HAL_JPEG_ConfigOutputBuffer() : Config Encoding/Decoding Output Buffer
1509       (+) HAL_JPEG_Abort()              : Aborts the JPEG Encoding/Decoding
1510 
1511 @endverbatim
1512   * @{
1513   */
1514 
1515 /**
1516   * @brief  Starts JPEG encoding with polling processing
1517   * @param  hjpeg pointer to a JPEG_HandleTypeDef structure that contains
1518   *         the configuration information for JPEG module
1519   * @param  pDataInMCU Pointer to the Input buffer
1520   * @param  InDataLength size in bytes Input buffer
1521   * @param  pDataOut Pointer to the jpeg output data buffer
1522   * @param  OutDataLength size in bytes of the Output buffer
1523   * @param  Timeout Specify Timeout value
1524   * @retval HAL status
1525   */
1526 HAL_StatusTypeDef  HAL_JPEG_Encode(JPEG_HandleTypeDef *hjpeg, uint8_t *pDataInMCU, uint32_t InDataLength,
1527                                    uint8_t *pDataOut, uint32_t OutDataLength, uint32_t Timeout)
1528 {
1529   uint32_t tickstart;
1530 
1531   /* Check the parameters */
1532   assert_param((InDataLength >= 4UL));
1533   assert_param((OutDataLength >= 4UL));
1534 
1535   /* Check In/out buffer allocation and size */
1536   if ((hjpeg == NULL) || (pDataInMCU == NULL) || (pDataOut == NULL))
1537   {
1538     return HAL_ERROR;
1539   }
1540   /* Process locked */
1541   __HAL_LOCK(hjpeg);
1542 
1543   if (hjpeg->State != HAL_JPEG_STATE_READY)
1544   {
1545     /* Process Unlocked */
1546     __HAL_UNLOCK(hjpeg);
1547 
1548     return HAL_BUSY;
1549   }
1550 
1551   if (hjpeg->State == HAL_JPEG_STATE_READY)
1552   {
1553     if ((hjpeg->Context & JPEG_CONTEXT_CONF_ENCODING) == JPEG_CONTEXT_CONF_ENCODING)
1554     {
1555       /*Change JPEG state*/
1556       hjpeg->State = HAL_JPEG_STATE_BUSY_ENCODING;
1557 
1558       /*Set the Context to Encode with Polling*/
1559       hjpeg->Context &= ~(JPEG_CONTEXT_OPERATION_MASK | JPEG_CONTEXT_METHOD_MASK);
1560       hjpeg->Context |= (JPEG_CONTEXT_ENCODE | JPEG_CONTEXT_POLLING);
1561 
1562       /* Get tick */
1563       tickstart = HAL_GetTick();
1564 
1565       /*Store In/out buffers pointers and size*/
1566       hjpeg->pJpegInBuffPtr = pDataInMCU;
1567       hjpeg->pJpegOutBuffPtr = pDataOut;
1568       hjpeg->InDataLength = InDataLength - (InDataLength % 4UL);    /* In Data length must be multiple of 4 Bytes (1 word)*/
1569       hjpeg->OutDataLength = OutDataLength - (OutDataLength % 4UL); /* Out Data length must be multiple of 4 Bytes (1 word)*/
1570 
1571       /*Reset In/out data counter */
1572       hjpeg->JpegInCount = 0;
1573       hjpeg->JpegOutCount = 0;
1574 
1575       /*Init decoding process*/
1576       JPEG_Init_Process(hjpeg);
1577 
1578       /*JPEG data processing : In/Out FIFO transfer*/
1579       while ((JPEG_Process(hjpeg) == JPEG_PROCESS_ONGOING))
1580       {
1581         if (Timeout != HAL_MAX_DELAY)
1582         {
1583           if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0UL))
1584           {
1585 
1586             /* Update error code */
1587             hjpeg->ErrorCode |= HAL_JPEG_ERROR_TIMEOUT;
1588 
1589             /* Process Unlocked */
1590             __HAL_UNLOCK(hjpeg);
1591 
1592             /*Change JPEG state*/
1593             hjpeg->State = HAL_JPEG_STATE_READY;
1594 
1595             return HAL_TIMEOUT;
1596           }
1597         }
1598       }
1599 
1600       /* Process Unlocked */
1601       __HAL_UNLOCK(hjpeg);
1602 
1603       /*Change JPEG state*/
1604       hjpeg->State = HAL_JPEG_STATE_READY;
1605 
1606     }
1607     else
1608     {
1609       /* Process Unlocked */
1610       __HAL_UNLOCK(hjpeg);
1611 
1612       return HAL_ERROR;
1613     }
1614   }
1615   /* Return function status */
1616   return HAL_OK;
1617 }
1618 
1619 /**
1620   * @brief  Starts JPEG decoding with polling processing
1621   * @param  hjpeg pointer to a JPEG_HandleTypeDef structure that contains
1622   *         the configuration information for JPEG module
1623   * @param  pDataIn Pointer to the input data buffer
1624   * @param  InDataLength size in bytes Input buffer
1625   * @param  pDataOutMCU Pointer to the Output data buffer
1626   * @param  OutDataLength size in bytes of the Output buffer
1627   * @param  Timeout Specify Timeout value
1628   * @retval HAL status
1629   */
1630 HAL_StatusTypeDef  HAL_JPEG_Decode(JPEG_HandleTypeDef *hjpeg, uint8_t *pDataIn, uint32_t InDataLength,
1631                                    uint8_t *pDataOutMCU, uint32_t OutDataLength, uint32_t Timeout)
1632 {
1633   uint32_t tickstart;
1634 
1635   /* Check the parameters */
1636   assert_param((InDataLength >= 4UL));
1637   assert_param((OutDataLength >= 4UL));
1638 
1639   /* Check In/out buffer allocation and size */
1640   if ((hjpeg == NULL) || (pDataIn == NULL) || (pDataOutMCU == NULL))
1641   {
1642     return HAL_ERROR;
1643   }
1644 
1645   /* Process Locked */
1646   __HAL_LOCK(hjpeg);
1647 
1648   /* Get tick */
1649   tickstart = HAL_GetTick();
1650 
1651   if (hjpeg->State == HAL_JPEG_STATE_READY)
1652   {
1653     /*Change JPEG state*/
1654     hjpeg->State = HAL_JPEG_STATE_BUSY_DECODING;
1655 
1656     /*Set the Context to Decode with Polling*/
1657     /*Set the Context to Encode with Polling*/
1658     hjpeg->Context &= ~(JPEG_CONTEXT_OPERATION_MASK | JPEG_CONTEXT_METHOD_MASK);
1659     hjpeg->Context |= (JPEG_CONTEXT_DECODE | JPEG_CONTEXT_POLLING);
1660 
1661     /*Store In/out buffers pointers and size*/
1662     hjpeg->pJpegInBuffPtr = pDataIn;
1663     hjpeg->pJpegOutBuffPtr = pDataOutMCU;
1664     hjpeg->InDataLength = InDataLength - (InDataLength % 4UL);    /*In Data length must be multiple of 4 Bytes (1 word)*/
1665     hjpeg->OutDataLength = OutDataLength - (OutDataLength % 4UL); /*Out Data length must be multiple of 4 Bytes (1 word)*/
1666 
1667     /*Reset In/out data counter */
1668     hjpeg->JpegInCount = 0;
1669     hjpeg->JpegOutCount = 0;
1670 
1671     /*Init decoding process*/
1672     JPEG_Init_Process(hjpeg);
1673 
1674     /*JPEG data processing : In/Out FIFO transfer*/
1675     while ((JPEG_Process(hjpeg) == JPEG_PROCESS_ONGOING))
1676     {
1677       if (Timeout != HAL_MAX_DELAY)
1678       {
1679         if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0UL))
1680         {
1681 
1682           /* Update error code */
1683           hjpeg->ErrorCode |= HAL_JPEG_ERROR_TIMEOUT;
1684 
1685           /* Process Unlocked */
1686           __HAL_UNLOCK(hjpeg);
1687 
1688           /*Change JPEG state*/
1689           hjpeg->State = HAL_JPEG_STATE_READY;
1690 
1691           return HAL_TIMEOUT;
1692         }
1693       }
1694     }
1695 
1696     /* Process Unlocked */
1697     __HAL_UNLOCK(hjpeg);
1698 
1699     /*Change JPEG state*/
1700     hjpeg->State = HAL_JPEG_STATE_READY;
1701 
1702   }
1703   else
1704   {
1705     /* Process Unlocked */
1706     __HAL_UNLOCK(hjpeg);
1707 
1708     return HAL_BUSY;
1709   }
1710   /* Return function status */
1711   return HAL_OK;
1712 }
1713 
1714 /**
1715   * @brief  Starts JPEG encoding with interrupt processing
1716   * @param  hjpeg pointer to a JPEG_HandleTypeDef structure that contains
1717   *         the configuration information for JPEG module
1718   * @param  pDataInMCU Pointer to the Input buffer
1719   * @param  InDataLength size in bytes Input buffer
1720   * @param  pDataOut Pointer to the jpeg output data buffer
1721   * @param  OutDataLength size in bytes of the Output buffer
1722   * @retval HAL status
1723   */
1724 HAL_StatusTypeDef  HAL_JPEG_Encode_IT(JPEG_HandleTypeDef *hjpeg, uint8_t *pDataInMCU, uint32_t InDataLength,
1725                                       uint8_t *pDataOut, uint32_t OutDataLength)
1726 {
1727   /* Check the parameters */
1728   assert_param((InDataLength >= 4UL));
1729   assert_param((OutDataLength >= 4UL));
1730 
1731   /* Check In/out buffer allocation and size */
1732   if ((hjpeg == NULL) || (pDataInMCU == NULL) || (pDataOut == NULL))
1733   {
1734     return HAL_ERROR;
1735   }
1736 
1737   /* Process Locked */
1738   __HAL_LOCK(hjpeg);
1739 
1740   if (hjpeg->State != HAL_JPEG_STATE_READY)
1741   {
1742     /* Process Unlocked */
1743     __HAL_UNLOCK(hjpeg);
1744 
1745     return HAL_BUSY;
1746   }
1747   else
1748   {
1749     if ((hjpeg->Context & JPEG_CONTEXT_CONF_ENCODING) == JPEG_CONTEXT_CONF_ENCODING)
1750     {
1751       /*Change JPEG state*/
1752       hjpeg->State = HAL_JPEG_STATE_BUSY_ENCODING;
1753 
1754       /*Set the Context to Encode with IT*/
1755       hjpeg->Context &= ~(JPEG_CONTEXT_OPERATION_MASK | JPEG_CONTEXT_METHOD_MASK);
1756       hjpeg->Context |= (JPEG_CONTEXT_ENCODE | JPEG_CONTEXT_IT);
1757 
1758       /*Store In/out buffers pointers and size*/
1759       hjpeg->pJpegInBuffPtr = pDataInMCU;
1760       hjpeg->pJpegOutBuffPtr = pDataOut;
1761       hjpeg->InDataLength = InDataLength - (InDataLength % 4UL);    /*In Data length must be multiple of 4 Bytes (1 word)*/
1762       hjpeg->OutDataLength = OutDataLength - (OutDataLength % 4UL); /*Out Data length must be multiple of 4 Bytes (1 word)*/
1763 
1764       /*Reset In/out data counter */
1765       hjpeg->JpegInCount = 0;
1766       hjpeg->JpegOutCount = 0;
1767 
1768       /*Init decoding process*/
1769       JPEG_Init_Process(hjpeg);
1770 
1771     }
1772     else
1773     {
1774       /* Process Unlocked */
1775       __HAL_UNLOCK(hjpeg);
1776 
1777       return HAL_ERROR;
1778     }
1779   }
1780   /* Return function status */
1781   return HAL_OK;
1782 }
1783 
1784 /**
1785   * @brief  Starts JPEG decoding with interrupt processing
1786   * @param  hjpeg pointer to a JPEG_HandleTypeDef structure that contains
1787   *         the configuration information for JPEG module
1788   * @param  pDataIn Pointer to the input data buffer
1789   * @param  InDataLength size in bytes Input buffer
1790   * @param  pDataOutMCU Pointer to the Output data buffer
1791   * @param  OutDataLength size in bytes of the Output buffer
1792   * @retval HAL status
1793   */
1794 HAL_StatusTypeDef  HAL_JPEG_Decode_IT(JPEG_HandleTypeDef *hjpeg, uint8_t *pDataIn, uint32_t InDataLength,
1795                                       uint8_t *pDataOutMCU, uint32_t OutDataLength)
1796 {
1797   /* Check the parameters */
1798   assert_param((InDataLength >= 4UL));
1799   assert_param((OutDataLength >= 4UL));
1800 
1801   /* Check In/out buffer allocation and size */
1802   if ((hjpeg == NULL) || (pDataIn == NULL) || (pDataOutMCU == NULL))
1803   {
1804     return HAL_ERROR;
1805   }
1806 
1807   /* Process Locked */
1808   __HAL_LOCK(hjpeg);
1809 
1810   if (hjpeg->State == HAL_JPEG_STATE_READY)
1811   {
1812     /*Change JPEG state*/
1813     hjpeg->State = HAL_JPEG_STATE_BUSY_DECODING;
1814 
1815     /*Set the Context to Decode with IT*/
1816     hjpeg->Context &= ~(JPEG_CONTEXT_OPERATION_MASK | JPEG_CONTEXT_METHOD_MASK);
1817     hjpeg->Context |= (JPEG_CONTEXT_DECODE | JPEG_CONTEXT_IT);
1818 
1819     /*Store In/out buffers pointers and size*/
1820     hjpeg->pJpegInBuffPtr = pDataIn;
1821     hjpeg->pJpegOutBuffPtr = pDataOutMCU;
1822     hjpeg->InDataLength = InDataLength - (InDataLength % 4UL);    /*In Data length must be multiple of 4 Bytes (1 word)*/
1823     hjpeg->OutDataLength = OutDataLength - (OutDataLength % 4UL); /*Out Data length must be multiple of 4 Bytes (1 word)*/
1824 
1825     /*Reset In/out data counter */
1826     hjpeg->JpegInCount = 0;
1827     hjpeg->JpegOutCount = 0;
1828 
1829     /*Init decoding process*/
1830     JPEG_Init_Process(hjpeg);
1831 
1832   }
1833   else
1834   {
1835     /* Process Unlocked */
1836     __HAL_UNLOCK(hjpeg);
1837 
1838     return HAL_BUSY;
1839   }
1840   /* Return function status */
1841   return HAL_OK;
1842 }
1843 
1844 /**
1845   * @brief  Starts JPEG encoding with DMA processing
1846   * @param  hjpeg pointer to a JPEG_HandleTypeDef structure that contains
1847   *         the configuration information for JPEG module
1848   * @param  pDataInMCU Pointer to the Input buffer
1849   * @param  InDataLength size in bytes Input buffer
1850   * @param  pDataOut Pointer to the jpeg output data buffer
1851   * @param  OutDataLength size in bytes of the Output buffer
1852   * @retval HAL status
1853   */
1854 HAL_StatusTypeDef  HAL_JPEG_Encode_DMA(JPEG_HandleTypeDef *hjpeg, uint8_t *pDataInMCU, uint32_t InDataLength,
1855                                        uint8_t *pDataOut, uint32_t OutDataLength)
1856 {
1857   /* Check the parameters */
1858   assert_param((InDataLength >= 4UL));
1859   assert_param((OutDataLength >= 4UL));
1860 
1861   /* Check In/out buffer allocation and size */
1862   if ((hjpeg == NULL) || (pDataInMCU == NULL) || (pDataOut == NULL))
1863   {
1864     return HAL_ERROR;
1865   }
1866 
1867   /* Process Locked */
1868   __HAL_LOCK(hjpeg);
1869 
1870   if (hjpeg->State != HAL_JPEG_STATE_READY)
1871   {
1872     /* Process Unlocked */
1873     __HAL_UNLOCK(hjpeg);
1874 
1875     return HAL_BUSY;
1876   }
1877   else
1878   {
1879     if ((hjpeg->Context & JPEG_CONTEXT_CONF_ENCODING) == JPEG_CONTEXT_CONF_ENCODING)
1880     {
1881       /*Change JPEG state*/
1882       hjpeg->State = HAL_JPEG_STATE_BUSY_ENCODING;
1883 
1884       /*Set the Context to Encode with DMA*/
1885       hjpeg->Context &= ~(JPEG_CONTEXT_OPERATION_MASK | JPEG_CONTEXT_METHOD_MASK);
1886       hjpeg->Context |= (JPEG_CONTEXT_ENCODE | JPEG_CONTEXT_DMA);
1887 
1888       /*Store In/out buffers pointers and size*/
1889       hjpeg->pJpegInBuffPtr = pDataInMCU;
1890       hjpeg->pJpegOutBuffPtr = pDataOut;
1891       hjpeg->InDataLength = InDataLength;
1892       hjpeg->OutDataLength = OutDataLength;
1893 
1894       /*Reset In/out data counter */
1895       hjpeg->JpegInCount = 0;
1896       hjpeg->JpegOutCount = 0;
1897 
1898       /*Init decoding process*/
1899       JPEG_Init_Process(hjpeg);
1900 
1901       /* JPEG encoding process using DMA */
1902       if (JPEG_DMA_StartProcess(hjpeg) != HAL_OK)
1903       {
1904         /* Update State */
1905         hjpeg->State = HAL_JPEG_STATE_ERROR;
1906         /* Process Unlocked */
1907         __HAL_UNLOCK(hjpeg);
1908 
1909         return HAL_ERROR;
1910       }
1911 
1912     }
1913     else
1914     {
1915       /* Process Unlocked */
1916       __HAL_UNLOCK(hjpeg);
1917 
1918       return HAL_ERROR;
1919     }
1920   }
1921   /* Return function status */
1922   return HAL_OK;
1923 }
1924 
1925 /**
1926   * @brief  Starts JPEG decoding with DMA processing
1927   * @param  hjpeg pointer to a JPEG_HandleTypeDef structure that contains
1928   *         the configuration information for JPEG module
1929   * @param  pDataIn Pointer to the input data buffer
1930   * @param  InDataLength size in bytes Input buffer
1931   * @param  pDataOutMCU Pointer to the Output data buffer
1932   * @param  OutDataLength size in bytes of the Output buffer
1933   * @retval HAL status
1934   */
1935 HAL_StatusTypeDef  HAL_JPEG_Decode_DMA(JPEG_HandleTypeDef *hjpeg, uint8_t *pDataIn, uint32_t InDataLength,
1936                                        uint8_t *pDataOutMCU, uint32_t OutDataLength)
1937 {
1938   /* Check the parameters */
1939   assert_param((InDataLength >= 4UL));
1940   assert_param((OutDataLength >= 4UL));
1941 
1942   /* Check In/out buffer allocation and size */
1943   if ((hjpeg == NULL) || (pDataIn == NULL) || (pDataOutMCU == NULL))
1944   {
1945     return HAL_ERROR;
1946   }
1947 
1948   /* Process Locked */
1949   __HAL_LOCK(hjpeg);
1950 
1951   if (hjpeg->State == HAL_JPEG_STATE_READY)
1952   {
1953     /*Change JPEG state*/
1954     hjpeg->State = HAL_JPEG_STATE_BUSY_DECODING;
1955 
1956     /*Set the Context to Decode with DMA*/
1957     hjpeg->Context &= ~(JPEG_CONTEXT_OPERATION_MASK | JPEG_CONTEXT_METHOD_MASK);
1958     hjpeg->Context |= (JPEG_CONTEXT_DECODE | JPEG_CONTEXT_DMA);
1959 
1960     /*Store In/out buffers pointers and size*/
1961     hjpeg->pJpegInBuffPtr = pDataIn;
1962     hjpeg->pJpegOutBuffPtr = pDataOutMCU;
1963     hjpeg->InDataLength = InDataLength;
1964     hjpeg->OutDataLength = OutDataLength;
1965 
1966     /*Reset In/out data counter */
1967     hjpeg->JpegInCount = 0;
1968     hjpeg->JpegOutCount = 0;
1969 
1970     /*Init decoding process*/
1971     JPEG_Init_Process(hjpeg);
1972 
1973     /* JPEG decoding process using DMA */
1974     if (JPEG_DMA_StartProcess(hjpeg) != HAL_OK)
1975     {
1976       /* Update State */
1977       hjpeg->State = HAL_JPEG_STATE_ERROR;
1978       /* Process Unlocked */
1979       __HAL_UNLOCK(hjpeg);
1980 
1981       return HAL_ERROR;
1982     }
1983   }
1984   else
1985   {
1986     /* Process Unlocked */
1987     __HAL_UNLOCK(hjpeg);
1988 
1989     return HAL_BUSY;
1990   }
1991   /* Return function status */
1992   return HAL_OK;
1993 }
1994 
1995 /**
1996   * @brief  Pause the JPEG Input/Output processing
1997   * @param  hjpeg pointer to a JPEG_HandleTypeDef structure that contains
1998   *         the configuration information for JPEG module
1999   * @param  XferSelection This parameter can be one of the following values :
2000   *                           JPEG_PAUSE_RESUME_INPUT : Pause Input processing
2001   *                           JPEG_PAUSE_RESUME_OUTPUT: Pause Output processing
2002   *                           JPEG_PAUSE_RESUME_INPUT_OUTPUT: Pause Input and Output processing
2003   * @retval HAL status
2004   */
2005 HAL_StatusTypeDef  HAL_JPEG_Pause(JPEG_HandleTypeDef *hjpeg, uint32_t XferSelection)
2006 {
2007   uint32_t mask = 0;
2008 
2009   assert_param(IS_JPEG_PAUSE_RESUME_STATE(XferSelection));
2010 
2011   if ((hjpeg->Context & JPEG_CONTEXT_METHOD_MASK) == JPEG_CONTEXT_DMA)
2012   {
2013     if ((XferSelection & JPEG_PAUSE_RESUME_INPUT) == JPEG_PAUSE_RESUME_INPUT)
2014     {
2015       hjpeg->Context |= JPEG_CONTEXT_PAUSE_INPUT;
2016     }
2017     if ((XferSelection & JPEG_PAUSE_RESUME_OUTPUT) == JPEG_PAUSE_RESUME_OUTPUT)
2018     {
2019       hjpeg->Context |= JPEG_CONTEXT_PAUSE_OUTPUT;
2020     }
2021 
2022   }
2023   else if ((hjpeg->Context & JPEG_CONTEXT_METHOD_MASK) == JPEG_CONTEXT_IT)
2024   {
2025 
2026     if ((XferSelection & JPEG_PAUSE_RESUME_INPUT) == JPEG_PAUSE_RESUME_INPUT)
2027     {
2028       hjpeg->Context |= JPEG_CONTEXT_PAUSE_INPUT;
2029       mask |= (JPEG_IT_IFT | JPEG_IT_IFNF);
2030     }
2031     if ((XferSelection & JPEG_PAUSE_RESUME_OUTPUT) == JPEG_PAUSE_RESUME_OUTPUT)
2032     {
2033       hjpeg->Context |= JPEG_CONTEXT_PAUSE_OUTPUT;
2034       mask |= (JPEG_IT_OFT | JPEG_IT_OFNE | JPEG_IT_EOC);
2035     }
2036     __HAL_JPEG_DISABLE_IT(hjpeg, mask);
2037 
2038   }
2039   else
2040   {
2041     /* Nothing to do */
2042   }
2043 
2044   /* Return function status */
2045   return HAL_OK;
2046 }
2047 
2048 /**
2049   * @brief  Resume the JPEG Input/Output processing
2050   * @param  hjpeg pointer to a JPEG_HandleTypeDef structure that contains
2051   *         the configuration information for JPEG module
2052   * @param  XferSelection This parameter can be one of the following values :
2053   *                           JPEG_PAUSE_RESUME_INPUT : Resume Input processing
2054   *                           JPEG_PAUSE_RESUME_OUTPUT: Resume Output processing
2055   *                           JPEG_PAUSE_RESUME_INPUT_OUTPUT: Resume Input and Output processing
2056   * @retval HAL status
2057   */
2058 HAL_StatusTypeDef  HAL_JPEG_Resume(JPEG_HandleTypeDef *hjpeg, uint32_t XferSelection)
2059 {
2060   uint32_t mask = 0;
2061   uint32_t xfrSize;
2062 
2063   assert_param(IS_JPEG_PAUSE_RESUME_STATE(XferSelection));
2064 
2065   if ((hjpeg->Context & (JPEG_CONTEXT_PAUSE_INPUT | JPEG_CONTEXT_PAUSE_OUTPUT)) == 0UL)
2066   {
2067     /* if nothing paused to resume return error*/
2068     return HAL_ERROR;
2069   }
2070 
2071   if ((hjpeg->Context & JPEG_CONTEXT_METHOD_MASK) == JPEG_CONTEXT_DMA)
2072   {
2073 
2074     if ((XferSelection & JPEG_PAUSE_RESUME_INPUT) == JPEG_PAUSE_RESUME_INPUT)
2075     {
2076       hjpeg->Context &= (~JPEG_CONTEXT_PAUSE_INPUT);
2077       /*if the MDMA In is triggred with JPEG In FIFO Threshold flag
2078         then MDMA In buffer size is 32 bytes
2079 
2080         else (MDMA In is triggred with JPEG In FIFO not full flag)
2081         then MDMA In buffer size is 4 bytes
2082         */
2083       xfrSize = hjpeg->hdmain->Init.BufferTransferLength;
2084 
2085       if (xfrSize == 0UL)
2086       {
2087         hjpeg->ErrorCode |= HAL_JPEG_ERROR_DMA;
2088         hjpeg->State = HAL_JPEG_STATE_ERROR;
2089         return HAL_ERROR;
2090       }
2091       /*MDMA transfer size (BNDTR) must be a multiple of MDMA buffer size (TLEN)*/
2092       hjpeg->InDataLength = hjpeg->InDataLength - (hjpeg->InDataLength % xfrSize);
2093 
2094 
2095       if (hjpeg->InDataLength > 0UL)
2096       {
2097         /* Start DMA FIFO In transfer */
2098         if (HAL_MDMA_Start_IT(hjpeg->hdmain, (uint32_t)hjpeg->pJpegInBuffPtr, (uint32_t)&hjpeg->Instance->DIR,
2099                               hjpeg->InDataLength, 1) != HAL_OK)
2100         {
2101           hjpeg->ErrorCode |= HAL_JPEG_ERROR_DMA;
2102           hjpeg->State = HAL_JPEG_STATE_ERROR;
2103           return HAL_ERROR;
2104         }
2105       }
2106     }
2107     if ((XferSelection & JPEG_PAUSE_RESUME_OUTPUT) == JPEG_PAUSE_RESUME_OUTPUT)
2108     {
2109       hjpeg->Context &= (~JPEG_CONTEXT_PAUSE_OUTPUT);
2110 
2111       if ((hjpeg->Context & JPEG_CONTEXT_ENDING_DMA) != 0UL)
2112       {
2113         JPEG_DMA_PollResidualData(hjpeg);
2114       }
2115       else
2116       {
2117         /*if the MDMA Out is triggred with JPEG Out FIFO Threshold flag
2118           then MDMA out buffer size is 32 bytes
2119           else (MDMA Out is triggred with JPEG Out FIFO not empty flag)
2120           then MDMA buffer size is 4 bytes
2121           */
2122         xfrSize = hjpeg->hdmaout->Init.BufferTransferLength;
2123 
2124         if (xfrSize == 0UL)
2125         {
2126           hjpeg->ErrorCode |= HAL_JPEG_ERROR_DMA;
2127           hjpeg->State = HAL_JPEG_STATE_ERROR;
2128           return HAL_ERROR;
2129         }
2130         /*MDMA transfer size (BNDTR) must be a multiple of MDMA buffer size (TLEN)*/
2131         hjpeg->OutDataLength = hjpeg->OutDataLength - (hjpeg->OutDataLength % xfrSize);
2132 
2133         /* Start DMA FIFO Out transfer */
2134         if (HAL_MDMA_Start_IT(hjpeg->hdmaout, (uint32_t)&hjpeg->Instance->DOR, (uint32_t)hjpeg->pJpegOutBuffPtr,
2135                               hjpeg->OutDataLength, 1) != HAL_OK)
2136         {
2137           hjpeg->ErrorCode |= HAL_JPEG_ERROR_DMA;
2138           hjpeg->State = HAL_JPEG_STATE_ERROR;
2139           return HAL_ERROR;
2140         }
2141       }
2142 
2143     }
2144 
2145   }
2146   else if ((hjpeg->Context & JPEG_CONTEXT_METHOD_MASK) == JPEG_CONTEXT_IT)
2147   {
2148     if ((XferSelection & JPEG_PAUSE_RESUME_INPUT) == JPEG_PAUSE_RESUME_INPUT)
2149     {
2150       hjpeg->Context &= (~JPEG_CONTEXT_PAUSE_INPUT);
2151       mask |= (JPEG_IT_IFT | JPEG_IT_IFNF);
2152     }
2153     if ((XferSelection & JPEG_PAUSE_RESUME_OUTPUT) == JPEG_PAUSE_RESUME_OUTPUT)
2154     {
2155       hjpeg->Context &= (~JPEG_CONTEXT_PAUSE_OUTPUT);
2156       mask |= (JPEG_IT_OFT | JPEG_IT_OFNE | JPEG_IT_EOC);
2157     }
2158     __HAL_JPEG_ENABLE_IT(hjpeg, mask);
2159 
2160   }
2161   else
2162   {
2163     /* Nothing to do */
2164   }
2165 
2166   /* Return function status */
2167   return HAL_OK;
2168 }
2169 
2170 /**
2171   * @brief  Config Encoding/Decoding Input Buffer.
2172   * @param  hjpeg pointer to a JPEG_HandleTypeDef structure that contains
2173   *         the configuration information for JPEG module.
2174   * @param  pNewInputBuffer Pointer to the new input data buffer
2175   * @param  InDataLength Size in bytes of the new Input data buffer
2176   * @retval HAL status
2177   */
2178 void HAL_JPEG_ConfigInputBuffer(JPEG_HandleTypeDef *hjpeg, uint8_t *pNewInputBuffer, uint32_t InDataLength)
2179 {
2180   hjpeg->pJpegInBuffPtr =  pNewInputBuffer;
2181   hjpeg->InDataLength = InDataLength;
2182 }
2183 
2184 /**
2185   * @brief  Config Encoding/Decoding Output Buffer.
2186   * @param  hjpeg pointer to a JPEG_HandleTypeDef structure that contains
2187   *         the configuration information for JPEG module.
2188   * @param  pNewOutputBuffer Pointer to the new output data buffer
2189   * @param  OutDataLength Size in bytes of the new Output data buffer
2190   * @retval HAL status
2191   */
2192 void HAL_JPEG_ConfigOutputBuffer(JPEG_HandleTypeDef *hjpeg, uint8_t *pNewOutputBuffer, uint32_t OutDataLength)
2193 {
2194   hjpeg->pJpegOutBuffPtr = pNewOutputBuffer;
2195   hjpeg->OutDataLength = OutDataLength;
2196 }
2197 
2198 /**
2199   * @brief  Aborts the JPEG Encoding/Decoding.
2200   * @param  hjpeg pointer to a JPEG_HandleTypeDef structure that contains
2201   *         the configuration information for JPEG module
2202   * @retval HAL status
2203   */
2204 HAL_StatusTypeDef HAL_JPEG_Abort(JPEG_HandleTypeDef *hjpeg)
2205 {
2206   uint32_t tickstart;
2207   uint32_t tmpContext;
2208   tmpContext = hjpeg->Context;
2209 
2210   /*Reset the Context operation and method*/
2211   hjpeg->Context &= ~(JPEG_CONTEXT_OPERATION_MASK | JPEG_CONTEXT_METHOD_MASK | JPEG_CONTEXT_ENDING_DMA);
2212 
2213   if ((tmpContext & JPEG_CONTEXT_METHOD_MASK) == JPEG_CONTEXT_DMA)
2214   {
2215     /* Stop the DMA In/out Xfer*/
2216     if (HAL_MDMA_Abort(hjpeg->hdmaout) != HAL_OK)
2217     {
2218       if (hjpeg->hdmaout->ErrorCode == HAL_MDMA_ERROR_TIMEOUT)
2219       {
2220         hjpeg->ErrorCode |= HAL_JPEG_ERROR_DMA;
2221       }
2222     }
2223     if (HAL_MDMA_Abort(hjpeg->hdmain) != HAL_OK)
2224     {
2225       if (hjpeg->hdmain->ErrorCode == HAL_MDMA_ERROR_TIMEOUT)
2226       {
2227         hjpeg->ErrorCode |= HAL_JPEG_ERROR_DMA;
2228       }
2229     }
2230 
2231   }
2232 
2233   /* Stop the JPEG encoding/decoding process*/
2234   hjpeg->Instance->CONFR0 &=  ~JPEG_CONFR0_START;
2235 
2236   /* Get tick */
2237   tickstart = HAL_GetTick();
2238 
2239   /* Check if the JPEG Codec is effectively disabled */
2240   while (__HAL_JPEG_GET_FLAG(hjpeg, JPEG_FLAG_COF) != 0UL)
2241   {
2242     /* Check for the Timeout */
2243     if ((HAL_GetTick() - tickstart) > JPEG_TIMEOUT_VALUE)
2244     {
2245       /* Update error code */
2246       hjpeg->ErrorCode |= HAL_JPEG_ERROR_TIMEOUT;
2247 
2248       /* Change the DMA state */
2249       hjpeg->State = HAL_JPEG_STATE_ERROR;
2250       break;
2251     }
2252   }
2253 
2254   /* Disable All Interrupts */
2255   __HAL_JPEG_DISABLE_IT(hjpeg, JPEG_INTERRUPT_MASK);
2256 
2257 
2258   /* Flush input and output FIFOs*/
2259   hjpeg->Instance->CR |= JPEG_CR_IFF;
2260   hjpeg->Instance->CR |= JPEG_CR_OFF;
2261 
2262   /* Clear all flags */
2263   __HAL_JPEG_CLEAR_FLAG(hjpeg, JPEG_FLAG_ALL);
2264 
2265   /* Reset JpegInCount and JpegOutCount */
2266   hjpeg->JpegInCount = 0;
2267   hjpeg->JpegOutCount = 0;
2268 
2269   /*Reset the Context Pause*/
2270   hjpeg->Context &= ~(JPEG_CONTEXT_PAUSE_INPUT | JPEG_CONTEXT_PAUSE_OUTPUT);
2271 
2272   /* Change the DMA state*/
2273   if (hjpeg->ErrorCode != HAL_JPEG_ERROR_NONE)
2274   {
2275     hjpeg->State = HAL_JPEG_STATE_ERROR;
2276     /* Process Unlocked */
2277     __HAL_UNLOCK(hjpeg);
2278     /* Return function status */
2279     return HAL_ERROR;
2280   }
2281   else
2282   {
2283     hjpeg->State = HAL_JPEG_STATE_READY;
2284     /* Process Unlocked */
2285     __HAL_UNLOCK(hjpeg);
2286     /* Return function status */
2287     return HAL_OK;
2288   }
2289 
2290 }
2291 
2292 
2293 /**
2294   * @}
2295   */
2296 
2297 /** @defgroup JPEG_Exported_Functions_Group4 JPEG Decode/Encode callback functions
2298   * @ingroup RTEMSBSPsARMSTM32H7
2299   *  @brief   JPEG process callback functions.
2300   *
2301 @verbatim
2302   ==============================================================================
2303               #####  JPEG Decode and Encode callback functions  #####
2304   ==============================================================================
2305     [..]  This section provides callback functions:
2306       (+) HAL_JPEG_InfoReadyCallback()  : Decoding JPEG Info ready callback
2307       (+) HAL_JPEG_EncodeCpltCallback() : Encoding complete callback.
2308       (+) HAL_JPEG_DecodeCpltCallback() : Decoding complete callback.
2309       (+) HAL_JPEG_ErrorCallback()      : JPEG error callback.
2310       (+) HAL_JPEG_GetDataCallback()    : Get New Data chunk callback.
2311       (+) HAL_JPEG_DataReadyCallback()  : Decoded/Encoded Data ready  callback.
2312 
2313 @endverbatim
2314   * @{
2315   */
2316 
2317 /**
2318   * @brief  Decoding JPEG Info ready callback.
2319   * @param  hjpeg pointer to a JPEG_HandleTypeDef structure that contains
2320   *         the configuration information for JPEG module
2321   * @param  pInfo pointer to a JPEG_ConfTypeDef structure that contains
2322   *         The JPEG decoded header information
2323   * @retval None
2324   */
2325 __weak void HAL_JPEG_InfoReadyCallback(JPEG_HandleTypeDef *hjpeg, JPEG_ConfTypeDef *pInfo)
2326 {
2327   /* Prevent unused argument(s) compilation warning */
2328   UNUSED(hjpeg);
2329   UNUSED(pInfo);
2330 
2331   /* NOTE : This function Should not be modified, when the callback is needed,
2332             the HAL_JPEG_HeaderParsingCpltCallback could be implemented in the user file
2333    */
2334 }
2335 
2336 /**
2337   * @brief  Encoding complete callback.
2338   * @param  hjpeg pointer to a JPEG_HandleTypeDef structure that contains
2339   *         the configuration information for JPEG module
2340   * @retval None
2341   */
2342 __weak void HAL_JPEG_EncodeCpltCallback(JPEG_HandleTypeDef *hjpeg)
2343 {
2344   /* Prevent unused argument(s) compilation warning */
2345   UNUSED(hjpeg);
2346 
2347   /* NOTE : This function Should not be modified, when the callback is needed,
2348             the HAL_JPEG_EncodeCpltCallback could be implemented in the user file
2349    */
2350 }
2351 
2352 /**
2353   * @brief  Decoding complete callback.
2354   * @param  hjpeg pointer to a JPEG_HandleTypeDef structure that contains
2355   *         the configuration information for JPEG module
2356   * @retval None
2357   */
2358 __weak void HAL_JPEG_DecodeCpltCallback(JPEG_HandleTypeDef *hjpeg)
2359 {
2360   /* Prevent unused argument(s) compilation warning */
2361   UNUSED(hjpeg);
2362 
2363   /* NOTE : This function Should not be modified, when the callback is needed,
2364             the HAL_JPEG_EncodeCpltCallback could be implemented in the user file
2365    */
2366 }
2367 
2368 /**
2369   * @brief  JPEG error callback.
2370   * @param  hjpeg pointer to a JPEG_HandleTypeDef structure that contains
2371   *         the configuration information for JPEG module
2372   * @retval None
2373   */
2374 __weak void HAL_JPEG_ErrorCallback(JPEG_HandleTypeDef *hjpeg)
2375 {
2376   /* Prevent unused argument(s) compilation warning */
2377   UNUSED(hjpeg);
2378 
2379   /* NOTE : This function Should not be modified, when the callback is needed,
2380             the HAL_JPEG_ErrorCallback could be implemented in the user file
2381    */
2382 }
2383 
2384 /**
2385   * @brief  Get New Data chunk callback.
2386   * @param  hjpeg pointer to a JPEG_HandleTypeDef structure that contains
2387   *         the configuration information for JPEG module
2388   * @param  NbDecodedData Number of consummed data in the previous chunk in bytes
2389   * @retval None
2390   */
2391 __weak void HAL_JPEG_GetDataCallback(JPEG_HandleTypeDef *hjpeg, uint32_t NbDecodedData)
2392 {
2393   /* Prevent unused argument(s) compilation warning */
2394   UNUSED(hjpeg);
2395   UNUSED(NbDecodedData);
2396 
2397   /* NOTE : This function Should not be modified, when the callback is needed,
2398             the HAL_JPEG_GetDataCallback could be implemented in the user file
2399    */
2400 }
2401 
2402 /**
2403   * @brief  Decoded/Encoded Data ready  callback.
2404   * @param  hjpeg pointer to a JPEG_HandleTypeDef structure that contains
2405   *         the configuration information for JPEG module
2406   * @param  pDataOut pointer to the output data buffer
2407   * @param  OutDataLength number in bytes of data available in the specified output buffer
2408   * @retval None
2409   */
2410 __weak void HAL_JPEG_DataReadyCallback(JPEG_HandleTypeDef *hjpeg, uint8_t *pDataOut, uint32_t OutDataLength)
2411 {
2412   /* Prevent unused argument(s) compilation warning */
2413   UNUSED(hjpeg);
2414   UNUSED(pDataOut);
2415   UNUSED(OutDataLength);
2416 
2417   /* NOTE : This function Should not be modified, when the callback is needed,
2418             the HAL_JPEG_DataReadyCallback could be implemented in the user file
2419    */
2420 }
2421 
2422 /**
2423   * @}
2424   */
2425 
2426 
2427 /** @defgroup JPEG_Exported_Functions_Group5 JPEG IRQ handler management
2428   * @ingroup RTEMSBSPsARMSTM32H7
2429   *  @brief   JPEG IRQ handler.
2430   *
2431 @verbatim
2432   ==============================================================================
2433                 ##### JPEG IRQ handler management #####
2434   ==============================================================================
2435     [..]  This section provides JPEG IRQ handler function.
2436       (+) HAL_JPEG_IRQHandler()  : handles JPEG interrupt request
2437 
2438 @endverbatim
2439   * @{
2440   */
2441 
2442 /**
2443   * @brief  This function handles JPEG interrupt request.
2444   * @param  hjpeg pointer to a JPEG_HandleTypeDef structure that contains
2445   *         the configuration information for JPEG module
2446   * @retval None
2447   */
2448 void HAL_JPEG_IRQHandler(JPEG_HandleTypeDef *hjpeg)
2449 {
2450   switch (hjpeg->State)
2451   {
2452     case HAL_JPEG_STATE_BUSY_ENCODING:
2453     case HAL_JPEG_STATE_BUSY_DECODING:
2454       /* continue JPEG data encoding/Decoding*/
2455       /* JPEG data processing : In/Out FIFO transfer*/
2456       if ((hjpeg->Context & JPEG_CONTEXT_METHOD_MASK) == JPEG_CONTEXT_IT)
2457       {
2458         (void) JPEG_Process(hjpeg);
2459       }
2460       else if ((hjpeg->Context & JPEG_CONTEXT_METHOD_MASK) == JPEG_CONTEXT_DMA)
2461       {
2462         JPEG_DMA_ContinueProcess(hjpeg);
2463       }
2464       else
2465       {
2466         /* Nothing to do */
2467       }
2468       break;
2469 
2470     default:
2471       break;
2472   }
2473 }
2474 
2475 /**
2476   * @}
2477   */
2478 
2479 /** @defgroup JPEG_Exported_Functions_Group6 Peripheral State functions
2480   * @ingroup RTEMSBSPsARMSTM32H7
2481   *  @brief   Peripheral State functions.
2482   *
2483 @verbatim
2484   ==============================================================================
2485                     ##### Peripheral State and Error functions #####
2486   ==============================================================================
2487     [..]  This section provides JPEG State and Errors function.
2488       (+) HAL_JPEG_GetState()  : permits to get in run-time the JPEG state.
2489       (+) HAL_JPEG_GetError()  : Returns the JPEG error code if any.
2490 
2491 @endverbatim
2492   * @{
2493   */
2494 
2495 /**
2496   * @brief  Returns the JPEG state.
2497   * @param  hjpeg pointer to a JPEG_HandleTypeDef structure that contains
2498   *         the configuration information for JPEG module
2499   * @retval JPEG state
2500   */
2501 HAL_JPEG_STATETypeDef HAL_JPEG_GetState(JPEG_HandleTypeDef *hjpeg)
2502 {
2503   return hjpeg->State;
2504 }
2505 
2506 /**
2507   * @brief  Return the JPEG error code
2508   * @param  hjpeg  pointer to a JPEG_HandleTypeDef structure that contains
2509   *              the configuration information for the specified JPEG.
2510   * @retval JPEG Error Code
2511   */
2512 uint32_t HAL_JPEG_GetError(JPEG_HandleTypeDef *hjpeg)
2513 {
2514   return hjpeg->ErrorCode;
2515 }
2516 
2517 /**
2518   * @}
2519   */
2520 
2521 /**
2522   * @}
2523   */
2524 
2525 
2526 /** @addtogroup JPEG_Private_Functions
2527   * @{
2528   */
2529 
2530 /**
2531   * @brief  Generates Huffman sizes/Codes Table from Bits/vals Table
2532   * @param  Bits pointer to bits table
2533   * @param  Huffsize pointer to sizes table
2534   * @param  Huffcode pointer to codes table
2535   * @param  LastK pointer to last Coeff (table dimension)
2536   * @retval HAL status
2537   */
2538 static HAL_StatusTypeDef JPEG_Bits_To_SizeCodes(uint8_t *Bits, uint8_t *Huffsize, uint32_t *Huffcode, uint32_t *LastK)
2539 {
2540   uint32_t i;
2541   uint32_t p;
2542   uint32_t l;
2543   uint32_t code;
2544   uint32_t si;
2545 
2546   /* Figure C.1: Generation of table of Huffman code sizes */
2547   p = 0;
2548   for (l = 0; l < 16UL; l++)
2549   {
2550     i = (uint32_t)Bits[l];
2551     if ((p + i) > 256UL)
2552     {
2553       /* check for table overflow */
2554       return HAL_ERROR;
2555     }
2556     while (i != 0UL)
2557     {
2558       Huffsize[p] = (uint8_t) l + 1U;
2559       p++;
2560       i--;
2561     }
2562   }
2563   Huffsize[p] = 0;
2564   *LastK = p;
2565 
2566   /* Figure C.2: Generation of table of Huffman codes */
2567   code = 0;
2568   si = Huffsize[0];
2569   p = 0;
2570   while (Huffsize[p] != 0U)
2571   {
2572     while (((uint32_t) Huffsize[p]) == si)
2573     {
2574       Huffcode[p] = code;
2575       p++;
2576       code++;
2577     }
2578     /* code must fit in "size" bits (si), no code is allowed to be all ones*/
2579     if(si > 31UL)
2580     {
2581       return HAL_ERROR;
2582     }
2583     if (((uint32_t) code) >= (((uint32_t) 1) << si))
2584     {
2585       return HAL_ERROR;
2586     }
2587     code <<= 1;
2588     si++;
2589   }
2590   /* Return function status */
2591   return HAL_OK;
2592 }
2593 
2594 /**
2595   * @brief  Transform a Bits/Vals AC Huffman table to sizes/Codes huffman Table
2596   *         that can programmed to the JPEG encoder registers
2597   * @param  AC_BitsValsTable pointer to AC huffman bits/vals table
2598   * @param  AC_SizeCodesTable pointer to AC huffman Sizes/Codes table
2599   * @retval HAL status
2600   */
2601 static HAL_StatusTypeDef JPEG_ACHuff_BitsVals_To_SizeCodes(JPEG_ACHuffTableTypeDef *AC_BitsValsTable,
2602                                                            JPEG_AC_HuffCodeTableTypeDef *AC_SizeCodesTable)
2603 {
2604   HAL_StatusTypeDef error;
2605   uint8_t huffsize[257];
2606   uint32_t huffcode[257];
2607   uint32_t k;
2608   uint32_t l, lsb, msb;
2609   uint32_t lastK;
2610 
2611   error = JPEG_Bits_To_SizeCodes(AC_BitsValsTable->Bits, huffsize, huffcode, &lastK);
2612   if (error != HAL_OK)
2613   {
2614     return  error;
2615   }
2616 
2617   /* Figure C.3: Ordering procedure for encoding procedure code tables */
2618   k = 0;
2619 
2620   while (k < lastK)
2621   {
2622     l = AC_BitsValsTable->HuffVal[k];
2623     if (l == 0UL)
2624     {
2625       l = 160; /*l = 0x00 EOB code*/
2626     }
2627     else if (l == 0xF0UL) /* l = 0xF0 ZRL code*/
2628     {
2629       l = 161;
2630     }
2631     else
2632     {
2633       msb = (l & 0xF0UL) >> 4;
2634       lsb = (l & 0x0FUL);
2635       l = (msb * 10UL) + lsb - 1UL;
2636     }
2637     if (l >= JPEG_AC_HUFF_TABLE_SIZE)
2638     {
2639       return HAL_ERROR; /* Huffman Table overflow error*/
2640     }
2641     else
2642     {
2643       AC_SizeCodesTable->HuffmanCode[l] = huffcode[k];
2644       AC_SizeCodesTable->CodeLength[l] = huffsize[k] - 1U;
2645       k++;
2646     }
2647   }
2648 
2649   /* Return function status */
2650   return HAL_OK;
2651 }
2652 
2653 /**
2654   * @brief  Transform a Bits/Vals DC Huffman table to sizes/Codes huffman Table
2655   *         that can programmed to the JPEG encoder registers
2656   * @param  DC_BitsValsTable pointer to DC huffman bits/vals table
2657   * @param  DC_SizeCodesTable pointer to DC huffman Sizes/Codes table
2658   * @retval HAL status
2659   */
2660 static HAL_StatusTypeDef JPEG_DCHuff_BitsVals_To_SizeCodes(JPEG_DCHuffTableTypeDef *DC_BitsValsTable,
2661                                                            JPEG_DC_HuffCodeTableTypeDef *DC_SizeCodesTable)
2662 {
2663   HAL_StatusTypeDef error;
2664 
2665   uint32_t k;
2666   uint32_t l;
2667   uint32_t lastK;
2668   uint8_t huffsize[257];
2669   uint32_t huffcode[257];
2670   error = JPEG_Bits_To_SizeCodes(DC_BitsValsTable->Bits, huffsize, huffcode, &lastK);
2671   if (error != HAL_OK)
2672   {
2673     return  error;
2674   }
2675   /* Figure C.3: ordering procedure for encoding procedure code tables */
2676   k = 0;
2677 
2678   while (k < lastK)
2679   {
2680     l = DC_BitsValsTable->HuffVal[k];
2681     if (l >= JPEG_DC_HUFF_TABLE_SIZE)
2682     {
2683       return HAL_ERROR; /* Huffman Table overflow error*/
2684     }
2685     else
2686     {
2687       DC_SizeCodesTable->HuffmanCode[l] = huffcode[k];
2688       DC_SizeCodesTable->CodeLength[l] = huffsize[k] - 1U;
2689       k++;
2690     }
2691   }
2692 
2693   /* Return function status */
2694   return HAL_OK;
2695 }
2696 
2697 /**
2698   * @brief  Set the JPEG register with an DC huffman table at the given DC table address
2699   * @param  hjpeg pointer to a JPEG_HandleTypeDef structure that contains
2700   *         the configuration information for JPEG module
2701   * @param  HuffTableDC pointer to DC huffman table
2702   * @param  DCTableAddress Encoder DC huffman table address it could be HUFFENC_DC0 or HUFFENC_DC1.
2703   * @retval HAL status
2704   */
2705 static HAL_StatusTypeDef JPEG_Set_HuffDC_Mem(JPEG_HandleTypeDef *hjpeg, JPEG_DCHuffTableTypeDef *HuffTableDC,
2706                                              const __IO uint32_t *DCTableAddress)
2707 {
2708   HAL_StatusTypeDef error;
2709   JPEG_DC_HuffCodeTableTypeDef dcSizeCodesTable;
2710   uint32_t i;
2711   uint32_t lsb;
2712   uint32_t msb;
2713   __IO uint32_t *address, *addressDef;
2714 
2715   if (DCTableAddress == (hjpeg->Instance->HUFFENC_DC0))
2716   {
2717     address = (hjpeg->Instance->HUFFENC_DC0 + (JPEG_DC_HUFF_TABLE_SIZE / 2UL));
2718   }
2719   else if (DCTableAddress == (hjpeg->Instance->HUFFENC_DC1))
2720   {
2721     address = (hjpeg->Instance->HUFFENC_DC1 + (JPEG_DC_HUFF_TABLE_SIZE / 2UL));
2722   }
2723   else
2724   {
2725     return HAL_ERROR;
2726   }
2727 
2728   if (HuffTableDC != NULL)
2729   {
2730     error = JPEG_DCHuff_BitsVals_To_SizeCodes(HuffTableDC, &dcSizeCodesTable);
2731     if (error != HAL_OK)
2732     {
2733       return  error;
2734     }
2735     addressDef = address;
2736     *addressDef = 0x0FFF0FFF;
2737     addressDef++;
2738     *addressDef = 0x0FFF0FFF;
2739 
2740     i = JPEG_DC_HUFF_TABLE_SIZE;
2741     while (i > 1UL)
2742     {
2743       i--;
2744       address --;
2745       msb = ((uint32_t)(((uint32_t)dcSizeCodesTable.CodeLength[i] & 0xFU) << 8)) | ((uint32_t)dcSizeCodesTable.HuffmanCode[i] &
2746                                                                                    0xFFUL);
2747       i--;
2748       lsb = ((uint32_t)(((uint32_t)dcSizeCodesTable.CodeLength[i] & 0xFU) << 8)) | ((uint32_t)dcSizeCodesTable.HuffmanCode[i] &
2749                                                                                    0xFFUL);
2750 
2751       *address = lsb | (msb << 16);
2752     }
2753   }
2754 
2755   /* Return function status */
2756   return HAL_OK;
2757 }
2758 
2759 /**
2760   * @brief  Set the JPEG register with an AC huffman table at the given AC table address
2761   * @param  hjpeg pointer to a JPEG_HandleTypeDef structure that contains
2762   *         the configuration information for JPEG module
2763   * @param  HuffTableAC pointer to AC huffman table
2764   * @param  ACTableAddress Encoder AC huffman table address it could be HUFFENC_AC0 or HUFFENC_AC1.
2765   * @retval HAL status
2766   */
2767 static HAL_StatusTypeDef JPEG_Set_HuffAC_Mem(JPEG_HandleTypeDef *hjpeg, JPEG_ACHuffTableTypeDef *HuffTableAC,
2768                                              const __IO uint32_t *ACTableAddress)
2769 {
2770   HAL_StatusTypeDef error;
2771   JPEG_AC_HuffCodeTableTypeDef acSizeCodesTable;
2772   uint32_t i, lsb, msb;
2773   __IO uint32_t *address, *addressDef;
2774 
2775   if (ACTableAddress == (hjpeg->Instance->HUFFENC_AC0))
2776   {
2777     address = (hjpeg->Instance->HUFFENC_AC0 + (JPEG_AC_HUFF_TABLE_SIZE / 2UL));
2778   }
2779   else if (ACTableAddress == (hjpeg->Instance->HUFFENC_AC1))
2780   {
2781     address = (hjpeg->Instance->HUFFENC_AC1 + (JPEG_AC_HUFF_TABLE_SIZE / 2UL));
2782   }
2783   else
2784   {
2785     return HAL_ERROR;
2786   }
2787 
2788   if (HuffTableAC != NULL)
2789   {
2790     error = JPEG_ACHuff_BitsVals_To_SizeCodes(HuffTableAC, &acSizeCodesTable);
2791     if (error != HAL_OK)
2792     {
2793       return  error;
2794     }
2795     /* Default values settings: 162:167 FFFh , 168:175 FD0h_FD7h */
2796     /* Locations 162:175 of each AC table contain information used internally by the core */
2797 
2798     addressDef = address;
2799     for (i = 0; i < 3UL; i++)
2800     {
2801       *addressDef = 0x0FFF0FFF;
2802       addressDef++;
2803     }
2804     *addressDef = 0x0FD10FD0;
2805     addressDef++;
2806     *addressDef = 0x0FD30FD2;
2807     addressDef++;
2808     *addressDef = 0x0FD50FD4;
2809     addressDef++;
2810     *addressDef = 0x0FD70FD6;
2811     /* end of Locations 162:175  */
2812 
2813 
2814     i = JPEG_AC_HUFF_TABLE_SIZE;
2815     while (i > 1UL)
2816     {
2817       i--;
2818       address--;
2819       msb = ((uint32_t)(((uint32_t)acSizeCodesTable.CodeLength[i] & 0xFU) << 8)) | ((uint32_t)acSizeCodesTable.HuffmanCode[i] &
2820                                                                                    0xFFUL);
2821       i--;
2822       lsb = ((uint32_t)(((uint32_t)acSizeCodesTable.CodeLength[i] & 0xFU) << 8)) | ((uint32_t)acSizeCodesTable.HuffmanCode[i] &
2823                                                                                    0xFFUL);
2824 
2825       *address = lsb | (msb << 16);
2826     }
2827   }
2828 
2829   /* Return function status */
2830   return HAL_OK;
2831 }
2832 
2833 /**
2834   * @brief  Configure the JPEG encoder register huffman tables to used during
2835   *         the encdoing operation
2836   * @param  hjpeg pointer to a JPEG_HandleTypeDef structure that contains
2837   *         the configuration information for JPEG module
2838   * @retval None
2839   */
2840 static HAL_StatusTypeDef JPEG_Set_HuffEnc_Mem(JPEG_HandleTypeDef *hjpeg)
2841 {
2842   HAL_StatusTypeDef error;
2843 
2844   JPEG_Set_Huff_DHTMem(hjpeg);
2845   error = JPEG_Set_HuffAC_Mem(hjpeg, (JPEG_ACHuffTableTypeDef *)(uint32_t)&JPEG_ACLUM_HuffTable,
2846                               (hjpeg->Instance->HUFFENC_AC0));
2847   if (error != HAL_OK)
2848   {
2849     return  error;
2850   }
2851 
2852   error = JPEG_Set_HuffAC_Mem(hjpeg, (JPEG_ACHuffTableTypeDef *)(uint32_t)&JPEG_ACCHROM_HuffTable,
2853                               (hjpeg->Instance->HUFFENC_AC1));
2854   if (error != HAL_OK)
2855   {
2856     return  error;
2857   }
2858 
2859   error = JPEG_Set_HuffDC_Mem(hjpeg, (JPEG_DCHuffTableTypeDef *)(uint32_t)&JPEG_DCLUM_HuffTable,
2860                               hjpeg->Instance->HUFFENC_DC0);
2861   if (error != HAL_OK)
2862   {
2863     return  error;
2864   }
2865 
2866   error = JPEG_Set_HuffDC_Mem(hjpeg, (JPEG_DCHuffTableTypeDef *)(uint32_t)&JPEG_DCCHROM_HuffTable,
2867                               hjpeg->Instance->HUFFENC_DC1);
2868   if (error != HAL_OK)
2869   {
2870     return  error;
2871   }
2872   /* Return function status */
2873   return HAL_OK;
2874 }
2875 
2876 /**
2877   * @brief  Configure the JPEG register huffman tables to be included in the JPEG
2878   *         file header (used for encoding only)
2879   * @param  hjpeg pointer to a JPEG_HandleTypeDef structure that contains
2880   *         the configuration information for JPEG module
2881   * @retval None
2882   */
2883 static void JPEG_Set_Huff_DHTMem(JPEG_HandleTypeDef *hjpeg)
2884 {
2885   JPEG_ACHuffTableTypeDef *HuffTableAC0 = (JPEG_ACHuffTableTypeDef *)(uint32_t)&JPEG_ACLUM_HuffTable;
2886   JPEG_ACHuffTableTypeDef *HuffTableAC1 = (JPEG_ACHuffTableTypeDef *)(uint32_t)&JPEG_ACCHROM_HuffTable;
2887   JPEG_DCHuffTableTypeDef *HuffTableDC0 = (JPEG_DCHuffTableTypeDef *)(uint32_t)&JPEG_DCLUM_HuffTable;
2888   JPEG_DCHuffTableTypeDef *HuffTableDC1 = (JPEG_DCHuffTableTypeDef *)(uint32_t)&JPEG_DCCHROM_HuffTable;
2889   uint32_t value, index;
2890   __IO uint32_t *address;
2891 
2892   /* DC0 Huffman Table : BITS*/
2893   /* DC0 BITS is a 16 Bytes table i.e 4x32bits words from DHTMEM base address to DHTMEM + 3*/
2894   address = (hjpeg->Instance->DHTMEM + 3);
2895   index = 16;
2896   while (index > 3UL)
2897   {
2898 
2899     *address = (((uint32_t)HuffTableDC0->Bits[index - 1UL] & 0xFFUL) << 24) |
2900                (((uint32_t)HuffTableDC0->Bits[index - 2UL] & 0xFFUL) << 16) |
2901                (((uint32_t)HuffTableDC0->Bits[index - 3UL] & 0xFFUL) << 8) |
2902                ((uint32_t)HuffTableDC0->Bits[index - 4UL] & 0xFFUL);
2903     address--;
2904     index -= 4UL;
2905 
2906   }
2907   /* DC0 Huffman Table : Val*/
2908   /* DC0 VALS is a 12 Bytes table i.e 3x32bits words from DHTMEM base address +4 to DHTMEM + 6 */
2909   address = (hjpeg->Instance->DHTMEM + 6);
2910   index = 12;
2911   while (index > 3UL)
2912   {
2913     *address = (((uint32_t)HuffTableDC0->HuffVal[index - 1UL] & 0xFFUL) << 24) |
2914                (((uint32_t)HuffTableDC0->HuffVal[index - 2UL] & 0xFFUL) << 16) |
2915                (((uint32_t)HuffTableDC0->HuffVal[index - 3UL] & 0xFFUL) << 8) |
2916                ((uint32_t)HuffTableDC0->HuffVal[index - 4UL] & 0xFFUL);
2917     address--;
2918     index -= 4UL;
2919   }
2920 
2921   /* AC0 Huffman Table : BITS*/
2922   /* AC0 BITS is a 16 Bytes table i.e 4x32bits words from DHTMEM base address + 7 to DHTMEM + 10*/
2923   address = (hjpeg->Instance->DHTMEM + 10UL);
2924   index = 16;
2925   while (index > 3UL)
2926   {
2927 
2928     *address = (((uint32_t)HuffTableAC0->Bits[index - 1UL] & 0xFFUL) << 24) |
2929                (((uint32_t)HuffTableAC0->Bits[index - 2UL] & 0xFFUL) << 16) |
2930                (((uint32_t)HuffTableAC0->Bits[index - 3UL] & 0xFFUL) << 8) |
2931                ((uint32_t)HuffTableAC0->Bits[index - 4UL] & 0xFFUL);
2932     address--;
2933     index -= 4UL;
2934 
2935   }
2936   /* AC0 Huffman Table : Val*/
2937   /* AC0 VALS is a 162 Bytes table i.e 41x32bits words from DHTMEM base address + 11 to DHTMEM + 51 */
2938   /* only Byte 0 and Byte 1 of the last word (@ DHTMEM + 51) belong to AC0 VALS table */
2939   address = (hjpeg->Instance->DHTMEM + 51);
2940   value = *address & 0xFFFF0000U;
2941   value = value | (((uint32_t)HuffTableAC0->HuffVal[161] & 0xFFUL) << 8) | ((uint32_t)HuffTableAC0->HuffVal[160] & 0xFFUL);
2942   *address = value;
2943 
2944   /*continue setting 160 AC0 huffman values */
2945   address--; /* address = hjpeg->Instance->DHTMEM + 50*/
2946   index = 160;
2947   while (index > 3UL)
2948   {
2949     *address = (((uint32_t)HuffTableAC0->HuffVal[index - 1UL] & 0xFFUL) << 24) |
2950                (((uint32_t)HuffTableAC0->HuffVal[index - 2UL] & 0xFFUL) << 16) |
2951                (((uint32_t)HuffTableAC0->HuffVal[index - 3UL] & 0xFFUL) << 8) |
2952                ((uint32_t)HuffTableAC0->HuffVal[index - 4UL] & 0xFFUL);
2953     address--;
2954     index -= 4UL;
2955   }
2956 
2957   /* DC1 Huffman Table : BITS*/
2958   /* DC1 BITS is a 16 Bytes table i.e 4x32bits words from DHTMEM + 51 base address to DHTMEM + 55*/
2959   /* only Byte 2 and Byte 3 of the first word (@ DHTMEM + 51) belong to DC1 Bits table */
2960   address = (hjpeg->Instance->DHTMEM + 51);
2961   value = *address & 0x0000FFFFU;
2962   value = value | (((uint32_t)HuffTableDC1->Bits[1] & 0xFFUL) << 24) | (((uint32_t)HuffTableDC1->Bits[0] & 0xFFUL) << 16);
2963   *address = value;
2964 
2965   /* only Byte 0 and Byte 1 of the last word (@ DHTMEM + 55) belong to DC1 Bits table */
2966   address = (hjpeg->Instance->DHTMEM + 55);
2967   value = *address & 0xFFFF0000U;
2968   value = value | (((uint32_t)HuffTableDC1->Bits[15] & 0xFFUL) << 8) | ((uint32_t)HuffTableDC1->Bits[14] & 0xFFUL);
2969   *address = value;
2970 
2971   /*continue setting 12 DC1 huffman Bits from DHTMEM + 54 down to DHTMEM + 52*/
2972   address--;
2973   index = 12;
2974   while (index > 3UL)
2975   {
2976 
2977     *address = (((uint32_t)HuffTableDC1->Bits[index + 1UL] & 0xFFUL) << 24) |
2978                (((uint32_t)HuffTableDC1->Bits[index] & 0xFFUL) << 16) |
2979                (((uint32_t)HuffTableDC1->Bits[index - 1UL] & 0xFFUL) << 8) |
2980                ((uint32_t)HuffTableDC1->Bits[index - 2UL] & 0xFFUL);
2981     address--;
2982     index -= 4UL;
2983 
2984   }
2985   /* DC1 Huffman Table : Val*/
2986   /* DC1 VALS is a 12 Bytes table i.e 3x32bits words from DHTMEM base address +55 to DHTMEM + 58 */
2987   /* only Byte 2 and Byte 3 of the first word (@ DHTMEM + 55) belong to DC1 Val table */
2988   address = (hjpeg->Instance->DHTMEM + 55);
2989   value = *address & 0x0000FFFFUL;
2990   value = value | (((uint32_t)HuffTableDC1->HuffVal[1] & 0xFFUL) << 24) | (((uint32_t)HuffTableDC1->HuffVal[0] & 0xFFUL) <<
2991                                                                          16);
2992   *address = value;
2993 
2994   /* only Byte 0 and Byte 1 of the last word (@ DHTMEM + 58) belong to DC1 Val table */
2995   address = (hjpeg->Instance->DHTMEM + 58);
2996   value = *address & 0xFFFF0000UL;
2997   value = value | (((uint32_t)HuffTableDC1->HuffVal[11] & 0xFFUL) << 8) | ((uint32_t)HuffTableDC1->HuffVal[10] & 0xFFUL);
2998   *address = value;
2999 
3000   /*continue setting 8 DC1 huffman val from DHTMEM + 57 down to DHTMEM + 56*/
3001   address--;
3002   index = 8;
3003   while (index > 3UL)
3004   {
3005     *address = (((uint32_t)HuffTableDC1->HuffVal[index + 1UL] & 0xFFUL) << 24) |
3006                (((uint32_t)HuffTableDC1->HuffVal[index] & 0xFFUL) << 16) |
3007                (((uint32_t)HuffTableDC1->HuffVal[index - 1UL] & 0xFFUL) << 8) |
3008                ((uint32_t)HuffTableDC1->HuffVal[index - 2UL] & 0xFFUL);
3009     address--;
3010     index -= 4UL;
3011   }
3012 
3013   /* AC1 Huffman Table : BITS*/
3014   /* AC1 BITS is a 16 Bytes table i.e 4x32bits words from DHTMEM base address + 58 to DHTMEM + 62*/
3015   /* only Byte 2 and Byte 3 of the first word (@ DHTMEM + 58) belong to AC1 Bits table */
3016   address = (hjpeg->Instance->DHTMEM + 58);
3017   value = *address & 0x0000FFFFU;
3018   value = value | (((uint32_t)HuffTableAC1->Bits[1] & 0xFFUL) << 24) | (((uint32_t)HuffTableAC1->Bits[0] & 0xFFUL) << 16);
3019   *address = value;
3020 
3021   /* only Byte 0 and Byte 1 of the last word (@ DHTMEM + 62) belong to Bits Val table */
3022   address = (hjpeg->Instance->DHTMEM + 62);
3023   value = *address & 0xFFFF0000U;
3024   value = value | (((uint32_t)HuffTableAC1->Bits[15] & 0xFFUL) << 8) | ((uint32_t)HuffTableAC1->Bits[14] & 0xFFUL);
3025   *address = value;
3026 
3027   /*continue setting 12 AC1 huffman Bits from DHTMEM + 61 down to DHTMEM + 59*/
3028   address--;
3029   index = 12;
3030   while (index > 3UL)
3031   {
3032 
3033     *address = (((uint32_t)HuffTableAC1->Bits[index + 1UL] & 0xFFUL) << 24) |
3034                (((uint32_t)HuffTableAC1->Bits[index] & 0xFFUL) << 16) |
3035                (((uint32_t)HuffTableAC1->Bits[index - 1UL] & 0xFFUL) << 8) |
3036                ((uint32_t)HuffTableAC1->Bits[index - 2UL] & 0xFFUL);
3037     address--;
3038     index -= 4UL;
3039 
3040   }
3041   /* AC1 Huffman Table : Val*/
3042   /* AC1 VALS is a 162 Bytes table i.e 41x32bits words from DHTMEM base address + 62 to DHTMEM + 102 */
3043   /* only Byte 2 and Byte 3 of the first word (@ DHTMEM + 62) belong to AC1 VALS table */
3044   address = (hjpeg->Instance->DHTMEM + 62);
3045   value = *address & 0x0000FFFFUL;
3046   value = value | (((uint32_t)HuffTableAC1->HuffVal[1] & 0xFFUL) << 24) | (((uint32_t)HuffTableAC1->HuffVal[0] & 0xFFUL) <<
3047                                                                          16);
3048   *address = value;
3049 
3050   /*continue setting 160 AC1 huffman values from DHTMEM + 63 to DHTMEM+102 */
3051   address = (hjpeg->Instance->DHTMEM + 102);
3052   index = 160;
3053   while (index > 3UL)
3054   {
3055     *address = (((uint32_t)HuffTableAC1->HuffVal[index + 1UL] & 0xFFUL) << 24) |
3056                (((uint32_t)HuffTableAC1->HuffVal[index] & 0xFFUL) << 16) |
3057                (((uint32_t)HuffTableAC1->HuffVal[index - 1UL] & 0xFFUL) << 8) |
3058                ((uint32_t)HuffTableAC1->HuffVal[index - 2UL] & 0xFFUL);
3059     address--;
3060     index -= 4UL;
3061   }
3062 
3063 }
3064 
3065 /**
3066   * @brief  Configure the JPEG registers with a given quantization table
3067   * @param  hjpeg pointer to a JPEG_HandleTypeDef structure that contains
3068   *         the configuration information for JPEG module
3069   * @param  QTable pointer to an array of 64 bytes giving the quantization table
3070   * @param  QTableAddress destination quantization address in the JPEG peripheral
3071   *         it could be QMEM0, QMEM1, QMEM2 or QMEM3
3072   * @retval 0 if no error, 1 if error
3073   */
3074 static uint32_t JPEG_Set_Quantization_Mem(JPEG_HandleTypeDef *hjpeg, uint8_t *QTable,
3075                                                     __IO uint32_t *QTableAddress)
3076 {
3077   uint32_t i;
3078   uint32_t j;
3079   uint32_t quantRow;
3080   uint32_t quantVal;
3081   uint32_t ScaleFactor;
3082   __IO uint32_t *tableAddress;
3083 
3084   tableAddress = QTableAddress;
3085 
3086   if ((hjpeg->Conf.ImageQuality >= 50UL) && (hjpeg->Conf.ImageQuality <= 100UL))
3087   {
3088     ScaleFactor = 200UL - (hjpeg->Conf.ImageQuality * 2UL);
3089   }
3090   else if (hjpeg->Conf.ImageQuality > 0UL)
3091   {
3092     ScaleFactor = ((uint32_t) 5000) / ((uint32_t) hjpeg->Conf.ImageQuality);
3093   }
3094   else
3095   {
3096     return 1UL;
3097   }
3098 
3099   /*Quantization_table = (Standard_quanization_table * ScaleFactor + 50) / 100*/
3100   i = 0;
3101   while (i < (JPEG_QUANT_TABLE_SIZE - 3UL))
3102   {
3103     quantRow = 0;
3104     for (j = 0; j < 4UL; j++)
3105     {
3106       /* Note that the quantization coefficients must be specified in the table in zigzag order */
3107       quantVal = ((((uint32_t) QTable[JPEG_ZIGZAG_ORDER[i + j]]) * ScaleFactor) + 50UL) / 100UL;
3108 
3109       if (quantVal == 0UL)
3110       {
3111         quantVal = 1UL;
3112       }
3113       else if (quantVal > 255UL)
3114       {
3115         quantVal = 255UL;
3116       }
3117       else
3118       {
3119         /* Nothing to do, keep same value of quantVal */
3120       }
3121 
3122       quantRow |= ((quantVal & 0xFFUL) << (8UL * j));
3123     }
3124 
3125     i += 4UL;
3126     *tableAddress = quantRow;
3127     tableAddress ++;
3128   }
3129 
3130   /* Return function status */
3131   return 0UL;
3132 }
3133 
3134 /**
3135   * @brief  Configure the JPEG registers for YCbCr color space
3136   * @param  hjpeg pointer to a JPEG_HandleTypeDef structure that contains
3137   *         the configuration information for JPEG module
3138   * @retval None
3139   */
3140 static void JPEG_SetColorYCBCR(JPEG_HandleTypeDef *hjpeg)
3141 {
3142   uint32_t ySamplingH;
3143   uint32_t ySamplingV;
3144   uint32_t yblockNb;
3145 
3146   /*Set Number of color components to 3*/
3147   hjpeg->Instance->CONFR1 &=  ~JPEG_CONFR1_NF;
3148   hjpeg->Instance->CONFR1 |=  JPEG_CONFR1_NF_1;
3149 
3150   /* compute MCU block size and Y, Cb ,Cr sampling factors*/
3151   if (hjpeg->Conf.ChromaSubsampling == JPEG_420_SUBSAMPLING)
3152   {
3153     ySamplingH  = JPEG_CONFR4_HSF_1;   /* Hs = 2*/
3154     ySamplingV  = JPEG_CONFR4_VSF_1;   /* Vs = 2*/
3155 
3156     yblockNb  = 0x30; /* 4 blocks of 8x8*/
3157   }
3158   else if (hjpeg->Conf.ChromaSubsampling == JPEG_422_SUBSAMPLING)
3159   {
3160     ySamplingH  = JPEG_CONFR4_HSF_1;   /* Hs = 2*/
3161     ySamplingV  = JPEG_CONFR4_VSF_0;   /* Vs = 1*/
3162 
3163     yblockNb  = 0x10; /* 2 blocks of 8x8*/
3164   }
3165   else /*JPEG_444_SUBSAMPLING and default*/
3166   {
3167     ySamplingH  = JPEG_CONFR4_HSF_0;   /* Hs = 1*/
3168     ySamplingV  = JPEG_CONFR4_VSF_0;   /* Vs = 1*/
3169 
3170     yblockNb  = 0; /* 1 block of 8x8*/
3171   }
3172 
3173   hjpeg->Instance->CONFR1 &= ~(JPEG_CONFR1_NF | JPEG_CONFR1_NS);
3174   hjpeg->Instance->CONFR1 |= (JPEG_CONFR1_NF_1 | JPEG_CONFR1_NS_1);
3175 
3176   /*Reset CONFR4 register*/
3177   hjpeg->Instance->CONFR4 =  0;
3178   /*Set Horizental and Vertical  sampling factor , number of blocks , Quantization table and Huffman AC/DC tables for component 0*/
3179   hjpeg->Instance->CONFR4 |= (ySamplingH | ySamplingV | (yblockNb & JPEG_CONFR4_NB));
3180 
3181   /*Reset CONFR5 register*/
3182   hjpeg->Instance->CONFR5 =  0;
3183   /*Set Horizental and Vertical  sampling factor , number of blocks , Quantization table and Huffman AC/DC tables for component 1*/
3184   hjpeg->Instance->CONFR5 |= (JPEG_CONFR5_HSF_0 | JPEG_CONFR5_VSF_0 | JPEG_CONFR5_QT_0 | JPEG_CONFR5_HA | JPEG_CONFR5_HD);
3185 
3186   /*Reset CONFR6 register*/
3187   hjpeg->Instance->CONFR6 =  0;
3188   /*Set Horizental and Vertical  sampling factor and number of blocks for component 2*/
3189   /* In YCBCR , by default, both chrominance components (component 1 and component 2) use the same Quantization table (table 1) */
3190   /* In YCBCR , both chrominance components (component 1 and component 2) use the same Huffman tables (table 1) */
3191   hjpeg->Instance->CONFR6 |= (JPEG_CONFR6_HSF_0 | JPEG_CONFR6_VSF_0 | JPEG_CONFR6_QT_0 | JPEG_CONFR6_HA | JPEG_CONFR6_HD);
3192 
3193 }
3194 
3195 /**
3196   * @brief  Configure the JPEG registers for GrayScale color space
3197   * @param  hjpeg pointer to a JPEG_HandleTypeDef structure that contains
3198   *         the configuration information for JPEG module
3199   * @retval None
3200   */
3201 static void JPEG_SetColorGrayScale(JPEG_HandleTypeDef *hjpeg)
3202 {
3203   /*Set Number of color components to 1*/
3204   hjpeg->Instance->CONFR1 &= ~(JPEG_CONFR1_NF | JPEG_CONFR1_NS);
3205 
3206   /*in GrayScale use 1 single Quantization table (Table 0)*/
3207   /*in GrayScale use only one couple of AC/DC huffman table (table 0)*/
3208 
3209   /*Reset CONFR4 register*/
3210   hjpeg->Instance->CONFR4 =  0;
3211   /*Set Horizental and Vertical  sampling factor , number of blocks , Quantization table and Huffman AC/DC tables for component 0*/
3212   hjpeg->Instance->CONFR4 |=  JPEG_CONFR4_HSF_0 | JPEG_CONFR4_VSF_0 ;
3213 }
3214 
3215 /**
3216   * @brief  Configure the JPEG registers for CMYK color space
3217   * @param  hjpeg pointer to a JPEG_HandleTypeDef structure that contains
3218   *         the configuration information for JPEG module
3219   * @retval None
3220   */
3221 static void JPEG_SetColorCMYK(JPEG_HandleTypeDef *hjpeg)
3222 {
3223   uint32_t ySamplingH;
3224   uint32_t ySamplingV;
3225   uint32_t yblockNb;
3226 
3227   /*Set Number of color components to 4*/
3228   hjpeg->Instance->CONFR1 |= (JPEG_CONFR1_NF | JPEG_CONFR1_NS);
3229 
3230   /* compute MCU block size and Y, Cb ,Cr sampling factors*/
3231   if (hjpeg->Conf.ChromaSubsampling == JPEG_420_SUBSAMPLING)
3232   {
3233     ySamplingH  = JPEG_CONFR4_HSF_1;   /* Hs = 2*/
3234     ySamplingV  = JPEG_CONFR4_VSF_1;   /* Vs = 2*/
3235 
3236     yblockNb  = 0x30; /* 4 blocks of 8x8*/
3237   }
3238   else if (hjpeg->Conf.ChromaSubsampling == JPEG_422_SUBSAMPLING)
3239   {
3240     ySamplingH  = JPEG_CONFR4_HSF_1;   /* Hs = 2*/
3241     ySamplingV  = JPEG_CONFR4_VSF_0;   /* Vs = 1*/
3242 
3243     yblockNb  = 0x10; /* 2 blocks of 8x8*/
3244   }
3245   else /*JPEG_444_SUBSAMPLING and default*/
3246   {
3247     ySamplingH  = JPEG_CONFR4_HSF_0;   /* Hs = 1*/
3248     ySamplingV  = JPEG_CONFR4_VSF_0;   /* Vs = 1*/
3249 
3250     yblockNb  = 0; /* 1 block of 8x8*/
3251   }
3252 
3253   /*Reset CONFR4 register*/
3254   hjpeg->Instance->CONFR4 =  0;
3255   /*Set Horizental and Vertical  sampling factor , number of blocks , Quantization table and Huffman AC/DC tables for component 0*/
3256   hjpeg->Instance->CONFR4 |= (ySamplingH | ySamplingV | (yblockNb & JPEG_CONFR4_NB));
3257 
3258   /*Reset CONFR5 register*/
3259   hjpeg->Instance->CONFR5 =  0;
3260   /*Set Horizental and Vertical  sampling factor , number of blocks , Quantization table and Huffman AC/DC tables for component 1*/
3261   hjpeg->Instance->CONFR5 |= (JPEG_CONFR5_HSF_0 | JPEG_CONFR5_VSF_0);
3262 
3263   /*Reset CONFR6 register*/
3264   hjpeg->Instance->CONFR6 =  0;
3265   /*Set Horizental and Vertical  sampling factor , number of blocks , Quantization table and Huffman AC/DC tables for component 2*/
3266   hjpeg->Instance->CONFR6 |= (JPEG_CONFR6_HSF_0 | JPEG_CONFR6_VSF_0);
3267 
3268   /*Reset CONFR7 register*/
3269   hjpeg->Instance->CONFR7 =  0;
3270   /*Set Horizental and Vertical  sampling factor , number of blocks , Quantization table and Huffman AC/DC tables for component 3*/
3271   hjpeg->Instance->CONFR7 |= (JPEG_CONFR7_HSF_0 | JPEG_CONFR7_VSF_0);
3272 }
3273 
3274 /**
3275   * @brief  Init the JPEG encoding/decoding process in case of Polling or Interrupt and DMA
3276   * @param  hjpeg pointer to a JPEG_HandleTypeDef structure that contains
3277   *         the configuration information for JPEG module
3278   * @retval None
3279   */
3280 static void JPEG_Init_Process(JPEG_HandleTypeDef *hjpeg)
3281 {
3282   /*Reset pause*/
3283   hjpeg->Context &= (~(JPEG_CONTEXT_PAUSE_INPUT | JPEG_CONTEXT_PAUSE_OUTPUT));
3284 
3285   if ((hjpeg->Context & JPEG_CONTEXT_OPERATION_MASK) == JPEG_CONTEXT_DECODE)
3286   {
3287     /*Set JPEG Codec to Decoding mode */
3288     hjpeg->Instance->CONFR1 |= JPEG_CONFR1_DE;
3289   }
3290   else /* JPEG_CONTEXT_ENCODE */
3291   {
3292     /*Set JPEG Codec to Encoding mode */
3293     hjpeg->Instance->CONFR1 &= ~JPEG_CONFR1_DE;
3294   }
3295 
3296   /*Stop JPEG processing */
3297   hjpeg->Instance->CONFR0 &=  ~JPEG_CONFR0_START;
3298 
3299   /* Disable All Interrupts */
3300   __HAL_JPEG_DISABLE_IT(hjpeg, JPEG_INTERRUPT_MASK);
3301 
3302   /* Flush input and output FIFOs*/
3303   hjpeg->Instance->CR |= JPEG_CR_IFF;
3304   hjpeg->Instance->CR |= JPEG_CR_OFF;
3305 
3306   /* Clear all flags */
3307   __HAL_JPEG_CLEAR_FLAG(hjpeg, JPEG_FLAG_ALL);
3308 
3309   /*Start Encoding/Decoding*/
3310   hjpeg->Instance->CONFR0 |=  JPEG_CONFR0_START;
3311 
3312   if ((hjpeg->Context & JPEG_CONTEXT_METHOD_MASK) == JPEG_CONTEXT_IT)
3313   {
3314     /*Enable IN/OUT, end of Conversation, and end of header parsing interruptions*/
3315     __HAL_JPEG_ENABLE_IT(hjpeg, JPEG_IT_IFT | JPEG_IT_IFNF | JPEG_IT_OFT | JPEG_IT_OFNE | JPEG_IT_EOC | JPEG_IT_HPD);
3316   }
3317   else if ((hjpeg->Context & JPEG_CONTEXT_METHOD_MASK) == JPEG_CONTEXT_DMA)
3318   {
3319     /*Enable End Of Conversation, and End Of Header parsing interruptions*/
3320     __HAL_JPEG_ENABLE_IT(hjpeg, JPEG_IT_EOC | JPEG_IT_HPD);
3321 
3322   }
3323   else
3324   {
3325     /* Nothing to do */
3326   }
3327 }
3328 
3329 /**
3330   * @brief  JPEG encoding/decoding process in case of Polling or Interrupt
3331   * @param  hjpeg pointer to a JPEG_HandleTypeDef structure that contains
3332   *         the configuration information for JPEG module
3333   * @retval JPEG_PROCESS_DONE if the process has ends else JPEG_PROCESS_ONGOING
3334   */
3335 static uint32_t JPEG_Process(JPEG_HandleTypeDef *hjpeg)
3336 {
3337   uint32_t tmpContext;
3338 
3339   /*End of header processing flag */
3340   if ((hjpeg->Context & JPEG_CONTEXT_OPERATION_MASK) == JPEG_CONTEXT_DECODE)
3341   {
3342     if (__HAL_JPEG_GET_FLAG(hjpeg, JPEG_FLAG_HPDF) != 0UL)
3343     {
3344       /*Call Header parsing complete callback */
3345       (void) HAL_JPEG_GetInfo(hjpeg, &hjpeg->Conf);
3346       /* Reset the ImageQuality */
3347       hjpeg->Conf.ImageQuality = 0;
3348       /* Note : the image quality is only available at the end of the decoding operation */
3349       /* at the current stage the calculated image quality is not correct so reset it */
3350 
3351       /*Call Info Ready callback */
3352 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
3353       hjpeg->InfoReadyCallback(hjpeg, &hjpeg->Conf);
3354 #else
3355       HAL_JPEG_InfoReadyCallback(hjpeg, &hjpeg->Conf);
3356 #endif /* USE_HAL_JPEG_REGISTER_CALLBACKS */
3357 
3358       __HAL_JPEG_DISABLE_IT(hjpeg, JPEG_IT_HPD);
3359 
3360       /* Clear header processing done flag */
3361       __HAL_JPEG_CLEAR_FLAG(hjpeg, JPEG_FLAG_HPDF);
3362     }
3363   }
3364 
3365   /*Input FIFO status handling*/
3366   if ((hjpeg->Context &  JPEG_CONTEXT_PAUSE_INPUT) == 0UL)
3367   {
3368     if (__HAL_JPEG_GET_FLAG(hjpeg, JPEG_FLAG_IFTF) != 0UL)
3369     {
3370       /*Input FIFO threshold flag */
3371       /*JPEG_FIFO_TH_SIZE words can be written in */
3372       JPEG_ReadInputData(hjpeg, JPEG_FIFO_TH_SIZE);
3373     }
3374     else if (__HAL_JPEG_GET_FLAG(hjpeg, JPEG_FLAG_IFNFF) != 0UL)
3375     {
3376       /*Input FIFO Not Full flag */
3377       /*32-bit value can be written in */
3378       JPEG_ReadInputData(hjpeg, 1);
3379     }
3380     else
3381     {
3382       /* Nothing to do */
3383     }
3384   }
3385 
3386 
3387   /*Output FIFO flag handling*/
3388   if ((hjpeg->Context &  JPEG_CONTEXT_PAUSE_OUTPUT) == 0UL)
3389   {
3390     if (__HAL_JPEG_GET_FLAG(hjpeg, JPEG_FLAG_OFTF) != 0UL)
3391     {
3392       /*Output FIFO threshold flag */
3393       /*JPEG_FIFO_TH_SIZE words can be read out */
3394       JPEG_StoreOutputData(hjpeg, JPEG_FIFO_TH_SIZE);
3395     }
3396     else if (__HAL_JPEG_GET_FLAG(hjpeg, JPEG_FLAG_OFNEF) != 0UL)
3397     {
3398       /*Output FIFO Not Empty flag */
3399       /*32-bit value can be read out */
3400       JPEG_StoreOutputData(hjpeg, 1);
3401     }
3402     else
3403     {
3404       /* Nothing to do */
3405     }
3406   }
3407 
3408   /*End of Conversion handling :i.e EOC flag is high and OFTF low and OFNEF low*/
3409   if (__HAL_JPEG_GET_FLAG(hjpeg, JPEG_FLAG_EOCF | JPEG_FLAG_OFTF | JPEG_FLAG_OFNEF) == JPEG_FLAG_EOCF)
3410   {
3411     /*Stop Encoding/Decoding*/
3412     hjpeg->Instance->CONFR0 &=  ~JPEG_CONFR0_START;
3413 
3414     if ((hjpeg->Context & JPEG_CONTEXT_METHOD_MASK) == JPEG_CONTEXT_IT)
3415     {
3416       /* Disable All Interrupts */
3417       __HAL_JPEG_DISABLE_IT(hjpeg, JPEG_INTERRUPT_MASK);
3418     }
3419 
3420     /* Clear all flags */
3421     __HAL_JPEG_CLEAR_FLAG(hjpeg, JPEG_FLAG_ALL);
3422 
3423     /*Call End of conversion callback */
3424     if (hjpeg->JpegOutCount > 0UL)
3425     {
3426       /*Output Buffer is not empty, call DecodedDataReadyCallback*/
3427 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
3428       hjpeg->DataReadyCallback(hjpeg, hjpeg->pJpegOutBuffPtr, hjpeg->JpegOutCount);
3429 #else
3430       HAL_JPEG_DataReadyCallback(hjpeg, hjpeg->pJpegOutBuffPtr, hjpeg->JpegOutCount);
3431 #endif /* USE_HAL_JPEG_REGISTER_CALLBACKS */
3432 
3433       hjpeg->JpegOutCount = 0;
3434     }
3435 
3436     /*Reset Context Operation*/
3437     tmpContext = hjpeg->Context;
3438     /*Clear all context fields execpt JPEG_CONTEXT_CONF_ENCODING and JPEG_CONTEXT_CUSTOM_TABLES*/
3439     hjpeg->Context &= (JPEG_CONTEXT_CONF_ENCODING | JPEG_CONTEXT_CUSTOM_TABLES);
3440 
3441     /* Process Unlocked */
3442     __HAL_UNLOCK(hjpeg);
3443 
3444     /* Change the JPEG state */
3445     hjpeg->State = HAL_JPEG_STATE_READY;
3446 
3447     /*Call End of Encoding/Decoding callback */
3448     if ((tmpContext & JPEG_CONTEXT_OPERATION_MASK) == JPEG_CONTEXT_DECODE)
3449     {
3450 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
3451       hjpeg->DecodeCpltCallback(hjpeg);
3452 #else
3453       HAL_JPEG_DecodeCpltCallback(hjpeg);
3454 #endif /*USE_HAL_JPEG_REGISTER_CALLBACKS*/
3455     }
3456     else /* JPEG_CONTEXT_ENCODE */
3457     {
3458 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
3459       hjpeg->EncodeCpltCallback(hjpeg);
3460 #else
3461       HAL_JPEG_EncodeCpltCallback(hjpeg);
3462 #endif
3463     }
3464 
3465     return JPEG_PROCESS_DONE;
3466   }
3467 
3468 
3469   return JPEG_PROCESS_ONGOING;
3470 }
3471 
3472 /**
3473   * @brief  Store some output data from the JPEG peripheral to the output buffer.
3474   *         This function is used when the JPEG peripheral has new data to output
3475   *         in case of Polling or Interrupt process
3476   * @param  hjpeg pointer to a JPEG_HandleTypeDef structure that contains
3477   *         the configuration information for JPEG module
3478   * @param  nbOutputWords Number of output words (of 32 bits) ready from the JPEG peripheral
3479   * @retval None
3480   */
3481 static void JPEG_StoreOutputData(JPEG_HandleTypeDef *hjpeg, uint32_t nbOutputWords)
3482 {
3483   uint32_t index;
3484   uint32_t nb_words;
3485   uint32_t nb_bytes;
3486   uint32_t dataword;
3487 
3488   if (hjpeg->OutDataLength >= (hjpeg->JpegOutCount + (nbOutputWords * 4UL)))
3489   {
3490     for (index = 0; index < nbOutputWords; index++)
3491     {
3492       /*Transfer 32 bits from the JPEG output FIFO*/
3493       dataword = hjpeg->Instance->DOR;
3494       hjpeg->pJpegOutBuffPtr[hjpeg->JpegOutCount] = (uint8_t)(dataword & 0x000000FFUL);
3495       hjpeg->pJpegOutBuffPtr[hjpeg->JpegOutCount + 1UL] = (uint8_t)((dataword & 0x0000FF00UL) >> 8);
3496       hjpeg->pJpegOutBuffPtr[hjpeg->JpegOutCount + 2UL] = (uint8_t)((dataword & 0x00FF0000UL) >> 16);
3497       hjpeg->pJpegOutBuffPtr[hjpeg->JpegOutCount + 3UL] = (uint8_t)((dataword & 0xFF000000UL) >> 24);
3498       hjpeg->JpegOutCount += 4UL;
3499     }
3500     if (hjpeg->OutDataLength == hjpeg->JpegOutCount)
3501     {
3502       /*Output Buffer is full, call DecodedDataReadyCallback*/
3503 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
3504       hjpeg->DataReadyCallback(hjpeg, hjpeg->pJpegOutBuffPtr, hjpeg->JpegOutCount);
3505 #else
3506       HAL_JPEG_DataReadyCallback(hjpeg, hjpeg->pJpegOutBuffPtr, hjpeg->JpegOutCount);
3507 #endif /*USE_HAL_JPEG_REGISTER_CALLBACKS*/
3508       hjpeg->JpegOutCount = 0;
3509     }
3510   }
3511   else if (hjpeg->OutDataLength > hjpeg->JpegOutCount)
3512   {
3513     nb_words = (hjpeg->OutDataLength - hjpeg->JpegOutCount) / 4UL;
3514     for (index = 0; index < nb_words; index++)
3515     {
3516       /*Transfer 32 bits from the JPEG output FIFO*/
3517       dataword = hjpeg->Instance->DOR;
3518       hjpeg->pJpegOutBuffPtr[hjpeg->JpegOutCount] = (uint8_t)(dataword & 0x000000FFUL);
3519       hjpeg->pJpegOutBuffPtr[hjpeg->JpegOutCount + 1UL] = (uint8_t)((dataword & 0x0000FF00UL) >> 8);
3520       hjpeg->pJpegOutBuffPtr[hjpeg->JpegOutCount + 2UL] = (uint8_t)((dataword & 0x00FF0000UL) >> 16);
3521       hjpeg->pJpegOutBuffPtr[hjpeg->JpegOutCount + 3UL] = (uint8_t)((dataword & 0xFF000000UL) >> 24);
3522       hjpeg->JpegOutCount += 4UL;
3523     }
3524     if (hjpeg->OutDataLength == hjpeg->JpegOutCount)
3525     {
3526       /*Output Buffer is full, call DecodedDataReadyCallback*/
3527 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
3528       hjpeg->DataReadyCallback(hjpeg, hjpeg->pJpegOutBuffPtr, hjpeg->JpegOutCount);
3529 #else
3530       HAL_JPEG_DataReadyCallback(hjpeg, hjpeg->pJpegOutBuffPtr, hjpeg->JpegOutCount);
3531 #endif /* USE_HAL_JPEG_REGISTER_CALLBACKS */
3532       hjpeg->JpegOutCount = 0;
3533     }
3534     else
3535     {
3536       nb_bytes = hjpeg->OutDataLength - hjpeg->JpegOutCount;
3537       dataword = hjpeg->Instance->DOR;
3538       for (index = 0; index < nb_bytes; index++)
3539       {
3540         hjpeg->pJpegOutBuffPtr[hjpeg->JpegOutCount] = (uint8_t)((dataword >> (8UL * (index & 0x3UL))) & 0xFFUL);
3541         hjpeg->JpegOutCount++;
3542       }
3543       /*Output Buffer is full, call DecodedDataReadyCallback*/
3544 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
3545       hjpeg->DataReadyCallback(hjpeg, hjpeg->pJpegOutBuffPtr, hjpeg->JpegOutCount);
3546 #else
3547       HAL_JPEG_DataReadyCallback(hjpeg, hjpeg->pJpegOutBuffPtr, hjpeg->JpegOutCount);
3548 #endif /* USE_HAL_JPEG_REGISTER_CALLBACKS */
3549 
3550       hjpeg->JpegOutCount = 0;
3551 
3552       nb_bytes = 4UL - nb_bytes;
3553       for (index = nb_bytes; index < 4UL; index++)
3554       {
3555         hjpeg->pJpegOutBuffPtr[hjpeg->JpegOutCount] = (uint8_t)((dataword >> (8UL * index)) & 0xFFUL);
3556         hjpeg->JpegOutCount++;
3557       }
3558     }
3559   }
3560   else
3561   {
3562     /* Nothing to do */
3563   }
3564 }
3565 
3566 /**
3567   * @brief  Read some input Data from the input buffer.
3568   *         This function is used when the JPEG peripheral needs new data
3569   *         in case of Polling or Interrupt process
3570   * @param  hjpeg pointer to a JPEG_HandleTypeDef structure that contains
3571   *         the configuration information for JPEG module
3572   * @param  nbRequestWords Number of input words (of 32 bits) that the JPE peripheral request
3573   * @retval None
3574   */
3575 static void JPEG_ReadInputData(JPEG_HandleTypeDef *hjpeg, uint32_t nbRequestWords)
3576 {
3577   uint32_t nb_bytes = 0;
3578   uint32_t nb_words;
3579   uint32_t index;
3580   uint32_t dataword;
3581   uint32_t input_count;
3582 
3583   if ((hjpeg->InDataLength == 0UL) || (nbRequestWords == 0UL))
3584   {
3585     /* No more Input data : nothing to do*/
3586     (void) HAL_JPEG_Pause(hjpeg, JPEG_PAUSE_RESUME_INPUT);
3587   }
3588   else if (hjpeg->InDataLength > hjpeg->JpegInCount)
3589   {
3590     nb_bytes = hjpeg->InDataLength - hjpeg->JpegInCount;
3591   }
3592   else if (hjpeg->InDataLength == hjpeg->JpegInCount)
3593   {
3594     /*Call HAL_JPEG_GetDataCallback to get new data */
3595 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
3596     hjpeg->GetDataCallback(hjpeg, hjpeg->JpegInCount);
3597 #else
3598     HAL_JPEG_GetDataCallback(hjpeg, hjpeg->JpegInCount);
3599 #endif /*USE_HAL_JPEG_REGISTER_CALLBACKS*/
3600 
3601     if (hjpeg->InDataLength > 4UL)
3602     {
3603       hjpeg->InDataLength = hjpeg->InDataLength - (hjpeg->InDataLength % 4UL);
3604     }
3605     hjpeg->JpegInCount = 0;
3606     nb_bytes = hjpeg->InDataLength;
3607   }
3608   else
3609   {
3610     /* Nothing to do */
3611   }
3612   if (((hjpeg->Context &  JPEG_CONTEXT_PAUSE_INPUT) == 0UL) && (nb_bytes > 0UL))
3613   {
3614     nb_words = nb_bytes / 4UL;
3615     if (nb_words >= nbRequestWords)
3616     {
3617       for (index = 0; index < nbRequestWords; index++)
3618       {
3619         input_count = hjpeg->JpegInCount;
3620         hjpeg->Instance->DIR = (((uint32_t)(hjpeg->pJpegInBuffPtr[input_count])) | \
3621                                 (((uint32_t)(hjpeg->pJpegInBuffPtr[input_count + 1UL])) << 8) | \
3622                                 (((uint32_t)(hjpeg->pJpegInBuffPtr[input_count + 2UL])) << 16) | \
3623                                 (((uint32_t)(hjpeg->pJpegInBuffPtr[input_count + 3UL])) << 24));
3624 
3625         hjpeg->JpegInCount += 4UL;
3626       }
3627     }
3628     else /*nb_words < nbRequestWords*/
3629     {
3630       if (nb_words > 0UL)
3631       {
3632         for (index = 0; index < nb_words; index++)
3633         {
3634           input_count = hjpeg->JpegInCount;
3635           hjpeg->Instance->DIR = (((uint32_t)(hjpeg->pJpegInBuffPtr[input_count])) | \
3636                                   (((uint32_t)(hjpeg->pJpegInBuffPtr[input_count + 1UL])) << 8) | \
3637                                   (((uint32_t)(hjpeg->pJpegInBuffPtr[input_count + 2UL])) << 16) | \
3638                                   (((uint32_t)(hjpeg->pJpegInBuffPtr[input_count + 3UL])) << 24));
3639 
3640           hjpeg->JpegInCount += 4UL;
3641         }
3642       }
3643       else
3644       {
3645         /* end of file*/
3646         dataword = 0;
3647         for (index = 0; index < nb_bytes; index++)
3648         {
3649           dataword |= (uint32_t)hjpeg->pJpegInBuffPtr[hjpeg->JpegInCount] << (8UL * (index & 0x03UL));
3650           hjpeg->JpegInCount++;
3651         }
3652         hjpeg->Instance->DIR = dataword;
3653       }
3654     }
3655   }
3656 }
3657 
3658 /**
3659   * @brief  Start the JPEG DMA process (encoding/decoding)
3660   * @param  hjpeg pointer to a JPEG_HandleTypeDef structure that contains
3661   *         the configuration information for JPEG module
3662   * @retval JPEG_PROCESS_DONE if process ends else JPEG_PROCESS_ONGOING
3663   */
3664 static HAL_StatusTypeDef JPEG_DMA_StartProcess(JPEG_HandleTypeDef *hjpeg)
3665 {
3666   uint32_t inXfrSize, outXfrSize;
3667 
3668   /*if the MDMA In is triggred with JPEG In FIFO Threshold flag
3669       then MDMA In buffer size is 32 bytes
3670     else (MDMA In is triggred with JPEG In FIFO not full flag)
3671       then MDMA In buffer size is 4 bytes
3672     */
3673   inXfrSize = hjpeg->hdmain->Init.BufferTransferLength;
3674 
3675   /*if the MDMA Out is triggred with JPEG Out FIFO Threshold flag
3676       then MDMA out buffer size is 32 bytes
3677     else (MDMA Out is triggred with JPEG Out FIFO not empty flag)
3678       then MDMA buffer size is 4 bytes
3679     */
3680   outXfrSize = hjpeg->hdmaout->Init.BufferTransferLength;
3681 
3682   if ((hjpeg->InDataLength < inXfrSize) || (hjpeg->OutDataLength < outXfrSize))
3683   {
3684     return HAL_ERROR;
3685   }
3686   /* Set the JPEG MDMA In transfer complete callback */
3687   hjpeg->hdmain->XferCpltCallback = JPEG_MDMAInCpltCallback;
3688   /* Set the MDMA In error callback */
3689   hjpeg->hdmain->XferErrorCallback = JPEG_MDMAErrorCallback;
3690 
3691   /* Set the JPEG MDMA Out transfer complete callback */
3692   hjpeg->hdmaout->XferCpltCallback = JPEG_MDMAOutCpltCallback;
3693   /* Set the MDMA In error callback */
3694   hjpeg->hdmaout->XferErrorCallback = JPEG_MDMAErrorCallback;
3695   /* Set the MDMA Out Abort callback */
3696   hjpeg->hdmaout->XferAbortCallback = JPEG_MDMAOutAbortCallback;
3697 
3698   if ((inXfrSize == 0UL) || (outXfrSize == 0UL))
3699   {
3700     hjpeg->ErrorCode |= HAL_JPEG_ERROR_DMA;
3701     return HAL_ERROR;
3702   }
3703   /*MDMA transfer size (BNDTR) must be a multiple of MDMA buffer size (TLEN)*/
3704   hjpeg->InDataLength = hjpeg->InDataLength - (hjpeg->InDataLength % inXfrSize);
3705 
3706   /*MDMA transfer size (BNDTR) must be a multiple of MDMA buffer size (TLEN)*/
3707   hjpeg->OutDataLength = hjpeg->OutDataLength - (hjpeg->OutDataLength % outXfrSize);
3708 
3709 
3710   /* Start MDMA FIFO Out transfer */
3711   if (HAL_MDMA_Start_IT(hjpeg->hdmaout, (uint32_t)&hjpeg->Instance->DOR, (uint32_t)hjpeg->pJpegOutBuffPtr,
3712                         hjpeg->OutDataLength, 1) != HAL_OK)
3713   {
3714     hjpeg->ErrorCode |= HAL_JPEG_ERROR_DMA;
3715     return HAL_ERROR;
3716   }
3717   /* Start DMA FIFO In transfer */
3718   if (HAL_MDMA_Start_IT(hjpeg->hdmain, (uint32_t)hjpeg->pJpegInBuffPtr, (uint32_t)&hjpeg->Instance->DIR,
3719                         hjpeg->InDataLength, 1) != HAL_OK)
3720   {
3721     hjpeg->ErrorCode |= HAL_JPEG_ERROR_DMA;
3722     return HAL_ERROR;
3723   }
3724 
3725   return HAL_OK;
3726 }
3727 
3728 /**
3729   * @brief  Continue the current JPEG DMA process (encoding/decoding)
3730   * @param  hjpeg pointer to a JPEG_HandleTypeDef structure that contains
3731   *         the configuration information for JPEG module
3732   * @retval JPEG_PROCESS_DONE if process ends else JPEG_PROCESS_ONGOING
3733   */
3734 static void JPEG_DMA_ContinueProcess(JPEG_HandleTypeDef *hjpeg)
3735 {
3736   /*End of header processing flag rises*/
3737   if ((hjpeg->Context & JPEG_CONTEXT_OPERATION_MASK) == JPEG_CONTEXT_DECODE)
3738   {
3739     if (__HAL_JPEG_GET_FLAG(hjpeg, JPEG_FLAG_HPDF) != 0UL)
3740     {
3741       /*Call Header parsing complete callback */
3742       (void) HAL_JPEG_GetInfo(hjpeg, &hjpeg->Conf);
3743 
3744       /* Reset the ImageQuality */
3745       hjpeg->Conf.ImageQuality = 0;
3746       /* Note : the image quality is only available at the end of the decoding operation */
3747       /* at the current stage the calculated image quality is not correct so reset it */
3748 
3749       /*Call Info Ready callback */
3750 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
3751       hjpeg->InfoReadyCallback(hjpeg, &hjpeg->Conf);
3752 #else
3753       HAL_JPEG_InfoReadyCallback(hjpeg, &hjpeg->Conf);
3754 #endif /* USE_HAL_JPEG_REGISTER_CALLBACKS */
3755 
3756       __HAL_JPEG_DISABLE_IT(hjpeg, JPEG_IT_HPD);
3757 
3758       /* Clear header processing done flag */
3759       __HAL_JPEG_CLEAR_FLAG(hjpeg, JPEG_FLAG_HPDF);
3760     }
3761   }
3762 
3763   /*End of Conversion handling*/
3764   if (__HAL_JPEG_GET_FLAG(hjpeg, JPEG_FLAG_EOCF) != 0UL)
3765   {
3766 
3767     hjpeg->Context |= JPEG_CONTEXT_ENDING_DMA;
3768 
3769     /*Stop Encoding/Decoding*/
3770     hjpeg->Instance->CONFR0 &=  ~JPEG_CONFR0_START;
3771 
3772     __HAL_JPEG_DISABLE_IT(hjpeg, JPEG_INTERRUPT_MASK);
3773 
3774     /* Clear all flags */
3775     __HAL_JPEG_CLEAR_FLAG(hjpeg, JPEG_FLAG_ALL);
3776 
3777     if (hjpeg->hdmain->State == HAL_MDMA_STATE_BUSY)
3778     {
3779       /* Stop the MDMA In Xfer*/
3780       (void) HAL_MDMA_Abort_IT(hjpeg->hdmain);
3781     }
3782 
3783     if (hjpeg->hdmaout->State == HAL_MDMA_STATE_BUSY)
3784     {
3785       /* Stop the MDMA out Xfer*/
3786       (void) HAL_MDMA_Abort_IT(hjpeg->hdmaout);
3787     }
3788     else
3789     {
3790       JPEG_DMA_EndProcess(hjpeg);
3791     }
3792   }
3793 
3794 
3795 }
3796 
3797 /**
3798   * @brief  Finalize the current JPEG DMA process (encoding/decoding)
3799   * @param  hjpeg pointer to a JPEG_HandleTypeDef structure that contains
3800   *         the configuration information for JPEG module
3801   * @retval JPEG_PROCESS_DONE
3802   */
3803 static void JPEG_DMA_EndProcess(JPEG_HandleTypeDef *hjpeg)
3804 {
3805   uint32_t tmpContext;
3806   hjpeg->JpegOutCount = hjpeg->OutDataLength - (hjpeg->hdmaout->Instance->CBNDTR & MDMA_CBNDTR_BNDT);
3807 
3808   /*if Output Buffer is full, call HAL_JPEG_DataReadyCallback*/
3809   if (hjpeg->JpegOutCount == hjpeg->OutDataLength)
3810   {
3811 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
3812     hjpeg->DataReadyCallback(hjpeg, hjpeg->pJpegOutBuffPtr, hjpeg->JpegOutCount);
3813 #else
3814     HAL_JPEG_DataReadyCallback(hjpeg, hjpeg->pJpegOutBuffPtr, hjpeg->JpegOutCount);
3815 #endif /* USE_HAL_JPEG_REGISTER_CALLBACKS */
3816 
3817     hjpeg->JpegOutCount = 0;
3818   }
3819 
3820   /*Check if remaining data in the output FIFO*/
3821   if (__HAL_JPEG_GET_FLAG(hjpeg, JPEG_FLAG_OFNEF) == 0UL)
3822   {
3823     if (hjpeg->JpegOutCount > 0UL)
3824     {
3825       /*Output Buffer is not empty, call DecodedDataReadyCallback*/
3826 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
3827       hjpeg->DataReadyCallback(hjpeg, hjpeg->pJpegOutBuffPtr, hjpeg->JpegOutCount);
3828 #else
3829       HAL_JPEG_DataReadyCallback(hjpeg, hjpeg->pJpegOutBuffPtr, hjpeg->JpegOutCount);
3830 #endif /* USE_HAL_JPEG_REGISTER_CALLBACKS */
3831 
3832       hjpeg->JpegOutCount = 0;
3833     }
3834 
3835     /*Stop Encoding/Decoding*/
3836     hjpeg->Instance->CONFR0 &=  ~JPEG_CONFR0_START;
3837 
3838     tmpContext = hjpeg->Context;
3839     /*Clear all context fields execpt JPEG_CONTEXT_CONF_ENCODING and JPEG_CONTEXT_CUSTOM_TABLES*/
3840     hjpeg->Context &= (JPEG_CONTEXT_CONF_ENCODING | JPEG_CONTEXT_CUSTOM_TABLES);
3841 
3842     /* Process Unlocked */
3843     __HAL_UNLOCK(hjpeg);
3844 
3845     /* Change the JPEG state */
3846     hjpeg->State = HAL_JPEG_STATE_READY;
3847 
3848     /*Call End of Encoding/Decoding callback */
3849     if ((tmpContext & JPEG_CONTEXT_OPERATION_MASK) == JPEG_CONTEXT_DECODE)
3850     {
3851 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
3852       hjpeg->DecodeCpltCallback(hjpeg);
3853 #else
3854       HAL_JPEG_DecodeCpltCallback(hjpeg);
3855 #endif /* USE_HAL_JPEG_REGISTER_CALLBACKS */
3856     }
3857     else /* JPEG_CONTEXT_ENCODE */
3858     {
3859 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
3860       hjpeg->EncodeCpltCallback(hjpeg);
3861 #else
3862       HAL_JPEG_EncodeCpltCallback(hjpeg);
3863 #endif /* USE_HAL_JPEG_REGISTER_CALLBACKS */
3864     }
3865   }
3866   else if ((hjpeg->Context &  JPEG_CONTEXT_PAUSE_OUTPUT) == 0UL)
3867   {
3868     JPEG_DMA_PollResidualData(hjpeg);
3869   }
3870   else
3871   {
3872     /* Nothing to do */
3873   }
3874 
3875 }
3876 
3877 /**
3878   * @brief  Poll residual output data when DMA process (encoding/decoding)
3879   * @param  hjpeg pointer to a JPEG_HandleTypeDef structure that contains
3880   *         the configuration information for JPEG module
3881   * @retval None.
3882   */
3883 static void JPEG_DMA_PollResidualData(JPEG_HandleTypeDef *hjpeg)
3884 {
3885   uint32_t tmpContext;
3886   uint32_t count;
3887   uint32_t dataOut;
3888 
3889   for (count = JPEG_FIFO_SIZE; count > 0UL; count--)
3890   {
3891     if ((hjpeg->Context &  JPEG_CONTEXT_PAUSE_OUTPUT) == 0UL)
3892     {
3893       if (__HAL_JPEG_GET_FLAG(hjpeg, JPEG_FLAG_OFNEF) != 0UL)
3894       {
3895         dataOut = hjpeg->Instance->DOR;
3896         hjpeg->pJpegOutBuffPtr[hjpeg->JpegOutCount] = (uint8_t)(dataOut & 0x000000FFUL);
3897         hjpeg->pJpegOutBuffPtr[hjpeg->JpegOutCount + 1UL] = (uint8_t)((dataOut & 0x0000FF00UL) >> 8);
3898         hjpeg->pJpegOutBuffPtr[hjpeg->JpegOutCount + 2UL] = (uint8_t)((dataOut & 0x00FF0000UL) >> 16);
3899         hjpeg->pJpegOutBuffPtr[hjpeg->JpegOutCount + 3UL] = (uint8_t)((dataOut & 0xFF000000UL) >> 24);
3900         hjpeg->JpegOutCount += 4UL;
3901 
3902         if (hjpeg->JpegOutCount == hjpeg->OutDataLength)
3903         {
3904           /*Output Buffer is full, call HAL_JPEG_DataReadyCallback*/
3905 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
3906           hjpeg->DataReadyCallback(hjpeg, hjpeg->pJpegOutBuffPtr, hjpeg->JpegOutCount);
3907 #else
3908           HAL_JPEG_DataReadyCallback(hjpeg, hjpeg->pJpegOutBuffPtr, hjpeg->JpegOutCount);
3909 #endif /* USE_HAL_JPEG_REGISTER_CALLBACKS */
3910 
3911           hjpeg->JpegOutCount = 0;
3912         }
3913 
3914       }
3915     }
3916   }
3917 
3918   tmpContext = hjpeg->Context;
3919 
3920   if ((__HAL_JPEG_GET_FLAG(hjpeg, JPEG_FLAG_OFNEF) == 0UL) || ((tmpContext & JPEG_CONTEXT_PAUSE_OUTPUT) == 0UL))
3921   {
3922     /*Stop Encoding/Decoding*/
3923     hjpeg->Instance->CONFR0 &=  ~JPEG_CONFR0_START;
3924 
3925     if (hjpeg->JpegOutCount > 0UL)
3926     {
3927       /*Output Buffer is not empty, call DecodedDataReadyCallback*/
3928 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
3929       hjpeg->DataReadyCallback(hjpeg, hjpeg->pJpegOutBuffPtr, hjpeg->JpegOutCount);
3930 #else
3931       HAL_JPEG_DataReadyCallback(hjpeg, hjpeg->pJpegOutBuffPtr, hjpeg->JpegOutCount);
3932 #endif /* USE_HAL_JPEG_REGISTER_CALLBACKS */
3933 
3934       hjpeg->JpegOutCount = 0;
3935     }
3936 
3937     tmpContext = hjpeg->Context;
3938     /*Clear all context fields execpt JPEG_CONTEXT_CONF_ENCODING and JPEG_CONTEXT_CUSTOM_TABLES*/
3939     hjpeg->Context &= (JPEG_CONTEXT_CONF_ENCODING | JPEG_CONTEXT_CUSTOM_TABLES);
3940 
3941     /* Process Unlocked */
3942     __HAL_UNLOCK(hjpeg);
3943 
3944     /* Change the JPEG state */
3945     hjpeg->State = HAL_JPEG_STATE_READY;
3946 
3947     /*Call End of Encoding/Decoding callback */
3948     if ((tmpContext & JPEG_CONTEXT_OPERATION_MASK) == JPEG_CONTEXT_DECODE)
3949     {
3950 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
3951       hjpeg->DecodeCpltCallback(hjpeg);
3952 #else
3953       HAL_JPEG_DecodeCpltCallback(hjpeg);
3954 #endif /* USE_HAL_JPEG_REGISTER_CALLBACKS */
3955     }
3956     else /* JPEG_CONTEXT_ENCODE */
3957     {
3958 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
3959       hjpeg->EncodeCpltCallback(hjpeg);
3960 #else
3961       HAL_JPEG_EncodeCpltCallback(hjpeg);
3962 #endif /* USE_HAL_JPEG_REGISTER_CALLBACKS */
3963     }
3964   }
3965 }
3966 
3967 /**
3968   * @brief  MDMA input transfer complete callback
3969   * @param  hmdma pointer to a MDMA_HandleTypeDef structure.
3970   * @retval None
3971   */
3972 static void JPEG_MDMAInCpltCallback(MDMA_HandleTypeDef *hmdma)
3973 {
3974   uint32_t inXfrSize;
3975 
3976   JPEG_HandleTypeDef *hjpeg = (JPEG_HandleTypeDef *)((MDMA_HandleTypeDef *)hmdma)->Parent;
3977 
3978   /* Disable The JPEG IT so the MDMA Input Callback can not be interrupted by the JPEG EOC IT or JPEG HPD IT */
3979   __HAL_JPEG_DISABLE_IT(hjpeg, JPEG_INTERRUPT_MASK);
3980 
3981   /* Check if context method is DMA and we are not in ending DMA stage */
3982   if ((hjpeg->Context & (JPEG_CONTEXT_METHOD_MASK | JPEG_CONTEXT_ENDING_DMA)) == JPEG_CONTEXT_DMA)
3983   {
3984 
3985     /*if the MDMA In is triggred with JPEG In FIFO Threshold flag
3986       then MDMA In buffer size is 32 bytes
3987       else (MDMA In is triggred with JPEG In FIFO not full flag)
3988       then MDMA In buffer size is 4 bytes
3989       */
3990     inXfrSize = hjpeg->hdmain->Init.BufferTransferLength;
3991 
3992     hjpeg->JpegInCount = hjpeg->InDataLength - (hmdma->Instance->CBNDTR & MDMA_CBNDTR_BNDT);
3993 
3994     /*Call HAL_JPEG_GetDataCallback to get new data */
3995 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
3996     hjpeg->GetDataCallback(hjpeg, hjpeg->JpegInCount);
3997 #else
3998     HAL_JPEG_GetDataCallback(hjpeg, hjpeg->JpegInCount);
3999 #endif /* USE_HAL_JPEG_REGISTER_CALLBACKS */
4000 
4001 
4002     if (hjpeg->InDataLength >= inXfrSize)
4003     {
4004       if (inXfrSize == 0UL)
4005       {
4006         hjpeg->ErrorCode |= HAL_JPEG_ERROR_DMA;
4007         hjpeg->State = HAL_JPEG_STATE_ERROR;
4008 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
4009         hjpeg->ErrorCallback(hjpeg);
4010 #else
4011         HAL_JPEG_ErrorCallback(hjpeg);
4012 #endif /* USE_HAL_JPEG_REGISTER_CALLBACKS */
4013         return;
4014       }
4015       /*JPEG Input MDMA transfer data number must be multiple of MDMA buffer size
4016         as the destination is a 32 bits register */
4017       hjpeg->InDataLength = hjpeg->InDataLength - (hjpeg->InDataLength % inXfrSize);
4018 
4019     }
4020     else if (hjpeg->InDataLength > 0UL)
4021     {
4022       /* Transfer the remaining Data, must be multiple of source data size (byte) and destination data size (word) */
4023       if ((hjpeg->InDataLength % 4UL) != 0UL)
4024       {
4025         hjpeg->InDataLength = ((hjpeg->InDataLength / 4UL) + 1UL) * 4UL;
4026       }
4027     }
4028     else
4029     {
4030       /* Nothing to do */
4031     }
4032 
4033     if (((hjpeg->Context &  JPEG_CONTEXT_PAUSE_INPUT) == 0UL) && (hjpeg->InDataLength > 0UL))
4034     {
4035       /* Start MDMA FIFO In transfer */
4036       if (HAL_MDMA_Start_IT(hjpeg->hdmain, (uint32_t)hjpeg->pJpegInBuffPtr, (uint32_t)&hjpeg->Instance->DIR,
4037                             hjpeg->InDataLength, 1) != HAL_OK)
4038       {
4039         hjpeg->ErrorCode |= HAL_JPEG_ERROR_DMA;
4040         hjpeg->State = HAL_JPEG_STATE_ERROR;
4041 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
4042         hjpeg->ErrorCallback(hjpeg);
4043 #else
4044         HAL_JPEG_ErrorCallback(hjpeg);
4045 #endif /* USE_HAL_JPEG_REGISTER_CALLBACKS */
4046         return;
4047       }
4048     }
4049 
4050     /* JPEG Conversion still on going : Enable the JPEG IT */
4051     __HAL_JPEG_ENABLE_IT(hjpeg, JPEG_IT_EOC | JPEG_IT_HPD);
4052   }
4053 }
4054 
4055 /**
4056   * @brief  MDMA output transfer complete callback
4057   * @param  hmdma pointer to a MDMA_HandleTypeDef structure.
4058   * @retval None
4059   */
4060 static void JPEG_MDMAOutCpltCallback(MDMA_HandleTypeDef *hmdma)
4061 {
4062   JPEG_HandleTypeDef *hjpeg = (JPEG_HandleTypeDef *)((MDMA_HandleTypeDef *)hmdma)->Parent;
4063 
4064 
4065   /* Disable The JPEG IT so the MDMA Output Callback can not be interrupted by the JPEG EOC IT or JPEG HPD IT */
4066   __HAL_JPEG_DISABLE_IT(hjpeg, JPEG_INTERRUPT_MASK);
4067 
4068   if ((hjpeg->Context & (JPEG_CONTEXT_METHOD_MASK | JPEG_CONTEXT_ENDING_DMA)) ==
4069       JPEG_CONTEXT_DMA) /* Check if context method is DMA and we are not in ending DMA stage */
4070   {
4071     if (__HAL_JPEG_GET_FLAG(hjpeg, JPEG_FLAG_EOCF) == 0UL)
4072     {
4073       hjpeg->JpegOutCount = hjpeg->OutDataLength - (hmdma->Instance->CBNDTR & MDMA_CBNDTR_BNDT);
4074 
4075       /*Output Buffer is full, call HAL_JPEG_DataReadyCallback*/
4076 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
4077       hjpeg->DataReadyCallback(hjpeg, hjpeg->pJpegOutBuffPtr, hjpeg->JpegOutCount);
4078 #else
4079       HAL_JPEG_DataReadyCallback(hjpeg, hjpeg->pJpegOutBuffPtr, hjpeg->JpegOutCount);
4080 #endif /* USE_HAL_JPEG_REGISTER_CALLBACKS */
4081 
4082       if ((hjpeg->Context &  JPEG_CONTEXT_PAUSE_OUTPUT) == 0UL)
4083       {
4084         /* Start MDMA FIFO Out transfer */
4085         if (HAL_MDMA_Start_IT(hjpeg->hdmaout, (uint32_t)&hjpeg->Instance->DOR, (uint32_t)hjpeg->pJpegOutBuffPtr,
4086                               hjpeg->OutDataLength, 1) != HAL_OK)
4087         {
4088           hjpeg->ErrorCode |= HAL_JPEG_ERROR_DMA;
4089           hjpeg->State = HAL_JPEG_STATE_ERROR;
4090 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
4091           hjpeg->ErrorCallback(hjpeg);
4092 #else
4093           HAL_JPEG_ErrorCallback(hjpeg);
4094 #endif /* USE_HAL_JPEG_REGISTER_CALLBACKS */
4095           return;
4096         }
4097       }
4098     }
4099 
4100     /* JPEG Conversion still on going : Enable the JPEG IT */
4101     __HAL_JPEG_ENABLE_IT(hjpeg, JPEG_IT_EOC | JPEG_IT_HPD);
4102   }
4103 
4104 }
4105 
4106 /**
4107   * @brief  MDMA Transfer error callback
4108   * @param  hmdma pointer to a MDMA_HandleTypeDef structure.
4109   * @retval None
4110   */
4111 static void JPEG_MDMAErrorCallback(MDMA_HandleTypeDef *hmdma)
4112 {
4113   JPEG_HandleTypeDef *hjpeg = (JPEG_HandleTypeDef *)((MDMA_HandleTypeDef *)hmdma)->Parent;
4114 
4115   /*Stop Encoding/Decoding*/
4116   hjpeg->Instance->CONFR0 &=  ~JPEG_CONFR0_START;
4117 
4118   /* Disable All Interrupts */
4119   __HAL_JPEG_DISABLE_IT(hjpeg, JPEG_INTERRUPT_MASK);
4120 
4121   hjpeg->State = HAL_JPEG_STATE_READY;
4122   hjpeg->ErrorCode |= HAL_JPEG_ERROR_DMA;
4123 
4124 #if (USE_HAL_JPEG_REGISTER_CALLBACKS == 1)
4125   hjpeg->ErrorCallback(hjpeg);
4126 #else
4127   HAL_JPEG_ErrorCallback(hjpeg);
4128 #endif /* USE_HAL_JPEG_REGISTER_CALLBACKS */
4129 }
4130 
4131 /**
4132   * @brief  MDMA output Abort callback
4133   * @param  hmdma pointer to a MDMA_HandleTypeDef structure.
4134   * @retval None
4135   */
4136 static void JPEG_MDMAOutAbortCallback(MDMA_HandleTypeDef *hmdma)
4137 {
4138   JPEG_HandleTypeDef *hjpeg = (JPEG_HandleTypeDef *)((MDMA_HandleTypeDef *)hmdma)->Parent;
4139 
4140   if ((hjpeg->Context & JPEG_CONTEXT_ENDING_DMA) != 0UL)
4141   {
4142     JPEG_DMA_EndProcess(hjpeg);
4143   }
4144 }
4145 
4146 
4147 /**
4148   * @brief  Calculate the decoded image quality (from 1 to 100)
4149   * @param  hjpeg pointer to a JPEG_HandleTypeDef structure that contains
4150   *         the configuration information for JPEG module
4151   * @retval JPEG image quality from 1 to 100.
4152   */
4153 static uint32_t JPEG_GetQuality(JPEG_HandleTypeDef *hjpeg)
4154 {
4155   uint32_t quality = 0;
4156   uint32_t quantRow, quantVal, scale, i, j;
4157   __IO uint32_t *tableAddress = hjpeg->Instance->QMEM0;
4158 
4159   i = 0;
4160   while (i < (JPEG_QUANT_TABLE_SIZE - 3UL))
4161   {
4162     quantRow = *tableAddress;
4163     for (j = 0; j < 4UL; j++)
4164     {
4165       quantVal = (quantRow >> (8UL * j)) & 0xFFUL;
4166       if (quantVal == 1UL)
4167       {
4168         /* if Quantization value = 1 then quality is 100%*/
4169         quality += 100UL;
4170       }
4171       else
4172       {
4173         /* Note that the quantization coefficients must be specified in the table in zigzag order */
4174         scale = (quantVal * 100UL) / ((uint32_t) hjpeg->QuantTable0[JPEG_ZIGZAG_ORDER[i + j]]);
4175 
4176         if (scale <= 100UL)
4177         {
4178           quality += (200UL - scale) / 2UL;
4179         }
4180         else
4181         {
4182           quality += 5000UL / scale;
4183         }
4184       }
4185     }
4186 
4187     i += 4UL;
4188     tableAddress ++;
4189   }
4190 
4191   return (quality / 64UL);
4192 }
4193 /**
4194   * @}
4195   */
4196 
4197 /**
4198   * @}
4199   */
4200 #endif /* JPEG */
4201 #endif /* HAL_JPEG_MODULE_ENABLED */
4202 
4203 
4204 /**
4205   * @}
4206   */
4207