stm32h7xx_hal_qspi.c 86 KB

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