PDL for FM4  Version1.2
Peripheral Driverl Library for FM4
dt.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 /******************************************************************************/
52 /******************************************************************************/
53 /* Include files */
54 /******************************************************************************/
55 #include "pdl.h"
56 
57 #if (defined(PDL_PERIPHERAL_DT_ACTIVE))
58 
64 
65 /******************************************************************************/
66 /* Local pre-processor symbols/macros ('#define') */
67 /******************************************************************************/
68 
69 /******************************************************************************/
70 /* Global variable definitions (declared in header file with 'extern') */
71 /******************************************************************************/
72 
75 {
76 #if (PDL_PERIPHERAL_ENABLE_DT0 == PDL_ON)
77  {
78  &DT0, /* pstcInstance */
79  {NULL} /* stcInternData (not initialized yet) */
80  }
81 #endif
82 };
83 
84 /******************************************************************************/
85 /* Local type definitions ('typedef') */
86 /******************************************************************************/
87 
88 
89 /******************************************************************************/
90 /* Local function prototypes ('static') */
91 /******************************************************************************/
92 static stc_dt_intern_data_t* DtGetInternDataPtr(volatile stc_dtn_t** ppstcDt,
93  uint8_t u8Ch);
94 static void Dt_InitIrq(void);
95 static void Dt_DeInitIrq(void);
96 
97 /******************************************************************************/
98 /* Local variable definitions ('static') */
99 /******************************************************************************/
100 
101 /*****************************************************************************/
102 /* Function implementation - global ('extern') and local ('static') */
103 /*****************************************************************************/
104 
117  uint8_t u8Ch)
118 {
119  stc_dt_intern_data_t* pstcDtInternData = NULL;
120  uint32_t u32Instance;
121 
122  /* check for channel */
123  if ((NULL != ppstcDt)
124  && (NULL != *ppstcDt)
125  && (DtMaxChannels > u8Ch)
126  )
127  {
128  /* Get ptr to internal data struct ... */
129  for (u32Instance = 0; u32Instance < (uint32_t)DtInstanceIndexMax; u32Instance++)
130  {
131  if (*ppstcDt == m_astcDtInstanceDataLut[u32Instance].pstcInstance)
132  {
133  /* Set actual address of register list of current channel */
134  *ppstcDt = &((*ppstcDt)[u8Ch]);
135  pstcDtInternData = &m_astcDtInstanceDataLut[u32Instance].stcInternData;
136  break;
137  }
138  }
139  }
140 
141  return (pstcDtInternData);
142 } /* DtGetInternDataPtr */
143 
150 static void Dt_InitIrq(void)
151 {
152 #if (PDL_PERIPHERAL_ENABLE_DT0 == PDL_ON)
153  NVIC_ClearPendingIRQ(DT1_2_IRQn);
154  NVIC_EnableIRQ(DT1_2_IRQn);
155  NVIC_SetPriority(DT1_2_IRQn, PDL_IRQ_LEVEL_DT0);
156 #endif
157 } /* Dt_InitIrq */
158 
164 static void Dt_DeInitIrq(void)
165 {
166 #if (PDL_PERIPHERAL_ENABLE_DT0 == PDL_ON)
167  NVIC_ClearPendingIRQ(DT1_2_IRQn);
168  NVIC_DisableIRQ(DT1_2_IRQn);
169  NVIC_SetPriority(DT1_2_IRQn, PDL_DEFAULT_INTERRUPT_LEVEL);
170 #endif
171 } /* Dt_DeInitIrq */
172 
188 void DtIrqHandler(uint8_t u8Ch)
189 {
190  volatile stc_dtn_t* pstcDt;
191  stc_dt_intern_data_t* pstcDtInternData;
192 
193  pstcDt = &DT0;
194  /* Get actual address of register list of current channel */
195  pstcDt = (volatile stc_dtn_t*)(&((pstcDt)[u8Ch]));
196  pstcDtInternData = &m_astcDtInstanceDataLut[0].stcInternData;
197 
198  if (TRUE == pstcDt->TIMERXRIS_f.TIMERXRIS) /* Timer 0 interrupt? */
199  {
200  pstcDt->TIMERXINTCLR = 0; /* Clear interrupt */
201 
202  /* Check for callback function pointer */
203  if (NULL != pstcDtInternData->pfnIntCallbackIntern[u8Ch])
204  {
205  pstcDtInternData->pfnIntCallbackIntern[u8Ch]() ;
206  }
207  }
208 } /* DtIrqHandler */
209 
229  uint8_t u8Ch
230  )
231 {
232  en_result_t enResult;
233  /* Pointer to Dual Timer instance register area */
234  volatile stc_dtn_t* pstcDt;
235  /* Pointer to internal data */
236  stc_dt_intern_data_t* pstcDtInternData;
237  stc_dtim_timerXcontrol_field_t stcTIMERXCTRL = { 0 }; /* Preset to zero */
238 
239  pstcDt = &DT0;
240  /*-------- Initialize internal data -------*/
241  /* Get pointer to internal data structure and check channel... */
242  pstcDtInternData = DtGetInternDataPtr(&pstcDt, u8Ch);
243  /* ... and check for NULL */
244  if ((NULL == pstcDtInternData)
245  || (NULL == pstcConfig)
246  )
247  {
248  enResult = ErrorInvalidParameter;
249  }
250  else
251  {
252  enResult = Ok;
253  /*-------- Configure the DT timer -------*/
254  pstcDt->TIMERXCONTROL = 0; /* Disable everything */
255 
256  /* Set Timer Mode */
257  switch (pstcConfig->u8Mode)
258  {
259  /* Free run mode */
260  case DtFreeRun:
261  stcTIMERXCTRL.TIMERMODE = FALSE;
262  stcTIMERXCTRL.ONESHOT = FALSE;
263  break;
264  /* Periodic mode */
265  case DtPeriodic:
266  stcTIMERXCTRL.TIMERMODE = TRUE;
267  stcTIMERXCTRL.ONESHOT = FALSE;
268  break;
269  /* One shot mode */
270  case DtOneShot:
271  stcTIMERXCTRL.TIMERMODE = FALSE;
272  stcTIMERXCTRL.ONESHOT = TRUE;
273  break;
274  default:
275  enResult = ErrorInvalidParameter;
276  break;
277  }
278 
279  /* Set Prescaler */
280  switch (pstcConfig->u8PrescalerDiv)
281  {
282  /* Clock/1 */
283  case DtPrescalerDiv1:
284  stcTIMERXCTRL.TIMERPRE = DT_PRE_TIMER_DIV_1;
285  break;
286  /* Clock/16 */
287  case DtPrescalerDiv16:
288  stcTIMERXCTRL.TIMERPRE = DT_PRE_TIMER_DIV_16;
289  break;
290  /* Clock/256 */
291  case DtPrescalerDiv256:
292  stcTIMERXCTRL.TIMERPRE = DT_PRE_TIMER_DIV_256;
293  break;
294  default:
295  enResult = ErrorInvalidParameter;
296  break;
297  }
298 
299  /* Set Counter Size */
300  switch (pstcConfig->u8CounterSize)
301  {
302  /* 16bit */
303  case DtCounterSize16:
304  stcTIMERXCTRL.TIMERSIZE = FALSE;
305  break;
306  /* 32bit */
307  case DtCounterSize32:
308  stcTIMERXCTRL.TIMERSIZE = TRUE;
309  break;
310  default:
311  enResult = ErrorInvalidParameter;
312  break;
313  }
314  }
315 
316  if (Ok == enResult)
317  {
318  /* Set control register */
319  pstcDt->TIMERXCONTROL_f = stcTIMERXCTRL;
320 
321  /* Initialize (clear) callback */
322  pstcDtInternData->pfnIntCallbackIntern[u8Ch] = NULL;
323  }
324 
325  return (enResult);
326 } /* Dt_Init */
327 
345 en_result_t Dt_DeInit(uint8_t u8Ch)
346 {
347  en_result_t enResult;
348  /* Pointer to Dual Timer instance register area */
349  volatile stc_dtn_t* pstcDt;
350  /* Pointer to internal data */
351  stc_dt_intern_data_t* pstcDtInternData;
352 
353  pstcDt = &DT0;
354  /*-------- Initialize internal data -------*/
355  /* Get pointer to internal data structure and check channel... */
356  pstcDtInternData = DtGetInternDataPtr(&pstcDt, u8Ch);
357  /* ... and check for NULL */
358  if (NULL == pstcDtInternData)
359  {
360  enResult = ErrorInvalidParameter;
361  }
362  else
363  {
364  /* De-Init DT Interrupt */
365  Dt_DeInitIrq();
366 
367  /* Clear all registers */
368  pstcDt->TIMERXCONTROL = 0;
369 
370  pstcDt->TIMERXLOAD = 0;
371  pstcDt->TIMERXINTCLR = 1;
372  pstcDt->TIMERXBGLOAD = 0;
373 
374  /* Clear callback */
375  pstcDtInternData->pfnIntCallbackIntern[u8Ch] = NULL;
376  enResult = Ok;
377  }
378 
379  return (enResult);
380 } /* Dt_DeInit */
381 
398 {
399  en_result_t enResult;
400  /* Pointer to Dual Timer instance register area */
401  volatile stc_dtn_t* pstcDt;
402  /* Pointer to internal data */
403  stc_dt_intern_data_t* pstcDtInternData;
404 
405  enResult = ErrorInvalidParameter;
406 
407  pstcDt = &DT0;
408  /* Get pointer to internal data structure and check channel... */
409  pstcDtInternData = DtGetInternDataPtr(&pstcDt, u8Ch);
410  /* ... and check for NULL */
411  if (NULL != pstcDtInternData)
412  {
413  /* Enable timer counter */
414  pstcDt->TIMERXCONTROL_f.TIMEREN = TRUE;
415  enResult = Ok;
416  }
417 
418  return (enResult);
419 } /* Dt_EnableCount */
420 
437 {
438  en_result_t enResult;
439  /* Pointer to Dual Timer instance register area */
440  volatile stc_dtn_t* pstcDt;
441  /* Pointer to internal data */
442  stc_dt_intern_data_t* pstcDtInternData;
443 
444  enResult = ErrorInvalidParameter;
445 
446  pstcDt = &DT0;
447  /* Get pointer to internal data structure and check channel... */
448  pstcDtInternData = DtGetInternDataPtr(&pstcDt, u8Ch);
449  /* ... and check for NULL */
450  if (NULL != pstcDtInternData)
451  {
452  /* Disable timer counter */
453  pstcDt->TIMERXCONTROL_f.TIMEREN = FALSE;
454  enResult = Ok;
455  }
456 
457  return (enResult);
458 } /* Dt_DisableCount */
459 
478  uint8_t u8Ch
479  )
480 {
481  en_result_t enResult;
482  /* Pointer to Dual Timer instance register area */
483  volatile stc_dtn_t* pstcDt;
484  /* Pointer to internal data */
485  stc_dt_intern_data_t* pstcDtInternData;
486 
487  enResult = ErrorInvalidParameter;
488 
489  pstcDt = &DT0;
490  /* Get pointer to internal data structure and check channel... */
491  pstcDtInternData = DtGetInternDataPtr(&pstcDt, u8Ch);
492  /* ... and check for NULL */
493  if (NULL != pstcDtInternData)
494  {
495  /* Set callback function */
496  pstcDtInternData->pfnIntCallbackIntern[u8Ch] = pfnIntCallback;
497  /* Enable interrupt */
498  pstcDt->TIMERXCONTROL_f.INTENABLE = TRUE;
499  /* Enable IRQ */
500  Dt_InitIrq();
501  enResult = Ok;
502  }
503 
504  return (enResult);
505 } /* Dt_EnableInt */
506 
523 {
524  en_result_t enResult;
525  /* Pointer to Dual Timer instance register area */
526  volatile stc_dtn_t* pstcDt;
527  /* Pointer to internal data */
528  stc_dt_intern_data_t* pstcDtInternData;
529 
530  enResult = ErrorInvalidParameter;
531 
532  pstcDt = &DT0;
533  /* Get pointer to internal data structure and check channel... */
534  pstcDtInternData = DtGetInternDataPtr(&pstcDt, u8Ch);
535  /* ... and check for NULL */
536  if (NULL != pstcDtInternData)
537  {
538  /* Disable IRQ */
539  Dt_DeInitIrq();
540  /* Disable interrupt */
541  pstcDt->TIMERXCONTROL_f.INTENABLE = FALSE;
542  /* Clear callback function */
543  pstcDtInternData->pfnIntCallbackIntern[u8Ch] = NULL;
544  enResult = Ok;
545  }
546 
547  return (enResult);
548 } /* Dt_DisableInt */
549 
561 {
562  /* Pointer to Dual Timer instance register area */
563  volatile stc_dtn_t* pstcDt;
564  boolean_t bRetVal = FALSE;
565  /* Pointer to internal data */
566  stc_dt_intern_data_t* pstcDtInternData;
567 
568  pstcDt = &DT0;
569  /* Get pointer to internal data structure and check channel... */
570  pstcDtInternData = DtGetInternDataPtr(&pstcDt, u8Ch);
571  /* ... and check for NULL */
572  if (NULL != pstcDtInternData)
573  {
574  /* Check the interrupt status */
575  if (TRUE == pstcDt->TIMERXRIS_f.TIMERXRIS)
576  {
577  bRetVal = TRUE;
578  }
579  }
580 
581  return (bRetVal);
582 } /* Dt_GetIntFlag */
583 
595 {
596  /* Pointer to Dual Timer instance register area */
597  volatile stc_dtn_t* pstcDt;
598  boolean_t bRetVal = FALSE;
599  /* Pointer to internal data */
600  stc_dt_intern_data_t* pstcDtInternData;
601 
602  pstcDt = &DT0;
603  /* Get pointer to internal data structure and check channel... */
604  pstcDtInternData = DtGetInternDataPtr(&pstcDt, u8Ch);
605  /* ... and check for NULL */
606  if (NULL != pstcDtInternData)
607  {
608  /* Check the mask interrupt status */
609  if (TRUE == pstcDt->TIMERXMIS_f.TIMERXMIS)
610  {
611  bRetVal = TRUE;
612  }
613  }
614 
615  return (bRetVal);
616 } /* Dt_GetMaskIntFlag */
617 
633 {
634  en_result_t enResult;
635  /* Pointer to Dual Timer instance register area */
636  volatile stc_dtn_t* pstcDt;
637  /* Pointer to internal data */
638  stc_dt_intern_data_t* pstcDtInternData;
639 
640  enResult = ErrorInvalidParameter;
641 
642  pstcDt = &DT0;
643  /* Get pointer to internal data structure and check channel... */
644  pstcDtInternData = DtGetInternDataPtr(&pstcDt, u8Ch);
645  /* ... and check for NULL */
646  if (NULL != pstcDtInternData)
647  {
648  /* Clear the interrupt status */
649  pstcDt->TIMERXINTCLR = 1;
650  enResult = Ok;
651  }
652 
653  return (enResult);
654 } /* Dt_ClrIntFlag */
655 
671 en_result_t Dt_WriteLoadVal(uint32_t u32LoadVal,
672  uint8_t u8Ch
673  )
674 {
675  en_result_t enResult;
676  /* Pointer to Dual Timer instance register area */
677  volatile stc_dtn_t* pstcDt;
678  /* Pointer to internal data */
679  stc_dt_intern_data_t* pstcDtInternData;
680 
681  enResult = ErrorInvalidParameter;
682 
683  pstcDt = &DT0;
684  /* Get pointer to internal data structure and check channel... */
685  pstcDtInternData = DtGetInternDataPtr(&pstcDt, u8Ch);
686  /* ... and check for NULL */
687  if (NULL != pstcDtInternData)
688  {
689  /* 16bit mode */
690  if (FALSE == pstcDt->TIMERXCONTROL_f.TIMERSIZE)
691  {
692  u32LoadVal &= 0x0000FFFF;
693  }
694  /* Write load value to register */
695  pstcDt->TIMERXLOAD = u32LoadVal;
696  enResult = Ok;
697  }
698 
699  return (enResult);
700 } /* Dt_WriteLoadVal */
701 
717 en_result_t Dt_WriteBgLoadVal(uint32_t u32BgLoadVal,
718  uint8_t u8Ch
719  )
720 {
721  en_result_t enResult;
722  /* Pointer to Dual Timer instance register area */
723  volatile stc_dtn_t* pstcDt;
724  /* Pointer to internal data */
725  stc_dt_intern_data_t* pstcDtInternData;
726 
727  enResult = ErrorInvalidParameter;
728 
729  pstcDt = &DT0;
730  /* Get pointer to internal data structure and check channel... */
731  pstcDtInternData = DtGetInternDataPtr(&pstcDt, u8Ch);
732  /* ... and check for NULL */
733  if (NULL != pstcDtInternData)
734  {
735  /* 16bit mode */
736  if (FALSE == pstcDt->TIMERXCONTROL_f.TIMERSIZE)
737  {
738  u32BgLoadVal &= 0x0000FFFF;
739  }
740  /* Write back-ground load value to register */
741  pstcDt->TIMERXBGLOAD = u32BgLoadVal;
742  enResult = Ok;
743  }
744 
745  return (enResult);
746 } /* Dt_WriteBgLoadVal */
747 
758 uint32_t Dt_ReadCurCntVal(uint8_t u8Ch)
759 {
760  /* Pointer to Dual Timer instance register area */
761  volatile stc_dtn_t* pstcDt;
762  uint32_t u32DtValue = 0;
763  /* Pointer to internal data */
764  stc_dt_intern_data_t* pstcDtInternData;
765 
766  pstcDt = &DT0;
767  /* Get pointer to internal data structure and check channel... */
768  pstcDtInternData = DtGetInternDataPtr(&pstcDt, u8Ch);
769  /* ... and check for NULL */
770  if (NULL != pstcDtInternData)
771  {
772  /* Read current count value */
773  u32DtValue = pstcDt->TIMERXVALUE;
774  /* 16bit mode */
775  if (FALSE == pstcDt->TIMERXCONTROL_f.TIMERSIZE)
776  {
777  u32DtValue &= 0x0000FFFF;
778  }
779  }
780 
781  return (u32DtValue);
782 } /* Dt_ReadCurCntVal */
783 
785 
786 #endif /* #if (defined(PDL_PERIPHERAL_DT_ACTIVE)) */
787 
788 /******************************************************************************/
789 /* EOF (not truncated) */
790 /******************************************************************************/
791