forked from teledyne-e2v/ModuleControl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModuleControl.cpp
More file actions
499 lines (426 loc) · 8.94 KB
/
ModuleControl.cpp
File metadata and controls
499 lines (426 loc) · 8.94 KB
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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <math.h>
#include "ModuleControl.hpp"
#define SENSORS_I2C_ADDR 0x10
#define PDA50_I2C_ADDR 0x0C
#define TEMP_NCT_I2C_ADDR 0x4C
#define TEMP_TMP_I2C_ADDR 0x4E
#define EEPROM_I2C_ADDR 0x50
#define REG_FB_STATE 0x53
#define REG_LINE_LENGTH 0x06
#define REG_TINT_LL 0x0B
#define REG_TINT_CK 0x0C
#define REG_ANA_GAIN 0x0D
#define REG_DIG_GAIN 0x0E
#define REG_FRAME_PERIOD 0x07
#define E2V_NODE_PATH "/sys/devices/platform/soc/ac50000.qcom,cci/ac50000.qcom,cci:qcom,cam-sensor3/e2v_node"
#define E2VTOPAZ_READ_MODE 0
#define E2VTOPAZ_WRITE_MODE 1
#define DEBUG 0
/************************************************
*Main fuction
************************************************/
ModuleCtrl::~ModuleCtrl()
{
#ifndef DEBUG_MODE
ModuleControlClose(); // close IC2
#endif
}
/************************************************
*Init I2C bus
************************************************/
void ModuleCtrl::ModuleControlInit()
{
/* Open device node */
fd = open(E2V_NODE_PATH, O_RDWR);
if(fd < 0)
{
fprintf(stderr, "E2V_NODE_PATH open error\n");
}
printf("open E2V_NODE_PATH\n");
}
/************************************************
*Close I2C bus
************************************************/
void ModuleCtrl::ModuleControlClose()
{
/* Close device node */
close(fd);
printf("close E2V_NODE_PATH\n");
}
/************************************************
*Read register
************************************************/
int ModuleCtrl::readReg(int regAddr, int *value)
{
char buf[50] = {0};
char read_buf[50] = {0};
int ret;
int error=0;
unsigned int read_addr;
unsigned int read_data;
sprintf(buf, "%d 0x%02hx", E2VTOPAZ_READ_MODE, regAddr);
ret = write(fd, buf, strlen(buf)+1);
if(DEBUG==1) printf("return write: %d\n", ret);
ret = lseek(fd, 0, SEEK_SET);
if(DEBUG==1) printf("return lseek: %d\n", ret);
//write() change the fd pos, need use lseek to make pos point to the start of the file
ret = read(fd, read_buf, sizeof(read_buf));
if(DEBUG==1) printf("return read: %d, buf: %s\n", ret, read_buf);
if (sscanf(read_buf, "addr:0x%02x data:0x%04x", &read_addr, &read_data) == 2)
{
if(DEBUG==1) printf("read_addr: 0x%02x / read_data: 0x%04x\n", read_addr, read_data);
*value=read_data;
}
else
{
if(DEBUG==1) printf("Failed to parse the read_buf: %s!\n", read_buf);
error=2;
}
return error;
}
/************************************************
*Write register
************************************************/
int ModuleCtrl::writeReg(int regAddr, int value)
{
int ret;
char buf[30] = {0};
int error=0;
if(DEBUG==1) printf("reg_addr: 0x%x, reg_data: 0x%x\n", regAddr, value);
sprintf(buf, "%d 0x%02hx 0x%04hx", E2VTOPAZ_WRITE_MODE, regAddr, value);
if(DEBUG==1) printf("buf val: %s\n", buf);
ret = write(fd, buf, strlen(buf)+1);
if(DEBUG==1) printf("return write: %d\n", ret);
return error;
}
/************************************************
*Get the sensor state
************************************************/
int ModuleCtrl::read_sensor_state(int *state)
{
int regAddr;
int value=0;
int error=0;
regAddr = REG_FB_STATE;
error=this->readReg(regAddr, &value);
*state=value;
return error;
}
/************************************************
*Set the integration time (ms)
************************************************/
int ModuleCtrl::setTint(float tint)
{
int regAddr, tline;
struct solution init;
int error=0;
tint = tint * 1000000; // convert to nanoseconds
tint = tint / 20; // convert to clock cycle 50MHz
// read the line length
regAddr = REG_LINE_LENGTH;
error=this->readReg(regAddr, &tline);
if (error != 0)
{
fprintf(stderr, "READ ERROR: 0x%x\n", regAddr);
error=-1;
return error;
}
init.tintLL = (int)tint / tline;
init.tintCK = (int)(tint - init.tintLL * tline);
// write in reg_tint_ll
regAddr = REG_TINT_LL;
error=this->writeReg(regAddr, init.tintLL);
if (error != 0)
{
fprintf(stderr, "WRITE ERROR: 0x%x\n", regAddr);
error=-2;
return error;
}
// write in reg_tint_ck
regAddr = REG_TINT_CK;
error=this->writeReg(regAddr, init.tintCK);
if (error != 0)
{
fprintf(stderr, "WRITE ERROR: 0x%x\n", regAddr);
error=-3;
return error;
}
return error;
}
/************************************************
*Set the analog gain
************************************************/
int ModuleCtrl::setAnalogGain(float again)
{
int regAddr, value;
int error=0;
value = (int)(again * 100);
switch (value)
{
case 100:
value = 0;
break;
case 120:
value = 1;
break;
case 145:
value = 2;
break;
case 150:
value = 2;
break;
case 171:
value = 3;
break;
case 200:
value = 4;
break;
case 240:
value = 5;
break;
case 300:
value = 6;
break;
case 343:
value = 7;
break;
case 400:
value = 8;
break;
case 480:
value = 9;
break;
case 500:
value = 9;
break;
case 600:
value = 10;
break;
case 686:
value = 11;
break;
case 700:
value = 11;
break;
case 800:
value = 12;
break;
case 900:
value = 13;
break;
case 960:
value = 13;
break;
case 1000:
value = 13;
break;
case 1200:
value = 14;
break;
case 1600:
value = 15;
break;
default:
printf("Forbidden value, couldn't set analog gain\n");
return 0;
}
// write in register
regAddr = REG_ANA_GAIN;
error=this->writeReg(regAddr, value);
if (error != 0)
{
fprintf(stderr, "WRITE ERROR: 0x%x\n", regAddr);
error=-1;
return error;
}
return 0;
}
/************************************************
*Set the digital gain
************************************************/
int ModuleCtrl::setDigitalGain(float dgain)
{
int regAddr, value;
int error=0;
regAddr = REG_DIG_GAIN;
//from x0.004 to x16 (decimal ex: x1=256, x1.5=384...)
value = (int)(dgain * 256);
if (value > 4095)
{
value = 4095;
}
if (value < 1)
{
value = 1;
}
// write in register
regAddr = REG_DIG_GAIN;
error=this->writeReg(regAddr, value);
if (error != 0)
{
fprintf(stderr, "WRITE ERROR: 0x%x\n", regAddr);
error=-1;
return error;
}
return error;
}
/************************************************
*Set the global gain
************************************************/
int ModuleCtrl::setGain(float gain)
{
float again, dgain;
int againVal, dgainVal, regAddr, value;
int error=0;
regAddr = REG_DIG_GAIN;
//RANGE: x0.004 to x256 (decimal ex: x1=256, x1.5=384...)
//again = (int)(gain * 100);
if(gain < 1.2)
{
again=1;
againVal = 0; //x1
}
else if(gain < 1.45)
{
again=1.2;
againVal = 1; //x1.2
}
else if(gain < 1.71)
{
again=1.45;
againVal = 2; //x1.45
}
else if(gain < 2.00)
{
again=1.71;
againVal = 3; //x1.71
}
else if(gain < 2.40)
{
again=2;
againVal = 4; //x2.0
}
else if(gain < 3.00)
{
again=2.4;
againVal = 5; //x2.4
}
else if(gain < 3.43)
{
again=3.0;
againVal = 6; //x3.0
}
else if(gain < 4.00)
{
again=3.43;
againVal = 7; //x3.43
}
else if(gain < 4.80)
{
again=4.0;
againVal = 8; //x4.0
}
else if(gain < 6.00)
{
again=4.80;
againVal = 9; //x4.8
}
else if(gain < 6.86)
{
again=6.0;
againVal = 10; //x6.0
}
else if(gain < 8.00)
{
again=6.86;
againVal = 11; //x6.86
}
else if(gain < 9.60)
{
again=8.0;
againVal = 12; //x8
}
else if(gain < 12.00)
{
again=9.60;
againVal = 13; //x9.6
}
else if(gain < 16.00)
{
again=12.0;
againVal = 14; //x12
}
else
{
again=16.0;
againVal = 15; //x16
}
//from x0.004 to x16 (decimal ex: x1=256, x1.5=384...)
dgain=gain/again;
dgainVal = (int)(dgain * 256);
if (dgainVal > 4095)
{
dgainVal = 4095;
}
if (dgainVal < 1)
{
dgainVal = 1;
}
printf("GAIN: %4.2lf => AGAIN: %4.2lf (%d) / DGAIN: %4.2lf (%d)\n",gain, again, againVal, dgain, dgainVal);
// write in register
regAddr = REG_ANA_GAIN;
error=this->writeReg(regAddr, againVal);
if (error != 0)
{
fprintf(stderr, "WRITE ERROR: 0x%x\n", regAddr);
error=-1;
return error;
}
// write in register
regAddr = REG_DIG_GAIN;
error=this->writeReg(regAddr, dgainVal);
if (error != 0)
{
fprintf(stderr, "WRITE ERROR: 0x%x\n", regAddr);
error=-2;
return error;
}
return error;
}
/************************************************
*Set the frame period (ms) for frame rate control
************************************************/
int ModuleCtrl::setFramePeriod(float tperiod)
{
int regAddr, regVal, tline;
int error=0;
tperiod = tperiod * 1000000; // convert to nanoseconds
tperiod = tperiod / 20; // convert to clock cycle 50MHz
// read the line length
regAddr = REG_LINE_LENGTH;
error=this->readReg(regAddr, &tline);
if (error != 0)
{
fprintf(stderr, "READ ERROR: 0x%x\n", regAddr);
error=-1;
return error;
}
regVal = (int)tperiod / tline;
// write in reg_frame_period
regAddr = REG_FRAME_PERIOD;
error=this->writeReg(regAddr, regVal);
if (error != 0)
{
fprintf(stderr, "WRITE ERROR: 0x%x\n", regAddr);
error=-2;
return error;
}
return error;
}