ch32v30x_fsmc.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. /********************************** (C) COPYRIGHT *******************************
  2. * File Name : ch32v30x_fsmc.c
  3. * Author : WCH
  4. * Version : V1.0.1
  5. * Date : 2025/04/06
  6. * Description : This file provides all the FSMC firmware functions.
  7. *********************************************************************************
  8. * Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
  9. * Attention: This software (modified or not) and binary are used for
  10. * microcontroller manufactured by Nanjing Qinheng Microelectronics.
  11. *******************************************************************************/
  12. #include "ch32v30x_fsmc.h"
  13. #include "ch32v30x_rcc.h"
  14. /* FSMC BCRx Mask */
  15. #define BCR_MBKEN_Set ((uint32_t)0x00000001)
  16. #define BCR_MBKEN_Reset ((uint32_t)0x000FFFFE)
  17. #define BCR_FACCEN_Set ((uint32_t)0x00000040)
  18. /* FSMC PCRx Mask */
  19. #define PCR_PBKEN_Set ((uint32_t)0x00000004)
  20. #define PCR_PBKEN_Reset ((uint32_t)0x000FFFFB)
  21. #define PCR_ECCEN_Set ((uint32_t)0x00000040)
  22. #define PCR_ECCEN_Reset ((uint32_t)0x000FFFBF)
  23. #define PCR_MemoryType_NAND ((uint32_t)0x00000008)
  24. /*********************************************************************
  25. * @fn FSMC_NORSRAMDeInit
  26. *
  27. * @brief Deinitializes the FSMC NOR/SRAM Banks registers to their default
  28. * reset values.
  29. *
  30. * @param FSMC_Bank-
  31. * FSMC_Bank1_NORSRAM1 - FSMC Bank1 NOR/SRAM1.
  32. *
  33. * @return none
  34. */
  35. void FSMC_NORSRAMDeInit(uint32_t FSMC_Bank)
  36. {
  37. if(FSMC_Bank == FSMC_Bank1_NORSRAM1)
  38. {
  39. FSMC_Bank1->BTCR[FSMC_Bank] = 0x000030DB;
  40. }
  41. else
  42. {
  43. FSMC_Bank1->BTCR[FSMC_Bank] = 0x000030D2;
  44. }
  45. FSMC_Bank1->BTCR[FSMC_Bank + 1] = 0x0FFFFFFF;
  46. FSMC_Bank1E->BWTR[FSMC_Bank] = 0x0FFFFFFF;
  47. }
  48. /*********************************************************************
  49. * @fn FSMC_NANDDeInit
  50. *
  51. * @brief Deinitializes the FSMC NAND Banks registers to their default
  52. * reset values.
  53. *
  54. * @param FSMC_Bank -
  55. * FSMC_Bank2_NAND - FSMC Bank2 NAND.
  56. *
  57. * @return none
  58. */
  59. void FSMC_NANDDeInit(uint32_t FSMC_Bank)
  60. {
  61. if(FSMC_Bank == FSMC_Bank2_NAND)
  62. {
  63. FSMC_Bank2->PCR2 = 0x00000018;
  64. FSMC_Bank2->SR2 = 0x00000040;
  65. FSMC_Bank2->PMEM2 = 0xFCFCFCFC;
  66. FSMC_Bank2->PATT2 = 0xFCFCFCFC;
  67. }
  68. }
  69. /*********************************************************************
  70. * @fn FSMC_NORSRAMInit
  71. *
  72. * @brief Initializes the FSMC NOR/SRAM Banks according to the specified
  73. * parameters in the FSMC_NORSRAMInitStruct.
  74. *
  75. * @param SMC_NORSRAMInitStruct:pointer to a FSMC_NORSRAMInitTypeDef
  76. * structure that contains the configuration information for the FSMC NOR/SRAM
  77. * specified Banks.
  78. *
  79. * @return none
  80. */
  81. void FSMC_NORSRAMInit(FSMC_NORSRAMInitTypeDef *FSMC_NORSRAMInitStruct)
  82. {
  83. FSMC_Bank1->BTCR[FSMC_NORSRAMInitStruct->FSMC_Bank] =
  84. (uint32_t)FSMC_NORSRAMInitStruct->FSMC_DataAddressMux |
  85. FSMC_NORSRAMInitStruct->FSMC_MemoryType |
  86. FSMC_NORSRAMInitStruct->FSMC_MemoryDataWidth |
  87. FSMC_NORSRAMInitStruct->FSMC_BurstAccessMode |
  88. FSMC_NORSRAMInitStruct->FSMC_AsynchronousWait |
  89. FSMC_NORSRAMInitStruct->FSMC_WaitSignalPolarity |
  90. FSMC_NORSRAMInitStruct->FSMC_WaitSignalActive |
  91. FSMC_NORSRAMInitStruct->FSMC_WriteOperation |
  92. FSMC_NORSRAMInitStruct->FSMC_WaitSignal |
  93. FSMC_NORSRAMInitStruct->FSMC_ExtendedMode |
  94. FSMC_NORSRAMInitStruct->FSMC_WriteBurst;
  95. if(FSMC_NORSRAMInitStruct->FSMC_MemoryType == FSMC_MemoryType_NOR)
  96. {
  97. FSMC_Bank1->BTCR[FSMC_NORSRAMInitStruct->FSMC_Bank] |= (uint32_t)BCR_FACCEN_Set;
  98. }
  99. FSMC_Bank1->BTCR[FSMC_NORSRAMInitStruct->FSMC_Bank + 1] =
  100. (uint32_t)FSMC_NORSRAMInitStruct->FSMC_ReadWriteTimingStruct->FSMC_AddressSetupTime |
  101. (FSMC_NORSRAMInitStruct->FSMC_ReadWriteTimingStruct->FSMC_AddressHoldTime << 4) |
  102. (FSMC_NORSRAMInitStruct->FSMC_ReadWriteTimingStruct->FSMC_DataSetupTime << 8) |
  103. (FSMC_NORSRAMInitStruct->FSMC_ReadWriteTimingStruct->FSMC_BusTurnAroundDuration << 16) |
  104. (FSMC_NORSRAMInitStruct->FSMC_ReadWriteTimingStruct->FSMC_CLKDivision << 20) |
  105. (FSMC_NORSRAMInitStruct->FSMC_ReadWriteTimingStruct->FSMC_DataLatency << 24) |
  106. FSMC_NORSRAMInitStruct->FSMC_ReadWriteTimingStruct->FSMC_AccessMode;
  107. if(FSMC_NORSRAMInitStruct->FSMC_ExtendedMode == FSMC_ExtendedMode_Enable)
  108. {
  109. FSMC_Bank1E->BWTR[FSMC_NORSRAMInitStruct->FSMC_Bank] =
  110. (uint32_t)FSMC_NORSRAMInitStruct->FSMC_WriteTimingStruct->FSMC_AddressSetupTime |
  111. (FSMC_NORSRAMInitStruct->FSMC_WriteTimingStruct->FSMC_AddressHoldTime << 4) |
  112. (FSMC_NORSRAMInitStruct->FSMC_WriteTimingStruct->FSMC_DataSetupTime << 8) |
  113. FSMC_NORSRAMInitStruct->FSMC_WriteTimingStruct->FSMC_AccessMode;
  114. }
  115. else
  116. {
  117. FSMC_Bank1E->BWTR[FSMC_NORSRAMInitStruct->FSMC_Bank] = 0x0FFFFFFF;
  118. }
  119. }
  120. /*********************************************************************
  121. * @fn FSMC_NANDInit
  122. *
  123. * @brief Initializes the FSMC NAND Banks according to the specified
  124. * parameters in the FSMC_NANDInitStruct.
  125. *
  126. * @param FSMC_NANDInitStruct - pointer to a FSMC_NANDInitTypeDef
  127. * structure that contains the configuration information for the FSMC
  128. * NAND specified Banks.
  129. *
  130. * @return none
  131. */
  132. void FSMC_NANDInit(FSMC_NANDInitTypeDef *FSMC_NANDInitStruct)
  133. {
  134. uint32_t tmppcr = 0x00000000, tmppmem = 0x00000000, tmppatt = 0x00000000;
  135. tmppcr = (uint32_t)FSMC_NANDInitStruct->FSMC_Waitfeature |
  136. PCR_MemoryType_NAND |
  137. FSMC_NANDInitStruct->FSMC_MemoryDataWidth |
  138. FSMC_NANDInitStruct->FSMC_ECC |
  139. FSMC_NANDInitStruct->FSMC_ECCPageSize |
  140. (FSMC_NANDInitStruct->FSMC_TCLRSetupTime << 9) |
  141. (FSMC_NANDInitStruct->FSMC_TARSetupTime << 13);
  142. tmppmem = (uint32_t)FSMC_NANDInitStruct->FSMC_CommonSpaceTimingStruct->FSMC_SetupTime |
  143. (FSMC_NANDInitStruct->FSMC_CommonSpaceTimingStruct->FSMC_WaitSetupTime << 8) |
  144. (FSMC_NANDInitStruct->FSMC_CommonSpaceTimingStruct->FSMC_HoldSetupTime << 16) |
  145. (FSMC_NANDInitStruct->FSMC_CommonSpaceTimingStruct->FSMC_HiZSetupTime << 24);
  146. tmppatt = (uint32_t)FSMC_NANDInitStruct->FSMC_AttributeSpaceTimingStruct->FSMC_SetupTime |
  147. (FSMC_NANDInitStruct->FSMC_AttributeSpaceTimingStruct->FSMC_WaitSetupTime << 8) |
  148. (FSMC_NANDInitStruct->FSMC_AttributeSpaceTimingStruct->FSMC_HoldSetupTime << 16) |
  149. (FSMC_NANDInitStruct->FSMC_AttributeSpaceTimingStruct->FSMC_HiZSetupTime << 24);
  150. if(FSMC_NANDInitStruct->FSMC_Bank == FSMC_Bank2_NAND)
  151. {
  152. FSMC_Bank2->PCR2 = tmppcr;
  153. FSMC_Bank2->PMEM2 = tmppmem;
  154. FSMC_Bank2->PATT2 = tmppatt;
  155. }
  156. }
  157. /*********************************************************************
  158. * @fn FSMC_NORSRAMStructInit
  159. *
  160. * @brief Fills each FSMC_NORSRAMInitStruct member with its default value.
  161. *
  162. * @param FSMC_NORSRAMInitStruct - pointer to a FSMC_NORSRAMInitTypeDef
  163. * structure which will be initialized.
  164. *
  165. * @return none
  166. */
  167. void FSMC_NORSRAMStructInit(FSMC_NORSRAMInitTypeDef *FSMC_NORSRAMInitStruct)
  168. {
  169. FSMC_NORSRAMInitStruct->FSMC_Bank = FSMC_Bank1_NORSRAM1;
  170. FSMC_NORSRAMInitStruct->FSMC_DataAddressMux = FSMC_DataAddressMux_Enable;
  171. FSMC_NORSRAMInitStruct->FSMC_MemoryType = FSMC_MemoryType_SRAM;
  172. FSMC_NORSRAMInitStruct->FSMC_MemoryDataWidth = FSMC_MemoryDataWidth_8b;
  173. FSMC_NORSRAMInitStruct->FSMC_BurstAccessMode = FSMC_BurstAccessMode_Disable;
  174. FSMC_NORSRAMInitStruct->FSMC_AsynchronousWait = FSMC_AsynchronousWait_Disable;
  175. FSMC_NORSRAMInitStruct->FSMC_WaitSignalPolarity = FSMC_WaitSignalPolarity_Low;
  176. FSMC_NORSRAMInitStruct->FSMC_WaitSignalActive = FSMC_WaitSignalActive_BeforeWaitState;
  177. FSMC_NORSRAMInitStruct->FSMC_WriteOperation = FSMC_WriteOperation_Enable;
  178. FSMC_NORSRAMInitStruct->FSMC_WaitSignal = FSMC_WaitSignal_Enable;
  179. FSMC_NORSRAMInitStruct->FSMC_ExtendedMode = FSMC_ExtendedMode_Disable;
  180. FSMC_NORSRAMInitStruct->FSMC_WriteBurst = FSMC_WriteBurst_Disable;
  181. FSMC_NORSRAMInitStruct->FSMC_ReadWriteTimingStruct->FSMC_AddressSetupTime = 0xF;
  182. FSMC_NORSRAMInitStruct->FSMC_ReadWriteTimingStruct->FSMC_AddressHoldTime = 0xF;
  183. FSMC_NORSRAMInitStruct->FSMC_ReadWriteTimingStruct->FSMC_DataSetupTime = 0xFF;
  184. FSMC_NORSRAMInitStruct->FSMC_ReadWriteTimingStruct->FSMC_BusTurnAroundDuration = 0xF;
  185. FSMC_NORSRAMInitStruct->FSMC_ReadWriteTimingStruct->FSMC_CLKDivision = 0xF;
  186. FSMC_NORSRAMInitStruct->FSMC_ReadWriteTimingStruct->FSMC_DataLatency = 0xF;
  187. FSMC_NORSRAMInitStruct->FSMC_ReadWriteTimingStruct->FSMC_AccessMode = FSMC_AccessMode_A;
  188. FSMC_NORSRAMInitStruct->FSMC_WriteTimingStruct->FSMC_AddressSetupTime = 0xF;
  189. FSMC_NORSRAMInitStruct->FSMC_WriteTimingStruct->FSMC_AddressHoldTime = 0xF;
  190. FSMC_NORSRAMInitStruct->FSMC_WriteTimingStruct->FSMC_DataSetupTime = 0xFF;
  191. FSMC_NORSRAMInitStruct->FSMC_WriteTimingStruct->FSMC_BusTurnAroundDuration = 0xF;
  192. FSMC_NORSRAMInitStruct->FSMC_WriteTimingStruct->FSMC_AccessMode = FSMC_AccessMode_A;
  193. }
  194. /*********************************************************************
  195. * @fn FSMC_NANDStructInit
  196. *
  197. * @brief Fills each FSMC_NANDInitStruct member with its default value.
  198. *
  199. * @param FSMC_NANDInitStruct - pointer to a FSMC_NANDInitTypeDef
  200. * structure which will be initialized.
  201. *
  202. * @return none
  203. */
  204. void FSMC_NANDStructInit(FSMC_NANDInitTypeDef *FSMC_NANDInitStruct)
  205. {
  206. FSMC_NANDInitStruct->FSMC_Bank = FSMC_Bank2_NAND;
  207. FSMC_NANDInitStruct->FSMC_Waitfeature = FSMC_Waitfeature_Disable;
  208. FSMC_NANDInitStruct->FSMC_MemoryDataWidth = FSMC_MemoryDataWidth_8b;
  209. FSMC_NANDInitStruct->FSMC_ECC = FSMC_ECC_Disable;
  210. FSMC_NANDInitStruct->FSMC_ECCPageSize = FSMC_ECCPageSize_256Bytes;
  211. FSMC_NANDInitStruct->FSMC_TCLRSetupTime = 0x0;
  212. FSMC_NANDInitStruct->FSMC_TARSetupTime = 0x0;
  213. FSMC_NANDInitStruct->FSMC_CommonSpaceTimingStruct->FSMC_SetupTime = 0xFC;
  214. FSMC_NANDInitStruct->FSMC_CommonSpaceTimingStruct->FSMC_WaitSetupTime = 0xFC;
  215. FSMC_NANDInitStruct->FSMC_CommonSpaceTimingStruct->FSMC_HoldSetupTime = 0xFC;
  216. FSMC_NANDInitStruct->FSMC_CommonSpaceTimingStruct->FSMC_HiZSetupTime = 0xFC;
  217. FSMC_NANDInitStruct->FSMC_AttributeSpaceTimingStruct->FSMC_SetupTime = 0xFC;
  218. FSMC_NANDInitStruct->FSMC_AttributeSpaceTimingStruct->FSMC_WaitSetupTime = 0xFC;
  219. FSMC_NANDInitStruct->FSMC_AttributeSpaceTimingStruct->FSMC_HoldSetupTime = 0xFC;
  220. FSMC_NANDInitStruct->FSMC_AttributeSpaceTimingStruct->FSMC_HiZSetupTime = 0xFC;
  221. }
  222. /*********************************************************************
  223. * @fn FSMC_NORSRAMCmd
  224. *
  225. * @brief Enables or disables the specified NOR/SRAM Memory Bank.
  226. *
  227. * @param FSMC_Bank - specifies the FSMC Bank to be used
  228. * FSMC_Bank1_NORSRAM1 - FSMC Bank1 NOR/SRAM1
  229. * FSMC_Bank1_NORSRAM2 - FSMC Bank1 NOR/SRAM2
  230. * FSMC_Bank1_NORSRAM3 - FSMC Bank1 NOR/SRAM3
  231. * FSMC_Bank1_NORSRAM4 - FSMC Bank1 NOR/SRAM4
  232. * NewState:ENABLE or DISABLE.
  233. *
  234. * @return none
  235. */
  236. void FSMC_NORSRAMCmd(uint32_t FSMC_Bank, FunctionalState NewState)
  237. {
  238. if(NewState != DISABLE)
  239. {
  240. FSMC_Bank1->BTCR[FSMC_Bank] |= BCR_MBKEN_Set;
  241. }
  242. else
  243. {
  244. FSMC_Bank1->BTCR[FSMC_Bank] &= BCR_MBKEN_Reset;
  245. }
  246. }
  247. /*********************************************************************
  248. * @fn FSMC_NANDCmd
  249. *
  250. * @brief Enables or disables the specified NAND Memory Bank.
  251. *
  252. * @param FSMC_Bank - specifies the FSMC Bank to be used
  253. * FSMC_Bank2_NAND - FSMC Bank2 NAND
  254. * NewStat - ENABLE or DISABLE.
  255. *
  256. * @return none
  257. */
  258. void FSMC_NANDCmd(uint32_t FSMC_Bank, FunctionalState NewState)
  259. {
  260. if(NewState != DISABLE)
  261. {
  262. if(FSMC_Bank == FSMC_Bank2_NAND)
  263. {
  264. FSMC_Bank2->PCR2 |= PCR_PBKEN_Set;
  265. }
  266. }
  267. else
  268. {
  269. if(FSMC_Bank == FSMC_Bank2_NAND)
  270. {
  271. FSMC_Bank2->PCR2 &= PCR_PBKEN_Reset;
  272. }
  273. }
  274. }
  275. /*********************************************************************
  276. * @fn FSMC_NANDECCCmd
  277. *
  278. * @brief Enables or disables the FSMC NAND ECC feature.
  279. *
  280. * @param FSMC_Bank - specifies the FSMC Bank to be used
  281. * FSMC_Bank2_NAND - FSMC Bank2 NAND
  282. * NewState - ENABLE or DISABLE.
  283. *
  284. * @return none
  285. */
  286. void FSMC_NANDECCCmd(uint32_t FSMC_Bank, FunctionalState NewState)
  287. {
  288. if(NewState != DISABLE)
  289. {
  290. if(FSMC_Bank == FSMC_Bank2_NAND)
  291. {
  292. FSMC_Bank2->PCR2 |= PCR_ECCEN_Set;
  293. }
  294. }
  295. else
  296. {
  297. if(FSMC_Bank == FSMC_Bank2_NAND)
  298. {
  299. FSMC_Bank2->PCR2 &= PCR_ECCEN_Reset;
  300. }
  301. }
  302. }
  303. /*********************************************************************
  304. * @fn FSMC_GetECC
  305. *
  306. * @brief Returns the error correction code register value.
  307. *
  308. * @param FSMC_Bank - specifies the FSMC Bank to be used
  309. * FSMC_Bank2_NAND - FSMC Bank2 NAND
  310. * NewState - ENABLE or DISABLE.
  311. *
  312. * @return eccval - The Error Correction Code (ECC) value.
  313. */
  314. uint32_t FSMC_GetECC(uint32_t FSMC_Bank)
  315. {
  316. uint32_t eccval = 0x00000000;
  317. if(FSMC_Bank == FSMC_Bank2_NAND)
  318. {
  319. eccval = FSMC_Bank2->ECCR2;
  320. }
  321. return (eccval);
  322. }
  323. /*********************************************************************
  324. * @fn FSMC_GetFlagStatus
  325. *
  326. * @brief Checks whether the specified FSMC flag is set or not.
  327. *
  328. * @param FSMC_Bank - specifies the FSMC Bank to be used
  329. * FSMC_Bank2_NAND - FSMC Bank2 NAND
  330. * FSMC_FLAG - specifies the flag to check.
  331. * FSMC_FLAG_FEMPT - Fifo empty Flag.
  332. * NewState - ENABLE or DISABLE.
  333. *
  334. * @return FlagStatus - The new state of FSMC_FLAG (SET or RESET).
  335. */
  336. FlagStatus FSMC_GetFlagStatus(uint32_t FSMC_Bank, uint32_t FSMC_FLAG)
  337. {
  338. FlagStatus bitstatus = RESET;
  339. uint32_t tmpsr = 0x00000000;
  340. if(FSMC_Bank == FSMC_Bank2_NAND)
  341. {
  342. tmpsr = FSMC_Bank2->SR2;
  343. }
  344. if((tmpsr & FSMC_FLAG) != (uint16_t)RESET)
  345. {
  346. bitstatus = SET;
  347. }
  348. else
  349. {
  350. bitstatus = RESET;
  351. }
  352. return bitstatus;
  353. }