stm32l4xx_hal_qspi.c 78 KB

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