stm32h7xx_hal_qspi.c 71 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207
  1. /**
  2. ******************************************************************************
  3. * @file stm32h7xx_hal_qspi.c
  4. * @author MCD Application Team
  5. * @version V1.0.0
  6. * @date 21-April-2017
  7. * @brief QSPI HAL module driver.
  8. * This file provides firmware functions to manage the following
  9. * functionalities of the QuadSPI interface (QSPI).
  10. * + Initialization and de-initialization functions
  11. * + Indirect functional mode management
  12. * + Memory-mapped functional mode management
  13. * + Auto-polling functional mode management
  14. * + Interrupts and flags management
  15. * + MDMA channel configuration for indirect functional mode
  16. * + Errors management and abort functionality
  17. *
  18. *
  19. @verbatim
  20. ===============================================================================
  21. ##### How to use this driver #####
  22. ===============================================================================
  23. [..]
  24. *** Initialization ***
  25. ======================
  26. [..]
  27. (#) As prerequisite, fill in the HAL_QSPI_MspInit() :
  28. (++) Enable QuadSPI clock interface with __HAL_RCC_QSPI_CLK_ENABLE().
  29. (++) Reset QuadSPI IP with __HAL_RCC_QSPI_FORCE_RESET() and __HAL_RCC_QSPI_RELEASE_RESET().
  30. (++) Enable the clocks for the QuadSPI GPIOS with __HAL_RCC_GPIOx_CLK_ENABLE().
  31. (++) Configure these QuadSPI pins in alternate mode using HAL_GPIO_Init().
  32. (++) If interrupt mode is used, enable and configure QuadSPI global
  33. interrupt with HAL_NVIC_SetPriority() and HAL_NVIC_EnableIRQ().
  34. (++) If DMA mode is used, enable the clocks for the QuadSPI MDMA
  35. with __HAL_RCC_MDMA_CLK_ENABLE(), configure MDMA with HAL_MDMA_Init(),
  36. link it with QuadSPI handle using __HAL_LINKDMA(), enable and configure
  37. MDMA global interrupt with HAL_NVIC_SetPriority() and HAL_NVIC_EnableIRQ().
  38. (#) Configure the flash size, the clock prescaler, the fifo threshold, the
  39. clock mode, the sample shifting and the CS high time using the HAL_QSPI_Init() function.
  40. *** Indirect functional mode ***
  41. ================================
  42. [..]
  43. (#) Configure the command sequence using the HAL_QSPI_Command() or HAL_QSPI_Command_IT()
  44. functions :
  45. (++) Instruction phase : the mode used and if present the instruction opcode.
  46. (++) Address phase : the mode used and if present the size and the address value.
  47. (++) Alternate-bytes phase : the mode used and if present the size and the alternate
  48. bytes values.
  49. (++) Dummy-cycles phase : the number of dummy cycles (mode used is same as data phase).
  50. (++) Data phase : the mode used and if present the number of bytes.
  51. (++) Double Data Rate (DDR) mode : the activation (or not) of this mode and the delay
  52. if activated.
  53. (++) Sending Instruction Only Once (SIOO) mode : the activation (or not) of this mode.
  54. (#) If no data is required for the command, it is sent directly to the memory :
  55. (++) In polling mode, the output of the function is done when the transfer is complete.
  56. (++) In interrupt mode, HAL_QSPI_CmdCpltCallback() will be called when the transfer is complete.
  57. (#) For the indirect write mode, use HAL_QSPI_Transmit(), HAL_QSPI_Transmit_DMA() or
  58. HAL_QSPI_Transmit_IT() after the command configuration :
  59. (++) In polling mode, the output of the function is done when the transfer is complete.
  60. (++) In interrupt mode, HAL_QSPI_FifoThresholdCallback() will be called when the fifo threshold
  61. is reached and HAL_QSPI_TxCpltCallback() will be called when the transfer is complete.
  62. (++) In DMA mode, HAL_QSPI_TxHalfCpltCallback() will be called at the half transfer and
  63. HAL_QSPI_TxCpltCallback() will be called when the transfer is complete.
  64. (#) For the indirect read mode, use HAL_QSPI_Receive(), HAL_QSPI_Receive_DMA() or
  65. HAL_QSPI_Receive_IT() after the command configuration :
  66. (++) In polling mode, the output of the function is done when the transfer is complete.
  67. (++) In interrupt mode, HAL_QSPI_FifoThresholdCallback() will be called when the fifo threshold
  68. is reached and HAL_QSPI_RxCpltCallback() will be called when the transfer is complete.
  69. (++) In DMA mode, HAL_QSPI_RxHalfCpltCallback() will be called at the half transfer and
  70. HAL_QSPI_RxCpltCallback() will be called when the transfer is complete.
  71. *** Auto-polling functional mode ***
  72. ====================================
  73. [..]
  74. (#) Configure the command sequence and the auto-polling functional mode using the
  75. HAL_QSPI_AutoPolling() or HAL_QSPI_AutoPolling_IT() functions :
  76. (++) Instruction phase : the mode used and if present the instruction opcode.
  77. (++) Address phase : the mode used and if present the size and the address value.
  78. (++) Alternate-bytes phase : the mode used and if present the size and the alternate
  79. bytes values.
  80. (++) Dummy-cycles phase : the number of dummy cycles (mode used is same as data phase).
  81. (++) Data phase : the mode used.
  82. (++) Double Data Rate (DDR) mode : the activation (or not) of this mode and the delay
  83. if activated.
  84. (++) Sending Instruction Only Once (SIOO) mode : the activation (or not) of this mode.
  85. (++) The size of the status bytes, the match value, the mask used, the match mode (OR/AND),
  86. the polling interval and the automatic stop activation.
  87. (#) After the configuration :
  88. (++) In polling mode, the output of the function is done when the status match is reached. The
  89. automatic stop is activated to avoid an infinite loop.
  90. (++) In interrupt mode, HAL_QSPI_StatusMatchCallback() will be called each time the status match is reached.
  91. *** Memory-mapped functional mode ***
  92. =====================================
  93. [..]
  94. (#) Configure the command sequence and the memory-mapped functional mode using the
  95. HAL_QSPI_MemoryMapped() functions :
  96. (++) Instruction phase : the mode used and if present the instruction opcode.
  97. (++) Address phase : the mode used and the size.
  98. (++) Alternate-bytes phase : the mode used and if present the size and the alternate
  99. bytes values.
  100. (++) Dummy-cycles phase : the number of dummy cycles (mode used is same as data phase).
  101. (++) Data phase : the mode used.
  102. (++) Double Data Rate (DDR) mode : the activation (or not) of this mode and the delay
  103. if activated.
  104. (++) Sending Instruction Only Once (SIOO) mode : the activation (or not) of this mode.
  105. (++) The timeout activation and the timeout period.
  106. (#) After the configuration, the QuadSPI will be used as soon as an access on the AHB is done on
  107. the address range. HAL_QSPI_TimeOutCallback() will be called when the timeout expires.
  108. *** Errors management and abort functionality ***
  109. =================================================
  110. [..]
  111. (#) HAL_QSPI_GetError() function gives the error raised during the last operation.
  112. (#) HAL_QSPI_Abort() and HAL_QSPI_AbortIT() functions aborts any on-going operation and
  113. flushes the fifo :
  114. (++) In polling mode, the output of the function is done when the transfer
  115. complete bit is set and the busy bit cleared.
  116. (++) In interrupt mode, HAL_QSPI_AbortCpltCallback() will be called when
  117. the transfer complete bi is set.
  118. *** Control functions ***
  119. =========================
  120. [..]
  121. (#) HAL_QSPI_GetState() function gives the current state of the HAL QuadSPI driver.
  122. (#) HAL_QSPI_SetTimeout() function configures the timeout value used in the driver.
  123. (#) HAL_QSPI_SetFifoThreshold() function configures the threshold on the Fifo of the QSPI IP.
  124. (#) HAL_QSPI_GetFifoThreshold() function gives the current of the Fifo's threshold
  125. *** Workarounds linked to Silicon Limitation ***
  126. ====================================================
  127. [..]
  128. (#) Workarounds Implemented inside HAL Driver
  129. (++) Extra data written in the FIFO at the end of a read transfer
  130. @endverbatim
  131. ******************************************************************************
  132. * @attention
  133. *
  134. * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
  135. *
  136. * Redistribution and use in source and binary forms, with or without modification,
  137. * are permitted provided that the following conditions are met:
  138. * 1. Redistributions of source code must retain the above copyright notice,
  139. * this list of conditions and the following disclaimer.
  140. * 2. Redistributions in binary form must reproduce the above copyright notice,
  141. * this list of conditions and the following disclaimer in the documentation
  142. * and/or other materials provided with the distribution.
  143. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  144. * may be used to endorse or promote products derived from this software
  145. * without specific prior written permission.
  146. *
  147. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  148. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  149. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  150. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  151. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  152. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  153. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  154. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  155. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  156. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  157. *
  158. ******************************************************************************
  159. */
  160. /* Includes ------------------------------------------------------------------*/
  161. #include "stm32h7xx_hal.h"
  162. /** @addtogroup STM32H7xx_HAL_Driver
  163. * @{
  164. */
  165. /** @defgroup QSPI QSPI
  166. * @brief QSPI HAL module driver
  167. * @{
  168. */
  169. #ifdef HAL_QSPI_MODULE_ENABLED
  170. /* Private typedef -----------------------------------------------------------*/
  171. /* Private define ------------------------------------------------------------*/
  172. /** @defgroup QSPI_Private_Constants QSPI Private Constants
  173. * @{
  174. */
  175. #define QSPI_FUNCTIONAL_MODE_INDIRECT_WRITE ((uint32_t)0x00000000) /*!<Indirect write mode*/
  176. #define QSPI_FUNCTIONAL_MODE_INDIRECT_READ ((uint32_t)QUADSPI_CCR_FMODE_0) /*!<Indirect read mode*/
  177. #define QSPI_FUNCTIONAL_MODE_AUTO_POLLING ((uint32_t)QUADSPI_CCR_FMODE_1) /*!<Automatic polling mode*/
  178. #define QSPI_FUNCTIONAL_MODE_MEMORY_MAPPED ((uint32_t)QUADSPI_CCR_FMODE) /*!<Memory-mapped mode*/
  179. /**
  180. * @}
  181. */
  182. /* Private macro -------------------------------------------------------------*/
  183. /** @defgroup QSPI_Private_Macros QSPI Private Macros
  184. * @{
  185. */
  186. #define IS_QSPI_FUNCTIONAL_MODE(MODE) (((MODE) == QSPI_FUNCTIONAL_MODE_INDIRECT_WRITE) || \
  187. ((MODE) == QSPI_FUNCTIONAL_MODE_INDIRECT_READ) || \
  188. ((MODE) == QSPI_FUNCTIONAL_MODE_AUTO_POLLING) || \
  189. ((MODE) == QSPI_FUNCTIONAL_MODE_MEMORY_MAPPED))
  190. /**
  191. * @}
  192. */
  193. /* Private variables ---------------------------------------------------------*/
  194. /* Private function prototypes -----------------------------------------------*/
  195. static void QSPI_DMARxCplt(MDMA_HandleTypeDef *hmdma);
  196. static void QSPI_DMATxCplt(MDMA_HandleTypeDef *hmdma);
  197. static void QSPI_DMAError(MDMA_HandleTypeDef *hmdma);
  198. static void QSPI_DMAAbortCplt(MDMA_HandleTypeDef *hmdma);
  199. static HAL_StatusTypeDef QSPI_WaitFlagStateUntilTimeout(QSPI_HandleTypeDef *hqspi, uint32_t Flag, FlagStatus State, uint32_t tickstart, uint32_t Timeout);
  200. static void QSPI_Config(QSPI_HandleTypeDef *hqspi, QSPI_CommandTypeDef *cmd, uint32_t FunctionalMode);
  201. /* Exported functions --------------------------------------------------------*/
  202. /** @defgroup QSPI_Exported_Functions QSPI Exported Functions
  203. * @{
  204. */
  205. /** @defgroup QSPI_Exported_Functions_Group1 Initialization/de-initialization functions
  206. * @brief Initialization and Configuration functions
  207. *
  208. @verbatim
  209. ===============================================================================
  210. ##### Initialization and Configuration functions #####
  211. ===============================================================================
  212. [..]
  213. This subsection provides a set of functions allowing to :
  214. (+) Initialize the QuadSPI.
  215. (+) De-initialize the QuadSPI.
  216. @endverbatim
  217. * @{
  218. */
  219. /**
  220. * @brief Initialize the QSPI mode according to the specified parameters
  221. * in the QSPI_InitTypeDef and initialize the associated handle.
  222. * @param hqspi: QSPI handle
  223. * @retval HAL status
  224. */
  225. HAL_StatusTypeDef HAL_QSPI_Init(QSPI_HandleTypeDef *hqspi)
  226. {
  227. HAL_StatusTypeDef status = HAL_ERROR;
  228. uint32_t tickstart = HAL_GetTick();
  229. /* Check the QSPI handle allocation */
  230. if(hqspi == NULL)
  231. {
  232. return HAL_ERROR;
  233. }
  234. /* Check the parameters */
  235. assert_param(IS_QSPI_ALL_INSTANCE(hqspi->Instance));
  236. assert_param(IS_QSPI_CLOCK_PRESCALER(hqspi->Init.ClockPrescaler));
  237. assert_param(IS_QSPI_FIFO_THRESHOLD(hqspi->Init.FifoThreshold));
  238. assert_param(IS_QSPI_SSHIFT(hqspi->Init.SampleShifting));
  239. assert_param(IS_QSPI_FLASH_SIZE(hqspi->Init.FlashSize));
  240. assert_param(IS_QSPI_CS_HIGH_TIME(hqspi->Init.ChipSelectHighTime));
  241. assert_param(IS_QSPI_CLOCK_MODE(hqspi->Init.ClockMode));
  242. assert_param(IS_QSPI_DUAL_FLASH_MODE(hqspi->Init.DualFlash));
  243. if (hqspi->Init.DualFlash != QSPI_DUALFLASH_ENABLE )
  244. {
  245. assert_param(IS_QSPI_FLASH_ID(hqspi->Init.FlashID));
  246. }
  247. /* Process locked */
  248. __HAL_LOCK(hqspi);
  249. if(hqspi->State == HAL_QSPI_STATE_RESET)
  250. {
  251. /* Allocate lock resource and initialize it */
  252. hqspi->Lock = HAL_UNLOCKED;
  253. /* Init the low level hardware : GPIO, CLOCK */
  254. HAL_QSPI_MspInit(hqspi);
  255. /* Configure the default timeout for the QSPI memory access */
  256. HAL_QSPI_SetTimeout(hqspi, HAL_QPSI_TIMEOUT_DEFAULT_VALUE);
  257. }
  258. /* Configure QSPI FIFO Threshold */
  259. MODIFY_REG(hqspi->Instance->CR, QUADSPI_CR_FTHRES,
  260. ((hqspi->Init.FifoThreshold - 1) << POSITION_VAL(QUADSPI_CR_FTHRES)));
  261. /* Wait till BUSY flag reset */
  262. status = QSPI_WaitFlagStateUntilTimeout(hqspi, QSPI_FLAG_BUSY, RESET, tickstart, hqspi->Timeout);
  263. if(status == HAL_OK)
  264. {
  265. /* Configure QSPI Clock Prescaler and Sample Shift */
  266. MODIFY_REG(hqspi->Instance->CR, (QUADSPI_CR_PRESCALER | QUADSPI_CR_SSHIFT | QUADSPI_CR_FSEL | QUADSPI_CR_DFM),
  267. ((hqspi->Init.ClockPrescaler << POSITION_VAL(QUADSPI_CR_PRESCALER)) |
  268. hqspi->Init.SampleShifting | hqspi->Init.FlashID | hqspi->Init.DualFlash));
  269. /* Configure QSPI Flash Size, CS High Time and Clock Mode */
  270. MODIFY_REG(hqspi->Instance->DCR, (QUADSPI_DCR_FSIZE | QUADSPI_DCR_CSHT | QUADSPI_DCR_CKMODE),
  271. ((hqspi->Init.FlashSize << POSITION_VAL(QUADSPI_DCR_FSIZE)) |
  272. hqspi->Init.ChipSelectHighTime | hqspi->Init.ClockMode));
  273. /* Enable the QSPI peripheral */
  274. __HAL_QSPI_ENABLE(hqspi);
  275. /* Set QSPI error code to none */
  276. hqspi->ErrorCode = HAL_QSPI_ERROR_NONE;
  277. /* Initialize the QSPI state */
  278. hqspi->State = HAL_QSPI_STATE_READY;
  279. }
  280. /* Release Lock */
  281. __HAL_UNLOCK(hqspi);
  282. /* Return function status */
  283. return status;
  284. }
  285. /**
  286. * @brief De-Initialize the QSPI peripheral.
  287. * @param hqspi: QSPI handle
  288. * @retval HAL status
  289. */
  290. HAL_StatusTypeDef HAL_QSPI_DeInit(QSPI_HandleTypeDef *hqspi)
  291. {
  292. /* Check the QSPI handle allocation */
  293. if(hqspi == NULL)
  294. {
  295. return HAL_ERROR;
  296. }
  297. /* Process locked */
  298. __HAL_LOCK(hqspi);
  299. /* Disable the QSPI Peripheral Clock */
  300. __HAL_QSPI_DISABLE(hqspi);
  301. /* DeInit the low level hardware: GPIO, CLOCK, NVIC... */
  302. HAL_QSPI_MspDeInit(hqspi);
  303. /* Set QSPI error code to none */
  304. hqspi->ErrorCode = HAL_QSPI_ERROR_NONE;
  305. /* Initialize the QSPI state */
  306. hqspi->State = HAL_QSPI_STATE_RESET;
  307. /* Release Lock */
  308. __HAL_UNLOCK(hqspi);
  309. return HAL_OK;
  310. }
  311. /**
  312. * @brief Initialize the QSPI MSP.
  313. * @param hqspi: QSPI handle
  314. * @retval None
  315. */
  316. __weak void HAL_QSPI_MspInit(QSPI_HandleTypeDef *hqspi)
  317. {
  318. /* Prevent unused argument(s) compilation warning */
  319. UNUSED(hqspi);
  320. /* NOTE : This function should not be modified, when the callback is needed,
  321. the HAL_QSPI_MspInit can be implemented in the user file
  322. */
  323. }
  324. /**
  325. * @brief DeInitialize the QSPI MSP.
  326. * @param hqspi: QSPI handle
  327. * @retval None
  328. */
  329. __weak void HAL_QSPI_MspDeInit(QSPI_HandleTypeDef *hqspi)
  330. {
  331. /* Prevent unused argument(s) compilation warning */
  332. UNUSED(hqspi);
  333. /* NOTE : This function should not be modified, when the callback is needed,
  334. the HAL_QSPI_MspDeInit can be implemented in the user file
  335. */
  336. }
  337. /**
  338. * @}
  339. */
  340. /** @defgroup QSPI_Exported_Functions_Group2 Input and Output operation functions
  341. * @brief QSPI Transmit/Receive functions
  342. *
  343. @verbatim
  344. ===============================================================================
  345. ##### IO operation functions #####
  346. ===============================================================================
  347. [..]
  348. This subsection provides a set of functions allowing to :
  349. (+) Handle the interrupts.
  350. (+) Handle the command sequence.
  351. (+) Transmit data in blocking, interrupt or DMA mode.
  352. (+) Receive data in blocking, interrupt or DMA mode.
  353. (+) Manage the auto-polling functional mode.
  354. (+) Manage the memory-mapped functional mode.
  355. @endverbatim
  356. * @{
  357. */
  358. /**
  359. * @brief Handle QSPI interrupt request.
  360. * @param hqspi: QSPI handle
  361. * @retval None
  362. */
  363. void HAL_QSPI_IRQHandler(QSPI_HandleTypeDef *hqspi)
  364. {
  365. __IO uint32_t *data_reg;
  366. uint32_t flag = READ_REG(hqspi->Instance->SR);
  367. uint32_t itsource = READ_REG(hqspi->Instance->CR);
  368. /* QSPI Fifo Threshold interrupt occurred ----------------------------------*/
  369. if((flag & QSPI_FLAG_FT) && (itsource & QSPI_IT_FT))
  370. {
  371. data_reg = &hqspi->Instance->DR;
  372. if(hqspi->State == HAL_QSPI_STATE_BUSY_INDIRECT_TX)
  373. {
  374. /* Transmission process */
  375. while(__HAL_QSPI_GET_FLAG(hqspi, QSPI_FLAG_FT) != 0)
  376. {
  377. if (hqspi->TxXferCount > 0)
  378. {
  379. /* Fill the FIFO until the threshold is reached */
  380. *(__IO uint8_t *)data_reg = *hqspi->pTxBuffPtr++;
  381. hqspi->TxXferCount--;
  382. }
  383. else
  384. {
  385. /* No more data available for the transfer */
  386. /* Disable the QSPI FIFO Threshold Interrupt */
  387. __HAL_QSPI_DISABLE_IT(hqspi, QSPI_IT_FT);
  388. break;
  389. }
  390. }
  391. }
  392. else if(hqspi->State == HAL_QSPI_STATE_BUSY_INDIRECT_RX)
  393. {
  394. /* Receiving Process */
  395. while(__HAL_QSPI_GET_FLAG(hqspi, QSPI_FLAG_FT) != 0)
  396. {
  397. if (hqspi->RxXferCount > 0)
  398. {
  399. /* Read the FIFO until the threshold is reached */
  400. *hqspi->pRxBuffPtr++ = *(__IO uint8_t *)data_reg;
  401. hqspi->RxXferCount--;
  402. }
  403. else
  404. {
  405. /* All data have been received for the transfer */
  406. /* Disable the QSPI FIFO Threshold Interrupt */
  407. __HAL_QSPI_DISABLE_IT(hqspi, QSPI_IT_FT);
  408. break;
  409. }
  410. }
  411. }
  412. /* FIFO Threshold callback */
  413. HAL_QSPI_FifoThresholdCallback(hqspi);
  414. }
  415. /* QSPI Transfer Complete interrupt occurred -------------------------------*/
  416. else if((flag & QSPI_FLAG_TC) && (itsource & QSPI_IT_TC))
  417. {
  418. /* Clear interrupt */
  419. WRITE_REG(hqspi->Instance->FCR, QSPI_FLAG_TC);
  420. /* Disable the QSPI FIFO Threshold, Transfer Error and Transfer complete Interrupts */
  421. __HAL_QSPI_DISABLE_IT(hqspi, QSPI_IT_TC | QSPI_IT_TE | QSPI_IT_FT);
  422. /* Transfer complete callback */
  423. if(hqspi->State == HAL_QSPI_STATE_BUSY_INDIRECT_TX)
  424. {
  425. if (hqspi->Instance->CR & QUADSPI_CR_DMAEN)
  426. {
  427. /* Disable the DMA transfer by clearing the DMAEN bit in the QSPI CR register */
  428. CLEAR_BIT(hqspi->Instance->CR, QUADSPI_CR_DMAEN);
  429. /* Disable the MDMA channel */
  430. __HAL_MDMA_DISABLE(hqspi->hmdma);
  431. }
  432. /* Change state of QSPI */
  433. hqspi->State = HAL_QSPI_STATE_READY;
  434. /* TX Complete callback */
  435. HAL_QSPI_TxCpltCallback(hqspi);
  436. }
  437. else if(hqspi->State == HAL_QSPI_STATE_BUSY_INDIRECT_RX)
  438. {
  439. if (hqspi->Instance->CR & QUADSPI_CR_DMAEN)
  440. {
  441. /* Disable the DMA transfer by clearing the DMAEN bit in the QSPI CR register */
  442. CLEAR_BIT(hqspi->Instance->CR, QUADSPI_CR_DMAEN);
  443. /* Disable the MDMA channel */
  444. __HAL_MDMA_DISABLE(hqspi->hmdma);
  445. }
  446. else
  447. {
  448. data_reg = &hqspi->Instance->DR;
  449. while(READ_BIT(hqspi->Instance->SR, QUADSPI_SR_FLEVEL) != 0)
  450. {
  451. if (hqspi->RxXferCount > 0)
  452. {
  453. /* Read the last data received in the FIFO until it is empty */
  454. *hqspi->pRxBuffPtr++ = *(__IO uint8_t *)data_reg;
  455. hqspi->RxXferCount--;
  456. }
  457. else
  458. {
  459. /* All data have been received for the transfer */
  460. break;
  461. }
  462. }
  463. }
  464. /* Change state of QSPI */
  465. hqspi->State = HAL_QSPI_STATE_READY;
  466. /* RX Complete callback */
  467. HAL_QSPI_RxCpltCallback(hqspi);
  468. }
  469. else if(hqspi->State == HAL_QSPI_STATE_BUSY)
  470. {
  471. /* Change state of QSPI */
  472. hqspi->State = HAL_QSPI_STATE_READY;
  473. /* Command Complete callback */
  474. HAL_QSPI_CmdCpltCallback(hqspi);
  475. }
  476. else if(hqspi->State == HAL_QSPI_STATE_ABORT)
  477. {
  478. /* Change state of QSPI */
  479. hqspi->State = HAL_QSPI_STATE_READY;
  480. if (hqspi->ErrorCode == HAL_QSPI_ERROR_NONE)
  481. {
  482. /* Abort called by the user */
  483. /* Abort Complete callback */
  484. HAL_QSPI_AbortCpltCallback(hqspi);
  485. }
  486. else
  487. {
  488. /* Abort due to an error (eg : MDMA error) */
  489. /* Error callback */
  490. HAL_QSPI_ErrorCallback(hqspi);
  491. }
  492. }
  493. }
  494. /* QSPI Status Match interrupt occurred ------------------------------------*/
  495. else if((flag & QSPI_FLAG_SM) && (itsource & QSPI_IT_SM))
  496. {
  497. /* Clear interrupt */
  498. WRITE_REG(hqspi->Instance->FCR, QSPI_FLAG_SM);
  499. /* Check if the automatic poll mode stop is activated */
  500. if(READ_BIT(hqspi->Instance->CR, QUADSPI_CR_APMS) != 0)
  501. {
  502. /* Disable the QSPI Transfer Error and Status Match Interrupts */
  503. __HAL_QSPI_DISABLE_IT(hqspi, (QSPI_IT_SM | QSPI_IT_TE));
  504. /* Change state of QSPI */
  505. hqspi->State = HAL_QSPI_STATE_READY;
  506. }
  507. /* Status match callback */
  508. HAL_QSPI_StatusMatchCallback(hqspi);
  509. }
  510. /* QSPI Transfer Error interrupt occurred ----------------------------------*/
  511. else if((flag & QSPI_FLAG_TE) && (itsource & QSPI_IT_TE))
  512. {
  513. /* Clear interrupt */
  514. WRITE_REG(hqspi->Instance->FCR, QSPI_FLAG_TE);
  515. /* Disable all the QSPI Interrupts */
  516. __HAL_QSPI_DISABLE_IT(hqspi, QSPI_IT_SM | QSPI_IT_TC | QSPI_IT_TE | QSPI_IT_FT);
  517. /* Set error code */
  518. hqspi->ErrorCode |= HAL_QSPI_ERROR_TRANSFER;
  519. if (hqspi->Instance->CR & QUADSPI_CR_DMAEN)
  520. {
  521. /* Disable the DMA transfer by clearing the DMAEN bit in the QSPI CR register */
  522. CLEAR_BIT(hqspi->Instance->CR, QUADSPI_CR_DMAEN);
  523. /* Disable the MDMA channel */
  524. hqspi->hmdma->XferAbortCallback = QSPI_DMAAbortCplt;
  525. HAL_MDMA_Abort_IT(hqspi->hmdma);
  526. }
  527. else
  528. {
  529. /* Change state of QSPI */
  530. hqspi->State = HAL_QSPI_STATE_READY;
  531. /* Error callback */
  532. HAL_QSPI_ErrorCallback(hqspi);
  533. }
  534. }
  535. /* QSPI Timeout interrupt occurred -----------------------------------------*/
  536. else if((flag & QSPI_FLAG_TO) && (itsource & QSPI_IT_TO))
  537. {
  538. /* Clear interrupt */
  539. WRITE_REG(hqspi->Instance->FCR, QSPI_FLAG_TO);
  540. /* Timeout callback */
  541. HAL_QSPI_TimeOutCallback(hqspi);
  542. }
  543. }
  544. /**
  545. * @brief Set the command configuration.
  546. * @param hqspi: QSPI handle
  547. * @param cmd : structure that contains the command configuration information
  548. * @param Timeout : Timeout duration
  549. * @note This function is used only in Indirect Read or Write Modes
  550. * @retval HAL status
  551. */
  552. HAL_StatusTypeDef HAL_QSPI_Command(QSPI_HandleTypeDef *hqspi, QSPI_CommandTypeDef *cmd, uint32_t Timeout)
  553. {
  554. HAL_StatusTypeDef status = HAL_ERROR;
  555. uint32_t tickstart = HAL_GetTick();
  556. /* Check the parameters */
  557. assert_param(IS_QSPI_INSTRUCTION_MODE(cmd->InstructionMode));
  558. if (cmd->InstructionMode != QSPI_INSTRUCTION_NONE)
  559. {
  560. assert_param(IS_QSPI_INSTRUCTION(cmd->Instruction));
  561. }
  562. assert_param(IS_QSPI_ADDRESS_MODE(cmd->AddressMode));
  563. if (cmd->AddressMode != QSPI_ADDRESS_NONE)
  564. {
  565. assert_param(IS_QSPI_ADDRESS_SIZE(cmd->AddressSize));
  566. }
  567. assert_param(IS_QSPI_ALTERNATE_BYTES_MODE(cmd->AlternateByteMode));
  568. if (cmd->AlternateByteMode != QSPI_ALTERNATE_BYTES_NONE)
  569. {
  570. assert_param(IS_QSPI_ALTERNATE_BYTES_SIZE(cmd->AlternateBytesSize));
  571. }
  572. assert_param(IS_QSPI_DUMMY_CYCLES(cmd->DummyCycles));
  573. assert_param(IS_QSPI_DATA_MODE(cmd->DataMode));
  574. assert_param(IS_QSPI_DDR_MODE(cmd->DdrMode));
  575. assert_param(IS_QSPI_DDR_HHC(cmd->DdrHoldHalfCycle));
  576. assert_param(IS_QSPI_SIOO_MODE(cmd->SIOOMode));
  577. /* Process locked */
  578. __HAL_LOCK(hqspi);
  579. if(hqspi->State == HAL_QSPI_STATE_READY)
  580. {
  581. hqspi->ErrorCode = HAL_QSPI_ERROR_NONE;
  582. /* Update QSPI state */
  583. hqspi->State = HAL_QSPI_STATE_BUSY;
  584. /* Wait till BUSY flag reset */
  585. status = QSPI_WaitFlagStateUntilTimeout(hqspi, QSPI_FLAG_BUSY, RESET, tickstart, Timeout);
  586. if (status == HAL_OK)
  587. {
  588. /* Call the configuration function */
  589. QSPI_Config(hqspi, cmd, QSPI_FUNCTIONAL_MODE_INDIRECT_WRITE);
  590. if (cmd->DataMode == QSPI_DATA_NONE)
  591. {
  592. /* When there is no data phase, the transfer start as soon as the configuration is done
  593. so wait until TC flag is set to go back in idle state */
  594. status = QSPI_WaitFlagStateUntilTimeout(hqspi, QSPI_FLAG_TC, SET, tickstart, Timeout);
  595. if (status == HAL_OK)
  596. {
  597. __HAL_QSPI_CLEAR_FLAG(hqspi, QSPI_FLAG_TC);
  598. /* Update QSPI state */
  599. hqspi->State = HAL_QSPI_STATE_READY;
  600. }
  601. }
  602. else
  603. {
  604. /* Update QSPI state */
  605. hqspi->State = HAL_QSPI_STATE_READY;
  606. }
  607. }
  608. }
  609. else
  610. {
  611. status = HAL_BUSY;
  612. }
  613. /* Process unlocked */
  614. __HAL_UNLOCK(hqspi);
  615. /* Return function status */
  616. return status;
  617. }
  618. /**
  619. * @brief Set the command configuration in interrupt mode.
  620. * @param hqspi: QSPI handle
  621. * @param cmd : structure that contains the command configuration information
  622. * @note This function is used only in Indirect Read or Write Modes
  623. * @retval HAL status
  624. */
  625. HAL_StatusTypeDef HAL_QSPI_Command_IT(QSPI_HandleTypeDef *hqspi, QSPI_CommandTypeDef *cmd)
  626. {
  627. HAL_StatusTypeDef status = HAL_ERROR;
  628. uint32_t tickstart = HAL_GetTick();
  629. /* Check the parameters */
  630. assert_param(IS_QSPI_INSTRUCTION_MODE(cmd->InstructionMode));
  631. if (cmd->InstructionMode != QSPI_INSTRUCTION_NONE)
  632. {
  633. assert_param(IS_QSPI_INSTRUCTION(cmd->Instruction));
  634. }
  635. assert_param(IS_QSPI_ADDRESS_MODE(cmd->AddressMode));
  636. if (cmd->AddressMode != QSPI_ADDRESS_NONE)
  637. {
  638. assert_param(IS_QSPI_ADDRESS_SIZE(cmd->AddressSize));
  639. }
  640. assert_param(IS_QSPI_ALTERNATE_BYTES_MODE(cmd->AlternateByteMode));
  641. if (cmd->AlternateByteMode != QSPI_ALTERNATE_BYTES_NONE)
  642. {
  643. assert_param(IS_QSPI_ALTERNATE_BYTES_SIZE(cmd->AlternateBytesSize));
  644. }
  645. assert_param(IS_QSPI_DUMMY_CYCLES(cmd->DummyCycles));
  646. assert_param(IS_QSPI_DATA_MODE(cmd->DataMode));
  647. assert_param(IS_QSPI_DDR_MODE(cmd->DdrMode));
  648. assert_param(IS_QSPI_DDR_HHC(cmd->DdrHoldHalfCycle));
  649. assert_param(IS_QSPI_SIOO_MODE(cmd->SIOOMode));
  650. /* Process locked */
  651. __HAL_LOCK(hqspi);
  652. if(hqspi->State == HAL_QSPI_STATE_READY)
  653. {
  654. hqspi->ErrorCode = HAL_QSPI_ERROR_NONE;
  655. /* Update QSPI state */
  656. hqspi->State = HAL_QSPI_STATE_BUSY;
  657. /* Wait till BUSY flag reset */
  658. status = QSPI_WaitFlagStateUntilTimeout(hqspi, QSPI_FLAG_BUSY, RESET, tickstart, hqspi->Timeout);
  659. if (status == HAL_OK)
  660. {
  661. if (cmd->DataMode == QSPI_DATA_NONE)
  662. {
  663. /* Clear interrupt */
  664. __HAL_QSPI_CLEAR_FLAG(hqspi, QSPI_FLAG_TE | QSPI_FLAG_TC);
  665. }
  666. /* Call the configuration function */
  667. QSPI_Config(hqspi, cmd, QSPI_FUNCTIONAL_MODE_INDIRECT_WRITE);
  668. if (cmd->DataMode == QSPI_DATA_NONE)
  669. {
  670. /* When there is no data phase, the transfer start as soon as the configuration is done
  671. so activate TC and TE interrupts */
  672. /* Process unlocked */
  673. __HAL_UNLOCK(hqspi);
  674. /* Enable the QSPI Transfer Error Interrupt */
  675. __HAL_QSPI_ENABLE_IT(hqspi, QSPI_IT_TE | QSPI_IT_TC);
  676. }
  677. else
  678. {
  679. /* Update QSPI state */
  680. hqspi->State = HAL_QSPI_STATE_READY;
  681. /* Process unlocked */
  682. __HAL_UNLOCK(hqspi);
  683. }
  684. }
  685. else
  686. {
  687. /* Process unlocked */
  688. __HAL_UNLOCK(hqspi);
  689. }
  690. }
  691. else
  692. {
  693. status = HAL_BUSY;
  694. /* Process unlocked */
  695. __HAL_UNLOCK(hqspi);
  696. }
  697. /* Return function status */
  698. return status;
  699. }
  700. /**
  701. * @brief Transmit an amount of data in blocking mode.
  702. * @param hqspi: QSPI handle
  703. * @param pData: pointer to data buffer
  704. * @param Timeout : Timeout duration
  705. * @note This function is used only in Indirect Write Mode
  706. * @retval HAL status
  707. */
  708. HAL_StatusTypeDef HAL_QSPI_Transmit(QSPI_HandleTypeDef *hqspi, uint8_t *pData, uint32_t Timeout)
  709. {
  710. HAL_StatusTypeDef status = HAL_OK;
  711. uint32_t tickstart = HAL_GetTick();
  712. __IO uint32_t *data_reg = &hqspi->Instance->DR;
  713. /* Process locked */
  714. __HAL_LOCK(hqspi);
  715. if(hqspi->State == HAL_QSPI_STATE_READY)
  716. {
  717. hqspi->ErrorCode = HAL_QSPI_ERROR_NONE;
  718. if(pData != NULL )
  719. {
  720. /* Update state */
  721. hqspi->State = HAL_QSPI_STATE_BUSY_INDIRECT_TX;
  722. /* Configure counters and size of the handle */
  723. hqspi->TxXferCount = READ_REG(hqspi->Instance->DLR) + 1;
  724. hqspi->TxXferSize = READ_REG(hqspi->Instance->DLR) + 1;
  725. hqspi->pTxBuffPtr = pData;
  726. /* Configure QSPI: CCR register with functional as indirect write */
  727. MODIFY_REG(hqspi->Instance->CCR, QUADSPI_CCR_FMODE, QSPI_FUNCTIONAL_MODE_INDIRECT_WRITE);
  728. while(hqspi->TxXferCount > 0)
  729. {
  730. /* Wait until FT flag is set to send data */
  731. status = QSPI_WaitFlagStateUntilTimeout(hqspi, QSPI_FLAG_FT, SET, tickstart, Timeout);
  732. if (status != HAL_OK)
  733. {
  734. break;
  735. }
  736. *(__IO uint8_t *)data_reg = *hqspi->pTxBuffPtr++;
  737. hqspi->TxXferCount--;
  738. }
  739. if (status == HAL_OK)
  740. {
  741. /* Wait until TC flag is set to go back in idle state */
  742. status = QSPI_WaitFlagStateUntilTimeout(hqspi, QSPI_FLAG_TC, SET, tickstart, Timeout);
  743. if (status == HAL_OK)
  744. {
  745. /* Clear Transfer Complete bit */
  746. __HAL_QSPI_CLEAR_FLAG(hqspi, QSPI_FLAG_TC);
  747. }
  748. }
  749. /* Update QSPI state */
  750. hqspi->State = HAL_QSPI_STATE_READY;
  751. }
  752. else
  753. {
  754. hqspi->ErrorCode |= HAL_QSPI_ERROR_INVALID_PARAM;
  755. status = HAL_ERROR;
  756. }
  757. }
  758. else
  759. {
  760. status = HAL_BUSY;
  761. }
  762. /* Process unlocked */
  763. __HAL_UNLOCK(hqspi);
  764. return status;
  765. }
  766. /**
  767. * @brief Receive an amount of data in blocking mode.
  768. * @param hqspi: QSPI handle
  769. * @param pData: pointer to data buffer
  770. * @param Timeout : Timeout duration
  771. * @note This function is used only in Indirect Read Mode
  772. * @retval HAL status
  773. */
  774. HAL_StatusTypeDef HAL_QSPI_Receive(QSPI_HandleTypeDef *hqspi, uint8_t *pData, uint32_t Timeout)
  775. {
  776. HAL_StatusTypeDef status = HAL_OK;
  777. uint32_t tickstart = HAL_GetTick();
  778. uint32_t addr_reg = READ_REG(hqspi->Instance->AR);
  779. __IO uint32_t *data_reg = &hqspi->Instance->DR;
  780. /* Process locked */
  781. __HAL_LOCK(hqspi);
  782. if(hqspi->State == HAL_QSPI_STATE_READY)
  783. {
  784. hqspi->ErrorCode = HAL_QSPI_ERROR_NONE;
  785. if(pData != NULL )
  786. {
  787. /* Update state */
  788. hqspi->State = HAL_QSPI_STATE_BUSY_INDIRECT_RX;
  789. /* Configure counters and size of the handle */
  790. hqspi->RxXferCount = READ_REG(hqspi->Instance->DLR) + 1;
  791. hqspi->RxXferSize = READ_REG(hqspi->Instance->DLR) + 1;
  792. hqspi->pRxBuffPtr = pData;
  793. /* Configure QSPI: CCR register with functional as indirect read */
  794. MODIFY_REG(hqspi->Instance->CCR, QUADSPI_CCR_FMODE, QSPI_FUNCTIONAL_MODE_INDIRECT_READ);
  795. /* Start the transfer by re-writing the address in AR register */
  796. WRITE_REG(hqspi->Instance->AR, addr_reg);
  797. while(hqspi->RxXferCount > 0)
  798. {
  799. /* Wait until FT or TC flag is set to read received data */
  800. status = QSPI_WaitFlagStateUntilTimeout(hqspi, (QSPI_FLAG_FT | QSPI_FLAG_TC), SET, tickstart, Timeout);
  801. if (status != HAL_OK)
  802. {
  803. break;
  804. }
  805. *hqspi->pRxBuffPtr++ = *(__IO uint8_t *)data_reg;
  806. hqspi->RxXferCount--;
  807. }
  808. if (status == HAL_OK)
  809. {
  810. /* Wait until TC flag is set to go back in idle state */
  811. status = QSPI_WaitFlagStateUntilTimeout(hqspi, QSPI_FLAG_TC, SET, tickstart, Timeout);
  812. if (status == HAL_OK)
  813. {
  814. /* Clear Transfer Complete bit */
  815. __HAL_QSPI_CLEAR_FLAG(hqspi, QSPI_FLAG_TC);
  816. }
  817. }
  818. /* Update QSPI state */
  819. hqspi->State = HAL_QSPI_STATE_READY;
  820. }
  821. else
  822. {
  823. hqspi->ErrorCode |= HAL_QSPI_ERROR_INVALID_PARAM;
  824. status = HAL_ERROR;
  825. }
  826. }
  827. else
  828. {
  829. status = HAL_BUSY;
  830. }
  831. /* Process unlocked */
  832. __HAL_UNLOCK(hqspi);
  833. return status;
  834. }
  835. /**
  836. * @brief Send an amount of data in non-blocking mode with interrupt.
  837. * @param hqspi: QSPI handle
  838. * @param pData: pointer to data buffer
  839. * @note This function is used only in Indirect Write Mode
  840. * @retval HAL status
  841. */
  842. HAL_StatusTypeDef HAL_QSPI_Transmit_IT(QSPI_HandleTypeDef *hqspi, uint8_t *pData)
  843. {
  844. HAL_StatusTypeDef status = HAL_OK;
  845. /* Process locked */
  846. __HAL_LOCK(hqspi);
  847. if(hqspi->State == HAL_QSPI_STATE_READY)
  848. {
  849. hqspi->ErrorCode = HAL_QSPI_ERROR_NONE;
  850. if(pData != NULL )
  851. {
  852. /* Update state */
  853. hqspi->State = HAL_QSPI_STATE_BUSY_INDIRECT_TX;
  854. /* Configure counters and size of the handle */
  855. hqspi->TxXferCount = READ_REG(hqspi->Instance->DLR) + 1;
  856. hqspi->TxXferSize = READ_REG(hqspi->Instance->DLR) + 1;
  857. hqspi->pTxBuffPtr = pData;
  858. /* Configure QSPI: CCR register with functional as indirect write */
  859. MODIFY_REG(hqspi->Instance->CCR, QUADSPI_CCR_FMODE, QSPI_FUNCTIONAL_MODE_INDIRECT_WRITE);
  860. /* Clear interrupt */
  861. __HAL_QSPI_CLEAR_FLAG(hqspi, QSPI_FLAG_TE | QSPI_FLAG_TC);
  862. /* Process unlocked */
  863. __HAL_UNLOCK(hqspi);
  864. /* Enable the QSPI transfer error, FIFO threshold and transfer complete Interrupts */
  865. __HAL_QSPI_ENABLE_IT(hqspi, QSPI_IT_TE | QSPI_IT_FT | QSPI_IT_TC);
  866. }
  867. else
  868. {
  869. hqspi->ErrorCode |= HAL_QSPI_ERROR_INVALID_PARAM;
  870. status = HAL_ERROR;
  871. /* Process unlocked */
  872. __HAL_UNLOCK(hqspi);
  873. }
  874. }
  875. else
  876. {
  877. status = HAL_BUSY;
  878. /* Process unlocked */
  879. __HAL_UNLOCK(hqspi);
  880. }
  881. return status;
  882. }
  883. /**
  884. * @brief Receive an amount of data in non-blocking mode with interrupt.
  885. * @param hqspi: QSPI handle
  886. * @param pData: pointer to data buffer
  887. * @note This function is used only in Indirect Read Mode
  888. * @retval HAL status
  889. */
  890. HAL_StatusTypeDef HAL_QSPI_Receive_IT(QSPI_HandleTypeDef *hqspi, uint8_t *pData)
  891. {
  892. HAL_StatusTypeDef status = HAL_OK;
  893. uint32_t addr_reg = READ_REG(hqspi->Instance->AR);
  894. /* Process locked */
  895. __HAL_LOCK(hqspi);
  896. if(hqspi->State == HAL_QSPI_STATE_READY)
  897. {
  898. hqspi->ErrorCode = HAL_QSPI_ERROR_NONE;
  899. if(pData != NULL )
  900. {
  901. /* Update state */
  902. hqspi->State = HAL_QSPI_STATE_BUSY_INDIRECT_RX;
  903. /* Configure counters and size of the handle */
  904. hqspi->RxXferCount = READ_REG(hqspi->Instance->DLR) + 1;
  905. hqspi->RxXferSize = READ_REG(hqspi->Instance->DLR) + 1;
  906. hqspi->pRxBuffPtr = pData;
  907. /* Configure QSPI: CCR register with functional as indirect read */
  908. MODIFY_REG(hqspi->Instance->CCR, QUADSPI_CCR_FMODE, QSPI_FUNCTIONAL_MODE_INDIRECT_READ);
  909. /* Start the transfer by re-writing the address in AR register */
  910. WRITE_REG(hqspi->Instance->AR, addr_reg);
  911. /* Clear interrupt */
  912. __HAL_QSPI_CLEAR_FLAG(hqspi, QSPI_FLAG_TE | QSPI_FLAG_TC);
  913. /* Process unlocked */
  914. __HAL_UNLOCK(hqspi);
  915. /* Enable the QSPI transfer error, FIFO threshold and transfer complete Interrupts */
  916. __HAL_QSPI_ENABLE_IT(hqspi, QSPI_IT_TE | QSPI_IT_FT | QSPI_IT_TC);
  917. }
  918. else
  919. {
  920. hqspi->ErrorCode |= HAL_QSPI_ERROR_INVALID_PARAM;
  921. status = HAL_ERROR;
  922. /* Process unlocked */
  923. __HAL_UNLOCK(hqspi);
  924. }
  925. }
  926. else
  927. {
  928. status = HAL_BUSY;
  929. /* Process unlocked */
  930. __HAL_UNLOCK(hqspi);
  931. }
  932. return status;
  933. }
  934. /**
  935. * @brief Send an amount of data in non-blocking mode with DMA.
  936. * @param hqspi: QSPI handle
  937. * @param pData: pointer to data buffer
  938. * @note This function is used only in Indirect Write Mode
  939. * @note If MDMA peripheral access is configured as halfword, the number
  940. * of data and the fifo threshold should be aligned on halfword
  941. * @note If MDMA peripheral access is configured as word, the number
  942. * of data and the fifo threshold should be aligned on word
  943. * @retval HAL status
  944. */
  945. HAL_StatusTypeDef HAL_QSPI_Transmit_DMA(QSPI_HandleTypeDef *hqspi, uint8_t *pData)
  946. {
  947. HAL_StatusTypeDef status = HAL_OK;
  948. uint32_t *tmp;
  949. /* Process locked */
  950. __HAL_LOCK(hqspi);
  951. if(hqspi->State == HAL_QSPI_STATE_READY)
  952. {
  953. /* Clear the error code */
  954. hqspi->ErrorCode = HAL_QSPI_ERROR_NONE;
  955. if(pData != NULL )
  956. {
  957. /* Update state */
  958. hqspi->State = HAL_QSPI_STATE_BUSY_INDIRECT_TX;
  959. /* Clear interrupt */
  960. __HAL_QSPI_CLEAR_FLAG(hqspi, (QSPI_FLAG_TE | QSPI_FLAG_TC));
  961. /* Configure counters and size of the handle */
  962. hqspi->TxXferCount = READ_REG(hqspi->Instance->DLR) + 1;
  963. hqspi->TxXferSize = READ_REG(hqspi->Instance->DLR) + 1;
  964. hqspi->pTxBuffPtr = pData;
  965. /* Configure QSPI: CCR register with functional mode as indirect write */
  966. MODIFY_REG(hqspi->Instance->CCR, QUADSPI_CCR_FMODE, QSPI_FUNCTIONAL_MODE_INDIRECT_WRITE);
  967. /* Set the QSPI MDMA transfer complete callback */
  968. hqspi->hmdma->XferCpltCallback = QSPI_DMATxCplt;
  969. /* Set the MDMA error callback */
  970. hqspi->hmdma->XferErrorCallback = QSPI_DMAError;
  971. /* Clear the MDMA abort callback */
  972. hqspi->hmdma->XferAbortCallback = NULL;
  973. if(hqspi->hmdma->Init.DestinationInc != MDMA_DEST_INC_DISABLE)
  974. {
  975. /* Update MDMA handle with the correct DestinationInc and SourceInc field for Write operation */
  976. hqspi->hmdma->Init.DestinationInc = MDMA_DEST_INC_DISABLE;
  977. hqspi->hmdma->Init.SourceInc = MDMA_SRC_INC_BYTE;
  978. HAL_MDMA_Init(hqspi->hmdma);
  979. }
  980. /* Enable the QSPI transmit MDMA */
  981. tmp = (uint32_t*)&pData;
  982. HAL_MDMA_Start_IT(hqspi->hmdma, *(uint32_t*)tmp, (uint32_t)&hqspi->Instance->DR, hqspi->TxXferSize, 1);
  983. /* Process unlocked */
  984. __HAL_UNLOCK(hqspi);
  985. /* Enable the QSPI transfer error Interrupt */
  986. __HAL_QSPI_ENABLE_IT(hqspi, QSPI_IT_TE);
  987. /* Enable the MDMA transfer by setting the DMAEN bit not needed for MDMA*/
  988. }
  989. else
  990. {
  991. hqspi->ErrorCode |= HAL_QSPI_ERROR_INVALID_PARAM;
  992. status = HAL_ERROR;
  993. /* Process unlocked */
  994. __HAL_UNLOCK(hqspi);
  995. }
  996. }
  997. else
  998. {
  999. status = HAL_BUSY;
  1000. /* Process unlocked */
  1001. __HAL_UNLOCK(hqspi);
  1002. }
  1003. return status;
  1004. }
  1005. /**
  1006. * @brief Receive an amount of data in non-blocking mode with DMA.
  1007. * @param hqspi: QSPI handle
  1008. * @param pData: pointer to data buffer.
  1009. * @note This function is used only in Indirect Read Mode
  1010. * @retval HAL status
  1011. */
  1012. HAL_StatusTypeDef HAL_QSPI_Receive_DMA(QSPI_HandleTypeDef *hqspi, uint8_t *pData)
  1013. {
  1014. HAL_StatusTypeDef status = HAL_OK;
  1015. uint32_t *tmp;
  1016. uint32_t addr_reg = READ_REG(hqspi->Instance->AR);
  1017. /* Process locked */
  1018. __HAL_LOCK(hqspi);
  1019. if(hqspi->State == HAL_QSPI_STATE_READY)
  1020. {
  1021. /* Clear the error code */
  1022. hqspi->ErrorCode = HAL_QSPI_ERROR_NONE;
  1023. if(pData != NULL )
  1024. {
  1025. /* Update state */
  1026. hqspi->State = HAL_QSPI_STATE_BUSY_INDIRECT_RX;
  1027. /* Clear interrupt */
  1028. __HAL_QSPI_CLEAR_FLAG(hqspi, (QSPI_FLAG_TE | QSPI_FLAG_TC));
  1029. /* Configure counters and size of the handle */
  1030. hqspi->RxXferCount = READ_REG(hqspi->Instance->DLR) + 1;
  1031. hqspi->RxXferSize = READ_REG(hqspi->Instance->DLR) + 1;
  1032. hqspi->pRxBuffPtr = pData;
  1033. /* Set the QSPI DMA transfer complete callback */
  1034. hqspi->hmdma->XferCpltCallback = QSPI_DMARxCplt;
  1035. /* Set the MDMA error callback */
  1036. hqspi->hmdma->XferErrorCallback = QSPI_DMAError;
  1037. /* Clear the MDMA abort callback */
  1038. hqspi->hmdma->XferAbortCallback = NULL;
  1039. /* QSPI need to be configured to indirect mode before starting
  1040. the MDMA to avoid primatury triggering for the MDMA transfert */
  1041. /* Configure QSPI: CCR register with functional as indirect read */
  1042. MODIFY_REG(hqspi->Instance->CCR, QUADSPI_CCR_FMODE, QSPI_FUNCTIONAL_MODE_INDIRECT_READ);
  1043. /* Start the transfer by re-writing the address in AR register */
  1044. WRITE_REG(hqspi->Instance->AR, addr_reg);
  1045. if(hqspi->hmdma->Init.DestinationInc != MDMA_DEST_INC_BYTE)
  1046. {
  1047. /* Update MDMA handle with the correct DestinationInc and SourceInc field for Read operation */
  1048. hqspi->hmdma->Init.DestinationInc = MDMA_DEST_INC_BYTE;
  1049. hqspi->hmdma->Init.SourceInc = MDMA_SRC_INC_DISABLE;
  1050. HAL_MDMA_Init(hqspi->hmdma);
  1051. }
  1052. /* Enable the MDMA */
  1053. tmp = (uint32_t*)&pData;
  1054. HAL_MDMA_Start_IT(hqspi->hmdma, (uint32_t)&hqspi->Instance->DR, *(uint32_t*)tmp, hqspi->RxXferSize, 1);
  1055. /* Process unlocked */
  1056. __HAL_UNLOCK(hqspi);
  1057. /* Enable the QSPI transfer error Interrupt */
  1058. __HAL_QSPI_ENABLE_IT(hqspi, QSPI_IT_TE);
  1059. /* Enable the MDMA transfer by setting the DMAEN bit in the QSPI CR register */
  1060. SET_BIT(hqspi->Instance->CR, QUADSPI_CR_DMAEN);
  1061. }
  1062. else
  1063. {
  1064. status = HAL_ERROR;
  1065. /* Process unlocked */
  1066. __HAL_UNLOCK(hqspi);
  1067. }
  1068. }
  1069. else
  1070. {
  1071. status = HAL_BUSY;
  1072. /* Process unlocked */
  1073. __HAL_UNLOCK(hqspi);
  1074. }
  1075. return status;
  1076. }
  1077. /**
  1078. * @brief Configure the QSPI Automatic Polling Mode in blocking mode.
  1079. * @param hqspi: QSPI handle
  1080. * @param cmd: structure that contains the command configuration information.
  1081. * @param cfg: structure that contains the polling configuration information.
  1082. * @param Timeout : Timeout duration
  1083. * @note This function is used only in Automatic Polling Mode
  1084. * @retval HAL status
  1085. */
  1086. HAL_StatusTypeDef HAL_QSPI_AutoPolling(QSPI_HandleTypeDef *hqspi, QSPI_CommandTypeDef *cmd, QSPI_AutoPollingTypeDef *cfg, uint32_t Timeout)
  1087. {
  1088. HAL_StatusTypeDef status = HAL_ERROR;
  1089. uint32_t tickstart = HAL_GetTick();
  1090. /* Check the parameters */
  1091. assert_param(IS_QSPI_INSTRUCTION_MODE(cmd->InstructionMode));
  1092. if (cmd->InstructionMode != QSPI_INSTRUCTION_NONE)
  1093. {
  1094. assert_param(IS_QSPI_INSTRUCTION(cmd->Instruction));
  1095. }
  1096. assert_param(IS_QSPI_ADDRESS_MODE(cmd->AddressMode));
  1097. if (cmd->AddressMode != QSPI_ADDRESS_NONE)
  1098. {
  1099. assert_param(IS_QSPI_ADDRESS_SIZE(cmd->AddressSize));
  1100. }
  1101. assert_param(IS_QSPI_ALTERNATE_BYTES_MODE(cmd->AlternateByteMode));
  1102. if (cmd->AlternateByteMode != QSPI_ALTERNATE_BYTES_NONE)
  1103. {
  1104. assert_param(IS_QSPI_ALTERNATE_BYTES_SIZE(cmd->AlternateBytesSize));
  1105. }
  1106. assert_param(IS_QSPI_DUMMY_CYCLES(cmd->DummyCycles));
  1107. assert_param(IS_QSPI_DATA_MODE(cmd->DataMode));
  1108. assert_param(IS_QSPI_DDR_MODE(cmd->DdrMode));
  1109. assert_param(IS_QSPI_DDR_HHC(cmd->DdrHoldHalfCycle));
  1110. assert_param(IS_QSPI_SIOO_MODE(cmd->SIOOMode));
  1111. assert_param(IS_QSPI_INTERVAL(cfg->Interval));
  1112. assert_param(IS_QSPI_STATUS_BYTES_SIZE(cfg->StatusBytesSize));
  1113. assert_param(IS_QSPI_MATCH_MODE(cfg->MatchMode));
  1114. /* Process locked */
  1115. __HAL_LOCK(hqspi);
  1116. if(hqspi->State == HAL_QSPI_STATE_READY)
  1117. {
  1118. hqspi->ErrorCode = HAL_QSPI_ERROR_NONE;
  1119. /* Update state */
  1120. hqspi->State = HAL_QSPI_STATE_BUSY_AUTO_POLLING;
  1121. /* Wait till BUSY flag reset */
  1122. status = QSPI_WaitFlagStateUntilTimeout(hqspi, QSPI_FLAG_BUSY, RESET, tickstart, Timeout);
  1123. if (status == HAL_OK)
  1124. {
  1125. /* Configure QSPI: PSMAR register with the status match value */
  1126. WRITE_REG(hqspi->Instance->PSMAR, cfg->Match);
  1127. /* Configure QSPI: PSMKR register with the status mask value */
  1128. WRITE_REG(hqspi->Instance->PSMKR, cfg->Mask);
  1129. /* Configure QSPI: PIR register with the interval value */
  1130. WRITE_REG(hqspi->Instance->PIR, cfg->Interval);
  1131. /* Configure QSPI: CR register with Match mode and Automatic stop enabled
  1132. (otherwise there will be an infinite loop in blocking mode) */
  1133. MODIFY_REG(hqspi->Instance->CR, (QUADSPI_CR_PMM | QUADSPI_CR_APMS),
  1134. (cfg->MatchMode | QSPI_AUTOMATIC_STOP_ENABLE));
  1135. /* Call the configuration function */
  1136. cmd->NbData = cfg->StatusBytesSize;
  1137. QSPI_Config(hqspi, cmd, QSPI_FUNCTIONAL_MODE_AUTO_POLLING);
  1138. /* Wait until SM flag is set to go back in idle state */
  1139. status = QSPI_WaitFlagStateUntilTimeout(hqspi, QSPI_FLAG_SM, SET, tickstart, Timeout);
  1140. if (status == HAL_OK)
  1141. {
  1142. __HAL_QSPI_CLEAR_FLAG(hqspi, QSPI_FLAG_SM);
  1143. /* Update state */
  1144. hqspi->State = HAL_QSPI_STATE_READY;
  1145. }
  1146. }
  1147. }
  1148. else
  1149. {
  1150. status = HAL_BUSY;
  1151. }
  1152. /* Process unlocked */
  1153. __HAL_UNLOCK(hqspi);
  1154. /* Return function status */
  1155. return status;
  1156. }
  1157. /**
  1158. * @brief Configure the QSPI Automatic Polling Mode in non-blocking mode.
  1159. * @param hqspi: QSPI handle
  1160. * @param cmd: structure that contains the command configuration information.
  1161. * @param cfg: structure that contains the polling configuration information.
  1162. * @note This function is used only in Automatic Polling Mode
  1163. * @retval HAL status
  1164. */
  1165. HAL_StatusTypeDef HAL_QSPI_AutoPolling_IT(QSPI_HandleTypeDef *hqspi, QSPI_CommandTypeDef *cmd, QSPI_AutoPollingTypeDef *cfg)
  1166. {
  1167. HAL_StatusTypeDef status = HAL_ERROR;
  1168. uint32_t tickstart = HAL_GetTick();
  1169. /* Check the parameters */
  1170. assert_param(IS_QSPI_INSTRUCTION_MODE(cmd->InstructionMode));
  1171. if (cmd->InstructionMode != QSPI_INSTRUCTION_NONE)
  1172. {
  1173. assert_param(IS_QSPI_INSTRUCTION(cmd->Instruction));
  1174. }
  1175. assert_param(IS_QSPI_ADDRESS_MODE(cmd->AddressMode));
  1176. if (cmd->AddressMode != QSPI_ADDRESS_NONE)
  1177. {
  1178. assert_param(IS_QSPI_ADDRESS_SIZE(cmd->AddressSize));
  1179. }
  1180. assert_param(IS_QSPI_ALTERNATE_BYTES_MODE(cmd->AlternateByteMode));
  1181. if (cmd->AlternateByteMode != QSPI_ALTERNATE_BYTES_NONE)
  1182. {
  1183. assert_param(IS_QSPI_ALTERNATE_BYTES_SIZE(cmd->AlternateBytesSize));
  1184. }
  1185. assert_param(IS_QSPI_DUMMY_CYCLES(cmd->DummyCycles));
  1186. assert_param(IS_QSPI_DATA_MODE(cmd->DataMode));
  1187. assert_param(IS_QSPI_DDR_MODE(cmd->DdrMode));
  1188. assert_param(IS_QSPI_DDR_HHC(cmd->DdrHoldHalfCycle));
  1189. assert_param(IS_QSPI_SIOO_MODE(cmd->SIOOMode));
  1190. assert_param(IS_QSPI_INTERVAL(cfg->Interval));
  1191. assert_param(IS_QSPI_STATUS_BYTES_SIZE(cfg->StatusBytesSize));
  1192. assert_param(IS_QSPI_MATCH_MODE(cfg->MatchMode));
  1193. assert_param(IS_QSPI_AUTOMATIC_STOP(cfg->AutomaticStop));
  1194. /* Process locked */
  1195. __HAL_LOCK(hqspi);
  1196. if(hqspi->State == HAL_QSPI_STATE_READY)
  1197. {
  1198. hqspi->ErrorCode = HAL_QSPI_ERROR_NONE;
  1199. /* Update state */
  1200. hqspi->State = HAL_QSPI_STATE_BUSY_AUTO_POLLING;
  1201. /* Wait till BUSY flag reset */
  1202. status = QSPI_WaitFlagStateUntilTimeout(hqspi, QSPI_FLAG_BUSY, RESET, tickstart, hqspi->Timeout);
  1203. if (status == HAL_OK)
  1204. {
  1205. /* Configure QSPI: PSMAR register with the status match value */
  1206. WRITE_REG(hqspi->Instance->PSMAR, cfg->Match);
  1207. /* Configure QSPI: PSMKR register with the status mask value */
  1208. WRITE_REG(hqspi->Instance->PSMKR, cfg->Mask);
  1209. /* Configure QSPI: PIR register with the interval value */
  1210. WRITE_REG(hqspi->Instance->PIR, cfg->Interval);
  1211. /* Configure QSPI: CR register with Match mode and Automatic stop mode */
  1212. MODIFY_REG(hqspi->Instance->CR, (QUADSPI_CR_PMM | QUADSPI_CR_APMS),
  1213. (cfg->MatchMode | cfg->AutomaticStop));
  1214. /* Clear interrupt */
  1215. __HAL_QSPI_CLEAR_FLAG(hqspi, QSPI_FLAG_TE | QSPI_FLAG_SM);
  1216. /* Call the configuration function */
  1217. cmd->NbData = cfg->StatusBytesSize;
  1218. QSPI_Config(hqspi, cmd, QSPI_FUNCTIONAL_MODE_AUTO_POLLING);
  1219. /* Process unlocked */
  1220. __HAL_UNLOCK(hqspi);
  1221. /* Enable the QSPI Transfer Error and status match Interrupt */
  1222. __HAL_QSPI_ENABLE_IT(hqspi, (QSPI_IT_SM | QSPI_IT_TE));
  1223. }
  1224. else
  1225. {
  1226. /* Process unlocked */
  1227. __HAL_UNLOCK(hqspi);
  1228. }
  1229. }
  1230. else
  1231. {
  1232. status = HAL_BUSY;
  1233. /* Process unlocked */
  1234. __HAL_UNLOCK(hqspi);
  1235. }
  1236. /* Return function status */
  1237. return status;
  1238. }
  1239. /**
  1240. * @brief Configure the Memory Mapped mode.
  1241. * @param hqspi: QSPI handle
  1242. * @param cmd: structure that contains the command configuration information.
  1243. * @param cfg: structure that contains the memory mapped configuration information.
  1244. * @note This function is used only in Memory mapped Mode
  1245. * @retval HAL status
  1246. */
  1247. HAL_StatusTypeDef HAL_QSPI_MemoryMapped(QSPI_HandleTypeDef *hqspi, QSPI_CommandTypeDef *cmd, QSPI_MemoryMappedTypeDef *cfg)
  1248. {
  1249. HAL_StatusTypeDef status = HAL_ERROR;
  1250. uint32_t tickstart = HAL_GetTick();
  1251. /* Check the parameters */
  1252. assert_param(IS_QSPI_INSTRUCTION_MODE(cmd->InstructionMode));
  1253. if (cmd->InstructionMode != QSPI_INSTRUCTION_NONE)
  1254. {
  1255. assert_param(IS_QSPI_INSTRUCTION(cmd->Instruction));
  1256. }
  1257. assert_param(IS_QSPI_ADDRESS_MODE(cmd->AddressMode));
  1258. if (cmd->AddressMode != QSPI_ADDRESS_NONE)
  1259. {
  1260. assert_param(IS_QSPI_ADDRESS_SIZE(cmd->AddressSize));
  1261. }
  1262. assert_param(IS_QSPI_ALTERNATE_BYTES_MODE(cmd->AlternateByteMode));
  1263. if (cmd->AlternateByteMode != QSPI_ALTERNATE_BYTES_NONE)
  1264. {
  1265. assert_param(IS_QSPI_ALTERNATE_BYTES_SIZE(cmd->AlternateBytesSize));
  1266. }
  1267. assert_param(IS_QSPI_DUMMY_CYCLES(cmd->DummyCycles));
  1268. assert_param(IS_QSPI_DATA_MODE(cmd->DataMode));
  1269. assert_param(IS_QSPI_DDR_MODE(cmd->DdrMode));
  1270. assert_param(IS_QSPI_DDR_HHC(cmd->DdrHoldHalfCycle));
  1271. assert_param(IS_QSPI_SIOO_MODE(cmd->SIOOMode));
  1272. assert_param(IS_QSPI_TIMEOUT_ACTIVATION(cfg->TimeOutActivation));
  1273. /* Process locked */
  1274. __HAL_LOCK(hqspi);
  1275. if(hqspi->State == HAL_QSPI_STATE_READY)
  1276. {
  1277. hqspi->ErrorCode = HAL_QSPI_ERROR_NONE;
  1278. /* Update state */
  1279. hqspi->State = HAL_QSPI_STATE_BUSY_MEM_MAPPED;
  1280. /* Wait till BUSY flag reset */
  1281. status = QSPI_WaitFlagStateUntilTimeout(hqspi, QSPI_FLAG_BUSY, RESET, tickstart, hqspi->Timeout);
  1282. if (status == HAL_OK)
  1283. {
  1284. /* Configure QSPI: CR register with timeout counter enable */
  1285. MODIFY_REG(hqspi->Instance->CR, QUADSPI_CR_TCEN, cfg->TimeOutActivation);
  1286. if (cfg->TimeOutActivation == QSPI_TIMEOUT_COUNTER_ENABLE)
  1287. {
  1288. assert_param(IS_QSPI_TIMEOUT_PERIOD(cfg->TimeOutPeriod));
  1289. /* Configure QSPI: LPTR register with the low-power timeout value */
  1290. WRITE_REG(hqspi->Instance->LPTR, cfg->TimeOutPeriod);
  1291. /* Clear interrupt */
  1292. __HAL_QSPI_CLEAR_FLAG(hqspi, QSPI_FLAG_TO);
  1293. /* Enable the QSPI TimeOut Interrupt */
  1294. __HAL_QSPI_ENABLE_IT(hqspi, QSPI_IT_TO);
  1295. }
  1296. /* Call the configuration function */
  1297. QSPI_Config(hqspi, cmd, QSPI_FUNCTIONAL_MODE_MEMORY_MAPPED);
  1298. }
  1299. }
  1300. else
  1301. {
  1302. status = HAL_BUSY;
  1303. }
  1304. /* Process unlocked */
  1305. __HAL_UNLOCK(hqspi);
  1306. /* Return function status */
  1307. return status;
  1308. }
  1309. /**
  1310. * @brief Transfer Error callback.
  1311. * @param hqspi: QSPI handle
  1312. * @retval None
  1313. */
  1314. __weak void HAL_QSPI_ErrorCallback(QSPI_HandleTypeDef *hqspi)
  1315. {
  1316. /* Prevent unused argument(s) compilation warning */
  1317. UNUSED(hqspi);
  1318. /* NOTE : This function should not be modified, when the callback is needed,
  1319. the HAL_QSPI_ErrorCallback could be implemented in the user file
  1320. */
  1321. }
  1322. /**
  1323. * @brief Abort completed callback.
  1324. * @param hqspi: QSPI handle
  1325. * @retval None
  1326. */
  1327. __weak void HAL_QSPI_AbortCpltCallback(QSPI_HandleTypeDef *hqspi)
  1328. {
  1329. /* Prevent unused argument(s) compilation warning */
  1330. UNUSED(hqspi);
  1331. /* NOTE: This function should not be modified, when the callback is needed,
  1332. the HAL_QSPI_AbortCpltCallback could be implemented in the user file
  1333. */
  1334. }
  1335. /**
  1336. * @brief Command completed callback.
  1337. * @param hqspi: QSPI handle
  1338. * @retval None
  1339. */
  1340. __weak void HAL_QSPI_CmdCpltCallback(QSPI_HandleTypeDef *hqspi)
  1341. {
  1342. /* Prevent unused argument(s) compilation warning */
  1343. UNUSED(hqspi);
  1344. /* NOTE: This function should not be modified, when the callback is needed,
  1345. the HAL_QSPI_CmdCpltCallback could be implemented in the user file
  1346. */
  1347. }
  1348. /**
  1349. * @brief Rx Transfer completed callback.
  1350. * @param hqspi: QSPI handle
  1351. * @retval None
  1352. */
  1353. __weak void HAL_QSPI_RxCpltCallback(QSPI_HandleTypeDef *hqspi)
  1354. {
  1355. /* Prevent unused argument(s) compilation warning */
  1356. UNUSED(hqspi);
  1357. /* NOTE: This function should not be modified, when the callback is needed,
  1358. the HAL_QSPI_RxCpltCallback could be implemented in the user file
  1359. */
  1360. }
  1361. /**
  1362. * @brief Tx Transfer completed callback.
  1363. * @param hqspi: QSPI handle
  1364. * @retval None
  1365. */
  1366. __weak void HAL_QSPI_TxCpltCallback(QSPI_HandleTypeDef *hqspi)
  1367. {
  1368. /* Prevent unused argument(s) compilation warning */
  1369. UNUSED(hqspi);
  1370. /* NOTE: This function should not be modified, when the callback is needed,
  1371. the HAL_QSPI_TxCpltCallback could be implemented in the user file
  1372. */
  1373. }
  1374. /**
  1375. * @brief Rx Half Transfer completed callback.
  1376. * @param hqspi: QSPI handle
  1377. * @retval None
  1378. */
  1379. __weak void HAL_QSPI_RxHalfCpltCallback(QSPI_HandleTypeDef *hqspi)
  1380. {
  1381. /* Prevent unused argument(s) compilation warning */
  1382. UNUSED(hqspi);
  1383. /* NOTE: This function should not be modified, when the callback is needed,
  1384. the HAL_QSPI_RxHalfCpltCallback could be implemented in the user file
  1385. */
  1386. }
  1387. /**
  1388. * @brief Tx Half Transfer completed callback.
  1389. * @param hqspi: QSPI handle
  1390. * @retval None
  1391. */
  1392. __weak void HAL_QSPI_TxHalfCpltCallback(QSPI_HandleTypeDef *hqspi)
  1393. {
  1394. /* Prevent unused argument(s) compilation warning */
  1395. UNUSED(hqspi);
  1396. /* NOTE: This function should not be modified, when the callback is needed,
  1397. the HAL_QSPI_TxHalfCpltCallback could be implemented in the user file
  1398. */
  1399. }
  1400. /**
  1401. * @brief FIFO Threshold callback.
  1402. * @param hqspi: QSPI handle
  1403. * @retval None
  1404. */
  1405. __weak void HAL_QSPI_FifoThresholdCallback(QSPI_HandleTypeDef *hqspi)
  1406. {
  1407. /* Prevent unused argument(s) compilation warning */
  1408. UNUSED(hqspi);
  1409. /* NOTE : This function should not be modified, when the callback is needed,
  1410. the HAL_QSPI_FIFOThresholdCallback could be implemented in the user file
  1411. */
  1412. }
  1413. /**
  1414. * @brief Status Match callback.
  1415. * @param hqspi: QSPI handle
  1416. * @retval None
  1417. */
  1418. __weak void HAL_QSPI_StatusMatchCallback(QSPI_HandleTypeDef *hqspi)
  1419. {
  1420. /* Prevent unused argument(s) compilation warning */
  1421. UNUSED(hqspi);
  1422. /* NOTE : This function should not be modified, when the callback is needed,
  1423. the HAL_QSPI_StatusMatchCallback could be implemented in the user file
  1424. */
  1425. }
  1426. /**
  1427. * @brief Timeout callback.
  1428. * @param hqspi: QSPI handle
  1429. * @retval None
  1430. */
  1431. __weak void HAL_QSPI_TimeOutCallback(QSPI_HandleTypeDef *hqspi)
  1432. {
  1433. /* Prevent unused argument(s) compilation warning */
  1434. UNUSED(hqspi);
  1435. /* NOTE : This function should not be modified, when the callback is needed,
  1436. the HAL_QSPI_TimeOutCallback could be implemented in the user file
  1437. */
  1438. }
  1439. /**
  1440. * @}
  1441. */
  1442. /** @defgroup QSPI_Exported_Functions_Group3 Peripheral Control and State functions
  1443. * @brief QSPI control and State functions
  1444. *
  1445. @verbatim
  1446. ===============================================================================
  1447. ##### Peripheral Control and State functions #####
  1448. ===============================================================================
  1449. [..]
  1450. This subsection provides a set of functions allowing to :
  1451. (+) Check in run-time the state of the driver.
  1452. (+) Check the error code set during last operation.
  1453. (+) Abort any operation.
  1454. @endverbatim
  1455. * @{
  1456. */
  1457. /**
  1458. * @brief Return the QSPI handle state.
  1459. * @param hqspi: QSPI handle
  1460. * @retval HAL state
  1461. */
  1462. HAL_QSPI_StateTypeDef HAL_QSPI_GetState(QSPI_HandleTypeDef *hqspi)
  1463. {
  1464. /* Return QSPI handle state */
  1465. return hqspi->State;
  1466. }
  1467. /**
  1468. * @brief Return the QSPI error code.
  1469. * @param hqspi: QSPI handle
  1470. * @retval QSPI Error Code
  1471. */
  1472. uint32_t HAL_QSPI_GetError(QSPI_HandleTypeDef *hqspi)
  1473. {
  1474. return hqspi->ErrorCode;
  1475. }
  1476. /**
  1477. * @brief Abort the current transmission.
  1478. * @param hqspi: QSPI handle
  1479. * @retval HAL status
  1480. */
  1481. HAL_StatusTypeDef HAL_QSPI_Abort(QSPI_HandleTypeDef *hqspi)
  1482. {
  1483. HAL_StatusTypeDef status = HAL_OK;
  1484. uint32_t tickstart = HAL_GetTick();
  1485. /* Check if the state is in one of the busy states */
  1486. if ((hqspi->State & 0x2) != 0)
  1487. {
  1488. /* Process unlocked */
  1489. __HAL_UNLOCK(hqspi);
  1490. if (hqspi->Instance->CR & QUADSPI_CR_DMAEN)
  1491. {
  1492. /* Disable the DMA transfer by clearing the DMAEN bit in the QSPI CR register */
  1493. CLEAR_BIT(hqspi->Instance->CR, QUADSPI_CR_DMAEN);
  1494. /* Abort MDMA */
  1495. status = HAL_MDMA_Abort(hqspi->hmdma);
  1496. if(status != HAL_OK)
  1497. {
  1498. hqspi->ErrorCode |= HAL_QSPI_ERROR_DMA;
  1499. }
  1500. }
  1501. /* Configure QSPI: CR register with Abort request */
  1502. SET_BIT(hqspi->Instance->CR, QUADSPI_CR_ABORT);
  1503. /* Wait until TC flag is set to go back in idle state */
  1504. status = QSPI_WaitFlagStateUntilTimeout(hqspi, QSPI_FLAG_TC, SET, tickstart, hqspi->Timeout);
  1505. if(status == HAL_OK)
  1506. {
  1507. __HAL_QSPI_CLEAR_FLAG(hqspi, QSPI_FLAG_TC);
  1508. /* Wait until BUSY flag is reset */
  1509. status = QSPI_WaitFlagStateUntilTimeout(hqspi, QSPI_FLAG_BUSY, RESET, tickstart, hqspi->Timeout);
  1510. }
  1511. if (status == HAL_OK)
  1512. {
  1513. /* Update state */
  1514. hqspi->State = HAL_QSPI_STATE_READY;
  1515. }
  1516. }
  1517. return status;
  1518. }
  1519. /**
  1520. * @brief Abort the current transmission (non-blocking function)
  1521. * @param hqspi: QSPI handle
  1522. * @retval HAL status
  1523. */
  1524. HAL_StatusTypeDef HAL_QSPI_Abort_IT(QSPI_HandleTypeDef *hqspi)
  1525. {
  1526. HAL_StatusTypeDef status = HAL_OK;
  1527. /* Check if the state is in one of the busy states */
  1528. if ((hqspi->State & 0x2) != 0)
  1529. {
  1530. /* Process unlocked */
  1531. __HAL_UNLOCK(hqspi);
  1532. /* Update QSPI state */
  1533. hqspi->State = HAL_QSPI_STATE_ABORT;
  1534. /* Disable all interrupts */
  1535. __HAL_QSPI_DISABLE_IT(hqspi, (QSPI_IT_TO | QSPI_IT_SM | QSPI_IT_FT | QSPI_IT_TC | QSPI_IT_TE));
  1536. if (hqspi->Instance->CR & QUADSPI_CR_DMAEN)
  1537. {
  1538. /* Disable the DMA transfer by clearing the DMAEN bit in the QSPI CR register */
  1539. CLEAR_BIT(hqspi->Instance->CR, QUADSPI_CR_DMAEN);
  1540. /* Abort MDMA channel */
  1541. hqspi->hmdma->XferAbortCallback = QSPI_DMAAbortCplt;
  1542. HAL_MDMA_Abort_IT(hqspi->hmdma);
  1543. }
  1544. else
  1545. {
  1546. /* Clear interrupt */
  1547. __HAL_QSPI_CLEAR_FLAG(hqspi, QSPI_FLAG_TC);
  1548. /* Enable the QSPI Transfer Complete Interrupt */
  1549. __HAL_QSPI_ENABLE_IT(hqspi, QSPI_IT_TC);
  1550. /* Configure QSPI: CR register with Abort request */
  1551. SET_BIT(hqspi->Instance->CR, QUADSPI_CR_ABORT);
  1552. }
  1553. }
  1554. return status;
  1555. }
  1556. /** @brief Set QSPI timeout.
  1557. * @param hqspi: QSPI handle.
  1558. * @param Timeout: Timeout for the QSPI memory access.
  1559. * @retval None
  1560. */
  1561. void HAL_QSPI_SetTimeout(QSPI_HandleTypeDef *hqspi, uint32_t Timeout)
  1562. {
  1563. hqspi->Timeout = Timeout;
  1564. }
  1565. /** @brief Set QSPI Fifo threshold.
  1566. * @param hqspi: QSPI handle.
  1567. * @param Threshold: Threshold of the Fifo (value between 1 and 16).
  1568. * @retval HAL status
  1569. */
  1570. HAL_StatusTypeDef HAL_QSPI_SetFifoThreshold(QSPI_HandleTypeDef *hqspi, uint32_t Threshold)
  1571. {
  1572. HAL_StatusTypeDef status = HAL_OK;
  1573. /* Process locked */
  1574. __HAL_LOCK(hqspi);
  1575. if(hqspi->State == HAL_QSPI_STATE_READY)
  1576. {
  1577. /* Synchronize init structure with new FIFO threshold value */
  1578. hqspi->Init.FifoThreshold = Threshold;
  1579. /* Configure QSPI FIFO Threshold */
  1580. MODIFY_REG(hqspi->Instance->CR, QUADSPI_CR_FTHRES,
  1581. ((hqspi->Init.FifoThreshold - 1) << POSITION_VAL(QUADSPI_CR_FTHRES)));
  1582. }
  1583. else
  1584. {
  1585. status = HAL_BUSY;
  1586. }
  1587. /* Process unlocked */
  1588. __HAL_UNLOCK(hqspi);
  1589. /* Return function status */
  1590. return status;
  1591. }
  1592. /** @brief Get QSPI Fifo threshold.
  1593. * @param hqspi: QSPI handle.
  1594. * @retval Fifo threshold (value between 1 and 16)
  1595. */
  1596. uint32_t HAL_QSPI_GetFifoThreshold(QSPI_HandleTypeDef *hqspi)
  1597. {
  1598. return ((READ_BIT(hqspi->Instance->CR, QUADSPI_CR_FTHRES) >> POSITION_VAL(QUADSPI_CR_FTHRES)) + 1);
  1599. }
  1600. /**
  1601. * @}
  1602. */
  1603. /**
  1604. * @brief DMA QSPI receive process complete callback.
  1605. * @param hmdma: MDMA handle
  1606. * @retval None
  1607. */
  1608. static void QSPI_DMARxCplt(MDMA_HandleTypeDef *hmdma)
  1609. {
  1610. QSPI_HandleTypeDef* hqspi = ( QSPI_HandleTypeDef* )((MDMA_HandleTypeDef* )hmdma)->Parent;
  1611. hqspi->RxXferCount = 0;
  1612. /* Enable the QSPI transfer complete Interrupt */
  1613. __HAL_QSPI_ENABLE_IT(hqspi, QSPI_IT_TC);
  1614. }
  1615. /**
  1616. * @brief DMA QSPI transmit process complete callback.
  1617. * @param hmdma: MDMA handle
  1618. * @retval None
  1619. */
  1620. static void QSPI_DMATxCplt(MDMA_HandleTypeDef *hmdma)
  1621. {
  1622. QSPI_HandleTypeDef* hqspi = ( QSPI_HandleTypeDef* )((MDMA_HandleTypeDef* )hmdma)->Parent;
  1623. hqspi->TxXferCount = 0;
  1624. /* Enable the QSPI transfer complete Interrupt */
  1625. __HAL_QSPI_ENABLE_IT(hqspi, QSPI_IT_TC);
  1626. }
  1627. /**
  1628. * @brief DMA QSPI communication error callback.
  1629. * @param hmdma: MDMA handle
  1630. * @retval None
  1631. */
  1632. static void QSPI_DMAError(MDMA_HandleTypeDef *hmdma)
  1633. {
  1634. QSPI_HandleTypeDef* hqspi = ( QSPI_HandleTypeDef* )((MDMA_HandleTypeDef* )hmdma)->Parent;
  1635. hqspi->RxXferCount = 0;
  1636. hqspi->TxXferCount = 0;
  1637. hqspi->ErrorCode |= HAL_QSPI_ERROR_DMA;
  1638. /* Disable the MDMA transfer by clearing the DMAEN bit in the QSPI CR register */
  1639. CLEAR_BIT(hqspi->Instance->CR, QUADSPI_CR_DMAEN);
  1640. /* Abort the QSPI */
  1641. HAL_QSPI_Abort_IT(hqspi);
  1642. }
  1643. /**
  1644. * @brief MDMA QSPI abort complete callback.
  1645. * @param hmdma: MDMA handle
  1646. * @retval None
  1647. */
  1648. static void QSPI_DMAAbortCplt(MDMA_HandleTypeDef *hmdma)
  1649. {
  1650. QSPI_HandleTypeDef* hqspi = ( QSPI_HandleTypeDef* )((MDMA_HandleTypeDef* )hmdma)->Parent;
  1651. hqspi->RxXferCount = 0;
  1652. hqspi->TxXferCount = 0;
  1653. if(hqspi->State == HAL_QSPI_STATE_ABORT)
  1654. {
  1655. /* MDMA Abort called by QSPI abort */
  1656. /* Clear interrupt */
  1657. __HAL_QSPI_CLEAR_FLAG(hqspi, QSPI_FLAG_TC);
  1658. /* Enable the QSPI Transfer Complete Interrupt */
  1659. __HAL_QSPI_ENABLE_IT(hqspi, QSPI_IT_TC);
  1660. /* Configure QSPI: CR register with Abort request */
  1661. SET_BIT(hqspi->Instance->CR, QUADSPI_CR_ABORT);
  1662. }
  1663. else
  1664. {
  1665. /* MDMA Abort called due to a transfer error interrupt */
  1666. /* Change state of QSPI */
  1667. hqspi->State = HAL_QSPI_STATE_READY;
  1668. /* Error callback */
  1669. HAL_QSPI_ErrorCallback(hqspi);
  1670. }
  1671. }
  1672. /**
  1673. * @brief Wait for a flag state until timeout.
  1674. * @param hqspi: QSPI handle
  1675. * @param Flag: Flag checked
  1676. * @param State: Value of the flag expected
  1677. * @param tickstart: Tick start value
  1678. * @param Timeout: Duration of the timeout
  1679. * @retval HAL status
  1680. */
  1681. static HAL_StatusTypeDef QSPI_WaitFlagStateUntilTimeout(QSPI_HandleTypeDef *hqspi, uint32_t Flag,
  1682. FlagStatus State, uint32_t tickstart, uint32_t Timeout)
  1683. {
  1684. /* Wait until flag is in expected state */
  1685. while((FlagStatus)(__HAL_QSPI_GET_FLAG(hqspi, Flag)) != State)
  1686. {
  1687. /* Check for the Timeout */
  1688. if (Timeout != HAL_MAX_DELAY)
  1689. {
  1690. if((Timeout == 0) || ((HAL_GetTick() - tickstart) > Timeout))
  1691. {
  1692. hqspi->State = HAL_QSPI_STATE_ERROR;
  1693. hqspi->ErrorCode |= HAL_QSPI_ERROR_TIMEOUT;
  1694. return HAL_ERROR;
  1695. }
  1696. }
  1697. }
  1698. return HAL_OK;
  1699. }
  1700. /**
  1701. * @brief Configure the communication registers.
  1702. * @param hqspi: QSPI handle
  1703. * @param cmd: structure that contains the command configuration information
  1704. * @param FunctionalMode: functional mode to configured
  1705. * This parameter can be one of the following values:
  1706. * @arg QSPI_FUNCTIONAL_MODE_INDIRECT_WRITE: Indirect write mode
  1707. * @arg QSPI_FUNCTIONAL_MODE_INDIRECT_READ: Indirect read mode
  1708. * @arg QSPI_FUNCTIONAL_MODE_AUTO_POLLING: Automatic polling mode
  1709. * @arg QSPI_FUNCTIONAL_MODE_MEMORY_MAPPED: Memory-mapped mode
  1710. * @retval None
  1711. */
  1712. static void QSPI_Config(QSPI_HandleTypeDef *hqspi, QSPI_CommandTypeDef *cmd, uint32_t FunctionalMode)
  1713. {
  1714. assert_param(IS_QSPI_FUNCTIONAL_MODE(FunctionalMode));
  1715. if ((cmd->DataMode != QSPI_DATA_NONE) && (FunctionalMode != QSPI_FUNCTIONAL_MODE_MEMORY_MAPPED))
  1716. {
  1717. /* Configure QSPI: DLR register with the number of data to read or write */
  1718. WRITE_REG(hqspi->Instance->DLR, (cmd->NbData - 1));
  1719. }
  1720. if (cmd->InstructionMode != QSPI_INSTRUCTION_NONE)
  1721. {
  1722. if (cmd->AlternateByteMode != QSPI_ALTERNATE_BYTES_NONE)
  1723. {
  1724. /* Configure QSPI: ABR register with alternate bytes value */
  1725. WRITE_REG(hqspi->Instance->ABR, cmd->AlternateBytes);
  1726. if (cmd->AddressMode != QSPI_ADDRESS_NONE)
  1727. {
  1728. /*---- Command with instruction, address and alternate bytes ----*/
  1729. /* Configure QSPI: CCR register with all communications parameters */
  1730. WRITE_REG(hqspi->Instance->CCR, (cmd->DdrMode | cmd->DdrHoldHalfCycle | cmd->SIOOMode |
  1731. cmd->DataMode | (cmd->DummyCycles << POSITION_VAL(QUADSPI_CCR_DCYC)) |
  1732. cmd->AlternateBytesSize | cmd->AlternateByteMode |
  1733. cmd->AddressSize | cmd->AddressMode | cmd->InstructionMode |
  1734. cmd->Instruction | FunctionalMode));
  1735. if (FunctionalMode != QSPI_FUNCTIONAL_MODE_MEMORY_MAPPED)
  1736. {
  1737. /* Configure QSPI: AR register with address value */
  1738. WRITE_REG(hqspi->Instance->AR, cmd->Address);
  1739. }
  1740. }
  1741. else
  1742. {
  1743. /*---- Command with instruction and alternate bytes ----*/
  1744. /* Configure QSPI: CCR register with all communications parameters */
  1745. WRITE_REG(hqspi->Instance->CCR, (cmd->DdrMode | cmd->DdrHoldHalfCycle | cmd->SIOOMode |
  1746. cmd->DataMode | (cmd->DummyCycles << POSITION_VAL(QUADSPI_CCR_DCYC)) |
  1747. cmd->AlternateBytesSize | cmd->AlternateByteMode |
  1748. cmd->AddressMode | cmd->InstructionMode |
  1749. cmd->Instruction | FunctionalMode));
  1750. }
  1751. }
  1752. else
  1753. {
  1754. if (cmd->AddressMode != QSPI_ADDRESS_NONE)
  1755. {
  1756. /*---- Command with instruction and address ----*/
  1757. /* Configure QSPI: CCR register with all communications parameters */
  1758. WRITE_REG(hqspi->Instance->CCR, (cmd->DdrMode | cmd->DdrHoldHalfCycle | cmd->SIOOMode |
  1759. cmd->DataMode | (cmd->DummyCycles << POSITION_VAL(QUADSPI_CCR_DCYC)) |
  1760. cmd->AlternateByteMode | cmd->AddressSize | cmd->AddressMode |
  1761. cmd->InstructionMode | cmd->Instruction | FunctionalMode));
  1762. if (FunctionalMode != QSPI_FUNCTIONAL_MODE_MEMORY_MAPPED)
  1763. {
  1764. /* Configure QSPI: AR register with address value */
  1765. WRITE_REG(hqspi->Instance->AR, cmd->Address);
  1766. }
  1767. }
  1768. else
  1769. {
  1770. /*---- Command with only instruction ----*/
  1771. /* Configure QSPI: CCR register with all communications parameters */
  1772. WRITE_REG(hqspi->Instance->CCR, (cmd->DdrMode | cmd->DdrHoldHalfCycle | cmd->SIOOMode |
  1773. cmd->DataMode | (cmd->DummyCycles << POSITION_VAL(QUADSPI_CCR_DCYC)) |
  1774. cmd->AlternateByteMode | cmd->AddressMode |
  1775. cmd->InstructionMode | cmd->Instruction | FunctionalMode));
  1776. }
  1777. }
  1778. }
  1779. else
  1780. {
  1781. if (cmd->AlternateByteMode != QSPI_ALTERNATE_BYTES_NONE)
  1782. {
  1783. /* Configure QSPI: ABR register with alternate bytes value */
  1784. WRITE_REG(hqspi->Instance->ABR, cmd->AlternateBytes);
  1785. if (cmd->AddressMode != QSPI_ADDRESS_NONE)
  1786. {
  1787. /*---- Command with address and alternate bytes ----*/
  1788. /* Configure QSPI: CCR register with all communications parameters */
  1789. WRITE_REG(hqspi->Instance->CCR, (cmd->DdrMode | cmd->DdrHoldHalfCycle | cmd->SIOOMode |
  1790. cmd->DataMode | (cmd->DummyCycles << POSITION_VAL(QUADSPI_CCR_DCYC)) |
  1791. cmd->AlternateBytesSize | cmd->AlternateByteMode |
  1792. cmd->AddressSize | cmd->AddressMode |
  1793. cmd->InstructionMode | FunctionalMode));
  1794. if (FunctionalMode != QSPI_FUNCTIONAL_MODE_MEMORY_MAPPED)
  1795. {
  1796. /* Configure QSPI: AR register with address value */
  1797. WRITE_REG(hqspi->Instance->AR, cmd->Address);
  1798. }
  1799. }
  1800. else
  1801. {
  1802. /*---- Command with only alternate bytes ----*/
  1803. /* Configure QSPI: CCR register with all communications parameters */
  1804. WRITE_REG(hqspi->Instance->CCR, (cmd->DdrMode | cmd->DdrHoldHalfCycle | cmd->SIOOMode |
  1805. cmd->DataMode | (cmd->DummyCycles << POSITION_VAL(QUADSPI_CCR_DCYC)) |
  1806. cmd->AlternateBytesSize | cmd->AlternateByteMode |
  1807. cmd->AddressMode | cmd->InstructionMode | FunctionalMode));
  1808. }
  1809. }
  1810. else
  1811. {
  1812. if (cmd->AddressMode != QSPI_ADDRESS_NONE)
  1813. {
  1814. /*---- Command with only address ----*/
  1815. /* Configure QSPI: CCR register with all communications parameters */
  1816. WRITE_REG(hqspi->Instance->CCR, (cmd->DdrMode | cmd->DdrHoldHalfCycle | cmd->SIOOMode |
  1817. cmd->DataMode | (cmd->DummyCycles << POSITION_VAL(QUADSPI_CCR_DCYC)) |
  1818. cmd->AlternateByteMode | cmd->AddressSize |
  1819. cmd->AddressMode | cmd->InstructionMode | FunctionalMode));
  1820. if (FunctionalMode != QSPI_FUNCTIONAL_MODE_MEMORY_MAPPED)
  1821. {
  1822. /* Configure QSPI: AR register with address value */
  1823. WRITE_REG(hqspi->Instance->AR, cmd->Address);
  1824. }
  1825. }
  1826. else
  1827. {
  1828. /*---- Command with only data phase ----*/
  1829. if (cmd->DataMode != QSPI_DATA_NONE)
  1830. {
  1831. /* Configure QSPI: CCR register with all communications parameters */
  1832. WRITE_REG(hqspi->Instance->CCR, (cmd->DdrMode | cmd->DdrHoldHalfCycle | cmd->SIOOMode |
  1833. cmd->DataMode | (cmd->DummyCycles << POSITION_VAL(QUADSPI_CCR_DCYC)) |
  1834. cmd->AlternateByteMode | cmd->AddressMode |
  1835. cmd->InstructionMode | FunctionalMode));
  1836. }
  1837. }
  1838. }
  1839. }
  1840. }
  1841. /**
  1842. * @}
  1843. */
  1844. #endif /* HAL_QSPI_MODULE_ENABLED */
  1845. /**
  1846. * @}
  1847. */
  1848. /**
  1849. * @}
  1850. */
  1851. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/