Merge pull request #10558 from iNavFlight/MrD_Correct-comments-on-OSD-symbols
[inav.git] / lib / main / STM32F7 / Drivers / STM32F7xx_HAL_Driver / Src / stm32f7xx_hal_qspi.c
blobde5fb4408fd63100753f0fd4e9dcfeac1b7a32de
1 /**
2 ******************************************************************************
3 * @file stm32f7xx_hal_qspi.c
4 * @author MCD Application Team
5 * @version V1.2.2
6 * @date 14-April-2017
7 * @brief QSPI HAL module driver.
8 * This file provides firmware functions to manage the following
9 * functionalities of the QuadSPI interface (QSPI).
10 * + Initialization and de-initialization functions
11 * + Indirect functional mode management
12 * + Memory-mapped functional mode management
13 * + Auto-polling functional mode management
14 * + Interrupts and flags management
15 * + DMA channel configuration for indirect functional mode
16 * + Errors management and abort functionality
19 @verbatim
20 ===============================================================================
21 ##### How to use this driver #####
22 ===============================================================================
23 [..]
24 *** Initialization ***
25 ======================
26 [..]
27 (#) As prerequisite, fill in the HAL_QSPI_MspInit() :
28 (++) Enable QuadSPI clock interface with __HAL_RCC_QSPI_CLK_ENABLE().
29 (++) Reset QuadSPI IP with __HAL_RCC_QSPI_FORCE_RESET() and __HAL_RCC_QSPI_RELEASE_RESET().
30 (++) Enable the clocks for the QuadSPI GPIOS with __HAL_RCC_GPIOx_CLK_ENABLE().
31 (++) Configure these QuadSPI pins in alternate mode using HAL_GPIO_Init().
32 (++) If interrupt mode is used, enable and configure QuadSPI global
33 interrupt with HAL_NVIC_SetPriority() and HAL_NVIC_EnableIRQ().
34 (++) If DMA mode is used, enable the clocks for the QuadSPI DMA channel
35 with __HAL_RCC_DMAx_CLK_ENABLE(), configure DMA with HAL_DMA_Init(),
36 link it with QuadSPI handle using __HAL_LINKDMA(), enable and configure
37 DMA channel global interrupt with HAL_NVIC_SetPriority() and HAL_NVIC_EnableIRQ().
38 (#) Configure the flash size, the clock prescaler, the fifo threshold, the
39 clock mode, the sample shifting and the CS high time using the HAL_QSPI_Init() function.
41 *** Indirect functional mode ***
42 ================================
43 [..]
44 (#) Configure the command sequence using the HAL_QSPI_Command() or HAL_QSPI_Command_IT()
45 functions :
46 (++) Instruction phase : the mode used and if present the instruction opcode.
47 (++) Address phase : the mode used and if present the size and the address value.
48 (++) Alternate-bytes phase : the mode used and if present the size and the alternate
49 bytes values.
50 (++) Dummy-cycles phase : the number of dummy cycles (mode used is same as data phase).
51 (++) Data phase : the mode used and if present the number of bytes.
52 (++) Double Data Rate (DDR) mode : the activation (or not) of this mode and the delay
53 if activated.
54 (++) Sending Instruction Only Once (SIOO) mode : the activation (or not) of this mode.
55 (#) If no data is required for the command, it is sent directly to the memory :
56 (++) In polling mode, the output of the function is done when the transfer is complete.
57 (++) In interrupt mode, HAL_QSPI_CmdCpltCallback() will be called when the transfer is complete.
58 (#) For the indirect write mode, use HAL_QSPI_Transmit(), HAL_QSPI_Transmit_DMA() or
59 HAL_QSPI_Transmit_IT() after the command configuration :
60 (++) In polling mode, the output of the function is done when the transfer is complete.
61 (++) In interrupt mode, HAL_QSPI_FifoThresholdCallback() will be called when the fifo threshold
62 is reached and HAL_QSPI_TxCpltCallback() will be called when the transfer is complete.
63 (++) In DMA mode, HAL_QSPI_TxHalfCpltCallback() will be called at the half transfer and
64 HAL_QSPI_TxCpltCallback() will be called when the transfer is complete.
65 (#) For the indirect read mode, use HAL_QSPI_Receive(), HAL_QSPI_Receive_DMA() or
66 HAL_QSPI_Receive_IT() after the command configuration :
67 (++) In polling mode, the output of the function is done when the transfer is complete.
68 (++) In interrupt mode, HAL_QSPI_FifoThresholdCallback() will be called when the fifo threshold
69 is reached and HAL_QSPI_RxCpltCallback() will be called when the transfer is complete.
70 (++) In DMA mode, HAL_QSPI_RxHalfCpltCallback() will be called at the half transfer and
71 HAL_QSPI_RxCpltCallback() will be called when the transfer is complete.
73 *** Auto-polling functional mode ***
74 ====================================
75 [..]
76 (#) Configure the command sequence and the auto-polling functional mode using the
77 HAL_QSPI_AutoPolling() or HAL_QSPI_AutoPolling_IT() functions :
78 (++) Instruction phase : the mode used and if present the instruction opcode.
79 (++) Address phase : the mode used and if present the size and the address value.
80 (++) Alternate-bytes phase : the mode used and if present the size and the alternate
81 bytes values.
82 (++) Dummy-cycles phase : the number of dummy cycles (mode used is same as data phase).
83 (++) Data phase : the mode used.
84 (++) Double Data Rate (DDR) mode : the activation (or not) of this mode and the delay
85 if activated.
86 (++) Sending Instruction Only Once (SIOO) mode : the activation (or not) of this mode.
87 (++) The size of the status bytes, the match value, the mask used, the match mode (OR/AND),
88 the polling interval and the automatic stop activation.
89 (#) After the configuration :
90 (++) In polling mode, the output of the function is done when the status match is reached. The
91 automatic stop is activated to avoid an infinite loop.
92 (++) In interrupt mode, HAL_QSPI_StatusMatchCallback() will be called each time the status match is reached.
94 *** Memory-mapped functional mode ***
95 =====================================
96 [..]
97 (#) Configure the command sequence and the memory-mapped functional mode using the
98 HAL_QSPI_MemoryMapped() functions :
99 (++) Instruction phase : the mode used and if present the instruction opcode.
100 (++) Address phase : the mode used and the size.
101 (++) Alternate-bytes phase : the mode used and if present the size and the alternate
102 bytes values.
103 (++) Dummy-cycles phase : the number of dummy cycles (mode used is same as data phase).
104 (++) Data phase : the mode used.
105 (++) Double Data Rate (DDR) mode : the activation (or not) of this mode and the delay
106 if activated.
107 (++) Sending Instruction Only Once (SIOO) mode : the activation (or not) of this mode.
108 (++) The timeout activation and the timeout period.
109 (#) After the configuration, the QuadSPI will be used as soon as an access on the AHB is done on
110 the address range. HAL_QSPI_TimeOutCallback() will be called when the timeout expires.
112 *** Errors management and abort functionality ***
113 ==================================================
114 [..]
115 (#) HAL_QSPI_GetError() function gives the error raised during the last operation.
116 (#) HAL_QSPI_Abort() and HAL_QSPI_AbortIT() functions aborts any on-going operation and
117 flushes the fifo :
118 (++) In polling mode, the output of the function is done when the transfer
119 complete bit is set and the busy bit cleared.
120 (++) In interrupt mode, HAL_QSPI_AbortCpltCallback() will be called when
121 the transfer complete bi is set.
123 *** Control functions ***
124 =========================
125 [..]
126 (#) HAL_QSPI_GetState() function gives the current state of the HAL QuadSPI driver.
127 (#) HAL_QSPI_SetTimeout() function configures the timeout value used in the driver.
128 (#) HAL_QSPI_SetFifoThreshold() function configures the threshold on the Fifo of the QSPI IP.
129 (#) HAL_QSPI_GetFifoThreshold() function gives the current of the Fifo's threshold
131 *** Workarounds linked to Silicon Limitation ***
132 ====================================================
133 [..]
134 (#) Workarounds Implemented inside HAL Driver
135 (++) Extra data written in the FIFO at the end of a read transfer
137 @endverbatim
138 ******************************************************************************
139 * @attention
141 * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
143 * Redistribution and use in source and binary forms, with or without modification,
144 * are permitted provided that the following conditions are met:
145 * 1. Redistributions of source code must retain the above copyright notice,
146 * this list of conditions and the following disclaimer.
147 * 2. Redistributions in binary form must reproduce the above copyright notice,
148 * this list of conditions and the following disclaimer in the documentation
149 * and/or other materials provided with the distribution.
150 * 3. Neither the name of STMicroelectronics nor the names of its contributors
151 * may be used to endorse or promote products derived from this software
152 * without specific prior written permission.
154 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
155 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
156 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
157 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
158 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
159 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
160 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
161 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
162 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
163 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
165 ******************************************************************************
168 /* Includes ------------------------------------------------------------------*/
169 #include "stm32f7xx_hal.h"
171 /** @addtogroup STM32F7xx_HAL_Driver
172 * @{
175 /** @defgroup QSPI QSPI
176 * @brief HAL QSPI module driver
177 * @{
179 #ifdef HAL_QSPI_MODULE_ENABLED
181 /* Private typedef -----------------------------------------------------------*/
182 /* Private define ------------------------------------------------------------*/
183 /** @addtogroup QSPI_Private_Constants
184 * @{
186 #define QSPI_FUNCTIONAL_MODE_INDIRECT_WRITE ((uint32_t)0x00000000U) /*!<Indirect write mode*/
187 #define QSPI_FUNCTIONAL_MODE_INDIRECT_READ ((uint32_t)QUADSPI_CCR_FMODE_0) /*!<Indirect read mode*/
188 #define QSPI_FUNCTIONAL_MODE_AUTO_POLLING ((uint32_t)QUADSPI_CCR_FMODE_1) /*!<Automatic polling mode*/
189 #define QSPI_FUNCTIONAL_MODE_MEMORY_MAPPED ((uint32_t)QUADSPI_CCR_FMODE) /*!<Memory-mapped mode*/
191 * @}
194 /* Private macro -------------------------------------------------------------*/
195 /** @addtogroup QSPI_Private_Macros QSPI Private Macros
196 * @{
198 #define IS_QSPI_FUNCTIONAL_MODE(MODE) (((MODE) == QSPI_FUNCTIONAL_MODE_INDIRECT_WRITE) || \
199 ((MODE) == QSPI_FUNCTIONAL_MODE_INDIRECT_READ) || \
200 ((MODE) == QSPI_FUNCTIONAL_MODE_AUTO_POLLING) || \
201 ((MODE) == QSPI_FUNCTIONAL_MODE_MEMORY_MAPPED))
203 * @}
206 /* Private variables ---------------------------------------------------------*/
207 /* Private function prototypes -----------------------------------------------*/
208 /** @addtogroup QSPI_Private_Functions QSPI Private Functions
209 * @{
211 static void QSPI_DMARxCplt(DMA_HandleTypeDef *hdma);
212 static void QSPI_DMATxCplt(DMA_HandleTypeDef *hdma);
213 static void QSPI_DMARxHalfCplt(DMA_HandleTypeDef *hdma);
214 static void QSPI_DMATxHalfCplt(DMA_HandleTypeDef *hdma);
215 static void QSPI_DMAError(DMA_HandleTypeDef *hdma);
216 static void QSPI_DMAAbortCplt(DMA_HandleTypeDef *hdma);
217 static HAL_StatusTypeDef QSPI_WaitFlagStateUntilTimeout(QSPI_HandleTypeDef *hqspi, uint32_t Flag, FlagStatus State, uint32_t tickstart, uint32_t Timeout);
218 static void QSPI_Config(QSPI_HandleTypeDef *hqspi, QSPI_CommandTypeDef *cmd, uint32_t FunctionalMode);
220 * @}
223 /* Exported functions ---------------------------------------------------------*/
225 /** @defgroup QSPI_Exported_Functions QSPI Exported Functions
226 * @{
229 /** @defgroup QSPI_Exported_Functions_Group1 Initialization/de-initialization functions
230 * @brief Initialization and Configuration functions
232 @verbatim
233 ===============================================================================
234 ##### Initialization and Configuration functions #####
235 ===============================================================================
236 [..]
237 This subsection provides a set of functions allowing to :
238 (+) Initialize the QuadSPI.
239 (+) De-initialize the QuadSPI.
241 @endverbatim
242 * @{
246 * @brief Initializes the QSPI mode according to the specified parameters
247 * in the QSPI_InitTypeDef and creates the associated handle.
248 * @param hqspi: qspi handle
249 * @retval HAL status
251 HAL_StatusTypeDef HAL_QSPI_Init(QSPI_HandleTypeDef *hqspi)
253 HAL_StatusTypeDef status = HAL_ERROR;
254 uint32_t tickstart = HAL_GetTick();
256 /* Check the QSPI handle allocation */
257 if(hqspi == NULL)
259 return HAL_ERROR;
262 /* Check the parameters */
263 assert_param(IS_QSPI_ALL_INSTANCE(hqspi->Instance));
264 assert_param(IS_QSPI_CLOCK_PRESCALER(hqspi->Init.ClockPrescaler));
265 assert_param(IS_QSPI_FIFO_THRESHOLD(hqspi->Init.FifoThreshold));
266 assert_param(IS_QSPI_SSHIFT(hqspi->Init.SampleShifting));
267 assert_param(IS_QSPI_FLASH_SIZE(hqspi->Init.FlashSize));
268 assert_param(IS_QSPI_CS_HIGH_TIME(hqspi->Init.ChipSelectHighTime));
269 assert_param(IS_QSPI_CLOCK_MODE(hqspi->Init.ClockMode));
270 assert_param(IS_QSPI_DUAL_FLASH_MODE(hqspi->Init.DualFlash));
272 if (hqspi->Init.DualFlash != QSPI_DUALFLASH_ENABLE )
274 assert_param(IS_QSPI_FLASH_ID(hqspi->Init.FlashID));
277 /* Process locked */
278 __HAL_LOCK(hqspi);
280 if(hqspi->State == HAL_QSPI_STATE_RESET)
282 /* Allocate lock resource and initialize it */
283 hqspi->Lock = HAL_UNLOCKED;
285 /* Init the low level hardware : GPIO, CLOCK */
286 HAL_QSPI_MspInit(hqspi);
288 /* Configure the default timeout for the QSPI memory access */
289 HAL_QSPI_SetTimeout(hqspi, HAL_QPSI_TIMEOUT_DEFAULT_VALUE);
292 /* Configure QSPI FIFO Threshold */
293 MODIFY_REG(hqspi->Instance->CR, QUADSPI_CR_FTHRES, ((hqspi->Init.FifoThreshold - 1) << 8));
295 /* Wait till BUSY flag reset */
296 status = QSPI_WaitFlagStateUntilTimeout(hqspi, QSPI_FLAG_BUSY, RESET, tickstart, hqspi->Timeout);
298 if(status == HAL_OK)
301 /* Configure QSPI Clock Prescaler and Sample Shift */
302 MODIFY_REG(hqspi->Instance->CR,(QUADSPI_CR_PRESCALER | QUADSPI_CR_SSHIFT | QUADSPI_CR_FSEL | QUADSPI_CR_DFM), ((hqspi->Init.ClockPrescaler << 24)| hqspi->Init.SampleShifting | hqspi->Init.FlashID| hqspi->Init.DualFlash ));
304 /* Configure QSPI Flash Size, CS High Time and Clock Mode */
305 MODIFY_REG(hqspi->Instance->DCR, (QUADSPI_DCR_FSIZE | QUADSPI_DCR_CSHT | QUADSPI_DCR_CKMODE),
306 ((hqspi->Init.FlashSize << 16) | hqspi->Init.ChipSelectHighTime | hqspi->Init.ClockMode));
308 /* Enable the QSPI peripheral */
309 __HAL_QSPI_ENABLE(hqspi);
311 /* Set QSPI error code to none */
312 hqspi->ErrorCode = HAL_QSPI_ERROR_NONE;
314 /* Initialize the QSPI state */
315 hqspi->State = HAL_QSPI_STATE_READY;
318 /* Release Lock */
319 __HAL_UNLOCK(hqspi);
321 /* Return function status */
322 return status;
326 * @brief DeInitializes the QSPI peripheral
327 * @param hqspi: qspi handle
328 * @retval HAL status
330 HAL_StatusTypeDef HAL_QSPI_DeInit(QSPI_HandleTypeDef *hqspi)
332 /* Check the QSPI handle allocation */
333 if(hqspi == NULL)
335 return HAL_ERROR;
338 /* Process locked */
339 __HAL_LOCK(hqspi);
341 /* Disable the QSPI Peripheral Clock */
342 __HAL_QSPI_DISABLE(hqspi);
344 /* DeInit the low level hardware: GPIO, CLOCK, NVIC... */
345 HAL_QSPI_MspDeInit(hqspi);
347 /* Set QSPI error code to none */
348 hqspi->ErrorCode = HAL_QSPI_ERROR_NONE;
350 /* Initialize the QSPI state */
351 hqspi->State = HAL_QSPI_STATE_RESET;
353 /* Release Lock */
354 __HAL_UNLOCK(hqspi);
356 return HAL_OK;
360 * @brief QSPI MSP Init
361 * @param hqspi: QSPI handle
362 * @retval None
364 __weak void HAL_QSPI_MspInit(QSPI_HandleTypeDef *hqspi)
366 /* Prevent unused argument(s) compilation warning */
367 UNUSED(hqspi);
369 /* NOTE : This function should not be modified, when the callback is needed,
370 the HAL_QSPI_MspInit can be implemented in the user file
375 * @brief QSPI MSP DeInit
376 * @param hqspi: QSPI handle
377 * @retval None
379 __weak void HAL_QSPI_MspDeInit(QSPI_HandleTypeDef *hqspi)
381 /* Prevent unused argument(s) compilation warning */
382 UNUSED(hqspi);
384 /* NOTE : This function should not be modified, when the callback is needed,
385 the HAL_QSPI_MspDeInit can be implemented in the user file
390 * @}
393 /** @defgroup QSPI_Exported_Functions_Group2 IO operation functions
394 * @brief QSPI Transmit/Receive functions
396 @verbatim
397 ===============================================================================
398 ##### IO operation functions #####
399 ===============================================================================
400 [..]
401 This subsection provides a set of functions allowing to :
402 (+) Handle the interrupts.
403 (+) Handle the command sequence.
404 (+) Transmit data in blocking, interrupt or DMA mode.
405 (+) Receive data in blocking, interrupt or DMA mode.
406 (+) Manage the auto-polling functional mode.
407 (+) Manage the memory-mapped functional mode.
409 @endverbatim
410 * @{
414 * @brief This function handles QSPI interrupt request.
415 * @param hqspi: QSPI handle
416 * @retval None.
418 void HAL_QSPI_IRQHandler(QSPI_HandleTypeDef *hqspi)
420 __IO uint32_t *data_reg;
421 uint32_t flag = READ_REG(hqspi->Instance->SR);
422 uint32_t itsource = READ_REG(hqspi->Instance->CR);
424 /* QSPI Fifo Threshold interrupt occurred ----------------------------------*/
425 if(((flag & QSPI_FLAG_FT)!= RESET) && ((itsource & QSPI_IT_FT)!= RESET))
427 data_reg = &hqspi->Instance->DR;
429 if(hqspi->State == HAL_QSPI_STATE_BUSY_INDIRECT_TX)
431 /* Transmission process */
432 while(__HAL_QSPI_GET_FLAG(hqspi, QSPI_FLAG_FT) != 0)
434 if (hqspi->TxXferCount > 0)
436 /* Fill the FIFO until it is full */
437 *(__IO uint8_t *)data_reg = *hqspi->pTxBuffPtr++;
438 hqspi->TxXferCount--;
440 else
442 /* No more data available for the transfer */
443 /* Disable the QSPI FIFO Threshold Interrupt */
444 __HAL_QSPI_DISABLE_IT(hqspi, QSPI_IT_FT);
445 break;
449 else if(hqspi->State == HAL_QSPI_STATE_BUSY_INDIRECT_RX)
451 /* Receiving Process */
452 while(__HAL_QSPI_GET_FLAG(hqspi, QSPI_FLAG_FT) != 0)
454 if (hqspi->RxXferCount > 0)
456 /* Read the FIFO until it is empty */
457 *hqspi->pRxBuffPtr++ = *(__IO uint8_t *)data_reg;
458 hqspi->RxXferCount--;
460 else
462 /* All data have been received for the transfer */
463 /* Disable the QSPI FIFO Threshold Interrupt */
464 __HAL_QSPI_DISABLE_IT(hqspi, QSPI_IT_FT);
465 break;
470 /* FIFO Threshold callback */
471 HAL_QSPI_FifoThresholdCallback(hqspi);
474 /* QSPI Transfer Complete interrupt occurred -------------------------------*/
475 else if(((flag & QSPI_FLAG_TC)!= RESET) && ((itsource & QSPI_IT_TC)!= RESET))
477 /* Clear interrupt */
478 WRITE_REG(hqspi->Instance->FCR, QSPI_FLAG_TC);
480 /* Disable the QSPI FIFO Threshold, Transfer Error and Transfer complete Interrupts */
481 __HAL_QSPI_DISABLE_IT(hqspi, QSPI_IT_TC | QSPI_IT_TE | QSPI_IT_FT);
483 /* Transfer complete callback */
484 if(hqspi->State == HAL_QSPI_STATE_BUSY_INDIRECT_TX)
486 if ((hqspi->Instance->CR & QUADSPI_CR_DMAEN)!= RESET)
488 /* Disable the DMA transfer by clearing the DMAEN bit in the QSPI CR register */
489 CLEAR_BIT(hqspi->Instance->CR, QUADSPI_CR_DMAEN);
491 /* Disable the DMA channel */
492 __HAL_DMA_DISABLE(hqspi->hdma);
495 #if defined(QSPI1_V1_0)
496 /* Clear Busy bit */
497 HAL_QSPI_Abort_IT(hqspi);
498 #endif
500 /* Change state of QSPI */
501 hqspi->State = HAL_QSPI_STATE_READY;
503 /* TX Complete callback */
504 HAL_QSPI_TxCpltCallback(hqspi);
506 else if(hqspi->State == HAL_QSPI_STATE_BUSY_INDIRECT_RX)
508 if ((hqspi->Instance->CR & QUADSPI_CR_DMAEN)!= RESET)
510 /* Disable the DMA transfer by clearing the DMAEN bit in the QSPI CR register */
511 CLEAR_BIT(hqspi->Instance->CR, QUADSPI_CR_DMAEN);
513 /* Disable the DMA channel */
514 __HAL_DMA_DISABLE(hqspi->hdma);
516 else
518 data_reg = &hqspi->Instance->DR;
519 while(READ_BIT(hqspi->Instance->SR, QUADSPI_SR_FLEVEL) != 0)
521 if (hqspi->RxXferCount > 0)
523 /* Read the last data received in the FIFO until it is empty */
524 *hqspi->pRxBuffPtr++ = *(__IO uint8_t *)data_reg;
525 hqspi->RxXferCount--;
527 else
529 /* All data have been received for the transfer */
530 break;
534 #if defined(QSPI1_V1_0)
535 /* Workaround - Extra data written in the FIFO at the end of a read transfer */
536 HAL_QSPI_Abort_IT(hqspi);
537 #endif /* QSPI_V1_0*/
539 /* Change state of QSPI */
540 hqspi->State = HAL_QSPI_STATE_READY;
542 /* RX Complete callback */
543 HAL_QSPI_RxCpltCallback(hqspi);
545 else if(hqspi->State == HAL_QSPI_STATE_BUSY)
547 /* Change state of QSPI */
548 hqspi->State = HAL_QSPI_STATE_READY;
550 /* Command Complete callback */
551 HAL_QSPI_CmdCpltCallback(hqspi);
553 else if(hqspi->State == HAL_QSPI_STATE_ABORT)
555 /* Change state of QSPI */
556 hqspi->State = HAL_QSPI_STATE_READY;
558 if (hqspi->ErrorCode == HAL_QSPI_ERROR_NONE)
560 /* Abort called by the user */
562 /* Abort Complete callback */
563 HAL_QSPI_AbortCpltCallback(hqspi);
565 else
567 /* Abort due to an error (eg : DMA error) */
569 /* Error callback */
570 HAL_QSPI_ErrorCallback(hqspi);
575 /* QSPI Status Match interrupt occurred ------------------------------------*/
576 else if(((flag & QSPI_FLAG_SM)!= RESET) && ((itsource & QSPI_IT_SM)!= RESET))
578 /* Clear interrupt */
579 WRITE_REG(hqspi->Instance->FCR, QSPI_FLAG_SM);
581 /* Check if the automatic poll mode stop is activated */
582 if(READ_BIT(hqspi->Instance->CR, QUADSPI_CR_APMS) != 0)
584 /* Disable the QSPI Transfer Error and Status Match Interrupts */
585 __HAL_QSPI_DISABLE_IT(hqspi, (QSPI_IT_SM | QSPI_IT_TE));
587 /* Change state of QSPI */
588 hqspi->State = HAL_QSPI_STATE_READY;
591 /* Status match callback */
592 HAL_QSPI_StatusMatchCallback(hqspi);
595 /* QSPI Transfer Error interrupt occurred ----------------------------------*/
596 else if(((flag & QSPI_FLAG_TE)!= RESET) && ((itsource & QSPI_IT_TE)!= RESET))
598 /* Clear interrupt */
599 WRITE_REG(hqspi->Instance->FCR, QSPI_FLAG_TE);
601 /* Disable all the QSPI Interrupts */
602 __HAL_QSPI_DISABLE_IT(hqspi, QSPI_IT_SM | QSPI_IT_TC | QSPI_IT_TE | QSPI_IT_FT);
604 /* Set error code */
605 hqspi->ErrorCode |= HAL_QSPI_ERROR_TRANSFER;
607 if ((hqspi->Instance->CR & QUADSPI_CR_DMAEN)!= RESET)
609 /* Disable the DMA transfer by clearing the DMAEN bit in the QSPI CR register */
610 CLEAR_BIT(hqspi->Instance->CR, QUADSPI_CR_DMAEN);
612 /* Disable the DMA channel */
613 hqspi->hdma->XferAbortCallback = QSPI_DMAAbortCplt;
614 HAL_DMA_Abort_IT(hqspi->hdma);
616 else
618 /* Change state of QSPI */
619 hqspi->State = HAL_QSPI_STATE_READY;
621 /* Error callback */
622 HAL_QSPI_ErrorCallback(hqspi);
626 /* QSPI Timeout interrupt occurred -----------------------------------------*/
627 else if(((flag & QSPI_FLAG_TO)!= RESET) && ((itsource & QSPI_IT_TO)!= RESET))
629 /* Clear interrupt */
630 WRITE_REG(hqspi->Instance->FCR, QSPI_FLAG_TO);
632 /* Time out callback */
633 HAL_QSPI_TimeOutCallback(hqspi);
638 * @brief Sets the command configuration.
639 * @param hqspi: QSPI handle
640 * @param cmd : structure that contains the command configuration information
641 * @param Timeout : Time out duration
642 * @note This function is used only in Indirect Read or Write Modes
643 * @retval HAL status
645 HAL_StatusTypeDef HAL_QSPI_Command(QSPI_HandleTypeDef *hqspi, QSPI_CommandTypeDef *cmd, uint32_t Timeout)
647 HAL_StatusTypeDef status = HAL_ERROR;
648 uint32_t tickstart = HAL_GetTick();
650 /* Check the parameters */
651 assert_param(IS_QSPI_INSTRUCTION_MODE(cmd->InstructionMode));
652 if (cmd->InstructionMode != QSPI_INSTRUCTION_NONE)
654 assert_param(IS_QSPI_INSTRUCTION(cmd->Instruction));
657 assert_param(IS_QSPI_ADDRESS_MODE(cmd->AddressMode));
658 if (cmd->AddressMode != QSPI_ADDRESS_NONE)
660 assert_param(IS_QSPI_ADDRESS_SIZE(cmd->AddressSize));
663 assert_param(IS_QSPI_ALTERNATE_BYTES_MODE(cmd->AlternateByteMode));
664 if (cmd->AlternateByteMode != QSPI_ALTERNATE_BYTES_NONE)
666 assert_param(IS_QSPI_ALTERNATE_BYTES_SIZE(cmd->AlternateBytesSize));
669 assert_param(IS_QSPI_DUMMY_CYCLES(cmd->DummyCycles));
670 assert_param(IS_QSPI_DATA_MODE(cmd->DataMode));
672 assert_param(IS_QSPI_DDR_MODE(cmd->DdrMode));
673 assert_param(IS_QSPI_DDR_HHC(cmd->DdrHoldHalfCycle));
674 assert_param(IS_QSPI_SIOO_MODE(cmd->SIOOMode));
676 /* Process locked */
677 __HAL_LOCK(hqspi);
679 if(hqspi->State == HAL_QSPI_STATE_READY)
681 hqspi->ErrorCode = HAL_QSPI_ERROR_NONE;
683 /* Update QSPI state */
684 hqspi->State = HAL_QSPI_STATE_BUSY;
686 /* Wait till BUSY flag reset */
687 status = QSPI_WaitFlagStateUntilTimeout(hqspi, QSPI_FLAG_BUSY, RESET, tickstart, Timeout);
689 if (status == HAL_OK)
691 /* Call the configuration function */
692 QSPI_Config(hqspi, cmd, QSPI_FUNCTIONAL_MODE_INDIRECT_WRITE);
694 if (cmd->DataMode == QSPI_DATA_NONE)
696 /* When there is no data phase, the transfer start as soon as the configuration is done
697 so wait until TC flag is set to go back in idle state */
698 status = QSPI_WaitFlagStateUntilTimeout(hqspi, QSPI_FLAG_TC, SET, tickstart, Timeout);
700 if (status == HAL_OK)
702 __HAL_QSPI_CLEAR_FLAG(hqspi, QSPI_FLAG_TC);
704 /* Update QSPI state */
705 hqspi->State = HAL_QSPI_STATE_READY;
709 else
711 /* Update QSPI state */
712 hqspi->State = HAL_QSPI_STATE_READY;
716 else
718 status = HAL_BUSY;
721 /* Process unlocked */
722 __HAL_UNLOCK(hqspi);
724 /* Return function status */
725 return status;
729 * @brief Sets the command configuration in interrupt mode.
730 * @param hqspi: QSPI handle
731 * @param cmd : structure that contains the command configuration information
732 * @note This function is used only in Indirect Read or Write Modes
733 * @retval HAL status
735 HAL_StatusTypeDef HAL_QSPI_Command_IT(QSPI_HandleTypeDef *hqspi, QSPI_CommandTypeDef *cmd)
737 HAL_StatusTypeDef status = HAL_ERROR;
738 uint32_t tickstart = HAL_GetTick();
740 /* Check the parameters */
741 assert_param(IS_QSPI_INSTRUCTION_MODE(cmd->InstructionMode));
742 if (cmd->InstructionMode != QSPI_INSTRUCTION_NONE)
744 assert_param(IS_QSPI_INSTRUCTION(cmd->Instruction));
747 assert_param(IS_QSPI_ADDRESS_MODE(cmd->AddressMode));
748 if (cmd->AddressMode != QSPI_ADDRESS_NONE)
750 assert_param(IS_QSPI_ADDRESS_SIZE(cmd->AddressSize));
753 assert_param(IS_QSPI_ALTERNATE_BYTES_MODE(cmd->AlternateByteMode));
754 if (cmd->AlternateByteMode != QSPI_ALTERNATE_BYTES_NONE)
756 assert_param(IS_QSPI_ALTERNATE_BYTES_SIZE(cmd->AlternateBytesSize));
759 assert_param(IS_QSPI_DUMMY_CYCLES(cmd->DummyCycles));
760 assert_param(IS_QSPI_DATA_MODE(cmd->DataMode));
762 assert_param(IS_QSPI_DDR_MODE(cmd->DdrMode));
763 assert_param(IS_QSPI_DDR_HHC(cmd->DdrHoldHalfCycle));
764 assert_param(IS_QSPI_SIOO_MODE(cmd->SIOOMode));
766 /* Process locked */
767 __HAL_LOCK(hqspi);
769 if(hqspi->State == HAL_QSPI_STATE_READY)
771 hqspi->ErrorCode = HAL_QSPI_ERROR_NONE;
773 /* Update QSPI state */
774 hqspi->State = HAL_QSPI_STATE_BUSY;
776 /* Wait till BUSY flag reset */
777 status = QSPI_WaitFlagStateUntilTimeout(hqspi, QSPI_FLAG_BUSY, RESET, tickstart, hqspi->Timeout);
779 if (status == HAL_OK)
781 if (cmd->DataMode == QSPI_DATA_NONE)
783 /* Clear interrupt */
784 __HAL_QSPI_CLEAR_FLAG(hqspi, QSPI_FLAG_TE | QSPI_FLAG_TC);
787 /* Call the configuration function */
788 QSPI_Config(hqspi, cmd, QSPI_FUNCTIONAL_MODE_INDIRECT_WRITE);
790 if (cmd->DataMode == QSPI_DATA_NONE)
792 /* When there is no data phase, the transfer start as soon as the configuration is done
793 so activate TC and TE interrupts */
794 /* Process unlocked */
795 __HAL_UNLOCK(hqspi);
797 /* Enable the QSPI Transfer Error Interrupt */
798 __HAL_QSPI_ENABLE_IT(hqspi, QSPI_IT_TE | QSPI_IT_TC);
800 else
802 /* Update QSPI state */
803 hqspi->State = HAL_QSPI_STATE_READY;
805 /* Process unlocked */
806 __HAL_UNLOCK(hqspi);
809 else
811 /* Process unlocked */
812 __HAL_UNLOCK(hqspi);
815 else
817 status = HAL_BUSY;
819 /* Process unlocked */
820 __HAL_UNLOCK(hqspi);
823 /* Return function status */
824 return status;
828 * @brief Transmit an amount of data in blocking mode.
829 * @param hqspi: QSPI handle
830 * @param pData: pointer to data buffer
831 * @param Timeout : Time out duration
832 * @note This function is used only in Indirect Write Mode
833 * @retval HAL status
835 HAL_StatusTypeDef HAL_QSPI_Transmit(QSPI_HandleTypeDef *hqspi, uint8_t *pData, uint32_t Timeout)
837 HAL_StatusTypeDef status = HAL_OK;
838 uint32_t tickstart = HAL_GetTick();
839 __IO uint32_t *data_reg = &hqspi->Instance->DR;
841 /* Process locked */
842 __HAL_LOCK(hqspi);
844 if(hqspi->State == HAL_QSPI_STATE_READY)
846 hqspi->ErrorCode = HAL_QSPI_ERROR_NONE;
848 if(pData != NULL )
850 /* Update state */
851 hqspi->State = HAL_QSPI_STATE_BUSY_INDIRECT_TX;
853 /* Configure counters and size of the handle */
854 hqspi->TxXferCount = READ_REG(hqspi->Instance->DLR) + 1;
855 hqspi->TxXferSize = READ_REG(hqspi->Instance->DLR) + 1;
856 hqspi->pTxBuffPtr = pData;
858 /* Configure QSPI: CCR register with functional as indirect write */
859 MODIFY_REG(hqspi->Instance->CCR, QUADSPI_CCR_FMODE, QSPI_FUNCTIONAL_MODE_INDIRECT_WRITE);
861 while(hqspi->TxXferCount > 0)
863 /* Wait until FT flag is set to send data */
864 status = QSPI_WaitFlagStateUntilTimeout(hqspi, QSPI_FLAG_FT, SET, tickstart, Timeout);
866 if (status != HAL_OK)
868 break;
871 *(__IO uint8_t *)data_reg = *hqspi->pTxBuffPtr++;
872 hqspi->TxXferCount--;
875 if (status == HAL_OK)
877 /* Wait until TC flag is set to go back in idle state */
878 status = QSPI_WaitFlagStateUntilTimeout(hqspi, QSPI_FLAG_TC, SET, tickstart, Timeout);
880 if (status == HAL_OK)
882 /* Clear Transfer Complete bit */
883 __HAL_QSPI_CLEAR_FLAG(hqspi, QSPI_FLAG_TC);
885 #if defined(QSPI1_V1_0)
886 /* Clear Busy bit */
887 status = HAL_QSPI_Abort(hqspi);
888 #endif /* QSPI_V1_0 */
892 /* Update QSPI state */
893 hqspi->State = HAL_QSPI_STATE_READY;
895 else
897 hqspi->ErrorCode |= HAL_QSPI_ERROR_INVALID_PARAM;
898 status = HAL_ERROR;
901 else
903 status = HAL_BUSY;
906 /* Process unlocked */
907 __HAL_UNLOCK(hqspi);
909 return status;
914 * @brief Receive an amount of data in blocking mode
915 * @param hqspi: QSPI handle
916 * @param pData: pointer to data buffer
917 * @param Timeout : Time out duration
918 * @note This function is used only in Indirect Read Mode
919 * @retval HAL status
921 HAL_StatusTypeDef HAL_QSPI_Receive(QSPI_HandleTypeDef *hqspi, uint8_t *pData, uint32_t Timeout)
923 HAL_StatusTypeDef status = HAL_OK;
924 uint32_t tickstart = HAL_GetTick();
925 uint32_t addr_reg = READ_REG(hqspi->Instance->AR);
926 __IO uint32_t *data_reg = &hqspi->Instance->DR;
928 /* Process locked */
929 __HAL_LOCK(hqspi);
931 if(hqspi->State == HAL_QSPI_STATE_READY)
933 hqspi->ErrorCode = HAL_QSPI_ERROR_NONE;
934 if(pData != NULL )
936 /* Update state */
937 hqspi->State = HAL_QSPI_STATE_BUSY_INDIRECT_RX;
939 /* Configure counters and size of the handle */
940 hqspi->RxXferCount = READ_REG(hqspi->Instance->DLR) + 1;
941 hqspi->RxXferSize = READ_REG(hqspi->Instance->DLR) + 1;
942 hqspi->pRxBuffPtr = pData;
944 /* Configure QSPI: CCR register with functional as indirect read */
945 MODIFY_REG(hqspi->Instance->CCR, QUADSPI_CCR_FMODE, QSPI_FUNCTIONAL_MODE_INDIRECT_READ);
947 /* Start the transfer by re-writing the address in AR register */
948 WRITE_REG(hqspi->Instance->AR, addr_reg);
950 while(hqspi->RxXferCount > 0)
952 /* Wait until FT or TC flag is set to read received data */
953 status = QSPI_WaitFlagStateUntilTimeout(hqspi, (QSPI_FLAG_FT | QSPI_FLAG_TC), SET, tickstart, Timeout);
955 if (status != HAL_OK)
957 break;
960 *hqspi->pRxBuffPtr++ = *(__IO uint8_t *)data_reg;
961 hqspi->RxXferCount--;
964 if (status == HAL_OK)
966 /* Wait until TC flag is set to go back in idle state */
967 status = QSPI_WaitFlagStateUntilTimeout(hqspi, QSPI_FLAG_TC, SET, tickstart, Timeout);
969 if (status == HAL_OK)
971 /* Clear Transfer Complete bit */
972 __HAL_QSPI_CLEAR_FLAG(hqspi, QSPI_FLAG_TC);
974 #if defined(QSPI1_V1_0)
975 /* Workaround - Extra data written in the FIFO at the end of a read transfer */
976 status = HAL_QSPI_Abort(hqspi);
977 #endif /* QSPI_V1_0 */
981 /* Update QSPI state */
982 hqspi->State = HAL_QSPI_STATE_READY;
984 else
986 hqspi->ErrorCode |= HAL_QSPI_ERROR_INVALID_PARAM;
987 status = HAL_ERROR;
990 else
992 status = HAL_BUSY;
995 /* Process unlocked */
996 __HAL_UNLOCK(hqspi);
998 return status;
1002 * @brief Send an amount of data in interrupt mode
1003 * @param hqspi: QSPI handle
1004 * @param pData: pointer to data buffer
1005 * @note This function is used only in Indirect Write Mode
1006 * @retval HAL status
1008 HAL_StatusTypeDef HAL_QSPI_Transmit_IT(QSPI_HandleTypeDef *hqspi, uint8_t *pData)
1010 HAL_StatusTypeDef status = HAL_OK;
1012 /* Process locked */
1013 __HAL_LOCK(hqspi);
1015 if(hqspi->State == HAL_QSPI_STATE_READY)
1017 hqspi->ErrorCode = HAL_QSPI_ERROR_NONE;
1018 if(pData != NULL )
1020 /* Update state */
1021 hqspi->State = HAL_QSPI_STATE_BUSY_INDIRECT_TX;
1023 /* Configure counters and size of the handle */
1024 hqspi->TxXferCount = READ_REG(hqspi->Instance->DLR) + 1;
1025 hqspi->TxXferSize = READ_REG(hqspi->Instance->DLR) + 1;
1026 hqspi->pTxBuffPtr = pData;
1028 /* Configure QSPI: CCR register with functional as indirect write */
1029 MODIFY_REG(hqspi->Instance->CCR, QUADSPI_CCR_FMODE, QSPI_FUNCTIONAL_MODE_INDIRECT_WRITE);
1031 /* Clear interrupt */
1032 __HAL_QSPI_CLEAR_FLAG(hqspi, QSPI_FLAG_TE | QSPI_FLAG_TC);
1034 /* Process unlocked */
1035 __HAL_UNLOCK(hqspi);
1037 /* Enable the QSPI transfer error, FIFO threshold and transfer complete Interrupts */
1038 __HAL_QSPI_ENABLE_IT(hqspi, QSPI_IT_TE | QSPI_IT_FT | QSPI_IT_TC);
1041 else
1043 hqspi->ErrorCode |= HAL_QSPI_ERROR_INVALID_PARAM;
1044 status = HAL_ERROR;
1046 /* Process unlocked */
1047 __HAL_UNLOCK(hqspi);
1050 else
1052 status = HAL_BUSY;
1054 /* Process unlocked */
1055 __HAL_UNLOCK(hqspi);
1058 return status;
1062 * @brief Receive an amount of data in no-blocking mode with Interrupt
1063 * @param hqspi: QSPI handle
1064 * @param pData: pointer to data buffer
1065 * @note This function is used only in Indirect Read Mode
1066 * @retval HAL status
1068 HAL_StatusTypeDef HAL_QSPI_Receive_IT(QSPI_HandleTypeDef *hqspi, uint8_t *pData)
1070 HAL_StatusTypeDef status = HAL_OK;
1071 uint32_t addr_reg = READ_REG(hqspi->Instance->AR);
1073 /* Process locked */
1074 __HAL_LOCK(hqspi);
1076 if(hqspi->State == HAL_QSPI_STATE_READY)
1078 hqspi->ErrorCode = HAL_QSPI_ERROR_NONE;
1080 if(pData != NULL )
1082 /* Update state */
1083 hqspi->State = HAL_QSPI_STATE_BUSY_INDIRECT_RX;
1085 /* Configure counters and size of the handle */
1086 hqspi->RxXferCount = READ_REG(hqspi->Instance->DLR) + 1;
1087 hqspi->RxXferSize = READ_REG(hqspi->Instance->DLR) + 1;
1088 hqspi->pRxBuffPtr = pData;
1090 /* Configure QSPI: CCR register with functional as indirect read */
1091 MODIFY_REG(hqspi->Instance->CCR, QUADSPI_CCR_FMODE, QSPI_FUNCTIONAL_MODE_INDIRECT_READ);
1093 /* Start the transfer by re-writing the address in AR register */
1094 WRITE_REG(hqspi->Instance->AR, addr_reg);
1096 /* Clear interrupt */
1097 __HAL_QSPI_CLEAR_FLAG(hqspi, QSPI_FLAG_TE | QSPI_FLAG_TC);
1099 /* Process unlocked */
1100 __HAL_UNLOCK(hqspi);
1102 /* Enable the QSPI transfer error, FIFO threshold and transfer complete Interrupts */
1103 __HAL_QSPI_ENABLE_IT(hqspi, QSPI_IT_TE | QSPI_IT_FT | QSPI_IT_TC);
1105 else
1107 hqspi->ErrorCode |= HAL_QSPI_ERROR_INVALID_PARAM;
1108 status = HAL_ERROR;
1110 /* Process unlocked */
1111 __HAL_UNLOCK(hqspi);
1114 else
1116 status = HAL_BUSY;
1118 /* Process unlocked */
1119 __HAL_UNLOCK(hqspi);
1122 return status;
1126 * @brief Sends an amount of data in non blocking mode with DMA.
1127 * @param hqspi: QSPI handle
1128 * @param pData: pointer to data buffer
1129 * @note This function is used only in Indirect Write Mode
1130 * @note If DMA peripheral access is configured as halfword, the number
1131 * of data and the fifo threshold should be aligned on halfword
1132 * @note If DMA peripheral access is configured as word, the number
1133 * of data and the fifo threshold should be aligned on word
1134 * @retval HAL status
1136 HAL_StatusTypeDef HAL_QSPI_Transmit_DMA(QSPI_HandleTypeDef *hqspi, uint8_t *pData)
1138 HAL_StatusTypeDef status = HAL_OK;
1139 uint32_t *tmp;
1140 uint32_t data_size = (READ_REG(hqspi->Instance->DLR) + 1);
1142 /* Process locked */
1143 __HAL_LOCK(hqspi);
1145 if(hqspi->State == HAL_QSPI_STATE_READY)
1147 /* Clear the error code */
1148 hqspi->ErrorCode = HAL_QSPI_ERROR_NONE;
1150 if(pData != NULL )
1152 /* Configure counters of the handle */
1153 if (hqspi->hdma->Init.PeriphDataAlignment == DMA_PDATAALIGN_BYTE)
1155 hqspi->TxXferCount = data_size;
1157 else if (hqspi->hdma->Init.PeriphDataAlignment == DMA_PDATAALIGN_HALFWORD)
1159 if (((data_size % 2) != 0) || ((hqspi->Init.FifoThreshold % 2) != 0))
1161 /* The number of data or the fifo threshold is not aligned on halfword
1162 => no transfer possible with DMA peripheral access configured as halfword */
1163 hqspi->ErrorCode |= HAL_QSPI_ERROR_INVALID_PARAM;
1164 status = HAL_ERROR;
1166 /* Process unlocked */
1167 __HAL_UNLOCK(hqspi);
1169 else
1171 hqspi->TxXferCount = (data_size >> 1);
1174 else if (hqspi->hdma->Init.PeriphDataAlignment == DMA_PDATAALIGN_WORD)
1176 if (((data_size % 4) != 0) || ((hqspi->Init.FifoThreshold % 4) != 0))
1178 /* The number of data or the fifo threshold is not aligned on word
1179 => no transfer possible with DMA peripheral access configured as word */
1180 hqspi->ErrorCode |= HAL_QSPI_ERROR_INVALID_PARAM;
1181 status = HAL_ERROR;
1183 /* Process unlocked */
1184 __HAL_UNLOCK(hqspi);
1186 else
1188 hqspi->TxXferCount = (data_size >> 2);
1192 if (status == HAL_OK)
1195 /* Update state */
1196 hqspi->State = HAL_QSPI_STATE_BUSY_INDIRECT_TX;
1198 /* Clear interrupt */
1199 __HAL_QSPI_CLEAR_FLAG(hqspi, (QSPI_FLAG_TE | QSPI_FLAG_TC));
1201 /* Configure size and pointer of the handle */
1202 hqspi->TxXferSize = hqspi->TxXferCount;
1203 hqspi->pTxBuffPtr = pData;
1205 /* Configure QSPI: CCR register with functional mode as indirect write */
1206 MODIFY_REG(hqspi->Instance->CCR, QUADSPI_CCR_FMODE, QSPI_FUNCTIONAL_MODE_INDIRECT_WRITE);
1208 /* Set the QSPI DMA transfer complete callback */
1209 hqspi->hdma->XferCpltCallback = QSPI_DMATxCplt;
1211 /* Set the QSPI DMA Half transfer complete callback */
1212 hqspi->hdma->XferHalfCpltCallback = QSPI_DMATxHalfCplt;
1214 /* Set the DMA error callback */
1215 hqspi->hdma->XferErrorCallback = QSPI_DMAError;
1217 /* Clear the DMA abort callback */
1218 hqspi->hdma->XferAbortCallback = NULL;
1220 /* Configure the direction of the DMA */
1221 hqspi->hdma->Init.Direction = DMA_MEMORY_TO_PERIPH;
1222 MODIFY_REG(hqspi->hdma->Instance->CR, DMA_SxCR_DIR, hqspi->hdma->Init.Direction);
1224 /* Enable the QSPI transmit DMA Channel */
1225 tmp = (uint32_t*)&pData;
1226 HAL_DMA_Start_IT(hqspi->hdma, *(uint32_t*)tmp, (uint32_t)&hqspi->Instance->DR, hqspi->TxXferSize);
1228 /* Process unlocked */
1229 __HAL_UNLOCK(hqspi);
1231 /* Enable the QSPI transfer error Interrupt */
1232 __HAL_QSPI_ENABLE_IT(hqspi, QSPI_IT_TE);
1234 /* Enable the DMA transfer by setting the DMAEN bit in the QSPI CR register */
1235 SET_BIT(hqspi->Instance->CR, QUADSPI_CR_DMAEN);
1238 else
1240 hqspi->ErrorCode |= HAL_QSPI_ERROR_INVALID_PARAM;
1242 status = HAL_ERROR;
1244 /* Process unlocked */
1245 __HAL_UNLOCK(hqspi);
1248 else
1250 status = HAL_BUSY;
1252 /* Process unlocked */
1253 __HAL_UNLOCK(hqspi);
1256 return status;
1260 * @brief Receives an amount of data in non blocking mode with DMA.
1261 * @param hqspi: QSPI handle
1262 * @param pData: pointer to data buffer.
1263 * @note This function is used only in Indirect Read Mode
1264 * @note If DMA peripheral access is configured as halfword, the number
1265 * of data and the fifo threshold should be aligned on halfword
1266 * @note If DMA peripheral access is configured as word, the number
1267 * of data and the fifo threshold should be aligned on word
1268 * @retval HAL status
1270 HAL_StatusTypeDef HAL_QSPI_Receive_DMA(QSPI_HandleTypeDef *hqspi, uint8_t *pData)
1272 HAL_StatusTypeDef status = HAL_OK;
1273 uint32_t *tmp;
1274 uint32_t addr_reg = READ_REG(hqspi->Instance->AR);
1275 uint32_t data_size = (READ_REG(hqspi->Instance->DLR) + 1);
1277 /* Process locked */
1278 __HAL_LOCK(hqspi);
1280 if(hqspi->State == HAL_QSPI_STATE_READY)
1282 hqspi->ErrorCode = HAL_QSPI_ERROR_NONE;
1284 if(pData != NULL )
1286 /* Configure counters of the handle */
1287 if (hqspi->hdma->Init.PeriphDataAlignment == DMA_PDATAALIGN_BYTE)
1289 hqspi->RxXferCount = data_size;
1291 else if (hqspi->hdma->Init.PeriphDataAlignment == DMA_PDATAALIGN_HALFWORD)
1293 if (((data_size % 2) != 0) || ((hqspi->Init.FifoThreshold % 2) != 0))
1295 /* The number of data or the fifo threshold is not aligned on halfword
1296 => no transfer possible with DMA peripheral access configured as halfword */
1297 hqspi->ErrorCode |= HAL_QSPI_ERROR_INVALID_PARAM;
1298 status = HAL_ERROR;
1300 /* Process unlocked */
1301 __HAL_UNLOCK(hqspi);
1303 else
1305 hqspi->RxXferCount = (data_size >> 1);
1308 else if (hqspi->hdma->Init.PeriphDataAlignment == DMA_PDATAALIGN_WORD)
1310 if (((data_size % 4) != 0) || ((hqspi->Init.FifoThreshold % 4) != 0))
1312 /* The number of data or the fifo threshold is not aligned on word
1313 => no transfer possible with DMA peripheral access configured as word */
1314 hqspi->ErrorCode |= HAL_QSPI_ERROR_INVALID_PARAM;
1315 status = HAL_ERROR;
1317 /* Process unlocked */
1318 __HAL_UNLOCK(hqspi);
1320 else
1322 hqspi->RxXferCount = (data_size >> 2);
1326 if (status == HAL_OK)
1329 /* Update state */
1330 hqspi->State = HAL_QSPI_STATE_BUSY_INDIRECT_RX;
1332 /* Clear interrupt */
1333 __HAL_QSPI_CLEAR_FLAG(hqspi, (QSPI_FLAG_TE | QSPI_FLAG_TC));
1335 /* Configure size and pointer of the handle */
1336 hqspi->RxXferSize = hqspi->RxXferCount;
1337 hqspi->pRxBuffPtr = pData;
1339 /* Set the QSPI DMA transfer complete callback */
1340 hqspi->hdma->XferCpltCallback = QSPI_DMARxCplt;
1342 /* Set the QSPI DMA Half transfer complete callback */
1343 hqspi->hdma->XferHalfCpltCallback = QSPI_DMARxHalfCplt;
1345 /* Set the DMA error callback */
1346 hqspi->hdma->XferErrorCallback = QSPI_DMAError;
1348 /* Clear the DMA abort callback */
1349 hqspi->hdma->XferAbortCallback = NULL;
1351 /* Configure the direction of the DMA */
1352 hqspi->hdma->Init.Direction = DMA_PERIPH_TO_MEMORY;
1353 MODIFY_REG(hqspi->hdma->Instance->CR, DMA_SxCR_DIR, hqspi->hdma->Init.Direction);
1355 /* Enable the DMA Channel */
1356 tmp = (uint32_t*)&pData;
1357 HAL_DMA_Start_IT(hqspi->hdma, (uint32_t)&hqspi->Instance->DR, *(uint32_t*)tmp, hqspi->RxXferSize);
1359 /* Configure QSPI: CCR register with functional as indirect read */
1360 MODIFY_REG(hqspi->Instance->CCR, QUADSPI_CCR_FMODE, QSPI_FUNCTIONAL_MODE_INDIRECT_READ);
1362 /* Start the transfer by re-writing the address in AR register */
1363 WRITE_REG(hqspi->Instance->AR, addr_reg);
1365 /* Process unlocked */
1366 __HAL_UNLOCK(hqspi);
1368 /* Enable the QSPI transfer error Interrupt */
1369 __HAL_QSPI_ENABLE_IT(hqspi, QSPI_IT_TE);
1371 /* Enable the DMA transfer by setting the DMAEN bit in the QSPI CR register */
1372 SET_BIT(hqspi->Instance->CR, QUADSPI_CR_DMAEN);
1375 else
1377 hqspi->ErrorCode |= HAL_QSPI_ERROR_INVALID_PARAM;
1378 status = HAL_ERROR;
1380 /* Process unlocked */
1381 __HAL_UNLOCK(hqspi);
1384 else
1386 status = HAL_BUSY;
1388 /* Process unlocked */
1389 __HAL_UNLOCK(hqspi);
1392 return status;
1396 * @brief Configure the QSPI Automatic Polling Mode in blocking mode.
1397 * @param hqspi: QSPI handle
1398 * @param cmd: structure that contains the command configuration information.
1399 * @param cfg: structure that contains the polling configuration information.
1400 * @param Timeout : Time out duration
1401 * @note This function is used only in Automatic Polling Mode
1402 * @retval HAL status
1404 HAL_StatusTypeDef HAL_QSPI_AutoPolling(QSPI_HandleTypeDef *hqspi, QSPI_CommandTypeDef *cmd, QSPI_AutoPollingTypeDef *cfg, uint32_t Timeout)
1406 HAL_StatusTypeDef status = HAL_ERROR;
1407 uint32_t tickstart = HAL_GetTick();
1409 /* Check the parameters */
1410 assert_param(IS_QSPI_INSTRUCTION_MODE(cmd->InstructionMode));
1411 if (cmd->InstructionMode != QSPI_INSTRUCTION_NONE)
1413 assert_param(IS_QSPI_INSTRUCTION(cmd->Instruction));
1416 assert_param(IS_QSPI_ADDRESS_MODE(cmd->AddressMode));
1417 if (cmd->AddressMode != QSPI_ADDRESS_NONE)
1419 assert_param(IS_QSPI_ADDRESS_SIZE(cmd->AddressSize));
1422 assert_param(IS_QSPI_ALTERNATE_BYTES_MODE(cmd->AlternateByteMode));
1423 if (cmd->AlternateByteMode != QSPI_ALTERNATE_BYTES_NONE)
1425 assert_param(IS_QSPI_ALTERNATE_BYTES_SIZE(cmd->AlternateBytesSize));
1428 assert_param(IS_QSPI_DUMMY_CYCLES(cmd->DummyCycles));
1429 assert_param(IS_QSPI_DATA_MODE(cmd->DataMode));
1431 assert_param(IS_QSPI_DDR_MODE(cmd->DdrMode));
1432 assert_param(IS_QSPI_DDR_HHC(cmd->DdrHoldHalfCycle));
1433 assert_param(IS_QSPI_SIOO_MODE(cmd->SIOOMode));
1435 assert_param(IS_QSPI_INTERVAL(cfg->Interval));
1436 assert_param(IS_QSPI_STATUS_BYTES_SIZE(cfg->StatusBytesSize));
1437 assert_param(IS_QSPI_MATCH_MODE(cfg->MatchMode));
1439 /* Process locked */
1440 __HAL_LOCK(hqspi);
1442 if(hqspi->State == HAL_QSPI_STATE_READY)
1445 hqspi->ErrorCode = HAL_QSPI_ERROR_NONE;
1447 /* Update state */
1448 hqspi->State = HAL_QSPI_STATE_BUSY_AUTO_POLLING;
1450 /* Wait till BUSY flag reset */
1451 status = QSPI_WaitFlagStateUntilTimeout(hqspi, QSPI_FLAG_BUSY, RESET, tickstart, Timeout);
1453 if (status == HAL_OK)
1455 /* Configure QSPI: PSMAR register with the status match value */
1456 WRITE_REG(hqspi->Instance->PSMAR, cfg->Match);
1458 /* Configure QSPI: PSMKR register with the status mask value */
1459 WRITE_REG(hqspi->Instance->PSMKR, cfg->Mask);
1461 /* Configure QSPI: PIR register with the interval value */
1462 WRITE_REG(hqspi->Instance->PIR, cfg->Interval);
1464 /* Configure QSPI: CR register with Match mode and Automatic stop enabled
1465 (otherwise there will be an infinite loop in blocking mode) */
1466 MODIFY_REG(hqspi->Instance->CR, (QUADSPI_CR_PMM | QUADSPI_CR_APMS),
1467 (cfg->MatchMode | QSPI_AUTOMATIC_STOP_ENABLE));
1469 /* Call the configuration function */
1470 cmd->NbData = cfg->StatusBytesSize;
1471 QSPI_Config(hqspi, cmd, QSPI_FUNCTIONAL_MODE_AUTO_POLLING);
1473 /* Wait until SM flag is set to go back in idle state */
1474 status = QSPI_WaitFlagStateUntilTimeout(hqspi, QSPI_FLAG_SM, SET, tickstart, Timeout);
1476 if (status == HAL_OK)
1478 __HAL_QSPI_CLEAR_FLAG(hqspi, QSPI_FLAG_SM);
1480 /* Update state */
1481 hqspi->State = HAL_QSPI_STATE_READY;
1485 else
1487 status = HAL_BUSY;
1489 /* Process unlocked */
1490 __HAL_UNLOCK(hqspi);
1492 /* Return function status */
1493 return status;
1497 * @brief Configure the QSPI Automatic Polling Mode in non-blocking mode.
1498 * @param hqspi: QSPI handle
1499 * @param cmd: structure that contains the command configuration information.
1500 * @param cfg: structure that contains the polling configuration information.
1501 * @note This function is used only in Automatic Polling Mode
1502 * @retval HAL status
1504 HAL_StatusTypeDef HAL_QSPI_AutoPolling_IT(QSPI_HandleTypeDef *hqspi, QSPI_CommandTypeDef *cmd, QSPI_AutoPollingTypeDef *cfg)
1506 HAL_StatusTypeDef status = HAL_ERROR;
1507 uint32_t tickstart = HAL_GetTick();
1509 /* Check the parameters */
1510 assert_param(IS_QSPI_INSTRUCTION_MODE(cmd->InstructionMode));
1511 if (cmd->InstructionMode != QSPI_INSTRUCTION_NONE)
1513 assert_param(IS_QSPI_INSTRUCTION(cmd->Instruction));
1516 assert_param(IS_QSPI_ADDRESS_MODE(cmd->AddressMode));
1517 if (cmd->AddressMode != QSPI_ADDRESS_NONE)
1519 assert_param(IS_QSPI_ADDRESS_SIZE(cmd->AddressSize));
1522 assert_param(IS_QSPI_ALTERNATE_BYTES_MODE(cmd->AlternateByteMode));
1523 if (cmd->AlternateByteMode != QSPI_ALTERNATE_BYTES_NONE)
1525 assert_param(IS_QSPI_ALTERNATE_BYTES_SIZE(cmd->AlternateBytesSize));
1528 assert_param(IS_QSPI_DUMMY_CYCLES(cmd->DummyCycles));
1529 assert_param(IS_QSPI_DATA_MODE(cmd->DataMode));
1531 assert_param(IS_QSPI_DDR_MODE(cmd->DdrMode));
1532 assert_param(IS_QSPI_DDR_HHC(cmd->DdrHoldHalfCycle));
1533 assert_param(IS_QSPI_SIOO_MODE(cmd->SIOOMode));
1535 assert_param(IS_QSPI_INTERVAL(cfg->Interval));
1536 assert_param(IS_QSPI_STATUS_BYTES_SIZE(cfg->StatusBytesSize));
1537 assert_param(IS_QSPI_MATCH_MODE(cfg->MatchMode));
1538 assert_param(IS_QSPI_AUTOMATIC_STOP(cfg->AutomaticStop));
1540 /* Process locked */
1541 __HAL_LOCK(hqspi);
1543 if(hqspi->State == HAL_QSPI_STATE_READY)
1545 hqspi->ErrorCode = HAL_QSPI_ERROR_NONE;
1547 /* Update state */
1548 hqspi->State = HAL_QSPI_STATE_BUSY_AUTO_POLLING;
1550 /* Wait till BUSY flag reset */
1551 status = QSPI_WaitFlagStateUntilTimeout(hqspi, QSPI_FLAG_BUSY, RESET, tickstart, hqspi->Timeout);
1553 if (status == HAL_OK)
1555 /* Configure QSPI: PSMAR register with the status match value */
1556 WRITE_REG(hqspi->Instance->PSMAR, cfg->Match);
1558 /* Configure QSPI: PSMKR register with the status mask value */
1559 WRITE_REG(hqspi->Instance->PSMKR, cfg->Mask);
1561 /* Configure QSPI: PIR register with the interval value */
1562 WRITE_REG(hqspi->Instance->PIR, cfg->Interval);
1564 /* Configure QSPI: CR register with Match mode and Automatic stop mode */
1565 MODIFY_REG(hqspi->Instance->CR, (QUADSPI_CR_PMM | QUADSPI_CR_APMS),
1566 (cfg->MatchMode | cfg->AutomaticStop));
1568 /* Clear interrupt */
1569 __HAL_QSPI_CLEAR_FLAG(hqspi, QSPI_FLAG_TE | QSPI_FLAG_SM);
1571 /* Call the configuration function */
1572 cmd->NbData = cfg->StatusBytesSize;
1573 QSPI_Config(hqspi, cmd, QSPI_FUNCTIONAL_MODE_AUTO_POLLING);
1575 /* Process unlocked */
1576 __HAL_UNLOCK(hqspi);
1578 /* Enable the QSPI Transfer Error and status match Interrupt */
1579 __HAL_QSPI_ENABLE_IT(hqspi, (QSPI_IT_SM | QSPI_IT_TE));
1582 else
1584 /* Process unlocked */
1585 __HAL_UNLOCK(hqspi);
1588 else
1590 status = HAL_BUSY;
1592 /* Process unlocked */
1593 __HAL_UNLOCK(hqspi);
1596 /* Return function status */
1597 return status;
1601 * @brief Configure the Memory Mapped mode.
1602 * @param hqspi: QSPI handle
1603 * @param cmd: structure that contains the command configuration information.
1604 * @param cfg: structure that contains the memory mapped configuration information.
1605 * @note This function is used only in Memory mapped Mode
1606 * @retval HAL status
1608 HAL_StatusTypeDef HAL_QSPI_MemoryMapped(QSPI_HandleTypeDef *hqspi, QSPI_CommandTypeDef *cmd, QSPI_MemoryMappedTypeDef *cfg)
1610 HAL_StatusTypeDef status = HAL_ERROR;
1611 uint32_t tickstart = HAL_GetTick();
1613 /* Check the parameters */
1614 assert_param(IS_QSPI_INSTRUCTION_MODE(cmd->InstructionMode));
1615 if (cmd->InstructionMode != QSPI_INSTRUCTION_NONE)
1617 assert_param(IS_QSPI_INSTRUCTION(cmd->Instruction));
1620 assert_param(IS_QSPI_ADDRESS_MODE(cmd->AddressMode));
1621 if (cmd->AddressMode != QSPI_ADDRESS_NONE)
1623 assert_param(IS_QSPI_ADDRESS_SIZE(cmd->AddressSize));
1626 assert_param(IS_QSPI_ALTERNATE_BYTES_MODE(cmd->AlternateByteMode));
1627 if (cmd->AlternateByteMode != QSPI_ALTERNATE_BYTES_NONE)
1629 assert_param(IS_QSPI_ALTERNATE_BYTES_SIZE(cmd->AlternateBytesSize));
1632 assert_param(IS_QSPI_DUMMY_CYCLES(cmd->DummyCycles));
1633 assert_param(IS_QSPI_DATA_MODE(cmd->DataMode));
1635 assert_param(IS_QSPI_DDR_MODE(cmd->DdrMode));
1636 assert_param(IS_QSPI_DDR_HHC(cmd->DdrHoldHalfCycle));
1637 assert_param(IS_QSPI_SIOO_MODE(cmd->SIOOMode));
1639 assert_param(IS_QSPI_TIMEOUT_ACTIVATION(cfg->TimeOutActivation));
1641 /* Process locked */
1642 __HAL_LOCK(hqspi);
1644 if(hqspi->State == HAL_QSPI_STATE_READY)
1646 hqspi->ErrorCode = HAL_QSPI_ERROR_NONE;
1648 /* Update state */
1649 hqspi->State = HAL_QSPI_STATE_BUSY_MEM_MAPPED;
1651 /* Wait till BUSY flag reset */
1652 status = QSPI_WaitFlagStateUntilTimeout(hqspi, QSPI_FLAG_BUSY, RESET, tickstart, hqspi->Timeout);
1654 if (status == HAL_OK)
1656 /* Configure QSPI: CR register with timeout counter enable */
1657 MODIFY_REG(hqspi->Instance->CR, QUADSPI_CR_TCEN, cfg->TimeOutActivation);
1659 if (cfg->TimeOutActivation == QSPI_TIMEOUT_COUNTER_ENABLE)
1661 assert_param(IS_QSPI_TIMEOUT_PERIOD(cfg->TimeOutPeriod));
1663 /* Configure QSPI: LPTR register with the low-power timeout value */
1664 WRITE_REG(hqspi->Instance->LPTR, cfg->TimeOutPeriod);
1666 /* Clear interrupt */
1667 __HAL_QSPI_CLEAR_FLAG(hqspi, QSPI_FLAG_TO);
1669 /* Enable the QSPI TimeOut Interrupt */
1670 __HAL_QSPI_ENABLE_IT(hqspi, QSPI_IT_TO);
1673 /* Call the configuration function */
1674 QSPI_Config(hqspi, cmd, QSPI_FUNCTIONAL_MODE_MEMORY_MAPPED);
1677 else
1679 status = HAL_BUSY;
1682 /* Process unlocked */
1683 __HAL_UNLOCK(hqspi);
1685 /* Return function status */
1686 return status;
1690 * @brief Transfer Error callbacks
1691 * @param hqspi: QSPI handle
1692 * @retval None
1694 __weak void HAL_QSPI_ErrorCallback(QSPI_HandleTypeDef *hqspi)
1696 /* Prevent unused argument(s) compilation warning */
1697 UNUSED(hqspi);
1699 /* NOTE : This function Should not be modified, when the callback is needed,
1700 the HAL_QSPI_ErrorCallback could be implemented in the user file
1705 * @brief Abort completed callback.
1706 * @param hqspi: QSPI handle
1707 * @retval None
1709 __weak void HAL_QSPI_AbortCpltCallback(QSPI_HandleTypeDef *hqspi)
1711 /* Prevent unused argument(s) compilation warning */
1712 UNUSED(hqspi);
1714 /* NOTE: This function should not be modified, when the callback is needed,
1715 the HAL_QSPI_AbortCpltCallback could be implemented in the user file
1720 * @brief Command completed callback.
1721 * @param hqspi: QSPI handle
1722 * @retval None
1724 __weak void HAL_QSPI_CmdCpltCallback(QSPI_HandleTypeDef *hqspi)
1726 /* Prevent unused argument(s) compilation warning */
1727 UNUSED(hqspi);
1729 /* NOTE: This function Should not be modified, when the callback is needed,
1730 the HAL_QSPI_CmdCpltCallback could be implemented in the user file
1735 * @brief Rx Transfer completed callbacks.
1736 * @param hqspi: QSPI handle
1737 * @retval None
1739 __weak void HAL_QSPI_RxCpltCallback(QSPI_HandleTypeDef *hqspi)
1741 /* Prevent unused argument(s) compilation warning */
1742 UNUSED(hqspi);
1744 /* NOTE: This function Should not be modified, when the callback is needed,
1745 the HAL_QSPI_RxCpltCallback could be implemented in the user file
1750 * @brief Tx Transfer completed callbacks.
1751 * @param hqspi: QSPI handle
1752 * @retval None
1754 __weak void HAL_QSPI_TxCpltCallback(QSPI_HandleTypeDef *hqspi)
1756 /* Prevent unused argument(s) compilation warning */
1757 UNUSED(hqspi);
1759 /* NOTE: This function Should not be modified, when the callback is needed,
1760 the HAL_QSPI_TxCpltCallback could be implemented in the user file
1765 * @brief Rx Half Transfer completed callbacks.
1766 * @param hqspi: QSPI handle
1767 * @retval None
1769 __weak void HAL_QSPI_RxHalfCpltCallback(QSPI_HandleTypeDef *hqspi)
1771 /* Prevent unused argument(s) compilation warning */
1772 UNUSED(hqspi);
1774 /* NOTE: This function Should not be modified, when the callback is needed,
1775 the HAL_QSPI_RxHalfCpltCallback could be implemented in the user file
1780 * @brief Tx Half Transfer completed callbacks.
1781 * @param hqspi: QSPI handle
1782 * @retval None
1784 __weak void HAL_QSPI_TxHalfCpltCallback(QSPI_HandleTypeDef *hqspi)
1786 /* Prevent unused argument(s) compilation warning */
1787 UNUSED(hqspi);
1789 /* NOTE: This function Should not be modified, when the callback is needed,
1790 the HAL_QSPI_TxHalfCpltCallback could be implemented in the user file
1795 * @brief FIFO Threshold callbacks
1796 * @param hqspi: QSPI handle
1797 * @retval None
1799 __weak void HAL_QSPI_FifoThresholdCallback(QSPI_HandleTypeDef *hqspi)
1801 /* Prevent unused argument(s) compilation warning */
1802 UNUSED(hqspi);
1804 /* NOTE : This function Should not be modified, when the callback is needed,
1805 the HAL_QSPI_FIFOThresholdCallback could be implemented in the user file
1810 * @brief Status Match callbacks
1811 * @param hqspi: QSPI handle
1812 * @retval None
1814 __weak void HAL_QSPI_StatusMatchCallback(QSPI_HandleTypeDef *hqspi)
1816 /* Prevent unused argument(s) compilation warning */
1817 UNUSED(hqspi);
1819 /* NOTE : This function Should not be modified, when the callback is needed,
1820 the HAL_QSPI_StatusMatchCallback could be implemented in the user file
1825 * @brief Timeout callbacks
1826 * @param hqspi: QSPI handle
1827 * @retval None
1829 __weak void HAL_QSPI_TimeOutCallback(QSPI_HandleTypeDef *hqspi)
1831 /* Prevent unused argument(s) compilation warning */
1832 UNUSED(hqspi);
1834 /* NOTE : This function Should not be modified, when the callback is needed,
1835 the HAL_QSPI_TimeOutCallback could be implemented in the user file
1840 * @}
1843 /** @defgroup QSPI_Exported_Functions_Group3 Peripheral Control and State functions
1844 * @brief QSPI control and State functions
1846 @verbatim
1847 ===============================================================================
1848 ##### Peripheral Control and State functions #####
1849 ===============================================================================
1850 [..]
1851 This subsection provides a set of functions allowing to :
1852 (+) Check in run-time the state of the driver.
1853 (+) Check the error code set during last operation.
1854 (+) Abort any operation.
1855 .....
1856 @endverbatim
1857 * @{
1861 * @brief Return the QSPI handle state.
1862 * @param hqspi: QSPI handle
1863 * @retval HAL state
1865 HAL_QSPI_StateTypeDef HAL_QSPI_GetState(QSPI_HandleTypeDef *hqspi)
1867 /* Return QSPI handle state */
1868 return hqspi->State;
1872 * @brief Return the QSPI error code
1873 * @param hqspi: QSPI handle
1874 * @retval QSPI Error Code
1876 uint32_t HAL_QSPI_GetError(QSPI_HandleTypeDef *hqspi)
1878 return hqspi->ErrorCode;
1882 * @brief Abort the current transmission
1883 * @param hqspi: QSPI handle
1884 * @retval HAL status
1886 HAL_StatusTypeDef HAL_QSPI_Abort(QSPI_HandleTypeDef *hqspi)
1888 HAL_StatusTypeDef status = HAL_OK;
1889 uint32_t tickstart = HAL_GetTick();
1891 /* Check if the state is in one of the busy states */
1892 if ((hqspi->State & 0x2) != 0)
1894 /* Process unlocked */
1895 __HAL_UNLOCK(hqspi);
1897 if ((hqspi->Instance->CR & QUADSPI_CR_DMAEN)!= RESET)
1899 /* Disable the DMA transfer by clearing the DMAEN bit in the QSPI CR register */
1900 CLEAR_BIT(hqspi->Instance->CR, QUADSPI_CR_DMAEN);
1902 /* Abort DMA channel */
1903 status = HAL_DMA_Abort(hqspi->hdma);
1904 if(status != HAL_OK)
1906 hqspi->ErrorCode |= HAL_QSPI_ERROR_DMA;
1910 /* Configure QSPI: CR register with Abort request */
1911 SET_BIT(hqspi->Instance->CR, QUADSPI_CR_ABORT);
1913 /* Wait until TC flag is set to go back in idle state */
1914 status = QSPI_WaitFlagStateUntilTimeout(hqspi, QSPI_FLAG_TC, SET, tickstart, hqspi->Timeout);
1916 if(status == HAL_OK)
1918 __HAL_QSPI_CLEAR_FLAG(hqspi, QSPI_FLAG_TC);
1920 /* Wait until BUSY flag is reset */
1921 status = QSPI_WaitFlagStateUntilTimeout(hqspi, QSPI_FLAG_BUSY, RESET, tickstart, hqspi->Timeout);
1924 if (status == HAL_OK)
1926 /* Update state */
1927 hqspi->State = HAL_QSPI_STATE_READY;
1931 return status;
1935 * @brief Abort the current transmission (non-blocking function)
1936 * @param hqspi: QSPI handle
1937 * @retval HAL status
1939 HAL_StatusTypeDef HAL_QSPI_Abort_IT(QSPI_HandleTypeDef *hqspi)
1941 HAL_StatusTypeDef status = HAL_OK;
1943 /* Check if the state is in one of the busy states */
1944 if ((hqspi->State & 0x2) != 0)
1946 /* Process unlocked */
1947 __HAL_UNLOCK(hqspi);
1949 /* Update QSPI state */
1950 hqspi->State = HAL_QSPI_STATE_ABORT;
1952 /* Disable all interrupts */
1953 __HAL_QSPI_DISABLE_IT(hqspi, (QSPI_IT_TO | QSPI_IT_SM | QSPI_IT_FT | QSPI_IT_TC | QSPI_IT_TE));
1955 if ((hqspi->Instance->CR & QUADSPI_CR_DMAEN)!= RESET)
1957 /* Disable the DMA transfer by clearing the DMAEN bit in the QSPI CR register */
1958 CLEAR_BIT(hqspi->Instance->CR, QUADSPI_CR_DMAEN);
1960 /* Abort DMA channel */
1961 hqspi->hdma->XferAbortCallback = QSPI_DMAAbortCplt;
1962 HAL_DMA_Abort_IT(hqspi->hdma);
1964 else
1966 /* Clear interrupt */
1967 __HAL_QSPI_CLEAR_FLAG(hqspi, QSPI_FLAG_TC);
1969 /* Enable the QSPI Transfer Complete Interrupt */
1970 __HAL_QSPI_ENABLE_IT(hqspi, QSPI_IT_TC);
1972 /* Configure QSPI: CR register with Abort request */
1973 SET_BIT(hqspi->Instance->CR, QUADSPI_CR_ABORT);
1977 return status;
1980 /** @brief Set QSPI timeout
1981 * @param hqspi: QSPI handle.
1982 * @param Timeout: Timeout for the QSPI memory access.
1983 * @retval None
1985 void HAL_QSPI_SetTimeout(QSPI_HandleTypeDef *hqspi, uint32_t Timeout)
1987 hqspi->Timeout = Timeout;
1990 /** @brief Set QSPI Fifo threshold.
1991 * @param hqspi: QSPI handle.
1992 * @param Threshold: Threshold of the Fifo (value between 1 and 16).
1993 * @retval HAL status
1995 HAL_StatusTypeDef HAL_QSPI_SetFifoThreshold(QSPI_HandleTypeDef *hqspi, uint32_t Threshold)
1997 HAL_StatusTypeDef status = HAL_OK;
1999 /* Process locked */
2000 __HAL_LOCK(hqspi);
2002 if(hqspi->State == HAL_QSPI_STATE_READY)
2004 /* Synchronize init structure with new FIFO threshold value */
2005 hqspi->Init.FifoThreshold = Threshold;
2007 /* Configure QSPI FIFO Threshold */
2008 MODIFY_REG(hqspi->Instance->CR, QUADSPI_CR_FTHRES,
2009 ((hqspi->Init.FifoThreshold - 1) << POSITION_VAL(QUADSPI_CR_FTHRES)));
2011 else
2013 status = HAL_BUSY;
2016 /* Process unlocked */
2017 __HAL_UNLOCK(hqspi);
2019 /* Return function status */
2020 return status;
2023 /** @brief Get QSPI Fifo threshold.
2024 * @param hqspi: QSPI handle.
2025 * @retval Fifo threshold (value between 1 and 16)
2027 uint32_t HAL_QSPI_GetFifoThreshold(QSPI_HandleTypeDef *hqspi)
2029 return ((READ_BIT(hqspi->Instance->CR, QUADSPI_CR_FTHRES) >> POSITION_VAL(QUADSPI_CR_FTHRES)) + 1);
2033 * @}
2036 /* Private functions ---------------------------------------------------------*/
2039 * @brief DMA QSPI receive process complete callback.
2040 * @param hdma: DMA handle
2041 * @retval None
2043 static void QSPI_DMARxCplt(DMA_HandleTypeDef *hdma)
2045 QSPI_HandleTypeDef* hqspi = ( QSPI_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
2046 hqspi->RxXferCount = 0;
2048 /* Enable the QSPI transfer complete Interrupt */
2049 __HAL_QSPI_ENABLE_IT(hqspi, QSPI_IT_TC);
2053 * @brief DMA QSPI transmit process complete callback.
2054 * @param hdma: DMA handle
2055 * @retval None
2057 static void QSPI_DMATxCplt(DMA_HandleTypeDef *hdma)
2059 QSPI_HandleTypeDef* hqspi = ( QSPI_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
2060 hqspi->TxXferCount = 0;
2062 /* Enable the QSPI transfer complete Interrupt */
2063 __HAL_QSPI_ENABLE_IT(hqspi, QSPI_IT_TC);
2067 * @brief DMA QSPI receive process half complete callback
2068 * @param hdma : DMA handle
2069 * @retval None
2071 static void QSPI_DMARxHalfCplt(DMA_HandleTypeDef *hdma)
2073 QSPI_HandleTypeDef* hqspi = (QSPI_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
2075 HAL_QSPI_RxHalfCpltCallback(hqspi);
2079 * @brief DMA QSPI transmit process half complete callback
2080 * @param hdma : DMA handle
2081 * @retval None
2083 static void QSPI_DMATxHalfCplt(DMA_HandleTypeDef *hdma)
2085 QSPI_HandleTypeDef* hqspi = (QSPI_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
2087 HAL_QSPI_TxHalfCpltCallback(hqspi);
2091 * @brief DMA QSPI communication error callback.
2092 * @param hdma: DMA handle
2093 * @retval None
2095 static void QSPI_DMAError(DMA_HandleTypeDef *hdma)
2097 QSPI_HandleTypeDef* hqspi = ( QSPI_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
2099 /* if DMA error is FIFO error ignore it */
2100 if(HAL_DMA_GetError(hdma) != HAL_DMA_ERROR_FE)
2102 hqspi->RxXferCount = 0;
2103 hqspi->TxXferCount = 0;
2104 hqspi->ErrorCode |= HAL_QSPI_ERROR_DMA;
2106 /* Disable the DMA transfer by clearing the DMAEN bit in the QSPI CR register */
2107 CLEAR_BIT(hqspi->Instance->CR, QUADSPI_CR_DMAEN);
2109 /* Abort the QSPI */
2110 HAL_QSPI_Abort_IT(hqspi);
2115 * @brief DMA QSPI abort complete callback.
2116 * @param hdma: DMA handle
2117 * @retval None
2119 static void QSPI_DMAAbortCplt(DMA_HandleTypeDef *hdma)
2121 QSPI_HandleTypeDef* hqspi = ( QSPI_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
2123 hqspi->RxXferCount = 0;
2124 hqspi->TxXferCount = 0;
2126 if(hqspi->State == HAL_QSPI_STATE_ABORT)
2128 /* DMA Abort called by QSPI abort */
2129 /* Clear interrupt */
2130 __HAL_QSPI_CLEAR_FLAG(hqspi, QSPI_FLAG_TC);
2132 /* Enable the QSPI Transfer Complete Interrupt */
2133 __HAL_QSPI_ENABLE_IT(hqspi, QSPI_IT_TC);
2135 /* Configure QSPI: CR register with Abort request */
2136 SET_BIT(hqspi->Instance->CR, QUADSPI_CR_ABORT);
2138 else
2140 /* DMA Abort called due to a transfer error interrupt */
2141 /* Change state of QSPI */
2142 hqspi->State = HAL_QSPI_STATE_READY;
2144 /* Error callback */
2145 HAL_QSPI_ErrorCallback(hqspi);
2150 * @brief Wait for a flag state until timeout.
2151 * @param hqspi: QSPI handle
2152 * @param Flag: Flag checked
2153 * @param State: Value of the flag expected
2154 * @param tickstart: Start tick value
2155 * @param Timeout: Duration of the time out
2156 * @retval HAL status
2158 static HAL_StatusTypeDef QSPI_WaitFlagStateUntilTimeout(QSPI_HandleTypeDef *hqspi, uint32_t Flag,
2159 FlagStatus State, uint32_t tickstart, uint32_t Timeout)
2161 /* Wait until flag is in expected state */
2162 while((FlagStatus)(__HAL_QSPI_GET_FLAG(hqspi, Flag)) != State)
2164 /* Check for the Timeout */
2165 if (Timeout != HAL_MAX_DELAY)
2167 if((Timeout == 0) || ((HAL_GetTick() - tickstart) > Timeout))
2169 hqspi->State = HAL_QSPI_STATE_ERROR;
2170 hqspi->ErrorCode |= HAL_QSPI_ERROR_TIMEOUT;
2172 return HAL_ERROR;
2176 return HAL_OK;
2180 * @brief Configure the communication registers.
2181 * @param hqspi: QSPI handle
2182 * @param cmd: structure that contains the command configuration information
2183 * @param FunctionalMode: functional mode to configured
2184 * This parameter can be one of the following values:
2185 * @arg QSPI_FUNCTIONAL_MODE_INDIRECT_WRITE: Indirect write mode
2186 * @arg QSPI_FUNCTIONAL_MODE_INDIRECT_READ: Indirect read mode
2187 * @arg QSPI_FUNCTIONAL_MODE_AUTO_POLLING: Automatic polling mode
2188 * @arg QSPI_FUNCTIONAL_MODE_MEMORY_MAPPED: Memory-mapped mode
2189 * @retval None
2191 static void QSPI_Config(QSPI_HandleTypeDef *hqspi, QSPI_CommandTypeDef *cmd, uint32_t FunctionalMode)
2193 assert_param(IS_QSPI_FUNCTIONAL_MODE(FunctionalMode));
2195 if ((cmd->DataMode != QSPI_DATA_NONE) && (FunctionalMode != QSPI_FUNCTIONAL_MODE_MEMORY_MAPPED))
2197 /* Configure QSPI: DLR register with the number of data to read or write */
2198 WRITE_REG(hqspi->Instance->DLR, (cmd->NbData - 1));
2201 if (cmd->InstructionMode != QSPI_INSTRUCTION_NONE)
2203 if (cmd->AlternateByteMode != QSPI_ALTERNATE_BYTES_NONE)
2205 /* Configure QSPI: ABR register with alternate bytes value */
2206 WRITE_REG(hqspi->Instance->ABR, cmd->AlternateBytes);
2208 if (cmd->AddressMode != QSPI_ADDRESS_NONE)
2210 /*---- Command with instruction, address and alternate bytes ----*/
2211 /* Configure QSPI: CCR register with all communications parameters */
2212 WRITE_REG(hqspi->Instance->CCR, (cmd->DdrMode | cmd->DdrHoldHalfCycle | cmd->SIOOMode |
2213 cmd->DataMode | (cmd->DummyCycles << 18) | cmd->AlternateBytesSize |
2214 cmd->AlternateByteMode | cmd->AddressSize | cmd->AddressMode |
2215 cmd->InstructionMode | cmd->Instruction | FunctionalMode));
2217 if (FunctionalMode != QSPI_FUNCTIONAL_MODE_MEMORY_MAPPED)
2219 /* Configure QSPI: AR register with address value */
2220 WRITE_REG(hqspi->Instance->AR, cmd->Address);
2223 else
2225 /*---- Command with instruction and alternate bytes ----*/
2226 /* Configure QSPI: CCR register with all communications parameters */
2227 WRITE_REG(hqspi->Instance->CCR, (cmd->DdrMode | cmd->DdrHoldHalfCycle | cmd->SIOOMode |
2228 cmd->DataMode | (cmd->DummyCycles << 18) | cmd->AlternateBytesSize |
2229 cmd->AlternateByteMode | cmd->AddressMode | cmd->InstructionMode |
2230 cmd->Instruction | FunctionalMode));
2233 else
2235 if (cmd->AddressMode != QSPI_ADDRESS_NONE)
2237 /*---- Command with instruction and address ----*/
2238 /* Configure QSPI: CCR register with all communications parameters */
2239 WRITE_REG(hqspi->Instance->CCR, (cmd->DdrMode | cmd->DdrHoldHalfCycle | cmd->SIOOMode |
2240 cmd->DataMode | (cmd->DummyCycles << 18) | cmd->AlternateByteMode |
2241 cmd->AddressSize | cmd->AddressMode | cmd->InstructionMode |
2242 cmd->Instruction | FunctionalMode));
2244 if (FunctionalMode != QSPI_FUNCTIONAL_MODE_MEMORY_MAPPED)
2246 /* Configure QSPI: AR register with address value */
2247 WRITE_REG(hqspi->Instance->AR, cmd->Address);
2250 else
2252 /*---- Command with only instruction ----*/
2253 /* Configure QSPI: CCR register with all communications parameters */
2254 WRITE_REG(hqspi->Instance->CCR, (cmd->DdrMode | cmd->DdrHoldHalfCycle | cmd->SIOOMode |
2255 cmd->DataMode | (cmd->DummyCycles << 18) | cmd->AlternateByteMode |
2256 cmd->AddressMode | cmd->InstructionMode | cmd->Instruction |
2257 FunctionalMode));
2261 else
2263 if (cmd->AlternateByteMode != QSPI_ALTERNATE_BYTES_NONE)
2265 /* Configure QSPI: ABR register with alternate bytes value */
2266 WRITE_REG(hqspi->Instance->ABR, cmd->AlternateBytes);
2268 if (cmd->AddressMode != QSPI_ADDRESS_NONE)
2270 /*---- Command with address and alternate bytes ----*/
2271 /* Configure QSPI: CCR register with all communications parameters */
2272 WRITE_REG(hqspi->Instance->CCR, (cmd->DdrMode | cmd->DdrHoldHalfCycle | cmd->SIOOMode |
2273 cmd->DataMode | (cmd->DummyCycles << 18) | cmd->AlternateBytesSize |
2274 cmd->AlternateByteMode | cmd->AddressSize | cmd->AddressMode |
2275 cmd->InstructionMode | FunctionalMode));
2277 if (FunctionalMode != QSPI_FUNCTIONAL_MODE_MEMORY_MAPPED)
2279 /* Configure QSPI: AR register with address value */
2280 WRITE_REG(hqspi->Instance->AR, cmd->Address);
2283 else
2285 /*---- Command with only alternate bytes ----*/
2286 /* Configure QSPI: CCR register with all communications parameters */
2287 WRITE_REG(hqspi->Instance->CCR, (cmd->DdrMode | cmd->DdrHoldHalfCycle | cmd->SIOOMode |
2288 cmd->DataMode | (cmd->DummyCycles << 18) | cmd->AlternateBytesSize |
2289 cmd->AlternateByteMode | cmd->AddressMode | cmd->InstructionMode |
2290 FunctionalMode));
2293 else
2295 if (cmd->AddressMode != QSPI_ADDRESS_NONE)
2297 /*---- Command with only address ----*/
2298 /* Configure QSPI: CCR register with all communications parameters */
2299 WRITE_REG(hqspi->Instance->CCR, (cmd->DdrMode | cmd->DdrHoldHalfCycle | cmd->SIOOMode |
2300 cmd->DataMode | (cmd->DummyCycles << 18) | cmd->AlternateByteMode |
2301 cmd->AddressSize | cmd->AddressMode | cmd->InstructionMode |
2302 FunctionalMode));
2304 if (FunctionalMode != QSPI_FUNCTIONAL_MODE_MEMORY_MAPPED)
2306 /* Configure QSPI: AR register with address value */
2307 WRITE_REG(hqspi->Instance->AR, cmd->Address);
2310 else
2312 /*---- Command with only data phase ----*/
2313 if (cmd->DataMode != QSPI_DATA_NONE)
2315 /* Configure QSPI: CCR register with all communications parameters */
2316 WRITE_REG(hqspi->Instance->CCR, (cmd->DdrMode | cmd->DdrHoldHalfCycle | cmd->SIOOMode |
2317 cmd->DataMode | (cmd->DummyCycles << 18) | cmd->AlternateByteMode |
2318 cmd->AddressMode | cmd->InstructionMode | FunctionalMode));
2325 * @}
2328 #endif /* HAL_QSPI_MODULE_ENABLED */
2330 * @}
2334 * @}
2337 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/