stm32f4xx_hal_qspi.c 79 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424
  1. /**
  2. ******************************************************************************
  3. * @file stm32f4xx_hal_qspi.c
  4. * @author MCD Application Team
  5. * @version V1.6.0
  6. * @date 04-November-2016
  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. * + DMA 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 DMA channel
  35. with __HAL_RCC_DMAx_CLK_ENABLE(), configure DMA with HAL_DMA_Init(),
  36. link it with QuadSPI handle using __HAL_LINKDMA(), enable and configure
  37. DMA channel 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) 2016 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 "stm32f4xx_hal.h"
  162. /** @addtogroup STM32F4xx_HAL_Driver
  163. * @{
  164. */
  165. /** @defgroup QSPI QSPI
  166. * @brief QSPI HAL module driver
  167. * @{
  168. */
  169. #ifdef HAL_QSPI_MODULE_ENABLED
  170. #if defined(STM32F446xx) || defined(STM32F469xx) || defined(STM32F479xx) || defined(STM32F412Zx) || defined(STM32F412Vx) || \
  171. defined(STM32F412Rx) || defined(STM32F413xx) || defined(STM32F423xx)
  172. /* Private typedef -----------------------------------------------------------*/
  173. /* Private define ------------------------------------------------------------*/
  174. /** @addtogroup QSPI_Private_Constants
  175. * @{
  176. */
  177. #define QSPI_FUNCTIONAL_MODE_INDIRECT_WRITE ((uint32_t)0x00000000U) /*!<Indirect write mode*/
  178. #define QSPI_FUNCTIONAL_MODE_INDIRECT_READ ((uint32_t)QUADSPI_CCR_FMODE_0) /*!<Indirect read mode*/
  179. #define QSPI_FUNCTIONAL_MODE_AUTO_POLLING ((uint32_t)QUADSPI_CCR_FMODE_1) /*!<Automatic polling mode*/
  180. #define QSPI_FUNCTIONAL_MODE_MEMORY_MAPPED ((uint32_t)QUADSPI_CCR_FMODE) /*!<Memory-mapped mode*/
  181. /**
  182. * @}
  183. */
  184. /* Private macro -------------------------------------------------------------*/
  185. /** @addtogroup QSPI_Private_Macros QSPI Private Macros
  186. * @{
  187. */
  188. #define IS_QSPI_FUNCTIONAL_MODE(MODE) (((MODE) == QSPI_FUNCTIONAL_MODE_INDIRECT_WRITE) || \
  189. ((MODE) == QSPI_FUNCTIONAL_MODE_INDIRECT_READ) || \
  190. ((MODE) == QSPI_FUNCTIONAL_MODE_AUTO_POLLING) || \
  191. ((MODE) == QSPI_FUNCTIONAL_MODE_MEMORY_MAPPED))
  192. /**
  193. * @}
  194. */
  195. /* Private variables ---------------------------------------------------------*/
  196. /* Private function prototypes -----------------------------------------------*/
  197. /** @addtogroup QSPI_Private_Functions QSPI Private Functions
  198. * @{
  199. */
  200. static void QSPI_DMARxCplt(DMA_HandleTypeDef *hdma);
  201. static void QSPI_DMATxCplt(DMA_HandleTypeDef *hdma);
  202. static void QSPI_DMARxHalfCplt(DMA_HandleTypeDef *hdma);
  203. static void QSPI_DMATxHalfCplt(DMA_HandleTypeDef *hdma);
  204. static void QSPI_DMAError(DMA_HandleTypeDef *hdma);
  205. static void QSPI_DMAAbortCplt(DMA_HandleTypeDef *hdma);
  206. static HAL_StatusTypeDef QSPI_WaitFlagStateUntilTimeout(QSPI_HandleTypeDef *hqspi, uint32_t Flag, FlagStatus State, uint32_t tickstart, uint32_t Timeout);
  207. static void QSPI_Config(QSPI_HandleTypeDef *hqspi, QSPI_CommandTypeDef *cmd, uint32_t FunctionalMode);
  208. /**
  209. * @}
  210. */
  211. /* Exported functions ---------------------------------------------------------*/
  212. /** @defgroup QSPI_Exported_Functions QSPI Exported Functions
  213. * @{
  214. */
  215. /** @defgroup QSPI_Exported_Functions_Group1 Initialization/de-initialization functions
  216. * @brief Initialization and Configuration functions
  217. *
  218. @verbatim
  219. ===============================================================================
  220. ##### Initialization and Configuration functions #####
  221. ===============================================================================
  222. [..]
  223. This subsection provides a set of functions allowing to :
  224. (+) Initialize the QuadSPI.
  225. (+) De-initialize the QuadSPI.
  226. @endverbatim
  227. * @{
  228. */
  229. /**
  230. * @brief Initializes the QSPI mode according to the specified parameters
  231. * in the QSPI_InitTypeDef and creates the associated handle.
  232. * @param hqspi: qspi handle
  233. * @retval HAL status
  234. */
  235. HAL_StatusTypeDef HAL_QSPI_Init(QSPI_HandleTypeDef *hqspi)
  236. {
  237. HAL_StatusTypeDef status = HAL_ERROR;
  238. uint32_t tickstart = HAL_GetTick();
  239. /* Check the QSPI handle allocation */
  240. if(hqspi == NULL)
  241. {
  242. return HAL_ERROR;
  243. }
  244. /* Check the parameters */
  245. assert_param(IS_QSPI_ALL_INSTANCE(hqspi->Instance));
  246. assert_param(IS_QSPI_CLOCK_PRESCALER(hqspi->Init.ClockPrescaler));
  247. assert_param(IS_QSPI_FIFO_THRESHOLD(hqspi->Init.FifoThreshold));
  248. assert_param(IS_QSPI_SSHIFT(hqspi->Init.SampleShifting));
  249. assert_param(IS_QSPI_FLASH_SIZE(hqspi->Init.FlashSize));
  250. assert_param(IS_QSPI_CS_HIGH_TIME(hqspi->Init.ChipSelectHighTime));
  251. assert_param(IS_QSPI_CLOCK_MODE(hqspi->Init.ClockMode));
  252. assert_param(IS_QSPI_DUAL_FLASH_MODE(hqspi->Init.DualFlash));
  253. if (hqspi->Init.DualFlash != QSPI_DUALFLASH_ENABLE )
  254. {
  255. assert_param(IS_QSPI_FLASH_ID(hqspi->Init.FlashID));
  256. }
  257. /* Process locked */
  258. __HAL_LOCK(hqspi);
  259. if(hqspi->State == HAL_QSPI_STATE_RESET)
  260. {
  261. /* Allocate lock resource and initialize it */
  262. hqspi->Lock = HAL_UNLOCKED;
  263. /* Init the low level hardware : GPIO, CLOCK */
  264. HAL_QSPI_MspInit(hqspi);
  265. /* Configure the default timeout for the QSPI memory access */
  266. HAL_QSPI_SetTimeout(hqspi, HAL_QPSI_TIMEOUT_DEFAULT_VALUE);
  267. }
  268. /* Configure QSPI FIFO Threshold */
  269. MODIFY_REG(hqspi->Instance->CR, QUADSPI_CR_FTHRES, ((hqspi->Init.FifoThreshold - 1) << 8));
  270. /* Wait till BUSY flag reset */
  271. status = QSPI_WaitFlagStateUntilTimeout(hqspi, QSPI_FLAG_BUSY, RESET, tickstart, hqspi->Timeout);
  272. if(status == HAL_OK)
  273. {
  274. /* Configure QSPI Clock Prescaler and Sample Shift */
  275. MODIFY_REG(hqspi->Instance->CR,(QUADSPI_CR_PRESCALER | QUADSPI_CR_SSHIFT | QUADSPI_CR_FSEL | QUADSPI_CR_DFM), ((hqspi->Init.ClockPrescaler << 24)| hqspi->Init.SampleShifting | hqspi->Init.FlashID| hqspi->Init.DualFlash ));
  276. /* Configure QSPI Flash Size, CS High Time and Clock Mode */
  277. MODIFY_REG(hqspi->Instance->DCR, (QUADSPI_DCR_FSIZE | QUADSPI_DCR_CSHT | QUADSPI_DCR_CKMODE),
  278. ((hqspi->Init.FlashSize << 16) | hqspi->Init.ChipSelectHighTime | hqspi->Init.ClockMode));
  279. /* Enable the QSPI peripheral */
  280. __HAL_QSPI_ENABLE(hqspi);
  281. /* Set QSPI error code to none */
  282. hqspi->ErrorCode = HAL_QSPI_ERROR_NONE;
  283. /* Initialize the QSPI state */
  284. hqspi->State = HAL_QSPI_STATE_READY;
  285. }
  286. /* Release Lock */
  287. __HAL_UNLOCK(hqspi);
  288. /* Return function status */
  289. return status;
  290. }
  291. /**
  292. * @brief DeInitializes the QSPI peripheral
  293. * @param hqspi: qspi handle
  294. * @retval HAL status
  295. */
  296. HAL_StatusTypeDef HAL_QSPI_DeInit(QSPI_HandleTypeDef *hqspi)
  297. {
  298. /* Check the QSPI handle allocation */
  299. if(hqspi == NULL)
  300. {
  301. return HAL_ERROR;
  302. }
  303. /* Process locked */
  304. __HAL_LOCK(hqspi);
  305. /* Disable the QSPI Peripheral Clock */
  306. __HAL_QSPI_DISABLE(hqspi);
  307. /* DeInit the low level hardware: GPIO, CLOCK, NVIC... */
  308. HAL_QSPI_MspDeInit(hqspi);
  309. /* Set QSPI error code to none */
  310. hqspi->ErrorCode = HAL_QSPI_ERROR_NONE;
  311. /* Initialize the QSPI state */
  312. hqspi->State = HAL_QSPI_STATE_RESET;
  313. /* Release Lock */
  314. __HAL_UNLOCK(hqspi);
  315. return HAL_OK;
  316. }
  317. /**
  318. * @brief QSPI MSP Init
  319. * @param hqspi: QSPI handle
  320. * @retval None
  321. */
  322. __weak void HAL_QSPI_MspInit(QSPI_HandleTypeDef *hqspi)
  323. {
  324. /* Prevent unused argument(s) compilation warning */
  325. UNUSED(hqspi);
  326. /* NOTE : This function should not be modified, when the callback is needed,
  327. the HAL_QSPI_MspInit can be implemented in the user file
  328. */
  329. }
  330. /**
  331. * @brief QSPI MSP DeInit
  332. * @param hqspi: QSPI handle
  333. * @retval None
  334. */
  335. __weak void HAL_QSPI_MspDeInit(QSPI_HandleTypeDef *hqspi)
  336. {
  337. /* Prevent unused argument(s) compilation warning */
  338. UNUSED(hqspi);
  339. /* NOTE : This function should not be modified, when the callback is needed,
  340. the HAL_QSPI_MspDeInit can be implemented in the user file
  341. */
  342. }
  343. /**
  344. * @}
  345. */
  346. /** @defgroup QSPI_Exported_Functions_Group2 IO operation functions
  347. * @brief QSPI Transmit/Receive functions
  348. *
  349. @verbatim
  350. ===============================================================================
  351. ##### IO operation functions #####
  352. ===============================================================================
  353. [..]
  354. This subsection provides a set of functions allowing to :
  355. (+) Handle the interrupts.
  356. (+) Handle the command sequence.
  357. (+) Transmit data in blocking, interrupt or DMA mode.
  358. (+) Receive data in blocking, interrupt or DMA mode.
  359. (+) Manage the auto-polling functional mode.
  360. (+) Manage the memory-mapped functional mode.
  361. @endverbatim
  362. * @{
  363. */
  364. /**
  365. * @brief This function handles QSPI interrupt request.
  366. * @param hqspi: QSPI handle
  367. * @retval None.
  368. */
  369. void HAL_QSPI_IRQHandler(QSPI_HandleTypeDef *hqspi)
  370. {
  371. __IO uint32_t *data_reg;
  372. uint32_t flag = READ_REG(hqspi->Instance->SR);
  373. uint32_t itsource = READ_REG(hqspi->Instance->CR);
  374. /* QSPI Fifo Threshold interrupt occurred ----------------------------------*/
  375. if(((flag & QSPI_FLAG_FT)!= RESET) && ((itsource & QSPI_IT_FT)!= RESET))
  376. {
  377. data_reg = &hqspi->Instance->DR;
  378. if(hqspi->State == HAL_QSPI_STATE_BUSY_INDIRECT_TX)
  379. {
  380. /* Transmission process */
  381. while(__HAL_QSPI_GET_FLAG(hqspi, QSPI_FLAG_FT) != 0)
  382. {
  383. if (hqspi->TxXferCount > 0)
  384. {
  385. /* Fill the FIFO until it is full */
  386. *(__IO uint8_t *)data_reg = *hqspi->pTxBuffPtr++;
  387. hqspi->TxXferCount--;
  388. }
  389. else
  390. {
  391. /* No more data available for the transfer */
  392. /* Disable the QSPI FIFO Threshold Interrupt */
  393. __HAL_QSPI_DISABLE_IT(hqspi, QSPI_IT_FT);
  394. break;
  395. }
  396. }
  397. }
  398. else if(hqspi->State == HAL_QSPI_STATE_BUSY_INDIRECT_RX)
  399. {
  400. /* Receiving Process */
  401. while(__HAL_QSPI_GET_FLAG(hqspi, QSPI_FLAG_FT) != 0)
  402. {
  403. if (hqspi->RxXferCount > 0)
  404. {
  405. /* Read the FIFO until it is empty */
  406. *hqspi->pRxBuffPtr++ = *(__IO uint8_t *)data_reg;
  407. hqspi->RxXferCount--;
  408. }
  409. else
  410. {
  411. /* All data have been received for the transfer */
  412. /* Disable the QSPI FIFO Threshold Interrupt */
  413. __HAL_QSPI_DISABLE_IT(hqspi, QSPI_IT_FT);
  414. break;
  415. }
  416. }
  417. }
  418. /* FIFO Threshold callback */
  419. HAL_QSPI_FifoThresholdCallback(hqspi);
  420. }
  421. /* QSPI Transfer Complete interrupt occurred -------------------------------*/
  422. else if(((flag & QSPI_FLAG_TC)!= RESET) && ((itsource & QSPI_IT_TC)!= RESET))
  423. {
  424. /* Clear interrupt */
  425. WRITE_REG(hqspi->Instance->FCR, QSPI_FLAG_TC);
  426. /* Disable the QSPI FIFO Threshold, Transfer Error and Transfer complete Interrupts */
  427. __HAL_QSPI_DISABLE_IT(hqspi, QSPI_IT_TC | QSPI_IT_TE | QSPI_IT_FT);
  428. /* Transfer complete callback */
  429. if(hqspi->State == HAL_QSPI_STATE_BUSY_INDIRECT_TX)
  430. {
  431. if ((hqspi->Instance->CR & QUADSPI_CR_DMAEN)!= RESET)
  432. {
  433. /* Disable the DMA transfer by clearing the DMAEN bit in the QSPI CR register */
  434. CLEAR_BIT(hqspi->Instance->CR, QUADSPI_CR_DMAEN);
  435. /* Disable the DMA channel */
  436. __HAL_DMA_DISABLE(hqspi->hdma);
  437. }
  438. /* Clear Busy bit */
  439. HAL_QSPI_Abort_IT(hqspi);
  440. /* Change state of QSPI */
  441. hqspi->State = HAL_QSPI_STATE_READY;
  442. /* TX Complete callback */
  443. HAL_QSPI_TxCpltCallback(hqspi);
  444. }
  445. else if(hqspi->State == HAL_QSPI_STATE_BUSY_INDIRECT_RX)
  446. {
  447. if ((hqspi->Instance->CR & QUADSPI_CR_DMAEN)!= RESET)
  448. {
  449. /* Disable the DMA transfer by clearing the DMAEN bit in the QSPI CR register */
  450. CLEAR_BIT(hqspi->Instance->CR, QUADSPI_CR_DMAEN);
  451. /* Disable the DMA channel */
  452. __HAL_DMA_DISABLE(hqspi->hdma);
  453. }
  454. else
  455. {
  456. data_reg = &hqspi->Instance->DR;
  457. while(READ_BIT(hqspi->Instance->SR, QUADSPI_SR_FLEVEL) != 0)
  458. {
  459. if (hqspi->RxXferCount > 0)
  460. {
  461. /* Read the last data received in the FIFO until it is empty */
  462. *hqspi->pRxBuffPtr++ = *(__IO uint8_t *)data_reg;
  463. hqspi->RxXferCount--;
  464. }
  465. else
  466. {
  467. /* All data have been received for the transfer */
  468. break;
  469. }
  470. }
  471. }
  472. /* Workaround - Extra data written in the FIFO at the end of a read transfer */
  473. HAL_QSPI_Abort_IT(hqspi);
  474. /* Change state of QSPI */
  475. hqspi->State = HAL_QSPI_STATE_READY;
  476. /* RX Complete callback */
  477. HAL_QSPI_RxCpltCallback(hqspi);
  478. }
  479. else if(hqspi->State == HAL_QSPI_STATE_BUSY)
  480. {
  481. /* Change state of QSPI */
  482. hqspi->State = HAL_QSPI_STATE_READY;
  483. /* Command Complete callback */
  484. HAL_QSPI_CmdCpltCallback(hqspi);
  485. }
  486. else if(hqspi->State == HAL_QSPI_STATE_ABORT)
  487. {
  488. /* Change state of QSPI */
  489. hqspi->State = HAL_QSPI_STATE_READY;
  490. if (hqspi->ErrorCode == HAL_QSPI_ERROR_NONE)
  491. {
  492. /* Abort called by the user */
  493. /* Abort Complete callback */
  494. HAL_QSPI_AbortCpltCallback(hqspi);
  495. }
  496. else
  497. {
  498. /* Abort due to an error (eg : DMA error) */
  499. /* Error callback */
  500. HAL_QSPI_ErrorCallback(hqspi);
  501. }
  502. }
  503. }
  504. /* QSPI Status Match interrupt occurred ------------------------------------*/
  505. else if(((flag & QSPI_FLAG_SM)!= RESET) && ((itsource & QSPI_IT_SM)!= RESET))
  506. {
  507. /* Clear interrupt */
  508. WRITE_REG(hqspi->Instance->FCR, QSPI_FLAG_SM);
  509. /* Check if the automatic poll mode stop is activated */
  510. if(READ_BIT(hqspi->Instance->CR, QUADSPI_CR_APMS) != 0)
  511. {
  512. /* Disable the QSPI Transfer Error and Status Match Interrupts */
  513. __HAL_QSPI_DISABLE_IT(hqspi, (QSPI_IT_SM | QSPI_IT_TE));
  514. /* Change state of QSPI */
  515. hqspi->State = HAL_QSPI_STATE_READY;
  516. }
  517. /* Status match callback */
  518. HAL_QSPI_StatusMatchCallback(hqspi);
  519. }
  520. /* QSPI Transfer Error interrupt occurred ----------------------------------*/
  521. else if(((flag & QSPI_FLAG_TE)!= RESET) && ((itsource & QSPI_IT_TE)!= RESET))
  522. {
  523. /* Clear interrupt */
  524. WRITE_REG(hqspi->Instance->FCR, QSPI_FLAG_TE);
  525. /* Disable all the QSPI Interrupts */
  526. __HAL_QSPI_DISABLE_IT(hqspi, QSPI_IT_SM | QSPI_IT_TC | QSPI_IT_TE | QSPI_IT_FT);
  527. /* Set error code */
  528. hqspi->ErrorCode |= HAL_QSPI_ERROR_TRANSFER;
  529. if ((hqspi->Instance->CR & QUADSPI_CR_DMAEN)!= RESET)
  530. {
  531. /* Disable the DMA transfer by clearing the DMAEN bit in the QSPI CR register */
  532. CLEAR_BIT(hqspi->Instance->CR, QUADSPI_CR_DMAEN);
  533. /* Disable the DMA channel */
  534. hqspi->hdma->XferAbortCallback = QSPI_DMAAbortCplt;
  535. HAL_DMA_Abort_IT(hqspi->hdma);
  536. }
  537. else
  538. {
  539. /* Change state of QSPI */
  540. hqspi->State = HAL_QSPI_STATE_READY;
  541. /* Error callback */
  542. HAL_QSPI_ErrorCallback(hqspi);
  543. }
  544. }
  545. /* QSPI Timeout interrupt occurred -----------------------------------------*/
  546. else if(((flag & QSPI_FLAG_TO)!= RESET) && ((itsource & QSPI_IT_TO)!= RESET))
  547. {
  548. /* Clear interrupt */
  549. WRITE_REG(hqspi->Instance->FCR, QSPI_FLAG_TO);
  550. /* Time out callback */
  551. HAL_QSPI_TimeOutCallback(hqspi);
  552. }
  553. }
  554. /**
  555. * @brief Sets the command configuration.
  556. * @param hqspi: QSPI handle
  557. * @param cmd : structure that contains the command configuration information
  558. * @param Timeout : Time out duration
  559. * @note This function is used only in Indirect Read or Write Modes
  560. * @retval HAL status
  561. */
  562. HAL_StatusTypeDef HAL_QSPI_Command(QSPI_HandleTypeDef *hqspi, QSPI_CommandTypeDef *cmd, uint32_t Timeout)
  563. {
  564. HAL_StatusTypeDef status = HAL_ERROR;
  565. uint32_t tickstart = HAL_GetTick();
  566. /* Check the parameters */
  567. assert_param(IS_QSPI_INSTRUCTION_MODE(cmd->InstructionMode));
  568. if (cmd->InstructionMode != QSPI_INSTRUCTION_NONE)
  569. {
  570. assert_param(IS_QSPI_INSTRUCTION(cmd->Instruction));
  571. }
  572. assert_param(IS_QSPI_ADDRESS_MODE(cmd->AddressMode));
  573. if (cmd->AddressMode != QSPI_ADDRESS_NONE)
  574. {
  575. assert_param(IS_QSPI_ADDRESS_SIZE(cmd->AddressSize));
  576. }
  577. assert_param(IS_QSPI_ALTERNATE_BYTES_MODE(cmd->AlternateByteMode));
  578. if (cmd->AlternateByteMode != QSPI_ALTERNATE_BYTES_NONE)
  579. {
  580. assert_param(IS_QSPI_ALTERNATE_BYTES_SIZE(cmd->AlternateBytesSize));
  581. }
  582. assert_param(IS_QSPI_DUMMY_CYCLES(cmd->DummyCycles));
  583. assert_param(IS_QSPI_DATA_MODE(cmd->DataMode));
  584. assert_param(IS_QSPI_DDR_MODE(cmd->DdrMode));
  585. assert_param(IS_QSPI_DDR_HHC(cmd->DdrHoldHalfCycle));
  586. assert_param(IS_QSPI_SIOO_MODE(cmd->SIOOMode));
  587. /* Process locked */
  588. __HAL_LOCK(hqspi);
  589. if(hqspi->State == HAL_QSPI_STATE_READY)
  590. {
  591. hqspi->ErrorCode = HAL_QSPI_ERROR_NONE;
  592. /* Update QSPI state */
  593. hqspi->State = HAL_QSPI_STATE_BUSY;
  594. /* Wait till BUSY flag reset */
  595. status = QSPI_WaitFlagStateUntilTimeout(hqspi, QSPI_FLAG_BUSY, RESET, tickstart, Timeout);
  596. if (status == HAL_OK)
  597. {
  598. /* Call the configuration function */
  599. QSPI_Config(hqspi, cmd, QSPI_FUNCTIONAL_MODE_INDIRECT_WRITE);
  600. if (cmd->DataMode == QSPI_DATA_NONE)
  601. {
  602. /* When there is no data phase, the transfer start as soon as the configuration is done
  603. so wait until TC flag is set to go back in idle state */
  604. status = QSPI_WaitFlagStateUntilTimeout(hqspi, QSPI_FLAG_TC, SET, tickstart, Timeout);
  605. if (status == HAL_OK)
  606. {
  607. __HAL_QSPI_CLEAR_FLAG(hqspi, QSPI_FLAG_TC);
  608. /* Update QSPI state */
  609. hqspi->State = HAL_QSPI_STATE_READY;
  610. }
  611. }
  612. else
  613. {
  614. /* Update QSPI state */
  615. hqspi->State = HAL_QSPI_STATE_READY;
  616. }
  617. }
  618. }
  619. else
  620. {
  621. status = HAL_BUSY;
  622. }
  623. /* Process unlocked */
  624. __HAL_UNLOCK(hqspi);
  625. /* Return function status */
  626. return status;
  627. }
  628. /**
  629. * @brief Sets the command configuration in interrupt mode.
  630. * @param hqspi: QSPI handle
  631. * @param cmd : structure that contains the command configuration information
  632. * @note This function is used only in Indirect Read or Write Modes
  633. * @retval HAL status
  634. */
  635. HAL_StatusTypeDef HAL_QSPI_Command_IT(QSPI_HandleTypeDef *hqspi, QSPI_CommandTypeDef *cmd)
  636. {
  637. __IO uint32_t count = 0U;
  638. HAL_StatusTypeDef status = HAL_OK;
  639. /* Check the parameters */
  640. assert_param(IS_QSPI_INSTRUCTION_MODE(cmd->InstructionMode));
  641. if (cmd->InstructionMode != QSPI_INSTRUCTION_NONE)
  642. {
  643. assert_param(IS_QSPI_INSTRUCTION(cmd->Instruction));
  644. }
  645. assert_param(IS_QSPI_ADDRESS_MODE(cmd->AddressMode));
  646. if (cmd->AddressMode != QSPI_ADDRESS_NONE)
  647. {
  648. assert_param(IS_QSPI_ADDRESS_SIZE(cmd->AddressSize));
  649. }
  650. assert_param(IS_QSPI_ALTERNATE_BYTES_MODE(cmd->AlternateByteMode));
  651. if (cmd->AlternateByteMode != QSPI_ALTERNATE_BYTES_NONE)
  652. {
  653. assert_param(IS_QSPI_ALTERNATE_BYTES_SIZE(cmd->AlternateBytesSize));
  654. }
  655. assert_param(IS_QSPI_DUMMY_CYCLES(cmd->DummyCycles));
  656. assert_param(IS_QSPI_DATA_MODE(cmd->DataMode));
  657. assert_param(IS_QSPI_DDR_MODE(cmd->DdrMode));
  658. assert_param(IS_QSPI_DDR_HHC(cmd->DdrHoldHalfCycle));
  659. assert_param(IS_QSPI_SIOO_MODE(cmd->SIOOMode));
  660. /* Process locked */
  661. __HAL_LOCK(hqspi);
  662. if(hqspi->State == HAL_QSPI_STATE_READY)
  663. {
  664. hqspi->ErrorCode = HAL_QSPI_ERROR_NONE;
  665. /* Update QSPI state */
  666. hqspi->State = HAL_QSPI_STATE_BUSY;
  667. /* Wait till BUSY flag reset */
  668. count = (hqspi->Timeout) * (SystemCoreClock / 16U / 1000U);
  669. do
  670. {
  671. if (count-- == 0)
  672. {
  673. hqspi->State = HAL_QSPI_STATE_ERROR;
  674. hqspi->ErrorCode |= HAL_QSPI_ERROR_TIMEOUT;
  675. status = HAL_TIMEOUT;
  676. }
  677. }
  678. while ((__HAL_QSPI_GET_FLAG(hqspi, QSPI_FLAG_BUSY)) != RESET);
  679. if (status == HAL_OK)
  680. {
  681. if (cmd->DataMode == QSPI_DATA_NONE)
  682. {
  683. /* Clear interrupt */
  684. __HAL_QSPI_CLEAR_FLAG(hqspi, QSPI_FLAG_TE | QSPI_FLAG_TC);
  685. }
  686. /* Call the configuration function */
  687. QSPI_Config(hqspi, cmd, QSPI_FUNCTIONAL_MODE_INDIRECT_WRITE);
  688. if (cmd->DataMode == QSPI_DATA_NONE)
  689. {
  690. /* When there is no data phase, the transfer start as soon as the configuration is done
  691. so activate TC and TE interrupts */
  692. /* Process unlocked */
  693. __HAL_UNLOCK(hqspi);
  694. /* Enable the QSPI Transfer Error Interrupt */
  695. __HAL_QSPI_ENABLE_IT(hqspi, QSPI_IT_TE | QSPI_IT_TC);
  696. }
  697. else
  698. {
  699. /* Update QSPI state */
  700. hqspi->State = HAL_QSPI_STATE_READY;
  701. /* Process unlocked */
  702. __HAL_UNLOCK(hqspi);
  703. }
  704. }
  705. else
  706. {
  707. /* Process unlocked */
  708. __HAL_UNLOCK(hqspi);
  709. }
  710. }
  711. else
  712. {
  713. status = HAL_BUSY;
  714. /* Process unlocked */
  715. __HAL_UNLOCK(hqspi);
  716. }
  717. /* Return function status */
  718. return status;
  719. }
  720. /**
  721. * @brief Transmit an amount of data in blocking mode.
  722. * @param hqspi: QSPI handle
  723. * @param pData: pointer to data buffer
  724. * @param Timeout : Time out duration
  725. * @note This function is used only in Indirect Write Mode
  726. * @retval HAL status
  727. */
  728. HAL_StatusTypeDef HAL_QSPI_Transmit(QSPI_HandleTypeDef *hqspi, uint8_t *pData, uint32_t Timeout)
  729. {
  730. HAL_StatusTypeDef status = HAL_OK;
  731. uint32_t tickstart = HAL_GetTick();
  732. __IO uint32_t *data_reg = &hqspi->Instance->DR;
  733. /* Process locked */
  734. __HAL_LOCK(hqspi);
  735. if(hqspi->State == HAL_QSPI_STATE_READY)
  736. {
  737. hqspi->ErrorCode = HAL_QSPI_ERROR_NONE;
  738. if(pData != NULL )
  739. {
  740. /* Update state */
  741. hqspi->State = HAL_QSPI_STATE_BUSY_INDIRECT_TX;
  742. /* Configure counters and size of the handle */
  743. hqspi->TxXferCount = READ_REG(hqspi->Instance->DLR) + 1;
  744. hqspi->TxXferSize = READ_REG(hqspi->Instance->DLR) + 1;
  745. hqspi->pTxBuffPtr = pData;
  746. /* Configure QSPI: CCR register with functional as indirect write */
  747. MODIFY_REG(hqspi->Instance->CCR, QUADSPI_CCR_FMODE, QSPI_FUNCTIONAL_MODE_INDIRECT_WRITE);
  748. while(hqspi->TxXferCount > 0)
  749. {
  750. /* Wait until FT flag is set to send data */
  751. status = QSPI_WaitFlagStateUntilTimeout(hqspi, QSPI_FLAG_FT, SET, tickstart, Timeout);
  752. if (status != HAL_OK)
  753. {
  754. break;
  755. }
  756. *(__IO uint8_t *)data_reg = *hqspi->pTxBuffPtr++;
  757. hqspi->TxXferCount--;
  758. }
  759. if (status == HAL_OK)
  760. {
  761. /* Wait until TC flag is set to go back in idle state */
  762. status = QSPI_WaitFlagStateUntilTimeout(hqspi, QSPI_FLAG_TC, SET, tickstart, Timeout);
  763. if (status == HAL_OK)
  764. {
  765. /* Clear Transfer Complete bit */
  766. __HAL_QSPI_CLEAR_FLAG(hqspi, QSPI_FLAG_TC);
  767. /* Clear Busy bit */
  768. status = HAL_QSPI_Abort(hqspi);
  769. }
  770. }
  771. /* Update QSPI state */
  772. hqspi->State = HAL_QSPI_STATE_READY;
  773. }
  774. else
  775. {
  776. hqspi->ErrorCode |= HAL_QSPI_ERROR_INVALID_PARAM;
  777. status = HAL_ERROR;
  778. }
  779. }
  780. else
  781. {
  782. status = HAL_BUSY;
  783. }
  784. /* Process unlocked */
  785. __HAL_UNLOCK(hqspi);
  786. return status;
  787. }
  788. /**
  789. * @brief Receive an amount of data in blocking mode
  790. * @param hqspi: QSPI handle
  791. * @param pData: pointer to data buffer
  792. * @param Timeout : Time out duration
  793. * @note This function is used only in Indirect Read Mode
  794. * @retval HAL status
  795. */
  796. HAL_StatusTypeDef HAL_QSPI_Receive(QSPI_HandleTypeDef *hqspi, uint8_t *pData, uint32_t Timeout)
  797. {
  798. HAL_StatusTypeDef status = HAL_OK;
  799. uint32_t tickstart = HAL_GetTick();
  800. uint32_t addr_reg = READ_REG(hqspi->Instance->AR);
  801. __IO uint32_t *data_reg = &hqspi->Instance->DR;
  802. /* Process locked */
  803. __HAL_LOCK(hqspi);
  804. if(hqspi->State == HAL_QSPI_STATE_READY)
  805. {
  806. hqspi->ErrorCode = HAL_QSPI_ERROR_NONE;
  807. if(pData != NULL )
  808. {
  809. /* Update state */
  810. hqspi->State = HAL_QSPI_STATE_BUSY_INDIRECT_RX;
  811. /* Configure counters and size of the handle */
  812. hqspi->RxXferCount = READ_REG(hqspi->Instance->DLR) + 1;
  813. hqspi->RxXferSize = READ_REG(hqspi->Instance->DLR) + 1;
  814. hqspi->pRxBuffPtr = pData;
  815. /* Configure QSPI: CCR register with functional as indirect read */
  816. MODIFY_REG(hqspi->Instance->CCR, QUADSPI_CCR_FMODE, QSPI_FUNCTIONAL_MODE_INDIRECT_READ);
  817. /* Start the transfer by re-writing the address in AR register */
  818. WRITE_REG(hqspi->Instance->AR, addr_reg);
  819. while(hqspi->RxXferCount > 0)
  820. {
  821. /* Wait until FT or TC flag is set to read received data */
  822. status = QSPI_WaitFlagStateUntilTimeout(hqspi, (QSPI_FLAG_FT | QSPI_FLAG_TC), SET, tickstart, Timeout);
  823. if (status != HAL_OK)
  824. {
  825. break;
  826. }
  827. *hqspi->pRxBuffPtr++ = *(__IO uint8_t *)data_reg;
  828. hqspi->RxXferCount--;
  829. }
  830. if (status == HAL_OK)
  831. {
  832. /* Wait until TC flag is set to go back in idle state */
  833. status = QSPI_WaitFlagStateUntilTimeout(hqspi, QSPI_FLAG_TC, SET, tickstart, Timeout);
  834. if (status == HAL_OK)
  835. {
  836. /* Clear Transfer Complete bit */
  837. __HAL_QSPI_CLEAR_FLAG(hqspi, QSPI_FLAG_TC);
  838. /* Workaround - Extra data written in the FIFO at the end of a read transfer */
  839. status = HAL_QSPI_Abort(hqspi);
  840. }
  841. }
  842. /* Update QSPI state */
  843. hqspi->State = HAL_QSPI_STATE_READY;
  844. }
  845. else
  846. {
  847. hqspi->ErrorCode |= HAL_QSPI_ERROR_INVALID_PARAM;
  848. status = HAL_ERROR;
  849. }
  850. }
  851. else
  852. {
  853. status = HAL_BUSY;
  854. }
  855. /* Process unlocked */
  856. __HAL_UNLOCK(hqspi);
  857. return status;
  858. }
  859. /**
  860. * @brief Send an amount of data in interrupt mode
  861. * @param hqspi: QSPI handle
  862. * @param pData: pointer to data buffer
  863. * @note This function is used only in Indirect Write Mode
  864. * @retval HAL status
  865. */
  866. HAL_StatusTypeDef HAL_QSPI_Transmit_IT(QSPI_HandleTypeDef *hqspi, uint8_t *pData)
  867. {
  868. HAL_StatusTypeDef status = HAL_OK;
  869. /* Process locked */
  870. __HAL_LOCK(hqspi);
  871. if(hqspi->State == HAL_QSPI_STATE_READY)
  872. {
  873. hqspi->ErrorCode = HAL_QSPI_ERROR_NONE;
  874. if(pData != NULL )
  875. {
  876. /* Update state */
  877. hqspi->State = HAL_QSPI_STATE_BUSY_INDIRECT_TX;
  878. /* Configure counters and size of the handle */
  879. hqspi->TxXferCount = READ_REG(hqspi->Instance->DLR) + 1;
  880. hqspi->TxXferSize = READ_REG(hqspi->Instance->DLR) + 1;
  881. hqspi->pTxBuffPtr = pData;
  882. /* Configure QSPI: CCR register with functional as indirect write */
  883. MODIFY_REG(hqspi->Instance->CCR, QUADSPI_CCR_FMODE, QSPI_FUNCTIONAL_MODE_INDIRECT_WRITE);
  884. /* Clear interrupt */
  885. __HAL_QSPI_CLEAR_FLAG(hqspi, QSPI_FLAG_TE | QSPI_FLAG_TC);
  886. /* Process unlocked */
  887. __HAL_UNLOCK(hqspi);
  888. /* Enable the QSPI transfer error, FIFO threshold and transfer complete Interrupts */
  889. __HAL_QSPI_ENABLE_IT(hqspi, QSPI_IT_TE | QSPI_IT_FT | QSPI_IT_TC);
  890. }
  891. else
  892. {
  893. hqspi->ErrorCode |= HAL_QSPI_ERROR_INVALID_PARAM;
  894. status = HAL_ERROR;
  895. /* Process unlocked */
  896. __HAL_UNLOCK(hqspi);
  897. }
  898. }
  899. else
  900. {
  901. status = HAL_BUSY;
  902. /* Process unlocked */
  903. __HAL_UNLOCK(hqspi);
  904. }
  905. return status;
  906. }
  907. /**
  908. * @brief Receive an amount of data in no-blocking mode with Interrupt
  909. * @param hqspi: QSPI handle
  910. * @param pData: pointer to data buffer
  911. * @note This function is used only in Indirect Read Mode
  912. * @retval HAL status
  913. */
  914. HAL_StatusTypeDef HAL_QSPI_Receive_IT(QSPI_HandleTypeDef *hqspi, uint8_t *pData)
  915. {
  916. HAL_StatusTypeDef status = HAL_OK;
  917. uint32_t addr_reg = READ_REG(hqspi->Instance->AR);
  918. /* Process locked */
  919. __HAL_LOCK(hqspi);
  920. if(hqspi->State == HAL_QSPI_STATE_READY)
  921. {
  922. hqspi->ErrorCode = HAL_QSPI_ERROR_NONE;
  923. if(pData != NULL )
  924. {
  925. /* Update state */
  926. hqspi->State = HAL_QSPI_STATE_BUSY_INDIRECT_RX;
  927. /* Configure counters and size of the handle */
  928. hqspi->RxXferCount = READ_REG(hqspi->Instance->DLR) + 1;
  929. hqspi->RxXferSize = READ_REG(hqspi->Instance->DLR) + 1;
  930. hqspi->pRxBuffPtr = pData;
  931. /* Configure QSPI: CCR register with functional as indirect read */
  932. MODIFY_REG(hqspi->Instance->CCR, QUADSPI_CCR_FMODE, QSPI_FUNCTIONAL_MODE_INDIRECT_READ);
  933. /* Start the transfer by re-writing the address in AR register */
  934. WRITE_REG(hqspi->Instance->AR, addr_reg);
  935. /* Clear interrupt */
  936. __HAL_QSPI_CLEAR_FLAG(hqspi, QSPI_FLAG_TE | QSPI_FLAG_TC);
  937. /* Process unlocked */
  938. __HAL_UNLOCK(hqspi);
  939. /* Enable the QSPI transfer error, FIFO threshold and transfer complete Interrupts */
  940. __HAL_QSPI_ENABLE_IT(hqspi, QSPI_IT_TE | QSPI_IT_FT | QSPI_IT_TC);
  941. }
  942. else
  943. {
  944. hqspi->ErrorCode |= HAL_QSPI_ERROR_INVALID_PARAM;
  945. status = HAL_ERROR;
  946. /* Process unlocked */
  947. __HAL_UNLOCK(hqspi);
  948. }
  949. }
  950. else
  951. {
  952. status = HAL_BUSY;
  953. /* Process unlocked */
  954. __HAL_UNLOCK(hqspi);
  955. }
  956. return status;
  957. }
  958. /**
  959. * @brief Sends an amount of data in non blocking mode with DMA.
  960. * @param hqspi: QSPI handle
  961. * @param pData: pointer to data buffer
  962. * @note This function is used only in Indirect Write Mode
  963. * @note If DMA peripheral access is configured as halfword, the number
  964. * of data and the fifo threshold should be aligned on halfword
  965. * @note If DMA peripheral access is configured as word, the number
  966. * of data and the fifo threshold should be aligned on word
  967. * @retval HAL status
  968. */
  969. HAL_StatusTypeDef HAL_QSPI_Transmit_DMA(QSPI_HandleTypeDef *hqspi, uint8_t *pData)
  970. {
  971. HAL_StatusTypeDef status = HAL_OK;
  972. uint32_t *tmp;
  973. uint32_t data_size = (READ_REG(hqspi->Instance->DLR) + 1);
  974. /* Process locked */
  975. __HAL_LOCK(hqspi);
  976. if(hqspi->State == HAL_QSPI_STATE_READY)
  977. {
  978. /* Clear the error code */
  979. hqspi->ErrorCode = HAL_QSPI_ERROR_NONE;
  980. if(pData != NULL )
  981. {
  982. /* Configure counters of the handle */
  983. if (hqspi->hdma->Init.PeriphDataAlignment == DMA_PDATAALIGN_BYTE)
  984. {
  985. hqspi->TxXferCount = data_size;
  986. }
  987. else if (hqspi->hdma->Init.PeriphDataAlignment == DMA_PDATAALIGN_HALFWORD)
  988. {
  989. if (((data_size % 2) != 0) || ((hqspi->Init.FifoThreshold % 2) != 0))
  990. {
  991. /* The number of data or the fifo threshold is not aligned on halfword
  992. => no transfer possible with DMA peripheral access configured as halfword */
  993. hqspi->ErrorCode |= HAL_QSPI_ERROR_INVALID_PARAM;
  994. status = HAL_ERROR;
  995. /* Process unlocked */
  996. __HAL_UNLOCK(hqspi);
  997. }
  998. else
  999. {
  1000. hqspi->TxXferCount = (data_size >> 1);
  1001. }
  1002. }
  1003. else if (hqspi->hdma->Init.PeriphDataAlignment == DMA_PDATAALIGN_WORD)
  1004. {
  1005. if (((data_size % 4) != 0) || ((hqspi->Init.FifoThreshold % 4) != 0))
  1006. {
  1007. /* The number of data or the fifo threshold is not aligned on word
  1008. => no transfer possible with DMA peripheral access configured as word */
  1009. hqspi->ErrorCode |= HAL_QSPI_ERROR_INVALID_PARAM;
  1010. status = HAL_ERROR;
  1011. /* Process unlocked */
  1012. __HAL_UNLOCK(hqspi);
  1013. }
  1014. else
  1015. {
  1016. hqspi->TxXferCount = (data_size >> 2);
  1017. }
  1018. }
  1019. if (status == HAL_OK)
  1020. {
  1021. /* Update state */
  1022. hqspi->State = HAL_QSPI_STATE_BUSY_INDIRECT_TX;
  1023. /* Clear interrupt */
  1024. __HAL_QSPI_CLEAR_FLAG(hqspi, (QSPI_FLAG_TE | QSPI_FLAG_TC));
  1025. /* Configure size and pointer of the handle */
  1026. hqspi->TxXferSize = hqspi->TxXferCount;
  1027. hqspi->pTxBuffPtr = pData;
  1028. /* Configure QSPI: CCR register with functional mode as indirect write */
  1029. MODIFY_REG(hqspi->Instance->CCR, QUADSPI_CCR_FMODE, QSPI_FUNCTIONAL_MODE_INDIRECT_WRITE);
  1030. /* Set the QSPI DMA transfer complete callback */
  1031. hqspi->hdma->XferCpltCallback = QSPI_DMATxCplt;
  1032. /* Set the QSPI DMA Half transfer complete callback */
  1033. hqspi->hdma->XferHalfCpltCallback = QSPI_DMATxHalfCplt;
  1034. /* Set the DMA error callback */
  1035. hqspi->hdma->XferErrorCallback = QSPI_DMAError;
  1036. /* Clear the DMA abort callback */
  1037. hqspi->hdma->XferAbortCallback = NULL;
  1038. #if defined (QSPI1_V2_1L)
  1039. /* Bug "ES0305 section 2.1.8 In some specific cases, DMA2 data corruption occurs when managing
  1040. AHB and APB2 peripherals in a concurrent way" Workaround Implementation:
  1041. Change the following configuration of DMA peripheral
  1042. - Enable peripheral increment
  1043. - Disable memory increment
  1044. - Set DMA direction as peripheral to memory mode */
  1045. /* Enable peripheral increment mode of the DMA */
  1046. hqspi->hdma->Init.PeriphInc = DMA_PINC_ENABLE;
  1047. /* Disable memory increment mode of the DMA */
  1048. hqspi->hdma->Init.MemInc = DMA_MINC_DISABLE;
  1049. /* Update peripheral/memory increment mode bits */
  1050. MODIFY_REG(hqspi->hdma->Instance->CR, (DMA_SxCR_MINC | DMA_SxCR_PINC), (hqspi->hdma->Init.MemInc | hqspi->hdma->Init.PeriphInc));
  1051. /* Configure the direction of the DMA */
  1052. hqspi->hdma->Init.Direction = DMA_PERIPH_TO_MEMORY;
  1053. #else
  1054. /* Configure the direction of the DMA */
  1055. hqspi->hdma->Init.Direction = DMA_MEMORY_TO_PERIPH;
  1056. #endif /* QSPI1_V2_1L */
  1057. /* Update direction mode bit */
  1058. MODIFY_REG(hqspi->hdma->Instance->CR, DMA_SxCR_DIR, hqspi->hdma->Init.Direction);
  1059. /* Enable the QSPI transmit DMA Channel */
  1060. tmp = (uint32_t*)&pData;
  1061. HAL_DMA_Start_IT(hqspi->hdma, *(uint32_t*)tmp, (uint32_t)&hqspi->Instance->DR, hqspi->TxXferSize);
  1062. /* Process unlocked */
  1063. __HAL_UNLOCK(hqspi);
  1064. /* Enable the QSPI transfer error Interrupt */
  1065. __HAL_QSPI_ENABLE_IT(hqspi, QSPI_IT_TE);
  1066. /* Enable the DMA transfer by setting the DMAEN bit in the QSPI CR register */
  1067. SET_BIT(hqspi->Instance->CR, QUADSPI_CR_DMAEN);
  1068. }
  1069. }
  1070. else
  1071. {
  1072. hqspi->ErrorCode |= HAL_QSPI_ERROR_INVALID_PARAM;
  1073. status = HAL_ERROR;
  1074. /* Process unlocked */
  1075. __HAL_UNLOCK(hqspi);
  1076. }
  1077. }
  1078. else
  1079. {
  1080. status = HAL_BUSY;
  1081. /* Process unlocked */
  1082. __HAL_UNLOCK(hqspi);
  1083. }
  1084. return status;
  1085. }
  1086. /**
  1087. * @brief Receives an amount of data in non blocking mode with DMA.
  1088. * @param hqspi: QSPI handle
  1089. * @param pData: pointer to data buffer.
  1090. * @note This function is used only in Indirect Read Mode
  1091. * @note If DMA peripheral access is configured as halfword, the number
  1092. * of data and the fifo threshold should be aligned on halfword
  1093. * @note If DMA peripheral access is configured as word, the number
  1094. * of data and the fifo threshold should be aligned on word
  1095. * @retval HAL status
  1096. */
  1097. HAL_StatusTypeDef HAL_QSPI_Receive_DMA(QSPI_HandleTypeDef *hqspi, uint8_t *pData)
  1098. {
  1099. HAL_StatusTypeDef status = HAL_OK;
  1100. uint32_t *tmp;
  1101. uint32_t addr_reg = READ_REG(hqspi->Instance->AR);
  1102. uint32_t data_size = (READ_REG(hqspi->Instance->DLR) + 1);
  1103. /* Process locked */
  1104. __HAL_LOCK(hqspi);
  1105. if(hqspi->State == HAL_QSPI_STATE_READY)
  1106. {
  1107. hqspi->ErrorCode = HAL_QSPI_ERROR_NONE;
  1108. if(pData != NULL )
  1109. {
  1110. /* Configure counters of the handle */
  1111. if (hqspi->hdma->Init.PeriphDataAlignment == DMA_PDATAALIGN_BYTE)
  1112. {
  1113. hqspi->RxXferCount = data_size;
  1114. }
  1115. else if (hqspi->hdma->Init.PeriphDataAlignment == DMA_PDATAALIGN_HALFWORD)
  1116. {
  1117. if (((data_size % 2) != 0) || ((hqspi->Init.FifoThreshold % 2) != 0))
  1118. {
  1119. /* The number of data or the fifo threshold is not aligned on halfword
  1120. => no transfer possible with DMA peripheral access configured as halfword */
  1121. hqspi->ErrorCode |= HAL_QSPI_ERROR_INVALID_PARAM;
  1122. status = HAL_ERROR;
  1123. /* Process unlocked */
  1124. __HAL_UNLOCK(hqspi);
  1125. }
  1126. else
  1127. {
  1128. hqspi->RxXferCount = (data_size >> 1);
  1129. }
  1130. }
  1131. else if (hqspi->hdma->Init.PeriphDataAlignment == DMA_PDATAALIGN_WORD)
  1132. {
  1133. if (((data_size % 4) != 0) || ((hqspi->Init.FifoThreshold % 4) != 0))
  1134. {
  1135. /* The number of data or the fifo threshold is not aligned on word
  1136. => no transfer possible with DMA peripheral access configured as word */
  1137. hqspi->ErrorCode |= HAL_QSPI_ERROR_INVALID_PARAM;
  1138. status = HAL_ERROR;
  1139. /* Process unlocked */
  1140. __HAL_UNLOCK(hqspi);
  1141. }
  1142. else
  1143. {
  1144. hqspi->RxXferCount = (data_size >> 2);
  1145. }
  1146. }
  1147. if (status == HAL_OK)
  1148. {
  1149. /* Update state */
  1150. hqspi->State = HAL_QSPI_STATE_BUSY_INDIRECT_RX;
  1151. /* Clear interrupt */
  1152. __HAL_QSPI_CLEAR_FLAG(hqspi, (QSPI_FLAG_TE | QSPI_FLAG_TC));
  1153. /* Configure size and pointer of the handle */
  1154. hqspi->RxXferSize = hqspi->RxXferCount;
  1155. hqspi->pRxBuffPtr = pData;
  1156. /* Set the QSPI DMA transfer complete callback */
  1157. hqspi->hdma->XferCpltCallback = QSPI_DMARxCplt;
  1158. /* Set the QSPI DMA Half transfer complete callback */
  1159. hqspi->hdma->XferHalfCpltCallback = QSPI_DMARxHalfCplt;
  1160. /* Set the DMA error callback */
  1161. hqspi->hdma->XferErrorCallback = QSPI_DMAError;
  1162. /* Clear the DMA abort callback */
  1163. hqspi->hdma->XferAbortCallback = NULL;
  1164. #if defined (QSPI1_V2_1L)
  1165. /* Bug "ES0305 section 2.1.8 In some specific cases, DMA2 data corruption occurs when managing
  1166. AHB and APB2 peripherals in a concurrent way" Workaround Implementation:
  1167. Change the following configuration of DMA peripheral
  1168. - Enable peripheral increment
  1169. - Disable memory increment
  1170. - Set DMA direction as memory to peripheral mode
  1171. - 4 Extra words (32-bits) are added for read operation to guarantee
  1172. the last data is transferred from DMA FIFO to RAM memory */
  1173. /* Enable peripheral increment of the DMA */
  1174. hqspi->hdma->Init.PeriphInc = DMA_PINC_ENABLE;
  1175. /* Disable memory increment of the DMA */
  1176. hqspi->hdma->Init.MemInc = DMA_MINC_DISABLE;
  1177. /* Update peripheral/memory increment mode bits */
  1178. MODIFY_REG(hqspi->hdma->Instance->CR, (DMA_SxCR_MINC | DMA_SxCR_PINC), (hqspi->hdma->Init.MemInc | hqspi->hdma->Init.PeriphInc));
  1179. /* Configure the direction of the DMA */
  1180. hqspi->hdma->Init.Direction = DMA_MEMORY_TO_PERIPH;
  1181. /* 4 Extra words (32-bits) are needed for read operation to guarantee
  1182. the last data is transferred from DMA FIFO to RAM memory */
  1183. WRITE_REG(hqspi->Instance->DLR, (data_size - 1 + 16));
  1184. /* Update direction mode bit */
  1185. MODIFY_REG(hqspi->hdma->Instance->CR, DMA_SxCR_DIR, hqspi->hdma->Init.Direction);
  1186. /* Configure QSPI: CCR register with functional as indirect read */
  1187. MODIFY_REG(hqspi->Instance->CCR, QUADSPI_CCR_FMODE, QSPI_FUNCTIONAL_MODE_INDIRECT_READ);
  1188. /* Start the transfer by re-writing the address in AR register */
  1189. WRITE_REG(hqspi->Instance->AR, addr_reg);
  1190. /* Enable the DMA Channel */
  1191. tmp = (uint32_t*)&pData;
  1192. HAL_DMA_Start_IT(hqspi->hdma, (uint32_t)&hqspi->Instance->DR, *(uint32_t*)tmp, hqspi->RxXferSize);
  1193. /* Enable the DMA transfer by setting the DMAEN bit in the QSPI CR register */
  1194. SET_BIT(hqspi->Instance->CR, QUADSPI_CR_DMAEN);
  1195. /* Process unlocked */
  1196. __HAL_UNLOCK(hqspi);
  1197. /* Enable the QSPI transfer error Interrupt */
  1198. __HAL_QSPI_ENABLE_IT(hqspi, QSPI_IT_TE);
  1199. #else
  1200. /* Configure the direction of the DMA */
  1201. hqspi->hdma->Init.Direction = DMA_PERIPH_TO_MEMORY;
  1202. MODIFY_REG(hqspi->hdma->Instance->CR, DMA_SxCR_DIR, hqspi->hdma->Init.Direction);
  1203. /* Enable the DMA Channel */
  1204. tmp = (uint32_t*)&pData;
  1205. HAL_DMA_Start_IT(hqspi->hdma, (uint32_t)&hqspi->Instance->DR, *(uint32_t*)tmp, hqspi->RxXferSize);
  1206. /* Configure QSPI: CCR register with functional as indirect read */
  1207. MODIFY_REG(hqspi->Instance->CCR, QUADSPI_CCR_FMODE, QSPI_FUNCTIONAL_MODE_INDIRECT_READ);
  1208. /* Start the transfer by re-writing the address in AR register */
  1209. WRITE_REG(hqspi->Instance->AR, addr_reg);
  1210. /* Process unlocked */
  1211. __HAL_UNLOCK(hqspi);
  1212. /* Enable the QSPI transfer error Interrupt */
  1213. __HAL_QSPI_ENABLE_IT(hqspi, QSPI_IT_TE);
  1214. /* Enable the DMA transfer by setting the DMAEN bit in the QSPI CR register */
  1215. SET_BIT(hqspi->Instance->CR, QUADSPI_CR_DMAEN);
  1216. #endif /* QSPI1_V2_1L */
  1217. }
  1218. }
  1219. else
  1220. {
  1221. hqspi->ErrorCode |= HAL_QSPI_ERROR_INVALID_PARAM;
  1222. status = HAL_ERROR;
  1223. /* Process unlocked */
  1224. __HAL_UNLOCK(hqspi);
  1225. }
  1226. }
  1227. else
  1228. {
  1229. status = HAL_BUSY;
  1230. /* Process unlocked */
  1231. __HAL_UNLOCK(hqspi);
  1232. }
  1233. return status;
  1234. }
  1235. /**
  1236. * @brief Configure the QSPI Automatic Polling Mode in blocking mode.
  1237. * @param hqspi: QSPI handle
  1238. * @param cmd: structure that contains the command configuration information.
  1239. * @param cfg: structure that contains the polling configuration information.
  1240. * @param Timeout : Time out duration
  1241. * @note This function is used only in Automatic Polling Mode
  1242. * @retval HAL status
  1243. */
  1244. HAL_StatusTypeDef HAL_QSPI_AutoPolling(QSPI_HandleTypeDef *hqspi, QSPI_CommandTypeDef *cmd, QSPI_AutoPollingTypeDef *cfg, uint32_t Timeout)
  1245. {
  1246. HAL_StatusTypeDef status = HAL_ERROR;
  1247. uint32_t tickstart = HAL_GetTick();
  1248. /* Check the parameters */
  1249. assert_param(IS_QSPI_INSTRUCTION_MODE(cmd->InstructionMode));
  1250. if (cmd->InstructionMode != QSPI_INSTRUCTION_NONE)
  1251. {
  1252. assert_param(IS_QSPI_INSTRUCTION(cmd->Instruction));
  1253. }
  1254. assert_param(IS_QSPI_ADDRESS_MODE(cmd->AddressMode));
  1255. if (cmd->AddressMode != QSPI_ADDRESS_NONE)
  1256. {
  1257. assert_param(IS_QSPI_ADDRESS_SIZE(cmd->AddressSize));
  1258. }
  1259. assert_param(IS_QSPI_ALTERNATE_BYTES_MODE(cmd->AlternateByteMode));
  1260. if (cmd->AlternateByteMode != QSPI_ALTERNATE_BYTES_NONE)
  1261. {
  1262. assert_param(IS_QSPI_ALTERNATE_BYTES_SIZE(cmd->AlternateBytesSize));
  1263. }
  1264. assert_param(IS_QSPI_DUMMY_CYCLES(cmd->DummyCycles));
  1265. assert_param(IS_QSPI_DATA_MODE(cmd->DataMode));
  1266. assert_param(IS_QSPI_DDR_MODE(cmd->DdrMode));
  1267. assert_param(IS_QSPI_DDR_HHC(cmd->DdrHoldHalfCycle));
  1268. assert_param(IS_QSPI_SIOO_MODE(cmd->SIOOMode));
  1269. assert_param(IS_QSPI_INTERVAL(cfg->Interval));
  1270. assert_param(IS_QSPI_STATUS_BYTES_SIZE(cfg->StatusBytesSize));
  1271. assert_param(IS_QSPI_MATCH_MODE(cfg->MatchMode));
  1272. /* Process locked */
  1273. __HAL_LOCK(hqspi);
  1274. if(hqspi->State == HAL_QSPI_STATE_READY)
  1275. {
  1276. hqspi->ErrorCode = HAL_QSPI_ERROR_NONE;
  1277. /* Update state */
  1278. hqspi->State = HAL_QSPI_STATE_BUSY_AUTO_POLLING;
  1279. /* Wait till BUSY flag reset */
  1280. status = QSPI_WaitFlagStateUntilTimeout(hqspi, QSPI_FLAG_BUSY, RESET, tickstart, Timeout);
  1281. if (status == HAL_OK)
  1282. {
  1283. /* Configure QSPI: PSMAR register with the status match value */
  1284. WRITE_REG(hqspi->Instance->PSMAR, cfg->Match);
  1285. /* Configure QSPI: PSMKR register with the status mask value */
  1286. WRITE_REG(hqspi->Instance->PSMKR, cfg->Mask);
  1287. /* Configure QSPI: PIR register with the interval value */
  1288. WRITE_REG(hqspi->Instance->PIR, cfg->Interval);
  1289. /* Configure QSPI: CR register with Match mode and Automatic stop enabled
  1290. (otherwise there will be an infinite loop in blocking mode) */
  1291. MODIFY_REG(hqspi->Instance->CR, (QUADSPI_CR_PMM | QUADSPI_CR_APMS),
  1292. (cfg->MatchMode | QSPI_AUTOMATIC_STOP_ENABLE));
  1293. /* Call the configuration function */
  1294. cmd->NbData = cfg->StatusBytesSize;
  1295. QSPI_Config(hqspi, cmd, QSPI_FUNCTIONAL_MODE_AUTO_POLLING);
  1296. /* Wait until SM flag is set to go back in idle state */
  1297. status = QSPI_WaitFlagStateUntilTimeout(hqspi, QSPI_FLAG_SM, SET, tickstart, Timeout);
  1298. if (status == HAL_OK)
  1299. {
  1300. __HAL_QSPI_CLEAR_FLAG(hqspi, QSPI_FLAG_SM);
  1301. /* Update state */
  1302. hqspi->State = HAL_QSPI_STATE_READY;
  1303. }
  1304. }
  1305. }
  1306. else
  1307. {
  1308. status = HAL_BUSY;
  1309. }
  1310. /* Process unlocked */
  1311. __HAL_UNLOCK(hqspi);
  1312. /* Return function status */
  1313. return status;
  1314. }
  1315. /**
  1316. * @brief Configure the QSPI Automatic Polling Mode in non-blocking mode.
  1317. * @param hqspi: QSPI handle
  1318. * @param cmd: structure that contains the command configuration information.
  1319. * @param cfg: structure that contains the polling configuration information.
  1320. * @note This function is used only in Automatic Polling Mode
  1321. * @retval HAL status
  1322. */
  1323. HAL_StatusTypeDef HAL_QSPI_AutoPolling_IT(QSPI_HandleTypeDef *hqspi, QSPI_CommandTypeDef *cmd, QSPI_AutoPollingTypeDef *cfg)
  1324. {
  1325. __IO uint32_t count = 0U;
  1326. HAL_StatusTypeDef status = HAL_OK;
  1327. /* Check the parameters */
  1328. assert_param(IS_QSPI_INSTRUCTION_MODE(cmd->InstructionMode));
  1329. if (cmd->InstructionMode != QSPI_INSTRUCTION_NONE)
  1330. {
  1331. assert_param(IS_QSPI_INSTRUCTION(cmd->Instruction));
  1332. }
  1333. assert_param(IS_QSPI_ADDRESS_MODE(cmd->AddressMode));
  1334. if (cmd->AddressMode != QSPI_ADDRESS_NONE)
  1335. {
  1336. assert_param(IS_QSPI_ADDRESS_SIZE(cmd->AddressSize));
  1337. }
  1338. assert_param(IS_QSPI_ALTERNATE_BYTES_MODE(cmd->AlternateByteMode));
  1339. if (cmd->AlternateByteMode != QSPI_ALTERNATE_BYTES_NONE)
  1340. {
  1341. assert_param(IS_QSPI_ALTERNATE_BYTES_SIZE(cmd->AlternateBytesSize));
  1342. }
  1343. assert_param(IS_QSPI_DUMMY_CYCLES(cmd->DummyCycles));
  1344. assert_param(IS_QSPI_DATA_MODE(cmd->DataMode));
  1345. assert_param(IS_QSPI_DDR_MODE(cmd->DdrMode));
  1346. assert_param(IS_QSPI_DDR_HHC(cmd->DdrHoldHalfCycle));
  1347. assert_param(IS_QSPI_SIOO_MODE(cmd->SIOOMode));
  1348. assert_param(IS_QSPI_INTERVAL(cfg->Interval));
  1349. assert_param(IS_QSPI_STATUS_BYTES_SIZE(cfg->StatusBytesSize));
  1350. assert_param(IS_QSPI_MATCH_MODE(cfg->MatchMode));
  1351. assert_param(IS_QSPI_AUTOMATIC_STOP(cfg->AutomaticStop));
  1352. /* Process locked */
  1353. __HAL_LOCK(hqspi);
  1354. if(hqspi->State == HAL_QSPI_STATE_READY)
  1355. {
  1356. hqspi->ErrorCode = HAL_QSPI_ERROR_NONE;
  1357. /* Update state */
  1358. hqspi->State = HAL_QSPI_STATE_BUSY_AUTO_POLLING;
  1359. /* Wait till BUSY flag reset */
  1360. count = (hqspi->Timeout) * (SystemCoreClock / 16U / 1000U);
  1361. do
  1362. {
  1363. if (count-- == 0)
  1364. {
  1365. hqspi->State = HAL_QSPI_STATE_ERROR;
  1366. hqspi->ErrorCode |= HAL_QSPI_ERROR_TIMEOUT;
  1367. status = HAL_TIMEOUT;
  1368. }
  1369. }
  1370. while ((__HAL_QSPI_GET_FLAG(hqspi, QSPI_FLAG_BUSY)) != RESET);
  1371. if (status == HAL_OK)
  1372. {
  1373. /* Configure QSPI: PSMAR register with the status match value */
  1374. WRITE_REG(hqspi->Instance->PSMAR, cfg->Match);
  1375. /* Configure QSPI: PSMKR register with the status mask value */
  1376. WRITE_REG(hqspi->Instance->PSMKR, cfg->Mask);
  1377. /* Configure QSPI: PIR register with the interval value */
  1378. WRITE_REG(hqspi->Instance->PIR, cfg->Interval);
  1379. /* Configure QSPI: CR register with Match mode and Automatic stop mode */
  1380. MODIFY_REG(hqspi->Instance->CR, (QUADSPI_CR_PMM | QUADSPI_CR_APMS),
  1381. (cfg->MatchMode | cfg->AutomaticStop));
  1382. /* Clear interrupt */
  1383. __HAL_QSPI_CLEAR_FLAG(hqspi, QSPI_FLAG_TE | QSPI_FLAG_SM);
  1384. /* Call the configuration function */
  1385. cmd->NbData = cfg->StatusBytesSize;
  1386. QSPI_Config(hqspi, cmd, QSPI_FUNCTIONAL_MODE_AUTO_POLLING);
  1387. /* Process unlocked */
  1388. __HAL_UNLOCK(hqspi);
  1389. /* Enable the QSPI Transfer Error and status match Interrupt */
  1390. __HAL_QSPI_ENABLE_IT(hqspi, (QSPI_IT_SM | QSPI_IT_TE));
  1391. }
  1392. else
  1393. {
  1394. /* Process unlocked */
  1395. __HAL_UNLOCK(hqspi);
  1396. }
  1397. }
  1398. else
  1399. {
  1400. status = HAL_BUSY;
  1401. /* Process unlocked */
  1402. __HAL_UNLOCK(hqspi);
  1403. }
  1404. /* Return function status */
  1405. return status;
  1406. }
  1407. /**
  1408. * @brief Configure the Memory Mapped mode.
  1409. * @param hqspi: QSPI handle
  1410. * @param cmd: structure that contains the command configuration information.
  1411. * @param cfg: structure that contains the memory mapped configuration information.
  1412. * @note This function is used only in Memory mapped Mode
  1413. * @retval HAL status
  1414. */
  1415. HAL_StatusTypeDef HAL_QSPI_MemoryMapped(QSPI_HandleTypeDef *hqspi, QSPI_CommandTypeDef *cmd, QSPI_MemoryMappedTypeDef *cfg)
  1416. {
  1417. HAL_StatusTypeDef status = HAL_ERROR;
  1418. uint32_t tickstart = HAL_GetTick();
  1419. /* Check the parameters */
  1420. assert_param(IS_QSPI_INSTRUCTION_MODE(cmd->InstructionMode));
  1421. if (cmd->InstructionMode != QSPI_INSTRUCTION_NONE)
  1422. {
  1423. assert_param(IS_QSPI_INSTRUCTION(cmd->Instruction));
  1424. }
  1425. assert_param(IS_QSPI_ADDRESS_MODE(cmd->AddressMode));
  1426. if (cmd->AddressMode != QSPI_ADDRESS_NONE)
  1427. {
  1428. assert_param(IS_QSPI_ADDRESS_SIZE(cmd->AddressSize));
  1429. }
  1430. assert_param(IS_QSPI_ALTERNATE_BYTES_MODE(cmd->AlternateByteMode));
  1431. if (cmd->AlternateByteMode != QSPI_ALTERNATE_BYTES_NONE)
  1432. {
  1433. assert_param(IS_QSPI_ALTERNATE_BYTES_SIZE(cmd->AlternateBytesSize));
  1434. }
  1435. assert_param(IS_QSPI_DUMMY_CYCLES(cmd->DummyCycles));
  1436. assert_param(IS_QSPI_DATA_MODE(cmd->DataMode));
  1437. assert_param(IS_QSPI_DDR_MODE(cmd->DdrMode));
  1438. assert_param(IS_QSPI_DDR_HHC(cmd->DdrHoldHalfCycle));
  1439. assert_param(IS_QSPI_SIOO_MODE(cmd->SIOOMode));
  1440. assert_param(IS_QSPI_TIMEOUT_ACTIVATION(cfg->TimeOutActivation));
  1441. /* Process locked */
  1442. __HAL_LOCK(hqspi);
  1443. if(hqspi->State == HAL_QSPI_STATE_READY)
  1444. {
  1445. hqspi->ErrorCode = HAL_QSPI_ERROR_NONE;
  1446. /* Update state */
  1447. hqspi->State = HAL_QSPI_STATE_BUSY_MEM_MAPPED;
  1448. /* Wait till BUSY flag reset */
  1449. status = QSPI_WaitFlagStateUntilTimeout(hqspi, QSPI_FLAG_BUSY, RESET, tickstart, hqspi->Timeout);
  1450. if (status == HAL_OK)
  1451. {
  1452. /* Configure QSPI: CR register with timeout counter enable */
  1453. MODIFY_REG(hqspi->Instance->CR, QUADSPI_CR_TCEN, cfg->TimeOutActivation);
  1454. if (cfg->TimeOutActivation == QSPI_TIMEOUT_COUNTER_ENABLE)
  1455. {
  1456. assert_param(IS_QSPI_TIMEOUT_PERIOD(cfg->TimeOutPeriod));
  1457. /* Configure QSPI: LPTR register with the low-power timeout value */
  1458. WRITE_REG(hqspi->Instance->LPTR, cfg->TimeOutPeriod);
  1459. /* Clear interrupt */
  1460. __HAL_QSPI_CLEAR_FLAG(hqspi, QSPI_FLAG_TO);
  1461. /* Enable the QSPI TimeOut Interrupt */
  1462. __HAL_QSPI_ENABLE_IT(hqspi, QSPI_IT_TO);
  1463. }
  1464. /* Call the configuration function */
  1465. QSPI_Config(hqspi, cmd, QSPI_FUNCTIONAL_MODE_MEMORY_MAPPED);
  1466. }
  1467. }
  1468. else
  1469. {
  1470. status = HAL_BUSY;
  1471. }
  1472. /* Process unlocked */
  1473. __HAL_UNLOCK(hqspi);
  1474. /* Return function status */
  1475. return status;
  1476. }
  1477. /**
  1478. * @brief Transfer Error callbacks
  1479. * @param hqspi: QSPI handle
  1480. * @retval None
  1481. */
  1482. __weak void HAL_QSPI_ErrorCallback(QSPI_HandleTypeDef *hqspi)
  1483. {
  1484. /* Prevent unused argument(s) compilation warning */
  1485. UNUSED(hqspi);
  1486. /* NOTE : This function Should not be modified, when the callback is needed,
  1487. the HAL_QSPI_ErrorCallback could be implemented in the user file
  1488. */
  1489. }
  1490. /**
  1491. * @brief Abort completed callback.
  1492. * @param hqspi: QSPI handle
  1493. * @retval None
  1494. */
  1495. __weak void HAL_QSPI_AbortCpltCallback(QSPI_HandleTypeDef *hqspi)
  1496. {
  1497. /* Prevent unused argument(s) compilation warning */
  1498. UNUSED(hqspi);
  1499. /* NOTE: This function should not be modified, when the callback is needed,
  1500. the HAL_QSPI_AbortCpltCallback could be implemented in the user file
  1501. */
  1502. }
  1503. /**
  1504. * @brief Command completed callback.
  1505. * @param hqspi: QSPI handle
  1506. * @retval None
  1507. */
  1508. __weak void HAL_QSPI_CmdCpltCallback(QSPI_HandleTypeDef *hqspi)
  1509. {
  1510. /* Prevent unused argument(s) compilation warning */
  1511. UNUSED(hqspi);
  1512. /* NOTE: This function Should not be modified, when the callback is needed,
  1513. the HAL_QSPI_CmdCpltCallback could be implemented in the user file
  1514. */
  1515. }
  1516. /**
  1517. * @brief Rx Transfer completed callbacks.
  1518. * @param hqspi: QSPI handle
  1519. * @retval None
  1520. */
  1521. __weak void HAL_QSPI_RxCpltCallback(QSPI_HandleTypeDef *hqspi)
  1522. {
  1523. /* Prevent unused argument(s) compilation warning */
  1524. UNUSED(hqspi);
  1525. /* NOTE: This function Should not be modified, when the callback is needed,
  1526. the HAL_QSPI_RxCpltCallback could be implemented in the user file
  1527. */
  1528. }
  1529. /**
  1530. * @brief Tx Transfer completed callbacks.
  1531. * @param hqspi: QSPI handle
  1532. * @retval None
  1533. */
  1534. __weak void HAL_QSPI_TxCpltCallback(QSPI_HandleTypeDef *hqspi)
  1535. {
  1536. /* Prevent unused argument(s) compilation warning */
  1537. UNUSED(hqspi);
  1538. /* NOTE: This function Should not be modified, when the callback is needed,
  1539. the HAL_QSPI_TxCpltCallback could be implemented in the user file
  1540. */
  1541. }
  1542. /**
  1543. * @brief Rx Half Transfer completed callbacks.
  1544. * @param hqspi: QSPI handle
  1545. * @retval None
  1546. */
  1547. __weak void HAL_QSPI_RxHalfCpltCallback(QSPI_HandleTypeDef *hqspi)
  1548. {
  1549. /* Prevent unused argument(s) compilation warning */
  1550. UNUSED(hqspi);
  1551. /* NOTE: This function Should not be modified, when the callback is needed,
  1552. the HAL_QSPI_RxHalfCpltCallback could be implemented in the user file
  1553. */
  1554. }
  1555. /**
  1556. * @brief Tx Half Transfer completed callbacks.
  1557. * @param hqspi: QSPI handle
  1558. * @retval None
  1559. */
  1560. __weak void HAL_QSPI_TxHalfCpltCallback(QSPI_HandleTypeDef *hqspi)
  1561. {
  1562. /* Prevent unused argument(s) compilation warning */
  1563. UNUSED(hqspi);
  1564. /* NOTE: This function Should not be modified, when the callback is needed,
  1565. the HAL_QSPI_TxHalfCpltCallback could be implemented in the user file
  1566. */
  1567. }
  1568. /**
  1569. * @brief FIFO Threshold callbacks
  1570. * @param hqspi: QSPI handle
  1571. * @retval None
  1572. */
  1573. __weak void HAL_QSPI_FifoThresholdCallback(QSPI_HandleTypeDef *hqspi)
  1574. {
  1575. /* Prevent unused argument(s) compilation warning */
  1576. UNUSED(hqspi);
  1577. /* NOTE : This function Should not be modified, when the callback is needed,
  1578. the HAL_QSPI_FIFOThresholdCallback could be implemented in the user file
  1579. */
  1580. }
  1581. /**
  1582. * @brief Status Match callbacks
  1583. * @param hqspi: QSPI handle
  1584. * @retval None
  1585. */
  1586. __weak void HAL_QSPI_StatusMatchCallback(QSPI_HandleTypeDef *hqspi)
  1587. {
  1588. /* Prevent unused argument(s) compilation warning */
  1589. UNUSED(hqspi);
  1590. /* NOTE : This function Should not be modified, when the callback is needed,
  1591. the HAL_QSPI_StatusMatchCallback could be implemented in the user file
  1592. */
  1593. }
  1594. /**
  1595. * @brief Timeout callbacks
  1596. * @param hqspi: QSPI handle
  1597. * @retval None
  1598. */
  1599. __weak void HAL_QSPI_TimeOutCallback(QSPI_HandleTypeDef *hqspi)
  1600. {
  1601. /* Prevent unused argument(s) compilation warning */
  1602. UNUSED(hqspi);
  1603. /* NOTE : This function Should not be modified, when the callback is needed,
  1604. the HAL_QSPI_TimeOutCallback could be implemented in the user file
  1605. */
  1606. }
  1607. /**
  1608. * @}
  1609. */
  1610. /** @defgroup QSPI_Exported_Functions_Group3 Peripheral Control and State functions
  1611. * @brief QSPI control and State functions
  1612. *
  1613. @verbatim
  1614. ===============================================================================
  1615. ##### Peripheral Control and State functions #####
  1616. ===============================================================================
  1617. [..]
  1618. This subsection provides a set of functions allowing to :
  1619. (+) Check in run-time the state of the driver.
  1620. (+) Check the error code set during last operation.
  1621. (+) Abort any operation.
  1622. @endverbatim
  1623. * @{
  1624. */
  1625. /**
  1626. * @brief Return the QSPI handle state.
  1627. * @param hqspi: QSPI handle
  1628. * @retval HAL state
  1629. */
  1630. HAL_QSPI_StateTypeDef HAL_QSPI_GetState(QSPI_HandleTypeDef *hqspi)
  1631. {
  1632. /* Return QSPI handle state */
  1633. return hqspi->State;
  1634. }
  1635. /**
  1636. * @brief Return the QSPI error code
  1637. * @param hqspi: QSPI handle
  1638. * @retval QSPI Error Code
  1639. */
  1640. uint32_t HAL_QSPI_GetError(QSPI_HandleTypeDef *hqspi)
  1641. {
  1642. return hqspi->ErrorCode;
  1643. }
  1644. /**
  1645. * @brief Abort the current transmission
  1646. * @param hqspi: QSPI handle
  1647. * @retval HAL status
  1648. */
  1649. HAL_StatusTypeDef HAL_QSPI_Abort(QSPI_HandleTypeDef *hqspi)
  1650. {
  1651. HAL_StatusTypeDef status = HAL_OK;
  1652. uint32_t tickstart = HAL_GetTick();
  1653. /* Check if the state is in one of the busy states */
  1654. if ((hqspi->State & 0x2) != 0)
  1655. {
  1656. /* Process unlocked */
  1657. __HAL_UNLOCK(hqspi);
  1658. if ((hqspi->Instance->CR & QUADSPI_CR_DMAEN)!= RESET)
  1659. {
  1660. /* Disable the DMA transfer by clearing the DMAEN bit in the QSPI CR register */
  1661. CLEAR_BIT(hqspi->Instance->CR, QUADSPI_CR_DMAEN);
  1662. /* Abort DMA channel */
  1663. status = HAL_DMA_Abort(hqspi->hdma);
  1664. if(status != HAL_OK)
  1665. {
  1666. hqspi->ErrorCode |= HAL_QSPI_ERROR_DMA;
  1667. }
  1668. }
  1669. /* Configure QSPI: CR register with Abort request */
  1670. SET_BIT(hqspi->Instance->CR, QUADSPI_CR_ABORT);
  1671. /* Wait until TC flag is set to go back in idle state */
  1672. status = QSPI_WaitFlagStateUntilTimeout(hqspi, QSPI_FLAG_TC, SET, tickstart, hqspi->Timeout);
  1673. if(status == HAL_OK)
  1674. {
  1675. __HAL_QSPI_CLEAR_FLAG(hqspi, QSPI_FLAG_TC);
  1676. /* Wait until BUSY flag is reset */
  1677. status = QSPI_WaitFlagStateUntilTimeout(hqspi, QSPI_FLAG_BUSY, RESET, tickstart, hqspi->Timeout);
  1678. }
  1679. if (status == HAL_OK)
  1680. {
  1681. /* Update state */
  1682. hqspi->State = HAL_QSPI_STATE_READY;
  1683. }
  1684. }
  1685. return status;
  1686. }
  1687. /**
  1688. * @brief Abort the current transmission (non-blocking function)
  1689. * @param hqspi: QSPI handle
  1690. * @retval HAL status
  1691. */
  1692. HAL_StatusTypeDef HAL_QSPI_Abort_IT(QSPI_HandleTypeDef *hqspi)
  1693. {
  1694. HAL_StatusTypeDef status = HAL_OK;
  1695. /* Check if the state is in one of the busy states */
  1696. if ((hqspi->State & 0x2) != 0)
  1697. {
  1698. /* Process unlocked */
  1699. __HAL_UNLOCK(hqspi);
  1700. /* Update QSPI state */
  1701. hqspi->State = HAL_QSPI_STATE_ABORT;
  1702. /* Disable all interrupts */
  1703. __HAL_QSPI_DISABLE_IT(hqspi, (QSPI_IT_TO | QSPI_IT_SM | QSPI_IT_FT | QSPI_IT_TC | QSPI_IT_TE));
  1704. if ((hqspi->Instance->CR & QUADSPI_CR_DMAEN)!= RESET)
  1705. {
  1706. /* Disable the DMA transfer by clearing the DMAEN bit in the QSPI CR register */
  1707. CLEAR_BIT(hqspi->Instance->CR, QUADSPI_CR_DMAEN);
  1708. /* Abort DMA channel */
  1709. hqspi->hdma->XferAbortCallback = QSPI_DMAAbortCplt;
  1710. HAL_DMA_Abort_IT(hqspi->hdma);
  1711. }
  1712. else
  1713. {
  1714. /* Clear interrupt */
  1715. __HAL_QSPI_CLEAR_FLAG(hqspi, QSPI_FLAG_TC);
  1716. /* Enable the QSPI Transfer Complete Interrupt */
  1717. __HAL_QSPI_ENABLE_IT(hqspi, QSPI_IT_TC);
  1718. /* Configure QSPI: CR register with Abort request */
  1719. SET_BIT(hqspi->Instance->CR, QUADSPI_CR_ABORT);
  1720. }
  1721. }
  1722. return status;
  1723. }
  1724. /** @brief Set QSPI timeout
  1725. * @param hqspi: QSPI handle.
  1726. * @param Timeout: Timeout for the QSPI memory access.
  1727. * @retval None
  1728. */
  1729. void HAL_QSPI_SetTimeout(QSPI_HandleTypeDef *hqspi, uint32_t Timeout)
  1730. {
  1731. hqspi->Timeout = Timeout;
  1732. }
  1733. /** @brief Set QSPI Fifo threshold.
  1734. * @param hqspi: QSPI handle.
  1735. * @param Threshold: Threshold of the Fifo (value between 1 and 16).
  1736. * @retval HAL status
  1737. */
  1738. HAL_StatusTypeDef HAL_QSPI_SetFifoThreshold(QSPI_HandleTypeDef *hqspi, uint32_t Threshold)
  1739. {
  1740. HAL_StatusTypeDef status = HAL_OK;
  1741. /* Process locked */
  1742. __HAL_LOCK(hqspi);
  1743. if(hqspi->State == HAL_QSPI_STATE_READY)
  1744. {
  1745. /* Synchronize init structure with new FIFO threshold value */
  1746. hqspi->Init.FifoThreshold = Threshold;
  1747. /* Configure QSPI FIFO Threshold */
  1748. MODIFY_REG(hqspi->Instance->CR, QUADSPI_CR_FTHRES,
  1749. ((hqspi->Init.FifoThreshold - 1) << POSITION_VAL(QUADSPI_CR_FTHRES)));
  1750. }
  1751. else
  1752. {
  1753. status = HAL_BUSY;
  1754. }
  1755. /* Process unlocked */
  1756. __HAL_UNLOCK(hqspi);
  1757. /* Return function status */
  1758. return status;
  1759. }
  1760. /** @brief Get QSPI Fifo threshold.
  1761. * @param hqspi: QSPI handle.
  1762. * @retval Fifo threshold (value between 1 and 16)
  1763. */
  1764. uint32_t HAL_QSPI_GetFifoThreshold(QSPI_HandleTypeDef *hqspi)
  1765. {
  1766. return ((READ_BIT(hqspi->Instance->CR, QUADSPI_CR_FTHRES) >> POSITION_VAL(QUADSPI_CR_FTHRES)) + 1);
  1767. }
  1768. /**
  1769. * @}
  1770. */
  1771. /* Private functions ---------------------------------------------------------*/
  1772. /**
  1773. * @brief DMA QSPI receive process complete callback.
  1774. * @param hdma: DMA handle
  1775. * @retval None
  1776. */
  1777. static void QSPI_DMARxCplt(DMA_HandleTypeDef *hdma)
  1778. {
  1779. QSPI_HandleTypeDef* hqspi = ( QSPI_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  1780. hqspi->RxXferCount = 0;
  1781. /* Enable the QSPI transfer complete Interrupt */
  1782. __HAL_QSPI_ENABLE_IT(hqspi, QSPI_IT_TC);
  1783. }
  1784. /**
  1785. * @brief DMA QSPI transmit process complete callback.
  1786. * @param hdma: DMA handle
  1787. * @retval None
  1788. */
  1789. static void QSPI_DMATxCplt(DMA_HandleTypeDef *hdma)
  1790. {
  1791. QSPI_HandleTypeDef* hqspi = ( QSPI_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  1792. hqspi->TxXferCount = 0;
  1793. /* Enable the QSPI transfer complete Interrupt */
  1794. __HAL_QSPI_ENABLE_IT(hqspi, QSPI_IT_TC);
  1795. }
  1796. /**
  1797. * @brief DMA QSPI receive process half complete callback
  1798. * @param hdma : DMA handle
  1799. * @retval None
  1800. */
  1801. static void QSPI_DMARxHalfCplt(DMA_HandleTypeDef *hdma)
  1802. {
  1803. QSPI_HandleTypeDef* hqspi = (QSPI_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
  1804. HAL_QSPI_RxHalfCpltCallback(hqspi);
  1805. }
  1806. /**
  1807. * @brief DMA QSPI transmit process half complete callback
  1808. * @param hdma : DMA handle
  1809. * @retval None
  1810. */
  1811. static void QSPI_DMATxHalfCplt(DMA_HandleTypeDef *hdma)
  1812. {
  1813. QSPI_HandleTypeDef* hqspi = (QSPI_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
  1814. HAL_QSPI_TxHalfCpltCallback(hqspi);
  1815. }
  1816. /**
  1817. * @brief DMA QSPI communication error callback.
  1818. * @param hdma: DMA handle
  1819. * @retval None
  1820. */
  1821. static void QSPI_DMAError(DMA_HandleTypeDef *hdma)
  1822. {
  1823. QSPI_HandleTypeDef* hqspi = ( QSPI_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  1824. /* if DMA error is FIFO error ignore it */
  1825. if(HAL_DMA_GetError(hdma) != HAL_DMA_ERROR_FE)
  1826. {
  1827. hqspi->RxXferCount = 0;
  1828. hqspi->TxXferCount = 0;
  1829. hqspi->ErrorCode |= HAL_QSPI_ERROR_DMA;
  1830. /* Disable the DMA transfer by clearing the DMAEN bit in the QSPI CR register */
  1831. CLEAR_BIT(hqspi->Instance->CR, QUADSPI_CR_DMAEN);
  1832. /* Abort the QSPI */
  1833. HAL_QSPI_Abort_IT(hqspi);
  1834. }
  1835. }
  1836. /**
  1837. * @brief DMA QSPI abort complete callback.
  1838. * @param hdma: DMA handle
  1839. * @retval None
  1840. */
  1841. static void QSPI_DMAAbortCplt(DMA_HandleTypeDef *hdma)
  1842. {
  1843. QSPI_HandleTypeDef* hqspi = ( QSPI_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  1844. hqspi->RxXferCount = 0;
  1845. hqspi->TxXferCount = 0;
  1846. if(hqspi->State == HAL_QSPI_STATE_ABORT)
  1847. {
  1848. /* DMA Abort called by QSPI abort */
  1849. /* Clear interrupt */
  1850. __HAL_QSPI_CLEAR_FLAG(hqspi, QSPI_FLAG_TC);
  1851. /* Enable the QSPI Transfer Complete Interrupt */
  1852. __HAL_QSPI_ENABLE_IT(hqspi, QSPI_IT_TC);
  1853. /* Configure QSPI: CR register with Abort request */
  1854. SET_BIT(hqspi->Instance->CR, QUADSPI_CR_ABORT);
  1855. }
  1856. else
  1857. {
  1858. /* DMA Abort called due to a transfer error interrupt */
  1859. /* Change state of QSPI */
  1860. hqspi->State = HAL_QSPI_STATE_READY;
  1861. /* Error callback */
  1862. HAL_QSPI_ErrorCallback(hqspi);
  1863. }
  1864. }
  1865. /**
  1866. * @brief Wait for a flag state until timeout.
  1867. * @param hqspi: QSPI handle
  1868. * @param Flag: Flag checked
  1869. * @param State: Value of the flag expected
  1870. * @param Timeout: Duration of the time out
  1871. * @param tickstart: tick start value
  1872. * @retval HAL status
  1873. */
  1874. static HAL_StatusTypeDef QSPI_WaitFlagStateUntilTimeout(QSPI_HandleTypeDef *hqspi, uint32_t Flag,
  1875. FlagStatus State, uint32_t tickstart, uint32_t Timeout)
  1876. {
  1877. /* Wait until flag is in expected state */
  1878. while((FlagStatus)(__HAL_QSPI_GET_FLAG(hqspi, Flag)) != State)
  1879. {
  1880. /* Check for the Timeout */
  1881. if (Timeout != HAL_MAX_DELAY)
  1882. {
  1883. if((Timeout == 0) || ((HAL_GetTick() - tickstart) > Timeout))
  1884. {
  1885. hqspi->State = HAL_QSPI_STATE_ERROR;
  1886. hqspi->ErrorCode |= HAL_QSPI_ERROR_TIMEOUT;
  1887. return HAL_ERROR;
  1888. }
  1889. }
  1890. }
  1891. return HAL_OK;
  1892. }
  1893. /**
  1894. * @brief Configure the communication registers.
  1895. * @param hqspi: QSPI handle
  1896. * @param cmd: structure that contains the command configuration information
  1897. * @param FunctionalMode: functional mode to configured
  1898. * This parameter can be one of the following values:
  1899. * @arg QSPI_FUNCTIONAL_MODE_INDIRECT_WRITE: Indirect write mode
  1900. * @arg QSPI_FUNCTIONAL_MODE_INDIRECT_READ: Indirect read mode
  1901. * @arg QSPI_FUNCTIONAL_MODE_AUTO_POLLING: Automatic polling mode
  1902. * @arg QSPI_FUNCTIONAL_MODE_MEMORY_MAPPED: Memory-mapped mode
  1903. * @retval None
  1904. */
  1905. static void QSPI_Config(QSPI_HandleTypeDef *hqspi, QSPI_CommandTypeDef *cmd, uint32_t FunctionalMode)
  1906. {
  1907. assert_param(IS_QSPI_FUNCTIONAL_MODE(FunctionalMode));
  1908. if ((cmd->DataMode != QSPI_DATA_NONE) && (FunctionalMode != QSPI_FUNCTIONAL_MODE_MEMORY_MAPPED))
  1909. {
  1910. /* Configure QSPI: DLR register with the number of data to read or write */
  1911. WRITE_REG(hqspi->Instance->DLR, (cmd->NbData - 1));
  1912. }
  1913. if (cmd->InstructionMode != QSPI_INSTRUCTION_NONE)
  1914. {
  1915. if (cmd->AlternateByteMode != QSPI_ALTERNATE_BYTES_NONE)
  1916. {
  1917. /* Configure QSPI: ABR register with alternate bytes value */
  1918. WRITE_REG(hqspi->Instance->ABR, cmd->AlternateBytes);
  1919. if (cmd->AddressMode != QSPI_ADDRESS_NONE)
  1920. {
  1921. /*---- Command with instruction, address and alternate bytes ----*/
  1922. /* Configure QSPI: CCR register with all communications parameters */
  1923. WRITE_REG(hqspi->Instance->CCR, (cmd->DdrMode | cmd->DdrHoldHalfCycle | cmd->SIOOMode |
  1924. cmd->DataMode | (cmd->DummyCycles << 18) | cmd->AlternateBytesSize |
  1925. cmd->AlternateByteMode | cmd->AddressSize | cmd->AddressMode |
  1926. cmd->InstructionMode | cmd->Instruction | FunctionalMode));
  1927. if (FunctionalMode != QSPI_FUNCTIONAL_MODE_MEMORY_MAPPED)
  1928. {
  1929. /* Configure QSPI: AR register with address value */
  1930. WRITE_REG(hqspi->Instance->AR, cmd->Address);
  1931. }
  1932. }
  1933. else
  1934. {
  1935. /*---- Command with instruction and alternate bytes ----*/
  1936. /* Configure QSPI: CCR register with all communications parameters */
  1937. WRITE_REG(hqspi->Instance->CCR, (cmd->DdrMode | cmd->DdrHoldHalfCycle | cmd->SIOOMode |
  1938. cmd->DataMode | (cmd->DummyCycles << 18) | cmd->AlternateBytesSize |
  1939. cmd->AlternateByteMode | cmd->AddressMode | cmd->InstructionMode |
  1940. cmd->Instruction | FunctionalMode));
  1941. }
  1942. }
  1943. else
  1944. {
  1945. if (cmd->AddressMode != QSPI_ADDRESS_NONE)
  1946. {
  1947. /*---- Command with instruction and address ----*/
  1948. /* Configure QSPI: CCR register with all communications parameters */
  1949. WRITE_REG(hqspi->Instance->CCR, (cmd->DdrMode | cmd->DdrHoldHalfCycle | cmd->SIOOMode |
  1950. cmd->DataMode | (cmd->DummyCycles << 18) | cmd->AlternateByteMode |
  1951. cmd->AddressSize | cmd->AddressMode | cmd->InstructionMode |
  1952. cmd->Instruction | FunctionalMode));
  1953. if (FunctionalMode != QSPI_FUNCTIONAL_MODE_MEMORY_MAPPED)
  1954. {
  1955. /* Configure QSPI: AR register with address value */
  1956. WRITE_REG(hqspi->Instance->AR, cmd->Address);
  1957. }
  1958. }
  1959. else
  1960. {
  1961. /*---- Command with only instruction ----*/
  1962. /* Configure QSPI: CCR register with all communications parameters */
  1963. WRITE_REG(hqspi->Instance->CCR, (cmd->DdrMode | cmd->DdrHoldHalfCycle | cmd->SIOOMode |
  1964. cmd->DataMode | (cmd->DummyCycles << 18) | cmd->AlternateByteMode |
  1965. cmd->AddressMode | cmd->InstructionMode | cmd->Instruction |
  1966. FunctionalMode));
  1967. }
  1968. }
  1969. }
  1970. else
  1971. {
  1972. if (cmd->AlternateByteMode != QSPI_ALTERNATE_BYTES_NONE)
  1973. {
  1974. /* Configure QSPI: ABR register with alternate bytes value */
  1975. WRITE_REG(hqspi->Instance->ABR, cmd->AlternateBytes);
  1976. if (cmd->AddressMode != QSPI_ADDRESS_NONE)
  1977. {
  1978. /*---- Command with address and alternate bytes ----*/
  1979. /* Configure QSPI: CCR register with all communications parameters */
  1980. WRITE_REG(hqspi->Instance->CCR, (cmd->DdrMode | cmd->DdrHoldHalfCycle | cmd->SIOOMode |
  1981. cmd->DataMode | (cmd->DummyCycles << 18) | cmd->AlternateBytesSize |
  1982. cmd->AlternateByteMode | cmd->AddressSize | cmd->AddressMode |
  1983. cmd->InstructionMode | FunctionalMode));
  1984. if (FunctionalMode != QSPI_FUNCTIONAL_MODE_MEMORY_MAPPED)
  1985. {
  1986. /* Configure QSPI: AR register with address value */
  1987. WRITE_REG(hqspi->Instance->AR, cmd->Address);
  1988. }
  1989. }
  1990. else
  1991. {
  1992. /*---- Command with only alternate bytes ----*/
  1993. /* Configure QSPI: CCR register with all communications parameters */
  1994. WRITE_REG(hqspi->Instance->CCR, (cmd->DdrMode | cmd->DdrHoldHalfCycle | cmd->SIOOMode |
  1995. cmd->DataMode | (cmd->DummyCycles << 18) | cmd->AlternateBytesSize |
  1996. cmd->AlternateByteMode | cmd->AddressMode | cmd->InstructionMode |
  1997. FunctionalMode));
  1998. }
  1999. }
  2000. else
  2001. {
  2002. if (cmd->AddressMode != QSPI_ADDRESS_NONE)
  2003. {
  2004. /*---- Command with only address ----*/
  2005. /* Configure QSPI: CCR register with all communications parameters */
  2006. WRITE_REG(hqspi->Instance->CCR, (cmd->DdrMode | cmd->DdrHoldHalfCycle | cmd->SIOOMode |
  2007. cmd->DataMode | (cmd->DummyCycles << 18) | cmd->AlternateByteMode |
  2008. cmd->AddressSize | cmd->AddressMode | cmd->InstructionMode |
  2009. FunctionalMode));
  2010. if (FunctionalMode != QSPI_FUNCTIONAL_MODE_MEMORY_MAPPED)
  2011. {
  2012. /* Configure QSPI: AR register with address value */
  2013. WRITE_REG(hqspi->Instance->AR, cmd->Address);
  2014. }
  2015. }
  2016. else
  2017. {
  2018. /*---- Command with only data phase ----*/
  2019. if (cmd->DataMode != QSPI_DATA_NONE)
  2020. {
  2021. /* Configure QSPI: CCR register with all communications parameters */
  2022. WRITE_REG(hqspi->Instance->CCR, (cmd->DdrMode | cmd->DdrHoldHalfCycle | cmd->SIOOMode |
  2023. cmd->DataMode | (cmd->DummyCycles << 18) | cmd->AlternateByteMode |
  2024. cmd->AddressMode | cmd->InstructionMode | FunctionalMode));
  2025. }
  2026. }
  2027. }
  2028. }
  2029. }
  2030. /**
  2031. * @}
  2032. */
  2033. #endif /* STM32F446xx || STM32F469xx || STM32F479xx || STM32F412Zx || STM32F412Vx || STM32F412Rx
  2034. STM32F413xx || STM32F423xx */
  2035. #endif /* HAL_QSPI_MODULE_ENABLED */
  2036. /**
  2037. * @}
  2038. */
  2039. /**
  2040. * @}
  2041. */
  2042. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/