stm32h7xx_hal_jpeg.c 119 KB

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