-
Notifications
You must be signed in to change notification settings - Fork 3
/
camera-configuration.c
224 lines (203 loc) · 5.16 KB
/
camera-configuration.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
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <errno.h>
#include <unistd.h>
#include <getopt.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
//#include <linux/i2c.h>
#include <linux/i2c-dev.h>
#include <sys/ioctl.h>
#include <stdint.h>
#define I2C_CHANNEL "/dev/i2c-12"
#define LEGACY_I2C_CHANNEL "/dev/i2c-4"
#define OV7670_ADDR 0x21
/*######################
## OV7670 registers ##
######################
*/
#define REG_CLKRC 0x11 // Clocl control
#define REG_COM7 0x12 // Control 7
#define REG_COM10 0x15 // Control 10
#define COM7_FMT_VGA 0x00
#define COM7_YUV 0x00 // YUV
#define COM7_RGB 0x04 // bits 0 and 2 - RGB format
#define REG_TSLB 0x3a // lots of stuff
#define REG_COM11 0x3b // Control 11
#define REG_COM15 0x40 // Control 15
#define COM15_R00FF 0xc0 // 00 to FF
#define COM15_RGB565 0x10 // RGB565 output
#define REG_HSTART 0x17 // Horiz start high bits
#define REG_HSTOP 0x18 // Horiz stop high bits
#define REG_VSTART 0x19 // Vert start high bits
#define REG_VSTOP 0x1a // Vert stop high bits
#define REG_VREF 0x03 // Pieces of GAIN, VSTART, VSTOP
#define REG_HREF 0x32 // HREF pieces
#define REG_RGB444 0x8c // RGB 444 control
#define REG_COM9 0x14 // Control 9 - gain ceiling
#define REG_COM13 0x3d // Control 13
#define COM13_GAMMA 0x80 // Gamma enable
#define COM13_UVSAT 0x40 // UV saturation auto adjustment
int file = -1;
uint8_t data[2];
uint32_t sensor_id = 0;
int write_i2c(uint8_t reg, uint8_t val) {
uint8_t buf[2] = {reg, val};
int trials = 0;
while(trials < 5) {
if (write(file, &buf, 2) != 2) {
//printf("Failed writing register 0x%02x!\n", reg);
trials++;
usleep(200000);
} else {
break;
}
}
if(trials == 5) {
return -1;
}
return 0;
}
int read_i2c(uint8_t reg, uint8_t *val) {
int trials = 0;
while(trials < 5) {
if (write(file, ®, 1) != 1) {
//printf("Failed writing register 0x%02x!\n", reg);
trials++;
usleep(200000);
} else {
break;
}
}
if(trials == 5) {
return -1;
}
trials = 0;
while(trials < 5) {
if (read(file, val, 1) != 1) {
//printf("Failed reading 0x%02x!\n", *val);
trials++;
usleep(200000);
} else {
break;
}
}
if(trials == 5) {
return -1;
}
return 0;
}
int ov7670_init(void) {
if(write_i2c(REG_COM7, 0x80) < 0) { // Reset to default values.
return -1;
}
usleep(200000);
if(write_i2c(REG_CLKRC, 0x80) < 0) { // No internal clock prescaler.
return -1;
}
if(write_i2c(REG_TSLB, 0x04) < 0) { // 0x04 = 0x0c (default) but with YUYV
return -1;
}
if(write_i2c(REG_COM7, COM7_YUV|COM7_FMT_VGA) < 0) { // Output format: YUV, VGA.
return -1;
}
if(write_i2c(REG_COM15, COM15_R00FF) < 0) {
return -1;
}
if(write_i2c(REG_COM13, 0x00) < 0) { // YUYV
return -1;
}
if(write_i2c(0xb0, 0x84) < 0) { // Color mode?? (Not documented!)
return -1;
}
if(write_i2c(REG_HSTART, 0x13) < 0) { // start = HSTART<<3 + HREF[2:0] = 19*8 + 6 = 158
return -1;
}
if(write_i2c(REG_HSTOP, 0x01) < 0) { // stop = HSTOP<<3 + HREF[5:3] = 1*8 + 6 = 14 (158+640-784)
return -1;
}
if(write_i2c(REG_HREF, 0x36) < 0) { // With flag "edge offset" set, then the image is strange (too much clear, not sharp); so clear this bit.
return -1;
}
if(write_i2c(REG_VSTART, 0x02) < 0) { // start = VSTART<<2 + VREF[1:0] = 2*4 + 2 = 10
return -1;
}
if(write_i2c(REG_VSTOP, 0x7a) < 0) { // stop = VSTOP<<2 + VREF[3:2] = 122*4 + 2 = 490
return -1;
}
if(write_i2c(REG_VREF, 0x0a) < 0) {
return -1;
}
// Output array is 784x510 => 21'000'000 / (784x510x2) = about 26 fps
// To lower the framerate to 15 fps we insert dummy pixels and dummy rows: 21'000'000 / [(784+91)x(510+290)x2] = 15 fps
if(write_i2c(0x2a, 0x00) < 0) { // Dummy pixels MSB
return -1;
}
if(write_i2c(0x2b, 0x5B) < 0) { // Dummy pixels LSB
return -1;
}
if(write_i2c(0x92, 0x22) < 0) { // Dummy rows LSB
return -1;
}
if(write_i2c(0x93, 0x01) < 0) { // Dummy rows LSB
return -1;
}
if(write_i2c(0x0c, 0x00) < 0) {
return -1;
}
if(write_i2c(0x3e, 0x00) < 0) {
return -1;
}
if(write_i2c(0x70, 0x3a) < 0) {
return -1;
}
if(write_i2c(0x71, 0x35) < 0) {
return -1;
}
if(write_i2c(0x72, 0x11) < 0) {
return -1;
}
if(write_i2c(0x73, 0xf0) < 0) {
return -1;
}
if(write_i2c(0xa2, 0x02) < 0) {
return -1;
}
return 0;
}
int main(int argc, char *argv[]) {
file = open(I2C_CHANNEL, O_RDWR);
if(file < 0) { // Try with bus number used in older kernel
file = open(LEGACY_I2C_CHANNEL, O_RDWR);
if(file < 0) {
perror("Cannot open I2C camera device");
exit(1);
}
}
/* open device to communicate with */
//if (ioctl(file, I2C_SLAVE_FORCE, OV7670_ADDR) < 0) {
if (ioctl(file, I2C_SLAVE, OV7670_ADDR) < 0) {
/* ERROR HANDLING; you can check errno to see what went wrong */
printf("Error setting slave device\n");
close(file);
exit(1);
}
if(read_i2c(0x0a, &data[0]) < 0) {
return -1;
}
if(read_i2c(0x0b, &data[1]) < 0) {
return -1;
}
sensor_id = (data[0] << 8) + data[1];
//printf("Read data: 0x%.4x\r\n", sensor_id);
if (sensor_id == 0x7673) {
printf("Detected camera sensor: OV7670\r\n");
return ov7670_init();
} else {
return -1;
}
return 0;
}