-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathZmodADC.c
407 lines (366 loc) · 15.9 KB
/
ZmodADC.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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
/************************************************************************/
/* */
/* ZmodADC.c - ZmodADC functions implementation */
/* */
/************************************************************************/
/* Author: Michael T. Alexander */
/* Copyright 2020, Digilent Inc. */
/************************************************************************/
/* Module Description: */
/* */
/* This source file contains the implementation of functions that can */
/* be used to calculate and display the calibration constants */
/* associated with a Zmod ADC. */
/* */
/* Note: the code for calculating the coefficients was provided by */
/* Tudor Gherman. */
/* */
/************************************************************************/
/* Revision History: */
/* */
/* 01/13/2020 (MichaelA): created */
/* 01/14/2020 (MichaelA): modified the way that coefficients are */
/* displayed based on feedback */
/* 01/15/2020 (MichaelA): modified FDisplayZmodADCCal to display the */
/* raw calibration constants as retrieved from flash */
/* 02/21/2024 (ArtVVB): added identification functions */
/* */
/************************************************************************/
/* ------------------------------------------------------------ */
/* Include File Definitions */
/* ------------------------------------------------------------ */
#if defined(__linux__)
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <linux/i2c-dev.h>
#endif
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include "stdtypes.h"
#include "syzygy.h"
#include "ZmodADC.h"
/* ------------------------------------------------------------ */
/* Miscellaneous Declarations */
/* ------------------------------------------------------------ */
#define ADC1410_IDEAL_RANGE_ADC_HIGH 1.0
#define ADC1410_IDEAL_RANGE_ADC_LOW 25.0
#define ADC1410_REAL_RANGE_ADC_HIGH 1.086
#define ADC1410_REAL_RANGE_ADC_LOW 26.25
/* ------------------------------------------------------------ */
/* Local Type Definitions */
/* ------------------------------------------------------------ */
/* ------------------------------------------------------------ */
/* Global Variables */
/* ------------------------------------------------------------ */
extern BOOL dpmutilfVerbose;
/* ------------------------------------------------------------ */
/* Local Variables */
/* ------------------------------------------------------------ */
/* ------------------------------------------------------------ */
/* Forward Declarations */
/* ------------------------------------------------------------ */
int32_t ComputeMultCoefADC1410(float cg, BOOL fHighGain);
int32_t ComputeAddCoefADC1410(float ca, BOOL fHighGain);
/* ------------------------------------------------------------ */
/* Procedure Definitions */
/* ------------------------------------------------------------ */
/* ------------------------------------------------------------ */
/*** FDisplayZmodADCCal
**
** Parameters:
** fdI2cDev - open file descriptor for underlying I2C device (linux only)
** addrI2cSlave - I2C bus address for the slave
**
** Return Value:
** fTrue for success, fFalse otherwise
**
** Errors:
** none
**
** Description:
** This function reads the factory calibration and user calibration
** areas from the ZmodADC with the specified I2C bus address,
** computes the multiplicative and additive coefficients, and then
** displays them using stdout.
*/
BOOL
FDisplayZmodADCCal(int fdI2cDev, BYTE addrI2cSlave) {
ZMOD_ADC_CAL adcal;
WORD cbRead;
time_t t;
struct tm time;
char szDate[256];
if ( ! SyzygyI2cRead(fdI2cDev, addrI2cSlave, addrAdcFactCalStart, (BYTE*)&adcal, sizeof(ZMOD_ADC_CAL), &cbRead) ) {
printf("Error: failed to read ZmodADC factory calibration from 0x%02X\n", addrI2cSlave);
printf("Error: received %d of %d bytes\n", cbRead, sizeof(ZMOD_ADC_CAL));
return fFalse;
}
t = (time_t)adcal.date;
localtime_r(&t, &time);
if ( 0 != strftime(szDate, sizeof(szDate), "%B %d, %Y at %T", &time) ) {
printf("\n Factory Calibration: %s\n", szDate);
}
printf(" CHAN_1_LG_GAIN: %f\n", adcal.cal[0][0][0]);
printf(" CHAN_1_LG_OFFSET: %f\n", adcal.cal[0][0][1]);
printf(" CHAN_1_HG_GAIN: %f\n", adcal.cal[0][1][0]);
printf(" CHAN_1_HG_OFFSET: %f\n", adcal.cal[0][1][1]);
printf(" CHAN_2_LG_GAIN: %f\n", adcal.cal[1][0][0]);
printf(" CHAN_2_LG_OFFSET: %f\n", adcal.cal[1][0][1]);
printf(" CHAN_2_HG_GAIN: %f\n", adcal.cal[1][1][0]);
printf(" CHAN_2_HG_OFFSET: %f\n", adcal.cal[1][1][1]);
printf(" Ch1LgCoefMultStatic: 0x%05X\n", (unsigned int)ComputeMultCoefADC1410(adcal.cal[0][0][0], fFalse));
printf(" Ch1LgCoefAddStatic: 0x%05X\n", (unsigned int)ComputeAddCoefADC1410(adcal.cal[0][0][1], fFalse));
printf(" Ch1HgCoefMultStatic: 0x%05X\n", (unsigned int)ComputeMultCoefADC1410(adcal.cal[0][1][0], fTrue));
printf(" Ch1HgCoefAddStatic: 0x%05X\n", (unsigned int)ComputeAddCoefADC1410(adcal.cal[0][1][1], fTrue));
printf(" Ch2LgCoefMultStatic: 0x%05X\n", (unsigned int)ComputeMultCoefADC1410(adcal.cal[1][0][0], fFalse));
printf(" Ch2LgCoefAddStatic: 0x%05X\n", (unsigned int)ComputeAddCoefADC1410(adcal.cal[1][0][1], fFalse));
printf(" Ch2HgCoefMultStatic: 0x%05X\n", (unsigned int)ComputeMultCoefADC1410(adcal.cal[1][1][0], fTrue));
printf(" Ch2HgCoefAddStatic: 0x%05X\n", (unsigned int)ComputeAddCoefADC1410(adcal.cal[1][1][1], fTrue));
if ( ! SyzygyI2cRead(fdI2cDev, addrI2cSlave, addrAdcUserCalStart, (BYTE*)&adcal, sizeof(ZMOD_ADC_CAL), &cbRead) ) {
printf("Error: failed to read ZmodADC user calibration from 0x%02X\n", addrI2cSlave);
printf("Error: received %d of %d bytes\n", cbRead, sizeof(ZMOD_ADC_CAL));
return fFalse;
}
t = (time_t)adcal.date;
localtime_r(&t, &time);
if ( 0 != strftime(szDate, sizeof(szDate), "%B %d, %Y at %T", &time) ) {
printf("\n User Calibration: %s\n", szDate);
}
printf(" CHAN_1_LG_GAIN: %f\n", adcal.cal[0][0][0]);
printf(" CHAN_1_LG_OFFSET: %f\n", adcal.cal[0][0][1]);
printf(" CHAN_1_HG_GAIN: %f\n", adcal.cal[0][1][0]);
printf(" CHAN_1_HG_OFFSET: %f\n", adcal.cal[0][1][1]);
printf(" CHAN_2_LG_GAIN: %f\n", adcal.cal[1][0][0]);
printf(" CHAN_2_LG_OFFSET: %f\n", adcal.cal[1][0][1]);
printf(" CHAN_2_HG_GAIN: %f\n", adcal.cal[1][1][0]);
printf(" CHAN_2_HG_OFFSET: %f\n", adcal.cal[1][1][1]);
printf(" Ch1LgCoefMultStatic: 0x%05X\n", (unsigned int)ComputeMultCoefADC1410(adcal.cal[0][0][0], fFalse));
printf(" Ch1LgCoefAddStatic: 0x%05X\n", (unsigned int)ComputeAddCoefADC1410(adcal.cal[0][0][1], fFalse));
printf(" Ch1HgCoefMultStatic: 0x%05X\n", (unsigned int)ComputeMultCoefADC1410(adcal.cal[0][1][0], fTrue));
printf(" Ch1HgCoefAddStatic: 0x%05X\n", (unsigned int)ComputeAddCoefADC1410(adcal.cal[0][1][1], fTrue));
printf(" Ch2LgCoefMultStatic: 0x%05X\n", (unsigned int)ComputeMultCoefADC1410(adcal.cal[1][0][0], fFalse));
printf(" Ch2LgCoefAddStatic: 0x%05X\n", (unsigned int)ComputeAddCoefADC1410(adcal.cal[1][0][1], fFalse));
printf(" Ch2HgCoefMultStatic: 0x%05X\n", (unsigned int)ComputeMultCoefADC1410(adcal.cal[1][1][0], fTrue));
printf(" Ch2HgCoefAddStatic: 0x%05X\n", (unsigned int)ComputeAddCoefADC1410(adcal.cal[1][1][1], fTrue));
return fTrue;
}
/* ------------------------------------------------------------ */
/*** FGetZmodADCCal
**
** Parameters:
** fdI2cDev - open file descriptor for underlying I2C device (linux only)
** addrI2cSlave - I2C bus address for the slave
** pFactoryCal - ZMOD_ADC_CAL object to return factory calibration data through
** pUserCal - ZMOD_ADC_CAL object to return user calibration data through
**
** Return Value:
** fTrue for success, fFalse otherwise
**
** Errors:
** none
**
** Description:
** This function reads the factory calibration and user calibration
** areas from the ZmodADC with the specified I2C bus address,
** then returns the calibration data by argument.
*/
BOOL
FGetZmodADCCal(int fdI2cDev, BYTE addrI2cSlave, ZMOD_ADC_CAL *pFactoryCal, ZMOD_ADC_CAL *pUserCal) {
WORD cbRead;
if ( ! SyzygyI2cRead(fdI2cDev, addrI2cSlave, addrAdcFactCalStart, (BYTE*)pFactoryCal, sizeof(ZMOD_ADC_CAL), &cbRead) ) {
printf("Error: failed to read ZmodADC factory calibration from 0x%02X\n", addrI2cSlave);
printf("Error: received %d of %d bytes\n", cbRead, sizeof(ZMOD_ADC_CAL));
return fFalse;
}
if ( ! SyzygyI2cRead(fdI2cDev, addrI2cSlave, addrAdcUserCalStart, (BYTE*)pUserCal, sizeof(ZMOD_ADC_CAL), &cbRead) ) {
printf("Error: failed to read ZmodADC user calibration from 0x%02X\n", addrI2cSlave);
printf("Error: received %d of %d bytes\n", cbRead, sizeof(ZMOD_ADC_CAL));
return fFalse;
}
return fTrue;
}
/* ------------------------------------------------------------ */
/*** FZmodADCCalConvertToS18
**
** Parameters:
** adcal - ZMOD_ADC_CAL object to pull calibration coefficients from
** pReturn - ZMOD_ADC_CAL_S18 object to return data by argument
**
** Return Value:
** none
**
** Errors:
** none
**
** Description:
** This function converts calibration coefficients to the 18-bit signed format used in PL hardware.
*/
void
FZmodADCCalConvertToS18(ZMOD_ADC_CAL adcal, ZMOD_ADC_CAL_S18 *pReturn) {
pReturn->cal[0][0][0] = (unsigned int)ComputeMultCoefADC1410(adcal.cal[0][0][0], fFalse);
pReturn->cal[0][0][1] = (unsigned int)ComputeAddCoefADC1410(adcal.cal[0][0][1], fFalse);
pReturn->cal[0][1][0] = (unsigned int)ComputeMultCoefADC1410(adcal.cal[0][1][0], fTrue);
pReturn->cal[0][1][1] = (unsigned int)ComputeAddCoefADC1410(adcal.cal[0][1][1], fTrue);
pReturn->cal[1][0][0] = (unsigned int)ComputeMultCoefADC1410(adcal.cal[1][0][0], fFalse);
pReturn->cal[1][0][1] = (unsigned int)ComputeAddCoefADC1410(adcal.cal[1][0][1], fFalse);
pReturn->cal[1][1][0] = (unsigned int)ComputeMultCoefADC1410(adcal.cal[1][1][0], fTrue);
pReturn->cal[1][1][1] = (unsigned int)ComputeAddCoefADC1410(adcal.cal[1][1][1], fTrue);
}
/* ------------------------------------------------------------ */
/*** ComputeMultCoefADC1410
**
** Parameters:
** cg - gain coefficient from ZmodADC flash memory
** fHighGain - fTrue for high gain setting, fFalse for low gain
** read
**
** Return Value:
** signed 32-bit value containing the multiplicative coefficient in the
** 18 least significant bits: bit 17 is the sign, bits 16:0 are the value
**
** Errors:
** none
**
** Description:
** This function computes a signed 18-bit value corresponding to the
** multiplicative calibration coefficient of the ZmodADC.
*/
int32_t
ComputeMultCoefADC1410(float cg, BOOL fHighGain) {
float fval;
int32_t ival;
fval = (fHighGain ? (ADC1410_REAL_RANGE_ADC_HIGH/ADC1410_IDEAL_RANGE_ADC_HIGH):(ADC1410_REAL_RANGE_ADC_LOW/ADC1410_IDEAL_RANGE_ADC_LOW))*(1 + cg)*(float)(1<<16);
ival = (int32_t)(fval + 0.5); // round
ival &= (1<<18) - 1; // keep only 18 bits
return ival;
}
/* ------------------------------------------------------------ */
/*** ComputeAddCoefADC1410
**
** Parameters:
** ca - additive coefficient from ZmodADC flash memory
** fHighGain - fTrue for high gain setting, fFalse for low gain
** read
**
** Return Value:
** signed 32-bit value containing the additive coefficient in the 18
** least significant bits: bit 17 is the sign, bits 16:0 are the value
**
** Errors:
** none
**
** Description:
** This function computes a signed 18-bit value corresponding to the
** additive calibration coefficient of the ZmodADC.
*/
int32_t
ComputeAddCoefADC1410(float ca, BOOL fHighGain) {
float fval;
int32_t ival;
fval = ca / (fHighGain ? ADC1410_IDEAL_RANGE_ADC_HIGH:ADC1410_IDEAL_RANGE_ADC_LOW)*(float)(1<<17);
ival = (int32_t)(fval + 0.5); // round
ival &= (1<<18) - 1; // keep only 18 bits
return ival;
}
/* ------------------------------------------------------------ */
/*** FZmodIsADC
**
** Parameters:
** Pdid - Product ID read from DNA
**
** Return Value:
** Bool, true if the PDID represents a valid Zmod ADC ID, false
** otherwise
**
** Errors:
** none
**
** Description:
** This function determines whether a given Zmod is a Zmod ADC
*/
BOOL
FZmodIsADC(DWORD Pdid) {
return ((Pdid >> 20) & 0xfff) == prodZmodADC;
}
/* ------------------------------------------------------------ */
/*** FGetZmodADCVariant
**
** Parameters:
** Pdid - Product ID read from DNA
** pVariant - ZMOD_ADC_VARIANT enum to return by argument
**
** Return Value:
** Vool, false if unsupported variant, true otherwise
**
** Errors:
** none
**
** Description:
** This function processes the product ID to determine what variant of the Zmod ADC the installed Zmod is
*/
BOOL
FGetZmodADCVariant(DWORD Pdid, ZMOD_ADC_VARIANT *pVariant) {
switch ((Pdid >> 8) & 0xfff) {
case 0x002:
*pVariant = ZMOD_ADC_VARIANT_1410_105;
return fTrue;
case 0x012:
*pVariant = ZMOD_ADC_VARIANT_1010_40;
return fTrue;
case 0x022:
*pVariant = ZMOD_ADC_VARIANT_1210_40;
return fTrue;
case 0x032:
*pVariant = ZMOD_ADC_VARIANT_1410_40;
return fTrue;
case 0x042:
*pVariant = ZMOD_ADC_VARIANT_1010_125;
return fTrue;
case 0x052:
*pVariant = ZMOD_ADC_VARIANT_1210_125;
return fTrue;
case 0x062:
*pVariant = ZMOD_ADC_VARIANT_1410_125;
return fTrue;
default:
*pVariant = ZMOD_ADC_VARIANT_UNSUPPORTED;
return fFalse;
}
}
/* ------------------------------------------------------------ */
/*** FGetZmodADCResolution
**
** Parameters:
** variant - Enum identifying the Zmod ADC variant, result of FGetZmodADCVariant
** pResolution - Pointer to return the ADC resolution by argument
**
** Return Value:
** Bool, false if the variant is unsupported, true otherwise
**
** Errors:
** none
**
** Description:
** This function uses the Zmod ADC variant to determine the ADC resolution
*/
BOOL FGetZmodADCResolution(ZMOD_ADC_VARIANT variant, DWORD *pResolution) {
switch (variant) {
case ZMOD_ADC_VARIANT_1410_105:
case ZMOD_ADC_VARIANT_1410_40:
case ZMOD_ADC_VARIANT_1410_125:
*pResolution = 14;
return fTrue;
case ZMOD_ADC_VARIANT_1210_40:
case ZMOD_ADC_VARIANT_1210_125:
*pResolution = 12;
return fTrue;
case ZMOD_ADC_VARIANT_1010_40:
case ZMOD_ADC_VARIANT_1010_125:
*pResolution = 10;
return fTrue;
case ZMOD_ADC_VARIANT_UNSUPPORTED:
return fFalse;
}
}