stm32f4xx_hal_qspi.c 92 KB

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