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.
 
 
 

1982 lines
64 KiB

  1. /**
  2. ******************************************************************************
  3. * @file stm32l4xx_hal_qspi.c
  4. * @author MCD Application Team
  5. * @version V1.3.0
  6. * @date 29-January-2016
  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
  17. *
  18. *
  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.
  40. *** Indirect functional mode ***
  41. ================================
  42. [..]
  43. (#) Configure the command sequence using the HAL_QSPI_Command() or HAL_QSPI_Command_IT()
  44. functions :
  45. (++) Instruction phase : the mode used and if present the instruction opcode.
  46. (++) Address phase : the mode used and if present the size and the address value.
  47. (++) Alternate-bytes phase : the mode used and if present the size and the alternate
  48. bytes values.
  49. (++) Dummy-cycles phase : the number of dummy cycles (mode used is same as data phase).
  50. (++) Data phase : the mode used and if present the number of bytes.
  51. (++) Double Data Rate (DDR) mode : the activation (or not) of this mode and the delay
  52. if activated.
  53. (++) Sending Instruction Only Once (SIOO) mode : the activation (or not) of this mode.
  54. (#) If no data is required for the command, it is sent directly to the memory :
  55. (++) In polling mode, the output of the function is done when the transfer is complete.
  56. (++) In interrupt mode, HAL_QSPI_CmdCpltCallback() will be called when the transfer is complete.
  57. (#) For the indirect write mode, use HAL_QSPI_Transmit(), HAL_QSPI_Transmit_DMA() or
  58. HAL_QSPI_Transmit_IT() after the command configuration :
  59. (++) In polling mode, the output of the function is done when the transfer is complete.
  60. (++) In interrupt mode, HAL_QSPI_FifoThresholdCallback() will be called when the fifo threshold
  61. is reached and HAL_QSPI_TxCpltCallback() will be called when the transfer is complete.
  62. (++) In DMA mode, HAL_QSPI_TxHalfCpltCallback() will be called at the half transfer and
  63. HAL_QSPI_TxCpltCallback() will be called when the transfer is complete.
  64. (#) For the indirect read mode, use HAL_QSPI_Receive(), HAL_QSPI_Receive_DMA() or
  65. HAL_QSPI_Receive_IT() after the command configuration :
  66. (++) In polling mode, the output of the function is done when the transfer is complete.
  67. (++) In interrupt mode, HAL_QSPI_FifoThresholdCallback() will be called when the fifo threshold
  68. is reached and HAL_QSPI_RxCpltCallback() will be called when the transfer is complete.
  69. (++) In DMA mode, HAL_QSPI_RxHalfCpltCallback() will be called at the half transfer and
  70. HAL_QSPI_RxCpltCallback() will be called when the transfer is complete.
  71. *** Auto-polling functional mode ***
  72. ====================================
  73. [..]
  74. (#) Configure the command sequence and the auto-polling functional mode using the
  75. HAL_QSPI_AutoPolling() or HAL_QSPI_AutoPolling_IT() functions :
  76. (++) Instruction phase : the mode used and if present the instruction opcode.
  77. (++) Address phase : the mode used and if present the size and the address value.
  78. (++) Alternate-bytes phase : the mode used and if present the size and the alternate
  79. bytes values.
  80. (++) Dummy-cycles phase : the number of dummy cycles (mode used is same as data phase).
  81. (++) Data phase : the mode used.
  82. (++) Double Data Rate (DDR) mode : the activation (or not) of this mode and the delay
  83. if activated.
  84. (++) Sending Instruction Only Once (SIOO) mode : the activation (or not) of this mode.
  85. (++) The size of the status bytes, the match value, the mask used, the match mode (OR/AND),
  86. the polling interval and the automatic stop activation.
  87. (#) After the configuration :
  88. (++) In polling mode, the output of the function is done when the status match is reached. The
  89. automatic stop is activated to avoid an infinite loop.
  90. (++) In interrupt mode, HAL_QSPI_StatusMatchCallback() will be called each time the status match is reached.
  91. *** Memory-mapped functional mode ***
  92. =====================================
  93. [..]
  94. (#) Configure the command sequence and the memory-mapped functional mode using the
  95. HAL_QSPI_MemoryMapped() functions :
  96. (++) Instruction phase : the mode used and if present the instruction opcode.
  97. (++) Address phase : the mode used and the size.
  98. (++) Alternate-bytes phase : the mode used and if present the size and the alternate
  99. bytes values.
  100. (++) Dummy-cycles phase : the number of dummy cycles (mode used is same as data phase).
  101. (++) Data phase : the mode used.
  102. (++) Double Data Rate (DDR) mode : the activation (or not) of this mode and the delay
  103. if activated.
  104. (++) Sending Instruction Only Once (SIOO) mode : the activation (or not) of this mode.
  105. (++) The timeout activation and the timeout period.
  106. (#) After the configuration, the QuadSPI will be used as soon as an access on the AHB is done on
  107. the address range. HAL_QSPI_TimeOutCallback() will be called when the timeout expires.
  108. *** Errors management and abort functionality ***
  109. ==================================================
  110. [..]
  111. (#) HAL_QSPI_GetError() function gives the error raised during the last operation.
  112. (#) HAL_QSPI_Abort() function aborts any on-going operation and flushes the fifo.
  113. (#) HAL_QSPI_GetState() function gives the current state of the HAL QuadSPI driver.
  114. *** Workarounds linked to Silicon Limitation ***
  115. ====================================================
  116. [..]
  117. (#) Workarounds Implemented inside HAL Driver
  118. (++) Extra data written in the FIFO at the end of a read transfer
  119. @endverbatim
  120. ******************************************************************************
  121. * @attention
  122. *
  123. * <h2><center>&copy; COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
  124. *
  125. * Redistribution and use in source and binary forms, with or without modification,
  126. * are permitted provided that the following conditions are met:
  127. * 1. Redistributions of source code must retain the above copyright notice,
  128. * this list of conditions and the following disclaimer.
  129. * 2. Redistributions in binary form must reproduce the above copyright notice,
  130. * this list of conditions and the following disclaimer in the documentation
  131. * and/or other materials provided with the distribution.
  132. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  133. * may be used to endorse or promote products derived from this software
  134. * without specific prior written permission.
  135. *
  136. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  137. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  138. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  139. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  140. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  141. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  142. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  143. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  144. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  145. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  146. *
  147. ******************************************************************************
  148. */
  149. /* Includes ------------------------------------------------------------------*/
  150. #include "stm32l4xx_hal.h"
  151. /** @addtogroup STM32L4xx_HAL_Driver
  152. * @{
  153. */
  154. /** @defgroup QSPI QSPI
  155. * @brief QSPI HAL module driver
  156. * @{
  157. */
  158. #ifdef HAL_QSPI_MODULE_ENABLED
  159. /* Private typedef -----------------------------------------------------------*/
  160. /* Private define ------------------------------------------------------------*/
  161. /** @defgroup QSPI_Private_Constants QSPI Private Constants
  162. * @{
  163. */
  164. #define QSPI_FUNCTIONAL_MODE_INDIRECT_WRITE ((uint32_t)0x00000000) /*!<Indirect write mode*/
  165. #define QSPI_FUNCTIONAL_MODE_INDIRECT_READ ((uint32_t)QUADSPI_CCR_FMODE_0) /*!<Indirect read mode*/
  166. #define QSPI_FUNCTIONAL_MODE_AUTO_POLLING ((uint32_t)QUADSPI_CCR_FMODE_1) /*!<Automatic polling mode*/
  167. #define QSPI_FUNCTIONAL_MODE_MEMORY_MAPPED ((uint32_t)QUADSPI_CCR_FMODE) /*!<Memory-mapped mode*/
  168. /**
  169. * @}
  170. */
  171. /* Private macro -------------------------------------------------------------*/
  172. /** @defgroup QSPI_Private_Macros QSPI Private Macros
  173. * @{
  174. */
  175. #define IS_QSPI_FUNCTIONAL_MODE(MODE) (((MODE) == QSPI_FUNCTIONAL_MODE_INDIRECT_WRITE) || \
  176. ((MODE) == QSPI_FUNCTIONAL_MODE_INDIRECT_READ) || \
  177. ((MODE) == QSPI_FUNCTIONAL_MODE_AUTO_POLLING) || \
  178. ((MODE) == QSPI_FUNCTIONAL_MODE_MEMORY_MAPPED))
  179. /**
  180. * @}
  181. */
  182. /* Private variables ---------------------------------------------------------*/
  183. /* Private function prototypes -----------------------------------------------*/
  184. static void QSPI_DMARxCplt(DMA_HandleTypeDef *hdma);
  185. static void QSPI_DMATxCplt(DMA_HandleTypeDef *hdma);
  186. static void QSPI_DMARxHalfCplt(DMA_HandleTypeDef *hdma);
  187. static void QSPI_DMATxHalfCplt(DMA_HandleTypeDef *hdma);
  188. static void QSPI_DMAError(DMA_HandleTypeDef *hdma);
  189. static HAL_StatusTypeDef QSPI_WaitFlagStateUntilTimeout(QSPI_HandleTypeDef *hqspi, uint32_t Flag, FlagStatus State, uint32_t Timeout);
  190. static void QSPI_Config(QSPI_HandleTypeDef *hqspi, QSPI_CommandTypeDef *cmd, uint32_t FunctionalMode);
  191. /* Exported functions --------------------------------------------------------*/
  192. /** @defgroup QSPI_Exported_Functions QSPI Exported Functions
  193. * @{
  194. */
  195. /** @defgroup QSPI_Exported_Functions_Group1 Initialization/de-initialization functions
  196. * @brief Initialization and Configuration functions
  197. *
  198. @verbatim
  199. ===============================================================================
  200. ##### Initialization and Configuration functions #####
  201. ===============================================================================
  202. [..]
  203. This subsection provides a set of functions allowing to :
  204. (+) Initialize the QuadSPI.
  205. (+) De-initialize the QuadSPI.
  206. @endverbatim
  207. * @{
  208. */
  209. /**
  210. * @brief Initialize the QSPI mode according to the specified parameters
  211. * in the QSPI_InitTypeDef and initialize the associated handle.
  212. * @param hqspi: QSPI handle
  213. * @retval HAL status
  214. */
  215. HAL_StatusTypeDef HAL_QSPI_Init(QSPI_HandleTypeDef *hqspi)
  216. {
  217. HAL_StatusTypeDef status = HAL_ERROR;
  218. /* Check the QSPI handle allocation */
  219. if(hqspi == NULL)
  220. {
  221. return HAL_ERROR;
  222. }
  223. /* Check the parameters */
  224. assert_param(IS_QSPI_ALL_INSTANCE(hqspi->Instance));
  225. assert_param(IS_QSPI_CLOCK_PRESCALER(hqspi->Init.ClockPrescaler));
  226. assert_param(IS_QSPI_FIFO_THRESHOLD(hqspi->Init.FifoThreshold));
  227. assert_param(IS_QSPI_SSHIFT(hqspi->Init.SampleShifting));
  228. assert_param(IS_QSPI_FLASH_SIZE(hqspi->Init.FlashSize));
  229. assert_param(IS_QSPI_CS_HIGH_TIME(hqspi->Init.ChipSelectHighTime));
  230. assert_param(IS_QSPI_CLOCK_MODE(hqspi->Init.ClockMode));
  231. /* Process locked */
  232. __HAL_LOCK(hqspi);
  233. if(hqspi->State == HAL_QSPI_STATE_RESET)
  234. {
  235. /* Allocate lock resource and initialize it */
  236. hqspi->Lock = HAL_UNLOCKED;
  237. /* Init the low level hardware : GPIO, CLOCK */
  238. HAL_QSPI_MspInit(hqspi);
  239. /* Configure the default timeout for the QSPI memory access */
  240. HAL_QSPI_SetTimeout(hqspi, HAL_QPSI_TIMEOUT_DEFAULT_VALUE);
  241. }
  242. /* Configure QSPI FIFO Threshold */
  243. MODIFY_REG(hqspi->Instance->CR, QUADSPI_CR_FTHRES,
  244. ((hqspi->Init.FifoThreshold - 1) << POSITION_VAL(QUADSPI_CR_FTHRES)));
  245. /* Wait till BUSY flag reset */
  246. status = QSPI_WaitFlagStateUntilTimeout(hqspi, QSPI_FLAG_BUSY, RESET, hqspi->Timeout);
  247. if(status == HAL_OK)
  248. {
  249. /* Configure QSPI Clock Prescaler and Sample Shift */
  250. MODIFY_REG(hqspi->Instance->CR, (QUADSPI_CR_PRESCALER | QUADSPI_CR_SSHIFT),
  251. ((hqspi->Init.ClockPrescaler << POSITION_VAL(QUADSPI_CR_PRESCALER)) |
  252. hqspi->Init.SampleShifting));
  253. /* Configure QSPI Flash Size, CS High Time and Clock Mode */
  254. MODIFY_REG(hqspi->Instance->DCR, (QUADSPI_DCR_FSIZE | QUADSPI_DCR_CSHT | QUADSPI_DCR_CKMODE),
  255. ((hqspi->Init.FlashSize << POSITION_VAL(QUADSPI_DCR_FSIZE)) |
  256. hqspi->Init.ChipSelectHighTime | hqspi->Init.ClockMode));
  257. /* Enable the QSPI peripheral */
  258. __HAL_QSPI_ENABLE(hqspi);
  259. /* Set QSPI error code to none */
  260. hqspi->ErrorCode = HAL_QSPI_ERROR_NONE;
  261. /* Initialize the QSPI state */
  262. hqspi->State = HAL_QSPI_STATE_READY;
  263. }
  264. /* Release Lock */
  265. __HAL_UNLOCK(hqspi);
  266. /* Return function status */
  267. return status;
  268. }
  269. /**
  270. * @brief De-Initialize the QSPI peripheral.
  271. * @param hqspi: QSPI handle
  272. * @retval HAL status
  273. */
  274. HAL_StatusTypeDef HAL_QSPI_DeInit(QSPI_HandleTypeDef *hqspi)
  275. {
  276. /* Check the QSPI handle allocation */
  277. if(hqspi == NULL)
  278. {
  279. return HAL_ERROR;
  280. }
  281. /* Process locked */
  282. __HAL_LOCK(hqspi);
  283. /* Disable the QSPI Peripheral Clock */
  284. __HAL_QSPI_DISABLE(hqspi);
  285. /* DeInit the low level hardware: GPIO, CLOCK, NVIC... */
  286. HAL_QSPI_MspDeInit(hqspi);
  287. /* Set QSPI error code to none */
  288. hqspi->ErrorCode = HAL_QSPI_ERROR_NONE;
  289. /* Initialize the QSPI state */
  290. hqspi->State = HAL_QSPI_STATE_RESET;
  291. /* Release Lock */
  292. __HAL_UNLOCK(hqspi);
  293. return HAL_OK;
  294. }
  295. /**
  296. * @brief Initialize the QSPI MSP.
  297. * @param hqspi: QSPI handle
  298. * @retval None
  299. */
  300. __weak void HAL_QSPI_MspInit(QSPI_HandleTypeDef *hqspi)
  301. {
  302. /* Prevent unused argument(s) compilation warning */
  303. UNUSED(hqspi);
  304. /* NOTE : This function should not be modified, when the callback is needed,
  305. the HAL_QSPI_MspInit can be implemented in the user file
  306. */
  307. }
  308. /**
  309. * @brief DeInitialize the QSPI MSP.
  310. * @param hqspi: QSPI handle
  311. * @retval None
  312. */
  313. __weak void HAL_QSPI_MspDeInit(QSPI_HandleTypeDef *hqspi)
  314. {
  315. /* Prevent unused argument(s) compilation warning */
  316. UNUSED(hqspi);
  317. /* NOTE : This function should not be modified, when the callback is needed,
  318. the HAL_QSPI_MspDeInit can be implemented in the user file
  319. */
  320. }
  321. /**
  322. * @}
  323. */
  324. /** @defgroup QSPI_Exported_Functions_Group2 Input and Output operation functions
  325. * @brief QSPI Transmit/Receive functions
  326. *
  327. @verbatim
  328. ===============================================================================
  329. ##### IO operation functions #####
  330. ===============================================================================
  331. [..]
  332. This subsection provides a set of functions allowing to :
  333. (+) Handle the interrupts.
  334. (+) Handle the command sequence.
  335. (+) Transmit data in blocking, interrupt or DMA mode.
  336. (+) Receive data in blocking, interrupt or DMA mode.
  337. (+) Manage the auto-polling functional mode.
  338. (+) Manage the memory-mapped functional mode.
  339. @endverbatim
  340. * @{
  341. */
  342. /**
  343. * @brief Handle QSPI interrupt request.
  344. * @param hqspi: QSPI handle
  345. * @retval None
  346. */
  347. void HAL_QSPI_IRQHandler(QSPI_HandleTypeDef *hqspi)
  348. {
  349. __IO uint32_t *data_reg;
  350. uint32_t flag = 0, itsource = 0;
  351. /* QSPI Fifo Threshold interrupt occurred ----------------------------------*/
  352. flag = __HAL_QSPI_GET_FLAG(hqspi, QSPI_FLAG_FT);
  353. itsource = __HAL_QSPI_GET_IT_SOURCE(hqspi, QSPI_IT_FT);
  354. if((flag != RESET) && (itsource != RESET))
  355. {
  356. data_reg = &hqspi->Instance->DR;
  357. if(hqspi->State == HAL_QSPI_STATE_BUSY_INDIRECT_TX)
  358. {
  359. /* Transmission process */
  360. while(__HAL_QSPI_GET_FLAG(hqspi, QSPI_FLAG_FT) != 0)
  361. {
  362. if (hqspi->TxXferCount > 0)
  363. {
  364. /* Fill the FIFO until the threshold is reached */
  365. *(__IO uint8_t *)data_reg = *hqspi->pTxBuffPtr++;
  366. hqspi->TxXferCount--;
  367. }
  368. else
  369. {
  370. /* No more data available for the transfer */
  371. /* Disable the QSPI FIFO Threshold Interrupt */
  372. __HAL_QSPI_DISABLE_IT(hqspi, QSPI_IT_FT);
  373. break;
  374. }
  375. }
  376. }
  377. else if(hqspi->State == HAL_QSPI_STATE_BUSY_INDIRECT_RX)
  378. {
  379. /* Receiving Process */
  380. while(__HAL_QSPI_GET_FLAG(hqspi, QSPI_FLAG_FT) != 0)
  381. {
  382. if (hqspi->RxXferCount > 0)
  383. {
  384. /* Read the FIFO until the threshold is reached */
  385. *hqspi->pRxBuffPtr++ = *(__IO uint8_t *)data_reg;
  386. hqspi->RxXferCount--;
  387. }
  388. else
  389. {
  390. /* All data have been received for the transfer */
  391. /* Disable the QSPI FIFO Threshold Interrupt */
  392. __HAL_QSPI_DISABLE_IT(hqspi, QSPI_IT_FT);
  393. break;
  394. }
  395. }
  396. }
  397. /* FIFO Threshold callback */
  398. HAL_QSPI_FifoThresholdCallback(hqspi);
  399. }
  400. /* QSPI Transfer Complete interrupt occurred -------------------------------*/
  401. flag = __HAL_QSPI_GET_FLAG(hqspi, QSPI_FLAG_TC);
  402. itsource = __HAL_QSPI_GET_IT_SOURCE(hqspi, QSPI_IT_TC);
  403. if((flag != RESET) && (itsource != RESET))
  404. {
  405. /* Clear interrupt */
  406. __HAL_QSPI_CLEAR_FLAG(hqspi, QSPI_FLAG_TC);
  407. /* Disable the QSPI FIFO Threshold, Transfer Error and Transfer complete Interrupts */
  408. __HAL_QSPI_DISABLE_IT(hqspi, QSPI_IT_TC | QSPI_IT_TE | QSPI_IT_FT);
  409. /* Transfer complete callback */
  410. if(hqspi->State == HAL_QSPI_STATE_BUSY_INDIRECT_TX)
  411. {
  412. /* Clear Busy bit */
  413. HAL_QSPI_Abort(hqspi);
  414. /* TX Complete callback */
  415. HAL_QSPI_TxCpltCallback(hqspi);
  416. }
  417. else if(hqspi->State == HAL_QSPI_STATE_BUSY_INDIRECT_RX)
  418. {
  419. data_reg = &hqspi->Instance->DR;
  420. while(READ_BIT(hqspi->Instance->SR, QUADSPI_SR_FLEVEL) != 0)
  421. {
  422. if (hqspi->RxXferCount > 0)
  423. {
  424. /* Read the last data received in the FIFO until it is empty */
  425. *hqspi->pRxBuffPtr++ = *(__IO uint8_t *)data_reg;
  426. hqspi->RxXferCount--;
  427. }
  428. else
  429. {
  430. /* All data have been received for the transfer */
  431. break;
  432. }
  433. }
  434. /* Workaround - Extra data written in the FIFO at the end of a read transfer */
  435. HAL_QSPI_Abort(hqspi);
  436. /* RX Complete callback */
  437. HAL_QSPI_RxCpltCallback(hqspi);
  438. }
  439. else if(hqspi->State == HAL_QSPI_STATE_BUSY)
  440. {
  441. /* Command Complete callback */
  442. HAL_QSPI_CmdCpltCallback(hqspi);
  443. }
  444. /* Change state of QSPI */
  445. hqspi->State = HAL_QSPI_STATE_READY;
  446. }
  447. /* QSPI Status Match interrupt occurred ------------------------------------*/
  448. flag = __HAL_QSPI_GET_FLAG(hqspi, QSPI_FLAG_SM);
  449. itsource = __HAL_QSPI_GET_IT_SOURCE(hqspi, QSPI_IT_SM);
  450. if((flag != RESET) && (itsource != RESET))
  451. {
  452. /* Clear interrupt */
  453. __HAL_QSPI_CLEAR_FLAG(hqspi, QSPI_FLAG_SM);
  454. /* Check if the automatic poll mode stop is activated */
  455. if(READ_BIT(hqspi->Instance->CR, QUADSPI_CR_APMS) != 0)
  456. {
  457. /* Disable the QSPI Transfer Error and Status Match Interrupts */
  458. __HAL_QSPI_DISABLE_IT(hqspi, (QSPI_IT_SM | QSPI_IT_TE));
  459. /* Change state of QSPI */
  460. hqspi->State = HAL_QSPI_STATE_READY;
  461. }
  462. /* Status match callback */
  463. HAL_QSPI_StatusMatchCallback(hqspi);
  464. }
  465. /* QSPI Transfer Error interrupt occurred ----------------------------------*/
  466. flag = __HAL_QSPI_GET_FLAG(hqspi, QSPI_FLAG_TE);
  467. itsource = __HAL_QSPI_GET_IT_SOURCE(hqspi, QSPI_IT_TE);
  468. if((flag != RESET) && (itsource != RESET))
  469. {
  470. /* Clear interrupt */
  471. __HAL_QSPI_CLEAR_FLAG(hqspi, QSPI_FLAG_TE);
  472. /* Disable all the QSPI Interrupts */
  473. __HAL_QSPI_DISABLE_IT(hqspi, QSPI_IT_SM | QSPI_IT_TC | QSPI_IT_TE | QSPI_IT_FT);
  474. /* Set error code */
  475. hqspi->ErrorCode |= HAL_QSPI_ERROR_TRANSFER;
  476. /* Change state of QSPI */
  477. hqspi->State = HAL_QSPI_STATE_ERROR;
  478. /* Error callback */
  479. HAL_QSPI_ErrorCallback(hqspi);
  480. }
  481. /* QSPI Timeout interrupt occurred -----------------------------------------*/
  482. flag = __HAL_QSPI_GET_FLAG(hqspi, QSPI_FLAG_TO);
  483. itsource = __HAL_QSPI_GET_IT_SOURCE(hqspi, QSPI_IT_TO);
  484. if((flag != RESET) && (itsource != RESET))
  485. {
  486. /* Clear interrupt */
  487. __HAL_QSPI_CLEAR_FLAG(hqspi, QSPI_FLAG_TO);
  488. /* Timeout callback */
  489. HAL_QSPI_TimeOutCallback(hqspi);
  490. }
  491. }
  492. /**
  493. * @brief Set the command configuration.
  494. * @param hqspi: QSPI handle
  495. * @param cmd : structure that contains the command configuration information
  496. * @param Timeout : Timeout duration
  497. * @note This function is used only in Indirect Read or Write Modes
  498. * @retval HAL status
  499. */
  500. HAL_StatusTypeDef HAL_QSPI_Command(QSPI_HandleTypeDef *hqspi, QSPI_CommandTypeDef *cmd, uint32_t Timeout)
  501. {
  502. HAL_StatusTypeDef status = HAL_ERROR;
  503. /* Check the parameters */
  504. assert_param(IS_QSPI_INSTRUCTION_MODE(cmd->InstructionMode));
  505. if (cmd->InstructionMode != QSPI_INSTRUCTION_NONE)
  506. {
  507. assert_param(IS_QSPI_INSTRUCTION(cmd->Instruction));
  508. }
  509. assert_param(IS_QSPI_ADDRESS_MODE(cmd->AddressMode));
  510. if (cmd->AddressMode != QSPI_ADDRESS_NONE)
  511. {
  512. assert_param(IS_QSPI_ADDRESS_SIZE(cmd->AddressSize));
  513. }
  514. assert_param(IS_QSPI_ALTERNATE_BYTES_MODE(cmd->AlternateByteMode));
  515. if (cmd->AlternateByteMode != QSPI_ALTERNATE_BYTES_NONE)
  516. {
  517. assert_param(IS_QSPI_ALTERNATE_BYTES_SIZE(cmd->AlternateBytesSize));
  518. }
  519. assert_param(IS_QSPI_DUMMY_CYCLES(cmd->DummyCycles));
  520. assert_param(IS_QSPI_DATA_MODE(cmd->DataMode));
  521. assert_param(IS_QSPI_DDR_MODE(cmd->DdrMode));
  522. assert_param(IS_QSPI_DDR_HHC(cmd->DdrHoldHalfCycle));
  523. assert_param(IS_QSPI_SIOO_MODE(cmd->SIOOMode));
  524. /* Process locked */
  525. __HAL_LOCK(hqspi);
  526. if(hqspi->State == HAL_QSPI_STATE_READY)
  527. {
  528. hqspi->ErrorCode = HAL_QSPI_ERROR_NONE;
  529. /* Update QSPI state */
  530. hqspi->State = HAL_QSPI_STATE_BUSY;
  531. /* Wait till BUSY flag reset */
  532. status = QSPI_WaitFlagStateUntilTimeout(hqspi, QSPI_FLAG_BUSY, RESET, Timeout);
  533. if (status == HAL_OK)
  534. {
  535. /* Call the configuration function */
  536. QSPI_Config(hqspi, cmd, QSPI_FUNCTIONAL_MODE_INDIRECT_WRITE);
  537. if (cmd->DataMode == QSPI_DATA_NONE)
  538. {
  539. /* When there is no data phase, the transfer start as soon as the configuration is done
  540. so wait until TC flag is set to go back in idle state */
  541. if(QSPI_WaitFlagStateUntilTimeout(hqspi, QSPI_FLAG_TC, SET, Timeout) != HAL_OK)
  542. {
  543. status = HAL_TIMEOUT;
  544. }
  545. else
  546. {
  547. __HAL_QSPI_CLEAR_FLAG(hqspi, QSPI_FLAG_TC);
  548. /* Update QSPI state */
  549. hqspi->State = HAL_QSPI_STATE_READY;
  550. }
  551. }
  552. else
  553. {
  554. /* Update QSPI state */
  555. hqspi->State = HAL_QSPI_STATE_READY;
  556. }
  557. }
  558. }
  559. else
  560. {
  561. status = HAL_BUSY;
  562. }
  563. /* Process unlocked */
  564. __HAL_UNLOCK(hqspi);
  565. /* Return function status */
  566. return status;
  567. }
  568. /**
  569. * @brief Set the command configuration in interrupt mode.
  570. * @param hqspi: QSPI handle
  571. * @param cmd : structure that contains the command configuration information
  572. * @note This function is used only in Indirect Read or Write Modes
  573. * @retval HAL status
  574. */
  575. HAL_StatusTypeDef HAL_QSPI_Command_IT(QSPI_HandleTypeDef *hqspi, QSPI_CommandTypeDef *cmd)
  576. {
  577. HAL_StatusTypeDef status = HAL_ERROR;
  578. /* Check the parameters */
  579. assert_param(IS_QSPI_INSTRUCTION_MODE(cmd->InstructionMode));
  580. if (cmd->InstructionMode != QSPI_INSTRUCTION_NONE)
  581. {
  582. assert_param(IS_QSPI_INSTRUCTION(cmd->Instruction));
  583. }
  584. assert_param(IS_QSPI_ADDRESS_MODE(cmd->AddressMode));
  585. if (cmd->AddressMode != QSPI_ADDRESS_NONE)
  586. {
  587. assert_param(IS_QSPI_ADDRESS_SIZE(cmd->AddressSize));
  588. }
  589. assert_param(IS_QSPI_ALTERNATE_BYTES_MODE(cmd->AlternateByteMode));
  590. if (cmd->AlternateByteMode != QSPI_ALTERNATE_BYTES_NONE)
  591. {
  592. assert_param(IS_QSPI_ALTERNATE_BYTES_SIZE(cmd->AlternateBytesSize));
  593. }
  594. assert_param(IS_QSPI_DUMMY_CYCLES(cmd->DummyCycles));
  595. assert_param(IS_QSPI_DATA_MODE(cmd->DataMode));
  596. assert_param(IS_QSPI_DDR_MODE(cmd->DdrMode));
  597. assert_param(IS_QSPI_DDR_HHC(cmd->DdrHoldHalfCycle));
  598. assert_param(IS_QSPI_SIOO_MODE(cmd->SIOOMode));
  599. /* Process locked */
  600. __HAL_LOCK(hqspi);
  601. if(hqspi->State == HAL_QSPI_STATE_READY)
  602. {
  603. hqspi->ErrorCode = HAL_QSPI_ERROR_NONE;
  604. /* Update QSPI state */
  605. hqspi->State = HAL_QSPI_STATE_BUSY;
  606. /* Wait till BUSY flag reset */
  607. status = QSPI_WaitFlagStateUntilTimeout(hqspi, QSPI_FLAG_BUSY, RESET, hqspi->Timeout);
  608. if (status == HAL_OK)
  609. {
  610. if (cmd->DataMode == QSPI_DATA_NONE)
  611. {
  612. /* When there is no data phase, the transfer start as soon as the configuration is done
  613. so activate TC and TE interrupts */
  614. /* Clear interrupt */
  615. __HAL_QSPI_CLEAR_FLAG(hqspi, QSPI_FLAG_TE | QSPI_FLAG_TC);
  616. /* Enable the QSPI Transfer Error Interrupt */
  617. __HAL_QSPI_ENABLE_IT(hqspi, QSPI_IT_TE | QSPI_IT_TC);
  618. }
  619. /* Call the configuration function */
  620. QSPI_Config(hqspi, cmd, QSPI_FUNCTIONAL_MODE_INDIRECT_WRITE);
  621. if (cmd->DataMode != QSPI_DATA_NONE)
  622. {
  623. /* Update QSPI state */
  624. hqspi->State = HAL_QSPI_STATE_READY;
  625. }
  626. }
  627. }
  628. else
  629. {
  630. status = HAL_BUSY;
  631. }
  632. /* Process unlocked */
  633. __HAL_UNLOCK(hqspi);
  634. /* Return function status */
  635. return status;
  636. }
  637. /**
  638. * @brief Transmit an amount of data in blocking mode.
  639. * @param hqspi: QSPI handle
  640. * @param pData: pointer to data buffer
  641. * @param Timeout : Timeout duration
  642. * @note This function is used only in Indirect Write Mode
  643. * @retval HAL status
  644. */
  645. HAL_StatusTypeDef HAL_QSPI_Transmit(QSPI_HandleTypeDef *hqspi, uint8_t *pData, uint32_t Timeout)
  646. {
  647. HAL_StatusTypeDef status = HAL_OK;
  648. __IO uint32_t *data_reg = &hqspi->Instance->DR;
  649. /* Process locked */
  650. __HAL_LOCK(hqspi);
  651. if(hqspi->State == HAL_QSPI_STATE_READY)
  652. {
  653. if(pData != NULL )
  654. {
  655. hqspi->ErrorCode = HAL_QSPI_ERROR_NONE;
  656. /* Update state */
  657. hqspi->State = HAL_QSPI_STATE_BUSY_INDIRECT_TX;
  658. /* Configure counters and size of the handle */
  659. hqspi->TxXferCount = READ_REG(hqspi->Instance->DLR) + 1;
  660. hqspi->TxXferSize = READ_REG(hqspi->Instance->DLR) + 1;
  661. hqspi->pTxBuffPtr = pData;
  662. /* Configure QSPI: CCR register with functional as indirect write */
  663. MODIFY_REG(hqspi->Instance->CCR, QUADSPI_CCR_FMODE, QSPI_FUNCTIONAL_MODE_INDIRECT_WRITE);
  664. while(hqspi->TxXferCount > 0)
  665. {
  666. /* Wait until FT flag is set to send data */
  667. if(QSPI_WaitFlagStateUntilTimeout(hqspi, QSPI_FLAG_FT, SET, Timeout) != HAL_OK)
  668. {
  669. status = HAL_TIMEOUT;
  670. break;
  671. }
  672. *(__IO uint8_t *)data_reg = *hqspi->pTxBuffPtr++;
  673. hqspi->TxXferCount--;
  674. }
  675. if (status == HAL_OK)
  676. {
  677. /* Wait until TC flag is set to go back in idle state */
  678. if(QSPI_WaitFlagStateUntilTimeout(hqspi, QSPI_FLAG_TC, SET, Timeout) != HAL_OK)
  679. {
  680. status = HAL_TIMEOUT;
  681. }
  682. else
  683. {
  684. /* Clear Transfer Complete bit */
  685. __HAL_QSPI_CLEAR_FLAG(hqspi, QSPI_FLAG_TC);
  686. /* Clear Busy bit */
  687. status = HAL_QSPI_Abort(hqspi);
  688. }
  689. }
  690. /* Update QSPI state */
  691. hqspi->State = HAL_QSPI_STATE_READY;
  692. }
  693. else
  694. {
  695. status = HAL_ERROR;
  696. }
  697. }
  698. else
  699. {
  700. status = HAL_BUSY;
  701. }
  702. /* Process unlocked */
  703. __HAL_UNLOCK(hqspi);
  704. return status;
  705. }
  706. /**
  707. * @brief Receive an amount of data in blocking mode.
  708. * @param hqspi: QSPI handle
  709. * @param pData: pointer to data buffer
  710. * @param Timeout : Timeout duration
  711. * @note This function is used only in Indirect Read Mode
  712. * @retval HAL status
  713. */
  714. HAL_StatusTypeDef HAL_QSPI_Receive(QSPI_HandleTypeDef *hqspi, uint8_t *pData, uint32_t Timeout)
  715. {
  716. HAL_StatusTypeDef status = HAL_OK;
  717. uint32_t addr_reg = READ_REG(hqspi->Instance->AR);
  718. __IO uint32_t *data_reg = &hqspi->Instance->DR;
  719. /* Process locked */
  720. __HAL_LOCK(hqspi);
  721. if(hqspi->State == HAL_QSPI_STATE_READY)
  722. {
  723. if(pData != NULL )
  724. {
  725. hqspi->ErrorCode = HAL_QSPI_ERROR_NONE;
  726. /* Update state */
  727. hqspi->State = HAL_QSPI_STATE_BUSY_INDIRECT_RX;
  728. /* Configure counters and size of the handle */
  729. hqspi->RxXferCount = READ_REG(hqspi->Instance->DLR) + 1;
  730. hqspi->RxXferSize = READ_REG(hqspi->Instance->DLR) + 1;
  731. hqspi->pRxBuffPtr = pData;
  732. /* Configure QSPI: CCR register with functional as indirect read */
  733. MODIFY_REG(hqspi->Instance->CCR, QUADSPI_CCR_FMODE, QSPI_FUNCTIONAL_MODE_INDIRECT_READ);
  734. /* Start the transfer by re-writing the address in AR register */
  735. WRITE_REG(hqspi->Instance->AR, addr_reg);
  736. while(hqspi->RxXferCount > 0)
  737. {
  738. /* Wait until FT or TC flag is set to read received data */
  739. if(QSPI_WaitFlagStateUntilTimeout(hqspi, (QSPI_FLAG_FT | QSPI_FLAG_TC), SET, Timeout) != HAL_OK)
  740. {
  741. status = HAL_TIMEOUT;
  742. break;
  743. }
  744. *hqspi->pRxBuffPtr++ = *(__IO uint8_t *)data_reg;
  745. hqspi->RxXferCount--;
  746. }
  747. if (status == HAL_OK)
  748. {
  749. /* Wait until TC flag is set to go back in idle state */
  750. if(QSPI_WaitFlagStateUntilTimeout(hqspi, QSPI_FLAG_TC, SET, Timeout) != HAL_OK)
  751. {
  752. status = HAL_TIMEOUT;
  753. }
  754. else
  755. {
  756. /* Clear Transfer Complete bit */
  757. __HAL_QSPI_CLEAR_FLAG(hqspi, QSPI_FLAG_TC);
  758. /* Workaround - Extra data written in the FIFO at the end of a read transfer */
  759. status = HAL_QSPI_Abort(hqspi);
  760. }
  761. }
  762. /* Update QSPI state */
  763. hqspi->State = HAL_QSPI_STATE_READY;
  764. }
  765. else
  766. {
  767. status = HAL_ERROR;
  768. }
  769. }
  770. else
  771. {
  772. status = HAL_BUSY;
  773. }
  774. /* Process unlocked */
  775. __HAL_UNLOCK(hqspi);
  776. return status;
  777. }
  778. /**
  779. * @brief Send an amount of data in non-blocking mode with interrupt.
  780. * @param hqspi: QSPI handle
  781. * @param pData: pointer to data buffer
  782. * @note This function is used only in Indirect Write Mode
  783. * @retval HAL status
  784. */
  785. HAL_StatusTypeDef HAL_QSPI_Transmit_IT(QSPI_HandleTypeDef *hqspi, uint8_t *pData)
  786. {
  787. HAL_StatusTypeDef status = HAL_OK;
  788. /* Process locked */
  789. __HAL_LOCK(hqspi);
  790. if(hqspi->State == HAL_QSPI_STATE_READY)
  791. {
  792. if(pData != NULL )
  793. {
  794. hqspi->ErrorCode = HAL_QSPI_ERROR_NONE;
  795. /* Update state */
  796. hqspi->State = HAL_QSPI_STATE_BUSY_INDIRECT_TX;
  797. /* Configure counters and size of the handle */
  798. hqspi->TxXferCount = READ_REG(hqspi->Instance->DLR) + 1;
  799. hqspi->TxXferSize = READ_REG(hqspi->Instance->DLR) + 1;
  800. hqspi->pTxBuffPtr = pData;
  801. /* Configure QSPI: CCR register with functional as indirect write */
  802. MODIFY_REG(hqspi->Instance->CCR, QUADSPI_CCR_FMODE, QSPI_FUNCTIONAL_MODE_INDIRECT_WRITE);
  803. /* Clear interrupt */
  804. __HAL_QSPI_CLEAR_FLAG(hqspi, QSPI_FLAG_TE | QSPI_FLAG_TC);
  805. /* Enable the QSPI transfer error, FIFO threshold and transfer complete Interrupts */
  806. __HAL_QSPI_ENABLE_IT(hqspi, QSPI_IT_TE | QSPI_IT_FT | QSPI_IT_TC);
  807. }
  808. else
  809. {
  810. status = HAL_ERROR;
  811. }
  812. }
  813. else
  814. {
  815. status = HAL_BUSY;
  816. }
  817. /* Process unlocked */
  818. __HAL_UNLOCK(hqspi);
  819. return status;
  820. }
  821. /**
  822. * @brief Receive an amount of data in non-blocking mode with interrupt.
  823. * @param hqspi: QSPI handle
  824. * @param pData: pointer to data buffer
  825. * @note This function is used only in Indirect Read Mode
  826. * @retval HAL status
  827. */
  828. HAL_StatusTypeDef HAL_QSPI_Receive_IT(QSPI_HandleTypeDef *hqspi, uint8_t *pData)
  829. {
  830. HAL_StatusTypeDef status = HAL_OK;
  831. uint32_t addr_reg = READ_REG(hqspi->Instance->AR);
  832. /* Process locked */
  833. __HAL_LOCK(hqspi);
  834. if(hqspi->State == HAL_QSPI_STATE_READY)
  835. {
  836. if(pData != NULL )
  837. {
  838. hqspi->ErrorCode = HAL_QSPI_ERROR_NONE;
  839. /* Update state */
  840. hqspi->State = HAL_QSPI_STATE_BUSY_INDIRECT_RX;
  841. /* Configure counters and size of the handle */
  842. hqspi->RxXferCount = READ_REG(hqspi->Instance->DLR) + 1;
  843. hqspi->RxXferSize = READ_REG(hqspi->Instance->DLR) + 1;
  844. hqspi->pRxBuffPtr = pData;
  845. /* Configure QSPI: CCR register with functional as indirect read */
  846. MODIFY_REG(hqspi->Instance->CCR, QUADSPI_CCR_FMODE, QSPI_FUNCTIONAL_MODE_INDIRECT_READ);
  847. /* Start the transfer by re-writing the address in AR register */
  848. WRITE_REG(hqspi->Instance->AR, addr_reg);
  849. /* Clear interrupt */
  850. __HAL_QSPI_CLEAR_FLAG(hqspi, QSPI_FLAG_TE | QSPI_FLAG_TC);
  851. /* Enable the QSPI transfer error, FIFO threshold and transfer complete Interrupts */
  852. __HAL_QSPI_ENABLE_IT(hqspi, QSPI_IT_TE | QSPI_IT_FT | QSPI_IT_TC);
  853. }
  854. else
  855. {
  856. status = HAL_ERROR;
  857. }
  858. }
  859. else
  860. {
  861. status = HAL_BUSY;
  862. }
  863. /* Process unlocked */
  864. __HAL_UNLOCK(hqspi);
  865. return status;
  866. }
  867. /**
  868. * @brief Send an amount of data in non-blocking mode with DMA.
  869. * @param hqspi: QSPI handle
  870. * @param pData: pointer to data buffer
  871. * @note This function is used only in Indirect Write Mode
  872. * @retval HAL status
  873. */
  874. HAL_StatusTypeDef HAL_QSPI_Transmit_DMA(QSPI_HandleTypeDef *hqspi, uint8_t *pData)
  875. {
  876. HAL_StatusTypeDef status = HAL_OK;
  877. uint32_t *tmp;
  878. /* Process locked */
  879. __HAL_LOCK(hqspi);
  880. if(hqspi->State == HAL_QSPI_STATE_READY)
  881. {
  882. if(pData != NULL )
  883. {
  884. hqspi->ErrorCode = HAL_QSPI_ERROR_NONE;
  885. /* Update state */
  886. hqspi->State = HAL_QSPI_STATE_BUSY_INDIRECT_TX;
  887. /* Configure counters and size of the handle */
  888. hqspi->TxXferCount = READ_REG(hqspi->Instance->DLR) + 1;
  889. hqspi->TxXferSize = READ_REG(hqspi->Instance->DLR) + 1;
  890. hqspi->pTxBuffPtr = pData;
  891. /* Configure QSPI: CCR register with functional mode as indirect write */
  892. MODIFY_REG(hqspi->Instance->CCR, QUADSPI_CCR_FMODE, QSPI_FUNCTIONAL_MODE_INDIRECT_WRITE);
  893. /* Set the QSPI DMA transfer complete callback */
  894. hqspi->hdma->XferCpltCallback = QSPI_DMATxCplt;
  895. /* Set the QSPI DMA Half transfer complete callback */
  896. hqspi->hdma->XferHalfCpltCallback = QSPI_DMATxHalfCplt;
  897. /* Set the DMA error callback */
  898. hqspi->hdma->XferErrorCallback = QSPI_DMAError;
  899. /* Configure the direction of the DMA */
  900. hqspi->hdma->Init.Direction = DMA_MEMORY_TO_PERIPH;
  901. MODIFY_REG(hqspi->hdma->Instance->CCR, DMA_CCR_DIR, hqspi->hdma->Init.Direction);
  902. /* Enable the QSPI transmit DMA Channel */
  903. tmp = (uint32_t*)&pData;
  904. HAL_DMA_Start_IT(hqspi->hdma, *(uint32_t*)tmp, (uint32_t)&hqspi->Instance->DR, hqspi->TxXferSize);
  905. /* Enable the DMA transfer by setting the DMAEN bit in the QSPI CR register */
  906. SET_BIT(hqspi->Instance->CR, QUADSPI_CR_DMAEN);
  907. }
  908. else
  909. {
  910. status = HAL_ERROR;
  911. }
  912. }
  913. else
  914. {
  915. status = HAL_BUSY;
  916. }
  917. /* Process unlocked */
  918. __HAL_UNLOCK(hqspi);
  919. return status;
  920. }
  921. /**
  922. * @brief Receive an amount of data in non-blocking mode with DMA.
  923. * @param hqspi: QSPI handle
  924. * @param pData: pointer to data buffer.
  925. * @note This function is used only in Indirect Read Mode
  926. * @retval HAL status
  927. */
  928. HAL_StatusTypeDef HAL_QSPI_Receive_DMA(QSPI_HandleTypeDef *hqspi, uint8_t *pData)
  929. {
  930. HAL_StatusTypeDef status = HAL_OK;
  931. uint32_t *tmp;
  932. uint32_t addr_reg = READ_REG(hqspi->Instance->AR);
  933. /* Process locked */
  934. __HAL_LOCK(hqspi);
  935. if(hqspi->State == HAL_QSPI_STATE_READY)
  936. {
  937. if(pData != NULL )
  938. {
  939. hqspi->ErrorCode = HAL_QSPI_ERROR_NONE;
  940. /* Update state */
  941. hqspi->State = HAL_QSPI_STATE_BUSY_INDIRECT_RX;
  942. /* Configure counters and size of the handle */
  943. hqspi->RxXferCount = READ_REG(hqspi->Instance->DLR) + 1;
  944. hqspi->RxXferSize = READ_REG(hqspi->Instance->DLR) + 1;
  945. hqspi->pRxBuffPtr = pData;
  946. /* Set the QSPI DMA transfer complete callback */
  947. hqspi->hdma->XferCpltCallback = QSPI_DMARxCplt;
  948. /* Set the QSPI DMA Half transfer complete callback */
  949. hqspi->hdma->XferHalfCpltCallback = QSPI_DMARxHalfCplt;
  950. /* Set the DMA error callback */
  951. hqspi->hdma->XferErrorCallback = QSPI_DMAError;
  952. /* Configure the direction of the DMA */
  953. hqspi->hdma->Init.Direction = DMA_PERIPH_TO_MEMORY;
  954. MODIFY_REG(hqspi->hdma->Instance->CCR, DMA_CCR_DIR, hqspi->hdma->Init.Direction);
  955. /* Enable the DMA Channel */
  956. tmp = (uint32_t*)&pData;
  957. HAL_DMA_Start_IT(hqspi->hdma, (uint32_t)&hqspi->Instance->DR, *(uint32_t*)tmp, hqspi->RxXferSize);
  958. /* Configure QSPI: CCR register with functional as indirect read */
  959. MODIFY_REG(hqspi->Instance->CCR, QUADSPI_CCR_FMODE, QSPI_FUNCTIONAL_MODE_INDIRECT_READ);
  960. /* Start the transfer by re-writing the address in AR register */
  961. WRITE_REG(hqspi->Instance->AR, addr_reg);
  962. /* Enable the DMA transfer by setting the DMAEN bit in the QSPI CR register */
  963. SET_BIT(hqspi->Instance->CR, QUADSPI_CR_DMAEN);
  964. }
  965. else
  966. {
  967. status = HAL_ERROR;
  968. }
  969. }
  970. else
  971. {
  972. status = HAL_BUSY;
  973. }
  974. /* Process unlocked */
  975. __HAL_UNLOCK(hqspi);
  976. return status;
  977. }
  978. /**
  979. * @brief Configure the QSPI Automatic Polling Mode in blocking mode.
  980. * @param hqspi: QSPI handle
  981. * @param cmd: structure that contains the command configuration information.
  982. * @param cfg: structure that contains the polling configuration information.
  983. * @param Timeout : Timeout duration
  984. * @note This function is used only in Automatic Polling Mode
  985. * @retval HAL status
  986. */
  987. HAL_StatusTypeDef HAL_QSPI_AutoPolling(QSPI_HandleTypeDef *hqspi, QSPI_CommandTypeDef *cmd, QSPI_AutoPollingTypeDef *cfg, uint32_t Timeout)
  988. {
  989. HAL_StatusTypeDef status = HAL_ERROR;
  990. /* Check the parameters */
  991. assert_param(IS_QSPI_INSTRUCTION_MODE(cmd->InstructionMode));
  992. if (cmd->InstructionMode != QSPI_INSTRUCTION_NONE)
  993. {
  994. assert_param(IS_QSPI_INSTRUCTION(cmd->Instruction));
  995. }
  996. assert_param(IS_QSPI_ADDRESS_MODE(cmd->AddressMode));
  997. if (cmd->AddressMode != QSPI_ADDRESS_NONE)
  998. {
  999. assert_param(IS_QSPI_ADDRESS_SIZE(cmd->AddressSize));
  1000. }
  1001. assert_param(IS_QSPI_ALTERNATE_BYTES_MODE(cmd->AlternateByteMode));
  1002. if (cmd->AlternateByteMode != QSPI_ALTERNATE_BYTES_NONE)
  1003. {
  1004. assert_param(IS_QSPI_ALTERNATE_BYTES_SIZE(cmd->AlternateBytesSize));
  1005. }
  1006. assert_param(IS_QSPI_DUMMY_CYCLES(cmd->DummyCycles));
  1007. assert_param(IS_QSPI_DATA_MODE(cmd->DataMode));
  1008. assert_param(IS_QSPI_DDR_MODE(cmd->DdrMode));
  1009. assert_param(IS_QSPI_DDR_HHC(cmd->DdrHoldHalfCycle));
  1010. assert_param(IS_QSPI_SIOO_MODE(cmd->SIOOMode));
  1011. assert_param(IS_QSPI_INTERVAL(cfg->Interval));
  1012. assert_param(IS_QSPI_STATUS_BYTES_SIZE(cfg->StatusBytesSize));
  1013. assert_param(IS_QSPI_MATCH_MODE(cfg->MatchMode));
  1014. /* Process locked */
  1015. __HAL_LOCK(hqspi);
  1016. if(hqspi->State == HAL_QSPI_STATE_READY)
  1017. {
  1018. hqspi->ErrorCode = HAL_QSPI_ERROR_NONE;
  1019. /* Update state */
  1020. hqspi->State = HAL_QSPI_STATE_BUSY_AUTO_POLLING;
  1021. /* Wait till BUSY flag reset */
  1022. status = QSPI_WaitFlagStateUntilTimeout(hqspi, QSPI_FLAG_BUSY, RESET, Timeout);
  1023. if (status == HAL_OK)
  1024. {
  1025. /* Configure QSPI: PSMAR register with the status match value */
  1026. WRITE_REG(hqspi->Instance->PSMAR, cfg->Match);
  1027. /* Configure QSPI: PSMKR register with the status mask value */
  1028. WRITE_REG(hqspi->Instance->PSMKR, cfg->Mask);
  1029. /* Configure QSPI: PIR register with the interval value */
  1030. WRITE_REG(hqspi->Instance->PIR, cfg->Interval);
  1031. /* Configure QSPI: CR register with Match mode and Automatic stop enabled
  1032. (otherwise there will be an infinite loop in blocking mode) */
  1033. MODIFY_REG(hqspi->Instance->CR, (QUADSPI_CR_PMM | QUADSPI_CR_APMS),
  1034. (cfg->MatchMode | QSPI_AUTOMATIC_STOP_ENABLE));
  1035. /* Call the configuration function */
  1036. cmd->NbData = cfg->StatusBytesSize;
  1037. QSPI_Config(hqspi, cmd, QSPI_FUNCTIONAL_MODE_AUTO_POLLING);
  1038. /* Wait until SM flag is set to go back in idle state */
  1039. if(QSPI_WaitFlagStateUntilTimeout(hqspi, QSPI_FLAG_SM, SET, Timeout) != HAL_OK)
  1040. {
  1041. status = HAL_TIMEOUT;
  1042. }
  1043. else
  1044. {
  1045. __HAL_QSPI_CLEAR_FLAG(hqspi, QSPI_FLAG_SM);
  1046. /* Update state */
  1047. hqspi->State = HAL_QSPI_STATE_READY;
  1048. }
  1049. }
  1050. }
  1051. else
  1052. {
  1053. status = HAL_BUSY;
  1054. }
  1055. /* Process unlocked */
  1056. __HAL_UNLOCK(hqspi);
  1057. /* Return function status */
  1058. return status;
  1059. }
  1060. /**
  1061. * @brief Configure the QSPI Automatic Polling Mode in non-blocking mode.
  1062. * @param hqspi: QSPI handle
  1063. * @param cmd: structure that contains the command configuration information.
  1064. * @param cfg: structure that contains the polling configuration information.
  1065. * @note This function is used only in Automatic Polling Mode
  1066. * @retval HAL status
  1067. */
  1068. HAL_StatusTypeDef HAL_QSPI_AutoPolling_IT(QSPI_HandleTypeDef *hqspi, QSPI_CommandTypeDef *cmd, QSPI_AutoPollingTypeDef *cfg)
  1069. {
  1070. HAL_StatusTypeDef status = HAL_ERROR;
  1071. /* Check the parameters */
  1072. assert_param(IS_QSPI_INSTRUCTION_MODE(cmd->InstructionMode));
  1073. if (cmd->InstructionMode != QSPI_INSTRUCTION_NONE)
  1074. {
  1075. assert_param(IS_QSPI_INSTRUCTION(cmd->Instruction));
  1076. }
  1077. assert_param(IS_QSPI_ADDRESS_MODE(cmd->AddressMode));
  1078. if (cmd->AddressMode != QSPI_ADDRESS_NONE)
  1079. {
  1080. assert_param(IS_QSPI_ADDRESS_SIZE(cmd->AddressSize));
  1081. }
  1082. assert_param(IS_QSPI_ALTERNATE_BYTES_MODE(cmd->AlternateByteMode));
  1083. if (cmd->AlternateByteMode != QSPI_ALTERNATE_BYTES_NONE)
  1084. {
  1085. assert_param(IS_QSPI_ALTERNATE_BYTES_SIZE(cmd->AlternateBytesSize));
  1086. }
  1087. assert_param(IS_QSPI_DUMMY_CYCLES(cmd->DummyCycles));
  1088. assert_param(IS_QSPI_DATA_MODE(cmd->DataMode));
  1089. assert_param(IS_QSPI_DDR_MODE(cmd->DdrMode));
  1090. assert_param(IS_QSPI_DDR_HHC(cmd->DdrHoldHalfCycle));
  1091. assert_param(IS_QSPI_SIOO_MODE(cmd->SIOOMode));
  1092. assert_param(IS_QSPI_INTERVAL(cfg->Interval));
  1093. assert_param(IS_QSPI_STATUS_BYTES_SIZE(cfg->StatusBytesSize));
  1094. assert_param(IS_QSPI_MATCH_MODE(cfg->MatchMode));
  1095. assert_param(IS_QSPI_AUTOMATIC_STOP(cfg->AutomaticStop));
  1096. /* Process locked */
  1097. __HAL_LOCK(hqspi);
  1098. if(hqspi->State == HAL_QSPI_STATE_READY)
  1099. {
  1100. hqspi->ErrorCode = HAL_QSPI_ERROR_NONE;
  1101. /* Update state */
  1102. hqspi->State = HAL_QSPI_STATE_BUSY_AUTO_POLLING;
  1103. /* Wait till BUSY flag reset */
  1104. status = QSPI_WaitFlagStateUntilTimeout(hqspi, QSPI_FLAG_BUSY, RESET, hqspi->Timeout);
  1105. if (status == HAL_OK)
  1106. {
  1107. /* Configure QSPI: PSMAR register with the status match value */
  1108. WRITE_REG(hqspi->Instance->PSMAR, cfg->Match);
  1109. /* Configure QSPI: PSMKR register with the status mask value */
  1110. WRITE_REG(hqspi->Instance->PSMKR, cfg->Mask);
  1111. /* Configure QSPI: PIR register with the interval value */
  1112. WRITE_REG(hqspi->Instance->PIR, cfg->Interval);
  1113. /* Configure QSPI: CR register with Match mode and Automatic stop mode */
  1114. MODIFY_REG(hqspi->Instance->CR, (QUADSPI_CR_PMM | QUADSPI_CR_APMS),
  1115. (cfg->MatchMode | cfg->AutomaticStop));
  1116. /* Clear interrupt */
  1117. __HAL_QSPI_CLEAR_FLAG(hqspi, QSPI_FLAG_TE | QSPI_FLAG_SM);
  1118. /* Enable the QSPI Transfer Error and status match Interrupt */
  1119. __HAL_QSPI_ENABLE_IT(hqspi, (QSPI_IT_SM | QSPI_IT_TE));
  1120. /* Call the configuration function */
  1121. cmd->NbData = cfg->StatusBytesSize;
  1122. QSPI_Config(hqspi, cmd, QSPI_FUNCTIONAL_MODE_AUTO_POLLING);
  1123. }
  1124. }
  1125. else
  1126. {
  1127. status = HAL_BUSY;
  1128. }
  1129. /* Process unlocked */
  1130. __HAL_UNLOCK(hqspi);
  1131. /* Return function status */
  1132. return status;
  1133. }
  1134. /**
  1135. * @brief Configure the Memory Mapped mode.
  1136. * @param hqspi: QSPI handle
  1137. * @param cmd: structure that contains the command configuration information.
  1138. * @param cfg: structure that contains the memory mapped configuration information.
  1139. * @note This function is used only in Memory mapped Mode
  1140. * @retval HAL status
  1141. */
  1142. HAL_StatusTypeDef HAL_QSPI_MemoryMapped(QSPI_HandleTypeDef *hqspi, QSPI_CommandTypeDef *cmd, QSPI_MemoryMappedTypeDef *cfg)
  1143. {
  1144. HAL_StatusTypeDef status = HAL_ERROR;
  1145. /* Check the parameters */
  1146. assert_param(IS_QSPI_INSTRUCTION_MODE(cmd->InstructionMode));
  1147. if (cmd->InstructionMode != QSPI_INSTRUCTION_NONE)
  1148. {
  1149. assert_param(IS_QSPI_INSTRUCTION(cmd->Instruction));
  1150. }
  1151. assert_param(IS_QSPI_ADDRESS_MODE(cmd->AddressMode));
  1152. if (cmd->AddressMode != QSPI_ADDRESS_NONE)
  1153. {
  1154. assert_param(IS_QSPI_ADDRESS_SIZE(cmd->AddressSize));
  1155. }
  1156. assert_param(IS_QSPI_ALTERNATE_BYTES_MODE(cmd->AlternateByteMode));
  1157. if (cmd->AlternateByteMode != QSPI_ALTERNATE_BYTES_NONE)
  1158. {
  1159. assert_param(IS_QSPI_ALTERNATE_BYTES_SIZE(cmd->AlternateBytesSize));
  1160. }
  1161. assert_param(IS_QSPI_DUMMY_CYCLES(cmd->DummyCycles));
  1162. assert_param(IS_QSPI_DATA_MODE(cmd->DataMode));
  1163. assert_param(IS_QSPI_DDR_MODE(cmd->DdrMode));
  1164. assert_param(IS_QSPI_DDR_HHC(cmd->DdrHoldHalfCycle));
  1165. assert_param(IS_QSPI_SIOO_MODE(cmd->SIOOMode));
  1166. assert_param(IS_QSPI_TIMEOUT_ACTIVATION(cfg->TimeOutActivation));
  1167. /* Process locked */
  1168. __HAL_LOCK(hqspi);
  1169. if(hqspi->State == HAL_QSPI_STATE_READY)
  1170. {
  1171. hqspi->ErrorCode = HAL_QSPI_ERROR_NONE;
  1172. /* Update state */
  1173. hqspi->State = HAL_QSPI_STATE_BUSY_MEM_MAPPED;
  1174. /* Wait till BUSY flag reset */
  1175. status = QSPI_WaitFlagStateUntilTimeout(hqspi, QSPI_FLAG_BUSY, RESET, hqspi->Timeout);
  1176. if (status == HAL_OK)
  1177. {
  1178. /* Configure QSPI: CR register with timeout counter enable */
  1179. MODIFY_REG(hqspi->Instance->CR, QUADSPI_CR_TCEN, cfg->TimeOutActivation);
  1180. if (cfg->TimeOutActivation == QSPI_TIMEOUT_COUNTER_ENABLE)
  1181. {
  1182. assert_param(IS_QSPI_TIMEOUT_PERIOD(cfg->TimeOutPeriod));
  1183. /* Configure QSPI: LPTR register with the low-power timeout value */
  1184. WRITE_REG(hqspi->Instance->LPTR, cfg->TimeOutPeriod);
  1185. /* Clear interrupt */
  1186. __HAL_QSPI_CLEAR_FLAG(hqspi, QSPI_FLAG_TO);
  1187. /* Enable the QSPI TimeOut Interrupt */
  1188. __HAL_QSPI_ENABLE_IT(hqspi, QSPI_IT_TO);
  1189. }
  1190. /* Call the configuration function */
  1191. QSPI_Config(hqspi, cmd, QSPI_FUNCTIONAL_MODE_MEMORY_MAPPED);
  1192. }
  1193. }
  1194. else
  1195. {
  1196. status = HAL_BUSY;
  1197. }
  1198. /* Process unlocked */
  1199. __HAL_UNLOCK(hqspi);
  1200. /* Return function status */
  1201. return status;
  1202. }
  1203. /**
  1204. * @brief Transfer Error callback.
  1205. * @param hqspi: QSPI handle
  1206. * @retval None
  1207. */
  1208. __weak void HAL_QSPI_ErrorCallback(QSPI_HandleTypeDef *hqspi)
  1209. {
  1210. /* Prevent unused argument(s) compilation warning */
  1211. UNUSED(hqspi);
  1212. /* NOTE : This function should not be modified, when the callback is needed,
  1213. the HAL_QSPI_ErrorCallback could be implemented in the user file
  1214. */
  1215. }
  1216. /**
  1217. * @brief Command completed callback.
  1218. * @param hqspi: QSPI handle
  1219. * @retval None
  1220. */
  1221. __weak void HAL_QSPI_CmdCpltCallback(QSPI_HandleTypeDef *hqspi)
  1222. {
  1223. /* Prevent unused argument(s) compilation warning */
  1224. UNUSED(hqspi);
  1225. /* NOTE: This function should not be modified, when the callback is needed,
  1226. the HAL_QSPI_CmdCpltCallback could be implemented in the user file
  1227. */
  1228. }
  1229. /**
  1230. * @brief Rx Transfer completed callback.
  1231. * @param hqspi: QSPI handle
  1232. * @retval None
  1233. */
  1234. __weak void HAL_QSPI_RxCpltCallback(QSPI_HandleTypeDef *hqspi)
  1235. {
  1236. /* Prevent unused argument(s) compilation warning */
  1237. UNUSED(hqspi);
  1238. /* NOTE: This function should not be modified, when the callback is needed,
  1239. the HAL_QSPI_RxCpltCallback could be implemented in the user file
  1240. */
  1241. }
  1242. /**
  1243. * @brief Tx Transfer completed callback.
  1244. * @param hqspi: QSPI handle
  1245. * @retval None
  1246. */
  1247. __weak void HAL_QSPI_TxCpltCallback(QSPI_HandleTypeDef *hqspi)
  1248. {
  1249. /* Prevent unused argument(s) compilation warning */
  1250. UNUSED(hqspi);
  1251. /* NOTE: This function should not be modified, when the callback is needed,
  1252. the HAL_QSPI_TxCpltCallback could be implemented in the user file
  1253. */
  1254. }
  1255. /**
  1256. * @brief Rx Half Transfer completed callback.
  1257. * @param hqspi: QSPI handle
  1258. * @retval None
  1259. */
  1260. __weak void HAL_QSPI_RxHalfCpltCallback(QSPI_HandleTypeDef *hqspi)
  1261. {
  1262. /* Prevent unused argument(s) compilation warning */
  1263. UNUSED(hqspi);
  1264. /* NOTE: This function should not be modified, when the callback is needed,
  1265. the HAL_QSPI_RxHalfCpltCallback could be implemented in the user file
  1266. */
  1267. }
  1268. /**
  1269. * @brief Tx Half Transfer completed callback.
  1270. * @param hqspi: QSPI handle
  1271. * @retval None
  1272. */
  1273. __weak void HAL_QSPI_TxHalfCpltCallback(QSPI_HandleTypeDef *hqspi)
  1274. {
  1275. /* Prevent unused argument(s) compilation warning */
  1276. UNUSED(hqspi);
  1277. /* NOTE: This function should not be modified, when the callback is needed,
  1278. the HAL_QSPI_TxHalfCpltCallback could be implemented in the user file
  1279. */
  1280. }
  1281. /**
  1282. * @brief FIFO Threshold callback.
  1283. * @param hqspi: QSPI handle
  1284. * @retval None
  1285. */
  1286. __weak void HAL_QSPI_FifoThresholdCallback(QSPI_HandleTypeDef *hqspi)
  1287. {
  1288. /* Prevent unused argument(s) compilation warning */
  1289. UNUSED(hqspi);
  1290. /* NOTE : This function should not be modified, when the callback is needed,
  1291. the HAL_QSPI_FIFOThresholdCallback could be implemented in the user file
  1292. */
  1293. }
  1294. /**
  1295. * @brief Status Match callback.
  1296. * @param hqspi: QSPI handle
  1297. * @retval None
  1298. */
  1299. __weak void HAL_QSPI_StatusMatchCallback(QSPI_HandleTypeDef *hqspi)
  1300. {
  1301. /* Prevent unused argument(s) compilation warning */
  1302. UNUSED(hqspi);
  1303. /* NOTE : This function should not be modified, when the callback is needed,
  1304. the HAL_QSPI_StatusMatchCallback could be implemented in the user file
  1305. */
  1306. }
  1307. /**
  1308. * @brief Timeout callback.
  1309. * @param hqspi: QSPI handle
  1310. * @retval None
  1311. */
  1312. __weak void HAL_QSPI_TimeOutCallback(QSPI_HandleTypeDef *hqspi)
  1313. {
  1314. /* Prevent unused argument(s) compilation warning */
  1315. UNUSED(hqspi);
  1316. /* NOTE : This function should not be modified, when the callback is needed,
  1317. the HAL_QSPI_TimeOutCallback could be implemented in the user file
  1318. */
  1319. }
  1320. /**
  1321. * @}
  1322. */
  1323. /** @defgroup QSPI_Exported_Functions_Group3 Peripheral Control and State functions
  1324. * @brief QSPI control and State functions
  1325. *
  1326. @verbatim
  1327. ===============================================================================
  1328. ##### Peripheral Control and State functions #####
  1329. ===============================================================================
  1330. [..]
  1331. This subsection provides a set of functions allowing to :
  1332. (+) Check in run-time the state of the driver.
  1333. (+) Check the error code set during last operation.
  1334. (+) Abort any operation.
  1335. @endverbatim
  1336. * @{
  1337. */
  1338. /**
  1339. * @brief Return the QSPI handle state.
  1340. * @param hqspi: QSPI handle
  1341. * @retval HAL state
  1342. */
  1343. HAL_QSPI_StateTypeDef HAL_QSPI_GetState(QSPI_HandleTypeDef *hqspi)
  1344. {
  1345. /* Return QSPI handle state */
  1346. return hqspi->State;
  1347. }
  1348. /**
  1349. * @brief Return the QSPI error code.
  1350. * @param hqspi: QSPI handle
  1351. * @retval QSPI Error Code
  1352. */
  1353. uint32_t HAL_QSPI_GetError(QSPI_HandleTypeDef *hqspi)
  1354. {
  1355. return hqspi->ErrorCode;
  1356. }
  1357. /**
  1358. * @brief Abort the current transmission.
  1359. * @param hqspi: QSPI handle
  1360. * @retval HAL status
  1361. */
  1362. HAL_StatusTypeDef HAL_QSPI_Abort(QSPI_HandleTypeDef *hqspi)
  1363. {
  1364. HAL_StatusTypeDef status = HAL_ERROR;
  1365. /* Configure QSPI: CR register with Abort request */
  1366. SET_BIT(hqspi->Instance->CR, QUADSPI_CR_ABORT);
  1367. /* Wait until TC flag is set to go back in idle state */
  1368. if(QSPI_WaitFlagStateUntilTimeout(hqspi, QSPI_FLAG_TC, SET, hqspi->Timeout) != HAL_OK)
  1369. {
  1370. status = HAL_TIMEOUT;
  1371. }
  1372. else
  1373. {
  1374. __HAL_QSPI_CLEAR_FLAG(hqspi, QSPI_FLAG_TC);
  1375. /* Wait until BUSY flag is reset */
  1376. status = QSPI_WaitFlagStateUntilTimeout(hqspi, QSPI_FLAG_BUSY, RESET, hqspi->Timeout);
  1377. /* Update state */
  1378. hqspi->State = HAL_QSPI_STATE_READY;
  1379. }
  1380. return status;
  1381. }
  1382. /** @brief Set QSPI timeout.
  1383. * @param hqspi: QSPI handle.
  1384. * @param Timeout: Timeout for the QSPI memory access.
  1385. * @retval None
  1386. */
  1387. void HAL_QSPI_SetTimeout(QSPI_HandleTypeDef *hqspi, uint32_t Timeout)
  1388. {
  1389. hqspi->Timeout = Timeout;
  1390. }
  1391. /**
  1392. * @}
  1393. */
  1394. /**
  1395. * @brief DMA QSPI receive process complete callback.
  1396. * @param hdma: DMA handle
  1397. * @retval None
  1398. */
  1399. static void QSPI_DMARxCplt(DMA_HandleTypeDef *hdma)
  1400. {
  1401. QSPI_HandleTypeDef* hqspi = ( QSPI_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  1402. hqspi->RxXferCount = 0;
  1403. /* Wait for QSPI TC Flag */
  1404. if(QSPI_WaitFlagStateUntilTimeout(hqspi, QSPI_FLAG_TC, SET, hqspi->Timeout) != HAL_OK)
  1405. {
  1406. /* Timeout occurred */
  1407. HAL_QSPI_ErrorCallback(hqspi);
  1408. }
  1409. else
  1410. {
  1411. /* Disable the DMA transfer by clearing the DMAEN bit in the QSPI CR register */
  1412. CLEAR_BIT(hqspi->Instance->CR, QUADSPI_CR_DMAEN);
  1413. /* Disable the DMA channel */
  1414. HAL_DMA_Abort(hdma);
  1415. /* Clear Transfer Complete bit */
  1416. __HAL_QSPI_CLEAR_FLAG(hqspi, QSPI_FLAG_TC);
  1417. /* Workaround - Extra data written in the FIFO at the end of a read transfer */
  1418. HAL_QSPI_Abort(hqspi);
  1419. /* Update state */
  1420. hqspi->State = HAL_QSPI_STATE_READY;
  1421. HAL_QSPI_RxCpltCallback(hqspi);
  1422. }
  1423. }
  1424. /**
  1425. * @brief DMA QSPI transmit process complete callback.
  1426. * @param hdma: DMA handle
  1427. * @retval None
  1428. */
  1429. static void QSPI_DMATxCplt(DMA_HandleTypeDef *hdma)
  1430. {
  1431. QSPI_HandleTypeDef* hqspi = ( QSPI_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  1432. hqspi->TxXferCount = 0;
  1433. /* Wait for QSPI TC Flag */
  1434. if(QSPI_WaitFlagStateUntilTimeout(hqspi, QSPI_FLAG_TC, SET, hqspi->Timeout) != HAL_OK)
  1435. {
  1436. /* Timeout occurred */
  1437. HAL_QSPI_ErrorCallback(hqspi);
  1438. }
  1439. else
  1440. {
  1441. /* Disable the DMA transfer by clearing the DMAEN bit in the QSPI CR register */
  1442. CLEAR_BIT(hqspi->Instance->CR, QUADSPI_CR_DMAEN);
  1443. /* Disable the DMA channel */
  1444. HAL_DMA_Abort(hdma);
  1445. /* Clear Transfer Complete bit */
  1446. __HAL_QSPI_CLEAR_FLAG(hqspi, QSPI_FLAG_TC);
  1447. /* Clear Busy bit */
  1448. HAL_QSPI_Abort(hqspi);
  1449. /* Update state */
  1450. hqspi->State = HAL_QSPI_STATE_READY;
  1451. HAL_QSPI_TxCpltCallback(hqspi);
  1452. }
  1453. }
  1454. /**
  1455. * @brief DMA QSPI receive process half complete callback.
  1456. * @param hdma : DMA handle
  1457. * @retval None
  1458. */
  1459. static void QSPI_DMARxHalfCplt(DMA_HandleTypeDef *hdma)
  1460. {
  1461. QSPI_HandleTypeDef* hqspi = (QSPI_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
  1462. HAL_QSPI_RxHalfCpltCallback(hqspi);
  1463. }
  1464. /**
  1465. * @brief DMA QSPI transmit process half complete callback.
  1466. * @param hdma : DMA handle
  1467. * @retval None
  1468. */
  1469. static void QSPI_DMATxHalfCplt(DMA_HandleTypeDef *hdma)
  1470. {
  1471. QSPI_HandleTypeDef* hqspi = (QSPI_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
  1472. HAL_QSPI_TxHalfCpltCallback(hqspi);
  1473. }
  1474. /**
  1475. * @brief DMA QSPI communication error callback.
  1476. * @param hdma: DMA handle
  1477. * @retval None
  1478. */
  1479. static void QSPI_DMAError(DMA_HandleTypeDef *hdma)
  1480. {
  1481. QSPI_HandleTypeDef* hqspi = ( QSPI_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  1482. hqspi->RxXferCount = 0;
  1483. hqspi->TxXferCount = 0;
  1484. hqspi->State = HAL_QSPI_STATE_ERROR;
  1485. hqspi->ErrorCode |= HAL_QSPI_ERROR_DMA;
  1486. HAL_QSPI_ErrorCallback(hqspi);
  1487. }
  1488. /**
  1489. * @brief Wait for a flag state until timeout.
  1490. * @param hqspi: QSPI handle
  1491. * @param Flag: Flag checked
  1492. * @param State: Value of the flag expected
  1493. * @param Timeout: Duration of the timeout
  1494. * @retval HAL status
  1495. */
  1496. static HAL_StatusTypeDef QSPI_WaitFlagStateUntilTimeout(QSPI_HandleTypeDef *hqspi, uint32_t Flag,
  1497. FlagStatus State, uint32_t Timeout)
  1498. {
  1499. uint32_t tickstart = HAL_GetTick();
  1500. /* Wait until flag is in expected state */
  1501. while((FlagStatus)(__HAL_QSPI_GET_FLAG(hqspi, Flag)) != State)
  1502. {
  1503. /* Check for the Timeout */
  1504. if (Timeout != HAL_MAX_DELAY)
  1505. {
  1506. if((Timeout == 0) || ((HAL_GetTick() - tickstart) > Timeout))
  1507. {
  1508. hqspi->State = HAL_QSPI_STATE_ERROR;
  1509. hqspi->ErrorCode |= HAL_QSPI_ERROR_TIMEOUT;
  1510. return HAL_TIMEOUT;
  1511. }
  1512. }
  1513. }
  1514. return HAL_OK;
  1515. }
  1516. /**
  1517. * @brief Configure the communication registers.
  1518. * @param hqspi: QSPI handle
  1519. * @param cmd: structure that contains the command configuration information
  1520. * @param FunctionalMode: functional mode to configured
  1521. * This parameter can be one of the following values:
  1522. * @arg QSPI_FUNCTIONAL_MODE_INDIRECT_WRITE: Indirect write mode
  1523. * @arg QSPI_FUNCTIONAL_MODE_INDIRECT_READ: Indirect read mode
  1524. * @arg QSPI_FUNCTIONAL_MODE_AUTO_POLLING: Automatic polling mode
  1525. * @arg QSPI_FUNCTIONAL_MODE_MEMORY_MAPPED: Memory-mapped mode
  1526. * @retval None
  1527. */
  1528. static void QSPI_Config(QSPI_HandleTypeDef *hqspi, QSPI_CommandTypeDef *cmd, uint32_t FunctionalMode)
  1529. {
  1530. assert_param(IS_QSPI_FUNCTIONAL_MODE(FunctionalMode));
  1531. if ((cmd->DataMode != QSPI_DATA_NONE) && (FunctionalMode != QSPI_FUNCTIONAL_MODE_MEMORY_MAPPED))
  1532. {
  1533. /* Configure QSPI: DLR register with the number of data to read or write */
  1534. WRITE_REG(hqspi->Instance->DLR, (cmd->NbData - 1));
  1535. }
  1536. if (cmd->InstructionMode != QSPI_INSTRUCTION_NONE)
  1537. {
  1538. if (cmd->AlternateByteMode != QSPI_ALTERNATE_BYTES_NONE)
  1539. {
  1540. /* Configure QSPI: ABR register with alternate bytes value */
  1541. WRITE_REG(hqspi->Instance->ABR, cmd->AlternateBytes);
  1542. if (cmd->AddressMode != QSPI_ADDRESS_NONE)
  1543. {
  1544. /*---- Command with instruction, address and alternate bytes ----*/
  1545. /* Configure QSPI: CCR register with all communications parameters */
  1546. WRITE_REG(hqspi->Instance->CCR, (cmd->DdrMode | cmd->DdrHoldHalfCycle | cmd->SIOOMode |
  1547. cmd->DataMode | (cmd->DummyCycles << POSITION_VAL(QUADSPI_CCR_DCYC)) |
  1548. cmd->AlternateBytesSize | cmd->AlternateByteMode |
  1549. cmd->AddressSize | cmd->AddressMode | cmd->InstructionMode |
  1550. cmd->Instruction | FunctionalMode));
  1551. if (FunctionalMode != QSPI_FUNCTIONAL_MODE_MEMORY_MAPPED)
  1552. {
  1553. /* Configure QSPI: AR register with address value */
  1554. WRITE_REG(hqspi->Instance->AR, cmd->Address);
  1555. }
  1556. }
  1557. else
  1558. {
  1559. /*---- Command with instruction and alternate bytes ----*/
  1560. /* Configure QSPI: CCR register with all communications parameters */
  1561. WRITE_REG(hqspi->Instance->CCR, (cmd->DdrMode | cmd->DdrHoldHalfCycle | cmd->SIOOMode |
  1562. cmd->DataMode | (cmd->DummyCycles << POSITION_VAL(QUADSPI_CCR_DCYC)) |
  1563. cmd->AlternateBytesSize | cmd->AlternateByteMode |
  1564. cmd->AddressMode | cmd->InstructionMode |
  1565. cmd->Instruction | FunctionalMode));
  1566. }
  1567. }
  1568. else
  1569. {
  1570. if (cmd->AddressMode != QSPI_ADDRESS_NONE)
  1571. {
  1572. /*---- Command with instruction and address ----*/
  1573. /* Configure QSPI: CCR register with all communications parameters */
  1574. WRITE_REG(hqspi->Instance->CCR, (cmd->DdrMode | cmd->DdrHoldHalfCycle | cmd->SIOOMode |
  1575. cmd->DataMode | (cmd->DummyCycles << POSITION_VAL(QUADSPI_CCR_DCYC)) |
  1576. cmd->AlternateByteMode | cmd->AddressSize | cmd->AddressMode |
  1577. cmd->InstructionMode | cmd->Instruction | FunctionalMode));
  1578. if (FunctionalMode != QSPI_FUNCTIONAL_MODE_MEMORY_MAPPED)
  1579. {
  1580. /* Configure QSPI: AR register with address value */
  1581. WRITE_REG(hqspi->Instance->AR, cmd->Address);
  1582. }
  1583. }
  1584. else
  1585. {
  1586. /*---- Command with only instruction ----*/
  1587. /* Configure QSPI: CCR register with all communications parameters */
  1588. WRITE_REG(hqspi->Instance->CCR, (cmd->DdrMode | cmd->DdrHoldHalfCycle | cmd->SIOOMode |
  1589. cmd->DataMode | (cmd->DummyCycles << POSITION_VAL(QUADSPI_CCR_DCYC)) |
  1590. cmd->AlternateByteMode | cmd->AddressMode |
  1591. cmd->InstructionMode | cmd->Instruction | FunctionalMode));
  1592. }
  1593. }
  1594. }
  1595. else
  1596. {
  1597. if (cmd->AlternateByteMode != QSPI_ALTERNATE_BYTES_NONE)
  1598. {
  1599. /* Configure QSPI: ABR register with alternate bytes value */
  1600. WRITE_REG(hqspi->Instance->ABR, cmd->AlternateBytes);
  1601. if (cmd->AddressMode != QSPI_ADDRESS_NONE)
  1602. {
  1603. /*---- Command with address and alternate bytes ----*/
  1604. /* Configure QSPI: CCR register with all communications parameters */
  1605. WRITE_REG(hqspi->Instance->CCR, (cmd->DdrMode | cmd->DdrHoldHalfCycle | cmd->SIOOMode |
  1606. cmd->DataMode | (cmd->DummyCycles << POSITION_VAL(QUADSPI_CCR_DCYC)) |
  1607. cmd->AlternateBytesSize | cmd->AlternateByteMode |
  1608. cmd->AddressSize | cmd->AddressMode |
  1609. cmd->InstructionMode | FunctionalMode));
  1610. if (FunctionalMode != QSPI_FUNCTIONAL_MODE_MEMORY_MAPPED)
  1611. {
  1612. /* Configure QSPI: AR register with address value */
  1613. WRITE_REG(hqspi->Instance->AR, cmd->Address);
  1614. }
  1615. }
  1616. else
  1617. {
  1618. /*---- Command with only alternate bytes ----*/
  1619. /* Configure QSPI: CCR register with all communications parameters */
  1620. WRITE_REG(hqspi->Instance->CCR, (cmd->DdrMode | cmd->DdrHoldHalfCycle | cmd->SIOOMode |
  1621. cmd->DataMode | (cmd->DummyCycles << POSITION_VAL(QUADSPI_CCR_DCYC)) |
  1622. cmd->AlternateBytesSize | cmd->AlternateByteMode |
  1623. cmd->AddressMode | cmd->InstructionMode | FunctionalMode));
  1624. }
  1625. }
  1626. else
  1627. {
  1628. if (cmd->AddressMode != QSPI_ADDRESS_NONE)
  1629. {
  1630. /*---- Command with only address ----*/
  1631. /* Configure QSPI: CCR register with all communications parameters */
  1632. WRITE_REG(hqspi->Instance->CCR, (cmd->DdrMode | cmd->DdrHoldHalfCycle | cmd->SIOOMode |
  1633. cmd->DataMode | (cmd->DummyCycles << POSITION_VAL(QUADSPI_CCR_DCYC)) |
  1634. cmd->AlternateByteMode | cmd->AddressSize |
  1635. cmd->AddressMode | cmd->InstructionMode | FunctionalMode));
  1636. if (FunctionalMode != QSPI_FUNCTIONAL_MODE_MEMORY_MAPPED)
  1637. {
  1638. /* Configure QSPI: AR register with address value */
  1639. WRITE_REG(hqspi->Instance->AR, cmd->Address);
  1640. }
  1641. }
  1642. else
  1643. {
  1644. /*---- Command with only data phase ----*/
  1645. if (cmd->DataMode != QSPI_DATA_NONE)
  1646. {
  1647. /* Configure QSPI: CCR register with all communications parameters */
  1648. WRITE_REG(hqspi->Instance->CCR, (cmd->DdrMode | cmd->DdrHoldHalfCycle | cmd->SIOOMode |
  1649. cmd->DataMode | (cmd->DummyCycles << POSITION_VAL(QUADSPI_CCR_DCYC)) |
  1650. cmd->AlternateByteMode | cmd->AddressMode |
  1651. cmd->InstructionMode | FunctionalMode));
  1652. }
  1653. }
  1654. }
  1655. }
  1656. }
  1657. /**
  1658. * @}
  1659. */
  1660. #endif /* HAL_QSPI_MODULE_ENABLED */
  1661. /**
  1662. * @}
  1663. */
  1664. /**
  1665. * @}
  1666. */
  1667. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/