gd32f30x_exmc.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677
  1. /*!
  2. \file gd32f30x_exmc.c
  3. \brief EXMC driver
  4. \version 2017-02-10, V1.0.0, firmware for GD32F30x
  5. \version 2018-10-10, V1.1.0, firmware for GD32F30x
  6. \version 2018-12-25, V2.0.0, firmware for GD32F30x
  7. \version 2020-09-30, V2.1.0, firmware for GD32F30x
  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 "gd32f30x_exmc.h"
  33. /* EXMC bank0 register reset value */
  34. #define BANK0_SNCTL_REGION0_RESET ((uint32_t)0x000030DBU)
  35. #define BANK0_SNCTL_REGION1_2_3_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)0x00000042U)
  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)0x00000043U)
  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] exmc_norsram_region: select the region of bank0
  78. only one parameter can be selected which is shown as below:
  79. \arg EXMC_BANK0_NORSRAM_REGIONx(x=0..3)
  80. \param[out] none
  81. \retval none
  82. */
  83. void exmc_norsram_deinit(uint32_t exmc_norsram_region)
  84. {
  85. /* reset the registers */
  86. if(EXMC_BANK0_NORSRAM_REGION0 == exmc_norsram_region){
  87. EXMC_SNCTL(exmc_norsram_region) = BANK0_SNCTL_REGION0_RESET;
  88. }else{
  89. EXMC_SNCTL(exmc_norsram_region) = BANK0_SNCTL_REGION1_2_3_RESET;
  90. }
  91. EXMC_SNTCFG(exmc_norsram_region) = BANK0_SNTCFG_RESET;
  92. EXMC_SNWTCFG(exmc_norsram_region) = BANK0_SNWTCFG_RESET;
  93. }
  94. /*!
  95. \brief initialize exmc_norsram_parameter_struct with the default values
  96. \param[in] none
  97. \param[out] exmc_norsram_init_struct: the initialized struct exmc_norsram_parameter_struct pointer
  98. \retval none
  99. */
  100. void exmc_norsram_struct_para_init(exmc_norsram_parameter_struct* exmc_norsram_init_struct)
  101. {
  102. /* configure the structure with default values */
  103. exmc_norsram_init_struct->norsram_region = EXMC_BANK0_NORSRAM_REGION0;
  104. exmc_norsram_init_struct->address_data_mux = ENABLE;
  105. exmc_norsram_init_struct->memory_type = EXMC_MEMORY_TYPE_SRAM;
  106. exmc_norsram_init_struct->databus_width = EXMC_NOR_DATABUS_WIDTH_8B;
  107. exmc_norsram_init_struct->burst_mode = DISABLE;
  108. exmc_norsram_init_struct->nwait_polarity = EXMC_NWAIT_POLARITY_LOW;
  109. exmc_norsram_init_struct->wrap_burst_mode = DISABLE;
  110. exmc_norsram_init_struct->nwait_config = EXMC_NWAIT_CONFIG_BEFORE;
  111. exmc_norsram_init_struct->memory_write = ENABLE;
  112. exmc_norsram_init_struct->nwait_signal = ENABLE;
  113. exmc_norsram_init_struct->extended_mode = DISABLE;
  114. exmc_norsram_init_struct->asyn_wait = DISABLE;
  115. exmc_norsram_init_struct->write_mode = EXMC_ASYN_WRITE;
  116. /* read/write timing configure */
  117. exmc_norsram_init_struct->read_write_timing->asyn_address_setuptime = 0xFU;
  118. exmc_norsram_init_struct->read_write_timing->asyn_address_holdtime = 0xFU;
  119. exmc_norsram_init_struct->read_write_timing->asyn_data_setuptime = 0xFFU;
  120. exmc_norsram_init_struct->read_write_timing->bus_latency = 0xFU;
  121. exmc_norsram_init_struct->read_write_timing->syn_clk_division = EXMC_SYN_CLOCK_RATIO_16_CLK;
  122. exmc_norsram_init_struct->read_write_timing->syn_data_latency = EXMC_DATALAT_17_CLK;
  123. exmc_norsram_init_struct->read_write_timing->asyn_access_mode = EXMC_ACCESS_MODE_A;
  124. /* write timing configure, when extended mode is used */
  125. exmc_norsram_init_struct->write_timing->asyn_address_setuptime = 0xFU;
  126. exmc_norsram_init_struct->write_timing->asyn_address_holdtime = 0xFU;
  127. exmc_norsram_init_struct->write_timing->asyn_data_setuptime = 0xFFU;
  128. exmc_norsram_init_struct->write_timing->bus_latency = 0xFU;
  129. exmc_norsram_init_struct->write_timing->asyn_access_mode = EXMC_ACCESS_MODE_A;
  130. }
  131. /*!
  132. \brief initialize EXMC NOR/SRAM region
  133. \param[in] exmc_norsram_parameter_struct: configure the EXMC NOR/SRAM parameter
  134. norsram_region: EXMC_BANK0_NORSRAM_REGIONx,x=0..3
  135. write_mode: EXMC_ASYN_WRITE,EXMC_SYN_WRITE
  136. extended_mode: ENABLE or DISABLE
  137. asyn_wait: ENABLE or DISABLE
  138. nwait_signal: ENABLE or DISABLE
  139. memory_write: ENABLE or DISABLE
  140. nwait_config: EXMC_NWAIT_CONFIG_BEFORE,EXMC_NWAIT_CONFIG_DURING
  141. wrap_burst_mode: ENABLE or DISABLE
  142. nwait_polarity: EXMC_NWAIT_POLARITY_LOW,EXMC_NWAIT_POLARITY_HIGH
  143. burst_mode: ENABLE or DISABLE
  144. databus_width: EXMC_NOR_DATABUS_WIDTH_8B,EXMC_NOR_DATABUS_WIDTH_16B
  145. memory_type: EXMC_MEMORY_TYPE_SRAM,EXMC_MEMORY_TYPE_PSRAM,EXMC_MEMORY_TYPE_NOR
  146. address_data_mux: ENABLE or DISABLE
  147. read_write_timing: struct exmc_norsram_timing_parameter_struct set the time
  148. write_timing: struct exmc_norsram_timing_parameter_struct set the time
  149. \param[out] none
  150. \retval none
  151. */
  152. void exmc_norsram_init(exmc_norsram_parameter_struct* exmc_norsram_init_struct)
  153. {
  154. uint32_t snctl = 0x00000000U,sntcfg = 0x00000000U,snwtcfg = 0x00000000U;
  155. /* get the register value */
  156. snctl = EXMC_SNCTL(exmc_norsram_init_struct->norsram_region);
  157. /* clear relative bits */
  158. snctl &= ((uint32_t)~(EXMC_SNCTL_NRMUX | EXMC_SNCTL_NRTP | EXMC_SNCTL_NRW | EXMC_SNCTL_SBRSTEN |
  159. EXMC_SNCTL_NREN | EXMC_SNCTL_NRWTPOL | EXMC_SNCTL_WRAPEN | EXMC_SNCTL_NRWTCFG |
  160. EXMC_SNCTL_WREN | EXMC_SNCTL_NRWTEN | EXMC_SNCTL_EXMODEN | EXMC_SNCTL_ASYNCWAIT |
  161. EXMC_SNCTL_SYNCWR ));
  162. snctl |= (uint32_t)(exmc_norsram_init_struct->address_data_mux << SNCTL_NRMUX_OFFSET) |
  163. exmc_norsram_init_struct->memory_type |
  164. exmc_norsram_init_struct->databus_width |
  165. (exmc_norsram_init_struct->burst_mode << SNCTL_SBRSTEN_OFFSET) |
  166. exmc_norsram_init_struct->nwait_polarity |
  167. (exmc_norsram_init_struct->wrap_burst_mode << SNCTL_WRAPEN_OFFSET) |
  168. exmc_norsram_init_struct->nwait_config |
  169. (exmc_norsram_init_struct->memory_write << SNCTL_WREN_OFFSET) |
  170. (exmc_norsram_init_struct->nwait_signal << SNCTL_NRWTEN_OFFSET) |
  171. (exmc_norsram_init_struct->extended_mode << SNCTL_EXMODEN_OFFSET) |
  172. (exmc_norsram_init_struct->asyn_wait << SNCTL_ASYNCWAIT_OFFSET) |
  173. exmc_norsram_init_struct->write_mode;
  174. sntcfg = (uint32_t)((exmc_norsram_init_struct->read_write_timing->asyn_address_setuptime - 1U ) & EXMC_SNTCFG_ASET )|
  175. (((exmc_norsram_init_struct->read_write_timing->asyn_address_holdtime - 1U ) << SNTCFG_AHLD_OFFSET ) & EXMC_SNTCFG_AHLD ) |
  176. (((exmc_norsram_init_struct->read_write_timing->asyn_data_setuptime - 1U ) << SNTCFG_DSET_OFFSET ) & EXMC_SNTCFG_DSET ) |
  177. (((exmc_norsram_init_struct->read_write_timing->bus_latency - 1U ) << SNTCFG_BUSLAT_OFFSET ) & EXMC_SNTCFG_BUSLAT )|
  178. exmc_norsram_init_struct->read_write_timing->syn_clk_division |
  179. exmc_norsram_init_struct->read_write_timing->syn_data_latency |
  180. exmc_norsram_init_struct->read_write_timing->asyn_access_mode;
  181. /* nor flash access enable */
  182. if(EXMC_MEMORY_TYPE_NOR == exmc_norsram_init_struct->memory_type){
  183. snctl |= (uint32_t)EXMC_SNCTL_NREN;
  184. }
  185. /* extended mode configure */
  186. if(ENABLE == exmc_norsram_init_struct->extended_mode){
  187. snwtcfg = (uint32_t)((exmc_norsram_init_struct->write_timing->asyn_address_setuptime - 1U) & EXMC_SNWTCFG_WASET ) |
  188. (((exmc_norsram_init_struct->write_timing->asyn_address_holdtime -1U ) << SNWTCFG_WAHLD_OFFSET ) & EXMC_SNWTCFG_WAHLD )|
  189. (((exmc_norsram_init_struct->write_timing->asyn_data_setuptime -1U ) << SNWTCFG_WDSET_OFFSET ) & EXMC_SNWTCFG_WDSET )|
  190. (((exmc_norsram_init_struct->write_timing->bus_latency - 1U ) << SNWTCFG_WBUSLAT_OFFSET ) & EXMC_SNWTCFG_WBUSLAT ) |
  191. exmc_norsram_init_struct->write_timing->asyn_access_mode;
  192. }else{
  193. snwtcfg = BANK0_SNWTCFG_RESET;
  194. }
  195. /* configure the registers */
  196. EXMC_SNCTL(exmc_norsram_init_struct->norsram_region) = snctl;
  197. EXMC_SNTCFG(exmc_norsram_init_struct->norsram_region) = sntcfg;
  198. EXMC_SNWTCFG(exmc_norsram_init_struct->norsram_region) = snwtcfg;
  199. }
  200. /*!
  201. \brief enable EXMC NOR/PSRAM bank region
  202. \param[in] exmc_norsram_region: specifie the region of NOR/PSRAM bank
  203. only one parameter can be selected which is shown as below:
  204. \arg EXMC_BANK0_NORSRAM_REGIONx(x=0..3)
  205. \param[out] none
  206. \retval none
  207. */
  208. void exmc_norsram_enable(uint32_t exmc_norsram_region)
  209. {
  210. EXMC_SNCTL(exmc_norsram_region) |= (uint32_t)EXMC_SNCTL_NRBKEN;
  211. }
  212. /*!
  213. \brief disable EXMC NOR/PSRAM bank region
  214. \param[in] exmc_norsram_region: specifie the region of NOR/PSRAM Bank
  215. only one parameter can be selected which is shown as below:
  216. \arg EXMC_BANK0_NORSRAM_REGIONx(x=0..3)
  217. \param[out] none
  218. \retval none
  219. */
  220. void exmc_norsram_disable(uint32_t exmc_norsram_region)
  221. {
  222. EXMC_SNCTL(exmc_norsram_region) &= ~(uint32_t)EXMC_SNCTL_NRBKEN;
  223. }
  224. /*!
  225. \brief deinitialize EXMC NAND bank
  226. \param[in] exmc_nand_bank: select the bank of NAND
  227. only one parameter can be selected which is shown as below:
  228. \arg EXMC_BANKx_NAND(x=1..2)
  229. \param[out] none
  230. \retval none
  231. */
  232. void exmc_nand_deinit(uint32_t exmc_nand_bank)
  233. {
  234. /* EXMC_BANK1_NAND or EXMC_BANK2_NAND */
  235. EXMC_NPCTL(exmc_nand_bank) = BANK1_2_NPCTL_RESET;
  236. EXMC_NPINTEN(exmc_nand_bank) = BANK1_2_NPINTEN_RESET;
  237. EXMC_NPCTCFG(exmc_nand_bank) = BANK1_2_NPCTCFG_RESET;
  238. EXMC_NPATCFG(exmc_nand_bank) = BANK1_2_NPATCFG_RESET;
  239. }
  240. /*!
  241. \brief initialize exmc_norsram_parameter_struct with the default values
  242. \param[in] none
  243. \param[out] the initialized struct exmc_norsram_parameter_struct pointer
  244. \retval none
  245. */
  246. void exmc_nand_struct_para_init(exmc_nand_parameter_struct* exmc_nand_init_struct)
  247. {
  248. /* configure the structure with default values */
  249. exmc_nand_init_struct->nand_bank = EXMC_BANK1_NAND;
  250. exmc_nand_init_struct->wait_feature = DISABLE;
  251. exmc_nand_init_struct->databus_width = EXMC_NAND_DATABUS_WIDTH_8B;
  252. exmc_nand_init_struct->ecc_logic = DISABLE;
  253. exmc_nand_init_struct->ecc_size = EXMC_ECC_SIZE_256BYTES;
  254. exmc_nand_init_struct->ctr_latency = 0x0U;
  255. exmc_nand_init_struct->atr_latency = 0x0U;
  256. exmc_nand_init_struct->common_space_timing->setuptime = 0xFCU;
  257. exmc_nand_init_struct->common_space_timing->waittime = 0xFCU;
  258. exmc_nand_init_struct->common_space_timing->holdtime = 0xFCU;
  259. exmc_nand_init_struct->common_space_timing->databus_hiztime = 0xFCU;
  260. exmc_nand_init_struct->attribute_space_timing->setuptime = 0xFCU;
  261. exmc_nand_init_struct->attribute_space_timing->waittime = 0xFCU;
  262. exmc_nand_init_struct->attribute_space_timing->holdtime = 0xFCU;
  263. exmc_nand_init_struct->attribute_space_timing->databus_hiztime = 0xFCU;
  264. }
  265. /*!
  266. \brief initialize EXMC NAND bank
  267. \param[in] exmc_nand_parameter_struct: configure the EXMC NAND parameter
  268. nand_bank: EXMC_BANK1_NAND,EXMC_BANK2_NAND
  269. ecc_size: EXMC_ECC_SIZE_xBYTES,x=256,512,1024,2048,4096
  270. atr_latency: EXMC_ALE_RE_DELAY_x_HCLK,x=1..16
  271. ctr_latency: EXMC_CLE_RE_DELAY_x_HCLK,x=1..16
  272. ecc_logic: ENABLE or DISABLE
  273. databus_width: EXMC_NAND_DATABUS_WIDTH_8B,EXMC_NAND_DATABUS_WIDTH_16B
  274. wait_feature: ENABLE or DISABLE
  275. common_space_timing: struct exmc_nand_pccard_timing_parameter_struct set the time
  276. attribute_space_timing: struct exmc_nand_pccard_timing_parameter_struct set the time
  277. \param[out] none
  278. \retval none
  279. */
  280. void exmc_nand_init(exmc_nand_parameter_struct* exmc_nand_init_struct)
  281. {
  282. uint32_t npctl = 0x00000000U, npctcfg = 0x00000000U, npatcfg = 0x00000000U;
  283. npctl = (uint32_t)(exmc_nand_init_struct->wait_feature << NPCTL_NDWTEN_OFFSET)|
  284. EXMC_NPCTL_NDTP |
  285. exmc_nand_init_struct->databus_width |
  286. (exmc_nand_init_struct->ecc_logic << NPCTL_ECCEN_OFFSET)|
  287. exmc_nand_init_struct->ecc_size |
  288. exmc_nand_init_struct->ctr_latency |
  289. exmc_nand_init_struct->atr_latency;
  290. npctcfg = (uint32_t)((exmc_nand_init_struct->common_space_timing->setuptime - 1U) & EXMC_NPCTCFG_COMSET ) |
  291. (((exmc_nand_init_struct->common_space_timing->waittime - 1U) << NPCTCFG_COMWAIT_OFFSET) & EXMC_NPCTCFG_COMWAIT ) |
  292. ((exmc_nand_init_struct->common_space_timing->holdtime << NPCTCFG_COMHLD_OFFSET) & EXMC_NPCTCFG_COMHLD ) |
  293. (((exmc_nand_init_struct->common_space_timing->databus_hiztime - 1U) << NPCTCFG_COMHIZ_OFFSET) & EXMC_NPCTCFG_COMHIZ );
  294. npatcfg = (uint32_t)((exmc_nand_init_struct->attribute_space_timing->setuptime - 1U) & EXMC_NPATCFG_ATTSET ) |
  295. (((exmc_nand_init_struct->attribute_space_timing->waittime - 1U) << NPATCFG_ATTWAIT_OFFSET) & EXMC_NPATCFG_ATTWAIT ) |
  296. ((exmc_nand_init_struct->attribute_space_timing->holdtime << NPATCFG_ATTHLD_OFFSET) & EXMC_NPATCFG_ATTHLD ) |
  297. (((exmc_nand_init_struct->attribute_space_timing->databus_hiztime -1U) << NPATCFG_ATTHIZ_OFFSET) & EXMC_NPATCFG_ATTHIZ );
  298. /* EXMC_BANK1_NAND or EXMC_BANK2_NAND initialize */
  299. EXMC_NPCTL(exmc_nand_init_struct->nand_bank) = npctl;
  300. EXMC_NPCTCFG(exmc_nand_init_struct->nand_bank) = npctcfg;
  301. EXMC_NPATCFG(exmc_nand_init_struct->nand_bank) = npatcfg;
  302. }
  303. /*!
  304. \brief enable NAND bank
  305. \param[in] exmc_nand_bank: specifie the NAND bank
  306. only one parameter can be selected which is shown as below:
  307. \arg EXMC_BANKx_NAND(x=1,2)
  308. \param[out] none
  309. \retval none
  310. */
  311. void exmc_nand_enable(uint32_t exmc_nand_bank)
  312. {
  313. EXMC_NPCTL(exmc_nand_bank) |= EXMC_NPCTL_NDBKEN;
  314. }
  315. /*!
  316. \brief disable NAND bank
  317. \param[in] exmc_nand_bank: specifie the NAND bank
  318. only one parameter can be selected which is shown as below:
  319. \arg EXMC_BANKx_NAND(x=1,2)
  320. \param[out] none
  321. \retval none
  322. */
  323. void exmc_nand_disable(uint32_t exmc_nand_bank)
  324. {
  325. EXMC_NPCTL(exmc_nand_bank) &= (~EXMC_NPCTL_NDBKEN);
  326. }
  327. /*!
  328. \brief deinitialize EXMC PC card bank
  329. \param[in] none
  330. \param[out] none
  331. \retval none
  332. */
  333. void exmc_pccard_deinit(void)
  334. {
  335. /* EXMC_BANK3_PCCARD */
  336. EXMC_NPCTL3 = BANK3_NPCTL_RESET;
  337. EXMC_NPINTEN3 = BANK3_NPINTEN_RESET;
  338. EXMC_NPCTCFG3 = BANK3_NPCTCFG_RESET;
  339. EXMC_NPATCFG3 = BANK3_NPATCFG_RESET;
  340. EXMC_PIOTCFG3 = BANK3_PIOTCFG3_RESET;
  341. }
  342. /*!
  343. \brief initialize exmc_pccard_parameter_struct parameter with the default values
  344. \param[in] none
  345. \param[out] the initialized struct exmc_pccard_parameter_struct pointer
  346. \retval none
  347. */
  348. void exmc_pccard_struct_para_init(exmc_pccard_parameter_struct* exmc_pccard_init_struct)
  349. {
  350. /* configure the structure with default values */
  351. exmc_pccard_init_struct->wait_feature = DISABLE;
  352. exmc_pccard_init_struct->ctr_latency = 0x0U;
  353. exmc_pccard_init_struct->atr_latency = 0x0U;
  354. exmc_pccard_init_struct->common_space_timing->setuptime = 0xFCU;
  355. exmc_pccard_init_struct->common_space_timing->waittime = 0xFCU;
  356. exmc_pccard_init_struct->common_space_timing->holdtime = 0xFCU;
  357. exmc_pccard_init_struct->common_space_timing->databus_hiztime = 0xFCU;
  358. exmc_pccard_init_struct->attribute_space_timing->setuptime = 0xFCU;
  359. exmc_pccard_init_struct->attribute_space_timing->waittime = 0xFCU;
  360. exmc_pccard_init_struct->attribute_space_timing->holdtime = 0xFCU;
  361. exmc_pccard_init_struct->attribute_space_timing->databus_hiztime = 0xFCU;
  362. exmc_pccard_init_struct->io_space_timing->setuptime = 0xFCU;
  363. exmc_pccard_init_struct->io_space_timing->waittime = 0xFCU;
  364. exmc_pccard_init_struct->io_space_timing->holdtime = 0xFCU;
  365. exmc_pccard_init_struct->io_space_timing->databus_hiztime = 0xFCU;
  366. }
  367. /*!
  368. \brief initialize EXMC PC card bank
  369. \param[in] exmc_pccard_parameter_struct: configure the EXMC NAND parameter
  370. atr_latency: EXMC_ALE_RE_DELAY_x_HCLK,x=1..16
  371. ctr_latency: EXMC_CLE_RE_DELAY_x_HCLK,x=1..16
  372. wait_feature: ENABLE or DISABLE
  373. common_space_timing: struct exmc_nand_pccard_timing_parameter_struct set the time
  374. attribute_space_timing: struct exmc_nand_pccard_timing_parameter_struct set the time
  375. io_space_timing: exmc_nand_pccard_timing_parameter_struct set the time
  376. \param[out] none
  377. \retval none
  378. */
  379. void exmc_pccard_init(exmc_pccard_parameter_struct* exmc_pccard_init_struct)
  380. {
  381. /* configure the EXMC bank3 PC card control register */
  382. EXMC_NPCTL3 = (uint32_t)(exmc_pccard_init_struct->wait_feature << NPCTL_NDWTEN_OFFSET) |
  383. EXMC_NAND_DATABUS_WIDTH_16B |
  384. exmc_pccard_init_struct->ctr_latency |
  385. exmc_pccard_init_struct->atr_latency ;
  386. /* configure the EXMC bank3 PC card common space timing configuration register */
  387. EXMC_NPCTCFG3 = (uint32_t)((exmc_pccard_init_struct->common_space_timing->setuptime - 1U)& EXMC_NPCTCFG_COMSET ) |
  388. (((exmc_pccard_init_struct->common_space_timing->waittime - 1U) << NPCTCFG_COMWAIT_OFFSET) & EXMC_NPCTCFG_COMWAIT ) |
  389. ((exmc_pccard_init_struct->common_space_timing->holdtime << NPCTCFG_COMHLD_OFFSET) & EXMC_NPCTCFG_COMHLD ) |
  390. (((exmc_pccard_init_struct->common_space_timing->databus_hiztime - 1U) << NPCTCFG_COMHIZ_OFFSET) & EXMC_NPCTCFG_COMHIZ );
  391. /* configure the EXMC bank3 PC card attribute space timing configuration register */
  392. EXMC_NPATCFG3 = (uint32_t)((exmc_pccard_init_struct->attribute_space_timing->setuptime - 1U) & EXMC_NPATCFG_ATTSET ) |
  393. (((exmc_pccard_init_struct->attribute_space_timing->waittime - 1U) << NPATCFG_ATTWAIT_OFFSET) & EXMC_NPATCFG_ATTWAIT ) |
  394. ((exmc_pccard_init_struct->attribute_space_timing->holdtime << NPATCFG_ATTHLD_OFFSET) & EXMC_NPATCFG_ATTHLD )|
  395. (((exmc_pccard_init_struct->attribute_space_timing->databus_hiztime -1U) << NPATCFG_ATTHIZ_OFFSET) & EXMC_NPATCFG_ATTHIZ );
  396. /* configure the EXMC bank3 PC card io space timing configuration register */
  397. EXMC_PIOTCFG3 = (uint32_t)((exmc_pccard_init_struct->io_space_timing->setuptime - 1U) & EXMC_PIOTCFG3_IOSET ) |
  398. (((exmc_pccard_init_struct->io_space_timing->waittime - 1U) << PIOTCFG_IOWAIT_OFFSET) & EXMC_PIOTCFG3_IOWAIT ) |
  399. ((exmc_pccard_init_struct->io_space_timing->holdtime << PIOTCFG_IOHLD_OFFSET) & EXMC_PIOTCFG3_IOHLD )|
  400. ((exmc_pccard_init_struct->io_space_timing->databus_hiztime << PIOTCFG_IOHIZ_OFFSET) & EXMC_PIOTCFG3_IOHIZ );
  401. }
  402. /*!
  403. \brief enable PC Card Bank
  404. \param[in] none
  405. \param[out] none
  406. \retval none
  407. */
  408. void exmc_pccard_enable(void)
  409. {
  410. EXMC_NPCTL3 |= EXMC_NPCTL_NDBKEN;
  411. }
  412. /*!
  413. \brief disable PC Card Bank
  414. \param[in] none
  415. \param[out] none
  416. \retval none
  417. */
  418. void exmc_pccard_disable(void)
  419. {
  420. EXMC_NPCTL3 &= (~EXMC_NPCTL_NDBKEN);
  421. }
  422. /*!
  423. \brief configure CRAM page size
  424. \param[in] exmc_norsram_region: specifie the region of NOR/PSRAM bank
  425. only one parameter can be selected which is shown as below:
  426. \arg EXMC_BANK0_NORSRAM_REGIONx(x=0..3)
  427. \param[in] page_size: CRAM page size
  428. only one parameter can be selected which is shown as below:
  429. \arg EXMC_CRAM_AUTO_SPLIT: the clock is generated only during synchronous access
  430. \arg EXMC_CRAM_PAGE_SIZE_128_BYTES: page size is 128 bytes
  431. \arg EXMC_CRAM_PAGE_SIZE_256_BYTES: page size is 256 bytes
  432. \arg EXMC_CRAM_PAGE_SIZE_512_BYTES: page size is 512 bytes
  433. \arg EXMC_CRAM_PAGE_SIZE_1024_BYTES: page size is 1024 bytes
  434. \param[out] none
  435. \retval none
  436. */
  437. void exmc_norsram_page_size_config(uint32_t exmc_norsram_region, uint32_t page_size)
  438. {
  439. /* reset the bits */
  440. EXMC_SNCTL(exmc_norsram_region) &= ~EXMC_SNCTL_CPS;
  441. /* set the CPS bits */
  442. EXMC_SNCTL(exmc_norsram_region) |= page_size;
  443. }
  444. /*!
  445. \brief enable or disable the EXMC NAND ECC function
  446. \param[in] exmc_nand_bank: specifie the NAND bank
  447. only one parameter can be selected which is shown as below:
  448. \arg EXMC_BANKx_NAND(x=1,2)
  449. \param[in] newvalue: ENABLE or DISABLE
  450. \param[out] none
  451. \retval none
  452. */
  453. void exmc_nand_ecc_config(uint32_t exmc_nand_bank, ControlStatus newvalue)
  454. {
  455. if (ENABLE == newvalue){
  456. /* enable the selected NAND bank ECC function */
  457. EXMC_NPCTL(exmc_nand_bank) |= EXMC_NPCTL_ECCEN;
  458. }else{
  459. /* disable the selected NAND bank ECC function */
  460. EXMC_NPCTL(exmc_nand_bank) &= (~EXMC_NPCTL_ECCEN);
  461. }
  462. }
  463. /*!
  464. \brief get the EXMC ECC value
  465. \param[in] exmc_nand_bank: specifie the NAND bank
  466. only one parameter can be selected which is shown as below:
  467. \arg EXMC_BANKx_NAND(x=1,2)
  468. \param[out] none
  469. \retval the error correction code(ECC) value
  470. */
  471. uint32_t exmc_ecc_get(uint32_t exmc_nand_bank)
  472. {
  473. return (EXMC_NECC(exmc_nand_bank));
  474. }
  475. /*!
  476. \brief enable EXMC interrupt
  477. \param[in] exmc_bank: specifies the NAND bank,PC card bank
  478. only one parameter can be selected which is shown as below:
  479. \arg EXMC_BANK1_NAND: the NAND bank1
  480. \arg EXMC_BANK2_NAND: the NAND bank2
  481. \arg EXMC_BANK3_PCCARD: the PC card bank
  482. \param[in] interrupt: EXMC interrupt flag
  483. only one parameter can be selected which are shown as below:
  484. \arg EXMC_NAND_PCCARD_INT_FLAG_RISE: rising edge interrupt and flag
  485. \arg EXMC_NAND_PCCARD_INT_FLAG_LEVEL: high-level interrupt and flag
  486. \arg EXMC_NAND_PCCARD_INT_FLAG_FALL: falling edge interrupt and flag
  487. \param[out] none
  488. \retval none
  489. */
  490. void exmc_interrupt_enable(uint32_t exmc_bank,uint32_t interrupt)
  491. {
  492. /* NAND bank1,bank2 or PC card bank3 */
  493. EXMC_NPINTEN(exmc_bank) |= interrupt;
  494. }
  495. /*!
  496. \brief disable EXMC interrupt
  497. \param[in] exmc_bank: specifies the NAND bank , PC card bank
  498. only one parameter can be selected which is shown as below:
  499. \arg EXMC_BANK1_NAND: the NAND bank1
  500. \arg EXMC_BANK2_NAND: the NAND bank2
  501. \arg EXMC_BANK3_PCCARD: the PC card bank
  502. \param[in] interrupt: EXMC interrupt flag
  503. only one parameter can be selected which are shown as below:
  504. \arg EXMC_NAND_PCCARD_INT_FLAG_RISE: rising edge interrupt and flag
  505. \arg EXMC_NAND_PCCARD_INT_FLAG_LEVEL: high-level interrupt and flag
  506. \arg EXMC_NAND_PCCARD_INT_FLAG_FALL: falling edge interrupt and flag
  507. \param[out] none
  508. \retval none
  509. */
  510. void exmc_interrupt_disable(uint32_t exmc_bank,uint32_t interrupt)
  511. {
  512. /* NAND bank1,bank2 or PC card bank3 */
  513. EXMC_NPINTEN(exmc_bank) &= (~interrupt);
  514. }
  515. /*!
  516. \brief get EXMC flag status
  517. \param[in] exmc_bank: specifies the NAND bank , PC card bank
  518. only one parameter can be selected which is shown as below:
  519. \arg EXMC_BANK1_NAND: the NAND bank1
  520. \arg EXMC_BANK2_NAND: the NAND bank2
  521. \arg EXMC_BANK3_PCCARD: the PC Card bank
  522. \param[in] flag: EXMC status and flag
  523. only one parameter can be selected which are shown as below:
  524. \arg EXMC_NAND_PCCARD_FLAG_RISE: interrupt rising edge status
  525. \arg EXMC_NAND_PCCARD_FLAG_LEVEL: interrupt high-level status
  526. \arg EXMC_NAND_PCCARD_FLAG_FALL: interrupt falling edge status
  527. \arg EXMC_NAND_PCCARD_FLAG_FIFOE: FIFO empty flag
  528. \param[out] none
  529. \retval FlagStatus: SET or RESET
  530. */
  531. FlagStatus exmc_flag_get(uint32_t exmc_bank,uint32_t flag)
  532. {
  533. uint32_t status = 0x00000000U;
  534. /* NAND bank1,bank2 or PC card bank3 */
  535. status = EXMC_NPINTEN(exmc_bank);
  536. if ((status & flag) != (uint32_t)flag ){
  537. /* flag is reset */
  538. return RESET;
  539. }else{
  540. /* flag is set */
  541. return SET;
  542. }
  543. }
  544. /*!
  545. \brief clear EXMC flag status
  546. \param[in] exmc_bank: specifie the NAND bank , PCCARD bank
  547. only one parameter can be selected which is shown as below:
  548. \arg EXMC_BANK1_NAND: the NAND bank1
  549. \arg EXMC_BANK2_NAND: the NAND bank2
  550. \arg EXMC_BANK3_PCCARD: the PC card bank
  551. \param[in] flag: EXMC status and flag
  552. only one parameter can be selected which are shown as below:
  553. \arg EXMC_NAND_PCCARD_FLAG_RISE: interrupt rising edge status
  554. \arg EXMC_NAND_PCCARD_FLAG_LEVEL: interrupt high-level status
  555. \arg EXMC_NAND_PCCARD_FLAG_FALL: interrupt falling edge status
  556. \arg EXMC_NAND_PCCARD_FLAG_FIFOE: FIFO empty flag
  557. \param[out] none
  558. \retval none
  559. */
  560. void exmc_flag_clear(uint32_t exmc_bank,uint32_t flag)
  561. {
  562. /* NAND bank1,bank2 or PC card bank3 */
  563. EXMC_NPINTEN(exmc_bank) &= (~flag);
  564. }
  565. /*!
  566. \brief get EXMC interrupt flag
  567. \param[in] exmc_bank: specifies the NAND bank , PC card bank
  568. only one parameter can be selected which is shown as below:
  569. \arg EXMC_BANK1_NAND: the NAND bank1
  570. \arg EXMC_BANK2_NAND: the NAND bank2
  571. \arg EXMC_BANK3_PCCARD: the PC card bank
  572. \param[in] interrupt: EXMC interrupt flag
  573. only one parameter can be selected which are shown as below:
  574. \arg EXMC_NAND_PCCARD_INT_FLAG_RISE: rising edge interrupt and flag
  575. \arg EXMC_NAND_PCCARD_INT_FLAG_LEVEL: high-level interrupt and flag
  576. \arg EXMC_NAND_PCCARD_INT_FLAG_FALL: falling edge interrupt and flag
  577. \param[out] none
  578. \retval FlagStatus: SET or RESET
  579. */
  580. FlagStatus exmc_interrupt_flag_get(uint32_t exmc_bank,uint32_t interrupt)
  581. {
  582. uint32_t status = 0x00000000U,interrupt_enable = 0x00000000U,interrupt_state = 0x00000000U;
  583. /* NAND bank1,bank2 or PC card bank3 */
  584. status = EXMC_NPINTEN(exmc_bank);
  585. interrupt_state = (status & (interrupt >> INTEN_INTS_OFFSET));
  586. interrupt_enable = (status & interrupt);
  587. if ((interrupt_enable) && (interrupt_state)){
  588. /* interrupt flag is set */
  589. return SET;
  590. }else{
  591. /* interrupt flag is reset */
  592. return RESET;
  593. }
  594. }
  595. /*!
  596. \brief clear EXMC interrupt flag
  597. \param[in] exmc_bank: specifies the NAND bank , PC card bank
  598. only one parameter can be selected which is shown as below:
  599. \arg EXMC_BANK1_NAND: the NAND bank1
  600. \arg EXMC_BANK2_NAND: the NAND bank2
  601. \arg EXMC_BANK3_PCCARD: the PC card bank
  602. \param[in] interrupt: EXMC interrupt flag
  603. only one parameter can be selected which are shown as below:
  604. \arg EXMC_NAND_PCCARD_INT_FLAG_RISE: rising edge interrupt and flag
  605. \arg EXMC_NAND_PCCARD_INT_FLAG_LEVEL: high-level interrupt and flag
  606. \arg EXMC_NAND_PCCARD_INT_FLAG_FALL: falling edge interrupt and flag
  607. \param[out] none
  608. \retval none
  609. */
  610. void exmc_interrupt_flag_clear(uint32_t exmc_bank,uint32_t interrupt)
  611. {
  612. /* NAND bank1,bank2 or PC card bank3 */
  613. EXMC_NPINTEN(exmc_bank) &= ~(interrupt >> INTEN_INTS_OFFSET);
  614. }