stm32l4xx_hal_qspi.c 90 KB

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