6 #include PLATFORM_HEADER
12 #if (NUM_ADC_USERS > 8)
13 #error NUM_ADC_USERS must not be greater than 8, or int8u variables in adc.c must be changed
16 static int16u adcData;
17 static int8u adcPendingRequests;
18 volatile static int8u adcPendingConversion;
19 static int8u adcReadingValid;
20 static int16u adcReadings[NUM_ADC_USERS];
21 static int16u adcConfig[NUM_ADC_USERS];
22 static boolean adcCalibrated;
32 #ifdef ENABLE_ADC_EXTENDED_RANGE_BROKEN
36 static int16u adcStaticConfig;
38 void halAdcSetClock(
boolean slow)
41 adcStaticConfig |= ADC_1MHZCLK_MASK;
43 adcStaticConfig &= ~ADC_1MHZCLK_MASK;
47 void halAdcSetRange(
boolean high)
50 adcStaticConfig |= (ADC_HVSELP_MASK | ADC_HVSELN_MASK);
52 adcStaticConfig &= ~(ADC_HVSELP_MASK | ADC_HVSELN_MASK);
56 boolean halAdcGetClock(
void)
59 return (adcStaticConfig & ADC_1MHZCLK_MASK) ?
TRUE :
FALSE;
62 boolean halAdcGetRange(
void)
65 return (adcStaticConfig & ((ADC_HVSELP_MASK | ADC_HVSELN_MASK))) ?
TRUE :
FALSE;
71 #define ADC_CHAN (ADC_MUXP | ADC_MUXN)
72 #define ADC_CHAN_BIT ADC_MUXN_BIT
77 int8u conversion = adcPendingConversion;
80 if ( (INT_ADCFLAG & INT_ADCULDFULL)
81 && (conversion < NUM_ADC_USERS) ) {
82 adcReadings[conversion] = adcData;
83 adcReadingValid |=
BIT(conversion);
85 if (adcPendingRequests) {
86 for (i = 0; i < NUM_ADC_USERS; i++) {
87 if (
BIT(i) & adcPendingRequests) {
88 adcPendingConversion = i;
89 adcPendingRequests ^=
BIT(i);
90 ADC_CFG = adcConfig[i];
96 adcPendingConversion = NUM_ADC_USERS;
106 ADCUser startNextConversion()
112 if (adcPendingRequests && !(ADC_CFG & ADC_ENABLE)) {
113 for (i = 0; i < NUM_ADC_USERS; i++) {
114 if (
BIT(i) & adcPendingRequests) {
115 adcPendingConversion = i;
116 adcPendingRequests ^=
BIT(i);
117 ADC_CFG = adcConfig[i];
118 INT_ADCFLAG = 0xFFFF;
119 INT_CFGSET = INT_ADC;
130 void halInternalInitAdc(
void)
133 adcPendingRequests = 0;
134 adcPendingConversion = NUM_ADC_USERS;
135 adcCalibrated =
FALSE;
136 adcStaticConfig = ADC_1MHZCLK | ADC_ENABLE;
143 ADC_OFFSET = ADC_OFFSET_RESET;
144 ADC_GAIN = ADC_GAIN_RESET;
145 ADC_DMACFG = ADC_DMARST;
146 ADC_DMABEG = (int32u)&adcData;
148 ADC_DMACFG = (ADC_DMAAUTOWRAP | ADC_DMALOAD);
151 INT_ADCCFG = INT_ADCULDFULL;
152 INT_ADCFLAG = 0xFFFF;
153 INT_CFGSET = INT_ADC;
158 StStatus halStartAdcConversion(ADCUser
id,
159 ADCReferenceType reference,
160 ADCChannelType channel,
164 if(reference != ADC_REF_INT)
168 adcConfig[id] = ( ((rate << ADC_PERIOD_BIT) & ADC_PERIOD)
169 | ((channel << ADC_CHAN_BIT) & ADC_CHAN)
173 if (adcPendingRequests &
BIT(
id)) {
174 return ST_ADC_CONVERSION_DEFERRED;
179 adcPendingRequests |=
BIT(
id);
181 adcReadingValid &= ~
BIT(
id);
183 if (startNextConversion() == id)
184 return ST_ADC_CONVERSION_BUSY;
186 return ST_ADC_CONVERSION_DEFERRED;
189 StStatus halRequestAdcData(ADCUser
id, int16u *value)
194 || !(INT_CFGSET & INT_ADC)
195 || !(INT_ADCCFG & INT_ADCULDFULL) );
203 && ( (INT_CFGSET & INT_ADC) && (INT_ADCCFG & INT_ADCULDFULL) )) {
208 if (
BIT(
id) & adcReadingValid) {
209 *value = adcReadings[id];
210 adcReadingValid ^=
BIT(
id);
211 stat = ST_ADC_CONVERSION_DONE;
212 }
else if (adcPendingRequests &
BIT(
id)) {
213 stat = ST_ADC_CONVERSION_DEFERRED;
214 }
else if (adcPendingConversion ==
id) {
215 stat = ST_ADC_CONVERSION_BUSY;
217 stat = ST_ADC_NO_CONVERSION_PENDING;
223 StStatus halReadAdcBlocking(ADCUser
id, int16u *value)
228 stat = halRequestAdcData(
id, value);
229 if (stat == ST_ADC_NO_CONVERSION_PENDING)
231 }
while(stat != ST_ADC_CONVERSION_DONE);
235 StStatus halAdcCalibrate(ADCUser
id)
245 #ifdef ENABLE_ADC_EXTENDED_RANGE_BROKEN
246 if(halAdcGetRange()){
248 halStartAdcConversion(
id,
250 ADC_SOURCE_VREF_VREF2,
251 ADC_CONVERSION_TIME_US_4096);
253 stat = halReadAdcBlocking(
id, (int16u *)(&Nvref));
254 if (stat == ST_ADC_CONVERSION_DONE) {
255 halStartAdcConversion(
id,
257 ADC_SOURCE_VREF2_VREF2,
258 ADC_CONVERSION_TIME_US_4096);
259 stat = halReadAdcBlocking(
id, (int16u *)(&Nvref2));
261 if (stat == ST_ADC_CONVERSION_DONE) {
262 adcCalibrated =
TRUE;
264 adcCalibrated =
FALSE;
271 halStartAdcConversion(
id,
273 ADC_SOURCE_GND_VREF2,
274 ADC_CONVERSION_TIME_US_4096);
275 stat = halReadAdcBlocking(
id, (int16u *)(&Nvss));
276 if (stat == ST_ADC_CONVERSION_DONE) {
277 halStartAdcConversion(
id,
279 ADC_SOURCE_VREG2_VREF2,
280 ADC_CONVERSION_TIME_US_4096);
281 stat = halReadAdcBlocking(
id, (int16u *)(&Nvdd));
283 if (stat == ST_ADC_CONVERSION_DONE) {
285 adcCalibrated =
TRUE;
287 adcCalibrated =
FALSE;
300 int16s halConvertValueToVolts(int16u value)
306 if (!adcCalibrated) {
307 halAdcCalibrate(ADC_USER_LQI);
317 #ifdef ENABLE_ADC_EXTENDED_RANGE_BROKEN
318 if(halAdcGetRange()){
320 N = (((int32s)value + Nvref - 2*Nvref2) << 16)/(2*(Nvref-Nvref2));
323 V = (int16s)((N*12000L) >> 16);
332 nvalue = value - Nvss;
334 N = ((nvalue << 16) + Nvdd/2) / Nvdd;
339 V = (int16s)((N*9000L) >> 16);
343 #ifdef ENABLE_ADC_EXTENDED_RANGE_BROKEN
352 int8u halGetADCChannelFromGPIO(int32u io)