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
|
/*
* Copyright (c) 2006-2023, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2022-07-29 rtthread qiu first version
*/
#include "drv_common.h"
#include "drv_hwtimer.h"
#include <board.h>
#ifdef BSP_USING_TIM
//#define DRV_DEBUG
#define LOG_TAG "drv.hwtimer"
#include <drv_log.h>
static void isr_timer(void *callback_arg, cyhal_timer_event_t event);
#ifdef RT_USING_HWTIMER
enum
{
#ifdef BSP_USING_TIM1
TIM1_INDEX,
#endif
#ifdef BSP_USING_TIM2
TIM2_INDEX,
#endif
};
struct cyp_hwtimer
{
rt_hwtimer_t time_device;
cyhal_timer_t tim_handle;
IRQn_Type tim_irqn;
char *name;
};
static struct cyp_hwtimer cyp_hwtimer_obj[] =
{
#ifdef BSP_USING_TIM1
TIM1_CONFIG,
#endif
#ifdef BSP_USING_TIM2
TIM2_CONFIG,
#endif
};
static void timer_init(rt_hwtimer_t *timer, rt_uint32_t state)
{
RT_ASSERT(timer != RT_NULL);
cy_rslt_t result = RT_EOK;
cyhal_timer_t *tim = RT_NULL;
tim = (cyhal_timer_t *)timer->parent.user_data;
const cyhal_timer_cfg_t init_timer_cfg =
{
.compare_value = 0, /* Timer compare value, not used */
.period = 9999, /* Defines the timer period */
.direction = CYHAL_TIMER_DIR_UP, /* Timer counts up */
.is_compare = false, /* Don't use compare mode */
.is_continuous = true, /* Run timer indefinitely */
.value = 0 /* Initial value of counter */
};
if (state)
{
/* Initialize the timer object. Does not use input pin ('pin' is NC) and
* does not use a pre-configured clock source ('clk' is NULL). */
result = cyhal_timer_init(tim, NC, NULL);
if (result != CY_RSLT_SUCCESS)
{
LOG_E("timer init error \r\n");
return;
}
else
{
/* Configure timer period and operation mode such as count direction,
duration */
cyhal_timer_configure(tim, &init_timer_cfg);
/* Set the frequency of timer's clock source */
cyhal_timer_set_frequency(tim, 10000);
cyhal_timer_start(tim);
}
}
else
{
cyhal_timer_free(tim);
LOG_E("free time \r\n");
}
}
static rt_err_t timer_start(rt_hwtimer_t *timer, rt_uint32_t t, rt_hwtimer_mode_t opmode)
{
RT_ASSERT(timer != RT_NULL);
RT_ASSERT(opmode != RT_NULL);
cy_rslt_t result = RT_EOK;
cyhal_timer_t *tim = RT_NULL;
tim = (cyhal_timer_t *)timer->parent.user_data;
const cyhal_timer_cfg_t init_timer_cfg =
{
.compare_value = 0, /* Timer compare value, not used */
.period = t - 1, /* Defines the timer period */
.direction = CYHAL_TIMER_DIR_UP, /* Timer counts up */
.is_compare = false, /* Don't use compare mode */
.is_continuous = true, /* Run timer indefinitely */
.value = 0 /* Initial value of counter */
};
/* Configure timer period and operation mode such as count direction,
duration */
cyhal_timer_configure(tim, &init_timer_cfg);
if (opmode == HWTIMER_MODE_ONESHOT)
{
/* set timer to single mode */
cyhal_timer_stop(tim);
}
else
{
cyhal_timer_reset(tim);
}
result = cyhal_timer_start(tim);
if (result != CY_RSLT_SUCCESS)
{
LOG_E("time start error\r\n");
cyhal_timer_free(tim);
}
/* Assign the ISR to execute on timer interrupt */
cyhal_timer_register_callback(tim, isr_timer, NULL);
/* Set the event on which timer interrupt occurs and enable it */
cyhal_timer_enable_event(tim, CYHAL_TIMER_IRQ_TERMINAL_COUNT, 1, true);
return result;
}
static void timer_stop(rt_hwtimer_t *timer)
{
RT_ASSERT(timer != RT_NULL);
cyhal_timer_t *tim = RT_NULL;
tim = (cyhal_timer_t *)timer->parent.user_data;
cyhal_timer_stop(tim);
}
static rt_uint32_t timer_counter_get(rt_hwtimer_t *timer)
{
cyhal_timer_t *tim = RT_NULL;
rt_uint32_t count;
RT_ASSERT(timer != RT_NULL);
tim = (cyhal_timer_t *)timer->parent.user_data;
count = cyhal_timer_read(tim);
return count;
}
static rt_err_t timer_ctrl(rt_hwtimer_t *timer, rt_uint32_t cmd, void *arg)
{
RT_ASSERT(timer != RT_NULL);
RT_ASSERT(arg != RT_NULL);
cyhal_timer_t *tim = RT_NULL;
rt_err_t result = -RT_ERROR;
tim = (cyhal_timer_t *)timer->parent.user_data;
switch (cmd)
{
case HWTIMER_CTRL_FREQ_SET:
{
rt_uint32_t freq;
rt_uint16_t val;
freq = *((rt_uint32_t *)arg);
result = cyhal_timer_set_frequency(tim, freq);
if (result != CY_RSLT_SUCCESS)
{
LOG_E("cyhal_timer_set_frequency error\r\n");
return RT_ERROR;
}
}
break;
default:
{
result = -RT_EINVAL;
}
break;
}
return result;
}
static const struct rt_hwtimer_info _info = TIM_DEV_INFO_CONFIG;
static const struct rt_hwtimer_ops _ops =
{
.init = timer_init,
.start = timer_start,
.stop = timer_stop,
.count_get = timer_counter_get,
.control = timer_ctrl,
};
#ifdef BSP_USING_TIM1
static void isr_timer(void *callback_arg, cyhal_timer_event_t event)
{
/* enter interrupt */
rt_interrupt_enter();
(void)callback_arg;
(void)event;
rt_device_hwtimer_isr(&cyp_hwtimer_obj[TIM1_INDEX].time_device);
/* leave interrupt */
rt_interrupt_leave();
}
#endif
#ifdef BSP_USING_TIM2
static void isr_timer(void *callback_arg, cyhal_timer_event_t event)
{
/* enter interrupt */
rt_interrupt_enter();
(void)callback_arg;
(void)event;
rt_device_hwtimer_isr(&cyp_hwtimer_obj[TIM2_INDEX].time_device);
/* leave interrupt */
rt_interrupt_leave();
}
#endif
int cyp_hwtimer_init(void)
{
int i = 0;
int result = RT_EOK;
for (i = 0; i < sizeof(cyp_hwtimer_obj) / sizeof(cyp_hwtimer_obj[0]); i++)
{
cyp_hwtimer_obj[i].time_device.info = &_info;
cyp_hwtimer_obj[i].time_device.ops = &_ops;
if (rt_device_hwtimer_register(&cyp_hwtimer_obj[i].time_device, cyp_hwtimer_obj[i].name, &cyp_hwtimer_obj[i].tim_handle) != RT_EOK)
{
LOG_E("%s register failed", cyp_hwtimer_obj[i].name);
result = -RT_ERROR;
}
}
return result;
}
INIT_BOARD_EXPORT(cyp_hwtimer_init);
#endif /*RT_USING_HWTIMER*/
#endif /*BSP_USING_TIM*/
/* this is a hwtimer test demo*/
#include <rtthread.h>
#include <rtdevice.h>
#define HWTIMER_DEV_NAME "time2" /* device name */
static rt_err_t timeout_cb(rt_device_t dev, rt_size_t size)
{
rt_kprintf("this is hwtimer timeout callback fucntion!\n");
rt_kprintf("tick is :%d !\n", rt_tick_get());
return 0;
}
int hwtimer_sample()
{
rt_err_t ret = RT_EOK;
rt_hwtimerval_t timeout_s;
rt_device_t hw_dev = RT_NULL;
rt_hwtimer_mode_t mode;
rt_uint32_t freq = 10000;
hw_dev = rt_device_find(HWTIMER_DEV_NAME);
if (hw_dev == RT_NULL)
{
rt_kprintf("hwtimer sample run failed! can't find %s device!\n", HWTIMER_DEV_NAME);
return RT_ERROR;
}
ret = rt_device_open(hw_dev, RT_DEVICE_OFLAG_RDWR);
if (ret != RT_EOK)
{
rt_kprintf("open %s device failed!\n", HWTIMER_DEV_NAME);
return ret;
}
rt_device_set_rx_indicate(hw_dev, timeout_cb);
rt_device_control(hw_dev, HWTIMER_CTRL_FREQ_SET, &freq);
mode = HWTIMER_MODE_PERIOD;
ret = rt_device_control(hw_dev, HWTIMER_CTRL_MODE_SET, &mode);
if (ret != RT_EOK)
{
rt_kprintf("set mode failed! ret is :%d\n", ret);
return ret;
}
/* Example Set the timeout period of the timer */
timeout_s.sec = 3; /* secend */
timeout_s.usec = 0; /* microsecend */
if (rt_device_write(hw_dev, 0, &timeout_s, sizeof(timeout_s)) != sizeof(timeout_s))
{
rt_kprintf("set timeout value failed\n");
return RT_ERROR;
}
while (1)
{
rt_thread_mdelay(1500);
rt_device_read(hw_dev, 0, &timeout_s, sizeof(timeout_s));
rt_kprintf("Read: Sec = %d, Usec = %d\n", timeout_s.sec, timeout_s.usec);
}
return ret;
}
MSH_CMD_EXPORT(hwtimer_sample, hwtimer sample);
|