forked from Digilent/openscope-mz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFeedBack.c
303 lines (246 loc) · 7.98 KB
/
FeedBack.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
/************************************************************************/
/* */
/* FeedBack.C */
/* */
/* Read the Feedback circuits */
/* */
/************************************************************************/
/* Author: Keith Vogel */
/* Copyright 2017, Digilent Inc. */
/************************************************************************/
/************************************************************************/
/* Revision History: */
/* */
/* 5/22/2017 (KeithV): Created */
/************************************************************************/
#include <OpenScope.h>
/* ------------------------------------------------------------ */
/*** convertADCs
**
** Parameters:
** channelNumber - The PIC32 analog channel number as in the PIC32 datasheet
**
** Return Value:
** The converted value for that channel
**
** Errors:
** If return value of zero and error may have occured
**
** Description:
** Coverts the analog signal to a digital value on the
** given pic32 analog channel number
*/
static int convertADCs(uint8_t channelNumber)
{
uint8_t vcn = channelNumber; // assume our vitual channel number is the real one
uint32_t adcTRGmode = ADCTRGMODE; // save trigger mode
// see if we are using alternate inputs
switch(vcn)
{
case 43:
case 44:
case 50:
return(0);
break;
case 45:
ADCTRGMODEbits.SH0ALT = 1;
vcn -= 45;
break;
case 46:
ADCTRGMODEbits.SH1ALT = 1;
vcn -= 45;
break;
case 47:
ADCTRGMODEbits.SH2ALT = 1;
vcn -= 45;
break;
case 48:
ADCTRGMODEbits.SH3ALT = 1;
vcn -= 45;
break;
case 49:
ADCTRGMODEbits.SH4ALT = 1;
vcn -= 45;
break;
default:
break;
}
ADCCON3bits.ADINSEL = vcn; // say which channel to manually trigger
ADCCON3bits.RQCNVRT = 1; // manually trigger it.
// wait for completion of the conversion
if(vcn < 32)
{
uint32_t mask = 0x1 << vcn;
while((ADCDSTAT1 & mask) == 0);
}
else
{
uint32_t mask = 0x1 << (vcn - 32);
while((ADCDSTAT2 & mask) == 0);
}
// return the trigger mode to what it was
ADCTRGMODE = adcTRGmode;
// return the converted data
return((int) ((uint32_t *) &ADCDATA0)[vcn]);
}
/* ------------------------------------------------------------ */
/*** ReadFeedBackADC
**
** Parameters:
** channelNumber - The PIC32 analog channel number as in the PIC32 datasheet
**
** Return Value:
** The converted value for that channel
**
** Errors:
** If return value of zero and error may have occured
**
** Description:
** Converts the analog signal to a digital value on the
** given pic32 analog channel number. The converter must
** a class 1 or 2 ADC, that is ADC 0 - 31
*/
static int32_t SumFeedBackADC(uint8_t channelNumber, uint8_t cSum)
{
uint32_t i = 0;
int32_t value = 0;
// do an initial conversion to prime the ADC
// this is a bug with the MZ ADC
convertADCs(channelNumber);
for(i=0; i<cSum; i++)
{
value += convertADCs(channelNumber);
}
return(value);
}
static STATE AverageADCInSoftware(uint8_t channelNumber, uint8_t pwr2Ave, int32_t * pResult)
{
int32_t cSum = 1<<pwr2Ave;
int32_t sum = SumFeedBackADC(channelNumber, cSum);
bool fNeg = (sum < 0);
if(fNeg) sum *= -1;
if(pwr2Ave != 0)
{
if((sum >> (pwr2Ave-1)) & 0x1) sum = (sum >> pwr2Ave) + 1;
else sum = (sum >> pwr2Ave);
}
if(fNeg) *pResult = -sum;
else *pResult = sum;
return(Idle);
}
STATE AverageADC(uint8_t channelNumber, uint8_t pwr2Ave, int32_t * pResult)
{
static uint8_t curChannel = 0xFF;
uint32_t volatile __attribute__((unused)) flushADC;
if(channelNumber != curChannel)
{
if(channelNumber >= ADCALT)
{
return(InvalidChannel);
}
else if(curChannel == 0xFF)
{
curChannel = channelNumber;
}
else
{
return(Waiting);
}
}
// have to do the averaging in software
if(curChannel >= ADCCLASS2)
{
curChannel = 0xFF;
return(AverageADCInSoftware(channelNumber, pwr2Ave, pResult));
}
if(ADCFLTR1bits.AFEN == 0)
{
ADCFLTR1 = 0;
// read the ADC to flush any interrupts
flushADC = (&ADCDATA0)[curChannel];
ADCFLTR1bits.DFMODE = 1; // put in averaging mode
ADCFLTR1bits.OVRSAM = 0b01; // 4 samples
ADCFLTR1bits.CHNLID = channelNumber; // the channel number
IFS1CLR = _IFS1_ADCDF1IF_MASK;
IEC1CLR = _IEC1_ADCDF1IE_MASK;
ADCFLTR1bits.AFGIEN = 1; // Enable the interrupt (set the IF flag)
ADCFLTR1bits.AFEN = 1; // turn on the digital filter
ADCCON3bits.ADINSEL = channelNumber; // say which channel to manually trigger
ADCCON3bits.RQCNVRT = 1; // manually trigger it.
// wait for the IF flag to get set, this will take about 8us
while(!IFS1bits.ADCDF1IF);
// now set up for the real conversion.
ADCFLTR1bits.OVRSAM = (pwr2Ave-1) & 0b111; // How many extra bits to oversample
IFS1CLR = _IFS1_ADCDF1IF_MASK;
ADCCON3bits.RQCNVRT = 1; // manually trigger it.
}
else if(IFS1bits.ADCDF1IF)
{
*pResult = (int32_t) ((int16_t) ADCFLTR1bits.FLTRDATA);
ADCFLTR1 = 0;
flushADC = (&ADCDATA0)[curChannel]; // flush any pending interrupts
curChannel = 0xFF;
return(Idle);
}
return(Acquiring);
}
STATE FBAWGorDCuV(uint32_t channelFB, int32_t * puVolts)
{
int32_t osValue;
STATE curState = AverageADC(channelFB, 6, &osValue);
if(curState == Idle)
{
*puVolts = (int32_t) ((595000000ll * osValue - 409600ll * uVRef3V0 + 120832) / 241664);
}
return(curState);
}
STATE FBUSB5V0uV(uint32_t * puVolts)
{
int32_t osValue;
STATE curState = AverageADC(CH_USB5V0_FB, 6, &osValue);
if(curState == Idle)
{
*puVolts = (uint32_t) ((6703125ll * osValue + 1376) / 2752);
}
return(curState);
}
STATE FBNUSB5V0uV(uint32_t * puVolts)
{
int32_t osValue;
STATE curState = AverageADC(CH_VSS5V0_FB, 6, &osValue);
if(curState == Idle)
{
*puVolts = (uint32_t) ((uVRef3V0 * 4000ll - 3609375ll * osValue + 464) / 928);
}
return(curState);
}
STATE FBREF1V5uV(uint32_t * puVolts)
{
int32_t osValue;
STATE curState = AverageADC(CH_VREF1V5_FB, 6, &osValue);
if(curState == Idle)
{
*puVolts = (uint32_t) ((46875l * osValue + 32)/64);
}
return(curState);
}
STATE FBREF3V0uV(uint32_t * puVolts)
{
int32_t osValue;
STATE curState = AverageADC(CH_VREF3V0_FB, 6, &osValue);
if(curState == Idle)
{
*puVolts = (uint32_t) ((46875l * osValue + 16)/32);
}
return(curState);
}
STATE FBVCC3V3uV(uint32_t * puVolts)
{
int32_t osValue;
STATE curState = AverageADC(CH_VCC3V3_FB, 6, &osValue);
if(curState == Idle)
{
*puVolts = (uint32_t) ((1140625ll * osValue + 352)/704);
}
return(curState);
}