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.
 
 
 
 
 

2904 regels
94 KiB

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