stm32f4xx_qspi.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903
  1. /**
  2. ******************************************************************************
  3. * @file stm32f4xx_qspi.c
  4. * @author MCD Application Team
  5. * @version V1.8.1
  6. * @date 27-January-2022
  7. * @brief This file provides firmware functions to manage the following
  8. * functionalities of the Serial peripheral interface (QSPI):
  9. * + Initialization and Configuration
  10. * + Indirect Data Read/Write functions
  11. * + Memory Mapped Mode Data Read functions
  12. * + Automatic Polling functions
  13. * + DMA transfers management
  14. * + Interrupts and flags management
  15. *
  16. * @verbatim
  17. *
  18. ===============================================================================
  19. ##### How to use this driver #####
  20. ===============================================================================
  21. [..]
  22. (#) Enable peripheral clock using RCC_AHB3PeriphClockCmd(RCC_AHB3Periph_QSPI,ENABLE);
  23. function.
  24. (#) Enable CLK, BK1_IO0, BK1_IO1, BK1_IO2, BK1_IO3, BK1_NCS, BK2_IO0,
  25. BK2_IO1, BK2_IO2, BK2_IO3 and BK2_NCS GPIO clocks using
  26. RCC_AHB1PeriphClockCmd() function.
  27. (#) Peripherals alternate function:
  28. (++) Connect the pin to the desired peripherals' Alternate
  29. Function (AF) using GPIO_PinAFConfig() function.
  30. (++) Configure the desired pin in alternate function by:
  31. GPIO_InitStruct->GPIO_Mode = GPIO_Mode_AF.
  32. (++) Select the type, pull-up/pull-down and output speed via
  33. GPIO_PuPd, GPIO_OType and GPIO_Speed members.
  34. (++) Call GPIO_Init() function.
  35. (#) Program the Flash Size, CS High Time, Sample Shift, Prescaler, Clock Mode
  36. values using the QSPI_Init() function.
  37. (#) Enable QSPI using QSPI_Cmd() function.
  38. (#) Set QSPI Data Length using QSPI_SetDataLength() function.
  39. (#) Configure the FIFO threshold using QSPI_SetFIFOThreshold() to select
  40. at which threshold the FTF event is generated.
  41. (#) Enable the NVIC and the corresponding interrupt using the function
  42. QSPI_ITConfig() if you need to use interrupt mode.
  43. (#) When using the DMA mode
  44. (++) Configure the DMA using DMA_Init() function.
  45. (++) Active the needed channel Request using SPI_I2S_DMACmd() function.
  46. (#) Enable the SPI using the QSPI_DMACmd() function.
  47. (#) Enable the DMA using the DMA_Cmd() function when using DMA mode.
  48. @endverbatim
  49. *
  50. ******************************************************************************
  51. * @attention
  52. *
  53. * Copyright (c) 2016 STMicroelectronics.
  54. * All rights reserved.
  55. *
  56. * This software is licensed under terms that can be found in the LICENSE file
  57. * in the root directory of this software component.
  58. * If no LICENSE file comes with this software, it is provided AS-IS.
  59. *
  60. ******************************************************************************
  61. */
  62. /* Includes ------------------------------------------------------------------*/
  63. #include "stm32f4xx_qspi.h"
  64. /** @addtogroup STM32F4xx_StdPeriph_Driver
  65. * @{
  66. */
  67. /** @defgroup QSPI
  68. * @brief QSPI driver modules
  69. * @{
  70. */
  71. #if defined(STM32F412xG) || defined(STM32F413_423xx) || defined(STM32F446xx) || defined(STM32F469_479xx)
  72. /* Private typedef -----------------------------------------------------------*/
  73. /* Private define ------------------------------------------------------------*/
  74. #define QSPI_CR_CLEAR_MASK 0x00FFFFCF
  75. #define QSPI_DCR_CLEAR_MASK 0xFFE0F7FE
  76. #define QSPI_CCR_CLEAR_MASK 0x90800000
  77. #define QSPI_PIR_CLEAR_MASK 0xFFFF0000
  78. #define QSPI_LPTR_CLEAR_MASK 0xFFFF0000
  79. #define QSPI_CCR_CLEAR_INSTRUCTION_MASK 0xFFFFFF00
  80. #define QSPI_CCR_CLEAR_DCY_MASK 0xFFC3FFFF
  81. #define QSPI_CR_CLEAR_FIFOTHRESHOLD_MASK 0xFFFFF0FF
  82. #define QSPI_CR_INTERRUPT_MASK 0x001F0000
  83. #define QSPI_SR_INTERRUPT_MASK 0x0000001F
  84. #define QSPI_FSR_INTERRUPT_MASK 0x0000001B
  85. /* Private macro -------------------------------------------------------------*/
  86. /* Private variables ---------------------------------------------------------*/
  87. /* Private function prototypes -----------------------------------------------*/
  88. /* Private functions ---------------------------------------------------------*/
  89. /* Initialization and Configuration functions *********************************/
  90. /** @defgroup <PPP>_Private_Functions
  91. * @{
  92. */
  93. /** @defgroup <PPP>_Group1 Function Group1 Name
  94. * @brief Function group1 name description (copied from the header file)
  95. *
  96. @verbatim
  97. ===============================================================================
  98. ##### < Function group1 name (copied from the header file)
  99. Note: do not use "Peripheral" or "PPP" word in the function group name > #####
  100. ===============================================================================
  101. [..] < OPTIONAL:
  102. Add here the most important information to know about the IP features
  103. covered by this group of function.
  104. For system IPs, this section contains how to use this group API.
  105. >
  106. @endverbatim
  107. * @{
  108. */
  109. /**
  110. * @brief Deinitializes the QSPI peripheral registers to their default
  111. * reset values.
  112. * @param None
  113. * @retval None
  114. */
  115. void QSPI_DeInit(void)
  116. {
  117. /* Enable QSPI reset state */
  118. RCC_AHB3PeriphResetCmd(RCC_AHB3Periph_QSPI, ENABLE);
  119. /* Release QSPI from reset state */
  120. RCC_AHB3PeriphResetCmd(RCC_AHB3Periph_QSPI, DISABLE);
  121. }
  122. /**
  123. * @brief Fills each QSPI_InitStruct member with its default value.
  124. * @param QSPI_InitStruct: pointer to a QSPI_InitTypeDef structure which will be initialized.
  125. * @retval None
  126. */
  127. void QSPI_StructInit(QSPI_InitTypeDef* QSPI_InitStruct)
  128. {
  129. /*--------- Reset QSPI init structure parameters default values ------------*/
  130. /* Initialize the QSPI_SShift member */
  131. QSPI_InitStruct->QSPI_SShift = QSPI_SShift_NoShift ;
  132. /* Initialize the QSPI_Prescaler member */
  133. QSPI_InitStruct->QSPI_Prescaler = 0 ;
  134. /* Initialize the QSPI_CKMode member */
  135. QSPI_InitStruct->QSPI_CKMode = QSPI_CKMode_Mode0 ;
  136. /* Initialize the QSPI_CSHTime member */
  137. QSPI_InitStruct->QSPI_CSHTime = QSPI_CSHTime_1Cycle ;
  138. /* Initialize the QSPI_FSize member */
  139. QSPI_InitStruct->QSPI_FSize = 0 ;
  140. /* Initialize the QSPI_FSelect member */
  141. QSPI_InitStruct->QSPI_FSelect = QSPI_FSelect_1 ;
  142. /* Initialize the QSPI_DFlash member */
  143. QSPI_InitStruct->QSPI_DFlash = QSPI_DFlash_Disable ;
  144. }
  145. /**
  146. * @brief Fills each QSPI_ComConfig_InitStruct member with its default value.
  147. * @param QSPI_ComConfig_InitStruct: pointer to a QSPI_ComConfig_InitTypeDef structure which will be initialized.
  148. * @retval None
  149. */
  150. void QSPI_ComConfig_StructInit(QSPI_ComConfig_InitTypeDef* QSPI_ComConfig_InitStruct)
  151. {
  152. /*--------- Reset QSPI ComConfig init structure parameters default values ------------*/
  153. /* Set QSPI Communication configuration structure parameters default values */
  154. /* Initialize the QSPI_ComConfig_DDRMode member */
  155. QSPI_ComConfig_InitStruct->QSPI_ComConfig_DDRMode = QSPI_ComConfig_DDRMode_Disable ;
  156. /* Initialize the QSPI_ComConfig_DHHC member */
  157. QSPI_ComConfig_InitStruct->QSPI_ComConfig_DHHC = QSPI_ComConfig_DHHC_Disable ;
  158. /* Initialize the QSPI_ComConfig_SIOOMode member */
  159. QSPI_ComConfig_InitStruct->QSPI_ComConfig_SIOOMode = QSPI_ComConfig_SIOOMode_Disable ;
  160. /* Initialize the QSPI_ComConfig_FMode member */
  161. QSPI_ComConfig_InitStruct->QSPI_ComConfig_FMode = QSPI_ComConfig_FMode_Indirect_Write ;
  162. /* Initialize the QSPI_ComConfig_DMode member */
  163. QSPI_ComConfig_InitStruct->QSPI_ComConfig_DMode = QSPI_ComConfig_DMode_NoData ;
  164. /* Initialize the QSPI_ComConfig_DummyCycles member */
  165. QSPI_ComConfig_InitStruct->QSPI_ComConfig_DummyCycles = 0 ;
  166. /* Initialize the QSPI_ComConfig_ABSize member */
  167. QSPI_ComConfig_InitStruct->QSPI_ComConfig_ABSize = QSPI_ComConfig_ABSize_8bit ;
  168. /* Initialize the QSPI_ComConfig_ABMode member */
  169. QSPI_ComConfig_InitStruct->QSPI_ComConfig_ABMode = QSPI_ComConfig_ABMode_NoAlternateByte ;
  170. /* Initialize the QSPI_ComConfig_ADSize member */
  171. QSPI_ComConfig_InitStruct->QSPI_ComConfig_ADSize = QSPI_ComConfig_ADSize_8bit ;
  172. /* Initialize the QSPI_ComConfig_ADMode member */
  173. QSPI_ComConfig_InitStruct->QSPI_ComConfig_ADMode = QSPI_ComConfig_ADMode_NoAddress ;
  174. /* Initialize the QSPI_ComConfig_IMode member */
  175. QSPI_ComConfig_InitStruct->QSPI_ComConfig_IMode = QSPI_ComConfig_IMode_NoInstruction ;
  176. /* Initialize the QSPI_ComConfig_Ins member */
  177. QSPI_ComConfig_InitStruct->QSPI_ComConfig_Ins = 0 ;
  178. }
  179. /**
  180. * @brief Initializes the QSPI peripheral according to the specified
  181. * parameters in the QSPI_InitStruct.
  182. * @param QSPI_InitStruct: pointer to a QSPI_InitTypeDef structure that
  183. * contains the configuration information for the specified QSPI peripheral.
  184. * @retval None
  185. */
  186. void QSPI_Init(QSPI_InitTypeDef* QSPI_InitStruct)
  187. {
  188. uint32_t tmpreg = 0;
  189. /* Check the QSPI parameters */
  190. assert_param(IS_QSPI_SSHIFT(QSPI_InitStruct->QSPI_SShift));
  191. assert_param(IS_QSPI_PRESCALER(QSPI_InitStruct->QSPI_Prescaler));
  192. assert_param(IS_QSPI_CKMODE(QSPI_InitStruct->QSPI_CKMode));
  193. assert_param(IS_QSPI_CSHTIME(QSPI_InitStruct->QSPI_CSHTime));
  194. assert_param(IS_QSPI_FSIZE(QSPI_InitStruct->QSPI_FSize));
  195. assert_param(IS_QSPI_FSEL(QSPI_InitStruct->QSPI_FSelect));
  196. assert_param(IS_QSPI_DFM(QSPI_InitStruct->QSPI_DFlash));
  197. /*------------------------ QSPI CR Configuration ------------------------*/
  198. /* Get the QUADSPI CR1 value */
  199. tmpreg = QUADSPI->CR;
  200. /* Clear PRESCALER and SSHIFT bits */
  201. tmpreg &= QSPI_CR_CLEAR_MASK;
  202. /* Configure QUADSPI: Prescaler and Sample Shift */
  203. tmpreg |= (uint32_t)(((QSPI_InitStruct->QSPI_Prescaler)<<24)
  204. |(QSPI_InitStruct->QSPI_SShift)
  205. |(QSPI_InitStruct->QSPI_FSelect)
  206. |(QSPI_InitStruct->QSPI_DFlash));
  207. /* Write to QUADSPI CR */
  208. QUADSPI->CR = tmpreg;
  209. /*------------------------ QUADSPI DCR Configuration ------------------------*/
  210. /* Get the QUADSPI DCR value */
  211. tmpreg = QUADSPI->DCR;
  212. /* Clear FSIZE, CSHT and CKMODE bits */
  213. tmpreg &= QSPI_DCR_CLEAR_MASK;
  214. /* Configure QSPI: Flash Size, Chip Select High Time and Clock Mode */
  215. tmpreg |= (uint32_t)(((QSPI_InitStruct->QSPI_FSize)<<16)
  216. |(QSPI_InitStruct->QSPI_CSHTime)
  217. |(QSPI_InitStruct->QSPI_CKMode));
  218. /* Write to QSPI DCR */
  219. QUADSPI->DCR = tmpreg;
  220. }
  221. /**
  222. * @brief Initializes the QSPI CCR according to the specified
  223. * parameters in the QSPI_ComConfig_InitStruct.
  224. * @param QSPI_ComConfig_InitStruct: pointer to a QSPI_ComConfig_InitTypeDef structure that
  225. * contains the communication configuration informations about QSPI peripheral.
  226. * @retval None
  227. */
  228. void QSPI_ComConfig_Init(QSPI_ComConfig_InitTypeDef* QSPI_ComConfig_InitStruct)
  229. {
  230. uint32_t tmpreg = 0;
  231. /* Check the QSPI Communication Control parameters */
  232. assert_param(IS_QSPI_FMODE (QSPI_ComConfig_InitStruct->QSPI_ComConfig_FMode));
  233. assert_param(IS_QSPI_SIOOMODE (QSPI_ComConfig_InitStruct->QSPI_ComConfig_SIOOMode));
  234. assert_param(IS_QSPI_DMODE (QSPI_ComConfig_InitStruct->QSPI_ComConfig_DMode));
  235. assert_param(IS_QSPI_DCY (QSPI_ComConfig_InitStruct->QSPI_ComConfig_DummyCycles));
  236. assert_param(IS_QSPI_ABSIZE (QSPI_ComConfig_InitStruct->QSPI_ComConfig_ABSize));
  237. assert_param(IS_QSPI_ABMODE (QSPI_ComConfig_InitStruct->QSPI_ComConfig_ABMode));
  238. assert_param(IS_QSPI_ADSIZE (QSPI_ComConfig_InitStruct->QSPI_ComConfig_ADSize));
  239. assert_param(IS_QSPI_ADMODE (QSPI_ComConfig_InitStruct->QSPI_ComConfig_ADMode));
  240. assert_param(IS_QSPI_IMODE (QSPI_ComConfig_InitStruct->QSPI_ComConfig_IMode));
  241. assert_param(IS_QSPI_INSTRUCTION (QSPI_ComConfig_InitStruct->QSPI_ComConfig_Ins));
  242. assert_param(IS_QSPI_DDRMODE (QSPI_ComConfig_InitStruct->QSPI_ComConfig_DDRMode));
  243. assert_param(IS_QSPI_DHHC (QSPI_ComConfig_InitStruct->QSPI_ComConfig_DHHC));
  244. /*------------------------ QUADSPI CCR Configuration ------------------------*/
  245. /* Get the QUADSPI CCR value */
  246. tmpreg = QUADSPI->CCR;
  247. /* Clear FMODE Mode bits */
  248. tmpreg &= QSPI_CCR_CLEAR_MASK;
  249. /* Configure QUADSPI: CCR Configuration */
  250. tmpreg |= (uint32_t)( (QSPI_ComConfig_InitStruct->QSPI_ComConfig_FMode)
  251. | (QSPI_ComConfig_InitStruct->QSPI_ComConfig_DDRMode)
  252. | (QSPI_ComConfig_InitStruct->QSPI_ComConfig_DHHC)
  253. | (QSPI_ComConfig_InitStruct->QSPI_ComConfig_SIOOMode)
  254. | (QSPI_ComConfig_InitStruct->QSPI_ComConfig_DMode)
  255. | (QSPI_ComConfig_InitStruct->QSPI_ComConfig_ABSize)
  256. | (QSPI_ComConfig_InitStruct->QSPI_ComConfig_ABMode)
  257. | (QSPI_ComConfig_InitStruct->QSPI_ComConfig_ADSize)
  258. | (QSPI_ComConfig_InitStruct->QSPI_ComConfig_ADMode)
  259. | (QSPI_ComConfig_InitStruct->QSPI_ComConfig_IMode)
  260. | (QSPI_ComConfig_InitStruct->QSPI_ComConfig_Ins)
  261. |((QSPI_ComConfig_InitStruct->QSPI_ComConfig_DummyCycles)<<18));
  262. /* Write to QUADSPI DCR */
  263. QUADSPI->CCR = tmpreg;
  264. }
  265. /**
  266. * @brief Enables or disables QSPI peripheral.
  267. * @param NewState: new state of the QSPI peripheral.
  268. * This parameter can be: ENABLE or DISABLE.
  269. * @retval None
  270. */
  271. void QSPI_Cmd(FunctionalState NewState)
  272. {
  273. /* Check the parameters */
  274. assert_param(IS_FUNCTIONAL_STATE(NewState));
  275. if (NewState != DISABLE)
  276. {
  277. /* Enable QSPI peripheral */
  278. QUADSPI->CR |= QUADSPI_CR_EN;
  279. }
  280. else
  281. {
  282. /* Disable QSPI peripheral */
  283. QUADSPI->CR &= ~ QUADSPI_CR_EN;
  284. }
  285. }
  286. /**
  287. * @brief Configure the QSPI Automatic Polling Mode.
  288. * @param QSPI_Match: Value to be compared with the masked status register to get a match.
  289. * This parameter can be any value between 0x00000000 and 0xFFFFFFFF.
  290. * @param QSPI_Mask: Mask to be applied to the status bytes received in polling mode..
  291. * This parameter can be any value between 0x00000000 and 0xFFFFFFFF.
  292. * @param QSPI_Match_Mode: indicates which method should be used for determining a “match” during
  293. * automatic polling mode.
  294. * This parameter can be any value of :
  295. * @arg QSPI_PMM_AND: AND match mode- SMF is set if all the unmasked bits received from the flash match
  296. * the corresponding bits in the match register
  297. * @arg QSPI_PMM_OR: OR match mode- SMF is set if any one of the unmasked bits received from the flash
  298. matches its corresponding bit in the match register.
  299. * @note This function is used only in Automatic Polling Mode
  300. * @retval None
  301. */
  302. void QSPI_AutoPollingMode_Config(uint32_t QSPI_Match, uint32_t QSPI_Mask , uint32_t QSPI_Match_Mode)
  303. {
  304. /* Check the parameters */
  305. assert_param(IS_QSPI_PMM(QSPI_Match_Mode));
  306. if ((QUADSPI->SR & QUADSPI_SR_BUSY) == RESET)
  307. /* Device is not Busy */
  308. {
  309. /* Set the Match Register */
  310. QUADSPI->PSMAR = QSPI_Match ;
  311. /* Set the Mask Register */
  312. QUADSPI->PSMKR = QSPI_Mask ;
  313. /* Set the Polling Match Mode */
  314. if(QSPI_Match_Mode)
  315. /* OR Match Mode */
  316. {
  317. /* Set the PMM bit */
  318. QUADSPI->CR |= QUADSPI_CR_PMM;
  319. }
  320. else
  321. /* AND Match Mode */
  322. {
  323. /* Reset the PMM bit */
  324. QUADSPI->CR &= ~ QUADSPI_CR_PMM;
  325. }
  326. }
  327. }
  328. /**
  329. * @brief Sets the number of CLK cycle between two read during automatic polling phases.
  330. * @param QSPI_Interval: The number of CLK cycle between two read during automatic polling phases.
  331. * This parameter can be any value of between 0x0000 and 0xFFFF
  332. * @note This function is used only in Automatic Polling Mode
  333. * @retval None
  334. */
  335. void QSPI_AutoPollingMode_SetInterval(uint32_t QSPI_Interval)
  336. {
  337. uint32_t tmpreg = 0;
  338. /* Check the parameters */
  339. assert_param(IS_QSPI_PIR(QSPI_Interval));
  340. if ((QUADSPI->SR & QUADSPI_SR_BUSY) == RESET)
  341. /* Device is not Busy */
  342. {
  343. /* Read the PIR Register */
  344. tmpreg = QUADSPI->PIR ;
  345. /* Clear Polling interval Bits */
  346. tmpreg &= QSPI_PIR_CLEAR_MASK ;
  347. /* Set the QSPI Polling Interval Bits */
  348. tmpreg |= QSPI_Interval;
  349. /* Write the PIR Register */
  350. QUADSPI->PIR = tmpreg;
  351. }
  352. }
  353. /**
  354. * @brief Sets the value of the Timeout in Memory Mapped mode
  355. * @param QSPI_Timeout: This field indicates how many CLK cycles QSPI waits after the
  356. * FIFO becomes full until it raises nCS, putting the flash memory
  357. * in a lowerconsumption state.
  358. * This parameter can be any value of between 0x0000 and 0xFFFF
  359. * @note This function is used only in Memory Mapped Mode
  360. * @retval None
  361. */
  362. void QSPI_MemoryMappedMode_SetTimeout(uint32_t QSPI_Timeout)
  363. {
  364. uint32_t tmpreg = 0;
  365. /* Check the parameters */
  366. assert_param(IS_QSPI_TIMEOUT(QSPI_Timeout));
  367. if ((QUADSPI->SR & QUADSPI_SR_BUSY) == RESET)
  368. /* Device is not Busy */
  369. {
  370. /* Read the LPTR Register */
  371. tmpreg = QUADSPI->LPTR ;
  372. /* Clear Timeout Bits */
  373. tmpreg &= QSPI_LPTR_CLEAR_MASK ;
  374. /* Set Timeout Bits */
  375. tmpreg |= QSPI_Timeout;
  376. /* Write the LPTR Register */
  377. QUADSPI->LPTR = tmpreg;
  378. }
  379. }
  380. /**
  381. * @brief Sets the value of the Address
  382. * @param QSPI_Address: Address to be send to the external flash memory.
  383. * This parameter can be any value of between 0x00000000 and 0xFFFFFFFF
  384. * @note This function is used only in Indirect Mode
  385. * @retval None
  386. */
  387. void QSPI_SetAddress(uint32_t QSPI_Address)
  388. {
  389. if((QUADSPI->SR & QUADSPI_SR_BUSY) == RESET)
  390. /* Device is not Busy */
  391. {
  392. /* Write the AR Register */
  393. QUADSPI->AR = QSPI_Address;
  394. }
  395. }
  396. /**
  397. * @brief Sets the value of the Alternate Bytes
  398. * @param QSPI_AlternateByte: Optional data to be send to the external QSPI device right after the address.
  399. * This parameter can be any value of between 0x00000000 and 0xFFFFFFFF
  400. * @note This function is used only in Indirect Mode
  401. * @retval None
  402. */
  403. void QSPI_SetAlternateByte(uint32_t QSPI_AlternateByte)
  404. {
  405. if((QUADSPI->SR & QUADSPI_SR_BUSY) == RESET)
  406. /* Device is not Busy */
  407. {
  408. /* Write the ABR Register */
  409. QUADSPI->ABR = QSPI_AlternateByte;
  410. }
  411. }
  412. /**
  413. * @brief Sets the FIFO Threshold
  414. * @param QSPI_FIFOThres: Defines, in indirect mode, the threshold number
  415. * of bytes in the FIFO which will cause the FIFO Threshold Flag
  416. * FTF to be set.
  417. * This parameter can be any value of between 0x00 and 0x0F
  418. * @retval None
  419. */
  420. void QSPI_SetFIFOThreshold(uint32_t QSPI_FIFOThreshold)
  421. {
  422. uint32_t tmpreg = 0;
  423. /* Check the parameters */
  424. assert_param(IS_QSPI_FIFOTHRESHOLD(QSPI_FIFOThreshold));
  425. /* Read the CR Register */
  426. tmpreg = QUADSPI->CR ;
  427. /* Clear FIFO Threshold Bits */
  428. tmpreg &= QSPI_CR_CLEAR_FIFOTHRESHOLD_MASK ;
  429. /* Set FIFO Threshold Bits */
  430. tmpreg |= (QSPI_FIFOThreshold << 8);
  431. /* Write the CR Register */
  432. QUADSPI->CR = tmpreg;
  433. }
  434. /**
  435. * @brief Sets number of Bytes to be transferred
  436. * @param QSPI_DataLength: Number of data to be retrieved (value+1)
  437. * in indirect and status-polling modes. A value no greater than 3
  438. * (indicating 4 bytes) should be used for status-polling mode.
  439. * All 1s in indirect mode means undefined length, where QSPI will
  440. * continue until the end of memory, as defined by FSIZE
  441. * This parameter can be any value of between 0x00000000 and 0xFFFFFFFF
  442. * 0x0000_0000: 1 byte is to be transferred
  443. * 0x0000_0001: 2 bytes are to be transferred
  444. * 0x0000_0002: 3 bytes are to be transferred
  445. * 0x0000_0003: 4 bytes are to be transferred
  446. * ...
  447. * 0xFFFF_FFFD: 4,294,967,294 (4G-2) bytes are to be transferred
  448. * 0xFFFF_FFFE: 4,294,967,295 (4G-1) bytes are to be transferred
  449. * 0xFFFF_FFFF: undefined length -- all bytes until the end of flash memory (as defined
  450. * by FSIZE) are to be transferred
  451. * @note This function is not used in Memory Mapped Mode.
  452. * @retval None
  453. */
  454. void QSPI_SetDataLength(uint32_t QSPI_DataLength)
  455. {
  456. if ((QUADSPI->SR & QUADSPI_SR_BUSY) == RESET)
  457. /* Device is not Busy */
  458. {
  459. /* Write the DLR Register */
  460. QUADSPI->DLR = QSPI_DataLength;
  461. }
  462. }
  463. /**
  464. * @brief Enables or disables The Timeout Counter.
  465. * @param NewState: new state of the Timeout Counter.
  466. * This parameter can be: ENABLE or DISABLE.
  467. * @note This function is used only in Memory Mapped Mode.
  468. * @retval None
  469. */
  470. void QSPI_TimeoutCounterCmd(FunctionalState NewState)
  471. {
  472. /* Check the parameters */
  473. assert_param(IS_FUNCTIONAL_STATE(NewState));
  474. if ((QUADSPI->SR & QUADSPI_SR_BUSY) == RESET)
  475. /* Device is not Busy */
  476. {
  477. if (NewState != DISABLE)
  478. {
  479. /* Enable Timeout Counter */
  480. QUADSPI->CR |= QUADSPI_CR_TCEN;
  481. }
  482. else
  483. {
  484. /* Disable Timeout Counter */
  485. QUADSPI->CR &= ~ QUADSPI_CR_TCEN;
  486. }
  487. }
  488. }
  489. /**
  490. * @brief Enables or disables Automatic Polling Mode Stop when a match occurs.
  491. * @param NewState: new state of the Automatic Polling Mode Stop.
  492. * This parameter can be: ENABLE or DISABLE.
  493. * @note This function is used only in Automatic Polling Mode.
  494. * @retval None
  495. */
  496. void QSPI_AutoPollingModeStopCmd(FunctionalState NewState)
  497. {
  498. /* Check the parameters */
  499. assert_param(IS_FUNCTIONAL_STATE(NewState));
  500. if ((QUADSPI->SR & QUADSPI_SR_BUSY) == RESET)
  501. /* Device is not Busy */
  502. {
  503. if (NewState != DISABLE)
  504. {
  505. /* Enable Automatic Polling Mode Stop */
  506. QUADSPI->CR |= QUADSPI_CR_APMS;
  507. }
  508. else
  509. {
  510. /* Disable Automatic Polling Mode Stop */
  511. QUADSPI->CR &= ~ QUADSPI_CR_APMS;
  512. }
  513. }
  514. }
  515. /**
  516. * @brief Abort the on-going command sequence.
  517. * @param None
  518. * @retval None
  519. */
  520. void QSPI_AbortRequest(void)
  521. {
  522. /* Enable the ABORT request bit in CR */
  523. QUADSPI->CR |= QUADSPI_CR_ABORT;
  524. }
  525. /* Data transfers functions ***************************************************/
  526. /**
  527. * @brief Transmits a 8bit Data through the QSPI peripheral.
  528. * @param Data: Data to be transmitted.
  529. * @retval None
  530. */
  531. void QSPI_SendData8(uint8_t Data)
  532. {
  533. uint32_t quadspibase = 0;
  534. quadspibase = (uint32_t)QUADSPI;
  535. quadspibase += 0x20;
  536. *(__IO uint8_t *) quadspibase = Data;
  537. }
  538. /**
  539. * @brief Transmits a 16bit Data through the QSPI peripheral.
  540. * @param Data: Data to be transmitted.
  541. * @retval None
  542. */
  543. void QSPI_SendData16(uint16_t Data)
  544. {
  545. uint32_t quadspibase = 0;
  546. quadspibase = (uint32_t)QUADSPI;
  547. quadspibase += 0x20;
  548. *(__IO uint16_t *) quadspibase = Data;
  549. }
  550. /**
  551. * @brief Transmits a 32bit Data through the QSPI peripheral.
  552. * @param Data: Data to be transmitted.
  553. * @retval None
  554. */
  555. void QSPI_SendData32(uint32_t Data)
  556. {
  557. QUADSPI->DR = Data;
  558. }
  559. /**
  560. * @brief Returns the most recent received 8bit data by the QSPI peripheral.
  561. * @retval The value of the received data.
  562. */
  563. uint8_t QSPI_ReceiveData8(void)
  564. {
  565. uint32_t quadspibase = 0;
  566. quadspibase = (uint32_t)QUADSPI;
  567. quadspibase += 0x20;
  568. return *(__IO uint8_t *) quadspibase;
  569. }
  570. /**
  571. * @brief Returns the most recent received 16bit data by the QSPI peripheral.
  572. * @retval The value of the received data.
  573. */
  574. uint16_t QSPI_ReceiveData16(void)
  575. {
  576. uint32_t quadspibase = 0;
  577. quadspibase = (uint32_t)QUADSPI;
  578. quadspibase += 0x20;
  579. return *(__IO uint16_t *) quadspibase;
  580. }
  581. /**
  582. * @brief Returns the most recent received 32bit data by the QSPI peripheral.
  583. * @retval The value of the received data.
  584. */
  585. uint32_t QSPI_ReceiveData32(void)
  586. {
  587. return QUADSPI->DR;
  588. }
  589. /* DMA transfers management functions *****************************************/
  590. /**
  591. * @brief Enables or disables DMA for Indirect Mode.
  592. * @param NewState: new state of the Timeout Counter.
  593. * This parameter can be: ENABLE or DISABLE.
  594. * @retval None
  595. */
  596. void QSPI_DMACmd(FunctionalState NewState)
  597. {
  598. /* Check the parameters */
  599. assert_param(IS_FUNCTIONAL_STATE(NewState));
  600. if (NewState != DISABLE)
  601. {
  602. /* Enable DMA */
  603. QUADSPI->CR |= QUADSPI_CR_DMAEN;
  604. }
  605. else
  606. {
  607. /* Disable DMA */
  608. QUADSPI->CR &= ~ QUADSPI_CR_DMAEN;
  609. }
  610. }
  611. /* Interrupts and flags management functions **********************************/
  612. /**
  613. * @brief Enables or disables the specified QSPI interrupts.
  614. * @param QSPI_IT: specifies the QSPI interrupt source to be enabled or disabled.
  615. * This parameter can be one of the following values:
  616. * @arg QSPI_IT_TO: Timeout interrupt
  617. * @arg QSPI_IT_SM: Status Match interrupt
  618. * @arg QSPI_IT_FT: FIFO Threshold
  619. * @arg QSPI_IT_TC: Transfer Complete
  620. * @arg QSPI_IT_TE: Transfer Error
  621. * @param NewState: new state of the specified QSPI interrupt.
  622. * This parameter can be: ENABLE or DISABLE.
  623. * @retval None
  624. */
  625. void QSPI_ITConfig(uint32_t QSPI_IT, FunctionalState NewState)
  626. {
  627. uint32_t tmpreg = 0;
  628. /* Check the parameters */
  629. assert_param(IS_FUNCTIONAL_STATE(NewState));
  630. assert_param(IS_QSPI_IT(QSPI_IT));
  631. /* Read the CR Register */
  632. tmpreg = QUADSPI->CR ;
  633. if(NewState != DISABLE)
  634. {
  635. /* Enable the selected QSPI interrupt */
  636. tmpreg |= (uint32_t)(QSPI_IT & QSPI_CR_INTERRUPT_MASK);
  637. }
  638. else
  639. {
  640. /* Disable the selected QSPI interrupt */
  641. tmpreg &= ~(uint32_t)(QSPI_IT & QSPI_CR_INTERRUPT_MASK);
  642. }
  643. /* Write the CR Register */
  644. QUADSPI->CR = tmpreg ;
  645. }
  646. /**
  647. * @brief Returns the current QSPI FIFO filled level.
  648. * @retval Number of valid bytes which are being held in the FIFO.
  649. * 0x00 : FIFO is empty
  650. * 0x1F : FIFO is full
  651. */
  652. uint32_t QSPI_GetFIFOLevel(void)
  653. {
  654. /* Get the QSPI FIFO level bits */
  655. return ((QUADSPI->SR & QUADSPI_SR_FLEVEL)>> 8);
  656. }
  657. /**
  658. * @brief Returns the QSPI functional mode.
  659. * @param None
  660. * @retval QSPI Functional Mode .The returned value can be one of the following:
  661. * - 0x00000000: QSPI_FMode_Indirect_Write
  662. * - 0x04000000: QSPI_FMode_Indirect_Read
  663. * - 0x08000000: QSPI_FMode_AutoPolling
  664. * - 0x0C000000: QSPI_FMode_MemoryMapped
  665. */
  666. uint32_t QSPI_GetFMode(void)
  667. {
  668. /* Return the QSPI_FMode */
  669. return (QUADSPI->CCR & QUADSPI_CCR_FMODE);
  670. }
  671. /**
  672. * @brief Checks whether the specified QSPI flag is set or not.
  673. * @param QSPI_FLAG: specifies the QSPI flag to check.
  674. * This parameter can be one of the following values:
  675. * @arg QSPI_FLAG_TO: Timeout interrupt flag
  676. * @arg QSPI_FLAG_SM: Status Match interrupt flag
  677. * @arg QSPI_FLAG_FT: FIFO Threshold flag
  678. * @arg QSPI_FLAG_TC: Transfer Complete flag
  679. * @arg QSPI_FLAG_TE: Transfer Error flag
  680. * @arg QSPI_FLAG_BUSY: Busy flag
  681. * @retval The new state of QSPI_FLAG (SET or RESET).
  682. */
  683. FlagStatus QSPI_GetFlagStatus(uint32_t QSPI_FLAG)
  684. {
  685. FlagStatus bitstatus = RESET;
  686. /* Check the parameters */
  687. assert_param(IS_QSPI_GET_FLAG(QSPI_FLAG));
  688. /* Check the status of the specified QSPI flag */
  689. if((QUADSPI->SR & QSPI_FLAG) != RESET)
  690. {
  691. /* QSPI_FLAG is set */
  692. bitstatus = SET;
  693. }
  694. else
  695. {
  696. /* QSPI_FLAG is reset */
  697. bitstatus = RESET;
  698. }
  699. /* Return the QSPI_FLAG status */
  700. return bitstatus;
  701. }
  702. /**
  703. * @brief Clears the QSPI flag.
  704. * @param QSPI_FLAG: specifies the QSPI flag to clear.
  705. * This parameter can be one of the following values:
  706. * @arg QSPI_FLAG_TO: Timeout interrupt flag
  707. * @arg QSPI_FLAG_SM: Status Match interrupt flag
  708. * @arg QSPI_FLAG_TC: Transfer Complete flag
  709. * @arg QSPI_FLAG_TE: Transfer Error flag
  710. * @retval None
  711. */
  712. void QSPI_ClearFlag(uint32_t QSPI_FLAG)
  713. {
  714. /* Check the parameters */
  715. assert_param(IS_QSPI_CLEAR_FLAG(QSPI_FLAG));
  716. /* Clear the selected QSPI flags */
  717. QUADSPI->FCR = QSPI_FLAG;
  718. }
  719. /**
  720. * @brief Checks whether the specified QSPI interrupt has occurred or not.
  721. * @param QSPI_IT: specifies the QSPI interrupt source to check.
  722. * This parameter can be one of the following values:
  723. * @arg QSPI_IT_TO: Timeout interrupt
  724. * @arg QSPI_IT_SM: Status Match interrupt
  725. * @arg QSPI_IT_FT: FIFO Threshold
  726. * @arg QSPI_IT_TC: Transfer Complete
  727. * @arg QSPI_IT_TE: Transfer Error
  728. * @retval The new state of QSPI_IT (SET or RESET).
  729. */
  730. ITStatus QSPI_GetITStatus(uint32_t QSPI_IT)
  731. {
  732. ITStatus bitstatus = RESET;
  733. uint32_t tmpcreg = 0, tmpsreg = 0;
  734. /* Check the parameters */
  735. assert_param(IS_QSPI_IT(QSPI_IT));
  736. /* Read the QUADSPI CR */
  737. tmpcreg = QUADSPI->CR;
  738. tmpcreg &= (uint32_t)(QSPI_IT & QSPI_CR_INTERRUPT_MASK);
  739. /* Read the QUADSPI SR */
  740. tmpsreg = QUADSPI->SR;
  741. tmpsreg &= (uint32_t)(QSPI_IT & QSPI_SR_INTERRUPT_MASK);
  742. /* Check the status of the specified QSPI interrupt */
  743. if((tmpcreg != RESET) && (tmpsreg != RESET))
  744. {
  745. /* QSPI_IT is set */
  746. bitstatus = SET;
  747. }
  748. else
  749. {
  750. /* QSPI_IT is reset */
  751. bitstatus = RESET;
  752. }
  753. /* Return the QSPI_IT status */
  754. return bitstatus;
  755. }
  756. /**
  757. * @brief Clears the QSPI's interrupt pending bits.
  758. * @param QSPI_IT: specifies the QSPI pending bit to clear.
  759. * This parameter can be one of the following values:
  760. * @arg QSPI_IT_TO: Timeout interrupt
  761. * @arg QSPI_IT_SM: Status Match interrupt
  762. * @arg QSPI_IT_TC: Transfer Complete
  763. * @arg QSPI_IT_TE: Transfer Error
  764. * @retval None
  765. */
  766. void QSPI_ClearITPendingBit(uint32_t QSPI_IT)
  767. {
  768. /* Check the parameters */
  769. assert_param(IS_QSPI_CLEAR_IT(QSPI_IT));
  770. QUADSPI->FCR = (uint32_t)(QSPI_IT & QSPI_FSR_INTERRUPT_MASK);
  771. }
  772. /**
  773. * @brief Enables or disables QSPI Dual Flash Mode.
  774. * @param NewState: new state of the QSPI Dual Flash Mode.
  775. * This parameter can be: ENABLE or DISABLE.
  776. * @retval None
  777. */
  778. void QSPI_DualFlashMode_Cmd(FunctionalState NewState)
  779. {
  780. /* Check the parameters */
  781. assert_param(IS_FUNCTIONAL_STATE(NewState));
  782. if (NewState != DISABLE)
  783. {
  784. /* Enable QSPI Dual Flash Mode */
  785. QUADSPI->CR |= QUADSPI_CR_DFM;
  786. }
  787. else
  788. {
  789. /* Disable QSPI Dual Flash Mode */
  790. QUADSPI->CR &= ~ QUADSPI_CR_DFM;
  791. }
  792. }
  793. /**
  794. * @}
  795. */
  796. /**
  797. * @}
  798. */
  799. #endif /* STM32F412xG || STM32F413_423xx || STM32F446xx || STM32F469_479xx */
  800. /**
  801. * @}
  802. */
  803. /**
  804. * @}
  805. */