PDL for FM4  Version1.2
Peripheral Driverl Library for FM4
extif.c
Go to the documentation of this file.
1 /*******************************************************************************
2 * Copyright (C) 2013 Spansion LLC. All Rights Reserved.
3 *
4 * This software is owned and published by:
5 * Spansion LLC, 915 DeGuigne Dr. Sunnyvale, CA 94088-3453 ("Spansion").
6 *
7 * BY DOWNLOADING, INSTALLING OR USING THIS SOFTWARE, YOU AGREE TO BE BOUND
8 * BY ALL THE TERMS AND CONDITIONS OF THIS AGREEMENT.
9 *
10 * This software contains source code for use with Spansion
11 * components. This software is licensed by Spansion to be adapted only
12 * for use in systems utilizing Spansion components. Spansion shall not be
13 * responsible for misuse or illegal use of this software for devices not
14 * supported herein. Spansion is providing this software "AS IS" and will
15 * not be responsible for issues arising from incorrect user implementation
16 * of the software.
17 *
18 * SPANSION MAKES NO WARRANTY, EXPRESS OR IMPLIED, ARISING BY LAW OR OTHERWISE,
19 * REGARDING THE SOFTWARE (INCLUDING ANY ACOOMPANYING WRITTEN MATERIALS),
20 * ITS PERFORMANCE OR SUITABILITY FOR YOUR INTENDED USE, INCLUDING,
21 * WITHOUT LIMITATION, THE IMPLIED WARRANTY OF MERCHANTABILITY, THE IMPLIED
22 * WARRANTY OF FITNESS FOR A PARTICULAR PURPOSE OR USE, AND THE IMPLIED
23 * WARRANTY OF NONINFRINGEMENT.
24 * SPANSION SHALL HAVE NO LIABILITY (WHETHER IN CONTRACT, WARRANTY, TORT,
25 * NEGLIGENCE OR OTHERWISE) FOR ANY DAMAGES WHATSOEVER (INCLUDING, WITHOUT
26 * LIMITATION, DAMAGES FOR LOSS OF BUSINESS PROFITS, BUSINESS INTERRUPTION,
27 * LOSS OF BUSINESS INFORMATION, OR OTHER PECUNIARY LOSS) ARISING FROM USE OR
28 * INABILITY TO USE THE SOFTWARE, INCLUDING, WITHOUT LIMITATION, ANY DIRECT,
29 * INDIRECT, INCIDENTAL, SPECIAL OR CONSEQUENTIAL DAMAGES OR LOSS OF DATA,
30 * SAVINGS OR PROFITS,
31 * EVEN IF SPANSION HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
32 * YOU ASSUME ALL RESPONSIBILITIES FOR SELECTION OF THE SOFTWARE TO ACHIEVE YOUR
33 * INTENDED RESULTS, AND FOR THE INSTALLATION OF, USE OF, AND RESULTS OBTAINED
34 * FROM, THE SOFTWARE.
35 *
36 * This software may be replicated in part or whole for the licensed use,
37 * with the restriction that this Disclaimer and Copyright notice must be
38 * included with each copy of this software, whether used in part or whole,
39 * at all times.
40 */
41 /**************************************************************************************************/
57 /******************************************************************************/
58 /* Include files */
59 /******************************************************************************/
60 #include "extif.h"
61 
62 #if (defined(PDL_PERIPHERAL_EXTIF_ACTIVE))
63 
69 
70 /******************************************************************************/
71 /* Local pre-processor symbols/macros ('#define') */
72 /******************************************************************************/
73 
74 /******************************************************************************/
75 /* Global variable definitions (declared in header file with 'extern') */
76 /******************************************************************************/
78 
79 /******************************************************************************/
80 /* Local type definitions ('typedef') */
81 /******************************************************************************/
82 
83 /******************************************************************************/
84 /* Local function prototypes ('static') */
85 /******************************************************************************/
86 
87 /******************************************************************************/
88 /* Local variable definitions ('static') */
89 /******************************************************************************/
90 
91 /******************************************************************************/
92 /* Function implementation - global ('extern') and local ('static') */
93 /******************************************************************************/
94 
102 void ExtifIrqHandler( void )
103 {
104  if ((TRUE == stcExtifInternData.bSdramErrorInterruptEnable) &&
105  (stcExtifInternData.pfnSdramErrorCallback != NULL))
106  {
107  if (1u == FM4_EXBUS->MEMCERR_f.SDER)
108  {
109  FM4_EXBUS->MEMCERR_f.SDER = 1u; // Clear SDRAM error interrupt
110  stcExtifInternData.pfnSdramErrorCallback();
111  }
112  }
113 
114  if ((TRUE == stcExtifInternData.bSramFlashErrorInterruptEnable) &&
115  (stcExtifInternData.pfnSramFlashErrorCallback != NULL))
116  {
117  if (1u == FM4_EXBUS->MEMCERR_f.SFER)
118  {
119  FM4_EXBUS->MEMCERR_f.SFER = 1u; // Clear SRAM/Flash error interrupt
120  stcExtifInternData.pfnSramFlashErrorCallback();
121  }
122  }
123 }
124 
137 en_result_t Extif_InitArea( uint8_t u8Area,
138  stc_extif_area_config_t* pstcConfig
139  )
140 {
141  // Local register predefinitions
142  stc_exbus_mode_field_t stcMODE;
143  stc_exbus_tim_field_t stcTIM;
144  stc_exbus_area_field_t stcAREA;
145  stc_exbus_atim_field_t stcATIM;
146  stc_exbus_sdmode_field_t stcSDMODE;
147  stc_exbus_reftim_field_t stcREFTIM;
148  stc_exbus_pwrdwn_field_t stcPWRDWN;
149  stc_exbus_sdtim_field_t stcSDTIM;
150  stc_exbus_memcerr_field_t stcMEMCERR;
151  stc_exbus_dclkr_field_t stcDCLKR;
152  stc_exbus_amode_field_t stcAMODE;
153  uint8_t u8Dummy;
154 
155  PDL_ZERO_STRUCT(stcMODE);
156  PDL_ZERO_STRUCT(stcTIM);
157  PDL_ZERO_STRUCT(stcAREA);
158  PDL_ZERO_STRUCT(stcATIM);
159  PDL_ZERO_STRUCT(stcSDMODE);
160  PDL_ZERO_STRUCT(stcREFTIM);
161  PDL_ZERO_STRUCT(stcPWRDWN);
162  PDL_ZERO_STRUCT(stcSDTIM);
163  PDL_ZERO_STRUCT(stcMEMCERR);
164  PDL_ZERO_STRUCT(stcDCLKR);
165  PDL_ZERO_STRUCT(stcAMODE);
166 
167  stcExtifInternData.pfnSdramErrorCallback = NULL;
168  stcExtifInternData.pfnSramFlashErrorCallback = NULL;
169 
170  if ((u8Area > 8u) || (NULL == pstcConfig))
171  {
172  return ErrorInvalidParameter;
173  }
174 
175  switch (pstcConfig->enWidth)
176  {
177  case Extif8Bit:
178  stcMODE.WDTH = 0u;
179  break;
180  case Extif16Bit:
181  stcMODE.WDTH = 1u;
182  break;
183  default:
184  return ErrorInvalidParameter;
185  }
186 
187  // MODE Register preparation
188  stcMODE.RBMON = (TRUE == pstcConfig->bReadByteMask) ? 1ul : 0ul ;
189  stcMODE.WEOFF = (TRUE == pstcConfig->bWriteEnableOff) ? 1ul : 0ul ;
190  stcMODE.NAND = (TRUE == pstcConfig->bNandFlash) ? 1ul : 0ul ;
191  stcMODE.PAGE = (TRUE == pstcConfig->bPageAccess) ? 1ul : 0ul ;
192  stcMODE.RDY = (TRUE == pstcConfig->bRdyOn) ? 1ul : 0ul ;
193  stcMODE.RDY = (TRUE == pstcConfig->bRdyOn) ? 1ul : 0ul ;
194  stcMODE.SHRTDOUT = (TRUE == pstcConfig->bStopDataOutAtFirstIdle) ? 1ul : 0ul ;
195  stcMODE.MPXMODE = (TRUE == pstcConfig->bMultiplexMode) ? 1ul : 0ul ;
196  stcMODE.ALEINV = (TRUE == pstcConfig->bAleInvert) ? 1ul : 0ul ;
197  stcMODE.MPXDOFF = (TRUE == pstcConfig->bMpxcsOff) ? 1ul : 0ul ;
198  stcMODE.MPXDOFF = (TRUE == pstcConfig->bAddrOnDataLinesOff) ? 1ul : 0ul ;
199  stcMODE.MPXCSOF = (TRUE == pstcConfig->bMpxcsOff) ? 1ul : 0ul ;
200  stcMODE.MOEXEUP = (TRUE == pstcConfig->bMoexWidthAsFradc) ? 1ul : 0ul ;
201 
202  // TIM Register preparation
203  if ((Extif0Cycle == pstcConfig->enReadAccessCycle) ||
204  (ExtifDisabled == pstcConfig->enReadAccessCycle))
205  {
206  return ErrorInvalidParameter;
207  }
208  stcTIM.RACC = (uint8_t) (pstcConfig->enReadAccessCycle - 1u);
209 
210  if ((Extif16Cycle == pstcConfig->enReadAddressSetupCycle) ||
211  (ExtifDisabled == pstcConfig->enReadAddressSetupCycle))
212  {
213  return ErrorInvalidParameter;
214  }
215  stcTIM.RADC = (uint8_t) pstcConfig->enReadAddressSetupCycle;
216 
217  if ((FALSE == pstcConfig->bPageAccess) && (FALSE == pstcConfig->bMoexWidthAsFradc))
218  {
219  if ((Extif0Cycle == pstcConfig->enFirstReadAddressCycle) ||
220  (ExtifDisabled == pstcConfig->enFirstReadAddressCycle))
221  {
222  return ErrorInvalidParameter;
223  }
224  stcTIM.FRADC = (uint8_t) (pstcConfig->enFirstReadAddressCycle - 1u);
225  }
226  else if ((TRUE == pstcConfig->bPageAccess) && (FALSE == pstcConfig->bMoexWidthAsFradc))
227  {
228  if ((Extif16Cycle == pstcConfig->enFirstReadAddressCycle) ||
229  (ExtifDisabled == pstcConfig->enFirstReadAddressCycle))
230  {
231  return ErrorInvalidParameter;
232  }
233  stcTIM.FRADC = (uint8_t) pstcConfig->enFirstReadAddressCycle;
234  }
235  else
236  {
237  return ErrorInvalidParameter; // Other setting for PAGE and MOEXEUP not allowed!
238  }
239 
240  if ((Extif0Cycle == pstcConfig->enReadIdleCycle) ||
241  (ExtifDisabled == pstcConfig->enReadIdleCycle))
242  {
243  return ErrorInvalidParameter;
244  }
245  stcTIM.RIDLC = (uint8_t) (pstcConfig->enReadIdleCycle - 1u);
246 
247  if ((Extif0Cycle == pstcConfig->enWriteAccessCycle) ||
248  (Extif1Cycle == pstcConfig->enWriteAccessCycle) ||
249  (Extif2Cycle == pstcConfig->enWriteAccessCycle) ||
250  (ExtifDisabled == pstcConfig->enWriteAccessCycle))
251  {
252  return ErrorInvalidParameter;
253  }
254  stcTIM.WACC = (uint8_t) (pstcConfig->enWriteAccessCycle - 1u);
255 
256  if ((Extif16Cycle == pstcConfig->enWriteAddressSetupCycle) ||
257  (Extif0Cycle == pstcConfig->enWriteAddressSetupCycle) ||
258  (ExtifDisabled == pstcConfig->enWriteAddressSetupCycle))
259  {
260  return ErrorInvalidParameter;
261  }
262  stcTIM.WADC = (uint8_t) (pstcConfig->enWriteAddressSetupCycle - 1u);
263 
264  if ((Extif16Cycle == pstcConfig->enWriteEnableCycle) ||
265  (Extif0Cycle == pstcConfig->enWriteEnableCycle) ||
266  (ExtifDisabled == pstcConfig->enWriteEnableCycle))
267  {
268  return ErrorInvalidParameter;
269  }
270  stcTIM.WWEC = (uint8_t) (pstcConfig->enWriteEnableCycle - 1u);
271 
272  if ((Extif0Cycle == pstcConfig->enWriteIdleCycle) ||
273  (ExtifDisabled == pstcConfig->enWriteIdleCycle))
274  {
275  return ErrorInvalidParameter;
276  }
277  stcTIM.WIDLC = (uint8_t) (pstcConfig->enWriteIdleCycle - 1u);
278 
279  // Check timing contraints
280  // WACC >= WADC + WWEC
281  u8Dummy = (uint8_t)stcTIM.WADC; // u8Dummy provided because of volatile
282  u8Dummy += (uint8_t)stcTIM.WWEC; // qualifiers, which cannot be mixed
283  if ((uint8_t)stcTIM.WACC < u8Dummy) // in one expression
284  {
285  return ErrorInvalidParameter;
286  }
287 
288  // RADC < RACC
289  u8Dummy = (uint8_t)stcTIM.RACC; // see above
290  if ((uint8_t)stcTIM.RADC >= u8Dummy)
291  {
292  return ErrorInvalidParameter;
293  }
294 
295  // AREA Register preparation
296  stcAREA.ADDR = (uint8_t) pstcConfig->u8AreaAddress;
297  stcAREA.MASK = (uint8_t) pstcConfig->enAreaMask;
298 
299  // ATIM Register preparation
300  if ((Extif0Cycle == pstcConfig->enAddressLatchCycle) ||
301  (ExtifDisabled == pstcConfig->enAddressLatchCycle))
302  {
303  return ErrorInvalidParameter;
304  }
305  stcATIM.ALC = (uint8_t) (pstcConfig->enAddressLatchCycle - 1u);
306 
307  if ((Extif16Cycle == pstcConfig->enReadAddressSetupCycle) ||
308  (ExtifDisabled == pstcConfig->enReadAddressSetupCycle))
309  {
310  return ErrorInvalidParameter;
311  }
312  stcATIM.ALES = (uint8_t) pstcConfig->enReadAddressSetupCycle;
313 
314  if ((Extif0Cycle == pstcConfig->enAddressLatchWidthCycle) ||
315  (ExtifDisabled == pstcConfig->enAddressLatchWidthCycle))
316  {
317  return ErrorInvalidParameter;
318  }
319  stcATIM.ALEW = (uint8_t) (pstcConfig->enAddressLatchWidthCycle - 1u);
320 
321  // Check for SDMODE and cheip select area 8
322  if ((TRUE == pstcConfig->bSdramEnable) && (8u != u8Area))
323  {
324  return ErrorInvalidMode;
325  }
326 
327  // SDMODE Register preparation
328  stcSDMODE.SDON = (TRUE == pstcConfig->bSdramEnable) ? 1ul : 0ul ;
329  stcSDMODE.PDON = (TRUE == pstcConfig->bSdramPowerDownMode) ? 1ul : 0ul ;
330  stcSDMODE.ROFF = (TRUE == pstcConfig->bSdramRefreshOff) ? 1ul : 0ul ;
331 
332  switch (pstcConfig->enCasel)
333  {
334  case ExtifCas16Bit:
335  stcSDMODE.CASEL = 0ul;
336  break;
337  case ExtifCas32Bit:
338  stcSDMODE.CASEL = 1ul;
339  break;
340  default:
341  return ErrorInvalidParameter;
342  }
343 
344  stcSDMODE.RASEL = (uint8_t) pstcConfig->enRasel;
345  stcSDMODE.BASEL = (uint8_t) pstcConfig->enBasel;
346  stcSDMODE.MSDCLK = (TRUE == pstcConfig->bMpxcsOff) ? 1ul : 0ul ;
347 
348  // REFTIM Register preparation
349  stcREFTIM.REFC = pstcConfig->u16RefreshCount;
350  stcREFTIM.NREF = pstcConfig->u8RefreshNumber;
351  stcREFTIM.PREF = (TRUE == pstcConfig->bRefreshEnable) ? 1ul : 0ul ;
352 
353  // PWRDWN Register preparation
354  stcPWRDWN.PDC = pstcConfig->u16PowerDownCount;
355 
356  // SDTIM Register preparation
357  stcSDTIM.CL = (uint8_t) pstcConfig->enSdramCasLatencyCycle;
358  stcSDTIM.TRC = (uint8_t) pstcConfig->enSdramRasCycle;
359  stcSDTIM.TRP = (uint8_t) pstcConfig->enSdramRasPrechargeCycle;
360  stcSDTIM.TRCD = (uint8_t) pstcConfig->enSdramRasCasDelayCycle;
361  stcSDTIM.TRAS = (uint8_t) pstcConfig->enSdramRasActiveCycle;
362  stcSDTIM.TREFC = (uint8_t) pstcConfig->enSdramRefreshCycle;
363  stcSDTIM.TDPL = (uint8_t) pstcConfig->enSdramPrechargeCycle;
364 
365  // MEMCERR Register preparation and Callback Pointer intern data settings
366  if (TRUE == pstcConfig->bSramFlashErrorInterruptEnable)
367  {
368  stcMEMCERR.SFION = 1u;
369  stcExtifInternData.bSramFlashErrorInterruptEnable = TRUE;
370  stcExtifInternData.pfnSramFlashErrorCallback = pstcConfig->pfnSramFlashErrorCallback;
371  }
372 
373  if (TRUE == pstcConfig->bSdramErrorInterruptEnable)
374  {
375  stcMEMCERR.SDION = 1u;
376  stcExtifInternData.bSdramErrorInterruptEnable = TRUE;
377  stcExtifInternData.pfnSdramErrorCallback = pstcConfig->pfnSdramErrorCallback;
378  }
379 
380  if ((TRUE == pstcConfig->bSramFlashErrorInterruptEnable) ||
381  (TRUE == pstcConfig->bSdramErrorInterruptEnable))
382  {
383  NVIC_ClearPendingIRQ(EXTBUS_ERR_IRQn);
384  NVIC_EnableIRQ(EXTBUS_ERR_IRQn);
385  NVIC_SetPriority(EXTBUS_ERR_IRQn, PDL_IRQ_LEVEL_EXTIF);
386  }
387 
388  // DCLKR Register preparation
389  if ((pstcConfig->u8MclkDivision == 0u) ||
390  (pstcConfig->u8MclkDivision > 16u))
391  {
392  return ErrorInvalidParameter;
393  }
394  else
395  {
396  stcDCLKR.MDIV = (uint8_t) pstcConfig->u8MclkDivision;
397  }
398 
399  stcDCLKR.MCLKON = (TRUE == pstcConfig->bMclkoutEnable) ? 1ul : 0ul ;
400 
401  // AMODE Register preparation
402  stcAMODE.WAEN = (TRUE == pstcConfig->bPrecedReadContinuousWrite) ? 1ul : 0ul ;
403 
404  // Finally setup hardware
405  EXTIF.astcMODE[u8Area] = stcMODE;
406  EXTIF.astcTIM[u8Area] = stcTIM;
407  EXTIF.astcAREA[u8Area] = stcAREA;
408  EXTIF.astcATIM[u8Area] = stcATIM;
409  FM4_EXBUS->SDMODE_f = stcSDMODE;
410  FM4_EXBUS->REFTIM_f = stcREFTIM;
411  FM4_EXBUS->PWRDWN_f = stcPWRDWN;
412  FM4_EXBUS->SDTIM_f = stcSDTIM;
413  FM4_EXBUS->MEMCERR_f = stcMEMCERR;
414  FM4_EXBUS->DCLKR_f = stcDCLKR;
415  FM4_EXBUS->AMODE_f = stcAMODE;
416 
417  return Ok;
418 } // Extif_InitArea
419 
428 {
429  if (TRUE == FM4_EXBUS->EST_f.WERR)
430  {
431  return Error;
432  }
433 
434  return Ok;
435 } // Extif_ReadErrorStatus
436 
443 uint32_t Extif_ReadErrorAddress( void )
444 {
445  return FM4_EXBUS->WEAD;
446 } // Extif_ReadErrorAddress
447 
455 {
456  FM4_EXBUS->ESCLR_f.WERRCLR = 0u;
457 
458  return Ok;
459 } // Extif_ClearErrorStatus
460 
469 {
470  if (TRUE == FM4_EXBUS->SDCMD_f.PEND)
471  {
472  return ErrorNotReady;
473  }
474 
475  return Ok;
476 } // Extif_CheckSdcmdReady
477 
494 en_result_t Extif_SetSdramCommand( uint16_t u16Address,
495  boolean_t bMsdwex,
496  boolean_t bMcasx,
497  boolean_t bMrasx,
498  boolean_t bMcsx8,
499  boolean_t bMadcke
500  )
501 {
502  stc_exbus_sdcmd_field_t stcSDCMD;
503 
505  {
506  return ErrorNotReady;
507  }
508 
509  stcSDCMD.SDAD = u16Address;
510  stcSDCMD.SDWE = bMsdwex;
511  stcSDCMD.SDCAS = bMcasx;
512  stcSDCMD.SDRAS = bMrasx;
513  stcSDCMD.SDCS = bMcsx8;
514  stcSDCMD.SDCKE = bMadcke;
515 
516  FM4_EXBUS->SDCMD_f = stcSDCMD;
517 
518  return Ok;
519 }
521 
522 #endif // #if (defined(PDL_PERIPHERAL_EXTIF_ACTIVE)
523 /******************************************************************************/
524 /* EOF (not truncated) */
525 /******************************************************************************/