1 /*
2  * Copyright (c) 2019 Winner Microelectronics Co., Ltd.
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  *
6  * Change Logs:
7  * Date           Author       Notes
8  * 2018-11-23     fanwenl      1st version
9  */
10 
11 #include <rtthread.h>
12 #include <rtdevice.h>
13 #include "wm_io.h"
14 #include "wm_adc.h"
15 #include "wm_gpio_afsel.h"
16 #include "drv_adc.h"
17 
18 #ifdef BSP_USING_ADC
19 #if defined(USING_ADC_CH1) || defined(USING_ADC_CH2) || defined(USING_ADC_CH3) || defined(USING_ADC_CH4) || \
20     defined(USING_ADC_CH5) || defined(USING_ADC_CH6) || defined(USING_ADC_CH7) || defined(USING_ADC_CH8)
21 
wm_adc_enabled(struct rt_adc_device * device,rt_uint32_t channel,rt_bool_t enabled)22 static rt_err_t wm_adc_enabled(struct rt_adc_device *device, rt_uint32_t channel, rt_bool_t enabled)
23 {
24     if (channel < 1 || channel > 8)
25         return -RT_ERROR;
26 
27     if (enabled == RT_TRUE)
28     {
29         tls_adc_start_with_cpu(channel - 1);
30     }
31     else
32     {
33         tls_adc_stop(0);
34     }
35     return RT_EOK;
36 }
37 
wm_adc_convert(struct rt_adc_device * device,rt_uint32_t channel,rt_uint32_t * value)38 static rt_err_t wm_adc_convert(struct rt_adc_device *device, rt_uint32_t channel, rt_uint32_t *value)
39 {
40     if (channel < 1 || channel > 8)
41         return -RT_ERROR;
42 
43     *value = adc_get_inputVolt(channel - 1) ;
44 
45     return RT_EOK;
46 }
47 
48 static struct rt_adc_ops wm_adc_ops =
49 {
50     wm_adc_enabled,
51     wm_adc_convert,
52 };
53 static struct rt_adc_device wm_adc;
54 #endif
55 
56 #ifdef USING_CPU_TEMP
wm_cpu_temp_enabled(struct rt_adc_device * device,rt_uint32_t channel,rt_bool_t enabled)57 static rt_err_t wm_cpu_temp_enabled(struct rt_adc_device *device, rt_uint32_t channel, rt_bool_t enabled)
58 {
59     if (enabled == RT_FALSE)
60     {
61         tls_adc_stop(0);
62     }
63     return RT_EOK;
64 
65 }
66 
wm_cpu_temp_convert(struct rt_adc_device * device,rt_uint32_t channel,rt_uint32_t * value)67 static rt_err_t wm_cpu_temp_convert(struct rt_adc_device *device, rt_uint32_t channel, rt_uint32_t *value)
68 {
69     *value = (rt_uint32_t)adc_temp();
70 
71     /**
72     sprintf(temperature, "%d.%d", *value/1000, (*value%1000)/100);
73     printf("tem: %s", temperature);
74     */
75 
76     return RT_EOK;
77 }
78 
79 static struct rt_adc_ops wm_cpu_temp_ops =
80 {
81     wm_cpu_temp_enabled,
82     wm_cpu_temp_convert,
83 };
84 static struct rt_adc_device wm_cpu_temp;
85 #endif
86 
wm_hw_adc_init(void)87 int wm_hw_adc_init(void)
88 {
89     /*adc io config*/
90 #ifdef USING_ADC_CH1
91     wm_adc_config(0);
92 #endif
93 #ifdef USING_ADC_CH2
94     wm_adc_config(1);
95 #endif
96 #ifdef USING_ADC_CH3
97     wm_adc_config(2);
98 #endif
99 #ifdef USING_ADC_CH4
100     wm_adc_config(3);
101 #endif
102 #ifdef USING_ADC_CH5
103     wm_adc_config(4);
104 #endif
105 #ifdef USING_ADC_CH6
106     wm_adc_config(5);
107 #endif
108 #ifdef USING_ADC_CH7
109     wm_adc_config(6);
110 #endif
111 #ifdef USING_ADC_CH8
112     wm_adc_config(7);
113 #endif
114 
115 #if defined(USING_ADC_CH1) || defined(USING_ADC_CH2) || defined(USING_ADC_CH3) || defined(USING_ADC_CH4) || \
116     defined(USING_ADC_CH5) || defined(USING_ADC_CH6) || defined(USING_ADC_CH7) || defined(USING_ADC_CH8)
117 
118     rt_hw_adc_register(&wm_adc, "adc", &wm_adc_ops, 0);
119 #endif
120 
121 #ifdef USING_CPU_TEMP
122     rt_hw_adc_register(&wm_cpu_temp, "cputemp", &wm_cpu_temp_ops, 0);
123 #endif
124 
125     return RT_EOK;
126 }
127 INIT_DEVICE_EXPORT(wm_hw_adc_init);
128 
129 #endif /* BSP_USING_ADC */
130