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.
 
 
 

2740 lines
87 KiB

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