1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
|
/*
* Copyright (c) 2006-2022, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2011-01-18 onelife Initial creation for EFM32
* 2011-06-17 onelife Modify init function for efm32lib v2 upgrading
*/
/***************************************************************************//**
* @addtogroup efm32
* @{
******************************************************************************/
/* Includes ------------------------------------------------------------------*/
#include "board.h"
#include "drv_timer.h"
#if (defined(RT_USING_TIMER0) || defined(RT_USING_TIMER1) || defined(RT_USING_TIMER2))
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
#ifdef RT_TIMER_DEBUG
#define timer_debug(format,args...) rt_kprintf(format, ##args)
#else
#define timer_debug(format,args...)
#endif
#define TIMER_TopCalculate(p) \
(p * (EFM32_HFXO_FREQUENCY / (1 << TMR_CFG_PRESCALER) / 1000))
/* Private variables ---------------------------------------------------------*/
#ifdef RT_USING_TIMER0
static struct rt_device timer0_device;
#endif
#ifdef RT_USING_TIMER1
static struct rt_device timer1_device;
#endif
#ifdef RT_USING_TIMER2
static struct rt_device timer2_device;
#endif
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
/***************************************************************************//**
* @brief
* Initialize Timer device
*
* @details
*
* @note
*
* @param[in] dev
* Pointer to device descriptor
*
* @return
* Error code
******************************************************************************/
static rt_err_t rt_hs_timer_init (rt_device_t dev)
{
RT_ASSERT(dev != RT_NULL);
struct efm32_timer_device_t *timer;
timer = (struct efm32_timer_device_t *)(dev->user_data);
timer->hook.cbFunc = RT_NULL;
timer->hook.userPtr = RT_NULL;
return RT_EOK;
}
/***************************************************************************//**
* @brief
* Configure Timer device
*
* @details
*
* @note
*
* @param[in] dev
* Pointer to device descriptor
*
* @param[in] cmd
* Timer control command
*
* @param[in] args
* Arguments
*
* @return
* Error code
******************************************************************************/
static rt_err_t rt_hs_timer_control (
rt_device_t dev,
rt_uint8_t cmd,
void *args)
{
RT_ASSERT(dev != RT_NULL);
struct efm32_timer_device_t *timer;
timer = (struct efm32_timer_device_t *)(dev->user_data);
switch (cmd)
{
case RT_DEVICE_CTRL_SUSPEND:
/* Suspend device */
dev->flag |= RT_DEVICE_FLAG_SUSPENDED;
TIMER_Enable(timer->timer_device, false);
break;
case RT_DEVICE_CTRL_RESUME:
/* Resume device */
dev->flag &= ~RT_DEVICE_FLAG_SUSPENDED;
TIMER_Enable(timer->timer_device, true);
break;
case RT_DEVICE_CTRL_TIMER_PERIOD:
{
/* change device setting */
struct efm32_timer_control_t *control;
rt_uint32_t running;
control = (struct efm32_timer_control_t *)args;
running = timer->timer_device->STATUS & 0x00000001;
TIMER_Enable(timer->timer_device, false);
timer->timer_device->CNT = _TIMER_CNT_RESETVALUE;
TIMER_TopSet(timer->timer_device, TIMER_TopCalculate(control->period));
timer->hook.cbFunc = control->hook.cbFunc;
timer->hook.userPtr = control->hook.userPtr;
if (running)
{
TIMER_Enable(timer->timer_device, true);
}
}
break;
}
return RT_EOK;
}
/***************************************************************************//**
* @brief
* Register Timer device
*
* @details
*
* @note
*
* @param[in] device
* Pointer to device descriptor
*
* @param[in] name
* Device name
*
* @param[in] flag
* Configuration flags
*
* @param[in] timer
* Pointer to Timer device descriptor
*
* @return
* Error code
******************************************************************************/
rt_err_t rt_hw_timer_register(
rt_device_t device,
const char *name,
rt_uint32_t flag,
struct efm32_timer_device_t *timer)
{
RT_ASSERT(device != RT_NULL);
device->type = RT_Device_Class_Char; /* fixme: should be timer type*/
device->rx_indicate = RT_NULL;
device->tx_complete = RT_NULL;
device->init = rt_hs_timer_init;
device->open = RT_NULL;
device->close = RT_NULL;
device->read = RT_NULL;
device->write = RT_NULL;
device->control = rt_hs_timer_control;
device->user_data = timer;
/* register a character device */
return rt_device_register(device, name, flag);
}
/***************************************************************************//**
* @brief
* Timer counter overflow interrupt handler
*
* @details
*
* @note
******************************************************************************/
void rt_hw_timer_isr(rt_device_t dev)
{
RT_ASSERT(dev != RT_NULL);
struct efm32_timer_device_t *timer;
timer = (struct efm32_timer_device_t *)(dev->user_data);
if (timer->hook.cbFunc != RT_NULL)
{
(timer->hook.cbFunc)(timer->hook.userPtr);
}
}
/***************************************************************************//**
* @brief
* Initialize the specified TIMER unit
*
* @details
*
* @note
*
* @param[in] device
* Pointer to device descriptor
*
* @param[in] unitNumber
* Unit number
*
* @return
* Pointer to TIMER device
******************************************************************************/
static struct efm32_timer_device_t *rt_hw_timer_unit_init(
rt_device_t device,
rt_uint8_t unitNumber,
rt_uint8_t mode)
{
struct efm32_timer_device_t *timer;
TIMER_Init_TypeDef init;
efm32_irq_hook_init_t hook;
CMU_Clock_TypeDef timerClock;
IRQn_Type timerIrq;
do
{
/* Allocate device */
timer = rt_malloc(sizeof(struct efm32_timer_device_t));
if (timer == RT_NULL)
{
timer_debug("no memory for TIMER%d driver\n", unitNumber);
break;
}
/* Initialization */
if (unitNumber >= TIMER_COUNT)
{
break;
}
switch (unitNumber)
{
case 0:
timer->timer_device = TIMER0;
timerClock = (CMU_Clock_TypeDef)cmuClock_TIMER0;
timerIrq = TIMER0_IRQn;
break;
case 1:
timer->timer_device = TIMER1;
timerClock = (CMU_Clock_TypeDef)cmuClock_TIMER1;
timerIrq = TIMER1_IRQn;
break;
case 2:
timer->timer_device = TIMER2;
timerClock = (CMU_Clock_TypeDef)cmuClock_TIMER2;
timerIrq = TIMER2_IRQn;
break;
default:
break;
}
/* Enable TIMER clock */
CMU_ClockEnable(timerClock, true);
/* Reset */
TIMER_Reset(timer->timer_device);
/* Init specified TIMER unit */
init.enable = false;
init.debugRun = true;
init.prescale = TMR_CFG_PRESCALER;
init.clkSel = timerClkSelHFPerClk;
init.fallAction = timerInputActionNone;
init.riseAction = timerInputActionNone;
init.mode = timerModeUp;
init.dmaClrAct = false;
init.quadModeX4 = false;
init.oneShot = (mode > 0) ? true : false;
init.sync = false;
TIMER_Init(timer->timer_device, &init);
/* Config interrupt and NVIC */
hook.type = efm32_irq_type_timer;
hook.unit = unitNumber;
hook.cbFunc = rt_hw_timer_isr;
hook.userPtr = device;
efm32_irq_hook_register(&hook);
/* Enable overflow interrupt */
TIMER_IntEnable(timer->timer_device, TIMER_IF_OF);
TIMER_IntClear(timer->timer_device, TIMER_IF_OF);
/* Enable TIMERn interrupt vector in NVIC */
NVIC_ClearPendingIRQ(timerIrq);
NVIC_SetPriority(timerIrq, EFM32_IRQ_PRI_DEFAULT);
NVIC_EnableIRQ(timerIrq);
return timer;
} while(0);
if (timer)
{
rt_free(timer);
}
rt_kprintf("TIMER: Init failed!\n");
return RT_NULL;
}
/***************************************************************************//**
* @brief
* Initialize all Timer module related hardware and register Timer device to
* kernel
*
* @details
*
* @note
******************************************************************************/
void rt_hw_timer_init(void)
{
struct efm32_timer_device_t *timer;
#ifdef RT_USING_TIMER0
if ((timer = rt_hw_timer_unit_init(&timer0_device, 0, RT_USING_TIMER0)) \
!= RT_NULL)
{
rt_hw_timer_register(&timer0_device, RT_TIMER0_NAME, 0, timer);
}
#endif
#ifdef RT_USING_TIMER1
if ((timer = rt_hw_timer_unit_init(&timer1_device, 1, RT_USING_TIMER1)) \
!= RT_NULL)
{
rt_hw_timer_register(&timer1_device, RT_TIMER1_NAME, 0, timer);
}
#endif
#ifdef RT_USING_TIMER2
if ((timer = rt_hw_timer_unit_init(&timer2_device, 2, RT_USING_TIMER2)) \
!= RT_NULL)
{
rt_hw_timer_register(&timer2_device, RT_TIMER2_NAME, 0, timer);
}
#endif
}
#endif
/***************************************************************************//**
* @}
******************************************************************************/
|