EFM32 Happy Gecko Software Documentation  efm32hg-doc-5.1.2
em_timer.c
Go to the documentation of this file.
1 /***************************************************************************/
33 #include "em_timer.h"
34 #if defined(TIMER_COUNT) && (TIMER_COUNT > 0)
35 
36 #include "em_assert.h"
37 
38 /***************************************************************************/
43 /***************************************************************************/
55 /*******************************************************************************
56  ************************** GLOBAL FUNCTIONS *******************************
57  ******************************************************************************/
58 
59 /***************************************************************************/
76 void TIMER_Init(TIMER_TypeDef *timer, const TIMER_Init_TypeDef *init)
77 {
78  EFM_ASSERT(TIMER_REF_VALID(timer));
79 
80  /* Stop timer if specified to be disabled (dosn't hurt if already stopped) */
81  if (!(init->enable))
82  {
83  timer->CMD = TIMER_CMD_STOP;
84  }
85 
86  /* Reset counter */
87  timer->CNT = _TIMER_CNT_RESETVALUE;
88 
89  timer->CTRL = ((uint32_t)(init->prescale) << _TIMER_CTRL_PRESC_SHIFT)
90  | ((uint32_t)(init->clkSel) << _TIMER_CTRL_CLKSEL_SHIFT)
91  | ((uint32_t)(init->fallAction) << _TIMER_CTRL_FALLA_SHIFT)
92  | ((uint32_t)(init->riseAction) << _TIMER_CTRL_RISEA_SHIFT)
93  | ((uint32_t)(init->mode) << _TIMER_CTRL_MODE_SHIFT)
94  | (init->debugRun ? TIMER_CTRL_DEBUGRUN : 0)
95  | (init->dmaClrAct ? TIMER_CTRL_DMACLRACT : 0)
96  | (init->quadModeX4 ? TIMER_CTRL_QDM_X4 : 0)
97  | (init->oneShot ? TIMER_CTRL_OSMEN : 0)
98 
99 #if defined(TIMER_CTRL_X2CNT) && defined(TIMER_CTRL_ATI)
100  | (init->count2x ? TIMER_CTRL_X2CNT : 0)
101  | (init->ati ? TIMER_CTRL_ATI : 0)
102 #endif
103  | (init->sync ? TIMER_CTRL_SYNC : 0);
104 
105  /* Start timer if specified to be enabled (dosn't hurt if already started) */
106  if (init->enable)
107  {
108  timer->CMD = TIMER_CMD_START;
109  }
110 }
111 
112 
113 /***************************************************************************/
131  unsigned int ch,
132  const TIMER_InitCC_TypeDef *init)
133 {
134  EFM_ASSERT(TIMER_REF_VALID(timer));
135  EFM_ASSERT(TIMER_CH_VALID(ch));
136 
137  timer->CC[ch].CTRL =
138  ((uint32_t)(init->eventCtrl) << _TIMER_CC_CTRL_ICEVCTRL_SHIFT)
139  | ((uint32_t)(init->edge) << _TIMER_CC_CTRL_ICEDGE_SHIFT)
140  | ((uint32_t)(init->prsSel) << _TIMER_CC_CTRL_PRSSEL_SHIFT)
141  | ((uint32_t)(init->cufoa) << _TIMER_CC_CTRL_CUFOA_SHIFT)
142  | ((uint32_t)(init->cofoa) << _TIMER_CC_CTRL_COFOA_SHIFT)
143  | ((uint32_t)(init->cmoa) << _TIMER_CC_CTRL_CMOA_SHIFT)
144  | ((uint32_t)(init->mode) << _TIMER_CC_CTRL_MODE_SHIFT)
145  | (init->filter ? TIMER_CC_CTRL_FILT_ENABLE : 0)
146  | (init->prsInput ? TIMER_CC_CTRL_INSEL_PRS : 0)
147  | (init->coist ? TIMER_CC_CTRL_COIST : 0)
148  | (init->outInvert ? TIMER_CC_CTRL_OUTINV : 0);
149 }
150 
151 
152 #if defined(_TIMER_DTCTRL_MASK)
153 /***************************************************************************/
164 {
165  EFM_ASSERT(TIMER0 == timer);
166 
167  /* Make sure the DTI unit is disabled while initializing. */
168  TIMER_EnableDTI (timer, false);
169 
170  /* Setup the DTCTRL register.
171  The enable bit will be set at the end of the function if specified. */
172  timer->DTCTRL =
173  (init->autoRestart ? TIMER_DTCTRL_DTDAS : 0)
174  | (init->activeLowOut ? TIMER_DTCTRL_DTIPOL : 0)
176  | (init->enablePrsSource ? TIMER_DTCTRL_DTPRSEN : 0)
177  | ((uint32_t)(init->prsSel) << _TIMER_DTCTRL_DTPRSSEL_SHIFT);
178 
179  /* Setup the DTTIME register. */
180  timer->DTTIME =
181  ((uint32_t)(init->prescale) << _TIMER_DTTIME_DTPRESC_SHIFT)
182  | ((uint32_t)(init->riseTime) << _TIMER_DTTIME_DTRISET_SHIFT)
183  | ((uint32_t)(init->fallTime) << _TIMER_DTTIME_DTFALLT_SHIFT);
184 
185  /* Setup the DTFC register. */
186  timer->DTFC =
191  | ((uint32_t)(init->faultAction) << _TIMER_DTFC_DTFA_SHIFT)
192  | ((uint32_t)(init->faultSourcePrsSel0) << _TIMER_DTFC_DTPRS0FSEL_SHIFT)
193  | ((uint32_t)(init->faultSourcePrsSel1) << _TIMER_DTFC_DTPRS1FSEL_SHIFT);
194 
195  /* Setup the DTOGEN register. */
196  timer->DTOGEN = init->outputsEnableMask;
197 
198  /* Clear any previous DTI faults. */
199  TIMER_ClearDTIFault(timer, TIMER_GetDTIFault(timer));
200 
201  /* Enable/disable before returning. */
202  TIMER_EnableDTI (timer, init->enable);
203 }
204 #endif
205 
206 
207 /***************************************************************************/
219 {
220  int i;
221 
222  EFM_ASSERT(TIMER_REF_VALID(timer));
223 
224  /* Make sure disabled first, before resetting other registers */
225  timer->CMD = TIMER_CMD_STOP;
226 
227  timer->CTRL = _TIMER_CTRL_RESETVALUE;
228  timer->IEN = _TIMER_IEN_RESETVALUE;
229  timer->IFC = _TIMER_IFC_MASK;
230  timer->TOPB = _TIMER_TOPB_RESETVALUE;
231  /* Write TOP after TOPB to invalidate TOPB (clear TIMER_STATUS_TOPBV) */
232  timer->TOP = _TIMER_TOP_RESETVALUE;
233  timer->CNT = _TIMER_CNT_RESETVALUE;
234  /* Do not reset route register, setting should be done independently */
235  /* (Note: ROUTE register may be locked by DTLOCK register.) */
236 
237  for (i = 0; TIMER_CH_VALID(i); i++)
238  {
239  timer->CC[i].CTRL = _TIMER_CC_CTRL_RESETVALUE;
240  timer->CC[i].CCV = _TIMER_CC_CCV_RESETVALUE;
241  timer->CC[i].CCVB = _TIMER_CC_CCVB_RESETVALUE;
242  }
243 
244  /* Reset dead time insertion module, no effect on timers without DTI */
245 
246 #if defined(TIMER_DTLOCK_LOCKKEY_UNLOCK)
247  /* Unlock DTI registers first in case locked */
249 
252  timer->DTFC = _TIMER_DTFC_RESETVALUE;
255 #endif
256 }
257 
258 
261 #endif /* defined(TIMER_COUNT) && (TIMER_COUNT > 0) */
#define _TIMER_DTTIME_DTRISET_SHIFT
uint32_t outputsEnableMask
Definition: em_timer.h:432
#define TIMER_CC_CTRL_FILT_ENABLE
#define _TIMER_CC_CTRL_RESETVALUE
__IOM uint32_t TOP
Definition: efm32hg_timer.h:50
TIMER_CC_TypeDef CC[3]
Definition: efm32hg_timer.h:56
TIMER_ClkSel_TypeDef clkSel
Definition: em_timer.h:266
#define TIMER_DTCTRL_DTCINV
bool enableFaultSourcePrsSel0
Definition: em_timer.h:441
TIMER_OutputAction_TypeDef cufoa
Definition: em_timer.h:350
Emlib peripheral API "assert" implementation.
TIMER_PRSSEL_TypeDef faultSourcePrsSel0
Definition: em_timer.h:444
void TIMER_Reset(TIMER_TypeDef *timer)
Reset TIMER to same state as after a HW reset.
Definition: em_timer.c:218
__IOM uint32_t DTTIME
Definition: efm32hg_timer.h:60
#define _TIMER_IEN_RESETVALUE
TIMER_InputAction_TypeDef fallAction
Definition: em_timer.h:278
#define _TIMER_CC_CTRL_CMOA_SHIFT
#define _TIMER_DTCTRL_DTPRSSEL_SHIFT
#define TIMER_CTRL_QDM_X4
#define _TIMER_CTRL_PRESC_SHIFT
#define _TIMER_CC_CTRL_CUFOA_SHIFT
__IOM uint32_t TOPB
Definition: efm32hg_timer.h:51
__OM uint32_t DTFAULTC
Definition: efm32hg_timer.h:64
#define _TIMER_CC_CTRL_ICEDGE_SHIFT
#define TIMER_CTRL_SYNC
Definition: efm32hg_timer.h:88
#define _TIMER_DTTIME_DTFALLT_SHIFT
Timer/counter (TIMER) peripheral API.
#define TIMER_DTFC_DTPRS1FEN
#define TIMER_CC_CTRL_INSEL_PRS
TIMER_Mode_TypeDef mode
Definition: em_timer.h:284
#define TIMER_DTCTRL_DTIPOL
__STATIC_INLINE void TIMER_EnableDTI(TIMER_TypeDef *timer, bool enable)
Enable or disable DTI unit.
Definition: em_timer.h:693
TIMER_CCMode_TypeDef mode
Definition: em_timer.h:359
void TIMER_InitDTI(TIMER_TypeDef *timer, const TIMER_InitDTI_TypeDef *init)
Initialize the TIMER DTI unit.
Definition: em_timer.c:163
#define TIMER_DTFC_DTDBGFEN
#define _TIMER_DTFC_DTPRS0FSEL_SHIFT
__IOM uint32_t DTOGEN
Definition: efm32hg_timer.h:62
#define TIMER0
#define _TIMER_CNT_RESETVALUE
#define TIMER_CC_CTRL_OUTINV
#define TIMER_DTCTRL_DTPRSEN
TIMER_PRSSEL_TypeDef prsSel
Definition: em_timer.h:347
#define TIMER_CMD_START
TIMER_Edge_TypeDef edge
Definition: em_timer.h:341
#define TIMER_DTLOCK_LOCKKEY_UNLOCK
#define _TIMER_DTFC_DTFA_SHIFT
#define TIMER_CTRL_DEBUGRUN
#define _TIMER_CTRL_CLKSEL_SHIFT
#define _TIMER_TOPB_RESETVALUE
#define _TIMER_DTFC_RESETVALUE
__IOM uint32_t DTCTRL
Definition: efm32hg_timer.h:59
TIMER_DtiFaultAction_TypeDef faultAction
Definition: em_timer.h:453
#define TIMER_CTRL_DMACLRACT
#define _TIMER_DTCTRL_RESETVALUE
__IOM uint32_t IFC
Definition: efm32hg_timer.h:49
TIMER_PRSSEL_TypeDef faultSourcePrsSel1
Definition: em_timer.h:450
unsigned int riseTime
Definition: em_timer.h:423
#define _TIMER_TOP_RESETVALUE
TIMER_Event_TypeDef eventCtrl
Definition: em_timer.h:338
__IOM uint32_t CNT
Definition: efm32hg_timer.h:52
__IOM uint32_t CCV
#define _TIMER_CTRL_RISEA_SHIFT
TIMER_Prescale_TypeDef prescale
Definition: em_timer.h:420
#define _TIMER_CTRL_FALLA_SHIFT
#define _TIMER_CTRL_RESETVALUE
Definition: efm32hg_timer.h:74
#define TIMER_DTCTRL_DTDAS
#define TIMER_CTRL_ATI
__IOM uint32_t CCVB
unsigned int fallTime
Definition: em_timer.h:426
TIMER_PRSSEL_TypeDef prsSel
Definition: em_timer.h:417
TIMER_Prescale_TypeDef prescale
Definition: em_timer.h:263
#define _TIMER_CC_CTRL_PRSSEL_SHIFT
TIMER_OutputAction_TypeDef cofoa
Definition: em_timer.h:353
__IOM uint32_t DTLOCK
Definition: efm32hg_timer.h:65
void TIMER_Init(TIMER_TypeDef *timer, const TIMER_Init_TypeDef *init)
Initialize TIMER.
Definition: em_timer.c:76
#define _TIMER_CTRL_MODE_SHIFT
Definition: efm32hg_timer.h:76
#define _TIMER_CC_CTRL_ICEVCTRL_SHIFT
#define TIMER_CTRL_X2CNT
#define TIMER_CC_CTRL_COIST
#define TIMER_DTFC_DTLOCKUPFEN
__IOM uint32_t CMD
Definition: efm32hg_timer.h:44
TIMER_InputAction_TypeDef riseAction
Definition: em_timer.h:281
#define TIMER_CMD_STOP
#define _TIMER_DTOGEN_RESETVALUE
#define _TIMER_CC_CCV_RESETVALUE
__STATIC_INLINE void TIMER_ClearDTIFault(TIMER_TypeDef *timer, uint32_t flags)
Clear DTI fault source flags.
Definition: em_timer.h:740
#define TIMER_CTRL_OSMEN
Definition: efm32hg_timer.h:93
#define _TIMER_CC_CCVB_RESETVALUE
#define _TIMER_DTFAULTC_MASK
#define _TIMER_DTTIME_RESETVALUE
bool enableFaultSourceCoreLockup
Definition: em_timer.h:435
bool enableFaultSourcePrsSel1
Definition: em_timer.h:447
#define _TIMER_CC_CTRL_MODE_SHIFT
#define _TIMER_DTFC_DTPRS1FSEL_SHIFT
void TIMER_InitCC(TIMER_TypeDef *timer, unsigned int ch, const TIMER_InitCC_TypeDef *init)
Initialize TIMER compare/capture channel.
Definition: em_timer.c:130
TIMER_OutputAction_TypeDef cmoa
Definition: em_timer.h:356
__STATIC_INLINE uint32_t TIMER_GetDTIFault(TIMER_TypeDef *timer)
Get DTI fault source flags status.
Definition: em_timer.h:722
#define TIMER_DTFC_DTPRS0FEN
__IOM uint32_t CTRL
Definition: efm32hg_timer.h:43
__IOM uint32_t IEN
Definition: efm32hg_timer.h:46
#define _TIMER_CC_CTRL_COFOA_SHIFT
__IOM uint32_t CTRL
#define _TIMER_DTTIME_DTPRESC_SHIFT
__IOM uint32_t DTFC
Definition: efm32hg_timer.h:61
bool enableFaultSourceDebugger
Definition: em_timer.h:438
#define _TIMER_IFC_MASK