-
Notifications
You must be signed in to change notification settings - Fork 1
/
sensor.c
520 lines (455 loc) · 14.9 KB
/
sensor.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
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
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
/*
## Cypress FX3 Camera Kit source file (sensor.c)
## ===========================
##
## Copyright Cypress Semiconductor Corporation, 2010-2012,
## All Rights Reserved
## UNPUBLISHED, LICENSED SOFTWARE.
##
## CONFIDENTIAL AND PROPRIETARY INFORMATION
## WHICH IS THE PROPERTY OF CYPRESS.
##
## Use of this file is governed
## by the license agreement included in the file
##
## <install>/license/license.txt
##
## where <install> is the Cypress software
## installation root directory path.
##
## ===========================
*/
/* This file implements the I2C based driver for the EV76C541 image sensor used
in the FX3 HD 720p camera kit.
Please refer to the Aptina EV76C541 sensor datasheet for the details of the
I2C commands used to configure the sensor.
*/
#include <cyu3system.h>
#include <cyu3os.h>
#include <cyu3dma.h>
#include <cyu3error.h>
#include <cyu3uart.h>
#include <cyu3i2c.h>
#include <cyu3types.h>
#include <cyu3gpio.h>
#include <cyu3utils.h>
#include "sensor.h"
#include "util.h"
#include "appi2c.h"
#include "auxiliary.h"
#define CONFIRM_TRIES 5
#define PLL_BYPASS
/* ATTiny20 registers */
#define REG_ATTINY_INIT 0xFF
/* Python 480 registers */
#define SENSOR_SOFT_RESET_ANALOG_VAL 0x0999
#define SENSOR_CHIP_ID_VAL 0x5004
#define SENSOR_SEQ_GEN_CONFIG_START 0x0001
#define SENSOR_SEQ_GEN_CONFIG_STOP 0x0001
#define SENSOR_ANALOG_GAIN_1X 0x00E1
#define SENSOR_ANALOG_GAIN_2X 0x00E4
#define SENSOR_ANALOG_GAIN_3_5X 0x0024
#define SENSOR_SYNC_CONFIG_EXPS_SYNC 0x037A
#define SENSOR_SYNC_CONFIG_EXPS_NOSYNC 0x0372
#define SENSOR_SYNC_CONFIG_ROI_SYNC 0x037A
#define SENSOR_SYNC_CONFIG_ROI_NOSYNC 0x037A & !(1 << 5)
#define SENSOR_ROI0_X_CONFIG_FULL (175 << 8) | 24 //0x9700 //0xC900 // assumes granularity is 4
#define SENSOR_ROI0_Y_CONFIG_FULL 0x9700
#define SENSOR_ROI1_X_CONFIG_FULL (136 << 8) | 65 // assumes granularity is 4
#define SENSOR_ROI1_Y_CONFIG_FULL (111 << 8) | 40
#define SENSOR_120FPS_MULT_TIMER 195
#ifdef PLL_BYPASS
#define SENSOR_120FPS_FR_LENGTH 667
#define SENSOR_120FPS_MAX_EXPOSURE 666 //546
#else
#define SENSOR_120FPS_FR_LENGTH 2214
#define SENSOR_120FPS_MAX_EXPOSURE 2213
#endif
#define SENSOR_30FPS_MULT_TIMER 1000
#ifdef PLL_BYPASS
#define SENSOR_30FPS_FR_LENGTH 547
#define SENSOR_30FPS_MAX_EXPOSURE 546
#else
#define SENSOR_30FPS_FR_LENGTH 2214
#define SENSOR_30FPS_MAX_EXPOSURE 2213
#endif
#define SENSOR_INITIAL_SETTING_ANALOG_GAIN SENSOR_ANALOG_GAIN_2X
// Startup values
CyU3PReturnStatus_t
SensorIsOn (
CyBool_t *isOn)
{
CyU3PReturnStatus_t apiRetStatus;
uint8_t sensor_ctrl_val = 0;
uint16_t sensor_reset_val = 0;
apiRetStatus = I2CRead (AUX_ADDR_RD, REG_SENSOR_CTRL, 1, &sensor_ctrl_val);
if (sensor_ctrl_val & SENSOR_CTRL_POWER_bm) {
I2CSensorConditionalRead (&apiRetStatus,
SENSOR_ADDR_RD,
SENSOR_REG_SOFT_RESET_ANALOG,
&sensor_reset_val);
if (apiRetStatus == CY_U3P_SUCCESS) {
*isOn = sensor_reset_val != SENSOR_SOFT_RESET_ANALOG_VAL;
}
}
return apiRetStatus;
}
/* EV76C541 sensor initialization sequence.
Sensor_Configure_Serdes: configure I2C bridge to CMOS board
Sensor_Configure_EV76C541: configure EV76C541 on CMOS board
Select Video Resolution
SensorChangeConfig : Update sensor configuration.
*/
CyU3PReturnStatus_t
SensorInit (
void)
{
CyU3PReturnStatus_t apiRetStatus;
apiRetStatus = SensorConfigurePython480 (CyTrue, CyTrue);
if (apiRetStatus == CY_U3P_SUCCESS) {
CyU3PThreadSleep (50); // sleep for 50 ms
/* Update sensor configuration based on desired video stream parameters.
* Using 808x608 at 30fps as default setting.
*/
apiRetStatus = SensorConfigureRoi1 ();
if (apiRetStatus == CY_U3P_SUCCESS) {
apiRetStatus = SensorConfigureRoi2 ();
if (apiRetStatus == CY_U3P_SUCCESS) {
//apiRetStatus = SensorScaling_808_608_30fps ();
//SensorSetRoi (0);
if (apiRetStatus == CY_U3P_SUCCESS) {
apiRetStatus = SensorSetGain (SENSOR_INITIAL_SETTING_ANALOG_GAIN);
if (apiRetStatus == CY_U3P_SUCCESS) {
apiRetStatus = ScopeAdcStart (CyTrue, 0x0034);
}
}
}
}
}
return apiRetStatus;
}
CyU3PReturnStatus_t
SensorStart (
void)
{
CyU3PReturnStatus_t apiRetStatus;
apiRetStatus = I2CSensorWrite (SENSOR_ADDR_WR,
SENSOR_REG_SEQ_GEN_CONFIG,
SENSOR_SEQ_GEN_CONFIG_START);
return apiRetStatus;
}
CyU3PReturnStatus_t
SensorStop (
void)
{
CyU3PReturnStatus_t apiRetStatus;
apiRetStatus = I2CSensorWrite (SENSOR_ADDR_WR,
SENSOR_REG_SEQ_GEN_CONFIG,
SENSOR_SEQ_GEN_CONFIG_STOP);
return apiRetStatus;
}
CyU3PReturnStatus_t
SensorDisable (
void)
{
CyU3PReturnStatus_t apiRetStatus;
CyBool_t isOn = CyFalse;
apiRetStatus = SensorIsOn (&isOn);
if (apiRetStatus == CY_U3P_SUCCESS && isOn) {
apiRetStatus = SensorConfigurePython480 (CyFalse, CyFalse); // turn off sensor
if (apiRetStatus == CY_U3P_SUCCESS) {
CyU3PThreadSleep (20); // sleep for 20 ms to wait for power down
}
}
return apiRetStatus;
}
/*
* Verify that the sensor can be accessed over the I2C bus from FX3.
*/
CyU3PReturnStatus_t
SensorI2CBusTest (
CyBool_t *success)
{
/* The sensor ID register can be read here to verify sensor connectivity. */
CyU3PReturnStatus_t apiRetStatus;
uint16_t chip_id = 0;
/* Reading sensor ID */
apiRetStatus = I2CSensorRead (SENSOR_ADDR_RD, SENSOR_REG_CHIP_ID, &chip_id);
if (apiRetStatus == CY_U3P_SUCCESS)
{
*success = chip_id == SENSOR_CHIP_ID_VAL;
}
return apiRetStatus;
}
CyU3PReturnStatus_t
SensorConfigureRoi1(
void)
{
CyU3PReturnStatus_t apiRetStatus;
apiRetStatus = I2CSensorWrite (SENSOR_ADDR_WR,
SENSOR_REG_ROI0_X_CONFIG,
SENSOR_ROI0_X_CONFIG_FULL);
I2CSensorConditionalWrite (&apiRetStatus,
SENSOR_ADDR_WR,
SENSOR_REG_ROI0_Y_CONFIG,
SENSOR_ROI0_Y_CONFIG_FULL);
return apiRetStatus;
}
CyU3PReturnStatus_t
SensorConfigureRoi2(
void)
{
CyU3PReturnStatus_t apiRetStatus;
apiRetStatus = I2CSensorWrite (SENSOR_ADDR_WR,
SENSOR_REG_ROI1_X_CONFIG,
SENSOR_ROI1_X_CONFIG_FULL);
I2CSensorConditionalWrite (&apiRetStatus,
SENSOR_ADDR_WR,
SENSOR_REG_ROI1_Y_CONFIG,
SENSOR_ROI1_Y_CONFIG_FULL);
return apiRetStatus;
}
/*
The procedure is adapted from Aptina's sensor initialization scripts. Please
refer to the EV76C541 sensor datasheet for details.
*/
CyU3PReturnStatus_t
SensorScaling_288_288_120fps (
void)
{
CyU3PReturnStatus_t apiRetStatus;
uint16_t regvalue;
I2CSensorRead (SENSOR_ADDR_RD,
SENSOR_REG_MULT_TIMER0,
®value
);
I2CSensorRead (SENSOR_ADDR_RD,
SENSOR_REG_FR_LENGTH0,
®value
);
I2CSensorRead (SENSOR_ADDR_RD,
SENSOR_REG_EXPOSURE0,
®value
);
/* Stop updating image sensor until all values are written */
apiRetStatus = I2CSensorWrite (SENSOR_ADDR_WR,
SENSOR_REG_SYNC_CONFIG,
SENSOR_SYNC_CONFIG_EXPS_NOSYNC);
/*
Video configuration
Oscillator frequency is 66.66 MHz, period 15 ns
PLL is enabled at 4x osc frequency, 266.66 MHz and 4 ns
set mult_timer to 1000 to get period of 4 us for timing registers
The actual frame length and exposure length was calculated using the ON
semi exposure calculator
*/
I2CSensorConditionalWrite (&apiRetStatus,
SENSOR_ADDR_WR,
SENSOR_REG_MULT_TIMER0,
SENSOR_120FPS_MULT_TIMER);
I2CSensorConditionalWrite (&apiRetStatus,
SENSOR_ADDR_WR,
SENSOR_REG_FR_LENGTH0,
SENSOR_120FPS_FR_LENGTH);
I2CSensorConditionalWrite (&apiRetStatus,
SENSOR_ADDR_WR,
SENSOR_REG_EXPOSURE0,
SENSOR_120FPS_MAX_EXPOSURE);
/* Resume updating image sensor */
I2CSensorConditionalWrite (&apiRetStatus,
SENSOR_ADDR_WR,
SENSOR_REG_SYNC_CONFIG,
SENSOR_SYNC_CONFIG_EXPS_SYNC);
I2CSensorRead (SENSOR_ADDR_RD,
SENSOR_REG_MULT_TIMER0,
®value
);
I2CSensorRead (SENSOR_ADDR_RD,
SENSOR_REG_FR_LENGTH0,
®value
);
I2CSensorRead (SENSOR_ADDR_RD,
SENSOR_REG_EXPOSURE0,
®value
);
return apiRetStatus;
}
/*
The procedure is adapted from Aptina's sensor initialization scripts. Please
refer to the EV76C541 sensor datasheet for details.
*/
CyU3PReturnStatus_t
SensorScaling_608_608_30fps (
void)
{
CyU3PReturnStatus_t apiRetStatus;
uint16_t regvalue;
I2CSensorRead (SENSOR_ADDR_RD,
SENSOR_REG_MULT_TIMER0,
®value
);
I2CSensorRead (SENSOR_ADDR_RD,
SENSOR_REG_FR_LENGTH0,
®value
);
I2CSensorRead (SENSOR_ADDR_RD,
SENSOR_REG_EXPOSURE0,
®value
);
/* Stop updating image sensor until all values are written */
apiRetStatus = I2CSensorWrite (SENSOR_ADDR_WR,
SENSOR_REG_SYNC_CONFIG,
SENSOR_SYNC_CONFIG_EXPS_NOSYNC);
/*
Video configuration
Oscillator frequency is 66.66 MHz, period 15 ns
PLL is enabled at 4x osc frequency, 266.66 MHz and 4 ns
set mult_timer to 1000 to get period of 4 us for timing registers
The actual frame length and exposure length was calculated using the ON
semi exposure calculator
*/
I2CSensorConditionalWrite (&apiRetStatus,
SENSOR_ADDR_WR,
SENSOR_REG_MULT_TIMER0,
SENSOR_30FPS_MULT_TIMER);
I2CSensorConditionalWrite (&apiRetStatus,
SENSOR_ADDR_WR,
SENSOR_REG_FR_LENGTH0,
SENSOR_30FPS_FR_LENGTH);
I2CSensorConditionalWrite (&apiRetStatus,
SENSOR_ADDR_WR,
SENSOR_REG_EXPOSURE0,
SENSOR_30FPS_MAX_EXPOSURE);
/* Resume updating image sensor */
I2CSensorConditionalWrite (&apiRetStatus,
SENSOR_ADDR_WR,
SENSOR_REG_SYNC_CONFIG,
SENSOR_SYNC_CONFIG_EXPS_SYNC);
I2CSensorRead (SENSOR_ADDR_RD,
SENSOR_REG_MULT_TIMER0,
®value
);
I2CSensorRead (SENSOR_ADDR_RD,
SENSOR_REG_FR_LENGTH0,
®value
);
I2CSensorRead (SENSOR_ADDR_RD,
SENSOR_REG_EXPOSURE0,
®value
);
return apiRetStatus;
}
/*
Get the current gain setting from the Python 480 sensor.
*/
CyU3PReturnStatus_t
SensorGetGain (
uint8_t *translated_gain)
{
CyU3PReturnStatus_t apiRetStatus;
uint16_t sensor_gain = 0;
apiRetStatus = I2CSensorRead (SENSOR_ADDR_RD,
SENSOR_REG_ANALOG_GAIN,
&sensor_gain);
if (apiRetStatus == CY_U3P_SUCCESS) {
switch (sensor_gain) {
case SENSOR_ANALOG_GAIN_1X:
*translated_gain = SENSOR_A_GAIN_TRANSL_1X;
break;
case SENSOR_ANALOG_GAIN_2X:
*translated_gain = SENSOR_A_GAIN_TRANSL_2X;
break;
case SENSOR_ANALOG_GAIN_3_5X:
*translated_gain = SENSOR_A_GAIN_TRANSL_3_5X;
break;
default:
*translated_gain = 0;
break;
}
}
return apiRetStatus;
}
/*
Update the gain setting for the Python 480 sensor.
*/
CyU3PReturnStatus_t
SensorSetGain (
uint8_t new_translated_gain)
{
uint16_t new_sensor_gain;
CyU3PReturnStatus_t apiRetStatus;
switch (new_translated_gain) {
case SENSOR_A_GAIN_TRANSL_1X:
new_sensor_gain = SENSOR_ANALOG_GAIN_1X;
break;
case SENSOR_A_GAIN_TRANSL_3_5X:
new_sensor_gain = SENSOR_ANALOG_GAIN_3_5X;
break;
case SENSOR_A_GAIN_TRANSL_2X:
// Fall-through
default:
new_sensor_gain = SENSOR_ANALOG_GAIN_2X;
break;
}
apiRetStatus = I2CSensorWrite (SENSOR_ADDR_WR,
SENSOR_REG_ANALOG_GAIN,
new_sensor_gain);
return apiRetStatus;
}
CyU3PReturnStatus_t
SensorGetRoi (
uint8_t *translated_roi)
{
CyU3PReturnStatus_t apiRetStatus;
uint16_t roi = 0;
apiRetStatus = I2CSensorRead (SENSOR_ADDR_RD,
SENSOR_REG_ROI_SELECTION,
&roi);
if (apiRetStatus == CY_U3P_SUCCESS) {
switch (roi) {
case SENSOR_ROI0: // 0b0001
*translated_roi = 0;
break;
case SENSOR_ROI1: // 0b0010
*translated_roi = 1;
break;
default:
*translated_roi = 0;
break;
}
}
return apiRetStatus;
}
/*
Update the gain setting for the Python 480 sensor.
*/
CyU3PReturnStatus_t
SensorSetRoi (
uint8_t new_translated_roi)
{
uint16_t new_roi;
CyU3PReturnStatus_t apiRetStatus;
switch (new_translated_roi) {
case 0:
new_roi = SENSOR_ROI0;
break;
case 1:
new_roi = SENSOR_ROI1;
break;
default:
new_roi = SENSOR_ROI0;
break;
}
/* Stop updating image sensor until all values are written */
apiRetStatus = I2CSensorWrite (SENSOR_ADDR_WR,
SENSOR_REG_SYNC_CONFIG,
SENSOR_SYNC_CONFIG_ROI_NOSYNC);
apiRetStatus = I2CSensorWrite (SENSOR_ADDR_WR,
SENSOR_REG_ROI_SELECTION,
new_roi);
I2CSensorConditionalWrite (&apiRetStatus,
SENSOR_ADDR_WR,
SENSOR_REG_SYNC_CONFIG,
SENSOR_SYNC_CONFIG_ROI_SYNC);
return apiRetStatus;
}