stm32f4xx_hal_qspi.c 64 KB

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