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
|
/*
* Copyright (c) 2006-2022, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2022-02-22 airm2m first version
*/
#include <rtthread.h>
#include <rtdevice.h>
#include <board.h>
#include "drv_timer.h"
#ifdef BSP_USING_TIM
#define LOG_TAG "drv.clock_timer"
#include <drv_log.h>
static void air32_clock_timer_init(struct rt_clock_timer_device *device, rt_uint32_t state)
{
struct clock_timer_device *clock_timer_dev;
struct rt_clock_timer_info *clock_timer_info;
rt_uint32_t clk = 0;
rt_uint16_t prescaler_value = 0;
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitType;
NVIC_InitTypeDef NVIC_InitStructure;
RT_ASSERT(device != RT_NULL);
clock_timer_dev = (struct clock_timer_device *)device;
if (state)
{
air32_tim_clock_init(clock_timer_dev->periph);
clock_timer_info = air32_clock_timer_info_config_get(clock_timer_dev->periph);
clk = air32_tim_clock_get(clock_timer_dev->periph);
prescaler_value = (rt_uint16_t)(clk / clock_timer_info->minfreq) - 1;
/*
* (1 / freq) = (cnt + 1) * (1 / (clk/(prescaler_value + 1) ) )
*/
TIM_TimeBaseInitType.TIM_Period = clock_timer_info->maxcnt - 1;
TIM_TimeBaseInitType.TIM_Prescaler = prescaler_value;
TIM_TimeBaseInitType.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseInitType.TIM_RepetitionCounter = 0;
if (clock_timer_info == RT_NULL)
{
TIM_TimeBaseInitType.TIM_CounterMode = TIM_CounterMode_Up;
}
else
{
if (clock_timer_info->cntmode == CLOCK_TIMER_CNTMODE_UP)
{
TIM_TimeBaseInitType.TIM_CounterMode = TIM_CounterMode_Up;
}
else
{
TIM_TimeBaseInitType.TIM_CounterMode = TIM_CounterMode_Down;
}
}
TIM_TimeBaseInit(clock_timer_dev->periph, &TIM_TimeBaseInitType);
NVIC_InitStructure.NVIC_IRQChannel = clock_timer_dev->irqn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
TIM_ITConfig(clock_timer_dev->periph, TIM_IT_Update, ENABLE);
TIM_ClearITPendingBit(clock_timer_dev->periph, TIM_IT_Update);
LOG_D("%s init success", clock_timer_dev->name);
}
}
static rt_err_t air32_clock_timer_start(struct rt_clock_timer_device *device, rt_uint32_t cnt, rt_clock_timer_mode_t mode)
{
struct clock_timer_device *clock_timer_dev;
RT_ASSERT(device != RT_NULL);
clock_timer_dev = (struct clock_timer_device *)device;
/*
* (1 / freq) = (cnt + 1) * (1 / (clk/(prescaler_value + 1) ) )
*/
TIM_SetCounter(clock_timer_dev->periph, 0);
TIM_SetAutoreload(clock_timer_dev->periph, cnt - 1);
if (mode == CLOCK_TIMER_MODE_ONESHOT)
{
TIM_SelectOnePulseMode(clock_timer_dev->periph, TIM_OPMode_Single);
}
else
{
TIM_SelectOnePulseMode(clock_timer_dev->periph, TIM_OPMode_Repetitive);
}
TIM_Cmd(clock_timer_dev->periph, ENABLE);
LOG_D("%s start, cnt = %d", clock_timer_dev->name, cnt);
return RT_EOK;
}
static void air32_clock_timer_stop(struct rt_clock_timer_device *device)
{
struct clock_timer_device *clock_timer_dev;
RT_ASSERT(device != RT_NULL);
clock_timer_dev = (struct clock_timer_device *)device;
TIM_Cmd(clock_timer_dev->periph, DISABLE);
TIM_SetCounter(clock_timer_dev->periph, 0);
}
static rt_uint32_t air32_clock_timer_counter_get(struct rt_clock_timer_device *device)
{
struct clock_timer_device *clock_timer_dev;
RT_ASSERT(device != RT_NULL);
clock_timer_dev = (struct clock_timer_device *)device;
return clock_timer_dev->periph->CNT;
}
static rt_err_t air32_clock_timer_control(struct rt_clock_timer_device *device, rt_uint32_t cmd, void *arg)
{
struct clock_timer_device *clock_timer_dev;
rt_err_t result = RT_EOK;
RT_ASSERT(device != RT_NULL);
clock_timer_dev = (struct clock_timer_device *)device;
switch (cmd)
{
case CLOCK_TIMER_CTRL_FREQ_SET:
{
rt_uint32_t freq = 0;
rt_uint32_t clk = 0;
rt_uint16_t prescaler_value = 0;
/*
* (1 / freq) = (cnt + 1) * (1 / (clk/(prescaler_value + 1) ) )
*/
if (arg != RT_NULL)
{
freq = *((rt_uint32_t *)arg);
clk = air32_tim_clock_get(clock_timer_dev->periph);
prescaler_value = (rt_uint16_t)(clk / freq) - 1;
TIM_PrescalerConfig(clock_timer_dev->periph, prescaler_value, TIM_PSCReloadMode_Immediate);
}
else
{
result = -RT_EINVAL;
}
}
break;
default:
result = -RT_EINVAL;
break;
}
return result;
}
static const struct rt_clock_timer_ops clock_timer_ops =
{
.init = air32_clock_timer_init,
.start = air32_clock_timer_start,
.stop = air32_clock_timer_stop,
.count_get = air32_clock_timer_counter_get,
.control = air32_clock_timer_control,
};
static int rt_hw_clock_timer_init(void)
{
rt_err_t ret;
struct rt_clock_timer_info *clock_timer_info;
#ifdef BSP_USING_TIM1
clock_timer_info = air32_clock_timer_info_config_get(clock_timer_device1.periph);
clock_timer_device1.parent.info = clock_timer_info;
clock_timer_device1.parent.ops = &clock_timer_ops;
ret = rt_clock_timer_register(&clock_timer_device1.parent, clock_timer_device1.name, RT_NULL);
if (ret == RT_EOK)
{
LOG_D("clock_timer: %s register success.", clock_timer_device1.name);
}
else
{
LOG_D("clock_timer: %s register failed.", clock_timer_device1.name);
}
#endif
#ifdef BSP_USING_TIM2
clock_timer_info = air32_clock_timer_info_config_get(clock_timer_device2.periph);
clock_timer_device2.parent.info = clock_timer_info;
clock_timer_device2.parent.ops = &clock_timer_ops;
ret = rt_clock_timer_register(&clock_timer_device2.parent, clock_timer_device2.name, RT_NULL);
if (ret == RT_EOK)
{
LOG_D("clock_timer: %s register success.", clock_timer_device2.name);
}
else
{
LOG_D("clock_timer: %s register failed.", clock_timer_device2.name);
}
#endif
#ifdef BSP_USING_TIM3
clock_timer_info = air32_clock_timer_info_config_get(clock_timer_device3.periph);
clock_timer_device3.parent.info = clock_timer_info;
clock_timer_device3.parent.ops = &clock_timer_ops;
ret = rt_clock_timer_register(&clock_timer_device3.parent, clock_timer_device3.name, RT_NULL);
if (ret == RT_EOK)
{
LOG_D("clock_timer: %s register success.", clock_timer_device3.name);
}
else
{
LOG_D("clock_timer: %s register failed.", clock_timer_device3.name);
}
#endif
#ifdef BSP_USING_TIM4
clock_timer_info = air32_clock_timer_info_config_get(clock_timer_device4.periph);
clock_timer_device4.parent.info = clock_timer_info;
clock_timer_device4.parent.ops = &clock_timer_ops;
ret = rt_clock_timer_register(&clock_timer_device4.parent, clock_timer_device4.name, RT_NULL);
if (ret == RT_EOK)
{
LOG_D("clock_timer: %s register success.", clock_timer_device4.name);
}
else
{
LOG_D("clock_timer: %s register failed.", clock_timer_device4.name);
}
#endif
return RT_EOK;
}
INIT_DEVICE_EXPORT(rt_hw_clock_timer_init);
#ifdef BSP_USING_TIM1
void TIM1_UP_IRQHandler(void)
{
/* enter interrupt */
rt_interrupt_enter();
if (TIM_GetITStatus(clock_timer_device1.periph, TIM_IT_Update) == SET)
{
TIM_ClearITPendingBit(clock_timer_device1.periph, TIM_IT_Update);
rt_clock_timer_isr(&clock_timer_device1.parent);
}
/* leave interrupt */
rt_interrupt_leave();
}
#endif
#ifdef BSP_USING_TIM2
void TIM2_IRQHandler(void)
{
/* enter interrupt */
rt_interrupt_enter();
if (TIM_GetITStatus(clock_timer_device2.periph, TIM_IT_Update) == SET)
{
TIM_ClearITPendingBit(clock_timer_device2.periph, TIM_IT_Update);
rt_clock_timer_isr(&clock_timer_device2.parent);
}
/* leave interrupt */
rt_interrupt_leave();
}
#endif
#ifdef BSP_USING_TIM3
void TIM3_IRQHandler(void)
{
/* enter interrupt */
rt_interrupt_enter();
if (TIM_GetITStatus(clock_timer_device3.periph, TIM_IT_Update) == SET)
{
TIM_ClearITPendingBit(clock_timer_device3.periph, TIM_IT_Update);
rt_clock_timer_isr(&clock_timer_device3.parent);
}
/* leave interrupt */
rt_interrupt_leave();
}
#endif
#ifdef BSP_USING_TIM4
void TIM4_IRQHandler(void)
{
/* enter interrupt */
rt_interrupt_enter();
if (TIM_GetITStatus(clock_timer_device4.periph, TIM_IT_Update) == SET)
{
TIM_ClearITPendingBit(clock_timer_device4.periph, TIM_IT_Update);
rt_clock_timer_isr(&clock_timer_device4.parent);
}
/* leave interrupt */
rt_interrupt_leave();
}
#endif
#endif /* BSP_USING_CLOCK_TIMER */
|