stm32f7xx_hal_jpeg.c 118 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458
  1. /**
  2. ******************************************************************************
  3. * @file stm32f7xx_hal_jpeg.c
  4. * @author MCD Application Team
  5. * @brief JPEG HAL module driver.
  6. * This file provides firmware functions to manage the following
  7. * functionalities of the JPEG encoder/decoder peripheral:
  8. * + Initialization and de-initialization functions
  9. * + JPEG processing functions encoding and decoding
  10. * + JPEG decoding Getting Info and encoding configuration setting
  11. * + JPEG enable/disable header parsing functions (for decoding)
  12. * + JPEG Input/Output Buffer configuration.
  13. * + JPEG callback functions
  14. * + JPEG Abort/Pause/Resume functions
  15. * + JPEG custom quantization tables setting functions
  16. * + IRQ handler management
  17. * + Peripheral State and Error functions
  18. *
  19. @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. @endverbatim
  125. ******************************************************************************
  126. * @attention
  127. *
  128. * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
  129. *
  130. * Redistribution and use in source and binary forms, with or without modification,
  131. * are permitted provided that the following conditions are met:
  132. * 1. Redistributions of source code must retain the above copyright notice,
  133. * this list of conditions and the following disclaimer.
  134. * 2. Redistributions in binary form must reproduce the above copyright notice,
  135. * this list of conditions and the following disclaimer in the documentation
  136. * and/or other materials provided with the distribution.
  137. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  138. * may be used to endorse or promote products derived from this software
  139. * without specific prior written permission.
  140. *
  141. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  142. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  143. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  144. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  145. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  146. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  147. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  148. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  149. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  150. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  151. *
  152. ******************************************************************************
  153. */
  154. /* Includes ------------------------------------------------------------------*/
  155. #include "stm32f7xx_hal.h"
  156. /** @addtogroup STM32F7xx_HAL_Driver
  157. * @{
  158. */
  159. /** @defgroup JPEG JPEG
  160. * @brief JPEG HAL module driver.
  161. * @{
  162. */
  163. #ifdef HAL_JPEG_MODULE_ENABLED
  164. #if defined (STM32F767xx) || defined (STM32F769xx) || defined (STM32F777xx) || defined (STM32F779xx)
  165. /* Private define ------------------------------------------------------------*/
  166. /** @addtogroup JPEG_Private_Constants
  167. * @{
  168. */
  169. #define JPEG_TIMEOUT_VALUE ((uint32_t)1000U) /* 1s */
  170. #define JPEG_AC_HUFF_TABLE_SIZE ((uint32_t)162U) /* Huffman AC table size : 162 codes*/
  171. #define JPEG_DC_HUFF_TABLE_SIZE ((uint32_t)12U) /* Huffman AC table size : 12 codes*/
  172. #define JPEG_FIFO_SIZE ((uint32_t)16U) /* JPEG Input/Output HW FIFO size in words*/
  173. #define JPEG_INTERRUPT_MASK ((uint32_t)0x0000007EU) /* JPEG Interrupt Mask*/
  174. #define JPEG_DMA_MASK ((uint32_t)0x00001800U) /* JPEG DMA request Mask*/
  175. #define JPEG_DMA_IDMA ((uint32_t)JPEG_CR_IDMAEN) /* DMA request for the input FIFO */
  176. #define JPEG_DMA_ODMA ((uint32_t)JPEG_CR_ODMAEN) /* DMA request for the output FIFO */
  177. #define JPEG_CONTEXT_ENCODE ((uint32_t)0x00000001U) /* JPEG context : operation is encoding*/
  178. #define JPEG_CONTEXT_DECODE ((uint32_t)0x00000002U) /* JPEG context : operation is decoding*/
  179. #define JPEG_CONTEXT_OPERATION_MASK ((uint32_t)0x00000003U) /* JPEG context : operation Mask */
  180. #define JPEG_CONTEXT_POLLING ((uint32_t)0x00000004U) /* JPEG context : Transfer use Polling */
  181. #define JPEG_CONTEXT_IT ((uint32_t)0x00000008U) /* JPEG context : Transfer use Interrupt */
  182. #define JPEG_CONTEXT_DMA ((uint32_t)0x0000000CU) /* JPEG context : Transfer use DMA */
  183. #define JPEG_CONTEXT_METHOD_MASK ((uint32_t)0x0000000CU) /* JPEG context : Transfer Mask */
  184. #define JPEG_CONTEXT_CONF_ENCODING ((uint32_t)0x00000100U) /* JPEG context : encoding config done */
  185. #define JPEG_CONTEXT_PAUSE_INPUT ((uint32_t)0x00001000U) /* JPEG context : Pause Input */
  186. #define JPEG_CONTEXT_PAUSE_OUTPUT ((uint32_t)0x00002000U) /* JPEG context : Pause Output */
  187. #define JPEG_CONTEXT_CUSTOM_TABLES ((uint32_t)0x00004000U) /* JPEG context : Use custom quantization tables */
  188. #define JPEG_CONTEXT_ENDING_DMA ((uint32_t)0x00008000U) /* JPEG context : ending with DMA in progress */
  189. #define JPEG_PROCESS_ONGOING ((uint32_t)0x00000000U) /* Process is on going */
  190. #define JPEG_PROCESS_DONE ((uint32_t)0x00000001U) /* Process is done (ends) */
  191. /**
  192. * @}
  193. */
  194. /* Private typedef -----------------------------------------------------------*/
  195. /** @addtogroup JPEG_Private_Types
  196. * @{
  197. */
  198. /*
  199. JPEG Huffman Table Structure definition :
  200. This implementation of Huffman table structure is compliant with ISO/IEC 10918-1 standard , Annex C Huffman Table specification
  201. */
  202. typedef struct
  203. {
  204. /* These two fields directly represent the contents of a JPEG DHT marker */
  205. uint8_t Bits[16]; /*!< bits[k] = # of symbols with codes of length k bits, this parameter corresponds to BITS list in the Annex C */
  206. uint8_t HuffVal[162]; /*!< The symbols, in order of incremented code length, this parameter corresponds to HUFFVAL list in the Annex C */
  207. }JPEG_ACHuffTableTypeDef;
  208. typedef struct
  209. {
  210. /* These two fields directly represent the contents of a JPEG DHT marker */
  211. uint8_t Bits[16]; /*!< bits[k] = # of symbols with codes of length k bits, this parameter corresponds to BITS list in the Annex C */
  212. uint8_t HuffVal[12]; /*!< The symbols, in order of incremented code length, this parameter corresponds to HUFFVAL list in the Annex C */
  213. }JPEG_DCHuffTableTypeDef;
  214. typedef struct
  215. {
  216. uint8_t CodeLength[JPEG_AC_HUFF_TABLE_SIZE]; /*!< Code length */
  217. uint32_t HuffmanCode[JPEG_AC_HUFF_TABLE_SIZE]; /*!< HuffmanCode */
  218. }JPEG_AC_HuffCodeTableTypeDef;
  219. typedef struct
  220. {
  221. uint8_t CodeLength[JPEG_DC_HUFF_TABLE_SIZE]; /*!< Code length */
  222. uint32_t HuffmanCode[JPEG_DC_HUFF_TABLE_SIZE]; /*!< HuffmanCode */
  223. }JPEG_DC_HuffCodeTableTypeDef;
  224. /**
  225. * @}
  226. */
  227. /* Private macro -------------------------------------------------------------*/
  228. /** @addtogroup JPEG_Private_Macros
  229. * @{
  230. */
  231. #define JPEG_ENABLE_DMA(__HANDLE__,__DMA__) ((__HANDLE__)->Instance->CR |= ((__DMA__) & JPEG_DMA_MASK))
  232. /*note : To disable a DMA request we must use MODIFY_REG macro to avoid writing "1" to the FIFO flush bits
  233. located in the same DMA request enable register (CR register). */
  234. #define JPEG_DISABLE_DMA(__HANDLE__,__DMA__) MODIFY_REG((__HANDLE__)->Instance->CR, ((__DMA__) & JPEG_DMA_MASK), 0)
  235. /**
  236. * @}
  237. */
  238. /* Private variables ---------------------------------------------------------*/
  239. /** @addtogroup JPEG_Private_Variables
  240. * @{
  241. */
  242. static const JPEG_DCHuffTableTypeDef JPEG_DCLUM_HuffTable =
  243. {
  244. { 0, 1, 5, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0 }, /*Bits*/
  245. { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0xa, 0xb } /*HUFFVAL */
  246. };
  247. static const JPEG_DCHuffTableTypeDef JPEG_DCCHROM_HuffTable =
  248. {
  249. { 0, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0 }, /*Bits*/
  250. { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0xa, 0xb } /*HUFFVAL */
  251. };
  252. static const JPEG_ACHuffTableTypeDef JPEG_ACLUM_HuffTable =
  253. {
  254. { 0, 2, 1, 3, 3, 2, 4, 3, 5, 5, 4, 4, 0, 0, 1, 0x7d }, /*Bits*/
  255. { 0x01, 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12, /*HUFFVAL */
  256. 0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61, 0x07,
  257. 0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xa1, 0x08,
  258. 0x23, 0x42, 0xb1, 0xc1, 0x15, 0x52, 0xd1, 0xf0,
  259. 0x24, 0x33, 0x62, 0x72, 0x82, 0x09, 0x0a, 0x16,
  260. 0x17, 0x18, 0x19, 0x1a, 0x25, 0x26, 0x27, 0x28,
  261. 0x29, 0x2a, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
  262. 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49,
  263. 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59,
  264. 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69,
  265. 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79,
  266. 0x7a, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89,
  267. 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
  268. 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
  269. 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6,
  270. 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3, 0xc4, 0xc5,
  271. 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 0xd3, 0xd4,
  272. 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe1, 0xe2,
  273. 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea,
  274. 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
  275. 0xf9, 0xfa }
  276. };
  277. static const JPEG_ACHuffTableTypeDef JPEG_ACCHROM_HuffTable =
  278. {
  279. { 0, 2, 1, 2, 4, 4, 3, 4, 7, 5, 4, 4, 0, 1, 2, 0x77 }, /*Bits*/
  280. { 0x00, 0x01, 0x02, 0x03, 0x11, 0x04, 0x05, 0x21, /*HUFFVAL */
  281. 0x31, 0x06, 0x12, 0x41, 0x51, 0x07, 0x61, 0x71,
  282. 0x13, 0x22, 0x32, 0x81, 0x08, 0x14, 0x42, 0x91,
  283. 0xa1, 0xb1, 0xc1, 0x09, 0x23, 0x33, 0x52, 0xf0,
  284. 0x15, 0x62, 0x72, 0xd1, 0x0a, 0x16, 0x24, 0x34,
  285. 0xe1, 0x25, 0xf1, 0x17, 0x18, 0x19, 0x1a, 0x26,
  286. 0x27, 0x28, 0x29, 0x2a, 0x35, 0x36, 0x37, 0x38,
  287. 0x39, 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48,
  288. 0x49, 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58,
  289. 0x59, 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68,
  290. 0x69, 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78,
  291. 0x79, 0x7a, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
  292. 0x88, 0x89, 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96,
  293. 0x97, 0x98, 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5,
  294. 0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4,
  295. 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3,
  296. 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2,
  297. 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda,
  298. 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9,
  299. 0xea, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
  300. 0xf9, 0xfa }
  301. };
  302. /*
  303. These are the sample quantization tables given in JPEG spec ISO/IEC 10918-1 standard , section K.1.
  304. */
  305. static const uint8_t JPEG_LUM_QuantTable[JPEG_QUANT_TABLE_SIZE] =
  306. {
  307. 16, 11, 10, 16, 24, 40, 51, 61,
  308. 12, 12, 14, 19, 26, 58, 60, 55,
  309. 14, 13, 16, 24, 40, 57, 69, 56,
  310. 14, 17, 22, 29, 51, 87, 80, 62,
  311. 18, 22, 37, 56, 68, 109, 103, 77,
  312. 24, 35, 55, 64, 81, 104, 113, 92,
  313. 49, 64, 78, 87, 103, 121, 120, 101,
  314. 72, 92, 95, 98, 112, 100, 103, 99
  315. };
  316. static const uint8_t JPEG_CHROM_QuantTable[JPEG_QUANT_TABLE_SIZE] =
  317. {
  318. 17, 18, 24, 47, 99, 99, 99, 99,
  319. 18, 21, 26, 66, 99, 99, 99, 99,
  320. 24, 26, 56, 99, 99, 99, 99, 99,
  321. 47, 66, 99, 99, 99, 99, 99, 99,
  322. 99, 99, 99, 99, 99, 99, 99, 99,
  323. 99, 99, 99, 99, 99, 99, 99, 99,
  324. 99, 99, 99, 99, 99, 99, 99, 99,
  325. 99, 99, 99, 99, 99, 99, 99, 99
  326. };
  327. static const uint8_t JPEG_ZIGZAG_ORDER[JPEG_QUANT_TABLE_SIZE] =
  328. {
  329. 0, 1, 8, 16, 9, 2, 3, 10,
  330. 17, 24, 32, 25, 18, 11, 4, 5,
  331. 12, 19, 26, 33, 40, 48, 41, 34,
  332. 27, 20, 13, 6, 7, 14, 21, 28,
  333. 35, 42, 49, 56, 57, 50, 43, 36,
  334. 29, 22, 15, 23, 30, 37, 44, 51,
  335. 58, 59, 52, 45, 38, 31, 39, 46,
  336. 53, 60, 61, 54, 47, 55, 62, 63
  337. };
  338. /**
  339. * @}
  340. */
  341. /* Private function prototypes -----------------------------------------------*/
  342. /** @addtogroup JPEG_Private_Functions_Prototypes
  343. * @{
  344. */
  345. static HAL_StatusTypeDef JPEG_Bits_To_SizeCodes(uint8_t *Bits, uint8_t *Huffsize, uint32_t *Huffcode, uint32_t *LastK);
  346. static HAL_StatusTypeDef JPEG_DCHuff_BitsVals_To_SizeCodes(JPEG_DCHuffTableTypeDef *DC_BitsValsTable, JPEG_DC_HuffCodeTableTypeDef *DC_SizeCodesTable);
  347. static HAL_StatusTypeDef JPEG_ACHuff_BitsVals_To_SizeCodes(JPEG_ACHuffTableTypeDef *AC_BitsValsTable, JPEG_AC_HuffCodeTableTypeDef *AC_SizeCodesTable);
  348. static HAL_StatusTypeDef JPEG_Set_HuffDC_Mem(JPEG_HandleTypeDef *hjpeg, JPEG_DCHuffTableTypeDef *HuffTableDC, uint32_t *DCTableAddress);
  349. static HAL_StatusTypeDef JPEG_Set_HuffAC_Mem(JPEG_HandleTypeDef *hjpeg, JPEG_ACHuffTableTypeDef *HuffTableAC, uint32_t *ACTableAddress);
  350. static HAL_StatusTypeDef JPEG_Set_HuffEnc_Mem(JPEG_HandleTypeDef *hjpeg, JPEG_ACHuffTableTypeDef *HuffTableAC0, JPEG_DCHuffTableTypeDef *HuffTableDC0 , JPEG_ACHuffTableTypeDef *HuffTableAC1, JPEG_DCHuffTableTypeDef *HuffTableDC1);
  351. static void JPEG_Set_Huff_DHTMem(JPEG_HandleTypeDef *hjpeg, JPEG_ACHuffTableTypeDef *HuffTableAC0, JPEG_DCHuffTableTypeDef *HuffTableDC0 , JPEG_ACHuffTableTypeDef *HuffTableAC1, JPEG_DCHuffTableTypeDef *HuffTableDC1);
  352. static HAL_StatusTypeDef JPEG_Set_Quantization_Mem(JPEG_HandleTypeDef *hjpeg, uint8_t *QTable, uint32_t *QTableAddress);
  353. static void JPEG_SetColorYCBCR(JPEG_HandleTypeDef *hjpeg);
  354. static void JPEG_SetColorGrayScale(JPEG_HandleTypeDef *hjpeg);
  355. static void JPEG_SetColorCMYK(JPEG_HandleTypeDef *hjpeg);
  356. static void JPEG_Init_Process(JPEG_HandleTypeDef *hjpeg);
  357. static uint32_t JPEG_Process(JPEG_HandleTypeDef *hjpeg);
  358. static void JPEG_ReadInputData(JPEG_HandleTypeDef *hjpeg, uint32_t nbRequestWords);
  359. static void JPEG_StoreOutputData(JPEG_HandleTypeDef *hjpeg, uint32_t nbOutputWords);
  360. static uint32_t JPEG_GetQuality(JPEG_HandleTypeDef *hjpeg);
  361. static HAL_StatusTypeDef JPEG_DMA_StartProcess(JPEG_HandleTypeDef *hjpeg);
  362. static uint32_t JPEG_DMA_ContinueProcess(JPEG_HandleTypeDef *hjpeg);
  363. static uint32_t JPEG_DMA_EndProcess(JPEG_HandleTypeDef *hjpeg);
  364. static void JPEG_DMA_PollResidualData(JPEG_HandleTypeDef *hjpeg);
  365. static void JPEG_DMAOutCpltCallback(DMA_HandleTypeDef *hdma);
  366. static void JPEG_DMAInCpltCallback(DMA_HandleTypeDef *hdma);
  367. static void JPEG_DMAErrorCallback(DMA_HandleTypeDef *hdma);
  368. static void JPEG_DMAOutAbortCallback(DMA_HandleTypeDef *hdma) ;
  369. /**
  370. * @}
  371. */
  372. /** @defgroup JPEG_Exported_Functions JPEG Exported Functions
  373. * @{
  374. */
  375. /** @defgroup JPEG_Exported_Functions_Group1 Initialization and de-initialization functions
  376. * @brief Initialization and de-initialization functions.
  377. *
  378. @verbatim
  379. ==============================================================================
  380. ##### Initialization and de-initialization functions #####
  381. ==============================================================================
  382. [..] This section provides functions allowing to:
  383. (+) Initialize the JPEG peripheral and creates the associated handle
  384. (+) DeInitialize the JPEG peripheral
  385. @endverbatim
  386. * @{
  387. */
  388. /**
  389. * @brief Initializes the JPEG according to the specified
  390. * parameters in the JPEG_InitTypeDef and creates the associated handle.
  391. * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
  392. * the configuration information for JPEG module
  393. * @retval HAL status
  394. */
  395. HAL_StatusTypeDef HAL_JPEG_Init(JPEG_HandleTypeDef *hjpeg)
  396. {
  397. /*Note : these intermediate variables are used to avoid MISRA warning
  398. regarding rule 11.5 */
  399. uint32_t acLum_huffmanTableAddr = (uint32_t)(&JPEG_ACLUM_HuffTable);
  400. uint32_t dcLum_huffmanTableAddr = (uint32_t)(&JPEG_DCLUM_HuffTable);
  401. uint32_t acChrom_huffmanTableAddr = (uint32_t)(&JPEG_ACCHROM_HuffTable);
  402. uint32_t dcChrom_huffmanTableAddr = (uint32_t)(&JPEG_DCCHROM_HuffTable);
  403. /* Check the JPEG handle allocation */
  404. if(hjpeg == NULL)
  405. {
  406. return HAL_ERROR;
  407. }
  408. if(hjpeg->State == HAL_JPEG_STATE_RESET)
  409. {
  410. /* Allocate lock resource and initialize it */
  411. hjpeg->Lock = HAL_UNLOCKED;
  412. /* Init the low level hardware : GPIO, CLOCK */
  413. HAL_JPEG_MspInit(hjpeg);
  414. }
  415. /* Change the JPEG state */
  416. hjpeg->State = HAL_JPEG_STATE_BUSY;
  417. /* Start the JPEG Core*/
  418. __HAL_JPEG_ENABLE(hjpeg);
  419. /* Stop the JPEG encoding/decoding process*/
  420. hjpeg->Instance->CONFR0 &= ~JPEG_CONFR0_START;
  421. /* Disable All Interrupts */
  422. __HAL_JPEG_DISABLE_IT(hjpeg,JPEG_INTERRUPT_MASK);
  423. /* Disable All DMA requests */
  424. JPEG_DISABLE_DMA(hjpeg,JPEG_DMA_MASK);
  425. /* Flush input and output FIFOs*/
  426. hjpeg->Instance->CR |= JPEG_CR_IFF;
  427. hjpeg->Instance->CR |= JPEG_CR_OFF;
  428. /* Clear all flags */
  429. __HAL_JPEG_CLEAR_FLAG(hjpeg,JPEG_FLAG_ALL);
  430. hjpeg->QuantTable0 = (uint8_t *)JPEG_LUM_QuantTable;
  431. hjpeg->QuantTable1 = (uint8_t *)JPEG_CHROM_QuantTable;
  432. hjpeg->QuantTable2 = NULL;
  433. hjpeg->QuantTable3 = NULL;
  434. /* init the default Huffman tables*/
  435. if(JPEG_Set_HuffEnc_Mem(hjpeg, (JPEG_ACHuffTableTypeDef *)acLum_huffmanTableAddr, (JPEG_DCHuffTableTypeDef *)dcLum_huffmanTableAddr, (JPEG_ACHuffTableTypeDef *)acChrom_huffmanTableAddr, (JPEG_DCHuffTableTypeDef *)dcChrom_huffmanTableAddr) != HAL_OK)
  436. {
  437. hjpeg->ErrorCode = HAL_JPEG_ERROR_HUFF_TABLE;
  438. return HAL_ERROR;
  439. }
  440. /* Enable header processing*/
  441. hjpeg->Instance->CONFR1 |= JPEG_CONFR1_HDR;
  442. /* Reset JpegInCount and JpegOutCount */
  443. hjpeg->JpegInCount = 0;
  444. hjpeg->JpegOutCount = 0;
  445. /* Change the JPEG state */
  446. hjpeg->State = HAL_JPEG_STATE_READY;
  447. /* Reset the JPEG ErrorCode */
  448. hjpeg->ErrorCode = HAL_JPEG_ERROR_NONE;
  449. /*Clear the context filelds*/
  450. hjpeg->Context = 0;
  451. /* Return function status */
  452. return HAL_OK;
  453. }
  454. /**
  455. * @brief DeInitializes the JPEG peripheral.
  456. * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
  457. * the configuration information for JPEG module
  458. * @retval HAL status
  459. */
  460. HAL_StatusTypeDef HAL_JPEG_DeInit(JPEG_HandleTypeDef *hjpeg)
  461. {
  462. /* Check the JPEG handle allocation */
  463. if(hjpeg == NULL)
  464. {
  465. return HAL_ERROR;
  466. }
  467. /* DeInit the low level hardware: CLOCK, NVIC.*/
  468. HAL_JPEG_MspDeInit(hjpeg);
  469. /* Change the JPEG state */
  470. hjpeg->State = HAL_JPEG_STATE_BUSY;
  471. /* Reset the JPEG ErrorCode */
  472. hjpeg->ErrorCode = HAL_JPEG_ERROR_NONE;
  473. /* Reset JpegInCount and JpegOutCount */
  474. hjpeg->JpegInCount = 0;
  475. hjpeg->JpegOutCount = 0;
  476. /* Change the JPEG state */
  477. hjpeg->State = HAL_JPEG_STATE_RESET;
  478. /*Clear the context fields*/
  479. hjpeg->Context = 0;
  480. /* Release Lock */
  481. __HAL_UNLOCK(hjpeg);
  482. /* Return function status */
  483. return HAL_OK;
  484. }
  485. /**
  486. * @brief Initializes the JPEG MSP.
  487. * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
  488. * the configuration information for JPEG module
  489. * @retval None
  490. */
  491. __weak void HAL_JPEG_MspInit(JPEG_HandleTypeDef *hjpeg)
  492. {
  493. /* Prevent unused argument(s) compilation warning */
  494. UNUSED(hjpeg);
  495. /* NOTE : This function Should not be modified, when the callback is needed,
  496. the HAL_JPEG_MspInit could be implemented in the user file
  497. */
  498. }
  499. /**
  500. * @brief DeInitializes JPEG MSP.
  501. * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
  502. * the configuration information for JPEG module
  503. * @retval None
  504. */
  505. __weak void HAL_JPEG_MspDeInit(JPEG_HandleTypeDef *hjpeg)
  506. {
  507. /* Prevent unused argument(s) compilation warning */
  508. UNUSED(hjpeg);
  509. /* NOTE : This function Should not be modified, when the callback is needed,
  510. the HAL_JPEG_MspDeInit could be implemented in the user file
  511. */
  512. }
  513. /**
  514. * @}
  515. */
  516. /** @defgroup JPEG_Exported_Functions_Group2 Configuration functions
  517. * @brief JPEG Configuration functions.
  518. *
  519. @verbatim
  520. ==============================================================================
  521. ##### Configuration functions #####
  522. ==============================================================================
  523. [..] This section provides functions allowing to:
  524. (+) HAL_JPEG_ConfigEncoding() : JPEG encoding configuration
  525. (+) HAL_JPEG_GetInfo() : Extract the image configuration from the JPEG header during the decoding
  526. (+) HAL_JPEG_EnableHeaderParsing() : Enable JPEG Header parsing for decoding
  527. (+) HAL_JPEG_DisableHeaderParsing() : Disable JPEG Header parsing for decoding
  528. (+) HAL_JPEG_SetUserQuantTables : Modify the default Quantization tables used for JPEG encoding.
  529. @endverbatim
  530. * @{
  531. */
  532. /**
  533. * @brief Set the JPEG encoding configuration.
  534. * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
  535. * the configuration information for JPEG module
  536. * @param pConf pointer to a JPEG_ConfTypeDef structure that contains
  537. * the encoding configuration
  538. * @retval HAL status
  539. */
  540. HAL_StatusTypeDef HAL_JPEG_ConfigEncoding(JPEG_HandleTypeDef *hjpeg, JPEG_ConfTypeDef *pConf)
  541. {
  542. uint32_t error = HAL_OK;
  543. uint32_t numberMCU, hfactor, vfactor,hMCU, vMCU;
  544. /* Check the JPEG handle allocation */
  545. if( (hjpeg == NULL) || (pConf == NULL) )
  546. {
  547. return HAL_ERROR;
  548. }
  549. else
  550. {
  551. /* Check the parameters */
  552. assert_param(IS_JPEG_COLORSPACE(pConf->ColorSpace));
  553. assert_param(IS_JPEG_CHROMASUBSAMPLING(pConf->ChromaSubsampling));
  554. assert_param(IS_JPEG_IMAGE_QUALITY(pConf->ImageQuality));
  555. /* Process Locked */
  556. __HAL_LOCK(hjpeg);
  557. if(hjpeg->State == HAL_JPEG_STATE_READY)
  558. {
  559. hjpeg->State = HAL_JPEG_STATE_BUSY;
  560. hjpeg->Conf.ColorSpace = pConf->ColorSpace;
  561. hjpeg->Conf.ChromaSubsampling = pConf->ChromaSubsampling;
  562. hjpeg->Conf.ImageHeight = pConf->ImageHeight;
  563. hjpeg->Conf.ImageWidth = pConf->ImageWidth;
  564. hjpeg->Conf.ImageQuality = pConf->ImageQuality;
  565. /* Reset the Color Space : by default only one quantization table is used*/
  566. hjpeg->Instance->CONFR1 &= ~JPEG_CONFR1_COLORSPACE;
  567. /* Set Number of color components*/
  568. if(hjpeg->Conf.ColorSpace == JPEG_GRAYSCALE_COLORSPACE)
  569. {
  570. /*Gray Scale is only one component 8x8 blocks i.e 4:4:4*/
  571. hjpeg->Conf.ChromaSubsampling = JPEG_444_SUBSAMPLING;
  572. JPEG_SetColorGrayScale(hjpeg);
  573. /* Set quantization table 0*/
  574. error = JPEG_Set_Quantization_Mem(hjpeg, hjpeg->QuantTable0, (uint32_t *)(hjpeg->Instance->QMEM0));
  575. }
  576. else if(hjpeg->Conf.ColorSpace == JPEG_YCBCR_COLORSPACE)
  577. {
  578. /*
  579. Set the Color Space for YCbCr : 2 quantization tables are used
  580. one for Luminance(Y) and one for both Chrominances (Cb & Cr)
  581. */
  582. hjpeg->Instance->CONFR1 |= JPEG_CONFR1_COLORSPACE_0;
  583. JPEG_SetColorYCBCR(hjpeg);
  584. /* Set quantization table 0*/
  585. error = JPEG_Set_Quantization_Mem(hjpeg, hjpeg->QuantTable0, (uint32_t *)(hjpeg->Instance->QMEM0));
  586. /*By default quantization table 0 for component 0 and quantization table 1 for both components 1 and 2*/
  587. error |= JPEG_Set_Quantization_Mem(hjpeg, hjpeg->QuantTable1, (uint32_t *)(hjpeg->Instance->QMEM1));
  588. if((hjpeg->Context & JPEG_CONTEXT_CUSTOM_TABLES) != 0) /*Use user customized quantization tables , 1 table per component*/
  589. {
  590. /* use 3 quantization tables , one for each component*/
  591. hjpeg->Instance->CONFR1 &= (~JPEG_CONFR1_COLORSPACE);
  592. hjpeg->Instance->CONFR1 |= JPEG_CONFR1_COLORSPACE_1;
  593. error |= JPEG_Set_Quantization_Mem(hjpeg, hjpeg->QuantTable2, (uint32_t *)(hjpeg->Instance->QMEM2));
  594. /*Use Quantization 1 table for component 1*/
  595. hjpeg->Instance->CONFR5 &= (~JPEG_CONFR5_QT);
  596. hjpeg->Instance->CONFR5 |= JPEG_CONFR5_QT_0;
  597. /*Use Quantization 2 table for component 2*/
  598. hjpeg->Instance->CONFR6 &= (~JPEG_CONFR6_QT);
  599. hjpeg->Instance->CONFR6 |= JPEG_CONFR6_QT_1;
  600. }
  601. }
  602. else if(hjpeg->Conf.ColorSpace == JPEG_CMYK_COLORSPACE)
  603. {
  604. JPEG_SetColorCMYK(hjpeg);
  605. /* Set quantization table 0*/
  606. error = JPEG_Set_Quantization_Mem(hjpeg, hjpeg->QuantTable0, (uint32_t *)(hjpeg->Instance->QMEM0));
  607. /*By default quantization table 0 for All components*/
  608. if((hjpeg->Context & JPEG_CONTEXT_CUSTOM_TABLES) != 0) /*Use user customized quantization tables , 1 table per component*/
  609. {
  610. /* use 4 quantization tables , one for each component*/
  611. hjpeg->Instance->CONFR1 |= JPEG_CONFR1_COLORSPACE;
  612. error |= JPEG_Set_Quantization_Mem(hjpeg, hjpeg->QuantTable1, (uint32_t *)(hjpeg->Instance->QMEM1));
  613. error |= JPEG_Set_Quantization_Mem(hjpeg, hjpeg->QuantTable2, (uint32_t *)(hjpeg->Instance->QMEM2));
  614. error |= JPEG_Set_Quantization_Mem(hjpeg, hjpeg->QuantTable3, (uint32_t *)(hjpeg->Instance->QMEM3));
  615. /*Use Quantization 1 table for component 1*/
  616. hjpeg->Instance->CONFR5 |= JPEG_CONFR5_QT_0;
  617. /*Use Quantization 2 table for component 2*/
  618. hjpeg->Instance->CONFR6 |= JPEG_CONFR6_QT_1;
  619. /*Use Quantization 3 table for component 3*/
  620. hjpeg->Instance->CONFR7 |= JPEG_CONFR7_QT;
  621. }
  622. }
  623. if(error != HAL_OK)
  624. {
  625. hjpeg->ErrorCode = HAL_JPEG_ERROR_QUANT_TABLE;
  626. /* Process Unlocked */
  627. __HAL_UNLOCK(hjpeg);
  628. /* Set the JPEG State to ready */
  629. hjpeg->State = HAL_JPEG_STATE_READY;
  630. return HAL_ERROR;
  631. }
  632. /* Set the image size*/
  633. MODIFY_REG(hjpeg->Instance->CONFR1, JPEG_CONFR1_YSIZE, ((hjpeg->Conf.ImageHeight & 0x0000FFFF) << 16)); /* set the number of lines*/
  634. MODIFY_REG(hjpeg->Instance->CONFR3, JPEG_CONFR3_XSIZE, ((hjpeg->Conf.ImageWidth & 0x0000FFFF) << 16)); /* set the number of pixels per line*/
  635. if(hjpeg->Conf.ChromaSubsampling == JPEG_420_SUBSAMPLING) /* 4:2:0*/
  636. {
  637. hfactor = 16;
  638. vfactor = 16;
  639. }
  640. else if(hjpeg->Conf.ChromaSubsampling == JPEG_422_SUBSAMPLING) /* 4:2:2*/
  641. {
  642. hfactor = 16;
  643. vfactor = 8;
  644. }
  645. else /* Default is 8x8 MCU, 4:4:4*/
  646. {
  647. hfactor = 8;
  648. vfactor = 8;
  649. }
  650. hMCU = (hjpeg->Conf.ImageWidth / hfactor);
  651. if((hjpeg->Conf.ImageWidth % hfactor) != 0)
  652. {
  653. hMCU++; /*+1 for horizontal incomplete MCU */
  654. }
  655. vMCU = (hjpeg->Conf.ImageHeight / vfactor);
  656. if((hjpeg->Conf.ImageHeight % vfactor) != 0)
  657. {
  658. vMCU++; /*+1 for vertical incomplete MCU */
  659. }
  660. numberMCU = (hMCU * vMCU) - 1; /* Bit Field JPEG_CONFR2_NMCU shall be set to NB_MCU - 1*/
  661. /* Set the number of MCU*/
  662. hjpeg->Instance->CONFR2 = (numberMCU & JPEG_CONFR2_NMCU);
  663. hjpeg->Context |= JPEG_CONTEXT_CONF_ENCODING;
  664. /* Process Unlocked */
  665. __HAL_UNLOCK(hjpeg);
  666. /* Set the JPEG State to ready */
  667. hjpeg->State = HAL_JPEG_STATE_READY;
  668. /* Return function status */
  669. return HAL_OK;
  670. }
  671. else
  672. {
  673. /* Process Unlocked */
  674. __HAL_UNLOCK(hjpeg);
  675. /* Return function status */
  676. return HAL_BUSY;
  677. }
  678. }
  679. }
  680. /**
  681. * @brief Extract the image configuration from the JPEG header during the decoding
  682. * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
  683. * the configuration information for JPEG module
  684. * @param pInfo pointer to a JPEG_ConfTypeDef structure that contains
  685. * The JPEG decoded header informations
  686. * @retval HAL status
  687. */
  688. HAL_StatusTypeDef HAL_JPEG_GetInfo(JPEG_HandleTypeDef *hjpeg, JPEG_ConfTypeDef *pInfo)
  689. {
  690. uint32_t yblockNb, cBblockNb, cRblockNb;
  691. /* Check the JPEG handle allocation */
  692. if((hjpeg == NULL) || (pInfo == NULL))
  693. {
  694. return HAL_ERROR;
  695. }
  696. /*Read the conf parameters */
  697. if((hjpeg->Instance->CONFR1 & JPEG_CONFR1_NF) == JPEG_CONFR1_NF_1)
  698. {
  699. pInfo->ColorSpace = JPEG_YCBCR_COLORSPACE;
  700. }
  701. else if((hjpeg->Instance->CONFR1 & JPEG_CONFR1_NF) == 0)
  702. {
  703. pInfo->ColorSpace = JPEG_GRAYSCALE_COLORSPACE;
  704. }
  705. else if((hjpeg->Instance->CONFR1 & JPEG_CONFR1_NF) == JPEG_CONFR1_NF)
  706. {
  707. pInfo->ColorSpace = JPEG_CMYK_COLORSPACE;
  708. }
  709. pInfo->ImageHeight = (hjpeg->Instance->CONFR1 & 0xFFFF0000U) >> 16;
  710. pInfo->ImageWidth = (hjpeg->Instance->CONFR3 & 0xFFFF0000U) >> 16;
  711. if((pInfo->ColorSpace == JPEG_YCBCR_COLORSPACE) || (pInfo->ColorSpace == JPEG_CMYK_COLORSPACE))
  712. {
  713. yblockNb = (hjpeg->Instance->CONFR4 & JPEG_CONFR4_NB) >> 4;
  714. cBblockNb = (hjpeg->Instance->CONFR5 & JPEG_CONFR5_NB) >> 4;
  715. cRblockNb = (hjpeg->Instance->CONFR6 & JPEG_CONFR6_NB) >> 4;
  716. if((yblockNb == 1) && (cBblockNb == 0) && (cRblockNb == 0))
  717. {
  718. pInfo->ChromaSubsampling = JPEG_422_SUBSAMPLING; /*16x8 block*/
  719. }
  720. else if((yblockNb == 0) && (cBblockNb == 0) && (cRblockNb == 0))
  721. {
  722. pInfo->ChromaSubsampling = JPEG_444_SUBSAMPLING;
  723. }
  724. else if((yblockNb == 3) && (cBblockNb == 0) && (cRblockNb == 0))
  725. {
  726. pInfo->ChromaSubsampling = JPEG_420_SUBSAMPLING;
  727. }
  728. else /*Default is 4:4:4*/
  729. {
  730. pInfo->ChromaSubsampling = JPEG_444_SUBSAMPLING;
  731. }
  732. }
  733. else
  734. {
  735. pInfo->ChromaSubsampling = JPEG_444_SUBSAMPLING;
  736. }
  737. pInfo->ImageQuality = JPEG_GetQuality(hjpeg);
  738. /* Return function status */
  739. return HAL_OK;
  740. }
  741. /**
  742. * @brief Enable JPEG Header parsing for decoding
  743. * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
  744. * the configuration information for the JPEG.
  745. * @retval HAL status
  746. */
  747. HAL_StatusTypeDef HAL_JPEG_EnableHeaderParsing(JPEG_HandleTypeDef *hjpeg)
  748. {
  749. /* Process locked */
  750. __HAL_LOCK(hjpeg);
  751. if(hjpeg->State == HAL_JPEG_STATE_READY)
  752. {
  753. /* Change the JPEG state */
  754. hjpeg->State = HAL_JPEG_STATE_BUSY;
  755. /* Enable header processing*/
  756. hjpeg->Instance->CONFR1 |= JPEG_CONFR1_HDR;
  757. /* Process unlocked */
  758. __HAL_UNLOCK(hjpeg);
  759. /* Change the JPEG state */
  760. hjpeg->State = HAL_JPEG_STATE_READY;
  761. return HAL_OK;
  762. }
  763. else
  764. {
  765. /* Process unlocked */
  766. __HAL_UNLOCK(hjpeg);
  767. return HAL_BUSY;
  768. }
  769. }
  770. /**
  771. * @brief Disable JPEG Header parsing for decoding
  772. * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
  773. * the configuration information for the JPEG.
  774. * @retval HAL status
  775. */
  776. HAL_StatusTypeDef HAL_JPEG_DisableHeaderParsing(JPEG_HandleTypeDef *hjpeg)
  777. {
  778. /* Process locked */
  779. __HAL_LOCK(hjpeg);
  780. if(hjpeg->State == HAL_JPEG_STATE_READY)
  781. {
  782. /* Change the JPEG state */
  783. hjpeg->State = HAL_JPEG_STATE_BUSY;
  784. /* Disable header processing*/
  785. hjpeg->Instance->CONFR1 &= ~JPEG_CONFR1_HDR;
  786. /* Process unlocked */
  787. __HAL_UNLOCK(hjpeg);
  788. /* Change the JPEG state */
  789. hjpeg->State = HAL_JPEG_STATE_READY;
  790. return HAL_OK;
  791. }
  792. else
  793. {
  794. /* Process unlocked */
  795. __HAL_UNLOCK(hjpeg);
  796. return HAL_BUSY;
  797. }
  798. }
  799. /**
  800. * @brief Modify the default Quantization tables used for JPEG encoding.
  801. * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
  802. * the configuration information for JPEG module
  803. * @param QTable0 pointer to uint8_t , define the user quantification table for color component 1.
  804. * If NULL assume no need to update the table and no error return
  805. * @param QTable1 pointer to uint8_t , define the user quantification table for color component 2.
  806. * If NULL assume no need to update the table and no error return.
  807. * @param QTable2 pointer to uint8_t , define the user quantification table for color component 3,
  808. * If NULL assume no need to update the table and no error return.
  809. * @param QTable3 pointer to uint8_t , define the user quantification table for color component 4.
  810. * If NULL assume no need to update the table and no error return.
  811. *
  812. * @retval HAL status
  813. */
  814. HAL_StatusTypeDef HAL_JPEG_SetUserQuantTables(JPEG_HandleTypeDef *hjpeg, uint8_t *QTable0, uint8_t *QTable1, uint8_t *QTable2, uint8_t *QTable3)
  815. {
  816. /* Process Locked */
  817. __HAL_LOCK(hjpeg);
  818. if(hjpeg->State == HAL_JPEG_STATE_READY)
  819. {
  820. /* Change the DMA state */
  821. hjpeg->State = HAL_JPEG_STATE_BUSY;
  822. hjpeg->Context |= JPEG_CONTEXT_CUSTOM_TABLES;
  823. hjpeg->QuantTable0 = QTable0;
  824. hjpeg->QuantTable1 = QTable1;
  825. hjpeg->QuantTable2 = QTable2;
  826. hjpeg->QuantTable3 = QTable3;
  827. /* Process Unlocked */
  828. __HAL_UNLOCK(hjpeg);
  829. /* Change the DMA state */
  830. hjpeg->State = HAL_JPEG_STATE_READY;
  831. /* Return function status */
  832. return HAL_OK;
  833. }
  834. else
  835. {
  836. /* Process Unlocked */
  837. __HAL_UNLOCK(hjpeg);
  838. return HAL_BUSY;
  839. }
  840. }
  841. /**
  842. * @}
  843. */
  844. /** @defgroup JPEG_Exported_Functions_Group3 encoding/decoding processing functions
  845. * @brief processing functions.
  846. *
  847. @verbatim
  848. ==============================================================================
  849. ##### JPEG processing functions #####
  850. ==============================================================================
  851. [..] This section provides functions allowing to:
  852. (+) HAL_JPEG_Encode() : JPEG encoding with polling process
  853. (+) HAL_JPEG_Decode() : JPEG decoding with polling process
  854. (+) HAL_JPEG_Encode_IT() : JPEG encoding with interrupt process
  855. (+) HAL_JPEG_Decode_IT() : JPEG decoding with interrupt process
  856. (+) HAL_JPEG_Encode_DMA() : JPEG encoding with DMA process
  857. (+) HAL_JPEG_Decode_DMA() : JPEG decoding with DMA process
  858. (+) HAL_JPEG_Pause() : Pause the Input/Output processing
  859. (+) HAL_JPEG_Resume() : Resume the JPEG Input/Output processing
  860. (+) HAL_JPEG_ConfigInputBuffer() : Config Encoding/Decoding Input Buffer
  861. (+) HAL_JPEG_ConfigOutputBuffer() : Config Encoding/Decoding Output Buffer
  862. (+) HAL_JPEG_Abort() : Aborts the JPEG Encoding/Decoding
  863. @endverbatim
  864. * @{
  865. */
  866. /**
  867. * @brief Starts JPEG encoding with polling processing
  868. * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
  869. * the configuration information for JPEG module
  870. * @param pDataInMCU Pointer to the Input buffer
  871. * @param InDataLength size in bytes Input buffer
  872. * @param pDataOut Pointer to the jpeg output data buffer
  873. * @param OutDataLength size in bytes of the Output buffer
  874. * @param Timeout Specify Timeout value
  875. * @retval HAL status
  876. */
  877. HAL_StatusTypeDef HAL_JPEG_Encode(JPEG_HandleTypeDef *hjpeg, uint8_t *pDataInMCU, uint32_t InDataLength, uint8_t *pDataOut, uint32_t OutDataLength, uint32_t Timeout)
  878. {
  879. uint32_t tickstart = 0;
  880. /* Check the parameters */
  881. assert_param((InDataLength >= 4));
  882. assert_param((OutDataLength >= 4));
  883. /* Check In/out buffer allocation and size */
  884. if((hjpeg == NULL) || (pDataInMCU == NULL) || (pDataOut == NULL) || \
  885. (InDataLength == 0) || (OutDataLength == 0))
  886. {
  887. return HAL_ERROR;
  888. }
  889. /* Process locked */
  890. __HAL_LOCK(hjpeg);
  891. if(hjpeg->State != HAL_JPEG_STATE_READY)
  892. {
  893. /* Process Unlocked */
  894. __HAL_UNLOCK(hjpeg);
  895. return HAL_BUSY;
  896. }
  897. if(hjpeg->State == HAL_JPEG_STATE_READY)
  898. {
  899. if((hjpeg->Context & JPEG_CONTEXT_CONF_ENCODING) == JPEG_CONTEXT_CONF_ENCODING )
  900. {
  901. /*Change JPEG state*/
  902. hjpeg->State = HAL_JPEG_STATE_BUSY_ENCODING;
  903. /*Set the Context to Encode with Polling*/
  904. hjpeg->Context &= ~(JPEG_CONTEXT_OPERATION_MASK | JPEG_CONTEXT_METHOD_MASK);
  905. hjpeg->Context |= (JPEG_CONTEXT_ENCODE | JPEG_CONTEXT_POLLING);
  906. /* Get tick */
  907. tickstart = HAL_GetTick();
  908. /*In/Out Data length must be multiple of 4 Bytes (1 word)*/
  909. InDataLength = InDataLength - (InDataLength % 4);
  910. OutDataLength = OutDataLength - (OutDataLength % 4);
  911. /*Store In/out buffers pointers and size*/
  912. hjpeg->pJpegInBuffPtr = pDataInMCU;
  913. hjpeg->pJpegOutBuffPtr = pDataOut;
  914. hjpeg->InDataLength = InDataLength;
  915. hjpeg->OutDataLength = OutDataLength;
  916. /*Reset In/out data counter */
  917. hjpeg->JpegInCount = 0;
  918. hjpeg->JpegOutCount = 0;
  919. /*Init decoding process*/
  920. JPEG_Init_Process(hjpeg);
  921. /*JPEG data processing : In/Out FIFO transfer*/
  922. while((JPEG_Process(hjpeg) == JPEG_PROCESS_ONGOING))
  923. {
  924. if(Timeout != HAL_MAX_DELAY)
  925. {
  926. if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
  927. {
  928. /* Update error code */
  929. hjpeg->ErrorCode |= HAL_JPEG_ERROR_TIMEOUT;
  930. /* Process Unlocked */
  931. __HAL_UNLOCK(hjpeg);
  932. /*Change JPEG state*/
  933. hjpeg->State= HAL_JPEG_STATE_READY;
  934. return HAL_TIMEOUT;
  935. }
  936. }
  937. }
  938. /* Process Unlocked */
  939. __HAL_UNLOCK(hjpeg);
  940. /*Change JPEG state*/
  941. hjpeg->State= HAL_JPEG_STATE_READY;
  942. }else
  943. {
  944. /* Process Unlocked */
  945. __HAL_UNLOCK(hjpeg);
  946. return HAL_ERROR;
  947. }
  948. }
  949. /* Return function status */
  950. return HAL_OK;
  951. }
  952. /**
  953. * @brief Starts JPEG decoding with polling processing
  954. * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
  955. * the configuration information for JPEG module
  956. * @param pDataIn Pointer to the input data buffer
  957. * @param InDataLength size in bytes Input buffer
  958. * @param pDataOutMCU Pointer to the Output data buffer
  959. * @param OutDataLength size in bytes of the Output buffer
  960. * @param Timeout Specify Timeout value
  961. * @retval HAL status
  962. */
  963. HAL_StatusTypeDef HAL_JPEG_Decode(JPEG_HandleTypeDef *hjpeg ,uint8_t *pDataIn ,uint32_t InDataLength ,uint8_t *pDataOutMCU ,uint32_t OutDataLength, uint32_t Timeout)
  964. {
  965. uint32_t tickstart = 0;
  966. /* Check the parameters */
  967. assert_param((InDataLength >= 4));
  968. assert_param((OutDataLength >= 4));
  969. /* Check In/out buffer allocation and size */
  970. if((hjpeg == NULL) || (pDataIn == NULL) || (pDataOutMCU == NULL) || \
  971. (InDataLength == 0) || (OutDataLength == 0))
  972. {
  973. return HAL_ERROR;
  974. }
  975. /* Process Locked */
  976. __HAL_LOCK(hjpeg);
  977. /* Get tick */
  978. tickstart = HAL_GetTick();
  979. if(hjpeg->State == HAL_JPEG_STATE_READY)
  980. {
  981. /*Change JPEG state*/
  982. hjpeg->State = HAL_JPEG_STATE_BUSY_DECODING;
  983. /*Set the Context to Decode with Polling*/
  984. /*Set the Context to Encode with Polling*/
  985. hjpeg->Context &= ~(JPEG_CONTEXT_OPERATION_MASK | JPEG_CONTEXT_METHOD_MASK);
  986. hjpeg->Context |= (JPEG_CONTEXT_DECODE | JPEG_CONTEXT_POLLING);
  987. /*In/Out Data length must be multiple of 4 Bytes (1 word)*/
  988. InDataLength = InDataLength - (InDataLength % 4);
  989. OutDataLength = OutDataLength - (OutDataLength % 4);
  990. /*Store In/out buffers pointers and size*/
  991. hjpeg->pJpegInBuffPtr = pDataIn;
  992. hjpeg->pJpegOutBuffPtr = pDataOutMCU;
  993. hjpeg->InDataLength = InDataLength;
  994. hjpeg->OutDataLength = OutDataLength;
  995. /*Reset In/out data counter */
  996. hjpeg->JpegInCount = 0;
  997. hjpeg->JpegOutCount = 0;
  998. /*Init decoding process*/
  999. JPEG_Init_Process(hjpeg);
  1000. /*JPEG data processing : In/Out FIFO transfer*/
  1001. while((JPEG_Process(hjpeg) == JPEG_PROCESS_ONGOING))
  1002. {
  1003. if(Timeout != HAL_MAX_DELAY)
  1004. {
  1005. if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
  1006. {
  1007. /* Update error code */
  1008. hjpeg->ErrorCode |= HAL_JPEG_ERROR_TIMEOUT;
  1009. /* Process Unlocked */
  1010. __HAL_UNLOCK(hjpeg);
  1011. /*Change JPEG state*/
  1012. hjpeg->State= HAL_JPEG_STATE_READY;
  1013. return HAL_TIMEOUT;
  1014. }
  1015. }
  1016. }
  1017. /* Process Unlocked */
  1018. __HAL_UNLOCK(hjpeg);
  1019. /*Change JPEG state*/
  1020. hjpeg->State= HAL_JPEG_STATE_READY;
  1021. }else
  1022. {
  1023. /* Process Unlocked */
  1024. __HAL_UNLOCK(hjpeg);
  1025. return HAL_BUSY;
  1026. }
  1027. /* Return function status */
  1028. return HAL_OK;
  1029. }
  1030. /**
  1031. * @brief Starts JPEG encoding with interrupt processing
  1032. * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
  1033. * the configuration information for JPEG module
  1034. * @param pDataInMCU Pointer to the Input buffer
  1035. * @param InDataLength size in bytes Input buffer
  1036. * @param pDataOut Pointer to the jpeg output data buffer
  1037. * @param OutDataLength size in bytes of the Output buffer
  1038. * @retval HAL status
  1039. */
  1040. HAL_StatusTypeDef HAL_JPEG_Encode_IT(JPEG_HandleTypeDef *hjpeg, uint8_t *pDataInMCU, uint32_t InDataLength, uint8_t *pDataOut, uint32_t OutDataLength)
  1041. {
  1042. /* Check the parameters */
  1043. assert_param((InDataLength >= 4));
  1044. assert_param((OutDataLength >= 4));
  1045. /* Check In/out buffer allocation and size */
  1046. if((hjpeg == NULL) || (pDataInMCU == NULL) || (pDataOut == NULL) || \
  1047. (InDataLength == 0) || (OutDataLength == 0))
  1048. {
  1049. return HAL_ERROR;
  1050. }
  1051. /* Process Locked */
  1052. __HAL_LOCK(hjpeg);
  1053. if(hjpeg->State != HAL_JPEG_STATE_READY)
  1054. {
  1055. /* Process Unlocked */
  1056. __HAL_UNLOCK(hjpeg);
  1057. return HAL_BUSY;
  1058. }
  1059. else
  1060. {
  1061. if((hjpeg->Context & JPEG_CONTEXT_CONF_ENCODING) == JPEG_CONTEXT_CONF_ENCODING )
  1062. {
  1063. /*Change JPEG state*/
  1064. hjpeg->State = HAL_JPEG_STATE_BUSY_ENCODING;
  1065. /*Set the Context to Encode with IT*/
  1066. hjpeg->Context &= ~(JPEG_CONTEXT_OPERATION_MASK | JPEG_CONTEXT_METHOD_MASK);
  1067. hjpeg->Context |= (JPEG_CONTEXT_ENCODE | JPEG_CONTEXT_IT);
  1068. /*In/Out Data length must be multiple of 4 Bytes (1 word)*/
  1069. InDataLength = InDataLength - (InDataLength % 4);
  1070. OutDataLength = OutDataLength - (OutDataLength % 4);
  1071. /*Store In/out buffers pointers and size*/
  1072. hjpeg->pJpegInBuffPtr = pDataInMCU;
  1073. hjpeg->pJpegOutBuffPtr = pDataOut;
  1074. hjpeg->InDataLength = InDataLength;
  1075. hjpeg->OutDataLength = OutDataLength;
  1076. /*Reset In/out data counter */
  1077. hjpeg->JpegInCount = 0;
  1078. hjpeg->JpegOutCount = 0;
  1079. /*Init decoding process*/
  1080. JPEG_Init_Process(hjpeg);
  1081. }
  1082. else
  1083. {
  1084. /* Process Unlocked */
  1085. __HAL_UNLOCK(hjpeg);
  1086. return HAL_ERROR;
  1087. }
  1088. }
  1089. /* Return function status */
  1090. return HAL_OK;
  1091. }
  1092. /**
  1093. * @brief Starts JPEG decoding with interrupt processing
  1094. * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
  1095. * the configuration information for JPEG module
  1096. * @param pDataIn Pointer to the input data buffer
  1097. * @param InDataLength size in bytes Input buffer
  1098. * @param pDataOutMCU Pointer to the Output data buffer
  1099. * @param OutDataLength size in bytes of the Output buffer
  1100. * @retval HAL status
  1101. */
  1102. HAL_StatusTypeDef HAL_JPEG_Decode_IT(JPEG_HandleTypeDef *hjpeg ,uint8_t *pDataIn ,uint32_t InDataLength ,uint8_t *pDataOutMCU ,uint32_t OutDataLength)
  1103. {
  1104. /* Check the parameters */
  1105. assert_param((InDataLength >= 4));
  1106. assert_param((OutDataLength >= 4));
  1107. /* Check In/out buffer allocation and size */
  1108. if((hjpeg == NULL) || (pDataIn == NULL) || (pDataOutMCU == NULL) || \
  1109. (InDataLength == 0) || (OutDataLength == 0))
  1110. {
  1111. return HAL_ERROR;
  1112. }
  1113. /* Process Locked */
  1114. __HAL_LOCK(hjpeg);
  1115. if(hjpeg->State == HAL_JPEG_STATE_READY)
  1116. {
  1117. /*Change JPEG state*/
  1118. hjpeg->State = HAL_JPEG_STATE_BUSY_DECODING;
  1119. /*Set the Context to Decode with IT*/
  1120. hjpeg->Context &= ~(JPEG_CONTEXT_OPERATION_MASK | JPEG_CONTEXT_METHOD_MASK);
  1121. hjpeg->Context |= (JPEG_CONTEXT_DECODE | JPEG_CONTEXT_IT);
  1122. /*In/Out Data length must be multiple of 4 Bytes (1 word)*/
  1123. InDataLength = InDataLength - (InDataLength % 4);
  1124. OutDataLength = OutDataLength - (OutDataLength % 4);
  1125. /*Store In/out buffers pointers and size*/
  1126. hjpeg->pJpegInBuffPtr = pDataIn;
  1127. hjpeg->pJpegOutBuffPtr = pDataOutMCU;
  1128. hjpeg->InDataLength = InDataLength;
  1129. hjpeg->OutDataLength = OutDataLength;
  1130. /*Reset In/out data counter */
  1131. hjpeg->JpegInCount = 0;
  1132. hjpeg->JpegOutCount = 0;
  1133. /*Init decoding process*/
  1134. JPEG_Init_Process(hjpeg);
  1135. }
  1136. else
  1137. {
  1138. /* Process Unlocked */
  1139. __HAL_UNLOCK(hjpeg);
  1140. return HAL_BUSY;
  1141. }
  1142. /* Return function status */
  1143. return HAL_OK;
  1144. }
  1145. /**
  1146. * @brief Starts JPEG encoding with DMA processing
  1147. * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
  1148. * the configuration information for JPEG module
  1149. * @param pDataInMCU Pointer to the Input buffer
  1150. * @param InDataLength size in bytes Input buffer
  1151. * @param pDataOut Pointer to the jpeg output data buffer
  1152. * @param OutDataLength size in bytes of the Output buffer
  1153. * @retval HAL status
  1154. */
  1155. HAL_StatusTypeDef HAL_JPEG_Encode_DMA(JPEG_HandleTypeDef *hjpeg, uint8_t *pDataInMCU, uint32_t InDataLength, uint8_t *pDataOut, uint32_t OutDataLength)
  1156. {
  1157. /* Check the parameters */
  1158. assert_param((InDataLength >= 4));
  1159. assert_param((OutDataLength >= 4));
  1160. /* Check In/out buffer allocation and size */
  1161. if((hjpeg == NULL) || (pDataInMCU == NULL) || (pDataOut == NULL) || \
  1162. (InDataLength == 0) || (OutDataLength == 0))
  1163. {
  1164. return HAL_ERROR;
  1165. }
  1166. /* Process Locked */
  1167. __HAL_LOCK(hjpeg);
  1168. if(hjpeg->State != HAL_JPEG_STATE_READY)
  1169. {
  1170. /* Process Unlocked */
  1171. __HAL_UNLOCK(hjpeg);
  1172. return HAL_BUSY;
  1173. }
  1174. else
  1175. {
  1176. if((hjpeg->Context & JPEG_CONTEXT_CONF_ENCODING) == JPEG_CONTEXT_CONF_ENCODING )
  1177. {
  1178. /*Change JPEG state*/
  1179. hjpeg->State = HAL_JPEG_STATE_BUSY_ENCODING;
  1180. /*Set the Context to Encode with DMA*/
  1181. hjpeg->Context &= ~(JPEG_CONTEXT_OPERATION_MASK | JPEG_CONTEXT_METHOD_MASK);
  1182. hjpeg->Context |= (JPEG_CONTEXT_ENCODE | JPEG_CONTEXT_DMA);
  1183. /*Store In/out buffers pointers and size*/
  1184. hjpeg->pJpegInBuffPtr = pDataInMCU;
  1185. hjpeg->pJpegOutBuffPtr = pDataOut;
  1186. hjpeg->InDataLength = InDataLength;
  1187. hjpeg->OutDataLength = OutDataLength;
  1188. /*Reset In/out data counter */
  1189. hjpeg->JpegInCount = 0;
  1190. hjpeg->JpegOutCount = 0;
  1191. /*Init decoding process*/
  1192. JPEG_Init_Process(hjpeg);
  1193. /* JPEG encoding process using DMA */
  1194. JPEG_DMA_StartProcess(hjpeg);
  1195. }
  1196. else
  1197. {
  1198. /* Process Unlocked */
  1199. __HAL_UNLOCK(hjpeg);
  1200. return HAL_ERROR;
  1201. }
  1202. }
  1203. /* Return function status */
  1204. return HAL_OK;
  1205. }
  1206. /**
  1207. * @brief Starts JPEG decoding with DMA processing
  1208. * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
  1209. * the configuration information for JPEG module
  1210. * @param pDataIn Pointer to the input data buffer
  1211. * @param InDataLength size in bytes Input buffer
  1212. * @param pDataOutMCU Pointer to the Output data buffer
  1213. * @param OutDataLength size in bytes of the Output buffer
  1214. * @retval HAL status
  1215. */
  1216. HAL_StatusTypeDef HAL_JPEG_Decode_DMA(JPEG_HandleTypeDef *hjpeg ,uint8_t *pDataIn ,uint32_t InDataLength ,uint8_t *pDataOutMCU ,uint32_t OutDataLength)
  1217. {
  1218. /* Check the parameters */
  1219. assert_param((InDataLength >= 4));
  1220. assert_param((OutDataLength >= 4));
  1221. /* Check In/out buffer allocation and size */
  1222. if((hjpeg == NULL) || (pDataIn == NULL) || (pDataOutMCU == NULL) || \
  1223. (InDataLength == 0) || (OutDataLength == 0))
  1224. {
  1225. return HAL_ERROR;
  1226. }
  1227. /* Process Locked */
  1228. __HAL_LOCK(hjpeg);
  1229. if(hjpeg->State == HAL_JPEG_STATE_READY)
  1230. {
  1231. /*Change JPEG state*/
  1232. hjpeg->State = HAL_JPEG_STATE_BUSY_DECODING;
  1233. /*Set the Context to Decode with DMA*/
  1234. hjpeg->Context &= ~(JPEG_CONTEXT_OPERATION_MASK | JPEG_CONTEXT_METHOD_MASK);
  1235. hjpeg->Context |= (JPEG_CONTEXT_DECODE | JPEG_CONTEXT_DMA);
  1236. /*Store In/out buffers pointers and size*/
  1237. hjpeg->pJpegInBuffPtr = pDataIn;
  1238. hjpeg->pJpegOutBuffPtr = pDataOutMCU;
  1239. hjpeg->InDataLength = InDataLength;
  1240. hjpeg->OutDataLength = OutDataLength;
  1241. /*Reset In/out data counter */
  1242. hjpeg->JpegInCount = 0;
  1243. hjpeg->JpegOutCount = 0;
  1244. /*Init decoding process*/
  1245. JPEG_Init_Process(hjpeg);
  1246. /* JPEG decoding process using DMA */
  1247. JPEG_DMA_StartProcess(hjpeg);
  1248. }
  1249. else
  1250. {
  1251. /* Process Unlocked */
  1252. __HAL_UNLOCK(hjpeg);
  1253. return HAL_BUSY;
  1254. }
  1255. /* Return function status */
  1256. return HAL_OK;
  1257. }
  1258. /**
  1259. * @brief Pause the JPEG Input/Output processing
  1260. * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
  1261. * the configuration information for JPEG module
  1262. * @param XferSelection This parameter can be one of the following values :
  1263. * JPEG_PAUSE_RESUME_INPUT : Pause Input processing
  1264. * JPEG_PAUSE_RESUME_OUTPUT: Pause Output processing
  1265. * JPEG_PAUSE_RESUME_INPUT_OUTPUT: Pause Input and Output processing
  1266. * @retval HAL status
  1267. */
  1268. HAL_StatusTypeDef HAL_JPEG_Pause(JPEG_HandleTypeDef *hjpeg, uint32_t XferSelection)
  1269. {
  1270. uint32_t mask = 0;
  1271. assert_param(IS_JPEG_PAUSE_RESUME_STATE(XferSelection));
  1272. if((hjpeg->Context & JPEG_CONTEXT_METHOD_MASK) == JPEG_CONTEXT_DMA)
  1273. {
  1274. if((XferSelection & JPEG_PAUSE_RESUME_INPUT) == JPEG_PAUSE_RESUME_INPUT)
  1275. {
  1276. hjpeg->Context |= JPEG_CONTEXT_PAUSE_INPUT;
  1277. mask |= JPEG_DMA_IDMA;
  1278. }
  1279. if((XferSelection & JPEG_PAUSE_RESUME_OUTPUT) == JPEG_PAUSE_RESUME_OUTPUT)
  1280. {
  1281. hjpeg->Context |= JPEG_CONTEXT_PAUSE_OUTPUT;
  1282. mask |= JPEG_DMA_ODMA;
  1283. }
  1284. JPEG_DISABLE_DMA(hjpeg,mask);
  1285. }
  1286. else if((hjpeg->Context & JPEG_CONTEXT_METHOD_MASK) == JPEG_CONTEXT_IT)
  1287. {
  1288. if((XferSelection & JPEG_PAUSE_RESUME_INPUT) == JPEG_PAUSE_RESUME_INPUT)
  1289. {
  1290. hjpeg->Context |= JPEG_CONTEXT_PAUSE_INPUT;
  1291. mask |= (JPEG_IT_IFT | JPEG_IT_IFNF);
  1292. }
  1293. if((XferSelection & JPEG_PAUSE_RESUME_OUTPUT) == JPEG_PAUSE_RESUME_OUTPUT)
  1294. {
  1295. hjpeg->Context |= JPEG_CONTEXT_PAUSE_OUTPUT;
  1296. mask |= (JPEG_IT_OFT | JPEG_IT_OFNE | JPEG_IT_EOC);
  1297. }
  1298. __HAL_JPEG_DISABLE_IT(hjpeg,mask);
  1299. }
  1300. /* Return function status */
  1301. return HAL_OK;
  1302. }
  1303. /**
  1304. * @brief Resume the JPEG Input/Output processing
  1305. * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
  1306. * the configuration information for JPEG module
  1307. * @param XferSelection This parameter can be one of the following values :
  1308. * JPEG_PAUSE_RESUME_INPUT : Resume Input processing
  1309. * JPEG_PAUSE_RESUME_OUTPUT: Resume Output processing
  1310. * JPEG_PAUSE_RESUME_INPUT_OUTPUT: Resume Input and Output processing
  1311. * @retval HAL status
  1312. */
  1313. HAL_StatusTypeDef HAL_JPEG_Resume(JPEG_HandleTypeDef *hjpeg, uint32_t XferSelection)
  1314. {
  1315. uint32_t mask = 0;
  1316. assert_param(IS_JPEG_PAUSE_RESUME_STATE(XferSelection));
  1317. if(((hjpeg->Context & JPEG_CONTEXT_PAUSE_INPUT) == 0) && ((hjpeg->Context & JPEG_CONTEXT_PAUSE_OUTPUT) == 0))
  1318. {
  1319. /* if nothing paused to resume return error*/
  1320. return HAL_ERROR;
  1321. }
  1322. if((hjpeg->Context & JPEG_CONTEXT_METHOD_MASK) == JPEG_CONTEXT_DMA)
  1323. {
  1324. if((XferSelection & JPEG_PAUSE_RESUME_INPUT) == JPEG_PAUSE_RESUME_INPUT)
  1325. {
  1326. hjpeg->Context &= (~JPEG_CONTEXT_PAUSE_INPUT);
  1327. mask |= JPEG_DMA_IDMA;
  1328. /*JPEG Input DMA transfer data number must be multiple of DMA buffer size
  1329. as the destination is a 32 bits register */
  1330. hjpeg->InDataLength = hjpeg->InDataLength - (hjpeg->InDataLength % 4);
  1331. if(hjpeg->InDataLength > 0)
  1332. {
  1333. /* Start DMA FIFO In transfer */
  1334. HAL_DMA_Start_IT(hjpeg->hdmain, (uint32_t)hjpeg->pJpegInBuffPtr, (uint32_t)&hjpeg->Instance->DIR, hjpeg->InDataLength >> 2);
  1335. }
  1336. }
  1337. if((XferSelection & JPEG_PAUSE_RESUME_OUTPUT) == JPEG_PAUSE_RESUME_OUTPUT)
  1338. {
  1339. if((hjpeg->Context & JPEG_CONTEXT_ENDING_DMA) != 0)
  1340. {
  1341. JPEG_DMA_PollResidualData(hjpeg);
  1342. }
  1343. else
  1344. {
  1345. hjpeg->Context &= (~JPEG_CONTEXT_PAUSE_OUTPUT);
  1346. mask |= JPEG_DMA_ODMA;
  1347. /* Start DMA FIFO Out transfer */
  1348. HAL_DMA_Start_IT(hjpeg->hdmaout, (uint32_t)&hjpeg->Instance->DOR, (uint32_t)hjpeg->pJpegOutBuffPtr, hjpeg->OutDataLength >> 2);
  1349. }
  1350. }
  1351. JPEG_ENABLE_DMA(hjpeg,mask);
  1352. }
  1353. else if((hjpeg->Context & JPEG_CONTEXT_METHOD_MASK) == JPEG_CONTEXT_IT)
  1354. {
  1355. if((XferSelection & JPEG_PAUSE_RESUME_INPUT) == JPEG_PAUSE_RESUME_INPUT)
  1356. {
  1357. hjpeg->Context &= (~JPEG_CONTEXT_PAUSE_INPUT);
  1358. mask |= (JPEG_IT_IFT | JPEG_IT_IFNF);
  1359. }
  1360. if((XferSelection & JPEG_PAUSE_RESUME_OUTPUT) == JPEG_PAUSE_RESUME_OUTPUT)
  1361. {
  1362. hjpeg->Context &= (~JPEG_CONTEXT_PAUSE_OUTPUT);
  1363. mask |= (JPEG_IT_OFT | JPEG_IT_OFNE | JPEG_IT_EOC);
  1364. }
  1365. __HAL_JPEG_ENABLE_IT(hjpeg,mask);
  1366. }
  1367. /* Return function status */
  1368. return HAL_OK;
  1369. }
  1370. /**
  1371. * @brief Config Encoding/Decoding Input Buffer.
  1372. * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
  1373. * the configuration information for JPEG module.
  1374. * @param pNewInputBuffer Pointer to the new input data buffer
  1375. * @param InDataLength Size in bytes of the new Input data buffer
  1376. * @retval HAL status
  1377. */
  1378. void HAL_JPEG_ConfigInputBuffer(JPEG_HandleTypeDef *hjpeg, uint8_t *pNewInputBuffer, uint32_t InDataLength)
  1379. {
  1380. hjpeg->pJpegInBuffPtr = pNewInputBuffer;
  1381. hjpeg->InDataLength = InDataLength;
  1382. }
  1383. /**
  1384. * @brief Config Encoding/Decoding Output Buffer.
  1385. * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
  1386. * the configuration information for JPEG module.
  1387. * @param pNewOutputBuffer Pointer to the new output data buffer
  1388. * @param OutDataLength Size in bytes of the new Output data buffer
  1389. * @retval HAL status
  1390. */
  1391. void HAL_JPEG_ConfigOutputBuffer(JPEG_HandleTypeDef *hjpeg, uint8_t *pNewOutputBuffer, uint32_t OutDataLength)
  1392. {
  1393. hjpeg->pJpegOutBuffPtr = pNewOutputBuffer;
  1394. hjpeg->OutDataLength = OutDataLength;
  1395. }
  1396. /**
  1397. * @brief Aborts the JPEG Encoding/Decoding.
  1398. * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
  1399. * the configuration information for JPEG module
  1400. * @retval HAL status
  1401. */
  1402. HAL_StatusTypeDef HAL_JPEG_Abort(JPEG_HandleTypeDef *hjpeg)
  1403. {
  1404. uint32_t tickstart, tmpContext;
  1405. tmpContext = hjpeg->Context;
  1406. /*Reset the Context operation and method*/
  1407. hjpeg->Context &= ~(JPEG_CONTEXT_OPERATION_MASK | JPEG_CONTEXT_METHOD_MASK | JPEG_CONTEXT_ENDING_DMA);
  1408. if((tmpContext & JPEG_CONTEXT_METHOD_MASK) == JPEG_CONTEXT_DMA)
  1409. {
  1410. /* Stop the DMA In/out Xfer*/
  1411. HAL_DMA_Abort(hjpeg->hdmaout);
  1412. HAL_DMA_Abort(hjpeg->hdmain);
  1413. }
  1414. /* Stop the JPEG encoding/decoding process*/
  1415. hjpeg->Instance->CONFR0 &= ~JPEG_CONFR0_START;
  1416. /* Get tick */
  1417. tickstart = HAL_GetTick();
  1418. /* Check if the JPEG Codec is effectively disabled */
  1419. while(__HAL_JPEG_GET_FLAG(hjpeg, JPEG_FLAG_COF) != RESET)
  1420. {
  1421. /* Check for the Timeout */
  1422. if((HAL_GetTick() - tickstart ) > JPEG_TIMEOUT_VALUE)
  1423. {
  1424. /* Update error code */
  1425. hjpeg->ErrorCode |= HAL_JPEG_ERROR_TIMEOUT;
  1426. /* Change the DMA state */
  1427. hjpeg->State = HAL_JPEG_STATE_TIMEOUT;
  1428. /* Process Unlocked */
  1429. __HAL_UNLOCK(hjpeg);
  1430. return HAL_TIMEOUT;
  1431. }
  1432. }
  1433. /* Disable All Interrupts */
  1434. __HAL_JPEG_DISABLE_IT(hjpeg,JPEG_INTERRUPT_MASK);
  1435. /* Disable All DMA requests */
  1436. JPEG_DISABLE_DMA(hjpeg,JPEG_DMA_MASK);
  1437. /* Flush input and output FIFOs*/
  1438. hjpeg->Instance->CR |= JPEG_CR_IFF;
  1439. hjpeg->Instance->CR |= JPEG_CR_OFF;
  1440. /* Clear all flags */
  1441. __HAL_JPEG_CLEAR_FLAG(hjpeg,JPEG_FLAG_ALL);
  1442. /* Reset JpegInCount and JpegOutCount */
  1443. hjpeg->JpegInCount = 0;
  1444. hjpeg->JpegOutCount = 0;
  1445. /*Reset the Context Pause*/
  1446. hjpeg->Context &= ~(JPEG_CONTEXT_PAUSE_INPUT | JPEG_CONTEXT_PAUSE_OUTPUT);
  1447. /* Change the DMA state*/
  1448. hjpeg->State = HAL_JPEG_STATE_READY;
  1449. /* Process Unlocked */
  1450. __HAL_UNLOCK(hjpeg);
  1451. /* Return function status */
  1452. return HAL_OK;
  1453. }
  1454. /**
  1455. * @}
  1456. */
  1457. /** @defgroup JPEG_Exported_Functions_Group4 JPEG Decode/Encode callback functions
  1458. * @brief JPEG process callback functions.
  1459. *
  1460. @verbatim
  1461. ==============================================================================
  1462. ##### JPEG Decode and Encode callback functions #####
  1463. ==============================================================================
  1464. [..] This section provides callback functions:
  1465. (+) HAL_JPEG_InfoReadyCallback() : Decoding JPEG Info ready callback
  1466. (+) HAL_JPEG_EncodeCpltCallback() : Encoding complete callback.
  1467. (+) HAL_JPEG_DecodeCpltCallback() : Decoding complete callback.
  1468. (+) HAL_JPEG_ErrorCallback() : JPEG error callback.
  1469. (+) HAL_JPEG_GetDataCallback() : Get New Data chunk callback.
  1470. (+) HAL_JPEG_DataReadyCallback() : Decoded/Encoded Data ready callback.
  1471. @endverbatim
  1472. * @{
  1473. */
  1474. /**
  1475. * @brief Decoding JPEG Info ready callback.
  1476. * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
  1477. * the configuration information for JPEG module
  1478. * @param pInfo pointer to a JPEG_ConfTypeDef structure that contains
  1479. * The JPEG decoded header informations
  1480. * @retval None
  1481. */
  1482. __weak void HAL_JPEG_InfoReadyCallback(JPEG_HandleTypeDef *hjpeg,JPEG_ConfTypeDef *pInfo)
  1483. {
  1484. /* Prevent unused argument(s) compilation warning */
  1485. UNUSED(hjpeg);
  1486. UNUSED(pInfo);
  1487. /* NOTE : This function Should not be modified, when the callback is needed,
  1488. the HAL_JPEG_HeaderParsingCpltCallback could be implemented in the user file
  1489. */
  1490. }
  1491. /**
  1492. * @brief Encoding complete callback.
  1493. * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
  1494. * the configuration information for JPEG module
  1495. * @retval None
  1496. */
  1497. __weak void HAL_JPEG_EncodeCpltCallback(JPEG_HandleTypeDef *hjpeg)
  1498. {
  1499. /* Prevent unused argument(s) compilation warning */
  1500. UNUSED(hjpeg);
  1501. /* NOTE : This function Should not be modified, when the callback is needed,
  1502. the HAL_JPEG_EncodeCpltCallback could be implemented in the user file
  1503. */
  1504. }
  1505. /**
  1506. * @brief Decoding complete callback.
  1507. * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
  1508. * the configuration information for JPEG module
  1509. * @retval None
  1510. */
  1511. __weak void HAL_JPEG_DecodeCpltCallback(JPEG_HandleTypeDef *hjpeg)
  1512. {
  1513. /* Prevent unused argument(s) compilation warning */
  1514. UNUSED(hjpeg);
  1515. /* NOTE : This function Should not be modified, when the callback is needed,
  1516. the HAL_JPEG_EncodeCpltCallback could be implemented in the user file
  1517. */
  1518. }
  1519. /**
  1520. * @brief JPEG error callback.
  1521. * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
  1522. * the configuration information for JPEG module
  1523. * @retval None
  1524. */
  1525. __weak void HAL_JPEG_ErrorCallback(JPEG_HandleTypeDef *hjpeg)
  1526. {
  1527. /* Prevent unused argument(s) compilation warning */
  1528. UNUSED(hjpeg);
  1529. /* NOTE : This function Should not be modified, when the callback is needed,
  1530. the HAL_JPEG_ErrorCallback could be implemented in the user file
  1531. */
  1532. }
  1533. /**
  1534. * @brief Get New Data chunk callback.
  1535. * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
  1536. * the configuration information for JPEG module
  1537. * @param NbDecodedData Number of consummed data in the previous chunk in bytes
  1538. * @retval None
  1539. */
  1540. __weak void HAL_JPEG_GetDataCallback(JPEG_HandleTypeDef *hjpeg, uint32_t NbDecodedData)
  1541. {
  1542. /* Prevent unused argument(s) compilation warning */
  1543. UNUSED(hjpeg);
  1544. UNUSED(NbDecodedData);
  1545. /* NOTE : This function Should not be modified, when the callback is needed,
  1546. the HAL_JPEG_GetDataCallback could be implemented in the user file
  1547. */
  1548. }
  1549. /**
  1550. * @brief Decoded/Encoded Data ready callback.
  1551. * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
  1552. * the configuration information for JPEG module
  1553. * @param pDataOut pointer to the output data buffer
  1554. * @param OutDataLength number in bytes of data available in the specified output buffer
  1555. * @retval None
  1556. */
  1557. __weak void HAL_JPEG_DataReadyCallback (JPEG_HandleTypeDef *hjpeg, uint8_t *pDataOut, uint32_t OutDataLength)
  1558. {
  1559. /* Prevent unused argument(s) compilation warning */
  1560. UNUSED(hjpeg);
  1561. UNUSED(pDataOut);
  1562. UNUSED(OutDataLength);
  1563. /* NOTE : This function Should not be modified, when the callback is needed,
  1564. the HAL_JPEG_DataReadyCallback could be implemented in the user file
  1565. */
  1566. }
  1567. /**
  1568. * @}
  1569. */
  1570. /** @defgroup JPEG_Exported_Functions_Group5 JPEG IRQ handler management
  1571. * @brief JPEG IRQ handler.
  1572. *
  1573. @verbatim
  1574. ==============================================================================
  1575. ##### JPEG IRQ handler management #####
  1576. ==============================================================================
  1577. [..] This section provides JPEG IRQ handler function.
  1578. (+) HAL_JPEG_IRQHandler() : handles JPEG interrupt request
  1579. @endverbatim
  1580. * @{
  1581. */
  1582. /**
  1583. * @brief This function handles JPEG interrupt request.
  1584. * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
  1585. * the configuration information for JPEG module
  1586. * @retval None
  1587. */
  1588. void HAL_JPEG_IRQHandler(JPEG_HandleTypeDef *hjpeg)
  1589. {
  1590. switch(hjpeg->State)
  1591. {
  1592. case HAL_JPEG_STATE_BUSY_ENCODING:
  1593. case HAL_JPEG_STATE_BUSY_DECODING:
  1594. /* continue JPEG data encoding/Decoding*/
  1595. /* JPEG data processing : In/Out FIFO transfer*/
  1596. if((hjpeg->Context & JPEG_CONTEXT_METHOD_MASK) == JPEG_CONTEXT_IT)
  1597. {
  1598. JPEG_Process(hjpeg);
  1599. }
  1600. else if((hjpeg->Context & JPEG_CONTEXT_METHOD_MASK) == JPEG_CONTEXT_DMA)
  1601. {
  1602. JPEG_DMA_ContinueProcess(hjpeg);
  1603. }
  1604. break;
  1605. default:
  1606. break;
  1607. }
  1608. }
  1609. /**
  1610. * @}
  1611. */
  1612. /** @defgroup JPEG_Exported_Functions_Group6 Peripheral State functions
  1613. * @brief Peripheral State functions.
  1614. *
  1615. @verbatim
  1616. ==============================================================================
  1617. ##### Peripheral State and Error functions #####
  1618. ==============================================================================
  1619. [..] This section provides JPEG State and Errors function.
  1620. (+) HAL_JPEG_GetState() : permits to get in run-time the JPEG state.
  1621. (+) HAL_JPEG_GetError() : Returns the JPEG error code if any.
  1622. @endverbatim
  1623. * @{
  1624. */
  1625. /**
  1626. * @brief Returns the JPEG state.
  1627. * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
  1628. * the configuration information for JPEG module
  1629. * @retval JPEG state
  1630. */
  1631. HAL_JPEG_STATETypeDef HAL_JPEG_GetState(JPEG_HandleTypeDef *hjpeg)
  1632. {
  1633. return hjpeg->State;
  1634. }
  1635. /**
  1636. * @brief Return the JPEG error code
  1637. * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
  1638. * the configuration information for the specified JPEG.
  1639. * @retval JPEG Error Code
  1640. */
  1641. uint32_t HAL_JPEG_GetError(JPEG_HandleTypeDef *hjpeg)
  1642. {
  1643. return hjpeg->ErrorCode;
  1644. }
  1645. /**
  1646. * @}
  1647. */
  1648. /**
  1649. * @}
  1650. */
  1651. /** @addtogroup JPEG_Private_Functions
  1652. * @{
  1653. */
  1654. /**
  1655. * @brief Generates Huffman sizes/Codes Table from Bits/vals Table
  1656. * @param Bits pointer to bits table
  1657. * @param Huffsize pointer to sizes table
  1658. * @param Huffcode pointer to codes table
  1659. * @param LastK pointer to last Coeff (table dimmension)
  1660. * @retval HAL status
  1661. */
  1662. static HAL_StatusTypeDef JPEG_Bits_To_SizeCodes(uint8_t *Bits, uint8_t *Huffsize, uint32_t *Huffcode, uint32_t *LastK)
  1663. {
  1664. uint32_t i, p, l, code, si;
  1665. /* Figure C.1: Generation of table of Huffman code sizes */
  1666. p = 0;
  1667. for (l = 0; l < 16; l++)
  1668. {
  1669. i = (uint32_t)Bits[l];
  1670. if ( (p + i) > 256)
  1671. { /* check for table overflow */
  1672. return HAL_ERROR;
  1673. }
  1674. while (i != 0)
  1675. {
  1676. Huffsize[p++] = (uint8_t) l+1;
  1677. i--;
  1678. }
  1679. }
  1680. Huffsize[p] = 0;
  1681. *LastK = p;
  1682. /* Figure C.2: Generation of table of Huffman codes */
  1683. code = 0;
  1684. si = Huffsize[0];
  1685. p = 0;
  1686. while (Huffsize[p] != 0)
  1687. {
  1688. while (((uint32_t) Huffsize[p]) == si)
  1689. {
  1690. Huffcode[p++] = code;
  1691. code++;
  1692. }
  1693. /* code must fit in "size" bits (si), no code is allowed to be all ones*/
  1694. if (((uint32_t) code) >= (((uint32_t) 1) << si))
  1695. {
  1696. return HAL_ERROR;
  1697. }
  1698. code <<= 1;
  1699. si++;
  1700. }
  1701. /* Return function status */
  1702. return HAL_OK;
  1703. }
  1704. /**
  1705. * @brief Transform a Bits/Vals AC Huffman table to sizes/Codes huffman Table
  1706. * that can programmed to the JPEG encoder registers
  1707. * @param AC_BitsValsTable pointer to AC huffman bits/vals table
  1708. * @param AC_SizeCodesTable pointer to AC huffman Sizes/Codes table
  1709. * @retval HAL status
  1710. */
  1711. static HAL_StatusTypeDef JPEG_ACHuff_BitsVals_To_SizeCodes(JPEG_ACHuffTableTypeDef *AC_BitsValsTable, JPEG_AC_HuffCodeTableTypeDef *AC_SizeCodesTable)
  1712. {
  1713. HAL_StatusTypeDef error;
  1714. uint8_t huffsize[257];
  1715. uint32_t huffcode[257];
  1716. uint32_t k;
  1717. uint32_t l,lsb, msb;
  1718. uint32_t lastK;
  1719. error = JPEG_Bits_To_SizeCodes(AC_BitsValsTable->Bits, huffsize, huffcode, &lastK);
  1720. if(error != HAL_OK)
  1721. {
  1722. return error;
  1723. }
  1724. /* Figure C.3: Ordering procedure for encoding procedure code tables */
  1725. k=0;
  1726. while(k < lastK)
  1727. {
  1728. l = AC_BitsValsTable->HuffVal[k];
  1729. if(l == 0)
  1730. {
  1731. l = 160; /*l = 0x00 EOB code*/
  1732. }
  1733. else if(l == 0xF0)/* l = 0xF0 ZRL code*/
  1734. {
  1735. l = 161;
  1736. }
  1737. else
  1738. {
  1739. msb = (l & 0xF0) >> 4;
  1740. lsb = (l & 0x0F);
  1741. l = (msb * 10) + lsb - 1;
  1742. }
  1743. if(l >= JPEG_AC_HUFF_TABLE_SIZE)
  1744. {
  1745. return HAL_ERROR; /* Huffman Table overflow error*/
  1746. }
  1747. else
  1748. {
  1749. AC_SizeCodesTable->HuffmanCode[l] = huffcode[k];
  1750. AC_SizeCodesTable->CodeLength[l] = huffsize[k] - 1;
  1751. k++;
  1752. }
  1753. }
  1754. /* Return function status */
  1755. return HAL_OK;
  1756. }
  1757. /**
  1758. * @brief Transform a Bits/Vals DC Huffman table to sizes/Codes huffman Table
  1759. * that can programmed to the JPEG encoder registers
  1760. * @param DC_BitsValsTable pointer to DC huffman bits/vals table
  1761. * @param DC_SizeCodesTable pointer to DC huffman Sizes/Codes table
  1762. * @retval HAL status
  1763. */
  1764. static HAL_StatusTypeDef JPEG_DCHuff_BitsVals_To_SizeCodes(JPEG_DCHuffTableTypeDef *DC_BitsValsTable, JPEG_DC_HuffCodeTableTypeDef *DC_SizeCodesTable)
  1765. {
  1766. HAL_StatusTypeDef error;
  1767. uint32_t k;
  1768. uint32_t l;
  1769. uint32_t lastK;
  1770. uint8_t huffsize[257];
  1771. uint32_t huffcode[257];
  1772. error = JPEG_Bits_To_SizeCodes(DC_BitsValsTable->Bits, huffsize, huffcode, &lastK);
  1773. if(error != HAL_OK)
  1774. {
  1775. return error;
  1776. }
  1777. /* Figure C.3: ordering procedure for encoding procedure code tables */
  1778. k=0;
  1779. while(k < lastK)
  1780. {
  1781. l = DC_BitsValsTable->HuffVal[k];
  1782. if(l >= JPEG_DC_HUFF_TABLE_SIZE)
  1783. {
  1784. return HAL_ERROR; /* Huffman Table overflow error*/
  1785. }
  1786. else
  1787. {
  1788. DC_SizeCodesTable->HuffmanCode[l] = huffcode[k];
  1789. DC_SizeCodesTable->CodeLength[l] = huffsize[k] - 1;
  1790. k++;
  1791. }
  1792. }
  1793. /* Return function status */
  1794. return HAL_OK;
  1795. }
  1796. /**
  1797. * @brief Set the JPEG register with an DC huffman table at the given DC table address
  1798. * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
  1799. * the configuration information for JPEG module
  1800. * @param HuffTableDC pointer to DC huffman table
  1801. * @param DCTableAddress Encoder DC huffman table address it could be HUFFENC_DC0 or HUFFENC_DC1.
  1802. * @retval HAL status
  1803. */
  1804. static HAL_StatusTypeDef JPEG_Set_HuffDC_Mem(JPEG_HandleTypeDef *hjpeg, JPEG_DCHuffTableTypeDef *HuffTableDC, uint32_t *DCTableAddress)
  1805. {
  1806. HAL_StatusTypeDef error = HAL_OK;
  1807. JPEG_DC_HuffCodeTableTypeDef dcSizeCodesTable;
  1808. uint32_t i, lsb, msb;
  1809. __IO uint32_t *address, *addressDef;
  1810. if(DCTableAddress == (uint32_t *)(hjpeg->Instance->HUFFENC_DC0))
  1811. {
  1812. address = (hjpeg->Instance->HUFFENC_DC0 + (JPEG_DC_HUFF_TABLE_SIZE/2));
  1813. }
  1814. else if (DCTableAddress == (uint32_t *)(hjpeg->Instance->HUFFENC_DC1))
  1815. {
  1816. address = (hjpeg->Instance->HUFFENC_DC1 + (JPEG_DC_HUFF_TABLE_SIZE/2));
  1817. }
  1818. else
  1819. {
  1820. return HAL_ERROR;
  1821. }
  1822. if(HuffTableDC != NULL)
  1823. {
  1824. error = JPEG_DCHuff_BitsVals_To_SizeCodes(HuffTableDC, &dcSizeCodesTable);
  1825. if(error != HAL_OK)
  1826. {
  1827. return error;
  1828. }
  1829. addressDef = address;
  1830. *addressDef = 0x0FFF0FFF;
  1831. addressDef++;
  1832. *addressDef = 0x0FFF0FFF;
  1833. i = JPEG_DC_HUFF_TABLE_SIZE;
  1834. while(i>0)
  1835. {
  1836. i--;
  1837. address --;
  1838. msb = ((uint32_t)(((uint32_t)dcSizeCodesTable.CodeLength[i] & 0xF) << 8 )) | ((uint32_t)dcSizeCodesTable.HuffmanCode[i] & 0xFF);
  1839. i--;
  1840. lsb = ((uint32_t)(((uint32_t)dcSizeCodesTable.CodeLength[i] & 0xF) << 8 )) | ((uint32_t)dcSizeCodesTable.HuffmanCode[i] & 0xFF);
  1841. *address = lsb | (msb << 16);
  1842. }
  1843. }
  1844. /* Return function status */
  1845. return HAL_OK;
  1846. }
  1847. /**
  1848. * @brief Set the JPEG register with an AC huffman table at the given AC table address
  1849. * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
  1850. * the configuration information for JPEG module
  1851. * @param HuffTableAC pointer to AC huffman table
  1852. * @param ACTableAddress Encoder AC huffman table address it could be HUFFENC_AC0 or HUFFENC_AC1.
  1853. * @retval HAL status
  1854. */
  1855. static HAL_StatusTypeDef JPEG_Set_HuffAC_Mem(JPEG_HandleTypeDef *hjpeg, JPEG_ACHuffTableTypeDef *HuffTableAC, uint32_t *ACTableAddress)
  1856. {
  1857. HAL_StatusTypeDef error = HAL_OK;
  1858. JPEG_AC_HuffCodeTableTypeDef acSizeCodesTable;
  1859. uint32_t i, lsb, msb;
  1860. __IO uint32_t *address, *addressDef;
  1861. if(ACTableAddress == (uint32_t *)(hjpeg->Instance->HUFFENC_AC0))
  1862. {
  1863. address = (hjpeg->Instance->HUFFENC_AC0 + (JPEG_AC_HUFF_TABLE_SIZE/2));
  1864. }
  1865. else if (ACTableAddress == (uint32_t *)(hjpeg->Instance->HUFFENC_AC1))
  1866. {
  1867. address = (hjpeg->Instance->HUFFENC_AC1 + (JPEG_AC_HUFF_TABLE_SIZE/2));
  1868. }
  1869. else
  1870. {
  1871. return HAL_ERROR;
  1872. }
  1873. if(HuffTableAC != NULL)
  1874. {
  1875. error = JPEG_ACHuff_BitsVals_To_SizeCodes(HuffTableAC, &acSizeCodesTable);
  1876. if(error != HAL_OK)
  1877. {
  1878. return error;
  1879. }
  1880. /* Default values settings: 162:167 FFFh , 168:175 FD0h_FD7h */
  1881. /* Locations 162:175 of each AC table contain information used internally by the core */
  1882. addressDef = address;
  1883. for(i=0; i<3; i++)
  1884. {
  1885. *addressDef = 0x0FFF0FFF;
  1886. addressDef++;
  1887. }
  1888. *addressDef = 0x0FD10FD0;
  1889. addressDef++;
  1890. *addressDef = 0x0FD30FD2;
  1891. addressDef++;
  1892. *addressDef = 0x0FD50FD4;
  1893. addressDef++;
  1894. *addressDef = 0x0FD70FD6;
  1895. /* end of Locations 162:175 */
  1896. i = JPEG_AC_HUFF_TABLE_SIZE;
  1897. while (i > 0)
  1898. {
  1899. i--;
  1900. address--;
  1901. msb = ((uint32_t)(((uint32_t)acSizeCodesTable.CodeLength[i] & 0xF) << 8 )) | ((uint32_t)acSizeCodesTable.HuffmanCode[i] & 0xFF);
  1902. i--;
  1903. lsb = ((uint32_t)(((uint32_t)acSizeCodesTable.CodeLength[i] & 0xF) << 8 )) | ((uint32_t)acSizeCodesTable.HuffmanCode[i] & 0xFF);
  1904. *address = lsb | (msb << 16);
  1905. }
  1906. }
  1907. /* Return function status */
  1908. return HAL_OK;
  1909. }
  1910. /**
  1911. * @brief Configure the JPEG encoder register huffman tables to used during
  1912. * the encdoing operation
  1913. * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
  1914. * the configuration information for JPEG module
  1915. * @param HuffTableAC0 AC0 huffman table
  1916. * @param HuffTableDC0 DC0 huffman table
  1917. * @param HuffTableAC1 AC1 huffman table
  1918. * @param HuffTableDC1 DC1 huffman table
  1919. * @retval None
  1920. */
  1921. static HAL_StatusTypeDef JPEG_Set_HuffEnc_Mem(JPEG_HandleTypeDef *hjpeg, JPEG_ACHuffTableTypeDef *HuffTableAC0, JPEG_DCHuffTableTypeDef *HuffTableDC0 , JPEG_ACHuffTableTypeDef *HuffTableAC1, JPEG_DCHuffTableTypeDef *HuffTableDC1)
  1922. {
  1923. HAL_StatusTypeDef error = HAL_OK;
  1924. JPEG_Set_Huff_DHTMem(hjpeg, HuffTableAC0, HuffTableDC0, HuffTableAC1, HuffTableDC1);
  1925. if(HuffTableAC0 != NULL)
  1926. {
  1927. error = JPEG_Set_HuffAC_Mem(hjpeg, HuffTableAC0, (uint32_t *)(hjpeg->Instance->HUFFENC_AC0));
  1928. if(error != HAL_OK)
  1929. {
  1930. return error;
  1931. }
  1932. }
  1933. if(HuffTableAC1 != NULL)
  1934. {
  1935. error = JPEG_Set_HuffAC_Mem(hjpeg, HuffTableAC1, (uint32_t *)(hjpeg->Instance->HUFFENC_AC1));
  1936. if(error != HAL_OK)
  1937. {
  1938. return error;
  1939. }
  1940. }
  1941. if(HuffTableDC0 != NULL)
  1942. {
  1943. error = JPEG_Set_HuffDC_Mem(hjpeg, HuffTableDC0, (uint32_t *)hjpeg->Instance->HUFFENC_DC0);
  1944. if(error != HAL_OK)
  1945. {
  1946. return error;
  1947. }
  1948. }
  1949. if(HuffTableDC1 != NULL)
  1950. {
  1951. error = JPEG_Set_HuffDC_Mem(hjpeg, HuffTableDC1, (uint32_t *)hjpeg->Instance->HUFFENC_DC1);
  1952. if(error != HAL_OK)
  1953. {
  1954. return error;
  1955. }
  1956. }
  1957. /* Return function status */
  1958. return HAL_OK;
  1959. }
  1960. /**
  1961. * @brief Configure the JPEG register huffman tables to be included in the JPEG
  1962. * file header (used for encoding only)
  1963. * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
  1964. * the configuration information for JPEG module
  1965. * @param HuffTableAC0 AC0 huffman table
  1966. * @param HuffTableDC0 DC0 huffman table
  1967. * @param HuffTableAC1 AC1 huffman table
  1968. * @param HuffTableDC1 DC1 huffman table
  1969. * @retval None
  1970. */
  1971. static void JPEG_Set_Huff_DHTMem(JPEG_HandleTypeDef *hjpeg, JPEG_ACHuffTableTypeDef *HuffTableAC0, JPEG_DCHuffTableTypeDef *HuffTableDC0 , JPEG_ACHuffTableTypeDef *HuffTableAC1, JPEG_DCHuffTableTypeDef *HuffTableDC1)
  1972. {
  1973. uint32_t value, index;
  1974. __IO uint32_t *address;
  1975. if(HuffTableDC0 != NULL)
  1976. {
  1977. /* DC0 Huffman Table : BITS*/
  1978. /* DC0 BITS is a 16 Bytes table i.e 4x32bits words from DHTMEM base address to DHTMEM + 3*/
  1979. address = (hjpeg->Instance->DHTMEM + 3);
  1980. index = 16;
  1981. while(index > 0)
  1982. {
  1983. *address = (((uint32_t)HuffTableDC0->Bits[index-1] & 0xFF) << 24)|
  1984. (((uint32_t)HuffTableDC0->Bits[index-2] & 0xFF) << 16)|
  1985. (((uint32_t)HuffTableDC0->Bits[index-3] & 0xFF) << 8) |
  1986. ((uint32_t)HuffTableDC0->Bits[index-4] & 0xFF);
  1987. address--;
  1988. index -=4;
  1989. }
  1990. /* DC0 Huffman Table : Val*/
  1991. /* DC0 VALS is a 12 Bytes table i.e 3x32bits words from DHTMEM base address +4 to DHTMEM + 6 */
  1992. address = (hjpeg->Instance->DHTMEM + 6);
  1993. index = 12;
  1994. while(index > 0)
  1995. {
  1996. *address = (((uint32_t)HuffTableDC0->HuffVal[index-1] & 0xFF) << 24)|
  1997. (((uint32_t)HuffTableDC0->HuffVal[index-2] & 0xFF) << 16)|
  1998. (((uint32_t)HuffTableDC0->HuffVal[index-3] & 0xFF) << 8) |
  1999. ((uint32_t)HuffTableDC0->HuffVal[index-4] & 0xFF);
  2000. address--;
  2001. index -=4;
  2002. }
  2003. }
  2004. if(HuffTableAC0 != NULL)
  2005. {
  2006. /* AC0 Huffman Table : BITS*/
  2007. /* AC0 BITS is a 16 Bytes table i.e 4x32bits words from DHTMEM base address + 7 to DHTMEM + 10*/
  2008. address = (hjpeg->Instance->DHTMEM + 10);
  2009. index = 16;
  2010. while(index > 0)
  2011. {
  2012. *address = (((uint32_t)HuffTableAC0->Bits[index-1] & 0xFF) << 24)|
  2013. (((uint32_t)HuffTableAC0->Bits[index-2] & 0xFF) << 16)|
  2014. (((uint32_t)HuffTableAC0->Bits[index-3] & 0xFF) << 8) |
  2015. ((uint32_t)HuffTableAC0->Bits[index-4] & 0xFF);
  2016. address--;
  2017. index -=4;
  2018. }
  2019. /* AC0 Huffman Table : Val*/
  2020. /* AC0 VALS is a 162 Bytes table i.e 41x32bits words from DHTMEM base address + 11 to DHTMEM + 51 */
  2021. /* only Byte 0 and Byte 1 of the last word (@ DHTMEM + 51) belong to AC0 VALS table */
  2022. address = (hjpeg->Instance->DHTMEM + 51);
  2023. value = *address & 0xFFFF0000U;
  2024. value = value | (((uint32_t)HuffTableAC0->HuffVal[161] & 0xFF) << 8) | ((uint32_t)HuffTableAC0->HuffVal[160] & 0xFF);
  2025. *address = value;
  2026. /*continue setting 160 AC0 huffman values */
  2027. address--; /* address = hjpeg->Instance->DHTMEM + 50*/
  2028. index = 160;
  2029. while(index > 0)
  2030. {
  2031. *address = (((uint32_t)HuffTableAC0->HuffVal[index-1] & 0xFF) << 24)|
  2032. (((uint32_t)HuffTableAC0->HuffVal[index-2] & 0xFF) << 16)|
  2033. (((uint32_t)HuffTableAC0->HuffVal[index-3] & 0xFF) << 8) |
  2034. ((uint32_t)HuffTableAC0->HuffVal[index-4] & 0xFF);
  2035. address--;
  2036. index -=4;
  2037. }
  2038. }
  2039. if(HuffTableDC1 != NULL)
  2040. {
  2041. /* DC1 Huffman Table : BITS*/
  2042. /* DC1 BITS is a 16 Bytes table i.e 4x32bits words from DHTMEM + 51 base address to DHTMEM + 55*/
  2043. /* only Byte 2 and Byte 3 of the first word (@ DHTMEM + 51) belong to DC1 Bits table */
  2044. address = (hjpeg->Instance->DHTMEM + 51);
  2045. value = *address & 0x0000FFFFU;
  2046. value = value | (((uint32_t)HuffTableDC1->Bits[1] & 0xFF) << 24) | (((uint32_t)HuffTableDC1->Bits[0] & 0xFF) << 16);
  2047. *address = value;
  2048. /* only Byte 0 and Byte 1 of the last word (@ DHTMEM + 55) belong to DC1 Bits table */
  2049. address = (hjpeg->Instance->DHTMEM + 55);
  2050. value = *address & 0xFFFF0000U;
  2051. value = value | (((uint32_t)HuffTableDC1->Bits[15] & 0xFF) << 8) | ((uint32_t)HuffTableDC1->Bits[14] & 0xFF);
  2052. *address = value;
  2053. /*continue setting 12 DC1 huffman Bits from DHTMEM + 54 down to DHTMEM + 52*/
  2054. address--;
  2055. index = 12;
  2056. while(index > 0)
  2057. {
  2058. *address = (((uint32_t)HuffTableDC1->Bits[index+1] & 0xFF) << 24)|
  2059. (((uint32_t)HuffTableDC1->Bits[index] & 0xFF) << 16)|
  2060. (((uint32_t)HuffTableDC1->Bits[index-1] & 0xFF) << 8) |
  2061. ((uint32_t)HuffTableDC1->Bits[index-2] & 0xFF);
  2062. address--;
  2063. index -=4;
  2064. }
  2065. /* DC1 Huffman Table : Val*/
  2066. /* DC1 VALS is a 12 Bytes table i.e 3x32bits words from DHTMEM base address +55 to DHTMEM + 58 */
  2067. /* only Byte 2 and Byte 3 of the first word (@ DHTMEM + 55) belong to DC1 Val table */
  2068. address = (hjpeg->Instance->DHTMEM + 55);
  2069. value = *address & 0x0000FFFF;
  2070. value = value | (((uint32_t)HuffTableDC1->HuffVal[1] & 0xFF) << 24) | (((uint32_t)HuffTableDC1->HuffVal[0] & 0xFF) << 16);
  2071. *address = value;
  2072. /* only Byte 0 and Byte 1 of the last word (@ DHTMEM + 58) belong to DC1 Val table */
  2073. address = (hjpeg->Instance->DHTMEM + 58);
  2074. value = *address & 0xFFFF0000U;
  2075. value = value | (((uint32_t)HuffTableDC1->HuffVal[11] & 0xFF) << 8) | ((uint32_t)HuffTableDC1->HuffVal[10] & 0xFF);
  2076. *address = value;
  2077. /*continue setting 8 DC1 huffman val from DHTMEM + 57 down to DHTMEM + 56*/
  2078. address--;
  2079. index = 8;
  2080. while(index > 0)
  2081. {
  2082. *address = (((uint32_t)HuffTableDC1->HuffVal[index+1] & 0xFF) << 24)|
  2083. (((uint32_t)HuffTableDC1->HuffVal[index] & 0xFF) << 16)|
  2084. (((uint32_t)HuffTableDC1->HuffVal[index-1] & 0xFF) << 8) |
  2085. ((uint32_t)HuffTableDC1->HuffVal[index-2] & 0xFF);
  2086. address--;
  2087. index -=4;
  2088. }
  2089. }
  2090. if(HuffTableAC1 != NULL)
  2091. {
  2092. /* AC1 Huffman Table : BITS*/
  2093. /* AC1 BITS is a 16 Bytes table i.e 4x32bits words from DHTMEM base address + 58 to DHTMEM + 62*/
  2094. /* only Byte 2 and Byte 3 of the first word (@ DHTMEM + 58) belong to AC1 Bits table */
  2095. address = (hjpeg->Instance->DHTMEM + 58);
  2096. value = *address & 0x0000FFFFU;
  2097. value = value | (((uint32_t)HuffTableAC1->Bits[1] & 0xFF) << 24) | (((uint32_t)HuffTableAC1->Bits[0] & 0xFF) << 16);
  2098. *address = value;
  2099. /* only Byte 0 and Byte 1 of the last word (@ DHTMEM + 62) belong to Bits Val table */
  2100. address = (hjpeg->Instance->DHTMEM + 62);
  2101. value = *address & 0xFFFF0000U;
  2102. value = value | (((uint32_t)HuffTableAC1->Bits[15] & 0xFF) << 8) | ((uint32_t)HuffTableAC1->Bits[14] & 0xFF);
  2103. *address = value;
  2104. /*continue setting 12 AC1 huffman Bits from DHTMEM + 61 down to DHTMEM + 59*/
  2105. address--;
  2106. index = 12;
  2107. while(index > 0)
  2108. {
  2109. *address = (((uint32_t)HuffTableAC1->Bits[index+1] & 0xFF) << 24)|
  2110. (((uint32_t)HuffTableAC1->Bits[index] & 0xFF) << 16)|
  2111. (((uint32_t)HuffTableAC1->Bits[index-1] & 0xFF) << 8) |
  2112. ((uint32_t)HuffTableAC1->Bits[index-2] & 0xFF);
  2113. address--;
  2114. index -=4;
  2115. }
  2116. /* AC1 Huffman Table : Val*/
  2117. /* AC1 VALS is a 162 Bytes table i.e 41x32bits words from DHTMEM base address + 62 to DHTMEM + 102 */
  2118. /* only Byte 2 and Byte 3 of the first word (@ DHTMEM + 62) belong to AC1 VALS table */
  2119. address = (hjpeg->Instance->DHTMEM + 62);
  2120. value = *address & 0x0000FFFF;
  2121. value = value | (((uint32_t)HuffTableAC1->HuffVal[1] & 0xFF) << 24) | (((uint32_t)HuffTableAC1->HuffVal[0] & 0xFF) << 16);
  2122. *address = value;
  2123. /*continue setting 160 AC1 huffman values from DHTMEM + 63 to DHTMEM+102 */
  2124. address = (hjpeg->Instance->DHTMEM + 102);
  2125. index = 160;
  2126. while(index > 0)
  2127. {
  2128. *address = (((uint32_t)HuffTableAC1->HuffVal[index+1] & 0xFF) << 24)|
  2129. (((uint32_t)HuffTableAC1->HuffVal[index] & 0xFF) << 16)|
  2130. (((uint32_t)HuffTableAC1->HuffVal[index-1] & 0xFF) << 8) |
  2131. ((uint32_t)HuffTableAC1->HuffVal[index-2] & 0xFF);
  2132. address--;
  2133. index -=4;
  2134. }
  2135. }
  2136. }
  2137. /**
  2138. * @brief Configure the JPEG registers with a given quantization table
  2139. * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
  2140. * the configuration information for JPEG module
  2141. * @param QTable pointer to an array of 64 bytes giving the quantization table
  2142. * @param QTableAddress destination quantization address in the JPEG peripheral
  2143. * it could be QMEM0, QMEM1, QMEM2 or QMEM3
  2144. * @retval None
  2145. */
  2146. static HAL_StatusTypeDef JPEG_Set_Quantization_Mem(JPEG_HandleTypeDef *hjpeg, uint8_t *QTable, uint32_t *QTableAddress)
  2147. {
  2148. uint32_t i, j, *tableAddress, quantRow, quantVal, ScaleFactor;
  2149. if((QTableAddress == ((uint32_t *)(hjpeg->Instance->QMEM0))) ||
  2150. (QTableAddress == ((uint32_t *)(hjpeg->Instance->QMEM1))) ||
  2151. (QTableAddress == ((uint32_t *)(hjpeg->Instance->QMEM2))) ||
  2152. (QTableAddress == ((uint32_t *)(hjpeg->Instance->QMEM3))))
  2153. {
  2154. tableAddress = QTableAddress;
  2155. }
  2156. else
  2157. {
  2158. return HAL_ERROR;
  2159. }
  2160. if ((hjpeg->Conf.ImageQuality >= 50) && (hjpeg->Conf.ImageQuality <= 100))
  2161. {
  2162. ScaleFactor = 200 - (hjpeg->Conf.ImageQuality * 2);
  2163. }
  2164. else if (hjpeg->Conf.ImageQuality > 0)
  2165. {
  2166. ScaleFactor = ((uint32_t) 5000) / ((uint32_t) hjpeg->Conf.ImageQuality);
  2167. }
  2168. else
  2169. {
  2170. return HAL_ERROR;
  2171. }
  2172. /*Quantization_table = (Standard_quanization_table * ScaleFactor + 50) / 100*/
  2173. i = 0;
  2174. while( i < JPEG_QUANT_TABLE_SIZE)
  2175. {
  2176. quantRow = 0;
  2177. for(j=0; j<4; j++)
  2178. {
  2179. /* Note that the quantization coefficients must be specified in the table in zigzag order */
  2180. quantVal = ((((uint32_t) QTable[JPEG_ZIGZAG_ORDER[i+j]]) * ScaleFactor) + 50) / 100;
  2181. if(quantVal == 0)
  2182. {
  2183. quantVal = 1;
  2184. }
  2185. else if (quantVal > 255)
  2186. {
  2187. quantVal = 255;
  2188. }
  2189. quantRow |= ((quantVal & 0xFF) << (8 * j));
  2190. }
  2191. i += 4;
  2192. *tableAddress = quantRow;
  2193. tableAddress ++;
  2194. }
  2195. /* Return function status */
  2196. return HAL_OK;
  2197. }
  2198. /**
  2199. * @brief Configure the JPEG registers for YCbCr color space
  2200. * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
  2201. * the configuration information for JPEG module
  2202. * @retval None
  2203. */
  2204. static void JPEG_SetColorYCBCR(JPEG_HandleTypeDef *hjpeg)
  2205. {
  2206. uint32_t ySamplingH;
  2207. uint32_t ySamplingV;
  2208. uint32_t yblockNb;
  2209. /*Set Number of color components to 3*/
  2210. hjpeg->Instance->CONFR1 &= ~JPEG_CONFR1_NF;
  2211. hjpeg->Instance->CONFR1 |= JPEG_CONFR1_NF_1;
  2212. /* compute MCU block size and Y, Cb ,Cr sampling factors*/
  2213. if(hjpeg->Conf.ChromaSubsampling == JPEG_420_SUBSAMPLING)
  2214. {
  2215. ySamplingH = JPEG_CONFR4_HSF_1; /* Hs = 2*/
  2216. ySamplingV = JPEG_CONFR4_VSF_1; /* Vs = 2*/
  2217. yblockNb = 0x30; /* 4 blocks of 8x8*/
  2218. }
  2219. else if(hjpeg->Conf.ChromaSubsampling == JPEG_422_SUBSAMPLING)
  2220. {
  2221. ySamplingH = JPEG_CONFR4_HSF_1; /* Hs = 2*/
  2222. ySamplingV = JPEG_CONFR4_VSF_0; /* Vs = 1*/
  2223. yblockNb = 0x10; /* 2 blocks of 8x8*/
  2224. }
  2225. else /*JPEG_444_SUBSAMPLING and default*/
  2226. {
  2227. ySamplingH = JPEG_CONFR4_HSF_0; /* Hs = 1*/
  2228. ySamplingV = JPEG_CONFR4_VSF_0; /* Vs = 1*/
  2229. yblockNb = 0; /* 1 block of 8x8*/
  2230. }
  2231. hjpeg->Instance->CONFR1 &= ~(JPEG_CONFR1_NF | JPEG_CONFR1_NS);
  2232. hjpeg->Instance->CONFR1 |= (JPEG_CONFR1_NF_1 | JPEG_CONFR1_NS_1);
  2233. /*Reset CONFR4 register*/
  2234. hjpeg->Instance->CONFR4 = 0;
  2235. /*Set Horizental and Vertical sampling factor , number of blocks , Quantization table and Huffman AC/DC tables for component 0*/
  2236. hjpeg->Instance->CONFR4 |= (ySamplingH | ySamplingV | (yblockNb & JPEG_CONFR4_NB) );
  2237. /*Reset CONFR5 register*/
  2238. hjpeg->Instance->CONFR5 = 0;
  2239. /*Set Horizental and Vertical sampling factor , number of blocks , Quantization table and Huffman AC/DC tables for component 1*/
  2240. hjpeg->Instance->CONFR5 |= (JPEG_CONFR5_HSF_0 | JPEG_CONFR5_VSF_0 | JPEG_CONFR5_QT_0 | JPEG_CONFR5_HA | JPEG_CONFR5_HD);
  2241. /*Reset CONFR6 register*/
  2242. hjpeg->Instance->CONFR6 = 0;
  2243. /*Set Horizental and Vertical sampling factor and number of blocks for component 2*/
  2244. /* In YCBCR , by default, both chrominance components (component 1 and component 2) use the same Quantization table (table 1) */
  2245. /* In YCBCR , both chrominance components (component 1 and component 2) use the same Huffman tables (table 1) */
  2246. hjpeg->Instance->CONFR6 |= (JPEG_CONFR6_HSF_0 | JPEG_CONFR6_VSF_0 | JPEG_CONFR6_QT_0 | JPEG_CONFR6_HA | JPEG_CONFR6_HD);
  2247. }
  2248. /**
  2249. * @brief Configure the JPEG registers for GrayScale color space
  2250. * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
  2251. * the configuration information for JPEG module
  2252. * @retval None
  2253. */
  2254. static void JPEG_SetColorGrayScale(JPEG_HandleTypeDef *hjpeg)
  2255. {
  2256. /*Set Number of color components to 1*/
  2257. hjpeg->Instance->CONFR1 &= ~(JPEG_CONFR1_NF | JPEG_CONFR1_NS);
  2258. /*in GrayScale use 1 single Quantization table (Table 0)*/
  2259. /*in GrayScale use only one couple of AC/DC huffman table (table 0)*/
  2260. /*Reset CONFR4 register*/
  2261. hjpeg->Instance->CONFR4 = 0;
  2262. /*Set Horizental and Vertical sampling factor , number of blocks , Quantization table and Huffman AC/DC tables for component 0*/
  2263. hjpeg->Instance->CONFR4 |= JPEG_CONFR4_HSF_0 | JPEG_CONFR4_VSF_0 ;
  2264. }
  2265. /**
  2266. * @brief Configure the JPEG registers for CMYK color space
  2267. * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
  2268. * the configuration information for JPEG module
  2269. * @retval None
  2270. */
  2271. static void JPEG_SetColorCMYK(JPEG_HandleTypeDef *hjpeg)
  2272. {
  2273. uint32_t ySamplingH;
  2274. uint32_t ySamplingV;
  2275. uint32_t yblockNb;
  2276. /*Set Number of color components to 4*/
  2277. hjpeg->Instance->CONFR1 |= (JPEG_CONFR1_NF | JPEG_CONFR1_NS);
  2278. /* compute MCU block size and Y, Cb ,Cr sampling factors*/
  2279. if(hjpeg->Conf.ChromaSubsampling == JPEG_420_SUBSAMPLING)
  2280. {
  2281. ySamplingH = JPEG_CONFR4_HSF_1; /* Hs = 2*/
  2282. ySamplingV = JPEG_CONFR4_VSF_1; /* Vs = 2*/
  2283. yblockNb = 0x30; /* 4 blocks of 8x8*/
  2284. }
  2285. else if(hjpeg->Conf.ChromaSubsampling == JPEG_422_SUBSAMPLING)
  2286. {
  2287. ySamplingH = JPEG_CONFR4_HSF_1; /* Hs = 2*/
  2288. ySamplingV = JPEG_CONFR4_VSF_0; /* Vs = 1*/
  2289. yblockNb = 0x10; /* 2 blocks of 8x8*/
  2290. }
  2291. else /*JPEG_444_SUBSAMPLING and default*/
  2292. {
  2293. ySamplingH = JPEG_CONFR4_HSF_0; /* Hs = 1*/
  2294. ySamplingV = JPEG_CONFR4_VSF_0; /* Vs = 1*/
  2295. yblockNb = 0; /* 1 block of 8x8*/
  2296. }
  2297. /*Reset CONFR4 register*/
  2298. hjpeg->Instance->CONFR4 = 0;
  2299. /*Set Horizental and Vertical sampling factor , number of blocks , Quantization table and Huffman AC/DC tables for component 0*/
  2300. hjpeg->Instance->CONFR4 |= (ySamplingH | ySamplingV | (yblockNb & JPEG_CONFR4_NB) );
  2301. /*Reset CONFR5 register*/
  2302. hjpeg->Instance->CONFR5 = 0;
  2303. /*Set Horizental and Vertical sampling factor , number of blocks , Quantization table and Huffman AC/DC tables for component 1*/
  2304. hjpeg->Instance->CONFR5 |= (JPEG_CONFR5_HSF_0 | JPEG_CONFR5_VSF_0);
  2305. /*Reset CONFR6 register*/
  2306. hjpeg->Instance->CONFR6 = 0;
  2307. /*Set Horizental and Vertical sampling factor , number of blocks , Quantization table and Huffman AC/DC tables for component 2*/
  2308. hjpeg->Instance->CONFR6 |= (JPEG_CONFR6_HSF_0 | JPEG_CONFR6_VSF_0);
  2309. /*Reset CONFR7 register*/
  2310. hjpeg->Instance->CONFR7 = 0;
  2311. /*Set Horizental and Vertical sampling factor , number of blocks , Quantization table and Huffman AC/DC tables for component 3*/
  2312. hjpeg->Instance->CONFR7 |= (JPEG_CONFR7_HSF_0 | JPEG_CONFR7_VSF_0);
  2313. }
  2314. /**
  2315. * @brief Init the JPEG encoding/decoding process in case of Polling or Interrupt and DMA
  2316. * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
  2317. * the configuration information for JPEG module
  2318. * @retval None
  2319. */
  2320. static void JPEG_Init_Process(JPEG_HandleTypeDef *hjpeg)
  2321. {
  2322. /*Reset pause*/
  2323. hjpeg->Context &= (~(JPEG_CONTEXT_PAUSE_INPUT | JPEG_CONTEXT_PAUSE_OUTPUT));
  2324. if((hjpeg->Context & JPEG_CONTEXT_OPERATION_MASK) == JPEG_CONTEXT_DECODE)
  2325. {
  2326. /*Set JPEG Codec to Decoding mode */
  2327. hjpeg->Instance->CONFR1 |= JPEG_CONFR1_DE;
  2328. }
  2329. else if((hjpeg->Context & JPEG_CONTEXT_OPERATION_MASK) == JPEG_CONTEXT_ENCODE)
  2330. {
  2331. /*Set JPEG Codec to Encoding mode */
  2332. hjpeg->Instance->CONFR1 &= ~JPEG_CONFR1_DE;
  2333. }
  2334. /*Stop JPEG processing */
  2335. hjpeg->Instance->CONFR0 &= ~JPEG_CONFR0_START;
  2336. /* Disable All Interrupts */
  2337. __HAL_JPEG_DISABLE_IT(hjpeg,JPEG_INTERRUPT_MASK);
  2338. /* Disable All DMA requests */
  2339. JPEG_DISABLE_DMA(hjpeg,JPEG_DMA_MASK);
  2340. /* Flush input and output FIFOs*/
  2341. hjpeg->Instance->CR |= JPEG_CR_IFF;
  2342. hjpeg->Instance->CR |= JPEG_CR_OFF;
  2343. /* Clear all flags */
  2344. __HAL_JPEG_CLEAR_FLAG(hjpeg,JPEG_FLAG_ALL);
  2345. /*Start Encoding/Decoding*/
  2346. hjpeg->Instance->CONFR0 |= JPEG_CONFR0_START;
  2347. if((hjpeg->Context & JPEG_CONTEXT_METHOD_MASK) == JPEG_CONTEXT_IT)
  2348. {
  2349. /*Enable IN/OUT, end of Conversation, and end of header parsing interruptions*/
  2350. __HAL_JPEG_ENABLE_IT(hjpeg, JPEG_IT_IFT | JPEG_IT_IFNF | JPEG_IT_OFT | JPEG_IT_OFNE | JPEG_IT_EOC |JPEG_IT_HPD);
  2351. }
  2352. else if((hjpeg->Context & JPEG_CONTEXT_METHOD_MASK) == JPEG_CONTEXT_DMA)
  2353. {
  2354. /*Enable End Of Conversation, and End Of Header parsing interruptions*/
  2355. __HAL_JPEG_ENABLE_IT(hjpeg, JPEG_IT_EOC |JPEG_IT_HPD);
  2356. }
  2357. }
  2358. /**
  2359. * @brief JPEG encoding/decoding process in case of Polling or Interrupt
  2360. * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
  2361. * the configuration information for JPEG module
  2362. * @retval JPEG_PROCESS_DONE if the process has ends else JPEG_PROCESS_ONGOING
  2363. */
  2364. static uint32_t JPEG_Process(JPEG_HandleTypeDef *hjpeg)
  2365. {
  2366. uint32_t tmpContext;
  2367. /*End of header processing flag rised*/
  2368. if(((hjpeg->Context & JPEG_CONTEXT_OPERATION_MASK) == JPEG_CONTEXT_DECODE) && (__HAL_JPEG_GET_FLAG(hjpeg, JPEG_FLAG_HPDF) != RESET))
  2369. {
  2370. /*Call Header parsing complet callback */
  2371. HAL_JPEG_GetInfo(hjpeg, &hjpeg->Conf);
  2372. /* Reset the ImageQuality */
  2373. hjpeg->Conf.ImageQuality = 0;
  2374. /* Note : the image quality is only available at the end of the decoding operation */
  2375. /* at the current stage the calculated image quality is not correct so reset it */
  2376. /*Call Info Ready callback */
  2377. HAL_JPEG_InfoReadyCallback(hjpeg, &hjpeg->Conf);
  2378. __HAL_JPEG_DISABLE_IT(hjpeg,JPEG_IT_HPD);
  2379. /* Clear header processing done flag */
  2380. __HAL_JPEG_CLEAR_FLAG(hjpeg,JPEG_FLAG_HPDF);
  2381. }
  2382. /*Input FIFO status handling*/
  2383. if((hjpeg->Context & JPEG_CONTEXT_PAUSE_INPUT) == 0)
  2384. {
  2385. if(__HAL_JPEG_GET_FLAG(hjpeg, JPEG_FLAG_IFTF) != RESET)
  2386. {
  2387. /*Input FIFO threshold flag rised*/
  2388. /*4 words (16 bytes) can be written in */
  2389. JPEG_ReadInputData(hjpeg,4);
  2390. }
  2391. else if(__HAL_JPEG_GET_FLAG(hjpeg, JPEG_FLAG_IFNFF) != RESET)
  2392. {
  2393. /*Input FIFO Not Full flag rised*/
  2394. /*32-bit value can be written in */
  2395. JPEG_ReadInputData(hjpeg,1);
  2396. }
  2397. }
  2398. /*Output FIFO flag handling*/
  2399. if((hjpeg->Context & JPEG_CONTEXT_PAUSE_OUTPUT) == 0)
  2400. {
  2401. if(__HAL_JPEG_GET_FLAG(hjpeg, JPEG_FLAG_OFTF) != RESET)
  2402. {
  2403. /*Output FIFO threshold flag rised*/
  2404. /*4 words (16 bytes) can be read out */
  2405. JPEG_StoreOutputData(hjpeg, 4);
  2406. }
  2407. else if(__HAL_JPEG_GET_FLAG(hjpeg, JPEG_FLAG_OFNEF) != RESET)
  2408. {
  2409. /*Output FIFO Not Empty flag rised*/
  2410. /*32-bit value can be read out */
  2411. JPEG_StoreOutputData(hjpeg, 1);
  2412. }
  2413. }
  2414. /*End of Conversion handling :i.e EOC flag is high and OFTF low and OFNEF low*/
  2415. if(__HAL_JPEG_GET_FLAG(hjpeg, JPEG_FLAG_EOCF | JPEG_FLAG_OFTF | JPEG_FLAG_OFNEF) == JPEG_FLAG_EOCF)
  2416. {
  2417. /*Stop Encoding/Decoding*/
  2418. hjpeg->Instance->CONFR0 &= ~JPEG_CONFR0_START;
  2419. if((hjpeg->Context & JPEG_CONTEXT_METHOD_MASK) == JPEG_CONTEXT_IT)
  2420. {
  2421. /* Disable All Interrupts */
  2422. __HAL_JPEG_DISABLE_IT(hjpeg,JPEG_INTERRUPT_MASK);
  2423. }
  2424. /* Clear all flags */
  2425. __HAL_JPEG_CLEAR_FLAG(hjpeg,JPEG_FLAG_ALL);
  2426. /*Call End of conversion callback */
  2427. if(hjpeg->JpegOutCount > 0)
  2428. {
  2429. /*Output Buffer is not empty, call DecodedDataReadyCallback*/
  2430. HAL_JPEG_DataReadyCallback (hjpeg, hjpeg->pJpegOutBuffPtr, hjpeg->JpegOutCount);
  2431. hjpeg->JpegOutCount = 0;
  2432. }
  2433. /*Reset Context Operation*/
  2434. tmpContext = hjpeg->Context;
  2435. /*Clear all context fields execpt JPEG_CONTEXT_CONF_ENCODING and JPEG_CONTEXT_CUSTOM_TABLES*/
  2436. hjpeg->Context &= (JPEG_CONTEXT_CONF_ENCODING | JPEG_CONTEXT_CUSTOM_TABLES);
  2437. /* Process Unlocked */
  2438. __HAL_UNLOCK(hjpeg);
  2439. /* Change the JPEG state */
  2440. hjpeg->State = HAL_JPEG_STATE_READY;
  2441. /*Call End of Encoding/Decoding callback */
  2442. if((tmpContext & JPEG_CONTEXT_OPERATION_MASK) == JPEG_CONTEXT_DECODE)
  2443. {
  2444. HAL_JPEG_DecodeCpltCallback(hjpeg);
  2445. }
  2446. else if((tmpContext & JPEG_CONTEXT_OPERATION_MASK) == JPEG_CONTEXT_ENCODE)
  2447. {
  2448. HAL_JPEG_EncodeCpltCallback(hjpeg);
  2449. }
  2450. return JPEG_PROCESS_DONE;
  2451. }
  2452. return JPEG_PROCESS_ONGOING;
  2453. }
  2454. /**
  2455. * @brief Store some output data from the JPEG peripheral to the output buffer.
  2456. * This function is used when the JPEG peripheral has new data to output
  2457. * in case of Polling or Interrupt process
  2458. * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
  2459. * the configuration information for JPEG module
  2460. * @param nbOutputWords Number of output words (of 32 bits) ready from the JPEG peripheral
  2461. * @retval None
  2462. */
  2463. static void JPEG_StoreOutputData(JPEG_HandleTypeDef *hjpeg, uint32_t nbOutputWords)
  2464. {
  2465. uint32_t index, nBwords, nbBytes , dataword, *pOutData;
  2466. pOutData = (uint32_t *)(((uint32_t *)hjpeg->pJpegOutBuffPtr) + (hjpeg->JpegOutCount/4));
  2467. if(hjpeg->OutDataLength >= (hjpeg->JpegOutCount + (nbOutputWords*4)))
  2468. {
  2469. for(index = 0; index < nbOutputWords; index++)
  2470. {
  2471. /*Transfer 32 bits from the JPEG output FIFO*/
  2472. *pOutData = hjpeg->Instance->DOR;
  2473. pOutData++;
  2474. hjpeg->JpegOutCount += 4;
  2475. }
  2476. if(hjpeg->OutDataLength == hjpeg->JpegOutCount)
  2477. {
  2478. /*Output Buffer is full, call DecodedDataReadyCallback*/
  2479. HAL_JPEG_DataReadyCallback (hjpeg, hjpeg->pJpegOutBuffPtr, hjpeg->JpegOutCount);
  2480. hjpeg->JpegOutCount = 0;
  2481. }
  2482. }
  2483. else if(hjpeg->OutDataLength > hjpeg->JpegOutCount)
  2484. {
  2485. nBwords = (hjpeg->OutDataLength - hjpeg->JpegOutCount)/4;
  2486. for(index = 0; index < nBwords; index++)
  2487. {
  2488. /*Transfer 32 bits from the JPEG output FIFO*/
  2489. *pOutData = hjpeg->Instance->DOR;
  2490. pOutData++;
  2491. hjpeg->JpegOutCount += 4;
  2492. }
  2493. if(hjpeg->OutDataLength == hjpeg->JpegOutCount)
  2494. {
  2495. /*Output Buffer is full, call DecodedDataReadyCallback*/
  2496. HAL_JPEG_DataReadyCallback (hjpeg, hjpeg->pJpegOutBuffPtr, hjpeg->JpegOutCount);
  2497. hjpeg->JpegOutCount = 0;
  2498. }
  2499. else
  2500. {
  2501. nbBytes = hjpeg->OutDataLength - hjpeg->JpegOutCount;
  2502. dataword = hjpeg->Instance->DOR;
  2503. for(index = 0; index < nbBytes; index++)
  2504. {
  2505. hjpeg->pJpegOutBuffPtr[hjpeg->JpegOutCount] = (dataword >> (8*index)) & 0xFF;
  2506. hjpeg->JpegOutCount++;
  2507. }
  2508. /*Output Buffer is full, call DecodedDataReadyCallback*/
  2509. HAL_JPEG_DataReadyCallback (hjpeg, hjpeg->pJpegOutBuffPtr, hjpeg->JpegOutCount);
  2510. hjpeg->JpegOutCount = 0;
  2511. nbBytes = 4 - nbBytes;
  2512. for(index = nbBytes; index < 4; index++)
  2513. {
  2514. hjpeg->pJpegOutBuffPtr[hjpeg->JpegOutCount] = (dataword >> (8*index)) & 0xFF;
  2515. hjpeg->JpegOutCount++;
  2516. }
  2517. }
  2518. }
  2519. }
  2520. /**
  2521. * @brief Read some input Data from the input buffer.
  2522. * This function is used when the JPEG peripheral needs new data
  2523. * in case of Polling or Interrupt process
  2524. * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
  2525. * the configuration information for JPEG module
  2526. * @param nbRequestWords Number of input words (of 32 bits) that the JPE peripheral request
  2527. * @retval None
  2528. */
  2529. static void JPEG_ReadInputData(JPEG_HandleTypeDef *hjpeg, uint32_t nbRequestWords)
  2530. {
  2531. uint32_t nbBytes = 0, nBwords, index, Dataword;
  2532. if((hjpeg->InDataLength == 0) || (nbRequestWords == 0))
  2533. {
  2534. /* No more Input data : nothing to do*/
  2535. HAL_JPEG_Pause(hjpeg, JPEG_PAUSE_RESUME_INPUT);
  2536. }
  2537. else if(hjpeg->InDataLength > hjpeg->JpegInCount)
  2538. {
  2539. nbBytes = hjpeg->InDataLength - hjpeg->JpegInCount;
  2540. }
  2541. else if(hjpeg->InDataLength == hjpeg->JpegInCount)
  2542. {
  2543. /*Call HAL_JPEG_GetDataCallback to get new data */
  2544. HAL_JPEG_GetDataCallback(hjpeg, hjpeg->JpegInCount);
  2545. if(hjpeg->InDataLength > 4)
  2546. {
  2547. hjpeg->InDataLength = hjpeg->InDataLength - (hjpeg->InDataLength % 4);
  2548. }
  2549. hjpeg->JpegInCount = 0;
  2550. nbBytes = hjpeg->InDataLength;
  2551. }
  2552. if((nbBytes > 0) && ((hjpeg->Context & JPEG_CONTEXT_PAUSE_INPUT) == 0))
  2553. {
  2554. nBwords = nbBytes / 4;
  2555. if(nBwords >= nbRequestWords)
  2556. {
  2557. for(index = 0; index < nbRequestWords; index++)
  2558. {
  2559. hjpeg->Instance->DIR = *((uint32_t *)(((uint32_t *)hjpeg->pJpegInBuffPtr) + (hjpeg->JpegInCount/4)));
  2560. hjpeg->JpegInCount += 4;
  2561. }
  2562. }
  2563. else /*nBwords < nbRequestWords*/
  2564. {
  2565. if(nBwords > 0)
  2566. {
  2567. for(index = 0; index < nBwords; index++)
  2568. {
  2569. hjpeg->Instance->DIR = *((uint32_t *)(((uint32_t *)hjpeg->pJpegInBuffPtr) + (hjpeg->JpegInCount/4)));
  2570. hjpeg->JpegInCount += 4;
  2571. }
  2572. }
  2573. else
  2574. {
  2575. /* end of file*/
  2576. Dataword = 0;
  2577. for(index=0; index< nbBytes; index++)
  2578. {
  2579. Dataword |= (uint32_t)hjpeg->pJpegInBuffPtr[hjpeg->JpegInCount] << (8 * index);
  2580. hjpeg->JpegInCount++;
  2581. }
  2582. hjpeg->Instance->DIR = Dataword;
  2583. }
  2584. }
  2585. }
  2586. }
  2587. /**
  2588. * @brief Start the JPEG DMA process (encoding/decoding)
  2589. * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
  2590. * the configuration information for JPEG module
  2591. * @retval JPEG_PROCESS_DONE if process ends else JPEG_PROCESS_ONGOING
  2592. */
  2593. static HAL_StatusTypeDef JPEG_DMA_StartProcess(JPEG_HandleTypeDef *hjpeg)
  2594. {
  2595. if((hjpeg->InDataLength < 4) || (hjpeg->OutDataLength < 4))
  2596. {
  2597. return HAL_ERROR;
  2598. }
  2599. /* Reset Ending DMA internal context flag*/
  2600. hjpeg->Context &= ~JPEG_CONTEXT_ENDING_DMA;
  2601. /* Disable DMA In/Out Request*/
  2602. JPEG_DISABLE_DMA(hjpeg,JPEG_DMA_ODMA | JPEG_DMA_IDMA);
  2603. /* Set the JPEG DMA In transfer complete callback */
  2604. hjpeg->hdmain->XferCpltCallback = JPEG_DMAInCpltCallback;
  2605. /* Set the DMA In error callback */
  2606. hjpeg->hdmain->XferErrorCallback = JPEG_DMAErrorCallback;
  2607. /* Set the JPEG DMA Out transfer complete callback */
  2608. hjpeg->hdmaout->XferCpltCallback = JPEG_DMAOutCpltCallback;
  2609. /* Set the DMA Out error callback */
  2610. hjpeg->hdmaout->XferErrorCallback = JPEG_DMAErrorCallback;
  2611. /* Set the DMA Out Abort callback */
  2612. hjpeg->hdmaout->XferAbortCallback = JPEG_DMAOutAbortCallback;
  2613. /*DMA transfer size must be a multiple of 4 bytes i.e mutliple of 32bits words*/
  2614. hjpeg->InDataLength = hjpeg->InDataLength - (hjpeg->InDataLength % 4);
  2615. /*DMA transfer size must be a multiple of 4 bytes i.e mutliple of 32bits words*/
  2616. hjpeg->OutDataLength = hjpeg->OutDataLength - (hjpeg->OutDataLength % 4);
  2617. /* Start DMA FIFO In transfer */
  2618. HAL_DMA_Start_IT(hjpeg->hdmain, (uint32_t)hjpeg->pJpegInBuffPtr, (uint32_t)&hjpeg->Instance->DIR, hjpeg->InDataLength >> 2);
  2619. /* Start DMA FIFO Out transfer */
  2620. HAL_DMA_Start_IT(hjpeg->hdmaout, (uint32_t)&hjpeg->Instance->DOR, (uint32_t)hjpeg->pJpegOutBuffPtr, hjpeg->OutDataLength >> 2);
  2621. /* Enable JPEG In/Out DMA requests*/
  2622. JPEG_ENABLE_DMA(hjpeg,JPEG_DMA_IDMA | JPEG_DMA_ODMA);
  2623. return HAL_OK;
  2624. }
  2625. /**
  2626. * @brief Continue the current JPEG DMA process (encoding/decoding)
  2627. * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
  2628. * the configuration information for JPEG module
  2629. * @retval JPEG_PROCESS_DONE if process ends else JPEG_PROCESS_ONGOING
  2630. */
  2631. static uint32_t JPEG_DMA_ContinueProcess(JPEG_HandleTypeDef *hjpeg)
  2632. {
  2633. /*End of header processing flag rises*/
  2634. if(((hjpeg->Context & JPEG_CONTEXT_OPERATION_MASK) == JPEG_CONTEXT_DECODE) && (__HAL_JPEG_GET_FLAG(hjpeg, JPEG_FLAG_HPDF) != RESET))
  2635. {
  2636. /*Call Header parsing complete callback */
  2637. HAL_JPEG_GetInfo(hjpeg, &hjpeg->Conf);
  2638. /* Reset the ImageQuality */
  2639. hjpeg->Conf.ImageQuality = 0;
  2640. /* Note : the image quality is only available at the end of the decoding operation */
  2641. /* at the current stage the calculated image quality is not correct so reset it */
  2642. /*Call Info Ready callback */
  2643. HAL_JPEG_InfoReadyCallback(hjpeg, &hjpeg->Conf);
  2644. __HAL_JPEG_DISABLE_IT(hjpeg,JPEG_IT_HPD);
  2645. /* Clear header processing done flag */
  2646. __HAL_JPEG_CLEAR_FLAG(hjpeg,JPEG_FLAG_HPDF);
  2647. }
  2648. /*End of Conversion handling*/
  2649. if(__HAL_JPEG_GET_FLAG(hjpeg, JPEG_FLAG_EOCF) != RESET)
  2650. {
  2651. /*Disabkle JPEG In/Out DMA Requests*/
  2652. JPEG_DISABLE_DMA(hjpeg,JPEG_DMA_ODMA | JPEG_DMA_IDMA);
  2653. hjpeg->Context |= JPEG_CONTEXT_ENDING_DMA;
  2654. /*Stop Encoding/Decoding*/
  2655. hjpeg->Instance->CONFR0 &= ~JPEG_CONFR0_START;
  2656. __HAL_JPEG_DISABLE_IT(hjpeg,JPEG_INTERRUPT_MASK);
  2657. /* Clear all flags */
  2658. __HAL_JPEG_CLEAR_FLAG(hjpeg,JPEG_FLAG_ALL);
  2659. if(hjpeg->hdmain->State == HAL_DMA_STATE_BUSY)
  2660. {
  2661. /* Stop the DMA In Xfer*/
  2662. HAL_DMA_Abort_IT(hjpeg->hdmain);
  2663. }
  2664. if(hjpeg->hdmaout->State == HAL_DMA_STATE_BUSY)
  2665. {
  2666. /* Stop the DMA out Xfer*/
  2667. HAL_DMA_Abort_IT(hjpeg->hdmaout);
  2668. }
  2669. else
  2670. {
  2671. return JPEG_DMA_EndProcess(hjpeg);
  2672. }
  2673. }
  2674. return JPEG_PROCESS_ONGOING;
  2675. }
  2676. /**
  2677. * @brief Finalize the current JPEG DMA process (encoding/decoding)
  2678. * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
  2679. * the configuration information for JPEG module
  2680. * @retval JPEG_PROCESS_DONE
  2681. */
  2682. static uint32_t JPEG_DMA_EndProcess(JPEG_HandleTypeDef *hjpeg)
  2683. {
  2684. uint32_t tmpContext;
  2685. hjpeg->JpegOutCount = hjpeg->OutDataLength - ((hjpeg->hdmaout->Instance->NDTR & DMA_SxNDT) << 2);
  2686. /*if Output Buffer is full, call HAL_JPEG_DataReadyCallback*/
  2687. if(hjpeg->JpegOutCount == hjpeg->OutDataLength)
  2688. {
  2689. HAL_JPEG_DataReadyCallback (hjpeg, hjpeg->pJpegOutBuffPtr, hjpeg->JpegOutCount);
  2690. hjpeg->JpegOutCount = 0;
  2691. }
  2692. /*Check if remaining data in the output FIFO*/
  2693. if(__HAL_JPEG_GET_FLAG(hjpeg, JPEG_FLAG_OFNEF) == 0)
  2694. {
  2695. /*Stop Encoding/Decoding*/
  2696. hjpeg->Instance->CONFR0 &= ~JPEG_CONFR0_START;
  2697. tmpContext = hjpeg->Context;
  2698. /*Clear all context fileds execpt JPEG_CONTEXT_CONF_ENCODING and JPEG_CONTEXT_CUSTOM_TABLES*/
  2699. hjpeg->Context &= (JPEG_CONTEXT_CONF_ENCODING | JPEG_CONTEXT_CUSTOM_TABLES);
  2700. /* Process Unlocked */
  2701. __HAL_UNLOCK(hjpeg);
  2702. /* Change the JPEG state */
  2703. hjpeg->State = HAL_JPEG_STATE_READY;
  2704. /*Call End of Encoding/Decoding callback */
  2705. if((tmpContext & JPEG_CONTEXT_OPERATION_MASK) == JPEG_CONTEXT_DECODE)
  2706. {
  2707. HAL_JPEG_DecodeCpltCallback(hjpeg);
  2708. }
  2709. else if((tmpContext & JPEG_CONTEXT_OPERATION_MASK) == JPEG_CONTEXT_ENCODE)
  2710. {
  2711. HAL_JPEG_EncodeCpltCallback(hjpeg);
  2712. }
  2713. }
  2714. else if((hjpeg->Context & JPEG_CONTEXT_PAUSE_OUTPUT) == 0)
  2715. {
  2716. JPEG_DMA_PollResidualData(hjpeg);
  2717. return JPEG_PROCESS_DONE;
  2718. }
  2719. return JPEG_PROCESS_ONGOING;
  2720. }
  2721. /**
  2722. * @brief Poll residual output data when DMA process (encoding/decoding)
  2723. * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
  2724. * the configuration information for JPEG module
  2725. * @retval None.
  2726. */
  2727. static void JPEG_DMA_PollResidualData(JPEG_HandleTypeDef *hjpeg)
  2728. {
  2729. uint32_t tmpContext, count = JPEG_FIFO_SIZE, *pDataOut;
  2730. pDataOut = (uint32_t *)(hjpeg->pJpegOutBuffPtr + hjpeg->JpegOutCount);
  2731. while((__HAL_JPEG_GET_FLAG(hjpeg, JPEG_FLAG_OFNEF) != 0) && (count > 0))
  2732. {
  2733. count--;
  2734. *pDataOut = hjpeg->Instance->DOR;
  2735. pDataOut++;
  2736. hjpeg->JpegOutCount += 4;
  2737. if(hjpeg->JpegOutCount == hjpeg->OutDataLength)
  2738. {
  2739. /*Output Buffer is full, call HAL_JPEG_DataReadyCallback*/
  2740. HAL_JPEG_DataReadyCallback (hjpeg, hjpeg->pJpegOutBuffPtr, hjpeg->JpegOutCount);
  2741. hjpeg->JpegOutCount = 0;
  2742. }
  2743. }
  2744. /*Stop Encoding/Decoding*/
  2745. hjpeg->Instance->CONFR0 &= ~JPEG_CONFR0_START;
  2746. if(hjpeg->JpegOutCount > 0)
  2747. {
  2748. /*Output Buffer is not empty, call DecodedDataReadyCallback*/
  2749. HAL_JPEG_DataReadyCallback (hjpeg, hjpeg->pJpegOutBuffPtr, hjpeg->JpegOutCount);
  2750. hjpeg->JpegOutCount = 0;
  2751. }
  2752. tmpContext = hjpeg->Context;
  2753. /*Clear all context fileds execpt JPEG_CONTEXT_CONF_ENCODING and JPEG_CONTEXT_CUSTOM_TABLES*/
  2754. hjpeg->Context &= (JPEG_CONTEXT_CONF_ENCODING | JPEG_CONTEXT_CUSTOM_TABLES);
  2755. /* Process Unlocked */
  2756. __HAL_UNLOCK(hjpeg);
  2757. /* Change the JPEG state */
  2758. hjpeg->State = HAL_JPEG_STATE_READY;
  2759. /*Call End of Encoding/Decoding callback */
  2760. if((tmpContext & JPEG_CONTEXT_OPERATION_MASK) == JPEG_CONTEXT_DECODE)
  2761. {
  2762. HAL_JPEG_DecodeCpltCallback(hjpeg);
  2763. }
  2764. else if((tmpContext & JPEG_CONTEXT_OPERATION_MASK) == JPEG_CONTEXT_ENCODE)
  2765. {
  2766. HAL_JPEG_EncodeCpltCallback(hjpeg);
  2767. }
  2768. }
  2769. /**
  2770. * @brief DMA input transfer complete callback
  2771. * @param hdma pointer to a DMA_HandleTypeDef structure.
  2772. * @retval None
  2773. */
  2774. static void JPEG_DMAInCpltCallback(DMA_HandleTypeDef *hdma)
  2775. {
  2776. JPEG_HandleTypeDef* hjpeg = (JPEG_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
  2777. /* Disable The JPEG IT so the DMA Input Callback can not be interrupted by the JPEG EOC IT or JPEG HPD IT */
  2778. __HAL_JPEG_DISABLE_IT(hjpeg,JPEG_INTERRUPT_MASK);
  2779. if(((hjpeg->Context & JPEG_CONTEXT_METHOD_MASK) == JPEG_CONTEXT_DMA) && ((hjpeg->Context & JPEG_CONTEXT_ENDING_DMA) == 0))
  2780. {
  2781. JPEG_DISABLE_DMA(hjpeg,JPEG_DMA_IDMA);
  2782. hjpeg->JpegInCount = hjpeg->InDataLength - ((hdma->Instance->NDTR & DMA_SxNDT) << 2);
  2783. /*Call HAL_JPEG_GetDataCallback to get new data */
  2784. HAL_JPEG_GetDataCallback(hjpeg, hjpeg->JpegInCount);
  2785. if(hjpeg->InDataLength >= 4)
  2786. {
  2787. /*JPEG Input DMA transfer data number must be multiple of 32 bits word
  2788. as the destination is a 32 bits (4 bytes) register */
  2789. hjpeg->InDataLength = hjpeg->InDataLength - (hjpeg->InDataLength % 4);
  2790. }
  2791. else if(hjpeg->InDataLength > 0)
  2792. {
  2793. /*Transfer last data word (i.e last 4 bytes)*/
  2794. hjpeg->InDataLength = 4;
  2795. }
  2796. if(((hjpeg->Context & JPEG_CONTEXT_PAUSE_INPUT) == 0) && (hjpeg->InDataLength > 0))
  2797. {
  2798. /* Start DMA FIFO In transfer */
  2799. HAL_DMA_Start_IT(hjpeg->hdmain, (uint32_t)hjpeg->pJpegInBuffPtr, (uint32_t)&hjpeg->Instance->DIR, hjpeg->InDataLength >> 2);
  2800. JPEG_ENABLE_DMA(hjpeg,JPEG_DMA_IDMA);
  2801. }
  2802. /* JPEG Conversion still on going : Enable the JPEG IT */
  2803. __HAL_JPEG_ENABLE_IT(hjpeg,JPEG_IT_EOC |JPEG_IT_HPD);
  2804. }
  2805. }
  2806. /**
  2807. * @brief DMA output transfer complete callback
  2808. * @param hdma pointer to a DMA_HandleTypeDef structure.
  2809. * @retval None
  2810. */
  2811. static void JPEG_DMAOutCpltCallback(DMA_HandleTypeDef *hdma)
  2812. {
  2813. JPEG_HandleTypeDef* hjpeg = (JPEG_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
  2814. /* Disable The JPEG IT so the DMA Output Callback can not be interrupted by the JPEG EOC IT or JPEG HPD IT */
  2815. __HAL_JPEG_DISABLE_IT(hjpeg,JPEG_INTERRUPT_MASK);
  2816. if(((hjpeg->Context & JPEG_CONTEXT_METHOD_MASK) == JPEG_CONTEXT_DMA) && ((hjpeg->Context & JPEG_CONTEXT_ENDING_DMA) == 0))
  2817. {
  2818. if(__HAL_JPEG_GET_FLAG(hjpeg, JPEG_FLAG_EOCF) == 0)
  2819. {
  2820. JPEG_DISABLE_DMA(hjpeg,JPEG_DMA_ODMA);
  2821. hjpeg->JpegOutCount = hjpeg->OutDataLength - ((hdma->Instance->NDTR & DMA_SxNDT) << 2);
  2822. /*Output Buffer is full, call HAL_JPEG_DataReadyCallback*/
  2823. HAL_JPEG_DataReadyCallback (hjpeg, hjpeg->pJpegOutBuffPtr, hjpeg->JpegOutCount);
  2824. if((hjpeg->Context & JPEG_CONTEXT_PAUSE_OUTPUT) == 0)
  2825. {
  2826. /* Start DMA FIFO Out transfer */
  2827. HAL_DMA_Start_IT(hjpeg->hdmaout, (uint32_t)&hjpeg->Instance->DOR, (uint32_t)hjpeg->pJpegOutBuffPtr, hjpeg->OutDataLength >> 2);
  2828. JPEG_ENABLE_DMA(hjpeg,JPEG_DMA_ODMA);
  2829. }
  2830. }
  2831. /* JPEG Conversion still on going : Enable the JPEG IT */
  2832. __HAL_JPEG_ENABLE_IT(hjpeg,JPEG_IT_EOC |JPEG_IT_HPD);
  2833. }
  2834. }
  2835. /**
  2836. * @brief DMA Transfer error callback
  2837. * @param hdma pointer to a DMA_HandleTypeDef structure.
  2838. * @retval None
  2839. */
  2840. static void JPEG_DMAErrorCallback(DMA_HandleTypeDef *hdma)
  2841. {
  2842. JPEG_HandleTypeDef* hjpeg = (JPEG_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
  2843. /* if DMA error is FIFO error ignore it */
  2844. if(HAL_DMA_GetError(hdma) != HAL_DMA_ERROR_FE)
  2845. {
  2846. /*Stop Encoding/Decoding*/
  2847. hjpeg->Instance->CONFR0 &= ~JPEG_CONFR0_START;
  2848. /* Disable All Interrupts */
  2849. __HAL_JPEG_DISABLE_IT(hjpeg,JPEG_INTERRUPT_MASK);
  2850. /* Disable All DMA requests */
  2851. JPEG_DISABLE_DMA(hjpeg,JPEG_DMA_MASK);
  2852. hjpeg->State= HAL_JPEG_STATE_READY;
  2853. hjpeg->ErrorCode |= HAL_JPEG_ERROR_DMA;
  2854. HAL_JPEG_ErrorCallback(hjpeg);
  2855. }
  2856. }
  2857. /**
  2858. * @brief DMA output Abort callback
  2859. * @param hdma pointer to a DMA_HandleTypeDef structure.
  2860. * @retval None
  2861. */
  2862. static void JPEG_DMAOutAbortCallback(DMA_HandleTypeDef *hdma)
  2863. {
  2864. JPEG_HandleTypeDef* hjpeg = (JPEG_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
  2865. if((hjpeg->Context & JPEG_CONTEXT_ENDING_DMA) != 0)
  2866. {
  2867. JPEG_DMA_EndProcess(hjpeg);
  2868. }
  2869. }
  2870. /**
  2871. * @brief Calculate the decoded image quality (from 1 to 100)
  2872. * @param hjpeg pointer to a JPEG_HandleTypeDef structure that contains
  2873. * the configuration information for JPEG module
  2874. * @retval JPEG image quality from 1 to 100.
  2875. */
  2876. static uint32_t JPEG_GetQuality(JPEG_HandleTypeDef *hjpeg)
  2877. {
  2878. uint32_t quality = 0;
  2879. uint32_t quantRow, quantVal,scale, i, j;
  2880. uint32_t *tableAddress = (uint32_t *)hjpeg->Instance->QMEM0;
  2881. i = 0;
  2882. while( i < JPEG_QUANT_TABLE_SIZE)
  2883. {
  2884. quantRow = *tableAddress;
  2885. for(j=0; j<4; j++)
  2886. {
  2887. quantVal = (quantRow >> (8 * j)) & 0xFF;
  2888. if(quantVal == 1)
  2889. {
  2890. /* if Quantization value = 1 then quality is 100%*/
  2891. quality += 100;
  2892. }
  2893. else
  2894. {
  2895. /* Note that the quantization coefficients must be specified in the table in zigzag order */
  2896. scale = (quantVal*100)/((uint32_t) JPEG_LUM_QuantTable[JPEG_ZIGZAG_ORDER[i+j]]);
  2897. if(scale <= 100)
  2898. {
  2899. quality += (200 - scale)/2;
  2900. }
  2901. else
  2902. {
  2903. quality += 5000/scale;
  2904. }
  2905. }
  2906. }
  2907. i += 4;
  2908. tableAddress ++;
  2909. }
  2910. return (quality/((uint32_t)64));
  2911. }
  2912. /**
  2913. * @}
  2914. */
  2915. #endif /* STM32F767xx || STM32F769xx || STM32F777xx || STM32F779xx */
  2916. #endif /* HAL_JPEG_MODULE_ENABLED */
  2917. /**
  2918. * @}
  2919. */
  2920. /**
  2921. * @}
  2922. */
  2923. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/