• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 Winner Microelectronics Co., Ltd. All rights reserved.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 /*****************************************************************************
17 *
18 * File Name : wm_adc.h
19 *
20 * Description: adc Driver Module
21 *
22 * Copyright (c) 2014 Winner Microelectronics Co., Ltd.
23 * All rights reserved.
24 *
25 * Author : dave
26 *
27 * Date : 2014-8-15
28 *****************************************************************************/
29 
30 #ifndef WM_ADC_H
31 #define WM_ADC_H
32 
33 #include "wm_type_def.h"
34 
35 #define ADC_DEST_BUFFER_SIZE			16383 // ����Ϊ��
36 
37 /* ADC Result */
38 #define ADC_RESULT_MASK					(0x3FFFC)
39 #define ADC_RESULT_VAL(n)				((n)&ADC_RESULT_MASK)
40 
41 /* ADC_ANALOG_CTRL */
42 #define CONFIG_ADC_CHL_SEL_MASK 		(0xF<<8)
43 #define CONFIG_ADC_CHL_SEL(n)	 		((n)<<8)
44 
45 #define CONFIG_PD_ADC_MASK             	(0x1<<2)
46 #define CONFIG_PD_ADC_VAL(n)           	((n)<<2)   /* 1:pd adc, 0: normal work */
47 
48 #define CONFIG_RSTN_ADC_MASK           	(0x1<<1)
49 #define CONFIG_RSTN_ADC_VAL(n)          ((n)<<1)   /* 1:normal work, 0:adc reset */
50 
51 #define CONFIG_EN_LDO_ADC_MASK         	(0x1<<0)
52 #define CONFIG_EN_LDO_ADC_VAL(n)       	((n)<<0)   /* 1:ldo work, 0: ldo shutdown */
53 
54 /* PGA_CTRL */
55 #define CLK_CHOP_SEL_PGA_MASK			(0x7<<4)
56 #define CLK_CHOP_SEL_PGA_VAL(n)			((n)<<4)
57 
58 #define GAIN_CTRL_PGA_MASK				(0x3<<7)
59 #define GAIN_CTRL_PGA_VAL(n)			((n)<<7)
60 
61 #define PGA_BYPASS_MASK					(0x1<<3)
62 #define PGA_BYPASS_VAL(n)				((n)<<3)   /* 1:bypass pga, 0:use pga */
63 
64 #define BYPASS_INNER_REF_SEL			(0x1<<2)   /* Internal or external reference select */
65 
66 #define PGA_CHOP_ENP_MASK				(0x1<<1)
67 #define PGA_CHOP_ENP_VAL(n)				((n)<<1)   /* 1: enable chop, 0: disable chop */
68 
69 #define PGA_EN_MASK						(0x1<<0)
70 #define PGA_EN_VAL(n)					((n)<<0)   /* 1: enable pga, 0: disable pga */
71 
72 /* Temperature Control */
73 #define TEMP_GAIN_MASK					(0x3<<4)
74 #define TEMP_GAIN_VAL(n)				((n)<<4)
75 
76 #define TEMP_CAL_OFFSET_MASK			(0x1<<1)
77 
78 #define TEMP_EN_MASK					(0x1<<0)
79 #define TEMP_EN_VAL(n)					((n)<<0)  /* 1: enable temperature, 0: disable temperature */
80 
81 /* ADC CTRL */
82 #define ANALOG_SWITCH_TIME_MASK			(0x3FF<<20)
83 #define ANALOG_SWITCH_TIME_VAL(n)		(((n)&0x3FF)<<20)
84 
85 #define ANALOG_INIT_TIME_MASK			(0x3FF<<8)
86 #define ANALOG_INIT_TIME_VAL(n)			(((n)&0x3FF)<<8)
87 
88 #define CMP_POLAR_MASK                  (0x1<<6)
89 
90 #define CMP_IRQ_EN_MASK                 (0x1<<5)
91 #define CMP_IRQ_EN_VAL(n)				((n)<<5)  /* 1: enable cmp irq, 0: disable cmp irq */
92 
93 #define CMP_EN_MASK                 	(0x1<<4)
94 #define CMP_EN_VAL(n)					((n)<<4) /* 1: enable cmp function, 0: disable cmp function */
95 
96 #define ADC_IRQ_EN_MASK                 (0x1<<1)
97 #define ADC_IRQ_EN_VAL(n)				((n)<<1)   /* 1:enable adc transfer irq, 0: disable */
98 
99 #define ADC_DMA_EN_MASK                 (0x1<<0)
100 #define ADC_DMA_EN_VAL(n)				((n)<<0)   /* 1:enable adc dma, 0: disable */
101 
102 /* ADC IRQ Status */
103 #define CMP_INT_MASK					(0x1<<1)
104 
105 #define ADC_INT_MASK					(0x1<<0)
106 
107 /* CMP Value */
108 #define CONFIG_ADC_INPUT_CMP_VAL(n)		((n)&0x3FFFF)
109 
110 /* ADC Channel */
111 #define CONFIG_ADC_CHL_OFFSET			(0x0E)
112 #define CONFIG_ADC_CHL_VOLT				(0x0D)
113 #define CONFIG_ADC_CHL_TEMP				(0x0C)
114 
115 #define ADC_INT_TYPE_ADC				0
116 #define ADC_INT_TYPE_DMA				1
117 #define ADC_INT_TYPE_ADC_COMP 			2
118 
119 #define ADC_REFERENCE_EXTERNAL  		0       // �ⲿ�ο�
120 #define ADC_REFERENCE_INTERNAL  		1       // �ڲ��ο�
121 
122 typedef struct adc_st {
123     u8 dmachannel;
124     void (*adc_cb)(int *buf, u16 len);
125     void (*adc_bigger_cb)(int *buf, u16 len);
126     void (*adc_dma_cb)(int *buf, u16 len);
127     u16 valuelen;     /* dma �������ݳ��� */
128     u16 offset;
129 }ST_ADC;
130 
131 /**
132  * @defgroup Driver_APIs Driver APIs
133  * @brief Driver APIs
134  */
135 
136 /**
137  * @addtogroup Driver_APIs
138  * @{
139  */
140 
141 /**
142  * @defgroup ADC_Driver_APIs ADC Driver APIs
143  * @brief ADC driver APIs
144  */
145 
146 /**
147  * @addtogroup ADC_Driver_APIs
148  * @{
149  */
150 
151 /**
152  * @brief          This function is used to init adc.
153  *
154  * @param[in]      ifusedma    if use dma
155  * @param[in]      dmachannel  dma channel
156  *
157  * @return         None
158  *
159  * @note           None
160  */
161 void tls_adc_init(u8 ifusedma, u8 dmachannel);
162 
163 /**
164  * @brief          This function is used to register interrupt callback function.
165  *
166  * @param[in]      inttype    interrupt type:
167  *                 ADC_INT_TYPE_ADC		adc interrupt,user get adc result from the callback function.
168  *				   ADC_INT_TYPE_DMA		dma interrupt,dma transfer the adc result to the user's buffer.
169  * @param[in]      callback   interrupt callback function.
170  *
171  * @return         None
172  *
173  * @note           None
174  */
175 void tls_adc_irq_register(int inttype, void (*callback)(int *buf, u16 len));
176 
177 /**
178  * @brief          This function is used to clear the interrupt source.
179  *
180  * @param[in]      inttype    interrupt type:
181  *                 ADC_INT_TYPE_ADC		adc interrupt,user get adc result from the callback function.
182  *				   ADC_INT_TYPE_DMA		dma interrupt,dma transfer the adc result to the user's buffer.
183  *
184  * @return         None
185  *
186  * @note           None
187  */
188 void tls_adc_clear_irq(int inttype);
189 
190 /**
191  * @brief          This function is used to register interrupt callback function.
192  *
193  * @param[in]      Channel    adc channel,from 0 to 3 is single input;4 and 5 is differential input.
194  * @param[in]      Length     byte data length,is an integer multiple of half word,need <= 0x500
195  *
196  * @return         None
197  *
198  * @note           None
199  */
200 void tls_adc_start_with_dma(int Channel, int Length);
201 
202 /**
203  * @brief          This function is used to start adc.
204  *
205  * @param[in]      Channel    adc channel,from 0 to 3 is single input;4 and 5 is differential input.
206  *
207  * @return         None
208  *
209  * @note           None
210  */
211 void tls_adc_start_with_cpu(int Channel);
212 
213 /**
214  * @brief           This function is used to read adc result.
215  *
216  * @param[in]      	None
217  *
218  * @retval          adc result
219  *
220  * @note            None
221  */
222 u32 tls_read_adc_result(void);
223 
224 /**
225  * @brief           This function is used to stop the adc.
226  *
227  * @param[in]      	ifusedma    if use dma
228  *
229  * @return          None
230  *
231  * @note            None
232  */
233 void tls_adc_stop(int ifusedma);
234 
235 /**
236  * @brief           This function is used to config adc bigger register.
237  *
238  * @param[in]      	cmp_data    compare data
239  * @param[in]      	cmp_pol     compare pol
240  *
241  * @return          None
242  *
243  * @note            None
244  */
245 void tls_adc_config_cmp_reg(int cmp_data, int cmp_pol);
246 
247 /**
248  * @brief           This function is used to set adc reference source.
249  *
250  * @param[in]      	ref     ADC_REFERENCE_EXTERNAL,ADC_REFERENCE_INTERNAL
251  *
252  * @return          None
253  *
254  * @note            None
255  */
256 void tls_adc_reference_sel(int ref);
257 
258 /**
259  * @brief           This function is used to read internal temperature.
260  *
261  * @param[in]      	None
262  *
263  * @retval          temperature
264  *
265  * @note            None
266  */
267 int adc_get_interTemp(void);
268 
269 /**
270  * @brief           This function is used to read input voltage.
271  *
272  * @param[in]      	channel    adc channel,from 0 to 3 is single input;8 and 9 is differential input.
273  *
274  * @retval          voltage    unit:mV
275  *
276  * @note            None
277  */
278 int adc_get_inputVolt(u8 channel);
279 
280 /**
281  * @brief           This function is used to read internal voltage.
282  *
283  * @param[in]      	None
284  *
285  * @retval          voltage (mV)
286  *
287  * @note            None
288  */
289 u32 adc_get_interVolt(void);
290 
291 /**
292  * @brief           This function is used to read temperature.
293  *
294  * @param[in]      	None
295  *
296  * @retval          temperature
297  *
298  * @note            None
299  */
300 int adc_temp(void);
301 
302 /**
303  * @}
304  */
305 
306 /**
307  * @}
308  */
309 
310 void tls_adc_enable_calibration_buffer_offset(void);
311 void tls_adc_voltage_start_with_cpu(void);
312 void tls_adc_temp_offset_with_cpu(u8 calTemp12);
313 void tls_adc_voltage_start_with_dma(int Length);
314 void tls_adc_set_clk(int div);
315 void signedToUnsignedData(int *adcValue);
316 void tls_adc_buffer_bypass_set(u8 isset);
317 void tls_adc_cmp_start(int Channel, int cmp_data, int cmp_pol);
318 u32  adc_get_offset(void);
319 
320 #endif