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.
 
 
 

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