forked from Digilent/openscope-mz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDCInstruments.c
194 lines (160 loc) · 6.13 KB
/
DCInstruments.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
/************************************************************************/
/* */
/* DCInstruments.C */
/* */
/* Calibration file for the DC Outs */
/* It also contains the reference voltage identification */
/* */
/************************************************************************/
/* Author: Keith Vogel */
/* Copyright 2016, Digilent Inc. */
/************************************************************************/
/************************************************************************/
/* Revision History: */
/* */
/* 2/22/2016 (KeithV): Created */
/************************************************************************/
#include <OpenScope.h>
#define ADC_FB_AVERAGING 32
// make sure (300000ul/ADC_FB_AVERAGING) is an integer 300000/32 = 9375
#define ADCuV(Vadc) 10ul * ((((Vadc) * (300000ul/ADC_FB_AVERAGING)) + 2048ul) / 4096ul)
STATE InitSystemVoltages(void)
{
static STATE stateVOLT = Idle;
switch(stateVOLT)
{
case Idle:
stateVOLT = VOLTUSB;
// fall thru
case VOLTUSB:
if(FBUSB5V0uV(&uVUSB) == Idle)
{
stateVOLT = VOLT3V3;
}
break;
case VOLT3V3:
if(FBVCC3V3uV(&uV3V3) == Idle)
{
stateVOLT = VOLT3V0;
}
break;
case VOLT3V0:
if(FBREF3V0uV(&uVRef3V0) == Idle)
{
stateVOLT = VOLT1V5;
}
break;
case VOLT1V5:
if(FBREF1V5uV(&uVRef1V5) == Idle)
{
stateVOLT = VOLTNUSB;
}
break;
case VOLTNUSB:
if(FBNUSB5V0uV(&uVNUSB) == Idle)
{
stateVOLT = Idle;
}
break;
}
return(stateVOLT);
}
// uVDC = -40000*Dpwm + 7000000 --- ideally
STATE DCCalibrate(HINSTR hDCVolt)
{
DCVOLT * pDCVOLT = (DCVOLT *) hDCVolt;
if(hDCVolt == NULL)
{
return(STATEError);
}
if(!(pDCVOLT->comhdr.activeFunc == DCFnCal || pDCVOLT->comhdr.activeFunc == SMFnNone))
{
return(Waiting);
}
switch(pDCVOLT->comhdr.state)
{
case Idle:
pDCVOLT->comhdr.activeFunc = DCFnCal;
pDCVOLT->pOCdc->OCxRS = 100; // set to +3v
pDCVOLT->comhdr.tStart = SYSGetMilliSecond();
pDCVOLT->comhdr.state = DCWaitHigh;
break;
case DCWaitHigh:
if(SYSGetMilliSecond() - pDCVOLT->comhdr.tStart >= PWM_SETTLING_TIME)
{
pDCVOLT->comhdr.state = DCReadHigh;
}
break;
case DCReadHigh:
if(FBAWGorDCuV(pDCVOLT->channelFB, ((int32_t *) &(pDCVOLT->A))) == Idle)
{
pDCVOLT->pOCdc->OCxRS = 250; // set to -3V
pDCVOLT->comhdr.tStart = SYSGetMilliSecond();
pDCVOLT->comhdr.state = DCWaitLow;
}
break;
case DCWaitLow:
if(SYSGetMilliSecond() - pDCVOLT->comhdr.tStart >= PWM_SETTLING_TIME)
{
pDCVOLT->comhdr.state = DCReadLow;
}
break;
case DCReadLow:
if(FBAWGorDCuV(pDCVOLT->channelFB, ((int32_t *) &(pDCVOLT->B))) == Idle)
{
int32_t D1 = pDCVOLT->A;
int32_t D2 = pDCVOLT->B;
// B==7000000, A==40000 -- ideally
*((int32_t *) &(pDCVOLT->A)) = ((D1 - D2) + 75l) / 150l; // remove scaling in D1, D2
*((int32_t *) &(pDCVOLT->B)) = D1 + pDCVOLT->A * 100l; // remove scaling from D1
pDCVOLT->comhdr.idhdr.cfg = CFGCAL;
pDCVOLT->comhdr.state = Idle;
pDCVOLT->comhdr.activeFunc = SMFnNone;
}
break;
default:
pDCVOLT->comhdr.state = Idle;
pDCVOLT->comhdr.activeFunc = SMFnNone;
return(STATEError);
}
return(pDCVOLT->comhdr.state);
}
STATE DCSetVoltage(HINSTR hDCVolt, int32_t mvDCout)
{
DCVOLT * pDCVOLT = (DCVOLT *) hDCVolt;
if(hDCVolt == NULL)
{
return(STATEError);
}
if(!( (pDCVOLT->comhdr.activeFunc == DCFnSet && pDCVOLT->mvDCout == mvDCout) || pDCVOLT->comhdr.activeFunc == SMFnNone))
{
return(Waiting);
}
switch(pDCVOLT->comhdr.state)
{
case Idle:
pDCVOLT->comhdr.activeFunc = DCFnSet;
pDCVOLT->mvDCout = mvDCout;
// uVout = -A * Dpwm + B
// A * Dpwm = B - uVout
// Dpwm = (B -uVout) / A
pDCVOLT->pOCdc->OCxRS = (uint16_t) (((pDCVOLT->B - (1000l * mvDCout)) + (pDCVOLT->A/2)) / pDCVOLT->A);
// set the timer
pDCVOLT->comhdr.tStart = SYSGetMilliSecond();
pDCVOLT->comhdr.state = DCSetWait;
break;
case DCSetWait:
if(SYSGetMilliSecond() - pDCVOLT->comhdr.tStart >= PWM_SETTLING_TIME)
{
// just right, get out
pDCVOLT->comhdr.state = Idle;
pDCVOLT->comhdr.activeFunc = SMFnNone;
}
break;
default:
pDCVOLT->comhdr.state = Idle;
pDCVOLT->comhdr.activeFunc = SMFnNone;
return(STATEError);
}
return(pDCVOLT->comhdr.state);
}