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
|
/*
* Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2020-07-27 thread-liu first version
*/
#include <board.h>
#if defined(BSP_USING_TIM14) && defined(BSP_USING_ADC2)
#include <rtthread.h>
#include <rtdevice.h>
#define CLOCK_TIMER_DEV_NAME "timer14"
#define HWADC_DEV_NAME "adc2"
#define REFER_VOLTAGE 330 /* voltage reference */
#define CONVERT_BITS (1 << 12) /* Conversion digit */
#define ADC_DEV_CHANNEL 6
static rt_adc_device_t adc_dev = RT_NULL;
static rt_err_t timeout_cb(rt_device_t dev, rt_size_t size)
{
rt_uint32_t value = 0 , vol = 0;
/* read adc value */
value = rt_adc_read(adc_dev, ADC_DEV_CHANNEL);
rt_kprintf("the value is :%d \n", value);
vol = value * REFER_VOLTAGE / CONVERT_BITS;
rt_kprintf("the voltage is :%d.%02d \n", vol / 100, vol % 100);
return 0;
}
static int clock_timer_stop(void)
{
rt_err_t ret = RT_EOK;
rt_device_t hw_dev = RT_NULL;
hw_dev = rt_device_find(CLOCK_TIMER_DEV_NAME);
if (hw_dev == RT_NULL)
{
rt_kprintf("clock_timer sample run failed! can't find %s device!\n", CLOCK_TIMER_DEV_NAME);
return -RT_ERROR;
}
ret = rt_device_close(hw_dev);
if (ret != RT_EOK)
{
rt_kprintf("close %s device failed!\n", CLOCK_TIMER_DEV_NAME);
return ret;
}
/* close adc channel */
ret = rt_adc_disable(adc_dev, ADC_DEV_CHANNEL);
return ret;
}
static int clock_timer_start(void)
{
rt_err_t ret = RT_EOK;
rt_clock_timerval_t timeout_s;
rt_device_t hw_dev = RT_NULL;
rt_clock_timer_mode_t mode;
hw_dev = rt_device_find(CLOCK_TIMER_DEV_NAME);
if (hw_dev == RT_NULL)
{
rt_kprintf("clock_timer sample run failed! can't find %s device!\n", CLOCK_TIMER_DEV_NAME);
return -RT_ERROR;
}
/* find adc dev */
adc_dev = (rt_adc_device_t)rt_device_find(HWADC_DEV_NAME);
if (adc_dev == RT_NULL)
{
rt_kprintf("clock_timer sample run failed! can't find %s device!\n", HWADC_DEV_NAME);
return -RT_ERROR;
}
/* Open the device in read/write mode */
ret = rt_device_open(hw_dev, RT_DEVICE_OFLAG_RDWR);
if (ret != RT_EOK)
{
rt_kprintf("open %s device failed!\n", CLOCK_TIMER_DEV_NAME);
return ret;
}
/* Set the timeout callback function */
rt_device_set_rx_indicate(hw_dev, timeout_cb);
/* Set the mode to periodic timer */
mode = CLOCK_TIMER_MODE_PERIOD;
ret = rt_device_control(hw_dev, CLOCK_TIMER_CTRL_MODE_SET, &mode);
if (ret != RT_EOK)
{
rt_kprintf("set mode failed! ret is :%d\n", ret);
return ret;
}
timeout_s.sec = 5;
timeout_s.usec = 0;
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;
}
rt_thread_mdelay(3500);
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);
/* enable adc channel */
ret = rt_adc_enable(adc_dev, ADC_DEV_CHANNEL);
return ret;
}
static int tim_sample(int argc, char *argv[])
{
if (argc > 1)
{
if (!rt_strcmp(argv[1], "start"))
{
rt_kprintf("tim14 will start\n");
clock_timer_start();
return RT_EOK;
}
else if (!rt_strcmp(argv[1], "stop"))
{
clock_timer_stop();
rt_kprintf("stop tim14 success!\n");
return RT_EOK;
}
else
{
goto _exit;
}
}
_exit:
{
rt_kprintf("Usage:\n");
rt_kprintf("tim_sample start - start TIM14 \n");
rt_kprintf("tim_sample stop - stop TIM14 \n");
}
return -RT_ERROR;
}
MSH_CMD_EXPORT(tim_sample, tim sample);
#endif
|