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
|
/**
*****************************************************************************
* @file cmem7_adc.h
*
* @brief CMEM7 ADC header file
*
*
* @version V1.0
* @date 3. September 2013
*
* @note
*
*****************************************************************************
* @attention
*
* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
* TIME. AS A RESULT, CAPITAL-MICRO SHALL NOT BE HELD LIABLE FOR ANY DIRECT,
* INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
* FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
* CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
*
* <h2><center>© COPYRIGHT 2013 Capital-micro </center></h2>
*****************************************************************************
*/
#ifndef __CMEM7_ADC_H
#define __CMEM7_ADC_H
#ifdef __cplusplus
extern "C" {
#endif
#include "cmem7.h"
#include "cmem7_conf.h"
/** @defgroup ADC_PERIPH
* @{
*/
typedef enum {
ADC_PERIPH_1,
ADC_PERIPH_2,
} ADC_PERIPH;
#define IS_ADC_ALL_PERIPH(PERIPH) (((PERIPH) == ADC_PERIPH_1) || \
((PERIPH) == ADC_PERIPH_2))
/**
* @}
*/
/** @defgroup ADC_VSEN
* @{
*/
#define ADC_VSEN_VDDCORE 1
#define ADC_VSEN_VDDIO 2
#define ADC_VSEN_VDDIO2 4
#define IS_ADC_VSEN(VSEN) (((VSEN) == ADC_VSEN_VDDCORE) || \
((VSEN) == ADC_VSEN_VDDIO) || \
((VSEN) == ADC_VSEN_VDDIO2))
/**
* @}
*/
/** @defgroup ADC_PHASE_CTRL
* @{
*/
#define ADC_PHASE_CTRL_0DEG_RISE_EDGE 0 /* ADC-1 and ADC-2 CLK are 0DEG Phase Difference(Rising Edge) */
#define ADC_PHASE_CTRL_90DEG_AHEAD 1 /* ADC-1 90DEG ahead of ADC-2 */
#define ADC_PHASE_CTRL_90DEG_LAG 2 /* ADC-1 90DEG lag of ADC-2 */
#define ADC_PHASE_CTRL_0DEG_FALL_EDGE 3 /* ADC-1 and ADC-2 CLK are 0DEG Phase Difference(falling Edge) */
#define IS_ADC_PHASE_CTRL(CTRL) (((CTRL) == ADC_PHASE_CTRL_0DEG_RISE_EDGE) || \
((CTRL) == ADC_PHASE_CTRL_90DEG_AHEAD) || \
((CTRL) == ADC_PHASE_CTRL_90DEG_LAG) || \
((CTRL) == ADC_PHASE_CTRL_0DEG_FALL_EDGE))
/**
* @}
*/
/** @defgroup ADC_CONVERSION
* @{
*/
#define ADC_SYSTEM_MODE_SINGLE_CONV 1
#define ADC_SYSTEM_MODE_CONTINUOUS_CONV 2
#define IS_ADC_CONVERSION(CONV) (((CONV) == ADC_SYSTEM_MODE_SINGLE_CONV) || \
((CONV) == ADC_SYSTEM_MODE_CONTINUOUS_CONV))
/**
* @}
*/
/** @defgroup ADC_CALIBRATION
* @{
*/
#define ADC_CALIBRATION_OFFSET 3
#define ADC_CALIBRATION_NEGTIVE_GAIN 4
#define ADC_CALIBRATION_POSTIVE_GAIN 5
#define IS_ADC_CALIBRATION(CALIB) (((CALIB) == ADC_CALIBRATION_OFFSET) || \
((CALIB) == ADC_CALIBRATION_NEGTIVE_GAIN) || \
((CALIB) == ADC_CALIBRATION_POSTIVE_GAIN))
/**
* @}
*/
/** @defgroup ADC_CHANNEL
* @{
*/
#define ADC_CHANNEL_CALIBRATION 0x0
/**
* @}
*/
/** @defgroup ADC1_CHANNEL
* @{
*/
#define ADC1_CHANNEL_VIP 0x1
#define ADC1_CHANNEL_VSEN 0x2
#define ADC1_CHANNEL_VADIO_0 0x4
#define ADC1_CHANNEL_VADIO_1 0x8
#define ADC1_CHANNEL_VADIO_2 0x10
#define ADC1_CHANNEL_VADIO_3 0x20
#define ADC1_CHANNEL_VADIO_4 0x40
#define ADC1_CHANNEL_VADIO_5 0x80
#define ADC1_CHANNEL_ALL 0xFF
#define IS_ADC1_CHANNEL(CHANNEL) (((CHANNEL) != 0) && ((CHANNEL) & ~ADC1_CHANNEL_ALL) == 0)
/**
* @}
*/
/** @defgroup ADC2_CHANNEL
* @{
*/
#define ADC2_CHANNEL_VIN 0x1
#define ADC2_CHANNEL_VTMP 0x2
#define ADC2_CHANNEL_VADIO_6 0x4
#define ADC2_CHANNEL_VADIO_7 0x8
#define ADC2_CHANNEL_VADIO_8 0x10
#define ADC2_CHANNEL_VADIO_9 0x20
#define ADC2_CHANNEL_VADIO_10 0x40
#define ADC2_CHANNEL_VADIO_11 0x80
#define ADC2_CHANNEL_ALL 0xFF
#define IS_ADC2_CHANNEL(CHANNEL) (((CHANNEL) != 0) && ((CHANNEL) & ~ADC2_CHANNEL_ALL) == 0)
/**
* @}
*/
/** @defgroup ADC_INT
* @{
*/
#define ADC1_INT_ALMOST_FULL 0x1
#define ADC2_INT_ALMOST_FULL 0x8
#define ADC_INT_ALL 0x9
#define IS_ADC_INT(INT) (((INT) != 0) && (((INT) & ~ADC_INT_ALL) == 0))
/**
* @}
*/
/**
* @brief ADC collection data structure
*/
typedef struct {
uint8_t channel; /*!< The channel of collected data, is a value of
@ref ADC_CHANNEL, @ref ADC1_CHANNEL or @ref ADC2_CHANNEL */
uint16_t data; /*!< collected data */
} ADC_Data;
/**
* @brief ADC initialization structure
*/
typedef struct
{
uint8_t ADC_PhaseCtrl; /*!< Phase between ADC1 and ADC2, is a value of @ref ADC_PHASE_CTRL */
uint8_t ADC_VsenSelection; /*!< ADC1 VSEN selection, is a value of @ref ADC_VSEN */
} ADC_InitTypeDef;
/**
* @brief ADC initialization
* @note This function should be called at first before any other interfaces.
* @param[in] init A pointer to structure ADC_InitTypeDef
* @retval None
*/
void ADC_Init(ADC_InitTypeDef* init);
/**
* @brief Enable or disable ADC.
* @param[in] adc ADC peripheral, which is a value of @ref ADC_PERIPH
* @param[in] Enable The bit indicates if the specific ADC is enable or not
* @retval None
*/
void ADC_Enable(uint8_t adc, BOOL enable);
/**
* @brief Enable or disable ADC interrupt.
* @param[in] Int interrupt mask bits, which can be a combination of @ref ADC_INT
* @param[in] Enable The bit indicates if specific interrupts are enable or not
* @retval None
*/
void ADC_EnableInt(uint32_t Int, BOOL enable);
/**
* @brief Check specific interrupts are set or not
* @param[in] Int interrupt mask bits, which can be a combination of @ref ADC_INT
* @retval BOOL The bit indicates if the specific interrupts are set or not
*/
BOOL ADC_GetIntStatus(uint32_t Int);
/**
* @brief Clear specific interrupts
* @param[in] Int interrupt mask bits, which can be a value of @ref ADC_INT
* @retval None
*/
void ADC_ClearInt(uint32_t Int);
/**
* @brief ADC starts to convert data
* @param[in] adc ADC peripheral, which is a value of @ref ADC_PERIPH
* @param[in] convMode It should be a value of @ref ADC_CONVERSION
* @param[in] channel It should be the value of @ref ADC1_CHANNEL
* or @ref ADC2_CHANNEL according to parameter 'adc'
* @retval BOOL The bit indicates if the specific ADC starts to convert data
*/
BOOL ADC_StartConversion(uint8_t adc, uint8_t convMode, uint32_t channel);
/**
* @brief ADC starts to calibrate and produces one sample
* @param[in] adc ADC peripheral, which is a value of @ref ADC_PERIPH
* @param[in] convMode It should be a value of @ref ADC_CALIBRATION
* @retval BOOL The bit indicates if the specific ADC starts to convert data
*/
BOOL ADC_StartCalibration(uint8_t adc, uint8_t calibration);
/**
* @brief ADC stops conversion or calibration
* @param[in] adc ADC peripheral, which is a value of @ref ADC_PERIPH
* @retval NULL
*/
void ADC_Stop(uint8_t adc);
/**
* @brief Check if ADC is busy or not
* @param[in] adc ADC peripheral, which is a value of @ref ADC_PERIPH
* @retval BOOL The bit indicates if the specific ADC is busy or not
*/
BOOL ADC_IsBusy(uint8_t adc);
/**
* @brief Read data from ADC
* @param[in] adc ADC peripheral, which is a value of @ref ADC_PERIPH
* @param[in] Size Expected data size to be read
* @param[out] data A user-allocated buffer to fetch data to be read
* @retval uint8_t Actual read data size
*/
uint8_t ADC_Read(uint8_t adc, uint8_t size, ADC_Data* data);
#ifdef __cplusplus
}
#endif
#endif /*__CMEM7_ADC_H */
|