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
|
/*
* Copyright (c) 2019 Winner Microelectronics Co., Ltd.
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2018-11-23 fanwenl 1st version
*/
#include <rtthread.h>
#include <rtdevice.h>
#include "wm_io.h"
#include "wm_adc.h"
#include "wm_gpio_afsel.h"
#include "drv_adc.h"
#ifdef BSP_USING_ADC
#if defined(USING_ADC_CH1) || defined(USING_ADC_CH2) || defined(USING_ADC_CH3) || defined(USING_ADC_CH4) || \
defined(USING_ADC_CH5) || defined(USING_ADC_CH6) || defined(USING_ADC_CH7) || defined(USING_ADC_CH8)
static rt_err_t wm_adc_enabled(struct rt_adc_device *device, rt_uint32_t channel, rt_bool_t enabled)
{
if (channel < 1 || channel > 8)
return -RT_ERROR;
if (enabled == RT_TRUE)
{
tls_adc_start_with_cpu(channel - 1);
}
else
{
tls_adc_stop(0);
}
return RT_EOK;
}
static rt_err_t wm_adc_convert(struct rt_adc_device *device, rt_uint32_t channel, rt_uint32_t *value)
{
if (channel < 1 || channel > 8)
return -RT_ERROR;
*value = adc_get_inputVolt(channel - 1) ;
return RT_EOK;
}
static struct rt_adc_ops wm_adc_ops =
{
wm_adc_enabled,
wm_adc_convert,
};
static struct rt_adc_device wm_adc;
#endif
#ifdef USING_CPU_TEMP
static rt_err_t wm_cpu_temp_enabled(struct rt_adc_device *device, rt_uint32_t channel, rt_bool_t enabled)
{
if (enabled == RT_FALSE)
{
tls_adc_stop(0);
}
return RT_EOK;
}
static rt_err_t wm_cpu_temp_convert(struct rt_adc_device *device, rt_uint32_t channel, rt_uint32_t *value)
{
*value = (rt_uint32_t)adc_temp();
/**
sprintf(temperature, "%d.%d", *value/1000, (*value%1000)/100);
printf("tem: %s", temperature);
*/
return RT_EOK;
}
static struct rt_adc_ops wm_cpu_temp_ops =
{
wm_cpu_temp_enabled,
wm_cpu_temp_convert,
};
static struct rt_adc_device wm_cpu_temp;
#endif
int wm_hw_adc_init(void)
{
/*adc io config*/
#ifdef USING_ADC_CH1
wm_adc_config(0);
#endif
#ifdef USING_ADC_CH2
wm_adc_config(1);
#endif
#ifdef USING_ADC_CH3
wm_adc_config(2);
#endif
#ifdef USING_ADC_CH4
wm_adc_config(3);
#endif
#ifdef USING_ADC_CH5
wm_adc_config(4);
#endif
#ifdef USING_ADC_CH6
wm_adc_config(5);
#endif
#ifdef USING_ADC_CH7
wm_adc_config(6);
#endif
#ifdef USING_ADC_CH8
wm_adc_config(7);
#endif
#if defined(USING_ADC_CH1) || defined(USING_ADC_CH2) || defined(USING_ADC_CH3) || defined(USING_ADC_CH4) || \
defined(USING_ADC_CH5) || defined(USING_ADC_CH6) || defined(USING_ADC_CH7) || defined(USING_ADC_CH8)
rt_hw_adc_register(&wm_adc, "adc", &wm_adc_ops, 0);
#endif
#ifdef USING_CPU_TEMP
rt_hw_adc_register(&wm_cpu_temp, "cputemp", &wm_cpu_temp_ops, 0);
#endif
return RT_EOK;
}
INIT_DEVICE_EXPORT(wm_hw_adc_init);
#endif /* BSP_USING_ADC */
|