lpc_emc.c 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398
  1. /**********************************************************************
  2. * $Id$ lpc_emc.c 2011-06-02
  3. *//**
  4. * @file lpc_emc.c
  5. * @brief Contains all functions support for EMC firmware library
  6. * on LPC
  7. * @version 1.0
  8. * @date 02. June. 2011
  9. * @author NXP MCU SW Application Team
  10. *
  11. * Copyright(C) 2011, NXP Semiconductor
  12. * All rights reserved.
  13. *
  14. ***********************************************************************
  15. * Software that is described herein is for illustrative purposes only
  16. * which provides customers with programming information regarding the
  17. * products. This software is supplied "AS IS" without any warranties.
  18. * NXP Semiconductors assumes no responsibility or liability for the
  19. * use of the software, conveys no license or title under any patent,
  20. * copyright, or mask work right to the product. NXP Semiconductors
  21. * reserves the right to make changes in the software without
  22. * notification. NXP Semiconductors also make no representation or
  23. * warranty that such application will be suitable for the specified
  24. * use without further testing or modification.
  25. * Permission to use, copy, modify, and distribute this software and its
  26. * documentation is hereby granted, under NXP Semiconductors'
  27. * relevant copyright in the software, without fee, provided that it
  28. * is used in conjunction with NXP Semiconductors microcontrollers. This
  29. * copyright, permission, and disclaimer notice must appear in all copies of
  30. * this code.
  31. **********************************************************************/
  32. #ifdef __BUILD_WITH_EXAMPLE__
  33. #include "lpc_libcfg.h"
  34. #else
  35. #include "lpc_libcfg_default.h"
  36. #endif /* __BUILD_WITH_EXAMPLE__ */
  37. #ifdef _EMC
  38. #include "lpc_emc.h"
  39. #include "lpc_clkpwr.h"
  40. #include "lpc_pinsel.h"
  41. /* Peripheral group ----------------------------------------------------------- */
  42. /** @addtogroup EMC
  43. * @{
  44. */
  45. /** @defgroup EMC_Public_Functions
  46. * @{
  47. */
  48. /*********************************************************************
  49. * @brief Calculate refresh timer (the multiple of 16 CCLKs)
  50. * @param[in] freq - frequency of EMC Clk
  51. * @param[in] time - micro second
  52. * @return None
  53. **********************************************************************/
  54. uint32_t EMC_SDRAM_REFRESH(uint32_t time)
  55. {
  56. uint32_t emc_freq = CLKPWR_GetCLK(CLKPWR_CLKTYPE_EMC);
  57. return (((uint64_t)((uint64_t)time * emc_freq)/16000000ull)+1);
  58. }
  59. /*********************************************************************
  60. * @brief Calculate EMC Clock from nano second
  61. * @param[in] time - nano second
  62. * @return None
  63. **********************************************************************/
  64. uint32_t EMC_NS2CLK(uint32_t time){
  65. uint32_t emc_freq = CLKPWR_GetCLK(CLKPWR_CLKTYPE_EMC);
  66. return (((uint64_t)time*emc_freq/1000000000ull));
  67. }
  68. /*********************************************************************
  69. * @brief Power on EMC Block
  70. * @param[in] None
  71. * @return None
  72. **********************************************************************/
  73. EMC_FUNC_CODE EMC_PwrOn(void)
  74. {
  75. // If CPU clock is > 80 MHz, then divide it by two to create the EMC clock
  76. if(CLKPWR_GetCLK(CLKPWR_CLKTYPE_CPU) > 80000000) {
  77. CLKPWR_SetCLKDiv(CLKPWR_CLKTYPE_EMC, 1); // CPU clock / 2
  78. } else {
  79. CLKPWR_SetCLKDiv(CLKPWR_CLKTYPE_EMC, 0); // Same clock as CPU
  80. }
  81. // Power on
  82. CLKPWR_ConfigPPWR(CLKPWR_PCONP_PCEMC, ENABLE);
  83. // Enable
  84. LPC_EMC->Control = EMC_Control_E;
  85. return EMC_FUNC_OK;
  86. }
  87. /*********************************************************************//**
  88. * @brief Initialize external dynamic memory
  89. * @param[in] pConfig Configuration
  90. * @return EMC_FUNC_OK/EMC_FUNC_INVALID_PARAM/EMC_FUNC_ERR
  91. **********************************************************************/
  92. EMC_FUNC_CODE DynMem_Init(EMC_DYN_MEM_Config_Type* pConfig)
  93. {
  94. uint32_t i = 0;
  95. EMC_FUNC_CODE ret = EMC_FUNC_OK;
  96. /* Pin configuration:
  97. * P2.16 - /EMC_CAS
  98. * P2.17 - /EMC_RAS
  99. * P2.18 - EMC_CLK[0]
  100. * P2.19 - EMC_CLK[1]
  101. *
  102. * P2.20 - EMC_DYCS0
  103. * P2.21 - EMC_DYCS1
  104. * P2.22 - EMC_DYCS2
  105. * P2.23 - EMC_DYCS3
  106. *
  107. * P2.24 - EMC_CKE0
  108. * P2.25 - EMC_CKE1
  109. * P2.26 - EMC_CKE2
  110. * P2.27 - EMC_CKE3
  111. *
  112. * P2.28 - EMC_DQM0
  113. * P2.29 - EMC_DQM1
  114. * P2.30 - EMC_DQM2
  115. * P2.31 - EMC_DQM3
  116. *
  117. * P3.0-P3.31 - EMC_D[0-31]
  118. * P4.0-P4.23 - EMC_A[0-23]
  119. * P5.0-P5.1 - EMC_A[24-25]
  120. *
  121. * P4.25 - EMC_WE
  122. */
  123. PINSEL_ConfigPin(2,16,1);
  124. PINSEL_ConfigPin(2,17,1);
  125. PINSEL_ConfigPin(2,18,1);
  126. PINSEL_ConfigPin(2,19,1);
  127. PINSEL_ConfigPin(2,20,1);
  128. PINSEL_ConfigPin(2,21,1);
  129. PINSEL_ConfigPin(2,22,1);
  130. PINSEL_ConfigPin(2,23,1);
  131. PINSEL_ConfigPin(2,24,1);
  132. PINSEL_ConfigPin(2,25,1);
  133. PINSEL_ConfigPin(2,26,1);
  134. PINSEL_ConfigPin(2,27,1);
  135. PINSEL_ConfigPin(2,28,1);
  136. PINSEL_ConfigPin(2,29,1);
  137. PINSEL_ConfigPin(2,30,1);
  138. PINSEL_ConfigPin(2,31,1);
  139. for(i = 0; i < 32; i++)
  140. {
  141. PINSEL_ConfigPin(3,i,1);
  142. PINSEL_ConfigPin(4,i,1);
  143. }
  144. PINSEL_ConfigPin(5,0,1);
  145. PINSEL_ConfigPin(5,1,1);
  146. // Power On
  147. ret |= EMC_PwrOn();
  148. /*Init SDRAM controller*/
  149. LPC_SC->EMCDLYCTL |= (8<<0);
  150. /*Set data read delay*/
  151. LPC_SC->EMCDLYCTL |=(8<<8);
  152. LPC_SC->EMCDLYCTL |= (0x08 <<16);
  153. ret |= EMC_ConfigEndianMode(EMC_Config_Little_Endian_Mode);
  154. /* Dynamic memory setting */
  155. ret |= EMC_DynCtrlSelfRefresh(EMC_DYNAMIC_CTRL_SR_NORMALMODE);
  156. ret |= EMC_DynCtrlPowerDownMode(EMC_DYNAMIC_CTRL_DP_NORMAL);
  157. ret |= EMC_DynCtrlClockEnable(EMC_DYNAMIC_CTRL_CE_ALLCLK_HI);
  158. ret |= EMC_DynCtrlMMC(EMC_DYNAMIC_CTRL_MMC_CLKOUT_ENABLED);
  159. ret |= EMC_DynCtrlClockControl(EMC_DYNAMIC_CTRL_CE_CLKOUT_CONT);
  160. /* Timing */
  161. ret |= EMC_SetDynMemoryParameter(EMC_DYN_MEM_REFRESH_TIMER, pConfig->RefreshTime);
  162. ret |= EMC_SetDynMemoryParameter(EMC_DYN_MEM_READ_CONFIG, pConfig->ReadConfig);
  163. ret |= EMC_SetDynMemoryParameter(EMC_DYN_MEM_TRP, pConfig->PrechargeCmdPeriod);
  164. ret |= EMC_SetDynMemoryParameter(EMC_DYN_MEM_TRAS, pConfig->Active2PreChargeTime);
  165. ret |= EMC_SetDynMemoryParameter(EMC_DYN_MEM_TSREX, pConfig->SeftRefreshExitTime);
  166. ret |= EMC_SetDynMemoryParameter(EMC_DYN_MEM_TAPR, pConfig->DataOut2ActiveTime);
  167. ret |= EMC_SetDynMemoryParameter(EMC_DYN_MEM_TDAL, pConfig->DataIn2ActiveTime);
  168. ret |= EMC_SetDynMemoryParameter(EMC_DYN_MEM_TWR, pConfig->WriteRecoveryTime);
  169. ret |= EMC_SetDynMemoryParameter(EMC_DYN_MEM_TRC, pConfig->Active2ActivePeriod);
  170. ret |= EMC_SetDynMemoryParameter(EMC_DYN_MEM_TRFC, pConfig->AutoRefrehPeriod);
  171. ret |= EMC_SetDynMemoryParameter(EMC_DYN_MEM_TXSR, pConfig->ExitSelfRefreshTime);
  172. ret |= EMC_SetDynMemoryParameter(EMC_DYN_MEM_TRRD, pConfig->ActiveBankLatency);
  173. ret |= EMC_SetDynMemoryParameter(EMC_DYN_MEM_TMRD, pConfig->LoadModeReg2Active);
  174. ret |= EMC_DynMemRAS(pConfig->CSn,pConfig->RASLatency);
  175. ret |= EMC_DynMemCAS(pConfig->CSn,pConfig->CASLatency);
  176. ret |= EMC_DynMemConfigMD(pConfig->CSn,EMC_DYNAMIC_CFG_MEMDEV_SDRAM);
  177. ret |= EMC_DynMemConfigAM(pConfig->CSn, pConfig->AddrBusWidth, pConfig->AddrMap, pConfig->DataWidth, pConfig->ChipSize);
  178. return ret;
  179. }
  180. /*********************************************************************//**
  181. * @brief Initialize external static memory
  182. * @param[in] pConfig Configuration
  183. * @return EMC_FUNC_OK/EMC_FUNC_INVALID_PARAM/EMC_FUNC_ERR
  184. **********************************************************************/
  185. EMC_FUNC_CODE StaticMem_Init(EMC_STATIC_MEM_Config_Type* pConfig)
  186. {
  187. uint32_t i;
  188. EMC_FUNC_CODE ret = EMC_FUNC_OK;
  189. /* Pin configuration:
  190. * P4.30 - /EMC_CS0
  191. * P4.31 - /EMC_CS1
  192. * P2.14 - /EMC_CS2
  193. * P2.15 - /EMC_CS3
  194. *
  195. *
  196. * P3.0-P3.31 - EMC_D[0-31]
  197. * P4.0-P4.23 - EMC_A[0-23]
  198. * P5.0-P5.1 - EMC_A[24-25]
  199. *
  200. * P4.24 - /EMC_OE
  201. * P4.25 - /EMC_WE
  202. *
  203. */
  204. PINSEL_ConfigPin(2,14,1);
  205. PINSEL_ConfigPin(2,15,1);
  206. for(i = 0; i < 32; i++)
  207. {
  208. PINSEL_ConfigPin(3,i,1);
  209. PINSEL_ConfigPin(4,i,1);
  210. }
  211. PINSEL_ConfigPin(5,0,1);
  212. PINSEL_ConfigPin(5,1,1);
  213. // Power On
  214. ret |= EMC_PwrOn();
  215. // Configuration
  216. if(pConfig->AddressMirror)
  217. {
  218. LPC_EMC->Control |= EMC_Control_M;
  219. }
  220. ret |= EMC_StaMemConfigMW(pConfig->CSn,pConfig->DataWidth);
  221. if(pConfig->PageMode)
  222. ret |= EMC_StaMemConfigPM(pConfig->CSn,EMC_CFG_PM_ASYNC_ENABLE);
  223. else
  224. ret |= EMC_StaMemConfigPM(pConfig->CSn,EMC_CFG_PM_DISABLE);
  225. if(pConfig->ByteLane)
  226. ret |= EMC_StaMemConfigPB(pConfig->CSn, EMC_CFG_BYTELAND_READ_BITSLOW);
  227. else
  228. ret |= EMC_StaMemConfigPB(pConfig->CSn, EMC_CFG_BYTELAND_READ_BITSHIGH);
  229. if(pConfig->ExtendedWait)
  230. ret |= EMC_StaMemConfigEW(pConfig->CSn,EMC_CFG_EW_ENABLED);
  231. else
  232. ret |= EMC_StaMemConfigEW(pConfig->CSn,EMC_CFG_EW_DISABLED);
  233. // Timing
  234. ret |= EMC_SetStaMemoryParameter(pConfig->CSn,EMC_STA_MEM_WAITWEN, pConfig->WaitWEn);
  235. ret |= EMC_SetStaMemoryParameter(pConfig->CSn,EMC_STA_MEM_WAITOEN, pConfig->WaitOEn);
  236. ret |= EMC_SetStaMemoryParameter(pConfig->CSn,EMC_STA_MEM_WAITRD, pConfig->WaitRd);
  237. ret |= EMC_SetStaMemoryParameter(pConfig->CSn,EMC_STA_MEM_WAITPAGE, pConfig->WaitPage);
  238. ret |= EMC_SetStaMemoryParameter(pConfig->CSn,EMC_STA_MEM_WAITWR, pConfig->WaitWr);
  239. ret |= EMC_SetStaMemoryParameter(pConfig->CSn,EMC_STA_MEM_WAITTURN, pConfig->WaitTurn);
  240. return ret;
  241. }
  242. /*********************************************************************//**
  243. * @brief EMC initialize (power on block, config EMC pins).
  244. * @param[in] None
  245. * @return None
  246. **********************************************************************/
  247. EMC_FUNC_CODE EMC_Init(void)
  248. {
  249. uint8_t i;
  250. // If CPU clock is > 80 MHz, then divide it by two to create the EMC clock
  251. if(CLKPWR_GetCLK(CLKPWR_CLKTYPE_CPU) > 80000000) {
  252. CLKPWR_SetCLKDiv(CLKPWR_CLKTYPE_EMC, 1); // CPU clock / 2
  253. } else {
  254. CLKPWR_SetCLKDiv(CLKPWR_CLKTYPE_EMC, 0); // Same clock as CPU
  255. }
  256. LPC_SC->PCONP |= 0x00000800;
  257. LPC_SC->EMCDLYCTL = 0x00001010;
  258. LPC_EMC->Control = 0x00000001;
  259. LPC_EMC->Config = 0x00000000;
  260. /* Pin configuration:
  261. * P2.14 - /EMC_CS2
  262. * P2.15 - /EMC_CS3
  263. *
  264. * P2.16 - /EMC_CAS
  265. * P2.17 - /EMC_RAS
  266. * P2.18 - EMC_CLK[0]
  267. * P2.19 - EMC_CLK[1]
  268. *
  269. * P2.20 - EMC_DYCS0
  270. * P2.21 - EMC_DYCS1
  271. * P2.22 - EMC_DYCS2
  272. * P2.23 - EMC_DYCS3
  273. *
  274. * P2.24 - EMC_CKE0
  275. * P2.25 - EMC_CKE1
  276. * P2.26 - EMC_CKE2
  277. * P2.27 - EMC_CKE3
  278. *
  279. * P2.28 - EMC_DQM0
  280. * P2.29 - EMC_DQM1
  281. * P2.30 - EMC_DQM2
  282. * P2.31 - EMC_DQM3
  283. *
  284. * P3.0-P3.31 - EMC_D[0-31]
  285. * P4.0-P4.23 - EMC_A[0-23]
  286. * P5.0-P5.1 - EMC_A[24-25]
  287. *
  288. * P4.24 - /EMC_OE
  289. * P4.25 - /EMC_WE
  290. *
  291. * P4.30 - /EMC_CS0
  292. * P4.31 - /EMC_CS1
  293. */
  294. PINSEL_ConfigPin(2,14,1);
  295. PINSEL_ConfigPin(2,15,1);
  296. PINSEL_ConfigPin(2,16,1);
  297. PINSEL_ConfigPin(2,17,1);
  298. PINSEL_ConfigPin(2,18,1);
  299. PINSEL_ConfigPin(2,19,1);
  300. PINSEL_ConfigPin(2,20,1);
  301. PINSEL_ConfigPin(2,21,1);
  302. PINSEL_ConfigPin(2,22,1);
  303. PINSEL_ConfigPin(2,23,1);
  304. PINSEL_ConfigPin(2,24,1);
  305. PINSEL_ConfigPin(2,25,1);
  306. PINSEL_ConfigPin(2,26,1);
  307. PINSEL_ConfigPin(2,27,1);
  308. PINSEL_ConfigPin(2,28,1);
  309. PINSEL_ConfigPin(2,29,1);
  310. PINSEL_ConfigPin(2,30,1);
  311. PINSEL_ConfigPin(2,31,1);
  312. for(i = 0; i < 32; i++)
  313. {
  314. PINSEL_ConfigPin(3,i,1);
  315. PINSEL_ConfigPin(4,i,1);
  316. }
  317. PINSEL_ConfigPin(5,0,1);
  318. PINSEL_ConfigPin(5,1,1);
  319. return EMC_FUNC_OK;
  320. }
  321. /*********************************************************************//**
  322. * @brief Configure Little Endian/Big Endian mode for EMC
  323. *
  324. * @param[in] endia_mode Endian mode, should be:
  325. *
  326. * - EMC_Config_Little_Endian_Mode: Little-endian mode
  327. *
  328. * - EMC_Config_Big_Endian_Mode : Big-endian mode
  329. *
  330. * @return EMC_FUNC_OK
  331. **********************************************************************/
  332. EMC_FUNC_CODE EMC_ConfigEndianMode(uint32_t endian_mode)
  333. {
  334. LPC_EMC->Config &= ~(EMC_Config_Endian_Mask);
  335. LPC_EMC->Config |= (endian_mode&EMC_Config_Endian_Mask);
  336. return EMC_FUNC_OK;
  337. }
  338. /****************** Group of Dynamic control functions************************/
  339. /*********************************************************************//**
  340. * @brief Set the dsvalue for dynamic clock enable bit
  341. *
  342. * @param[in] clock_enable clock enable mode, should be:
  343. *
  344. * - EMC_DYNAMIC_CTRL_CE_SAVEPWR: Clock enable of idle devices
  345. * are deasserted to save power
  346. *
  347. * - EMC_DYNAMIC_CTRL_CE_ALLCLK_HI: All clock enables are driven
  348. * HIGH continuously
  349. *
  350. * @return EMC_FUNC_CODE
  351. **********************************************************************/
  352. EMC_FUNC_CODE EMC_DynCtrlClockEnable(uint32_t clock_enable)
  353. {
  354. LPC_EMC->DynamicControl &= ~(EMC_DYNAMIC_CTRL_MEMCLK_EN_BMASK);
  355. LPC_EMC->DynamicControl |= clock_enable & EMC_DYNAMIC_CTRL_MEMCLK_EN_BMASK;
  356. return EMC_FUNC_OK;
  357. }
  358. /*********************************************************************//**
  359. * @brief Set the value for dynamic memory clock control: stops or
  360. * runs continuously
  361. *
  362. * @param[in] clock_control clock control mode, should be:
  363. *
  364. * - EMC_DYNAMIC_CTRL_CS_CLKOUT_STOP: CLKOUT stops when all
  365. * SDRAMs are idle and during self-refresh mode
  366. *
  367. * - EMC_DYNAMIC_CTRL_CS_CLKOUT_CONT: CLKOUT runs continuously
  368. *
  369. * @return EMC_FUNC_OK
  370. **********************************************************************/
  371. EMC_FUNC_CODE EMC_DynCtrlClockControl(int32_t clock_control)
  372. {
  373. LPC_EMC->DynamicControl &= ~EMC_DYNAMIC_CTRL_CLKCTRL_BMASK;
  374. LPC_EMC->DynamicControl |= clock_control & EMC_DYNAMIC_CTRL_CLKCTRL_BMASK;
  375. return EMC_FUNC_OK;
  376. }
  377. /*********************************************************************//**
  378. * @brief Switch the Self-refresh mode between normal and self-refresh mode
  379. *
  380. * @param[in] self_refresh_mode self refresh mode, should be:
  381. *
  382. * - EMC_DYNAMIC_CTRL_SR_NORMALMODE: Normal mode
  383. *
  384. * - EMC_DYNAMIC_CTRL_SR_SELFREFRESH: Enter self-refresh mode
  385. *
  386. * @return EMC_FUNC_OK
  387. **********************************************************************/
  388. EMC_FUNC_CODE EMC_DynCtrlSelfRefresh(uint32_t self_refresh_mode)
  389. {
  390. LPC_EMC->DynamicControl &= ~EMC_DYNAMIC_CTRL_SELFREFRESH_REQ_BMASK;
  391. LPC_EMC->DynamicControl =self_refresh_mode & EMC_DYNAMIC_CTRL_SELFREFRESH_REQ_BMASK;
  392. return EMC_FUNC_OK;
  393. }
  394. /*********************************************************************//**
  395. * @brief Enable/disable CLKOUT
  396. *
  397. * @param[in] MMC_val Memory clock control mode, should be:
  398. *
  399. * - EMC_DYNAMIC_CTRL_MMC_CLKOUT_ENABLED: CLKOUT enabled
  400. *
  401. * - EMC_DYNAMIC_CTRL_MMC_CLKOUT_DISABLED: CLKOUT disabled
  402. *
  403. * @return EMC_FUNC_OK
  404. **********************************************************************/
  405. EMC_FUNC_CODE EMC_DynCtrlMMC(uint32_t MMC_val)
  406. {
  407. LPC_EMC->DynamicControl &= ~EMC_DYNAMIC_CTRL_MMC_CLKOUTCTRL_BMASK;
  408. LPC_EMC->DynamicControl |=MMC_val & EMC_DYNAMIC_CTRL_MMC_CLKOUTCTRL_BMASK;
  409. return EMC_FUNC_OK;
  410. }
  411. /*********************************************************************//**
  412. * @brief Issue SDRAM command
  413. *
  414. * @param[in] SDRAM_command Command mode, should be:
  415. *
  416. * - EMC_DYNAMIC_CTRL_SDRAM_NORMAL: Issue SDRAM NORMAL operation command
  417. *
  418. * - EMC_DYNAMIC_CTRL_SDRAM_MODE: Issue SDRAM MODE command
  419. *
  420. * - EMC_DYNAMIC_CTRL_SDRAM_PALL: Issue SDRAM PALL (precharge all) command
  421. *
  422. * - EMC_DYNAMIC_CTRL_SDRAM_NOP: Issue SRAM NOP (no operation) command
  423. *
  424. * @return EMC_FUNC_OK
  425. **********************************************************************/
  426. EMC_FUNC_CODE EMC_DynCtrlSDRAMInit(uint32_t SDRAM_command)
  427. {
  428. LPC_EMC->DynamicControl &= ~EMC_DYNAMIC_CTRL_SDRAM_INIT_BMASK;
  429. LPC_EMC->DynamicControl |= SDRAM_command & EMC_DYNAMIC_CTRL_SDRAM_INIT_BMASK;
  430. return EMC_FUNC_OK;
  431. }
  432. /*********************************************************************//**
  433. * @brief Switch between Normal operation and deep sleep power mode
  434. *
  435. * @param[in] Power_command Low-power SDRAM deep-sleep mode, should be:
  436. *
  437. * - EMC_DYNAMIC_CTRL_DP_NORMAL: Normal operation
  438. *
  439. * - EMC_DYNAMIC_CTRL_DP_DEEPSLEEP: Enter deep-sleep mode
  440. *
  441. * @return EMC_FUNC_OK
  442. **********************************************************************/
  443. EMC_FUNC_CODE EMC_DynCtrlPowerDownMode(uint32_t Power_command)
  444. {
  445. LPC_EMC->DynamicControl &= ~EMC_DYNAMIC_CTRL_SDRAM_PWRMODE_BMASK;
  446. LPC_EMC->DynamicControl |= Power_command & EMC_DYNAMIC_CTRL_SDRAM_PWRMODE_BMASK;
  447. return EMC_FUNC_OK;
  448. }
  449. /*********************************************************************//**
  450. * @brief Set the value of EMC dynamic memory registers
  451. *
  452. * @param[in] par EMC register that will set value, should be:
  453. * - EMC_DYN_MEM_REFRESH_TIMER: Dynamic Refresh register
  454. * - EMC_DYN_MEM_READ_CONFIG: Dynamic Read Config register
  455. * - EMC_DYN_MEM_TRP: Dynamic RP register
  456. * - EMC_DYN_MEM_TRAS: Dynamic RAS register
  457. * - EMC_DYN_MEM_TSREX: Dynamic SREX register
  458. * - EMC_DYN_MEM_TAPR: Dynamic APR register
  459. * - EMC_DYN_MEM_TDAL: Dynamic DAL register
  460. * - EMC_DYN_MEM_TWR: Dynamic WR register
  461. * - EMC_DYN_MEM_TRC: Dynamic RC register
  462. * - EMC_DYN_MEM_TRFC: Dynamic RFC register
  463. * - EMC_DYN_MEM_TXSR: Dynamic XSR register
  464. * - EMC_DYN_MEM_TRRD: Dynamic RRD register
  465. * - EMC_DYN_MEM_TMRD: Dynamic MRD register
  466. *
  467. * @return EMC_FUNC_OK/EMC_FUNC_INVALID_PARAM
  468. **********************************************************************/
  469. EMC_FUNC_CODE EMC_SetDynMemoryParameter(EMC_DYN_MEM_PAR par, uint32_t val)
  470. {
  471. switch ( par)
  472. {
  473. case EMC_DYN_MEM_REFRESH_TIMER:
  474. LPC_EMC->DynamicRefresh = EMC_DynamicRefresh_REFRESH(val);
  475. break;
  476. case EMC_DYN_MEM_READ_CONFIG:
  477. LPC_EMC->DynamicReadConfig = EMC_DynamicReadConfig_RD(val);
  478. break;
  479. case EMC_DYN_MEM_TRP:
  480. LPC_EMC->DynamicRP = EMC_DynamictRP_tRP(val);
  481. break;
  482. case EMC_DYN_MEM_TRAS:
  483. LPC_EMC->DynamicRAS = EMC_DynamictRP_tRAS(val);
  484. break;
  485. case EMC_DYN_MEM_TSREX:
  486. LPC_EMC->DynamicSREX = EMC_DynamictRP_tSREX(val);
  487. break;
  488. case EMC_DYN_MEM_TAPR:
  489. LPC_EMC->DynamicAPR = EMC_DynamictAPR_tAPR(val);
  490. break;
  491. case EMC_DYN_MEM_TDAL:
  492. LPC_EMC->DynamicDAL =EMC_DynamictDAL_tDAL(val);
  493. break;
  494. case EMC_DYN_MEM_TWR:
  495. LPC_EMC->DynamicWR = EMC_DynamictWR_tWR(val);
  496. break;
  497. case EMC_DYN_MEM_TRC:
  498. LPC_EMC->DynamicRC = EMC_DynamictRC_tRC(val);
  499. break;
  500. case EMC_DYN_MEM_TRFC:
  501. LPC_EMC->DynamicRFC = EMC_DynamictRFC_tRFC(val);
  502. break;
  503. case EMC_DYN_MEM_TXSR:
  504. LPC_EMC->DynamicXSR = EMC_DynamictXSR_tXSR(val);
  505. break;
  506. case EMC_DYN_MEM_TRRD:
  507. LPC_EMC->DynamicRRD = EMC_DynamictRRD_tRRD(val);
  508. break;
  509. case EMC_DYN_MEM_TMRD:
  510. LPC_EMC->DynamicMRD = EMC_DynamictMRD_tMRD(val);
  511. break;
  512. default:
  513. return EMC_FUNC_INVALID_PARAM;
  514. }
  515. return EMC_FUNC_OK;
  516. }
  517. /*********************************************************************//**
  518. * @brief Configure the memory device
  519. *
  520. * @param[in] index index number, should be from 0 to 3
  521. *
  522. * @param[in] mem_dev Memory device, should be:
  523. *
  524. * - EMC_DYNAMIC_CFG_MEMDEV_SDRAM: SDRAM
  525. *
  526. * - EMC_DYNAMIC_CFG_MEMDEV_LOWPWR_SDRAM: Low-power SDRAM
  527. *
  528. *
  529. * @return EMC_FUNC_OK/EMC_FUNC_INVALID_PARAM
  530. **********************************************************************/
  531. EMC_FUNC_CODE EMC_DynMemConfigMD(uint32_t index , uint32_t mem_dev)
  532. {
  533. switch (index)
  534. {
  535. case 0:
  536. LPC_EMC->DynamicConfig0 &= ~EMC_DYNAMIC_CFG_MEMDEV_BMASK;
  537. LPC_EMC->DynamicConfig0 |= mem_dev & EMC_DYNAMIC_CFG_MEMDEV_BMASK;
  538. break;
  539. case 1:
  540. LPC_EMC->DynamicConfig1 &= ~EMC_DYNAMIC_CFG_MEMDEV_BMASK;
  541. LPC_EMC->DynamicConfig1 |= mem_dev & EMC_DYNAMIC_CFG_MEMDEV_BMASK;
  542. break;
  543. case 2:
  544. LPC_EMC->DynamicConfig2 &= ~EMC_DYNAMIC_CFG_MEMDEV_BMASK;
  545. LPC_EMC->DynamicConfig2 |= mem_dev & EMC_DYNAMIC_CFG_MEMDEV_BMASK;
  546. break;
  547. case 3:
  548. LPC_EMC->DynamicConfig3 &= ~EMC_DYNAMIC_CFG_MEMDEV_BMASK;
  549. LPC_EMC->DynamicConfig3 |= mem_dev & EMC_DYNAMIC_CFG_MEMDEV_BMASK;
  550. break;
  551. default:
  552. return EMC_FUNC_INVALID_PARAM;
  553. }
  554. return EMC_FUNC_OK;
  555. }
  556. /*********************************************************************//**
  557. * @brief Map the address for the memory device
  558. *
  559. * @param[in] index index number, should be from 0 to 3
  560. *
  561. * @param[in] add_mapped address where the memory will be mapped
  562. *
  563. * @return EMC_FUNC_OK
  564. **********************************************************************/
  565. EMC_FUNC_CODE EMC_DynMemConfigAM(uint32_t index ,
  566. uint8_t addr_bus_width, uint8_t addr_map,
  567. uint8_t data_bus_width,
  568. uint16_t chip_size)
  569. {
  570. const int chip_max_size = 512; // 512Mb
  571. uint8_t data_bus_max_size = 0;
  572. uint32_t add_mapped_p1 = 0x00, add_mapped_p2 = 0x00, add_mapped_p3 = 0x00;
  573. uint32_t tmp = 16, i = 0, j = 0;
  574. /* Get part 3 of address map */
  575. switch(addr_bus_width)
  576. {
  577. case 16:
  578. add_mapped_p3 = 0;
  579. data_bus_max_size = 16;
  580. break;
  581. case 32:
  582. add_mapped_p3 = 1;
  583. data_bus_max_size = 32;
  584. break;
  585. default:
  586. return EMC_FUNC_INVALID_PARAM;
  587. }
  588. /* Get part 2 of address map */
  589. add_mapped_p2 = EMC_DYNAMIC_CFG_ADD_MAP_P2(addr_map);
  590. /* Get part 1 of address map */
  591. if(chip_size == 16)
  592. {
  593. if(data_bus_width == 8)
  594. add_mapped_p1 = 0;
  595. else if(data_bus_width == 16)
  596. add_mapped_p1 = 1;
  597. else
  598. return EMC_FUNC_INVALID_PARAM;
  599. }
  600. else
  601. {
  602. while(1)
  603. {
  604. i++;
  605. tmp = 16*(0x01 << (i+1));
  606. if(tmp == chip_size)
  607. {
  608. for(j = 0; (8<<j)<=data_bus_max_size;j++)
  609. {
  610. if((8<<j) == data_bus_width)
  611. break;
  612. }
  613. if( (8<<j) > data_bus_max_size)
  614. return EMC_FUNC_INVALID_PARAM;
  615. add_mapped_p1 = (i<<2) + j;
  616. break;
  617. }
  618. if(tmp >= chip_max_size)
  619. {
  620. return EMC_FUNC_INVALID_PARAM;
  621. }
  622. }
  623. }
  624. switch ( index)
  625. {
  626. case 0:
  627. LPC_EMC->DynamicConfig0 &= ~EMC_DYNAMIC_CFG_ADD_MAP_P1_MASK;
  628. LPC_EMC->DynamicConfig0 |= EMC_DYNAMIC_CFG_ADD_MAP_P1(add_mapped_p1);
  629. LPC_EMC->DynamicConfig0 &= ~EMC_DYNAMIC_CFG_ADD_MAP_P2_MASK;
  630. LPC_EMC->DynamicConfig0 |= EMC_DYNAMIC_CFG_ADD_MAP_P2(add_mapped_p2);
  631. LPC_EMC->DynamicConfig0 &= ~EMC_DYNAMIC_CFG_ADD_MAP_P3_MASK;
  632. LPC_EMC->DynamicConfig0 |= EMC_DYNAMIC_CFG_ADD_MAP_P3(add_mapped_p3);
  633. break;
  634. case 1:
  635. LPC_EMC->DynamicConfig1 &= ~EMC_DYNAMIC_CFG_ADD_MAP_P1_MASK;
  636. LPC_EMC->DynamicConfig1 |= EMC_DYNAMIC_CFG_ADD_MAP_P1(add_mapped_p1);
  637. LPC_EMC->DynamicConfig1 &= ~EMC_DYNAMIC_CFG_ADD_MAP_P2_MASK;
  638. LPC_EMC->DynamicConfig1 |= EMC_DYNAMIC_CFG_ADD_MAP_P2(add_mapped_p2);
  639. LPC_EMC->DynamicConfig1 &= ~EMC_DYNAMIC_CFG_ADD_MAP_P3_MASK;
  640. LPC_EMC->DynamicConfig1 |= EMC_DYNAMIC_CFG_ADD_MAP_P3(add_mapped_p3);
  641. break;
  642. case 2:
  643. LPC_EMC->DynamicConfig2 &= ~EMC_DYNAMIC_CFG_ADD_MAP_P1_MASK;
  644. LPC_EMC->DynamicConfig2 |= EMC_DYNAMIC_CFG_ADD_MAP_P1(add_mapped_p1);
  645. LPC_EMC->DynamicConfig2 &= ~EMC_DYNAMIC_CFG_ADD_MAP_P2_MASK;
  646. LPC_EMC->DynamicConfig2 |= EMC_DYNAMIC_CFG_ADD_MAP_P2( add_mapped_p2);
  647. LPC_EMC->DynamicConfig2 &= ~EMC_DYNAMIC_CFG_ADD_MAP_P3_MASK;
  648. LPC_EMC->DynamicConfig2 |= EMC_DYNAMIC_CFG_ADD_MAP_P3(add_mapped_p3);
  649. break;
  650. case 3:
  651. LPC_EMC->DynamicConfig3 &= ~EMC_DYNAMIC_CFG_ADD_MAP_P1_MASK;
  652. LPC_EMC->DynamicConfig3 |= EMC_DYNAMIC_CFG_ADD_MAP_P1(add_mapped_p1);
  653. LPC_EMC->DynamicConfig3 &= ~EMC_DYNAMIC_CFG_ADD_MAP_P2_MASK;
  654. LPC_EMC->DynamicConfig3 |= EMC_DYNAMIC_CFG_ADD_MAP_P2(add_mapped_p2);
  655. LPC_EMC->DynamicConfig3 &= ~EMC_DYNAMIC_CFG_ADD_MAP_P3_MASK;
  656. LPC_EMC->DynamicConfig3 |= EMC_DYNAMIC_CFG_ADD_MAP_P3(add_mapped_p3);
  657. break;
  658. default:
  659. return EMC_FUNC_INVALID_PARAM;
  660. }
  661. return EMC_FUNC_OK;
  662. }
  663. /*********************************************************************//**
  664. * @brief Enable/disable the buffer
  665. *
  666. * @param[in] index index number, should be from 0 to 3
  667. *
  668. * @param[in] buff_control buffer control mode, should be:
  669. *
  670. * - EMC_DYNAMIC_CFG_BUFF_DISABLED: buffer is disabled
  671. *
  672. * - EMC_DYNAMIC_CFG_BUFF_ENABLED: buffer is enable
  673. *
  674. * @return EMC_FUNC_OK/EMC_FUNC_INVALID_PARAM
  675. **********************************************************************/
  676. EMC_FUNC_CODE EMC_DynMemConfigB(uint32_t index , uint32_t buff_control)
  677. {
  678. switch ( index)
  679. {
  680. case 0:
  681. LPC_EMC->DynamicConfig0 &= ~EMC_DYNAMIC_CFG_BUFFENABLE_BMASK;
  682. LPC_EMC->DynamicConfig0 |= buff_control & EMC_DYNAMIC_CFG_BUFFENABLE_BMASK;
  683. break;
  684. case 1:
  685. LPC_EMC->DynamicConfig1 &= ~EMC_DYNAMIC_CFG_BUFFENABLE_BMASK;
  686. LPC_EMC->DynamicConfig1 |= buff_control& EMC_DYNAMIC_CFG_BUFFENABLE_BMASK;
  687. break;
  688. case 2:
  689. LPC_EMC->DynamicConfig2 &= ~EMC_DYNAMIC_CFG_BUFFENABLE_BMASK;
  690. LPC_EMC->DynamicConfig2 |= buff_control& EMC_DYNAMIC_CFG_BUFFENABLE_BMASK;
  691. break;
  692. case 3:
  693. LPC_EMC->DynamicConfig3 &= ~EMC_DYNAMIC_CFG_BUFFENABLE_BMASK;
  694. LPC_EMC->DynamicConfig3|= buff_control& EMC_DYNAMIC_CFG_BUFFENABLE_BMASK;
  695. break;
  696. default:
  697. return EMC_FUNC_INVALID_PARAM;
  698. }
  699. return EMC_FUNC_OK;
  700. }
  701. /*********************************************************************//**
  702. * @brief Configure write permission: protect or not
  703. *
  704. * @param[in] index index number, should be from 0 to 3
  705. *
  706. * @param[in] permission permission mode, should be:
  707. *
  708. * - EMC_DYNAMIC_CFG_WR_UNPROTECTED: will not protect
  709. *
  710. * - EMC_DYNAMIC_CFG_WR_PROTECTED: will protect
  711. *
  712. * @return EMC_FUNC_OK
  713. **********************************************************************/
  714. EMC_FUNC_CODE EMC_DynMemConfigP(uint32_t index , uint32_t permission)
  715. {
  716. switch ( index)
  717. {
  718. case 0:
  719. LPC_EMC->DynamicConfig0 &= ~ EMC_DYNAMIC_CFG_WRPROTECT_BMASK;
  720. LPC_EMC->DynamicConfig0 |= permission&EMC_DYNAMIC_CFG_WRPROTECT_BMASK;
  721. break;
  722. case 1:
  723. LPC_EMC->DynamicConfig1 &= ~ EMC_DYNAMIC_CFG_WRPROTECT_BMASK;
  724. LPC_EMC->DynamicConfig1 |= permission&EMC_DYNAMIC_CFG_WRPROTECT_BMASK;
  725. break;
  726. case 2:
  727. LPC_EMC->DynamicConfig2 &= ~ EMC_DYNAMIC_CFG_WRPROTECT_BMASK;
  728. LPC_EMC->DynamicConfig2 |= permission&EMC_DYNAMIC_CFG_WRPROTECT_BMASK;
  729. break;
  730. case 3:
  731. LPC_EMC->DynamicConfig3 &= ~ EMC_DYNAMIC_CFG_WRPROTECT_BMASK;
  732. LPC_EMC->DynamicConfig3 |= permission&EMC_DYNAMIC_CFG_WRPROTECT_BMASK;
  733. break;
  734. default:
  735. return EMC_FUNC_INVALID_PARAM;
  736. }
  737. return EMC_FUNC_OK;
  738. }
  739. /*********************************************************************//**
  740. * @brief Set value for RAS latency
  741. *
  742. * @param[in] index index number, should be from 0 to 3
  743. *
  744. * @param[in] ras_val RAS value should be in range: 0..3
  745. *
  746. * @return EMC_FUNC_OK
  747. **********************************************************************/
  748. EMC_FUNC_CODE EMC_DynMemRAS(uint32_t index , uint32_t ras_val)
  749. {
  750. switch ( index)
  751. {
  752. case 0:
  753. LPC_EMC->DynamicRasCas0 &= ~EMC_DYNAMIC_RASCAS_RASCFG_BMASK;
  754. LPC_EMC->DynamicRasCas0 |=( ras_val << EMC_DYNAMIC_RASCAS_RASCFG_POS)
  755. &EMC_DYNAMIC_RASCAS_RASCFG_BMASK;
  756. break;
  757. case 1:
  758. LPC_EMC->DynamicRasCas1 &= ~EMC_DYNAMIC_RASCAS_RASCFG_BMASK;
  759. LPC_EMC->DynamicRasCas1 |= ( ras_val << EMC_DYNAMIC_RASCAS_RASCFG_POS)
  760. &EMC_DYNAMIC_RASCAS_RASCFG_BMASK;
  761. break;
  762. case 2:
  763. LPC_EMC->DynamicRasCas2 &= ~EMC_DYNAMIC_RASCAS_RASCFG_BMASK;
  764. LPC_EMC->DynamicRasCas2 |= ( ras_val << EMC_DYNAMIC_RASCAS_RASCFG_POS)
  765. &EMC_DYNAMIC_RASCAS_RASCFG_BMASK;
  766. break;
  767. case 3:
  768. LPC_EMC->DynamicRasCas3 &= ~EMC_DYNAMIC_RASCAS_RASCFG_BMASK;
  769. LPC_EMC->DynamicRasCas3 |= ( ras_val << EMC_DYNAMIC_RASCAS_RASCFG_POS)
  770. &EMC_DYNAMIC_RASCAS_RASCFG_BMASK;
  771. break;
  772. default:
  773. return EMC_FUNC_INVALID_PARAM;
  774. }
  775. return EMC_FUNC_OK;
  776. }
  777. /*********************************************************************//**
  778. * @brief Set value for CAS latency
  779. *
  780. * @param[in] index index number, should be from 0 to 3
  781. *
  782. * @param[in] ras_val CAS value should be in range: 0..3
  783. *
  784. * @return EMC_FUNC_OK/EMC_FUNC_INVALID_PARAM
  785. **********************************************************************/
  786. EMC_FUNC_CODE EMC_DynMemCAS(uint32_t index , uint32_t cas_val)
  787. {
  788. switch ( index)
  789. {
  790. case 0:
  791. LPC_EMC->DynamicRasCas0 &= ~EMC_DYNAMIC_RASCAS_CASCFG_BMASK;
  792. LPC_EMC->DynamicRasCas0 |= (cas_val<<EMC_DYNAMIC_RASCAS_CASCFG_POS)
  793. &EMC_DYNAMIC_RASCAS_CASCFG_BMASK;
  794. break;
  795. case 1:
  796. LPC_EMC->DynamicRasCas1 &= ~EMC_DYNAMIC_RASCAS_CASCFG_BMASK;
  797. LPC_EMC->DynamicRasCas1 |= (cas_val<<EMC_DYNAMIC_RASCAS_CASCFG_POS)
  798. &EMC_DYNAMIC_RASCAS_CASCFG_BMASK;
  799. break;
  800. case 2:
  801. LPC_EMC->DynamicRasCas2 &= ~EMC_DYNAMIC_RASCAS_CASCFG_BMASK;
  802. LPC_EMC->DynamicRasCas2 |= (cas_val<<EMC_DYNAMIC_RASCAS_CASCFG_POS)
  803. &EMC_DYNAMIC_RASCAS_CASCFG_BMASK;
  804. break;
  805. case 3:
  806. LPC_EMC->DynamicRasCas3 &= ~EMC_DYNAMIC_RASCAS_CASCFG_BMASK;
  807. LPC_EMC->DynamicRasCas3 |= (cas_val<<EMC_DYNAMIC_RASCAS_CASCFG_POS)
  808. &EMC_DYNAMIC_RASCAS_CASCFG_BMASK;
  809. break;
  810. default:
  811. return EMC_FUNC_INVALID_PARAM;
  812. }
  813. return EMC_FUNC_OK;
  814. }
  815. /*********************************************************************//**
  816. * @brief Set extended wait time out for accessing static memory
  817. *
  818. * @param[in] Extended_wait_time_out timeout value that will be set
  819. *
  820. * @return EMC_FUNC_OK
  821. **********************************************************************/
  822. EMC_FUNC_CODE EMC_StaticExtendedWait(uint32_t Extended_wait_time_out)
  823. {
  824. LPC_EMC->StaticExtendedWait = EMC_StaticExtendedWait_EXTENDEDWAIT(Extended_wait_time_out);
  825. return EMC_FUNC_OK;
  826. }
  827. /*********************************************************************//**
  828. * @brief Configure the memory width
  829. *
  830. * @param[in] index index number, should be from 0 to 3
  831. *
  832. * @param[in] mem_width memory width, should be:
  833. *
  834. * - EMC_STATIC_CFG_MW_8BITS: 8-bits
  835. *
  836. * - EMC_STATIC_CFG_MW_16BITS: 16-bits
  837. *
  838. * - EMC_STATIC_CFG_MW_32BITS02: 32-bits
  839. *
  840. * @return EMC_FUNC_OK/EMC_FUNC_INVALID_PARAM
  841. **********************************************************************/
  842. EMC_FUNC_CODE EMC_StaMemConfigMW(uint32_t index , uint32_t mem_width)
  843. {
  844. uint32_t mem_width_flg = 0;
  845. switch(mem_width)
  846. {
  847. case 8:
  848. mem_width_flg = EMC_STATIC_CFG_MW_8BITS;
  849. break;
  850. case 16:
  851. mem_width_flg = EMC_STATIC_CFG_MW_16BITS;
  852. break;
  853. case 32:
  854. mem_width_flg = EMC_STATIC_CFG_MW_32BITS;
  855. break;
  856. default:
  857. return EMC_FUNC_INVALID_PARAM;
  858. }
  859. switch ( index)
  860. {
  861. case 0:
  862. LPC_EMC->StaticConfig0 &= ~ EMC_STATIC_CFG_MEMWIDTH_BMASK;
  863. LPC_EMC->StaticConfig0 |= mem_width_flg;
  864. break;
  865. case 1:
  866. LPC_EMC->StaticConfig1 &= ~ EMC_STATIC_CFG_MEMWIDTH_BMASK;
  867. LPC_EMC->StaticConfig1 |= mem_width_flg;
  868. break;
  869. case 2:
  870. LPC_EMC->StaticConfig2 &= ~ EMC_STATIC_CFG_MEMWIDTH_BMASK;
  871. LPC_EMC->StaticConfig2 |= mem_width_flg;
  872. break;
  873. case 3:
  874. LPC_EMC->StaticConfig3 &= ~ EMC_STATIC_CFG_MEMWIDTH_BMASK;
  875. LPC_EMC->StaticConfig3 |= mem_width_flg;
  876. break;
  877. default:
  878. return EMC_FUNC_INVALID_PARAM;
  879. }
  880. return EMC_FUNC_OK;
  881. }
  882. /*********************************************************************//**
  883. * @brief Configure the page mode
  884. *
  885. * @param[in] index index number, should be from 0 to 3
  886. *
  887. * @param[in] page_mode page mode, should be:
  888. *
  889. * - EMC_CFG_PM_DISABLE: disable
  890. *
  891. * - EMC_CFG_PM_ASYNC_ENABLE: asynchronous page mode enable
  892. *
  893. * @return EMC_FUNC_OK/EMC_FUNC_INVALID_PARAM
  894. **********************************************************************/
  895. EMC_FUNC_CODE EMC_StaMemConfigPM(uint32_t index , uint32_t page_mode)
  896. {
  897. switch ( index)
  898. {
  899. case 0:
  900. LPC_EMC->StaticConfig0 &= ~EMC_STATIC_CFG_PAGEMODE_MASK;
  901. LPC_EMC->StaticConfig0 |= page_mode&EMC_STATIC_CFG_PAGEMODE_MASK;
  902. break;
  903. case 1:
  904. LPC_EMC->StaticConfig1 &= ~EMC_STATIC_CFG_PAGEMODE_MASK;
  905. LPC_EMC->StaticConfig1 |= page_mode&EMC_STATIC_CFG_PAGEMODE_MASK;
  906. break;
  907. case 2:
  908. LPC_EMC->StaticConfig2 &= ~EMC_STATIC_CFG_PAGEMODE_MASK;
  909. LPC_EMC->StaticConfig2 |= page_mode&EMC_STATIC_CFG_PAGEMODE_MASK;
  910. break;
  911. case 3:
  912. LPC_EMC->StaticConfig3 &= ~EMC_STATIC_CFG_PAGEMODE_MASK;
  913. LPC_EMC->StaticConfig3 |= page_mode&EMC_STATIC_CFG_PAGEMODE_MASK;
  914. break;
  915. default:
  916. return EMC_FUNC_INVALID_PARAM;
  917. }
  918. return EMC_FUNC_OK;
  919. }
  920. /*********************************************************************//**
  921. * @brief Configure the chip select polarity
  922. *
  923. * @param[in] index index number, should be from 0 to 3
  924. *
  925. * @param[in] pagepol_val_mode page mode, should be:
  926. *
  927. * - EMC_CFG_BYTELAND_PC_ACTIVE_LO: Active LOW ship select
  928. *
  929. * - EMC_CFG_BYTELAND_PC_ACTIVE_HI: Active HIGH chip select
  930. *
  931. * @return EMC_FUNC_OK/EMC_FUNC_INVALID_PARAM
  932. **********************************************************************/
  933. EMC_FUNC_CODE EMC_StaMemConfigPC(uint32_t index , uint32_t pol_val)
  934. {
  935. switch ( index)
  936. {
  937. case 0:
  938. LPC_EMC->StaticConfig0 &= ~EMC_STATIC_CFG_CHIPPOLARITY_MASK;
  939. LPC_EMC->StaticConfig0 |= pol_val&EMC_STATIC_CFG_CHIPPOLARITY_MASK;
  940. break;
  941. case 1:
  942. LPC_EMC->StaticConfig1 &= ~EMC_STATIC_CFG_CHIPPOLARITY_MASK;
  943. LPC_EMC->StaticConfig1 |= pol_val&EMC_STATIC_CFG_CHIPPOLARITY_MASK;
  944. break;
  945. case 2:
  946. LPC_EMC->StaticConfig2 &= ~EMC_STATIC_CFG_CHIPPOLARITY_MASK;
  947. LPC_EMC->StaticConfig2 |= pol_val&EMC_STATIC_CFG_CHIPPOLARITY_MASK;
  948. break;
  949. case 3:
  950. LPC_EMC->StaticConfig3 &= ~EMC_STATIC_CFG_CHIPPOLARITY_MASK;
  951. LPC_EMC->StaticConfig3 |= pol_val&EMC_STATIC_CFG_CHIPPOLARITY_MASK;
  952. break;
  953. default:
  954. return EMC_FUNC_INVALID_PARAM;
  955. }
  956. return EMC_FUNC_OK;
  957. }
  958. /*********************************************************************//**
  959. * @brief Configure the byte lane state
  960. *
  961. * @param[in] index index number, should be from 0 to 3
  962. *
  963. * @param[in] pb_val Byte lane state, should be:
  964. *
  965. * - EMC_CFG_BYTELAND_READ_BITSHIGH: For reads all bits
  966. * in BLSn[3:0] are HIGH.
  967. *
  968. * - EMC_CFG_BYTELAND_READ_BITSLOW: For reads all bits
  969. * in BLSn[3:0] are LOW.
  970. *
  971. * @return EMC_FUNC_OK/EMC_FUNC_INVALID_PARAM
  972. **********************************************************************/
  973. EMC_FUNC_CODE EMC_StaMemConfigPB(uint32_t index , uint32_t pb_val)
  974. {
  975. switch ( index)
  976. {
  977. case 0:
  978. LPC_EMC->StaticConfig0 &= ~EMC_STATIC_CFG_BYTELAND_MASK;
  979. LPC_EMC->StaticConfig0 |= pb_val&EMC_STATIC_CFG_BYTELAND_MASK;
  980. break;
  981. case 1:
  982. LPC_EMC->StaticConfig1 &= ~EMC_STATIC_CFG_BYTELAND_MASK;
  983. LPC_EMC->StaticConfig1 |= pb_val&EMC_STATIC_CFG_BYTELAND_MASK;
  984. break;
  985. case 2:
  986. LPC_EMC->StaticConfig2 &= ~EMC_STATIC_CFG_BYTELAND_MASK;
  987. LPC_EMC->StaticConfig2 |= pb_val&EMC_STATIC_CFG_BYTELAND_MASK;
  988. break;
  989. case 3:
  990. LPC_EMC->StaticConfig3 &= ~EMC_STATIC_CFG_BYTELAND_MASK;
  991. LPC_EMC->StaticConfig3 |= pb_val&EMC_STATIC_CFG_BYTELAND_MASK;
  992. break;
  993. default:
  994. return EMC_FUNC_INVALID_PARAM;
  995. }
  996. return EMC_FUNC_OK;
  997. }
  998. /*********************************************************************//**
  999. * @brief Configure the extended wait value
  1000. *
  1001. * @param[in] index index number, should be from 0 to 3
  1002. *
  1003. * @param[in] ex_wait Extended wait mode, should be:
  1004. *
  1005. * - EMC_CFG_EW_DISABLED: Extended wait disabled.
  1006. *
  1007. * - EMC_CFG_EW_ENABLED: Extended wait enabled.
  1008. *
  1009. * @return EMC_FUNC_OK/EMC_FUNC_INVALID_PARAM
  1010. **********************************************************************/
  1011. EMC_FUNC_CODE EMC_StaMemConfigEW(uint32_t index , uint32_t ex_wait)
  1012. {
  1013. switch ( index)
  1014. {
  1015. case 0:
  1016. LPC_EMC->StaticConfig0 &= ~EMC_STATIC_CFG_EXTWAIT_MASK;
  1017. LPC_EMC->StaticConfig0 |= ex_wait&EMC_STATIC_CFG_EXTWAIT_MASK;
  1018. break;
  1019. case 1:
  1020. LPC_EMC->StaticConfig1 &= ~EMC_STATIC_CFG_EXTWAIT_MASK;
  1021. LPC_EMC->StaticConfig1 |= ex_wait&EMC_STATIC_CFG_EXTWAIT_MASK;
  1022. break;
  1023. case 2:
  1024. LPC_EMC->StaticConfig2 &= ~EMC_STATIC_CFG_EXTWAIT_MASK;
  1025. LPC_EMC->StaticConfig2 |= ex_wait&EMC_STATIC_CFG_EXTWAIT_MASK;
  1026. break;
  1027. case 3:
  1028. LPC_EMC->StaticConfig3 &= ~EMC_STATIC_CFG_EXTWAIT_MASK;
  1029. LPC_EMC->StaticConfig3 |= ex_wait&EMC_STATIC_CFG_EXTWAIT_MASK;
  1030. break;
  1031. default:
  1032. return EMC_FUNC_INVALID_PARAM;
  1033. }
  1034. return EMC_FUNC_OK;
  1035. }
  1036. /*********************************************************************//**
  1037. * @brief Configure the buffer enable value
  1038. *
  1039. * @param[in] index index number, should be from 0 to 3
  1040. *
  1041. * @param[in] buf_val Buffer mode, should be:
  1042. *
  1043. * - EMC_CFG_BUF_DISABLED: Buffer disabled.
  1044. *
  1045. * - EMC_CFG_BUF_ENABLED: Buffer enabled.
  1046. *
  1047. * @return EMC_FUNC_OK/EMC_FUNC_INVALID_PARAM
  1048. **********************************************************************/
  1049. EMC_FUNC_CODE EMC_StaMemConfigB(uint32_t index , uint32_t buf_val)
  1050. {
  1051. switch ( index)
  1052. {
  1053. case 0:
  1054. LPC_EMC->StaticConfig0 &= ~EMC_STATIC_CFG_BUFENABLE_MASK;
  1055. LPC_EMC->StaticConfig0 |= buf_val&EMC_STATIC_CFG_BUFENABLE_MASK;
  1056. break;
  1057. case 1:
  1058. LPC_EMC->StaticConfig1 &= ~EMC_STATIC_CFG_BUFENABLE_MASK;
  1059. LPC_EMC->StaticConfig1 |= buf_val&EMC_STATIC_CFG_BUFENABLE_MASK;
  1060. break;
  1061. case 2:
  1062. LPC_EMC->StaticConfig2 &= ~EMC_STATIC_CFG_BUFENABLE_MASK;
  1063. LPC_EMC->StaticConfig2 |= buf_val&EMC_STATIC_CFG_BUFENABLE_MASK;
  1064. break;
  1065. case 3:
  1066. LPC_EMC->StaticConfig3 &= ~EMC_STATIC_CFG_BUFENABLE_MASK;
  1067. LPC_EMC->StaticConfig3 |= buf_val&EMC_STATIC_CFG_BUFENABLE_MASK;
  1068. break;
  1069. default:
  1070. return EMC_FUNC_INVALID_PARAM;
  1071. }
  1072. return EMC_FUNC_OK;
  1073. }
  1074. /*********************************************************************//**
  1075. * @brief Configure the write permission
  1076. *
  1077. * @param[in] index index number, should be from 0 to 3
  1078. *
  1079. * @param[in] per_val Permission mode, should be:
  1080. *
  1081. * - EMC_CFG_WRITEPROTECT_DISABLED: Write not protected.
  1082. *
  1083. * - EMC_CFG_WRITEPROTECT_ENABLED: Write protected.
  1084. *
  1085. * @return EMC_FUNC_OK/EMC_FUNC_INVALID_PARAM
  1086. **********************************************************************/
  1087. EMC_FUNC_CODE EMC_StaMemConfigpP(uint32_t index , uint32_t per_val)
  1088. {
  1089. switch ( index)
  1090. {
  1091. case 0:
  1092. LPC_EMC->StaticConfig0 &= ~EMC_STATIC_CFG_WRIEPROTECT_MASK;
  1093. LPC_EMC->StaticConfig0 |= per_val&EMC_STATIC_CFG_WRIEPROTECT_MASK;
  1094. break;
  1095. case 1:
  1096. LPC_EMC->StaticConfig1 &= ~EMC_STATIC_CFG_WRIEPROTECT_MASK;
  1097. LPC_EMC->StaticConfig1 |= per_val&EMC_STATIC_CFG_WRIEPROTECT_MASK;
  1098. break;
  1099. case 2:
  1100. LPC_EMC->StaticConfig2 &= ~EMC_STATIC_CFG_WRIEPROTECT_MASK;
  1101. LPC_EMC->StaticConfig2 |= per_val&EMC_STATIC_CFG_WRIEPROTECT_MASK;
  1102. break;
  1103. case 3:
  1104. LPC_EMC->StaticConfig3 &= ~EMC_STATIC_CFG_WRIEPROTECT_MASK;
  1105. LPC_EMC->StaticConfig3 |= per_val&EMC_STATIC_CFG_WRIEPROTECT_MASK;
  1106. break;
  1107. default:
  1108. return EMC_FUNC_INVALID_PARAM;
  1109. }
  1110. return EMC_FUNC_OK;
  1111. }
  1112. /*********************************************************************//**
  1113. * @brief Set the value of LPC_EMC static memory registers
  1114. *
  1115. * @param[in] index index number, should be from 0 to 3
  1116. *
  1117. * @param[in] EMC_STA_MEM_PAR Static register, should be:
  1118. *
  1119. * - EMC_STA_MEM_WAITWEN: StaticWaitWen0 register
  1120. * - EMC_STA_MEM_WAITOEN: StaticWaitOen0 register
  1121. * - EMC_STA_MEM_WAITRD: StaticWaitRd0 register
  1122. * - EMC_STA_MEM_WAITPAGE: StaticWaitPage0 register
  1123. * - EMC_STA_MEM_WAITWR: StaticWaitWr0 register
  1124. * - EMC_STA_MEM_WAITTURN: StaticWaitTurn0 register
  1125. *
  1126. * @return EMC_FUNC_OK/EMC_FUNC_INVALID_PARAM
  1127. **********************************************************************/
  1128. EMC_FUNC_CODE EMC_SetStaMemoryParameter(uint32_t index ,EMC_STA_MEM_PAR par, uint32_t val)
  1129. {
  1130. switch (index)
  1131. {
  1132. case 0:
  1133. switch ( par)
  1134. {
  1135. case EMC_STA_MEM_WAITWEN:
  1136. LPC_EMC->StaticWaitWen0 = EMC_StaticWaitWen_WAITWEN(val);
  1137. break;
  1138. case EMC_STA_MEM_WAITOEN:
  1139. LPC_EMC->StaticWaitOen0 = EMC_StaticWaitOen_WAITOEN(val);
  1140. break;
  1141. case EMC_STA_MEM_WAITRD:
  1142. LPC_EMC->StaticWaitRd0 = EMC_StaticWaitRd_WAITRD(val);
  1143. break;
  1144. case EMC_STA_MEM_WAITPAGE:
  1145. LPC_EMC->StaticWaitPage0 = EMC_StaticwaitPage_WAITPAGE(val);
  1146. break;
  1147. case EMC_STA_MEM_WAITWR:
  1148. LPC_EMC->StaticWaitWr0 = EMC_StaticWaitwr_WAITWR(val);
  1149. break;
  1150. case EMC_STA_MEM_WAITTURN:
  1151. LPC_EMC->StaticWaitTurn0 =EMC_StaticWaitTurn_WAITTURN(val);
  1152. break;
  1153. default:
  1154. return EMC_FUNC_INVALID_PARAM;
  1155. }
  1156. break;
  1157. case 1:
  1158. switch ( par)
  1159. {
  1160. case EMC_STA_MEM_WAITWEN:
  1161. LPC_EMC->StaticWaitWen1 = EMC_StaticWaitWen_WAITWEN(val);
  1162. break;
  1163. case EMC_STA_MEM_WAITOEN:
  1164. LPC_EMC->StaticWaitOen1 = EMC_StaticWaitOen_WAITOEN(val);
  1165. break;
  1166. case EMC_STA_MEM_WAITRD:
  1167. LPC_EMC->StaticWaitRd1 = EMC_StaticWaitRd_WAITRD(val);
  1168. break;
  1169. case EMC_STA_MEM_WAITPAGE:
  1170. LPC_EMC->StaticWaitPage1 = EMC_StaticwaitPage_WAITPAGE(val);
  1171. break;
  1172. case EMC_STA_MEM_WAITWR:
  1173. LPC_EMC->StaticWaitWr1 = EMC_StaticWaitwr_WAITWR(val);
  1174. break;
  1175. case EMC_STA_MEM_WAITTURN:
  1176. LPC_EMC->StaticWaitTurn1 =EMC_StaticWaitTurn_WAITTURN(val);
  1177. break;
  1178. default:
  1179. return EMC_FUNC_INVALID_PARAM;
  1180. }
  1181. break;
  1182. case 2:
  1183. switch ( par)
  1184. {
  1185. case EMC_STA_MEM_WAITWEN:
  1186. LPC_EMC->StaticWaitWen2 = EMC_StaticWaitWen_WAITWEN(val);
  1187. break;
  1188. case EMC_STA_MEM_WAITOEN:
  1189. LPC_EMC->StaticWaitOen2 = EMC_StaticWaitOen_WAITOEN(val);
  1190. break;
  1191. case EMC_STA_MEM_WAITRD:
  1192. LPC_EMC->StaticWaitRd2 = EMC_StaticWaitRd_WAITRD(val);
  1193. break;
  1194. case EMC_STA_MEM_WAITPAGE:
  1195. LPC_EMC->StaticWaitPage2 = EMC_StaticwaitPage_WAITPAGE(val);
  1196. break;
  1197. case EMC_STA_MEM_WAITWR:
  1198. LPC_EMC->StaticWaitWr2 = EMC_StaticWaitwr_WAITWR(val);
  1199. break;
  1200. case EMC_STA_MEM_WAITTURN:
  1201. LPC_EMC->StaticWaitTurn2 =EMC_StaticWaitTurn_WAITTURN(val);
  1202. break;
  1203. default:
  1204. return EMC_FUNC_INVALID_PARAM;
  1205. }
  1206. break;
  1207. case 3:
  1208. switch ( par)
  1209. {
  1210. case EMC_STA_MEM_WAITWEN:
  1211. LPC_EMC->StaticWaitWen3 = EMC_StaticWaitWen_WAITWEN(val);
  1212. break;
  1213. case EMC_STA_MEM_WAITOEN:
  1214. LPC_EMC->StaticWaitOen3 = EMC_StaticWaitOen_WAITOEN(val);
  1215. break;
  1216. case EMC_STA_MEM_WAITRD:
  1217. LPC_EMC->StaticWaitRd3 = EMC_StaticWaitRd_WAITRD(val);
  1218. break;
  1219. case EMC_STA_MEM_WAITPAGE:
  1220. LPC_EMC->StaticWaitPage3 = EMC_StaticwaitPage_WAITPAGE(val);
  1221. break;
  1222. case EMC_STA_MEM_WAITWR:
  1223. LPC_EMC->StaticWaitWr3 = EMC_StaticWaitwr_WAITWR(val);
  1224. break;
  1225. case EMC_STA_MEM_WAITTURN:
  1226. LPC_EMC->StaticWaitTurn3 =EMC_StaticWaitTurn_WAITTURN(val);
  1227. break;
  1228. default:
  1229. return EMC_FUNC_INVALID_PARAM;
  1230. }
  1231. break;
  1232. }
  1233. return EMC_FUNC_OK;
  1234. }
  1235. /**
  1236. * @}
  1237. */
  1238. /**
  1239. * @}
  1240. */
  1241. #endif /*_EMC*/