fsl_semc.c 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382
  1. /*
  2. * Copyright 2017-2020 NXP
  3. * All rights reserved.
  4. *
  5. * SPDX-License-Identifier: BSD-3-Clause
  6. */
  7. #include "fsl_semc.h"
  8. /*******************************************************************************
  9. * Definitions
  10. ******************************************************************************/
  11. /* Component ID definition, used by tools. */
  12. #ifndef FSL_COMPONENT_ID
  13. #define FSL_COMPONENT_ID "platform.drivers.semc"
  14. #endif
  15. /*! @brief Define macros for SEMC driver. */
  16. #define SEMC_IPCOMMANDDATASIZEBYTEMAX (4U)
  17. #define SEMC_IPCOMMANDMAGICKEY (0xA55A)
  18. #if defined(FSL_FEATURE_SEMC_SUPPORT_SRAM_COUNT) && (FSL_FEATURE_SEMC_SUPPORT_SRAM_COUNT > 0x01U)
  19. #define SEMC_IOCR_PINMUXBITWIDTH (0x4UL)
  20. #else
  21. #define SEMC_IOCR_PINMUXBITWIDTH (0x3UL)
  22. #endif /* FSL_FEATURE_SEMC_SUPPORT_SRAM_COUNT */
  23. #define SEMC_IOCR_NAND_CE (4UL)
  24. #define SEMC_IOCR_NOR_CE (5UL)
  25. #define SEMC_IOCR_NOR_CE_A8 (2UL)
  26. #define SEMC_IOCR_PSRAM_CE (6UL)
  27. #define SEMC_IOCR_PSRAM_CE_A8 (3UL)
  28. #define SEMC_IOCR_DBI_CSX (7UL)
  29. #define SEMC_IOCR_DBI_CSX_A8 (4UL)
  30. #define SEMC_NORFLASH_SRAM_ADDR_PORTWIDTHBASE (24U)
  31. #define SEMC_NORFLASH_SRAM_ADDR_PORTWIDTHMAX (28U)
  32. #define SEMC_BMCR0_TYPICAL_WQOS (5U)
  33. #define SEMC_BMCR0_TYPICAL_WAGE (8U)
  34. #define SEMC_BMCR0_TYPICAL_WSH (0x40U)
  35. #define SEMC_BMCR0_TYPICAL_WRWS (0x10U)
  36. #define SEMC_BMCR1_TYPICAL_WQOS (5U)
  37. #define SEMC_BMCR1_TYPICAL_WAGE (8U)
  38. #define SEMC_BMCR1_TYPICAL_WPH (0x60U)
  39. #define SEMC_BMCR1_TYPICAL_WBR (0x40U)
  40. #define SEMC_BMCR1_TYPICAL_WRWS (0x24U)
  41. #define SEMC_STARTADDRESS (0x80000000UL)
  42. #define SEMC_ENDADDRESS (0xDFFFFFFFUL)
  43. #define SEMC_BR_MEMSIZE_MIN (4U)
  44. #define SEMC_BR_MEMSIZE_OFFSET (2U)
  45. #define SEMC_BR_MEMSIZE_MAX (4UL * 1024UL * 1024UL)
  46. #define SEMC_SDRAM_MODESETCAL_OFFSET (4U)
  47. #define SEMC_BR_REG_NUM (9U)
  48. #define SEMC_BYTE_NUMBIT (8U)
  49. /*******************************************************************************
  50. * Prototypes
  51. ******************************************************************************/
  52. /*!
  53. * @brief Get instance number for SEMC module.
  54. *
  55. * @param base SEMC peripheral base address
  56. */
  57. static uint32_t SEMC_GetInstance(SEMC_Type *base);
  58. /*!
  59. * @brief Covert the input memory size to internal register set value.
  60. *
  61. * @param base SEMC peripheral base address
  62. * @param size_kbytes SEMC memory size in unit of kbytes.
  63. * @param sizeConverted SEMC converted memory size to 0 ~ 0x1F.
  64. * @return Execution status.
  65. */
  66. static status_t SEMC_CovertMemorySize(SEMC_Type *base, uint32_t size_kbytes, uint8_t *sizeConverted);
  67. /*!
  68. * @brief Covert the external timing nanosecond to internal clock cycle.
  69. *
  70. * @param time_ns SEMC external time interval in unit of nanosecond.
  71. * @param clkSrc_Hz SEMC clock source frequency.
  72. * @return The changed internal clock cycle.
  73. */
  74. static uint8_t SEMC_ConvertTiming(uint32_t time_ns, uint32_t clkSrc_Hz);
  75. /*!
  76. * @brief Configure IP command.
  77. *
  78. * @param base SEMC peripheral base address.
  79. * @param size_bytes SEMC IP command data size.
  80. * @return Execution status.
  81. */
  82. static status_t SEMC_ConfigureIPCommand(SEMC_Type *base, uint8_t size_bytes);
  83. /*!
  84. * @brief Check if the IP command has finished.
  85. *
  86. * @param base SEMC peripheral base address.
  87. * @return Execution status.
  88. */
  89. static status_t SEMC_IsIPCommandDone(SEMC_Type *base);
  90. /*******************************************************************************
  91. * Variables
  92. ******************************************************************************/
  93. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  94. /*! @brief Pointers to SEMC clocks for each instance. */
  95. static const clock_ip_name_t s_semcClock[FSL_FEATURE_SOC_SEMC_COUNT] = SEMC_CLOCKS;
  96. #if (defined(SEMC_EXSC_CLOCKS))
  97. static const clock_ip_name_t s_semcExtClock[FSL_FEATURE_SOC_SEMC_COUNT] = SEMC_EXSC_CLOCKS;
  98. #endif /* SEMC_EXSC_CLOCKS */
  99. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  100. /*! @brief Pointers to SEMC bases for each instance. */
  101. static SEMC_Type *const s_semcBases[] = SEMC_BASE_PTRS;
  102. /*******************************************************************************
  103. * Code
  104. ******************************************************************************/
  105. static uint32_t SEMC_GetInstance(SEMC_Type *base)
  106. {
  107. uint32_t instance;
  108. /* Find the instance index from base address mappings. */
  109. for (instance = 0; instance < ARRAY_SIZE(s_semcBases); instance++)
  110. {
  111. if (s_semcBases[instance] == base)
  112. {
  113. break;
  114. }
  115. }
  116. assert(instance < ARRAY_SIZE(s_semcBases));
  117. return instance;
  118. }
  119. static status_t SEMC_CovertMemorySize(SEMC_Type *base, uint32_t size_kbytes, uint8_t *sizeConverted)
  120. {
  121. assert(sizeConverted != NULL);
  122. uint32_t memsize;
  123. status_t status = kStatus_Success;
  124. if ((size_kbytes < SEMC_BR_MEMSIZE_MIN) || (size_kbytes > SEMC_BR_MEMSIZE_MAX))
  125. {
  126. status = kStatus_SEMC_InvalidMemorySize;
  127. }
  128. else
  129. {
  130. *sizeConverted = 0U;
  131. memsize = size_kbytes / 8U;
  132. while (memsize != 0x00U)
  133. {
  134. memsize >>= 1U;
  135. (*sizeConverted)++;
  136. }
  137. }
  138. return status;
  139. }
  140. static uint8_t SEMC_ConvertTiming(uint32_t time_ns, uint32_t clkSrc_Hz)
  141. {
  142. assert(clkSrc_Hz != 0x00U);
  143. uint8_t clockCycles = 0;
  144. uint32_t tClk_ps;
  145. clkSrc_Hz /= 1000000U;
  146. /* Using ps for high resolution */
  147. tClk_ps = 1000000U / clkSrc_Hz;
  148. while (tClk_ps * clockCycles < time_ns * 1000U)
  149. {
  150. clockCycles++;
  151. }
  152. return (clockCycles == 0x00U) ? clockCycles : (clockCycles - 0x01U);
  153. }
  154. static status_t SEMC_ConfigureIPCommand(SEMC_Type *base, uint8_t size_bytes)
  155. {
  156. status_t status = kStatus_Success;
  157. if ((size_bytes > SEMC_IPCOMMANDDATASIZEBYTEMAX) || (size_bytes == 0x00U))
  158. {
  159. status = kStatus_SEMC_InvalidIpcmdDataSize;
  160. }
  161. else
  162. {
  163. /* Set data size. */
  164. /* Note: It is better to set data size as the device data port width when transferring
  165. * device command data. But for device memory data transfer, it can be set freely.
  166. * Note: If the data size is greater than data port width, for example, datsz = 4, data port = 16bit,
  167. * then the 4-byte data transfer will be split into two 2-byte transfers, the slave address
  168. * will be switched automatically according to connected device type*/
  169. base->IPCR1 = SEMC_IPCR1_DATSZ(size_bytes);
  170. /* Clear data size. */
  171. base->IPCR2 = 0;
  172. /* Set data size. */
  173. if (size_bytes < 4U)
  174. {
  175. base->IPCR2 |= SEMC_IPCR2_BM3_MASK;
  176. }
  177. if (size_bytes < 3U)
  178. {
  179. base->IPCR2 |= SEMC_IPCR2_BM2_MASK;
  180. }
  181. if (size_bytes < 2U)
  182. {
  183. base->IPCR2 |= SEMC_IPCR2_BM1_MASK;
  184. }
  185. }
  186. return status;
  187. }
  188. static status_t SEMC_IsIPCommandDone(SEMC_Type *base)
  189. {
  190. status_t status = kStatus_Success;
  191. /* Poll status bit till command is done*/
  192. while ((base->INTR & (uint32_t)SEMC_INTR_IPCMDDONE_MASK) == 0x00U)
  193. {
  194. };
  195. /* Clear status bit */
  196. base->INTR |= SEMC_INTR_IPCMDDONE_MASK;
  197. /* Check error status */
  198. if ((base->INTR & (uint32_t)SEMC_INTR_IPCMDERR_MASK) != 0x00U)
  199. {
  200. base->INTR |= SEMC_INTR_IPCMDERR_MASK;
  201. status = kStatus_SEMC_IpCommandExecutionError;
  202. }
  203. return status;
  204. }
  205. /*!
  206. * brief Gets the SEMC default basic configuration structure.
  207. *
  208. * The purpose of this API is to get the default SEMC
  209. * configure structure for SEMC_Init(). User may use the initialized
  210. * structure unchanged in SEMC_Init(), or modify some fields of the
  211. * structure before calling SEMC_Init().
  212. * Example:
  213. code
  214. semc_config_t config;
  215. SEMC_GetDefaultConfig(&config);
  216. endcode
  217. * param config The SEMC configuration structure pointer.
  218. */
  219. void SEMC_GetDefaultConfig(semc_config_t *config)
  220. {
  221. assert(config != NULL);
  222. /* Initializes the configure structure to zero. */
  223. (void)memset(config, 0, sizeof(*config));
  224. config->queueWeight.queueaEnable = true;
  225. semc_queuea_weight_struct_t *queueaWeight = &(config->queueWeight.queueaWeight.queueaConfig);
  226. config->queueWeight.queuebEnable = true;
  227. semc_queueb_weight_struct_t *queuebWeight = &(config->queueWeight.queuebWeight.queuebConfig);
  228. /* Get default settings. */
  229. config->dqsMode = kSEMC_Loopbackinternal;
  230. config->cmdTimeoutCycles = 0xFF;
  231. config->busTimeoutCycles = 0x1F;
  232. queueaWeight->qos = SEMC_BMCR0_TYPICAL_WQOS;
  233. queueaWeight->aging = SEMC_BMCR0_TYPICAL_WAGE;
  234. queueaWeight->slaveHitSwith = SEMC_BMCR0_TYPICAL_WSH;
  235. queueaWeight->slaveHitNoswitch = SEMC_BMCR0_TYPICAL_WRWS;
  236. queuebWeight->qos = SEMC_BMCR1_TYPICAL_WQOS;
  237. queuebWeight->aging = SEMC_BMCR1_TYPICAL_WAGE;
  238. queuebWeight->slaveHitSwith = SEMC_BMCR1_TYPICAL_WRWS;
  239. queuebWeight->weightPagehit = SEMC_BMCR1_TYPICAL_WPH;
  240. queuebWeight->bankRotation = SEMC_BMCR1_TYPICAL_WBR;
  241. }
  242. /*!
  243. * brief Initializes SEMC.
  244. * This function ungates the SEMC clock and initializes SEMC.
  245. * This function must be called before calling any other SEMC driver functions.
  246. *
  247. * param base SEMC peripheral base address.
  248. * param configure The SEMC configuration structure pointer.
  249. */
  250. void SEMC_Init(SEMC_Type *base, semc_config_t *configure)
  251. {
  252. assert(configure != NULL);
  253. uint8_t index = 0;
  254. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  255. /* Un-gate sdram controller clock. */
  256. CLOCK_EnableClock(s_semcClock[SEMC_GetInstance(base)]);
  257. #if (defined(SEMC_EXSC_CLOCKS))
  258. CLOCK_EnableClock(s_semcExtClock[SEMC_GetInstance(base)]);
  259. #endif /* SEMC_EXSC_CLOCKS */
  260. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  261. /* Initialize all BR to zero due to the default base address set. */
  262. for (index = 0; index < SEMC_BR_REG_NUM; index++)
  263. {
  264. base->BR[index] = 0;
  265. }
  266. /* Software reset for SEMC internal logical . */
  267. base->MCR = SEMC_MCR_SWRST_MASK;
  268. while ((base->MCR & (uint32_t)SEMC_MCR_SWRST_MASK) != 0x00U)
  269. {
  270. }
  271. /* Configure, disable module first. */
  272. base->MCR |= SEMC_MCR_MDIS_MASK | SEMC_MCR_BTO(configure->busTimeoutCycles) |
  273. SEMC_MCR_CTO(configure->cmdTimeoutCycles) | SEMC_MCR_DQSMD(configure->dqsMode);
  274. if (configure->queueWeight.queueaEnable == true)
  275. {
  276. /* Configure Queue A for AXI bus access to SDRAM, NAND, NOR, SRAM and DBI slaves.*/
  277. base->BMCR0 = (uint32_t)(configure->queueWeight.queueaWeight.queueaValue);
  278. }
  279. else
  280. {
  281. base->BMCR0 = 0x00U;
  282. }
  283. if (configure->queueWeight.queuebEnable == true)
  284. {
  285. /* Configure Queue B for AXI bus access to SDRAM slave. */
  286. base->BMCR1 = (uint32_t)(configure->queueWeight.queuebWeight.queuebValue);
  287. }
  288. else
  289. {
  290. base->BMCR1 = 0x00U;
  291. }
  292. /* Enable SEMC. */
  293. base->MCR &= ~SEMC_MCR_MDIS_MASK;
  294. }
  295. /*!
  296. * brief Deinitializes the SEMC module and gates the clock.
  297. * This function gates the SEMC clock. As a result, the SEMC
  298. * module doesn't work after calling this function.
  299. *
  300. * param base SEMC peripheral base address.
  301. */
  302. void SEMC_Deinit(SEMC_Type *base)
  303. {
  304. /* Disable module. Check there is no pending command before disable module. */
  305. while ((base->STS0 & (uint32_t)SEMC_STS0_IDLE_MASK) == 0x00U)
  306. {
  307. ;
  308. }
  309. base->MCR |= SEMC_MCR_MDIS_MASK | SEMC_MCR_SWRST_MASK;
  310. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  311. /* Disable SDRAM clock. */
  312. CLOCK_DisableClock(s_semcClock[SEMC_GetInstance(base)]);
  313. #if (defined(SEMC_EXSC_CLOCKS))
  314. CLOCK_DisableClock(s_semcExtClock[SEMC_GetInstance(base)]);
  315. #endif /* SEMC_EXSC_CLOCKS */
  316. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  317. }
  318. /*!
  319. * brief Configures SDRAM controller in SEMC.
  320. *
  321. * param base SEMC peripheral base address.
  322. * param cs The chip selection.
  323. * param config The sdram configuration.
  324. * param clkSrc_Hz The SEMC clock frequency.
  325. */
  326. status_t SEMC_ConfigureSDRAM(SEMC_Type *base, semc_sdram_cs_t cs, semc_sdram_config_t *config, uint32_t clkSrc_Hz)
  327. {
  328. assert(config != NULL);
  329. assert(clkSrc_Hz > 0x00U);
  330. assert(config->refreshBurstLen > 0x00U);
  331. uint8_t memsize;
  332. status_t result = kStatus_Success;
  333. uint16_t prescale = (uint16_t)(config->tPrescalePeriod_Ns / 16U / (1000000000U / clkSrc_Hz));
  334. uint32_t refresh;
  335. uint32_t urgentRef;
  336. uint32_t idle;
  337. uint32_t mode;
  338. uint32_t timing;
  339. if ((config->address < SEMC_STARTADDRESS) || (config->address > SEMC_ENDADDRESS))
  340. {
  341. return kStatus_SEMC_InvalidBaseAddress;
  342. }
  343. if (config->csxPinMux == kSEMC_MUXA8)
  344. {
  345. return kStatus_SEMC_InvalidSwPinmuxSelection;
  346. }
  347. if (prescale > 256U)
  348. {
  349. return kStatus_SEMC_InvalidTimerSetting;
  350. }
  351. refresh = config->refreshPeriod_nsPerRow / config->tPrescalePeriod_Ns;
  352. urgentRef = config->refreshUrgThreshold / config->tPrescalePeriod_Ns;
  353. idle = config->tIdleTimeout_Ns / config->tPrescalePeriod_Ns;
  354. uint32_t iocReg = base->IOCR & (~(SEMC_IOCR_PINMUXBITWIDTH << (uint32_t)config->csxPinMux));
  355. /* Base control. */
  356. result = SEMC_CovertMemorySize(base, config->memsize_kbytes, &memsize);
  357. if (result != kStatus_Success)
  358. {
  359. return result;
  360. }
  361. base->BR[cs] = (config->address & SEMC_BR_BA_MASK) | SEMC_BR_MS(memsize) | SEMC_BR_VLD_MASK;
  362. #if defined(FSL_FEATURE_SEMC_SDRAM_SUPPORT_COLUMN_ADDRESS_8BIT) && (FSL_FEATURE_SEMC_SDRAM_SUPPORT_COLUMN_ADDRESS_8BIT)
  363. if (kSEMC_SdramColunm_8bit == config->columnAddrBitNum)
  364. {
  365. base->SDRAMCR0 = SEMC_SDRAMCR0_PS(config->portSize) | SEMC_SDRAMCR0_BL(config->burstLen) |
  366. SEMC_SDRAMCR0_COL8(true) | SEMC_SDRAMCR0_CL(config->casLatency);
  367. }
  368. else
  369. #endif /* FSL_FEATURE_SEMC_SDRAM_SUPPORT_COLUMN_ADDRESS_8BIT */
  370. {
  371. base->SDRAMCR0 = SEMC_SDRAMCR0_PS(config->portSize) | SEMC_SDRAMCR0_BL(config->burstLen) |
  372. SEMC_SDRAMCR0_COL(config->columnAddrBitNum) | SEMC_SDRAMCR0_CL(config->casLatency);
  373. }
  374. /* IOMUX setting. */
  375. if (cs != kSEMC_SDRAM_CS0)
  376. {
  377. base->IOCR = iocReg | ((uint32_t)cs << (uint32_t)config->csxPinMux);
  378. }
  379. base->IOCR &= ~SEMC_IOCR_MUX_A8_MASK;
  380. #if defined(FSL_FEATURE_SEMC_HAS_DELAY_CHAIN_CONTROL) && (FSL_FEATURE_SEMC_HAS_DELAY_CHAIN_CONTROL)
  381. uint32_t tempDelayChain = base->DCCR;
  382. tempDelayChain &= ~(SEMC_DCCR_SDRAMVAL_MASK | SEMC_DCCR_SDRAMEN_MASK);
  383. /* Configure delay chain. */
  384. base->DCCR = tempDelayChain | SEMC_DCCR_SDRAMVAL((uint32_t)config->delayChain - 0x01U) | SEMC_DCCR_SDRAMEN_MASK;
  385. #endif /* FSL_FEATURE_SEMC_HAS_DELAY_CHAIN_CONTROL */
  386. timing = SEMC_SDRAMCR1_PRE2ACT(SEMC_ConvertTiming(config->tPrecharge2Act_Ns, clkSrc_Hz));
  387. timing |= SEMC_SDRAMCR1_ACT2RW(SEMC_ConvertTiming(config->tAct2ReadWrite_Ns, clkSrc_Hz));
  388. timing |= SEMC_SDRAMCR1_RFRC(SEMC_ConvertTiming(config->tRefreshRecovery_Ns, clkSrc_Hz));
  389. timing |= SEMC_SDRAMCR1_WRC(SEMC_ConvertTiming(config->tWriteRecovery_Ns, clkSrc_Hz));
  390. timing |= SEMC_SDRAMCR1_CKEOFF(SEMC_ConvertTiming(config->tCkeOff_Ns, clkSrc_Hz));
  391. timing |= SEMC_SDRAMCR1_ACT2PRE(SEMC_ConvertTiming(config->tAct2Prechage_Ns, clkSrc_Hz));
  392. /* SDRAMCR1 timing setting. */
  393. base->SDRAMCR1 = timing;
  394. timing = SEMC_SDRAMCR2_SRRC(SEMC_ConvertTiming(config->tSelfRefRecovery_Ns, clkSrc_Hz));
  395. timing |= SEMC_SDRAMCR2_REF2REF(SEMC_ConvertTiming(config->tRefresh2Refresh_Ns, clkSrc_Hz));
  396. timing |= SEMC_SDRAMCR2_ACT2ACT(SEMC_ConvertTiming(config->tAct2Act_Ns, clkSrc_Hz)) | SEMC_SDRAMCR2_ITO(idle);
  397. /* SDRAMCR2 timing setting. */
  398. base->SDRAMCR2 = timing;
  399. /* SDRAMCR3 timing setting. */
  400. base->SDRAMCR3 = SEMC_SDRAMCR3_REBL((uint32_t)config->refreshBurstLen - 1UL) |
  401. /* N * 16 * 1s / clkSrc_Hz = config->tPrescalePeriod_Ns */
  402. SEMC_SDRAMCR3_PRESCALE(prescale) | SEMC_SDRAMCR3_RT(refresh - 1UL) | SEMC_SDRAMCR3_UT(urgentRef);
  403. SEMC->IPCR1 = 0x2U;
  404. SEMC->IPCR2 = 0U;
  405. result =
  406. SEMC_SendIPCommand(base, kSEMC_MemType_SDRAM, config->address, (uint32_t)kSEMC_SDRAMCM_Prechargeall, 0, NULL);
  407. if (result != kStatus_Success)
  408. {
  409. return result;
  410. }
  411. result =
  412. SEMC_SendIPCommand(base, kSEMC_MemType_SDRAM, config->address, (uint32_t)kSEMC_SDRAMCM_AutoRefresh, 0, NULL);
  413. if (result != kStatus_Success)
  414. {
  415. return result;
  416. }
  417. result =
  418. SEMC_SendIPCommand(base, kSEMC_MemType_SDRAM, config->address, (uint32_t)kSEMC_SDRAMCM_AutoRefresh, 0, NULL);
  419. if (result != kStatus_Success)
  420. {
  421. return result;
  422. }
  423. /* Mode setting value. */
  424. mode = (uint32_t)config->burstLen | (((uint32_t)config->casLatency) << SEMC_SDRAM_MODESETCAL_OFFSET);
  425. result =
  426. SEMC_SendIPCommand(base, kSEMC_MemType_SDRAM, config->address, (uint32_t)kSEMC_SDRAMCM_Modeset, mode, NULL);
  427. if (result != kStatus_Success)
  428. {
  429. return result;
  430. }
  431. /* Enables refresh */
  432. base->SDRAMCR3 |= SEMC_SDRAMCR3_REN_MASK;
  433. return kStatus_Success;
  434. }
  435. /*!
  436. * brief Configures NAND controller in SEMC.
  437. *
  438. * param base SEMC peripheral base address.
  439. * param config The nand configuration.
  440. * param clkSrc_Hz The SEMC clock frequency.
  441. */
  442. status_t SEMC_ConfigureNAND(SEMC_Type *base, semc_nand_config_t *config, uint32_t clkSrc_Hz)
  443. {
  444. assert(config != NULL);
  445. assert(config->timingConfig != NULL);
  446. uint8_t memsize;
  447. status_t result;
  448. uint32_t timing;
  449. if ((config->axiAddress < SEMC_STARTADDRESS) || (config->axiAddress > SEMC_ENDADDRESS))
  450. {
  451. return kStatus_SEMC_InvalidBaseAddress;
  452. }
  453. if (config->cePinMux == kSEMC_MUXRDY)
  454. {
  455. return kStatus_SEMC_InvalidSwPinmuxSelection;
  456. }
  457. /* Disable SEMC module during configuring control registers. */
  458. base->MCR |= SEMC_MCR_MDIS_MASK;
  459. uint32_t iocReg =
  460. base->IOCR & (~((SEMC_IOCR_PINMUXBITWIDTH << (uint32_t)config->cePinMux) | SEMC_IOCR_MUX_RDY_MASK));
  461. /* Base control. */
  462. if (config->rdyactivePolarity == kSEMC_RdyActivehigh)
  463. {
  464. base->MCR |= SEMC_MCR_WPOL1_MASK;
  465. }
  466. else
  467. {
  468. base->MCR &= ~SEMC_MCR_WPOL1_MASK;
  469. }
  470. result = SEMC_CovertMemorySize(base, config->axiMemsize_kbytes, &memsize);
  471. if (result != kStatus_Success)
  472. {
  473. return result;
  474. }
  475. base->BR[4] = (config->axiAddress & SEMC_BR_BA_MASK) | SEMC_BR_MS(memsize) | SEMC_BR_VLD_MASK;
  476. result = SEMC_CovertMemorySize(base, config->ipgMemsize_kbytes, &memsize);
  477. if (result != kStatus_Success)
  478. {
  479. return result;
  480. }
  481. base->BR[8] = (config->ipgAddress & SEMC_BR_BA_MASK) | SEMC_BR_MS(memsize) | SEMC_BR_VLD_MASK;
  482. /* IOMUX setting. */
  483. if ((uint32_t)config->cePinMux != 0x00U)
  484. {
  485. base->IOCR = iocReg | (SEMC_IOCR_NAND_CE << (uint32_t)config->cePinMux);
  486. }
  487. else
  488. {
  489. base->IOCR = iocReg | (1UL << (uint32_t)config->cePinMux);
  490. }
  491. base->NANDCR0 = SEMC_NANDCR0_PS(config->portSize) | SEMC_NANDCR0_BL(config->burstLen) |
  492. SEMC_NANDCR0_EDO(config->edoModeEnabled) | SEMC_NANDCR0_COL(config->columnAddrBitNum);
  493. timing = SEMC_NANDCR1_CES(SEMC_ConvertTiming(config->timingConfig->tCeSetup_Ns, clkSrc_Hz));
  494. timing |= SEMC_NANDCR1_CEH(SEMC_ConvertTiming(config->timingConfig->tCeHold_Ns, clkSrc_Hz));
  495. timing |= SEMC_NANDCR1_WEL(SEMC_ConvertTiming(config->timingConfig->tWeLow_Ns, clkSrc_Hz));
  496. timing |= SEMC_NANDCR1_WEH(SEMC_ConvertTiming(config->timingConfig->tWeHigh_Ns, clkSrc_Hz));
  497. timing |= SEMC_NANDCR1_REL(SEMC_ConvertTiming(config->timingConfig->tReLow_Ns, clkSrc_Hz));
  498. timing |= SEMC_NANDCR1_REH(SEMC_ConvertTiming(config->timingConfig->tReHigh_Ns, clkSrc_Hz));
  499. timing |= SEMC_NANDCR1_TA(SEMC_ConvertTiming(config->timingConfig->tTurnAround_Ns, clkSrc_Hz));
  500. timing |= SEMC_NANDCR1_CEITV(SEMC_ConvertTiming(config->timingConfig->tCeInterval_Ns, clkSrc_Hz));
  501. /* NANDCR1 timing setting. */
  502. base->NANDCR1 = timing;
  503. timing = SEMC_NANDCR2_TWHR(SEMC_ConvertTiming(config->timingConfig->tWehigh2Relow_Ns, clkSrc_Hz));
  504. timing |= SEMC_NANDCR2_TRHW(SEMC_ConvertTiming(config->timingConfig->tRehigh2Welow_Ns, clkSrc_Hz));
  505. timing |= SEMC_NANDCR2_TADL(SEMC_ConvertTiming(config->timingConfig->tAle2WriteStart_Ns, clkSrc_Hz));
  506. timing |= SEMC_NANDCR2_TRR(SEMC_ConvertTiming(config->timingConfig->tReady2Relow_Ns, clkSrc_Hz));
  507. timing |= SEMC_NANDCR2_TWB(SEMC_ConvertTiming(config->timingConfig->tWehigh2Busy_Ns, clkSrc_Hz));
  508. /* NANDCR2 timing setting. */
  509. base->NANDCR2 = timing;
  510. /* NANDCR3 timing setting. */
  511. base->NANDCR3 = (uint32_t)config->arrayAddrOption;
  512. /* Enables SEMC module after configuring control registers completely. */
  513. base->MCR &= ~SEMC_MCR_MDIS_MASK;
  514. return kStatus_Success;
  515. }
  516. /*!
  517. * brief Configures NOR controller in SEMC.
  518. *
  519. * param base SEMC peripheral base address.
  520. * param config The nor configuration.
  521. * param clkSrc_Hz The SEMC clock frequency.
  522. */
  523. status_t SEMC_ConfigureNOR(SEMC_Type *base, semc_nor_config_t *config, uint32_t clkSrc_Hz)
  524. {
  525. assert(config != NULL);
  526. uint8_t memsize;
  527. status_t result;
  528. uint32_t timing;
  529. if ((config->address < SEMC_STARTADDRESS) || (config->address > SEMC_ENDADDRESS))
  530. {
  531. return kStatus_SEMC_InvalidBaseAddress;
  532. }
  533. uint32_t iocReg = base->IOCR & (~(SEMC_IOCR_PINMUXBITWIDTH << (uint32_t)config->cePinMux));
  534. uint32_t muxCe = (config->cePinMux == kSEMC_MUXRDY) ?
  535. (SEMC_IOCR_NOR_CE - 1U) :
  536. ((config->cePinMux == kSEMC_MUXA8) ? SEMC_IOCR_NOR_CE_A8 : SEMC_IOCR_NOR_CE);
  537. /* IOMUX setting. */
  538. base->IOCR = iocReg | (muxCe << (uint32_t)config->cePinMux);
  539. /* Address bit setting. */
  540. if (config->addrPortWidth > SEMC_NORFLASH_SRAM_ADDR_PORTWIDTHBASE)
  541. {
  542. if (config->addrPortWidth >= (SEMC_NORFLASH_SRAM_ADDR_PORTWIDTHBASE + 1U))
  543. {
  544. /* Address bit 24 (A24) */
  545. base->IOCR &= ~(uint32_t)SEMC_IOCR_MUX_CSX0_MASK;
  546. if (config->cePinMux == kSEMC_MUXCSX0)
  547. {
  548. return kStatus_SEMC_InvalidSwPinmuxSelection;
  549. }
  550. }
  551. if (config->addrPortWidth >= (SEMC_NORFLASH_SRAM_ADDR_PORTWIDTHBASE + 2U))
  552. {
  553. /* Address bit 25 (A25) */
  554. base->IOCR &= ~(uint32_t)SEMC_IOCR_MUX_CSX1_MASK;
  555. if (config->cePinMux == kSEMC_MUXCSX1)
  556. {
  557. return kStatus_SEMC_InvalidSwPinmuxSelection;
  558. }
  559. }
  560. if (config->addrPortWidth >= (SEMC_NORFLASH_SRAM_ADDR_PORTWIDTHBASE + 3U))
  561. {
  562. /* Address bit 26 (A26) */
  563. base->IOCR &= ~(uint32_t)SEMC_IOCR_MUX_CSX2_MASK;
  564. if (config->cePinMux == kSEMC_MUXCSX2)
  565. {
  566. return kStatus_SEMC_InvalidSwPinmuxSelection;
  567. }
  568. }
  569. if (config->addrPortWidth >= (SEMC_NORFLASH_SRAM_ADDR_PORTWIDTHBASE + 4U))
  570. {
  571. if (config->addr27 == kSEMC_NORA27_MUXCSX3)
  572. {
  573. /* Address bit 27 (A27) */
  574. base->IOCR &= ~(uint32_t)SEMC_IOCR_MUX_CSX3_MASK;
  575. }
  576. else if (config->addr27 == kSEMC_NORA27_MUXRDY)
  577. {
  578. base->IOCR |= SEMC_IOCR_MUX_RDY_MASK;
  579. }
  580. else
  581. {
  582. return kStatus_SEMC_InvalidSwPinmuxSelection;
  583. }
  584. if (config->cePinMux == kSEMC_MUXCSX3)
  585. {
  586. return kStatus_SEMC_InvalidSwPinmuxSelection;
  587. }
  588. }
  589. if (config->addrPortWidth > SEMC_NORFLASH_SRAM_ADDR_PORTWIDTHMAX)
  590. {
  591. return kStatus_SEMC_InvalidAddressPortWidth;
  592. }
  593. }
  594. /* Base control. */
  595. if (config->rdyactivePolarity == kSEMC_RdyActivehigh)
  596. {
  597. base->MCR |= SEMC_MCR_WPOL0_MASK;
  598. }
  599. else
  600. {
  601. base->MCR &= ~SEMC_MCR_WPOL0_MASK;
  602. }
  603. result = SEMC_CovertMemorySize(base, config->memsize_kbytes, &memsize);
  604. if (result != kStatus_Success)
  605. {
  606. return result;
  607. }
  608. base->BR[5] = (config->address & SEMC_BR_BA_MASK) | SEMC_BR_MS(memsize) | SEMC_BR_VLD_MASK;
  609. base->NORCR0 = SEMC_NORCR0_PS(config->portSize) | SEMC_NORCR0_BL(config->burstLen) |
  610. SEMC_NORCR0_AM(config->addrMode) | SEMC_NORCR0_ADVP(config->advActivePolarity) |
  611. SEMC_NORCR0_COL(config->columnAddrBitNum);
  612. #if defined(FSL_FEATURE_SEMC_HAS_DELAY_CHAIN_CONTROL) && (FSL_FEATURE_SEMC_HAS_DELAY_CHAIN_CONTROL)
  613. uint32_t tempDelayChain = base->DCCR;
  614. tempDelayChain &= ~(SEMC_DCCR_NORVAL_MASK | SEMC_DCCR_NOREN_MASK);
  615. /* Configure delay chain. */
  616. base->DCCR = tempDelayChain | SEMC_DCCR_NORVAL((uint32_t)config->delayChain - 0x01U) | SEMC_DCCR_NOREN_MASK;
  617. #endif /* FSL_FEATURE_SEMC_HAS_DELAY_CHAIN_CONTROL */
  618. timing = SEMC_NORCR1_CES(SEMC_ConvertTiming(config->tCeSetup_Ns, clkSrc_Hz));
  619. timing |= SEMC_NORCR1_CEH(SEMC_ConvertTiming(config->tCeHold_Ns, clkSrc_Hz));
  620. timing |= SEMC_NORCR1_AS(SEMC_ConvertTiming(config->tAddrSetup_Ns, clkSrc_Hz));
  621. timing |= SEMC_NORCR1_AH(SEMC_ConvertTiming(config->tAddrHold_Ns, clkSrc_Hz));
  622. timing |= SEMC_NORCR1_WEL(SEMC_ConvertTiming(config->tWeLow_Ns, clkSrc_Hz));
  623. timing |= SEMC_NORCR1_WEH(SEMC_ConvertTiming(config->tWeHigh_Ns, clkSrc_Hz));
  624. timing |= SEMC_NORCR1_REL(SEMC_ConvertTiming(config->tReLow_Ns, clkSrc_Hz));
  625. timing |= SEMC_NORCR1_REH(SEMC_ConvertTiming(config->tReHigh_Ns, clkSrc_Hz));
  626. /* NORCR1 timing setting. */
  627. base->NORCR1 = timing;
  628. timing = SEMC_NORCR2_CEITV(SEMC_ConvertTiming(config->tCeInterval_Ns, clkSrc_Hz));
  629. #if defined(FSL_FEATURE_SEMC_HAS_NOR_WDS_TIME) && (FSL_FEATURE_SEMC_HAS_NOR_WDS_TIME)
  630. timing |= SEMC_NORCR2_WDS(SEMC_ConvertTiming(config->tWriteSetup_Ns, clkSrc_Hz));
  631. #endif /* FSL_FEATURE_SEMC_HAS_NOR_WDS_TIME */
  632. #if defined(FSL_FEATURE_SEMC_HAS_NOR_WDH_TIME) && (FSL_FEATURE_SEMC_HAS_NOR_WDH_TIME)
  633. timing |= SEMC_NORCR2_WDH(SEMC_ConvertTiming(config->tWriteHold_Ns, clkSrc_Hz));
  634. #endif /* FSL_FEATURE_SEMC_HAS_NOR_WDH_TIME */
  635. timing |= SEMC_NORCR2_TA(SEMC_ConvertTiming(config->tTurnAround_Ns, clkSrc_Hz));
  636. timing |= SEMC_NORCR2_AWDH((uint32_t)SEMC_ConvertTiming(config->tAddr2WriteHold_Ns, clkSrc_Hz) + 0x01UL);
  637. #if defined(FSL_FEATURE_SEMC_HAS_NOR_LC_TIME) && (FSL_FEATURE_SEMC_HAS_NOR_LC_TIME)
  638. timing |= SEMC_NORCR2_LC(config->latencyCount);
  639. #endif
  640. #if defined(FSL_FEATURE_SEMC_HAS_NOR_RD_TIME) && (FSL_FEATURE_SEMC_HAS_NOR_RD_TIME)
  641. timing |= SEMC_NORCR2_RD((uint32_t)config->readCycle - 0x01UL);
  642. #endif
  643. /* NORCR2 timing setting. */
  644. base->NORCR2 = timing;
  645. return SEMC_ConfigureIPCommand(base, ((uint8_t)config->portSize + 1U));
  646. }
  647. /*!
  648. * brief Configures SRAM controller in SEMC, which can be used only for specific chip selection CS0.
  649. *
  650. * param base SEMC peripheral base address.
  651. * param config The sram configuration.
  652. * param clkSrc_Hz The SEMC clock frequency.
  653. */
  654. status_t SEMC_ConfigureSRAM(SEMC_Type *base, semc_sram_config_t *config, uint32_t clkSrc_Hz)
  655. {
  656. return SEMC_ConfigureSRAMWithChipSelection(base, kSEMC_SRAM_CS0, config, clkSrc_Hz);
  657. }
  658. /*!
  659. * brief Configures SRAM controller in SEMC, which can be used up to four chip selections CS0/CS1/CS2/CS3..
  660. *
  661. * param base SEMC peripheral base address.
  662. * param cs The chip selection.
  663. * param config The sram configuration.
  664. * param clkSrc_Hz The SEMC clock frequency.
  665. */
  666. status_t SEMC_ConfigureSRAMWithChipSelection(SEMC_Type *base,
  667. semc_sram_cs_t cs,
  668. semc_sram_config_t *config,
  669. uint32_t clkSrc_Hz)
  670. {
  671. assert(config != NULL);
  672. uint32_t tempBRVal;
  673. uint32_t timing;
  674. uint8_t memsize;
  675. status_t result = kStatus_Success;
  676. if ((config->address < SEMC_STARTADDRESS) || (config->address > SEMC_ENDADDRESS))
  677. {
  678. return kStatus_SEMC_InvalidBaseAddress;
  679. }
  680. uint32_t iocReg = base->IOCR & (~(SEMC_IOCR_PINMUXBITWIDTH << (uint32_t)config->cePinMux));
  681. uint32_t muxCe = (config->cePinMux == kSEMC_MUXRDY) ?
  682. (SEMC_IOCR_PSRAM_CE - 1U) :
  683. ((config->cePinMux == kSEMC_MUXA8) ? SEMC_IOCR_PSRAM_CE_A8 : SEMC_IOCR_PSRAM_CE);
  684. /* IOMUX setting. */
  685. base->IOCR = iocReg | (muxCe << (uint32_t)config->cePinMux);
  686. /* Address bit setting. */
  687. if (config->addrPortWidth > SEMC_NORFLASH_SRAM_ADDR_PORTWIDTHBASE)
  688. {
  689. if (config->addrPortWidth >= (SEMC_NORFLASH_SRAM_ADDR_PORTWIDTHBASE + 1U))
  690. {
  691. /* Address bit 24 (A24) */
  692. base->IOCR &= ~(uint32_t)SEMC_IOCR_MUX_CSX0_MASK;
  693. if (config->cePinMux == kSEMC_MUXCSX0)
  694. {
  695. return kStatus_SEMC_InvalidSwPinmuxSelection;
  696. }
  697. }
  698. if (config->addrPortWidth >= (SEMC_NORFLASH_SRAM_ADDR_PORTWIDTHBASE + 2U))
  699. {
  700. /* Address bit 25 (A25) */
  701. base->IOCR &= ~(uint32_t)SEMC_IOCR_MUX_CSX1_MASK;
  702. if (config->cePinMux == kSEMC_MUXCSX1)
  703. {
  704. return kStatus_SEMC_InvalidSwPinmuxSelection;
  705. }
  706. }
  707. if (config->addrPortWidth >= (SEMC_NORFLASH_SRAM_ADDR_PORTWIDTHBASE + 3U))
  708. {
  709. /* Address bit 26 (A26) */
  710. base->IOCR &= ~(uint32_t)SEMC_IOCR_MUX_CSX2_MASK;
  711. if (config->cePinMux == kSEMC_MUXCSX2)
  712. {
  713. return kStatus_SEMC_InvalidSwPinmuxSelection;
  714. }
  715. }
  716. if (config->addrPortWidth >= (SEMC_NORFLASH_SRAM_ADDR_PORTWIDTHBASE + 4U))
  717. {
  718. if (config->addr27 == kSEMC_NORA27_MUXCSX3)
  719. {
  720. /* Address bit 27 (A27) */
  721. base->IOCR &= ~(uint32_t)SEMC_IOCR_MUX_CSX3_MASK;
  722. }
  723. else if (config->addr27 == kSEMC_NORA27_MUXRDY)
  724. {
  725. base->IOCR |= SEMC_IOCR_MUX_RDY_MASK;
  726. }
  727. else
  728. {
  729. return kStatus_SEMC_InvalidSwPinmuxSelection;
  730. }
  731. if (config->cePinMux == kSEMC_MUXCSX3)
  732. {
  733. return kStatus_SEMC_InvalidSwPinmuxSelection;
  734. }
  735. }
  736. if (config->addrPortWidth > SEMC_NORFLASH_SRAM_ADDR_PORTWIDTHMAX)
  737. {
  738. return kStatus_SEMC_InvalidAddressPortWidth;
  739. }
  740. }
  741. /* Base control. */
  742. result = SEMC_CovertMemorySize(base, config->memsize_kbytes, &memsize);
  743. if (result != kStatus_Success)
  744. {
  745. return result;
  746. }
  747. tempBRVal = (config->address & SEMC_BR_BA_MASK) | SEMC_BR_MS(memsize) | SEMC_BR_VLD_MASK;
  748. uint32_t tempCtrlVal;
  749. switch (cs)
  750. {
  751. case kSEMC_SRAM_CS0:
  752. base->BR[6] = tempBRVal;
  753. break;
  754. #if defined(FSL_FEATURE_SEMC_SUPPORT_SRAM_COUNT) && (FSL_FEATURE_SEMC_SUPPORT_SRAM_COUNT > 0x01U)
  755. case kSEMC_SRAM_CS1:
  756. base->BR9 = tempBRVal;
  757. break;
  758. case kSEMC_SRAM_CS2:
  759. base->BR10 = tempBRVal;
  760. break;
  761. case kSEMC_SRAM_CS3:
  762. base->BR11 = tempBRVal;
  763. break;
  764. #endif /* FSL_FEATURE_SEMC_SUPPORT_SRAM_COUNT */
  765. default:
  766. assert(NULL);
  767. break;
  768. }
  769. /* PSRAM0 SRAMCRx timing setting. */
  770. if (kSEMC_SRAM_CS0 == cs)
  771. {
  772. #if defined(FSL_FEATURE_SEMC_SUPPORT_SRAM_COUNT) && (FSL_FEATURE_SEMC_SUPPORT_SRAM_COUNT > 0x01U)
  773. /* Ready/wait(WAITEN and WAITSP) feature is only for async mode. */
  774. if (kSEMC_AsyncMode == config->syncMode)
  775. {
  776. tempCtrlVal = SEMC_SRAMCR0_PS(config->portSize) |
  777. #if defined(SEMC_SRAMCR4_SYNCEN_MASK) && (SEMC_SRAMCR4_SYNCEN_MASK)
  778. SEMC_SRAMCR4_SYNCEN(config->syncMode) |
  779. #endif /* SEMC_SRAMCR4_SYNCEN_MASK */
  780. #if defined(SEMC_SRAMCR0_WAITEN_MASK) && (SEMC_SRAMCR0_WAITEN_MASK)
  781. SEMC_SRAMCR0_WAITEN(config->waitEnable) |
  782. #endif /* SEMC_SRAMCR0_WAITEN_MASK */
  783. #if defined(SEMC_SRAMCR0_WAITSP_MASK) && (SEMC_SRAMCR0_WAITSP_MASK)
  784. SEMC_SRAMCR0_WAITSP(config->waitSample) |
  785. #endif /* SEMC_SRAMCR0_WAITSP_MASK */
  786. SEMC_SRAMCR0_BL(config->burstLen) | SEMC_SRAMCR0_AM(config->addrMode) |
  787. SEMC_SRAMCR0_ADVP(config->advActivePolarity) |
  788. #if defined(SEMC_SRAMCR4_ADVH_MASK) && (SEMC_SRAMCR4_ADVH_MASK)
  789. SEMC_SRAMCR4_ADVH(config->advLevelCtrl) |
  790. #endif /* SEMC_SRAMCR4_ADVH_MASK */
  791. SEMC_SRAMCR0_COL_MASK;
  792. }
  793. else
  794. #endif /* FSL_FEATURE_SEMC_SUPPORT_SRAM_COUNT */
  795. {
  796. tempCtrlVal = SEMC_SRAMCR0_PS(config->portSize) |
  797. #if defined(SEMC_SRAMCR4_SYNCEN_MASK) && (SEMC_SRAMCR4_SYNCEN_MASK)
  798. SEMC_SRAMCR4_SYNCEN(config->syncMode) |
  799. #endif /* SEMC_SRAMCR4_SYNCEN_MASK */
  800. SEMC_SRAMCR0_BL(config->burstLen) | SEMC_SRAMCR0_AM(config->addrMode) |
  801. SEMC_SRAMCR0_ADVP(config->advActivePolarity) |
  802. #if defined(SEMC_SRAMCR4_ADVH_MASK) && (SEMC_SRAMCR4_ADVH_MASK)
  803. SEMC_SRAMCR4_ADVH(config->advLevelCtrl) |
  804. #endif /* SEMC_SRAMCR4_ADVH_MASK */
  805. SEMC_SRAMCR0_COL_MASK;
  806. }
  807. base->SRAMCR0 = tempCtrlVal;
  808. }
  809. #if defined(FSL_FEATURE_SEMC_SUPPORT_SRAM_COUNT) && (FSL_FEATURE_SEMC_SUPPORT_SRAM_COUNT > 0x01U)
  810. /* PSRAM1~PSRAM3 SRAMCRx timing setting. */
  811. else
  812. {
  813. /* Ready/wait(WAITEN and WAITSP) feature is only for async mode. */
  814. if (kSEMC_AsyncMode == config->syncMode)
  815. {
  816. tempCtrlVal = SEMC_SRAMCR4_PS(config->portSize) | SEMC_SRAMCR4_SYNCEN(config->syncMode) |
  817. SEMC_SRAMCR4_WAITEN(config->waitEnable) | SEMC_SRAMCR4_WAITSP(config->waitSample) |
  818. SEMC_SRAMCR4_BL(config->burstLen) | SEMC_SRAMCR4_AM(config->addrMode) |
  819. SEMC_SRAMCR4_ADVP(config->advActivePolarity) | SEMC_SRAMCR4_ADVH(config->advLevelCtrl) |
  820. SEMC_SRAMCR4_COL_MASK;
  821. }
  822. else
  823. {
  824. tempCtrlVal = SEMC_SRAMCR4_PS(config->portSize) | SEMC_SRAMCR4_SYNCEN(config->syncMode) |
  825. SEMC_SRAMCR4_BL(config->burstLen) | SEMC_SRAMCR4_AM(config->addrMode) |
  826. SEMC_SRAMCR4_ADVP(config->advActivePolarity) | SEMC_SRAMCR4_ADVH(config->advLevelCtrl) |
  827. SEMC_SRAMCR4_COL_MASK;
  828. }
  829. base->SRAMCR4 = tempCtrlVal;
  830. }
  831. #endif /* FSL_FEATURE_SEMC_SUPPORT_SRAM_COUNT */
  832. #if defined(FSL_FEATURE_SEMC_HAS_DELAY_CHAIN_CONTROL) && (FSL_FEATURE_SEMC_HAS_DELAY_CHAIN_CONTROL)
  833. uint32_t tempDelayChain = base->DCCR;
  834. /* Configure delay chain. */
  835. switch (cs)
  836. {
  837. case kSEMC_SRAM_CS0:
  838. tempDelayChain &= ~(SEMC_DCCR_SRAM0VAL_MASK | SEMC_DCCR_SRAM0EN_MASK);
  839. base->DCCR =
  840. tempDelayChain | SEMC_DCCR_SRAM0VAL((uint32_t)config->delayChain - 0x01U) | SEMC_DCCR_SRAM0EN_MASK;
  841. break;
  842. #if defined(FSL_FEATURE_SEMC_SUPPORT_SRAM_COUNT) && (FSL_FEATURE_SEMC_SUPPORT_SRAM_COUNT > 0x01U)
  843. case kSEMC_SRAM_CS1:
  844. SUPPRESS_FALL_THROUGH_WARNING();
  845. case kSEMC_SRAM_CS2:
  846. SUPPRESS_FALL_THROUGH_WARNING();
  847. case kSEMC_SRAM_CS3:
  848. tempDelayChain &= ~(SEMC_DCCR_SRAMXVAL_MASK | SEMC_DCCR_SRAMXEN_MASK);
  849. base->DCCR =
  850. tempDelayChain | SEMC_DCCR_SRAMXVAL((uint32_t)config->delayChain - 0x01U) | SEMC_DCCR_SRAMXEN_MASK;
  851. break;
  852. #endif /* FSL_FEATURE_SEMC_SUPPORT_SRAM_COUNT */
  853. default:
  854. assert(NULL);
  855. break;
  856. }
  857. #endif /* FSL_FEATURE_SEMC_HAS_DELAY_CHAIN_CONTROL */
  858. if (kSEMC_SRAM_CS0 == cs)
  859. {
  860. timing = SEMC_SRAMCR1_CES(SEMC_ConvertTiming(config->tCeSetup_Ns, clkSrc_Hz));
  861. timing |= SEMC_SRAMCR1_CEH(SEMC_ConvertTiming(config->tCeHold_Ns, clkSrc_Hz));
  862. timing |= SEMC_SRAMCR1_AS(SEMC_ConvertTiming(config->tAddrSetup_Ns, clkSrc_Hz));
  863. timing |= SEMC_SRAMCR1_AH(SEMC_ConvertTiming(config->tAddrHold_Ns, clkSrc_Hz));
  864. timing |= SEMC_SRAMCR1_WEL(SEMC_ConvertTiming(config->tWeLow_Ns, clkSrc_Hz));
  865. timing |= SEMC_SRAMCR1_WEH(SEMC_ConvertTiming(config->tWeHigh_Ns, clkSrc_Hz));
  866. timing |= SEMC_SRAMCR1_REL(SEMC_ConvertTiming(config->tReLow_Ns, clkSrc_Hz));
  867. timing |= SEMC_SRAMCR1_REH(SEMC_ConvertTiming(config->tReHigh_Ns, clkSrc_Hz));
  868. /* SRAMCR1 timing setting. */
  869. base->SRAMCR1 = timing;
  870. timing = 0x00U;
  871. #if defined(FSL_FEATURE_SEMC_HAS_SRAM_WDS_TIME) && (FSL_FEATURE_SEMC_HAS_SRAM_WDS_TIME)
  872. timing |= SEMC_SRAMCR2_WDS(SEMC_ConvertTiming(config->tWriteSetup_Ns, clkSrc_Hz));
  873. #endif
  874. #if defined(FSL_FEATURE_SEMC_HAS_SRAM_WDH_TIME) && (FSL_FEATURE_SEMC_HAS_SRAM_WDH_TIME)
  875. timing |= SEMC_SRAMCR2_WDH((uint32_t)SEMC_ConvertTiming(config->tWriteHold_Ns, clkSrc_Hz) + 1UL);
  876. #endif
  877. timing |= SEMC_SRAMCR2_TA(SEMC_ConvertTiming(config->tTurnAround_Ns, clkSrc_Hz));
  878. timing |= SEMC_SRAMCR2_AWDH(SEMC_ConvertTiming(config->tAddr2WriteHold_Ns, clkSrc_Hz));
  879. #if defined(FSL_FEATURE_SEMC_HAS_SRAM_LC_TIME) && (FSL_FEATURE_SEMC_HAS_SRAM_LC_TIME)
  880. timing |= SEMC_SRAMCR2_LC(config->latencyCount);
  881. #endif
  882. #if defined(FSL_FEATURE_SEMC_HAS_SRAM_RD_TIME) && (FSL_FEATURE_SEMC_HAS_SRAM_RD_TIME)
  883. timing |= SEMC_SRAMCR2_RD((uint32_t)config->readCycle - 1UL);
  884. #endif
  885. timing |= SEMC_SRAMCR2_CEITV(SEMC_ConvertTiming(config->tCeInterval_Ns, clkSrc_Hz));
  886. #if defined(FSL_FEATURE_SEMC_HAS_SRAM_RDH_TIME) && (FSL_FEATURE_SEMC_HAS_SRAM_RDH_TIME)
  887. timing |= SEMC_SRAMCR2_RDH((uint32_t)SEMC_ConvertTiming(config->readHoldTime_Ns, clkSrc_Hz) + 0x01U);
  888. #endif /* FSL_FEATURE_SEMC_HAS_SRAM_RDH_TIME */
  889. /* SRAMCR2 timing setting. */
  890. base->SRAMCR2 = timing;
  891. }
  892. #if defined(FSL_FEATURE_SEMC_SUPPORT_SRAM_COUNT) && (FSL_FEATURE_SEMC_SUPPORT_SRAM_COUNT > 0x01U)
  893. else
  894. {
  895. timing = SEMC_SRAMCR5_CES(SEMC_ConvertTiming(config->tCeSetup_Ns, clkSrc_Hz));
  896. timing |= SEMC_SRAMCR5_CEH(SEMC_ConvertTiming(config->tCeHold_Ns, clkSrc_Hz));
  897. timing |= SEMC_SRAMCR5_AS(SEMC_ConvertTiming(config->tAddrSetup_Ns, clkSrc_Hz));
  898. timing |= SEMC_SRAMCR5_AH(SEMC_ConvertTiming(config->tAddrHold_Ns, clkSrc_Hz));
  899. timing |= SEMC_SRAMCR5_WEL(SEMC_ConvertTiming(config->tWeLow_Ns, clkSrc_Hz));
  900. timing |= SEMC_SRAMCR5_WEH(SEMC_ConvertTiming(config->tWeHigh_Ns, clkSrc_Hz));
  901. timing |= SEMC_SRAMCR5_REL(SEMC_ConvertTiming(config->tReLow_Ns, clkSrc_Hz));
  902. timing |= SEMC_SRAMCR5_REH(SEMC_ConvertTiming(config->tReHigh_Ns, clkSrc_Hz));
  903. /* SRAMCR5 timing setting. */
  904. base->SRAMCR5 = timing;
  905. timing = 0x00U;
  906. #if defined(FSL_FEATURE_SEMC_HAS_SRAM_WDS_TIME) && (FSL_FEATURE_SEMC_HAS_SRAM_WDS_TIME)
  907. timing = SEMC_SRAMCR6_WDS(SEMC_ConvertTiming(config->tWriteSetup_Ns, clkSrc_Hz));
  908. #endif
  909. #if defined(FSL_FEATURE_SEMC_HAS_SRAM_WDH_TIME) && (FSL_FEATURE_SEMC_HAS_SRAM_WDH_TIME)
  910. timing |= SEMC_SRAMCR6_WDH((uint32_t)SEMC_ConvertTiming(config->tWriteHold_Ns, clkSrc_Hz) + 1UL);
  911. #endif
  912. timing |= SEMC_SRAMCR6_TA(SEMC_ConvertTiming(config->tTurnAround_Ns, clkSrc_Hz));
  913. timing |= SEMC_SRAMCR6_AWDH(SEMC_ConvertTiming(config->tAddr2WriteHold_Ns, clkSrc_Hz));
  914. #if defined(FSL_FEATURE_SEMC_HAS_SRAM_LC_TIME) && (FSL_FEATURE_SEMC_HAS_SRAM_LC_TIME)
  915. timing |= SEMC_SRAMCR6_LC(config->latencyCount);
  916. #endif
  917. #if defined(FSL_FEATURE_SEMC_HAS_SRAM_RD_TIME) && (FSL_FEATURE_SEMC_HAS_SRAM_RD_TIME)
  918. timing |= SEMC_SRAMCR6_RD((uint32_t)config->readCycle - 1UL);
  919. #endif
  920. timing |= SEMC_SRAMCR6_CEITV(SEMC_ConvertTiming(config->tCeInterval_Ns, clkSrc_Hz));
  921. #if defined(FSL_FEATURE_SEMC_HAS_SRAM_RDH_TIME) && (FSL_FEATURE_SEMC_HAS_SRAM_RDH_TIME)
  922. timing |= SEMC_SRAMCR6_RDH((uint32_t)SEMC_ConvertTiming(config->readHoldTime_Ns, clkSrc_Hz) + 0x01U);
  923. #endif /* FSL_FEATURE_SEMC_HAS_SRAM_RDH_TIME */
  924. /* SRAMCR6 timing setting. */
  925. base->SRAMCR6 = timing;
  926. }
  927. #endif /* FSL_FEATURE_SEMC_SUPPORT_SRAM_COUNT */
  928. return result;
  929. }
  930. /*!
  931. * brief Configures DBI controller in SEMC.
  932. *
  933. * param base SEMC peripheral base address.
  934. * param config The dbi configuration.
  935. * param clkSrc_Hz The SEMC clock frequency.
  936. */
  937. status_t SEMC_ConfigureDBI(SEMC_Type *base, semc_dbi_config_t *config, uint32_t clkSrc_Hz)
  938. {
  939. assert(config != NULL);
  940. uint8_t memsize;
  941. status_t result;
  942. uint32_t timing;
  943. if ((config->address < SEMC_STARTADDRESS) || (config->address > SEMC_ENDADDRESS))
  944. {
  945. return kStatus_SEMC_InvalidBaseAddress;
  946. }
  947. uint32_t iocReg = base->IOCR & (~(SEMC_IOCR_PINMUXBITWIDTH << (uint32_t)config->csxPinMux));
  948. uint32_t muxCsx = (config->csxPinMux == kSEMC_MUXRDY) ?
  949. (SEMC_IOCR_DBI_CSX - 1U) :
  950. ((config->csxPinMux == kSEMC_MUXA8) ? SEMC_IOCR_DBI_CSX_A8 : SEMC_IOCR_DBI_CSX);
  951. /* IOMUX setting. */
  952. base->IOCR = iocReg | (muxCsx << (uint32_t)config->csxPinMux);
  953. /* Base control. */
  954. result = SEMC_CovertMemorySize(base, config->memsize_kbytes, &memsize);
  955. if (result != kStatus_Success)
  956. {
  957. return result;
  958. }
  959. base->BR[7] = (config->address & SEMC_BR_BA_MASK) | SEMC_BR_MS(memsize) | SEMC_BR_VLD_MASK;
  960. /* DBICR0 timing setting. */
  961. base->DBICR0 =
  962. SEMC_DBICR0_PS(config->portSize) | SEMC_DBICR0_BL(config->burstLen) | SEMC_DBICR0_COL(config->columnAddrBitNum);
  963. timing = SEMC_DBICR1_CES(SEMC_ConvertTiming(config->tCsxSetup_Ns, clkSrc_Hz));
  964. timing |= SEMC_DBICR1_CEH(SEMC_ConvertTiming(config->tCsxHold_Ns, clkSrc_Hz));
  965. timing |= SEMC_DBICR1_WEL(SEMC_ConvertTiming(config->tWexLow_Ns, clkSrc_Hz));
  966. timing |= SEMC_DBICR1_WEH(SEMC_ConvertTiming(config->tWexHigh_Ns, clkSrc_Hz));
  967. timing |= SEMC_DBICR1_REL(SEMC_ConvertTiming(config->tRdxLow_Ns, clkSrc_Hz));
  968. timing |= SEMC_DBICR1_REH(SEMC_ConvertTiming(config->tRdxHigh_Ns, clkSrc_Hz));
  969. #if defined(SEMC_DBICR1_CEITV_MASK)
  970. timing |= SEMC_DBICR1_CEITV(SEMC_ConvertTiming(config->tCsxInterval_Ns, clkSrc_Hz));
  971. #endif /* SEMC_DBICR1_CEITV_MASK */
  972. /* DBICR1 timing setting. */
  973. base->DBICR1 = timing;
  974. #if defined(SEMC_DBICR2_CEITV_MASK)
  975. timing = SEMC_DBICR2_CEITV(SEMC_ConvertTiming(config->tCsxInterval_Ns, clkSrc_Hz));
  976. /* DBICR2 timing setting. */
  977. base->DBICR2 = timing;
  978. #endif /* SEMC_DBICR2_CEITV_MASK */
  979. return SEMC_ConfigureIPCommand(base, ((uint8_t)config->portSize + 1U));
  980. }
  981. /*!
  982. * brief SEMC IP command access.
  983. *
  984. * param base SEMC peripheral base address.
  985. * param type SEMC memory type. refer to "semc_mem_type_t"
  986. * param address SEMC device address.
  987. * param command SEMC IP command.
  988. * For NAND device, we should use the SEMC_BuildNandIPCommand to get the right nand command.
  989. * For NOR/DBI device, take refer to "semc_ipcmd_nor_dbi_t".
  990. * For SRAM device, take refer to "semc_ipcmd_sram_t".
  991. * For SDRAM device, take refer to "semc_ipcmd_sdram_t".
  992. * param write Data for write access.
  993. * param read Data pointer for read data out.
  994. */
  995. status_t SEMC_SendIPCommand(
  996. SEMC_Type *base, semc_mem_type_t type, uint32_t address, uint32_t command, uint32_t write, uint32_t *read)
  997. {
  998. uint32_t cmdMode;
  999. bool readCmd = false;
  1000. bool writeCmd = false;
  1001. status_t result;
  1002. /* Clear status bit */
  1003. base->INTR |= SEMC_INTR_IPCMDDONE_MASK;
  1004. /* Set address. */
  1005. base->IPCR0 = address;
  1006. /* Check command mode. */
  1007. cmdMode = (uint32_t)command & 0x0FU;
  1008. switch (type)
  1009. {
  1010. case kSEMC_MemType_NAND:
  1011. readCmd = (cmdMode == (uint32_t)kSEMC_NANDCM_CommandAddressRead) ||
  1012. (cmdMode == (uint32_t)kSEMC_NANDCM_CommandRead) || (cmdMode == (uint32_t)kSEMC_NANDCM_Read);
  1013. writeCmd = (cmdMode == (uint32_t)kSEMC_NANDCM_CommandAddressWrite) ||
  1014. (cmdMode == (uint32_t)kSEMC_NANDCM_CommandWrite) || (cmdMode == (uint32_t)kSEMC_NANDCM_Write);
  1015. break;
  1016. case kSEMC_MemType_NOR:
  1017. case kSEMC_MemType_8080:
  1018. readCmd = (cmdMode == (uint32_t)kSEMC_NORDBICM_Read);
  1019. writeCmd = (cmdMode == (uint32_t)kSEMC_NORDBICM_Write);
  1020. break;
  1021. case kSEMC_MemType_SRAM:
  1022. readCmd = (cmdMode == (uint32_t)kSEMC_SRAMCM_ArrayRead) || (cmdMode == (uint32_t)kSEMC_SRAMCM_RegRead);
  1023. writeCmd = (cmdMode == (uint32_t)kSEMC_SRAMCM_ArrayWrite) || (cmdMode == (uint32_t)kSEMC_SRAMCM_RegWrite);
  1024. break;
  1025. case kSEMC_MemType_SDRAM:
  1026. readCmd = (cmdMode == (uint32_t)kSEMC_SDRAMCM_Read);
  1027. writeCmd = (cmdMode == (uint32_t)kSEMC_SDRAMCM_Write) || (cmdMode == (uint32_t)kSEMC_SDRAMCM_Modeset);
  1028. break;
  1029. default:
  1030. assert(false);
  1031. break;
  1032. }
  1033. if (writeCmd)
  1034. {
  1035. /* Set data. */
  1036. base->IPTXDAT = write;
  1037. }
  1038. /* Set command code. */
  1039. base->IPCMD = command | SEMC_IPCMD_KEY(SEMC_IPCOMMANDMAGICKEY);
  1040. /* Wait for command done. */
  1041. result = SEMC_IsIPCommandDone(base);
  1042. if (result != kStatus_Success)
  1043. {
  1044. return result;
  1045. }
  1046. if (readCmd)
  1047. {
  1048. /* Get the read data */
  1049. *read = base->IPRXDAT;
  1050. }
  1051. return kStatus_Success;
  1052. }
  1053. /*!
  1054. * brief SEMC NAND device memory write through IP command.
  1055. *
  1056. * param base SEMC peripheral base address.
  1057. * param address SEMC NAND device address.
  1058. * param data Data for write access.
  1059. * param size_bytes Data length.
  1060. */
  1061. status_t SEMC_IPCommandNandWrite(SEMC_Type *base, uint32_t address, uint8_t *data, uint32_t size_bytes)
  1062. {
  1063. assert(data != NULL);
  1064. status_t result = kStatus_Success;
  1065. uint16_t ipCmd;
  1066. uint32_t tempData = 0;
  1067. /* Write command built */
  1068. ipCmd = SEMC_BuildNandIPCommand(0, kSEMC_NANDAM_ColumnRow, kSEMC_NANDCM_Write);
  1069. while (size_bytes >= SEMC_IPCOMMANDDATASIZEBYTEMAX)
  1070. {
  1071. /* Configure IP command data size. */
  1072. (void)SEMC_ConfigureIPCommand(base, SEMC_IPCOMMANDDATASIZEBYTEMAX);
  1073. result = SEMC_SendIPCommand(base, kSEMC_MemType_NAND, address, ipCmd, *(uint32_t *)(void *)data, NULL);
  1074. if (result != kStatus_Success)
  1075. {
  1076. break;
  1077. }
  1078. data += SEMC_IPCOMMANDDATASIZEBYTEMAX;
  1079. size_bytes -= SEMC_IPCOMMANDDATASIZEBYTEMAX;
  1080. }
  1081. if ((result == kStatus_Success) && (size_bytes != 0x00U))
  1082. {
  1083. (void)SEMC_ConfigureIPCommand(base, (uint8_t)size_bytes);
  1084. while (size_bytes != 0x00U)
  1085. {
  1086. size_bytes--;
  1087. tempData <<= SEMC_BYTE_NUMBIT;
  1088. tempData |= data[size_bytes];
  1089. }
  1090. result = SEMC_SendIPCommand(base, kSEMC_MemType_NAND, address, ipCmd, tempData, NULL);
  1091. }
  1092. return result;
  1093. }
  1094. /*!
  1095. * brief SEMC NAND device memory read through IP command.
  1096. *
  1097. * param base SEMC peripheral base address.
  1098. * param address SEMC NAND device address.
  1099. * param data Data pointer for data read out.
  1100. * param size_bytes Data length.
  1101. */
  1102. status_t SEMC_IPCommandNandRead(SEMC_Type *base, uint32_t address, uint8_t *data, uint32_t size_bytes)
  1103. {
  1104. assert(data != NULL);
  1105. status_t result = kStatus_Success;
  1106. uint16_t ipCmd;
  1107. uint32_t tempData = 0;
  1108. /* Configure IP command data size. */
  1109. (void)SEMC_ConfigureIPCommand(base, SEMC_IPCOMMANDDATASIZEBYTEMAX);
  1110. /* Read command built */
  1111. ipCmd = SEMC_BuildNandIPCommand(0, kSEMC_NANDAM_ColumnRow, kSEMC_NANDCM_Read);
  1112. while (size_bytes >= SEMC_IPCOMMANDDATASIZEBYTEMAX)
  1113. {
  1114. result = SEMC_SendIPCommand(base, kSEMC_MemType_NAND, address, ipCmd, 0, (uint32_t *)(void *)data);
  1115. if (result != kStatus_Success)
  1116. {
  1117. break;
  1118. }
  1119. data += SEMC_IPCOMMANDDATASIZEBYTEMAX;
  1120. size_bytes -= SEMC_IPCOMMANDDATASIZEBYTEMAX;
  1121. }
  1122. if ((result == kStatus_Success) && (size_bytes != 0x00U))
  1123. {
  1124. (void)SEMC_ConfigureIPCommand(base, (uint8_t)size_bytes);
  1125. result = SEMC_SendIPCommand(base, kSEMC_MemType_NAND, address, ipCmd, 0, &tempData);
  1126. while (size_bytes != 0x00U)
  1127. {
  1128. size_bytes--;
  1129. *(data + size_bytes) = (uint8_t)((tempData >> (SEMC_BYTE_NUMBIT * size_bytes)) & 0xFFU);
  1130. }
  1131. }
  1132. return result;
  1133. }
  1134. /*!
  1135. * brief SEMC NOR device memory read through IP command.
  1136. *
  1137. * param base SEMC peripheral base address.
  1138. * param address SEMC NOR device address.
  1139. * param data Data pointer for data read out.
  1140. * param size_bytes Data length.
  1141. */
  1142. status_t SEMC_IPCommandNorRead(SEMC_Type *base, uint32_t address, uint8_t *data, uint32_t size_bytes)
  1143. {
  1144. assert(data != NULL);
  1145. uint32_t tempData = 0;
  1146. status_t result = kStatus_Success;
  1147. uint8_t dataSize = (uint8_t)base->NORCR0 & SEMC_NORCR0_PS_MASK;
  1148. /* Configure IP command data size. */
  1149. (void)SEMC_ConfigureIPCommand(base, SEMC_IPCOMMANDDATASIZEBYTEMAX);
  1150. while (size_bytes >= SEMC_IPCOMMANDDATASIZEBYTEMAX)
  1151. {
  1152. result = SEMC_SendIPCommand(base, kSEMC_MemType_NOR, address, (uint32_t)kSEMC_NORDBICM_Read, 0,
  1153. (uint32_t *)(void *)data);
  1154. if (result != kStatus_Success)
  1155. {
  1156. break;
  1157. }
  1158. data += SEMC_IPCOMMANDDATASIZEBYTEMAX;
  1159. size_bytes -= SEMC_IPCOMMANDDATASIZEBYTEMAX;
  1160. }
  1161. if ((result == kStatus_Success) && (size_bytes != 0x00U))
  1162. {
  1163. (void)SEMC_ConfigureIPCommand(base, (uint8_t)size_bytes);
  1164. result = SEMC_SendIPCommand(base, kSEMC_MemType_NOR, address, (uint16_t)kSEMC_NORDBICM_Read, 0, &tempData);
  1165. while (size_bytes != 0x00U)
  1166. {
  1167. size_bytes--;
  1168. *(data + size_bytes) = (uint8_t)((tempData >> (SEMC_BYTE_NUMBIT * size_bytes)) & 0xFFU);
  1169. }
  1170. }
  1171. (void)SEMC_ConfigureIPCommand(base, dataSize);
  1172. return result;
  1173. }
  1174. /*!
  1175. * brief SEMC NOR device memory write through IP command.
  1176. *
  1177. * param base SEMC peripheral base address.
  1178. * param address SEMC NOR device address.
  1179. * param data Data for write access.
  1180. * param size_bytes Data length.
  1181. */
  1182. status_t SEMC_IPCommandNorWrite(SEMC_Type *base, uint32_t address, uint8_t *data, uint32_t size_bytes)
  1183. {
  1184. assert(data != NULL);
  1185. uint32_t tempData = 0;
  1186. status_t result = kStatus_Success;
  1187. uint8_t dataSize = (uint8_t)base->NORCR0 & SEMC_NORCR0_PS_MASK;
  1188. /* Write command built */
  1189. while (size_bytes >= SEMC_IPCOMMANDDATASIZEBYTEMAX)
  1190. {
  1191. /* Configure IP command data size. */
  1192. (void)SEMC_ConfigureIPCommand(base, SEMC_IPCOMMANDDATASIZEBYTEMAX);
  1193. result = SEMC_SendIPCommand(base, kSEMC_MemType_NOR, address, (uint16_t)kSEMC_NORDBICM_Write,
  1194. *(uint32_t *)(void *)data, NULL);
  1195. if (result != kStatus_Success)
  1196. {
  1197. break;
  1198. }
  1199. size_bytes -= SEMC_IPCOMMANDDATASIZEBYTEMAX;
  1200. data += SEMC_IPCOMMANDDATASIZEBYTEMAX;
  1201. }
  1202. if ((result == kStatus_Success) && (size_bytes != 0x00U))
  1203. {
  1204. (void)SEMC_ConfigureIPCommand(base, (uint8_t)size_bytes);
  1205. while (size_bytes != 0x00U)
  1206. {
  1207. tempData |= ((uint32_t) * (data + size_bytes - 1U) << ((size_bytes - 1U) * SEMC_BYTE_NUMBIT));
  1208. size_bytes--;
  1209. }
  1210. result = SEMC_SendIPCommand(base, kSEMC_MemType_NOR, address, (uint16_t)kSEMC_NORDBICM_Write, tempData, NULL);
  1211. }
  1212. (void)SEMC_ConfigureIPCommand(base, dataSize);
  1213. return result;
  1214. }