stm32f4xx_hal_qspi.c 81 KB

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