You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

2622 lines
85 KiB

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