hc32f4a0_qspi.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. /**
  2. *******************************************************************************
  3. * @file hc32f4a0_qspi.h
  4. * @brief This file contains all the functions prototypes of the QSPI driver
  5. * library.
  6. @verbatim
  7. Change Logs:
  8. Date Author Notes
  9. 2020-06-12 Wuze First version
  10. 2020-07-15 Wuze Corrected the definition of 'QSPI_4BIC_ENABLE'.
  11. @endverbatim
  12. *******************************************************************************
  13. * Copyright (C) 2020, Huada Semiconductor Co., Ltd. All rights reserved.
  14. *
  15. * This software component is licensed by HDSC under BSD 3-Clause license
  16. * (the "License"); You may not use this file except in compliance with the
  17. * License. You may obtain a copy of the License at:
  18. * opensource.org/licenses/BSD-3-Clause
  19. *
  20. *******************************************************************************
  21. */
  22. #ifndef __HC32F4A0_QSPI_H__
  23. #define __HC32F4A0_QSPI_H__
  24. /* C binding of definitions if building with C++ compiler */
  25. #ifdef __cplusplus
  26. extern "C"
  27. {
  28. #endif
  29. /*******************************************************************************
  30. * Include files
  31. ******************************************************************************/
  32. #include "hc32_common.h"
  33. #include "ddl_config.h"
  34. /**
  35. * @addtogroup HC32F4A0_DDL_Driver
  36. * @{
  37. */
  38. /**
  39. * @addtogroup DDL_QSPI
  40. * @{
  41. */
  42. #if (DDL_QSPI_ENABLE == DDL_ON)
  43. /*******************************************************************************
  44. * Global type definitions ('typedef')
  45. ******************************************************************************/
  46. /**
  47. * @defgroup QSPI_Global_Types QSPI Global Types
  48. * @{
  49. */
  50. /**
  51. * @brief QSPI initialization structure.
  52. */
  53. typedef struct
  54. {
  55. uint32_t u32ClkDiv; /*!< Specifies the clock divider for QSCK. QSCK = HCLK / u32ClkDiv.
  56. This parameter can be a value between 2U and 64U, inclusive. */
  57. uint32_t u32CSSetupTiming; /*!< Specifies the setup timing of CS.
  58. This parameter can be a value of @ref QSPI_CS_Setup_Timing */
  59. uint32_t u32CSReleaseTiming; /*!< Specifies the hold timing of CS.
  60. This parameter can be a value of @ref QSPI_CS_Release_Timing */
  61. uint32_t u32CSIdleTime; /*!< Specifies the minimum idle time for CS. CS needs idle(stay high) for several cycles between commands.
  62. This parameter can be a value of between 1U and 16U, inclusive. */
  63. uint32_t u32CSExtendTime; /*!< Extend the time of chip-select signal after SPI bus access.
  64. This parameter can be a value of @ref QSPI_CS_Extend_Time */
  65. uint32_t u32SPIMode; /*!< Specifies the SPI mode. The difference between SPI modes 0 and 3 is the standby level of the QSCK signal.
  66. The standby level of the QSCK signal in SPI mode 0 is low, and high in SPI mode 3.
  67. This parameter can be a value of @ref QSPI_SPI_Mode */
  68. uint32_t u32PrefetchCmd; /*!< Enable or disable prefeth function.
  69. This parameter can be a value of @ref QSPI_Prefetch_Cmd */
  70. uint32_t u32PrefetchStopPos; /*!< Specifies the position of prefetch stop.
  71. This parameter can be a value of @ref QSPI_Prefetch_Stop_Position */
  72. uint32_t u32WPLevel; /*!< Specifies the level of pin WP(IO2).
  73. This parameter can be a value of @ref QSPI_WP_Level */
  74. uint32_t u32CommMode; /*!< Specifies the communication mode.
  75. This parameter can be a value of @ref QSPI_Communication_Mode */
  76. uint32_t u32AddrWidth; /*!< Specifies the address width.
  77. This parameter can be a value of @ref QSPI_Addr_Width */
  78. uint32_t u32InstrMode; /*!< Specifies the instruction mode.
  79. This parameter can be a value of @ref QSPI_Instruction_Mode */
  80. uint32_t u32AddrMode; /*!< Specifies the address mode.
  81. This parameter can be a value of @ref QSPI_Addr_Mode */
  82. uint32_t u32DataMode; /*!< Specifies the data mode (used for dummy cycles and data phases)
  83. This parameter can be a value of @ref QSPI_Data_Mode */
  84. uint32_t u32ReadMode; /*!< Specifies the read mode.
  85. This parameter can be a value of @ref QSPI_Read_Mode */
  86. uint8_t u8RomAccessInstr; /*!< Rom access instruction. This parameter only supports read instruction of QSPI flash now.
  87. Tis instruction must correspond to the read mode that specified by parameter 'u32ReadMode'. */
  88. uint32_t u32DummyCycles; /*!< Specifies the number of dummy cycles for fast read.
  89. This parameter can be a value between 3U and 18U. */
  90. } stc_qspi_init_t;
  91. /**
  92. * @}
  93. */
  94. /*******************************************************************************
  95. * Global pre-processor symbols/macros ('#define')
  96. ******************************************************************************/
  97. /**
  98. * @defgroup QSPI_Global_Macros QSPI Global Macros
  99. * @{
  100. */
  101. /**
  102. * @defgroup QSPI_Read_Mode QSPI Read Mode
  103. * @{
  104. */
  105. #define QSPI_READ_STANDARD_READ (0x0U) /*!< Standard read mode (no dummy cycles). */
  106. #define QSPI_READ_FAST_READ (QSPI_CR_MDSEL_0) /*!< Fast read mode (dummy cycles between address and data). */
  107. #define QSPI_READ_FAST_READ_DUAL_OUTPUT (QSPI_CR_MDSEL_1) /*!< Fast read dual output mode (data on 2 lines). */
  108. #define QSPI_READ_FAST_READ_DUAL_IO (QSPI_CR_MDSEL_1 | \
  109. QSPI_CR_MDSEL_0) /*!< Fast read dual I/O mode (address and data on 2 lines). */
  110. #define QSPI_READ_FAST_READ_QUAD_OUTPUT (QSPI_CR_MDSEL_2) /*!< Fast read quad output mode (data on 4 lines). */
  111. #define QSPI_READ_FAST_READ_QUAD_IO (QSPI_CR_MDSEL_2 | \
  112. QSPI_CR_MDSEL_0) /*!< Fast read quad I/O mode (address and data on 4 lines). */
  113. #define QSPI_READ_CUSTOM_STANDARD_READ (QSPI_CR_MDSEL_2 | \
  114. QSPI_CR_MDSEL_1) /*!< Custom standard read mode. */
  115. #define QSPI_READ_CUSTOM_FAST_READ (QSPI_CR_MDSEL_2 | \
  116. QSPI_CR_MDSEL_1 | \
  117. QSPI_CR_MDSEL_0) /*!< Custom fast read mode. */
  118. /**
  119. * @}
  120. */
  121. /**
  122. * @defgroup QSPI_Prefetch_Cmd QSPI Prefetch Cmd
  123. * @{
  124. */
  125. #define QSPI_PREFETCH_DISABLE (0x0U) /*!< Disable prefetch. */
  126. #define QSPI_PREFETCH_ENABLE (QSPI_CR_PFE) /*!< Enable prefetch. */
  127. /**
  128. * @}
  129. */
  130. /**
  131. * @defgroup QSPI_Prefetch_Stop_Position QSPI Prefetch Stop Position
  132. * @{
  133. */
  134. #define QSPI_PREFETCH_STOP_BYTE_EDGE (0x0U) /*!< Stop prefetch at the edge of byte. */
  135. #define QSPI_PREFETCH_STOP_IMMED (QSPI_CR_PFSAE) /*!< Stop prefetch at current position immediately. */
  136. /**
  137. * @}
  138. */
  139. /**
  140. * @defgroup QSPI_Communication_Mode QSPI Communication Mode
  141. * @{
  142. */
  143. #define QSPI_COMM_ROM_ACCESS (0x0U) /*!< ROM access mode. */
  144. #define QSPI_COMM_DIRECT_COMM (QSPI_CR_DCOME) /*!< Direct communication mode. */
  145. /**
  146. * @}
  147. */
  148. /**
  149. * @defgroup QSPI_XIP_Mode_Command QSPI XIP Mode Command
  150. * @{
  151. */
  152. #define QSPI_XIP_DISABLE (0x0U) /*!< XIP mode disable. */
  153. #define QSPI_XIP_ENABLE (QSPI_CR_XIPE) /*!< XIP mode enable. */
  154. /**
  155. * @}
  156. */
  157. /**
  158. * @defgroup QSPI_SPI_Mode QSPI SPI Mode
  159. * @{
  160. */
  161. #define QSPI_SPI_MODE_0 (0x0U) /*!< Selects SPI mode 0. */
  162. #define QSPI_SPI_MODE_3 (QSPI_CR_SPIMD3) /*!< Selects SPI mode 3. */
  163. /**
  164. * @}
  165. */
  166. /**
  167. * @defgroup QSPI_Instruction_Mode QSPI Instruction Mode
  168. * @{
  169. */
  170. #define QSPI_INSTR_1LINE (0x0U) /*!< Instruction on a single line. */
  171. #define QSPI_INSTR_2LINE (QSPI_CR_IPRSL_0) /*!< Instruction on 2 lines. */
  172. #define QSPI_INSTR_4LINE (QSPI_CR_IPRSL_1) /*!< Instruction on 4 lines. */
  173. /**
  174. * @}
  175. */
  176. /**
  177. * @defgroup QSPI_Addr_Mode QSPI Address Mode
  178. * @{
  179. */
  180. #define QSPI_ADDR_1LINE (0x0U) /*!< Address on a single line. */
  181. #define QSPI_ADDR_2LINE (QSPI_CR_APRSL_0) /*!< Address on 2 lines. */
  182. #define QSPI_ADDR_4LINE (QSPI_CR_APRSL_1) /*!< Address on 4 lines. */
  183. /**
  184. * @}
  185. */
  186. /**
  187. * @defgroup QSPI_Data_Mode QSPI Data Mode
  188. * @{
  189. */
  190. #define QSPI_DATA_1LINE (0x0U) /*!< Data on a single line. */
  191. #define QSPI_DATA_2LINE (QSPI_CR_DPRSL_0) /*!< Data on 2 lines. */
  192. #define QSPI_DATA_4LINE (QSPI_CR_DPRSL_1) /*!< Data on 4 lines. */
  193. /**
  194. * @}
  195. */
  196. /**
  197. * @defgroup QSPI_CS_Extend_Time QSPI Chip-select Extend Time
  198. * @{
  199. */
  200. #define QSPI_CS_EXTEND_0CYCLE (0x0U) /*!< Do not extend chip-select signal time. */
  201. #define QSPI_CS_EXTEND_32CYCLE (QSPI_CSCR_SSNW_0) /*!< Extend chip-select time by 32 QSCK cycles. */
  202. #define QSPI_CS_EXTEND_128CYCLE (QSPI_CSCR_SSNW_1) /*!< Extend chip-select time by 128 QSCK cycles. */
  203. #define QSPI_CS_EXTEND_INFINITE (QSPI_CSCR_SSNW_1 | \
  204. QSPI_CSCR_SSNW_0) /*!< Extend chip-select time infinitely. */
  205. /**
  206. * @}
  207. */
  208. /**
  209. * @defgroup QSPI_Addr_Width QSPI Address Width
  210. * @{
  211. */
  212. #define QSPI_ADDR_WIDTH_1BYTE (0x0U) /*!< QSPI address width is 1 byte. */
  213. #define QSPI_ADDR_WIDTH_2BYTE (QSPI_FCR_AWSL_0) /*!< QSPI address width is 2 bytes. */
  214. #define QSPI_ADDR_WIDTH_3BYTE (QSPI_FCR_AWSL_1) /*!< QSPI address width is 3 bytes. */
  215. #define QSPI_ADDR_WIDTH_4BYTE (QSPI_FCR_AWSL_1 | \
  216. QSPI_FCR_AWSL_0) /*!< QSPI address width is 4 bytes. */
  217. /**
  218. * @}
  219. */
  220. /**
  221. * @defgroup QSPI_4BIC_Command QSPI 4-Byte Address Instruction Read Code Command
  222. * @note Valid when the serial interface address width is 4 bytes.
  223. * @{
  224. */
  225. #define QSPI_4BIC_DISABLE (0x0U) /*!< Do not use 4-byte address read instruction code. */
  226. #define QSPI_4BIC_ENABLE (QSPI_FCR_FOUR_BIC) /*!< Use 4-byte address read instruction code. */
  227. /**
  228. * @}
  229. */
  230. /**
  231. * @defgroup QSPI_CS_Release_Timing QSPI Chip-select Hold Timing
  232. * @{
  233. */
  234. #define QSPI_CS_RELEASE_AFTER_0P5_CYCLE (0x0U) /*!< Release chip-select signal 0.5 QSCK cycles after the last rising edge of QSCK. */
  235. #define QSPI_CS_RELEASE_AFTER_1P5_CYCLE (QSPI_FCR_SSNHD) /*!< Release chip-select signal 1.5 QSCK cycles after the last rising edge of QSCK. */
  236. /**
  237. * @}
  238. */
  239. /**
  240. * @defgroup QSPI_CS_Setup_Timing QSPI Chip-select Setup Timing
  241. * @{
  242. */
  243. #define QSPI_CS_SETUP_BEFORE_0P5_CYCLE (0x0U) /*!< Output chip-select signal 0.5 QSCK cycles before the first rising edge of QSCK. */
  244. #define QSPI_CS_SETUP_BEFORE_1P5_CYCLE (QSPI_FCR_SSNLD) /*!< Output chip-select signal 1.5 QSCK cycles before the first rising edge of QSCK. */
  245. /**
  246. * @}
  247. */
  248. /**
  249. * @defgroup QSPI_WP_Level QSPI Write Protect Pin Level
  250. * @{
  251. */
  252. #define QSPI_WP_LOW (0x0U) /*!< WP(QIO2) output low. */
  253. #define QSPI_WP_HIGH (QSPI_FCR_WPOL) /*!< WP(QIO2) output high. */
  254. /**
  255. * @}
  256. */
  257. /**
  258. * @defgroup QSPI_QSCK_Duty_Correction_Command QSPI QSCK Duty Correction Command
  259. * @{
  260. */
  261. #define QSPI_DUTY_CORRECTION_DISABLE (0x0U) /*!< Make no duty ratio correction on QSCK. */
  262. #define QSPI_DUTY_CORRECTION_ENABLE (QSPI_FCR_DUTY) /*!< Delay the rising of the QSCK signal by 0.5 HCLK cycles. \
  263. (Valid when HCLK is multiplied by an odd number.) */
  264. /**
  265. * @}
  266. */
  267. /**
  268. * @defgroup QSPI_Status_Flag QSPI Status Flag
  269. * @{
  270. */
  271. #define QSPI_FLAG_DIRECT_COMM_BUSY (QSPI_SR_BUSY) /*!< Serial transfer being processed. */
  272. #define QSPI_FLAG_XIP_MODE (QSPI_SR_XIPF) /*!< XIP mode. */
  273. #define QSPI_FLAG_ROM_ACCESS_ERR (QSPI_SR_RAER) /*!< ROM access detection status in direct communication mode. */
  274. #define QSPI_FLAG_PREFETCH_BUF_FULL (QSPI_SR_PFFUL) /*!< Prefetch buffer is full. */
  275. #define QSPI_FLAG_PREFETCH_ACTIVE (QSPI_SR_PFAN) /*!< Prefetch function operating. */
  276. #define QSPI_FLAG_ALL (QSPI_FLAG_DIRECT_COMM_BUSY | \
  277. QSPI_FLAG_XIP_MODE | \
  278. QSPI_FLAG_ROM_ACCESS_ERR | \
  279. QSPI_FLAG_PREFETCH_BUF_FULL | \
  280. QSPI_FLAG_PREFETCH_ACTIVE)
  281. /**
  282. * @}
  283. */
  284. /**
  285. * @}
  286. */
  287. /*******************************************************************************
  288. * Global variable definitions ('extern')
  289. ******************************************************************************/
  290. /*******************************************************************************
  291. Global function prototypes (definition in C source)
  292. ******************************************************************************/
  293. /**
  294. * @addtogroup QSPI_Global_Functions
  295. * @{
  296. */
  297. /**
  298. * @brief Enter direct communication mode.
  299. * @param None
  300. * @retval None
  301. */
  302. __STATIC_INLINE void QSPI_EnterDirectCommMode(void)
  303. {
  304. /* Set standard read mode. */
  305. CLEAR_REG32_BIT(M4_QSPI->CR, QSPI_CR_MDSEL);
  306. /* Enter direct communication mode. */
  307. SET_REG32_BIT(M4_QSPI->CR, QSPI_CR_DCOME);
  308. }
  309. /**
  310. * @brief Exit direct communication mode.
  311. * @param None
  312. * @retval None
  313. */
  314. __STATIC_INLINE void QSPI_ExitDirectCommMode(void)
  315. {
  316. CLEAR_REG32_BIT(M4_QSPI->CR, QSPI_CR_DCOME);
  317. }
  318. /**
  319. * @brief Write data in direct communication mode.
  320. * @param [in] u8Val Byte data.
  321. * @retval None
  322. */
  323. __STATIC_INLINE void QSPI_WriteDirectCommValue(uint8_t u8Val)
  324. {
  325. WRITE_REG32(M4_QSPI->DCOM, u8Val);
  326. }
  327. /**
  328. * @brief Read data in communication mode.
  329. * @param None
  330. * @retval Byte data.
  331. */
  332. __STATIC_INLINE uint8_t QSPI_ReadDirectCommValue(void)
  333. {
  334. return (uint8_t)M4_QSPI->DCOM;
  335. }
  336. /**
  337. * @brief Set ROM access instruction.
  338. * @param [in] u8Instr ROM access instruction.
  339. * @retval None
  340. */
  341. __STATIC_INLINE void QSPI_SetRomAccressInstr(uint8_t u8Instr)
  342. {
  343. WRITE_REG32(M4_QSPI->CCMD, u8Instr);
  344. }
  345. en_result_t QSPI_Init(const stc_qspi_init_t *pstcInit);
  346. void QSPI_DeInit(void);
  347. en_result_t QSPI_StructInit(stc_qspi_init_t *pstcInit);
  348. en_result_t QSPI_WriteData(uint32_t u32Instr, uint32_t u32Address, \
  349. const uint8_t pu8Src[], uint32_t u32SrcSize);
  350. en_result_t QSPI_ReadData(uint32_t u32Address, uint8_t pu8Dest[], uint32_t u32DestSize);
  351. void QSPI_SetReadMode(uint32_t u32ReadMode, uint8_t u8ReadInstr, uint32_t u32DummyCycles);
  352. void QSPI_DutyCorrectCmd(en_functional_state_t enNewState);
  353. void QSPI_PrefetchCmd(en_functional_state_t enNewState);
  354. void QSPI_XIPModeCmd(en_functional_state_t enNewState);
  355. void QSPI_SetWPPinLevel(uint32_t u32Level);
  356. void QSPI_4ByteAddrModeCmd(en_functional_state_t enNewState);
  357. void QSPI_SelectBlock(uint32_t u32Block);
  358. uint32_t QSPI_GetPrefetchedSize(void);
  359. en_flag_status_t QSPI_GetStatus(uint32_t u32Flag);
  360. void QSPI_ClrStatus(uint32_t u32Flag);
  361. void QSPI_EnterDirectCommMode(void);
  362. void QSPI_ExitDirectCommMode(void);
  363. void QSPI_WriteDirectCommValue(uint8_t u8Val);
  364. uint8_t QSPI_ReadDirectCommValue(void);
  365. void QSPI_SetRomAccressInstr(uint8_t u8Instr);
  366. /**
  367. * @}
  368. */
  369. #endif /* DDL_QSPI_ENABLE */
  370. /**
  371. * @}
  372. */
  373. /**
  374. * @}
  375. */
  376. #ifdef __cplusplus
  377. }
  378. #endif
  379. #endif /* __HC32F4A0_QSPI_H__ */
  380. /*******************************************************************************
  381. * EOF (not truncated)
  382. ******************************************************************************/