stm32f4xx_qspi.c 31 KB

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