em_timer.c

Go to the documentation of this file.
00001 /***************************************************************************/
00034 #include "em_timer.h"
00035 #if defined(TIMER_COUNT) && (TIMER_COUNT > 0)
00036 
00037 #include "em_assert.h"
00038 
00039 /***************************************************************************/
00044 /***************************************************************************/
00056 /*******************************************************************************
00057  **************************   GLOBAL FUNCTIONS   *******************************
00058  ******************************************************************************/
00059 
00060 /***************************************************************************/
00077 void TIMER_Init(TIMER_TypeDef *timer, const TIMER_Init_TypeDef *init)
00078 {
00079   EFM_ASSERT(TIMER_REF_VALID(timer));
00080 
00081   /* Stop timer if specified to be disabled (dosn't hurt if already stopped) */
00082   if (!(init->enable))
00083   {
00084     timer->CMD = TIMER_CMD_STOP;
00085   }
00086 
00087   /* Reset counter */
00088   timer->CNT = _TIMER_CNT_RESETVALUE;
00089 
00090   timer->CTRL =
00091     ((uint32_t)(init->prescale) << _TIMER_CTRL_PRESC_SHIFT) |
00092     ((uint32_t)(init->clkSel) << _TIMER_CTRL_CLKSEL_SHIFT) |
00093     ((uint32_t)(init->fallAction) << _TIMER_CTRL_FALLA_SHIFT) |
00094     ((uint32_t)(init->riseAction) << _TIMER_CTRL_RISEA_SHIFT) |
00095     ((uint32_t)(init->mode) << _TIMER_CTRL_MODE_SHIFT) |
00096     (init->debugRun               ?   TIMER_CTRL_DEBUGRUN  : 0) |
00097     (init->dmaClrAct              ?   TIMER_CTRL_DMACLRACT : 0) |
00098     (init->quadModeX4             ?   TIMER_CTRL_QDM_X4    : 0) |
00099     (init->oneShot                ?   TIMER_CTRL_OSMEN     : 0) |
00100 
00101 #if defined( TIMER_CTRL_X2CNT ) && defined( TIMER_CTRL_ATI )
00102     (init->count2x                ?   TIMER_CTRL_X2CNT     : 0) |
00103     (init->ati                    ?   TIMER_CTRL_ATI       : 0) |
00104 #endif
00105     (init->sync                   ?   TIMER_CTRL_SYNC      : 0);
00106 
00107   /* Start timer if specified to be enabled (dosn't hurt if already started) */
00108   if (init->enable)
00109   {
00110     timer->CMD = TIMER_CMD_START;
00111   }
00112 }
00113 
00114 
00115 /***************************************************************************/
00132 void TIMER_InitCC(TIMER_TypeDef *timer,
00133                   unsigned int ch,
00134                   const TIMER_InitCC_TypeDef *init)
00135 {
00136   EFM_ASSERT(TIMER_REF_VALID(timer));
00137   EFM_ASSERT(TIMER_CH_VALID(ch));
00138 
00139   timer->CC[ch].CTRL =
00140     ((uint32_t)(init->eventCtrl) << _TIMER_CC_CTRL_ICEVCTRL_SHIFT) |
00141     ((uint32_t)(init->edge) << _TIMER_CC_CTRL_ICEDGE_SHIFT) |
00142     ((uint32_t)(init->prsSel) << _TIMER_CC_CTRL_PRSSEL_SHIFT) |
00143     ((uint32_t)(init->cufoa) << _TIMER_CC_CTRL_CUFOA_SHIFT) |
00144     ((uint32_t)(init->cofoa) << _TIMER_CC_CTRL_COFOA_SHIFT) |
00145     ((uint32_t)(init->cmoa) << _TIMER_CC_CTRL_CMOA_SHIFT) |
00146     ((uint32_t)(init->mode) << _TIMER_CC_CTRL_MODE_SHIFT) |
00147     (init->filter                ?   TIMER_CC_CTRL_FILT_ENABLE : 0) |
00148     (init->prsInput              ?   TIMER_CC_CTRL_INSEL_PRS   : 0) |
00149     (init->coist                 ?   TIMER_CC_CTRL_COIST       : 0) |
00150     (init->outInvert             ?   TIMER_CC_CTRL_OUTINV      : 0);
00151 }
00152 
00153 
00154 #ifdef _TIMER_DTCTRL_MASK
00155 /***************************************************************************/
00165 void TIMER_InitDTI(TIMER_TypeDef *timer, const TIMER_InitDTI_TypeDef *init)
00166 {
00167   EFM_ASSERT(TIMER0 == timer);
00168 
00169   /* Make sure the DTI unit is disabled while initializing. */
00170   TIMER_EnableDTI (timer, false);
00171 
00172   /* Setup the DTCTRL register.
00173      The enable bit will be set at the end of the function if specified. */
00174   timer->DTCTRL =
00175     (init->autoRestart             ?  TIMER_DTCTRL_DTDAS      : 0) |
00176     (init->activeLowOut            ?  TIMER_DTCTRL_DTIPOL     : 0) |
00177     (init->invertComplementaryOut  ?  TIMER_DTCTRL_DTCINV     : 0) |
00178     (init->enablePrsSource         ?  TIMER_DTCTRL_DTPRSEN    : 0) |
00179     ((uint32_t)(init->prsSel)     << _TIMER_DTCTRL_DTPRSSEL_SHIFT);
00180 
00181   /* Setup the DTTIME register. */
00182   timer->DTTIME =
00183     ((uint32_t)(init->prescale)   << _TIMER_DTTIME_DTPRESC_SHIFT) |
00184     ((uint32_t)(init->riseTime)   << _TIMER_DTTIME_DTRISET_SHIFT) |
00185     ((uint32_t)(init->fallTime)   << _TIMER_DTTIME_DTFALLT_SHIFT);
00186 
00187   /* Setup the DTFC register. */
00188   timer->DTFC =
00189     (init->enableFaultSourceCoreLockup     ?  TIMER_DTFC_DTLOCKUPFEN     : 0) |
00190     (init->enableFaultSourceDebugger       ?  TIMER_DTFC_DTDBGFEN        : 0) |
00191     (init->enableFaultSourcePrsSel0        ?  TIMER_DTFC_DTPRS0FEN       : 0) |
00192     (init->enableFaultSourcePrsSel1        ?  TIMER_DTFC_DTPRS1FEN       : 0) |
00193     ((uint32_t)(init->faultAction)        << _TIMER_DTFC_DTFA_SHIFT)          |
00194     ((uint32_t)(init->faultSourcePrsSel0) << _TIMER_DTFC_DTPRS0FSEL_SHIFT)    |
00195     ((uint32_t)(init->faultSourcePrsSel1) << _TIMER_DTFC_DTPRS1FSEL_SHIFT);
00196 
00197   /* Setup the DTOGEN register. */
00198   timer->DTOGEN = init->outputsEnableMask;
00199 
00200   /* Clear any previous DTI faults.  */
00201   TIMER_ClearDTIFault(timer, TIMER_GetDTIFault(timer));
00202 
00203   /* Enable/disable before returning. */
00204   TIMER_EnableDTI (timer, init->enable);
00205 }
00206 #endif
00207 
00208 
00209 /***************************************************************************/
00220 void TIMER_Reset(TIMER_TypeDef *timer)
00221 {
00222   int i;
00223 
00224   EFM_ASSERT(TIMER_REF_VALID(timer));
00225 
00226   /* Make sure disabled first, before resetting other registers */
00227   timer->CMD = TIMER_CMD_STOP;
00228 
00229   timer->CTRL = _TIMER_CTRL_RESETVALUE;
00230   timer->IEN  = _TIMER_IEN_RESETVALUE;
00231   timer->IFC  = _TIMER_IFC_MASK;
00232   timer->TOP  = _TIMER_TOP_RESETVALUE;
00233   timer->TOPB = _TIMER_TOPB_RESETVALUE;
00234   timer->CNT  = _TIMER_CNT_RESETVALUE;
00235   /* Do not reset route register, setting should be done independently */
00236   /* (Note: ROUTE register may be locked by DTLOCK register.) */
00237 
00238   for (i = 0; TIMER_CH_VALID(i); i++)
00239   {
00240     timer->CC[i].CTRL = _TIMER_CC_CTRL_RESETVALUE;
00241     timer->CC[i].CCV  = _TIMER_CC_CCV_RESETVALUE;
00242     timer->CC[i].CCVB = _TIMER_CC_CCVB_RESETVALUE;
00243   }
00244 
00245   /* Reset dead time insertion module, no effect on timers without DTI */
00246 
00247 #ifdef TIMER_DTLOCK_LOCKKEY_UNLOCK
00248   /* Unlock DTI registers first in case locked */
00249   timer->DTLOCK = TIMER_DTLOCK_LOCKKEY_UNLOCK;
00250 
00251   timer->DTCTRL   = _TIMER_DTCTRL_RESETVALUE;
00252   timer->DTTIME   = _TIMER_DTTIME_RESETVALUE;
00253   timer->DTFC     = _TIMER_DTFC_RESETVALUE;
00254   timer->DTOGEN   = _TIMER_DTOGEN_RESETVALUE;
00255   timer->DTFAULTC = _TIMER_DTFAULTC_MASK;
00256 #endif
00257 }
00258 
00259 
00262 #endif /* defined(TIMER_COUNT) && (TIMER_COUNT > 0) */