1 /******************************************************************************
2 * @brief providing APIs for configuring ADC module (ADC).
3 *
4 *******************************************************************************
5 *
6 * provide APIs for configuring ADC module (ADC)
7 ******************************************************************************/
8 #include "common.h"
9 #include "adc.h"
10 /******************************************************************************
11 * Local function
12 ******************************************************************************/
13 ADC_CallbackType ADC_Callback[1] = {NULL};
14 /******************************************************************************
15 * Local variables
16 ******************************************************************************/
17 
18 /******************************************************************************
19 * Local function prototypes
20 ******************************************************************************/
21 
22 /******************************************************************************
23 * define ADC APIs
24 *
25 *//*! @addtogroup adc_api_list
26 * @{
27 *******************************************************************************/
28 
29 
30 /*****************************************************************************//**
31    *
32    * @brief initialize ADC module.
33    *
34    * @param[in]  pADC point to ADC module type.
35    * @param[in]  pADC_Config point to ADC configuration structure.
36    *
37    * @return none
38    *
39    * @ Pass/ Fail criteria: none
40    *****************************************************************************/
ADC_Init(ADC_Type * pADC,ADC_ConfigTypePtr pADC_Config)41 void ADC_Init(ADC_Type *pADC, ADC_ConfigTypePtr pADC_Config)
42 {
43     if( pADC == ADC)
44     {
45         SIM->SCGC |= SIM_SCGC_ADC_MASK;
46     }
47 
48     /* set clock cource for ADC */
49     ADC_SelectClock(pADC,pADC_Config->u8ClockSource);
50 
51     /* set clock divide */
52     ADC_SelectClockDivide(pADC,pADC_Config->u8ClockDiv);
53 
54     /* set ADC mode */
55     ADC_SetMode(pADC,pADC_Config->u8Mode);
56 
57     /* set FIFO level */
58     ADC_SetFifoLevel(pADC,pADC_Config->u8FiFoLevel);
59 
60     /* set pin control */
61     pADC->APCTL1 = pADC_Config->u16PinControl;
62 
63     if( pADC_Config->sSetting.bCompareEn )
64     {
65         ADC_CompareEnable(pADC);
66     }
67 
68     if( pADC_Config->sSetting.bCompareGreaterEn )
69     {
70         ADC_CompareGreaterFunction(pADC);
71     }
72 
73     if( pADC_Config->sSetting.bContinuousEn )
74     {
75         ADC_ContinuousConversion(pADC);
76     }
77 
78     if( pADC_Config->sSetting.bCompareAndEn )
79     {
80         ADC_CompareFifoAnd(pADC);
81     }
82 
83     if( pADC_Config->sSetting.bFiFoScanModeEn )
84     {
85         ADC_FifoScanModeEnable(pADC);
86     }
87 
88     if( pADC_Config->sSetting.bHardwareTriggerEn )
89     {
90         ADC_SetHardwareTrigger(pADC);
91     }
92 
93     if( pADC_Config->sSetting.bIntEn )
94     {
95         ADC_IntEnable(pADC);
96         NVIC_EnableIRQ( ADC0_IRQn );
97     }
98 
99     if( pADC_Config->sSetting.bLongSampleEn )
100     {
101         ADC_SetLongSample(pADC);
102     }
103 
104     if( pADC_Config->sSetting.bLowPowerEn )
105     {
106         ADC_SetLowPower(pADC);
107     }
108 
109 #if !defined(CPU_NV32)
110 
111     if( pADC_Config->sSetting.bHTRGMEn )
112     {
113         ADC_HardwareTriggerMultiple(pADC);
114     }
115     else
116     {
117 		ADC_HardwareTriggerSingle(pADC);
118     }
119     if( pADC_Config->sSetting.bHTRGMASKEn )
120     {
121         ADC_HardwareTriggerMaskEnable(pADC);
122     }
123     else
124     {
125 		ADC_HardwareTriggerMaskDisable(pADC);
126     }
127     if( pADC_Config->sSetting.bHTRGMASKSEL )
128     {
129         ADC_HardwareTriggerMaskAuto(pADC);
130     }
131     else
132     {
133 		ADC_HardwareTriggerMaskNonAuto(pADC);
134     }
135 #endif
136 }
137 
138 /*****************************************************************************//*!
139    *
140    * @brief disable ADC module.
141    *
142    * @param[in]  pADC point to ADC module type.
143    *
144    * @return none.
145    *
146    * @ Pass/ Fail criteria: none.
147    *****************************************************************************/
ADC_DeInit(ADC_Type * pADC)148 void ADC_DeInit( ADC_Type *pADC )
149 {
150     ADC_SetChannel(pADC,ADC_CHANNEL_DISABLE);
151 
152     SIM->SCGC &= ~SIM_SCGC_ADC_MASK;
153 }
154 
155 /*****************************************************************************//*!
156    *
157    * @brief start a conversion and get conversion result
158    *
159    * @param[in]  pADC point to ADC module type.
160    * @param[in]  u8Channel adc channel to conversion.
161    *
162    * @return ADC conversion result.
163    *
164    * @ Pass/ Fail criteria: none
165    *****************************************************************************/
ADC_PollRead(ADC_Type * pADC,uint8_t u8Channel)166 unsigned int ADC_PollRead( ADC_Type *pADC, uint8_t u8Channel )
167 {
168 		ADC_SetChannel(pADC,u8Channel);
169 		while( !ADC_IsCOCOFlag(pADC) );
170 		return ADC_ReadResultReg(pADC);
171 }
172 
173 
174 /*****************************************************************************//*!
175    *
176    * @brief install ADC call back function.
177    *
178    * @param[in]	 pADC_CallBack point to address of  adc call back function.
179    *
180    * @return none.
181    *
182    * @ Pass/ Fail criteria: none.
183    *****************************************************************************/
ADC_SetCallBack(ADC_CallbackType pADC_CallBack)184 void ADC_SetCallBack(ADC_CallbackType pADC_CallBack)
185 {
186     ADC_Callback[0] = pADC_CallBack;
187 }
188 
189 /*****************************************************************************//*!
190    *
191    * @brief set ADC channel.
192    *
193    * @param[in]  pADC point to ADC module type.
194    * @param[in]  u8Channel adc channel to conversion.
195    *
196    * @return none
197    *
198    * @ Pass/ Fail criteria: none
199    *****************************************************************************/
ADC_SetChannel(ADC_Type * pADC,uint8_t u8Channel)200 void ADC_SetChannel( ADC_Type *pADC, uint8_t u8Channel )
201 {
202     uint32_t u32temp;
203     u32temp = pADC->SC1;
204     u32temp &= ~ADC_SC1_ADCH_MASK;
205     pADC->SC1 = u32temp|ADC_SC1_ADCH(u8Channel);
206 }
207 /*****************************************************************************//*!
208    *
209    * @brief Voltage Reference Selection.
210    *
211    * @param[in]  pADC point to ADC module type.
212    * @param[in]  u8Vref adc reference voltage selection.
213    *
214    * @return none
215    *
216    * @ Pass/ Fail criteria: none
217    *****************************************************************************/
ADC_VrefSelect(ADC_Type * pADC,uint8_t u8Vref)218 void ADC_VrefSelect( ADC_Type *pADC, uint8_t u8Vref )
219 {
220     uint32_t u32Temp;
221     u32Temp = pADC->SC2;
222     u32Temp &= ~ADC_SC2_REFSEL_MASK;
223     pADC->SC2 = u32Temp|ADC_SC2_REFSEL(u8Vref);
224 }
225 
226 /*****************************************************************************//*!
227    *
228    * @brief select clock divide
229    *
230    * @param[in]  pADC point to ADC module type.
231    * @param[in]  u8Div Clock Divide Select.
232    *
233    * @return none
234    *
235    * @ Pass/ Fail criteria: none
236    *****************************************************************************/
ADC_SelectClockDivide(ADC_Type * pADC,uint8_t u8Div)237 void ADC_SelectClockDivide( ADC_Type *pADC, uint8_t u8Div )
238 {
239     uint32_t u32Temp;
240     u32Temp = pADC->SC3;
241     u32Temp &= ~ADC_SC3_ADIV_MASK;
242     pADC->SC3 = u32Temp|ADC_SC3_ADIV(u8Div);
243 }
244 
245 /*****************************************************************************//*!
246    *
247    * @brief set ADC mode.
248    *
249    * @param[in]  pADC point to ADC module type.
250    * @param[in]  u8Mode Conversion Mode Selection.
251    *
252    * @return none
253    *
254    * @ Pass/ Fail criteria: none
255    *****************************************************************************/
ADC_SetMode(ADC_Type * pADC,uint8_t u8Mode)256 void ADC_SetMode( ADC_Type *pADC, uint8_t u8Mode )
257 {
258     uint32_t u32Temp;
259     u32Temp = pADC->SC3;
260     u32Temp &= ~ADC_SC3_MODE_MASK;
261     pADC->SC3 = u32Temp|ADC_SC3_MODE(u8Mode);
262 }
263 /*****************************************************************************//*!
264    *
265    * @brief Input Clock Select.
266    *
267    * @param[in]  pADC point to ADC module type.
268    * @param[in]  u8Clock Input Clock Select.
269    *
270    * @return none
271    *
272    * @ Pass/ Fail criteria: none
273    *****************************************************************************/
ADC_SelectClock(ADC_Type * pADC,uint8_t u8Clock)274 void ADC_SelectClock( ADC_Type *pADC, uint8_t u8Clock )
275 {
276     uint32_t u32Temp;
277     u32Temp = pADC->SC3;
278     u32Temp &= ~ADC_SC3_ADICLK_MASK;
279     pADC->SC3 = u32Temp|ADC_SC3_ADICLK(u8Clock);
280 }
281 
282 /*****************************************************************************//*!
283    *
284    * @brief FIFO Depth enables
285    *
286    * @param[in]  pADC point to ADC module type.
287    * @param[in]  u8FifoLevel set FIFO level.
288    *
289    * @return none
290    *
291    * @ Pass/ Fail criteria: none
292    *****************************************************************************/
ADC_SetFifoLevel(ADC_Type * pADC,uint8_t u8FifoLevel)293 void ADC_SetFifoLevel( ADC_Type *pADC, uint8_t u8FifoLevel )
294 {
295     uint32_t u32Temp;
296     u32Temp = pADC->SC4;
297     u32Temp &= ~ADC_SC4_AFDEP_MASK;
298     pADC->SC4 = u32Temp|ADC_SC4_AFDEP(u8FifoLevel);
299 }
300 
301 /*! @} End of adc_api_list                                               						*/
302 
303 
304 /*****************************************************************************//*!
305    *
306    * @brief ADC interrupt service routine.
307    *
308    * @param  none.
309    *
310    * @return none.
311    *
312    * @ Pass/ Fail criteria: none.
313    *****************************************************************************/
ADC_Isr(void)314 void ADC_Isr(void)
315 {
316 //	printf("input any character to start a new conversion!\n");
317     if( ADC_Callback[0] )
318     {
319         ADC_Callback[0]();
320     }
321 }
322 
323 
324 
325 
326 
327 
328 
329 
330 
331 
332 
333 
334 
335 
336 
337