gd32f10x_exmc.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645
  1. /*!
  2. \file gd32f10x_exmc.c
  3. \brief EXMC driver
  4. \version 2014-12-26, V1.0.0, firmware for GD32F10x
  5. \version 2017-06-20, V2.0.0, firmware for GD32F10x
  6. \version 2018-07-31, V2.1.0, firmware for GD32F10x
  7. \version 2020-09-30, V2.2.0, firmware for GD32F10x
  8. */
  9. /*
  10. Copyright (c) 2020, GigaDevice Semiconductor Inc.
  11. Redistribution and use in source and binary forms, with or without modification,
  12. are permitted provided that the following conditions are met:
  13. 1. Redistributions of source code must retain the above copyright notice, this
  14. list of conditions and the following disclaimer.
  15. 2. Redistributions in binary form must reproduce the above copyright notice,
  16. this list of conditions and the following disclaimer in the documentation
  17. and/or other materials provided with the distribution.
  18. 3. Neither the name of the copyright holder nor the names of its contributors
  19. may be used to endorse or promote products derived from this software without
  20. specific prior written permission.
  21. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  22. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  23. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  24. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  25. INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  26. NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  27. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  28. WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  29. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  30. OF SUCH DAMAGE.
  31. */
  32. #include "gd32f10x_exmc.h"
  33. /* EXMC bank0 register reset value */
  34. #define BANK0_SNCTL0_REGION_RESET ((uint32_t)0x000030DBU)
  35. #define BANK0_SNCTL1_2_3_REGION_RESET ((uint32_t)0x000030D2U)
  36. #define BANK0_SNTCFG_RESET ((uint32_t)0x0FFFFFFFU)
  37. #define BANK0_SNWTCFG_RESET ((uint32_t)0x0FFFFFFFU)
  38. /* EXMC bank1/2 register reset mask*/
  39. #define BANK1_2_NPCTL_RESET ((uint32_t)0x00000018U)
  40. #define BANK1_2_NPINTEN_RESET ((uint32_t)0x00000040U)
  41. #define BANK1_2_NPCTCFG_RESET ((uint32_t)0xFCFCFCFCU)
  42. #define BANK1_2_NPATCFG_RESET ((uint32_t)0xFCFCFCFCU)
  43. /* EXMC bank3 register reset mask*/
  44. #define BANK3_NPCTL_RESET ((uint32_t)0x00000018U)
  45. #define BANK3_NPINTEN_RESET ((uint32_t)0x00000040U)
  46. #define BANK3_NPCTCFG_RESET ((uint32_t)0xFCFCFCFCU)
  47. #define BANK3_NPATCFG_RESET ((uint32_t)0xFCFCFCFCU)
  48. #define BANK3_PIOTCFG3_RESET ((uint32_t)0xFCFCFCFCU)
  49. /* EXMC register bit offset */
  50. #define SNCTL_NRMUX_OFFSET ((uint32_t)1U)
  51. #define SNCTL_SBRSTEN_OFFSET ((uint32_t)8U)
  52. #define SNCTL_WRAPEN_OFFSET ((uint32_t)10U)
  53. #define SNCTL_WREN_OFFSET ((uint32_t)12U)
  54. #define SNCTL_NRWTEN_OFFSET ((uint32_t)13U)
  55. #define SNCTL_EXMODEN_OFFSET ((uint32_t)14U)
  56. #define SNCTL_ASYNCWAIT_OFFSET ((uint32_t)15U)
  57. #define SNTCFG_AHLD_OFFSET ((uint32_t)4U)
  58. #define SNTCFG_DSET_OFFSET ((uint32_t)8U)
  59. #define SNTCFG_BUSLAT_OFFSET ((uint32_t)16U)
  60. #define SNWTCFG_WAHLD_OFFSET ((uint32_t)4U)
  61. #define SNWTCFG_WDSET_OFFSET ((uint32_t)8U)
  62. #define SNWTCFG_WBUSLAT_OFFSET ((uint32_t)16U)
  63. #define NPCTL_NDWTEN_OFFSET ((uint32_t)1U)
  64. #define NPCTL_ECCEN_OFFSET ((uint32_t)6U)
  65. #define NPCTCFG_COMWAIT_OFFSET ((uint32_t)8U)
  66. #define NPCTCFG_COMHLD_OFFSET ((uint32_t)16U)
  67. #define NPCTCFG_COMHIZ_OFFSET ((uint32_t)24U)
  68. #define NPATCFG_ATTWAIT_OFFSET ((uint32_t)8U)
  69. #define NPATCFG_ATTHLD_OFFSET ((uint32_t)16U)
  70. #define NPATCFG_ATTHIZ_OFFSET ((uint32_t)24U)
  71. #define PIOTCFG_IOWAIT_OFFSET ((uint32_t)8U)
  72. #define PIOTCFG_IOHLD_OFFSET ((uint32_t)16U)
  73. #define PIOTCFG_IOHIZ_OFFSET ((uint32_t)24U)
  74. #define INTEN_INTS_OFFSET ((uint32_t)3U)
  75. /*!
  76. \brief deinitialize EXMC NOR/SRAM region
  77. \param[in] norsram_region: select the region of bank0
  78. \arg EXMC_BANK0_NORSRAM_REGIONx(x=0..3)
  79. \param[out] none
  80. \retval none
  81. */
  82. void exmc_norsram_deinit(uint32_t norsram_region)
  83. {
  84. /* reset the registers */
  85. if(EXMC_BANK0_NORSRAM_REGION0 == norsram_region){
  86. EXMC_SNCTL(norsram_region) = BANK0_SNCTL0_REGION_RESET;
  87. }else{
  88. EXMC_SNCTL(norsram_region) = BANK0_SNCTL1_2_3_REGION_RESET;
  89. }
  90. EXMC_SNTCFG(norsram_region) = BANK0_SNTCFG_RESET;
  91. EXMC_SNWTCFG(norsram_region) = BANK0_SNWTCFG_RESET;
  92. }
  93. /*!
  94. \brief initialize EXMC NOR/SRAM region
  95. \param[in] exmc_norsram_parameter_struct: configure the EXMC NOR/SRAM parameter
  96. norsram_region: EXMC_BANK0_NORSRAM_REGIONx,x=0..3
  97. write_mode: EXMC_ASYN_WRITE,EXMC_SYN_WRITE
  98. extended_mode: ENABLE or DISABLE
  99. asyn_wait: ENABLE or DISABLE
  100. nwait_signal: ENABLE or DISABLE
  101. memory_write: ENABLE or DISABLE
  102. nwait_config: EXMC_NWAIT_CONFIG_BEFORE,EXMC_NWAIT_CONFIG_DURING
  103. wrap_burst_mode: ENABLE or DISABLE
  104. nwait_polarity: EXMC_NWAIT_POLARITY_LOW,EXMC_NWAIT_POLARITY_HIGH
  105. burst_mode: ENABLE or DISABLE
  106. databus_width: EXMC_NOR_DATABUS_WIDTH_8B,EXMC_NOR_DATABUS_WIDTH_16B
  107. memory_type: EXMC_MEMORY_TYPE_SRAM,EXMC_MEMORY_TYPE_PSRAM,EXMC_MEMORY_TYPE_NOR
  108. address_data_mux: ENABLE or DISABLE
  109. read_write_timing: struct exmc_norsram_timing_parameter_struct set the time
  110. write_timing: struct exmc_norsram_timing_parameter_struct set the time
  111. \param[out] none
  112. \retval none
  113. */
  114. void exmc_norsram_init(exmc_norsram_parameter_struct* exmc_norsram_init_struct)
  115. {
  116. uint32_t snctl = 0x00000000U, sntcfg = 0x00000000U, snwtcfg = 0x00000000U;
  117. /* get the register value */
  118. snctl = EXMC_SNCTL(exmc_norsram_init_struct->norsram_region);
  119. /* clear relative bits */
  120. snctl &= ((uint32_t)~(EXMC_SNCTL_NREN | EXMC_SNCTL_NRTP | EXMC_SNCTL_NRW | EXMC_SNCTL_SBRSTEN |
  121. EXMC_SNCTL_NRWTPOL | EXMC_SNCTL_WRAPEN | EXMC_SNCTL_NRWTCFG | EXMC_SNCTL_WREN |
  122. EXMC_SNCTL_NRWTEN | EXMC_SNCTL_EXMODEN | EXMC_SNCTL_ASYNCWAIT | EXMC_SNCTL_SYNCWR |
  123. EXMC_SNCTL_NRMUX ));
  124. snctl |= (uint32_t)(exmc_norsram_init_struct->address_data_mux << SNCTL_NRMUX_OFFSET) |
  125. exmc_norsram_init_struct->memory_type |
  126. exmc_norsram_init_struct->databus_width |
  127. (exmc_norsram_init_struct->burst_mode << SNCTL_SBRSTEN_OFFSET) |
  128. exmc_norsram_init_struct->nwait_polarity |
  129. (exmc_norsram_init_struct->wrap_burst_mode << SNCTL_WRAPEN_OFFSET) |
  130. exmc_norsram_init_struct->nwait_config |
  131. (exmc_norsram_init_struct->memory_write << SNCTL_WREN_OFFSET) |
  132. (exmc_norsram_init_struct->nwait_signal << SNCTL_NRWTEN_OFFSET) |
  133. (exmc_norsram_init_struct->extended_mode << SNCTL_EXMODEN_OFFSET) |
  134. (exmc_norsram_init_struct->asyn_wait << SNCTL_ASYNCWAIT_OFFSET) |
  135. exmc_norsram_init_struct->write_mode;
  136. sntcfg = (uint32_t)((exmc_norsram_init_struct->read_write_timing->asyn_address_setuptime - 1U ) & EXMC_SNTCFG_ASET )|
  137. (((exmc_norsram_init_struct->read_write_timing->asyn_address_holdtime - 1U ) << SNTCFG_AHLD_OFFSET ) & EXMC_SNTCFG_AHLD ) |
  138. (((exmc_norsram_init_struct->read_write_timing->asyn_data_setuptime - 1U ) << SNTCFG_DSET_OFFSET ) & EXMC_SNTCFG_DSET ) |
  139. (((exmc_norsram_init_struct->read_write_timing->bus_latency - 1U ) << SNTCFG_BUSLAT_OFFSET ) & EXMC_SNTCFG_BUSLAT )|
  140. exmc_norsram_init_struct->read_write_timing->syn_clk_division |
  141. exmc_norsram_init_struct->read_write_timing->syn_data_latency |
  142. exmc_norsram_init_struct->read_write_timing->asyn_access_mode;
  143. /* nor flash access enable */
  144. if(EXMC_MEMORY_TYPE_NOR == exmc_norsram_init_struct->memory_type){
  145. snctl |= (uint32_t)EXMC_SNCTL_NREN;
  146. }
  147. /* extended mode configure */
  148. if(ENABLE == exmc_norsram_init_struct->extended_mode){
  149. snwtcfg = (uint32_t)(((exmc_norsram_init_struct->write_timing->asyn_address_setuptime - 1U) & EXMC_SNWTCFG_WASET) |
  150. (((exmc_norsram_init_struct->write_timing->asyn_address_holdtime - 1U) << SNTCFG_AHLD_OFFSET ) & EXMC_SNWTCFG_WAHLD)|
  151. (((exmc_norsram_init_struct->write_timing->asyn_data_setuptime - 1U) << SNTCFG_DSET_OFFSET) & EXMC_SNWTCFG_WDSET) |
  152. exmc_norsram_init_struct->write_timing->asyn_access_mode);
  153. }else{
  154. snwtcfg = BANK0_SNWTCFG_RESET;
  155. }
  156. /* configure the registers */
  157. EXMC_SNCTL(exmc_norsram_init_struct->norsram_region) = snctl;
  158. EXMC_SNTCFG(exmc_norsram_init_struct->norsram_region) = sntcfg;
  159. EXMC_SNWTCFG(exmc_norsram_init_struct->norsram_region) = snwtcfg;
  160. }
  161. /*!
  162. \brief initialize the struct exmc_norsram_parameter_struct
  163. \param[in] none
  164. \param[out] exmc_norsram_init_struct: the initialized struct exmc_norsram_parameter_struct pointer
  165. \retval none
  166. */
  167. void exmc_norsram_struct_para_init(exmc_norsram_parameter_struct* exmc_norsram_init_struct)
  168. {
  169. /* configure the structure with default value */
  170. exmc_norsram_init_struct->norsram_region = EXMC_BANK0_NORSRAM_REGION0;
  171. exmc_norsram_init_struct->address_data_mux = ENABLE;
  172. exmc_norsram_init_struct->memory_type = EXMC_MEMORY_TYPE_SRAM;
  173. exmc_norsram_init_struct->databus_width = EXMC_NOR_DATABUS_WIDTH_8B;
  174. exmc_norsram_init_struct->burst_mode = DISABLE;
  175. exmc_norsram_init_struct->nwait_polarity = EXMC_NWAIT_POLARITY_LOW;
  176. exmc_norsram_init_struct->wrap_burst_mode = DISABLE;
  177. exmc_norsram_init_struct->nwait_config = EXMC_NWAIT_CONFIG_BEFORE;
  178. exmc_norsram_init_struct->memory_write = ENABLE;
  179. exmc_norsram_init_struct->nwait_signal = ENABLE;
  180. exmc_norsram_init_struct->extended_mode = DISABLE;
  181. exmc_norsram_init_struct->asyn_wait = DISABLE;
  182. exmc_norsram_init_struct->write_mode = EXMC_ASYN_WRITE;
  183. /* read/write timing configure */
  184. exmc_norsram_init_struct->read_write_timing->asyn_address_setuptime = 0xFU;
  185. exmc_norsram_init_struct->read_write_timing->asyn_address_holdtime = 0xFU;
  186. exmc_norsram_init_struct->read_write_timing->asyn_data_setuptime = 0xFFU;
  187. exmc_norsram_init_struct->read_write_timing->bus_latency = 0xFU;
  188. exmc_norsram_init_struct->read_write_timing->syn_clk_division = EXMC_SYN_CLOCK_RATIO_16_CLK;
  189. exmc_norsram_init_struct->read_write_timing->syn_data_latency = EXMC_DATALAT_17_CLK;
  190. exmc_norsram_init_struct->read_write_timing->asyn_access_mode = EXMC_ACCESS_MODE_A;
  191. /* write timing configure, when extended mode is used */
  192. exmc_norsram_init_struct->write_timing->asyn_address_setuptime = 0xFU;
  193. exmc_norsram_init_struct->write_timing->asyn_address_holdtime = 0xFU;
  194. exmc_norsram_init_struct->write_timing->asyn_data_setuptime = 0xFFU;
  195. exmc_norsram_init_struct->write_timing->bus_latency = 0xFU;
  196. exmc_norsram_init_struct->write_timing->asyn_access_mode = EXMC_ACCESS_MODE_A;
  197. }
  198. /*!
  199. \brief enable EXMC NOR/PSRAM bank region
  200. \param[in] norsram_region: specifie the region of NOR/PSRAM bank
  201. \arg EXMC_BANK0_NORSRAM_REGIONx(x=0..3)
  202. \param[out] none
  203. \retval none
  204. */
  205. void exmc_norsram_enable(uint32_t norsram_region)
  206. {
  207. EXMC_SNCTL(norsram_region) |= (uint32_t)EXMC_SNCTL_NRBKEN;
  208. }
  209. /*!
  210. \brief disable EXMC NOR/PSRAM bank region
  211. \param[in] norsram_region: specifie the region of NOR/PSRAM Bank
  212. \arg EXMC_BANK0_NORSRAM_REGIONx(x=0..3)
  213. \param[out] none
  214. \retval none
  215. */
  216. void exmc_norsram_disable(uint32_t norsram_region)
  217. {
  218. EXMC_SNCTL(norsram_region) &= ~(uint32_t)EXMC_SNCTL_NRBKEN;
  219. }
  220. /*!
  221. \brief deinitialize EXMC NAND bank
  222. \param[in] nand_bank: select the bank of NAND
  223. \arg EXMC_BANKx_NAND(x=1..2)
  224. \param[out] none
  225. \retval none
  226. */
  227. void exmc_nand_deinit(uint32_t nand_bank)
  228. {
  229. /* EXMC_BANK1_NAND or EXMC_BANK2_NAND */
  230. EXMC_NPCTL(nand_bank) = BANK1_2_NPCTL_RESET;
  231. EXMC_NPINTEN(nand_bank) = BANK1_2_NPINTEN_RESET;
  232. EXMC_NPCTCFG(nand_bank) = BANK1_2_NPCTCFG_RESET;
  233. EXMC_NPATCFG(nand_bank) = BANK1_2_NPATCFG_RESET;
  234. }
  235. /*!
  236. \brief initialize EXMC NAND bank
  237. \param[in] exmc_nand_parameter_struct: configure the EXMC NAND parameter
  238. nand_bank: EXMC_BANK1_NAND,EXMC_BANK2_NAND
  239. ecc_size: EXMC_ECC_SIZE_xBYTES,x=256,512,1024,2048,4096
  240. atr_latency: EXMC_ALE_RE_DELAY_x_HCLK,x=1..16
  241. ctr_latency: EXMC_CLE_RE_DELAY_x_HCLK,x=1..16
  242. ecc_logic: ENABLE or DISABLE
  243. databus_width: EXMC_NAND_DATABUS_WIDTH_8B,EXMC_NAND_DATABUS_WIDTH_16B
  244. wait_feature: ENABLE or DISABLE
  245. common_space_timing: struct exmc_nand_pccard_timing_parameter_struct set the time
  246. attribute_space_timing: struct exmc_nand_pccard_timing_parameter_struct set the time
  247. \param[out] none
  248. \retval none
  249. */
  250. void exmc_nand_init(exmc_nand_parameter_struct* exmc_nand_init_struct)
  251. {
  252. uint32_t npctl = 0x00000000U, npctcfg = 0x00000000U, npatcfg = 0x00000000U;
  253. npctl = (uint32_t)(exmc_nand_init_struct->wait_feature << NPCTL_NDWTEN_OFFSET)|
  254. EXMC_NPCTL_NDTP |
  255. exmc_nand_init_struct->databus_width |
  256. (exmc_nand_init_struct->ecc_logic << NPCTL_ECCEN_OFFSET)|
  257. exmc_nand_init_struct->ecc_size |
  258. exmc_nand_init_struct->ctr_latency |
  259. exmc_nand_init_struct->atr_latency;
  260. npctcfg = (uint32_t)((exmc_nand_init_struct->common_space_timing->setuptime - 1U) & EXMC_NPCTCFG_COMSET ) |
  261. (((exmc_nand_init_struct->common_space_timing->waittime - 1U) << NPCTCFG_COMWAIT_OFFSET) & EXMC_NPCTCFG_COMWAIT ) |
  262. ((exmc_nand_init_struct->common_space_timing->holdtime << NPCTCFG_COMHLD_OFFSET) & EXMC_NPCTCFG_COMHLD ) |
  263. (((exmc_nand_init_struct->common_space_timing->databus_hiztime - 1U) << NPCTCFG_COMHIZ_OFFSET) & EXMC_NPCTCFG_COMHIZ );
  264. npatcfg = (uint32_t)((exmc_nand_init_struct->attribute_space_timing->setuptime - 1U) & EXMC_NPATCFG_ATTSET ) |
  265. (((exmc_nand_init_struct->attribute_space_timing->waittime - 1U) << NPATCFG_ATTWAIT_OFFSET) & EXMC_NPATCFG_ATTWAIT ) |
  266. ((exmc_nand_init_struct->attribute_space_timing->holdtime << NPATCFG_ATTHLD_OFFSET) & EXMC_NPATCFG_ATTHLD ) |
  267. (((exmc_nand_init_struct->attribute_space_timing->databus_hiztime -1U) << NPATCFG_ATTHIZ_OFFSET) & EXMC_NPATCFG_ATTHIZ );
  268. /* EXMC_BANK1_NAND or EXMC_BANK2_NAND initialize */
  269. EXMC_NPCTL(exmc_nand_init_struct->nand_bank) = npctl;
  270. EXMC_NPCTCFG(exmc_nand_init_struct->nand_bank) = npctcfg;
  271. EXMC_NPATCFG(exmc_nand_init_struct->nand_bank) = npatcfg;
  272. }
  273. /*!
  274. \brief initialize the struct exmc_nand_init_struct
  275. \param[in] none
  276. \param[out] the initialized struct exmc_nand_init_struct pointer
  277. \retval none
  278. */
  279. void exmc_nand_struct_para_init(exmc_nand_parameter_struct* exmc_nand_init_struct)
  280. {
  281. /* configure the structure with default value */
  282. exmc_nand_init_struct->nand_bank = EXMC_BANK1_NAND;
  283. exmc_nand_init_struct->wait_feature = DISABLE;
  284. exmc_nand_init_struct->databus_width = EXMC_NAND_DATABUS_WIDTH_8B;
  285. exmc_nand_init_struct->ecc_logic = DISABLE;
  286. exmc_nand_init_struct->ecc_size = EXMC_ECC_SIZE_256BYTES;
  287. exmc_nand_init_struct->ctr_latency = 0x0U;
  288. exmc_nand_init_struct->atr_latency = 0x0U;
  289. exmc_nand_init_struct->common_space_timing->setuptime = 0xFCU;
  290. exmc_nand_init_struct->common_space_timing->waittime = 0xFCU;
  291. exmc_nand_init_struct->common_space_timing->holdtime = 0xFCU;
  292. exmc_nand_init_struct->common_space_timing->databus_hiztime = 0xFCU;
  293. exmc_nand_init_struct->attribute_space_timing->setuptime = 0xFCU;
  294. exmc_nand_init_struct->attribute_space_timing->waittime = 0xFCU;
  295. exmc_nand_init_struct->attribute_space_timing->holdtime = 0xFCU;
  296. exmc_nand_init_struct->attribute_space_timing->databus_hiztime = 0xFCU;
  297. }
  298. /*!
  299. \brief enable NAND bank
  300. \param[in] nand_bank: specifie the NAND bank
  301. \arg EXMC_BANKx_NAND(x=1,2)
  302. \param[out] none
  303. \retval none
  304. */
  305. void exmc_nand_enable(uint32_t nand_bank)
  306. {
  307. EXMC_NPCTL(nand_bank) |= EXMC_NPCTL_NDBKEN;
  308. }
  309. /*!
  310. \brief disable NAND bank
  311. \param[in] nand_bank: specifie the NAND bank
  312. \arg EXMC_BANKx_NAND(x=1,2)
  313. \param[out] none
  314. \retval none
  315. */
  316. void exmc_nand_disable(uint32_t nand_bank)
  317. {
  318. EXMC_NPCTL(nand_bank) &= ~EXMC_NPCTL_NDBKEN;
  319. }
  320. /*!
  321. \brief enable or disable the EXMC NAND ECC function
  322. \param[in] nand_bank: specifie the NAND bank
  323. \arg EXMC_BANKx_NAND(x=1,2)
  324. \param[in] newvalue: ENABLE or DISABLE
  325. \param[out] none
  326. \retval none
  327. */
  328. void exmc_nand_ecc_config(uint32_t nand_bank, ControlStatus newvalue)
  329. {
  330. if (ENABLE == newvalue){
  331. /* enable the selected NAND bank ECC function */
  332. EXMC_NPCTL(nand_bank) |= EXMC_NPCTL_ECCEN;
  333. }else{
  334. /* disable the selected NAND bank ECC function */
  335. EXMC_NPCTL(nand_bank) &= ~EXMC_NPCTL_ECCEN;
  336. }
  337. }
  338. /*!
  339. \brief get the EXMC ECC value
  340. \param[in] nand_bank: specifie the NAND bank
  341. \arg EXMC_BANKx_NAND(x=1,2)
  342. \param[out] none
  343. \retval the error correction code(ECC) value
  344. */
  345. uint32_t exmc_ecc_get(uint32_t nand_bank)
  346. {
  347. return (EXMC_NECC(nand_bank));
  348. }
  349. /*!
  350. \brief deinitialize EXMC PC card bank
  351. \param[in] none
  352. \param[out] none
  353. \retval none
  354. */
  355. void exmc_pccard_deinit(void)
  356. {
  357. /* EXMC_BANK3_PCCARD */
  358. EXMC_NPCTL3 = BANK3_NPCTL_RESET;
  359. EXMC_NPINTEN3 = BANK3_NPINTEN_RESET;
  360. EXMC_NPCTCFG3 = BANK3_NPCTCFG_RESET;
  361. EXMC_NPATCFG3 = BANK3_NPATCFG_RESET;
  362. EXMC_PIOTCFG3 = BANK3_PIOTCFG3_RESET;
  363. }
  364. /*!
  365. \brief initialize EXMC PC card bank
  366. \param[in] exmc_pccard_parameter_struct: configure the EXMC NAND parameter
  367. atr_latency: EXMC_ALE_RE_DELAY_x_HCLK,x=1..16
  368. ctr_latency: EXMC_CLE_RE_DELAY_x_HCLK,x=1..16
  369. wait_feature: ENABLE or DISABLE
  370. common_space_timing: struct exmc_nand_pccard_timing_parameter_struct set the time
  371. attribute_space_timing: struct exmc_nand_pccard_timing_parameter_struct set the time
  372. io_space_timing: exmc_nand_pccard_timing_parameter_struct set the time
  373. \param[out] none
  374. \retval none
  375. */
  376. void exmc_pccard_init(exmc_pccard_parameter_struct* exmc_pccard_init_struct)
  377. {
  378. /* configure the EXMC bank3 PC card control register */
  379. EXMC_NPCTL3 = (uint32_t)(exmc_pccard_init_struct->wait_feature << NPCTL_NDWTEN_OFFSET) |
  380. EXMC_NAND_DATABUS_WIDTH_16B |
  381. exmc_pccard_init_struct->ctr_latency |
  382. exmc_pccard_init_struct->atr_latency ;
  383. /* configure the EXMC bank3 PC card common space timing configuration register */
  384. EXMC_NPCTCFG3 = (uint32_t)((exmc_pccard_init_struct->common_space_timing->setuptime - 1U)& EXMC_NPCTCFG_COMSET ) |
  385. (((exmc_pccard_init_struct->common_space_timing->waittime - 1U) << NPCTCFG_COMWAIT_OFFSET) & EXMC_NPCTCFG_COMWAIT ) |
  386. ((exmc_pccard_init_struct->common_space_timing->holdtime << NPCTCFG_COMHLD_OFFSET) & EXMC_NPCTCFG_COMHLD ) |
  387. (((exmc_pccard_init_struct->common_space_timing->databus_hiztime - 1U) << NPCTCFG_COMHIZ_OFFSET) & EXMC_NPCTCFG_COMHIZ );
  388. /* configure the EXMC bank3 PC card attribute space timing configuration register */
  389. EXMC_NPATCFG3 = (uint32_t)((exmc_pccard_init_struct->attribute_space_timing->setuptime - 1U) & EXMC_NPATCFG_ATTSET ) |
  390. (((exmc_pccard_init_struct->attribute_space_timing->waittime - 1U) << NPATCFG_ATTWAIT_OFFSET) & EXMC_NPATCFG_ATTWAIT ) |
  391. ((exmc_pccard_init_struct->attribute_space_timing->holdtime << NPATCFG_ATTHLD_OFFSET) & EXMC_NPATCFG_ATTHLD )|
  392. (((exmc_pccard_init_struct->attribute_space_timing->databus_hiztime -1U) << NPATCFG_ATTHIZ_OFFSET) & EXMC_NPATCFG_ATTHIZ );
  393. /* configure the EXMC bank3 PC card io space timing configuration register */
  394. EXMC_PIOTCFG3 = (uint32_t)((exmc_pccard_init_struct->io_space_timing->setuptime - 1U) & EXMC_PIOTCFG3_IOSET ) |
  395. (((exmc_pccard_init_struct->io_space_timing->waittime - 1U) << PIOTCFG_IOWAIT_OFFSET) & EXMC_PIOTCFG3_IOWAIT ) |
  396. ((exmc_pccard_init_struct->io_space_timing->holdtime << PIOTCFG_IOHLD_OFFSET) & EXMC_PIOTCFG3_IOHLD )|
  397. ((exmc_pccard_init_struct->io_space_timing->databus_hiztime << PIOTCFG_IOHIZ_OFFSET) & EXMC_PIOTCFG3_IOHIZ );
  398. }
  399. /*!
  400. \brief initialize the struct exmc_pccard_parameter_struct
  401. \param[in] none
  402. \param[out] the initialized struct exmc_pccard_parameter_struct pointer
  403. \retval none
  404. */
  405. void exmc_pccard_struct_para_init(exmc_pccard_parameter_struct* exmc_pccard_init_struct)
  406. {
  407. /* configure the structure with default value */
  408. exmc_pccard_init_struct->wait_feature = DISABLE;
  409. exmc_pccard_init_struct->ctr_latency = 0x0U;
  410. exmc_pccard_init_struct->atr_latency = 0x0U;
  411. exmc_pccard_init_struct->common_space_timing->setuptime = 0xFCU;
  412. exmc_pccard_init_struct->common_space_timing->waittime = 0xFCU;
  413. exmc_pccard_init_struct->common_space_timing->holdtime = 0xFCU;
  414. exmc_pccard_init_struct->common_space_timing->databus_hiztime = 0xFCU;
  415. exmc_pccard_init_struct->attribute_space_timing->setuptime = 0xFCU;
  416. exmc_pccard_init_struct->attribute_space_timing->waittime = 0xFCU;
  417. exmc_pccard_init_struct->attribute_space_timing->holdtime = 0xFCU;
  418. exmc_pccard_init_struct->attribute_space_timing->databus_hiztime = 0xFCU;
  419. exmc_pccard_init_struct->io_space_timing->setuptime = 0xFCU;
  420. exmc_pccard_init_struct->io_space_timing->waittime = 0xFCU;
  421. exmc_pccard_init_struct->io_space_timing->holdtime = 0xFCU;
  422. exmc_pccard_init_struct->io_space_timing->databus_hiztime = 0xFCU;
  423. }
  424. /*!
  425. \brief enable PC Card Bank
  426. \param[in] none
  427. \param[out] none
  428. \retval none
  429. */
  430. void exmc_pccard_enable(void)
  431. {
  432. EXMC_NPCTL3 |= EXMC_NPCTL_NDBKEN;
  433. }
  434. /*!
  435. \brief disable PC Card Bank
  436. \param[in] none
  437. \param[out] none
  438. \retval none
  439. */
  440. void exmc_pccard_disable(void)
  441. {
  442. EXMC_NPCTL3 &= ~EXMC_NPCTL_NDBKEN;
  443. }
  444. /*!
  445. \brief enable EXMC interrupt
  446. \param[in] bank: specifies the NAND bank, PC card bank
  447. only one parameter can be selected which is shown as below:
  448. \arg EXMC_BANK1_NAND: the NAND bank1
  449. \arg EXMC_BANK2_NAND: the NAND bank2
  450. \arg EXMC_BANK3_PCCARD: the PC card bank
  451. \param[in] interrupt_source: specify get which interrupt flag
  452. one or more parameters can be selected which is shown as below:
  453. \arg EXMC_NAND_PCCARD_INT_RISE: interrupt source of rising edge
  454. \arg EXMC_NAND_PCCARD_INT_LEVEL: interrupt source of high-level
  455. \arg EXMC_NAND_PCCARD_INT_FALL: interrupt source of falling edge
  456. \param[out] none
  457. \retval none
  458. */
  459. void exmc_interrupt_enable(uint32_t bank, uint32_t interrupt_source)
  460. {
  461. /* NAND bank1, bank2 or PC card bank3 */
  462. EXMC_NPINTEN(bank) |= interrupt_source;
  463. }
  464. /*!
  465. \brief disable EXMC interrupt
  466. \param[in] bank: specifies the NAND bank, PC card bank
  467. only one parameter can be selected which is shown as below:
  468. \arg EXMC_BANK1_NAND: the NAND bank1
  469. \arg EXMC_BANK2_NAND: the NAND bank2
  470. \arg EXMC_BANK3_PCCARD: the PC card bank
  471. \param[in] interrupt_source: specify get which interrupt flag
  472. one or more parameters can be selected which is shown as below:
  473. \arg EXMC_NAND_PCCARD_INT_RISE: interrupt source of rising edge
  474. \arg EXMC_NAND_PCCARD_INT_LEVEL: interrupt source of high-level
  475. \arg EXMC_NAND_PCCARD_INT_FALL: interrupt source of falling edge
  476. \param[out] none
  477. \retval none
  478. */
  479. void exmc_interrupt_disable(uint32_t bank, uint32_t interrupt_source)
  480. {
  481. /* NAND bank1,bank2 or PC card bank3 */
  482. EXMC_NPINTEN(bank) &= ~interrupt_source;
  483. }
  484. /*!
  485. \brief check EXMC flag is set or not
  486. \param[in] bank: specifies the NAND bank, PC card bank
  487. only one parameter can be selected which is shown as below:
  488. \arg EXMC_BANK1_NAND: the NAND bank1
  489. \arg EXMC_BANK2_NAND: the NAND bank2
  490. \arg EXMC_BANK3_PCCARD: the PC Card bank
  491. \param[in] flag: specify get which flag
  492. only one parameter can be selected which is shown as below:
  493. \arg EXMC_NAND_PCCARD_FLAG_RISE: interrupt rising edge status
  494. \arg EXMC_NAND_PCCARD_FLAG_LEVEL: interrupt high-level status
  495. \arg EXMC_NAND_PCCARD_FLAG_FALL: interrupt falling edge status
  496. \arg EXMC_NAND_PCCARD_FLAG_FIFOE: FIFO empty flag
  497. \param[out] none
  498. \retval FlagStatus: SET or RESET
  499. */
  500. FlagStatus exmc_flag_get(uint32_t bank, uint32_t flag)
  501. {
  502. uint32_t status = 0x00000000U;
  503. /* NAND bank1,bank2 or PC card bank3 */
  504. status = EXMC_NPINTEN(bank);
  505. if ((status & flag) != (uint32_t)flag ){
  506. /* flag is reset */
  507. return RESET;
  508. }else{
  509. /* flag is set */
  510. return SET;
  511. }
  512. }
  513. /*!
  514. \brief clear EXMC flag
  515. \param[in] bank: specifie the NAND bank, PCCARD bank
  516. only one parameter can be selected which is shown as below:
  517. \arg EXMC_BANK1_NAND: the NAND bank1
  518. \arg EXMC_BANK2_NAND: the NAND bank2
  519. \arg EXMC_BANK3_PCCARD: the PC card bank
  520. \param[in] flag: specify get which flag
  521. one or more parameters can be selected which is shown as below:
  522. \arg EXMC_NAND_PCCARD_FLAG_RISE: interrupt rising edge status
  523. \arg EXMC_NAND_PCCARD_FLAG_LEVEL: interrupt high-level status
  524. \arg EXMC_NAND_PCCARD_FLAG_FALL: interrupt falling edge status
  525. \arg EXMC_NAND_PCCARD_FLAG_FIFOE: FIFO empty flag
  526. \param[out] none
  527. \retval none
  528. */
  529. void exmc_flag_clear(uint32_t bank, uint32_t flag)
  530. {
  531. /* NAND bank1,bank2 or PC card bank3 */
  532. EXMC_NPINTEN(bank) &= ~flag;
  533. }
  534. /*!
  535. \brief check EXMC interrupt flag is set or not
  536. \param[in] bank: specifies the NAND bank, PC card bank
  537. only one parameter can be selected which is shown as below:
  538. \arg EXMC_BANK1_NAND: the NAND bank1
  539. \arg EXMC_BANK2_NAND: the NAND bank2
  540. \arg EXMC_BANK3_PCCARD: the PC card bank
  541. \param[in] interrupt_source: specify get which interrupt flag
  542. only one parameter can be selected which is shown as below:
  543. \arg EXMC_NAND_PCCARD_INT_RISE: interrupt source of rising edge
  544. \arg EXMC_NAND_PCCARD_INT_LEVEL: interrupt source of high-level
  545. \arg EXMC_NAND_PCCARD_INT_FALL: interrupt source of falling edge
  546. \param[out] none
  547. \retval FlagStatus: SET or RESET
  548. */
  549. FlagStatus exmc_interrupt_flag_get(uint32_t bank, uint32_t interrupt_source)
  550. {
  551. uint32_t status = 0x00000000U,interrupt_enable = 0x00000000U,interrupt_state = 0x00000000U;
  552. /* NAND bank1,bank2 or PC card bank3 */
  553. status = EXMC_NPINTEN(bank);
  554. interrupt_state = (status & (interrupt_source >> INTEN_INTS_OFFSET));
  555. interrupt_enable = (status & interrupt_source);
  556. if ((interrupt_enable) && (interrupt_state)){
  557. /* interrupt flag is set */
  558. return SET;
  559. }else{
  560. /* interrupt flag is reset */
  561. return RESET;
  562. }
  563. }
  564. /*!
  565. \brief clear EXMC interrupt flag
  566. \param[in] bank: specifies the NAND bank, PC card bank
  567. only one parameter can be selected which is shown as below:
  568. \arg EXMC_BANK1_NAND: the NAND bank1
  569. \arg EXMC_BANK2_NAND: the NAND bank2
  570. \arg EXMC_BANK3_PCCARD: the PC card bank
  571. \param[in] interrupt_source: specify get which interrupt flag
  572. one or more parameters can be selected which is shown as below:
  573. \arg EXMC_NAND_PCCARD_INT_RISE: interrupt source of rising edge
  574. \arg EXMC_NAND_PCCARD_INT_LEVEL: interrupt source of high-level
  575. \arg EXMC_NAND_PCCARD_INT_FALL: interrupt source of falling edge
  576. \param[out] none
  577. \retval none
  578. */
  579. void exmc_interrupt_flag_clear(uint32_t bank, uint32_t interrupt_source)
  580. {
  581. /* NAND bank1, bank2 or PC card bank3 */
  582. EXMC_NPINTEN(bank) &= ~(interrupt_source >> INTEN_INTS_OFFSET);
  583. }