stm32f4xx_hal_qspi.c 91 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781
  1. /**
  2. ******************************************************************************
  3. * @file stm32f4xx_hal_qspi.c
  4. * @author MCD Application Team
  5. * @brief QSPI HAL module driver.
  6. * This file provides firmware functions to manage the following
  7. * functionalities of the QuadSPI interface (QSPI).
  8. * + Initialization and de-initialization functions
  9. * + Indirect functional mode management
  10. * + Memory-mapped functional mode management
  11. * + Auto-polling functional mode management
  12. * + Interrupts and flags management
  13. * + DMA channel configuration for indirect functional mode
  14. * + Errors management and abort functionality
  15. *
  16. *
  17. @verbatim
  18. ===============================================================================
  19. ##### How to use this driver #####
  20. ===============================================================================
  21. [..]
  22. *** Initialization ***
  23. ======================
  24. [..]
  25. (#) As prerequisite, fill in the HAL_QSPI_MspInit() :
  26. (++) Enable QuadSPI clock interface with __HAL_RCC_QSPI_CLK_ENABLE().
  27. (++) Reset QuadSPI IP with __HAL_RCC_QSPI_FORCE_RESET() and __HAL_RCC_QSPI_RELEASE_RESET().
  28. (++) Enable the clocks for the QuadSPI GPIOS with __HAL_RCC_GPIOx_CLK_ENABLE().
  29. (++) Configure these QuadSPI pins in alternate mode using HAL_GPIO_Init().
  30. (++) If interrupt mode is used, enable and configure QuadSPI global
  31. interrupt with HAL_NVIC_SetPriority() and HAL_NVIC_EnableIRQ().
  32. (++) If DMA mode is used, enable the clocks for the QuadSPI DMA channel
  33. with __HAL_RCC_DMAx_CLK_ENABLE(), configure DMA with HAL_DMA_Init(),
  34. link it with QuadSPI handle using __HAL_LINKDMA(), enable and configure
  35. DMA channel global interrupt with HAL_NVIC_SetPriority() and HAL_NVIC_EnableIRQ().
  36. (#) Configure the flash size, the clock prescaler, the fifo threshold, the
  37. clock mode, the sample shifting and the CS high time using the HAL_QSPI_Init() function.
  38. *** Indirect functional mode ***
  39. ================================
  40. [..]
  41. (#) Configure the command sequence using the HAL_QSPI_Command() or HAL_QSPI_Command_IT()
  42. functions :
  43. (++) Instruction phase : the mode used and if present the instruction opcode.
  44. (++) Address phase : the mode used and if present the size and the address value.
  45. (++) Alternate-bytes phase : the mode used and if present the size and the alternate
  46. bytes values.
  47. (++) Dummy-cycles phase : the number of dummy cycles (mode used is same as data phase).
  48. (++) Data phase : the mode used and if present the number of bytes.
  49. (++) Double Data Rate (DDR) mode : the activation (or not) of this mode and the delay
  50. if activated.
  51. (++) Sending Instruction Only Once (SIOO) mode : the activation (or not) of this mode.
  52. (#) If no data is required for the command, it is sent directly to the memory :
  53. (++) In polling mode, the output of the function is done when the transfer is complete.
  54. (++) In interrupt mode, HAL_QSPI_CmdCpltCallback() will be called when the transfer is complete.
  55. (#) For the indirect write mode, use HAL_QSPI_Transmit(), HAL_QSPI_Transmit_DMA() or
  56. HAL_QSPI_Transmit_IT() after the command configuration :
  57. (++) In polling mode, the output of the function is done when the transfer is complete.
  58. (++) In interrupt mode, HAL_QSPI_FifoThresholdCallback() will be called when the fifo threshold
  59. is reached and HAL_QSPI_TxCpltCallback() will be called when the transfer is complete.
  60. (++) In DMA mode, HAL_QSPI_TxHalfCpltCallback() will be called at the half transfer and
  61. HAL_QSPI_TxCpltCallback() will be called when the transfer is complete.
  62. (#) For the indirect read mode, use HAL_QSPI_Receive(), HAL_QSPI_Receive_DMA() or
  63. HAL_QSPI_Receive_IT() after the command configuration :
  64. (++) In polling mode, the output of the function is done when the transfer is complete.
  65. (++) In interrupt mode, HAL_QSPI_FifoThresholdCallback() will be called when the fifo threshold
  66. is reached and HAL_QSPI_RxCpltCallback() will be called when the transfer is complete.
  67. (++) In DMA mode, HAL_QSPI_RxHalfCpltCallback() will be called at the half transfer and
  68. HAL_QSPI_RxCpltCallback() will be called when the transfer is complete.
  69. *** Auto-polling functional mode ***
  70. ====================================
  71. [..]
  72. (#) Configure the command sequence and the auto-polling functional mode using the
  73. HAL_QSPI_AutoPolling() or HAL_QSPI_AutoPolling_IT() functions :
  74. (++) Instruction phase : the mode used and if present the instruction opcode.
  75. (++) Address phase : the mode used and if present the size and the address value.
  76. (++) Alternate-bytes phase : the mode used and if present the size and the alternate
  77. bytes values.
  78. (++) Dummy-cycles phase : the number of dummy cycles (mode used is same as data phase).
  79. (++) Data phase : the mode used.
  80. (++) Double Data Rate (DDR) mode : the activation (or not) of this mode and the delay
  81. if activated.
  82. (++) Sending Instruction Only Once (SIOO) mode : the activation (or not) of this mode.
  83. (++) The size of the status bytes, the match value, the mask used, the match mode (OR/AND),
  84. the polling interval and the automatic stop activation.
  85. (#) After the configuration :
  86. (++) In polling mode, the output of the function is done when the status match is reached. The
  87. automatic stop is activated to avoid an infinite loop.
  88. (++) In interrupt mode, HAL_QSPI_StatusMatchCallback() will be called each time the status match is reached.
  89. *** Memory-mapped functional mode ***
  90. =====================================
  91. [..]
  92. (#) Configure the command sequence and the memory-mapped functional mode using the
  93. HAL_QSPI_MemoryMapped() functions :
  94. (++) Instruction phase : the mode used and if present the instruction opcode.
  95. (++) Address phase : the mode used and the size.
  96. (++) Alternate-bytes phase : the mode used and if present the size and the alternate
  97. bytes values.
  98. (++) Dummy-cycles phase : the number of dummy cycles (mode used is same as data phase).
  99. (++) Data phase : the mode used.
  100. (++) Double Data Rate (DDR) mode : the activation (or not) of this mode and the delay
  101. if activated.
  102. (++) Sending Instruction Only Once (SIOO) mode : the activation (or not) of this mode.
  103. (++) The timeout activation and the timeout period.
  104. (#) After the configuration, the QuadSPI will be used as soon as an access on the AHB is done on
  105. the address range. HAL_QSPI_TimeOutCallback() will be called when the timeout expires.
  106. *** Errors management and abort functionality ***
  107. ==================================================
  108. [..]
  109. (#) HAL_QSPI_GetError() function gives the error raised during the last operation.
  110. (#) HAL_QSPI_Abort() and HAL_QSPI_AbortIT() functions aborts any on-going operation and
  111. flushes the fifo :
  112. (++) In polling mode, the output of the function is done when the transfer
  113. complete bit is set and the busy bit cleared.
  114. (++) In interrupt mode, HAL_QSPI_AbortCpltCallback() will be called when
  115. the transfer complete bi is set.
  116. *** Control functions ***
  117. =========================
  118. [..]
  119. (#) HAL_QSPI_GetState() function gives the current state of the HAL QuadSPI driver.
  120. (#) HAL_QSPI_SetTimeout() function configures the timeout value used in the driver.
  121. (#) HAL_QSPI_SetFifoThreshold() function configures the threshold on the Fifo of the QSPI IP.
  122. (#) HAL_QSPI_GetFifoThreshold() function gives the current of the Fifo's threshold
  123. *** Callback registration ***
  124. =============================================
  125. [..]
  126. The compilation define USE_HAL_QSPI_REGISTER_CALLBACKS when set to 1
  127. allows the user to configure dynamically the driver callbacks.
  128. Use Functions @ref HAL_QSPI_RegisterCallback() to register a user callback,
  129. it allows to register following callbacks:
  130. (+) ErrorCallback : callback when error occurs.
  131. (+) AbortCpltCallback : callback when abort is completed.
  132. (+) FifoThresholdCallback : callback when the fifo threshold is reached.
  133. (+) CmdCpltCallback : callback when a command without data is completed.
  134. (+) RxCpltCallback : callback when a reception transfer is completed.
  135. (+) TxCpltCallback : callback when a transmission transfer is completed.
  136. (+) RxHalfCpltCallback : callback when half of the reception transfer is completed.
  137. (+) TxHalfCpltCallback : callback when half of the transmission transfer is completed.
  138. (+) StatusMatchCallback : callback when a status match occurs.
  139. (+) TimeOutCallback : callback when the timeout perioed expires.
  140. (+) MspInitCallback : QSPI MspInit.
  141. (+) MspDeInitCallback : QSPI MspDeInit.
  142. This function takes as parameters the HAL peripheral handle, the Callback ID
  143. and a pointer to the user callback function.
  144. Use function @ref HAL_QSPI_UnRegisterCallback() to reset a callback to the default
  145. weak (surcharged) function. It allows to reset following callbacks:
  146. (+) ErrorCallback : callback when error occurs.
  147. (+) AbortCpltCallback : callback when abort is completed.
  148. (+) FifoThresholdCallback : callback when the fifo threshold is reached.
  149. (+) CmdCpltCallback : callback when a command without data is completed.
  150. (+) RxCpltCallback : callback when a reception transfer is completed.
  151. (+) TxCpltCallback : callback when a transmission transfer is completed.
  152. (+) RxHalfCpltCallback : callback when half of the reception transfer is completed.
  153. (+) TxHalfCpltCallback : callback when half of the transmission transfer is completed.
  154. (+) StatusMatchCallback : callback when a status match occurs.
  155. (+) TimeOutCallback : callback when the timeout perioed expires.
  156. (+) MspInitCallback : QSPI MspInit.
  157. (+) MspDeInitCallback : QSPI MspDeInit.
  158. This function) takes as parameters the HAL peripheral handle and the Callback ID.
  159. By default, after the @ref HAL_QSPI_Init and if the state is HAL_QSPI_STATE_RESET
  160. all callbacks are reset to the corresponding legacy weak (surcharged) functions.
  161. Exception done for MspInit and MspDeInit callbacks that are respectively
  162. reset to the legacy weak (surcharged) functions in the @ref HAL_QSPI_Init
  163. and @ref HAL_QSPI_DeInit only when these callbacks are null (not registered beforehand).
  164. If not, MspInit or MspDeInit are not null, the @ref HAL_QSPI_Init and @ref HAL_QSPI_DeInit
  165. keep and use the user MspInit/MspDeInit callbacks (registered beforehand)
  166. Callbacks can be registered/unregistered in READY state only.
  167. Exception done for MspInit/MspDeInit callbacks that can be registered/unregistered
  168. in READY or RESET state, thus registered (user) MspInit/DeInit callbacks can be used
  169. during the Init/DeInit.
  170. In that case first register the MspInit/MspDeInit user callbacks
  171. using @ref HAL_QSPI_RegisterCallback before calling @ref HAL_QSPI_DeInit
  172. or @ref HAL_QSPI_Init function.
  173. When The compilation define USE_HAL_QSPI_REGISTER_CALLBACKS is set to 0 or
  174. not defined, the callback registering feature is not available
  175. and weak (surcharged) callbacks are used.
  176. *** Workarounds linked to Silicon Limitation ***
  177. ====================================================
  178. [..]
  179. (#) Workarounds Implemented inside HAL Driver
  180. (++) Extra data written in the FIFO at the end of a read transfer
  181. @endverbatim
  182. ******************************************************************************
  183. * @attention
  184. *
  185. * <h2><center>&copy; Copyright (c) 2017 STMicroelectronics.
  186. * All rights reserved.</center></h2>
  187. *
  188. * This software component is licensed by ST under BSD 3-Clause license,
  189. * the "License"; You may not use this file except in compliance with the
  190. * License. You may obtain a copy of the License at:
  191. * opensource.org/licenses/BSD-3-Clause
  192. *
  193. ******************************************************************************
  194. */
  195. /* Includes ------------------------------------------------------------------*/
  196. #include "stm32f4xx_hal.h"
  197. /** @addtogroup STM32F4xx_HAL_Driver
  198. * @{
  199. */
  200. /** @defgroup QSPI QSPI
  201. * @brief QSPI HAL module driver
  202. * @{
  203. */
  204. #ifdef HAL_QSPI_MODULE_ENABLED
  205. #if defined(STM32F446xx) || defined(STM32F469xx) || defined(STM32F479xx) || defined(STM32F412Zx) || defined(STM32F412Vx) || \
  206. defined(STM32F412Rx) || defined(STM32F413xx) || defined(STM32F423xx)
  207. /* Private typedef -----------------------------------------------------------*/
  208. /* Private define ------------------------------------------------------------*/
  209. /** @addtogroup QSPI_Private_Constants
  210. * @{
  211. */
  212. #define QSPI_FUNCTIONAL_MODE_INDIRECT_WRITE 0x00000000U /*!<Indirect write mode*/
  213. #define QSPI_FUNCTIONAL_MODE_INDIRECT_READ ((uint32_t)QUADSPI_CCR_FMODE_0) /*!<Indirect read mode*/
  214. #define QSPI_FUNCTIONAL_MODE_AUTO_POLLING ((uint32_t)QUADSPI_CCR_FMODE_1) /*!<Automatic polling mode*/
  215. #define QSPI_FUNCTIONAL_MODE_MEMORY_MAPPED ((uint32_t)QUADSPI_CCR_FMODE) /*!<Memory-mapped mode*/
  216. /**
  217. * @}
  218. */
  219. /* Private macro -------------------------------------------------------------*/
  220. /** @addtogroup QSPI_Private_Macros QSPI Private Macros
  221. * @{
  222. */
  223. #define IS_QSPI_FUNCTIONAL_MODE(MODE) (((MODE) == QSPI_FUNCTIONAL_MODE_INDIRECT_WRITE) || \
  224. ((MODE) == QSPI_FUNCTIONAL_MODE_INDIRECT_READ) || \
  225. ((MODE) == QSPI_FUNCTIONAL_MODE_AUTO_POLLING) || \
  226. ((MODE) == QSPI_FUNCTIONAL_MODE_MEMORY_MAPPED))
  227. /**
  228. * @}
  229. */
  230. /* Private variables ---------------------------------------------------------*/
  231. /* Private function prototypes -----------------------------------------------*/
  232. /** @addtogroup QSPI_Private_Functions QSPI Private Functions
  233. * @{
  234. */
  235. static void QSPI_DMARxCplt(DMA_HandleTypeDef *hdma);
  236. static void QSPI_DMATxCplt(DMA_HandleTypeDef *hdma);
  237. static void QSPI_DMARxHalfCplt(DMA_HandleTypeDef *hdma);
  238. static void QSPI_DMATxHalfCplt(DMA_HandleTypeDef *hdma);
  239. static void QSPI_DMAError(DMA_HandleTypeDef *hdma);
  240. static void QSPI_DMAAbortCplt(DMA_HandleTypeDef *hdma);
  241. static HAL_StatusTypeDef QSPI_WaitFlagStateUntilTimeout(QSPI_HandleTypeDef *hqspi, uint32_t Flag, FlagStatus State, uint32_t tickstart, uint32_t Timeout);
  242. static void QSPI_Config(QSPI_HandleTypeDef *hqspi, QSPI_CommandTypeDef *cmd, uint32_t FunctionalMode);
  243. /**
  244. * @}
  245. */
  246. /* Exported functions ---------------------------------------------------------*/
  247. /** @defgroup QSPI_Exported_Functions QSPI Exported Functions
  248. * @{
  249. */
  250. /** @defgroup QSPI_Exported_Functions_Group1 Initialization/de-initialization functions
  251. * @brief Initialization and Configuration functions
  252. *
  253. @verbatim
  254. ===============================================================================
  255. ##### Initialization and Configuration functions #####
  256. ===============================================================================
  257. [..]
  258. This subsection provides a set of functions allowing to :
  259. (+) Initialize the QuadSPI.
  260. (+) De-initialize the QuadSPI.
  261. @endverbatim
  262. * @{
  263. */
  264. /**
  265. * @brief Initializes the QSPI mode according to the specified parameters
  266. * in the QSPI_InitTypeDef and creates the associated handle.
  267. * @param hqspi qspi handle
  268. * @retval HAL status
  269. */
  270. HAL_StatusTypeDef HAL_QSPI_Init(QSPI_HandleTypeDef *hqspi)
  271. {
  272. HAL_StatusTypeDef status = HAL_ERROR;
  273. uint32_t tickstart = HAL_GetTick();
  274. /* Check the QSPI handle allocation */
  275. if(hqspi == NULL)
  276. {
  277. return HAL_ERROR;
  278. }
  279. /* Check the parameters */
  280. assert_param(IS_QSPI_ALL_INSTANCE(hqspi->Instance));
  281. assert_param(IS_QSPI_CLOCK_PRESCALER(hqspi->Init.ClockPrescaler));
  282. assert_param(IS_QSPI_FIFO_THRESHOLD(hqspi->Init.FifoThreshold));
  283. assert_param(IS_QSPI_SSHIFT(hqspi->Init.SampleShifting));
  284. assert_param(IS_QSPI_FLASH_SIZE(hqspi->Init.FlashSize));
  285. assert_param(IS_QSPI_CS_HIGH_TIME(hqspi->Init.ChipSelectHighTime));
  286. assert_param(IS_QSPI_CLOCK_MODE(hqspi->Init.ClockMode));
  287. assert_param(IS_QSPI_DUAL_FLASH_MODE(hqspi->Init.DualFlash));
  288. if (hqspi->Init.DualFlash != QSPI_DUALFLASH_ENABLE )
  289. {
  290. assert_param(IS_QSPI_FLASH_ID(hqspi->Init.FlashID));
  291. }
  292. if(hqspi->State == HAL_QSPI_STATE_RESET)
  293. {
  294. /* Allocate lock resource and initialize it */
  295. hqspi->Lock = HAL_UNLOCKED;
  296. #if (USE_HAL_QSPI_REGISTER_CALLBACKS == 1)
  297. /* Reset Callback pointers in HAL_QSPI_STATE_RESET only */
  298. hqspi->ErrorCallback = HAL_QSPI_ErrorCallback;
  299. hqspi->AbortCpltCallback = HAL_QSPI_AbortCpltCallback;
  300. hqspi->FifoThresholdCallback = HAL_QSPI_FifoThresholdCallback;
  301. hqspi->CmdCpltCallback = HAL_QSPI_CmdCpltCallback;
  302. hqspi->RxCpltCallback = HAL_QSPI_RxCpltCallback;
  303. hqspi->TxCpltCallback = HAL_QSPI_TxCpltCallback;
  304. hqspi->RxHalfCpltCallback = HAL_QSPI_RxHalfCpltCallback;
  305. hqspi->TxHalfCpltCallback = HAL_QSPI_TxHalfCpltCallback;
  306. hqspi->StatusMatchCallback = HAL_QSPI_StatusMatchCallback;
  307. hqspi->TimeOutCallback = HAL_QSPI_TimeOutCallback;
  308. if(hqspi->MspInitCallback == NULL)
  309. {
  310. hqspi->MspInitCallback = HAL_QSPI_MspInit;
  311. }
  312. /* Init the low level hardware */
  313. hqspi->MspInitCallback(hqspi);
  314. #else
  315. /* Init the low level hardware : GPIO, CLOCK */
  316. HAL_QSPI_MspInit(hqspi);
  317. #endif
  318. /* Configure the default timeout for the QSPI memory access */
  319. HAL_QSPI_SetTimeout(hqspi, HAL_QPSI_TIMEOUT_DEFAULT_VALUE);
  320. }
  321. /* Configure QSPI FIFO Threshold */
  322. MODIFY_REG(hqspi->Instance->CR, QUADSPI_CR_FTHRES, ((hqspi->Init.FifoThreshold - 1U) << 8U));
  323. /* Wait till BUSY flag reset */
  324. status = QSPI_WaitFlagStateUntilTimeout(hqspi, QSPI_FLAG_BUSY, RESET, tickstart, hqspi->Timeout);
  325. if(status == HAL_OK)
  326. {
  327. /* Configure QSPI Clock Prescaler and Sample Shift */
  328. MODIFY_REG(hqspi->Instance->CR,(QUADSPI_CR_PRESCALER | QUADSPI_CR_SSHIFT | QUADSPI_CR_FSEL | QUADSPI_CR_DFM), ((hqspi->Init.ClockPrescaler << 24U)| hqspi->Init.SampleShifting | hqspi->Init.FlashID| hqspi->Init.DualFlash ));
  329. /* Configure QSPI Flash Size, CS High Time and Clock Mode */
  330. MODIFY_REG(hqspi->Instance->DCR, (QUADSPI_DCR_FSIZE | QUADSPI_DCR_CSHT | QUADSPI_DCR_CKMODE),
  331. ((hqspi->Init.FlashSize << 16U) | hqspi->Init.ChipSelectHighTime | hqspi->Init.ClockMode));
  332. /* Enable the QSPI peripheral */
  333. __HAL_QSPI_ENABLE(hqspi);
  334. /* Set QSPI error code to none */
  335. hqspi->ErrorCode = HAL_QSPI_ERROR_NONE;
  336. /* Initialize the QSPI state */
  337. hqspi->State = HAL_QSPI_STATE_READY;
  338. }
  339. /* Release Lock */
  340. __HAL_UNLOCK(hqspi);
  341. /* Return function status */
  342. return status;
  343. }
  344. /**
  345. * @brief DeInitializes the QSPI peripheral
  346. * @param hqspi qspi handle
  347. * @retval HAL status
  348. */
  349. HAL_StatusTypeDef HAL_QSPI_DeInit(QSPI_HandleTypeDef *hqspi)
  350. {
  351. /* Check the QSPI handle allocation */
  352. if(hqspi == NULL)
  353. {
  354. return HAL_ERROR;
  355. }
  356. /* Disable the QSPI Peripheral Clock */
  357. __HAL_QSPI_DISABLE(hqspi);
  358. #if (USE_HAL_QSPI_REGISTER_CALLBACKS == 1)
  359. if(hqspi->MspDeInitCallback == NULL)
  360. {
  361. hqspi->MspDeInitCallback = HAL_QSPI_MspDeInit;
  362. }
  363. /* DeInit the low level hardware */
  364. hqspi->MspDeInitCallback(hqspi);
  365. #else
  366. /* DeInit the low level hardware: GPIO, CLOCK, NVIC... */
  367. HAL_QSPI_MspDeInit(hqspi);
  368. #endif
  369. /* Set QSPI error code to none */
  370. hqspi->ErrorCode = HAL_QSPI_ERROR_NONE;
  371. /* Initialize the QSPI state */
  372. hqspi->State = HAL_QSPI_STATE_RESET;
  373. /* Release Lock */
  374. __HAL_UNLOCK(hqspi);
  375. return HAL_OK;
  376. }
  377. /**
  378. * @brief QSPI MSP Init
  379. * @param hqspi QSPI handle
  380. * @retval None
  381. */
  382. __weak void HAL_QSPI_MspInit(QSPI_HandleTypeDef *hqspi)
  383. {
  384. /* Prevent unused argument(s) compilation warning */
  385. UNUSED(hqspi);
  386. /* NOTE : This function should not be modified, when the callback is needed,
  387. the HAL_QSPI_MspInit can be implemented in the user file
  388. */
  389. }
  390. /**
  391. * @brief QSPI MSP DeInit
  392. * @param hqspi QSPI handle
  393. * @retval None
  394. */
  395. __weak void HAL_QSPI_MspDeInit(QSPI_HandleTypeDef *hqspi)
  396. {
  397. /* Prevent unused argument(s) compilation warning */
  398. UNUSED(hqspi);
  399. /* NOTE : This function should not be modified, when the callback is needed,
  400. the HAL_QSPI_MspDeInit can be implemented in the user file
  401. */
  402. }
  403. /**
  404. * @}
  405. */
  406. /** @defgroup QSPI_Exported_Functions_Group2 IO operation functions
  407. * @brief QSPI Transmit/Receive functions
  408. *
  409. @verbatim
  410. ===============================================================================
  411. ##### IO operation functions #####
  412. ===============================================================================
  413. [..]
  414. This subsection provides a set of functions allowing to :
  415. (+) Handle the interrupts.
  416. (+) Handle the command sequence.
  417. (+) Transmit data in blocking, interrupt or DMA mode.
  418. (+) Receive data in blocking, interrupt or DMA mode.
  419. (+) Manage the auto-polling functional mode.
  420. (+) Manage the memory-mapped functional mode.
  421. @endverbatim
  422. * @{
  423. */
  424. /**
  425. * @brief This function handles QSPI interrupt request.
  426. * @param hqspi QSPI handle
  427. * @retval None.
  428. */
  429. void HAL_QSPI_IRQHandler(QSPI_HandleTypeDef *hqspi)
  430. {
  431. __IO uint32_t *data_reg;
  432. uint32_t flag = READ_REG(hqspi->Instance->SR);
  433. uint32_t itsource = READ_REG(hqspi->Instance->CR);
  434. /* QSPI Fifo Threshold interrupt occurred ----------------------------------*/
  435. if(((flag & QSPI_FLAG_FT)!= RESET) && ((itsource & QSPI_IT_FT)!= RESET))
  436. {
  437. data_reg = &hqspi->Instance->DR;
  438. if(hqspi->State == HAL_QSPI_STATE_BUSY_INDIRECT_TX)
  439. {
  440. /* Transmission process */
  441. while(__HAL_QSPI_GET_FLAG(hqspi, QSPI_FLAG_FT) != 0U)
  442. {
  443. if (hqspi->TxXferCount > 0U)
  444. {
  445. /* Fill the FIFO until it is full */
  446. *(__IO uint8_t *)data_reg = *hqspi->pTxBuffPtr++;
  447. hqspi->TxXferCount--;
  448. }
  449. else
  450. {
  451. /* No more data available for the transfer */
  452. /* Disable the QSPI FIFO Threshold Interrupt */
  453. __HAL_QSPI_DISABLE_IT(hqspi, QSPI_IT_FT);
  454. break;
  455. }
  456. }
  457. }
  458. else if(hqspi->State == HAL_QSPI_STATE_BUSY_INDIRECT_RX)
  459. {
  460. /* Receiving Process */
  461. while(__HAL_QSPI_GET_FLAG(hqspi, QSPI_FLAG_FT) != 0U)
  462. {
  463. if (hqspi->RxXferCount > 0U)
  464. {
  465. /* Read the FIFO until it is empty */
  466. *hqspi->pRxBuffPtr++ = *(__IO uint8_t *)data_reg;
  467. hqspi->RxXferCount--;
  468. }
  469. else
  470. {
  471. /* All data have been received for the transfer */
  472. /* Disable the QSPI FIFO Threshold Interrupt */
  473. __HAL_QSPI_DISABLE_IT(hqspi, QSPI_IT_FT);
  474. break;
  475. }
  476. }
  477. }
  478. /* FIFO Threshold callback */
  479. #if (USE_HAL_QSPI_REGISTER_CALLBACKS == 1)
  480. hqspi->FifoThresholdCallback(hqspi);
  481. #else
  482. HAL_QSPI_FifoThresholdCallback(hqspi);
  483. #endif
  484. }
  485. /* QSPI Transfer Complete interrupt occurred -------------------------------*/
  486. else if(((flag & QSPI_FLAG_TC)!= RESET) && ((itsource & QSPI_IT_TC)!= RESET))
  487. {
  488. /* Clear interrupt */
  489. WRITE_REG(hqspi->Instance->FCR, QSPI_FLAG_TC);
  490. /* Disable the QSPI FIFO Threshold, Transfer Error and Transfer complete Interrupts */
  491. __HAL_QSPI_DISABLE_IT(hqspi, QSPI_IT_TC | QSPI_IT_TE | QSPI_IT_FT);
  492. /* Transfer complete callback */
  493. if(hqspi->State == HAL_QSPI_STATE_BUSY_INDIRECT_TX)
  494. {
  495. if ((hqspi->Instance->CR & QUADSPI_CR_DMAEN)!= RESET)
  496. {
  497. /* Disable the DMA transfer by clearing the DMAEN bit in the QSPI CR register */
  498. CLEAR_BIT(hqspi->Instance->CR, QUADSPI_CR_DMAEN);
  499. /* Disable the DMA channel */
  500. __HAL_DMA_DISABLE(hqspi->hdma);
  501. }
  502. /* Clear Busy bit */
  503. HAL_QSPI_Abort_IT(hqspi);
  504. /* Change state of QSPI */
  505. hqspi->State = HAL_QSPI_STATE_READY;
  506. /* TX Complete callback */
  507. #if (USE_HAL_QSPI_REGISTER_CALLBACKS == 1)
  508. hqspi->TxCpltCallback(hqspi);
  509. #else
  510. HAL_QSPI_TxCpltCallback(hqspi);
  511. #endif
  512. }
  513. else if(hqspi->State == HAL_QSPI_STATE_BUSY_INDIRECT_RX)
  514. {
  515. if ((hqspi->Instance->CR & QUADSPI_CR_DMAEN)!= RESET)
  516. {
  517. /* Disable the DMA transfer by clearing the DMAEN bit in the QSPI CR register */
  518. CLEAR_BIT(hqspi->Instance->CR, QUADSPI_CR_DMAEN);
  519. /* Disable the DMA channel */
  520. __HAL_DMA_DISABLE(hqspi->hdma);
  521. }
  522. else
  523. {
  524. data_reg = &hqspi->Instance->DR;
  525. while(READ_BIT(hqspi->Instance->SR, QUADSPI_SR_FLEVEL) != 0U)
  526. {
  527. if (hqspi->RxXferCount > 0U)
  528. {
  529. /* Read the last data received in the FIFO until it is empty */
  530. *hqspi->pRxBuffPtr++ = *(__IO uint8_t *)data_reg;
  531. hqspi->RxXferCount--;
  532. }
  533. else
  534. {
  535. /* All data have been received for the transfer */
  536. break;
  537. }
  538. }
  539. }
  540. /* Workaround - Extra data written in the FIFO at the end of a read transfer */
  541. HAL_QSPI_Abort_IT(hqspi);
  542. /* Change state of QSPI */
  543. hqspi->State = HAL_QSPI_STATE_READY;
  544. /* RX Complete callback */
  545. #if (USE_HAL_QSPI_REGISTER_CALLBACKS == 1)
  546. hqspi->RxCpltCallback(hqspi);
  547. #else
  548. HAL_QSPI_RxCpltCallback(hqspi);
  549. #endif
  550. }
  551. else if(hqspi->State == HAL_QSPI_STATE_BUSY)
  552. {
  553. /* Change state of QSPI */
  554. hqspi->State = HAL_QSPI_STATE_READY;
  555. /* Command Complete callback */
  556. #if (USE_HAL_QSPI_REGISTER_CALLBACKS == 1)
  557. hqspi->CmdCpltCallback(hqspi);
  558. #else
  559. HAL_QSPI_CmdCpltCallback(hqspi);
  560. #endif
  561. }
  562. else if(hqspi->State == HAL_QSPI_STATE_ABORT)
  563. {
  564. /* Change state of QSPI */
  565. hqspi->State = HAL_QSPI_STATE_READY;
  566. if (hqspi->ErrorCode == HAL_QSPI_ERROR_NONE)
  567. {
  568. /* Abort called by the user */
  569. /* Abort Complete callback */
  570. #if (USE_HAL_QSPI_REGISTER_CALLBACKS == 1)
  571. hqspi->AbortCpltCallback(hqspi);
  572. #else
  573. HAL_QSPI_AbortCpltCallback(hqspi);
  574. #endif
  575. }
  576. else
  577. {
  578. /* Abort due to an error (eg : DMA error) */
  579. /* Error callback */
  580. #if (USE_HAL_QSPI_REGISTER_CALLBACKS == 1)
  581. hqspi->ErrorCallback(hqspi);
  582. #else
  583. HAL_QSPI_ErrorCallback(hqspi);
  584. #endif
  585. }
  586. }
  587. }
  588. /* QSPI Status Match interrupt occurred ------------------------------------*/
  589. else if(((flag & QSPI_FLAG_SM)!= RESET) && ((itsource & QSPI_IT_SM)!= RESET))
  590. {
  591. /* Clear interrupt */
  592. WRITE_REG(hqspi->Instance->FCR, QSPI_FLAG_SM);
  593. /* Check if the automatic poll mode stop is activated */
  594. if(READ_BIT(hqspi->Instance->CR, QUADSPI_CR_APMS) != 0U)
  595. {
  596. /* Disable the QSPI Transfer Error and Status Match Interrupts */
  597. __HAL_QSPI_DISABLE_IT(hqspi, (QSPI_IT_SM | QSPI_IT_TE));
  598. /* Change state of QSPI */
  599. hqspi->State = HAL_QSPI_STATE_READY;
  600. }
  601. /* Status match callback */
  602. #if (USE_HAL_QSPI_REGISTER_CALLBACKS == 1)
  603. hqspi->StatusMatchCallback(hqspi);
  604. #else
  605. HAL_QSPI_StatusMatchCallback(hqspi);
  606. #endif
  607. }
  608. /* QSPI Transfer Error interrupt occurred ----------------------------------*/
  609. else if(((flag & QSPI_FLAG_TE)!= RESET) && ((itsource & QSPI_IT_TE)!= RESET))
  610. {
  611. /* Clear interrupt */
  612. WRITE_REG(hqspi->Instance->FCR, QSPI_FLAG_TE);
  613. /* Disable all the QSPI Interrupts */
  614. __HAL_QSPI_DISABLE_IT(hqspi, QSPI_IT_SM | QSPI_IT_TC | QSPI_IT_TE | QSPI_IT_FT);
  615. /* Set error code */
  616. hqspi->ErrorCode |= HAL_QSPI_ERROR_TRANSFER;
  617. if ((hqspi->Instance->CR & QUADSPI_CR_DMAEN)!= RESET)
  618. {
  619. /* Disable the DMA transfer by clearing the DMAEN bit in the QSPI CR register */
  620. CLEAR_BIT(hqspi->Instance->CR, QUADSPI_CR_DMAEN);
  621. /* Disable the DMA channel */
  622. hqspi->hdma->XferAbortCallback = QSPI_DMAAbortCplt;
  623. if (HAL_DMA_Abort_IT(hqspi->hdma) != HAL_OK)
  624. {
  625. /* Set error code to DMA */
  626. hqspi->ErrorCode |= HAL_QSPI_ERROR_DMA;
  627. /* Change state of QSPI */
  628. hqspi->State = HAL_QSPI_STATE_READY;
  629. /* Error callback */
  630. #if (USE_HAL_QSPI_REGISTER_CALLBACKS == 1)
  631. hqspi->ErrorCallback(hqspi);
  632. #else
  633. HAL_QSPI_ErrorCallback(hqspi);
  634. #endif
  635. }
  636. }
  637. else
  638. {
  639. /* Change state of QSPI */
  640. hqspi->State = HAL_QSPI_STATE_READY;
  641. /* Error callback */
  642. #if (USE_HAL_QSPI_REGISTER_CALLBACKS == 1)
  643. hqspi->ErrorCallback(hqspi);
  644. #else
  645. HAL_QSPI_ErrorCallback(hqspi);
  646. #endif
  647. }
  648. }
  649. /* QSPI Timeout interrupt occurred -----------------------------------------*/
  650. else if(((flag & QSPI_FLAG_TO)!= RESET) && ((itsource & QSPI_IT_TO)!= RESET))
  651. {
  652. /* Clear interrupt */
  653. WRITE_REG(hqspi->Instance->FCR, QSPI_FLAG_TO);
  654. /* Timeout callback */
  655. #if (USE_HAL_QSPI_REGISTER_CALLBACKS == 1)
  656. hqspi->TimeOutCallback(hqspi);
  657. #else
  658. HAL_QSPI_TimeOutCallback(hqspi);
  659. #endif
  660. }
  661. }
  662. /**
  663. * @brief Sets the command configuration.
  664. * @param hqspi QSPI handle
  665. * @param cmd structure that contains the command configuration information
  666. * @param Timeout Time out duration
  667. * @note This function is used only in Indirect Read or Write Modes
  668. * @retval HAL status
  669. */
  670. HAL_StatusTypeDef HAL_QSPI_Command(QSPI_HandleTypeDef *hqspi, QSPI_CommandTypeDef *cmd, uint32_t Timeout)
  671. {
  672. HAL_StatusTypeDef status = HAL_ERROR;
  673. uint32_t tickstart = HAL_GetTick();
  674. /* Check the parameters */
  675. assert_param(IS_QSPI_INSTRUCTION_MODE(cmd->InstructionMode));
  676. if (cmd->InstructionMode != QSPI_INSTRUCTION_NONE)
  677. {
  678. assert_param(IS_QSPI_INSTRUCTION(cmd->Instruction));
  679. }
  680. assert_param(IS_QSPI_ADDRESS_MODE(cmd->AddressMode));
  681. if (cmd->AddressMode != QSPI_ADDRESS_NONE)
  682. {
  683. assert_param(IS_QSPI_ADDRESS_SIZE(cmd->AddressSize));
  684. }
  685. assert_param(IS_QSPI_ALTERNATE_BYTES_MODE(cmd->AlternateByteMode));
  686. if (cmd->AlternateByteMode != QSPI_ALTERNATE_BYTES_NONE)
  687. {
  688. assert_param(IS_QSPI_ALTERNATE_BYTES_SIZE(cmd->AlternateBytesSize));
  689. }
  690. assert_param(IS_QSPI_DUMMY_CYCLES(cmd->DummyCycles));
  691. assert_param(IS_QSPI_DATA_MODE(cmd->DataMode));
  692. assert_param(IS_QSPI_DDR_MODE(cmd->DdrMode));
  693. assert_param(IS_QSPI_DDR_HHC(cmd->DdrHoldHalfCycle));
  694. assert_param(IS_QSPI_SIOO_MODE(cmd->SIOOMode));
  695. /* Process locked */
  696. __HAL_LOCK(hqspi);
  697. if(hqspi->State == HAL_QSPI_STATE_READY)
  698. {
  699. hqspi->ErrorCode = HAL_QSPI_ERROR_NONE;
  700. /* Update QSPI state */
  701. hqspi->State = HAL_QSPI_STATE_BUSY;
  702. /* Wait till BUSY flag reset */
  703. status = QSPI_WaitFlagStateUntilTimeout(hqspi, QSPI_FLAG_BUSY, RESET, tickstart, Timeout);
  704. if (status == HAL_OK)
  705. {
  706. /* Call the configuration function */
  707. QSPI_Config(hqspi, cmd, QSPI_FUNCTIONAL_MODE_INDIRECT_WRITE);
  708. if (cmd->DataMode == QSPI_DATA_NONE)
  709. {
  710. /* When there is no data phase, the transfer start as soon as the configuration is done
  711. so wait until TC flag is set to go back in idle state */
  712. status = QSPI_WaitFlagStateUntilTimeout(hqspi, QSPI_FLAG_TC, SET, tickstart, Timeout);
  713. if (status == HAL_OK)
  714. {
  715. __HAL_QSPI_CLEAR_FLAG(hqspi, QSPI_FLAG_TC);
  716. /* Update QSPI state */
  717. hqspi->State = HAL_QSPI_STATE_READY;
  718. }
  719. }
  720. else
  721. {
  722. /* Update QSPI state */
  723. hqspi->State = HAL_QSPI_STATE_READY;
  724. }
  725. }
  726. }
  727. else
  728. {
  729. status = HAL_BUSY;
  730. }
  731. /* Process unlocked */
  732. __HAL_UNLOCK(hqspi);
  733. /* Return function status */
  734. return status;
  735. }
  736. /**
  737. * @brief Sets the command configuration in interrupt mode.
  738. * @param hqspi QSPI handle
  739. * @param cmd structure that contains the command configuration information
  740. * @note This function is used only in Indirect Read or Write Modes
  741. * @retval HAL status
  742. */
  743. HAL_StatusTypeDef HAL_QSPI_Command_IT(QSPI_HandleTypeDef *hqspi, QSPI_CommandTypeDef *cmd)
  744. {
  745. __IO uint32_t count = 0U;
  746. HAL_StatusTypeDef status = HAL_OK;
  747. /* Check the parameters */
  748. assert_param(IS_QSPI_INSTRUCTION_MODE(cmd->InstructionMode));
  749. if (cmd->InstructionMode != QSPI_INSTRUCTION_NONE)
  750. {
  751. assert_param(IS_QSPI_INSTRUCTION(cmd->Instruction));
  752. }
  753. assert_param(IS_QSPI_ADDRESS_MODE(cmd->AddressMode));
  754. if (cmd->AddressMode != QSPI_ADDRESS_NONE)
  755. {
  756. assert_param(IS_QSPI_ADDRESS_SIZE(cmd->AddressSize));
  757. }
  758. assert_param(IS_QSPI_ALTERNATE_BYTES_MODE(cmd->AlternateByteMode));
  759. if (cmd->AlternateByteMode != QSPI_ALTERNATE_BYTES_NONE)
  760. {
  761. assert_param(IS_QSPI_ALTERNATE_BYTES_SIZE(cmd->AlternateBytesSize));
  762. }
  763. assert_param(IS_QSPI_DUMMY_CYCLES(cmd->DummyCycles));
  764. assert_param(IS_QSPI_DATA_MODE(cmd->DataMode));
  765. assert_param(IS_QSPI_DDR_MODE(cmd->DdrMode));
  766. assert_param(IS_QSPI_DDR_HHC(cmd->DdrHoldHalfCycle));
  767. assert_param(IS_QSPI_SIOO_MODE(cmd->SIOOMode));
  768. /* Process locked */
  769. __HAL_LOCK(hqspi);
  770. if(hqspi->State == HAL_QSPI_STATE_READY)
  771. {
  772. hqspi->ErrorCode = HAL_QSPI_ERROR_NONE;
  773. /* Update QSPI state */
  774. hqspi->State = HAL_QSPI_STATE_BUSY;
  775. /* Wait till BUSY flag reset */
  776. count = (hqspi->Timeout) * (SystemCoreClock / 16U / 1000U);
  777. do
  778. {
  779. if (count-- == 0U)
  780. {
  781. hqspi->State = HAL_QSPI_STATE_ERROR;
  782. hqspi->ErrorCode |= HAL_QSPI_ERROR_TIMEOUT;
  783. status = HAL_TIMEOUT;
  784. }
  785. }
  786. while ((__HAL_QSPI_GET_FLAG(hqspi, QSPI_FLAG_BUSY)) != RESET);
  787. if (status == HAL_OK)
  788. {
  789. if (cmd->DataMode == QSPI_DATA_NONE)
  790. {
  791. /* Clear interrupt */
  792. __HAL_QSPI_CLEAR_FLAG(hqspi, QSPI_FLAG_TE | QSPI_FLAG_TC);
  793. }
  794. /* Call the configuration function */
  795. QSPI_Config(hqspi, cmd, QSPI_FUNCTIONAL_MODE_INDIRECT_WRITE);
  796. if (cmd->DataMode == QSPI_DATA_NONE)
  797. {
  798. /* When there is no data phase, the transfer start as soon as the configuration is done
  799. so activate TC and TE interrupts */
  800. /* Process unlocked */
  801. __HAL_UNLOCK(hqspi);
  802. /* Enable the QSPI Transfer Error Interrupt */
  803. __HAL_QSPI_ENABLE_IT(hqspi, QSPI_IT_TE | QSPI_IT_TC);
  804. }
  805. else
  806. {
  807. /* Update QSPI state */
  808. hqspi->State = HAL_QSPI_STATE_READY;
  809. /* Process unlocked */
  810. __HAL_UNLOCK(hqspi);
  811. }
  812. }
  813. else
  814. {
  815. /* Process unlocked */
  816. __HAL_UNLOCK(hqspi);
  817. }
  818. }
  819. else
  820. {
  821. status = HAL_BUSY;
  822. /* Process unlocked */
  823. __HAL_UNLOCK(hqspi);
  824. }
  825. /* Return function status */
  826. return status;
  827. }
  828. /**
  829. * @brief Transmit an amount of data in blocking mode.
  830. * @param hqspi QSPI handle
  831. * @param pData pointer to data buffer
  832. * @param Timeout Time out duration
  833. * @note This function is used only in Indirect Write Mode
  834. * @retval HAL status
  835. */
  836. HAL_StatusTypeDef HAL_QSPI_Transmit(QSPI_HandleTypeDef *hqspi, uint8_t *pData, uint32_t Timeout)
  837. {
  838. HAL_StatusTypeDef status = HAL_OK;
  839. uint32_t tickstart = HAL_GetTick();
  840. __IO uint32_t *data_reg = &hqspi->Instance->DR;
  841. /* Process locked */
  842. __HAL_LOCK(hqspi);
  843. if(hqspi->State == HAL_QSPI_STATE_READY)
  844. {
  845. hqspi->ErrorCode = HAL_QSPI_ERROR_NONE;
  846. if(pData != NULL )
  847. {
  848. /* Update state */
  849. hqspi->State = HAL_QSPI_STATE_BUSY_INDIRECT_TX;
  850. /* Configure counters and size of the handle */
  851. hqspi->TxXferCount = READ_REG(hqspi->Instance->DLR) + 1U;
  852. hqspi->TxXferSize = READ_REG(hqspi->Instance->DLR) + 1U;
  853. hqspi->pTxBuffPtr = pData;
  854. /* Configure QSPI: CCR register with functional as indirect write */
  855. MODIFY_REG(hqspi->Instance->CCR, QUADSPI_CCR_FMODE, QSPI_FUNCTIONAL_MODE_INDIRECT_WRITE);
  856. while(hqspi->TxXferCount > 0U)
  857. {
  858. /* Wait until FT flag is set to send data */
  859. status = QSPI_WaitFlagStateUntilTimeout(hqspi, QSPI_FLAG_FT, SET, tickstart, Timeout);
  860. if (status != HAL_OK)
  861. {
  862. break;
  863. }
  864. *(__IO uint8_t *)data_reg = *hqspi->pTxBuffPtr++;
  865. hqspi->TxXferCount--;
  866. }
  867. if (status == HAL_OK)
  868. {
  869. /* Wait until TC flag is set to go back in idle state */
  870. status = QSPI_WaitFlagStateUntilTimeout(hqspi, QSPI_FLAG_TC, SET, tickstart, Timeout);
  871. if (status == HAL_OK)
  872. {
  873. /* Clear Transfer Complete bit */
  874. __HAL_QSPI_CLEAR_FLAG(hqspi, QSPI_FLAG_TC);
  875. /* Clear Busy bit */
  876. status = HAL_QSPI_Abort(hqspi);
  877. }
  878. }
  879. /* Update QSPI state */
  880. hqspi->State = HAL_QSPI_STATE_READY;
  881. }
  882. else
  883. {
  884. hqspi->ErrorCode |= HAL_QSPI_ERROR_INVALID_PARAM;
  885. status = HAL_ERROR;
  886. }
  887. }
  888. else
  889. {
  890. status = HAL_BUSY;
  891. }
  892. /* Process unlocked */
  893. __HAL_UNLOCK(hqspi);
  894. return status;
  895. }
  896. /**
  897. * @brief Receive an amount of data in blocking mode
  898. * @param hqspi QSPI handle
  899. * @param pData pointer to data buffer
  900. * @param Timeout Time out duration
  901. * @note This function is used only in Indirect Read Mode
  902. * @retval HAL status
  903. */
  904. HAL_StatusTypeDef HAL_QSPI_Receive(QSPI_HandleTypeDef *hqspi, uint8_t *pData, uint32_t Timeout)
  905. {
  906. HAL_StatusTypeDef status = HAL_OK;
  907. uint32_t tickstart = HAL_GetTick();
  908. uint32_t addr_reg = READ_REG(hqspi->Instance->AR);
  909. __IO uint32_t *data_reg = &hqspi->Instance->DR;
  910. /* Process locked */
  911. __HAL_LOCK(hqspi);
  912. if(hqspi->State == HAL_QSPI_STATE_READY)
  913. {
  914. hqspi->ErrorCode = HAL_QSPI_ERROR_NONE;
  915. if(pData != NULL )
  916. {
  917. /* Update state */
  918. hqspi->State = HAL_QSPI_STATE_BUSY_INDIRECT_RX;
  919. /* Configure counters and size of the handle */
  920. hqspi->RxXferCount = READ_REG(hqspi->Instance->DLR) + 1U;
  921. hqspi->RxXferSize = READ_REG(hqspi->Instance->DLR) + 1U;
  922. hqspi->pRxBuffPtr = pData;
  923. /* Configure QSPI: CCR register with functional as indirect read */
  924. MODIFY_REG(hqspi->Instance->CCR, QUADSPI_CCR_FMODE, QSPI_FUNCTIONAL_MODE_INDIRECT_READ);
  925. /* Start the transfer by re-writing the address in AR register */
  926. WRITE_REG(hqspi->Instance->AR, addr_reg);
  927. while(hqspi->RxXferCount > 0U)
  928. {
  929. /* Wait until FT or TC flag is set to read received data */
  930. status = QSPI_WaitFlagStateUntilTimeout(hqspi, (QSPI_FLAG_FT | QSPI_FLAG_TC), SET, tickstart, Timeout);
  931. if (status != HAL_OK)
  932. {
  933. break;
  934. }
  935. *hqspi->pRxBuffPtr++ = *(__IO uint8_t *)data_reg;
  936. hqspi->RxXferCount--;
  937. }
  938. if (status == HAL_OK)
  939. {
  940. /* Wait until TC flag is set to go back in idle state */
  941. status = QSPI_WaitFlagStateUntilTimeout(hqspi, QSPI_FLAG_TC, SET, tickstart, Timeout);
  942. if (status == HAL_OK)
  943. {
  944. /* Clear Transfer Complete bit */
  945. __HAL_QSPI_CLEAR_FLAG(hqspi, QSPI_FLAG_TC);
  946. /* Workaround - Extra data written in the FIFO at the end of a read transfer */
  947. status = HAL_QSPI_Abort(hqspi);
  948. }
  949. }
  950. /* Update QSPI state */
  951. hqspi->State = HAL_QSPI_STATE_READY;
  952. }
  953. else
  954. {
  955. hqspi->ErrorCode |= HAL_QSPI_ERROR_INVALID_PARAM;
  956. status = HAL_ERROR;
  957. }
  958. }
  959. else
  960. {
  961. status = HAL_BUSY;
  962. }
  963. /* Process unlocked */
  964. __HAL_UNLOCK(hqspi);
  965. return status;
  966. }
  967. /**
  968. * @brief Send an amount of data in interrupt mode
  969. * @param hqspi QSPI handle
  970. * @param pData pointer to data buffer
  971. * @note This function is used only in Indirect Write Mode
  972. * @retval HAL status
  973. */
  974. HAL_StatusTypeDef HAL_QSPI_Transmit_IT(QSPI_HandleTypeDef *hqspi, uint8_t *pData)
  975. {
  976. HAL_StatusTypeDef status = HAL_OK;
  977. /* Process locked */
  978. __HAL_LOCK(hqspi);
  979. if(hqspi->State == HAL_QSPI_STATE_READY)
  980. {
  981. hqspi->ErrorCode = HAL_QSPI_ERROR_NONE;
  982. if(pData != NULL )
  983. {
  984. /* Update state */
  985. hqspi->State = HAL_QSPI_STATE_BUSY_INDIRECT_TX;
  986. /* Configure counters and size of the handle */
  987. hqspi->TxXferCount = READ_REG(hqspi->Instance->DLR) + 1U;
  988. hqspi->TxXferSize = READ_REG(hqspi->Instance->DLR) + 1U;
  989. hqspi->pTxBuffPtr = pData;
  990. /* Configure QSPI: CCR register with functional as indirect write */
  991. MODIFY_REG(hqspi->Instance->CCR, QUADSPI_CCR_FMODE, QSPI_FUNCTIONAL_MODE_INDIRECT_WRITE);
  992. /* Clear interrupt */
  993. __HAL_QSPI_CLEAR_FLAG(hqspi, QSPI_FLAG_TE | QSPI_FLAG_TC);
  994. /* Process unlocked */
  995. __HAL_UNLOCK(hqspi);
  996. /* Enable the QSPI transfer error, FIFO threshold and transfer complete Interrupts */
  997. __HAL_QSPI_ENABLE_IT(hqspi, QSPI_IT_TE | QSPI_IT_FT | QSPI_IT_TC);
  998. }
  999. else
  1000. {
  1001. hqspi->ErrorCode |= HAL_QSPI_ERROR_INVALID_PARAM;
  1002. status = HAL_ERROR;
  1003. /* Process unlocked */
  1004. __HAL_UNLOCK(hqspi);
  1005. }
  1006. }
  1007. else
  1008. {
  1009. status = HAL_BUSY;
  1010. /* Process unlocked */
  1011. __HAL_UNLOCK(hqspi);
  1012. }
  1013. return status;
  1014. }
  1015. /**
  1016. * @brief Receive an amount of data in no-blocking mode with Interrupt
  1017. * @param hqspi QSPI handle
  1018. * @param pData pointer to data buffer
  1019. * @note This function is used only in Indirect Read Mode
  1020. * @retval HAL status
  1021. */
  1022. HAL_StatusTypeDef HAL_QSPI_Receive_IT(QSPI_HandleTypeDef *hqspi, uint8_t *pData)
  1023. {
  1024. HAL_StatusTypeDef status = HAL_OK;
  1025. uint32_t addr_reg = READ_REG(hqspi->Instance->AR);
  1026. /* Process locked */
  1027. __HAL_LOCK(hqspi);
  1028. if(hqspi->State == HAL_QSPI_STATE_READY)
  1029. {
  1030. hqspi->ErrorCode = HAL_QSPI_ERROR_NONE;
  1031. if(pData != NULL )
  1032. {
  1033. /* Update state */
  1034. hqspi->State = HAL_QSPI_STATE_BUSY_INDIRECT_RX;
  1035. /* Configure counters and size of the handle */
  1036. hqspi->RxXferCount = READ_REG(hqspi->Instance->DLR) + 1U;
  1037. hqspi->RxXferSize = READ_REG(hqspi->Instance->DLR) + 1U;
  1038. hqspi->pRxBuffPtr = pData;
  1039. /* Configure QSPI: CCR register with functional as indirect read */
  1040. MODIFY_REG(hqspi->Instance->CCR, QUADSPI_CCR_FMODE, QSPI_FUNCTIONAL_MODE_INDIRECT_READ);
  1041. /* Start the transfer by re-writing the address in AR register */
  1042. WRITE_REG(hqspi->Instance->AR, addr_reg);
  1043. /* Clear interrupt */
  1044. __HAL_QSPI_CLEAR_FLAG(hqspi, QSPI_FLAG_TE | QSPI_FLAG_TC);
  1045. /* Process unlocked */
  1046. __HAL_UNLOCK(hqspi);
  1047. /* Enable the QSPI transfer error, FIFO threshold and transfer complete Interrupts */
  1048. __HAL_QSPI_ENABLE_IT(hqspi, QSPI_IT_TE | QSPI_IT_FT | QSPI_IT_TC);
  1049. }
  1050. else
  1051. {
  1052. hqspi->ErrorCode |= HAL_QSPI_ERROR_INVALID_PARAM;
  1053. status = HAL_ERROR;
  1054. /* Process unlocked */
  1055. __HAL_UNLOCK(hqspi);
  1056. }
  1057. }
  1058. else
  1059. {
  1060. status = HAL_BUSY;
  1061. /* Process unlocked */
  1062. __HAL_UNLOCK(hqspi);
  1063. }
  1064. return status;
  1065. }
  1066. /**
  1067. * @brief Sends an amount of data in non blocking mode with DMA.
  1068. * @param hqspi QSPI handle
  1069. * @param pData pointer to data buffer
  1070. * @note This function is used only in Indirect Write Mode
  1071. * @note If DMA peripheral access is configured as halfword, the number
  1072. * of data and the fifo threshold should be aligned on halfword
  1073. * @note If DMA peripheral access is configured as word, the number
  1074. * of data and the fifo threshold should be aligned on word
  1075. * @retval HAL status
  1076. */
  1077. HAL_StatusTypeDef HAL_QSPI_Transmit_DMA(QSPI_HandleTypeDef *hqspi, uint8_t *pData)
  1078. {
  1079. HAL_StatusTypeDef status = HAL_OK;
  1080. uint32_t *tmp;
  1081. uint32_t data_size = (READ_REG(hqspi->Instance->DLR) + 1U);
  1082. /* Process locked */
  1083. __HAL_LOCK(hqspi);
  1084. if(hqspi->State == HAL_QSPI_STATE_READY)
  1085. {
  1086. /* Clear the error code */
  1087. hqspi->ErrorCode = HAL_QSPI_ERROR_NONE;
  1088. if(pData != NULL )
  1089. {
  1090. /* Configure counters of the handle */
  1091. if (hqspi->hdma->Init.PeriphDataAlignment == DMA_PDATAALIGN_BYTE)
  1092. {
  1093. hqspi->TxXferCount = data_size;
  1094. }
  1095. else if (hqspi->hdma->Init.PeriphDataAlignment == DMA_PDATAALIGN_HALFWORD)
  1096. {
  1097. if (((data_size % 2U) != 0U) || ((hqspi->Init.FifoThreshold % 2U) != 0U))
  1098. {
  1099. /* The number of data or the fifo threshold is not aligned on halfword
  1100. => no transfer possible with DMA peripheral access configured as halfword */
  1101. hqspi->ErrorCode |= HAL_QSPI_ERROR_INVALID_PARAM;
  1102. status = HAL_ERROR;
  1103. /* Process unlocked */
  1104. __HAL_UNLOCK(hqspi);
  1105. }
  1106. else
  1107. {
  1108. hqspi->TxXferCount = (data_size >> 1);
  1109. }
  1110. }
  1111. else if (hqspi->hdma->Init.PeriphDataAlignment == DMA_PDATAALIGN_WORD)
  1112. {
  1113. if (((data_size % 4U) != 0U) || ((hqspi->Init.FifoThreshold % 4U) != 0U))
  1114. {
  1115. /* The number of data or the fifo threshold is not aligned on word
  1116. => no transfer possible with DMA peripheral access configured as word */
  1117. hqspi->ErrorCode |= HAL_QSPI_ERROR_INVALID_PARAM;
  1118. status = HAL_ERROR;
  1119. /* Process unlocked */
  1120. __HAL_UNLOCK(hqspi);
  1121. }
  1122. else
  1123. {
  1124. hqspi->TxXferCount = (data_size >> 2U);
  1125. }
  1126. }
  1127. if (status == HAL_OK)
  1128. {
  1129. /* Update state */
  1130. hqspi->State = HAL_QSPI_STATE_BUSY_INDIRECT_TX;
  1131. /* Clear interrupt */
  1132. __HAL_QSPI_CLEAR_FLAG(hqspi, (QSPI_FLAG_TE | QSPI_FLAG_TC));
  1133. /* Configure size and pointer of the handle */
  1134. hqspi->TxXferSize = hqspi->TxXferCount;
  1135. hqspi->pTxBuffPtr = pData;
  1136. /* Configure QSPI: CCR register with functional mode as indirect write */
  1137. MODIFY_REG(hqspi->Instance->CCR, QUADSPI_CCR_FMODE, QSPI_FUNCTIONAL_MODE_INDIRECT_WRITE);
  1138. /* Set the QSPI DMA transfer complete callback */
  1139. hqspi->hdma->XferCpltCallback = QSPI_DMATxCplt;
  1140. /* Set the QSPI DMA Half transfer complete callback */
  1141. hqspi->hdma->XferHalfCpltCallback = QSPI_DMATxHalfCplt;
  1142. /* Set the DMA error callback */
  1143. hqspi->hdma->XferErrorCallback = QSPI_DMAError;
  1144. /* Clear the DMA abort callback */
  1145. hqspi->hdma->XferAbortCallback = NULL;
  1146. #if defined (QSPI1_V2_1L)
  1147. /* Bug "ES0305 section 2.1.8 In some specific cases, DMA2 data corruption occurs when managing
  1148. AHB and APB2 peripherals in a concurrent way" Workaround Implementation:
  1149. Change the following configuration of DMA peripheral
  1150. - Enable peripheral increment
  1151. - Disable memory increment
  1152. - Set DMA direction as peripheral to memory mode */
  1153. /* Enable peripheral increment mode of the DMA */
  1154. hqspi->hdma->Init.PeriphInc = DMA_PINC_ENABLE;
  1155. /* Disable memory increment mode of the DMA */
  1156. hqspi->hdma->Init.MemInc = DMA_MINC_DISABLE;
  1157. /* Update peripheral/memory increment mode bits */
  1158. MODIFY_REG(hqspi->hdma->Instance->CR, (DMA_SxCR_MINC | DMA_SxCR_PINC), (hqspi->hdma->Init.MemInc | hqspi->hdma->Init.PeriphInc));
  1159. /* Configure the direction of the DMA */
  1160. hqspi->hdma->Init.Direction = DMA_PERIPH_TO_MEMORY;
  1161. #else
  1162. /* Configure the direction of the DMA */
  1163. hqspi->hdma->Init.Direction = DMA_MEMORY_TO_PERIPH;
  1164. #endif /* QSPI1_V2_1L */
  1165. /* Update direction mode bit */
  1166. MODIFY_REG(hqspi->hdma->Instance->CR, DMA_SxCR_DIR, hqspi->hdma->Init.Direction);
  1167. /* Enable the QSPI transmit DMA Channel */
  1168. tmp = (uint32_t*)&pData;
  1169. HAL_DMA_Start_IT(hqspi->hdma, *(uint32_t*)tmp, (uint32_t)&hqspi->Instance->DR, hqspi->TxXferSize);
  1170. /* Process unlocked */
  1171. __HAL_UNLOCK(hqspi);
  1172. /* Enable the QSPI transfer error Interrupt */
  1173. __HAL_QSPI_ENABLE_IT(hqspi, QSPI_IT_TE);
  1174. /* Enable the DMA transfer by setting the DMAEN bit in the QSPI CR register */
  1175. SET_BIT(hqspi->Instance->CR, QUADSPI_CR_DMAEN);
  1176. }
  1177. }
  1178. else
  1179. {
  1180. hqspi->ErrorCode |= HAL_QSPI_ERROR_INVALID_PARAM;
  1181. status = HAL_ERROR;
  1182. /* Process unlocked */
  1183. __HAL_UNLOCK(hqspi);
  1184. }
  1185. }
  1186. else
  1187. {
  1188. status = HAL_BUSY;
  1189. /* Process unlocked */
  1190. __HAL_UNLOCK(hqspi);
  1191. }
  1192. return status;
  1193. }
  1194. /**
  1195. * @brief Receives an amount of data in non blocking mode with DMA.
  1196. * @param hqspi QSPI handle
  1197. * @param pData pointer to data buffer.
  1198. * @note This function is used only in Indirect Read Mode
  1199. * @note If DMA peripheral access is configured as halfword, the number
  1200. * of data and the fifo threshold should be aligned on halfword
  1201. * @note If DMA peripheral access is configured as word, the number
  1202. * of data and the fifo threshold should be aligned on word
  1203. * @retval HAL status
  1204. */
  1205. HAL_StatusTypeDef HAL_QSPI_Receive_DMA(QSPI_HandleTypeDef *hqspi, uint8_t *pData)
  1206. {
  1207. HAL_StatusTypeDef status = HAL_OK;
  1208. uint32_t *tmp;
  1209. uint32_t addr_reg = READ_REG(hqspi->Instance->AR);
  1210. uint32_t data_size = (READ_REG(hqspi->Instance->DLR) + 1U);
  1211. /* Process locked */
  1212. __HAL_LOCK(hqspi);
  1213. if(hqspi->State == HAL_QSPI_STATE_READY)
  1214. {
  1215. hqspi->ErrorCode = HAL_QSPI_ERROR_NONE;
  1216. if(pData != NULL )
  1217. {
  1218. /* Configure counters of the handle */
  1219. if (hqspi->hdma->Init.PeriphDataAlignment == DMA_PDATAALIGN_BYTE)
  1220. {
  1221. hqspi->RxXferCount = data_size;
  1222. }
  1223. else if (hqspi->hdma->Init.PeriphDataAlignment == DMA_PDATAALIGN_HALFWORD)
  1224. {
  1225. if (((data_size % 2U) != 0U) || ((hqspi->Init.FifoThreshold % 2U) != 0U))
  1226. {
  1227. /* The number of data or the fifo threshold is not aligned on halfword
  1228. => no transfer possible with DMA peripheral access configured as halfword */
  1229. hqspi->ErrorCode |= HAL_QSPI_ERROR_INVALID_PARAM;
  1230. status = HAL_ERROR;
  1231. /* Process unlocked */
  1232. __HAL_UNLOCK(hqspi);
  1233. }
  1234. else
  1235. {
  1236. hqspi->RxXferCount = (data_size >> 1U);
  1237. }
  1238. }
  1239. else if (hqspi->hdma->Init.PeriphDataAlignment == DMA_PDATAALIGN_WORD)
  1240. {
  1241. if (((data_size % 4U) != 0U) || ((hqspi->Init.FifoThreshold % 4U) != 0U))
  1242. {
  1243. /* The number of data or the fifo threshold is not aligned on word
  1244. => no transfer possible with DMA peripheral access configured as word */
  1245. hqspi->ErrorCode |= HAL_QSPI_ERROR_INVALID_PARAM;
  1246. status = HAL_ERROR;
  1247. /* Process unlocked */
  1248. __HAL_UNLOCK(hqspi);
  1249. }
  1250. else
  1251. {
  1252. hqspi->RxXferCount = (data_size >> 2U);
  1253. }
  1254. }
  1255. if (status == HAL_OK)
  1256. {
  1257. /* Update state */
  1258. hqspi->State = HAL_QSPI_STATE_BUSY_INDIRECT_RX;
  1259. /* Clear interrupt */
  1260. __HAL_QSPI_CLEAR_FLAG(hqspi, (QSPI_FLAG_TE | QSPI_FLAG_TC));
  1261. /* Configure size and pointer of the handle */
  1262. hqspi->RxXferSize = hqspi->RxXferCount;
  1263. hqspi->pRxBuffPtr = pData;
  1264. /* Set the QSPI DMA transfer complete callback */
  1265. hqspi->hdma->XferCpltCallback = QSPI_DMARxCplt;
  1266. /* Set the QSPI DMA Half transfer complete callback */
  1267. hqspi->hdma->XferHalfCpltCallback = QSPI_DMARxHalfCplt;
  1268. /* Set the DMA error callback */
  1269. hqspi->hdma->XferErrorCallback = QSPI_DMAError;
  1270. /* Clear the DMA abort callback */
  1271. hqspi->hdma->XferAbortCallback = NULL;
  1272. #if defined (QSPI1_V2_1L)
  1273. /* Bug "ES0305 section 2.1.8 In some specific cases, DMA2 data corruption occurs when managing
  1274. AHB and APB2 peripherals in a concurrent way" Workaround Implementation:
  1275. Change the following configuration of DMA peripheral
  1276. - Enable peripheral increment
  1277. - Disable memory increment
  1278. - Set DMA direction as memory to peripheral mode
  1279. - 4 Extra words (32-bits) are added for read operation to guarantee
  1280. the last data is transferred from DMA FIFO to RAM memory */
  1281. /* Enable peripheral increment of the DMA */
  1282. hqspi->hdma->Init.PeriphInc = DMA_PINC_ENABLE;
  1283. /* Disable memory increment of the DMA */
  1284. hqspi->hdma->Init.MemInc = DMA_MINC_DISABLE;
  1285. /* Update peripheral/memory increment mode bits */
  1286. MODIFY_REG(hqspi->hdma->Instance->CR, (DMA_SxCR_MINC | DMA_SxCR_PINC), (hqspi->hdma->Init.MemInc | hqspi->hdma->Init.PeriphInc));
  1287. /* Configure the direction of the DMA */
  1288. hqspi->hdma->Init.Direction = DMA_MEMORY_TO_PERIPH;
  1289. /* 4 Extra words (32-bits) are needed for read operation to guarantee
  1290. the last data is transferred from DMA FIFO to RAM memory */
  1291. WRITE_REG(hqspi->Instance->DLR, (data_size - 1U + 16U));
  1292. /* Update direction mode bit */
  1293. MODIFY_REG(hqspi->hdma->Instance->CR, DMA_SxCR_DIR, hqspi->hdma->Init.Direction);
  1294. /* Configure QSPI: CCR register with functional as indirect read */
  1295. MODIFY_REG(hqspi->Instance->CCR, QUADSPI_CCR_FMODE, QSPI_FUNCTIONAL_MODE_INDIRECT_READ);
  1296. /* Start the transfer by re-writing the address in AR register */
  1297. WRITE_REG(hqspi->Instance->AR, addr_reg);
  1298. /* Enable the DMA Channel */
  1299. tmp = (uint32_t*)&pData;
  1300. HAL_DMA_Start_IT(hqspi->hdma, (uint32_t)&hqspi->Instance->DR, *(uint32_t*)tmp, hqspi->RxXferSize);
  1301. /* Enable the DMA transfer by setting the DMAEN bit in the QSPI CR register */
  1302. SET_BIT(hqspi->Instance->CR, QUADSPI_CR_DMAEN);
  1303. /* Process unlocked */
  1304. __HAL_UNLOCK(hqspi);
  1305. /* Enable the QSPI transfer error Interrupt */
  1306. __HAL_QSPI_ENABLE_IT(hqspi, QSPI_IT_TE);
  1307. #else
  1308. /* Configure the direction of the DMA */
  1309. hqspi->hdma->Init.Direction = DMA_PERIPH_TO_MEMORY;
  1310. MODIFY_REG(hqspi->hdma->Instance->CR, DMA_SxCR_DIR, hqspi->hdma->Init.Direction);
  1311. /* Enable the DMA Channel */
  1312. tmp = (uint32_t*)&pData;
  1313. HAL_DMA_Start_IT(hqspi->hdma, (uint32_t)&hqspi->Instance->DR, *(uint32_t*)tmp, hqspi->RxXferSize);
  1314. /* Configure QSPI: CCR register with functional as indirect read */
  1315. MODIFY_REG(hqspi->Instance->CCR, QUADSPI_CCR_FMODE, QSPI_FUNCTIONAL_MODE_INDIRECT_READ);
  1316. /* Start the transfer by re-writing the address in AR register */
  1317. WRITE_REG(hqspi->Instance->AR, addr_reg);
  1318. /* Process unlocked */
  1319. __HAL_UNLOCK(hqspi);
  1320. /* Enable the QSPI transfer error Interrupt */
  1321. __HAL_QSPI_ENABLE_IT(hqspi, QSPI_IT_TE);
  1322. /* Enable the DMA transfer by setting the DMAEN bit in the QSPI CR register */
  1323. SET_BIT(hqspi->Instance->CR, QUADSPI_CR_DMAEN);
  1324. #endif /* QSPI1_V2_1L */
  1325. }
  1326. }
  1327. else
  1328. {
  1329. hqspi->ErrorCode |= HAL_QSPI_ERROR_INVALID_PARAM;
  1330. status = HAL_ERROR;
  1331. /* Process unlocked */
  1332. __HAL_UNLOCK(hqspi);
  1333. }
  1334. }
  1335. else
  1336. {
  1337. status = HAL_BUSY;
  1338. /* Process unlocked */
  1339. __HAL_UNLOCK(hqspi);
  1340. }
  1341. return status;
  1342. }
  1343. /**
  1344. * @brief Configure the QSPI Automatic Polling Mode in blocking mode.
  1345. * @param hqspi QSPI handle
  1346. * @param cmd structure that contains the command configuration information.
  1347. * @param cfg structure that contains the polling configuration information.
  1348. * @param Timeout Time out duration
  1349. * @note This function is used only in Automatic Polling Mode
  1350. * @retval HAL status
  1351. */
  1352. HAL_StatusTypeDef HAL_QSPI_AutoPolling(QSPI_HandleTypeDef *hqspi, QSPI_CommandTypeDef *cmd, QSPI_AutoPollingTypeDef *cfg, uint32_t Timeout)
  1353. {
  1354. HAL_StatusTypeDef status = HAL_ERROR;
  1355. uint32_t tickstart = HAL_GetTick();
  1356. /* Check the parameters */
  1357. assert_param(IS_QSPI_INSTRUCTION_MODE(cmd->InstructionMode));
  1358. if (cmd->InstructionMode != QSPI_INSTRUCTION_NONE)
  1359. {
  1360. assert_param(IS_QSPI_INSTRUCTION(cmd->Instruction));
  1361. }
  1362. assert_param(IS_QSPI_ADDRESS_MODE(cmd->AddressMode));
  1363. if (cmd->AddressMode != QSPI_ADDRESS_NONE)
  1364. {
  1365. assert_param(IS_QSPI_ADDRESS_SIZE(cmd->AddressSize));
  1366. }
  1367. assert_param(IS_QSPI_ALTERNATE_BYTES_MODE(cmd->AlternateByteMode));
  1368. if (cmd->AlternateByteMode != QSPI_ALTERNATE_BYTES_NONE)
  1369. {
  1370. assert_param(IS_QSPI_ALTERNATE_BYTES_SIZE(cmd->AlternateBytesSize));
  1371. }
  1372. assert_param(IS_QSPI_DUMMY_CYCLES(cmd->DummyCycles));
  1373. assert_param(IS_QSPI_DATA_MODE(cmd->DataMode));
  1374. assert_param(IS_QSPI_DDR_MODE(cmd->DdrMode));
  1375. assert_param(IS_QSPI_DDR_HHC(cmd->DdrHoldHalfCycle));
  1376. assert_param(IS_QSPI_SIOO_MODE(cmd->SIOOMode));
  1377. assert_param(IS_QSPI_INTERVAL(cfg->Interval));
  1378. assert_param(IS_QSPI_STATUS_BYTES_SIZE(cfg->StatusBytesSize));
  1379. assert_param(IS_QSPI_MATCH_MODE(cfg->MatchMode));
  1380. /* Process locked */
  1381. __HAL_LOCK(hqspi);
  1382. if(hqspi->State == HAL_QSPI_STATE_READY)
  1383. {
  1384. hqspi->ErrorCode = HAL_QSPI_ERROR_NONE;
  1385. /* Update state */
  1386. hqspi->State = HAL_QSPI_STATE_BUSY_AUTO_POLLING;
  1387. /* Wait till BUSY flag reset */
  1388. status = QSPI_WaitFlagStateUntilTimeout(hqspi, QSPI_FLAG_BUSY, RESET, tickstart, Timeout);
  1389. if (status == HAL_OK)
  1390. {
  1391. /* Configure QSPI: PSMAR register with the status match value */
  1392. WRITE_REG(hqspi->Instance->PSMAR, cfg->Match);
  1393. /* Configure QSPI: PSMKR register with the status mask value */
  1394. WRITE_REG(hqspi->Instance->PSMKR, cfg->Mask);
  1395. /* Configure QSPI: PIR register with the interval value */
  1396. WRITE_REG(hqspi->Instance->PIR, cfg->Interval);
  1397. /* Configure QSPI: CR register with Match mode and Automatic stop enabled
  1398. (otherwise there will be an infinite loop in blocking mode) */
  1399. MODIFY_REG(hqspi->Instance->CR, (QUADSPI_CR_PMM | QUADSPI_CR_APMS),
  1400. (cfg->MatchMode | QSPI_AUTOMATIC_STOP_ENABLE));
  1401. /* Call the configuration function */
  1402. cmd->NbData = cfg->StatusBytesSize;
  1403. QSPI_Config(hqspi, cmd, QSPI_FUNCTIONAL_MODE_AUTO_POLLING);
  1404. /* Wait until SM flag is set to go back in idle state */
  1405. status = QSPI_WaitFlagStateUntilTimeout(hqspi, QSPI_FLAG_SM, SET, tickstart, Timeout);
  1406. if (status == HAL_OK)
  1407. {
  1408. __HAL_QSPI_CLEAR_FLAG(hqspi, QSPI_FLAG_SM);
  1409. /* Update state */
  1410. hqspi->State = HAL_QSPI_STATE_READY;
  1411. }
  1412. }
  1413. }
  1414. else
  1415. {
  1416. status = HAL_BUSY;
  1417. }
  1418. /* Process unlocked */
  1419. __HAL_UNLOCK(hqspi);
  1420. /* Return function status */
  1421. return status;
  1422. }
  1423. /**
  1424. * @brief Configure the QSPI Automatic Polling Mode in non-blocking mode.
  1425. * @param hqspi QSPI handle
  1426. * @param cmd structure that contains the command configuration information.
  1427. * @param cfg structure that contains the polling configuration information.
  1428. * @note This function is used only in Automatic Polling Mode
  1429. * @retval HAL status
  1430. */
  1431. HAL_StatusTypeDef HAL_QSPI_AutoPolling_IT(QSPI_HandleTypeDef *hqspi, QSPI_CommandTypeDef *cmd, QSPI_AutoPollingTypeDef *cfg)
  1432. {
  1433. __IO uint32_t count = 0U;
  1434. HAL_StatusTypeDef status = HAL_OK;
  1435. /* Check the parameters */
  1436. assert_param(IS_QSPI_INSTRUCTION_MODE(cmd->InstructionMode));
  1437. if (cmd->InstructionMode != QSPI_INSTRUCTION_NONE)
  1438. {
  1439. assert_param(IS_QSPI_INSTRUCTION(cmd->Instruction));
  1440. }
  1441. assert_param(IS_QSPI_ADDRESS_MODE(cmd->AddressMode));
  1442. if (cmd->AddressMode != QSPI_ADDRESS_NONE)
  1443. {
  1444. assert_param(IS_QSPI_ADDRESS_SIZE(cmd->AddressSize));
  1445. }
  1446. assert_param(IS_QSPI_ALTERNATE_BYTES_MODE(cmd->AlternateByteMode));
  1447. if (cmd->AlternateByteMode != QSPI_ALTERNATE_BYTES_NONE)
  1448. {
  1449. assert_param(IS_QSPI_ALTERNATE_BYTES_SIZE(cmd->AlternateBytesSize));
  1450. }
  1451. assert_param(IS_QSPI_DUMMY_CYCLES(cmd->DummyCycles));
  1452. assert_param(IS_QSPI_DATA_MODE(cmd->DataMode));
  1453. assert_param(IS_QSPI_DDR_MODE(cmd->DdrMode));
  1454. assert_param(IS_QSPI_DDR_HHC(cmd->DdrHoldHalfCycle));
  1455. assert_param(IS_QSPI_SIOO_MODE(cmd->SIOOMode));
  1456. assert_param(IS_QSPI_INTERVAL(cfg->Interval));
  1457. assert_param(IS_QSPI_STATUS_BYTES_SIZE(cfg->StatusBytesSize));
  1458. assert_param(IS_QSPI_MATCH_MODE(cfg->MatchMode));
  1459. assert_param(IS_QSPI_AUTOMATIC_STOP(cfg->AutomaticStop));
  1460. /* Process locked */
  1461. __HAL_LOCK(hqspi);
  1462. if(hqspi->State == HAL_QSPI_STATE_READY)
  1463. {
  1464. hqspi->ErrorCode = HAL_QSPI_ERROR_NONE;
  1465. /* Update state */
  1466. hqspi->State = HAL_QSPI_STATE_BUSY_AUTO_POLLING;
  1467. /* Wait till BUSY flag reset */
  1468. count = (hqspi->Timeout) * (SystemCoreClock / 16U / 1000U);
  1469. do
  1470. {
  1471. if (count-- == 0U)
  1472. {
  1473. hqspi->State = HAL_QSPI_STATE_ERROR;
  1474. hqspi->ErrorCode |= HAL_QSPI_ERROR_TIMEOUT;
  1475. status = HAL_TIMEOUT;
  1476. }
  1477. }
  1478. while ((__HAL_QSPI_GET_FLAG(hqspi, QSPI_FLAG_BUSY)) != RESET);
  1479. if (status == HAL_OK)
  1480. {
  1481. /* Configure QSPI: PSMAR register with the status match value */
  1482. WRITE_REG(hqspi->Instance->PSMAR, cfg->Match);
  1483. /* Configure QSPI: PSMKR register with the status mask value */
  1484. WRITE_REG(hqspi->Instance->PSMKR, cfg->Mask);
  1485. /* Configure QSPI: PIR register with the interval value */
  1486. WRITE_REG(hqspi->Instance->PIR, cfg->Interval);
  1487. /* Configure QSPI: CR register with Match mode and Automatic stop mode */
  1488. MODIFY_REG(hqspi->Instance->CR, (QUADSPI_CR_PMM | QUADSPI_CR_APMS),
  1489. (cfg->MatchMode | cfg->AutomaticStop));
  1490. /* Clear interrupt */
  1491. __HAL_QSPI_CLEAR_FLAG(hqspi, QSPI_FLAG_TE | QSPI_FLAG_SM);
  1492. /* Call the configuration function */
  1493. cmd->NbData = cfg->StatusBytesSize;
  1494. QSPI_Config(hqspi, cmd, QSPI_FUNCTIONAL_MODE_AUTO_POLLING);
  1495. /* Process unlocked */
  1496. __HAL_UNLOCK(hqspi);
  1497. /* Enable the QSPI Transfer Error and status match Interrupt */
  1498. __HAL_QSPI_ENABLE_IT(hqspi, (QSPI_IT_SM | QSPI_IT_TE));
  1499. }
  1500. else
  1501. {
  1502. /* Process unlocked */
  1503. __HAL_UNLOCK(hqspi);
  1504. }
  1505. }
  1506. else
  1507. {
  1508. status = HAL_BUSY;
  1509. /* Process unlocked */
  1510. __HAL_UNLOCK(hqspi);
  1511. }
  1512. /* Return function status */
  1513. return status;
  1514. }
  1515. /**
  1516. * @brief Configure the Memory Mapped mode.
  1517. * @param hqspi QSPI handle
  1518. * @param cmd structure that contains the command configuration information.
  1519. * @param cfg structure that contains the memory mapped configuration information.
  1520. * @note This function is used only in Memory mapped Mode
  1521. * @retval HAL status
  1522. */
  1523. HAL_StatusTypeDef HAL_QSPI_MemoryMapped(QSPI_HandleTypeDef *hqspi, QSPI_CommandTypeDef *cmd, QSPI_MemoryMappedTypeDef *cfg)
  1524. {
  1525. HAL_StatusTypeDef status = HAL_ERROR;
  1526. uint32_t tickstart = HAL_GetTick();
  1527. /* Check the parameters */
  1528. assert_param(IS_QSPI_INSTRUCTION_MODE(cmd->InstructionMode));
  1529. if (cmd->InstructionMode != QSPI_INSTRUCTION_NONE)
  1530. {
  1531. assert_param(IS_QSPI_INSTRUCTION(cmd->Instruction));
  1532. }
  1533. assert_param(IS_QSPI_ADDRESS_MODE(cmd->AddressMode));
  1534. if (cmd->AddressMode != QSPI_ADDRESS_NONE)
  1535. {
  1536. assert_param(IS_QSPI_ADDRESS_SIZE(cmd->AddressSize));
  1537. }
  1538. assert_param(IS_QSPI_ALTERNATE_BYTES_MODE(cmd->AlternateByteMode));
  1539. if (cmd->AlternateByteMode != QSPI_ALTERNATE_BYTES_NONE)
  1540. {
  1541. assert_param(IS_QSPI_ALTERNATE_BYTES_SIZE(cmd->AlternateBytesSize));
  1542. }
  1543. assert_param(IS_QSPI_DUMMY_CYCLES(cmd->DummyCycles));
  1544. assert_param(IS_QSPI_DATA_MODE(cmd->DataMode));
  1545. assert_param(IS_QSPI_DDR_MODE(cmd->DdrMode));
  1546. assert_param(IS_QSPI_DDR_HHC(cmd->DdrHoldHalfCycle));
  1547. assert_param(IS_QSPI_SIOO_MODE(cmd->SIOOMode));
  1548. assert_param(IS_QSPI_TIMEOUT_ACTIVATION(cfg->TimeOutActivation));
  1549. /* Process locked */
  1550. __HAL_LOCK(hqspi);
  1551. if(hqspi->State == HAL_QSPI_STATE_READY)
  1552. {
  1553. hqspi->ErrorCode = HAL_QSPI_ERROR_NONE;
  1554. /* Update state */
  1555. hqspi->State = HAL_QSPI_STATE_BUSY_MEM_MAPPED;
  1556. /* Wait till BUSY flag reset */
  1557. status = QSPI_WaitFlagStateUntilTimeout(hqspi, QSPI_FLAG_BUSY, RESET, tickstart, hqspi->Timeout);
  1558. if (status == HAL_OK)
  1559. {
  1560. /* Configure QSPI: CR register with timeout counter enable */
  1561. MODIFY_REG(hqspi->Instance->CR, QUADSPI_CR_TCEN, cfg->TimeOutActivation);
  1562. if (cfg->TimeOutActivation == QSPI_TIMEOUT_COUNTER_ENABLE)
  1563. {
  1564. assert_param(IS_QSPI_TIMEOUT_PERIOD(cfg->TimeOutPeriod));
  1565. /* Configure QSPI: LPTR register with the low-power timeout value */
  1566. WRITE_REG(hqspi->Instance->LPTR, cfg->TimeOutPeriod);
  1567. /* Clear interrupt */
  1568. __HAL_QSPI_CLEAR_FLAG(hqspi, QSPI_FLAG_TO);
  1569. /* Enable the QSPI TimeOut Interrupt */
  1570. __HAL_QSPI_ENABLE_IT(hqspi, QSPI_IT_TO);
  1571. }
  1572. /* Call the configuration function */
  1573. QSPI_Config(hqspi, cmd, QSPI_FUNCTIONAL_MODE_MEMORY_MAPPED);
  1574. }
  1575. }
  1576. else
  1577. {
  1578. status = HAL_BUSY;
  1579. }
  1580. /* Process unlocked */
  1581. __HAL_UNLOCK(hqspi);
  1582. /* Return function status */
  1583. return status;
  1584. }
  1585. /**
  1586. * @brief Transfer Error callbacks
  1587. * @param hqspi QSPI handle
  1588. * @retval None
  1589. */
  1590. __weak void HAL_QSPI_ErrorCallback(QSPI_HandleTypeDef *hqspi)
  1591. {
  1592. /* Prevent unused argument(s) compilation warning */
  1593. UNUSED(hqspi);
  1594. /* NOTE : This function Should not be modified, when the callback is needed,
  1595. the HAL_QSPI_ErrorCallback could be implemented in the user file
  1596. */
  1597. }
  1598. /**
  1599. * @brief Abort completed callback.
  1600. * @param hqspi QSPI handle
  1601. * @retval None
  1602. */
  1603. __weak void HAL_QSPI_AbortCpltCallback(QSPI_HandleTypeDef *hqspi)
  1604. {
  1605. /* Prevent unused argument(s) compilation warning */
  1606. UNUSED(hqspi);
  1607. /* NOTE: This function should not be modified, when the callback is needed,
  1608. the HAL_QSPI_AbortCpltCallback could be implemented in the user file
  1609. */
  1610. }
  1611. /**
  1612. * @brief Command completed callback.
  1613. * @param hqspi QSPI handle
  1614. * @retval None
  1615. */
  1616. __weak void HAL_QSPI_CmdCpltCallback(QSPI_HandleTypeDef *hqspi)
  1617. {
  1618. /* Prevent unused argument(s) compilation warning */
  1619. UNUSED(hqspi);
  1620. /* NOTE: This function Should not be modified, when the callback is needed,
  1621. the HAL_QSPI_CmdCpltCallback could be implemented in the user file
  1622. */
  1623. }
  1624. /**
  1625. * @brief Rx Transfer completed callbacks.
  1626. * @param hqspi QSPI handle
  1627. * @retval None
  1628. */
  1629. __weak void HAL_QSPI_RxCpltCallback(QSPI_HandleTypeDef *hqspi)
  1630. {
  1631. /* Prevent unused argument(s) compilation warning */
  1632. UNUSED(hqspi);
  1633. /* NOTE: This function Should not be modified, when the callback is needed,
  1634. the HAL_QSPI_RxCpltCallback could be implemented in the user file
  1635. */
  1636. }
  1637. /**
  1638. * @brief Tx Transfer completed callbacks.
  1639. * @param hqspi QSPI handle
  1640. * @retval None
  1641. */
  1642. __weak void HAL_QSPI_TxCpltCallback(QSPI_HandleTypeDef *hqspi)
  1643. {
  1644. /* Prevent unused argument(s) compilation warning */
  1645. UNUSED(hqspi);
  1646. /* NOTE: This function Should not be modified, when the callback is needed,
  1647. the HAL_QSPI_TxCpltCallback could be implemented in the user file
  1648. */
  1649. }
  1650. /**
  1651. * @brief Rx Half Transfer completed callbacks.
  1652. * @param hqspi QSPI handle
  1653. * @retval None
  1654. */
  1655. __weak void HAL_QSPI_RxHalfCpltCallback(QSPI_HandleTypeDef *hqspi)
  1656. {
  1657. /* Prevent unused argument(s) compilation warning */
  1658. UNUSED(hqspi);
  1659. /* NOTE: This function Should not be modified, when the callback is needed,
  1660. the HAL_QSPI_RxHalfCpltCallback could be implemented in the user file
  1661. */
  1662. }
  1663. /**
  1664. * @brief Tx Half Transfer completed callbacks.
  1665. * @param hqspi QSPI handle
  1666. * @retval None
  1667. */
  1668. __weak void HAL_QSPI_TxHalfCpltCallback(QSPI_HandleTypeDef *hqspi)
  1669. {
  1670. /* Prevent unused argument(s) compilation warning */
  1671. UNUSED(hqspi);
  1672. /* NOTE: This function Should not be modified, when the callback is needed,
  1673. the HAL_QSPI_TxHalfCpltCallback could be implemented in the user file
  1674. */
  1675. }
  1676. /**
  1677. * @brief FIFO Threshold callbacks
  1678. * @param hqspi QSPI handle
  1679. * @retval None
  1680. */
  1681. __weak void HAL_QSPI_FifoThresholdCallback(QSPI_HandleTypeDef *hqspi)
  1682. {
  1683. /* Prevent unused argument(s) compilation warning */
  1684. UNUSED(hqspi);
  1685. /* NOTE : This function Should not be modified, when the callback is needed,
  1686. the HAL_QSPI_FIFOThresholdCallback could be implemented in the user file
  1687. */
  1688. }
  1689. /**
  1690. * @brief Status Match callbacks
  1691. * @param hqspi QSPI handle
  1692. * @retval None
  1693. */
  1694. __weak void HAL_QSPI_StatusMatchCallback(QSPI_HandleTypeDef *hqspi)
  1695. {
  1696. /* Prevent unused argument(s) compilation warning */
  1697. UNUSED(hqspi);
  1698. /* NOTE : This function Should not be modified, when the callback is needed,
  1699. the HAL_QSPI_StatusMatchCallback could be implemented in the user file
  1700. */
  1701. }
  1702. /**
  1703. * @brief Timeout callbacks
  1704. * @param hqspi QSPI handle
  1705. * @retval None
  1706. */
  1707. __weak void HAL_QSPI_TimeOutCallback(QSPI_HandleTypeDef *hqspi)
  1708. {
  1709. /* Prevent unused argument(s) compilation warning */
  1710. UNUSED(hqspi);
  1711. /* NOTE : This function Should not be modified, when the callback is needed,
  1712. the HAL_QSPI_TimeOutCallback could be implemented in the user file
  1713. */
  1714. }
  1715. #if (USE_HAL_QSPI_REGISTER_CALLBACKS == 1)
  1716. /**
  1717. * @brief Register a User QSPI Callback
  1718. * To be used instead of the weak (surcharged) predefined callback
  1719. * @param hqspi : QSPI handle
  1720. * @param CallbackId : ID of the callback to be registered
  1721. * This parameter can be one of the following values:
  1722. * @arg @ref HAL_QSPI_ERROR_CB_ID QSPI Error Callback ID
  1723. * @arg @ref HAL_QSPI_ABORT_CB_ID QSPI Abort Callback ID
  1724. * @arg @ref HAL_QSPI_FIFO_THRESHOLD_CB_ID QSPI FIFO Threshold Callback ID
  1725. * @arg @ref HAL_QSPI_CMD_CPLT_CB_ID QSPI Command Complete Callback ID
  1726. * @arg @ref HAL_QSPI_RX_CPLT_CB_ID QSPI Rx Complete Callback ID
  1727. * @arg @ref HAL_QSPI_TX_CPLT_CB_ID QSPI Tx Complete Callback ID
  1728. * @arg @ref HAL_QSPI_RX_HALF_CPLT_CB_ID QSPI Rx Half Complete Callback ID
  1729. * @arg @ref HAL_QSPI_TX_HALF_CPLT_CB_ID QSPI Tx Half Complete Callback ID
  1730. * @arg @ref HAL_QSPI_STATUS_MATCH_CB_ID QSPI Status Match Callback ID
  1731. * @arg @ref HAL_QSPI_TIMEOUT_CB_ID QSPI Timeout Callback ID
  1732. * @arg @ref HAL_QSPI_MSP_INIT_CB_ID QSPI MspInit callback ID
  1733. * @arg @ref HAL_QSPI_MSP_DEINIT_CB_ID QSPI MspDeInit callback ID
  1734. * @param pCallback : pointer to the Callback function
  1735. * @retval status
  1736. */
  1737. HAL_StatusTypeDef HAL_QSPI_RegisterCallback (QSPI_HandleTypeDef *hqspi, HAL_QSPI_CallbackIDTypeDef CallbackId, pQSPI_CallbackTypeDef pCallback)
  1738. {
  1739. HAL_StatusTypeDef status = HAL_OK;
  1740. if(pCallback == NULL)
  1741. {
  1742. /* Update the error code */
  1743. hqspi->ErrorCode |= HAL_QSPI_ERROR_INVALID_CALLBACK;
  1744. return HAL_ERROR;
  1745. }
  1746. /* Process locked */
  1747. __HAL_LOCK(hqspi);
  1748. if(hqspi->State == HAL_QSPI_STATE_READY)
  1749. {
  1750. switch (CallbackId)
  1751. {
  1752. case HAL_QSPI_ERROR_CB_ID :
  1753. hqspi->ErrorCallback = pCallback;
  1754. break;
  1755. case HAL_QSPI_ABORT_CB_ID :
  1756. hqspi->AbortCpltCallback = pCallback;
  1757. break;
  1758. case HAL_QSPI_FIFO_THRESHOLD_CB_ID :
  1759. hqspi->FifoThresholdCallback = pCallback;
  1760. break;
  1761. case HAL_QSPI_CMD_CPLT_CB_ID :
  1762. hqspi->CmdCpltCallback = pCallback;
  1763. break;
  1764. case HAL_QSPI_RX_CPLT_CB_ID :
  1765. hqspi->RxCpltCallback = pCallback;
  1766. break;
  1767. case HAL_QSPI_TX_CPLT_CB_ID :
  1768. hqspi->TxCpltCallback = pCallback;
  1769. break;
  1770. case HAL_QSPI_RX_HALF_CPLT_CB_ID :
  1771. hqspi->RxHalfCpltCallback = pCallback;
  1772. break;
  1773. case HAL_QSPI_TX_HALF_CPLT_CB_ID :
  1774. hqspi->TxHalfCpltCallback = pCallback;
  1775. break;
  1776. case HAL_QSPI_STATUS_MATCH_CB_ID :
  1777. hqspi->StatusMatchCallback = pCallback;
  1778. break;
  1779. case HAL_QSPI_TIMEOUT_CB_ID :
  1780. hqspi->TimeOutCallback = pCallback;
  1781. break;
  1782. case HAL_QSPI_MSP_INIT_CB_ID :
  1783. hqspi->MspInitCallback = pCallback;
  1784. break;
  1785. case HAL_QSPI_MSP_DEINIT_CB_ID :
  1786. hqspi->MspDeInitCallback = pCallback;
  1787. break;
  1788. default :
  1789. /* Update the error code */
  1790. hqspi->ErrorCode |= HAL_QSPI_ERROR_INVALID_CALLBACK;
  1791. /* update return status */
  1792. status = HAL_ERROR;
  1793. break;
  1794. }
  1795. }
  1796. else if (hqspi->State == HAL_QSPI_STATE_RESET)
  1797. {
  1798. switch (CallbackId)
  1799. {
  1800. case HAL_QSPI_MSP_INIT_CB_ID :
  1801. hqspi->MspInitCallback = pCallback;
  1802. break;
  1803. case HAL_QSPI_MSP_DEINIT_CB_ID :
  1804. hqspi->MspDeInitCallback = pCallback;
  1805. break;
  1806. default :
  1807. /* Update the error code */
  1808. hqspi->ErrorCode |= HAL_QSPI_ERROR_INVALID_CALLBACK;
  1809. /* update return status */
  1810. status = HAL_ERROR;
  1811. break;
  1812. }
  1813. }
  1814. else
  1815. {
  1816. /* Update the error code */
  1817. hqspi->ErrorCode |= HAL_QSPI_ERROR_INVALID_CALLBACK;
  1818. /* update return status */
  1819. status = HAL_ERROR;
  1820. }
  1821. /* Release Lock */
  1822. __HAL_UNLOCK(hqspi);
  1823. return status;
  1824. }
  1825. /**
  1826. * @brief Unregister a User QSPI Callback
  1827. * QSPI Callback is redirected to the weak (surcharged) predefined callback
  1828. * @param hqspi : QSPI handle
  1829. * @param CallbackId : ID of the callback to be unregistered
  1830. * This parameter can be one of the following values:
  1831. * @arg @ref HAL_QSPI_ERROR_CB_ID QSPI Error Callback ID
  1832. * @arg @ref HAL_QSPI_ABORT_CB_ID QSPI Abort Callback ID
  1833. * @arg @ref HAL_QSPI_FIFO_THRESHOLD_CB_ID QSPI FIFO Threshold Callback ID
  1834. * @arg @ref HAL_QSPI_CMD_CPLT_CB_ID QSPI Command Complete Callback ID
  1835. * @arg @ref HAL_QSPI_RX_CPLT_CB_ID QSPI Rx Complete Callback ID
  1836. * @arg @ref HAL_QSPI_TX_CPLT_CB_ID QSPI Tx Complete Callback ID
  1837. * @arg @ref HAL_QSPI_RX_HALF_CPLT_CB_ID QSPI Rx Half Complete Callback ID
  1838. * @arg @ref HAL_QSPI_TX_HALF_CPLT_CB_ID QSPI Tx Half Complete Callback ID
  1839. * @arg @ref HAL_QSPI_STATUS_MATCH_CB_ID QSPI Status Match Callback ID
  1840. * @arg @ref HAL_QSPI_TIMEOUT_CB_ID QSPI Timeout Callback ID
  1841. * @arg @ref HAL_QSPI_MSP_INIT_CB_ID QSPI MspInit callback ID
  1842. * @arg @ref HAL_QSPI_MSP_DEINIT_CB_ID QSPI MspDeInit callback ID
  1843. * @retval status
  1844. */
  1845. HAL_StatusTypeDef HAL_QSPI_UnRegisterCallback (QSPI_HandleTypeDef *hqspi, HAL_QSPI_CallbackIDTypeDef CallbackId)
  1846. {
  1847. HAL_StatusTypeDef status = HAL_OK;
  1848. /* Process locked */
  1849. __HAL_LOCK(hqspi);
  1850. if(hqspi->State == HAL_QSPI_STATE_READY)
  1851. {
  1852. switch (CallbackId)
  1853. {
  1854. case HAL_QSPI_ERROR_CB_ID :
  1855. hqspi->ErrorCallback = HAL_QSPI_ErrorCallback;
  1856. break;
  1857. case HAL_QSPI_ABORT_CB_ID :
  1858. hqspi->AbortCpltCallback = HAL_QSPI_AbortCpltCallback;
  1859. break;
  1860. case HAL_QSPI_FIFO_THRESHOLD_CB_ID :
  1861. hqspi->FifoThresholdCallback = HAL_QSPI_FifoThresholdCallback;
  1862. break;
  1863. case HAL_QSPI_CMD_CPLT_CB_ID :
  1864. hqspi->CmdCpltCallback = HAL_QSPI_CmdCpltCallback;
  1865. break;
  1866. case HAL_QSPI_RX_CPLT_CB_ID :
  1867. hqspi->RxCpltCallback = HAL_QSPI_RxCpltCallback;
  1868. break;
  1869. case HAL_QSPI_TX_CPLT_CB_ID :
  1870. hqspi->TxCpltCallback = HAL_QSPI_TxCpltCallback;
  1871. break;
  1872. case HAL_QSPI_RX_HALF_CPLT_CB_ID :
  1873. hqspi->RxHalfCpltCallback = HAL_QSPI_RxHalfCpltCallback;
  1874. break;
  1875. case HAL_QSPI_TX_HALF_CPLT_CB_ID :
  1876. hqspi->TxHalfCpltCallback = HAL_QSPI_TxHalfCpltCallback;
  1877. break;
  1878. case HAL_QSPI_STATUS_MATCH_CB_ID :
  1879. hqspi->StatusMatchCallback = HAL_QSPI_StatusMatchCallback;
  1880. break;
  1881. case HAL_QSPI_TIMEOUT_CB_ID :
  1882. hqspi->TimeOutCallback = HAL_QSPI_TimeOutCallback;
  1883. break;
  1884. case HAL_QSPI_MSP_INIT_CB_ID :
  1885. hqspi->MspInitCallback = HAL_QSPI_MspInit;
  1886. break;
  1887. case HAL_QSPI_MSP_DEINIT_CB_ID :
  1888. hqspi->MspDeInitCallback = HAL_QSPI_MspDeInit;
  1889. break;
  1890. default :
  1891. /* Update the error code */
  1892. hqspi->ErrorCode |= HAL_QSPI_ERROR_INVALID_CALLBACK;
  1893. /* update return status */
  1894. status = HAL_ERROR;
  1895. break;
  1896. }
  1897. }
  1898. else if (hqspi->State == HAL_QSPI_STATE_RESET)
  1899. {
  1900. switch (CallbackId)
  1901. {
  1902. case HAL_QSPI_MSP_INIT_CB_ID :
  1903. hqspi->MspInitCallback = HAL_QSPI_MspInit;
  1904. break;
  1905. case HAL_QSPI_MSP_DEINIT_CB_ID :
  1906. hqspi->MspDeInitCallback = HAL_QSPI_MspDeInit;
  1907. break;
  1908. default :
  1909. /* Update the error code */
  1910. hqspi->ErrorCode |= HAL_QSPI_ERROR_INVALID_CALLBACK;
  1911. /* update return status */
  1912. status = HAL_ERROR;
  1913. break;
  1914. }
  1915. }
  1916. else
  1917. {
  1918. /* Update the error code */
  1919. hqspi->ErrorCode |= HAL_QSPI_ERROR_INVALID_CALLBACK;
  1920. /* update return status */
  1921. status = HAL_ERROR;
  1922. }
  1923. /* Release Lock */
  1924. __HAL_UNLOCK(hqspi);
  1925. return status;
  1926. }
  1927. #endif
  1928. /**
  1929. * @}
  1930. */
  1931. /** @defgroup QSPI_Exported_Functions_Group3 Peripheral Control and State functions
  1932. * @brief QSPI control and State functions
  1933. *
  1934. @verbatim
  1935. ===============================================================================
  1936. ##### Peripheral Control and State functions #####
  1937. ===============================================================================
  1938. [..]
  1939. This subsection provides a set of functions allowing to :
  1940. (+) Check in run-time the state of the driver.
  1941. (+) Check the error code set during last operation.
  1942. (+) Abort any operation.
  1943. @endverbatim
  1944. * @{
  1945. */
  1946. /**
  1947. * @brief Return the QSPI handle state.
  1948. * @param hqspi QSPI handle
  1949. * @retval HAL state
  1950. */
  1951. HAL_QSPI_StateTypeDef HAL_QSPI_GetState(QSPI_HandleTypeDef *hqspi)
  1952. {
  1953. /* Return QSPI handle state */
  1954. return hqspi->State;
  1955. }
  1956. /**
  1957. * @brief Return the QSPI error code
  1958. * @param hqspi QSPI handle
  1959. * @retval QSPI Error Code
  1960. */
  1961. uint32_t HAL_QSPI_GetError(QSPI_HandleTypeDef *hqspi)
  1962. {
  1963. return hqspi->ErrorCode;
  1964. }
  1965. /**
  1966. * @brief Abort the current transmission
  1967. * @param hqspi QSPI handle
  1968. * @retval HAL status
  1969. */
  1970. HAL_StatusTypeDef HAL_QSPI_Abort(QSPI_HandleTypeDef *hqspi)
  1971. {
  1972. HAL_StatusTypeDef status = HAL_OK;
  1973. uint32_t tickstart = HAL_GetTick();
  1974. /* Check if the state is in one of the busy states */
  1975. if ((hqspi->State & 0x2U) != 0U)
  1976. {
  1977. /* Process unlocked */
  1978. __HAL_UNLOCK(hqspi);
  1979. if ((hqspi->Instance->CR & QUADSPI_CR_DMAEN)!= RESET)
  1980. {
  1981. /* Disable the DMA transfer by clearing the DMAEN bit in the QSPI CR register */
  1982. CLEAR_BIT(hqspi->Instance->CR, QUADSPI_CR_DMAEN);
  1983. /* Abort DMA channel */
  1984. status = HAL_DMA_Abort(hqspi->hdma);
  1985. if(status != HAL_OK)
  1986. {
  1987. hqspi->ErrorCode |= HAL_QSPI_ERROR_DMA;
  1988. }
  1989. }
  1990. /* Configure QSPI: CR register with Abort request */
  1991. SET_BIT(hqspi->Instance->CR, QUADSPI_CR_ABORT);
  1992. /* Wait until TC flag is set to go back in idle state */
  1993. status = QSPI_WaitFlagStateUntilTimeout(hqspi, QSPI_FLAG_TC, SET, tickstart, hqspi->Timeout);
  1994. if(status == HAL_OK)
  1995. {
  1996. __HAL_QSPI_CLEAR_FLAG(hqspi, QSPI_FLAG_TC);
  1997. /* Wait until BUSY flag is reset */
  1998. status = QSPI_WaitFlagStateUntilTimeout(hqspi, QSPI_FLAG_BUSY, RESET, tickstart, hqspi->Timeout);
  1999. }
  2000. if (status == HAL_OK)
  2001. {
  2002. /* Update state */
  2003. hqspi->State = HAL_QSPI_STATE_READY;
  2004. }
  2005. }
  2006. return status;
  2007. }
  2008. /**
  2009. * @brief Abort the current transmission (non-blocking function)
  2010. * @param hqspi QSPI handle
  2011. * @retval HAL status
  2012. */
  2013. HAL_StatusTypeDef HAL_QSPI_Abort_IT(QSPI_HandleTypeDef *hqspi)
  2014. {
  2015. HAL_StatusTypeDef status = HAL_OK;
  2016. /* Check if the state is in one of the busy states */
  2017. if ((hqspi->State & 0x2U) != 0U)
  2018. {
  2019. /* Process unlocked */
  2020. __HAL_UNLOCK(hqspi);
  2021. /* Update QSPI state */
  2022. hqspi->State = HAL_QSPI_STATE_ABORT;
  2023. /* Disable all interrupts */
  2024. __HAL_QSPI_DISABLE_IT(hqspi, (QSPI_IT_TO | QSPI_IT_SM | QSPI_IT_FT | QSPI_IT_TC | QSPI_IT_TE));
  2025. if ((hqspi->Instance->CR & QUADSPI_CR_DMAEN)!= RESET)
  2026. {
  2027. /* Disable the DMA transfer by clearing the DMAEN bit in the QSPI CR register */
  2028. CLEAR_BIT(hqspi->Instance->CR, QUADSPI_CR_DMAEN);
  2029. /* Abort DMA channel */
  2030. hqspi->hdma->XferAbortCallback = QSPI_DMAAbortCplt;
  2031. if (HAL_DMA_Abort_IT(hqspi->hdma) != HAL_OK)
  2032. {
  2033. /* Change state of QSPI */
  2034. hqspi->State = HAL_QSPI_STATE_READY;
  2035. /* Abort Complete callback */
  2036. #if (USE_HAL_QSPI_REGISTER_CALLBACKS == 1)
  2037. hqspi->AbortCpltCallback(hqspi);
  2038. #else
  2039. HAL_QSPI_AbortCpltCallback(hqspi);
  2040. #endif
  2041. }
  2042. }
  2043. else
  2044. {
  2045. /* Clear interrupt */
  2046. __HAL_QSPI_CLEAR_FLAG(hqspi, QSPI_FLAG_TC);
  2047. /* Enable the QSPI Transfer Complete Interrupt */
  2048. __HAL_QSPI_ENABLE_IT(hqspi, QSPI_IT_TC);
  2049. /* Configure QSPI: CR register with Abort request */
  2050. SET_BIT(hqspi->Instance->CR, QUADSPI_CR_ABORT);
  2051. }
  2052. }
  2053. return status;
  2054. }
  2055. /** @brief Set QSPI timeout
  2056. * @param hqspi QSPI handle.
  2057. * @param Timeout Timeout for the QSPI memory access.
  2058. * @retval None
  2059. */
  2060. void HAL_QSPI_SetTimeout(QSPI_HandleTypeDef *hqspi, uint32_t Timeout)
  2061. {
  2062. hqspi->Timeout = Timeout;
  2063. }
  2064. /** @brief Set QSPI Fifo threshold.
  2065. * @param hqspi QSPI handle.
  2066. * @param Threshold Threshold of the Fifo (value between 1 and 16).
  2067. * @retval HAL status
  2068. */
  2069. HAL_StatusTypeDef HAL_QSPI_SetFifoThreshold(QSPI_HandleTypeDef *hqspi, uint32_t Threshold)
  2070. {
  2071. HAL_StatusTypeDef status = HAL_OK;
  2072. /* Process locked */
  2073. __HAL_LOCK(hqspi);
  2074. if(hqspi->State == HAL_QSPI_STATE_READY)
  2075. {
  2076. /* Synchronize init structure with new FIFO threshold value */
  2077. hqspi->Init.FifoThreshold = Threshold;
  2078. /* Configure QSPI FIFO Threshold */
  2079. MODIFY_REG(hqspi->Instance->CR, QUADSPI_CR_FTHRES,
  2080. ((hqspi->Init.FifoThreshold - 1U) << QUADSPI_CR_FTHRES_Pos));
  2081. }
  2082. else
  2083. {
  2084. status = HAL_BUSY;
  2085. }
  2086. /* Process unlocked */
  2087. __HAL_UNLOCK(hqspi);
  2088. /* Return function status */
  2089. return status;
  2090. }
  2091. /** @brief Get QSPI Fifo threshold.
  2092. * @param hqspi QSPI handle.
  2093. * @retval Fifo threshold (value between 1 and 16)
  2094. */
  2095. uint32_t HAL_QSPI_GetFifoThreshold(QSPI_HandleTypeDef *hqspi)
  2096. {
  2097. return ((READ_BIT(hqspi->Instance->CR, QUADSPI_CR_FTHRES) >> QUADSPI_CR_FTHRES_Pos) + 1U);
  2098. }
  2099. /**
  2100. * @}
  2101. */
  2102. /* Private functions ---------------------------------------------------------*/
  2103. /**
  2104. * @brief DMA QSPI receive process complete callback.
  2105. * @param hdma DMA handle
  2106. * @retval None
  2107. */
  2108. static void QSPI_DMARxCplt(DMA_HandleTypeDef *hdma)
  2109. {
  2110. QSPI_HandleTypeDef* hqspi = ( QSPI_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  2111. hqspi->RxXferCount = 0U;
  2112. /* Enable the QSPI transfer complete Interrupt */
  2113. __HAL_QSPI_ENABLE_IT(hqspi, QSPI_IT_TC);
  2114. }
  2115. /**
  2116. * @brief DMA QSPI transmit process complete callback.
  2117. * @param hdma DMA handle
  2118. * @retval None
  2119. */
  2120. static void QSPI_DMATxCplt(DMA_HandleTypeDef *hdma)
  2121. {
  2122. QSPI_HandleTypeDef* hqspi = ( QSPI_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  2123. hqspi->TxXferCount = 0U;
  2124. /* Enable the QSPI transfer complete Interrupt */
  2125. __HAL_QSPI_ENABLE_IT(hqspi, QSPI_IT_TC);
  2126. }
  2127. /**
  2128. * @brief DMA QSPI receive process half complete callback
  2129. * @param hdma DMA handle
  2130. * @retval None
  2131. */
  2132. static void QSPI_DMARxHalfCplt(DMA_HandleTypeDef *hdma)
  2133. {
  2134. QSPI_HandleTypeDef* hqspi = (QSPI_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
  2135. #if (USE_HAL_QSPI_REGISTER_CALLBACKS == 1)
  2136. hqspi->RxHalfCpltCallback(hqspi);
  2137. #else
  2138. HAL_QSPI_RxHalfCpltCallback(hqspi);
  2139. #endif
  2140. }
  2141. /**
  2142. * @brief DMA QSPI transmit process half complete callback
  2143. * @param hdma DMA handle
  2144. * @retval None
  2145. */
  2146. static void QSPI_DMATxHalfCplt(DMA_HandleTypeDef *hdma)
  2147. {
  2148. QSPI_HandleTypeDef* hqspi = (QSPI_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
  2149. #if (USE_HAL_QSPI_REGISTER_CALLBACKS == 1)
  2150. hqspi->TxHalfCpltCallback(hqspi);
  2151. #else
  2152. HAL_QSPI_TxHalfCpltCallback(hqspi);
  2153. #endif
  2154. }
  2155. /**
  2156. * @brief DMA QSPI communication error callback.
  2157. * @param hdma DMA handle
  2158. * @retval None
  2159. */
  2160. static void QSPI_DMAError(DMA_HandleTypeDef *hdma)
  2161. {
  2162. QSPI_HandleTypeDef* hqspi = ( QSPI_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  2163. /* if DMA error is FIFO error ignore it */
  2164. if(HAL_DMA_GetError(hdma) != HAL_DMA_ERROR_FE)
  2165. {
  2166. hqspi->RxXferCount = 0U;
  2167. hqspi->TxXferCount = 0U;
  2168. hqspi->ErrorCode |= HAL_QSPI_ERROR_DMA;
  2169. /* Disable the DMA transfer by clearing the DMAEN bit in the QSPI CR register */
  2170. CLEAR_BIT(hqspi->Instance->CR, QUADSPI_CR_DMAEN);
  2171. /* Abort the QSPI */
  2172. HAL_QSPI_Abort_IT(hqspi);
  2173. }
  2174. }
  2175. /**
  2176. * @brief DMA QSPI abort complete callback.
  2177. * @param hdma DMA handle
  2178. * @retval None
  2179. */
  2180. static void QSPI_DMAAbortCplt(DMA_HandleTypeDef *hdma)
  2181. {
  2182. QSPI_HandleTypeDef* hqspi = ( QSPI_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  2183. hqspi->RxXferCount = 0U;
  2184. hqspi->TxXferCount = 0U;
  2185. if(hqspi->State == HAL_QSPI_STATE_ABORT)
  2186. {
  2187. /* DMA Abort called by QSPI abort */
  2188. /* Clear interrupt */
  2189. __HAL_QSPI_CLEAR_FLAG(hqspi, QSPI_FLAG_TC);
  2190. /* Enable the QSPI Transfer Complete Interrupt */
  2191. __HAL_QSPI_ENABLE_IT(hqspi, QSPI_IT_TC);
  2192. /* Configure QSPI: CR register with Abort request */
  2193. SET_BIT(hqspi->Instance->CR, QUADSPI_CR_ABORT);
  2194. }
  2195. else
  2196. {
  2197. /* DMA Abort called due to a transfer error interrupt */
  2198. /* Change state of QSPI */
  2199. hqspi->State = HAL_QSPI_STATE_READY;
  2200. /* Error callback */
  2201. HAL_QSPI_ErrorCallback(hqspi);
  2202. }
  2203. }
  2204. /**
  2205. * @brief Wait for a flag state until timeout.
  2206. * @param hqspi QSPI handle
  2207. * @param Flag Flag checked
  2208. * @param State Value of the flag expected
  2209. * @param Timeout Duration of the time out
  2210. * @param tickstart tick start value
  2211. * @retval HAL status
  2212. */
  2213. static HAL_StatusTypeDef QSPI_WaitFlagStateUntilTimeout(QSPI_HandleTypeDef *hqspi, uint32_t Flag,
  2214. FlagStatus State, uint32_t tickstart, uint32_t Timeout)
  2215. {
  2216. /* Wait until flag is in expected state */
  2217. while((FlagStatus)(__HAL_QSPI_GET_FLAG(hqspi, Flag)) != State)
  2218. {
  2219. /* Check for the Timeout */
  2220. if (Timeout != HAL_MAX_DELAY)
  2221. {
  2222. if((Timeout == 0U) || ((HAL_GetTick() - tickstart) > Timeout))
  2223. {
  2224. hqspi->State = HAL_QSPI_STATE_ERROR;
  2225. hqspi->ErrorCode |= HAL_QSPI_ERROR_TIMEOUT;
  2226. return HAL_ERROR;
  2227. }
  2228. }
  2229. }
  2230. return HAL_OK;
  2231. }
  2232. /**
  2233. * @brief Configure the communication registers.
  2234. * @param hqspi QSPI handle
  2235. * @param cmd structure that contains the command configuration information
  2236. * @param FunctionalMode functional mode to configured
  2237. * This parameter can be one of the following values:
  2238. * @arg QSPI_FUNCTIONAL_MODE_INDIRECT_WRITE: Indirect write mode
  2239. * @arg QSPI_FUNCTIONAL_MODE_INDIRECT_READ: Indirect read mode
  2240. * @arg QSPI_FUNCTIONAL_MODE_AUTO_POLLING: Automatic polling mode
  2241. * @arg QSPI_FUNCTIONAL_MODE_MEMORY_MAPPED: Memory-mapped mode
  2242. * @retval None
  2243. */
  2244. static void QSPI_Config(QSPI_HandleTypeDef *hqspi, QSPI_CommandTypeDef *cmd, uint32_t FunctionalMode)
  2245. {
  2246. assert_param(IS_QSPI_FUNCTIONAL_MODE(FunctionalMode));
  2247. if ((cmd->DataMode != QSPI_DATA_NONE) && (FunctionalMode != QSPI_FUNCTIONAL_MODE_MEMORY_MAPPED))
  2248. {
  2249. /* Configure QSPI: DLR register with the number of data to read or write */
  2250. WRITE_REG(hqspi->Instance->DLR, (cmd->NbData - 1U));
  2251. }
  2252. if (cmd->InstructionMode != QSPI_INSTRUCTION_NONE)
  2253. {
  2254. if (cmd->AlternateByteMode != QSPI_ALTERNATE_BYTES_NONE)
  2255. {
  2256. /* Configure QSPI: ABR register with alternate bytes value */
  2257. WRITE_REG(hqspi->Instance->ABR, cmd->AlternateBytes);
  2258. if (cmd->AddressMode != QSPI_ADDRESS_NONE)
  2259. {
  2260. /*---- Command with instruction, address and alternate bytes ----*/
  2261. /* Configure QSPI: CCR register with all communications parameters */
  2262. WRITE_REG(hqspi->Instance->CCR, (cmd->DdrMode | cmd->DdrHoldHalfCycle | cmd->SIOOMode |
  2263. cmd->DataMode | (cmd->DummyCycles << 18U) | cmd->AlternateBytesSize |
  2264. cmd->AlternateByteMode | cmd->AddressSize | cmd->AddressMode |
  2265. cmd->InstructionMode | cmd->Instruction | FunctionalMode));
  2266. if (FunctionalMode != QSPI_FUNCTIONAL_MODE_MEMORY_MAPPED)
  2267. {
  2268. /* Configure QSPI: AR register with address value */
  2269. WRITE_REG(hqspi->Instance->AR, cmd->Address);
  2270. }
  2271. }
  2272. else
  2273. {
  2274. /*---- Command with instruction and alternate bytes ----*/
  2275. /* Configure QSPI: CCR register with all communications parameters */
  2276. WRITE_REG(hqspi->Instance->CCR, (cmd->DdrMode | cmd->DdrHoldHalfCycle | cmd->SIOOMode |
  2277. cmd->DataMode | (cmd->DummyCycles << 18U) | cmd->AlternateBytesSize |
  2278. cmd->AlternateByteMode | cmd->AddressMode | cmd->InstructionMode |
  2279. cmd->Instruction | FunctionalMode));
  2280. }
  2281. }
  2282. else
  2283. {
  2284. if (cmd->AddressMode != QSPI_ADDRESS_NONE)
  2285. {
  2286. /*---- Command with instruction and address ----*/
  2287. /* Configure QSPI: CCR register with all communications parameters */
  2288. WRITE_REG(hqspi->Instance->CCR, (cmd->DdrMode | cmd->DdrHoldHalfCycle | cmd->SIOOMode |
  2289. cmd->DataMode | (cmd->DummyCycles << 18U) | cmd->AlternateByteMode |
  2290. cmd->AddressSize | cmd->AddressMode | cmd->InstructionMode |
  2291. cmd->Instruction | FunctionalMode));
  2292. if (FunctionalMode != QSPI_FUNCTIONAL_MODE_MEMORY_MAPPED)
  2293. {
  2294. /* Configure QSPI: AR register with address value */
  2295. WRITE_REG(hqspi->Instance->AR, cmd->Address);
  2296. }
  2297. }
  2298. else
  2299. {
  2300. /*---- Command with only instruction ----*/
  2301. /* Configure QSPI: CCR register with all communications parameters */
  2302. WRITE_REG(hqspi->Instance->CCR, (cmd->DdrMode | cmd->DdrHoldHalfCycle | cmd->SIOOMode |
  2303. cmd->DataMode | (cmd->DummyCycles << 18U) | cmd->AlternateByteMode |
  2304. cmd->AddressMode | cmd->InstructionMode | cmd->Instruction |
  2305. FunctionalMode));
  2306. }
  2307. }
  2308. }
  2309. else
  2310. {
  2311. if (cmd->AlternateByteMode != QSPI_ALTERNATE_BYTES_NONE)
  2312. {
  2313. /* Configure QSPI: ABR register with alternate bytes value */
  2314. WRITE_REG(hqspi->Instance->ABR, cmd->AlternateBytes);
  2315. if (cmd->AddressMode != QSPI_ADDRESS_NONE)
  2316. {
  2317. /*---- Command with address and alternate bytes ----*/
  2318. /* Configure QSPI: CCR register with all communications parameters */
  2319. WRITE_REG(hqspi->Instance->CCR, (cmd->DdrMode | cmd->DdrHoldHalfCycle | cmd->SIOOMode |
  2320. cmd->DataMode | (cmd->DummyCycles << 18U) | cmd->AlternateBytesSize |
  2321. cmd->AlternateByteMode | cmd->AddressSize | cmd->AddressMode |
  2322. cmd->InstructionMode | FunctionalMode));
  2323. if (FunctionalMode != QSPI_FUNCTIONAL_MODE_MEMORY_MAPPED)
  2324. {
  2325. /* Configure QSPI: AR register with address value */
  2326. WRITE_REG(hqspi->Instance->AR, cmd->Address);
  2327. }
  2328. }
  2329. else
  2330. {
  2331. /*---- Command with only alternate bytes ----*/
  2332. /* Configure QSPI: CCR register with all communications parameters */
  2333. WRITE_REG(hqspi->Instance->CCR, (cmd->DdrMode | cmd->DdrHoldHalfCycle | cmd->SIOOMode |
  2334. cmd->DataMode | (cmd->DummyCycles << 18U) | cmd->AlternateBytesSize |
  2335. cmd->AlternateByteMode | cmd->AddressMode | cmd->InstructionMode |
  2336. FunctionalMode));
  2337. }
  2338. }
  2339. else
  2340. {
  2341. if (cmd->AddressMode != QSPI_ADDRESS_NONE)
  2342. {
  2343. /*---- Command with only address ----*/
  2344. /* Configure QSPI: CCR register with all communications parameters */
  2345. WRITE_REG(hqspi->Instance->CCR, (cmd->DdrMode | cmd->DdrHoldHalfCycle | cmd->SIOOMode |
  2346. cmd->DataMode | (cmd->DummyCycles << 18U) | cmd->AlternateByteMode |
  2347. cmd->AddressSize | cmd->AddressMode | cmd->InstructionMode |
  2348. FunctionalMode));
  2349. if (FunctionalMode != QSPI_FUNCTIONAL_MODE_MEMORY_MAPPED)
  2350. {
  2351. /* Configure QSPI: AR register with address value */
  2352. WRITE_REG(hqspi->Instance->AR, cmd->Address);
  2353. }
  2354. }
  2355. else
  2356. {
  2357. /*---- Command with only data phase ----*/
  2358. if (cmd->DataMode != QSPI_DATA_NONE)
  2359. {
  2360. /* Configure QSPI: CCR register with all communications parameters */
  2361. WRITE_REG(hqspi->Instance->CCR, (cmd->DdrMode | cmd->DdrHoldHalfCycle | cmd->SIOOMode |
  2362. cmd->DataMode | (cmd->DummyCycles << 18U) | cmd->AlternateByteMode |
  2363. cmd->AddressMode | cmd->InstructionMode | FunctionalMode));
  2364. }
  2365. }
  2366. }
  2367. }
  2368. }
  2369. /**
  2370. * @}
  2371. */
  2372. #endif /* STM32F446xx || STM32F469xx || STM32F479xx || STM32F412Zx || STM32F412Vx || STM32F412Rx
  2373. STM32F413xx || STM32F423xx */
  2374. #endif /* HAL_QSPI_MODULE_ENABLED */
  2375. /**
  2376. * @}
  2377. */
  2378. /**
  2379. * @}
  2380. */
  2381. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/