stm32f4xx_hal_qspi.c 63 KB

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