stm32f7xx_hal_qspi.c 63 KB

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