28 #define USTIMER_TIMER0 0
29 #define USTIMER_TIMER1 1
30 #define USTIMER_TIMER2 2
31 #define USTIMER_TIMER3 3
32 #define USTIMER_TIMER4 4
33 #define USTIMER_TIMER5 5
34 #define USTIMER_TIMER6 6
36 #define USTIMER_WTIMER0 10
37 #define USTIMER_WTIMER1 11
38 #define USTIMER_WTIMER2 12
39 #define USTIMER_WTIMER3 13
42 #define USTIMER_TIMER USTIMER_TIMER0
45 #if ( USTIMER_TIMER == USTIMER_TIMER0 ) && ( TIMER_COUNT >= 1 )
47 #define TIMER_CLK cmuClock_TIMER0
48 #define TIMER_IRQ TIMER0_IRQn
49 #define TIMER_IRQHandler TIMER0_IRQHandler
51 #elif ( USTIMER_TIMER == USTIMER_TIMER1 ) && ( TIMER_COUNT >= 2 )
53 #define TIMER_CLK cmuClock_TIMER1
54 #define TIMER_IRQ TIMER1_IRQn
55 #define TIMER_IRQHandler TIMER1_IRQHandler
57 #elif ( USTIMER_TIMER == USTIMER_TIMER2 ) && ( TIMER_COUNT >= 3 )
59 #define TIMER_CLK cmuClock_TIMER2
60 #define TIMER_IRQ TIMER2_IRQn
61 #define TIMER_IRQHandler TIMER2_IRQHandler
63 #elif ( USTIMER_TIMER == USTIMER_TIMER3 ) && ( TIMER_COUNT >= 4 )
65 #define TIMER_CLK cmuClock_TIMER3
66 #define TIMER_IRQ TIMER3_IRQn
67 #define TIMER_IRQHandler TIMER3_IRQHandler
69 #elif ( USTIMER_TIMER == USTIMER_TIMER4 ) && ( TIMER_COUNT >= 5 )
71 #define TIMER_CLK cmuClock_TIMER4
72 #define TIMER_IRQ TIMER4_IRQn
73 #define TIMER_IRQHandler TIMER4_IRQHandler
75 #elif ( USTIMER_TIMER == USTIMER_TIMER5 ) && ( TIMER_COUNT >= 6 )
77 #define TIMER_CLK cmuClock_TIMER5
78 #define TIMER_IRQ TIMER5_IRQn
79 #define TIMER_IRQHandler TIMER5_IRQHandler
81 #elif ( USTIMER_TIMER == USTIMER_TIMER6 ) && ( TIMER_COUNT >= 7 )
83 #define TIMER_CLK cmuClock_TIMER6
84 #define TIMER_IRQ TIMER6_IRQn
85 #define TIMER_IRQHandler TIMER6_IRQHandler
87 #elif ( USTIMER_TIMER == USTIMER_WTIMER0 ) && ( WTIMER_COUNT >= 1 )
89 #define TIMER_CLK cmuClock_WTIMER0
90 #define TIMER_IRQ WTIMER0_IRQn
91 #define TIMER_IRQHandler WTIMER0_IRQHandler
93 #elif ( USTIMER_TIMER == USTIMER_WTIMER1 ) && ( WTIMER_COUNT >= 2 )
95 #define TIMER_CLK cmuClock_WTIMER1
96 #define TIMER_IRQ WTIMER1_IRQn
97 #define TIMER_IRQHandler WTIMER1_IRQHandler
99 #elif ( USTIMER_TIMER == USTIMER_WTIMER2 ) && ( WTIMER_COUNT >= 3 )
100 #define TIMER WTIMER2
101 #define TIMER_CLK cmuClock_WTIMER2
102 #define TIMER_IRQ WTIMER2_IRQn
103 #define TIMER_IRQHandler WTIMER2_IRQHandler
105 #elif ( USTIMER_TIMER == USTIMER_WTIMER3 ) && ( WTIMER_COUNT >= 4 )
106 #define TIMER WTIMER3
107 #define TIMER_CLK cmuClock_WTIMER3
108 #define TIMER_IRQ WTIMER3_IRQn
109 #define TIMER_IRQHandler WTIMER3_IRQHandler
112 #error "Illegal USTIMER TIMER selection"
116 #define TIMER_MAX 0xFFFF
118 static uint32_t freq;
119 static uint32_t minTicks;
120 static volatile bool timeElapsed =
false;
122 static void DelayTicksEM1( uint16_t ticks );
123 static void DelayTicksPolled( uint16_t ticks );
143 uint32_t coreClockScale;
161 && ( freq > 2000000 ) );
169 minTicks = ( ( (uint64_t)freq * coreClockScale ) + 500000 ) / 1000000;
173 NVIC_ClearPendingIRQ( TIMER_IRQ );
174 NVIC_EnableIRQ( TIMER_IRQ );
192 NVIC_DisableIRQ( TIMER_IRQ );
196 NVIC_ClearPendingIRQ( TIMER_IRQ );
225 totalTicks = ( ( (uint64_t)freq * usec ) + 500000 ) / 1000000;
229 while ( totalTicks > 65000 )
231 DelayTicksEM1( 65000 );
234 DelayTicksEM1( (uint16_t)totalTicks );
260 totalTicks = ( ( (uint64_t)freq * usec ) + 500000 ) / 1000000;
264 while ( totalTicks > 65000 )
266 DelayTicksPolled( 65000 );
269 DelayTicksPolled( (uint16_t)totalTicks );
277 void TIMER_IRQHandler(
void )
290 static void DelayTicksPolled( uint16_t ticks )
293 volatile uint16_t now;
301 }
while ( (uint16_t)( now - startTime ) < ticks );
305 static void DelayTicksEM1( uint16_t ticks )
321 while ( ! timeElapsed )
Clock management unit (CMU) API.
__STATIC_INLINE void TIMER_IntDisable(TIMER_TypeDef *timer, uint32_t flags)
Disable one or more TIMER interrupts.
__STATIC_INLINE void TIMER_TopSet(TIMER_TypeDef *timer, uint32_t val)
Set top value for timer.
__STATIC_INLINE void TIMER_Enable(TIMER_TypeDef *timer, bool enable)
Start/stop TIMER.
#define TIMER_INITCC_DEFAULT
Timer/counter (TIMER) peripheral API.
__STATIC_INLINE void TIMER_IntClear(TIMER_TypeDef *timer, uint32_t flags)
Clear one or more pending TIMER interrupts.
CMSIS Cortex-M Peripheral Access Layer for Silicon Laboratories microcontroller devices.
TIMER_CCMode_TypeDef mode
__STATIC_INLINE void TIMER_IntEnable(TIMER_TypeDef *timer, uint32_t flags)
Enable one or more TIMER interrupts.
General purpose utilities.
Ecode_t USTIMER_Init(void)
Activate and initialize the hardware timer used to pace the 1 microsecond delay functions.
#define _TIMER_CTRL_PRESC_DIV1024
#define _TIMER_CTRL_PRESC_DIV1
#define CORE_ATOMIC_SECTION(yourcode)
__STATIC_INLINE void EMU_EnterEM1(void)
Enter energy mode 1 (EM1).
__STATIC_INLINE uint32_t TIMER_IntGet(TIMER_TypeDef *timer)
Get pending TIMER interrupt flags.
#define TIMER_INIT_DEFAULT
uint32_t Ecode_t
Typedef for API function error code return values.
Core interrupt handling API.
void CMU_ClockEnable(CMU_Clock_TypeDef clock, bool enable)
Enable/disable a clock.
Ecode_t USTIMER_DeInit(void)
Deinitialize USTIMER driver.
__STATIC_INLINE void TIMER_CompareSet(TIMER_TypeDef *timer, unsigned int ch, uint32_t val)
Set compare value for compare/capture channel when operating in compare or PWM mode.
__STATIC_INLINE uint32_t TIMER_CounterGet(TIMER_TypeDef *timer)
Get TIMER counter value.
TIMER_Prescale_TypeDef prescale
void TIMER_Init(TIMER_TypeDef *timer, const TIMER_Init_TypeDef *init)
Initialize TIMER.
Energy management unit (EMU) peripheral API.
Ecode_t USTIMER_DelayIntSafe(uint32_t usec)
Delay a given number of microseconds.
#define ECODE_EMDRV_USTIMER_OK
Success return value.
#define SL_MAX(a, b)
Macro for getting maximum value. No sideeffects, a and b are evaluated once only. ...
Microsecond delay function API definition.
Ecode_t USTIMER_Delay(uint32_t usec)
Delay a given number of microseconds.
void TIMER_InitCC(TIMER_TypeDef *timer, unsigned int ch, const TIMER_InitCC_TypeDef *init)
Initialize TIMER compare/capture channel.
uint32_t CMU_ClockFreqGet(CMU_Clock_TypeDef clock)
Get clock frequency for a clock point.