-
Notifications
You must be signed in to change notification settings - Fork 0
/
fsl_adc.c
327 lines (273 loc) · 9.73 KB
/
fsl_adc.c
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
/*
* Copyright (c) 2013 -2014, Freescale Semiconductor, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* o Redistributions of source code must retain the above copyright notice, this list
* of conditions and the following disclaimer.
*
* o Redistributions in binary form must reproduce the above copyright notice, this
* list of conditions and the following disclaimer in the documentation and/or
* other materials provided with the distribution.
*
* o Neither the name of Freescale Semiconductor, Inc. nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
///////////////////////////////////////////////////////////////////////////////
// Includes
///////////////////////////////////////////////////////////////////////////////
// Standard C Included Files
#include <stdint.h>
#include <stdbool.h>
// SDK Included Files
#include "fsl_adc16_driver.h"
#include "adc_hw_trigger.h"
///////////////////////////////////////////////////////////////////////////////
// Variables
///////////////////////////////////////////////////////////////////////////////
uint16_t g_uiAI1[NR_SAMPLES] = {0};
uint16_t g_uiAI2[NR_SAMPLES] = {0};
uint16_t g_uiAI3[NR_SAMPLES] = {0};
uint8_t g_ucAI1Count = 0;
uint8_t g_ucAI2Count = 0;
uint8_t g_ucAI3Count = 0;
uint16_t g_uiAI1_Current = 0;
uint16_t g_uiAI2_Current = 0;
uint16_t g_uiAI3_Current = 0;
// Define array to keep run-time callback set by application.
void (* volatile g_AdcTestCallback[ADC_INSTANCE_COUNT][ADC_SC1_COUNT])(void);
volatile uint16_t g_AdcValueInt[ADC_INSTANCE_COUNT][ADC_SC1_COUNT];
///////////////////////////////////////////////////////////////////////////////
// Code
///////////////////////////////////////////////////////////////////////////////
/*!
* @brief ADC channel0 callback for fetching sample data.
*/
static void adc_chn0_isr_callback(void)
{
uint16_t temp = 0;
//ADC0_SE4b
if((ADC0_SC1A & 0x1F) == 0x04)
{
g_uiAI3_Current = ADC16_DRV_GetConvValueRAW(ADC_INST, 0);
g_uiAI3[g_ucAI3Count++] = g_uiAI3_Current;
temp = ADC0_SC1A;
temp &= ~0x1F;
temp|= 0x09;
ADC0_SC1A = temp;
if(g_ucAI3Count == NR_SAMPLES)
{
g_ucAI3Count = 0;
}
}
else if((ADC0_SC1A & 0x1F) == 0x09)
{
g_uiAI1_Current = ADC16_DRV_GetConvValueRAW(ADC_INST, 0);
g_uiAI1[g_ucAI1Count++] = g_uiAI1_Current;
temp = ADC0_SC1A;
temp &= ~0x1F;
//temp|= 0x0F;
temp|= 0x04;
ADC0_SC1A = temp;
if(g_ucAI1Count == NR_SAMPLES)
{
g_ucAI1Count = 0;
}
}
//ADC0_SE15
else if((ADC0_SC1A & 0x1F) == 0x0F)
{
g_uiAI2_Current = ADC16_DRV_GetConvValueRAW(ADC_INST, 0);
g_uiAI2[g_ucAI2Count++] = g_uiAI2_Current;
temp = ADC0_SC1A;
temp &= ~0x1F;
temp|= 0x04;
ADC0_SC1A = temp;
if(g_ucAI2Count == NR_SAMPLES)
{
g_ucAI2Count = 0;
}
}
//PC3_T = 1;
//PC3 = 0;
}
#if 0
/*!
* @brief ADC channel1 callback for fetching sample data.
*/
static void adc_chn1_isr_callback(void)
{
static uint8_t j = 0, k = 0;
uint32_t temp = 0;
//ADC0_SE9
if((ADC0_SC1B & 0x1F) == 0x9)
{
g_uiAI1[j++] = ADC16_DRV_GetConvValueRAW(ADC_INST, 1);
temp = ADC0_SC1B;
temp &= ~0x1F;
temp|= 0x0F;
ADC0_SC1B = temp;
if(j == NR_SAMPLES)
{
j = 0;
}
}
//ADC0_SE15
else if((ADC0_SC1B & 0x1F) == 0xF)
{
g_uiAI2[k++] = ADC16_DRV_GetConvValueRAW(ADC_INST, 1);
temp = ADC0_SC1B;
temp &= ~0x1F;
temp|= 0x04;
ADC0_SC1B = temp;
if(k == NR_SAMPLES)
{
k = 0;
}
}
}
#endif
/*!
* @brief Initialize the ADCx for HW trigger.
*
* @param instance The ADC instance number
*/
int32_t init_adc(uint32_t instance)
{
#if FSL_FEATURE_ADC16_HAS_CALIBRATION
adc16_calibration_param_t adcCalibraitionParam;
#endif
adc16_converter_config_t adcUserConfig;
adc16_chn_config_t adcChnConfig;
adc16_hw_average_config_t adchwaverageConfig;
//ADC16_DRV_StructInitUserConfigDefault(&adcUserConfig);
/* Special configuration for highest accuracy. */
adcUserConfig.lowPowerEnable = false;
adcUserConfig.clkDividerMode = kAdc16ClkDividerOf1;
adcUserConfig.longSampleTimeEnable = false;
//yanglliang
adcUserConfig.resolution = kAdc16ResolutionBitOfSingleEndAs16;
//adcUserConfig.resolution = kAdc16ResolutionBitOfSingleEndAs12;
adcUserConfig.clkSrc = kAdc16ClkSrcOfBusClk; //kAdc16ClkSrcOfAsynClk;
adcUserConfig.asyncClkEnable = true;
adcUserConfig.highSpeedEnable = true;
adcUserConfig.longSampleCycleMode = kAdc16LongSampleCycleOf24;
adcUserConfig.hwTriggerEnable = false;
adcUserConfig.refVoltSrc = kAdc16RefVoltSrcOfVref;
adcUserConfig.continuousConvEnable = false;
#if ( defined(FRDM_KL43Z) /* CPU_MKL43Z256VLH4 */ \
|| defined(TWR_KL43Z48M) /* CPU_MKL43Z256VLH4 */ \
|| defined(FRDM_KL27Z) /* CPU_MKL27Z64VLH4 */ \
)
adcUserConfig.refVoltSrc = kAdc16RefVoltSrcOfValt;
#endif
ADC16_DRV_Init(instance, &adcUserConfig);
#if FSL_FEATURE_ADC16_HAS_CALIBRATION
// Auto calibraion.
ADC16_DRV_GetAutoCalibrationParam(instance, &adcCalibraitionParam);
ADC16_DRV_SetCalibrationParam(instance, &adcCalibraitionParam);
#endif
// Initialization ADC for
// 12bit resolution, interrrupt mode, hw trigger enabled.
// normal convert speed, VREFH/L as reference,
// disable continuouse convert mode.
//ADC16_DRV_StructInitUserConfigDefault(&adcUserConfig);
adcUserConfig.hwTriggerEnable = true;
adcUserConfig.continuousConvEnable = false;
#if ( defined(FRDM_KL43Z) /* CPU_MKL43Z256VLH4 */ \
|| defined(TWR_KL43Z48M) /* CPU_MKL43Z256VLH4 */ \
|| defined(FRDM_KL27Z) /* CPU_MKL27Z64VLH4 */ \
)
adcUserConfig.refVoltSrc = kAdc16RefVoltSrcOfValt;
#endif
ADC16_DRV_Init(instance, &adcUserConfig);
// Install Callback function into ISR
ADC_TEST_InstallCallback(instance, 0U, adc_chn0_isr_callback);
//ADC_TEST_InstallCallback(instance, 1U, adc_chn1_isr_callback);
adcChnConfig.chnIdx = (adc16_chn_t)ADC_AI1;
#if FSL_FEATURE_ADC16_HAS_DIFF_MODE
adcChnConfig.diffConvEnable = false;
#endif /* FSL_FEATURE_ADC16_HAS_DIFF_MODE */
adcChnConfig.convCompletedIntEnable = true;
ADC0_CFG2 |= ADC_CFG2_MUXSEL_MASK;
// Configure channel0
ADC16_DRV_ConfigConvChn(instance, 0U, &adcChnConfig);
adchwaverageConfig.hwAverageEnable=false;
//yangliang for hardware average
adchwaverageConfig.hwAverageCountMode=kAdc16HwAverageCountOf4;
//adchwaverageConfig.hwAverageCountMode=kAdc16HwAverageCountOf16;
ADC16_DRV_ConfigHwAverage(0, & adchwaverageConfig); //Set Hardware Average numbers
// Configure channel1, which is used in PDB trigger case
//ADC16_DRV_ConfigConvChn(instance, 1U, &adcChnConfig);
return 0;
}
/* User-defined function to install callback. */
void ADC_TEST_InstallCallback(uint32_t instance, uint32_t chnGroup, void (*callbackFunc)(void) )
{
g_AdcTestCallback[instance][chnGroup] = callbackFunc;
}
/* User-defined function to read conversion value in ADC ISR. */
uint16_t ADC_TEST_GetConvValueRAWInt(uint32_t instance, uint32_t chnGroup)
{
return g_AdcValueInt[instance][chnGroup];
}
/* User-defined ADC ISR. */
static void ADC_TEST_IRQHandler(uint32_t instance)
{
uint32_t chnGroup;
for (chnGroup = 0U; chnGroup < ADC_SC1_COUNT; chnGroup++)
{
if ( ADC16_DRV_GetChnFlag(instance, chnGroup, kAdcChnConvCompleteFlag) )
{
g_AdcValueInt[instance][chnGroup] = ADC16_DRV_GetConvValueRAW(instance, chnGroup);
if ( g_AdcTestCallback[instance][chnGroup] )
{
(void)(*(g_AdcTestCallback[instance][chnGroup]))();
}
}
}
}
///////////////////////////////////////////////////////////////////////////////
// IRQ Handlers
///////////////////////////////////////////////////////////////////////////////
/* ADC IRQ handler that would cover the same name's APIs in startup code */
void ADC0_IRQHandler(void)
{
// Add user-defined ISR for ADC0
ADC_TEST_IRQHandler(0U);
}
#if (HW_ADC_INSTANCE_COUNT > 1U)
void ADC1_IRQHandler(void)
{
// Add user-defined ISR for ADC1
ADC_TEST_IRQHandler(1U);
}
#endif
#if (HW_ADC_INSTANCE_COUNT > 2U)
void ADC2_IRQHandler(void)
{
// Add user-defined ISR for ADC2
ADC_TEST_IRQHandler(2U);
}
#endif
#if (HW_ADC_INSTANCE_COUNT > 3U)
void ADC3_IRQHandler(void)
{
// Add user-defined ISR for ADC3
ADC_TEST_IRQHandler(3U);
}
#endif