You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

4174 lines
138 KiB

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