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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
|
/******************************************************************************
* @brief providing APIs for configuring ADC module (ADC).
*
*******************************************************************************
*
* provide APIs for configuring ADC module (ADC)
******************************************************************************/
#include "common.h"
#include "adc.h"
/******************************************************************************
* Local function
******************************************************************************/
ADC_CallbackType ADC_Callback[1] = {NULL};
/******************************************************************************
* Local variables
******************************************************************************/
/******************************************************************************
* Local function prototypes
******************************************************************************/
/******************************************************************************
* define ADC APIs
*
*//*! @addtogroup adc_api_list
* @{
*******************************************************************************/
/*****************************************************************************//**
*
* @brief initialize ADC module.
*
* @param[in] pADC point to ADC module type.
* @param[in] pADC_Config point to ADC configuration structure.
*
* @return none
*
* @ Pass/ Fail criteria: none
*****************************************************************************/
void ADC_Init(ADC_Type *pADC, ADC_ConfigTypePtr pADC_Config)
{
if( pADC == ADC)
{
SIM->SCGC |= SIM_SCGC_ADC_MASK;
}
/* set clock cource for ADC */
ADC_SelectClock(pADC,pADC_Config->u8ClockSource);
/* set clock divide */
ADC_SelectClockDivide(pADC,pADC_Config->u8ClockDiv);
/* set ADC mode */
ADC_SetMode(pADC,pADC_Config->u8Mode);
/* set FIFO level */
ADC_SetFifoLevel(pADC,pADC_Config->u8FiFoLevel);
/* set pin control */
pADC->APCTL1 = pADC_Config->u16PinControl;
if( pADC_Config->sSetting.bCompareEn )
{
ADC_CompareEnable(pADC);
}
if( pADC_Config->sSetting.bCompareGreaterEn )
{
ADC_CompareGreaterFunction(pADC);
}
if( pADC_Config->sSetting.bContinuousEn )
{
ADC_ContinuousConversion(pADC);
}
if( pADC_Config->sSetting.bCompareAndEn )
{
ADC_CompareFifoAnd(pADC);
}
if( pADC_Config->sSetting.bFiFoScanModeEn )
{
ADC_FifoScanModeEnable(pADC);
}
if( pADC_Config->sSetting.bHardwareTriggerEn )
{
ADC_SetHardwareTrigger(pADC);
}
if( pADC_Config->sSetting.bIntEn )
{
ADC_IntEnable(pADC);
NVIC_EnableIRQ( ADC0_IRQn );
}
if( pADC_Config->sSetting.bLongSampleEn )
{
ADC_SetLongSample(pADC);
}
if( pADC_Config->sSetting.bLowPowerEn )
{
ADC_SetLowPower(pADC);
}
#if !defined(CPU_NV32)
if( pADC_Config->sSetting.bHTRGMEn )
{
ADC_HardwareTriggerMultiple(pADC);
}
else
{
ADC_HardwareTriggerSingle(pADC);
}
if( pADC_Config->sSetting.bHTRGMASKEn )
{
ADC_HardwareTriggerMaskEnable(pADC);
}
else
{
ADC_HardwareTriggerMaskDisable(pADC);
}
if( pADC_Config->sSetting.bHTRGMASKSEL )
{
ADC_HardwareTriggerMaskAuto(pADC);
}
else
{
ADC_HardwareTriggerMaskNonAuto(pADC);
}
#endif
}
/*****************************************************************************//*!
*
* @brief disable ADC module.
*
* @param[in] pADC point to ADC module type.
*
* @return none.
*
* @ Pass/ Fail criteria: none.
*****************************************************************************/
void ADC_DeInit( ADC_Type *pADC )
{
ADC_SetChannel(pADC,ADC_CHANNEL_DISABLE);
SIM->SCGC &= ~SIM_SCGC_ADC_MASK;
}
/*****************************************************************************//*!
*
* @brief start a conversion and get conversion result
*
* @param[in] pADC point to ADC module type.
* @param[in] u8Channel adc channel to conversion.
*
* @return ADC conversion result.
*
* @ Pass/ Fail criteria: none
*****************************************************************************/
unsigned int ADC_PollRead( ADC_Type *pADC, uint8_t u8Channel )
{
ADC_SetChannel(pADC,u8Channel);
while( !ADC_IsCOCOFlag(pADC) );
return ADC_ReadResultReg(pADC);
}
/*****************************************************************************//*!
*
* @brief install ADC call back function.
*
* @param[in] pADC_CallBack point to address of adc call back function.
*
* @return none.
*
* @ Pass/ Fail criteria: none.
*****************************************************************************/
void ADC_SetCallBack(ADC_CallbackType pADC_CallBack)
{
ADC_Callback[0] = pADC_CallBack;
}
/*****************************************************************************//*!
*
* @brief set ADC channel.
*
* @param[in] pADC point to ADC module type.
* @param[in] u8Channel adc channel to conversion.
*
* @return none
*
* @ Pass/ Fail criteria: none
*****************************************************************************/
void ADC_SetChannel( ADC_Type *pADC, uint8_t u8Channel )
{
uint32_t u32temp;
u32temp = pADC->SC1;
u32temp &= ~ADC_SC1_ADCH_MASK;
pADC->SC1 = u32temp|ADC_SC1_ADCH(u8Channel);
}
/*****************************************************************************//*!
*
* @brief Voltage Reference Selection.
*
* @param[in] pADC point to ADC module type.
* @param[in] u8Vref adc reference voltage selection.
*
* @return none
*
* @ Pass/ Fail criteria: none
*****************************************************************************/
void ADC_VrefSelect( ADC_Type *pADC, uint8_t u8Vref )
{
uint32_t u32Temp;
u32Temp = pADC->SC2;
u32Temp &= ~ADC_SC2_REFSEL_MASK;
pADC->SC2 = u32Temp|ADC_SC2_REFSEL(u8Vref);
}
/*****************************************************************************//*!
*
* @brief select clock divide
*
* @param[in] pADC point to ADC module type.
* @param[in] u8Div Clock Divide Select.
*
* @return none
*
* @ Pass/ Fail criteria: none
*****************************************************************************/
void ADC_SelectClockDivide( ADC_Type *pADC, uint8_t u8Div )
{
uint32_t u32Temp;
u32Temp = pADC->SC3;
u32Temp &= ~ADC_SC3_ADIV_MASK;
pADC->SC3 = u32Temp|ADC_SC3_ADIV(u8Div);
}
/*****************************************************************************//*!
*
* @brief set ADC mode.
*
* @param[in] pADC point to ADC module type.
* @param[in] u8Mode Conversion Mode Selection.
*
* @return none
*
* @ Pass/ Fail criteria: none
*****************************************************************************/
void ADC_SetMode( ADC_Type *pADC, uint8_t u8Mode )
{
uint32_t u32Temp;
u32Temp = pADC->SC3;
u32Temp &= ~ADC_SC3_MODE_MASK;
pADC->SC3 = u32Temp|ADC_SC3_MODE(u8Mode);
}
/*****************************************************************************//*!
*
* @brief Input Clock Select.
*
* @param[in] pADC point to ADC module type.
* @param[in] u8Clock Input Clock Select.
*
* @return none
*
* @ Pass/ Fail criteria: none
*****************************************************************************/
void ADC_SelectClock( ADC_Type *pADC, uint8_t u8Clock )
{
uint32_t u32Temp;
u32Temp = pADC->SC3;
u32Temp &= ~ADC_SC3_ADICLK_MASK;
pADC->SC3 = u32Temp|ADC_SC3_ADICLK(u8Clock);
}
/*****************************************************************************//*!
*
* @brief FIFO Depth enables
*
* @param[in] pADC point to ADC module type.
* @param[in] u8FifoLevel set FIFO level.
*
* @return none
*
* @ Pass/ Fail criteria: none
*****************************************************************************/
void ADC_SetFifoLevel( ADC_Type *pADC, uint8_t u8FifoLevel )
{
uint32_t u32Temp;
u32Temp = pADC->SC4;
u32Temp &= ~ADC_SC4_AFDEP_MASK;
pADC->SC4 = u32Temp|ADC_SC4_AFDEP(u8FifoLevel);
}
/*! @} End of adc_api_list */
/*****************************************************************************//*!
*
* @brief ADC interrupt service routine.
*
* @param none.
*
* @return none.
*
* @ Pass/ Fail criteria: none.
*****************************************************************************/
void ADC_Isr(void)
{
// printf("input any character to start a new conversion!\n");
if( ADC_Callback[0] )
{
ADC_Callback[0]();
}
}
|